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 2007 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 int ip_modclose_ackwait_ms = 3000; 255 256 /* 257 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 258 */ 259 260 struct listptr_s { 261 mblk_t *lp_head; /* pointer to the head of the list */ 262 mblk_t *lp_tail; /* pointer to the tail of the list */ 263 }; 264 265 typedef struct listptr_s listptr_t; 266 267 /* 268 * This is used by ip_snmp_get_mib2_ip_route_media and 269 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 270 */ 271 typedef struct iproutedata_s { 272 uint_t ird_idx; 273 listptr_t ird_route; /* ipRouteEntryTable */ 274 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 275 listptr_t ird_attrs; /* ipRouteAttributeTable */ 276 } iproutedata_t; 277 278 /* 279 * Cluster specific hooks. These should be NULL when booted as a non-cluster 280 */ 281 282 /* 283 * Hook functions to enable cluster networking 284 * On non-clustered systems these vectors must always be NULL. 285 * 286 * Hook function to Check ip specified ip address is a shared ip address 287 * in the cluster 288 * 289 */ 290 int (*cl_inet_isclusterwide)(uint8_t protocol, 291 sa_family_t addr_family, uint8_t *laddrp) = NULL; 292 293 /* 294 * Hook function to generate cluster wide ip fragment identifier 295 */ 296 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 297 uint8_t *laddrp, uint8_t *faddrp) = NULL; 298 299 /* 300 * Synchronization notes: 301 * 302 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 303 * MT level protection given by STREAMS. IP uses a combination of its own 304 * internal serialization mechanism and standard Solaris locking techniques. 305 * The internal serialization is per phyint (no IPMP) or per IPMP group. 306 * This is used to serialize plumbing operations, IPMP operations, certain 307 * multicast operations, most set ioctls, igmp/mld timers etc. 308 * 309 * Plumbing is a long sequence of operations involving message 310 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 311 * involved in plumbing operations. A natural model is to serialize these 312 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 313 * parallel without any interference. But various set ioctls on hme0 are best 314 * serialized. However if the system uses IPMP, the operations are easier if 315 * they are serialized on a per IPMP group basis since IPMP operations 316 * happen across ill's of a group. Thus the lowest common denominator is to 317 * serialize most set ioctls, multicast join/leave operations, IPMP operations 318 * igmp/mld timer operations, and processing of DLPI control messages received 319 * from drivers on a per IPMP group basis. If the system does not employ 320 * IPMP the serialization is on a per phyint basis. This serialization is 321 * provided by the ipsq_t and primitives operating on this. Details can 322 * be found in ip_if.c above the core primitives operating on ipsq_t. 323 * 324 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 325 * Simiarly lookup of an ire by a thread also returns a refheld ire. 326 * In addition ipif's and ill's referenced by the ire are also indirectly 327 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 328 * the ipif's address or netmask change as long as an ipif is refheld 329 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the 330 * address of an ipif has to go through the ipsq_t. This ensures that only 331 * 1 such exclusive operation proceeds at any time on the ipif. It then 332 * deletes all ires associated with this ipif, and waits for all refcnts 333 * associated with this ipif to come down to zero. The address is changed 334 * only after the ipif has been quiesced. Then the ipif is brought up again. 335 * More details are described above the comment in ip_sioctl_flags. 336 * 337 * Packet processing is based mostly on IREs and are fully multi-threaded 338 * using standard Solaris MT techniques. 339 * 340 * There are explicit locks in IP to handle: 341 * - The ip_g_head list maintained by mi_open_link() and friends. 342 * 343 * - The reassembly data structures (one lock per hash bucket) 344 * 345 * - conn_lock is meant to protect conn_t fields. The fields actually 346 * protected by conn_lock are documented in the conn_t definition. 347 * 348 * - ire_lock to protect some of the fields of the ire, IRE tables 349 * (one lock per hash bucket). Refer to ip_ire.c for details. 350 * 351 * - ndp_g_lock and nce_lock for protecting NCEs. 352 * 353 * - ill_lock protects fields of the ill and ipif. Details in ip.h 354 * 355 * - ill_g_lock: This is a global reader/writer lock. Protects the following 356 * * The AVL tree based global multi list of all ills. 357 * * The linked list of all ipifs of an ill 358 * * The <ill-ipsq> mapping 359 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next 360 * * The illgroup list threaded by ill_group_next. 361 * * <ill-phyint> association 362 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 363 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion 364 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill 365 * will all have to hold the ill_g_lock as writer for the actual duration 366 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping 367 * may be found in the IPMP section. 368 * 369 * - ill_lock: This is a per ill mutex. 370 * It protects some members of the ill and is documented below. 371 * It also protects the <ill-ipsq> mapping 372 * It also protects the illgroup list threaded by ill_group_next. 373 * It also protects the <ill-phyint> assoc. 374 * It also protects the list of ipifs hanging off the ill. 375 * 376 * - ipsq_lock: This is a per ipsq_t mutex lock. 377 * This protects all the other members of the ipsq struct except 378 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock 379 * 380 * - illgrp_lock: This is a per ill_group mutex lock. 381 * The only thing it protects is the illgrp_ill_schednext member of ill_group 382 * which dictates which is the next ill in an ill_group that is to be chosen 383 * for sending outgoing packets, through creation of an IRE_CACHE that 384 * references this ill. 385 * 386 * - phyint_lock: This is a per phyint mutex lock. Protects just the 387 * phyint_flags 388 * 389 * - ip_g_nd_lock: This is a global reader/writer lock. 390 * Any call to nd_load to load a new parameter to the ND table must hold the 391 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 392 * as reader. 393 * 394 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 395 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 396 * uniqueness check also done atomically. 397 * 398 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 399 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 400 * as a writer when adding or deleting elements from these lists, and 401 * as a reader when walking these lists to send a SADB update to the 402 * IPsec capable ills. 403 * 404 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 405 * group list linked by ill_usesrc_grp_next. It also protects the 406 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 407 * group is being added or deleted. This lock is taken as a reader when 408 * walking the list/group(eg: to get the number of members in a usesrc group). 409 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 410 * field is changing state i.e from NULL to non-NULL or vice-versa. For 411 * example, it is not necessary to take this lock in the initial portion 412 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and 413 * ip_sioctl_flags since the these operations are executed exclusively and 414 * that ensures that the "usesrc group state" cannot change. The "usesrc 415 * group state" change can happen only in the latter part of 416 * ip_sioctl_slifusesrc and in ill_delete. 417 * 418 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications. 419 * 420 * To change the <ill-phyint> association, the ill_g_lock must be held 421 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 422 * must be held. 423 * 424 * To change the <ill-ipsq> association the ill_g_lock must be held as writer 425 * and the ill_lock of the ill in question must be held. 426 * 427 * To change the <ill-illgroup> association the ill_g_lock must be held as 428 * writer and the ill_lock of the ill in question must be held. 429 * 430 * To add or delete an ipif from the list of ipifs hanging off the ill, 431 * ill_g_lock (writer) and ill_lock must be held and the thread must be 432 * a writer on the associated ipsq,. 433 * 434 * To add or delete an ill to the system, the ill_g_lock must be held as 435 * writer and the thread must be a writer on the associated ipsq. 436 * 437 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 438 * must be a writer on the associated ipsq. 439 * 440 * Lock hierarchy 441 * 442 * Some lock hierarchy scenarios are listed below. 443 * 444 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 445 * ill_g_lock -> illgrp_lock -> ill_lock 446 * ill_g_lock -> ill_lock(s) -> phyint_lock 447 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 448 * ill_g_lock -> ip_addr_avail_lock 449 * conn_lock -> irb_lock -> ill_lock -> ire_lock 450 * ill_g_lock -> ip_g_nd_lock 451 * 452 * When more than 1 ill lock is needed to be held, all ill lock addresses 453 * are sorted on address and locked starting from highest addressed lock 454 * downward. 455 * 456 * Mobile-IP scenarios 457 * 458 * irb_lock -> ill_lock -> ire_mrtun_lock 459 * irb_lock -> ill_lock -> ire_srcif_table_lock 460 * 461 * IPsec scenarios 462 * 463 * ipsa_lock -> ill_g_lock -> ill_lock 464 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 465 * ipsec_capab_ills_lock -> ipsa_lock 466 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 467 * 468 * Trusted Solaris scenarios 469 * 470 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 471 * igsa_lock -> gcdb_lock 472 * gcgrp_rwlock -> ire_lock 473 * gcgrp_rwlock -> gcdb_lock 474 * 475 * 476 * Routing/forwarding table locking notes: 477 * 478 * Lock acquisition order: Radix tree lock, irb_lock. 479 * Requirements: 480 * i. Walker must not hold any locks during the walker callback. 481 * ii Walker must not see a truncated tree during the walk because of any node 482 * deletion. 483 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 484 * in many places in the code to walk the irb list. Thus even if all the 485 * ires in a bucket have been deleted, we still can't free the radix node 486 * until the ires have actually been inactive'd (freed). 487 * 488 * Tree traversal - Need to hold the global tree lock in read mode. 489 * Before dropping the global tree lock, need to either increment the ire_refcnt 490 * to ensure that the radix node can't be deleted. 491 * 492 * Tree add - Need to hold the global tree lock in write mode to add a 493 * radix node. To prevent the node from being deleted, increment the 494 * irb_refcnt, after the node is added to the tree. The ire itself is 495 * added later while holding the irb_lock, but not the tree lock. 496 * 497 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 498 * All associated ires must be inactive (i.e. freed), and irb_refcnt 499 * must be zero. 500 * 501 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 502 * global tree lock (read mode) for traversal. 503 * 504 * IPSEC notes : 505 * 506 * IP interacts with the IPSEC code (AH/ESP) by tagging a M_CTL message 507 * in front of the actual packet. For outbound datagrams, the M_CTL 508 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 509 * information used by the IPSEC code for applying the right level of 510 * protection. The information initialized by IP in the ipsec_out_t 511 * is determined by the per-socket policy or global policy in the system. 512 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 513 * ipsec_info.h) which starts out with nothing in it. It gets filled 514 * with the right information if it goes through the AH/ESP code, which 515 * happens if the incoming packet is secure. The information initialized 516 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 517 * the policy requirements needed by per-socket policy or global policy 518 * is met or not. 519 * 520 * If there is both per-socket policy (set using setsockopt) and there 521 * is also global policy match for the 5 tuples of the socket, 522 * ipsec_override_policy() makes the decision of which one to use. 523 * 524 * For fully connected sockets i.e dst, src [addr, port] is known, 525 * conn_policy_cached is set indicating that policy has been cached. 526 * conn_in_enforce_policy may or may not be set depending on whether 527 * there is a global policy match or per-socket policy match. 528 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 529 * Once the right policy is set on the conn_t, policy cannot change for 530 * this socket. This makes life simpler for TCP (UDP ?) where 531 * re-transmissions go out with the same policy. For symmetry, policy 532 * is cached for fully connected UDP sockets also. Thus if policy is cached, 533 * it also implies that policy is latched i.e policy cannot change 534 * on these sockets. As we have the right policy on the conn, we don't 535 * have to lookup global policy for every outbound and inbound datagram 536 * and thus serving as an optimization. Note that a global policy change 537 * does not affect fully connected sockets if they have policy. If fully 538 * connected sockets did not have any policy associated with it, global 539 * policy change may affect them. 540 * 541 * IP Flow control notes: 542 * 543 * Non-TCP streams are flow controlled by IP. On the send side, if the packet 544 * cannot be sent down to the driver by IP, because of a canput failure, IP 545 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. 546 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained 547 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the 548 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the 549 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains 550 * the queued messages, and removes the conn from the drain list, if all 551 * messages were drained. It also qenables the next conn in the drain list to 552 * continue the drain process. 553 * 554 * In reality the drain list is not a single list, but a configurable number 555 * of lists. The ip_wsrv on the IP module, qenables the first conn in each 556 * list. If the ip_wsrv of the next qenabled conn does not run, because the 557 * stream closes, ip_close takes responsibility to qenable the next conn in 558 * the drain list. The directly called ip_wput path always does a putq, if 559 * it cannot putnext. Thus synchronization problems are handled between 560 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 561 * functions that manipulate this drain list. Furthermore conn_drain_insert 562 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv 563 * running on a queue at any time. conn_drain_tail can be simultaneously called 564 * from both ip_wsrv and ip_close. 565 * 566 * IPQOS notes: 567 * 568 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 569 * and IPQoS modules. IPPF includes hooks in IP at different control points 570 * (callout positions) which direct packets to IPQoS modules for policy 571 * processing. Policies, if present, are global. 572 * 573 * The callout positions are located in the following paths: 574 * o local_in (packets destined for this host) 575 * o local_out (packets orginating from this host ) 576 * o fwd_in (packets forwarded by this m/c - inbound) 577 * o fwd_out (packets forwarded by this m/c - outbound) 578 * Hooks at these callout points can be enabled/disabled using the ndd variable 579 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 580 * By default all the callout positions are enabled. 581 * 582 * Outbound (local_out) 583 * Hooks are placed in ip_wput_ire and ipsec_out_process. 584 * 585 * Inbound (local_in) 586 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 587 * TCP and UDP fanout routines. 588 * 589 * Forwarding (in and out) 590 * Hooks are placed in ip_rput_forward and ip_mrtun_forward. 591 * 592 * IP Policy Framework processing (IPPF processing) 593 * Policy processing for a packet is initiated by ip_process, which ascertains 594 * that the classifier (ipgpc) is loaded and configured, failing which the 595 * packet resumes normal processing in IP. If the clasifier is present, the 596 * packet is acted upon by one or more IPQoS modules (action instances), per 597 * filters configured in ipgpc and resumes normal IP processing thereafter. 598 * An action instance can drop a packet in course of its processing. 599 * 600 * A boolean variable, ip_policy, is used in all the fanout routines that can 601 * invoke ip_process for a packet. This variable indicates if the packet should 602 * to be sent for policy processing. The variable is set to B_TRUE by default, 603 * i.e. when the routines are invoked in the normal ip procesing path for a 604 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 605 * ip_policy is set to B_FALSE for all the routines called in these two 606 * functions because, in the former case, we don't process loopback traffic 607 * currently while in the latter, the packets have already been processed in 608 * icmp_inbound. 609 * 610 * Zones notes: 611 * 612 * The partitioning rules for networking are as follows: 613 * 1) Packets coming from a zone must have a source address belonging to that 614 * zone. 615 * 2) Packets coming from a zone can only be sent on a physical interface on 616 * which the zone has an IP address. 617 * 3) Between two zones on the same machine, packet delivery is only allowed if 618 * there's a matching route for the destination and zone in the forwarding 619 * table. 620 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 621 * different zones can bind to the same port with the wildcard address 622 * (INADDR_ANY). 623 * 624 * The granularity of interface partitioning is at the logical interface level. 625 * Therefore, every zone has its own IP addresses, and incoming packets can be 626 * attributed to a zone unambiguously. A logical interface is placed into a zone 627 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 628 * structure. Rule (1) is implemented by modifying the source address selection 629 * algorithm so that the list of eligible addresses is filtered based on the 630 * sending process zone. 631 * 632 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 633 * across all zones, depending on their type. Here is the break-up: 634 * 635 * IRE type Shared/exclusive 636 * -------- ---------------- 637 * IRE_BROADCAST Exclusive 638 * IRE_DEFAULT (default routes) Shared (*) 639 * IRE_LOCAL Exclusive (x) 640 * IRE_LOOPBACK Exclusive 641 * IRE_PREFIX (net routes) Shared (*) 642 * IRE_CACHE Exclusive 643 * IRE_IF_NORESOLVER (interface routes) Exclusive 644 * IRE_IF_RESOLVER (interface routes) Exclusive 645 * IRE_HOST (host routes) Shared (*) 646 * 647 * (*) A zone can only use a default or off-subnet route if the gateway is 648 * directly reachable from the zone, that is, if the gateway's address matches 649 * one of the zone's logical interfaces. 650 * 651 * (x) IRE_LOCAL are handled a bit differently, since for all other entries 652 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source 653 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP 654 * address of the zone itself (the destination). Since IRE_LOCAL is used 655 * for communication between zones, ip_wput_ire has special logic to set 656 * the right source address when sending using an IRE_LOCAL. 657 * 658 * Furthermore, when ip_restrict_interzone_loopback is set (the default), 659 * ire_cache_lookup restricts loopback using an IRE_LOCAL 660 * between zone to the case when L2 would have conceptually looped the packet 661 * back, i.e. the loopback which is required since neither Ethernet drivers 662 * nor Ethernet hardware loops them back. This is the case when the normal 663 * routes (ignoring IREs with different zoneids) would send out the packet on 664 * the same ill (or ill group) as the ill with which is IRE_LOCAL is 665 * associated. 666 * 667 * Multiple zones can share a common broadcast address; typically all zones 668 * share the 255.255.255.255 address. Incoming as well as locally originated 669 * broadcast packets must be dispatched to all the zones on the broadcast 670 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 671 * since some zones may not be on the 10.16.72/24 network. To handle this, each 672 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 673 * sent to every zone that has an IRE_BROADCAST entry for the destination 674 * address on the input ill, see conn_wantpacket(). 675 * 676 * Applications in different zones can join the same multicast group address. 677 * For IPv4, group memberships are per-logical interface, so they're already 678 * inherently part of a zone. For IPv6, group memberships are per-physical 679 * interface, so we distinguish IPv6 group memberships based on group address, 680 * interface and zoneid. In both cases, received multicast packets are sent to 681 * every zone for which a group membership entry exists. On IPv6 we need to 682 * check that the target zone still has an address on the receiving physical 683 * interface; it could have been removed since the application issued the 684 * IPV6_JOIN_GROUP. 685 */ 686 687 /* 688 * Squeue Fanout flags: 689 * 0: No fanout. 690 * 1: Fanout across all squeues 691 */ 692 boolean_t ip_squeue_fanout = 0; 693 694 /* 695 * Maximum dups allowed per packet. 696 */ 697 uint_t ip_max_frag_dups = 10; 698 699 #define IS_SIMPLE_IPH(ipha) \ 700 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 701 702 /* RFC1122 Conformance */ 703 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 704 705 #define ILL_MAX_NAMELEN LIFNAMSIZ 706 707 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 708 709 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t); 710 static void ip_ipsec_out_prepend(mblk_t *, mblk_t *, ill_t *); 711 712 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t); 713 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 714 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 715 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); 716 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, 717 mblk_t *, int); 718 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 719 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 720 ill_t *, zoneid_t); 721 static void icmp_options_update(ipha_t *); 722 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t); 723 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, 724 zoneid_t zoneid); 725 static mblk_t *icmp_pkt_err_ok(mblk_t *); 726 static void icmp_redirect(mblk_t *); 727 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t); 728 729 static void ip_arp_news(queue_t *, mblk_t *); 730 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *); 731 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 732 char *ip_dot_addr(ipaddr_t, char *); 733 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 734 int ip_close(queue_t *, int); 735 static char *ip_dot_saddr(uchar_t *, char *); 736 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 737 boolean_t, boolean_t, ill_t *, zoneid_t); 738 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 739 boolean_t, boolean_t, zoneid_t); 740 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 741 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 742 static void ip_lrput(queue_t *, mblk_t *); 743 ipaddr_t ip_massage_options(ipha_t *); 744 static void ip_mrtun_forward(ire_t *, ill_t *, mblk_t *); 745 ipaddr_t ip_net_mask(ipaddr_t); 746 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, ill_t *, conn_t *, 747 zoneid_t); 748 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 749 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *); 750 char *ip_nv_lookup(nv_t *, int); 751 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 752 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 753 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 754 static boolean_t ip_param_register(ipparam_t *, size_t, ipndp_t *, 755 size_t); 756 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 757 void ip_rput(queue_t *, mblk_t *); 758 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 759 void *dummy_arg); 760 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 761 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *); 762 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 763 ire_t *); 764 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *, 765 mblk_t *, ipha_t **, ipaddr_t *); 766 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *); 767 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, 768 uint16_t *); 769 int ip_snmp_get(queue_t *, mblk_t *); 770 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, 771 mib2_ipIfStatsEntry_t *); 772 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *); 773 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *); 774 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *); 775 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *); 776 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *); 777 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *); 778 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *); 779 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *); 780 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *); 781 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *); 782 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *); 783 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *); 784 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *); 785 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *); 786 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *); 787 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *); 788 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 789 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 790 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); 791 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 792 static boolean_t ip_source_routed(ipha_t *); 793 static boolean_t ip_source_route_included(ipha_t *); 794 795 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, 796 zoneid_t); 797 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int); 798 static void ip_wput_local_options(ipha_t *); 799 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 800 zoneid_t); 801 802 static void conn_drain_init(void); 803 static void conn_drain_fini(void); 804 static void conn_drain_tail(conn_t *connp, boolean_t closing); 805 806 static void conn_walk_drain(void); 807 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, 808 zoneid_t); 809 810 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 811 zoneid_t); 812 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 813 void *dummy_arg); 814 815 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 816 817 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 818 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 819 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 820 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 821 822 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 823 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 824 caddr_t, cred_t *); 825 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, 826 caddr_t cp, cred_t *cr); 827 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, 828 cred_t *); 829 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 830 caddr_t cp, cred_t *cr); 831 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 832 cred_t *); 833 static squeue_func_t ip_squeue_switch(int); 834 835 static void ip_kstat_init(void); 836 static void ip_kstat_fini(void); 837 static int ip_kstat_update(kstat_t *kp, int rw); 838 static void icmp_kstat_init(void); 839 static void icmp_kstat_fini(void); 840 static int icmp_kstat_update(kstat_t *kp, int rw); 841 842 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); 843 844 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 845 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 846 847 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, 848 ipha_t *, ill_t *, boolean_t); 849 850 timeout_id_t ip_ire_expire_id; /* IRE expiration timer. */ 851 static clock_t ip_ire_arp_time_elapsed; /* Time since IRE cache last flushed */ 852 static clock_t ip_ire_rd_time_elapsed; /* ... redirect IREs last flushed */ 853 static clock_t ip_ire_pmtu_time_elapsed; /* Time since path mtu increase */ 854 855 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 856 clock_t icmp_pkt_err_last = 0; /* Time since last icmp_pkt_err */ 857 uint_t icmp_pkt_err_sent = 0; /* Number of packets sent in burst */ 858 859 /* How long, in seconds, we allow frags to hang around. */ 860 #define IP_FRAG_TIMEOUT 60 861 862 time_t ip_g_frag_timeout = IP_FRAG_TIMEOUT; 863 clock_t ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 864 865 /* 866 * Threshold which determines whether MDT should be used when 867 * generating IP fragments; payload size must be greater than 868 * this threshold for MDT to take place. 869 */ 870 #define IP_WPUT_FRAG_MDT_MIN 32768 871 872 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 873 874 /* Protected by ip_mi_lock */ 875 static void *ip_g_head; /* Instance Data List Head */ 876 kmutex_t ip_mi_lock; /* Lock for list of instances */ 877 878 /* Only modified during _init and _fini thus no locking is needed. */ 879 caddr_t ip_g_nd; /* Named Dispatch List Head */ 880 881 882 static long ip_rput_pullups; 883 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 884 885 vmem_t *ip_minor_arena; 886 887 /* 888 * MIB-2 stuff for SNMP (both IP and ICMP) 889 */ 890 mib2_ipIfStatsEntry_t ip_mib; 891 mib2_icmp_t icmp_mib; 892 893 #ifdef DEBUG 894 uint32_t ipsechw_debug = 0; 895 #endif 896 897 kstat_t *ip_mibkp; /* kstat exporting ip_mib data */ 898 kstat_t *icmp_mibkp; /* kstat exporting icmp_mib data */ 899 900 uint_t loopback_packets = 0; 901 902 /* 903 * Multirouting/CGTP stuff 904 */ 905 cgtp_filter_ops_t *ip_cgtp_filter_ops; /* CGTP hooks */ 906 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 907 boolean_t ip_cgtp_filter; /* Enable/disable CGTP hooks */ 908 /* Interval (in ms) between consecutive 'bad MTU' warnings */ 909 hrtime_t ip_multirt_log_interval = 1000; 910 /* Time since last warning issued. */ 911 static hrtime_t multirt_bad_mtu_last_time = 0; 912 913 kmutex_t ip_trash_timer_lock; 914 krwlock_t ip_g_nd_lock; 915 916 /* 917 * XXX following really should only be in a header. Would need more 918 * header and .c clean up first. 919 */ 920 extern optdb_obj_t ip_opt_obj; 921 922 ulong_t ip_squeue_enter_unbound = 0; 923 924 /* 925 * Named Dispatch Parameter Table. 926 * All of these are alterable, within the min/max values given, at run time. 927 */ 928 static ipparam_t lcl_param_arr[] = { 929 /* min max value name */ 930 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 931 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 932 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 933 { 0, 1, 0, "ip_respond_to_timestamp"}, 934 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 935 { 0, 1, 1, "ip_send_redirects"}, 936 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 937 { 0, 10, 0, "ip_debug"}, 938 { 0, 10, 0, "ip_mrtdebug"}, 939 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 940 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 941 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 942 { 1, 255, 255, "ip_def_ttl" }, 943 { 0, 1, 0, "ip_forward_src_routed"}, 944 { 0, 256, 32, "ip_wroff_extra" }, 945 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 946 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 947 { 0, 1, 1, "ip_path_mtu_discovery" }, 948 { 0, 240, 30, "ip_ignore_delete_time" }, 949 { 0, 1, 0, "ip_ignore_redirect" }, 950 { 0, 1, 1, "ip_output_queue" }, 951 { 1, 254, 1, "ip_broadcast_ttl" }, 952 { 0, 99999, 100, "ip_icmp_err_interval" }, 953 { 1, 99999, 10, "ip_icmp_err_burst" }, 954 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 955 { 0, 1, 0, "ip_strict_dst_multihoming" }, 956 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 957 { 0, 1, 0, "ipsec_override_persocket_policy" }, 958 { 0, 1, 1, "icmp_accept_clear_messages" }, 959 { 0, 1, 1, "igmp_accept_clear_messages" }, 960 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 961 "ip_ndp_delay_first_probe_time"}, 962 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 963 "ip_ndp_max_unicast_solicit"}, 964 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 965 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 966 { 0, 1, 0, "ip6_forward_src_routed"}, 967 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 968 { 0, 1, 1, "ip6_send_redirects"}, 969 { 0, 1, 0, "ip6_ignore_redirect" }, 970 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 971 972 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 973 974 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 975 976 { 0, 1, 1, "pim_accept_clear_messages" }, 977 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 978 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 979 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 980 { 0, 15, 0, "ip_policy_mask" }, 981 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 982 { 0, 255, 1, "ip_multirt_ttl" }, 983 { 0, 1, 1, "ip_multidata_outbound" }, 984 { 0, 3600000, 300000, "ip_ndp_defense_interval" }, 985 { 0, 999999, 60*60*24, "ip_max_temp_idle" }, 986 { 0, 1000, 1, "ip_max_temp_defend" }, 987 { 0, 1000, 3, "ip_max_defend" }, 988 { 0, 999999, 30, "ip_defend_interval" }, 989 { 0, 3600000, 300000, "ip_dup_recovery" }, 990 { 0, 1, 1, "ip_restrict_interzone_loopback" }, 991 { 0, 1, 1, "ip_lso_outbound" }, 992 #ifdef DEBUG 993 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 994 #endif 995 }; 996 997 ipparam_t *ip_param_arr = lcl_param_arr; 998 999 /* Extended NDP table */ 1000 static ipndp_t lcl_ndp_arr[] = { 1001 /* getf setf data name */ 1002 { ip_param_generic_get, ip_forward_set, (caddr_t)&ip_g_forward, 1003 "ip_forwarding" }, 1004 { ip_param_generic_get, ip_forward_set, (caddr_t)&ipv6_forward, 1005 "ip6_forwarding" }, 1006 { ip_ill_report, NULL, NULL, 1007 "ip_ill_status" }, 1008 { ip_ipif_report, NULL, NULL, 1009 "ip_ipif_status" }, 1010 { ip_ire_report, NULL, NULL, 1011 "ipv4_ire_status" }, 1012 { ip_ire_report_mrtun, NULL, NULL, 1013 "ipv4_mrtun_ire_status" }, 1014 { ip_ire_report_srcif, NULL, NULL, 1015 "ipv4_srcif_ire_status" }, 1016 { ip_ire_report_v6, NULL, NULL, 1017 "ipv6_ire_status" }, 1018 { ip_conn_report, NULL, NULL, 1019 "ip_conn_status" }, 1020 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, 1021 "ip_rput_pullups" }, 1022 { ndp_report, NULL, NULL, 1023 "ip_ndp_cache_report" }, 1024 { ip_srcid_report, NULL, NULL, 1025 "ip_srcid_status" }, 1026 { ip_param_generic_get, ip_squeue_profile_set, 1027 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, 1028 { ip_param_generic_get, ip_squeue_bind_set, 1029 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, 1030 { ip_param_generic_get, ip_input_proc_set, 1031 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 1032 { ip_param_generic_get, ip_int_set, 1033 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 1034 { ip_cgtp_filter_get, ip_cgtp_filter_set, (caddr_t)&ip_cgtp_filter, 1035 "ip_cgtp_filter" }, 1036 { ip_param_generic_get, ip_int_set, 1037 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" } 1038 }; 1039 1040 /* 1041 * ip_g_forward controls IP forwarding. It takes two values: 1042 * 0: IP_FORWARD_NEVER Don't forward packets ever. 1043 * 1: IP_FORWARD_ALWAYS Forward packets for elsewhere. 1044 * 1045 * RFC1122 says there must be a configuration switch to control forwarding, 1046 * but that the default MUST be to not forward packets ever. Implicit 1047 * control based on configuration of multiple interfaces MUST NOT be 1048 * implemented (Section 3.1). SunOS 4.1 did provide the "automatic" capability 1049 * and, in fact, it was the default. That capability is now provided in the 1050 * /etc/rc2.d/S69inet script. 1051 */ 1052 int ip_g_forward = IP_FORWARD_DEFAULT; 1053 1054 /* It also has an IPv6 counterpart. */ 1055 1056 int ipv6_forward = IP_FORWARD_DEFAULT; 1057 1058 /* 1059 * Table of IP ioctls encoding the various properties of the ioctl and 1060 * indexed based on the last byte of the ioctl command. Occasionally there 1061 * is a clash, and there is more than 1 ioctl with the same last byte. 1062 * In such a case 1 ioctl is encoded in the ndx table and the remaining 1063 * ioctls are encoded in the misc table. An entry in the ndx table is 1064 * retrieved by indexing on the last byte of the ioctl command and comparing 1065 * the ioctl command with the value in the ndx table. In the event of a 1066 * mismatch the misc table is then searched sequentially for the desired 1067 * ioctl command. 1068 * 1069 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 1070 */ 1071 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 1072 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1073 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1074 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1075 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1076 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1077 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1078 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1079 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1080 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1081 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1082 1083 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 1084 MISC_CMD, ip_siocaddrt, NULL }, 1085 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 1086 MISC_CMD, ip_siocdelrt, NULL }, 1087 1088 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1089 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1090 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1091 IF_CMD, ip_sioctl_get_addr, NULL }, 1092 1093 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1094 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1095 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 1096 IPI_GET_CMD | IPI_REPL, 1097 IF_CMD, ip_sioctl_get_dstaddr, NULL }, 1098 1099 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 1100 IPI_PRIV | IPI_WR | IPI_REPL, 1101 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1102 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 1103 IPI_MODOK | IPI_GET_CMD | IPI_REPL, 1104 IF_CMD, ip_sioctl_get_flags, NULL }, 1105 1106 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1107 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1108 1109 /* copyin size cannot be coded for SIOCGIFCONF */ 1110 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1111 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1112 1113 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1114 IF_CMD, ip_sioctl_mtu, NULL }, 1115 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1116 IF_CMD, ip_sioctl_get_mtu, NULL }, 1117 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 1118 IPI_GET_CMD | IPI_REPL, 1119 IF_CMD, ip_sioctl_get_brdaddr, NULL }, 1120 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1121 IF_CMD, ip_sioctl_brdaddr, NULL }, 1122 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 1123 IPI_GET_CMD | IPI_REPL, 1124 IF_CMD, ip_sioctl_get_netmask, NULL }, 1125 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1126 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1127 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 1128 IPI_GET_CMD | IPI_REPL, 1129 IF_CMD, ip_sioctl_get_metric, NULL }, 1130 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 1131 IF_CMD, ip_sioctl_metric, NULL }, 1132 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1133 1134 /* See 166-168 below for extended SIOC*XARP ioctls */ 1135 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV, 1136 MISC_CMD, ip_sioctl_arp, NULL }, 1137 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, 1138 MISC_CMD, ip_sioctl_arp, NULL }, 1139 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, 1140 MISC_CMD, ip_sioctl_arp, NULL }, 1141 1142 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1143 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1144 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1145 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1146 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1147 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1148 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1149 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1150 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1151 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1152 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1153 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1154 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1155 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1156 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1157 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1158 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1159 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1160 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1161 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1162 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1163 1164 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 1165 MISC_CMD, if_unitsel, if_unitsel_restart }, 1166 1167 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1168 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1169 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1170 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1171 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1172 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1173 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1174 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1175 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1176 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1177 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1178 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1179 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1180 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1181 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1182 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1183 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1184 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1185 1186 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 1187 IPI_PRIV | IPI_WR | IPI_MODOK, 1188 IF_CMD, ip_sioctl_sifname, NULL }, 1189 1190 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1191 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1192 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1193 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1194 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1195 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1196 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1197 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1198 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1199 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1200 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1201 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1202 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1203 1204 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL, 1205 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 1206 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1207 IF_CMD, ip_sioctl_get_muxid, NULL }, 1208 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 1209 IPI_PRIV | IPI_WR | IPI_REPL, 1210 IF_CMD, ip_sioctl_muxid, NULL }, 1211 1212 /* Both if and lif variants share same func */ 1213 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1214 IF_CMD, ip_sioctl_get_lifindex, NULL }, 1215 /* Both if and lif variants share same func */ 1216 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 1217 IPI_PRIV | IPI_WR | IPI_REPL, 1218 IF_CMD, ip_sioctl_slifindex, NULL }, 1219 1220 /* copyin size cannot be coded for SIOCGIFCONF */ 1221 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1222 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1223 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1224 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1225 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1226 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1227 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1228 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1229 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1230 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1231 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1232 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1233 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1234 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1235 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1236 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1237 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1238 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1239 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1240 1241 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 1242 IPI_PRIV | IPI_WR | IPI_REPL, 1243 LIF_CMD, ip_sioctl_removeif, 1244 ip_sioctl_removeif_restart }, 1245 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 1246 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL, 1247 LIF_CMD, ip_sioctl_addif, NULL }, 1248 #define SIOCLIFADDR_NDX 112 1249 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1250 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1251 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 1252 IPI_GET_CMD | IPI_REPL, 1253 LIF_CMD, ip_sioctl_get_addr, NULL }, 1254 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1255 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1256 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 1257 IPI_GET_CMD | IPI_REPL, 1258 LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 1259 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 1260 IPI_PRIV | IPI_WR | IPI_REPL, 1261 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1262 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 1263 IPI_GET_CMD | IPI_MODOK | IPI_REPL, 1264 LIF_CMD, ip_sioctl_get_flags, NULL }, 1265 1266 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1267 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1268 1269 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1270 ip_sioctl_get_lifconf, NULL }, 1271 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1272 LIF_CMD, ip_sioctl_mtu, NULL }, 1273 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, 1274 LIF_CMD, ip_sioctl_get_mtu, NULL }, 1275 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 1276 IPI_GET_CMD | IPI_REPL, 1277 LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 1278 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1279 LIF_CMD, ip_sioctl_brdaddr, NULL }, 1280 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 1281 IPI_GET_CMD | IPI_REPL, 1282 LIF_CMD, ip_sioctl_get_netmask, NULL }, 1283 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1284 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1285 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 1286 IPI_GET_CMD | IPI_REPL, 1287 LIF_CMD, ip_sioctl_get_metric, NULL }, 1288 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1289 LIF_CMD, ip_sioctl_metric, NULL }, 1290 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1291 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL, 1292 LIF_CMD, ip_sioctl_slifname, 1293 ip_sioctl_slifname_restart }, 1294 1295 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL, 1296 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1297 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1298 IPI_GET_CMD | IPI_REPL, 1299 LIF_CMD, ip_sioctl_get_muxid, NULL }, 1300 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1301 IPI_PRIV | IPI_WR | IPI_REPL, 1302 LIF_CMD, ip_sioctl_muxid, NULL }, 1303 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1304 IPI_GET_CMD | IPI_REPL, 1305 LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1306 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1307 IPI_PRIV | IPI_WR | IPI_REPL, 1308 LIF_CMD, ip_sioctl_slifindex, 0 }, 1309 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1310 LIF_CMD, ip_sioctl_token, NULL }, 1311 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1312 IPI_GET_CMD | IPI_REPL, 1313 LIF_CMD, ip_sioctl_get_token, NULL }, 1314 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1315 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1316 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1317 IPI_GET_CMD | IPI_REPL, 1318 LIF_CMD, ip_sioctl_get_subnet, NULL }, 1319 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1320 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1321 1322 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1323 IPI_GET_CMD | IPI_REPL, 1324 LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1325 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1326 LIF_CMD, ip_siocdelndp_v6, NULL }, 1327 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1328 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1329 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1330 LIF_CMD, ip_siocsetndp_v6, NULL }, 1331 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1332 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1333 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1334 MISC_CMD, ip_sioctl_tonlink, NULL }, 1335 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1336 MISC_CMD, ip_sioctl_tmysite, NULL }, 1337 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL, 1338 TUN_CMD, ip_sioctl_tunparam, NULL }, 1339 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), 1340 IPI_PRIV | IPI_WR, 1341 TUN_CMD, ip_sioctl_tunparam, NULL }, 1342 1343 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1344 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1345 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1346 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1347 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1348 1349 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq), 1350 IPI_PRIV | IPI_WR | IPI_REPL, 1351 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1352 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq), 1353 IPI_PRIV | IPI_WR | IPI_REPL, 1354 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1355 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1356 IPI_PRIV | IPI_WR, 1357 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1358 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1359 IPI_GET_CMD | IPI_REPL, 1360 LIF_CMD, ip_sioctl_get_groupname, NULL }, 1361 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq), 1362 IPI_GET_CMD | IPI_REPL, 1363 LIF_CMD, ip_sioctl_get_oindex, NULL }, 1364 1365 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1366 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1367 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1368 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1369 1370 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1371 LIF_CMD, ip_sioctl_slifoindex, NULL }, 1372 1373 /* These are handled in ip_sioctl_copyin_setup itself */ 1374 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1375 MISC_CMD, NULL, NULL }, 1376 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1377 MISC_CMD, NULL, NULL }, 1378 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1379 1380 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1381 ip_sioctl_get_lifconf, NULL }, 1382 1383 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, 1384 MISC_CMD, ip_sioctl_xarp, NULL }, 1385 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, 1386 MISC_CMD, ip_sioctl_xarp, NULL }, 1387 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, 1388 MISC_CMD, ip_sioctl_xarp, NULL }, 1389 1390 /* SIOCPOPSOCKFS is not handled by IP */ 1391 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1392 1393 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1394 IPI_GET_CMD | IPI_REPL, 1395 LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1396 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1397 IPI_PRIV | IPI_WR | IPI_REPL, 1398 LIF_CMD, ip_sioctl_slifzone, 1399 ip_sioctl_slifzone_restart }, 1400 /* 172-174 are SCTP ioctls and not handled by IP */ 1401 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1402 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1403 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1404 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1405 IPI_GET_CMD, LIF_CMD, 1406 ip_sioctl_get_lifusesrc, 0 }, 1407 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1408 IPI_PRIV | IPI_WR, 1409 LIF_CMD, ip_sioctl_slifusesrc, 1410 NULL }, 1411 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1412 ip_sioctl_get_lifsrcof, NULL }, 1413 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1414 MISC_CMD, ip_sioctl_msfilter, NULL }, 1415 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1416 MISC_CMD, ip_sioctl_msfilter, NULL }, 1417 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1418 MISC_CMD, ip_sioctl_msfilter, NULL }, 1419 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1420 MISC_CMD, ip_sioctl_msfilter, NULL }, 1421 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD, 1422 ip_sioctl_set_ipmpfailback, NULL } 1423 }; 1424 1425 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1426 1427 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1428 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), 1429 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, 1430 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, 1431 TUN_CMD, ip_sioctl_tunparam, NULL }, 1432 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1433 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1434 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1435 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1436 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, 1437 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1438 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1439 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD, 1440 MISC_CMD, mrt_ioctl}, 1441 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD, 1442 MISC_CMD, mrt_ioctl}, 1443 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD, 1444 MISC_CMD, mrt_ioctl} 1445 }; 1446 1447 int ip_misc_ioctl_count = 1448 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1449 1450 static idl_t *conn_drain_list; /* The array of conn drain lists */ 1451 static uint_t conn_drain_list_cnt; /* Total count of conn_drain_list */ 1452 static int conn_drain_list_index; /* Next drain_list to be used */ 1453 int conn_drain_nthreads; /* Number of drainers reqd. */ 1454 /* Settable in /etc/system */ 1455 uint_t ip_redirect_cnt; /* Num of redirect routes in ftable */ 1456 1457 /* Defined in ip_ire.c */ 1458 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1459 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1460 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1461 1462 static nv_t ire_nv_arr[] = { 1463 { IRE_BROADCAST, "BROADCAST" }, 1464 { IRE_LOCAL, "LOCAL" }, 1465 { IRE_LOOPBACK, "LOOPBACK" }, 1466 { IRE_CACHE, "CACHE" }, 1467 { IRE_DEFAULT, "DEFAULT" }, 1468 { IRE_PREFIX, "PREFIX" }, 1469 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1470 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1471 { IRE_HOST, "HOST" }, 1472 { 0 } 1473 }; 1474 1475 nv_t *ire_nv_tbl = ire_nv_arr; 1476 1477 /* Defined in ip_if.c, protect the list of IPsec capable ills */ 1478 extern krwlock_t ipsec_capab_ills_lock; 1479 1480 /* Defined in ip_netinfo.c */ 1481 extern ddi_taskq_t *eventq_queue_nic; 1482 1483 /* Packet dropper for IP IPsec processing failures */ 1484 ipdropper_t ip_dropper; 1485 1486 /* Simple ICMP IP Header Template */ 1487 static ipha_t icmp_ipha = { 1488 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1489 }; 1490 1491 struct module_info ip_mod_info = { 1492 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024 1493 }; 1494 1495 /* 1496 * Duplicate static symbols within a module confuses mdb; so we avoid the 1497 * problem by making the symbols here distinct from those in udp.c. 1498 */ 1499 1500 static struct qinit iprinit = { 1501 (pfi_t)ip_rput, NULL, ip_open, ip_close, NULL, 1502 &ip_mod_info 1503 }; 1504 1505 static struct qinit ipwinit = { 1506 (pfi_t)ip_wput, (pfi_t)ip_wsrv, ip_open, ip_close, NULL, 1507 &ip_mod_info 1508 }; 1509 1510 static struct qinit iplrinit = { 1511 (pfi_t)ip_lrput, NULL, ip_open, ip_close, NULL, 1512 &ip_mod_info 1513 }; 1514 1515 static struct qinit iplwinit = { 1516 (pfi_t)ip_lwput, NULL, ip_open, ip_close, NULL, 1517 &ip_mod_info 1518 }; 1519 1520 struct streamtab ipinfo = { 1521 &iprinit, &ipwinit, &iplrinit, &iplwinit 1522 }; 1523 1524 #ifdef DEBUG 1525 static boolean_t skip_sctp_cksum = B_FALSE; 1526 #endif 1527 1528 /* 1529 * Prepend the zoneid using an ipsec_out_t for later use by functions like 1530 * ip_rput_v6(), ip_output(), etc. If the message 1531 * block already has a M_CTL at the front of it, then simply set the zoneid 1532 * appropriately. 1533 */ 1534 mblk_t * 1535 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid) 1536 { 1537 mblk_t *first_mp; 1538 ipsec_out_t *io; 1539 1540 ASSERT(zoneid != ALL_ZONES); 1541 if (mp->b_datap->db_type == M_CTL) { 1542 io = (ipsec_out_t *)mp->b_rptr; 1543 ASSERT(io->ipsec_out_type == IPSEC_OUT); 1544 io->ipsec_out_zoneid = zoneid; 1545 return (mp); 1546 } 1547 1548 first_mp = ipsec_alloc_ipsec_out(); 1549 if (first_mp == NULL) 1550 return (NULL); 1551 io = (ipsec_out_t *)first_mp->b_rptr; 1552 /* This is not a secure packet */ 1553 io->ipsec_out_secure = B_FALSE; 1554 io->ipsec_out_zoneid = zoneid; 1555 first_mp->b_cont = mp; 1556 return (first_mp); 1557 } 1558 1559 /* 1560 * Copy an M_CTL-tagged message, preserving reference counts appropriately. 1561 */ 1562 mblk_t * 1563 ip_copymsg(mblk_t *mp) 1564 { 1565 mblk_t *nmp; 1566 ipsec_info_t *in; 1567 1568 if (mp->b_datap->db_type != M_CTL) 1569 return (copymsg(mp)); 1570 1571 in = (ipsec_info_t *)mp->b_rptr; 1572 1573 /* 1574 * Note that M_CTL is also used for delivering ICMP error messages 1575 * upstream to transport layers. 1576 */ 1577 if (in->ipsec_info_type != IPSEC_OUT && 1578 in->ipsec_info_type != IPSEC_IN) 1579 return (copymsg(mp)); 1580 1581 nmp = copymsg(mp->b_cont); 1582 1583 if (in->ipsec_info_type == IPSEC_OUT) 1584 return (ipsec_out_tag(mp, nmp)); 1585 else 1586 return (ipsec_in_tag(mp, nmp)); 1587 } 1588 1589 /* Generate an ICMP fragmentation needed message. */ 1590 static void 1591 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid) 1592 { 1593 icmph_t icmph; 1594 mblk_t *first_mp; 1595 boolean_t mctl_present; 1596 1597 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1598 1599 if (!(mp = icmp_pkt_err_ok(mp))) { 1600 if (mctl_present) 1601 freeb(first_mp); 1602 return; 1603 } 1604 1605 bzero(&icmph, sizeof (icmph_t)); 1606 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1607 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1608 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1609 BUMP_MIB(&icmp_mib, icmpOutFragNeeded); 1610 BUMP_MIB(&icmp_mib, icmpOutDestUnreachs); 1611 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid); 1612 } 1613 1614 /* 1615 * icmp_inbound deals with ICMP messages in the following ways. 1616 * 1617 * 1) It needs to send a reply back and possibly delivering it 1618 * to the "interested" upper clients. 1619 * 2) It needs to send it to the upper clients only. 1620 * 3) It needs to change some values in IP only. 1621 * 4) It needs to change some values in IP and upper layers e.g TCP. 1622 * 1623 * We need to accomodate icmp messages coming in clear until we get 1624 * everything secure from the wire. If icmp_accept_clear_messages 1625 * is zero we check with the global policy and act accordingly. If 1626 * it is non-zero, we accept the message without any checks. But 1627 * *this does not mean* that this will be delivered to the upper 1628 * clients. By accepting we might send replies back, change our MTU 1629 * value etc. but delivery to the ULP/clients depends on their policy 1630 * dispositions. 1631 * 1632 * We handle the above 4 cases in the context of IPSEC in the 1633 * following way : 1634 * 1635 * 1) Send the reply back in the same way as the request came in. 1636 * If it came in encrypted, it goes out encrypted. If it came in 1637 * clear, it goes out in clear. Thus, this will prevent chosen 1638 * plain text attack. 1639 * 2) The client may or may not expect things to come in secure. 1640 * If it comes in secure, the policy constraints are checked 1641 * before delivering it to the upper layers. If it comes in 1642 * clear, ipsec_inbound_accept_clear will decide whether to 1643 * accept this in clear or not. In both the cases, if the returned 1644 * message (IP header + 8 bytes) that caused the icmp message has 1645 * AH/ESP headers, it is sent up to AH/ESP for validation before 1646 * sending up. If there are only 8 bytes of returned message, then 1647 * upper client will not be notified. 1648 * 3) Check with global policy to see whether it matches the constaints. 1649 * But this will be done only if icmp_accept_messages_in_clear is 1650 * zero. 1651 * 4) If we need to change both in IP and ULP, then the decision taken 1652 * while affecting the values in IP and while delivering up to TCP 1653 * should be the same. 1654 * 1655 * There are two cases. 1656 * 1657 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1658 * failed), we will not deliver it to the ULP, even though they 1659 * are *willing* to accept in *clear*. This is fine as our global 1660 * disposition to icmp messages asks us reject the datagram. 1661 * 1662 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1663 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1664 * to deliver it to ULP (policy failed), it can lead to 1665 * consistency problems. The cases known at this time are 1666 * ICMP_DESTINATION_UNREACHABLE messages with following code 1667 * values : 1668 * 1669 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1670 * and Upper layer rejects. Then the communication will 1671 * come to a stop. This is solved by making similar decisions 1672 * at both levels. Currently, when we are unable to deliver 1673 * to the Upper Layer (due to policy failures) while IP has 1674 * adjusted ire_max_frag, the next outbound datagram would 1675 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1676 * will be with the right level of protection. Thus the right 1677 * value will be communicated even if we are not able to 1678 * communicate when we get from the wire initially. But this 1679 * assumes there would be at least one outbound datagram after 1680 * IP has adjusted its ire_max_frag value. To make things 1681 * simpler, we accept in clear after the validation of 1682 * AH/ESP headers. 1683 * 1684 * - Other ICMP ERRORS : We may not be able to deliver it to the 1685 * upper layer depending on the level of protection the upper 1686 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1687 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1688 * should be accepted in clear when the Upper layer expects secure. 1689 * Thus the communication may get aborted by some bad ICMP 1690 * packets. 1691 * 1692 * IPQoS Notes: 1693 * The only instance when a packet is sent for processing is when there 1694 * isn't an ICMP client and if we are interested in it. 1695 * If there is a client, IPPF processing will take place in the 1696 * ip_fanout_proto routine. 1697 * 1698 * Zones notes: 1699 * The packet is only processed in the context of the specified zone: typically 1700 * only this zone will reply to an echo request, and only interested clients in 1701 * this zone will receive a copy of the packet. This means that the caller must 1702 * call icmp_inbound() for each relevant zone. 1703 */ 1704 static void 1705 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, 1706 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, 1707 ill_t *recv_ill, zoneid_t zoneid) 1708 { 1709 icmph_t *icmph; 1710 ipha_t *ipha; 1711 int iph_hdr_length; 1712 int hdr_length; 1713 boolean_t interested; 1714 uint32_t ts; 1715 uchar_t *wptr; 1716 ipif_t *ipif; 1717 mblk_t *first_mp; 1718 ipsec_in_t *ii; 1719 ire_t *src_ire; 1720 boolean_t onlink; 1721 timestruc_t now; 1722 uint32_t ill_index; 1723 1724 ASSERT(ill != NULL); 1725 1726 first_mp = mp; 1727 if (mctl_present) { 1728 mp = first_mp->b_cont; 1729 ASSERT(mp != NULL); 1730 } 1731 1732 ipha = (ipha_t *)mp->b_rptr; 1733 if (icmp_accept_clear_messages == 0) { 1734 first_mp = ipsec_check_global_policy(first_mp, NULL, 1735 ipha, NULL, mctl_present); 1736 if (first_mp == NULL) 1737 return; 1738 } 1739 1740 /* 1741 * On a labeled system, we have to check whether the zone itself is 1742 * permitted to receive raw traffic. 1743 */ 1744 if (is_system_labeled()) { 1745 if (zoneid == ALL_ZONES) 1746 zoneid = tsol_packet_to_zoneid(mp); 1747 if (!tsol_can_accept_raw(mp, B_FALSE)) { 1748 ip1dbg(("icmp_inbound: zone %d can't receive raw", 1749 zoneid)); 1750 BUMP_MIB(&icmp_mib, icmpInErrors); 1751 freemsg(first_mp); 1752 return; 1753 } 1754 } 1755 1756 /* 1757 * We have accepted the ICMP message. It means that we will 1758 * respond to the packet if needed. It may not be delivered 1759 * to the upper client depending on the policy constraints 1760 * and the disposition in ipsec_inbound_accept_clear. 1761 */ 1762 1763 ASSERT(ill != NULL); 1764 1765 BUMP_MIB(&icmp_mib, icmpInMsgs); 1766 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1767 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { 1768 /* Last chance to get real. */ 1769 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { 1770 BUMP_MIB(&icmp_mib, icmpInErrors); 1771 freemsg(first_mp); 1772 return; 1773 } 1774 /* Refresh iph following the pullup. */ 1775 ipha = (ipha_t *)mp->b_rptr; 1776 } 1777 /* ICMP header checksum, including checksum field, should be zero. */ 1778 if (sum_valid ? (sum != 0 && sum != 0xFFFF) : 1779 IP_CSUM(mp, iph_hdr_length, 0)) { 1780 BUMP_MIB(&icmp_mib, icmpInCksumErrs); 1781 freemsg(first_mp); 1782 return; 1783 } 1784 /* The IP header will always be a multiple of four bytes */ 1785 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1786 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, 1787 icmph->icmph_code)); 1788 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1789 /* We will set "interested" to "true" if we want a copy */ 1790 interested = B_FALSE; 1791 switch (icmph->icmph_type) { 1792 case ICMP_ECHO_REPLY: 1793 BUMP_MIB(&icmp_mib, icmpInEchoReps); 1794 break; 1795 case ICMP_DEST_UNREACHABLE: 1796 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1797 BUMP_MIB(&icmp_mib, icmpInFragNeeded); 1798 interested = B_TRUE; /* Pass up to transport */ 1799 BUMP_MIB(&icmp_mib, icmpInDestUnreachs); 1800 break; 1801 case ICMP_SOURCE_QUENCH: 1802 interested = B_TRUE; /* Pass up to transport */ 1803 BUMP_MIB(&icmp_mib, icmpInSrcQuenchs); 1804 break; 1805 case ICMP_REDIRECT: 1806 if (!ip_ignore_redirect) 1807 interested = B_TRUE; 1808 BUMP_MIB(&icmp_mib, icmpInRedirects); 1809 break; 1810 case ICMP_ECHO_REQUEST: 1811 /* 1812 * Whether to respond to echo requests that come in as IP 1813 * broadcasts or as IP multicast is subject to debate 1814 * (what isn't?). We aim to please, you pick it. 1815 * Default is do it. 1816 */ 1817 if (!broadcast && !CLASSD(ipha->ipha_dst)) { 1818 /* unicast: always respond */ 1819 interested = B_TRUE; 1820 } else if (CLASSD(ipha->ipha_dst)) { 1821 /* multicast: respond based on tunable */ 1822 interested = ip_g_resp_to_echo_mcast; 1823 } else if (broadcast) { 1824 /* broadcast: respond based on tunable */ 1825 interested = ip_g_resp_to_echo_bcast; 1826 } 1827 BUMP_MIB(&icmp_mib, icmpInEchos); 1828 break; 1829 case ICMP_ROUTER_ADVERTISEMENT: 1830 case ICMP_ROUTER_SOLICITATION: 1831 break; 1832 case ICMP_TIME_EXCEEDED: 1833 interested = B_TRUE; /* Pass up to transport */ 1834 BUMP_MIB(&icmp_mib, icmpInTimeExcds); 1835 break; 1836 case ICMP_PARAM_PROBLEM: 1837 interested = B_TRUE; /* Pass up to transport */ 1838 BUMP_MIB(&icmp_mib, icmpInParmProbs); 1839 break; 1840 case ICMP_TIME_STAMP_REQUEST: 1841 /* Response to Time Stamp Requests is local policy. */ 1842 if (ip_g_resp_to_timestamp && 1843 /* So is whether to respond if it was an IP broadcast. */ 1844 (!broadcast || ip_g_resp_to_timestamp_bcast)) { 1845 int tstamp_len = 3 * sizeof (uint32_t); 1846 1847 if (wptr + tstamp_len > mp->b_wptr) { 1848 if (!pullupmsg(mp, wptr + tstamp_len - 1849 mp->b_rptr)) { 1850 BUMP_MIB(ill->ill_ip_mib, 1851 ipIfStatsInDiscards); 1852 freemsg(first_mp); 1853 return; 1854 } 1855 /* Refresh ipha following the pullup. */ 1856 ipha = (ipha_t *)mp->b_rptr; 1857 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1858 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1859 } 1860 interested = B_TRUE; 1861 } 1862 BUMP_MIB(&icmp_mib, icmpInTimestamps); 1863 break; 1864 case ICMP_TIME_STAMP_REPLY: 1865 BUMP_MIB(&icmp_mib, icmpInTimestampReps); 1866 break; 1867 case ICMP_INFO_REQUEST: 1868 /* Per RFC 1122 3.2.2.7, ignore this. */ 1869 case ICMP_INFO_REPLY: 1870 break; 1871 case ICMP_ADDRESS_MASK_REQUEST: 1872 if ((ip_respond_to_address_mask_broadcast || !broadcast) && 1873 /* TODO m_pullup of complete header? */ 1874 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) 1875 interested = B_TRUE; 1876 BUMP_MIB(&icmp_mib, icmpInAddrMasks); 1877 break; 1878 case ICMP_ADDRESS_MASK_REPLY: 1879 BUMP_MIB(&icmp_mib, icmpInAddrMaskReps); 1880 break; 1881 default: 1882 interested = B_TRUE; /* Pass up to transport */ 1883 BUMP_MIB(&icmp_mib, icmpInUnknowns); 1884 break; 1885 } 1886 /* See if there is an ICMP client. */ 1887 if (ipcl_proto_search(IPPROTO_ICMP) != NULL) { 1888 /* If there is an ICMP client and we want one too, copy it. */ 1889 mblk_t *first_mp1; 1890 1891 if (!interested) { 1892 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1893 ip_policy, recv_ill, zoneid); 1894 return; 1895 } 1896 first_mp1 = ip_copymsg(first_mp); 1897 if (first_mp1 != NULL) { 1898 ip_fanout_proto(q, first_mp1, ill, ipha, 1899 0, mctl_present, ip_policy, recv_ill, zoneid); 1900 } 1901 } else if (!interested) { 1902 freemsg(first_mp); 1903 return; 1904 } else { 1905 /* 1906 * Initiate policy processing for this packet if ip_policy 1907 * is true. 1908 */ 1909 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 1910 ill_index = ill->ill_phyint->phyint_ifindex; 1911 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1912 if (mp == NULL) { 1913 if (mctl_present) { 1914 freeb(first_mp); 1915 } 1916 BUMP_MIB(&icmp_mib, icmpInErrors); 1917 return; 1918 } 1919 } 1920 } 1921 /* We want to do something with it. */ 1922 /* Check db_ref to make sure we can modify the packet. */ 1923 if (mp->b_datap->db_ref > 1) { 1924 mblk_t *first_mp1; 1925 1926 first_mp1 = ip_copymsg(first_mp); 1927 freemsg(first_mp); 1928 if (!first_mp1) { 1929 BUMP_MIB(&icmp_mib, icmpOutDrops); 1930 return; 1931 } 1932 first_mp = first_mp1; 1933 if (mctl_present) { 1934 mp = first_mp->b_cont; 1935 ASSERT(mp != NULL); 1936 } else { 1937 mp = first_mp; 1938 } 1939 ipha = (ipha_t *)mp->b_rptr; 1940 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1941 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1942 } 1943 switch (icmph->icmph_type) { 1944 case ICMP_ADDRESS_MASK_REQUEST: 1945 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1946 if (ipif == NULL) { 1947 freemsg(first_mp); 1948 return; 1949 } 1950 /* 1951 * outging interface must be IPv4 1952 */ 1953 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1954 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1955 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1956 ipif_refrele(ipif); 1957 BUMP_MIB(&icmp_mib, icmpOutAddrMaskReps); 1958 break; 1959 case ICMP_ECHO_REQUEST: 1960 icmph->icmph_type = ICMP_ECHO_REPLY; 1961 BUMP_MIB(&icmp_mib, icmpOutEchoReps); 1962 break; 1963 case ICMP_TIME_STAMP_REQUEST: { 1964 uint32_t *tsp; 1965 1966 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1967 tsp = (uint32_t *)wptr; 1968 tsp++; /* Skip past 'originate time' */ 1969 /* Compute # of milliseconds since midnight */ 1970 gethrestime(&now); 1971 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1972 now.tv_nsec / (NANOSEC / MILLISEC); 1973 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1974 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1975 BUMP_MIB(&icmp_mib, icmpOutTimestampReps); 1976 break; 1977 } 1978 default: 1979 ipha = (ipha_t *)&icmph[1]; 1980 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1981 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1982 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1983 freemsg(first_mp); 1984 return; 1985 } 1986 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1987 ipha = (ipha_t *)&icmph[1]; 1988 } 1989 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1990 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1991 freemsg(first_mp); 1992 return; 1993 } 1994 hdr_length = IPH_HDR_LENGTH(ipha); 1995 if (hdr_length < sizeof (ipha_t)) { 1996 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1997 freemsg(first_mp); 1998 return; 1999 } 2000 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 2001 if (!pullupmsg(mp, 2002 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 2003 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2004 freemsg(first_mp); 2005 return; 2006 } 2007 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2008 ipha = (ipha_t *)&icmph[1]; 2009 } 2010 switch (icmph->icmph_type) { 2011 case ICMP_REDIRECT: 2012 /* 2013 * As there is no upper client to deliver, we don't 2014 * need the first_mp any more. 2015 */ 2016 if (mctl_present) { 2017 freeb(first_mp); 2018 } 2019 icmp_redirect(mp); 2020 return; 2021 case ICMP_DEST_UNREACHABLE: 2022 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 2023 if (!icmp_inbound_too_big(icmph, ipha, ill, 2024 zoneid, mp, iph_hdr_length)) { 2025 freemsg(first_mp); 2026 return; 2027 } 2028 /* 2029 * icmp_inbound_too_big() may alter mp. 2030 * Resynch ipha and icmph accordingly. 2031 */ 2032 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2033 ipha = (ipha_t *)&icmph[1]; 2034 } 2035 /* FALLTHRU */ 2036 default : 2037 /* 2038 * IPQoS notes: Since we have already done IPQoS 2039 * processing we don't want to do it again in 2040 * the fanout routines called by 2041 * icmp_inbound_error_fanout, hence the last 2042 * argument, ip_policy, is B_FALSE. 2043 */ 2044 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 2045 ipha, iph_hdr_length, hdr_length, mctl_present, 2046 B_FALSE, recv_ill, zoneid); 2047 } 2048 return; 2049 } 2050 /* Send out an ICMP packet */ 2051 icmph->icmph_checksum = 0; 2052 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 2053 if (icmph->icmph_checksum == 0) 2054 icmph->icmph_checksum = 0xFFFF; 2055 if (broadcast || CLASSD(ipha->ipha_dst)) { 2056 ipif_t *ipif_chosen; 2057 /* 2058 * Make it look like it was directed to us, so we don't look 2059 * like a fool with a broadcast or multicast source address. 2060 */ 2061 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 2062 /* 2063 * Make sure that we haven't grabbed an interface that's DOWN. 2064 */ 2065 if (ipif != NULL) { 2066 ipif_chosen = ipif_select_source(ipif->ipif_ill, 2067 ipha->ipha_src, zoneid); 2068 if (ipif_chosen != NULL) { 2069 ipif_refrele(ipif); 2070 ipif = ipif_chosen; 2071 } 2072 } 2073 if (ipif == NULL) { 2074 ip0dbg(("icmp_inbound: " 2075 "No source for broadcast/multicast:\n" 2076 "\tsrc 0x%x dst 0x%x ill %p " 2077 "ipif_lcl_addr 0x%x\n", 2078 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 2079 (void *)ill, 2080 ill->ill_ipif->ipif_lcl_addr)); 2081 freemsg(first_mp); 2082 return; 2083 } 2084 ASSERT(ipif != NULL && !ipif->ipif_isv6); 2085 ipha->ipha_dst = ipif->ipif_src_addr; 2086 ipif_refrele(ipif); 2087 } 2088 /* Reset time to live. */ 2089 ipha->ipha_ttl = ip_def_ttl; 2090 { 2091 /* Swap source and destination addresses */ 2092 ipaddr_t tmp; 2093 2094 tmp = ipha->ipha_src; 2095 ipha->ipha_src = ipha->ipha_dst; 2096 ipha->ipha_dst = tmp; 2097 } 2098 ipha->ipha_ident = 0; 2099 if (!IS_SIMPLE_IPH(ipha)) 2100 icmp_options_update(ipha); 2101 2102 /* 2103 * ICMP echo replies should go out on the same interface 2104 * the request came on as probes used by in.mpathd for detecting 2105 * NIC failures are ECHO packets. We turn-off load spreading 2106 * by setting ipsec_in_attach_if to B_TRUE, which is copied 2107 * to ipsec_out_attach_if by ipsec_in_to_out called later in this 2108 * function. This is in turn handled by ip_wput and ip_newroute 2109 * to make sure that the packet goes out on the interface it came 2110 * in on. If we don't turnoff load spreading, the packets might get 2111 * dropped if there are no non-FAILED/INACTIVE interfaces for it 2112 * to go out and in.mpathd would wrongly detect a failure or 2113 * mis-detect a NIC failure for link failure. As load spreading 2114 * can happen only if ill_group is not NULL, we do only for 2115 * that case and this does not affect the normal case. 2116 * 2117 * We turn off load spreading only on echo packets that came from 2118 * on-link hosts. If the interface route has been deleted, this will 2119 * not be enforced as we can't do much. For off-link hosts, as the 2120 * default routes in IPv4 does not typically have an ire_ipif 2121 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. 2122 * Moreover, expecting a default route through this interface may 2123 * not be correct. We use ipha_dst because of the swap above. 2124 */ 2125 onlink = B_FALSE; 2126 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { 2127 /* 2128 * First, we need to make sure that it is not one of our 2129 * local addresses. If we set onlink when it is one of 2130 * our local addresses, we will end up creating IRE_CACHES 2131 * for one of our local addresses. Then, we will never 2132 * accept packets for them afterwards. 2133 */ 2134 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, 2135 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 2136 if (src_ire == NULL) { 2137 ipif = ipif_get_next_ipif(NULL, ill); 2138 if (ipif == NULL) { 2139 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2140 freemsg(mp); 2141 return; 2142 } 2143 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 2144 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 2145 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE); 2146 ipif_refrele(ipif); 2147 if (src_ire != NULL) { 2148 onlink = B_TRUE; 2149 ire_refrele(src_ire); 2150 } 2151 } else { 2152 ire_refrele(src_ire); 2153 } 2154 } 2155 if (!mctl_present) { 2156 /* 2157 * This packet should go out the same way as it 2158 * came in i.e in clear. To make sure that global 2159 * policy will not be applied to this in ip_wput_ire, 2160 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2161 */ 2162 ASSERT(first_mp == mp); 2163 if ((first_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 2164 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2165 freemsg(mp); 2166 return; 2167 } 2168 ii = (ipsec_in_t *)first_mp->b_rptr; 2169 2170 /* This is not a secure packet */ 2171 ii->ipsec_in_secure = B_FALSE; 2172 if (onlink) { 2173 ii->ipsec_in_attach_if = B_TRUE; 2174 ii->ipsec_in_ill_index = 2175 ill->ill_phyint->phyint_ifindex; 2176 ii->ipsec_in_rill_index = 2177 recv_ill->ill_phyint->phyint_ifindex; 2178 } 2179 first_mp->b_cont = mp; 2180 } else if (onlink) { 2181 ii = (ipsec_in_t *)first_mp->b_rptr; 2182 ii->ipsec_in_attach_if = B_TRUE; 2183 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 2184 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; 2185 } else { 2186 ii = (ipsec_in_t *)first_mp->b_rptr; 2187 } 2188 ii->ipsec_in_zoneid = zoneid; 2189 ASSERT(zoneid != ALL_ZONES); 2190 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2191 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2192 return; 2193 } 2194 BUMP_MIB(&icmp_mib, icmpOutMsgs); 2195 put(WR(q), first_mp); 2196 } 2197 2198 static ipaddr_t 2199 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) 2200 { 2201 conn_t *connp; 2202 connf_t *connfp; 2203 ipaddr_t nexthop_addr = INADDR_ANY; 2204 int hdr_length = IPH_HDR_LENGTH(ipha); 2205 uint16_t *up; 2206 uint32_t ports; 2207 2208 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2209 switch (ipha->ipha_protocol) { 2210 case IPPROTO_TCP: 2211 { 2212 tcph_t *tcph; 2213 2214 /* do a reverse lookup */ 2215 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2216 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, 2217 TCPS_LISTEN); 2218 break; 2219 } 2220 case IPPROTO_UDP: 2221 { 2222 uint32_t dstport, srcport; 2223 2224 ((uint16_t *)&ports)[0] = up[1]; 2225 ((uint16_t *)&ports)[1] = up[0]; 2226 2227 /* Extract ports in net byte order */ 2228 dstport = htons(ntohl(ports) & 0xFFFF); 2229 srcport = htons(ntohl(ports) >> 16); 2230 2231 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 2232 mutex_enter(&connfp->connf_lock); 2233 connp = connfp->connf_head; 2234 2235 /* do a reverse lookup */ 2236 while ((connp != NULL) && 2237 (!IPCL_UDP_MATCH(connp, dstport, 2238 ipha->ipha_src, srcport, ipha->ipha_dst) || 2239 !IPCL_ZONE_MATCH(connp, zoneid))) { 2240 connp = connp->conn_next; 2241 } 2242 if (connp != NULL) 2243 CONN_INC_REF(connp); 2244 mutex_exit(&connfp->connf_lock); 2245 break; 2246 } 2247 case IPPROTO_SCTP: 2248 { 2249 in6_addr_t map_src, map_dst; 2250 2251 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); 2252 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); 2253 ((uint16_t *)&ports)[0] = up[1]; 2254 ((uint16_t *)&ports)[1] = up[0]; 2255 2256 if ((connp = sctp_find_conn(&map_src, &map_dst, ports, 2257 0, zoneid)) == NULL) { 2258 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, 2259 zoneid, ports, ipha); 2260 } else { 2261 CONN_INC_REF(connp); 2262 SCTP_REFRELE(CONN2SCTP(connp)); 2263 } 2264 break; 2265 } 2266 default: 2267 { 2268 ipha_t ripha; 2269 2270 ripha.ipha_src = ipha->ipha_dst; 2271 ripha.ipha_dst = ipha->ipha_src; 2272 ripha.ipha_protocol = ipha->ipha_protocol; 2273 2274 connfp = &ipcl_proto_fanout[ipha->ipha_protocol]; 2275 mutex_enter(&connfp->connf_lock); 2276 connp = connfp->connf_head; 2277 for (connp = connfp->connf_head; connp != NULL; 2278 connp = connp->conn_next) { 2279 if (IPCL_PROTO_MATCH(connp, 2280 ipha->ipha_protocol, &ripha, ill, 2281 0, zoneid)) { 2282 CONN_INC_REF(connp); 2283 break; 2284 } 2285 } 2286 mutex_exit(&connfp->connf_lock); 2287 } 2288 } 2289 if (connp != NULL) { 2290 if (connp->conn_nexthop_set) 2291 nexthop_addr = connp->conn_nexthop_v4; 2292 CONN_DEC_REF(connp); 2293 } 2294 return (nexthop_addr); 2295 } 2296 2297 /* Table from RFC 1191 */ 2298 static int icmp_frag_size_table[] = 2299 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2300 2301 /* 2302 * Process received ICMP Packet too big. 2303 * After updating any IRE it does the fanout to any matching transport streams. 2304 * Assumes the message has been pulled up till the IP header that caused 2305 * the error. 2306 * 2307 * Returns B_FALSE on failure and B_TRUE on success. 2308 */ 2309 static boolean_t 2310 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, 2311 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length) 2312 { 2313 ire_t *ire, *first_ire; 2314 int mtu; 2315 int hdr_length; 2316 ipaddr_t nexthop_addr; 2317 2318 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 2319 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 2320 ASSERT(ill != NULL); 2321 2322 hdr_length = IPH_HDR_LENGTH(ipha); 2323 2324 /* Drop if the original packet contained a source route */ 2325 if (ip_source_route_included(ipha)) { 2326 return (B_FALSE); 2327 } 2328 /* 2329 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport 2330 * header. 2331 */ 2332 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2333 mp->b_wptr) { 2334 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2335 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2336 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2337 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n")); 2338 return (B_FALSE); 2339 } 2340 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2341 ipha = (ipha_t *)&icmph[1]; 2342 } 2343 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp); 2344 if (nexthop_addr != INADDR_ANY) { 2345 /* nexthop set */ 2346 first_ire = ire_ctable_lookup(ipha->ipha_dst, 2347 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp), 2348 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW); 2349 } else { 2350 /* nexthop not set */ 2351 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, 2352 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 2353 } 2354 2355 if (!first_ire) { 2356 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", 2357 ntohl(ipha->ipha_dst))); 2358 return (B_FALSE); 2359 } 2360 /* Check for MTU discovery advice as described in RFC 1191 */ 2361 mtu = ntohs(icmph->icmph_du_mtu); 2362 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2363 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2364 ire = ire->ire_next) { 2365 /* 2366 * Look for the connection to which this ICMP message is 2367 * directed. If it has the IP_NEXTHOP option set, then the 2368 * search is limited to IREs with the MATCH_IRE_PRIVATE 2369 * option. Else the search is limited to regular IREs. 2370 */ 2371 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2372 (nexthop_addr != ire->ire_gateway_addr)) || 2373 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && 2374 (nexthop_addr != INADDR_ANY))) 2375 continue; 2376 2377 mutex_enter(&ire->ire_lock); 2378 if (icmph->icmph_du_zero == 0 && mtu > 68) { 2379 /* Reduce the IRE max frag value as advised. */ 2380 ip1dbg(("Received mtu from router: %d (was %d)\n", 2381 mtu, ire->ire_max_frag)); 2382 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2383 } else { 2384 uint32_t length; 2385 int i; 2386 2387 /* 2388 * Use the table from RFC 1191 to figure out 2389 * the next "plateau" based on the length in 2390 * the original IP packet. 2391 */ 2392 length = ntohs(ipha->ipha_length); 2393 if (ire->ire_max_frag <= length && 2394 ire->ire_max_frag >= length - hdr_length) { 2395 /* 2396 * Handle broken BSD 4.2 systems that 2397 * return the wrong iph_length in ICMP 2398 * errors. 2399 */ 2400 ip1dbg(("Wrong mtu: sent %d, ire %d\n", 2401 length, ire->ire_max_frag)); 2402 length -= hdr_length; 2403 } 2404 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2405 if (length > icmp_frag_size_table[i]) 2406 break; 2407 } 2408 if (i == A_CNT(icmp_frag_size_table)) { 2409 /* Smaller than 68! */ 2410 ip1dbg(("Too big for packet size %d\n", 2411 length)); 2412 ire->ire_max_frag = MIN(ire->ire_max_frag, 576); 2413 ire->ire_frag_flag = 0; 2414 } else { 2415 mtu = icmp_frag_size_table[i]; 2416 ip1dbg(("Calculated mtu %d, packet size %d, " 2417 "before %d", mtu, length, 2418 ire->ire_max_frag)); 2419 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2420 ip1dbg((", after %d\n", ire->ire_max_frag)); 2421 } 2422 /* Record the new max frag size for the ULP. */ 2423 icmph->icmph_du_zero = 0; 2424 icmph->icmph_du_mtu = 2425 htons((uint16_t)ire->ire_max_frag); 2426 } 2427 mutex_exit(&ire->ire_lock); 2428 } 2429 rw_exit(&first_ire->ire_bucket->irb_lock); 2430 ire_refrele(first_ire); 2431 return (B_TRUE); 2432 } 2433 2434 /* 2435 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2436 * calls this function. 2437 */ 2438 static mblk_t * 2439 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2440 { 2441 ipha_t *ipha; 2442 icmph_t *icmph; 2443 ipha_t *in_ipha; 2444 int length; 2445 2446 ASSERT(mp->b_datap->db_type == M_DATA); 2447 2448 /* 2449 * For Self-encapsulated packets, we added an extra IP header 2450 * without the options. Inner IP header is the one from which 2451 * the outer IP header was formed. Thus, we need to remove the 2452 * outer IP header. To do this, we pullup the whole message 2453 * and overlay whatever follows the outer IP header over the 2454 * outer IP header. 2455 */ 2456 2457 if (!pullupmsg(mp, -1)) 2458 return (NULL); 2459 2460 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2461 ipha = (ipha_t *)&icmph[1]; 2462 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2463 2464 /* 2465 * The length that we want to overlay is following the inner 2466 * IP header. Subtracting the IP header + icmp header + outer 2467 * IP header's length should give us the length that we want to 2468 * overlay. 2469 */ 2470 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2471 hdr_length; 2472 /* 2473 * Overlay whatever follows the inner header over the 2474 * outer header. 2475 */ 2476 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2477 2478 /* Set the wptr to account for the outer header */ 2479 mp->b_wptr -= hdr_length; 2480 return (mp); 2481 } 2482 2483 /* 2484 * Try to pass the ICMP message upstream in case the ULP cares. 2485 * 2486 * If the packet that caused the ICMP error is secure, we send 2487 * it to AH/ESP to make sure that the attached packet has a 2488 * valid association. ipha in the code below points to the 2489 * IP header of the packet that caused the error. 2490 * 2491 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2492 * in the context of IPSEC. Normally we tell the upper layer 2493 * whenever we send the ire (including ip_bind), the IPSEC header 2494 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2495 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2496 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2497 * same thing. As TCP has the IPSEC options size that needs to be 2498 * adjusted, we just pass the MTU unchanged. 2499 * 2500 * IFN could have been generated locally or by some router. 2501 * 2502 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2503 * This happens because IP adjusted its value of MTU on an 2504 * earlier IFN message and could not tell the upper layer, 2505 * the new adjusted value of MTU e.g. Packet was encrypted 2506 * or there was not enough information to fanout to upper 2507 * layers. Thus on the next outbound datagram, ip_wput_ire 2508 * generates the IFN, where IPSEC processing has *not* been 2509 * done. 2510 * 2511 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2512 * could have generated this. This happens because ire_max_frag 2513 * value in IP was set to a new value, while the IPSEC processing 2514 * was being done and after we made the fragmentation check in 2515 * ip_wput_ire. Thus on return from IPSEC processing, 2516 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2517 * and generates the IFN. As IPSEC processing is over, we fanout 2518 * to AH/ESP to remove the header. 2519 * 2520 * In both these cases, ipsec_in_loopback will be set indicating 2521 * that IFN was generated locally. 2522 * 2523 * ROUTER : IFN could be secure or non-secure. 2524 * 2525 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2526 * packet in error has AH/ESP headers to validate the AH/ESP 2527 * headers. AH/ESP will verify whether there is a valid SA or 2528 * not and send it back. We will fanout again if we have more 2529 * data in the packet. 2530 * 2531 * If the packet in error does not have AH/ESP, we handle it 2532 * like any other case. 2533 * 2534 * * NON_SECURE : If the packet in error has AH/ESP headers, 2535 * we attach a dummy ipsec_in and send it up to AH/ESP 2536 * for validation. AH/ESP will verify whether there is a 2537 * valid SA or not and send it back. We will fanout again if 2538 * we have more data in the packet. 2539 * 2540 * If the packet in error does not have AH/ESP, we handle it 2541 * like any other case. 2542 */ 2543 static void 2544 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2545 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2546 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2547 zoneid_t zoneid) 2548 { 2549 uint16_t *up; /* Pointer to ports in ULP header */ 2550 uint32_t ports; /* reversed ports for fanout */ 2551 ipha_t ripha; /* With reversed addresses */ 2552 mblk_t *first_mp; 2553 ipsec_in_t *ii; 2554 tcph_t *tcph; 2555 conn_t *connp; 2556 2557 ASSERT(ill != NULL); 2558 2559 first_mp = mp; 2560 if (mctl_present) { 2561 mp = first_mp->b_cont; 2562 ASSERT(mp != NULL); 2563 2564 ii = (ipsec_in_t *)first_mp->b_rptr; 2565 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2566 } else { 2567 ii = NULL; 2568 } 2569 2570 switch (ipha->ipha_protocol) { 2571 case IPPROTO_UDP: 2572 /* 2573 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2574 * transport header. 2575 */ 2576 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2577 mp->b_wptr) { 2578 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2579 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2580 goto discard_pkt; 2581 } 2582 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2583 ipha = (ipha_t *)&icmph[1]; 2584 } 2585 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2586 2587 /* 2588 * Attempt to find a client stream based on port. 2589 * Note that we do a reverse lookup since the header is 2590 * in the form we sent it out. 2591 * The ripha header is only used for the IP_UDP_MATCH and we 2592 * only set the src and dst addresses and protocol. 2593 */ 2594 ripha.ipha_src = ipha->ipha_dst; 2595 ripha.ipha_dst = ipha->ipha_src; 2596 ripha.ipha_protocol = ipha->ipha_protocol; 2597 ((uint16_t *)&ports)[0] = up[1]; 2598 ((uint16_t *)&ports)[1] = up[0]; 2599 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2600 ntohl(ipha->ipha_src), ntohs(up[0]), 2601 ntohl(ipha->ipha_dst), ntohs(up[1]), 2602 icmph->icmph_type, icmph->icmph_code)); 2603 2604 /* Have to change db_type after any pullupmsg */ 2605 DB_TYPE(mp) = M_CTL; 2606 2607 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2608 mctl_present, ip_policy, recv_ill, zoneid); 2609 return; 2610 2611 case IPPROTO_TCP: 2612 /* 2613 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2614 * transport header. 2615 */ 2616 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2617 mp->b_wptr) { 2618 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2619 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2620 goto discard_pkt; 2621 } 2622 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2623 ipha = (ipha_t *)&icmph[1]; 2624 } 2625 /* 2626 * Find a TCP client stream for this packet. 2627 * Note that we do a reverse lookup since the header is 2628 * in the form we sent it out. 2629 */ 2630 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2631 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN); 2632 if (connp == NULL) 2633 goto discard_pkt; 2634 2635 /* Have to change db_type after any pullupmsg */ 2636 DB_TYPE(mp) = M_CTL; 2637 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 2638 connp, SQTAG_TCP_INPUT_ICMP_ERR); 2639 return; 2640 2641 case IPPROTO_SCTP: 2642 /* 2643 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2644 * transport header. 2645 */ 2646 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2647 mp->b_wptr) { 2648 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2649 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2650 goto discard_pkt; 2651 } 2652 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2653 ipha = (ipha_t *)&icmph[1]; 2654 } 2655 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2656 /* 2657 * Find a SCTP client stream for this packet. 2658 * Note that we do a reverse lookup since the header is 2659 * in the form we sent it out. 2660 * The ripha header is only used for the matching and we 2661 * only set the src and dst addresses, protocol, and version. 2662 */ 2663 ripha.ipha_src = ipha->ipha_dst; 2664 ripha.ipha_dst = ipha->ipha_src; 2665 ripha.ipha_protocol = ipha->ipha_protocol; 2666 ripha.ipha_version_and_hdr_length = 2667 ipha->ipha_version_and_hdr_length; 2668 ((uint16_t *)&ports)[0] = up[1]; 2669 ((uint16_t *)&ports)[1] = up[0]; 2670 2671 /* Have to change db_type after any pullupmsg */ 2672 DB_TYPE(mp) = M_CTL; 2673 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2674 mctl_present, ip_policy, 0, zoneid); 2675 return; 2676 2677 case IPPROTO_ESP: 2678 case IPPROTO_AH: { 2679 int ipsec_rc; 2680 2681 /* 2682 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2683 * We will re-use the IPSEC_IN if it is already present as 2684 * AH/ESP will not affect any fields in the IPSEC_IN for 2685 * ICMP errors. If there is no IPSEC_IN, allocate a new 2686 * one and attach it in the front. 2687 */ 2688 if (ii != NULL) { 2689 /* 2690 * ip_fanout_proto_again converts the ICMP errors 2691 * that come back from AH/ESP to M_DATA so that 2692 * if it is non-AH/ESP and we do a pullupmsg in 2693 * this function, it would work. Convert it back 2694 * to M_CTL before we send up as this is a ICMP 2695 * error. This could have been generated locally or 2696 * by some router. Validate the inner IPSEC 2697 * headers. 2698 * 2699 * NOTE : ill_index is used by ip_fanout_proto_again 2700 * to locate the ill. 2701 */ 2702 ASSERT(ill != NULL); 2703 ii->ipsec_in_ill_index = 2704 ill->ill_phyint->phyint_ifindex; 2705 ii->ipsec_in_rill_index = 2706 recv_ill->ill_phyint->phyint_ifindex; 2707 DB_TYPE(first_mp->b_cont) = M_CTL; 2708 } else { 2709 /* 2710 * IPSEC_IN is not present. We attach a ipsec_in 2711 * message and send up to IPSEC for validating 2712 * and removing the IPSEC headers. Clear 2713 * ipsec_in_secure so that when we return 2714 * from IPSEC, we don't mistakenly think that this 2715 * is a secure packet came from the network. 2716 * 2717 * NOTE : ill_index is used by ip_fanout_proto_again 2718 * to locate the ill. 2719 */ 2720 ASSERT(first_mp == mp); 2721 first_mp = ipsec_in_alloc(B_TRUE); 2722 if (first_mp == NULL) { 2723 freemsg(mp); 2724 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2725 return; 2726 } 2727 ii = (ipsec_in_t *)first_mp->b_rptr; 2728 2729 /* This is not a secure packet */ 2730 ii->ipsec_in_secure = B_FALSE; 2731 first_mp->b_cont = mp; 2732 DB_TYPE(mp) = M_CTL; 2733 ASSERT(ill != NULL); 2734 ii->ipsec_in_ill_index = 2735 ill->ill_phyint->phyint_ifindex; 2736 ii->ipsec_in_rill_index = 2737 recv_ill->ill_phyint->phyint_ifindex; 2738 } 2739 ip2dbg(("icmp_inbound_error: ipsec\n")); 2740 2741 if (!ipsec_loaded()) { 2742 ip_proto_not_sup(q, first_mp, 0, zoneid); 2743 return; 2744 } 2745 2746 if (ipha->ipha_protocol == IPPROTO_ESP) 2747 ipsec_rc = ipsecesp_icmp_error(first_mp); 2748 else 2749 ipsec_rc = ipsecah_icmp_error(first_mp); 2750 if (ipsec_rc == IPSEC_STATUS_FAILED) 2751 return; 2752 2753 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2754 return; 2755 } 2756 default: 2757 /* 2758 * The ripha header is only used for the lookup and we 2759 * only set the src and dst addresses and protocol. 2760 */ 2761 ripha.ipha_src = ipha->ipha_dst; 2762 ripha.ipha_dst = ipha->ipha_src; 2763 ripha.ipha_protocol = ipha->ipha_protocol; 2764 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2765 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2766 ntohl(ipha->ipha_dst), 2767 icmph->icmph_type, icmph->icmph_code)); 2768 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2769 ipha_t *in_ipha; 2770 2771 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2772 mp->b_wptr) { 2773 if (!pullupmsg(mp, (uchar_t *)ipha + 2774 hdr_length + sizeof (ipha_t) - 2775 mp->b_rptr)) { 2776 goto discard_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 goto discard_pkt; 2793 } 2794 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2795 goto discard_pkt; 2796 } 2797 /* Check for Self-encapsulated tunnels */ 2798 if (in_ipha->ipha_src == ipha->ipha_src && 2799 in_ipha->ipha_dst == ipha->ipha_dst) { 2800 2801 mp = icmp_inbound_self_encap_error(mp, 2802 iph_hdr_length, hdr_length); 2803 if (mp == NULL) 2804 goto discard_pkt; 2805 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2806 ipha = (ipha_t *)&icmph[1]; 2807 hdr_length = IPH_HDR_LENGTH(ipha); 2808 /* 2809 * The packet in error is self-encapsualted. 2810 * And we are finding it further encapsulated 2811 * which we could not have possibly generated. 2812 */ 2813 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2814 goto discard_pkt; 2815 } 2816 icmp_inbound_error_fanout(q, ill, first_mp, 2817 icmph, ipha, iph_hdr_length, hdr_length, 2818 mctl_present, ip_policy, recv_ill, zoneid); 2819 return; 2820 } 2821 } 2822 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2823 ipha->ipha_protocol == IPPROTO_IPV6) && 2824 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2825 ii != NULL && 2826 ii->ipsec_in_loopback && 2827 ii->ipsec_in_secure) { 2828 /* 2829 * For IP tunnels that get a looped-back 2830 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2831 * reported new MTU to take into account the IPsec 2832 * headers protecting this configured tunnel. 2833 * 2834 * This allows the tunnel module (tun.c) to blindly 2835 * accept the MTU reported in an ICMP "too big" 2836 * message. 2837 * 2838 * Non-looped back ICMP messages will just be 2839 * handled by the security protocols (if needed), 2840 * and the first subsequent packet will hit this 2841 * path. 2842 */ 2843 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2844 ipsec_in_extra_length(first_mp)); 2845 } 2846 /* Have to change db_type after any pullupmsg */ 2847 DB_TYPE(mp) = M_CTL; 2848 2849 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2850 ip_policy, recv_ill, zoneid); 2851 return; 2852 } 2853 /* NOTREACHED */ 2854 discard_pkt: 2855 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2856 drop_pkt:; 2857 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2858 freemsg(first_mp); 2859 } 2860 2861 /* 2862 * Common IP options parser. 2863 * 2864 * Setup routine: fill in *optp with options-parsing state, then 2865 * tail-call ipoptp_next to return the first option. 2866 */ 2867 uint8_t 2868 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2869 { 2870 uint32_t totallen; /* total length of all options */ 2871 2872 totallen = ipha->ipha_version_and_hdr_length - 2873 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2874 totallen <<= 2; 2875 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2876 optp->ipoptp_end = optp->ipoptp_next + totallen; 2877 optp->ipoptp_flags = 0; 2878 return (ipoptp_next(optp)); 2879 } 2880 2881 /* 2882 * Common IP options parser: extract next option. 2883 */ 2884 uint8_t 2885 ipoptp_next(ipoptp_t *optp) 2886 { 2887 uint8_t *end = optp->ipoptp_end; 2888 uint8_t *cur = optp->ipoptp_next; 2889 uint8_t opt, len, pointer; 2890 2891 /* 2892 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2893 * has been corrupted. 2894 */ 2895 ASSERT(cur <= end); 2896 2897 if (cur == end) 2898 return (IPOPT_EOL); 2899 2900 opt = cur[IPOPT_OPTVAL]; 2901 2902 /* 2903 * Skip any NOP options. 2904 */ 2905 while (opt == IPOPT_NOP) { 2906 cur++; 2907 if (cur == end) 2908 return (IPOPT_EOL); 2909 opt = cur[IPOPT_OPTVAL]; 2910 } 2911 2912 if (opt == IPOPT_EOL) 2913 return (IPOPT_EOL); 2914 2915 /* 2916 * Option requiring a length. 2917 */ 2918 if ((cur + 1) >= end) { 2919 optp->ipoptp_flags |= IPOPTP_ERROR; 2920 return (IPOPT_EOL); 2921 } 2922 len = cur[IPOPT_OLEN]; 2923 if (len < 2) { 2924 optp->ipoptp_flags |= IPOPTP_ERROR; 2925 return (IPOPT_EOL); 2926 } 2927 optp->ipoptp_cur = cur; 2928 optp->ipoptp_len = len; 2929 optp->ipoptp_next = cur + len; 2930 if (cur + len > end) { 2931 optp->ipoptp_flags |= IPOPTP_ERROR; 2932 return (IPOPT_EOL); 2933 } 2934 2935 /* 2936 * For the options which require a pointer field, make sure 2937 * its there, and make sure it points to either something 2938 * inside this option, or the end of the option. 2939 */ 2940 switch (opt) { 2941 case IPOPT_RR: 2942 case IPOPT_TS: 2943 case IPOPT_LSRR: 2944 case IPOPT_SSRR: 2945 if (len <= IPOPT_OFFSET) { 2946 optp->ipoptp_flags |= IPOPTP_ERROR; 2947 return (opt); 2948 } 2949 pointer = cur[IPOPT_OFFSET]; 2950 if (pointer - 1 > len) { 2951 optp->ipoptp_flags |= IPOPTP_ERROR; 2952 return (opt); 2953 } 2954 break; 2955 } 2956 2957 /* 2958 * Sanity check the pointer field based on the type of the 2959 * option. 2960 */ 2961 switch (opt) { 2962 case IPOPT_RR: 2963 case IPOPT_SSRR: 2964 case IPOPT_LSRR: 2965 if (pointer < IPOPT_MINOFF_SR) 2966 optp->ipoptp_flags |= IPOPTP_ERROR; 2967 break; 2968 case IPOPT_TS: 2969 if (pointer < IPOPT_MINOFF_IT) 2970 optp->ipoptp_flags |= IPOPTP_ERROR; 2971 /* 2972 * Note that the Internet Timestamp option also 2973 * contains two four bit fields (the Overflow field, 2974 * and the Flag field), which follow the pointer 2975 * field. We don't need to check that these fields 2976 * fall within the length of the option because this 2977 * was implicitely done above. We've checked that the 2978 * pointer value is at least IPOPT_MINOFF_IT, and that 2979 * it falls within the option. Since IPOPT_MINOFF_IT > 2980 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2981 */ 2982 ASSERT(len > IPOPT_POS_OV_FLG); 2983 break; 2984 } 2985 2986 return (opt); 2987 } 2988 2989 /* 2990 * Use the outgoing IP header to create an IP_OPTIONS option the way 2991 * it was passed down from the application. 2992 */ 2993 int 2994 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) 2995 { 2996 ipoptp_t opts; 2997 const uchar_t *opt; 2998 uint8_t optval; 2999 uint8_t optlen; 3000 uint32_t len = 0; 3001 uchar_t *buf1 = buf; 3002 3003 buf += IP_ADDR_LEN; /* Leave room for final destination */ 3004 len += IP_ADDR_LEN; 3005 bzero(buf1, IP_ADDR_LEN); 3006 3007 /* 3008 * OK to cast away const here, as we don't store through the returned 3009 * opts.ipoptp_cur pointer. 3010 */ 3011 for (optval = ipoptp_first(&opts, (ipha_t *)ipha); 3012 optval != IPOPT_EOL; 3013 optval = ipoptp_next(&opts)) { 3014 int off; 3015 3016 opt = opts.ipoptp_cur; 3017 optlen = opts.ipoptp_len; 3018 switch (optval) { 3019 case IPOPT_SSRR: 3020 case IPOPT_LSRR: 3021 3022 /* 3023 * Insert ipha_dst as the first entry in the source 3024 * route and move down the entries on step. 3025 * The last entry gets placed at buf1. 3026 */ 3027 buf[IPOPT_OPTVAL] = optval; 3028 buf[IPOPT_OLEN] = optlen; 3029 buf[IPOPT_OFFSET] = optlen; 3030 3031 off = optlen - IP_ADDR_LEN; 3032 if (off < 0) { 3033 /* No entries in source route */ 3034 break; 3035 } 3036 /* Last entry in source route */ 3037 bcopy(opt + off, buf1, IP_ADDR_LEN); 3038 off -= IP_ADDR_LEN; 3039 3040 while (off > 0) { 3041 bcopy(opt + off, 3042 buf + off + IP_ADDR_LEN, 3043 IP_ADDR_LEN); 3044 off -= IP_ADDR_LEN; 3045 } 3046 /* ipha_dst into first slot */ 3047 bcopy(&ipha->ipha_dst, 3048 buf + off + IP_ADDR_LEN, 3049 IP_ADDR_LEN); 3050 buf += optlen; 3051 len += optlen; 3052 break; 3053 3054 case IPOPT_COMSEC: 3055 case IPOPT_SECURITY: 3056 /* if passing up a label is not ok, then remove */ 3057 if (is_system_labeled()) 3058 break; 3059 /* FALLTHROUGH */ 3060 default: 3061 bcopy(opt, buf, optlen); 3062 buf += optlen; 3063 len += optlen; 3064 break; 3065 } 3066 } 3067 done: 3068 /* Pad the resulting options */ 3069 while (len & 0x3) { 3070 *buf++ = IPOPT_EOL; 3071 len++; 3072 } 3073 return (len); 3074 } 3075 3076 /* 3077 * Update any record route or timestamp options to include this host. 3078 * Reverse any source route option. 3079 * This routine assumes that the options are well formed i.e. that they 3080 * have already been checked. 3081 */ 3082 static void 3083 icmp_options_update(ipha_t *ipha) 3084 { 3085 ipoptp_t opts; 3086 uchar_t *opt; 3087 uint8_t optval; 3088 ipaddr_t src; /* Our local address */ 3089 ipaddr_t dst; 3090 3091 ip2dbg(("icmp_options_update\n")); 3092 src = ipha->ipha_src; 3093 dst = ipha->ipha_dst; 3094 3095 for (optval = ipoptp_first(&opts, ipha); 3096 optval != IPOPT_EOL; 3097 optval = ipoptp_next(&opts)) { 3098 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 3099 opt = opts.ipoptp_cur; 3100 ip2dbg(("icmp_options_update: opt %d, len %d\n", 3101 optval, opts.ipoptp_len)); 3102 switch (optval) { 3103 int off1, off2; 3104 case IPOPT_SSRR: 3105 case IPOPT_LSRR: 3106 /* 3107 * Reverse the source route. The first entry 3108 * should be the next to last one in the current 3109 * source route (the last entry is our address). 3110 * The last entry should be the final destination. 3111 */ 3112 off1 = IPOPT_MINOFF_SR - 1; 3113 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 3114 if (off2 < 0) { 3115 /* No entries in source route */ 3116 ip1dbg(( 3117 "icmp_options_update: bad src route\n")); 3118 break; 3119 } 3120 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 3121 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 3122 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 3123 off2 -= IP_ADDR_LEN; 3124 3125 while (off1 < off2) { 3126 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 3127 bcopy((char *)opt + off2, (char *)opt + off1, 3128 IP_ADDR_LEN); 3129 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 3130 off1 += IP_ADDR_LEN; 3131 off2 -= IP_ADDR_LEN; 3132 } 3133 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 3134 break; 3135 } 3136 } 3137 } 3138 3139 /* 3140 * Process received ICMP Redirect messages. 3141 */ 3142 /* ARGSUSED */ 3143 static void 3144 icmp_redirect(mblk_t *mp) 3145 { 3146 ipha_t *ipha; 3147 int iph_hdr_length; 3148 icmph_t *icmph; 3149 ipha_t *ipha_err; 3150 ire_t *ire; 3151 ire_t *prev_ire; 3152 ire_t *save_ire; 3153 ipaddr_t src, dst, gateway; 3154 iulp_t ulp_info = { 0 }; 3155 int error; 3156 3157 ipha = (ipha_t *)mp->b_rptr; 3158 iph_hdr_length = IPH_HDR_LENGTH(ipha); 3159 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 3160 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 3161 BUMP_MIB(&icmp_mib, icmpInErrors); 3162 freemsg(mp); 3163 return; 3164 } 3165 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 3166 ipha_err = (ipha_t *)&icmph[1]; 3167 src = ipha->ipha_src; 3168 dst = ipha_err->ipha_dst; 3169 gateway = icmph->icmph_rd_gateway; 3170 /* Make sure the new gateway is reachable somehow. */ 3171 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 3172 ALL_ZONES, NULL, MATCH_IRE_TYPE); 3173 /* 3174 * Make sure we had a route for the dest in question and that 3175 * that route was pointing to the old gateway (the source of the 3176 * redirect packet.) 3177 */ 3178 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 3179 NULL, MATCH_IRE_GW); 3180 /* 3181 * Check that 3182 * the redirect was not from ourselves 3183 * the new gateway and the old gateway are directly reachable 3184 */ 3185 if (!prev_ire || 3186 !ire || 3187 ire->ire_type == IRE_LOCAL) { 3188 BUMP_MIB(&icmp_mib, icmpInBadRedirects); 3189 freemsg(mp); 3190 if (ire != NULL) 3191 ire_refrele(ire); 3192 if (prev_ire != NULL) 3193 ire_refrele(prev_ire); 3194 return; 3195 } 3196 3197 /* 3198 * Should we use the old ULP info to create the new gateway? From 3199 * a user's perspective, we should inherit the info so that it 3200 * is a "smooth" transition. If we do not do that, then new 3201 * connections going thru the new gateway will have no route metrics, 3202 * which is counter-intuitive to user. From a network point of 3203 * view, this may or may not make sense even though the new gateway 3204 * is still directly connected to us so the route metrics should not 3205 * change much. 3206 * 3207 * But if the old ire_uinfo is not initialized, we do another 3208 * recursive lookup on the dest using the new gateway. There may 3209 * be a route to that. If so, use it to initialize the redirect 3210 * route. 3211 */ 3212 if (prev_ire->ire_uinfo.iulp_set) { 3213 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3214 } else { 3215 ire_t *tmp_ire; 3216 ire_t *sire; 3217 3218 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 3219 ALL_ZONES, 0, NULL, 3220 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT)); 3221 if (sire != NULL) { 3222 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 3223 /* 3224 * If sire != NULL, ire_ftable_lookup() should not 3225 * return a NULL value. 3226 */ 3227 ASSERT(tmp_ire != NULL); 3228 ire_refrele(tmp_ire); 3229 ire_refrele(sire); 3230 } else if (tmp_ire != NULL) { 3231 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 3232 sizeof (iulp_t)); 3233 ire_refrele(tmp_ire); 3234 } 3235 } 3236 if (prev_ire->ire_type == IRE_CACHE) 3237 ire_delete(prev_ire); 3238 ire_refrele(prev_ire); 3239 /* 3240 * TODO: more precise handling for cases 0, 2, 3, the latter two 3241 * require TOS routing 3242 */ 3243 switch (icmph->icmph_code) { 3244 case 0: 3245 case 1: 3246 /* TODO: TOS specificity for cases 2 and 3 */ 3247 case 2: 3248 case 3: 3249 break; 3250 default: 3251 freemsg(mp); 3252 BUMP_MIB(&icmp_mib, icmpInBadRedirects); 3253 ire_refrele(ire); 3254 return; 3255 } 3256 /* 3257 * Create a Route Association. This will allow us to remember that 3258 * someone we believe told us to use the particular gateway. 3259 */ 3260 save_ire = ire; 3261 ire = ire_create( 3262 (uchar_t *)&dst, /* dest addr */ 3263 (uchar_t *)&ip_g_all_ones, /* mask */ 3264 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 3265 (uchar_t *)&gateway, /* gateway addr */ 3266 NULL, /* no in_srcaddr */ 3267 &save_ire->ire_max_frag, /* max frag */ 3268 NULL, /* Fast Path header */ 3269 NULL, /* no rfq */ 3270 NULL, /* no stq */ 3271 IRE_HOST, 3272 NULL, 3273 NULL, 3274 NULL, 3275 0, 3276 0, 3277 0, 3278 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 3279 &ulp_info, 3280 NULL, 3281 NULL); 3282 3283 if (ire == NULL) { 3284 freemsg(mp); 3285 ire_refrele(save_ire); 3286 return; 3287 } 3288 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); 3289 ire_refrele(save_ire); 3290 atomic_inc_32(&ip_redirect_cnt); 3291 3292 if (error == 0) { 3293 ire_refrele(ire); /* Held in ire_add_v4 */ 3294 /* tell routing sockets that we received a redirect */ 3295 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 3296 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 3297 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR)); 3298 } 3299 3300 /* 3301 * Delete any existing IRE_HOST type redirect ires for this destination. 3302 * This together with the added IRE has the effect of 3303 * modifying an existing redirect. 3304 */ 3305 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL, 3306 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE)); 3307 if (prev_ire != NULL) { 3308 if (prev_ire ->ire_flags & RTF_DYNAMIC) 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, ipIfStatsOutDiscards); 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, ipIfStatsOutDiscards); 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, ipIfStatsOutDiscards); 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, ipIfStatsOutNoRoutes); 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, ipIfStatsOutNoRoutes); 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, ipIfStatsOutNoRoutes); 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, ipIfStatsInCksumErrs); 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 /* 3821 * No lock needed for moving or condemned check, as this is just an 3822 * optimization. 3823 */ 3824 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || 3825 (ipif->ipif_flags & IPIF_POINTOPOINT) || 3826 (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) { 3827 /* No reason to try to bring this address back. */ 3828 return; 3829 } 3830 3831 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL) 3832 goto alloc_fail; 3833 3834 if (ipif->ipif_arp_del_mp == NULL) { 3835 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) 3836 goto alloc_fail; 3837 ipif->ipif_arp_del_mp = arp_del_mp; 3838 } 3839 3840 /* Setting the 'unverified' flag restarts DAD */ 3841 area = (area_t *)arp_add_mp->b_rptr; 3842 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | 3843 ACE_F_UNVERIFIED; 3844 putnext(ill->ill_rq, arp_add_mp); 3845 return; 3846 3847 alloc_fail: 3848 /* 3849 * On allocation failure, just restart the timer. Note that the ipif 3850 * is down here, so no other thread could be trying to start a recovery 3851 * timer. The ill_lock protects the condemned flag and the recovery 3852 * timer ID. 3853 */ 3854 freemsg(arp_add_mp); 3855 mutex_enter(&ill->ill_lock); 3856 if (ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 && 3857 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 3858 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, 3859 MSEC_TO_TICK(ip_dup_recovery)); 3860 } 3861 mutex_exit(&ill->ill_lock); 3862 } 3863 3864 /* 3865 * This is for exclusive changes due to ARP. Either tear down an interface due 3866 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. 3867 */ 3868 /* ARGSUSED */ 3869 static void 3870 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3871 { 3872 ill_t *ill = rq->q_ptr; 3873 arh_t *arh; 3874 ipaddr_t src; 3875 ipif_t *ipif; 3876 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ 3877 char hbuf[MAC_STR_LEN]; 3878 char sbuf[INET_ADDRSTRLEN]; 3879 const char *failtype; 3880 boolean_t bring_up; 3881 3882 switch (((arcn_t *)mp->b_rptr)->arcn_code) { 3883 case AR_CN_READY: 3884 failtype = NULL; 3885 bring_up = B_TRUE; 3886 break; 3887 case AR_CN_FAILED: 3888 failtype = "in use"; 3889 bring_up = B_FALSE; 3890 break; 3891 default: 3892 failtype = "claimed"; 3893 bring_up = B_FALSE; 3894 break; 3895 } 3896 3897 arh = (arh_t *)mp->b_cont->b_rptr; 3898 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 3899 3900 /* Handle failures due to probes */ 3901 if (src == 0) { 3902 bcopy((char *)&arh[1] + 2 * arh->arh_hlen + IP_ADDR_LEN, &src, 3903 IP_ADDR_LEN); 3904 } 3905 3906 (void) strlcpy(ibuf, ill->ill_name, sizeof (ibuf)); 3907 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, 3908 sizeof (hbuf)); 3909 (void) ip_dot_addr(src, sbuf); 3910 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 3911 3912 if ((ipif->ipif_flags & IPIF_POINTOPOINT) || 3913 ipif->ipif_lcl_addr != src) { 3914 continue; 3915 } 3916 3917 /* 3918 * If we failed on a recovery probe, then restart the timer to 3919 * try again later. 3920 */ 3921 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) && 3922 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3923 ill->ill_net_type == IRE_IF_RESOLVER && 3924 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3925 ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0) { 3926 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3927 ipif, MSEC_TO_TICK(ip_dup_recovery)); 3928 continue; 3929 } 3930 3931 /* 3932 * If what we're trying to do has already been done, then do 3933 * nothing. 3934 */ 3935 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0)) 3936 continue; 3937 3938 if (ipif->ipif_id != 0) { 3939 (void) snprintf(ibuf + ill->ill_name_length - 1, 3940 sizeof (ibuf) - ill->ill_name_length + 1, ":%d", 3941 ipif->ipif_id); 3942 } 3943 if (failtype == NULL) { 3944 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf, 3945 ibuf); 3946 } else { 3947 cmn_err(CE_WARN, "%s has duplicate address %s (%s " 3948 "by %s); disabled", ibuf, sbuf, failtype, hbuf); 3949 } 3950 3951 if (bring_up) { 3952 ASSERT(ill->ill_dl_up); 3953 /* 3954 * Free up the ARP delete message so we can allocate 3955 * a fresh one through the normal path. 3956 */ 3957 freemsg(ipif->ipif_arp_del_mp); 3958 ipif->ipif_arp_del_mp = NULL; 3959 if (ipif_resolver_up(ipif, Res_act_initial) != 3960 EINPROGRESS) { 3961 ipif->ipif_addr_ready = 1; 3962 (void) ipif_up_done(ipif); 3963 } 3964 continue; 3965 } 3966 3967 mutex_enter(&ill->ill_lock); 3968 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 3969 ipif->ipif_flags |= IPIF_DUPLICATE; 3970 ill->ill_ipif_dup_count++; 3971 mutex_exit(&ill->ill_lock); 3972 /* 3973 * Already exclusive on the ill; no need to handle deferred 3974 * processing here. 3975 */ 3976 (void) ipif_down(ipif, NULL, NULL); 3977 ipif_down_tail(ipif); 3978 mutex_enter(&ill->ill_lock); 3979 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && 3980 ill->ill_net_type == IRE_IF_RESOLVER && 3981 !(ipif->ipif_state_flags & IPIF_CONDEMNED) && 3982 ip_dup_recovery > 0) { 3983 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, 3984 ipif, MSEC_TO_TICK(ip_dup_recovery)); 3985 } 3986 mutex_exit(&ill->ill_lock); 3987 } 3988 freemsg(mp); 3989 } 3990 3991 /* ARGSUSED */ 3992 static void 3993 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) 3994 { 3995 ill_t *ill = rq->q_ptr; 3996 arh_t *arh; 3997 ipaddr_t src; 3998 ipif_t *ipif; 3999 4000 arh = (arh_t *)mp->b_cont->b_rptr; 4001 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); 4002 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 4003 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) 4004 (void) ipif_resolver_up(ipif, Res_act_defend); 4005 } 4006 freemsg(mp); 4007 } 4008 4009 /* 4010 * News from ARP. ARP sends notification of interesting events down 4011 * to its clients using M_CTL messages with the interesting ARP packet 4012 * attached via b_cont. 4013 * The interesting event from a device comes up the corresponding ARP-IP-DEV 4014 * queue as opposed to ARP sending the message to all the clients, i.e. all 4015 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 4016 * table if a cache IRE is found to delete all the entries for the address in 4017 * the packet. 4018 */ 4019 static void 4020 ip_arp_news(queue_t *q, mblk_t *mp) 4021 { 4022 arcn_t *arcn; 4023 arh_t *arh; 4024 ire_t *ire = NULL; 4025 char hbuf[MAC_STR_LEN]; 4026 char sbuf[INET_ADDRSTRLEN]; 4027 ipaddr_t src; 4028 in6_addr_t v6src; 4029 boolean_t isv6 = B_FALSE; 4030 ipif_t *ipif; 4031 ill_t *ill; 4032 4033 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 4034 if (q->q_next) { 4035 putnext(q, mp); 4036 } else 4037 freemsg(mp); 4038 return; 4039 } 4040 arh = (arh_t *)mp->b_cont->b_rptr; 4041 /* Is it one we are interested in? */ 4042 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 4043 isv6 = B_TRUE; 4044 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 4045 IPV6_ADDR_LEN); 4046 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 4047 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 4048 IP_ADDR_LEN); 4049 } else { 4050 freemsg(mp); 4051 return; 4052 } 4053 4054 ill = q->q_ptr; 4055 4056 arcn = (arcn_t *)mp->b_rptr; 4057 switch (arcn->arcn_code) { 4058 case AR_CN_BOGON: 4059 /* 4060 * Someone is sending ARP packets with a source protocol 4061 * address that we have published and for which we believe our 4062 * entry is authoritative and (when ill_arp_extend is set) 4063 * verified to be unique on the network. 4064 * 4065 * The ARP module internally handles the cases where the sender 4066 * is just probing (for DAD) and where the hardware address of 4067 * a non-authoritative entry has changed. Thus, these are the 4068 * real conflicts, and we have to do resolution. 4069 * 4070 * We back away quickly from the address if it's from DHCP or 4071 * otherwise temporary and hasn't been used recently (or at 4072 * all). We'd like to include "deprecated" addresses here as 4073 * well (as there's no real reason to defend something we're 4074 * discarding), but IPMP "reuses" this flag to mean something 4075 * other than the standard meaning. 4076 * 4077 * If the ARP module above is not extended (meaning that it 4078 * doesn't know how to defend the address), then we just log 4079 * the problem as we always did and continue on. It's not 4080 * right, but there's little else we can do, and those old ATM 4081 * users are going away anyway. 4082 */ 4083 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, 4084 hbuf, sizeof (hbuf)); 4085 (void) ip_dot_addr(src, sbuf); 4086 if (isv6) 4087 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL); 4088 else 4089 ire = ire_cache_lookup(src, ALL_ZONES, NULL); 4090 4091 if (ire != NULL && IRE_IS_LOCAL(ire)) { 4092 uint32_t now; 4093 uint32_t maxage; 4094 clock_t lused; 4095 uint_t maxdefense; 4096 uint_t defs; 4097 4098 /* 4099 * First, figure out if this address hasn't been used 4100 * in a while. If it hasn't, then it's a better 4101 * candidate for abandoning. 4102 */ 4103 ipif = ire->ire_ipif; 4104 ASSERT(ipif != NULL); 4105 now = gethrestime_sec(); 4106 maxage = now - ire->ire_create_time; 4107 if (maxage > ip_max_temp_idle) 4108 maxage = ip_max_temp_idle; 4109 lused = drv_hztousec(ddi_get_lbolt() - 4110 ire->ire_last_used_time) / MICROSEC + 1; 4111 if (lused >= maxage && (ipif->ipif_flags & 4112 (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) 4113 maxdefense = ip_max_temp_defend; 4114 else 4115 maxdefense = ip_max_defend; 4116 4117 /* 4118 * Now figure out how many times we've defended 4119 * ourselves. Ignore defenses that happened long in 4120 * the past. 4121 */ 4122 mutex_enter(&ire->ire_lock); 4123 if ((defs = ire->ire_defense_count) > 0 && 4124 now - ire->ire_defense_time > ip_defend_interval) { 4125 ire->ire_defense_count = defs = 0; 4126 } 4127 ire->ire_defense_count++; 4128 ire->ire_defense_time = now; 4129 mutex_exit(&ire->ire_lock); 4130 ill_refhold(ill); 4131 ire_refrele(ire); 4132 4133 /* 4134 * If we've defended ourselves too many times already, 4135 * then give up and tear down the interface(s) using 4136 * this address. Otherwise, defend by sending out a 4137 * gratuitous ARP. 4138 */ 4139 if (defs >= maxdefense && ill->ill_arp_extend) { 4140 (void) qwriter_ip(NULL, ill, q, mp, 4141 ip_arp_excl, CUR_OP, B_FALSE); 4142 } else { 4143 cmn_err(CE_WARN, 4144 "node %s is using our IP address %s on %s", 4145 hbuf, sbuf, ill->ill_name); 4146 /* 4147 * If this is an old (ATM) ARP module, then 4148 * don't try to defend the address. Remain 4149 * compatible with the old behavior. Defend 4150 * only with new ARP. 4151 */ 4152 if (ill->ill_arp_extend) { 4153 (void) qwriter_ip(NULL, ill, q, mp, 4154 ip_arp_defend, CUR_OP, B_FALSE); 4155 } else { 4156 ill_refrele(ill); 4157 } 4158 } 4159 return; 4160 } 4161 cmn_err(CE_WARN, 4162 "proxy ARP problem? Node '%s' is using %s on %s", 4163 hbuf, sbuf, ill->ill_name); 4164 if (ire != NULL) 4165 ire_refrele(ire); 4166 break; 4167 case AR_CN_ANNOUNCE: 4168 if (isv6) { 4169 /* 4170 * For XRESOLV interfaces. 4171 * Delete the IRE cache entry and NCE for this 4172 * v6 address 4173 */ 4174 ip_ire_clookup_and_delete_v6(&v6src); 4175 /* 4176 * If v6src is a non-zero, it's a router address 4177 * as below. Do the same sort of thing to clean 4178 * out off-net IRE_CACHE entries that go through 4179 * the router. 4180 */ 4181 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 4182 ire_walk_v6(ire_delete_cache_gw_v6, 4183 (char *)&v6src, ALL_ZONES); 4184 } 4185 } else { 4186 nce_hw_map_t hwm; 4187 4188 /* 4189 * ARP gives us a copy of any packet where it thinks 4190 * the address has changed, so that we can update our 4191 * caches. We're responsible for caching known answers 4192 * in the current design. We check whether the 4193 * hardware address really has changed in all of our 4194 * entries that have cached this mapping, and if so, we 4195 * blow them away. This way we will immediately pick 4196 * up the rare case of a host changing hardware 4197 * address. 4198 */ 4199 if (src == 0) 4200 break; 4201 hwm.hwm_addr = src; 4202 hwm.hwm_hwlen = arh->arh_hlen; 4203 hwm.hwm_hwaddr = (uchar_t *)(arh + 1); 4204 ndp_walk_common(&ndp4, NULL, 4205 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); 4206 } 4207 break; 4208 case AR_CN_READY: 4209 /* No external v6 resolver has a contract to use this */ 4210 if (isv6) 4211 break; 4212 /* If the link is down, we'll retry this later */ 4213 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) 4214 break; 4215 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, 4216 NULL, NULL); 4217 if (ipif != NULL) { 4218 /* 4219 * If this is a duplicate recovery, then we now need to 4220 * go exclusive to bring this thing back up. 4221 */ 4222 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == 4223 IPIF_DUPLICATE) { 4224 ipif_refrele(ipif); 4225 ill_refhold(ill); 4226 (void) qwriter_ip(NULL, ill, q, mp, 4227 ip_arp_excl, CUR_OP, B_FALSE); 4228 return; 4229 } 4230 /* 4231 * If this is the first notice that this address is 4232 * ready, then let the user know now. 4233 */ 4234 if ((ipif->ipif_flags & IPIF_UP) && 4235 !ipif->ipif_addr_ready) { 4236 ipif_mask_reply(ipif); 4237 ip_rts_ifmsg(ipif); 4238 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 4239 sctp_update_ipif(ipif, SCTP_IPIF_UP); 4240 } 4241 ipif->ipif_addr_ready = 1; 4242 ipif_refrele(ipif); 4243 } 4244 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp)); 4245 if (ire != NULL) { 4246 ire->ire_defense_count = 0; 4247 ire_refrele(ire); 4248 } 4249 break; 4250 case AR_CN_FAILED: 4251 /* No external v6 resolver has a contract to use this */ 4252 if (isv6) 4253 break; 4254 ill_refhold(ill); 4255 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_excl, CUR_OP, 4256 B_FALSE); 4257 return; 4258 } 4259 freemsg(mp); 4260 } 4261 4262 /* 4263 * Create a mblk suitable for carrying the interface index and/or source link 4264 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 4265 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 4266 * application. 4267 */ 4268 mblk_t * 4269 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid) 4270 { 4271 mblk_t *mp; 4272 ip_pktinfo_t *pinfo; 4273 ipha_t *ipha; 4274 struct ether_header *pether; 4275 4276 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED); 4277 if (mp == NULL) { 4278 ip1dbg(("ip_add_info: allocation failure.\n")); 4279 return (data_mp); 4280 } 4281 4282 ipha = (ipha_t *)data_mp->b_rptr; 4283 pinfo = (ip_pktinfo_t *)mp->b_rptr; 4284 bzero(pinfo, sizeof (ip_pktinfo_t)); 4285 pinfo->ip_pkt_flags = (uchar_t)flags; 4286 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 4287 4288 if (flags & (IPF_RECVIF | IPF_RECVADDR)) 4289 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 4290 if (flags & IPF_RECVADDR) { 4291 ipif_t *ipif; 4292 ire_t *ire; 4293 4294 /* 4295 * Only valid for V4 4296 */ 4297 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) == 4298 (IPV4_VERSION << 4)); 4299 4300 ipif = ipif_get_next_ipif(NULL, ill); 4301 if (ipif != NULL) { 4302 /* 4303 * Since a decision has already been made to deliver the 4304 * packet, there is no need to test for SECATTR and 4305 * ZONEONLY. 4306 */ 4307 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 0, ipif, 4308 zoneid, NULL, MATCH_IRE_ILL_GROUP); 4309 if (ire == NULL) { 4310 /* 4311 * packet must have come on a different 4312 * interface. 4313 * Since a decision has already been made to 4314 * deliver the packet, there is no need to test 4315 * for SECATTR and ZONEONLY. 4316 */ 4317 ire = ire_ctable_lookup(ipha->ipha_dst, 0, 0, 4318 ipif, zoneid, NULL, NULL); 4319 } 4320 4321 if (ire == NULL) { 4322 /* 4323 * This is either a multicast packet or 4324 * the address has been removed since 4325 * the packet was received. 4326 * Return INADDR_ANY so that normal source 4327 * selection occurs for the response. 4328 */ 4329 4330 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4331 } else { 4332 ASSERT(ire->ire_type != IRE_CACHE); 4333 pinfo->ip_pkt_match_addr.s_addr = 4334 ire->ire_src_addr; 4335 ire_refrele(ire); 4336 } 4337 ipif_refrele(ipif); 4338 } else { 4339 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; 4340 } 4341 } 4342 4343 pether = (struct ether_header *)((char *)ipha 4344 - sizeof (struct ether_header)); 4345 /* 4346 * Make sure the interface is an ethernet type, since this option 4347 * is currently supported only on this type of interface. Also make 4348 * sure we are pointing correctly above db_base. 4349 */ 4350 4351 if ((flags & IPF_RECVSLLA) && 4352 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 4353 (ill->ill_type == IFT_ETHER) && 4354 (ill->ill_net_type == IRE_IF_RESOLVER)) { 4355 4356 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER; 4357 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, 4358 (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL); 4359 } else { 4360 /* 4361 * Clear the bit. Indicate to upper layer that IP is not 4362 * sending this ancillary info. 4363 */ 4364 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA; 4365 } 4366 4367 mp->b_datap->db_type = M_CTL; 4368 mp->b_wptr += sizeof (ip_pktinfo_t); 4369 mp->b_cont = data_mp; 4370 4371 return (mp); 4372 } 4373 4374 /* 4375 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 4376 * part of the bind request. 4377 */ 4378 4379 boolean_t 4380 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 4381 { 4382 ipsec_in_t *ii; 4383 4384 ASSERT(policy_mp != NULL); 4385 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 4386 4387 ii = (ipsec_in_t *)policy_mp->b_rptr; 4388 ASSERT(ii->ipsec_in_type == IPSEC_IN); 4389 4390 connp->conn_policy = ii->ipsec_in_policy; 4391 ii->ipsec_in_policy = NULL; 4392 4393 if (ii->ipsec_in_action != NULL) { 4394 if (connp->conn_latch == NULL) { 4395 connp->conn_latch = iplatch_create(); 4396 if (connp->conn_latch == NULL) 4397 return (B_FALSE); 4398 } 4399 ipsec_latch_inbound(connp->conn_latch, ii); 4400 } 4401 return (B_TRUE); 4402 } 4403 4404 /* 4405 * Upper level protocols (ULP) pass through bind requests to IP for inspection 4406 * and to arrange for power-fanout assist. The ULP is identified by 4407 * adding a single byte at the end of the original bind message. 4408 * A ULP other than UDP or TCP that wishes to be recognized passes 4409 * down a bind with a zero length address. 4410 * 4411 * The binding works as follows: 4412 * - A zero byte address means just bind to the protocol. 4413 * - A four byte address is treated as a request to validate 4414 * that the address is a valid local address, appropriate for 4415 * an application to bind to. This does not affect any fanout 4416 * information in IP. 4417 * - A sizeof sin_t byte address is used to bind to only the local address 4418 * and port. 4419 * - A sizeof ipa_conn_t byte address contains complete fanout information 4420 * consisting of local and remote addresses and ports. In 4421 * this case, the addresses are both validated as appropriate 4422 * for this operation, and, if so, the information is retained 4423 * for use in the inbound fanout. 4424 * 4425 * The ULP (except in the zero-length bind) can append an 4426 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 4427 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 4428 * a copy of the source or destination IRE (source for local bind; 4429 * destination for complete bind). IPSEC_POLICY_SET indicates that the 4430 * policy information contained should be copied on to the conn. 4431 * 4432 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 4433 */ 4434 mblk_t * 4435 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 4436 { 4437 ssize_t len; 4438 struct T_bind_req *tbr; 4439 sin_t *sin; 4440 ipa_conn_t *ac; 4441 uchar_t *ucp; 4442 mblk_t *mp1; 4443 boolean_t ire_requested; 4444 boolean_t ipsec_policy_set = B_FALSE; 4445 int error = 0; 4446 int protocol; 4447 ipa_conn_x_t *acx; 4448 4449 ASSERT(!connp->conn_af_isv6); 4450 connp->conn_pkt_isv6 = B_FALSE; 4451 4452 len = MBLKL(mp); 4453 if (len < (sizeof (*tbr) + 1)) { 4454 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 4455 "ip_bind: bogus msg, len %ld", len); 4456 /* XXX: Need to return something better */ 4457 goto bad_addr; 4458 } 4459 /* Back up and extract the protocol identifier. */ 4460 mp->b_wptr--; 4461 protocol = *mp->b_wptr & 0xFF; 4462 tbr = (struct T_bind_req *)mp->b_rptr; 4463 /* Reset the message type in preparation for shipping it back. */ 4464 DB_TYPE(mp) = M_PCPROTO; 4465 4466 connp->conn_ulp = (uint8_t)protocol; 4467 4468 /* 4469 * Check for a zero length address. This is from a protocol that 4470 * wants to register to receive all packets of its type. 4471 */ 4472 if (tbr->ADDR_length == 0) { 4473 /* 4474 * These protocols are now intercepted in ip_bind_v6(). 4475 * Reject protocol-level binds here for now. 4476 * 4477 * For SCTP raw socket, ICMP sends down a bind with sin_t 4478 * so that the protocol type cannot be SCTP. 4479 */ 4480 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 4481 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 4482 goto bad_addr; 4483 } 4484 4485 /* 4486 * 4487 * The udp module never sends down a zero-length address, 4488 * and allowing this on a labeled system will break MLP 4489 * functionality. 4490 */ 4491 if (is_system_labeled() && protocol == IPPROTO_UDP) 4492 goto bad_addr; 4493 4494 if (connp->conn_mac_exempt) 4495 goto bad_addr; 4496 4497 /* No hash here really. The table is big enough. */ 4498 connp->conn_srcv6 = ipv6_all_zeros; 4499 4500 ipcl_proto_insert(connp, protocol); 4501 4502 tbr->PRIM_type = T_BIND_ACK; 4503 return (mp); 4504 } 4505 4506 /* Extract the address pointer from the message. */ 4507 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 4508 tbr->ADDR_length); 4509 if (ucp == NULL) { 4510 ip1dbg(("ip_bind: no address\n")); 4511 goto bad_addr; 4512 } 4513 if (!OK_32PTR(ucp)) { 4514 ip1dbg(("ip_bind: unaligned address\n")); 4515 goto bad_addr; 4516 } 4517 /* 4518 * Check for trailing mps. 4519 */ 4520 4521 mp1 = mp->b_cont; 4522 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 4523 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); 4524 4525 switch (tbr->ADDR_length) { 4526 default: 4527 ip1dbg(("ip_bind: bad address length %d\n", 4528 (int)tbr->ADDR_length)); 4529 goto bad_addr; 4530 4531 case IP_ADDR_LEN: 4532 /* Verification of local address only */ 4533 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, 4534 ire_requested, ipsec_policy_set, B_FALSE); 4535 break; 4536 4537 case sizeof (sin_t): 4538 sin = (sin_t *)ucp; 4539 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, 4540 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); 4541 break; 4542 4543 case sizeof (ipa_conn_t): 4544 ac = (ipa_conn_t *)ucp; 4545 /* For raw socket, the local port is not set. */ 4546 if (ac->ac_lport == 0) 4547 ac->ac_lport = connp->conn_lport; 4548 /* Always verify destination reachability. */ 4549 error = ip_bind_connected(connp, mp, &ac->ac_laddr, 4550 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, 4551 ipsec_policy_set, B_TRUE, B_TRUE); 4552 break; 4553 4554 case sizeof (ipa_conn_x_t): 4555 acx = (ipa_conn_x_t *)ucp; 4556 /* 4557 * Whether or not to verify destination reachability depends 4558 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 4559 */ 4560 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, 4561 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, 4562 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, 4563 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); 4564 break; 4565 } 4566 if (error == EINPROGRESS) 4567 return (NULL); 4568 else if (error != 0) 4569 goto bad_addr; 4570 /* 4571 * Pass the IPSEC headers size in ire_ipsec_overhead. 4572 * We can't do this in ip_bind_insert_ire because the policy 4573 * may not have been inherited at that point in time and hence 4574 * conn_out_enforce_policy may not be set. 4575 */ 4576 mp1 = mp->b_cont; 4577 if (ire_requested && connp->conn_out_enforce_policy && 4578 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 4579 ire_t *ire = (ire_t *)mp1->b_rptr; 4580 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 4581 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 4582 } 4583 4584 /* Send it home. */ 4585 mp->b_datap->db_type = M_PCPROTO; 4586 tbr->PRIM_type = T_BIND_ACK; 4587 return (mp); 4588 4589 bad_addr: 4590 /* 4591 * If error = -1 then we generate a TBADADDR - otherwise error is 4592 * a unix errno. 4593 */ 4594 if (error > 0) 4595 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 4596 else 4597 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 4598 return (mp); 4599 } 4600 4601 /* 4602 * Here address is verified to be a valid local address. 4603 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 4604 * address is also considered a valid local address. 4605 * In the case of a broadcast/multicast address, however, the 4606 * upper protocol is expected to reset the src address 4607 * to 0 if it sees a IRE_BROADCAST type returned so that 4608 * no packets are emitted with broadcast/multicast address as 4609 * source address (that violates hosts requirements RFC1122) 4610 * The addresses valid for bind are: 4611 * (1) - INADDR_ANY (0) 4612 * (2) - IP address of an UP interface 4613 * (3) - IP address of a DOWN interface 4614 * (4) - valid local IP broadcast addresses. In this case 4615 * the conn will only receive packets destined to 4616 * the specified broadcast address. 4617 * (5) - a multicast address. In this case 4618 * the conn will only receive packets destined to 4619 * the specified multicast address. Note: the 4620 * application still has to issue an 4621 * IP_ADD_MEMBERSHIP socket option. 4622 * 4623 * On error, return -1 for TBADADDR otherwise pass the 4624 * errno with TSYSERR reply. 4625 * 4626 * In all the above cases, the bound address must be valid in the current zone. 4627 * When the address is loopback, multicast or broadcast, there might be many 4628 * matching IREs so bind has to look up based on the zone. 4629 * 4630 * Note: lport is in network byte order. 4631 */ 4632 int 4633 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, 4634 boolean_t ire_requested, boolean_t ipsec_policy_set, 4635 boolean_t fanout_insert) 4636 { 4637 int error = 0; 4638 ire_t *src_ire; 4639 mblk_t *policy_mp; 4640 ipif_t *ipif; 4641 zoneid_t zoneid; 4642 4643 if (ipsec_policy_set) { 4644 policy_mp = mp->b_cont; 4645 } 4646 4647 /* 4648 * If it was previously connected, conn_fully_bound would have 4649 * been set. 4650 */ 4651 connp->conn_fully_bound = B_FALSE; 4652 4653 src_ire = NULL; 4654 ipif = NULL; 4655 4656 zoneid = IPCL_ZONEID(connp); 4657 4658 if (src_addr) { 4659 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 4660 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY); 4661 /* 4662 * If an address other than 0.0.0.0 is requested, 4663 * we verify that it is a valid address for bind 4664 * Note: Following code is in if-else-if form for 4665 * readability compared to a condition check. 4666 */ 4667 /* LINTED - statement has no consequent */ 4668 if (IRE_IS_LOCAL(src_ire)) { 4669 /* 4670 * (2) Bind to address of local UP interface 4671 */ 4672 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 4673 /* 4674 * (4) Bind to broadcast address 4675 * Note: permitted only from transports that 4676 * request IRE 4677 */ 4678 if (!ire_requested) 4679 error = EADDRNOTAVAIL; 4680 } else { 4681 /* 4682 * (3) Bind to address of local DOWN interface 4683 * (ipif_lookup_addr() looks up all interfaces 4684 * but we do not get here for UP interfaces 4685 * - case (2) above) 4686 * We put the protocol byte back into the mblk 4687 * since we may come back via ip_wput_nondata() 4688 * later with this mblk if ipif_lookup_addr chooses 4689 * to defer processing. 4690 */ 4691 *mp->b_wptr++ = (char)connp->conn_ulp; 4692 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, 4693 CONNP_TO_WQ(connp), mp, ip_wput_nondata, 4694 &error)) != NULL) { 4695 ipif_refrele(ipif); 4696 } else if (error == EINPROGRESS) { 4697 if (src_ire != NULL) 4698 ire_refrele(src_ire); 4699 return (EINPROGRESS); 4700 } else if (CLASSD(src_addr)) { 4701 error = 0; 4702 if (src_ire != NULL) 4703 ire_refrele(src_ire); 4704 /* 4705 * (5) bind to multicast address. 4706 * Fake out the IRE returned to upper 4707 * layer to be a broadcast IRE. 4708 */ 4709 src_ire = ire_ctable_lookup( 4710 INADDR_BROADCAST, INADDR_ANY, 4711 IRE_BROADCAST, NULL, zoneid, NULL, 4712 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY)); 4713 if (src_ire == NULL || !ire_requested) 4714 error = EADDRNOTAVAIL; 4715 } else { 4716 /* 4717 * Not a valid address for bind 4718 */ 4719 error = EADDRNOTAVAIL; 4720 } 4721 /* 4722 * Just to keep it consistent with the processing in 4723 * ip_bind_v4() 4724 */ 4725 mp->b_wptr--; 4726 } 4727 if (error) { 4728 /* Red Alert! Attempting to be a bogon! */ 4729 ip1dbg(("ip_bind: bad src address 0x%x\n", 4730 ntohl(src_addr))); 4731 goto bad_addr; 4732 } 4733 } 4734 4735 /* 4736 * Allow setting new policies. For example, disconnects come 4737 * down as ipa_t bind. As we would have set conn_policy_cached 4738 * to B_TRUE before, we should set it to B_FALSE, so that policy 4739 * can change after the disconnect. 4740 */ 4741 connp->conn_policy_cached = B_FALSE; 4742 4743 /* 4744 * If not fanout_insert this was just an address verification 4745 */ 4746 if (fanout_insert) { 4747 /* 4748 * The addresses have been verified. Time to insert in 4749 * the correct fanout list. 4750 */ 4751 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4752 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4753 connp->conn_lport = lport; 4754 connp->conn_fport = 0; 4755 /* 4756 * Do we need to add a check to reject Multicast packets 4757 * 4758 * We need to make sure that the conn_recv is set to a non-null 4759 * value before we insert the conn into the classifier table. 4760 * This is to avoid a race with an incoming packet which does an 4761 * ipcl_classify(). 4762 */ 4763 if (*mp->b_wptr == IPPROTO_TCP) 4764 connp->conn_recv = tcp_conn_request; 4765 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); 4766 } 4767 4768 if (error == 0) { 4769 if (ire_requested) { 4770 if (!ip_bind_insert_ire(mp, src_ire, NULL)) { 4771 error = -1; 4772 /* Falls through to bad_addr */ 4773 } 4774 } else if (ipsec_policy_set) { 4775 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4776 error = -1; 4777 /* Falls through to bad_addr */ 4778 } 4779 } 4780 } else if (connp->conn_ulp == IPPROTO_TCP) { 4781 connp->conn_recv = tcp_input; 4782 } 4783 bad_addr: 4784 if (error != 0) { 4785 if (connp->conn_anon_port) { 4786 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4787 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 4788 B_FALSE); 4789 } 4790 connp->conn_mlp_type = mlptSingle; 4791 } 4792 if (src_ire != NULL) 4793 IRE_REFRELE(src_ire); 4794 if (ipsec_policy_set) { 4795 ASSERT(policy_mp == mp->b_cont); 4796 ASSERT(policy_mp != NULL); 4797 freeb(policy_mp); 4798 /* 4799 * As of now assume that nothing else accompanies 4800 * IPSEC_POLICY_SET. 4801 */ 4802 mp->b_cont = NULL; 4803 } 4804 return (error); 4805 } 4806 4807 /* 4808 * Verify that both the source and destination addresses 4809 * are valid. If verify_dst is false, then the destination address may be 4810 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4811 * destination reachability, while tunnels do not. 4812 * Note that we allow connect to broadcast and multicast 4813 * addresses when ire_requested is set. Thus the ULP 4814 * has to check for IRE_BROADCAST and multicast. 4815 * 4816 * Returns zero if ok. 4817 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4818 * (for use with TSYSERR reply). 4819 * 4820 * Note: lport and fport are in network byte order. 4821 */ 4822 int 4823 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, 4824 uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4825 boolean_t ire_requested, boolean_t ipsec_policy_set, 4826 boolean_t fanout_insert, boolean_t verify_dst) 4827 { 4828 ire_t *src_ire; 4829 ire_t *dst_ire; 4830 int error = 0; 4831 int protocol; 4832 mblk_t *policy_mp; 4833 ire_t *sire = NULL; 4834 ire_t *md_dst_ire = NULL; 4835 ire_t *lso_dst_ire = NULL; 4836 ill_t *ill = NULL; 4837 zoneid_t zoneid; 4838 ipaddr_t src_addr = *src_addrp; 4839 4840 src_ire = dst_ire = NULL; 4841 protocol = *mp->b_wptr & 0xFF; 4842 4843 /* 4844 * If we never got a disconnect before, clear it now. 4845 */ 4846 connp->conn_fully_bound = B_FALSE; 4847 4848 if (ipsec_policy_set) { 4849 policy_mp = mp->b_cont; 4850 } 4851 4852 zoneid = IPCL_ZONEID(connp); 4853 4854 if (CLASSD(dst_addr)) { 4855 /* Pick up an IRE_BROADCAST */ 4856 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4857 NULL, zoneid, MBLK_GETLABEL(mp), 4858 (MATCH_IRE_RECURSIVE | 4859 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | 4860 MATCH_IRE_SECATTR)); 4861 } else { 4862 /* 4863 * If conn_dontroute is set or if conn_nexthop_set is set, 4864 * and onlink ipif is not found set ENETUNREACH error. 4865 */ 4866 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4867 ipif_t *ipif; 4868 4869 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4870 dst_addr : connp->conn_nexthop_v4, 4871 connp->conn_zoneid); 4872 if (ipif == NULL) { 4873 error = ENETUNREACH; 4874 goto bad_addr; 4875 } 4876 ipif_refrele(ipif); 4877 } 4878 4879 if (connp->conn_nexthop_set) { 4880 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4881 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), 4882 MATCH_IRE_SECATTR); 4883 } else { 4884 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4885 &sire, zoneid, MBLK_GETLABEL(mp), 4886 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4887 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | 4888 MATCH_IRE_SECATTR)); 4889 } 4890 } 4891 /* 4892 * dst_ire can't be a broadcast when not ire_requested. 4893 * We also prevent ire's with src address INADDR_ANY to 4894 * be used, which are created temporarily for 4895 * sending out packets from endpoints that have 4896 * conn_unspec_src set. If verify_dst is true, the destination must be 4897 * reachable. If verify_dst is false, the destination needn't be 4898 * reachable. 4899 * 4900 * If we match on a reject or black hole, then we've got a 4901 * local failure. May as well fail out the connect() attempt, 4902 * since it's never going to succeed. 4903 */ 4904 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4905 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4906 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4907 /* 4908 * If we're verifying destination reachability, we always want 4909 * to complain here. 4910 * 4911 * If we're not verifying destination reachability but the 4912 * destination has a route, we still want to fail on the 4913 * temporary address and broadcast address tests. 4914 */ 4915 if (verify_dst || (dst_ire != NULL)) { 4916 if (ip_debug > 2) { 4917 pr_addr_dbg("ip_bind_connected: bad connected " 4918 "dst %s\n", AF_INET, &dst_addr); 4919 } 4920 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4921 error = ENETUNREACH; 4922 else 4923 error = EHOSTUNREACH; 4924 goto bad_addr; 4925 } 4926 } 4927 4928 /* 4929 * We now know that routing will allow us to reach the destination. 4930 * Check whether Trusted Solaris policy allows communication with this 4931 * host, and pretend that the destination is unreachable if not. 4932 * 4933 * This is never a problem for TCP, since that transport is known to 4934 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 4935 * handling. If the remote is unreachable, it will be detected at that 4936 * point, so there's no reason to check it here. 4937 * 4938 * Note that for sendto (and other datagram-oriented friends), this 4939 * check is done as part of the data path label computation instead. 4940 * The check here is just to make non-TCP connect() report the right 4941 * error. 4942 */ 4943 if (dst_ire != NULL && is_system_labeled() && 4944 !IPCL_IS_TCP(connp) && 4945 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL, 4946 connp->conn_mac_exempt) != 0) { 4947 error = EHOSTUNREACH; 4948 if (ip_debug > 2) { 4949 pr_addr_dbg("ip_bind_connected: no label for dst %s\n", 4950 AF_INET, &dst_addr); 4951 } 4952 goto bad_addr; 4953 } 4954 4955 /* 4956 * If the app does a connect(), it means that it will most likely 4957 * send more than 1 packet to the destination. It makes sense 4958 * to clear the temporary flag. 4959 */ 4960 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4961 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4962 irb_t *irb = dst_ire->ire_bucket; 4963 4964 rw_enter(&irb->irb_lock, RW_WRITER); 4965 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4966 irb->irb_tmp_ire_cnt--; 4967 rw_exit(&irb->irb_lock); 4968 } 4969 4970 /* 4971 * See if we should notify ULP about LSO/MDT; we do this whether or not 4972 * ire_requested is TRUE, in order to handle active connects; LSO/MDT 4973 * eligibility tests for passive connects are handled separately 4974 * through tcp_adapt_ire(). We do this before the source address 4975 * selection, because dst_ire may change after a call to 4976 * ipif_select_source(). This is a best-effort check, as the 4977 * packet for this connection may not actually go through 4978 * dst_ire->ire_stq, and the exact IRE can only be known after 4979 * calling ip_newroute(). This is why we further check on the 4980 * IRE during LSO/Multidata packet transmission in 4981 * tcp_lsosend()/tcp_multisend(). 4982 */ 4983 if (!ipsec_policy_set && dst_ire != NULL && 4984 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4985 (ill = ire_to_ill(dst_ire), ill != NULL)) { 4986 if (ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 4987 lso_dst_ire = dst_ire; 4988 IRE_REFHOLD(lso_dst_ire); 4989 } else if (ip_multidata_outbound && ILL_MDT_CAPABLE(ill)) { 4990 md_dst_ire = dst_ire; 4991 IRE_REFHOLD(md_dst_ire); 4992 } 4993 } 4994 4995 if (dst_ire != NULL && 4996 dst_ire->ire_type == IRE_LOCAL && 4997 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { 4998 /* 4999 * If the IRE belongs to a different zone, look for a matching 5000 * route in the forwarding table and use the source address from 5001 * that route. 5002 */ 5003 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 5004 zoneid, 0, NULL, 5005 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 5006 MATCH_IRE_RJ_BHOLE); 5007 if (src_ire == NULL) { 5008 error = EHOSTUNREACH; 5009 goto bad_addr; 5010 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 5011 if (!(src_ire->ire_type & IRE_HOST)) 5012 error = ENETUNREACH; 5013 else 5014 error = EHOSTUNREACH; 5015 goto bad_addr; 5016 } 5017 if (src_addr == INADDR_ANY) 5018 src_addr = src_ire->ire_src_addr; 5019 ire_refrele(src_ire); 5020 src_ire = NULL; 5021 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 5022 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 5023 src_addr = sire->ire_src_addr; 5024 ire_refrele(dst_ire); 5025 dst_ire = sire; 5026 sire = NULL; 5027 } else { 5028 /* 5029 * Pick a source address so that a proper inbound 5030 * load spreading would happen. 5031 */ 5032 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; 5033 ipif_t *src_ipif = NULL; 5034 ire_t *ipif_ire; 5035 5036 /* 5037 * Supply a local source address such that inbound 5038 * load spreading happens. 5039 * 5040 * Determine the best source address on this ill for 5041 * the destination. 5042 * 5043 * 1) For broadcast, we should return a broadcast ire 5044 * found above so that upper layers know that the 5045 * destination address is a broadcast address. 5046 * 5047 * 2) If this is part of a group, select a better 5048 * source address so that better inbound load 5049 * balancing happens. Do the same if the ipif 5050 * is DEPRECATED. 5051 * 5052 * 3) If the outgoing interface is part of a usesrc 5053 * group, then try selecting a source address from 5054 * the usesrc ILL. 5055 */ 5056 if ((dst_ire->ire_zoneid != zoneid && 5057 dst_ire->ire_zoneid != ALL_ZONES) || 5058 (!(dst_ire->ire_type & IRE_BROADCAST) && 5059 ((dst_ill->ill_group != NULL) || 5060 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 5061 (dst_ill->ill_usesrc_ifindex != 0)))) { 5062 /* 5063 * If the destination is reachable via a 5064 * given gateway, the selected source address 5065 * should be in the same subnet as the gateway. 5066 * Otherwise, the destination is not reachable. 5067 * 5068 * If there are no interfaces on the same subnet 5069 * as the destination, ipif_select_source gives 5070 * first non-deprecated interface which might be 5071 * on a different subnet than the gateway. 5072 * This is not desirable. Hence pass the dst_ire 5073 * source address to ipif_select_source. 5074 * It is sure that the destination is reachable 5075 * with the dst_ire source address subnet. 5076 * So passing dst_ire source address to 5077 * ipif_select_source will make sure that the 5078 * selected source will be on the same subnet 5079 * as dst_ire source address. 5080 */ 5081 ipaddr_t saddr = 5082 dst_ire->ire_ipif->ipif_src_addr; 5083 src_ipif = ipif_select_source(dst_ill, 5084 saddr, zoneid); 5085 if (src_ipif != NULL) { 5086 if (IS_VNI(src_ipif->ipif_ill)) { 5087 /* 5088 * For VNI there is no 5089 * interface route 5090 */ 5091 src_addr = 5092 src_ipif->ipif_src_addr; 5093 } else { 5094 ipif_ire = 5095 ipif_to_ire(src_ipif); 5096 if (ipif_ire != NULL) { 5097 IRE_REFRELE(dst_ire); 5098 dst_ire = ipif_ire; 5099 } 5100 src_addr = 5101 dst_ire->ire_src_addr; 5102 } 5103 ipif_refrele(src_ipif); 5104 } else { 5105 src_addr = dst_ire->ire_src_addr; 5106 } 5107 } else { 5108 src_addr = dst_ire->ire_src_addr; 5109 } 5110 } 5111 } 5112 5113 /* 5114 * We do ire_route_lookup() here (and not 5115 * interface lookup as we assert that 5116 * src_addr should only come from an 5117 * UP interface for hard binding. 5118 */ 5119 ASSERT(src_ire == NULL); 5120 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 5121 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY); 5122 /* src_ire must be a local|loopback */ 5123 if (!IRE_IS_LOCAL(src_ire)) { 5124 if (ip_debug > 2) { 5125 pr_addr_dbg("ip_bind_connected: bad connected " 5126 "src %s\n", AF_INET, &src_addr); 5127 } 5128 error = EADDRNOTAVAIL; 5129 goto bad_addr; 5130 } 5131 5132 /* 5133 * If the source address is a loopback address, the 5134 * destination had best be local or multicast. 5135 * The transports that can't handle multicast will reject 5136 * those addresses. 5137 */ 5138 if (src_ire->ire_type == IRE_LOOPBACK && 5139 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 5140 ip1dbg(("ip_bind_connected: bad connected loopback\n")); 5141 error = -1; 5142 goto bad_addr; 5143 } 5144 5145 /* 5146 * Allow setting new policies. For example, disconnects come 5147 * down as ipa_t bind. As we would have set conn_policy_cached 5148 * to B_TRUE before, we should set it to B_FALSE, so that policy 5149 * can change after the disconnect. 5150 */ 5151 connp->conn_policy_cached = B_FALSE; 5152 5153 /* 5154 * Set the conn addresses/ports immediately, so the IPsec policy calls 5155 * can handle their passed-in conn's. 5156 */ 5157 5158 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 5159 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 5160 connp->conn_lport = lport; 5161 connp->conn_fport = fport; 5162 *src_addrp = src_addr; 5163 5164 ASSERT(!(ipsec_policy_set && ire_requested)); 5165 if (ire_requested) { 5166 iulp_t *ulp_info = NULL; 5167 5168 /* 5169 * Note that sire will not be NULL if this is an off-link 5170 * connection and there is not cache for that dest yet. 5171 * 5172 * XXX Because of an existing bug, if there are multiple 5173 * default routes, the IRE returned now may not be the actual 5174 * default route used (default routes are chosen in a 5175 * round robin fashion). So if the metrics for different 5176 * default routes are different, we may return the wrong 5177 * metrics. This will not be a problem if the existing 5178 * bug is fixed. 5179 */ 5180 if (sire != NULL) { 5181 ulp_info = &(sire->ire_uinfo); 5182 } 5183 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info)) { 5184 error = -1; 5185 goto bad_addr; 5186 } 5187 } else if (ipsec_policy_set) { 5188 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 5189 error = -1; 5190 goto bad_addr; 5191 } 5192 } 5193 5194 /* 5195 * Cache IPsec policy in this conn. If we have per-socket policy, 5196 * we'll cache that. If we don't, we'll inherit global policy. 5197 * 5198 * We can't insert until the conn reflects the policy. Note that 5199 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 5200 * connections where we don't have a policy. This is to prevent 5201 * global policy lookups in the inbound path. 5202 * 5203 * If we insert before we set conn_policy_cached, 5204 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 5205 * because global policy cound be non-empty. We normally call 5206 * ipsec_check_policy() for conn_policy_cached connections only if 5207 * ipc_in_enforce_policy is set. But in this case, 5208 * conn_policy_cached can get set anytime since we made the 5209 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 5210 * called, which will make the above assumption false. Thus, we 5211 * need to insert after we set conn_policy_cached. 5212 */ 5213 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 5214 goto bad_addr; 5215 5216 if (fanout_insert) { 5217 /* 5218 * The addresses have been verified. Time to insert in 5219 * the correct fanout list. 5220 * We need to make sure that the conn_recv is set to a non-null 5221 * value before we insert into the classifier table to avoid a 5222 * race with an incoming packet which does an ipcl_classify(). 5223 */ 5224 if (protocol == IPPROTO_TCP) 5225 connp->conn_recv = tcp_input; 5226 error = ipcl_conn_insert(connp, protocol, src_addr, 5227 dst_addr, connp->conn_ports); 5228 } 5229 5230 if (error == 0) { 5231 connp->conn_fully_bound = B_TRUE; 5232 /* 5233 * Our initial checks for LSO/MDT have passed; the IRE is not 5234 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 5235 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into 5236 * ip_xxinfo_return(), which performs further checks 5237 * against them and upon success, returns the LSO/MDT info 5238 * mblk which we will attach to the bind acknowledgment. 5239 */ 5240 if (lso_dst_ire != NULL) { 5241 mblk_t *lsoinfo_mp; 5242 5243 ASSERT(ill->ill_lso_capab != NULL); 5244 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp, 5245 ill->ill_name, ill->ill_lso_capab)) != NULL) 5246 linkb(mp, lsoinfo_mp); 5247 } else if (md_dst_ire != NULL) { 5248 mblk_t *mdinfo_mp; 5249 5250 ASSERT(ill->ill_mdt_capab != NULL); 5251 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 5252 ill->ill_name, ill->ill_mdt_capab)) != NULL) 5253 linkb(mp, mdinfo_mp); 5254 } 5255 } 5256 bad_addr: 5257 if (ipsec_policy_set) { 5258 ASSERT(policy_mp == mp->b_cont); 5259 ASSERT(policy_mp != NULL); 5260 freeb(policy_mp); 5261 /* 5262 * As of now assume that nothing else accompanies 5263 * IPSEC_POLICY_SET. 5264 */ 5265 mp->b_cont = NULL; 5266 } 5267 if (src_ire != NULL) 5268 IRE_REFRELE(src_ire); 5269 if (dst_ire != NULL) 5270 IRE_REFRELE(dst_ire); 5271 if (sire != NULL) 5272 IRE_REFRELE(sire); 5273 if (md_dst_ire != NULL) 5274 IRE_REFRELE(md_dst_ire); 5275 if (lso_dst_ire != NULL) 5276 IRE_REFRELE(lso_dst_ire); 5277 return (error); 5278 } 5279 5280 /* 5281 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 5282 * Prefers dst_ire over src_ire. 5283 */ 5284 static boolean_t 5285 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info) 5286 { 5287 mblk_t *mp1; 5288 ire_t *ret_ire = NULL; 5289 5290 mp1 = mp->b_cont; 5291 ASSERT(mp1 != NULL); 5292 5293 if (ire != NULL) { 5294 /* 5295 * mp1 initialized above to IRE_DB_REQ_TYPE 5296 * appended mblk. Its <upper protocol>'s 5297 * job to make sure there is room. 5298 */ 5299 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 5300 return (0); 5301 5302 mp1->b_datap->db_type = IRE_DB_TYPE; 5303 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 5304 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 5305 ret_ire = (ire_t *)mp1->b_rptr; 5306 /* 5307 * Pass the latest setting of the ip_path_mtu_discovery and 5308 * copy the ulp info if any. 5309 */ 5310 ret_ire->ire_frag_flag |= (ip_path_mtu_discovery) ? 5311 IPH_DF : 0; 5312 if (ulp_info != NULL) { 5313 bcopy(ulp_info, &(ret_ire->ire_uinfo), 5314 sizeof (iulp_t)); 5315 } 5316 ret_ire->ire_mp = mp1; 5317 } else { 5318 /* 5319 * No IRE was found. Remove IRE mblk. 5320 */ 5321 mp->b_cont = mp1->b_cont; 5322 freeb(mp1); 5323 } 5324 5325 return (1); 5326 } 5327 5328 /* 5329 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 5330 * the final piece where we don't. Return a pointer to the first mblk in the 5331 * result, and update the pointer to the next mblk to chew on. If anything 5332 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 5333 * NULL pointer. 5334 */ 5335 mblk_t * 5336 ip_carve_mp(mblk_t **mpp, ssize_t len) 5337 { 5338 mblk_t *mp0; 5339 mblk_t *mp1; 5340 mblk_t *mp2; 5341 5342 if (!len || !mpp || !(mp0 = *mpp)) 5343 return (NULL); 5344 /* If we aren't going to consume the first mblk, we need a dup. */ 5345 if (mp0->b_wptr - mp0->b_rptr > len) { 5346 mp1 = dupb(mp0); 5347 if (mp1) { 5348 /* Partition the data between the two mblks. */ 5349 mp1->b_wptr = mp1->b_rptr + len; 5350 mp0->b_rptr = mp1->b_wptr; 5351 /* 5352 * after adjustments if mblk not consumed is now 5353 * unaligned, try to align it. If this fails free 5354 * all messages and let upper layer recover. 5355 */ 5356 if (!OK_32PTR(mp0->b_rptr)) { 5357 if (!pullupmsg(mp0, -1)) { 5358 freemsg(mp0); 5359 freemsg(mp1); 5360 *mpp = NULL; 5361 return (NULL); 5362 } 5363 } 5364 } 5365 return (mp1); 5366 } 5367 /* Eat through as many mblks as we need to get len bytes. */ 5368 len -= mp0->b_wptr - mp0->b_rptr; 5369 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 5370 if (mp2->b_wptr - mp2->b_rptr > len) { 5371 /* 5372 * We won't consume the entire last mblk. Like 5373 * above, dup and partition it. 5374 */ 5375 mp1->b_cont = dupb(mp2); 5376 mp1 = mp1->b_cont; 5377 if (!mp1) { 5378 /* 5379 * Trouble. Rather than go to a lot of 5380 * trouble to clean up, we free the messages. 5381 * This won't be any worse than losing it on 5382 * the wire. 5383 */ 5384 freemsg(mp0); 5385 freemsg(mp2); 5386 *mpp = NULL; 5387 return (NULL); 5388 } 5389 mp1->b_wptr = mp1->b_rptr + len; 5390 mp2->b_rptr = mp1->b_wptr; 5391 /* 5392 * after adjustments if mblk not consumed is now 5393 * unaligned, try to align it. If this fails free 5394 * all messages and let upper layer recover. 5395 */ 5396 if (!OK_32PTR(mp2->b_rptr)) { 5397 if (!pullupmsg(mp2, -1)) { 5398 freemsg(mp0); 5399 freemsg(mp2); 5400 *mpp = NULL; 5401 return (NULL); 5402 } 5403 } 5404 *mpp = mp2; 5405 return (mp0); 5406 } 5407 /* Decrement len by the amount we just got. */ 5408 len -= mp2->b_wptr - mp2->b_rptr; 5409 } 5410 /* 5411 * len should be reduced to zero now. If not our caller has 5412 * screwed up. 5413 */ 5414 if (len) { 5415 /* Shouldn't happen! */ 5416 freemsg(mp0); 5417 *mpp = NULL; 5418 return (NULL); 5419 } 5420 /* 5421 * We consumed up to exactly the end of an mblk. Detach the part 5422 * we are returning from the rest of the chain. 5423 */ 5424 mp1->b_cont = NULL; 5425 *mpp = mp2; 5426 return (mp0); 5427 } 5428 5429 /* The ill stream is being unplumbed. Called from ip_close */ 5430 int 5431 ip_modclose(ill_t *ill) 5432 { 5433 5434 boolean_t success; 5435 ipsq_t *ipsq; 5436 ipif_t *ipif; 5437 queue_t *q = ill->ill_rq; 5438 hook_nic_event_t *info; 5439 clock_t timeout; 5440 5441 /* 5442 * Wait for the ACKs of all deferred control messages to be processed. 5443 * In particular, we wait for a potential capability reset initiated 5444 * in ip_sioctl_plink() to complete before proceeding. 5445 * 5446 * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms) 5447 * in case the driver never replies. 5448 */ 5449 timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms); 5450 mutex_enter(&ill->ill_lock); 5451 while (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 5452 if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) { 5453 /* Timeout */ 5454 break; 5455 } 5456 } 5457 mutex_exit(&ill->ill_lock); 5458 5459 /* 5460 * Forcibly enter the ipsq after some delay. This is to take 5461 * care of the case when some ioctl does not complete because 5462 * we sent a control message to the driver and it did not 5463 * send us a reply. We want to be able to at least unplumb 5464 * and replumb rather than force the user to reboot the system. 5465 */ 5466 success = ipsq_enter(ill, B_FALSE); 5467 5468 /* 5469 * Open/close/push/pop is guaranteed to be single threaded 5470 * per stream by STREAMS. FS guarantees that all references 5471 * from top are gone before close is called. So there can't 5472 * be another close thread that has set CONDEMNED on this ill. 5473 * and cause ipsq_enter to return failure. 5474 */ 5475 ASSERT(success); 5476 ipsq = ill->ill_phyint->phyint_ipsq; 5477 5478 /* 5479 * Mark it condemned. No new reference will be made to this ill. 5480 * Lookup functions will return an error. Threads that try to 5481 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 5482 * that the refcnt will drop down to zero. 5483 */ 5484 mutex_enter(&ill->ill_lock); 5485 ill->ill_state_flags |= ILL_CONDEMNED; 5486 for (ipif = ill->ill_ipif; ipif != NULL; 5487 ipif = ipif->ipif_next) { 5488 ipif->ipif_state_flags |= IPIF_CONDEMNED; 5489 } 5490 /* 5491 * Wake up anybody waiting to enter the ipsq. ipsq_enter 5492 * returns error if ILL_CONDEMNED is set 5493 */ 5494 cv_broadcast(&ill->ill_cv); 5495 mutex_exit(&ill->ill_lock); 5496 5497 /* 5498 * Send all the deferred control messages downstream which came in 5499 * during the small window right before ipsq_enter(). We do this 5500 * without waiting for the ACKs because all the ACKs for M_PROTO 5501 * messages are ignored in ip_rput() when ILL_CONDEMNED is set. 5502 */ 5503 ill_send_all_deferred_mp(ill); 5504 5505 /* 5506 * Shut down fragmentation reassembly. 5507 * ill_frag_timer won't start a timer again. 5508 * Now cancel any existing timer 5509 */ 5510 (void) untimeout(ill->ill_frag_timer_id); 5511 (void) ill_frag_timeout(ill, 0); 5512 5513 /* 5514 * If MOVE was in progress, clear the 5515 * move_in_progress fields also. 5516 */ 5517 if (ill->ill_move_in_progress) { 5518 ILL_CLEAR_MOVE(ill); 5519 } 5520 5521 /* 5522 * Call ill_delete to bring down the ipifs, ilms and ill on 5523 * this ill. Then wait for the refcnts to drop to zero. 5524 * ill_is_quiescent checks whether the ill is really quiescent. 5525 * Then make sure that threads that are waiting to enter the 5526 * ipsq have seen the error returned by ipsq_enter and have 5527 * gone away. Then we call ill_delete_tail which does the 5528 * DL_UNBIND and DL_DETACH with the driver and then qprocsoff. 5529 */ 5530 ill_delete(ill); 5531 mutex_enter(&ill->ill_lock); 5532 while (!ill_is_quiescent(ill)) 5533 cv_wait(&ill->ill_cv, &ill->ill_lock); 5534 while (ill->ill_waiters) 5535 cv_wait(&ill->ill_cv, &ill->ill_lock); 5536 5537 mutex_exit(&ill->ill_lock); 5538 5539 /* qprocsoff is called in ill_delete_tail */ 5540 ill_delete_tail(ill); 5541 5542 /* 5543 * Walk through all upper (conn) streams and qenable 5544 * those that have queued data. 5545 * close synchronization needs this to 5546 * be done to ensure that all upper layers blocked 5547 * due to flow control to the closing device 5548 * get unblocked. 5549 */ 5550 ip1dbg(("ip_wsrv: walking\n")); 5551 conn_walk_drain(); 5552 5553 mutex_enter(&ip_mi_lock); 5554 mi_close_unlink(&ip_g_head, (IDP)ill); 5555 mutex_exit(&ip_mi_lock); 5556 5557 /* 5558 * credp could be null if the open didn't succeed and ip_modopen 5559 * itself calls ip_close. 5560 */ 5561 if (ill->ill_credp != NULL) 5562 crfree(ill->ill_credp); 5563 5564 /* 5565 * Unhook the nic event message from the ill and enqueue it into the nic 5566 * event taskq. 5567 */ 5568 if ((info = ill->ill_nic_event_info) != NULL) { 5569 if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, 5570 (void *)info, DDI_SLEEP) == DDI_FAILURE) { 5571 ip2dbg(("ip_ioctl_finish:ddi_taskq_dispatch failed\n")); 5572 if (info->hne_data != NULL) 5573 kmem_free(info->hne_data, info->hne_datalen); 5574 kmem_free(info, sizeof (hook_nic_event_t)); 5575 } 5576 ill->ill_nic_event_info = NULL; 5577 } 5578 5579 mi_close_free((IDP)ill); 5580 q->q_ptr = WR(q)->q_ptr = NULL; 5581 5582 ipsq_exit(ipsq, B_TRUE, B_TRUE); 5583 5584 return (0); 5585 } 5586 5587 /* 5588 * This is called as part of close() for both IP and UDP 5589 * in order to quiesce the conn. 5590 */ 5591 void 5592 ip_quiesce_conn(conn_t *connp) 5593 { 5594 boolean_t drain_cleanup_reqd = B_FALSE; 5595 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 5596 boolean_t ilg_cleanup_reqd = B_FALSE; 5597 5598 ASSERT(!IPCL_IS_TCP(connp)); 5599 5600 /* 5601 * Mark the conn as closing, and this conn must not be 5602 * inserted in future into any list. Eg. conn_drain_insert(), 5603 * won't insert this conn into the conn_drain_list. 5604 * Similarly ill_pending_mp_add() will not add any mp to 5605 * the pending mp list, after this conn has started closing. 5606 * 5607 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 5608 * cannot get set henceforth. 5609 */ 5610 mutex_enter(&connp->conn_lock); 5611 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 5612 connp->conn_state_flags |= CONN_CLOSING; 5613 if (connp->conn_idl != NULL) 5614 drain_cleanup_reqd = B_TRUE; 5615 if (connp->conn_oper_pending_ill != NULL) 5616 conn_ioctl_cleanup_reqd = B_TRUE; 5617 if (connp->conn_ilg_inuse != 0) 5618 ilg_cleanup_reqd = B_TRUE; 5619 mutex_exit(&connp->conn_lock); 5620 5621 if (IPCL_IS_UDP(connp)) 5622 udp_quiesce_conn(connp); 5623 5624 if (conn_ioctl_cleanup_reqd) 5625 conn_ioctl_cleanup(connp); 5626 5627 if (is_system_labeled() && connp->conn_anon_port) { 5628 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 5629 connp->conn_mlp_type, connp->conn_ulp, 5630 ntohs(connp->conn_lport), B_FALSE); 5631 connp->conn_anon_port = 0; 5632 } 5633 connp->conn_mlp_type = mlptSingle; 5634 5635 /* 5636 * Remove this conn from any fanout list it is on. 5637 * and then wait for any threads currently operating 5638 * on this endpoint to finish 5639 */ 5640 ipcl_hash_remove(connp); 5641 5642 /* 5643 * Remove this conn from the drain list, and do 5644 * any other cleanup that may be required. 5645 * (Only non-tcp streams may have a non-null conn_idl. 5646 * TCP streams are never flow controlled, and 5647 * conn_idl will be null) 5648 */ 5649 if (drain_cleanup_reqd) 5650 conn_drain_tail(connp, B_TRUE); 5651 5652 if (connp->conn_rq == ip_g_mrouter || connp->conn_wq == ip_g_mrouter) 5653 (void) ip_mrouter_done(NULL); 5654 5655 if (ilg_cleanup_reqd) 5656 ilg_delete_all(connp); 5657 5658 conn_delete_ire(connp, NULL); 5659 5660 /* 5661 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 5662 * callers from write side can't be there now because close 5663 * is in progress. The only other caller is ipcl_walk 5664 * which checks for the condemned flag. 5665 */ 5666 mutex_enter(&connp->conn_lock); 5667 connp->conn_state_flags |= CONN_CONDEMNED; 5668 while (connp->conn_ref != 1) 5669 cv_wait(&connp->conn_cv, &connp->conn_lock); 5670 connp->conn_state_flags |= CONN_QUIESCED; 5671 mutex_exit(&connp->conn_lock); 5672 } 5673 5674 /* ARGSUSED */ 5675 int 5676 ip_close(queue_t *q, int flags) 5677 { 5678 conn_t *connp; 5679 5680 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 5681 5682 /* 5683 * Call the appropriate delete routine depending on whether this is 5684 * a module or device. 5685 */ 5686 if (WR(q)->q_next != NULL) { 5687 /* This is a module close */ 5688 return (ip_modclose((ill_t *)q->q_ptr)); 5689 } 5690 5691 connp = q->q_ptr; 5692 ip_quiesce_conn(connp); 5693 5694 qprocsoff(q); 5695 5696 /* 5697 * Now we are truly single threaded on this stream, and can 5698 * delete the things hanging off the connp, and finally the connp. 5699 * We removed this connp from the fanout list, it cannot be 5700 * accessed thru the fanouts, and we already waited for the 5701 * conn_ref to drop to 0. We are already in close, so 5702 * there cannot be any other thread from the top. qprocsoff 5703 * has completed, and service has completed or won't run in 5704 * future. 5705 */ 5706 ASSERT(connp->conn_ref == 1); 5707 5708 /* 5709 * A conn which was previously marked as IPCL_UDP cannot 5710 * retain the flag because it would have been cleared by 5711 * udp_close(). 5712 */ 5713 ASSERT(!IPCL_IS_UDP(connp)); 5714 5715 if (connp->conn_latch != NULL) { 5716 IPLATCH_REFRELE(connp->conn_latch); 5717 connp->conn_latch = NULL; 5718 } 5719 if (connp->conn_policy != NULL) { 5720 IPPH_REFRELE(connp->conn_policy); 5721 connp->conn_policy = NULL; 5722 } 5723 if (connp->conn_ipsec_opt_mp != NULL) { 5724 freemsg(connp->conn_ipsec_opt_mp); 5725 connp->conn_ipsec_opt_mp = NULL; 5726 } 5727 5728 inet_minor_free(ip_minor_arena, connp->conn_dev); 5729 5730 connp->conn_ref--; 5731 ipcl_conn_destroy(connp); 5732 5733 q->q_ptr = WR(q)->q_ptr = NULL; 5734 return (0); 5735 } 5736 5737 int 5738 ip_snmpmod_close(queue_t *q) 5739 { 5740 conn_t *connp = Q_TO_CONN(q); 5741 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5742 5743 qprocsoff(q); 5744 5745 if (connp->conn_flags & IPCL_UDPMOD) 5746 udp_close_free(connp); 5747 5748 if (connp->conn_cred != NULL) { 5749 crfree(connp->conn_cred); 5750 connp->conn_cred = NULL; 5751 } 5752 CONN_DEC_REF(connp); 5753 q->q_ptr = WR(q)->q_ptr = NULL; 5754 return (0); 5755 } 5756 5757 /* 5758 * Write side put procedure for TCP module or UDP module instance. TCP/UDP 5759 * as a module is only used for MIB browsers that push TCP/UDP over IP or ARP. 5760 * The only supported primitives are T_SVR4_OPTMGMT_REQ and T_OPTMGMT_REQ. 5761 * M_FLUSH messages and ioctls are only passed downstream; we don't flush our 5762 * queues as we never enqueue messages there and we don't handle any ioctls. 5763 * Everything else is freed. 5764 */ 5765 void 5766 ip_snmpmod_wput(queue_t *q, mblk_t *mp) 5767 { 5768 conn_t *connp = q->q_ptr; 5769 pfi_t setfn; 5770 pfi_t getfn; 5771 5772 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 5773 5774 switch (DB_TYPE(mp)) { 5775 case M_PROTO: 5776 case M_PCPROTO: 5777 if ((MBLKL(mp) >= sizeof (t_scalar_t)) && 5778 ((((union T_primitives *)mp->b_rptr)->type == 5779 T_SVR4_OPTMGMT_REQ) || 5780 (((union T_primitives *)mp->b_rptr)->type == 5781 T_OPTMGMT_REQ))) { 5782 /* 5783 * This is the only TPI primitive supported. Its 5784 * handling does not require tcp_t, but it does require 5785 * conn_t to check permissions. 5786 */ 5787 cred_t *cr = DB_CREDDEF(mp, connp->conn_cred); 5788 5789 if (connp->conn_flags & IPCL_TCPMOD) { 5790 setfn = tcp_snmp_set; 5791 getfn = tcp_snmp_get; 5792 } else { 5793 setfn = udp_snmp_set; 5794 getfn = udp_snmp_get; 5795 } 5796 if (!snmpcom_req(q, mp, setfn, getfn, cr)) { 5797 freemsg(mp); 5798 return; 5799 } 5800 } else if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, ENOTSUP)) 5801 != NULL) 5802 qreply(q, mp); 5803 break; 5804 case M_FLUSH: 5805 case M_IOCTL: 5806 putnext(q, mp); 5807 break; 5808 default: 5809 freemsg(mp); 5810 break; 5811 } 5812 } 5813 5814 /* Return the IP checksum for the IP header at "iph". */ 5815 uint16_t 5816 ip_csum_hdr(ipha_t *ipha) 5817 { 5818 uint16_t *uph; 5819 uint32_t sum; 5820 int opt_len; 5821 5822 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - 5823 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 5824 uph = (uint16_t *)ipha; 5825 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 5826 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 5827 if (opt_len > 0) { 5828 do { 5829 sum += uph[10]; 5830 sum += uph[11]; 5831 uph += 2; 5832 } while (--opt_len); 5833 } 5834 sum = (sum & 0xFFFF) + (sum >> 16); 5835 sum = ~(sum + (sum >> 16)) & 0xFFFF; 5836 if (sum == 0xffff) 5837 sum = 0; 5838 return ((uint16_t)sum); 5839 } 5840 5841 void 5842 ip_ddi_destroy(void) 5843 { 5844 ipv4_hook_destroy(); 5845 ipv6_hook_destroy(); 5846 ip_net_destroy(); 5847 5848 tnet_fini(); 5849 tcp_ddi_destroy(); 5850 sctp_ddi_destroy(); 5851 ipsec_loader_destroy(); 5852 ipsec_policy_destroy(); 5853 ipsec_kstat_destroy(); 5854 nd_free(&ip_g_nd); 5855 mutex_destroy(&igmp_timer_lock); 5856 mutex_destroy(&mld_timer_lock); 5857 mutex_destroy(&igmp_slowtimeout_lock); 5858 mutex_destroy(&mld_slowtimeout_lock); 5859 mutex_destroy(&ip_mi_lock); 5860 mutex_destroy(&rts_clients.connf_lock); 5861 ip_ire_fini(); 5862 ip6_asp_free(); 5863 conn_drain_fini(); 5864 ipcl_destroy(); 5865 inet_minor_destroy(ip_minor_arena); 5866 icmp_kstat_fini(); 5867 ip_kstat_fini(); 5868 rw_destroy(&ipsec_capab_ills_lock); 5869 rw_destroy(&ill_g_usesrc_lock); 5870 ip_drop_unregister(&ip_dropper); 5871 } 5872 5873 5874 void 5875 ip_ddi_init(void) 5876 { 5877 TCP6_MAJ = ddi_name_to_major(TCP6); 5878 TCP_MAJ = ddi_name_to_major(TCP); 5879 SCTP_MAJ = ddi_name_to_major(SCTP); 5880 SCTP6_MAJ = ddi_name_to_major(SCTP6); 5881 5882 ip_input_proc = ip_squeue_switch(ip_squeue_enter); 5883 5884 /* IP's IPsec code calls the packet dropper */ 5885 ip_drop_register(&ip_dropper, "IP IPsec processing"); 5886 5887 if (!ip_g_nd) { 5888 if (!ip_param_register(lcl_param_arr, A_CNT(lcl_param_arr), 5889 lcl_ndp_arr, A_CNT(lcl_ndp_arr))) { 5890 nd_free(&ip_g_nd); 5891 } 5892 } 5893 5894 ipsec_loader_init(); 5895 ipsec_policy_init(); 5896 ipsec_kstat_init(); 5897 rw_init(&ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 5898 mutex_init(&igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5899 mutex_init(&mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5900 mutex_init(&igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5901 mutex_init(&mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5902 mutex_init(&ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 5903 mutex_init(&ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 5904 rw_init(&ill_g_lock, NULL, RW_DEFAULT, NULL); 5905 rw_init(&ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 5906 rw_init(&ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 5907 5908 /* 5909 * For IP and TCP the minor numbers should start from 2 since we have 4 5910 * initial devices: ip, ip6, tcp, tcp6. 5911 */ 5912 if ((ip_minor_arena = inet_minor_create("ip_minor_arena", 5913 INET_MIN_DEV + 2, KM_SLEEP)) == NULL) { 5914 cmn_err(CE_PANIC, 5915 "ip_ddi_init: ip_minor_arena creation failed\n"); 5916 } 5917 5918 ipcl_init(); 5919 mutex_init(&rts_clients.connf_lock, NULL, MUTEX_DEFAULT, NULL); 5920 ip_ire_init(); 5921 ip6_asp_init(); 5922 ipif_init(); 5923 conn_drain_init(); 5924 tcp_ddi_init(); 5925 sctp_ddi_init(); 5926 5927 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5928 5929 if ((ip_kstat = kstat_create("ip", 0, "ipstat", 5930 "net", KSTAT_TYPE_NAMED, 5931 sizeof (ip_statistics) / sizeof (kstat_named_t), 5932 KSTAT_FLAG_VIRTUAL)) != NULL) { 5933 ip_kstat->ks_data = &ip_statistics; 5934 kstat_install(ip_kstat); 5935 } 5936 ip_kstat_init(); 5937 ip6_kstat_init(); 5938 icmp_kstat_init(); 5939 ipsec_loader_start(); 5940 tnet_init(); 5941 5942 ip_net_init(); 5943 ipv4_hook_init(); 5944 ipv6_hook_init(); 5945 } 5946 5947 /* 5948 * Allocate and initialize a DLPI template of the specified length. (May be 5949 * called as writer.) 5950 */ 5951 mblk_t * 5952 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 5953 { 5954 mblk_t *mp; 5955 5956 mp = allocb(len, BPRI_MED); 5957 if (!mp) 5958 return (NULL); 5959 5960 /* 5961 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 5962 * of which we don't seem to use) are sent with M_PCPROTO, and 5963 * that other DLPI are M_PROTO. 5964 */ 5965 if (prim == DL_INFO_REQ) { 5966 mp->b_datap->db_type = M_PCPROTO; 5967 } else { 5968 mp->b_datap->db_type = M_PROTO; 5969 } 5970 5971 mp->b_wptr = mp->b_rptr + len; 5972 bzero(mp->b_rptr, len); 5973 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 5974 return (mp); 5975 } 5976 5977 const char * 5978 dlpi_prim_str(int prim) 5979 { 5980 switch (prim) { 5981 case DL_INFO_REQ: return ("DL_INFO_REQ"); 5982 case DL_INFO_ACK: return ("DL_INFO_ACK"); 5983 case DL_ATTACH_REQ: return ("DL_ATTACH_REQ"); 5984 case DL_DETACH_REQ: return ("DL_DETACH_REQ"); 5985 case DL_BIND_REQ: return ("DL_BIND_REQ"); 5986 case DL_BIND_ACK: return ("DL_BIND_ACK"); 5987 case DL_UNBIND_REQ: return ("DL_UNBIND_REQ"); 5988 case DL_OK_ACK: return ("DL_OK_ACK"); 5989 case DL_ERROR_ACK: return ("DL_ERROR_ACK"); 5990 case DL_ENABMULTI_REQ: return ("DL_ENABMULTI_REQ"); 5991 case DL_DISABMULTI_REQ: return ("DL_DISABMULTI_REQ"); 5992 case DL_PROMISCON_REQ: return ("DL_PROMISCON_REQ"); 5993 case DL_PROMISCOFF_REQ: return ("DL_PROMISCOFF_REQ"); 5994 case DL_UNITDATA_REQ: return ("DL_UNITDATA_REQ"); 5995 case DL_UNITDATA_IND: return ("DL_UNITDATA_IND"); 5996 case DL_UDERROR_IND: return ("DL_UDERROR_IND"); 5997 case DL_PHYS_ADDR_REQ: return ("DL_PHYS_ADDR_REQ"); 5998 case DL_PHYS_ADDR_ACK: return ("DL_PHYS_ADDR_ACK"); 5999 case DL_SET_PHYS_ADDR_REQ: return ("DL_SET_PHYS_ADDR_REQ"); 6000 case DL_NOTIFY_REQ: return ("DL_NOTIFY_REQ"); 6001 case DL_NOTIFY_ACK: return ("DL_NOTIFY_ACK"); 6002 case DL_NOTIFY_IND: return ("DL_NOTIFY_IND"); 6003 case DL_CAPABILITY_REQ: return ("DL_CAPABILITY_REQ"); 6004 case DL_CAPABILITY_ACK: return ("DL_CAPABILITY_ACK"); 6005 case DL_CONTROL_REQ: return ("DL_CONTROL_REQ"); 6006 case DL_CONTROL_ACK: return ("DL_CONTROL_ACK"); 6007 default: return ("<unknown primitive>"); 6008 } 6009 } 6010 6011 const char * 6012 dlpi_err_str(int err) 6013 { 6014 switch (err) { 6015 case DL_ACCESS: return ("DL_ACCESS"); 6016 case DL_BADADDR: return ("DL_BADADDR"); 6017 case DL_BADCORR: return ("DL_BADCORR"); 6018 case DL_BADDATA: return ("DL_BADDATA"); 6019 case DL_BADPPA: return ("DL_BADPPA"); 6020 case DL_BADPRIM: return ("DL_BADPRIM"); 6021 case DL_BADQOSPARAM: return ("DL_BADQOSPARAM"); 6022 case DL_BADQOSTYPE: return ("DL_BADQOSTYPE"); 6023 case DL_BADSAP: return ("DL_BADSAP"); 6024 case DL_BADTOKEN: return ("DL_BADTOKEN"); 6025 case DL_BOUND: return ("DL_BOUND"); 6026 case DL_INITFAILED: return ("DL_INITFAILED"); 6027 case DL_NOADDR: return ("DL_NOADDR"); 6028 case DL_NOTINIT: return ("DL_NOTINIT"); 6029 case DL_OUTSTATE: return ("DL_OUTSTATE"); 6030 case DL_SYSERR: return ("DL_SYSERR"); 6031 case DL_UNSUPPORTED: return ("DL_UNSUPPORTED"); 6032 case DL_UNDELIVERABLE: return ("DL_UNDELIVERABLE"); 6033 case DL_NOTSUPPORTED : return ("DL_NOTSUPPORTED "); 6034 case DL_TOOMANY: return ("DL_TOOMANY"); 6035 case DL_NOTENAB: return ("DL_NOTENAB"); 6036 case DL_BUSY: return ("DL_BUSY"); 6037 case DL_NOAUTO: return ("DL_NOAUTO"); 6038 case DL_NOXIDAUTO: return ("DL_NOXIDAUTO"); 6039 case DL_NOTESTAUTO: return ("DL_NOTESTAUTO"); 6040 case DL_XIDAUTO: return ("DL_XIDAUTO"); 6041 case DL_TESTAUTO: return ("DL_TESTAUTO"); 6042 case DL_PENDING: return ("DL_PENDING"); 6043 default: return ("<unknown error>"); 6044 } 6045 } 6046 6047 /* 6048 * Debug formatting routine. Returns a character string representation of the 6049 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 6050 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 6051 * 6052 * Once the ndd table-printing interfaces are removed, this can be changed to 6053 * standard dotted-decimal form. 6054 */ 6055 char * 6056 ip_dot_addr(ipaddr_t addr, char *buf) 6057 { 6058 uint8_t *ap = (uint8_t *)&addr; 6059 6060 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 6061 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 6062 return (buf); 6063 } 6064 6065 /* 6066 * Write the given MAC address as a printable string in the usual colon- 6067 * separated format. 6068 */ 6069 const char * 6070 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 6071 { 6072 char *bp; 6073 6074 if (alen == 0 || buflen < 4) 6075 return ("?"); 6076 bp = buf; 6077 for (;;) { 6078 /* 6079 * If there are more MAC address bytes available, but we won't 6080 * have any room to print them, then add "..." to the string 6081 * instead. See below for the 'magic number' explanation. 6082 */ 6083 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 6084 (void) strcpy(bp, "..."); 6085 break; 6086 } 6087 (void) sprintf(bp, "%02x", *addr++); 6088 bp += 2; 6089 if (--alen == 0) 6090 break; 6091 *bp++ = ':'; 6092 buflen -= 3; 6093 /* 6094 * At this point, based on the first 'if' statement above, 6095 * either alen == 1 and buflen >= 3, or alen > 1 and 6096 * buflen >= 4. The first case leaves room for the final "xx" 6097 * number and trailing NUL byte. The second leaves room for at 6098 * least "...". Thus the apparently 'magic' numbers chosen for 6099 * that statement. 6100 */ 6101 } 6102 return (buf); 6103 } 6104 6105 /* 6106 * Send an ICMP error after patching up the packet appropriately. Returns 6107 * non-zero if the appropriate MIB should be bumped; zero otherwise. 6108 */ 6109 static boolean_t 6110 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 6111 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, zoneid_t zoneid) 6112 { 6113 ipha_t *ipha; 6114 mblk_t *first_mp; 6115 boolean_t secure; 6116 unsigned char db_type; 6117 6118 first_mp = mp; 6119 if (mctl_present) { 6120 mp = mp->b_cont; 6121 secure = ipsec_in_is_secure(first_mp); 6122 ASSERT(mp != NULL); 6123 } else { 6124 /* 6125 * If this is an ICMP error being reported - which goes 6126 * up as M_CTLs, we need to convert them to M_DATA till 6127 * we finish checking with global policy because 6128 * ipsec_check_global_policy() assumes M_DATA as clear 6129 * and M_CTL as secure. 6130 */ 6131 db_type = DB_TYPE(mp); 6132 DB_TYPE(mp) = M_DATA; 6133 secure = B_FALSE; 6134 } 6135 /* 6136 * We are generating an icmp error for some inbound packet. 6137 * Called from all ip_fanout_(udp, tcp, proto) functions. 6138 * Before we generate an error, check with global policy 6139 * to see whether this is allowed to enter the system. As 6140 * there is no "conn", we are checking with global policy. 6141 */ 6142 ipha = (ipha_t *)mp->b_rptr; 6143 if (secure || ipsec_inbound_v4_policy_present) { 6144 first_mp = ipsec_check_global_policy(first_mp, NULL, 6145 ipha, NULL, mctl_present); 6146 if (first_mp == NULL) 6147 return (B_FALSE); 6148 } 6149 6150 if (!mctl_present) 6151 DB_TYPE(mp) = db_type; 6152 6153 if (flags & IP_FF_SEND_ICMP) { 6154 if (flags & IP_FF_HDR_COMPLETE) { 6155 if (ip_hdr_complete(ipha, zoneid)) { 6156 freemsg(first_mp); 6157 return (B_TRUE); 6158 } 6159 } 6160 if (flags & IP_FF_CKSUM) { 6161 /* 6162 * Have to correct checksum since 6163 * the packet might have been 6164 * fragmented and the reassembly code in ip_rput 6165 * does not restore the IP checksum. 6166 */ 6167 ipha->ipha_hdr_checksum = 0; 6168 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6169 } 6170 switch (icmp_type) { 6171 case ICMP_DEST_UNREACHABLE: 6172 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid); 6173 break; 6174 default: 6175 freemsg(first_mp); 6176 break; 6177 } 6178 } else { 6179 freemsg(first_mp); 6180 return (B_FALSE); 6181 } 6182 6183 return (B_TRUE); 6184 } 6185 6186 /* 6187 * Used to send an ICMP error message when a packet is received for 6188 * a protocol that is not supported. The mblk passed as argument 6189 * is consumed by this function. 6190 */ 6191 void 6192 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid) 6193 { 6194 mblk_t *mp; 6195 ipha_t *ipha; 6196 ill_t *ill; 6197 ipsec_in_t *ii; 6198 6199 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6200 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6201 6202 mp = ipsec_mp->b_cont; 6203 ipsec_mp->b_cont = NULL; 6204 ipha = (ipha_t *)mp->b_rptr; 6205 /* Get ill from index in ipsec_in_t. */ 6206 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 6207 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL); 6208 if (ill != NULL) { 6209 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 6210 if (ip_fanout_send_icmp(q, mp, flags, 6211 ICMP_DEST_UNREACHABLE, 6212 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid)) { 6213 BUMP_MIB(ill->ill_ip_mib, 6214 ipIfStatsInUnknownProtos); 6215 } 6216 } else { 6217 if (ip_fanout_send_icmp_v6(q, mp, flags, 6218 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 6219 0, B_FALSE, zoneid)) { 6220 BUMP_MIB(ill->ill_ip_mib, 6221 ipIfStatsInUnknownProtos); 6222 } 6223 } 6224 ill_refrele(ill); 6225 } else { /* re-link for the freemsg() below. */ 6226 ipsec_mp->b_cont = mp; 6227 } 6228 6229 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 6230 freemsg(ipsec_mp); 6231 } 6232 6233 /* 6234 * See if the inbound datagram has had IPsec processing applied to it. 6235 */ 6236 boolean_t 6237 ipsec_in_is_secure(mblk_t *ipsec_mp) 6238 { 6239 ipsec_in_t *ii; 6240 6241 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 6242 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6243 6244 if (ii->ipsec_in_loopback) { 6245 return (ii->ipsec_in_secure); 6246 } else { 6247 return (ii->ipsec_in_ah_sa != NULL || 6248 ii->ipsec_in_esp_sa != NULL || 6249 ii->ipsec_in_decaps); 6250 } 6251 } 6252 6253 /* 6254 * Handle protocols with which IP is less intimate. There 6255 * can be more than one stream bound to a particular 6256 * protocol. When this is the case, normally each one gets a copy 6257 * of any incoming packets. 6258 * 6259 * IPSEC NOTE : 6260 * 6261 * Don't allow a secure packet going up a non-secure connection. 6262 * We don't allow this because 6263 * 6264 * 1) Reply might go out in clear which will be dropped at 6265 * the sending side. 6266 * 2) If the reply goes out in clear it will give the 6267 * adversary enough information for getting the key in 6268 * most of the cases. 6269 * 6270 * Moreover getting a secure packet when we expect clear 6271 * implies that SA's were added without checking for 6272 * policy on both ends. This should not happen once ISAKMP 6273 * is used to negotiate SAs as SAs will be added only after 6274 * verifying the policy. 6275 * 6276 * NOTE : If the packet was tunneled and not multicast we only send 6277 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 6278 * back to delivering packets to AF_INET6 raw sockets. 6279 * 6280 * IPQoS Notes: 6281 * Once we have determined the client, invoke IPPF processing. 6282 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6283 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6284 * ip_policy will be false. 6285 * 6286 * Zones notes: 6287 * Currently only applications in the global zone can create raw sockets for 6288 * protocols other than ICMP. So unlike the broadcast / multicast case of 6289 * ip_fanout_udp(), we only send a copy of the packet to streams in the 6290 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 6291 */ 6292 static void 6293 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 6294 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 6295 zoneid_t zoneid) 6296 { 6297 queue_t *rq; 6298 mblk_t *mp1, *first_mp1; 6299 uint_t protocol = ipha->ipha_protocol; 6300 ipaddr_t dst; 6301 boolean_t one_only; 6302 mblk_t *first_mp = mp; 6303 boolean_t secure; 6304 uint32_t ill_index; 6305 conn_t *connp, *first_connp, *next_connp; 6306 connf_t *connfp; 6307 boolean_t shared_addr; 6308 mib2_ipIfStatsEntry_t *mibptr; 6309 6310 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ip_mib; 6311 if (mctl_present) { 6312 mp = first_mp->b_cont; 6313 secure = ipsec_in_is_secure(first_mp); 6314 ASSERT(mp != NULL); 6315 } else { 6316 secure = B_FALSE; 6317 } 6318 dst = ipha->ipha_dst; 6319 /* 6320 * If the packet was tunneled and not multicast we only send to it 6321 * the first match. 6322 */ 6323 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 6324 !CLASSD(dst)); 6325 6326 shared_addr = (zoneid == ALL_ZONES); 6327 if (shared_addr) { 6328 /* 6329 * We don't allow multilevel ports for raw IP, so no need to 6330 * check for that here. 6331 */ 6332 zoneid = tsol_packet_to_zoneid(mp); 6333 } 6334 6335 connfp = &ipcl_proto_fanout[protocol]; 6336 mutex_enter(&connfp->connf_lock); 6337 connp = connfp->connf_head; 6338 for (connp = connfp->connf_head; connp != NULL; 6339 connp = connp->conn_next) { 6340 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, 6341 zoneid) && 6342 (!is_system_labeled() || 6343 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6344 connp))) 6345 break; 6346 } 6347 6348 if (connp == NULL || connp->conn_upq == NULL) { 6349 /* 6350 * No one bound to these addresses. Is 6351 * there a client that wants all 6352 * unclaimed datagrams? 6353 */ 6354 mutex_exit(&connfp->connf_lock); 6355 /* 6356 * Check for IPPROTO_ENCAP... 6357 */ 6358 if (protocol == IPPROTO_ENCAP && ip_g_mrouter) { 6359 /* 6360 * If an IPsec mblk is here on a multicast 6361 * tunnel (using ip_mroute stuff), check policy here, 6362 * THEN ship off to ip_mroute_decap(). 6363 * 6364 * BTW, If I match a configured IP-in-IP 6365 * tunnel, this path will not be reached, and 6366 * ip_mroute_decap will never be called. 6367 */ 6368 first_mp = ipsec_check_global_policy(first_mp, connp, 6369 ipha, NULL, mctl_present); 6370 if (first_mp != NULL) { 6371 if (mctl_present) 6372 freeb(first_mp); 6373 ip_mroute_decap(q, mp); 6374 } /* Else we already freed everything! */ 6375 } else { 6376 /* 6377 * Otherwise send an ICMP protocol unreachable. 6378 */ 6379 if (ip_fanout_send_icmp(q, first_mp, flags, 6380 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 6381 mctl_present, zoneid)) { 6382 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos); 6383 } 6384 } 6385 return; 6386 } 6387 CONN_INC_REF(connp); 6388 first_connp = connp; 6389 6390 /* 6391 * Only send message to one tunnel driver by immediately 6392 * terminating the loop. 6393 */ 6394 connp = one_only ? NULL : connp->conn_next; 6395 6396 for (;;) { 6397 while (connp != NULL) { 6398 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 6399 flags, zoneid) && 6400 (!is_system_labeled() || 6401 tsol_receive_local(mp, &dst, IPV4_VERSION, 6402 shared_addr, connp))) 6403 break; 6404 connp = connp->conn_next; 6405 } 6406 6407 /* 6408 * Copy the packet. 6409 */ 6410 if (connp == NULL || connp->conn_upq == NULL || 6411 (((first_mp1 = dupmsg(first_mp)) == NULL) && 6412 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 6413 /* 6414 * No more interested clients or memory 6415 * allocation failed 6416 */ 6417 connp = first_connp; 6418 break; 6419 } 6420 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 6421 CONN_INC_REF(connp); 6422 mutex_exit(&connfp->connf_lock); 6423 rq = connp->conn_rq; 6424 if (!canputnext(rq)) { 6425 if (flags & IP_FF_RAWIP) { 6426 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6427 } else { 6428 BUMP_MIB(&icmp_mib, icmpInOverflows); 6429 } 6430 6431 freemsg(first_mp1); 6432 } else { 6433 /* 6434 * Don't enforce here if we're an actual tunnel - 6435 * let "tun" do it instead. 6436 */ 6437 if (!IPCL_IS_IPTUN(connp) && 6438 (CONN_INBOUND_POLICY_PRESENT(connp) || secure)) { 6439 first_mp1 = ipsec_check_inbound_policy 6440 (first_mp1, connp, ipha, NULL, 6441 mctl_present); 6442 } 6443 if (first_mp1 != NULL) { 6444 int in_flags = 0; 6445 /* 6446 * ip_fanout_proto also gets called from 6447 * icmp_inbound_error_fanout, in which case 6448 * the msg type is M_CTL. Don't add info 6449 * in this case for the time being. In future 6450 * when there is a need for knowing the 6451 * inbound iface index for ICMP error msgs, 6452 * then this can be changed. 6453 */ 6454 if (connp->conn_recvif) 6455 in_flags = IPF_RECVIF; 6456 /* 6457 * The ULP may support IP_RECVPKTINFO for both 6458 * IP v4 and v6 so pass the appropriate argument 6459 * based on conn IP version. 6460 */ 6461 if (connp->conn_ip_recvpktinfo) { 6462 if (connp->conn_af_isv6) { 6463 /* 6464 * V6 only needs index 6465 */ 6466 in_flags |= IPF_RECVIF; 6467 } else { 6468 /* 6469 * V4 needs index + 6470 * matching address. 6471 */ 6472 in_flags |= IPF_RECVADDR; 6473 } 6474 } 6475 if ((in_flags != 0) && 6476 (mp->b_datap->db_type != M_CTL)) { 6477 /* 6478 * the actual data will be 6479 * contained in b_cont upon 6480 * successful return of the 6481 * following call else 6482 * original mblk is returned 6483 */ 6484 ASSERT(recv_ill != NULL); 6485 mp1 = ip_add_info(mp1, recv_ill, 6486 in_flags, IPCL_ZONEID(connp)); 6487 } 6488 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6489 if (mctl_present) 6490 freeb(first_mp1); 6491 putnext(rq, mp1); 6492 } 6493 } 6494 mutex_enter(&connfp->connf_lock); 6495 /* Follow the next pointer before releasing the conn. */ 6496 next_connp = connp->conn_next; 6497 CONN_DEC_REF(connp); 6498 connp = next_connp; 6499 } 6500 6501 /* Last one. Send it upstream. */ 6502 mutex_exit(&connfp->connf_lock); 6503 6504 /* 6505 * If this packet is coming from icmp_inbound_error_fanout ip_policy 6506 * will be set to false. 6507 */ 6508 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 6509 ill_index = ill->ill_phyint->phyint_ifindex; 6510 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6511 if (mp == NULL) { 6512 CONN_DEC_REF(connp); 6513 if (mctl_present) { 6514 freeb(first_mp); 6515 } 6516 return; 6517 } 6518 } 6519 6520 rq = connp->conn_rq; 6521 if (!canputnext(rq)) { 6522 if (flags & IP_FF_RAWIP) { 6523 BUMP_MIB(mibptr, rawipIfStatsInOverflows); 6524 } else { 6525 BUMP_MIB(&icmp_mib, icmpInOverflows); 6526 } 6527 6528 freemsg(first_mp); 6529 } else { 6530 if (IPCL_IS_IPTUN(connp)) { 6531 /* 6532 * Tunneled packet. We enforce policy in the tunnel 6533 * module itself. 6534 * 6535 * Send the WHOLE packet up (incl. IPSEC_IN) without 6536 * a policy check. 6537 */ 6538 putnext(rq, first_mp); 6539 CONN_DEC_REF(connp); 6540 return; 6541 } 6542 6543 if ((CONN_INBOUND_POLICY_PRESENT(connp) || secure)) { 6544 first_mp = ipsec_check_inbound_policy(first_mp, connp, 6545 ipha, NULL, mctl_present); 6546 } 6547 6548 if (first_mp != NULL) { 6549 int in_flags = 0; 6550 6551 /* 6552 * ip_fanout_proto also gets called 6553 * from icmp_inbound_error_fanout, in 6554 * which case the msg type is M_CTL. 6555 * Don't add info in this case for time 6556 * being. In future when there is a 6557 * need for knowing the inbound iface 6558 * index for ICMP error msgs, then this 6559 * can be changed 6560 */ 6561 if (connp->conn_recvif) 6562 in_flags = IPF_RECVIF; 6563 if (connp->conn_ip_recvpktinfo) { 6564 if (connp->conn_af_isv6) { 6565 /* 6566 * V6 only needs index 6567 */ 6568 in_flags |= IPF_RECVIF; 6569 } else { 6570 /* 6571 * V4 needs index + 6572 * matching address. 6573 */ 6574 in_flags |= IPF_RECVADDR; 6575 } 6576 } 6577 if ((in_flags != 0) && 6578 (mp->b_datap->db_type != M_CTL)) { 6579 6580 /* 6581 * the actual data will be contained in 6582 * b_cont upon successful return 6583 * of the following call else original 6584 * mblk is returned 6585 */ 6586 ASSERT(recv_ill != NULL); 6587 mp = ip_add_info(mp, recv_ill, 6588 in_flags, IPCL_ZONEID(connp)); 6589 } 6590 BUMP_MIB(mibptr, ipIfStatsHCInDelivers); 6591 putnext(rq, mp); 6592 if (mctl_present) 6593 freeb(first_mp); 6594 } 6595 } 6596 CONN_DEC_REF(connp); 6597 } 6598 6599 /* 6600 * Fanout for TCP packets 6601 * The caller puts <fport, lport> in the ports parameter. 6602 * 6603 * IPQoS Notes 6604 * Before sending it to the client, invoke IPPF processing. 6605 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 6606 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 6607 * ip_policy is false. 6608 */ 6609 static void 6610 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 6611 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 6612 { 6613 mblk_t *first_mp; 6614 boolean_t secure; 6615 uint32_t ill_index; 6616 int ip_hdr_len; 6617 tcph_t *tcph; 6618 boolean_t syn_present = B_FALSE; 6619 conn_t *connp; 6620 6621 ASSERT(recv_ill != NULL); 6622 6623 first_mp = mp; 6624 if (mctl_present) { 6625 ASSERT(first_mp->b_datap->db_type == M_CTL); 6626 mp = first_mp->b_cont; 6627 secure = ipsec_in_is_secure(first_mp); 6628 ASSERT(mp != NULL); 6629 } else { 6630 secure = B_FALSE; 6631 } 6632 6633 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 6634 6635 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid)) == 6636 NULL) { 6637 /* 6638 * No connected connection or listener. Send a 6639 * TH_RST via tcp_xmit_listeners_reset. 6640 */ 6641 6642 /* Initiate IPPf processing, if needed. */ 6643 if (IPP_ENABLED(IPP_LOCAL_IN)) { 6644 uint32_t ill_index; 6645 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6646 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 6647 if (first_mp == NULL) 6648 return; 6649 } 6650 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6651 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", 6652 zoneid)); 6653 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid); 6654 return; 6655 } 6656 6657 /* 6658 * Allocate the SYN for the TCP connection here itself 6659 */ 6660 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 6661 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 6662 if (IPCL_IS_TCP(connp)) { 6663 squeue_t *sqp; 6664 6665 /* 6666 * For fused tcp loopback, assign the eager's 6667 * squeue to be that of the active connect's. 6668 * Note that we don't check for IP_FF_LOOPBACK 6669 * here since this routine gets called only 6670 * for loopback (unlike the IPv6 counterpart). 6671 */ 6672 ASSERT(Q_TO_CONN(q) != NULL); 6673 if (do_tcp_fusion && 6674 !CONN_INBOUND_POLICY_PRESENT(connp) && !secure && 6675 !IPP_ENABLED(IPP_LOCAL_IN) && !ip_policy && 6676 IPCL_IS_TCP(Q_TO_CONN(q))) { 6677 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 6678 sqp = Q_TO_CONN(q)->conn_sqp; 6679 } else { 6680 sqp = IP_SQUEUE_GET(lbolt); 6681 } 6682 6683 mp->b_datap->db_struioflag |= STRUIO_EAGER; 6684 DB_CKSUMSTART(mp) = (intptr_t)sqp; 6685 syn_present = B_TRUE; 6686 } 6687 } 6688 6689 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 6690 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 6691 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6692 if ((flags & TH_RST) || (flags & TH_URG)) { 6693 CONN_DEC_REF(connp); 6694 freemsg(first_mp); 6695 return; 6696 } 6697 if (flags & TH_ACK) { 6698 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid); 6699 CONN_DEC_REF(connp); 6700 return; 6701 } 6702 6703 CONN_DEC_REF(connp); 6704 freemsg(first_mp); 6705 return; 6706 } 6707 6708 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 6709 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6710 NULL, mctl_present); 6711 if (first_mp == NULL) { 6712 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6713 CONN_DEC_REF(connp); 6714 return; 6715 } 6716 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 6717 ASSERT(syn_present); 6718 if (mctl_present) { 6719 ASSERT(first_mp != mp); 6720 first_mp->b_datap->db_struioflag |= 6721 STRUIO_POLICY; 6722 } else { 6723 ASSERT(first_mp == mp); 6724 mp->b_datap->db_struioflag &= 6725 ~STRUIO_EAGER; 6726 mp->b_datap->db_struioflag |= 6727 STRUIO_POLICY; 6728 } 6729 } else { 6730 /* 6731 * Discard first_mp early since we're dealing with a 6732 * fully-connected conn_t and tcp doesn't do policy in 6733 * this case. 6734 */ 6735 if (mctl_present) { 6736 freeb(first_mp); 6737 mctl_present = B_FALSE; 6738 } 6739 first_mp = mp; 6740 } 6741 } 6742 6743 /* 6744 * Initiate policy processing here if needed. If we get here from 6745 * icmp_inbound_error_fanout, ip_policy is false. 6746 */ 6747 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 6748 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6749 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6750 if (mp == NULL) { 6751 CONN_DEC_REF(connp); 6752 if (mctl_present) 6753 freeb(first_mp); 6754 return; 6755 } else if (mctl_present) { 6756 ASSERT(first_mp != mp); 6757 first_mp->b_cont = mp; 6758 } else { 6759 first_mp = mp; 6760 } 6761 } 6762 6763 6764 6765 /* Handle socket options. */ 6766 if (!syn_present && 6767 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6768 /* Add header */ 6769 ASSERT(recv_ill != NULL); 6770 /* 6771 * Since tcp does not support IP_RECVPKTINFO for V4, only pass 6772 * IPF_RECVIF. 6773 */ 6774 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp)); 6775 if (mp == NULL) { 6776 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 6777 CONN_DEC_REF(connp); 6778 if (mctl_present) 6779 freeb(first_mp); 6780 return; 6781 } else if (mctl_present) { 6782 /* 6783 * ip_add_info might return a new mp. 6784 */ 6785 ASSERT(first_mp != mp); 6786 first_mp->b_cont = mp; 6787 } else { 6788 first_mp = mp; 6789 } 6790 } 6791 6792 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 6793 if (IPCL_IS_TCP(connp)) { 6794 (*ip_input_proc)(connp->conn_sqp, first_mp, 6795 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); 6796 } else { 6797 putnext(connp->conn_rq, first_mp); 6798 CONN_DEC_REF(connp); 6799 } 6800 } 6801 6802 /* 6803 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 6804 * We are responsible for disposing of mp, such as by freemsg() or putnext() 6805 * Caller is responsible for dropping references to the conn, and freeing 6806 * first_mp. 6807 * 6808 * IPQoS Notes 6809 * Before sending it to the client, invoke IPPF processing. Policy processing 6810 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 6811 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 6812 * ip_wput_local, ip_policy is false. 6813 */ 6814 static void 6815 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 6816 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 6817 boolean_t ip_policy) 6818 { 6819 boolean_t mctl_present = (first_mp != NULL); 6820 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 6821 uint32_t ill_index; 6822 6823 ASSERT(ill != NULL); 6824 6825 if (mctl_present) 6826 first_mp->b_cont = mp; 6827 else 6828 first_mp = mp; 6829 6830 if (CONN_UDP_FLOWCTLD(connp)) { 6831 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 6832 freemsg(first_mp); 6833 return; 6834 } 6835 6836 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 6837 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 6838 NULL, mctl_present); 6839 if (first_mp == NULL) { 6840 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 6841 return; /* Freed by ipsec_check_inbound_policy(). */ 6842 } 6843 } 6844 if (mctl_present) 6845 freeb(first_mp); 6846 6847 /* Handle options. */ 6848 if (connp->conn_recvif) 6849 in_flags = IPF_RECVIF; 6850 /* 6851 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag 6852 * passed to ip_add_info is based on IP version of connp. 6853 */ 6854 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { 6855 if (connp->conn_af_isv6) { 6856 /* 6857 * V6 only needs index 6858 */ 6859 in_flags |= IPF_RECVIF; 6860 } else { 6861 /* 6862 * V4 needs index + matching address. 6863 */ 6864 in_flags |= IPF_RECVADDR; 6865 } 6866 } 6867 6868 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 6869 in_flags |= IPF_RECVSLLA; 6870 6871 /* 6872 * Initiate IPPF processing here, if needed. Note first_mp won't be 6873 * freed if the packet is dropped. The caller will do so. 6874 */ 6875 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 6876 ill_index = recv_ill->ill_phyint->phyint_ifindex; 6877 ip_process(IPP_LOCAL_IN, &mp, ill_index); 6878 if (mp == NULL) { 6879 return; 6880 } 6881 } 6882 if ((in_flags != 0) && 6883 (mp->b_datap->db_type != M_CTL)) { 6884 /* 6885 * The actual data will be contained in b_cont 6886 * upon successful return of the following call 6887 * else original mblk is returned 6888 */ 6889 ASSERT(recv_ill != NULL); 6890 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp)); 6891 } 6892 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 6893 /* Send it upstream */ 6894 CONN_UDP_RECV(connp, mp); 6895 } 6896 6897 /* 6898 * Fanout for UDP packets. 6899 * The caller puts <fport, lport> in the ports parameter. 6900 * 6901 * If SO_REUSEADDR is set all multicast and broadcast packets 6902 * will be delivered to all streams bound to the same port. 6903 * 6904 * Zones notes: 6905 * Multicast and broadcast packets will be distributed to streams in all zones. 6906 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 6907 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 6908 * packets. To maintain this behavior with multiple zones, the conns are grouped 6909 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 6910 * each zone. If unset, all the following conns in the same zone are skipped. 6911 */ 6912 static void 6913 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 6914 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 6915 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 6916 { 6917 uint32_t dstport, srcport; 6918 ipaddr_t dst; 6919 mblk_t *first_mp; 6920 boolean_t secure; 6921 in6_addr_t v6src; 6922 conn_t *connp; 6923 connf_t *connfp; 6924 conn_t *first_connp; 6925 conn_t *next_connp; 6926 mblk_t *mp1, *first_mp1; 6927 ipaddr_t src; 6928 zoneid_t last_zoneid; 6929 boolean_t reuseaddr; 6930 boolean_t shared_addr; 6931 6932 first_mp = mp; 6933 if (mctl_present) { 6934 mp = first_mp->b_cont; 6935 first_mp->b_cont = NULL; 6936 secure = ipsec_in_is_secure(first_mp); 6937 ASSERT(mp != NULL); 6938 } else { 6939 first_mp = NULL; 6940 secure = B_FALSE; 6941 } 6942 6943 /* Extract ports in net byte order */ 6944 dstport = htons(ntohl(ports) & 0xFFFF); 6945 srcport = htons(ntohl(ports) >> 16); 6946 dst = ipha->ipha_dst; 6947 src = ipha->ipha_src; 6948 6949 shared_addr = (zoneid == ALL_ZONES); 6950 if (shared_addr) { 6951 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 6952 if (zoneid == ALL_ZONES) 6953 zoneid = tsol_packet_to_zoneid(mp); 6954 } 6955 6956 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 6957 mutex_enter(&connfp->connf_lock); 6958 connp = connfp->connf_head; 6959 if (!broadcast && !CLASSD(dst)) { 6960 /* 6961 * Not broadcast or multicast. Send to the one (first) 6962 * client we find. No need to check conn_wantpacket() 6963 * since IP_BOUND_IF/conn_incoming_ill does not apply to 6964 * IPv4 unicast packets. 6965 */ 6966 while ((connp != NULL) && 6967 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || 6968 !IPCL_ZONE_MATCH(connp, zoneid))) { 6969 connp = connp->conn_next; 6970 } 6971 6972 if (connp == NULL || connp->conn_upq == NULL) 6973 goto notfound; 6974 6975 if (is_system_labeled() && 6976 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 6977 connp)) 6978 goto notfound; 6979 6980 CONN_INC_REF(connp); 6981 mutex_exit(&connfp->connf_lock); 6982 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 6983 flags, recv_ill, ip_policy); 6984 IP_STAT(ip_udp_fannorm); 6985 CONN_DEC_REF(connp); 6986 return; 6987 } 6988 6989 /* 6990 * Broadcast and multicast case 6991 * 6992 * Need to check conn_wantpacket(). 6993 * If SO_REUSEADDR has been set on the first we send the 6994 * packet to all clients that have joined the group and 6995 * match the port. 6996 */ 6997 6998 while (connp != NULL) { 6999 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 7000 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7001 (!is_system_labeled() || 7002 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7003 connp))) 7004 break; 7005 connp = connp->conn_next; 7006 } 7007 7008 if (connp == NULL || connp->conn_upq == NULL) 7009 goto notfound; 7010 7011 first_connp = connp; 7012 /* 7013 * When SO_REUSEADDR is not set, send the packet only to the first 7014 * matching connection in its zone by keeping track of the zoneid. 7015 */ 7016 reuseaddr = first_connp->conn_reuseaddr; 7017 last_zoneid = first_connp->conn_zoneid; 7018 7019 CONN_INC_REF(connp); 7020 connp = connp->conn_next; 7021 for (;;) { 7022 while (connp != NULL) { 7023 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 7024 (reuseaddr || connp->conn_zoneid != last_zoneid) && 7025 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7026 (!is_system_labeled() || 7027 tsol_receive_local(mp, &dst, IPV4_VERSION, 7028 shared_addr, connp))) 7029 break; 7030 connp = connp->conn_next; 7031 } 7032 /* 7033 * Just copy the data part alone. The mctl part is 7034 * needed just for verifying policy and it is never 7035 * sent up. 7036 */ 7037 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7038 ((mp1 = copymsg(mp)) == NULL))) { 7039 /* 7040 * No more interested clients or memory 7041 * allocation failed 7042 */ 7043 connp = first_connp; 7044 break; 7045 } 7046 if (connp->conn_zoneid != last_zoneid) { 7047 /* 7048 * Update the zoneid so that the packet isn't sent to 7049 * any more conns in the same zone unless SO_REUSEADDR 7050 * is set. 7051 */ 7052 reuseaddr = connp->conn_reuseaddr; 7053 last_zoneid = connp->conn_zoneid; 7054 } 7055 if (first_mp != NULL) { 7056 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7057 ipsec_info_type == IPSEC_IN); 7058 first_mp1 = ipsec_in_tag(first_mp, NULL); 7059 if (first_mp1 == NULL) { 7060 freemsg(mp1); 7061 connp = first_connp; 7062 break; 7063 } 7064 } else { 7065 first_mp1 = NULL; 7066 } 7067 CONN_INC_REF(connp); 7068 mutex_exit(&connfp->connf_lock); 7069 /* 7070 * IPQoS notes: We don't send the packet for policy 7071 * processing here, will do it for the last one (below). 7072 * i.e. we do it per-packet now, but if we do policy 7073 * processing per-conn, then we would need to do it 7074 * here too. 7075 */ 7076 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7077 ipha, flags, recv_ill, B_FALSE); 7078 mutex_enter(&connfp->connf_lock); 7079 /* Follow the next pointer before releasing the conn. */ 7080 next_connp = connp->conn_next; 7081 IP_STAT(ip_udp_fanmb); 7082 CONN_DEC_REF(connp); 7083 connp = next_connp; 7084 } 7085 7086 /* Last one. Send it upstream. */ 7087 mutex_exit(&connfp->connf_lock); 7088 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7089 recv_ill, ip_policy); 7090 IP_STAT(ip_udp_fanmb); 7091 CONN_DEC_REF(connp); 7092 return; 7093 7094 notfound: 7095 7096 mutex_exit(&connfp->connf_lock); 7097 IP_STAT(ip_udp_fanothers); 7098 /* 7099 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 7100 * have already been matched above, since they live in the IPv4 7101 * fanout tables. This implies we only need to 7102 * check for IPv6 in6addr_any endpoints here. 7103 * Thus we compare using ipv6_all_zeros instead of the destination 7104 * address, except for the multicast group membership lookup which 7105 * uses the IPv4 destination. 7106 */ 7107 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 7108 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 7109 mutex_enter(&connfp->connf_lock); 7110 connp = connfp->connf_head; 7111 if (!broadcast && !CLASSD(dst)) { 7112 while (connp != NULL) { 7113 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7114 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && 7115 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7116 !connp->conn_ipv6_v6only) 7117 break; 7118 connp = connp->conn_next; 7119 } 7120 7121 if (connp != NULL && is_system_labeled() && 7122 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7123 connp)) 7124 connp = NULL; 7125 7126 if (connp == NULL || connp->conn_upq == NULL) { 7127 /* 7128 * No one bound to this port. Is 7129 * there a client that wants all 7130 * unclaimed datagrams? 7131 */ 7132 mutex_exit(&connfp->connf_lock); 7133 7134 if (mctl_present) 7135 first_mp->b_cont = mp; 7136 else 7137 first_mp = mp; 7138 if (ipcl_proto_search(IPPROTO_UDP) != NULL) { 7139 ip_fanout_proto(q, first_mp, ill, ipha, 7140 flags | IP_FF_RAWIP, mctl_present, 7141 ip_policy, recv_ill, zoneid); 7142 } else { 7143 if (ip_fanout_send_icmp(q, first_mp, flags, 7144 ICMP_DEST_UNREACHABLE, 7145 ICMP_PORT_UNREACHABLE, 7146 mctl_present, zoneid)) { 7147 BUMP_MIB(ill->ill_ip_mib, 7148 udpIfStatsNoPorts); 7149 } 7150 } 7151 return; 7152 } 7153 7154 CONN_INC_REF(connp); 7155 mutex_exit(&connfp->connf_lock); 7156 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, 7157 flags, recv_ill, ip_policy); 7158 CONN_DEC_REF(connp); 7159 return; 7160 } 7161 /* 7162 * IPv4 multicast packet being delivered to an AF_INET6 7163 * in6addr_any endpoint. 7164 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 7165 * and not conn_wantpacket_v6() since any multicast membership is 7166 * for an IPv4-mapped multicast address. 7167 * The packet is sent to all clients in all zones that have joined the 7168 * group and match the port. 7169 */ 7170 while (connp != NULL) { 7171 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 7172 srcport, v6src) && 7173 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7174 (!is_system_labeled() || 7175 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, 7176 connp))) 7177 break; 7178 connp = connp->conn_next; 7179 } 7180 7181 if (connp == NULL || connp->conn_upq == NULL) { 7182 /* 7183 * No one bound to this port. Is 7184 * there a client that wants all 7185 * unclaimed datagrams? 7186 */ 7187 mutex_exit(&connfp->connf_lock); 7188 7189 if (mctl_present) 7190 first_mp->b_cont = mp; 7191 else 7192 first_mp = mp; 7193 if (ipcl_proto_search(IPPROTO_UDP) != NULL) { 7194 ip_fanout_proto(q, first_mp, ill, ipha, 7195 flags | IP_FF_RAWIP, mctl_present, ip_policy, 7196 recv_ill, zoneid); 7197 } else { 7198 /* 7199 * We used to attempt to send an icmp error here, but 7200 * since this is known to be a multicast packet 7201 * and we don't send icmp errors in response to 7202 * multicast, just drop the packet and give up sooner. 7203 */ 7204 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 7205 freemsg(first_mp); 7206 } 7207 return; 7208 } 7209 7210 first_connp = connp; 7211 7212 CONN_INC_REF(connp); 7213 connp = connp->conn_next; 7214 for (;;) { 7215 while (connp != NULL) { 7216 if (IPCL_UDP_MATCH_V6(connp, dstport, 7217 ipv6_all_zeros, srcport, v6src) && 7218 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 7219 (!is_system_labeled() || 7220 tsol_receive_local(mp, &dst, IPV4_VERSION, 7221 shared_addr, connp))) 7222 break; 7223 connp = connp->conn_next; 7224 } 7225 /* 7226 * Just copy the data part alone. The mctl part is 7227 * needed just for verifying policy and it is never 7228 * sent up. 7229 */ 7230 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 7231 ((mp1 = copymsg(mp)) == NULL))) { 7232 /* 7233 * No more intested clients or memory 7234 * allocation failed 7235 */ 7236 connp = first_connp; 7237 break; 7238 } 7239 if (first_mp != NULL) { 7240 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 7241 ipsec_info_type == IPSEC_IN); 7242 first_mp1 = ipsec_in_tag(first_mp, NULL); 7243 if (first_mp1 == NULL) { 7244 freemsg(mp1); 7245 connp = first_connp; 7246 break; 7247 } 7248 } else { 7249 first_mp1 = NULL; 7250 } 7251 CONN_INC_REF(connp); 7252 mutex_exit(&connfp->connf_lock); 7253 /* 7254 * IPQoS notes: We don't send the packet for policy 7255 * processing here, will do it for the last one (below). 7256 * i.e. we do it per-packet now, but if we do policy 7257 * processing per-conn, then we would need to do it 7258 * here too. 7259 */ 7260 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, 7261 ipha, flags, recv_ill, B_FALSE); 7262 mutex_enter(&connfp->connf_lock); 7263 /* Follow the next pointer before releasing the conn. */ 7264 next_connp = connp->conn_next; 7265 CONN_DEC_REF(connp); 7266 connp = next_connp; 7267 } 7268 7269 /* Last one. Send it upstream. */ 7270 mutex_exit(&connfp->connf_lock); 7271 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, 7272 recv_ill, ip_policy); 7273 CONN_DEC_REF(connp); 7274 } 7275 7276 /* 7277 * Complete the ip_wput header so that it 7278 * is possible to generate ICMP 7279 * errors. 7280 */ 7281 int 7282 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid) 7283 { 7284 ire_t *ire; 7285 7286 if (ipha->ipha_src == INADDR_ANY) { 7287 ire = ire_lookup_local(zoneid); 7288 if (ire == NULL) { 7289 ip1dbg(("ip_hdr_complete: no source IRE\n")); 7290 return (1); 7291 } 7292 ipha->ipha_src = ire->ire_addr; 7293 ire_refrele(ire); 7294 } 7295 ipha->ipha_ttl = ip_def_ttl; 7296 ipha->ipha_hdr_checksum = 0; 7297 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 7298 return (0); 7299 } 7300 7301 /* 7302 * Nobody should be sending 7303 * packets up this stream 7304 */ 7305 static void 7306 ip_lrput(queue_t *q, mblk_t *mp) 7307 { 7308 mblk_t *mp1; 7309 7310 switch (mp->b_datap->db_type) { 7311 case M_FLUSH: 7312 /* Turn around */ 7313 if (*mp->b_rptr & FLUSHW) { 7314 *mp->b_rptr &= ~FLUSHR; 7315 qreply(q, mp); 7316 return; 7317 } 7318 break; 7319 } 7320 /* Could receive messages that passed through ar_rput */ 7321 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 7322 mp1->b_prev = mp1->b_next = NULL; 7323 freemsg(mp); 7324 } 7325 7326 /* Nobody should be sending packets down this stream */ 7327 /* ARGSUSED */ 7328 void 7329 ip_lwput(queue_t *q, mblk_t *mp) 7330 { 7331 freemsg(mp); 7332 } 7333 7334 /* 7335 * Move the first hop in any source route to ipha_dst and remove that part of 7336 * the source route. Called by other protocols. Errors in option formatting 7337 * are ignored - will be handled by ip_wput_options Return the final 7338 * destination (either ipha_dst or the last entry in a source route.) 7339 */ 7340 ipaddr_t 7341 ip_massage_options(ipha_t *ipha) 7342 { 7343 ipoptp_t opts; 7344 uchar_t *opt; 7345 uint8_t optval; 7346 uint8_t optlen; 7347 ipaddr_t dst; 7348 int i; 7349 ire_t *ire; 7350 7351 ip2dbg(("ip_massage_options\n")); 7352 dst = ipha->ipha_dst; 7353 for (optval = ipoptp_first(&opts, ipha); 7354 optval != IPOPT_EOL; 7355 optval = ipoptp_next(&opts)) { 7356 opt = opts.ipoptp_cur; 7357 switch (optval) { 7358 uint8_t off; 7359 case IPOPT_SSRR: 7360 case IPOPT_LSRR: 7361 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 7362 ip1dbg(("ip_massage_options: bad src route\n")); 7363 break; 7364 } 7365 optlen = opts.ipoptp_len; 7366 off = opt[IPOPT_OFFSET]; 7367 off--; 7368 redo_srr: 7369 if (optlen < IP_ADDR_LEN || 7370 off > optlen - IP_ADDR_LEN) { 7371 /* End of source route */ 7372 ip1dbg(("ip_massage_options: end of SR\n")); 7373 break; 7374 } 7375 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 7376 ip1dbg(("ip_massage_options: next hop 0x%x\n", 7377 ntohl(dst))); 7378 /* 7379 * Check if our address is present more than 7380 * once as consecutive hops in source route. 7381 * XXX verify per-interface ip_forwarding 7382 * for source route? 7383 */ 7384 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 7385 ALL_ZONES, NULL, MATCH_IRE_TYPE); 7386 if (ire != NULL) { 7387 ire_refrele(ire); 7388 off += IP_ADDR_LEN; 7389 goto redo_srr; 7390 } 7391 if (dst == htonl(INADDR_LOOPBACK)) { 7392 ip1dbg(("ip_massage_options: loopback addr in " 7393 "source route!\n")); 7394 break; 7395 } 7396 /* 7397 * Update ipha_dst to be the first hop and remove the 7398 * first hop from the source route (by overwriting 7399 * part of the option with NOP options). 7400 */ 7401 ipha->ipha_dst = dst; 7402 /* Put the last entry in dst */ 7403 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 7404 3; 7405 bcopy(&opt[off], &dst, IP_ADDR_LEN); 7406 7407 ip1dbg(("ip_massage_options: last hop 0x%x\n", 7408 ntohl(dst))); 7409 /* Move down and overwrite */ 7410 opt[IP_ADDR_LEN] = opt[0]; 7411 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 7412 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 7413 for (i = 0; i < IP_ADDR_LEN; i++) 7414 opt[i] = IPOPT_NOP; 7415 break; 7416 } 7417 } 7418 return (dst); 7419 } 7420 7421 /* 7422 * This function's job is to forward data to the reverse tunnel (FA->HA) 7423 * after doing a few checks. It is assumed that the incoming interface 7424 * of the packet is always different than the outgoing interface and the 7425 * ire_type of the found ire has to be a non-resolver type. 7426 * 7427 * IPQoS notes 7428 * IP policy is invoked twice for a forwarded packet, once on the read side 7429 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 7430 * enabled. 7431 */ 7432 static void 7433 ip_mrtun_forward(ire_t *ire, ill_t *in_ill, mblk_t *mp) 7434 { 7435 ipha_t *ipha; 7436 queue_t *q; 7437 uint32_t pkt_len; 7438 #define rptr ((uchar_t *)ipha) 7439 uint32_t sum; 7440 uint32_t max_frag; 7441 mblk_t *first_mp; 7442 uint32_t ill_index; 7443 ipxmit_state_t pktxmit_state; 7444 ill_t *out_ill; 7445 7446 ASSERT(ire != NULL); 7447 ASSERT(ire->ire_ipif->ipif_net_type == IRE_IF_NORESOLVER); 7448 ASSERT(ire->ire_stq != NULL); 7449 7450 /* Initiate read side IPPF processing */ 7451 if (IPP_ENABLED(IPP_FWD_IN)) { 7452 ill_index = in_ill->ill_phyint->phyint_ifindex; 7453 ip_process(IPP_FWD_IN, &mp, ill_index); 7454 if (mp == NULL) { 7455 ip2dbg(("ip_mrtun_forward: inbound pkt " 7456 "dropped during IPPF processing\n")); 7457 return; 7458 } 7459 } 7460 7461 if (((in_ill->ill_flags & ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 7462 ILLF_ROUTER) == 0) || 7463 (in_ill == (ill_t *)ire->ire_stq->q_ptr)) { 7464 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsForwProhibits); 7465 ip0dbg(("ip_mrtun_forward: Can't forward :" 7466 "forwarding is not turned on\n")); 7467 goto drop_pkt; 7468 } 7469 7470 /* 7471 * Don't forward if the interface is down 7472 */ 7473 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 7474 goto discard_pkt; 7475 } 7476 7477 ipha = (ipha_t *)mp->b_rptr; 7478 pkt_len = ntohs(ipha->ipha_length); 7479 /* Adjust the checksum to reflect the ttl decrement. */ 7480 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 7481 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 7482 if (ipha->ipha_ttl-- <= 1) { 7483 if (ip_csum_hdr(ipha)) { 7484 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInCksumErrs); 7485 goto drop_pkt; 7486 } 7487 q = ire->ire_stq; 7488 if ((first_mp = allocb(sizeof (ipsec_info_t), 7489 BPRI_HI)) == NULL) { 7490 goto discard_pkt; 7491 } 7492 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsForwProhibits); 7493 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7494 /* Sent by forwarding path, and router is global zone */ 7495 icmp_time_exceeded(q, first_mp, ICMP_TTL_EXCEEDED, 7496 GLOBAL_ZONEID); 7497 return; 7498 } 7499 7500 /* Get the ill_index of the ILL */ 7501 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 7502 7503 /* 7504 * This location is chosen for the placement of the forwarding hook 7505 * because at this point we know that we have a path out for the 7506 * packet but haven't yet applied any logic (such as fragmenting) 7507 * that happen as part of transmitting the packet out. 7508 */ 7509 out_ill = ire->ire_ipif->ipif_ill; 7510 7511 DTRACE_PROBE4(ip4__forwarding__start, 7512 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 7513 7514 FW_HOOKS(ip4_forwarding_event, ipv4firewall_forwarding, 7515 in_ill, out_ill, ipha, mp, mp); 7516 7517 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 7518 7519 if (mp == NULL) 7520 return; 7521 pkt_len = ntohs(ipha->ipha_length); 7522 7523 /* 7524 * ip_mrtun_forward is only used by foreign agent to reverse 7525 * tunnel the incoming packet. So it does not do any option 7526 * processing for source routing. 7527 */ 7528 max_frag = ire->ire_max_frag; 7529 if (pkt_len > max_frag) { 7530 /* 7531 * It needs fragging on its way out. We haven't 7532 * verified the header checksum yet. Since we 7533 * are going to put a surely good checksum in the 7534 * outgoing header, we have to make sure that it 7535 * was good coming in. 7536 */ 7537 if (ip_csum_hdr(ipha)) { 7538 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInCksumErrs); 7539 goto drop_pkt; 7540 } 7541 7542 /* Initiate write side IPPF processing */ 7543 if (IPP_ENABLED(IPP_FWD_OUT)) { 7544 ip_process(IPP_FWD_OUT, &mp, ill_index); 7545 if (mp == NULL) { 7546 ip2dbg(("ip_mrtun_forward: outbound pkt "\ 7547 "dropped/deferred during ip policy "\ 7548 "processing\n")); 7549 return; 7550 } 7551 } 7552 if ((first_mp = allocb(sizeof (ipsec_info_t), 7553 BPRI_HI)) == NULL) { 7554 goto discard_pkt; 7555 } 7556 ip_ipsec_out_prepend(first_mp, mp, in_ill); 7557 mp = first_mp; 7558 7559 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID); 7560 return; 7561 } 7562 7563 ip2dbg(("ip_mrtun_forward: ire type (%d)\n", ire->ire_type)); 7564 7565 ASSERT(ire->ire_ipif != NULL); 7566 7567 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 7568 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 7569 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 7570 NULL, out_ill, ipha, mp, mp); 7571 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 7572 if (mp == NULL) 7573 return; 7574 7575 /* Now send the packet to the tunnel interface */ 7576 mp->b_prev = SET_BPREV_FLAG(IPP_FWD_OUT); 7577 q = ire->ire_stq; 7578 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_FALSE); 7579 if ((pktxmit_state == SEND_FAILED) || 7580 (pktxmit_state == LLHDR_RESLV_FAILED)) { 7581 ip2dbg(("ip_mrtun_forward: failed to send packet to ill %p\n", 7582 q->q_ptr)); 7583 } 7584 7585 return; 7586 discard_pkt: 7587 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInDiscards); 7588 drop_pkt:; 7589 ip2dbg(("ip_mrtun_forward: dropping pkt\n")); 7590 freemsg(mp); 7591 #undef rptr 7592 } 7593 7594 /* 7595 * Fills the ipsec_out_t data structure with appropriate fields and 7596 * prepends it to mp which contains the IP hdr + data that was meant 7597 * to be forwarded. Please note that ipsec_out_info data structure 7598 * is used here to communicate the outgoing ill path at ip_wput() 7599 * for the ICMP error packet. This has nothing to do with ipsec IP 7600 * security. ipsec_out_t is really used to pass the info to the module 7601 * IP where this information cannot be extracted from conn. 7602 * This functions is called by ip_mrtun_forward(). 7603 */ 7604 void 7605 ip_ipsec_out_prepend(mblk_t *first_mp, mblk_t *mp, ill_t *xmit_ill) 7606 { 7607 ipsec_out_t *io; 7608 7609 ASSERT(xmit_ill != NULL); 7610 first_mp->b_datap->db_type = M_CTL; 7611 first_mp->b_wptr += sizeof (ipsec_info_t); 7612 /* 7613 * This is to pass info to ip_wput in absence of conn. 7614 * ipsec_out_secure will be B_FALSE because of this. 7615 * Thus ipsec_out_secure being B_FALSE indicates that 7616 * this is not IPSEC security related information. 7617 */ 7618 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 7619 io = (ipsec_out_t *)first_mp->b_rptr; 7620 io->ipsec_out_type = IPSEC_OUT; 7621 io->ipsec_out_len = sizeof (ipsec_out_t); 7622 first_mp->b_cont = mp; 7623 io->ipsec_out_ill_index = 7624 xmit_ill->ill_phyint->phyint_ifindex; 7625 io->ipsec_out_xmit_if = B_TRUE; 7626 } 7627 7628 /* 7629 * Return the network mask 7630 * associated with the specified address. 7631 */ 7632 ipaddr_t 7633 ip_net_mask(ipaddr_t addr) 7634 { 7635 uchar_t *up = (uchar_t *)&addr; 7636 ipaddr_t mask = 0; 7637 uchar_t *maskp = (uchar_t *)&mask; 7638 7639 #if defined(__i386) || defined(__amd64) 7640 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 7641 #endif 7642 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 7643 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 7644 #endif 7645 if (CLASSD(addr)) { 7646 maskp[0] = 0xF0; 7647 return (mask); 7648 } 7649 if (addr == 0) 7650 return (0); 7651 maskp[0] = 0xFF; 7652 if ((up[0] & 0x80) == 0) 7653 return (mask); 7654 7655 maskp[1] = 0xFF; 7656 if ((up[0] & 0xC0) == 0x80) 7657 return (mask); 7658 7659 maskp[2] = 0xFF; 7660 if ((up[0] & 0xE0) == 0xC0) 7661 return (mask); 7662 7663 /* Must be experimental or multicast, indicate as much */ 7664 return ((ipaddr_t)0); 7665 } 7666 7667 /* 7668 * Select an ill for the packet by considering load spreading across 7669 * a different ill in the group if dst_ill is part of some group. 7670 */ 7671 ill_t * 7672 ip_newroute_get_dst_ill(ill_t *dst_ill) 7673 { 7674 ill_t *ill; 7675 7676 /* 7677 * We schedule irrespective of whether the source address is 7678 * INADDR_ANY or not. illgrp_scheduler returns a held ill. 7679 */ 7680 ill = illgrp_scheduler(dst_ill); 7681 if (ill == NULL) 7682 return (NULL); 7683 7684 /* 7685 * For groups with names ip_sioctl_groupname ensures that all 7686 * ills are of same type. For groups without names, ifgrp_insert 7687 * ensures this. 7688 */ 7689 ASSERT(dst_ill->ill_type == ill->ill_type); 7690 7691 return (ill); 7692 } 7693 7694 /* 7695 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. 7696 */ 7697 ill_t * 7698 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6) 7699 { 7700 ill_t *ret_ill; 7701 7702 ASSERT(ifindex != 0); 7703 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL); 7704 if (ret_ill == NULL || 7705 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { 7706 if (isv6) { 7707 if (ill != NULL) { 7708 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7709 } else { 7710 BUMP_MIB(&ip6_mib, ipIfStatsOutDiscards); 7711 } 7712 ip1dbg(("ip_grab_attach_ill (IPv6): " 7713 "bad ifindex %d.\n", ifindex)); 7714 } else { 7715 if (ill != NULL) { 7716 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 7717 } else { 7718 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 7719 } 7720 ip1dbg(("ip_grab_attach_ill (IPv4): " 7721 "bad ifindex %d.\n", ifindex)); 7722 } 7723 if (ret_ill != NULL) 7724 ill_refrele(ret_ill); 7725 freemsg(first_mp); 7726 return (NULL); 7727 } 7728 7729 return (ret_ill); 7730 } 7731 7732 /* 7733 * IPv4 - 7734 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 7735 * out a packet to a destination address for which we do not have specific 7736 * (or sufficient) routing information. 7737 * 7738 * NOTE : These are the scopes of some of the variables that point at IRE, 7739 * which needs to be followed while making any future modifications 7740 * to avoid memory leaks. 7741 * 7742 * - ire and sire are the entries looked up initially by 7743 * ire_ftable_lookup. 7744 * - ipif_ire is used to hold the interface ire associated with 7745 * the new cache ire. But it's scope is limited, so we always REFRELE 7746 * it before branching out to error paths. 7747 * - save_ire is initialized before ire_create, so that ire returned 7748 * by ire_create will not over-write the ire. We REFRELE save_ire 7749 * before breaking out of the switch. 7750 * 7751 * Thus on failures, we have to REFRELE only ire and sire, if they 7752 * are not NULL. 7753 */ 7754 void 7755 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, ill_t *in_ill, conn_t *connp, 7756 zoneid_t zoneid) 7757 { 7758 areq_t *areq; 7759 ipaddr_t gw = 0; 7760 ire_t *ire = NULL; 7761 mblk_t *res_mp; 7762 ipaddr_t *addrp; 7763 ipaddr_t nexthop_addr; 7764 ipif_t *src_ipif = NULL; 7765 ill_t *dst_ill = NULL; 7766 ipha_t *ipha; 7767 ire_t *sire = NULL; 7768 mblk_t *first_mp; 7769 ire_t *save_ire; 7770 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ 7771 ushort_t ire_marks = 0; 7772 boolean_t mctl_present; 7773 ipsec_out_t *io; 7774 mblk_t *saved_mp; 7775 ire_t *first_sire = NULL; 7776 mblk_t *copy_mp = NULL; 7777 mblk_t *xmit_mp = NULL; 7778 ipaddr_t save_dst; 7779 uint32_t multirt_flags = 7780 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 7781 boolean_t multirt_is_resolvable; 7782 boolean_t multirt_resolve_next; 7783 boolean_t do_attach_ill = B_FALSE; 7784 boolean_t ip_nexthop = B_FALSE; 7785 tsol_ire_gw_secattr_t *attrp = NULL; 7786 tsol_gcgrp_t *gcgrp = NULL; 7787 tsol_gcgrp_addr_t ga; 7788 7789 if (ip_debug > 2) { 7790 /* ip1dbg */ 7791 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 7792 } 7793 7794 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7795 if (mctl_present) { 7796 io = (ipsec_out_t *)first_mp->b_rptr; 7797 ASSERT(io->ipsec_out_type == IPSEC_OUT); 7798 ASSERT(zoneid == io->ipsec_out_zoneid); 7799 ASSERT(zoneid != ALL_ZONES); 7800 } 7801 7802 ipha = (ipha_t *)mp->b_rptr; 7803 7804 /* All multicast lookups come through ip_newroute_ipif() */ 7805 if (CLASSD(dst)) { 7806 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 7807 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 7808 freemsg(first_mp); 7809 return; 7810 } 7811 7812 if (mctl_present && io->ipsec_out_attach_if) { 7813 /* ip_grab_attach_ill returns a held ill */ 7814 attach_ill = ip_grab_attach_ill(NULL, first_mp, 7815 io->ipsec_out_ill_index, B_FALSE); 7816 7817 /* Failure case frees things for us. */ 7818 if (attach_ill == NULL) 7819 return; 7820 7821 /* 7822 * Check if we need an ire that will not be 7823 * looked up by anybody else i.e. HIDDEN. 7824 */ 7825 if (ill_is_probeonly(attach_ill)) 7826 ire_marks = IRE_MARK_HIDDEN; 7827 } 7828 if (mctl_present && io->ipsec_out_ip_nexthop) { 7829 ip_nexthop = B_TRUE; 7830 nexthop_addr = io->ipsec_out_nexthop_addr; 7831 } 7832 /* 7833 * If this IRE is created for forwarding or it is not for 7834 * traffic for congestion controlled protocols, mark it as temporary. 7835 */ 7836 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 7837 ire_marks |= IRE_MARK_TEMPORARY; 7838 7839 /* 7840 * Get what we can from ire_ftable_lookup which will follow an IRE 7841 * chain until it gets the most specific information available. 7842 * For example, we know that there is no IRE_CACHE for this dest, 7843 * but there may be an IRE_OFFSUBNET which specifies a gateway. 7844 * ire_ftable_lookup will look up the gateway, etc. 7845 * Check if in_ill != NULL. If it is true, the packet must be 7846 * from an incoming interface where RTA_SRCIFP is set. 7847 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 7848 * to the destination, of equal netmask length in the forward table, 7849 * will be recursively explored. If no information is available 7850 * for the final gateway of that route, we force the returned ire 7851 * to be equal to sire using MATCH_IRE_PARENT. 7852 * At least, in this case we have a starting point (in the buckets) 7853 * to look for other routes to the destination in the forward table. 7854 * This is actually used only for multirouting, where a list 7855 * of routes has to be processed in sequence. 7856 * 7857 * In the process of coming up with the most specific information, 7858 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry 7859 * for the gateway (i.e., one for which the ire_nce->nce_state is 7860 * not yet ND_REACHABLE, and is in the middle of arp resolution). 7861 * Two caveats when handling incomplete ire's in ip_newroute: 7862 * - we should be careful when accessing its ire_nce (specifically 7863 * the nce_res_mp) ast it might change underneath our feet, and, 7864 * - not all legacy code path callers are prepared to handle 7865 * incomplete ire's, so we should not create/add incomplete 7866 * ire_cache entries here. (See discussion about temporary solution 7867 * further below). 7868 * 7869 * In order to minimize packet dropping, and to preserve existing 7870 * behavior, we treat this case as if there were no IRE_CACHE for the 7871 * gateway, and instead use the IF_RESOLVER ire to send out 7872 * another request to ARP (this is achieved by passing the 7873 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the 7874 * arp response comes back in ip_wput_nondata, we will create 7875 * a per-dst ire_cache that has an ND_COMPLETE ire. 7876 * 7877 * Note that this is a temporary solution; the correct solution is 7878 * to create an incomplete per-dst ire_cache entry, and send the 7879 * packet out when the gw's nce is resolved. In order to achieve this, 7880 * all packet processing must have been completed prior to calling 7881 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need 7882 * to be modified to accomodate this solution. 7883 */ 7884 if (in_ill != NULL) { 7885 ire = ire_srcif_table_lookup(dst, IRE_IF_RESOLVER, NULL, 7886 in_ill, MATCH_IRE_TYPE); 7887 } else if (ip_nexthop) { 7888 /* 7889 * The first time we come here, we look for an IRE_INTERFACE 7890 * entry for the specified nexthop, set the dst to be the 7891 * nexthop address and create an IRE_CACHE entry for the 7892 * nexthop. The next time around, we are able to find an 7893 * IRE_CACHE entry for the nexthop, set the gateway to be the 7894 * nexthop address and create an IRE_CACHE entry for the 7895 * destination address via the specified nexthop. 7896 */ 7897 ire = ire_cache_lookup(nexthop_addr, zoneid, 7898 MBLK_GETLABEL(mp)); 7899 if (ire != NULL) { 7900 gw = nexthop_addr; 7901 ire_marks |= IRE_MARK_PRIVATE_ADDR; 7902 } else { 7903 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 7904 IRE_INTERFACE, NULL, NULL, zoneid, 0, 7905 MBLK_GETLABEL(mp), 7906 MATCH_IRE_TYPE | MATCH_IRE_SECATTR); 7907 if (ire != NULL) { 7908 dst = nexthop_addr; 7909 } 7910 } 7911 } else if (attach_ill == NULL) { 7912 ire = ire_ftable_lookup(dst, 0, 0, 0, 7913 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), 7914 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 7915 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | 7916 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE); 7917 } else { 7918 /* 7919 * attach_ill is set only for communicating with 7920 * on-link hosts. So, don't look for DEFAULT. 7921 */ 7922 ipif_t *attach_ipif; 7923 7924 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 7925 if (attach_ipif == NULL) { 7926 ill_refrele(attach_ill); 7927 goto icmp_err_ret; 7928 } 7929 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, 7930 &sire, zoneid, 0, MBLK_GETLABEL(mp), 7931 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL | 7932 MATCH_IRE_SECATTR); 7933 ipif_refrele(attach_ipif); 7934 } 7935 ip3dbg(("ip_newroute: ire_ftable_lookup() " 7936 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 7937 7938 /* 7939 * This loop is run only once in most cases. 7940 * We loop to resolve further routes only when the destination 7941 * can be reached through multiple RTF_MULTIRT-flagged ires. 7942 */ 7943 do { 7944 /* Clear the previous iteration's values */ 7945 if (src_ipif != NULL) { 7946 ipif_refrele(src_ipif); 7947 src_ipif = NULL; 7948 } 7949 if (dst_ill != NULL) { 7950 ill_refrele(dst_ill); 7951 dst_ill = NULL; 7952 } 7953 7954 multirt_resolve_next = B_FALSE; 7955 /* 7956 * We check if packets have to be multirouted. 7957 * In this case, given the current <ire, sire> couple, 7958 * we look for the next suitable <ire, sire>. 7959 * This check is done in ire_multirt_lookup(), 7960 * which applies various criteria to find the next route 7961 * to resolve. ire_multirt_lookup() leaves <ire, sire> 7962 * unchanged if it detects it has not been tried yet. 7963 */ 7964 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 7965 ip3dbg(("ip_newroute: starting next_resolution " 7966 "with first_mp %p, tag %d\n", 7967 (void *)first_mp, 7968 MULTIRT_DEBUG_TAGGED(first_mp))); 7969 7970 ASSERT(sire != NULL); 7971 multirt_is_resolvable = 7972 ire_multirt_lookup(&ire, &sire, multirt_flags, 7973 MBLK_GETLABEL(mp)); 7974 7975 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 7976 "ire %p, sire %p\n", 7977 multirt_is_resolvable, 7978 (void *)ire, (void *)sire)); 7979 7980 if (!multirt_is_resolvable) { 7981 /* 7982 * No more multirt route to resolve; give up 7983 * (all routes resolved or no more 7984 * resolvable routes). 7985 */ 7986 if (ire != NULL) { 7987 ire_refrele(ire); 7988 ire = NULL; 7989 } 7990 } else { 7991 ASSERT(sire != NULL); 7992 ASSERT(ire != NULL); 7993 /* 7994 * We simply use first_sire as a flag that 7995 * indicates if a resolvable multirt route 7996 * has already been found. 7997 * If it is not the case, we may have to send 7998 * an ICMP error to report that the 7999 * destination is unreachable. 8000 * We do not IRE_REFHOLD first_sire. 8001 */ 8002 if (first_sire == NULL) { 8003 first_sire = sire; 8004 } 8005 } 8006 } 8007 if (ire == NULL) { 8008 if (ip_debug > 3) { 8009 /* ip2dbg */ 8010 pr_addr_dbg("ip_newroute: " 8011 "can't resolve %s\n", AF_INET, &dst); 8012 } 8013 ip3dbg(("ip_newroute: " 8014 "ire %p, sire %p, first_sire %p\n", 8015 (void *)ire, (void *)sire, (void *)first_sire)); 8016 8017 if (sire != NULL) { 8018 ire_refrele(sire); 8019 sire = NULL; 8020 } 8021 8022 if (first_sire != NULL) { 8023 /* 8024 * At least one multirt route has been found 8025 * in the same call to ip_newroute(); 8026 * there is no need to report an ICMP error. 8027 * first_sire was not IRE_REFHOLDed. 8028 */ 8029 MULTIRT_DEBUG_UNTAG(first_mp); 8030 freemsg(first_mp); 8031 return; 8032 } 8033 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 8034 RTA_DST); 8035 if (attach_ill != NULL) 8036 ill_refrele(attach_ill); 8037 goto icmp_err_ret; 8038 } 8039 8040 /* 8041 * When RTA_SRCIFP is used to add a route, then an interface 8042 * route is added in the source interface's routing table. 8043 * If the outgoing interface of this route is of type 8044 * IRE_IF_RESOLVER, then upon creation of the ire, 8045 * ire_nce->nce_res_mp is set to NULL. 8046 * Later, when this route is first used for forwarding 8047 * a packet, ip_newroute() is called 8048 * to resolve the hardware address of the outgoing ipif. 8049 * We do not come here for IRE_IF_NORESOLVER entries in the 8050 * source interface based table. We only come here if the 8051 * outgoing interface is a resolver interface and we don't 8052 * have the ire_nce->nce_res_mp information yet. 8053 * If in_ill is not null that means it is called from 8054 * ip_rput. 8055 */ 8056 8057 ASSERT(ire->ire_in_ill == NULL || 8058 (ire->ire_type == IRE_IF_RESOLVER && 8059 ire->ire_nce != NULL && ire->ire_nce->nce_res_mp == NULL)); 8060 8061 /* 8062 * Verify that the returned IRE does not have either 8063 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 8064 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 8065 */ 8066 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 8067 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 8068 if (attach_ill != NULL) 8069 ill_refrele(attach_ill); 8070 goto icmp_err_ret; 8071 } 8072 /* 8073 * Increment the ire_ob_pkt_count field for ire if it is an 8074 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 8075 * increment the same for the parent IRE, sire, if it is some 8076 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST 8077 * and HOST_REDIRECT). 8078 */ 8079 if ((ire->ire_type & IRE_INTERFACE) != 0) { 8080 UPDATE_OB_PKT_COUNT(ire); 8081 ire->ire_last_used_time = lbolt; 8082 } 8083 8084 if (sire != NULL) { 8085 gw = sire->ire_gateway_addr; 8086 ASSERT((sire->ire_type & (IRE_CACHETABLE | 8087 IRE_INTERFACE)) == 0); 8088 UPDATE_OB_PKT_COUNT(sire); 8089 sire->ire_last_used_time = lbolt; 8090 } 8091 /* 8092 * We have a route to reach the destination. 8093 * 8094 * 1) If the interface is part of ill group, try to get a new 8095 * ill taking load spreading into account. 8096 * 8097 * 2) After selecting the ill, get a source address that 8098 * might create good inbound load spreading. 8099 * ipif_select_source does this for us. 8100 * 8101 * If the application specified the ill (ifindex), we still 8102 * load spread. Only if the packets needs to go out 8103 * specifically on a given ill e.g. binding to 8104 * IPIF_NOFAILOVER address, then we don't try to use a 8105 * different ill for load spreading. 8106 */ 8107 if (attach_ill == NULL) { 8108 /* 8109 * Don't perform outbound load spreading in the 8110 * case of an RTF_MULTIRT route, as we actually 8111 * typically want to replicate outgoing packets 8112 * through particular interfaces. 8113 */ 8114 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8115 dst_ill = ire->ire_ipif->ipif_ill; 8116 /* for uniformity */ 8117 ill_refhold(dst_ill); 8118 } else { 8119 /* 8120 * If we are here trying to create an IRE_CACHE 8121 * for an offlink destination and have the 8122 * IRE_CACHE for the next hop and the latter is 8123 * using virtual IP source address selection i.e 8124 * it's ire->ire_ipif is pointing to a virtual 8125 * network interface (vni) then 8126 * ip_newroute_get_dst_ll() will return the vni 8127 * interface as the dst_ill. Since the vni is 8128 * virtual i.e not associated with any physical 8129 * interface, it cannot be the dst_ill, hence 8130 * in such a case call ip_newroute_get_dst_ll() 8131 * with the stq_ill instead of the ire_ipif ILL. 8132 * The function returns a refheld ill. 8133 */ 8134 if ((ire->ire_type == IRE_CACHE) && 8135 IS_VNI(ire->ire_ipif->ipif_ill)) 8136 dst_ill = ip_newroute_get_dst_ill( 8137 ire->ire_stq->q_ptr); 8138 else 8139 dst_ill = ip_newroute_get_dst_ill( 8140 ire->ire_ipif->ipif_ill); 8141 } 8142 if (dst_ill == NULL) { 8143 if (ip_debug > 2) { 8144 pr_addr_dbg("ip_newroute: " 8145 "no dst ill for dst" 8146 " %s\n", AF_INET, &dst); 8147 } 8148 goto icmp_err_ret; 8149 } 8150 } else { 8151 dst_ill = ire->ire_ipif->ipif_ill; 8152 /* for uniformity */ 8153 ill_refhold(dst_ill); 8154 /* 8155 * We should have found a route matching ill as we 8156 * called ire_ftable_lookup with MATCH_IRE_ILL. 8157 * Rather than asserting, when there is a mismatch, 8158 * we just drop the packet. 8159 */ 8160 if (dst_ill != attach_ill) { 8161 ip0dbg(("ip_newroute: Packet dropped as " 8162 "IPIF_NOFAILOVER ill is %s, " 8163 "ire->ire_ipif->ipif_ill is %s\n", 8164 attach_ill->ill_name, 8165 dst_ill->ill_name)); 8166 ill_refrele(attach_ill); 8167 goto icmp_err_ret; 8168 } 8169 } 8170 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ 8171 if (attach_ill != NULL) { 8172 ill_refrele(attach_ill); 8173 attach_ill = NULL; 8174 do_attach_ill = B_TRUE; 8175 } 8176 ASSERT(dst_ill != NULL); 8177 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 8178 8179 /* 8180 * Pick the best source address from dst_ill. 8181 * 8182 * 1) If it is part of a multipathing group, we would 8183 * like to spread the inbound packets across different 8184 * interfaces. ipif_select_source picks a random source 8185 * across the different ills in the group. 8186 * 8187 * 2) If it is not part of a multipathing group, we try 8188 * to pick the source address from the destination 8189 * route. Clustering assumes that when we have multiple 8190 * prefixes hosted on an interface, the prefix of the 8191 * source address matches the prefix of the destination 8192 * route. We do this only if the address is not 8193 * DEPRECATED. 8194 * 8195 * 3) If the conn is in a different zone than the ire, we 8196 * need to pick a source address from the right zone. 8197 * 8198 * NOTE : If we hit case (1) above, the prefix of the source 8199 * address picked may not match the prefix of the 8200 * destination routes prefix as ipif_select_source 8201 * does not look at "dst" while picking a source 8202 * address. 8203 * If we want the same behavior as (2), we will need 8204 * to change the behavior of ipif_select_source. 8205 */ 8206 ASSERT(src_ipif == NULL); 8207 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 8208 /* 8209 * The RTF_SETSRC flag is set in the parent ire (sire). 8210 * Check that the ipif matching the requested source 8211 * address still exists. 8212 */ 8213 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 8214 zoneid, NULL, NULL, NULL, NULL); 8215 } 8216 if (src_ipif == NULL) { 8217 ire_marks |= IRE_MARK_USESRC_CHECK; 8218 if ((dst_ill->ill_group != NULL) || 8219 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 8220 (connp != NULL && ire->ire_zoneid != zoneid && 8221 ire->ire_zoneid != ALL_ZONES) || 8222 (dst_ill->ill_usesrc_ifindex != 0)) { 8223 /* 8224 * If the destination is reachable via a 8225 * given gateway, the selected source address 8226 * should be in the same subnet as the gateway. 8227 * Otherwise, the destination is not reachable. 8228 * 8229 * If there are no interfaces on the same subnet 8230 * as the destination, ipif_select_source gives 8231 * first non-deprecated interface which might be 8232 * on a different subnet than the gateway. 8233 * This is not desirable. Hence pass the dst_ire 8234 * source address to ipif_select_source. 8235 * It is sure that the destination is reachable 8236 * with the dst_ire source address subnet. 8237 * So passing dst_ire source address to 8238 * ipif_select_source will make sure that the 8239 * selected source will be on the same subnet 8240 * as dst_ire source address. 8241 */ 8242 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 8243 src_ipif = ipif_select_source(dst_ill, saddr, 8244 zoneid); 8245 if (src_ipif == NULL) { 8246 if (ip_debug > 2) { 8247 pr_addr_dbg("ip_newroute: " 8248 "no src for dst %s ", 8249 AF_INET, &dst); 8250 printf("through interface %s\n", 8251 dst_ill->ill_name); 8252 } 8253 goto icmp_err_ret; 8254 } 8255 } else { 8256 src_ipif = ire->ire_ipif; 8257 ASSERT(src_ipif != NULL); 8258 /* hold src_ipif for uniformity */ 8259 ipif_refhold(src_ipif); 8260 } 8261 } 8262 8263 /* 8264 * Assign a source address while we have the conn. 8265 * We can't have ip_wput_ire pick a source address when the 8266 * packet returns from arp since we need to look at 8267 * conn_unspec_src and conn_zoneid, and we lose the conn when 8268 * going through arp. 8269 * 8270 * NOTE : ip_newroute_v6 does not have this piece of code as 8271 * it uses ip6i to store this information. 8272 */ 8273 if (ipha->ipha_src == INADDR_ANY && 8274 (connp == NULL || !connp->conn_unspec_src)) { 8275 ipha->ipha_src = src_ipif->ipif_src_addr; 8276 } 8277 if (ip_debug > 3) { 8278 /* ip2dbg */ 8279 pr_addr_dbg("ip_newroute: first hop %s\n", 8280 AF_INET, &gw); 8281 } 8282 ip2dbg(("\tire type %s (%d)\n", 8283 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 8284 8285 /* 8286 * The TTL of multirouted packets is bounded by the 8287 * ip_multirt_ttl ndd variable. 8288 */ 8289 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 8290 /* Force TTL of multirouted packets */ 8291 if ((ip_multirt_ttl > 0) && 8292 (ipha->ipha_ttl > ip_multirt_ttl)) { 8293 ip2dbg(("ip_newroute: forcing multirt TTL " 8294 "to %d (was %d), dst 0x%08x\n", 8295 ip_multirt_ttl, ipha->ipha_ttl, 8296 ntohl(sire->ire_addr))); 8297 ipha->ipha_ttl = ip_multirt_ttl; 8298 } 8299 } 8300 /* 8301 * At this point in ip_newroute(), ire is either the 8302 * IRE_CACHE of the next-hop gateway for an off-subnet 8303 * destination or an IRE_INTERFACE type that should be used 8304 * to resolve an on-subnet destination or an on-subnet 8305 * next-hop gateway. 8306 * 8307 * In the IRE_CACHE case, we have the following : 8308 * 8309 * 1) src_ipif - used for getting a source address. 8310 * 8311 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8312 * means packets using this IRE_CACHE will go out on 8313 * dst_ill. 8314 * 8315 * 3) The IRE sire will point to the prefix that is the 8316 * longest matching route for the destination. These 8317 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST. 8318 * 8319 * The newly created IRE_CACHE entry for the off-subnet 8320 * destination is tied to both the prefix route and the 8321 * interface route used to resolve the next-hop gateway 8322 * via the ire_phandle and ire_ihandle fields, 8323 * respectively. 8324 * 8325 * In the IRE_INTERFACE case, we have the following : 8326 * 8327 * 1) src_ipif - used for getting a source address. 8328 * 8329 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 8330 * means packets using the IRE_CACHE that we will build 8331 * here will go out on dst_ill. 8332 * 8333 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 8334 * to be created will only be tied to the IRE_INTERFACE 8335 * that was derived from the ire_ihandle field. 8336 * 8337 * If sire is non-NULL, it means the destination is 8338 * off-link and we will first create the IRE_CACHE for the 8339 * gateway. Next time through ip_newroute, we will create 8340 * the IRE_CACHE for the final destination as described 8341 * above. 8342 * 8343 * In both cases, after the current resolution has been 8344 * completed (or possibly initialised, in the IRE_INTERFACE 8345 * case), the loop may be re-entered to attempt the resolution 8346 * of another RTF_MULTIRT route. 8347 * 8348 * When an IRE_CACHE entry for the off-subnet destination is 8349 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 8350 * for further processing in emission loops. 8351 */ 8352 save_ire = ire; 8353 switch (ire->ire_type) { 8354 case IRE_CACHE: { 8355 ire_t *ipif_ire; 8356 mblk_t *ire_fp_mp; 8357 8358 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); 8359 if (gw == 0) 8360 gw = ire->ire_gateway_addr; 8361 /* 8362 * We need 3 ire's to create a new cache ire for an 8363 * off-link destination from the cache ire of the 8364 * gateway. 8365 * 8366 * 1. The prefix ire 'sire' (Note that this does 8367 * not apply to the conn_nexthop_set case) 8368 * 2. The cache ire of the gateway 'ire' 8369 * 3. The interface ire 'ipif_ire' 8370 * 8371 * We have (1) and (2). We lookup (3) below. 8372 * 8373 * If there is no interface route to the gateway, 8374 * it is a race condition, where we found the cache 8375 * but the interface route has been deleted. 8376 */ 8377 if (ip_nexthop) { 8378 ipif_ire = ire_ihandle_lookup_onlink(ire); 8379 } else { 8380 ipif_ire = 8381 ire_ihandle_lookup_offlink(ire, sire); 8382 } 8383 if (ipif_ire == NULL) { 8384 ip1dbg(("ip_newroute: " 8385 "ire_ihandle_lookup_offlink failed\n")); 8386 goto icmp_err_ret; 8387 } 8388 /* 8389 * XXX We are using the same res_mp 8390 * (DL_UNITDATA_REQ) though the save_ire is not 8391 * pointing at the same ill. 8392 * This is incorrect. We need to send it up to the 8393 * resolver to get the right res_mp. For ethernets 8394 * this may be okay (ill_type == DL_ETHER). 8395 */ 8396 res_mp = save_ire->ire_nce->nce_res_mp; 8397 ire_fp_mp = NULL; 8398 /* 8399 * save_ire's nce_fp_mp can't change since it is 8400 * not an IRE_MIPRTUN or IRE_BROADCAST 8401 * LOCK_IRE_FP_MP does not do any useful work in 8402 * the case of IRE_CACHE. So we don't use it below. 8403 */ 8404 if (save_ire->ire_stq == dst_ill->ill_wq) 8405 ire_fp_mp = save_ire->ire_nce->nce_fp_mp; 8406 8407 /* 8408 * Check cached gateway IRE for any security 8409 * attributes; if found, associate the gateway 8410 * credentials group to the destination IRE. 8411 */ 8412 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 8413 mutex_enter(&attrp->igsa_lock); 8414 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 8415 GCGRP_REFHOLD(gcgrp); 8416 mutex_exit(&attrp->igsa_lock); 8417 } 8418 8419 ire = ire_create( 8420 (uchar_t *)&dst, /* dest address */ 8421 (uchar_t *)&ip_g_all_ones, /* mask */ 8422 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8423 (uchar_t *)&gw, /* gateway address */ 8424 NULL, 8425 &save_ire->ire_max_frag, 8426 ire_fp_mp, /* Fast Path header */ 8427 dst_ill->ill_rq, /* recv-from queue */ 8428 dst_ill->ill_wq, /* send-to queue */ 8429 IRE_CACHE, /* IRE type */ 8430 res_mp, 8431 src_ipif, 8432 in_ill, /* incoming ill */ 8433 (sire != NULL) ? 8434 sire->ire_mask : 0, /* Parent mask */ 8435 (sire != NULL) ? 8436 sire->ire_phandle : 0, /* Parent handle */ 8437 ipif_ire->ire_ihandle, /* Interface handle */ 8438 (sire != NULL) ? (sire->ire_flags & 8439 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 8440 (sire != NULL) ? 8441 &(sire->ire_uinfo) : &(save_ire->ire_uinfo), 8442 NULL, 8443 gcgrp); 8444 8445 if (ire == NULL) { 8446 if (gcgrp != NULL) { 8447 GCGRP_REFRELE(gcgrp); 8448 gcgrp = NULL; 8449 } 8450 ire_refrele(ipif_ire); 8451 ire_refrele(save_ire); 8452 break; 8453 } 8454 8455 /* reference now held by IRE */ 8456 gcgrp = NULL; 8457 8458 ire->ire_marks |= ire_marks; 8459 8460 /* 8461 * Prevent sire and ipif_ire from getting deleted. 8462 * The newly created ire is tied to both of them via 8463 * the phandle and ihandle respectively. 8464 */ 8465 if (sire != NULL) { 8466 IRB_REFHOLD(sire->ire_bucket); 8467 /* Has it been removed already ? */ 8468 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 8469 IRB_REFRELE(sire->ire_bucket); 8470 ire_refrele(ipif_ire); 8471 ire_refrele(save_ire); 8472 break; 8473 } 8474 } 8475 8476 IRB_REFHOLD(ipif_ire->ire_bucket); 8477 /* Has it been removed already ? */ 8478 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 8479 IRB_REFRELE(ipif_ire->ire_bucket); 8480 if (sire != NULL) 8481 IRB_REFRELE(sire->ire_bucket); 8482 ire_refrele(ipif_ire); 8483 ire_refrele(save_ire); 8484 break; 8485 } 8486 8487 xmit_mp = first_mp; 8488 /* 8489 * In the case of multirouting, a copy 8490 * of the packet is done before its sending. 8491 * The copy is used to attempt another 8492 * route resolution, in a next loop. 8493 */ 8494 if (ire->ire_flags & RTF_MULTIRT) { 8495 copy_mp = copymsg(first_mp); 8496 if (copy_mp != NULL) { 8497 xmit_mp = copy_mp; 8498 MULTIRT_DEBUG_TAG(first_mp); 8499 } 8500 } 8501 ire_add_then_send(q, ire, xmit_mp); 8502 ire_refrele(save_ire); 8503 8504 /* Assert that sire is not deleted yet. */ 8505 if (sire != NULL) { 8506 ASSERT(sire->ire_ptpn != NULL); 8507 IRB_REFRELE(sire->ire_bucket); 8508 } 8509 8510 /* Assert that ipif_ire is not deleted yet. */ 8511 ASSERT(ipif_ire->ire_ptpn != NULL); 8512 IRB_REFRELE(ipif_ire->ire_bucket); 8513 ire_refrele(ipif_ire); 8514 8515 /* 8516 * If copy_mp is not NULL, multirouting was 8517 * requested. We loop to initiate a next 8518 * route resolution attempt, starting from sire. 8519 */ 8520 if (copy_mp != NULL) { 8521 /* 8522 * Search for the next unresolved 8523 * multirt route. 8524 */ 8525 copy_mp = NULL; 8526 ipif_ire = NULL; 8527 ire = NULL; 8528 multirt_resolve_next = B_TRUE; 8529 continue; 8530 } 8531 if (sire != NULL) 8532 ire_refrele(sire); 8533 ipif_refrele(src_ipif); 8534 ill_refrele(dst_ill); 8535 return; 8536 } 8537 case IRE_IF_NORESOLVER: { 8538 /* 8539 * We have what we need to build an IRE_CACHE. 8540 * 8541 * Create a new res_mp with the IP gateway address 8542 * in destination address in the DLPI hdr if the 8543 * physical length is exactly 4 bytes. 8544 */ 8545 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 8546 uchar_t *addr; 8547 8548 if (gw) 8549 addr = (uchar_t *)&gw; 8550 else 8551 addr = (uchar_t *)&dst; 8552 8553 res_mp = ill_dlur_gen(addr, 8554 dst_ill->ill_phys_addr_length, 8555 dst_ill->ill_sap, 8556 dst_ill->ill_sap_length); 8557 8558 if (res_mp == NULL) { 8559 ip1dbg(("ip_newroute: res_mp NULL\n")); 8560 break; 8561 } 8562 } else if (dst_ill->ill_resolver_mp == NULL) { 8563 ip1dbg(("ip_newroute: dst_ill %p " 8564 "for IF_NORESOLV ire %p has " 8565 "no ill_resolver_mp\n", 8566 (void *)dst_ill, (void *)ire)); 8567 break; 8568 } else { 8569 res_mp = NULL; 8570 } 8571 8572 /* 8573 * TSol note: We are creating the ire cache for the 8574 * destination 'dst'. If 'dst' is offlink, going 8575 * through the first hop 'gw', the security attributes 8576 * of 'dst' must be set to point to the gateway 8577 * credentials of gateway 'gw'. If 'dst' is onlink, it 8578 * is possible that 'dst' is a potential gateway that is 8579 * referenced by some route that has some security 8580 * attributes. Thus in the former case, we need to do a 8581 * gcgrp_lookup of 'gw' while in the latter case we 8582 * need to do gcgrp_lookup of 'dst' itself. 8583 */ 8584 ga.ga_af = AF_INET; 8585 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, 8586 &ga.ga_addr); 8587 gcgrp = gcgrp_lookup(&ga, B_FALSE); 8588 8589 ire = ire_create( 8590 (uchar_t *)&dst, /* dest address */ 8591 (uchar_t *)&ip_g_all_ones, /* mask */ 8592 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8593 (uchar_t *)&gw, /* gateway address */ 8594 NULL, 8595 &save_ire->ire_max_frag, 8596 NULL, /* Fast Path header */ 8597 dst_ill->ill_rq, /* recv-from queue */ 8598 dst_ill->ill_wq, /* send-to queue */ 8599 IRE_CACHE, 8600 res_mp, 8601 src_ipif, 8602 in_ill, /* Incoming ill */ 8603 save_ire->ire_mask, /* Parent mask */ 8604 (sire != NULL) ? /* Parent handle */ 8605 sire->ire_phandle : 0, 8606 save_ire->ire_ihandle, /* Interface handle */ 8607 (sire != NULL) ? sire->ire_flags & 8608 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 8609 &(save_ire->ire_uinfo), 8610 NULL, 8611 gcgrp); 8612 8613 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) 8614 freeb(res_mp); 8615 8616 if (ire == NULL) { 8617 if (gcgrp != NULL) { 8618 GCGRP_REFRELE(gcgrp); 8619 gcgrp = NULL; 8620 } 8621 ire_refrele(save_ire); 8622 break; 8623 } 8624 8625 /* reference now held by IRE */ 8626 gcgrp = NULL; 8627 8628 ire->ire_marks |= ire_marks; 8629 8630 /* Prevent save_ire from getting deleted */ 8631 IRB_REFHOLD(save_ire->ire_bucket); 8632 /* Has it been removed already ? */ 8633 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8634 IRB_REFRELE(save_ire->ire_bucket); 8635 ire_refrele(save_ire); 8636 break; 8637 } 8638 8639 /* 8640 * In the case of multirouting, a copy 8641 * of the packet is made before it is sent. 8642 * The copy is used in the next 8643 * loop to attempt another resolution. 8644 */ 8645 xmit_mp = first_mp; 8646 if ((sire != NULL) && 8647 (sire->ire_flags & RTF_MULTIRT)) { 8648 copy_mp = copymsg(first_mp); 8649 if (copy_mp != NULL) { 8650 xmit_mp = copy_mp; 8651 MULTIRT_DEBUG_TAG(first_mp); 8652 } 8653 } 8654 ire_add_then_send(q, ire, xmit_mp); 8655 8656 /* Assert that it is not deleted yet. */ 8657 ASSERT(save_ire->ire_ptpn != NULL); 8658 IRB_REFRELE(save_ire->ire_bucket); 8659 ire_refrele(save_ire); 8660 8661 if (copy_mp != NULL) { 8662 /* 8663 * If we found a (no)resolver, we ignore any 8664 * trailing top priority IRE_CACHE in further 8665 * loops. This ensures that we do not omit any 8666 * (no)resolver. 8667 * This IRE_CACHE, if any, will be processed 8668 * by another thread entering ip_newroute(). 8669 * IRE_CACHE entries, if any, will be processed 8670 * by another thread entering ip_newroute(), 8671 * (upon resolver response, for instance). 8672 * This aims to force parallel multirt 8673 * resolutions as soon as a packet must be sent. 8674 * In the best case, after the tx of only one 8675 * packet, all reachable routes are resolved. 8676 * Otherwise, the resolution of all RTF_MULTIRT 8677 * routes would require several emissions. 8678 */ 8679 multirt_flags &= ~MULTIRT_CACHEGW; 8680 8681 /* 8682 * Search for the next unresolved multirt 8683 * route. 8684 */ 8685 copy_mp = NULL; 8686 save_ire = NULL; 8687 ire = NULL; 8688 multirt_resolve_next = B_TRUE; 8689 continue; 8690 } 8691 8692 /* 8693 * Don't need sire anymore 8694 */ 8695 if (sire != NULL) 8696 ire_refrele(sire); 8697 8698 ipif_refrele(src_ipif); 8699 ill_refrele(dst_ill); 8700 return; 8701 } 8702 case IRE_IF_RESOLVER: 8703 /* 8704 * We can't build an IRE_CACHE yet, but at least we 8705 * found a resolver that can help. 8706 */ 8707 res_mp = dst_ill->ill_resolver_mp; 8708 if (!OK_RESOLVER_MP(res_mp)) 8709 break; 8710 8711 /* 8712 * To be at this point in the code with a non-zero gw 8713 * means that dst is reachable through a gateway that 8714 * we have never resolved. By changing dst to the gw 8715 * addr we resolve the gateway first. 8716 * When ire_add_then_send() tries to put the IP dg 8717 * to dst, it will reenter ip_newroute() at which 8718 * time we will find the IRE_CACHE for the gw and 8719 * create another IRE_CACHE in case IRE_CACHE above. 8720 */ 8721 if (gw != INADDR_ANY) { 8722 /* 8723 * The source ipif that was determined above was 8724 * relative to the destination address, not the 8725 * gateway's. If src_ipif was not taken out of 8726 * the IRE_IF_RESOLVER entry, we'll need to call 8727 * ipif_select_source() again. 8728 */ 8729 if (src_ipif != ire->ire_ipif) { 8730 ipif_refrele(src_ipif); 8731 src_ipif = ipif_select_source(dst_ill, 8732 gw, zoneid); 8733 if (src_ipif == NULL) { 8734 if (ip_debug > 2) { 8735 pr_addr_dbg( 8736 "ip_newroute: no " 8737 "src for gw %s ", 8738 AF_INET, &gw); 8739 printf("through " 8740 "interface %s\n", 8741 dst_ill->ill_name); 8742 } 8743 goto icmp_err_ret; 8744 } 8745 } 8746 save_dst = dst; 8747 dst = gw; 8748 gw = INADDR_ANY; 8749 } 8750 8751 /* 8752 * We obtain a partial IRE_CACHE which we will pass 8753 * along with the resolver query. When the response 8754 * comes back it will be there ready for us to add. 8755 * The ire_max_frag is atomically set under the 8756 * irebucket lock in ire_add_v[46]. 8757 */ 8758 8759 ire = ire_create_mp( 8760 (uchar_t *)&dst, /* dest address */ 8761 (uchar_t *)&ip_g_all_ones, /* mask */ 8762 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8763 (uchar_t *)&gw, /* gateway address */ 8764 NULL, /* no in_src_addr */ 8765 NULL, /* ire_max_frag */ 8766 NULL, /* Fast Path header */ 8767 dst_ill->ill_rq, /* recv-from queue */ 8768 dst_ill->ill_wq, /* send-to queue */ 8769 IRE_CACHE, 8770 NULL, 8771 src_ipif, /* Interface ipif */ 8772 in_ill, /* Incoming ILL */ 8773 save_ire->ire_mask, /* Parent mask */ 8774 0, 8775 save_ire->ire_ihandle, /* Interface handle */ 8776 0, /* flags if any */ 8777 &(save_ire->ire_uinfo), 8778 NULL, 8779 NULL); 8780 8781 if (ire == NULL) { 8782 ire_refrele(save_ire); 8783 break; 8784 } 8785 8786 if ((sire != NULL) && 8787 (sire->ire_flags & RTF_MULTIRT)) { 8788 copy_mp = copymsg(first_mp); 8789 if (copy_mp != NULL) 8790 MULTIRT_DEBUG_TAG(copy_mp); 8791 } 8792 8793 ire->ire_marks |= ire_marks; 8794 8795 /* 8796 * Construct message chain for the resolver 8797 * of the form: 8798 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8799 * Packet could contain a IPSEC_OUT mp. 8800 * 8801 * NOTE : ire will be added later when the response 8802 * comes back from ARP. If the response does not 8803 * come back, ARP frees the packet. For this reason, 8804 * we can't REFHOLD the bucket of save_ire to prevent 8805 * deletions. We may not be able to REFRELE the bucket 8806 * if the response never comes back. Thus, before 8807 * adding the ire, ire_add_v4 will make sure that the 8808 * interface route does not get deleted. This is the 8809 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 8810 * where we can always prevent deletions because of 8811 * the synchronous nature of adding IRES i.e 8812 * ire_add_then_send is called after creating the IRE. 8813 */ 8814 ASSERT(ire->ire_mp != NULL); 8815 ire->ire_mp->b_cont = first_mp; 8816 /* Have saved_mp handy, for cleanup if canput fails */ 8817 saved_mp = mp; 8818 mp = copyb(res_mp); 8819 if (mp == NULL) { 8820 /* Prepare for cleanup */ 8821 mp = saved_mp; /* pkt */ 8822 ire_delete(ire); /* ire_mp */ 8823 ire = NULL; 8824 ire_refrele(save_ire); 8825 if (copy_mp != NULL) { 8826 MULTIRT_DEBUG_UNTAG(copy_mp); 8827 freemsg(copy_mp); 8828 copy_mp = NULL; 8829 } 8830 break; 8831 } 8832 linkb(mp, ire->ire_mp); 8833 8834 /* 8835 * Fill in the source and dest addrs for the resolver. 8836 * NOTE: this depends on memory layouts imposed by 8837 * ill_init(). 8838 */ 8839 areq = (areq_t *)mp->b_rptr; 8840 addrp = (ipaddr_t *)((char *)areq + 8841 areq->areq_sender_addr_offset); 8842 if (do_attach_ill) { 8843 /* 8844 * This is bind to no failover case. 8845 * arp packet also must go out on attach_ill. 8846 */ 8847 ASSERT(ipha->ipha_src != NULL); 8848 *addrp = ipha->ipha_src; 8849 } else { 8850 *addrp = save_ire->ire_src_addr; 8851 } 8852 8853 ire_refrele(save_ire); 8854 addrp = (ipaddr_t *)((char *)areq + 8855 areq->areq_target_addr_offset); 8856 *addrp = dst; 8857 /* Up to the resolver. */ 8858 if (canputnext(dst_ill->ill_rq) && 8859 !(dst_ill->ill_arp_closing)) { 8860 putnext(dst_ill->ill_rq, mp); 8861 ire = NULL; 8862 if (copy_mp != NULL) { 8863 /* 8864 * If we found a resolver, we ignore 8865 * any trailing top priority IRE_CACHE 8866 * in the further loops. This ensures 8867 * that we do not omit any resolver. 8868 * IRE_CACHE entries, if any, will be 8869 * processed next time we enter 8870 * ip_newroute(). 8871 */ 8872 multirt_flags &= ~MULTIRT_CACHEGW; 8873 /* 8874 * Search for the next unresolved 8875 * multirt route. 8876 */ 8877 first_mp = copy_mp; 8878 copy_mp = NULL; 8879 /* Prepare the next resolution loop. */ 8880 mp = first_mp; 8881 EXTRACT_PKT_MP(mp, first_mp, 8882 mctl_present); 8883 if (mctl_present) 8884 io = (ipsec_out_t *) 8885 first_mp->b_rptr; 8886 ipha = (ipha_t *)mp->b_rptr; 8887 8888 ASSERT(sire != NULL); 8889 8890 dst = save_dst; 8891 multirt_resolve_next = B_TRUE; 8892 continue; 8893 } 8894 8895 if (sire != NULL) 8896 ire_refrele(sire); 8897 8898 /* 8899 * The response will come back in ip_wput 8900 * with db_type IRE_DB_TYPE. 8901 */ 8902 ipif_refrele(src_ipif); 8903 ill_refrele(dst_ill); 8904 return; 8905 } else { 8906 /* Prepare for cleanup */ 8907 DTRACE_PROBE1(ip__newroute__drop, mblk_t *, 8908 mp); 8909 mp->b_cont = NULL; 8910 freeb(mp); /* areq */ 8911 /* 8912 * this is an ire that is not added to the 8913 * cache. ire_freemblk will handle the release 8914 * of any resources associated with the ire. 8915 */ 8916 ire_delete(ire); /* ire_mp */ 8917 mp = saved_mp; /* pkt */ 8918 ire = NULL; 8919 if (copy_mp != NULL) { 8920 MULTIRT_DEBUG_UNTAG(copy_mp); 8921 freemsg(copy_mp); 8922 copy_mp = NULL; 8923 } 8924 break; 8925 } 8926 default: 8927 break; 8928 } 8929 } while (multirt_resolve_next); 8930 8931 ip1dbg(("ip_newroute: dropped\n")); 8932 /* Did this packet originate externally? */ 8933 if (mp->b_prev) { 8934 mp->b_next = NULL; 8935 mp->b_prev = NULL; 8936 if (in_ill != NULL) { 8937 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInDiscards); 8938 } else { 8939 BUMP_MIB(&ip_mib, ipIfStatsInDiscards); 8940 } 8941 } else { 8942 if (dst_ill != NULL) { 8943 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards); 8944 } else { 8945 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 8946 } 8947 } 8948 ASSERT(copy_mp == NULL); 8949 MULTIRT_DEBUG_UNTAG(first_mp); 8950 freemsg(first_mp); 8951 if (ire != NULL) 8952 ire_refrele(ire); 8953 if (sire != NULL) 8954 ire_refrele(sire); 8955 if (src_ipif != NULL) 8956 ipif_refrele(src_ipif); 8957 if (dst_ill != NULL) 8958 ill_refrele(dst_ill); 8959 return; 8960 8961 icmp_err_ret: 8962 ip1dbg(("ip_newroute: no route\n")); 8963 if (src_ipif != NULL) 8964 ipif_refrele(src_ipif); 8965 if (dst_ill != NULL) 8966 ill_refrele(dst_ill); 8967 if (sire != NULL) 8968 ire_refrele(sire); 8969 /* Did this packet originate externally? */ 8970 if (mp->b_prev) { 8971 mp->b_next = NULL; 8972 mp->b_prev = NULL; 8973 if (in_ill != NULL) { 8974 BUMP_MIB(in_ill->ill_ip_mib, ipIfStatsInNoRoutes); 8975 } else { 8976 BUMP_MIB(&ip_mib, ipIfStatsInNoRoutes); 8977 } 8978 q = WR(q); 8979 } else { 8980 /* 8981 * There is no outgoing ill, so just increment the 8982 * system MIB. 8983 */ 8984 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 8985 /* 8986 * Since ip_wput() isn't close to finished, we fill 8987 * in enough of the header for credible error reporting. 8988 */ 8989 if (ip_hdr_complete(ipha, zoneid)) { 8990 /* Failed */ 8991 MULTIRT_DEBUG_UNTAG(first_mp); 8992 freemsg(first_mp); 8993 if (ire != NULL) 8994 ire_refrele(ire); 8995 return; 8996 } 8997 } 8998 8999 /* 9000 * At this point we will have ire only if RTF_BLACKHOLE 9001 * or RTF_REJECT flags are set on the IRE. It will not 9002 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9003 */ 9004 if (ire != NULL) { 9005 if (ire->ire_flags & RTF_BLACKHOLE) { 9006 ire_refrele(ire); 9007 MULTIRT_DEBUG_UNTAG(first_mp); 9008 freemsg(first_mp); 9009 return; 9010 } 9011 ire_refrele(ire); 9012 } 9013 if (ip_source_routed(ipha)) { 9014 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, 9015 zoneid); 9016 return; 9017 } 9018 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid); 9019 } 9020 9021 ip_opt_info_t zero_info; 9022 9023 /* 9024 * IPv4 - 9025 * ip_newroute_ipif is called by ip_wput_multicast and 9026 * ip_rput_forward_multicast whenever we need to send 9027 * out a packet to a destination address for which we do not have specific 9028 * routing information. It is used when the packet will be sent out 9029 * on a specific interface. It is also called by ip_wput() when IP_XMIT_IF 9030 * socket option is set or icmp error message wants to go out on a particular 9031 * interface for a unicast packet. 9032 * 9033 * In most cases, the destination address is resolved thanks to the ipif 9034 * intrinsic resolver. However, there are some cases where the call to 9035 * ip_newroute_ipif must take into account the potential presence of 9036 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 9037 * that uses the interface. This is specified through flags, 9038 * which can be a combination of: 9039 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 9040 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 9041 * and flags. Additionally, the packet source address has to be set to 9042 * the specified address. The caller is thus expected to set this flag 9043 * if the packet has no specific source address yet. 9044 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 9045 * flag, the resulting ire will inherit the flag. All unresolved routes 9046 * to the destination must be explored in the same call to 9047 * ip_newroute_ipif(). 9048 */ 9049 static void 9050 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 9051 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop) 9052 { 9053 areq_t *areq; 9054 ire_t *ire = NULL; 9055 mblk_t *res_mp; 9056 ipaddr_t *addrp; 9057 mblk_t *first_mp; 9058 ire_t *save_ire = NULL; 9059 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ 9060 ipif_t *src_ipif = NULL; 9061 ushort_t ire_marks = 0; 9062 ill_t *dst_ill = NULL; 9063 boolean_t mctl_present; 9064 ipsec_out_t *io; 9065 ipha_t *ipha; 9066 int ihandle = 0; 9067 mblk_t *saved_mp; 9068 ire_t *fire = NULL; 9069 mblk_t *copy_mp = NULL; 9070 boolean_t multirt_resolve_next; 9071 ipaddr_t ipha_dst; 9072 9073 /* 9074 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 9075 * here for uniformity 9076 */ 9077 ipif_refhold(ipif); 9078 9079 /* 9080 * This loop is run only once in most cases. 9081 * We loop to resolve further routes only when the destination 9082 * can be reached through multiple RTF_MULTIRT-flagged ires. 9083 */ 9084 do { 9085 if (dst_ill != NULL) { 9086 ill_refrele(dst_ill); 9087 dst_ill = NULL; 9088 } 9089 if (src_ipif != NULL) { 9090 ipif_refrele(src_ipif); 9091 src_ipif = NULL; 9092 } 9093 multirt_resolve_next = B_FALSE; 9094 9095 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 9096 ipif->ipif_ill->ill_name)); 9097 9098 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 9099 if (mctl_present) 9100 io = (ipsec_out_t *)first_mp->b_rptr; 9101 9102 ipha = (ipha_t *)mp->b_rptr; 9103 9104 /* 9105 * Save the packet destination address, we may need it after 9106 * the packet has been consumed. 9107 */ 9108 ipha_dst = ipha->ipha_dst; 9109 9110 /* 9111 * If the interface is a pt-pt interface we look for an 9112 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 9113 * local_address and the pt-pt destination address. Otherwise 9114 * we just match the local address. 9115 * NOTE: dst could be different than ipha->ipha_dst in case 9116 * of sending igmp multicast packets over a point-to-point 9117 * connection. 9118 * Thus we must be careful enough to check ipha_dst to be a 9119 * multicast address, otherwise it will take xmit_if path for 9120 * multicast packets resulting into kernel stack overflow by 9121 * repeated calls to ip_newroute_ipif from ire_send(). 9122 */ 9123 if (CLASSD(ipha_dst) && 9124 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 9125 goto err_ret; 9126 } 9127 9128 /* 9129 * We check if an IRE_OFFSUBNET for the addr that goes through 9130 * ipif exists. We need it to determine if the RTF_SETSRC and/or 9131 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 9132 * propagate its flags to the new ire. 9133 */ 9134 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 9135 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 9136 ip2dbg(("ip_newroute_ipif: " 9137 "ipif_lookup_multi_ire(" 9138 "ipif %p, dst %08x) = fire %p\n", 9139 (void *)ipif, ntohl(dst), (void *)fire)); 9140 } 9141 9142 if (mctl_present && io->ipsec_out_attach_if) { 9143 attach_ill = ip_grab_attach_ill(NULL, first_mp, 9144 io->ipsec_out_ill_index, B_FALSE); 9145 9146 /* Failure case frees things for us. */ 9147 if (attach_ill == NULL) { 9148 ipif_refrele(ipif); 9149 if (fire != NULL) 9150 ire_refrele(fire); 9151 return; 9152 } 9153 9154 /* 9155 * Check if we need an ire that will not be 9156 * looked up by anybody else i.e. HIDDEN. 9157 */ 9158 if (ill_is_probeonly(attach_ill)) { 9159 ire_marks = IRE_MARK_HIDDEN; 9160 } 9161 /* 9162 * ip_wput passes the right ipif for IPIF_NOFAILOVER 9163 * case. 9164 */ 9165 dst_ill = ipif->ipif_ill; 9166 /* attach_ill has been refheld by ip_grab_attach_ill */ 9167 ASSERT(dst_ill == attach_ill); 9168 } else { 9169 /* 9170 * If this is set by IP_XMIT_IF, then make sure that 9171 * ipif is pointing to the same ill as the IP_XMIT_IF 9172 * specified ill. 9173 */ 9174 ASSERT((connp == NULL) || 9175 (connp->conn_xmit_if_ill == NULL) || 9176 (connp->conn_xmit_if_ill == ipif->ipif_ill)); 9177 /* 9178 * If the interface belongs to an interface group, 9179 * make sure the next possible interface in the group 9180 * is used. This encourages load spreading among 9181 * peers in an interface group. 9182 * Note: load spreading is disabled for RTF_MULTIRT 9183 * routes. 9184 */ 9185 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9186 (fire->ire_flags & RTF_MULTIRT)) { 9187 /* 9188 * Don't perform outbound load spreading 9189 * in the case of an RTF_MULTIRT issued route, 9190 * we actually typically want to replicate 9191 * outgoing packets through particular 9192 * interfaces. 9193 */ 9194 dst_ill = ipif->ipif_ill; 9195 ill_refhold(dst_ill); 9196 } else { 9197 dst_ill = ip_newroute_get_dst_ill( 9198 ipif->ipif_ill); 9199 } 9200 if (dst_ill == NULL) { 9201 if (ip_debug > 2) { 9202 pr_addr_dbg("ip_newroute_ipif: " 9203 "no dst ill for dst %s\n", 9204 AF_INET, &dst); 9205 } 9206 goto err_ret; 9207 } 9208 } 9209 9210 /* 9211 * Pick a source address preferring non-deprecated ones. 9212 * Unlike ip_newroute, we don't do any source address 9213 * selection here since for multicast it really does not help 9214 * in inbound load spreading as in the unicast case. 9215 */ 9216 if ((flags & RTF_SETSRC) && (fire != NULL) && 9217 (fire->ire_flags & RTF_SETSRC)) { 9218 /* 9219 * As requested by flags, an IRE_OFFSUBNET was looked up 9220 * on that interface. This ire has RTF_SETSRC flag, so 9221 * the source address of the packet must be changed. 9222 * Check that the ipif matching the requested source 9223 * address still exists. 9224 */ 9225 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 9226 zoneid, NULL, NULL, NULL, NULL); 9227 } 9228 if (((ipif->ipif_flags & IPIF_DEPRECATED) || 9229 (connp != NULL && ipif->ipif_zoneid != zoneid && 9230 ipif->ipif_zoneid != ALL_ZONES)) && 9231 (src_ipif == NULL)) { 9232 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 9233 if (src_ipif == NULL) { 9234 if (ip_debug > 2) { 9235 /* ip1dbg */ 9236 pr_addr_dbg("ip_newroute_ipif: " 9237 "no src for dst %s", 9238 AF_INET, &dst); 9239 } 9240 ip1dbg((" through interface %s\n", 9241 dst_ill->ill_name)); 9242 goto err_ret; 9243 } 9244 ipif_refrele(ipif); 9245 ipif = src_ipif; 9246 ipif_refhold(ipif); 9247 } 9248 if (src_ipif == NULL) { 9249 src_ipif = ipif; 9250 ipif_refhold(src_ipif); 9251 } 9252 9253 /* 9254 * Assign a source address while we have the conn. 9255 * We can't have ip_wput_ire pick a source address when the 9256 * packet returns from arp since conn_unspec_src might be set 9257 * and we loose the conn when going through arp. 9258 */ 9259 if (ipha->ipha_src == INADDR_ANY && 9260 (connp == NULL || !connp->conn_unspec_src)) { 9261 ipha->ipha_src = src_ipif->ipif_src_addr; 9262 } 9263 9264 /* 9265 * In case of IP_XMIT_IF, it is possible that the outgoing 9266 * interface does not have an interface ire. 9267 * Example: Thousands of mobileip PPP interfaces to mobile 9268 * nodes. We don't want to create interface ires because 9269 * packets from other mobile nodes must not take the route 9270 * via interface ires to the visiting mobile node without 9271 * going through the home agent, in absence of mobileip 9272 * route optimization. 9273 */ 9274 if (CLASSD(ipha_dst) && (connp == NULL || 9275 connp->conn_xmit_if_ill == NULL) && 9276 infop->ip_opt_ill_index == 0) { 9277 /* ipif_to_ire returns an held ire */ 9278 ire = ipif_to_ire(ipif); 9279 if (ire == NULL) 9280 goto err_ret; 9281 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 9282 goto err_ret; 9283 /* 9284 * ihandle is needed when the ire is added to 9285 * cache table. 9286 */ 9287 save_ire = ire; 9288 ihandle = save_ire->ire_ihandle; 9289 9290 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 9291 "flags %04x\n", 9292 (void *)ire, (void *)ipif, flags)); 9293 if ((flags & RTF_MULTIRT) && (fire != NULL) && 9294 (fire->ire_flags & RTF_MULTIRT)) { 9295 /* 9296 * As requested by flags, an IRE_OFFSUBNET was 9297 * looked up on that interface. This ire has 9298 * RTF_MULTIRT flag, so the resolution loop will 9299 * be re-entered to resolve additional routes on 9300 * other interfaces. For that purpose, a copy of 9301 * the packet is performed at this point. 9302 */ 9303 fire->ire_last_used_time = lbolt; 9304 copy_mp = copymsg(first_mp); 9305 if (copy_mp) { 9306 MULTIRT_DEBUG_TAG(copy_mp); 9307 } 9308 } 9309 if ((flags & RTF_SETSRC) && (fire != NULL) && 9310 (fire->ire_flags & RTF_SETSRC)) { 9311 /* 9312 * As requested by flags, an IRE_OFFSUBET was 9313 * looked up on that interface. This ire has 9314 * RTF_SETSRC flag, so the source address of the 9315 * packet must be changed. 9316 */ 9317 ipha->ipha_src = fire->ire_src_addr; 9318 } 9319 } else { 9320 ASSERT((connp == NULL) || 9321 (connp->conn_xmit_if_ill != NULL) || 9322 (connp->conn_dontroute) || 9323 infop->ip_opt_ill_index != 0); 9324 /* 9325 * The only ways we can come here are: 9326 * 1) IP_XMIT_IF socket option is set 9327 * 2) ICMP error message generated from 9328 * ip_mrtun_forward() routine and it needs 9329 * to go through the specified ill. 9330 * 3) SO_DONTROUTE socket option is set 9331 * 4) IP_PKTINFO option is passed in as ancillary data. 9332 * In all cases, the new ire will not be added 9333 * into cache table. 9334 */ 9335 ire_marks |= IRE_MARK_NOADD; 9336 } 9337 9338 switch (ipif->ipif_net_type) { 9339 case IRE_IF_NORESOLVER: { 9340 /* We have what we need to build an IRE_CACHE. */ 9341 mblk_t *res_mp; 9342 9343 /* 9344 * Create a new res_mp with the 9345 * IP gateway address as destination address in the 9346 * DLPI hdr if the physical length is exactly 4 bytes. 9347 */ 9348 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 9349 res_mp = ill_dlur_gen((uchar_t *)&dst, 9350 dst_ill->ill_phys_addr_length, 9351 dst_ill->ill_sap, 9352 dst_ill->ill_sap_length); 9353 } else if (dst_ill->ill_resolver_mp == NULL) { 9354 ip1dbg(("ip_newroute: dst_ill %p " 9355 "for IF_NORESOLV ire %p has " 9356 "no ill_resolver_mp\n", 9357 (void *)dst_ill, (void *)ire)); 9358 break; 9359 } else { 9360 /* use the value set in ip_ll_subnet_defaults */ 9361 res_mp = ill_dlur_gen(NULL, 9362 dst_ill->ill_phys_addr_length, 9363 dst_ill->ill_sap, 9364 dst_ill->ill_sap_length); 9365 } 9366 9367 if (res_mp == NULL) 9368 break; 9369 /* 9370 * The new ire inherits the IRE_OFFSUBNET flags 9371 * and source address, if this was requested. 9372 */ 9373 ire = ire_create( 9374 (uchar_t *)&dst, /* dest address */ 9375 (uchar_t *)&ip_g_all_ones, /* mask */ 9376 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9377 NULL, /* gateway address */ 9378 NULL, 9379 &ipif->ipif_mtu, 9380 NULL, /* Fast Path header */ 9381 dst_ill->ill_rq, /* recv-from queue */ 9382 dst_ill->ill_wq, /* send-to queue */ 9383 IRE_CACHE, 9384 res_mp, 9385 src_ipif, 9386 NULL, 9387 (save_ire != NULL ? save_ire->ire_mask : 0), 9388 (fire != NULL) ? /* Parent handle */ 9389 fire->ire_phandle : 0, 9390 ihandle, /* Interface handle */ 9391 (fire != NULL) ? 9392 (fire->ire_flags & 9393 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9394 (save_ire == NULL ? &ire_uinfo_null : 9395 &save_ire->ire_uinfo), 9396 NULL, 9397 NULL); 9398 9399 freeb(res_mp); 9400 9401 if (ire == NULL) { 9402 if (save_ire != NULL) 9403 ire_refrele(save_ire); 9404 break; 9405 } 9406 9407 ire->ire_marks |= ire_marks; 9408 9409 /* 9410 * If IRE_MARK_NOADD is set then we need to convert 9411 * the max_fragp to a useable value now. This is 9412 * normally done in ire_add_v[46]. We also need to 9413 * associate the ire with an nce (normally would be 9414 * done in ip_wput_nondata()). 9415 * 9416 * Note that IRE_MARK_NOADD packets created here 9417 * do not have a non-null ire_mp pointer. The null 9418 * value of ire_bucket indicates that they were 9419 * never added. 9420 */ 9421 if (ire->ire_marks & IRE_MARK_NOADD) { 9422 uint_t max_frag; 9423 9424 max_frag = *ire->ire_max_fragp; 9425 ire->ire_max_fragp = NULL; 9426 ire->ire_max_frag = max_frag; 9427 9428 if ((ire->ire_nce = ndp_lookup_v4( 9429 ire_to_ill(ire), 9430 (ire->ire_gateway_addr != INADDR_ANY ? 9431 &ire->ire_gateway_addr : &ire->ire_addr), 9432 B_FALSE)) == NULL) { 9433 if (save_ire != NULL) 9434 ire_refrele(save_ire); 9435 break; 9436 } 9437 ASSERT(ire->ire_nce->nce_state == 9438 ND_REACHABLE); 9439 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 9440 } 9441 9442 /* Prevent save_ire from getting deleted */ 9443 if (save_ire != NULL) { 9444 IRB_REFHOLD(save_ire->ire_bucket); 9445 /* Has it been removed already ? */ 9446 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 9447 IRB_REFRELE(save_ire->ire_bucket); 9448 ire_refrele(save_ire); 9449 break; 9450 } 9451 } 9452 9453 ire_add_then_send(q, ire, first_mp); 9454 9455 /* Assert that save_ire is not deleted yet. */ 9456 if (save_ire != NULL) { 9457 ASSERT(save_ire->ire_ptpn != NULL); 9458 IRB_REFRELE(save_ire->ire_bucket); 9459 ire_refrele(save_ire); 9460 save_ire = NULL; 9461 } 9462 if (fire != NULL) { 9463 ire_refrele(fire); 9464 fire = NULL; 9465 } 9466 9467 /* 9468 * the resolution loop is re-entered if this 9469 * was requested through flags and if we 9470 * actually are in a multirouting case. 9471 */ 9472 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9473 boolean_t need_resolve = 9474 ire_multirt_need_resolve(ipha_dst, 9475 MBLK_GETLABEL(copy_mp)); 9476 if (!need_resolve) { 9477 MULTIRT_DEBUG_UNTAG(copy_mp); 9478 freemsg(copy_mp); 9479 copy_mp = NULL; 9480 } else { 9481 /* 9482 * ipif_lookup_group() calls 9483 * ire_lookup_multi() that uses 9484 * ire_ftable_lookup() to find 9485 * an IRE_INTERFACE for the group. 9486 * In the multirt case, 9487 * ire_lookup_multi() then invokes 9488 * ire_multirt_lookup() to find 9489 * the next resolvable ire. 9490 * As a result, we obtain an new 9491 * interface, derived from the 9492 * next ire. 9493 */ 9494 ipif_refrele(ipif); 9495 ipif = ipif_lookup_group(ipha_dst, 9496 zoneid); 9497 ip2dbg(("ip_newroute_ipif: " 9498 "multirt dst %08x, ipif %p\n", 9499 htonl(dst), (void *)ipif)); 9500 if (ipif != NULL) { 9501 mp = copy_mp; 9502 copy_mp = NULL; 9503 multirt_resolve_next = B_TRUE; 9504 continue; 9505 } else { 9506 freemsg(copy_mp); 9507 } 9508 } 9509 } 9510 if (ipif != NULL) 9511 ipif_refrele(ipif); 9512 ill_refrele(dst_ill); 9513 ipif_refrele(src_ipif); 9514 return; 9515 } 9516 case IRE_IF_RESOLVER: 9517 /* 9518 * We can't build an IRE_CACHE yet, but at least 9519 * we found a resolver that can help. 9520 */ 9521 res_mp = dst_ill->ill_resolver_mp; 9522 if (!OK_RESOLVER_MP(res_mp)) 9523 break; 9524 9525 /* 9526 * We obtain a partial IRE_CACHE which we will pass 9527 * along with the resolver query. When the response 9528 * comes back it will be there ready for us to add. 9529 * The new ire inherits the IRE_OFFSUBNET flags 9530 * and source address, if this was requested. 9531 * The ire_max_frag is atomically set under the 9532 * irebucket lock in ire_add_v[46]. Only in the 9533 * case of IRE_MARK_NOADD, we set it here itself. 9534 */ 9535 ire = ire_create_mp( 9536 (uchar_t *)&dst, /* dest address */ 9537 (uchar_t *)&ip_g_all_ones, /* mask */ 9538 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 9539 NULL, /* gateway address */ 9540 NULL, /* no in_src_addr */ 9541 (ire_marks & IRE_MARK_NOADD) ? 9542 ipif->ipif_mtu : 0, /* max_frag */ 9543 NULL, /* Fast path header */ 9544 dst_ill->ill_rq, /* recv-from queue */ 9545 dst_ill->ill_wq, /* send-to queue */ 9546 IRE_CACHE, 9547 NULL, /* let ire_nce_init figure res_mp out */ 9548 src_ipif, 9549 NULL, 9550 (save_ire != NULL ? save_ire->ire_mask : 0), 9551 (fire != NULL) ? /* Parent handle */ 9552 fire->ire_phandle : 0, 9553 ihandle, /* Interface handle */ 9554 (fire != NULL) ? /* flags if any */ 9555 (fire->ire_flags & 9556 (RTF_SETSRC | RTF_MULTIRT)) : 0, 9557 (save_ire == NULL ? &ire_uinfo_null : 9558 &save_ire->ire_uinfo), 9559 NULL, 9560 NULL); 9561 9562 if (save_ire != NULL) { 9563 ire_refrele(save_ire); 9564 save_ire = NULL; 9565 } 9566 if (ire == NULL) 9567 break; 9568 9569 ire->ire_marks |= ire_marks; 9570 /* 9571 * Construct message chain for the resolver of the 9572 * form: 9573 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 9574 * 9575 * NOTE : ire will be added later when the response 9576 * comes back from ARP. If the response does not 9577 * come back, ARP frees the packet. For this reason, 9578 * we can't REFHOLD the bucket of save_ire to prevent 9579 * deletions. We may not be able to REFRELE the 9580 * bucket if the response never comes back. 9581 * Thus, before adding the ire, ire_add_v4 will make 9582 * sure that the interface route does not get deleted. 9583 * This is the only case unlike ip_newroute_v6, 9584 * ip_newroute_ipif_v6 where we can always prevent 9585 * deletions because ire_add_then_send is called after 9586 * creating the IRE. 9587 * If IRE_MARK_NOADD is set, then ire_add_then_send 9588 * does not add this IRE into the IRE CACHE. 9589 */ 9590 ASSERT(ire->ire_mp != NULL); 9591 ire->ire_mp->b_cont = first_mp; 9592 /* Have saved_mp handy, for cleanup if canput fails */ 9593 saved_mp = mp; 9594 mp = copyb(res_mp); 9595 if (mp == NULL) { 9596 /* Prepare for cleanup */ 9597 mp = saved_mp; /* pkt */ 9598 ire_delete(ire); /* ire_mp */ 9599 ire = NULL; 9600 if (copy_mp != NULL) { 9601 MULTIRT_DEBUG_UNTAG(copy_mp); 9602 freemsg(copy_mp); 9603 copy_mp = NULL; 9604 } 9605 break; 9606 } 9607 linkb(mp, ire->ire_mp); 9608 9609 /* 9610 * Fill in the source and dest addrs for the resolver. 9611 * NOTE: this depends on memory layouts imposed by 9612 * ill_init(). 9613 */ 9614 areq = (areq_t *)mp->b_rptr; 9615 addrp = (ipaddr_t *)((char *)areq + 9616 areq->areq_sender_addr_offset); 9617 *addrp = ire->ire_src_addr; 9618 addrp = (ipaddr_t *)((char *)areq + 9619 areq->areq_target_addr_offset); 9620 *addrp = dst; 9621 /* Up to the resolver. */ 9622 if (canputnext(dst_ill->ill_rq) && 9623 !(dst_ill->ill_arp_closing)) { 9624 putnext(dst_ill->ill_rq, mp); 9625 /* 9626 * The response will come back in ip_wput 9627 * with db_type IRE_DB_TYPE. 9628 */ 9629 } else { 9630 mp->b_cont = NULL; 9631 freeb(mp); /* areq */ 9632 ire_delete(ire); /* ire_mp */ 9633 saved_mp->b_next = NULL; 9634 saved_mp->b_prev = NULL; 9635 freemsg(first_mp); /* pkt */ 9636 ip2dbg(("ip_newroute_ipif: dropped\n")); 9637 } 9638 9639 if (fire != NULL) { 9640 ire_refrele(fire); 9641 fire = NULL; 9642 } 9643 9644 9645 /* 9646 * The resolution loop is re-entered if this was 9647 * requested through flags and we actually are 9648 * in a multirouting case. 9649 */ 9650 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 9651 boolean_t need_resolve = 9652 ire_multirt_need_resolve(ipha_dst, 9653 MBLK_GETLABEL(copy_mp)); 9654 if (!need_resolve) { 9655 MULTIRT_DEBUG_UNTAG(copy_mp); 9656 freemsg(copy_mp); 9657 copy_mp = NULL; 9658 } else { 9659 /* 9660 * ipif_lookup_group() calls 9661 * ire_lookup_multi() that uses 9662 * ire_ftable_lookup() to find 9663 * an IRE_INTERFACE for the group. 9664 * In the multirt case, 9665 * ire_lookup_multi() then invokes 9666 * ire_multirt_lookup() to find 9667 * the next resolvable ire. 9668 * As a result, we obtain an new 9669 * interface, derived from the 9670 * next ire. 9671 */ 9672 ipif_refrele(ipif); 9673 ipif = ipif_lookup_group(ipha_dst, 9674 zoneid); 9675 if (ipif != NULL) { 9676 mp = copy_mp; 9677 copy_mp = NULL; 9678 multirt_resolve_next = B_TRUE; 9679 continue; 9680 } else { 9681 freemsg(copy_mp); 9682 } 9683 } 9684 } 9685 if (ipif != NULL) 9686 ipif_refrele(ipif); 9687 ill_refrele(dst_ill); 9688 ipif_refrele(src_ipif); 9689 return; 9690 default: 9691 break; 9692 } 9693 } while (multirt_resolve_next); 9694 9695 err_ret: 9696 ip2dbg(("ip_newroute_ipif: dropped\n")); 9697 if (fire != NULL) 9698 ire_refrele(fire); 9699 ipif_refrele(ipif); 9700 /* Did this packet originate externally? */ 9701 if (dst_ill != NULL) 9702 ill_refrele(dst_ill); 9703 if (src_ipif != NULL) 9704 ipif_refrele(src_ipif); 9705 if (mp->b_prev || mp->b_next) { 9706 mp->b_next = NULL; 9707 mp->b_prev = NULL; 9708 } else { 9709 /* 9710 * Since ip_wput() isn't close to finished, we fill 9711 * in enough of the header for credible error reporting. 9712 */ 9713 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 9714 /* Failed */ 9715 freemsg(first_mp); 9716 if (ire != NULL) 9717 ire_refrele(ire); 9718 return; 9719 } 9720 } 9721 /* 9722 * At this point we will have ire only if RTF_BLACKHOLE 9723 * or RTF_REJECT flags are set on the IRE. It will not 9724 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 9725 */ 9726 if (ire != NULL) { 9727 if (ire->ire_flags & RTF_BLACKHOLE) { 9728 ire_refrele(ire); 9729 freemsg(first_mp); 9730 return; 9731 } 9732 ire_refrele(ire); 9733 } 9734 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid); 9735 } 9736 9737 /* Name/Value Table Lookup Routine */ 9738 char * 9739 ip_nv_lookup(nv_t *nv, int value) 9740 { 9741 if (!nv) 9742 return (NULL); 9743 for (; nv->nv_name; nv++) { 9744 if (nv->nv_value == value) 9745 return (nv->nv_name); 9746 } 9747 return ("unknown"); 9748 } 9749 9750 /* 9751 * one day it can be patched to 1 from /etc/system for machines that have few 9752 * fast network interfaces feeding multiple cpus. 9753 */ 9754 int ill_stream_putlocks = 0; 9755 9756 /* 9757 * This is a module open, i.e. this is a control stream for access 9758 * to a DLPI device. We allocate an ill_t as the instance data in 9759 * this case. 9760 */ 9761 int 9762 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9763 { 9764 uint32_t mem_cnt; 9765 uint32_t cpu_cnt; 9766 uint32_t min_cnt; 9767 pgcnt_t mem_avail; 9768 ill_t *ill; 9769 int err; 9770 9771 /* 9772 * Prevent unprivileged processes from pushing IP so that 9773 * they can't send raw IP. 9774 */ 9775 if (secpolicy_net_rawaccess(credp) != 0) 9776 return (EPERM); 9777 9778 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 9779 q->q_ptr = WR(q)->q_ptr = ill; 9780 9781 /* 9782 * ill_init initializes the ill fields and then sends down 9783 * down a DL_INFO_REQ after calling qprocson. 9784 */ 9785 err = ill_init(q, ill); 9786 if (err != 0) { 9787 mi_free(ill); 9788 q->q_ptr = NULL; 9789 WR(q)->q_ptr = NULL; 9790 return (err); 9791 } 9792 9793 /* ill_init initializes the ipsq marking this thread as writer */ 9794 ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE, B_TRUE); 9795 /* Wait for the DL_INFO_ACK */ 9796 mutex_enter(&ill->ill_lock); 9797 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 9798 /* 9799 * Return value of 0 indicates a pending signal. 9800 */ 9801 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 9802 if (err == 0) { 9803 mutex_exit(&ill->ill_lock); 9804 (void) ip_close(q, 0); 9805 return (EINTR); 9806 } 9807 } 9808 mutex_exit(&ill->ill_lock); 9809 9810 /* 9811 * ip_rput_other could have set an error in ill_error on 9812 * receipt of M_ERROR. 9813 */ 9814 9815 err = ill->ill_error; 9816 if (err != 0) { 9817 (void) ip_close(q, 0); 9818 return (err); 9819 } 9820 9821 /* 9822 * ip_ire_max_bucket_cnt is sized below based on the memory 9823 * size and the cpu speed of the machine. This is upper 9824 * bounded by the compile time value of ip_ire_max_bucket_cnt 9825 * and is lower bounded by the compile time value of 9826 * ip_ire_min_bucket_cnt. Similar logic applies to 9827 * ip6_ire_max_bucket_cnt. 9828 */ 9829 mem_avail = kmem_avail(); 9830 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 9831 ip_cache_table_size / sizeof (ire_t); 9832 cpu_cnt = CPU->cpu_type_info.pi_clock >> ip_ire_cpu_ratio; 9833 9834 min_cnt = MIN(cpu_cnt, mem_cnt); 9835 if (min_cnt < ip_ire_min_bucket_cnt) 9836 min_cnt = ip_ire_min_bucket_cnt; 9837 if (ip_ire_max_bucket_cnt > min_cnt) { 9838 ip_ire_max_bucket_cnt = min_cnt; 9839 } 9840 9841 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 9842 ip6_cache_table_size / sizeof (ire_t); 9843 min_cnt = MIN(cpu_cnt, mem_cnt); 9844 if (min_cnt < ip6_ire_min_bucket_cnt) 9845 min_cnt = ip6_ire_min_bucket_cnt; 9846 if (ip6_ire_max_bucket_cnt > min_cnt) { 9847 ip6_ire_max_bucket_cnt = min_cnt; 9848 } 9849 9850 ill->ill_credp = credp; 9851 crhold(credp); 9852 9853 mutex_enter(&ip_mi_lock); 9854 err = mi_open_link(&ip_g_head, (IDP)ill, devp, flag, sflag, credp); 9855 mutex_exit(&ip_mi_lock); 9856 if (err) { 9857 (void) ip_close(q, 0); 9858 return (err); 9859 } 9860 return (0); 9861 } 9862 9863 /* IP open routine. */ 9864 int 9865 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9866 { 9867 conn_t *connp; 9868 major_t maj; 9869 9870 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 9871 9872 /* Allow reopen. */ 9873 if (q->q_ptr != NULL) 9874 return (0); 9875 9876 if (sflag & MODOPEN) { 9877 /* This is a module open */ 9878 return (ip_modopen(q, devp, flag, sflag, credp)); 9879 } 9880 9881 /* 9882 * We are opening as a device. This is an IP client stream, and we 9883 * allocate an conn_t as the instance data. 9884 */ 9885 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP); 9886 connp->conn_upq = q; 9887 q->q_ptr = WR(q)->q_ptr = connp; 9888 9889 if (flag & SO_SOCKSTR) 9890 connp->conn_flags |= IPCL_SOCKET; 9891 9892 /* Minor tells us which /dev entry was opened */ 9893 if (geteminor(*devp) == IPV6_MINOR) { 9894 connp->conn_flags |= IPCL_ISV6; 9895 connp->conn_af_isv6 = B_TRUE; 9896 ip_setqinfo(q, geteminor(*devp), B_FALSE); 9897 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9898 } else { 9899 connp->conn_af_isv6 = B_FALSE; 9900 connp->conn_pkt_isv6 = B_FALSE; 9901 } 9902 9903 if ((connp->conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 9904 q->q_ptr = WR(q)->q_ptr = NULL; 9905 CONN_DEC_REF(connp); 9906 return (EBUSY); 9907 } 9908 9909 maj = getemajor(*devp); 9910 *devp = makedevice(maj, (minor_t)connp->conn_dev); 9911 9912 /* 9913 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 9914 */ 9915 connp->conn_cred = credp; 9916 crhold(connp->conn_cred); 9917 9918 /* 9919 * If the caller has the process-wide flag set, then default to MAC 9920 * exempt mode. This allows read-down to unlabeled hosts. 9921 */ 9922 if (getpflags(NET_MAC_AWARE, credp) != 0) 9923 connp->conn_mac_exempt = B_TRUE; 9924 9925 connp->conn_zoneid = getzoneid(); 9926 9927 /* 9928 * This should only happen for ndd, netstat, raw socket or other SCTP 9929 * administrative ops. In these cases, we just need a normal conn_t 9930 * with ulp set to IPPROTO_SCTP. All other ops are trapped and 9931 * an error will be returned. 9932 */ 9933 if (maj != SCTP_MAJ && maj != SCTP6_MAJ) { 9934 connp->conn_rq = q; 9935 connp->conn_wq = WR(q); 9936 } else { 9937 connp->conn_ulp = IPPROTO_SCTP; 9938 connp->conn_rq = connp->conn_wq = NULL; 9939 } 9940 /* Non-zero default values */ 9941 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9942 9943 /* 9944 * Make the conn globally visible to walkers 9945 */ 9946 mutex_enter(&connp->conn_lock); 9947 connp->conn_state_flags &= ~CONN_INCIPIENT; 9948 mutex_exit(&connp->conn_lock); 9949 ASSERT(connp->conn_ref == 1); 9950 9951 qprocson(q); 9952 9953 return (0); 9954 } 9955 9956 /* 9957 * Change q_qinfo based on the value of isv6. 9958 * This can not called on an ill queue. 9959 * Note that there is no race since either q_qinfo works for conn queues - it 9960 * is just an optimization to enter the best wput routine directly. 9961 */ 9962 void 9963 ip_setqinfo(queue_t *q, minor_t minor, boolean_t bump_mib) 9964 { 9965 ASSERT(q->q_flag & QREADR); 9966 ASSERT(WR(q)->q_next == NULL); 9967 ASSERT(q->q_ptr != NULL); 9968 9969 if (minor == IPV6_MINOR) { 9970 if (bump_mib) 9971 BUMP_MIB(&ip6_mib, ipIfStatsOutSwitchIPVersion); 9972 q->q_qinfo = &rinit_ipv6; 9973 WR(q)->q_qinfo = &winit_ipv6; 9974 (Q_TO_CONN(q))->conn_pkt_isv6 = B_TRUE; 9975 } else { 9976 if (bump_mib) 9977 BUMP_MIB(&ip_mib, ipIfStatsOutSwitchIPVersion); 9978 q->q_qinfo = &iprinit; 9979 WR(q)->q_qinfo = &ipwinit; 9980 (Q_TO_CONN(q))->conn_pkt_isv6 = B_FALSE; 9981 } 9982 9983 } 9984 9985 /* 9986 * See if IPsec needs loading because of the options in mp. 9987 */ 9988 static boolean_t 9989 ipsec_opt_present(mblk_t *mp) 9990 { 9991 uint8_t *optcp, *next_optcp, *opt_endcp; 9992 struct opthdr *opt; 9993 struct T_opthdr *topt; 9994 int opthdr_len; 9995 t_uscalar_t optname, optlevel; 9996 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 9997 ipsec_req_t *ipsr; 9998 9999 /* 10000 * Walk through the mess, and find IP_SEC_OPT. If it's there, 10001 * return TRUE. 10002 */ 10003 10004 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 10005 opt_endcp = optcp + tor->OPT_length; 10006 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10007 opthdr_len = sizeof (struct T_opthdr); 10008 } else { /* O_OPTMGMT_REQ */ 10009 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 10010 opthdr_len = sizeof (struct opthdr); 10011 } 10012 for (; optcp < opt_endcp; optcp = next_optcp) { 10013 if (optcp + opthdr_len > opt_endcp) 10014 return (B_FALSE); /* Not enough option header. */ 10015 if (tor->PRIM_type == T_OPTMGMT_REQ) { 10016 topt = (struct T_opthdr *)optcp; 10017 optlevel = topt->level; 10018 optname = topt->name; 10019 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 10020 } else { 10021 opt = (struct opthdr *)optcp; 10022 optlevel = opt->level; 10023 optname = opt->name; 10024 next_optcp = optcp + opthdr_len + 10025 _TPI_ALIGN_OPT(opt->len); 10026 } 10027 if ((next_optcp < optcp) || /* wraparound pointer space */ 10028 ((next_optcp >= opt_endcp) && /* last option bad len */ 10029 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 10030 return (B_FALSE); /* bad option buffer */ 10031 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 10032 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 10033 /* 10034 * Check to see if it's an all-bypass or all-zeroes 10035 * IPsec request. Don't bother loading IPsec if 10036 * the socket doesn't want to use it. (A good example 10037 * is a bypass request.) 10038 * 10039 * Basically, if any of the non-NEVER bits are set, 10040 * load IPsec. 10041 */ 10042 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 10043 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 10044 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 10045 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 10046 != 0) 10047 return (B_TRUE); 10048 } 10049 } 10050 return (B_FALSE); 10051 } 10052 10053 /* 10054 * If conn is is waiting for ipsec to finish loading, kick it. 10055 */ 10056 /* ARGSUSED */ 10057 static void 10058 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 10059 { 10060 t_scalar_t optreq_prim; 10061 mblk_t *mp; 10062 cred_t *cr; 10063 int err = 0; 10064 10065 /* 10066 * This function is called, after ipsec loading is complete. 10067 * Since IP checks exclusively and atomically (i.e it prevents 10068 * ipsec load from completing until ip_optcom_req completes) 10069 * whether ipsec load is complete, there cannot be a race with IP 10070 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 10071 */ 10072 mutex_enter(&connp->conn_lock); 10073 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 10074 ASSERT(connp->conn_ipsec_opt_mp != NULL); 10075 mp = connp->conn_ipsec_opt_mp; 10076 connp->conn_ipsec_opt_mp = NULL; 10077 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 10078 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); 10079 mutex_exit(&connp->conn_lock); 10080 10081 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 10082 10083 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 10084 if (optreq_prim == T_OPTMGMT_REQ) { 10085 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10086 &ip_opt_obj); 10087 } else { 10088 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 10089 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 10090 &ip_opt_obj); 10091 } 10092 if (err != EINPROGRESS) 10093 CONN_OPER_PENDING_DONE(connp); 10094 return; 10095 } 10096 mutex_exit(&connp->conn_lock); 10097 } 10098 10099 /* 10100 * Called from the ipsec_loader thread, outside any perimeter, to tell 10101 * ip qenable any of the queues waiting for the ipsec loader to 10102 * complete. 10103 * 10104 * Use ip_mi_lock to be safe here: all modifications of the mi lists 10105 * are done with this lock held, so it's guaranteed that none of the 10106 * links will change along the way. 10107 */ 10108 void 10109 ip_ipsec_load_complete() 10110 { 10111 ipcl_walk(conn_restart_ipsec_waiter, NULL); 10112 } 10113 10114 /* 10115 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 10116 * determines the grp on which it has to become exclusive, queues the mp 10117 * and sq draining restarts the optmgmt 10118 */ 10119 static boolean_t 10120 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 10121 { 10122 conn_t *connp; 10123 10124 /* 10125 * Take IPsec requests and treat them special. 10126 */ 10127 if (ipsec_opt_present(mp)) { 10128 /* First check if IPsec is loaded. */ 10129 mutex_enter(&ipsec_loader_lock); 10130 if (ipsec_loader_state != IPSEC_LOADER_WAIT) { 10131 mutex_exit(&ipsec_loader_lock); 10132 return (B_FALSE); 10133 } 10134 connp = Q_TO_CONN(q); 10135 mutex_enter(&connp->conn_lock); 10136 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 10137 10138 ASSERT(connp->conn_ipsec_opt_mp == NULL); 10139 connp->conn_ipsec_opt_mp = mp; 10140 mutex_exit(&connp->conn_lock); 10141 mutex_exit(&ipsec_loader_lock); 10142 10143 ipsec_loader_loadnow(); 10144 return (B_TRUE); 10145 } 10146 return (B_FALSE); 10147 } 10148 10149 /* 10150 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 10151 * all of them are copied to the conn_t. If the req is "zero", the policy is 10152 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 10153 * fields. 10154 * We keep only the latest setting of the policy and thus policy setting 10155 * is not incremental/cumulative. 10156 * 10157 * Requests to set policies with multiple alternative actions will 10158 * go through a different API. 10159 */ 10160 int 10161 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 10162 { 10163 uint_t ah_req = 0; 10164 uint_t esp_req = 0; 10165 uint_t se_req = 0; 10166 ipsec_selkey_t sel; 10167 ipsec_act_t *actp = NULL; 10168 uint_t nact; 10169 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 10170 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 10171 ipsec_policy_root_t *pr; 10172 ipsec_policy_head_t *ph; 10173 int fam; 10174 boolean_t is_pol_reset; 10175 int error = 0; 10176 10177 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 10178 10179 /* 10180 * The IP_SEC_OPT option does not allow variable length parameters, 10181 * hence a request cannot be NULL. 10182 */ 10183 if (req == NULL) 10184 return (EINVAL); 10185 10186 ah_req = req->ipsr_ah_req; 10187 esp_req = req->ipsr_esp_req; 10188 se_req = req->ipsr_self_encap_req; 10189 10190 /* 10191 * Are we dealing with a request to reset the policy (i.e. 10192 * zero requests). 10193 */ 10194 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 10195 (esp_req & REQ_MASK) == 0 && 10196 (se_req & REQ_MASK) == 0); 10197 10198 if (!is_pol_reset) { 10199 /* 10200 * If we couldn't load IPsec, fail with "protocol 10201 * not supported". 10202 * IPsec may not have been loaded for a request with zero 10203 * policies, so we don't fail in this case. 10204 */ 10205 mutex_enter(&ipsec_loader_lock); 10206 if (ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 10207 mutex_exit(&ipsec_loader_lock); 10208 return (EPROTONOSUPPORT); 10209 } 10210 mutex_exit(&ipsec_loader_lock); 10211 10212 /* 10213 * Test for valid requests. Invalid algorithms 10214 * need to be tested by IPSEC code because new 10215 * algorithms can be added dynamically. 10216 */ 10217 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10218 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 10219 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 10220 return (EINVAL); 10221 } 10222 10223 /* 10224 * Only privileged users can issue these 10225 * requests. 10226 */ 10227 if (((ah_req & IPSEC_PREF_NEVER) || 10228 (esp_req & IPSEC_PREF_NEVER) || 10229 (se_req & IPSEC_PREF_NEVER)) && 10230 secpolicy_net_config(cr, B_FALSE) != 0) { 10231 return (EPERM); 10232 } 10233 10234 /* 10235 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 10236 * are mutually exclusive. 10237 */ 10238 if (((ah_req & REQ_MASK) == REQ_MASK) || 10239 ((esp_req & REQ_MASK) == REQ_MASK) || 10240 ((se_req & REQ_MASK) == REQ_MASK)) { 10241 /* Both of them are set */ 10242 return (EINVAL); 10243 } 10244 } 10245 10246 mutex_enter(&connp->conn_lock); 10247 10248 /* 10249 * If we have already cached policies in ip_bind_connected*(), don't 10250 * let them change now. We cache policies for connections 10251 * whose src,dst [addr, port] is known. 10252 */ 10253 if (connp->conn_policy_cached) { 10254 mutex_exit(&connp->conn_lock); 10255 return (EINVAL); 10256 } 10257 10258 /* 10259 * We have a zero policies, reset the connection policy if already 10260 * set. This will cause the connection to inherit the 10261 * global policy, if any. 10262 */ 10263 if (is_pol_reset) { 10264 if (connp->conn_policy != NULL) { 10265 IPPH_REFRELE(connp->conn_policy); 10266 connp->conn_policy = NULL; 10267 } 10268 connp->conn_flags &= ~IPCL_CHECK_POLICY; 10269 connp->conn_in_enforce_policy = B_FALSE; 10270 connp->conn_out_enforce_policy = B_FALSE; 10271 mutex_exit(&connp->conn_lock); 10272 return (0); 10273 } 10274 10275 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy); 10276 if (ph == NULL) 10277 goto enomem; 10278 10279 ipsec_actvec_from_req(req, &actp, &nact); 10280 if (actp == NULL) 10281 goto enomem; 10282 10283 /* 10284 * Always allocate IPv4 policy entries, since they can also 10285 * apply to ipv6 sockets being used in ipv4-compat mode. 10286 */ 10287 bzero(&sel, sizeof (sel)); 10288 sel.ipsl_valid = IPSL_IPV4; 10289 10290 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL); 10291 if (pin4 == NULL) 10292 goto enomem; 10293 10294 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL); 10295 if (pout4 == NULL) 10296 goto enomem; 10297 10298 if (connp->conn_pkt_isv6) { 10299 /* 10300 * We're looking at a v6 socket, also allocate the 10301 * v6-specific entries... 10302 */ 10303 sel.ipsl_valid = IPSL_IPV6; 10304 pin6 = ipsec_policy_create(&sel, actp, nact, 10305 IPSEC_PRIO_SOCKET, NULL); 10306 if (pin6 == NULL) 10307 goto enomem; 10308 10309 pout6 = ipsec_policy_create(&sel, actp, nact, 10310 IPSEC_PRIO_SOCKET, NULL); 10311 if (pout6 == NULL) 10312 goto enomem; 10313 10314 /* 10315 * .. and file them away in the right place. 10316 */ 10317 fam = IPSEC_AF_V6; 10318 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10319 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 10320 ipsec_insert_always(&ph->iph_rulebyid, pin6); 10321 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10322 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 10323 ipsec_insert_always(&ph->iph_rulebyid, pout6); 10324 } 10325 10326 ipsec_actvec_free(actp, nact); 10327 10328 /* 10329 * File the v4 policies. 10330 */ 10331 fam = IPSEC_AF_V4; 10332 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 10333 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 10334 ipsec_insert_always(&ph->iph_rulebyid, pin4); 10335 10336 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 10337 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 10338 ipsec_insert_always(&ph->iph_rulebyid, pout4); 10339 10340 /* 10341 * If the requests need security, set enforce_policy. 10342 * If the requests are IPSEC_PREF_NEVER, one should 10343 * still set conn_out_enforce_policy so that an ipsec_out 10344 * gets attached in ip_wput. This is needed so that 10345 * for connections that we don't cache policy in ip_bind, 10346 * if global policy matches in ip_wput_attach_policy, we 10347 * don't wrongly inherit global policy. Similarly, we need 10348 * to set conn_in_enforce_policy also so that we don't verify 10349 * policy wrongly. 10350 */ 10351 if ((ah_req & REQ_MASK) != 0 || 10352 (esp_req & REQ_MASK) != 0 || 10353 (se_req & REQ_MASK) != 0) { 10354 connp->conn_in_enforce_policy = B_TRUE; 10355 connp->conn_out_enforce_policy = B_TRUE; 10356 connp->conn_flags |= IPCL_CHECK_POLICY; 10357 } 10358 10359 mutex_exit(&connp->conn_lock); 10360 return (error); 10361 #undef REQ_MASK 10362 10363 /* 10364 * Common memory-allocation-failure exit path. 10365 */ 10366 enomem: 10367 mutex_exit(&connp->conn_lock); 10368 if (actp != NULL) 10369 ipsec_actvec_free(actp, nact); 10370 if (pin4 != NULL) 10371 IPPOL_REFRELE(pin4); 10372 if (pout4 != NULL) 10373 IPPOL_REFRELE(pout4); 10374 if (pin6 != NULL) 10375 IPPOL_REFRELE(pin6); 10376 if (pout6 != NULL) 10377 IPPOL_REFRELE(pout6); 10378 return (ENOMEM); 10379 } 10380 10381 /* 10382 * Only for options that pass in an IP addr. Currently only V4 options 10383 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 10384 * So this function assumes level is IPPROTO_IP 10385 */ 10386 int 10387 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 10388 mblk_t *first_mp) 10389 { 10390 ipif_t *ipif = NULL; 10391 int error; 10392 ill_t *ill; 10393 int zoneid; 10394 10395 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 10396 10397 if (addr != INADDR_ANY || checkonly) { 10398 ASSERT(connp != NULL); 10399 zoneid = IPCL_ZONEID(connp); 10400 if (option == IP_NEXTHOP) { 10401 ipif = ipif_lookup_onlink_addr(addr, 10402 connp->conn_zoneid); 10403 } else { 10404 ipif = ipif_lookup_addr(addr, NULL, zoneid, 10405 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 10406 &error); 10407 } 10408 if (ipif == NULL) { 10409 if (error == EINPROGRESS) 10410 return (error); 10411 else if ((option == IP_MULTICAST_IF) || 10412 (option == IP_NEXTHOP)) 10413 return (EHOSTUNREACH); 10414 else 10415 return (EINVAL); 10416 } else if (checkonly) { 10417 if (option == IP_MULTICAST_IF) { 10418 ill = ipif->ipif_ill; 10419 /* not supported by the virtual network iface */ 10420 if (IS_VNI(ill)) { 10421 ipif_refrele(ipif); 10422 return (EINVAL); 10423 } 10424 } 10425 ipif_refrele(ipif); 10426 return (0); 10427 } 10428 ill = ipif->ipif_ill; 10429 mutex_enter(&connp->conn_lock); 10430 mutex_enter(&ill->ill_lock); 10431 if ((ill->ill_state_flags & ILL_CONDEMNED) || 10432 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 10433 mutex_exit(&ill->ill_lock); 10434 mutex_exit(&connp->conn_lock); 10435 ipif_refrele(ipif); 10436 return (option == IP_MULTICAST_IF ? 10437 EHOSTUNREACH : EINVAL); 10438 } 10439 } else { 10440 mutex_enter(&connp->conn_lock); 10441 } 10442 10443 /* None of the options below are supported on the VNI */ 10444 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 10445 mutex_exit(&ill->ill_lock); 10446 mutex_exit(&connp->conn_lock); 10447 ipif_refrele(ipif); 10448 return (EINVAL); 10449 } 10450 10451 switch (option) { 10452 case IP_DONTFAILOVER_IF: 10453 /* 10454 * This option is used by in.mpathd to ensure 10455 * that IPMP probe packets only go out on the 10456 * test interfaces. in.mpathd sets this option 10457 * on the non-failover interfaces. 10458 * For backward compatibility, this option 10459 * implicitly sets IP_MULTICAST_IF, as used 10460 * be done in bind(), so that ip_wput gets 10461 * this ipif to send mcast packets. 10462 */ 10463 if (ipif != NULL) { 10464 ASSERT(addr != INADDR_ANY); 10465 connp->conn_nofailover_ill = ipif->ipif_ill; 10466 connp->conn_multicast_ipif = ipif; 10467 } else { 10468 ASSERT(addr == INADDR_ANY); 10469 connp->conn_nofailover_ill = NULL; 10470 connp->conn_multicast_ipif = NULL; 10471 } 10472 break; 10473 10474 case IP_MULTICAST_IF: 10475 connp->conn_multicast_ipif = ipif; 10476 break; 10477 case IP_NEXTHOP: 10478 connp->conn_nexthop_v4 = addr; 10479 connp->conn_nexthop_set = B_TRUE; 10480 break; 10481 } 10482 10483 if (ipif != NULL) { 10484 mutex_exit(&ill->ill_lock); 10485 mutex_exit(&connp->conn_lock); 10486 ipif_refrele(ipif); 10487 return (0); 10488 } 10489 mutex_exit(&connp->conn_lock); 10490 /* We succeded in cleared the option */ 10491 return (0); 10492 } 10493 10494 /* 10495 * For options that pass in an ifindex specifying the ill. V6 options always 10496 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 10497 */ 10498 int 10499 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 10500 int level, int option, mblk_t *first_mp) 10501 { 10502 ill_t *ill = NULL; 10503 int error = 0; 10504 10505 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 10506 if (ifindex != 0) { 10507 ASSERT(connp != NULL); 10508 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 10509 first_mp, ip_restart_optmgmt, &error); 10510 if (ill != NULL) { 10511 if (checkonly) { 10512 /* not supported by the virtual network iface */ 10513 if (IS_VNI(ill)) { 10514 ill_refrele(ill); 10515 return (EINVAL); 10516 } 10517 ill_refrele(ill); 10518 return (0); 10519 } 10520 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 10521 0, NULL)) { 10522 ill_refrele(ill); 10523 ill = NULL; 10524 mutex_enter(&connp->conn_lock); 10525 goto setit; 10526 } 10527 mutex_enter(&connp->conn_lock); 10528 mutex_enter(&ill->ill_lock); 10529 if (ill->ill_state_flags & ILL_CONDEMNED) { 10530 mutex_exit(&ill->ill_lock); 10531 mutex_exit(&connp->conn_lock); 10532 ill_refrele(ill); 10533 ill = NULL; 10534 mutex_enter(&connp->conn_lock); 10535 } 10536 goto setit; 10537 } else if (error == EINPROGRESS) { 10538 return (error); 10539 } else { 10540 error = 0; 10541 } 10542 } 10543 mutex_enter(&connp->conn_lock); 10544 setit: 10545 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 10546 10547 /* 10548 * The options below assume that the ILL (if any) transmits and/or 10549 * receives traffic. Neither of which is true for the virtual network 10550 * interface, so fail setting these on a VNI. 10551 */ 10552 if (IS_VNI(ill)) { 10553 ASSERT(ill != NULL); 10554 mutex_exit(&ill->ill_lock); 10555 mutex_exit(&connp->conn_lock); 10556 ill_refrele(ill); 10557 return (EINVAL); 10558 } 10559 10560 if (level == IPPROTO_IP) { 10561 switch (option) { 10562 case IP_BOUND_IF: 10563 connp->conn_incoming_ill = ill; 10564 connp->conn_outgoing_ill = ill; 10565 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10566 0 : ifindex; 10567 break; 10568 10569 case IP_XMIT_IF: 10570 /* 10571 * Similar to IP_BOUND_IF, but this only 10572 * determines the outgoing interface for 10573 * unicast packets. Also no IRE_CACHE entry 10574 * is added for the destination of the 10575 * outgoing packets. This feature is needed 10576 * for mobile IP. 10577 */ 10578 connp->conn_xmit_if_ill = ill; 10579 connp->conn_orig_xmit_ifindex = (ill == NULL) ? 10580 0 : ifindex; 10581 break; 10582 10583 case IP_MULTICAST_IF: 10584 /* 10585 * This option is an internal special. The socket 10586 * level IP_MULTICAST_IF specifies an 'ipaddr' and 10587 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 10588 * specifies an ifindex and we try first on V6 ill's. 10589 * If we don't find one, we they try using on v4 ill's 10590 * intenally and we come here. 10591 */ 10592 if (!checkonly && ill != NULL) { 10593 ipif_t *ipif; 10594 ipif = ill->ill_ipif; 10595 10596 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 10597 mutex_exit(&ill->ill_lock); 10598 mutex_exit(&connp->conn_lock); 10599 ill_refrele(ill); 10600 ill = NULL; 10601 mutex_enter(&connp->conn_lock); 10602 } else { 10603 connp->conn_multicast_ipif = ipif; 10604 } 10605 } 10606 break; 10607 } 10608 } else { 10609 switch (option) { 10610 case IPV6_BOUND_IF: 10611 connp->conn_incoming_ill = ill; 10612 connp->conn_outgoing_ill = ill; 10613 connp->conn_orig_bound_ifindex = (ill == NULL) ? 10614 0 : ifindex; 10615 break; 10616 10617 case IPV6_BOUND_PIF: 10618 /* 10619 * Limit all transmit to this ill. 10620 * Unlike IPV6_BOUND_IF, using this option 10621 * prevents load spreading and failover from 10622 * happening when the interface is part of the 10623 * group. That's why we don't need to remember 10624 * the ifindex in orig_bound_ifindex as in 10625 * IPV6_BOUND_IF. 10626 */ 10627 connp->conn_outgoing_pill = ill; 10628 break; 10629 10630 case IPV6_DONTFAILOVER_IF: 10631 /* 10632 * This option is used by in.mpathd to ensure 10633 * that IPMP probe packets only go out on the 10634 * test interfaces. in.mpathd sets this option 10635 * on the non-failover interfaces. 10636 */ 10637 connp->conn_nofailover_ill = ill; 10638 /* 10639 * For backward compatibility, this option 10640 * implicitly sets ip_multicast_ill as used in 10641 * IP_MULTICAST_IF so that ip_wput gets 10642 * this ipif to send mcast packets. 10643 */ 10644 connp->conn_multicast_ill = ill; 10645 connp->conn_orig_multicast_ifindex = (ill == NULL) ? 10646 0 : ifindex; 10647 break; 10648 10649 case IPV6_MULTICAST_IF: 10650 /* 10651 * Set conn_multicast_ill to be the IPv6 ill. 10652 * Set conn_multicast_ipif to be an IPv4 ipif 10653 * for ifindex to make IPv4 mapped addresses 10654 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 10655 * Even if no IPv6 ill exists for the ifindex 10656 * we need to check for an IPv4 ifindex in order 10657 * for this to work with mapped addresses. In that 10658 * case only set conn_multicast_ipif. 10659 */ 10660 if (!checkonly) { 10661 if (ifindex == 0) { 10662 connp->conn_multicast_ill = NULL; 10663 connp->conn_orig_multicast_ifindex = 0; 10664 connp->conn_multicast_ipif = NULL; 10665 } else if (ill != NULL) { 10666 connp->conn_multicast_ill = ill; 10667 connp->conn_orig_multicast_ifindex = 10668 ifindex; 10669 } 10670 } 10671 break; 10672 } 10673 } 10674 10675 if (ill != NULL) { 10676 mutex_exit(&ill->ill_lock); 10677 mutex_exit(&connp->conn_lock); 10678 ill_refrele(ill); 10679 return (0); 10680 } 10681 mutex_exit(&connp->conn_lock); 10682 /* 10683 * We succeeded in clearing the option (ifindex == 0) or failed to 10684 * locate the ill and could not set the option (ifindex != 0) 10685 */ 10686 return (ifindex == 0 ? 0 : EINVAL); 10687 } 10688 10689 /* This routine sets socket options. */ 10690 /* ARGSUSED */ 10691 int 10692 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10693 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10694 void *dummy, cred_t *cr, mblk_t *first_mp) 10695 { 10696 int *i1 = (int *)invalp; 10697 conn_t *connp = Q_TO_CONN(q); 10698 int error = 0; 10699 boolean_t checkonly; 10700 ire_t *ire; 10701 boolean_t found; 10702 10703 switch (optset_context) { 10704 10705 case SETFN_OPTCOM_CHECKONLY: 10706 checkonly = B_TRUE; 10707 /* 10708 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10709 * inlen != 0 implies value supplied and 10710 * we have to "pretend" to set it. 10711 * inlen == 0 implies that there is no 10712 * value part in T_CHECK request and just validation 10713 * done elsewhere should be enough, we just return here. 10714 */ 10715 if (inlen == 0) { 10716 *outlenp = 0; 10717 return (0); 10718 } 10719 break; 10720 case SETFN_OPTCOM_NEGOTIATE: 10721 case SETFN_UD_NEGOTIATE: 10722 case SETFN_CONN_NEGOTIATE: 10723 checkonly = B_FALSE; 10724 break; 10725 default: 10726 /* 10727 * We should never get here 10728 */ 10729 *outlenp = 0; 10730 return (EINVAL); 10731 } 10732 10733 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10734 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10735 10736 /* 10737 * For fixed length options, no sanity check 10738 * of passed in length is done. It is assumed *_optcom_req() 10739 * routines do the right thing. 10740 */ 10741 10742 switch (level) { 10743 case SOL_SOCKET: 10744 /* 10745 * conn_lock protects the bitfields, and is used to 10746 * set the fields atomically. 10747 */ 10748 switch (name) { 10749 case SO_BROADCAST: 10750 if (!checkonly) { 10751 /* TODO: use value someplace? */ 10752 mutex_enter(&connp->conn_lock); 10753 connp->conn_broadcast = *i1 ? 1 : 0; 10754 mutex_exit(&connp->conn_lock); 10755 } 10756 break; /* goto sizeof (int) option return */ 10757 case SO_USELOOPBACK: 10758 if (!checkonly) { 10759 /* TODO: use value someplace? */ 10760 mutex_enter(&connp->conn_lock); 10761 connp->conn_loopback = *i1 ? 1 : 0; 10762 mutex_exit(&connp->conn_lock); 10763 } 10764 break; /* goto sizeof (int) option return */ 10765 case SO_DONTROUTE: 10766 if (!checkonly) { 10767 mutex_enter(&connp->conn_lock); 10768 connp->conn_dontroute = *i1 ? 1 : 0; 10769 mutex_exit(&connp->conn_lock); 10770 } 10771 break; /* goto sizeof (int) option return */ 10772 case SO_REUSEADDR: 10773 if (!checkonly) { 10774 mutex_enter(&connp->conn_lock); 10775 connp->conn_reuseaddr = *i1 ? 1 : 0; 10776 mutex_exit(&connp->conn_lock); 10777 } 10778 break; /* goto sizeof (int) option return */ 10779 case SO_PROTOTYPE: 10780 if (!checkonly) { 10781 mutex_enter(&connp->conn_lock); 10782 connp->conn_proto = *i1; 10783 mutex_exit(&connp->conn_lock); 10784 } 10785 break; /* goto sizeof (int) option return */ 10786 case SO_ALLZONES: 10787 if (!checkonly) { 10788 mutex_enter(&connp->conn_lock); 10789 if (IPCL_IS_BOUND(connp)) { 10790 mutex_exit(&connp->conn_lock); 10791 return (EINVAL); 10792 } 10793 connp->conn_allzones = *i1 != 0 ? 1 : 0; 10794 mutex_exit(&connp->conn_lock); 10795 } 10796 break; /* goto sizeof (int) option return */ 10797 case SO_ANON_MLP: 10798 if (!checkonly) { 10799 mutex_enter(&connp->conn_lock); 10800 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; 10801 mutex_exit(&connp->conn_lock); 10802 } 10803 break; /* goto sizeof (int) option return */ 10804 case SO_MAC_EXEMPT: 10805 if (secpolicy_net_mac_aware(cr) != 0 || 10806 IPCL_IS_BOUND(connp)) 10807 return (EACCES); 10808 if (!checkonly) { 10809 mutex_enter(&connp->conn_lock); 10810 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; 10811 mutex_exit(&connp->conn_lock); 10812 } 10813 break; /* goto sizeof (int) option return */ 10814 default: 10815 /* 10816 * "soft" error (negative) 10817 * option not handled at this level 10818 * Note: Do not modify *outlenp 10819 */ 10820 return (-EINVAL); 10821 } 10822 break; 10823 case IPPROTO_IP: 10824 switch (name) { 10825 case IP_NEXTHOP: 10826 if (secpolicy_net_config(cr, B_FALSE) != 0) 10827 return (EPERM); 10828 /* FALLTHRU */ 10829 case IP_MULTICAST_IF: 10830 case IP_DONTFAILOVER_IF: { 10831 ipaddr_t addr = *i1; 10832 10833 error = ip_opt_set_ipif(connp, addr, checkonly, name, 10834 first_mp); 10835 if (error != 0) 10836 return (error); 10837 break; /* goto sizeof (int) option return */ 10838 } 10839 10840 case IP_MULTICAST_TTL: 10841 /* Recorded in transport above IP */ 10842 *outvalp = *invalp; 10843 *outlenp = sizeof (uchar_t); 10844 return (0); 10845 case IP_MULTICAST_LOOP: 10846 if (!checkonly) { 10847 mutex_enter(&connp->conn_lock); 10848 connp->conn_multicast_loop = *invalp ? 1 : 0; 10849 mutex_exit(&connp->conn_lock); 10850 } 10851 *outvalp = *invalp; 10852 *outlenp = sizeof (uchar_t); 10853 return (0); 10854 case IP_ADD_MEMBERSHIP: 10855 case MCAST_JOIN_GROUP: 10856 case IP_DROP_MEMBERSHIP: 10857 case MCAST_LEAVE_GROUP: { 10858 struct ip_mreq *mreqp; 10859 struct group_req *greqp; 10860 ire_t *ire; 10861 boolean_t done = B_FALSE; 10862 ipaddr_t group, ifaddr; 10863 struct sockaddr_in *sin; 10864 uint32_t *ifindexp; 10865 boolean_t mcast_opt = B_TRUE; 10866 mcast_record_t fmode; 10867 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10868 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10869 10870 switch (name) { 10871 case IP_ADD_MEMBERSHIP: 10872 mcast_opt = B_FALSE; 10873 /* FALLTHRU */ 10874 case MCAST_JOIN_GROUP: 10875 fmode = MODE_IS_EXCLUDE; 10876 optfn = ip_opt_add_group; 10877 break; 10878 10879 case IP_DROP_MEMBERSHIP: 10880 mcast_opt = B_FALSE; 10881 /* FALLTHRU */ 10882 case MCAST_LEAVE_GROUP: 10883 fmode = MODE_IS_INCLUDE; 10884 optfn = ip_opt_delete_group; 10885 break; 10886 } 10887 10888 if (mcast_opt) { 10889 greqp = (struct group_req *)i1; 10890 sin = (struct sockaddr_in *)&greqp->gr_group; 10891 if (sin->sin_family != AF_INET) { 10892 *outlenp = 0; 10893 return (ENOPROTOOPT); 10894 } 10895 group = (ipaddr_t)sin->sin_addr.s_addr; 10896 ifaddr = INADDR_ANY; 10897 ifindexp = &greqp->gr_interface; 10898 } else { 10899 mreqp = (struct ip_mreq *)i1; 10900 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 10901 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 10902 ifindexp = NULL; 10903 } 10904 10905 /* 10906 * In the multirouting case, we need to replicate 10907 * the request on all interfaces that will take part 10908 * in replication. We do so because multirouting is 10909 * reflective, thus we will probably receive multi- 10910 * casts on those interfaces. 10911 * The ip_multirt_apply_membership() succeeds if the 10912 * operation succeeds on at least one interface. 10913 */ 10914 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 10915 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 10916 MATCH_IRE_MASK | MATCH_IRE_TYPE); 10917 if (ire != NULL) { 10918 if (ire->ire_flags & RTF_MULTIRT) { 10919 error = ip_multirt_apply_membership( 10920 optfn, ire, connp, checkonly, group, 10921 fmode, INADDR_ANY, first_mp); 10922 done = B_TRUE; 10923 } 10924 ire_refrele(ire); 10925 } 10926 if (!done) { 10927 error = optfn(connp, checkonly, group, ifaddr, 10928 ifindexp, fmode, INADDR_ANY, first_mp); 10929 } 10930 if (error) { 10931 /* 10932 * EINPROGRESS is a soft error, needs retry 10933 * so don't make *outlenp zero. 10934 */ 10935 if (error != EINPROGRESS) 10936 *outlenp = 0; 10937 return (error); 10938 } 10939 /* OK return - copy input buffer into output buffer */ 10940 if (invalp != outvalp) { 10941 /* don't trust bcopy for identical src/dst */ 10942 bcopy(invalp, outvalp, inlen); 10943 } 10944 *outlenp = inlen; 10945 return (0); 10946 } 10947 case IP_BLOCK_SOURCE: 10948 case IP_UNBLOCK_SOURCE: 10949 case IP_ADD_SOURCE_MEMBERSHIP: 10950 case IP_DROP_SOURCE_MEMBERSHIP: 10951 case MCAST_BLOCK_SOURCE: 10952 case MCAST_UNBLOCK_SOURCE: 10953 case MCAST_JOIN_SOURCE_GROUP: 10954 case MCAST_LEAVE_SOURCE_GROUP: { 10955 struct ip_mreq_source *imreqp; 10956 struct group_source_req *gsreqp; 10957 in_addr_t grp, src, ifaddr = INADDR_ANY; 10958 uint32_t ifindex = 0; 10959 mcast_record_t fmode; 10960 struct sockaddr_in *sin; 10961 ire_t *ire; 10962 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 10963 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 10964 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 10965 10966 switch (name) { 10967 case IP_BLOCK_SOURCE: 10968 mcast_opt = B_FALSE; 10969 /* FALLTHRU */ 10970 case MCAST_BLOCK_SOURCE: 10971 fmode = MODE_IS_EXCLUDE; 10972 optfn = ip_opt_add_group; 10973 break; 10974 10975 case IP_UNBLOCK_SOURCE: 10976 mcast_opt = B_FALSE; 10977 /* FALLTHRU */ 10978 case MCAST_UNBLOCK_SOURCE: 10979 fmode = MODE_IS_EXCLUDE; 10980 optfn = ip_opt_delete_group; 10981 break; 10982 10983 case IP_ADD_SOURCE_MEMBERSHIP: 10984 mcast_opt = B_FALSE; 10985 /* FALLTHRU */ 10986 case MCAST_JOIN_SOURCE_GROUP: 10987 fmode = MODE_IS_INCLUDE; 10988 optfn = ip_opt_add_group; 10989 break; 10990 10991 case IP_DROP_SOURCE_MEMBERSHIP: 10992 mcast_opt = B_FALSE; 10993 /* FALLTHRU */ 10994 case MCAST_LEAVE_SOURCE_GROUP: 10995 fmode = MODE_IS_INCLUDE; 10996 optfn = ip_opt_delete_group; 10997 break; 10998 } 10999 11000 if (mcast_opt) { 11001 gsreqp = (struct group_source_req *)i1; 11002 if (gsreqp->gsr_group.ss_family != AF_INET) { 11003 *outlenp = 0; 11004 return (ENOPROTOOPT); 11005 } 11006 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 11007 grp = (ipaddr_t)sin->sin_addr.s_addr; 11008 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 11009 src = (ipaddr_t)sin->sin_addr.s_addr; 11010 ifindex = gsreqp->gsr_interface; 11011 } else { 11012 imreqp = (struct ip_mreq_source *)i1; 11013 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 11014 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 11015 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 11016 } 11017 11018 /* 11019 * In the multirouting case, we need to replicate 11020 * the request as noted in the mcast cases above. 11021 */ 11022 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 11023 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11024 MATCH_IRE_MASK | MATCH_IRE_TYPE); 11025 if (ire != NULL) { 11026 if (ire->ire_flags & RTF_MULTIRT) { 11027 error = ip_multirt_apply_membership( 11028 optfn, ire, connp, checkonly, grp, 11029 fmode, src, first_mp); 11030 done = B_TRUE; 11031 } 11032 ire_refrele(ire); 11033 } 11034 if (!done) { 11035 error = optfn(connp, checkonly, grp, ifaddr, 11036 &ifindex, fmode, src, first_mp); 11037 } 11038 if (error != 0) { 11039 /* 11040 * EINPROGRESS is a soft error, needs retry 11041 * so don't make *outlenp zero. 11042 */ 11043 if (error != EINPROGRESS) 11044 *outlenp = 0; 11045 return (error); 11046 } 11047 /* OK return - copy input buffer into output buffer */ 11048 if (invalp != outvalp) { 11049 bcopy(invalp, outvalp, inlen); 11050 } 11051 *outlenp = inlen; 11052 return (0); 11053 } 11054 case IP_SEC_OPT: 11055 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11056 if (error != 0) { 11057 *outlenp = 0; 11058 return (error); 11059 } 11060 break; 11061 case IP_HDRINCL: 11062 case IP_OPTIONS: 11063 case T_IP_OPTIONS: 11064 case IP_TOS: 11065 case T_IP_TOS: 11066 case IP_TTL: 11067 case IP_RECVDSTADDR: 11068 case IP_RECVOPTS: 11069 /* OK return - copy input buffer into output buffer */ 11070 if (invalp != outvalp) { 11071 /* don't trust bcopy for identical src/dst */ 11072 bcopy(invalp, outvalp, inlen); 11073 } 11074 *outlenp = inlen; 11075 return (0); 11076 case IP_RECVIF: 11077 /* Retrieve the inbound interface index */ 11078 if (!checkonly) { 11079 mutex_enter(&connp->conn_lock); 11080 connp->conn_recvif = *i1 ? 1 : 0; 11081 mutex_exit(&connp->conn_lock); 11082 } 11083 break; /* goto sizeof (int) option return */ 11084 case IP_RECVPKTINFO: 11085 if (!checkonly) { 11086 mutex_enter(&connp->conn_lock); 11087 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11088 mutex_exit(&connp->conn_lock); 11089 } 11090 break; /* goto sizeof (int) option return */ 11091 case IP_RECVSLLA: 11092 /* Retrieve the source link layer address */ 11093 if (!checkonly) { 11094 mutex_enter(&connp->conn_lock); 11095 connp->conn_recvslla = *i1 ? 1 : 0; 11096 mutex_exit(&connp->conn_lock); 11097 } 11098 break; /* goto sizeof (int) option return */ 11099 case MRT_INIT: 11100 case MRT_DONE: 11101 case MRT_ADD_VIF: 11102 case MRT_DEL_VIF: 11103 case MRT_ADD_MFC: 11104 case MRT_DEL_MFC: 11105 case MRT_ASSERT: 11106 if ((error = secpolicy_net_config(cr, B_FALSE)) != 0) { 11107 *outlenp = 0; 11108 return (error); 11109 } 11110 error = ip_mrouter_set((int)name, q, checkonly, 11111 (uchar_t *)invalp, inlen, first_mp); 11112 if (error) { 11113 *outlenp = 0; 11114 return (error); 11115 } 11116 /* OK return - copy input buffer into output buffer */ 11117 if (invalp != outvalp) { 11118 /* don't trust bcopy for identical src/dst */ 11119 bcopy(invalp, outvalp, inlen); 11120 } 11121 *outlenp = inlen; 11122 return (0); 11123 case IP_BOUND_IF: 11124 case IP_XMIT_IF: 11125 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11126 level, name, first_mp); 11127 if (error != 0) 11128 return (error); 11129 break; /* goto sizeof (int) option return */ 11130 11131 case IP_UNSPEC_SRC: 11132 /* Allow sending with a zero source address */ 11133 if (!checkonly) { 11134 mutex_enter(&connp->conn_lock); 11135 connp->conn_unspec_src = *i1 ? 1 : 0; 11136 mutex_exit(&connp->conn_lock); 11137 } 11138 break; /* goto sizeof (int) option return */ 11139 default: 11140 /* 11141 * "soft" error (negative) 11142 * option not handled at this level 11143 * Note: Do not modify *outlenp 11144 */ 11145 return (-EINVAL); 11146 } 11147 break; 11148 case IPPROTO_IPV6: 11149 switch (name) { 11150 case IPV6_BOUND_IF: 11151 case IPV6_BOUND_PIF: 11152 case IPV6_DONTFAILOVER_IF: 11153 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11154 level, name, first_mp); 11155 if (error != 0) 11156 return (error); 11157 break; /* goto sizeof (int) option return */ 11158 11159 case IPV6_MULTICAST_IF: 11160 /* 11161 * The only possible errors are EINPROGRESS and 11162 * EINVAL. EINPROGRESS will be restarted and is not 11163 * a hard error. We call this option on both V4 and V6 11164 * If both return EINVAL, then this call returns 11165 * EINVAL. If at least one of them succeeds we 11166 * return success. 11167 */ 11168 found = B_FALSE; 11169 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 11170 level, name, first_mp); 11171 if (error == EINPROGRESS) 11172 return (error); 11173 if (error == 0) 11174 found = B_TRUE; 11175 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 11176 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 11177 if (error == 0) 11178 found = B_TRUE; 11179 if (!found) 11180 return (error); 11181 break; /* goto sizeof (int) option return */ 11182 11183 case IPV6_MULTICAST_HOPS: 11184 /* Recorded in transport above IP */ 11185 break; /* goto sizeof (int) option return */ 11186 case IPV6_MULTICAST_LOOP: 11187 if (!checkonly) { 11188 mutex_enter(&connp->conn_lock); 11189 connp->conn_multicast_loop = *i1; 11190 mutex_exit(&connp->conn_lock); 11191 } 11192 break; /* goto sizeof (int) option return */ 11193 case IPV6_JOIN_GROUP: 11194 case MCAST_JOIN_GROUP: 11195 case IPV6_LEAVE_GROUP: 11196 case MCAST_LEAVE_GROUP: { 11197 struct ipv6_mreq *ip_mreqp; 11198 struct group_req *greqp; 11199 ire_t *ire; 11200 boolean_t done = B_FALSE; 11201 in6_addr_t groupv6; 11202 uint32_t ifindex; 11203 boolean_t mcast_opt = B_TRUE; 11204 mcast_record_t fmode; 11205 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11206 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11207 11208 switch (name) { 11209 case IPV6_JOIN_GROUP: 11210 mcast_opt = B_FALSE; 11211 /* FALLTHRU */ 11212 case MCAST_JOIN_GROUP: 11213 fmode = MODE_IS_EXCLUDE; 11214 optfn = ip_opt_add_group_v6; 11215 break; 11216 11217 case IPV6_LEAVE_GROUP: 11218 mcast_opt = B_FALSE; 11219 /* FALLTHRU */ 11220 case MCAST_LEAVE_GROUP: 11221 fmode = MODE_IS_INCLUDE; 11222 optfn = ip_opt_delete_group_v6; 11223 break; 11224 } 11225 11226 if (mcast_opt) { 11227 struct sockaddr_in *sin; 11228 struct sockaddr_in6 *sin6; 11229 greqp = (struct group_req *)i1; 11230 if (greqp->gr_group.ss_family == AF_INET) { 11231 sin = (struct sockaddr_in *) 11232 &(greqp->gr_group); 11233 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 11234 &groupv6); 11235 } else { 11236 sin6 = (struct sockaddr_in6 *) 11237 &(greqp->gr_group); 11238 groupv6 = sin6->sin6_addr; 11239 } 11240 ifindex = greqp->gr_interface; 11241 } else { 11242 ip_mreqp = (struct ipv6_mreq *)i1; 11243 groupv6 = ip_mreqp->ipv6mr_multiaddr; 11244 ifindex = ip_mreqp->ipv6mr_interface; 11245 } 11246 /* 11247 * In the multirouting case, we need to replicate 11248 * the request on all interfaces that will take part 11249 * in replication. We do so because multirouting is 11250 * reflective, thus we will probably receive multi- 11251 * casts on those interfaces. 11252 * The ip_multirt_apply_membership_v6() succeeds if 11253 * the operation succeeds on at least one interface. 11254 */ 11255 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 11256 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11257 MATCH_IRE_MASK | MATCH_IRE_TYPE); 11258 if (ire != NULL) { 11259 if (ire->ire_flags & RTF_MULTIRT) { 11260 error = ip_multirt_apply_membership_v6( 11261 optfn, ire, connp, checkonly, 11262 &groupv6, fmode, &ipv6_all_zeros, 11263 first_mp); 11264 done = B_TRUE; 11265 } 11266 ire_refrele(ire); 11267 } 11268 if (!done) { 11269 error = optfn(connp, checkonly, &groupv6, 11270 ifindex, fmode, &ipv6_all_zeros, first_mp); 11271 } 11272 if (error) { 11273 /* 11274 * EINPROGRESS is a soft error, needs retry 11275 * so don't make *outlenp zero. 11276 */ 11277 if (error != EINPROGRESS) 11278 *outlenp = 0; 11279 return (error); 11280 } 11281 /* OK return - copy input buffer into output buffer */ 11282 if (invalp != outvalp) { 11283 /* don't trust bcopy for identical src/dst */ 11284 bcopy(invalp, outvalp, inlen); 11285 } 11286 *outlenp = inlen; 11287 return (0); 11288 } 11289 case MCAST_BLOCK_SOURCE: 11290 case MCAST_UNBLOCK_SOURCE: 11291 case MCAST_JOIN_SOURCE_GROUP: 11292 case MCAST_LEAVE_SOURCE_GROUP: { 11293 struct group_source_req *gsreqp; 11294 in6_addr_t v6grp, v6src; 11295 uint32_t ifindex; 11296 mcast_record_t fmode; 11297 ire_t *ire; 11298 boolean_t done = B_FALSE; 11299 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 11300 int, mcast_record_t, const in6_addr_t *, mblk_t *); 11301 11302 switch (name) { 11303 case MCAST_BLOCK_SOURCE: 11304 fmode = MODE_IS_EXCLUDE; 11305 optfn = ip_opt_add_group_v6; 11306 break; 11307 case MCAST_UNBLOCK_SOURCE: 11308 fmode = MODE_IS_EXCLUDE; 11309 optfn = ip_opt_delete_group_v6; 11310 break; 11311 case MCAST_JOIN_SOURCE_GROUP: 11312 fmode = MODE_IS_INCLUDE; 11313 optfn = ip_opt_add_group_v6; 11314 break; 11315 case MCAST_LEAVE_SOURCE_GROUP: 11316 fmode = MODE_IS_INCLUDE; 11317 optfn = ip_opt_delete_group_v6; 11318 break; 11319 } 11320 11321 gsreqp = (struct group_source_req *)i1; 11322 ifindex = gsreqp->gsr_interface; 11323 if (gsreqp->gsr_group.ss_family == AF_INET) { 11324 struct sockaddr_in *s; 11325 s = (struct sockaddr_in *)&gsreqp->gsr_group; 11326 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 11327 s = (struct sockaddr_in *)&gsreqp->gsr_source; 11328 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 11329 } else { 11330 struct sockaddr_in6 *s6; 11331 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 11332 v6grp = s6->sin6_addr; 11333 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 11334 v6src = s6->sin6_addr; 11335 } 11336 11337 /* 11338 * In the multirouting case, we need to replicate 11339 * the request as noted in the mcast cases above. 11340 */ 11341 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 11342 IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, 11343 MATCH_IRE_MASK | MATCH_IRE_TYPE); 11344 if (ire != NULL) { 11345 if (ire->ire_flags & RTF_MULTIRT) { 11346 error = ip_multirt_apply_membership_v6( 11347 optfn, ire, connp, checkonly, 11348 &v6grp, fmode, &v6src, first_mp); 11349 done = B_TRUE; 11350 } 11351 ire_refrele(ire); 11352 } 11353 if (!done) { 11354 error = optfn(connp, checkonly, &v6grp, 11355 ifindex, fmode, &v6src, first_mp); 11356 } 11357 if (error != 0) { 11358 /* 11359 * EINPROGRESS is a soft error, needs retry 11360 * so don't make *outlenp zero. 11361 */ 11362 if (error != EINPROGRESS) 11363 *outlenp = 0; 11364 return (error); 11365 } 11366 /* OK return - copy input buffer into output buffer */ 11367 if (invalp != outvalp) { 11368 bcopy(invalp, outvalp, inlen); 11369 } 11370 *outlenp = inlen; 11371 return (0); 11372 } 11373 case IPV6_UNICAST_HOPS: 11374 /* Recorded in transport above IP */ 11375 break; /* goto sizeof (int) option return */ 11376 case IPV6_UNSPEC_SRC: 11377 /* Allow sending with a zero source address */ 11378 if (!checkonly) { 11379 mutex_enter(&connp->conn_lock); 11380 connp->conn_unspec_src = *i1 ? 1 : 0; 11381 mutex_exit(&connp->conn_lock); 11382 } 11383 break; /* goto sizeof (int) option return */ 11384 case IPV6_RECVPKTINFO: 11385 if (!checkonly) { 11386 mutex_enter(&connp->conn_lock); 11387 connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; 11388 mutex_exit(&connp->conn_lock); 11389 } 11390 break; /* goto sizeof (int) option return */ 11391 case IPV6_RECVTCLASS: 11392 if (!checkonly) { 11393 if (*i1 < 0 || *i1 > 1) { 11394 return (EINVAL); 11395 } 11396 mutex_enter(&connp->conn_lock); 11397 connp->conn_ipv6_recvtclass = *i1; 11398 mutex_exit(&connp->conn_lock); 11399 } 11400 break; 11401 case IPV6_RECVPATHMTU: 11402 if (!checkonly) { 11403 if (*i1 < 0 || *i1 > 1) { 11404 return (EINVAL); 11405 } 11406 mutex_enter(&connp->conn_lock); 11407 connp->conn_ipv6_recvpathmtu = *i1; 11408 mutex_exit(&connp->conn_lock); 11409 } 11410 break; 11411 case IPV6_RECVHOPLIMIT: 11412 if (!checkonly) { 11413 mutex_enter(&connp->conn_lock); 11414 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 11415 mutex_exit(&connp->conn_lock); 11416 } 11417 break; /* goto sizeof (int) option return */ 11418 case IPV6_RECVHOPOPTS: 11419 if (!checkonly) { 11420 mutex_enter(&connp->conn_lock); 11421 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 11422 mutex_exit(&connp->conn_lock); 11423 } 11424 break; /* goto sizeof (int) option return */ 11425 case IPV6_RECVDSTOPTS: 11426 if (!checkonly) { 11427 mutex_enter(&connp->conn_lock); 11428 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 11429 mutex_exit(&connp->conn_lock); 11430 } 11431 break; /* goto sizeof (int) option return */ 11432 case IPV6_RECVRTHDR: 11433 if (!checkonly) { 11434 mutex_enter(&connp->conn_lock); 11435 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 11436 mutex_exit(&connp->conn_lock); 11437 } 11438 break; /* goto sizeof (int) option return */ 11439 case IPV6_RECVRTHDRDSTOPTS: 11440 if (!checkonly) { 11441 mutex_enter(&connp->conn_lock); 11442 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 11443 mutex_exit(&connp->conn_lock); 11444 } 11445 break; /* goto sizeof (int) option return */ 11446 case IPV6_PKTINFO: 11447 if (inlen == 0) 11448 return (-EINVAL); /* clearing option */ 11449 error = ip6_set_pktinfo(cr, connp, 11450 (struct in6_pktinfo *)invalp, first_mp); 11451 if (error != 0) 11452 *outlenp = 0; 11453 else 11454 *outlenp = inlen; 11455 return (error); 11456 case IPV6_NEXTHOP: { 11457 struct sockaddr_in6 *sin6; 11458 11459 /* Verify that the nexthop is reachable */ 11460 if (inlen == 0) 11461 return (-EINVAL); /* clearing option */ 11462 11463 sin6 = (struct sockaddr_in6 *)invalp; 11464 ire = ire_route_lookup_v6(&sin6->sin6_addr, 11465 0, 0, 0, NULL, NULL, connp->conn_zoneid, 11466 NULL, MATCH_IRE_DEFAULT); 11467 11468 if (ire == NULL) { 11469 *outlenp = 0; 11470 return (EHOSTUNREACH); 11471 } 11472 ire_refrele(ire); 11473 return (-EINVAL); 11474 } 11475 case IPV6_SEC_OPT: 11476 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 11477 if (error != 0) { 11478 *outlenp = 0; 11479 return (error); 11480 } 11481 break; 11482 case IPV6_SRC_PREFERENCES: { 11483 /* 11484 * This is implemented strictly in the ip module 11485 * (here and in tcp_opt_*() to accomodate tcp 11486 * sockets). Modules above ip pass this option 11487 * down here since ip is the only one that needs to 11488 * be aware of source address preferences. 11489 * 11490 * This socket option only affects connected 11491 * sockets that haven't already bound to a specific 11492 * IPv6 address. In other words, sockets that 11493 * don't call bind() with an address other than the 11494 * unspecified address and that call connect(). 11495 * ip_bind_connected_v6() passes these preferences 11496 * to the ipif_select_source_v6() function. 11497 */ 11498 if (inlen != sizeof (uint32_t)) 11499 return (EINVAL); 11500 error = ip6_set_src_preferences(connp, 11501 *(uint32_t *)invalp); 11502 if (error != 0) { 11503 *outlenp = 0; 11504 return (error); 11505 } else { 11506 *outlenp = sizeof (uint32_t); 11507 } 11508 break; 11509 } 11510 case IPV6_V6ONLY: 11511 if (*i1 < 0 || *i1 > 1) { 11512 return (EINVAL); 11513 } 11514 mutex_enter(&connp->conn_lock); 11515 connp->conn_ipv6_v6only = *i1; 11516 mutex_exit(&connp->conn_lock); 11517 break; 11518 default: 11519 return (-EINVAL); 11520 } 11521 break; 11522 default: 11523 /* 11524 * "soft" error (negative) 11525 * option not handled at this level 11526 * Note: Do not modify *outlenp 11527 */ 11528 return (-EINVAL); 11529 } 11530 /* 11531 * Common case of return from an option that is sizeof (int) 11532 */ 11533 *(int *)outvalp = *i1; 11534 *outlenp = sizeof (int); 11535 return (0); 11536 } 11537 11538 /* 11539 * This routine gets default values of certain options whose default 11540 * values are maintained by protocol specific code 11541 */ 11542 /* ARGSUSED */ 11543 int 11544 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 11545 { 11546 int *i1 = (int *)ptr; 11547 11548 switch (level) { 11549 case IPPROTO_IP: 11550 switch (name) { 11551 case IP_MULTICAST_TTL: 11552 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 11553 return (sizeof (uchar_t)); 11554 case IP_MULTICAST_LOOP: 11555 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 11556 return (sizeof (uchar_t)); 11557 default: 11558 return (-1); 11559 } 11560 case IPPROTO_IPV6: 11561 switch (name) { 11562 case IPV6_UNICAST_HOPS: 11563 *i1 = ipv6_def_hops; 11564 return (sizeof (int)); 11565 case IPV6_MULTICAST_HOPS: 11566 *i1 = IP_DEFAULT_MULTICAST_TTL; 11567 return (sizeof (int)); 11568 case IPV6_MULTICAST_LOOP: 11569 *i1 = IP_DEFAULT_MULTICAST_LOOP; 11570 return (sizeof (int)); 11571 case IPV6_V6ONLY: 11572 *i1 = 1; 11573 return (sizeof (int)); 11574 default: 11575 return (-1); 11576 } 11577 default: 11578 return (-1); 11579 } 11580 /* NOTREACHED */ 11581 } 11582 11583 /* 11584 * Given a destination address and a pointer to where to put the information 11585 * this routine fills in the mtuinfo. 11586 */ 11587 int 11588 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 11589 struct ip6_mtuinfo *mtuinfo) 11590 { 11591 ire_t *ire; 11592 11593 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 11594 return (-1); 11595 11596 bzero(mtuinfo, sizeof (*mtuinfo)); 11597 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 11598 mtuinfo->ip6m_addr.sin6_port = port; 11599 mtuinfo->ip6m_addr.sin6_addr = *in6; 11600 11601 ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL); 11602 if (ire != NULL) { 11603 mtuinfo->ip6m_mtu = ire->ire_max_frag; 11604 ire_refrele(ire); 11605 } else { 11606 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 11607 } 11608 return (sizeof (struct ip6_mtuinfo)); 11609 } 11610 11611 /* 11612 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 11613 * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and 11614 * isn't. This doesn't matter as the error checking is done properly for the 11615 * other MRT options coming in through ip_opt_set. 11616 */ 11617 int 11618 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 11619 { 11620 conn_t *connp = Q_TO_CONN(q); 11621 ipsec_req_t *req = (ipsec_req_t *)ptr; 11622 11623 switch (level) { 11624 case IPPROTO_IP: 11625 switch (name) { 11626 case MRT_VERSION: 11627 case MRT_ASSERT: 11628 (void) ip_mrouter_get(name, q, ptr); 11629 return (sizeof (int)); 11630 case IP_SEC_OPT: 11631 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 11632 case IP_NEXTHOP: 11633 if (connp->conn_nexthop_set) { 11634 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 11635 return (sizeof (ipaddr_t)); 11636 } else 11637 return (0); 11638 case IP_RECVPKTINFO: 11639 *(int *)ptr = connp->conn_ip_recvpktinfo ? 1: 0; 11640 return (sizeof (int)); 11641 default: 11642 break; 11643 } 11644 break; 11645 case IPPROTO_IPV6: 11646 switch (name) { 11647 case IPV6_SEC_OPT: 11648 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 11649 case IPV6_SRC_PREFERENCES: { 11650 return (ip6_get_src_preferences(connp, 11651 (uint32_t *)ptr)); 11652 } 11653 case IPV6_V6ONLY: 11654 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 11655 return (sizeof (int)); 11656 case IPV6_PATHMTU: 11657 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 11658 (struct ip6_mtuinfo *)ptr)); 11659 default: 11660 break; 11661 } 11662 break; 11663 default: 11664 break; 11665 } 11666 return (-1); 11667 } 11668 11669 /* Named Dispatch routine to get a current value out of our parameter table. */ 11670 /* ARGSUSED */ 11671 static int 11672 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11673 { 11674 ipparam_t *ippa = (ipparam_t *)cp; 11675 11676 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 11677 return (0); 11678 } 11679 11680 /* ARGSUSED */ 11681 static int 11682 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 11683 { 11684 11685 (void) mi_mpprintf(mp, "%d", *(int *)cp); 11686 return (0); 11687 } 11688 11689 /* 11690 * Set ip{,6}_forwarding values. This means walking through all of the 11691 * ill's and toggling their forwarding values. 11692 */ 11693 /* ARGSUSED */ 11694 static int 11695 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11696 { 11697 long new_value; 11698 int *forwarding_value = (int *)cp; 11699 ill_t *walker; 11700 boolean_t isv6 = (forwarding_value == &ipv6_forward); 11701 ill_walk_context_t ctx; 11702 11703 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11704 new_value < 0 || new_value > 1) { 11705 return (EINVAL); 11706 } 11707 11708 *forwarding_value = new_value; 11709 11710 /* 11711 * Regardless of the current value of ip_forwarding, set all per-ill 11712 * values of ip_forwarding to the value being set. 11713 * 11714 * Bring all the ill's up to date with the new global value. 11715 */ 11716 rw_enter(&ill_g_lock, RW_READER); 11717 11718 if (isv6) 11719 walker = ILL_START_WALK_V6(&ctx); 11720 else 11721 walker = ILL_START_WALK_V4(&ctx); 11722 for (; walker != NULL; walker = ill_next(&ctx, walker)) { 11723 (void) ill_forward_set(q, mp, (new_value != 0), 11724 (caddr_t)walker); 11725 } 11726 rw_exit(&ill_g_lock); 11727 11728 return (0); 11729 } 11730 11731 /* 11732 * Walk through the param array specified registering each element with the 11733 * Named Dispatch handler. This is called only during init. So it is ok 11734 * not to acquire any locks 11735 */ 11736 static boolean_t 11737 ip_param_register(ipparam_t *ippa, size_t ippa_cnt, 11738 ipndp_t *ipnd, size_t ipnd_cnt) 11739 { 11740 for (; ippa_cnt-- > 0; ippa++) { 11741 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 11742 if (!nd_load(&ip_g_nd, ippa->ip_param_name, 11743 ip_param_get, ip_param_set, (caddr_t)ippa)) { 11744 nd_free(&ip_g_nd); 11745 return (B_FALSE); 11746 } 11747 } 11748 } 11749 11750 for (; ipnd_cnt-- > 0; ipnd++) { 11751 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 11752 if (!nd_load(&ip_g_nd, ipnd->ip_ndp_name, 11753 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 11754 ipnd->ip_ndp_data)) { 11755 nd_free(&ip_g_nd); 11756 return (B_FALSE); 11757 } 11758 } 11759 } 11760 11761 return (B_TRUE); 11762 } 11763 11764 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 11765 /* ARGSUSED */ 11766 static int 11767 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 11768 { 11769 long new_value; 11770 ipparam_t *ippa = (ipparam_t *)cp; 11771 11772 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11773 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 11774 return (EINVAL); 11775 } 11776 ippa->ip_param_value = new_value; 11777 return (0); 11778 } 11779 11780 /* 11781 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 11782 * When an ipf is passed here for the first time, if 11783 * we already have in-order fragments on the queue, we convert from the fast- 11784 * path reassembly scheme to the hard-case scheme. From then on, additional 11785 * fragments are reassembled here. We keep track of the start and end offsets 11786 * of each piece, and the number of holes in the chain. When the hole count 11787 * goes to zero, we are done! 11788 * 11789 * The ipf_count will be updated to account for any mblk(s) added (pointed to 11790 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 11791 * ipfb_count and ill_frag_count by the difference of ipf_count before and 11792 * after the call to ip_reassemble(). 11793 */ 11794 int 11795 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 11796 size_t msg_len) 11797 { 11798 uint_t end; 11799 mblk_t *next_mp; 11800 mblk_t *mp1; 11801 uint_t offset; 11802 boolean_t incr_dups = B_TRUE; 11803 boolean_t offset_zero_seen = B_FALSE; 11804 boolean_t pkt_boundary_checked = B_FALSE; 11805 11806 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 11807 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 11808 11809 /* Add in byte count */ 11810 ipf->ipf_count += msg_len; 11811 if (ipf->ipf_end) { 11812 /* 11813 * We were part way through in-order reassembly, but now there 11814 * is a hole. We walk through messages already queued, and 11815 * mark them for hard case reassembly. We know that up till 11816 * now they were in order starting from offset zero. 11817 */ 11818 offset = 0; 11819 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 11820 IP_REASS_SET_START(mp1, offset); 11821 if (offset == 0) { 11822 ASSERT(ipf->ipf_nf_hdr_len != 0); 11823 offset = -ipf->ipf_nf_hdr_len; 11824 } 11825 offset += mp1->b_wptr - mp1->b_rptr; 11826 IP_REASS_SET_END(mp1, offset); 11827 } 11828 /* One hole at the end. */ 11829 ipf->ipf_hole_cnt = 1; 11830 /* Brand it as a hard case, forever. */ 11831 ipf->ipf_end = 0; 11832 } 11833 /* Walk through all the new pieces. */ 11834 do { 11835 end = start + (mp->b_wptr - mp->b_rptr); 11836 /* 11837 * If start is 0, decrease 'end' only for the first mblk of 11838 * the fragment. Otherwise 'end' can get wrong value in the 11839 * second pass of the loop if first mblk is exactly the 11840 * size of ipf_nf_hdr_len. 11841 */ 11842 if (start == 0 && !offset_zero_seen) { 11843 /* First segment */ 11844 ASSERT(ipf->ipf_nf_hdr_len != 0); 11845 end -= ipf->ipf_nf_hdr_len; 11846 offset_zero_seen = B_TRUE; 11847 } 11848 next_mp = mp->b_cont; 11849 /* 11850 * We are checking to see if there is any interesing data 11851 * to process. If there isn't and the mblk isn't the 11852 * one which carries the unfragmentable header then we 11853 * drop it. It's possible to have just the unfragmentable 11854 * header come through without any data. That needs to be 11855 * saved. 11856 * 11857 * If the assert at the top of this function holds then the 11858 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 11859 * is infrequently traveled enough that the test is left in 11860 * to protect against future code changes which break that 11861 * invariant. 11862 */ 11863 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 11864 /* Empty. Blast it. */ 11865 IP_REASS_SET_START(mp, 0); 11866 IP_REASS_SET_END(mp, 0); 11867 /* 11868 * If the ipf points to the mblk we are about to free, 11869 * update ipf to point to the next mblk (or NULL 11870 * if none). 11871 */ 11872 if (ipf->ipf_mp->b_cont == mp) 11873 ipf->ipf_mp->b_cont = next_mp; 11874 freeb(mp); 11875 continue; 11876 } 11877 mp->b_cont = NULL; 11878 IP_REASS_SET_START(mp, start); 11879 IP_REASS_SET_END(mp, end); 11880 if (!ipf->ipf_tail_mp) { 11881 ipf->ipf_tail_mp = mp; 11882 ipf->ipf_mp->b_cont = mp; 11883 if (start == 0 || !more) { 11884 ipf->ipf_hole_cnt = 1; 11885 /* 11886 * if the first fragment comes in more than one 11887 * mblk, this loop will be executed for each 11888 * mblk. Need to adjust hole count so exiting 11889 * this routine will leave hole count at 1. 11890 */ 11891 if (next_mp) 11892 ipf->ipf_hole_cnt++; 11893 } else 11894 ipf->ipf_hole_cnt = 2; 11895 continue; 11896 } else if (ipf->ipf_last_frag_seen && !more && 11897 !pkt_boundary_checked) { 11898 /* 11899 * We check datagram boundary only if this fragment 11900 * claims to be the last fragment and we have seen a 11901 * last fragment in the past too. We do this only 11902 * once for a given fragment. 11903 * 11904 * start cannot be 0 here as fragments with start=0 11905 * and MF=0 gets handled as a complete packet. These 11906 * fragments should not reach here. 11907 */ 11908 11909 if (start + msgdsize(mp) != 11910 IP_REASS_END(ipf->ipf_tail_mp)) { 11911 /* 11912 * We have two fragments both of which claim 11913 * to be the last fragment but gives conflicting 11914 * information about the whole datagram size. 11915 * Something fishy is going on. Drop the 11916 * fragment and free up the reassembly list. 11917 */ 11918 return (IP_REASS_FAILED); 11919 } 11920 11921 /* 11922 * We shouldn't come to this code block again for this 11923 * particular fragment. 11924 */ 11925 pkt_boundary_checked = B_TRUE; 11926 } 11927 11928 /* New stuff at or beyond tail? */ 11929 offset = IP_REASS_END(ipf->ipf_tail_mp); 11930 if (start >= offset) { 11931 if (ipf->ipf_last_frag_seen) { 11932 /* current fragment is beyond last fragment */ 11933 return (IP_REASS_FAILED); 11934 } 11935 /* Link it on end. */ 11936 ipf->ipf_tail_mp->b_cont = mp; 11937 ipf->ipf_tail_mp = mp; 11938 if (more) { 11939 if (start != offset) 11940 ipf->ipf_hole_cnt++; 11941 } else if (start == offset && next_mp == NULL) 11942 ipf->ipf_hole_cnt--; 11943 continue; 11944 } 11945 mp1 = ipf->ipf_mp->b_cont; 11946 offset = IP_REASS_START(mp1); 11947 /* New stuff at the front? */ 11948 if (start < offset) { 11949 if (start == 0) { 11950 if (end >= offset) { 11951 /* Nailed the hole at the begining. */ 11952 ipf->ipf_hole_cnt--; 11953 } 11954 } else if (end < offset) { 11955 /* 11956 * A hole, stuff, and a hole where there used 11957 * to be just a hole. 11958 */ 11959 ipf->ipf_hole_cnt++; 11960 } 11961 mp->b_cont = mp1; 11962 /* Check for overlap. */ 11963 while (end > offset) { 11964 if (end < IP_REASS_END(mp1)) { 11965 mp->b_wptr -= end - offset; 11966 IP_REASS_SET_END(mp, offset); 11967 BUMP_MIB(ill->ill_ip_mib, 11968 ipIfStatsReasmPartDups); 11969 break; 11970 } 11971 /* Did we cover another hole? */ 11972 if ((mp1->b_cont && 11973 IP_REASS_END(mp1) != 11974 IP_REASS_START(mp1->b_cont) && 11975 end >= IP_REASS_START(mp1->b_cont)) || 11976 (!ipf->ipf_last_frag_seen && !more)) { 11977 ipf->ipf_hole_cnt--; 11978 } 11979 /* Clip out mp1. */ 11980 if ((mp->b_cont = mp1->b_cont) == NULL) { 11981 /* 11982 * After clipping out mp1, this guy 11983 * is now hanging off the end. 11984 */ 11985 ipf->ipf_tail_mp = mp; 11986 } 11987 IP_REASS_SET_START(mp1, 0); 11988 IP_REASS_SET_END(mp1, 0); 11989 /* Subtract byte count */ 11990 ipf->ipf_count -= mp1->b_datap->db_lim - 11991 mp1->b_datap->db_base; 11992 freeb(mp1); 11993 BUMP_MIB(ill->ill_ip_mib, 11994 ipIfStatsReasmPartDups); 11995 mp1 = mp->b_cont; 11996 if (!mp1) 11997 break; 11998 offset = IP_REASS_START(mp1); 11999 } 12000 ipf->ipf_mp->b_cont = mp; 12001 continue; 12002 } 12003 /* 12004 * The new piece starts somewhere between the start of the head 12005 * and before the end of the tail. 12006 */ 12007 for (; mp1; mp1 = mp1->b_cont) { 12008 offset = IP_REASS_END(mp1); 12009 if (start < offset) { 12010 if (end <= offset) { 12011 /* Nothing new. */ 12012 IP_REASS_SET_START(mp, 0); 12013 IP_REASS_SET_END(mp, 0); 12014 /* Subtract byte count */ 12015 ipf->ipf_count -= mp->b_datap->db_lim - 12016 mp->b_datap->db_base; 12017 if (incr_dups) { 12018 ipf->ipf_num_dups++; 12019 incr_dups = B_FALSE; 12020 } 12021 freeb(mp); 12022 BUMP_MIB(ill->ill_ip_mib, 12023 ipIfStatsReasmDuplicates); 12024 break; 12025 } 12026 /* 12027 * Trim redundant stuff off beginning of new 12028 * piece. 12029 */ 12030 IP_REASS_SET_START(mp, offset); 12031 mp->b_rptr += offset - start; 12032 BUMP_MIB(ill->ill_ip_mib, 12033 ipIfStatsReasmPartDups); 12034 start = offset; 12035 if (!mp1->b_cont) { 12036 /* 12037 * After trimming, this guy is now 12038 * hanging off the end. 12039 */ 12040 mp1->b_cont = mp; 12041 ipf->ipf_tail_mp = mp; 12042 if (!more) { 12043 ipf->ipf_hole_cnt--; 12044 } 12045 break; 12046 } 12047 } 12048 if (start >= IP_REASS_START(mp1->b_cont)) 12049 continue; 12050 /* Fill a hole */ 12051 if (start > offset) 12052 ipf->ipf_hole_cnt++; 12053 mp->b_cont = mp1->b_cont; 12054 mp1->b_cont = mp; 12055 mp1 = mp->b_cont; 12056 offset = IP_REASS_START(mp1); 12057 if (end >= offset) { 12058 ipf->ipf_hole_cnt--; 12059 /* Check for overlap. */ 12060 while (end > offset) { 12061 if (end < IP_REASS_END(mp1)) { 12062 mp->b_wptr -= end - offset; 12063 IP_REASS_SET_END(mp, offset); 12064 /* 12065 * TODO we might bump 12066 * this up twice if there is 12067 * overlap at both ends. 12068 */ 12069 BUMP_MIB(ill->ill_ip_mib, 12070 ipIfStatsReasmPartDups); 12071 break; 12072 } 12073 /* Did we cover another hole? */ 12074 if ((mp1->b_cont && 12075 IP_REASS_END(mp1) 12076 != IP_REASS_START(mp1->b_cont) && 12077 end >= 12078 IP_REASS_START(mp1->b_cont)) || 12079 (!ipf->ipf_last_frag_seen && 12080 !more)) { 12081 ipf->ipf_hole_cnt--; 12082 } 12083 /* Clip out mp1. */ 12084 if ((mp->b_cont = mp1->b_cont) == 12085 NULL) { 12086 /* 12087 * After clipping out mp1, 12088 * this guy is now hanging 12089 * off the end. 12090 */ 12091 ipf->ipf_tail_mp = mp; 12092 } 12093 IP_REASS_SET_START(mp1, 0); 12094 IP_REASS_SET_END(mp1, 0); 12095 /* Subtract byte count */ 12096 ipf->ipf_count -= 12097 mp1->b_datap->db_lim - 12098 mp1->b_datap->db_base; 12099 freeb(mp1); 12100 BUMP_MIB(ill->ill_ip_mib, 12101 ipIfStatsReasmPartDups); 12102 mp1 = mp->b_cont; 12103 if (!mp1) 12104 break; 12105 offset = IP_REASS_START(mp1); 12106 } 12107 } 12108 break; 12109 } 12110 } while (start = end, mp = next_mp); 12111 12112 /* Fragment just processed could be the last one. Remember this fact */ 12113 if (!more) 12114 ipf->ipf_last_frag_seen = B_TRUE; 12115 12116 /* Still got holes? */ 12117 if (ipf->ipf_hole_cnt) 12118 return (IP_REASS_PARTIAL); 12119 /* Clean up overloaded fields to avoid upstream disasters. */ 12120 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 12121 IP_REASS_SET_START(mp1, 0); 12122 IP_REASS_SET_END(mp1, 0); 12123 } 12124 return (IP_REASS_COMPLETE); 12125 } 12126 12127 /* 12128 * ipsec processing for the fast path, used for input UDP Packets 12129 */ 12130 static boolean_t 12131 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 12132 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present) 12133 { 12134 uint32_t ill_index; 12135 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 12136 12137 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12138 /* The ill_index of the incoming ILL */ 12139 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 12140 12141 /* pass packet up to the transport */ 12142 if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) { 12143 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 12144 NULL, mctl_present); 12145 if (*first_mpp == NULL) { 12146 return (B_FALSE); 12147 } 12148 } 12149 12150 /* Initiate IPPF processing for fastpath UDP */ 12151 if (IPP_ENABLED(IPP_LOCAL_IN)) { 12152 ip_process(IPP_LOCAL_IN, mpp, ill_index); 12153 if (*mpp == NULL) { 12154 ip2dbg(("ip_input_ipsec_process: UDP pkt " 12155 "deferred/dropped during IPPF processing\n")); 12156 return (B_FALSE); 12157 } 12158 } 12159 /* 12160 * We make the checks as below since we are in the fast path 12161 * and want to minimize the number of checks if the IP_RECVIF and/or 12162 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 12163 */ 12164 if (connp->conn_recvif || connp->conn_recvslla || 12165 connp->conn_ip_recvpktinfo) { 12166 if (connp->conn_recvif) { 12167 in_flags = IPF_RECVIF; 12168 } 12169 /* 12170 * UDP supports IP_RECVPKTINFO option for both v4 and v6 12171 * so the flag passed to ip_add_info is based on IP version 12172 * of connp. 12173 */ 12174 if (connp->conn_ip_recvpktinfo) { 12175 if (connp->conn_af_isv6) { 12176 /* 12177 * V6 only needs index 12178 */ 12179 in_flags |= IPF_RECVIF; 12180 } else { 12181 /* 12182 * V4 needs index + matching address. 12183 */ 12184 in_flags |= IPF_RECVADDR; 12185 } 12186 } 12187 if (connp->conn_recvslla) { 12188 in_flags |= IPF_RECVSLLA; 12189 } 12190 /* 12191 * since in_flags are being set ill will be 12192 * referenced in ip_add_info, so it better not 12193 * be NULL. 12194 */ 12195 /* 12196 * the actual data will be contained in b_cont 12197 * upon successful return of the following call. 12198 * If the call fails then the original mblk is 12199 * returned. 12200 */ 12201 *mpp = ip_add_info(*mpp, ill, in_flags, IPCL_ZONEID(connp)); 12202 } 12203 12204 return (B_TRUE); 12205 } 12206 12207 /* 12208 * Fragmentation reassembly. Each ILL has a hash table for 12209 * queuing packets undergoing reassembly for all IPIFs 12210 * associated with the ILL. The hash is based on the packet 12211 * IP ident field. The ILL frag hash table was allocated 12212 * as a timer block at the time the ILL was created. Whenever 12213 * there is anything on the reassembly queue, the timer will 12214 * be running. Returns B_TRUE if successful else B_FALSE; 12215 * frees mp on failure. 12216 */ 12217 static boolean_t 12218 ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, 12219 uint32_t *cksum_val, uint16_t *cksum_flags) 12220 { 12221 uint32_t frag_offset_flags; 12222 ill_t *ill = (ill_t *)q->q_ptr; 12223 mblk_t *mp = *mpp; 12224 mblk_t *t_mp; 12225 ipaddr_t dst; 12226 uint8_t proto = ipha->ipha_protocol; 12227 uint32_t sum_val; 12228 uint16_t sum_flags; 12229 ipf_t *ipf; 12230 ipf_t **ipfp; 12231 ipfb_t *ipfb; 12232 uint16_t ident; 12233 uint32_t offset; 12234 ipaddr_t src; 12235 uint_t hdr_length; 12236 uint32_t end; 12237 mblk_t *mp1; 12238 mblk_t *tail_mp; 12239 size_t count; 12240 size_t msg_len; 12241 uint8_t ecn_info = 0; 12242 uint32_t packet_size; 12243 boolean_t pruned = B_FALSE; 12244 12245 if (cksum_val != NULL) 12246 *cksum_val = 0; 12247 if (cksum_flags != NULL) 12248 *cksum_flags = 0; 12249 12250 /* 12251 * Drop the fragmented as early as possible, if 12252 * we don't have resource(s) to re-assemble. 12253 */ 12254 if (ip_reass_queue_bytes == 0) { 12255 freemsg(mp); 12256 return (B_FALSE); 12257 } 12258 12259 /* Check for fragmentation offset; return if there's none */ 12260 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 12261 (IPH_MF | IPH_OFFSET)) == 0) 12262 return (B_TRUE); 12263 12264 /* 12265 * We utilize hardware computed checksum info only for UDP since 12266 * IP fragmentation is a normal occurence for the protocol. In 12267 * addition, checksum offload support for IP fragments carrying 12268 * UDP payload is commonly implemented across network adapters. 12269 */ 12270 ASSERT(ill != NULL); 12271 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 12272 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 12273 mblk_t *mp1 = mp->b_cont; 12274 int32_t len; 12275 12276 /* Record checksum information from the packet */ 12277 sum_val = (uint32_t)DB_CKSUM16(mp); 12278 sum_flags = DB_CKSUMFLAGS(mp); 12279 12280 /* IP payload offset from beginning of mblk */ 12281 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 12282 12283 if ((sum_flags & HCK_PARTIALCKSUM) && 12284 (mp1 == NULL || mp1->b_cont == NULL) && 12285 offset >= DB_CKSUMSTART(mp) && 12286 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 12287 uint32_t adj; 12288 /* 12289 * Partial checksum has been calculated by hardware 12290 * and attached to the packet; in addition, any 12291 * prepended extraneous data is even byte aligned. 12292 * If any such data exists, we adjust the checksum; 12293 * this would also handle any postpended data. 12294 */ 12295 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 12296 mp, mp1, len, adj); 12297 12298 /* One's complement subtract extraneous checksum */ 12299 if (adj >= sum_val) 12300 sum_val = ~(adj - sum_val) & 0xFFFF; 12301 else 12302 sum_val -= adj; 12303 } 12304 } else { 12305 sum_val = 0; 12306 sum_flags = 0; 12307 } 12308 12309 /* Clear hardware checksumming flag */ 12310 DB_CKSUMFLAGS(mp) = 0; 12311 12312 ident = ipha->ipha_ident; 12313 offset = (frag_offset_flags << 3) & 0xFFFF; 12314 src = ipha->ipha_src; 12315 dst = ipha->ipha_dst; 12316 hdr_length = IPH_HDR_LENGTH(ipha); 12317 end = ntohs(ipha->ipha_length) - hdr_length; 12318 12319 /* If end == 0 then we have a packet with no data, so just free it */ 12320 if (end == 0) { 12321 freemsg(mp); 12322 return (B_FALSE); 12323 } 12324 12325 /* Record the ECN field info. */ 12326 ecn_info = (ipha->ipha_type_of_service & 0x3); 12327 if (offset != 0) { 12328 /* 12329 * If this isn't the first piece, strip the header, and 12330 * add the offset to the end value. 12331 */ 12332 mp->b_rptr += hdr_length; 12333 end += offset; 12334 } 12335 12336 msg_len = MBLKSIZE(mp); 12337 tail_mp = mp; 12338 while (tail_mp->b_cont != NULL) { 12339 tail_mp = tail_mp->b_cont; 12340 msg_len += MBLKSIZE(tail_mp); 12341 } 12342 12343 /* If the reassembly list for this ILL will get too big, prune it */ 12344 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 12345 ip_reass_queue_bytes) { 12346 ill_frag_prune(ill, 12347 (ip_reass_queue_bytes < msg_len) ? 0 : 12348 (ip_reass_queue_bytes - msg_len)); 12349 pruned = B_TRUE; 12350 } 12351 12352 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 12353 mutex_enter(&ipfb->ipfb_lock); 12354 12355 ipfp = &ipfb->ipfb_ipf; 12356 /* Try to find an existing fragment queue for this packet. */ 12357 for (;;) { 12358 ipf = ipfp[0]; 12359 if (ipf != NULL) { 12360 /* 12361 * It has to match on ident and src/dst address. 12362 */ 12363 if (ipf->ipf_ident == ident && 12364 ipf->ipf_src == src && 12365 ipf->ipf_dst == dst && 12366 ipf->ipf_protocol == proto) { 12367 /* 12368 * If we have received too many 12369 * duplicate fragments for this packet 12370 * free it. 12371 */ 12372 if (ipf->ipf_num_dups > ip_max_frag_dups) { 12373 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12374 freemsg(mp); 12375 mutex_exit(&ipfb->ipfb_lock); 12376 return (B_FALSE); 12377 } 12378 /* Found it. */ 12379 break; 12380 } 12381 ipfp = &ipf->ipf_hash_next; 12382 continue; 12383 } 12384 12385 /* 12386 * If we pruned the list, do we want to store this new 12387 * fragment?. We apply an optimization here based on the 12388 * fact that most fragments will be received in order. 12389 * So if the offset of this incoming fragment is zero, 12390 * it is the first fragment of a new packet. We will 12391 * keep it. Otherwise drop the fragment, as we have 12392 * probably pruned the packet already (since the 12393 * packet cannot be found). 12394 */ 12395 if (pruned && offset != 0) { 12396 mutex_exit(&ipfb->ipfb_lock); 12397 freemsg(mp); 12398 return (B_FALSE); 12399 } 12400 12401 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS) { 12402 /* 12403 * Too many fragmented packets in this hash 12404 * bucket. Free the oldest. 12405 */ 12406 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 12407 } 12408 12409 /* New guy. Allocate a frag message. */ 12410 mp1 = allocb(sizeof (*ipf), BPRI_MED); 12411 if (mp1 == NULL) { 12412 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12413 freemsg(mp); 12414 reass_done: 12415 mutex_exit(&ipfb->ipfb_lock); 12416 return (B_FALSE); 12417 } 12418 12419 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); 12420 mp1->b_cont = mp; 12421 12422 /* Initialize the fragment header. */ 12423 ipf = (ipf_t *)mp1->b_rptr; 12424 ipf->ipf_mp = mp1; 12425 ipf->ipf_ptphn = ipfp; 12426 ipfp[0] = ipf; 12427 ipf->ipf_hash_next = NULL; 12428 ipf->ipf_ident = ident; 12429 ipf->ipf_protocol = proto; 12430 ipf->ipf_src = src; 12431 ipf->ipf_dst = dst; 12432 ipf->ipf_nf_hdr_len = 0; 12433 /* Record reassembly start time. */ 12434 ipf->ipf_timestamp = gethrestime_sec(); 12435 /* Record ipf generation and account for frag header */ 12436 ipf->ipf_gen = ill->ill_ipf_gen++; 12437 ipf->ipf_count = MBLKSIZE(mp1); 12438 ipf->ipf_last_frag_seen = B_FALSE; 12439 ipf->ipf_ecn = ecn_info; 12440 ipf->ipf_num_dups = 0; 12441 ipfb->ipfb_frag_pkts++; 12442 ipf->ipf_checksum = 0; 12443 ipf->ipf_checksum_flags = 0; 12444 12445 /* Store checksum value in fragment header */ 12446 if (sum_flags != 0) { 12447 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12448 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12449 ipf->ipf_checksum = sum_val; 12450 ipf->ipf_checksum_flags = sum_flags; 12451 } 12452 12453 /* 12454 * We handle reassembly two ways. In the easy case, 12455 * where all the fragments show up in order, we do 12456 * minimal bookkeeping, and just clip new pieces on 12457 * the end. If we ever see a hole, then we go off 12458 * to ip_reassemble which has to mark the pieces and 12459 * keep track of the number of holes, etc. Obviously, 12460 * the point of having both mechanisms is so we can 12461 * handle the easy case as efficiently as possible. 12462 */ 12463 if (offset == 0) { 12464 /* Easy case, in-order reassembly so far. */ 12465 ipf->ipf_count += msg_len; 12466 ipf->ipf_tail_mp = tail_mp; 12467 /* 12468 * Keep track of next expected offset in 12469 * ipf_end. 12470 */ 12471 ipf->ipf_end = end; 12472 ipf->ipf_nf_hdr_len = hdr_length; 12473 } else { 12474 /* Hard case, hole at the beginning. */ 12475 ipf->ipf_tail_mp = NULL; 12476 /* 12477 * ipf_end == 0 means that we have given up 12478 * on easy reassembly. 12479 */ 12480 ipf->ipf_end = 0; 12481 12482 /* Forget checksum offload from now on */ 12483 ipf->ipf_checksum_flags = 0; 12484 12485 /* 12486 * ipf_hole_cnt is set by ip_reassemble. 12487 * ipf_count is updated by ip_reassemble. 12488 * No need to check for return value here 12489 * as we don't expect reassembly to complete 12490 * or fail for the first fragment itself. 12491 */ 12492 (void) ip_reassemble(mp, ipf, 12493 (frag_offset_flags & IPH_OFFSET) << 3, 12494 (frag_offset_flags & IPH_MF), ill, msg_len); 12495 } 12496 /* Update per ipfb and ill byte counts */ 12497 ipfb->ipfb_count += ipf->ipf_count; 12498 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12499 ill->ill_frag_count += ipf->ipf_count; 12500 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12501 /* If the frag timer wasn't already going, start it. */ 12502 mutex_enter(&ill->ill_lock); 12503 ill_frag_timer_start(ill); 12504 mutex_exit(&ill->ill_lock); 12505 goto reass_done; 12506 } 12507 12508 /* 12509 * If the packet's flag has changed (it could be coming up 12510 * from an interface different than the previous, therefore 12511 * possibly different checksum capability), then forget about 12512 * any stored checksum states. Otherwise add the value to 12513 * the existing one stored in the fragment header. 12514 */ 12515 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 12516 sum_val += ipf->ipf_checksum; 12517 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12518 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 12519 ipf->ipf_checksum = sum_val; 12520 } else if (ipf->ipf_checksum_flags != 0) { 12521 /* Forget checksum offload from now on */ 12522 ipf->ipf_checksum_flags = 0; 12523 } 12524 12525 /* 12526 * We have a new piece of a datagram which is already being 12527 * reassembled. Update the ECN info if all IP fragments 12528 * are ECN capable. If there is one which is not, clear 12529 * all the info. If there is at least one which has CE 12530 * code point, IP needs to report that up to transport. 12531 */ 12532 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 12533 if (ecn_info == IPH_ECN_CE) 12534 ipf->ipf_ecn = IPH_ECN_CE; 12535 } else { 12536 ipf->ipf_ecn = IPH_ECN_NECT; 12537 } 12538 if (offset && ipf->ipf_end == offset) { 12539 /* The new fragment fits at the end */ 12540 ipf->ipf_tail_mp->b_cont = mp; 12541 /* Update the byte count */ 12542 ipf->ipf_count += msg_len; 12543 /* Update per ipfb and ill byte counts */ 12544 ipfb->ipfb_count += msg_len; 12545 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12546 ill->ill_frag_count += msg_len; 12547 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 12548 if (frag_offset_flags & IPH_MF) { 12549 /* More to come. */ 12550 ipf->ipf_end = end; 12551 ipf->ipf_tail_mp = tail_mp; 12552 goto reass_done; 12553 } 12554 } else { 12555 /* Go do the hard cases. */ 12556 int ret; 12557 12558 if (offset == 0) 12559 ipf->ipf_nf_hdr_len = hdr_length; 12560 12561 /* Save current byte count */ 12562 count = ipf->ipf_count; 12563 ret = ip_reassemble(mp, ipf, 12564 (frag_offset_flags & IPH_OFFSET) << 3, 12565 (frag_offset_flags & IPH_MF), ill, msg_len); 12566 /* Count of bytes added and subtracted (freeb()ed) */ 12567 count = ipf->ipf_count - count; 12568 if (count) { 12569 /* Update per ipfb and ill byte counts */ 12570 ipfb->ipfb_count += count; 12571 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 12572 ill->ill_frag_count += count; 12573 ASSERT(ill->ill_frag_count > 0); 12574 } 12575 if (ret == IP_REASS_PARTIAL) { 12576 goto reass_done; 12577 } else if (ret == IP_REASS_FAILED) { 12578 /* Reassembly failed. Free up all resources */ 12579 ill_frag_free_pkts(ill, ipfb, ipf, 1); 12580 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 12581 IP_REASS_SET_START(t_mp, 0); 12582 IP_REASS_SET_END(t_mp, 0); 12583 } 12584 freemsg(mp); 12585 goto reass_done; 12586 } 12587 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 12588 } 12589 /* 12590 * We have completed reassembly. Unhook the frag header from 12591 * the reassembly list. 12592 * 12593 * Before we free the frag header, record the ECN info 12594 * to report back to the transport. 12595 */ 12596 ecn_info = ipf->ipf_ecn; 12597 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); 12598 ipfp = ipf->ipf_ptphn; 12599 12600 /* We need to supply these to caller */ 12601 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 12602 sum_val = ipf->ipf_checksum; 12603 else 12604 sum_val = 0; 12605 12606 mp1 = ipf->ipf_mp; 12607 count = ipf->ipf_count; 12608 ipf = ipf->ipf_hash_next; 12609 if (ipf != NULL) 12610 ipf->ipf_ptphn = ipfp; 12611 ipfp[0] = ipf; 12612 ill->ill_frag_count -= count; 12613 ASSERT(ipfb->ipfb_count >= count); 12614 ipfb->ipfb_count -= count; 12615 ipfb->ipfb_frag_pkts--; 12616 mutex_exit(&ipfb->ipfb_lock); 12617 /* Ditch the frag header. */ 12618 mp = mp1->b_cont; 12619 12620 freeb(mp1); 12621 12622 /* Restore original IP length in header. */ 12623 packet_size = (uint32_t)msgdsize(mp); 12624 if (packet_size > IP_MAXPACKET) { 12625 freemsg(mp); 12626 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 12627 return (B_FALSE); 12628 } 12629 12630 if (DB_REF(mp) > 1) { 12631 mblk_t *mp2 = copymsg(mp); 12632 12633 freemsg(mp); 12634 if (mp2 == NULL) { 12635 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12636 return (B_FALSE); 12637 } 12638 mp = mp2; 12639 } 12640 ipha = (ipha_t *)mp->b_rptr; 12641 12642 ipha->ipha_length = htons((uint16_t)packet_size); 12643 /* We're now complete, zip the frag state */ 12644 ipha->ipha_fragment_offset_and_flags = 0; 12645 /* Record the ECN info. */ 12646 ipha->ipha_type_of_service &= 0xFC; 12647 ipha->ipha_type_of_service |= ecn_info; 12648 *mpp = mp; 12649 12650 /* Reassembly is successful; return checksum information if needed */ 12651 if (cksum_val != NULL) 12652 *cksum_val = sum_val; 12653 if (cksum_flags != NULL) 12654 *cksum_flags = sum_flags; 12655 12656 return (B_TRUE); 12657 } 12658 12659 /* 12660 * Perform ip header check sum update local options. 12661 * return B_TRUE if all is well, else return B_FALSE and release 12662 * the mp. caller is responsible for decrementing ire ref cnt. 12663 */ 12664 static boolean_t 12665 ip_options_cksum(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t *ipha, ire_t *ire) 12666 { 12667 mblk_t *first_mp; 12668 boolean_t mctl_present; 12669 uint16_t sum; 12670 12671 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12672 /* 12673 * Don't do the checksum if it has gone through AH/ESP 12674 * processing. 12675 */ 12676 if (!mctl_present) { 12677 sum = ip_csum_hdr(ipha); 12678 if (sum != 0) { 12679 if (ill != NULL) { 12680 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12681 } else { 12682 BUMP_MIB(&ip_mib, ipIfStatsInCksumErrs); 12683 } 12684 freemsg(first_mp); 12685 return (B_FALSE); 12686 } 12687 } 12688 12689 if (!ip_rput_local_options(q, mp, ipha, ire)) { 12690 if (mctl_present) 12691 freeb(first_mp); 12692 return (B_FALSE); 12693 } 12694 12695 return (B_TRUE); 12696 } 12697 12698 /* 12699 * All udp packet are delivered to the local host via this routine. 12700 */ 12701 void 12702 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 12703 ill_t *recv_ill) 12704 { 12705 uint32_t sum; 12706 uint32_t u1; 12707 boolean_t mctl_present; 12708 conn_t *connp; 12709 mblk_t *first_mp; 12710 uint16_t *up; 12711 ill_t *ill = (ill_t *)q->q_ptr; 12712 uint16_t reass_hck_flags = 0; 12713 12714 #define rptr ((uchar_t *)ipha) 12715 12716 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 12717 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 12718 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 12719 ASSERT(ill != NULL); 12720 12721 /* 12722 * FAST PATH for udp packets 12723 */ 12724 12725 /* u1 is # words of IP options */ 12726 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 12727 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12728 12729 /* IP options present */ 12730 if (u1 != 0) 12731 goto ipoptions; 12732 12733 /* Check the IP header checksum. */ 12734 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12735 /* Clear the IP header h/w cksum flag */ 12736 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12737 } else { 12738 #define uph ((uint16_t *)ipha) 12739 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 12740 uph[6] + uph[7] + uph[8] + uph[9]; 12741 #undef uph 12742 /* finish doing IP checksum */ 12743 sum = (sum & 0xFFFF) + (sum >> 16); 12744 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12745 /* 12746 * Don't verify header checksum if this packet is coming 12747 * back from AH/ESP as we already did it. 12748 */ 12749 if (!mctl_present && sum != 0 && sum != 0xFFFF) { 12750 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 12751 freemsg(first_mp); 12752 return; 12753 } 12754 } 12755 12756 /* 12757 * Count for SNMP of inbound packets for ire. 12758 * if mctl is present this might be a secure packet and 12759 * has already been counted for in ip_proto_input(). 12760 */ 12761 if (!mctl_present) { 12762 UPDATE_IB_PKT_COUNT(ire); 12763 ire->ire_last_used_time = lbolt; 12764 } 12765 12766 /* packet part of fragmented IP packet? */ 12767 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12768 if (u1 & (IPH_MF | IPH_OFFSET)) { 12769 goto fragmented; 12770 } 12771 12772 /* u1 = IP header length (20 bytes) */ 12773 u1 = IP_SIMPLE_HDR_LENGTH; 12774 12775 /* packet does not contain complete IP & UDP headers */ 12776 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 12777 goto udppullup; 12778 12779 /* up points to UDP header */ 12780 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 12781 #define iphs ((uint16_t *)ipha) 12782 12783 /* if udp hdr cksum != 0, then need to checksum udp packet */ 12784 if (up[3] != 0) { 12785 mblk_t *mp1 = mp->b_cont; 12786 boolean_t cksum_err; 12787 uint16_t hck_flags = 0; 12788 12789 /* Pseudo-header checksum */ 12790 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12791 iphs[9] + up[2]; 12792 12793 /* 12794 * Revert to software checksum calculation if the interface 12795 * isn't capable of checksum offload or if IPsec is present. 12796 */ 12797 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 12798 hck_flags = DB_CKSUMFLAGS(mp); 12799 12800 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12801 IP_STAT(ip_in_sw_cksum); 12802 12803 IP_CKSUM_RECV(hck_flags, u1, 12804 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 12805 (int32_t)((uchar_t *)up - rptr), 12806 mp, mp1, cksum_err); 12807 12808 if (cksum_err) { 12809 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12810 if (hck_flags & HCK_FULLCKSUM) 12811 IP_STAT(ip_udp_in_full_hw_cksum_err); 12812 else if (hck_flags & HCK_PARTIALCKSUM) 12813 IP_STAT(ip_udp_in_part_hw_cksum_err); 12814 else 12815 IP_STAT(ip_udp_in_sw_cksum_err); 12816 12817 freemsg(first_mp); 12818 return; 12819 } 12820 } 12821 12822 /* Non-fragmented broadcast or multicast packet? */ 12823 if (ire->ire_type == IRE_BROADCAST) 12824 goto udpslowpath; 12825 12826 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 12827 ire->ire_zoneid)) != NULL) { 12828 ASSERT(connp->conn_upq != NULL); 12829 IP_STAT(ip_udp_fast_path); 12830 12831 if (CONN_UDP_FLOWCTLD(connp)) { 12832 freemsg(mp); 12833 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 12834 } else { 12835 if (!mctl_present) { 12836 BUMP_MIB(ill->ill_ip_mib, 12837 ipIfStatsHCInDelivers); 12838 } 12839 /* 12840 * mp and first_mp can change. 12841 */ 12842 if (ip_udp_check(q, connp, recv_ill, 12843 ipha, &mp, &first_mp, mctl_present)) { 12844 /* Send it upstream */ 12845 CONN_UDP_RECV(connp, mp); 12846 } 12847 } 12848 /* 12849 * freeb() cannot deal with null mblk being passed 12850 * in and first_mp can be set to null in the call 12851 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 12852 */ 12853 if (mctl_present && first_mp != NULL) { 12854 freeb(first_mp); 12855 } 12856 CONN_DEC_REF(connp); 12857 return; 12858 } 12859 12860 /* 12861 * if we got here we know the packet is not fragmented and 12862 * has no options. The classifier could not find a conn_t and 12863 * most likely its an icmp packet so send it through slow path. 12864 */ 12865 12866 goto udpslowpath; 12867 12868 ipoptions: 12869 if (!ip_options_cksum(q, ill, mp, ipha, ire)) { 12870 goto slow_done; 12871 } 12872 12873 UPDATE_IB_PKT_COUNT(ire); 12874 ire->ire_last_used_time = lbolt; 12875 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12876 if (u1 & (IPH_MF | IPH_OFFSET)) { 12877 fragmented: 12878 /* 12879 * "sum" and "reass_hck_flags" are non-zero if the 12880 * reassembled packet has a valid hardware computed 12881 * checksum information associated with it. 12882 */ 12883 if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) 12884 goto slow_done; 12885 /* 12886 * Make sure that first_mp points back to mp as 12887 * the mp we came in with could have changed in 12888 * ip_rput_fragment(). 12889 */ 12890 ASSERT(!mctl_present); 12891 ipha = (ipha_t *)mp->b_rptr; 12892 first_mp = mp; 12893 } 12894 12895 /* Now we have a complete datagram, destined for this machine. */ 12896 u1 = IPH_HDR_LENGTH(ipha); 12897 /* Pull up the UDP header, if necessary. */ 12898 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 12899 udppullup: 12900 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 12901 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 12902 freemsg(first_mp); 12903 goto slow_done; 12904 } 12905 ipha = (ipha_t *)mp->b_rptr; 12906 } 12907 12908 /* 12909 * Validate the checksum for the reassembled packet; for the 12910 * pullup case we calculate the payload checksum in software. 12911 */ 12912 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 12913 if (up[3] != 0) { 12914 boolean_t cksum_err; 12915 12916 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 12917 IP_STAT(ip_in_sw_cksum); 12918 12919 IP_CKSUM_RECV_REASS(reass_hck_flags, 12920 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 12921 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 12922 iphs[9] + up[2], sum, cksum_err); 12923 12924 if (cksum_err) { 12925 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); 12926 12927 if (reass_hck_flags & HCK_FULLCKSUM) 12928 IP_STAT(ip_udp_in_full_hw_cksum_err); 12929 else if (reass_hck_flags & HCK_PARTIALCKSUM) 12930 IP_STAT(ip_udp_in_part_hw_cksum_err); 12931 else 12932 IP_STAT(ip_udp_in_sw_cksum_err); 12933 12934 freemsg(first_mp); 12935 goto slow_done; 12936 } 12937 } 12938 udpslowpath: 12939 12940 /* Clear hardware checksum flag to be safe */ 12941 DB_CKSUMFLAGS(mp) = 0; 12942 12943 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 12944 (ire->ire_type == IRE_BROADCAST), 12945 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IPINFO, 12946 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 12947 12948 slow_done: 12949 IP_STAT(ip_udp_slow_path); 12950 return; 12951 12952 #undef iphs 12953 #undef rptr 12954 } 12955 12956 /* ARGSUSED */ 12957 static mblk_t * 12958 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 12959 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 12960 ill_rx_ring_t *ill_ring) 12961 { 12962 conn_t *connp; 12963 uint32_t sum; 12964 uint32_t u1; 12965 uint16_t *up; 12966 int offset; 12967 ssize_t len; 12968 mblk_t *mp1; 12969 boolean_t syn_present = B_FALSE; 12970 tcph_t *tcph; 12971 uint_t ip_hdr_len; 12972 ill_t *ill = (ill_t *)q->q_ptr; 12973 zoneid_t zoneid = ire->ire_zoneid; 12974 boolean_t cksum_err; 12975 uint16_t hck_flags = 0; 12976 12977 #define rptr ((uchar_t *)ipha) 12978 12979 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 12980 ASSERT(ill != NULL); 12981 12982 /* 12983 * FAST PATH for tcp packets 12984 */ 12985 12986 /* u1 is # words of IP options */ 12987 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 12988 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12989 12990 /* IP options present */ 12991 if (u1) { 12992 goto ipoptions; 12993 } else { 12994 /* Check the IP header checksum. */ 12995 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12996 /* Clear the IP header h/w cksum flag */ 12997 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 12998 } else { 12999 #define uph ((uint16_t *)ipha) 13000 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13001 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13002 #undef uph 13003 /* finish doing IP checksum */ 13004 sum = (sum & 0xFFFF) + (sum >> 16); 13005 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13006 /* 13007 * Don't verify header checksum if this packet 13008 * is coming back from AH/ESP as we already did it. 13009 */ 13010 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 13011 BUMP_MIB(ill->ill_ip_mib, 13012 ipIfStatsInCksumErrs); 13013 goto error; 13014 } 13015 } 13016 } 13017 13018 if (!mctl_present) { 13019 UPDATE_IB_PKT_COUNT(ire); 13020 ire->ire_last_used_time = lbolt; 13021 } 13022 13023 /* packet part of fragmented IP packet? */ 13024 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13025 if (u1 & (IPH_MF | IPH_OFFSET)) { 13026 goto fragmented; 13027 } 13028 13029 /* u1 = IP header length (20 bytes) */ 13030 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 13031 13032 /* does packet contain IP+TCP headers? */ 13033 len = mp->b_wptr - rptr; 13034 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 13035 IP_STAT(ip_tcppullup); 13036 goto tcppullup; 13037 } 13038 13039 /* TCP options present? */ 13040 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 13041 13042 /* 13043 * If options need to be pulled up, then goto tcpoptions. 13044 * otherwise we are still in the fast path 13045 */ 13046 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 13047 IP_STAT(ip_tcpoptions); 13048 goto tcpoptions; 13049 } 13050 13051 /* multiple mblks of tcp data? */ 13052 if ((mp1 = mp->b_cont) != NULL) { 13053 /* more then two? */ 13054 if (mp1->b_cont != NULL) { 13055 IP_STAT(ip_multipkttcp); 13056 goto multipkttcp; 13057 } 13058 len += mp1->b_wptr - mp1->b_rptr; 13059 } 13060 13061 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 13062 13063 /* part of pseudo checksum */ 13064 13065 /* TCP datagram length */ 13066 u1 = len - IP_SIMPLE_HDR_LENGTH; 13067 13068 #define iphs ((uint16_t *)ipha) 13069 13070 #ifdef _BIG_ENDIAN 13071 u1 += IPPROTO_TCP; 13072 #else 13073 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13074 #endif 13075 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13076 13077 /* 13078 * Revert to software checksum calculation if the interface 13079 * isn't capable of checksum offload or if IPsec is present. 13080 */ 13081 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 13082 hck_flags = DB_CKSUMFLAGS(mp); 13083 13084 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 13085 IP_STAT(ip_in_sw_cksum); 13086 13087 IP_CKSUM_RECV(hck_flags, u1, 13088 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 13089 (int32_t)((uchar_t *)up - rptr), 13090 mp, mp1, cksum_err); 13091 13092 if (cksum_err) { 13093 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13094 13095 if (hck_flags & HCK_FULLCKSUM) 13096 IP_STAT(ip_tcp_in_full_hw_cksum_err); 13097 else if (hck_flags & HCK_PARTIALCKSUM) 13098 IP_STAT(ip_tcp_in_part_hw_cksum_err); 13099 else 13100 IP_STAT(ip_tcp_in_sw_cksum_err); 13101 13102 goto error; 13103 } 13104 13105 try_again: 13106 13107 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid)) == 13108 NULL) { 13109 /* Send the TH_RST */ 13110 goto no_conn; 13111 } 13112 13113 /* 13114 * TCP FAST PATH for AF_INET socket. 13115 * 13116 * TCP fast path to avoid extra work. An AF_INET socket type 13117 * does not have facility to receive extra information via 13118 * ip_process or ip_add_info. Also, when the connection was 13119 * established, we made a check if this connection is impacted 13120 * by any global IPSec policy or per connection policy (a 13121 * policy that comes in effect later will not apply to this 13122 * connection). Since all this can be determined at the 13123 * connection establishment time, a quick check of flags 13124 * can avoid extra work. 13125 */ 13126 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 13127 !IPP_ENABLED(IPP_LOCAL_IN)) { 13128 ASSERT(first_mp == mp); 13129 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13130 SET_SQUEUE(mp, tcp_rput_data, connp); 13131 return (mp); 13132 } 13133 13134 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 13135 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 13136 if (IPCL_IS_TCP(connp)) { 13137 mp->b_datap->db_struioflag |= STRUIO_EAGER; 13138 DB_CKSUMSTART(mp) = 13139 (intptr_t)ip_squeue_get(ill_ring); 13140 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 13141 !CONN_INBOUND_POLICY_PRESENT(connp)) { 13142 BUMP_MIB(ill->ill_ip_mib, 13143 ipIfStatsHCInDelivers); 13144 SET_SQUEUE(mp, connp->conn_recv, connp); 13145 return (mp); 13146 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 13147 !CONN_INBOUND_POLICY_PRESENT(connp)) { 13148 BUMP_MIB(ill->ill_ip_mib, 13149 ipIfStatsHCInDelivers); 13150 ip_squeue_enter_unbound++; 13151 SET_SQUEUE(mp, tcp_conn_request_unbound, 13152 connp); 13153 return (mp); 13154 } 13155 syn_present = B_TRUE; 13156 } 13157 13158 } 13159 13160 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 13161 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13162 13163 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13164 /* No need to send this packet to TCP */ 13165 if ((flags & TH_RST) || (flags & TH_URG)) { 13166 CONN_DEC_REF(connp); 13167 freemsg(first_mp); 13168 return (NULL); 13169 } 13170 if (flags & TH_ACK) { 13171 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid); 13172 CONN_DEC_REF(connp); 13173 return (NULL); 13174 } 13175 13176 CONN_DEC_REF(connp); 13177 freemsg(first_mp); 13178 return (NULL); 13179 } 13180 13181 if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) { 13182 first_mp = ipsec_check_inbound_policy(first_mp, connp, 13183 ipha, NULL, mctl_present); 13184 if (first_mp == NULL) { 13185 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13186 CONN_DEC_REF(connp); 13187 return (NULL); 13188 } 13189 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 13190 ASSERT(syn_present); 13191 if (mctl_present) { 13192 ASSERT(first_mp != mp); 13193 first_mp->b_datap->db_struioflag |= 13194 STRUIO_POLICY; 13195 } else { 13196 ASSERT(first_mp == mp); 13197 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 13198 mp->b_datap->db_struioflag |= STRUIO_POLICY; 13199 } 13200 } else { 13201 /* 13202 * Discard first_mp early since we're dealing with a 13203 * fully-connected conn_t and tcp doesn't do policy in 13204 * this case. 13205 */ 13206 if (mctl_present) { 13207 freeb(first_mp); 13208 mctl_present = B_FALSE; 13209 } 13210 first_mp = mp; 13211 } 13212 } 13213 13214 /* Initiate IPPF processing for fastpath */ 13215 if (IPP_ENABLED(IPP_LOCAL_IN)) { 13216 uint32_t ill_index; 13217 13218 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13219 ip_process(IPP_LOCAL_IN, &mp, ill_index); 13220 if (mp == NULL) { 13221 ip2dbg(("ip_input_ipsec_process: TCP pkt " 13222 "deferred/dropped during IPPF processing\n")); 13223 CONN_DEC_REF(connp); 13224 if (mctl_present) 13225 freeb(first_mp); 13226 return (NULL); 13227 } else if (mctl_present) { 13228 /* 13229 * ip_process might return a new mp. 13230 */ 13231 ASSERT(first_mp != mp); 13232 first_mp->b_cont = mp; 13233 } else { 13234 first_mp = mp; 13235 } 13236 13237 } 13238 13239 if (!syn_present && connp->conn_ip_recvpktinfo) { 13240 /* 13241 * TCP does not support IP_RECVPKTINFO for v4 so lets 13242 * make sure IPF_RECVIF is passed to ip_add_info. 13243 */ 13244 mp = ip_add_info(mp, recv_ill, flags|IPF_RECVIF, 13245 IPCL_ZONEID(connp)); 13246 if (mp == NULL) { 13247 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13248 CONN_DEC_REF(connp); 13249 if (mctl_present) 13250 freeb(first_mp); 13251 return (NULL); 13252 } else if (mctl_present) { 13253 /* 13254 * ip_add_info might return a new mp. 13255 */ 13256 ASSERT(first_mp != mp); 13257 first_mp->b_cont = mp; 13258 } else { 13259 first_mp = mp; 13260 } 13261 } 13262 13263 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13264 if (IPCL_IS_TCP(connp)) { 13265 SET_SQUEUE(first_mp, connp->conn_recv, connp); 13266 return (first_mp); 13267 } else { 13268 putnext(connp->conn_rq, first_mp); 13269 CONN_DEC_REF(connp); 13270 return (NULL); 13271 } 13272 13273 no_conn: 13274 /* Initiate IPPf processing, if needed. */ 13275 if (IPP_ENABLED(IPP_LOCAL_IN)) { 13276 uint32_t ill_index; 13277 ill_index = recv_ill->ill_phyint->phyint_ifindex; 13278 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 13279 if (first_mp == NULL) { 13280 return (NULL); 13281 } 13282 } 13283 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13284 13285 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid); 13286 return (NULL); 13287 ipoptions: 13288 if (!ip_options_cksum(q, ill, first_mp, ipha, ire)) { 13289 goto slow_done; 13290 } 13291 13292 UPDATE_IB_PKT_COUNT(ire); 13293 ire->ire_last_used_time = lbolt; 13294 13295 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13296 if (u1 & (IPH_MF | IPH_OFFSET)) { 13297 fragmented: 13298 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 13299 if (mctl_present) 13300 freeb(first_mp); 13301 goto slow_done; 13302 } 13303 /* 13304 * Make sure that first_mp points back to mp as 13305 * the mp we came in with could have changed in 13306 * ip_rput_fragment(). 13307 */ 13308 ASSERT(!mctl_present); 13309 ipha = (ipha_t *)mp->b_rptr; 13310 first_mp = mp; 13311 } 13312 13313 /* Now we have a complete datagram, destined for this machine. */ 13314 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 13315 13316 len = mp->b_wptr - mp->b_rptr; 13317 /* Pull up a minimal TCP header, if necessary. */ 13318 if (len < (u1 + 20)) { 13319 tcppullup: 13320 if (!pullupmsg(mp, u1 + 20)) { 13321 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13322 goto error; 13323 } 13324 ipha = (ipha_t *)mp->b_rptr; 13325 len = mp->b_wptr - mp->b_rptr; 13326 } 13327 13328 /* 13329 * Extract the offset field from the TCP header. As usual, we 13330 * try to help the compiler more than the reader. 13331 */ 13332 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 13333 if (offset != 5) { 13334 tcpoptions: 13335 if (offset < 5) { 13336 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13337 goto error; 13338 } 13339 /* 13340 * There must be TCP options. 13341 * Make sure we can grab them. 13342 */ 13343 offset <<= 2; 13344 offset += u1; 13345 if (len < offset) { 13346 if (!pullupmsg(mp, offset)) { 13347 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13348 goto error; 13349 } 13350 ipha = (ipha_t *)mp->b_rptr; 13351 len = mp->b_wptr - rptr; 13352 } 13353 } 13354 13355 /* Get the total packet length in len, including headers. */ 13356 if (mp->b_cont) { 13357 multipkttcp: 13358 len = msgdsize(mp); 13359 } 13360 13361 /* 13362 * Check the TCP checksum by pulling together the pseudo- 13363 * header checksum, and passing it to ip_csum to be added in 13364 * with the TCP datagram. 13365 * 13366 * Since we are not using the hwcksum if available we must 13367 * clear the flag. We may come here via tcppullup or tcpoptions. 13368 * If either of these fails along the way the mblk is freed. 13369 * If this logic ever changes and mblk is reused to say send 13370 * ICMP's back, then this flag may need to be cleared in 13371 * other places as well. 13372 */ 13373 DB_CKSUMFLAGS(mp) = 0; 13374 13375 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 13376 13377 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 13378 #ifdef _BIG_ENDIAN 13379 u1 += IPPROTO_TCP; 13380 #else 13381 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 13382 #endif 13383 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 13384 /* 13385 * Not M_DATA mblk or its a dup, so do the checksum now. 13386 */ 13387 IP_STAT(ip_in_sw_cksum); 13388 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 13389 BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); 13390 goto error; 13391 } 13392 13393 IP_STAT(ip_tcp_slow_path); 13394 goto try_again; 13395 #undef iphs 13396 #undef rptr 13397 13398 error: 13399 freemsg(first_mp); 13400 slow_done: 13401 return (NULL); 13402 } 13403 13404 /* ARGSUSED */ 13405 static void 13406 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 13407 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 13408 { 13409 conn_t *connp; 13410 uint32_t sum; 13411 uint32_t u1; 13412 ssize_t len; 13413 sctp_hdr_t *sctph; 13414 zoneid_t zoneid = ire->ire_zoneid; 13415 uint32_t pktsum; 13416 uint32_t calcsum; 13417 uint32_t ports; 13418 uint_t ipif_seqid; 13419 in6_addr_t map_src, map_dst; 13420 ill_t *ill = (ill_t *)q->q_ptr; 13421 13422 #define rptr ((uchar_t *)ipha) 13423 13424 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 13425 ASSERT(ill != NULL); 13426 13427 /* u1 is # words of IP options */ 13428 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 13429 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 13430 13431 /* IP options present */ 13432 if (u1 > 0) { 13433 goto ipoptions; 13434 } else { 13435 /* Check the IP header checksum. */ 13436 if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 13437 #define uph ((uint16_t *)ipha) 13438 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 13439 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 13440 #undef uph 13441 /* finish doing IP checksum */ 13442 sum = (sum & 0xFFFF) + (sum >> 16); 13443 sum = ~(sum + (sum >> 16)) & 0xFFFF; 13444 /* 13445 * Don't verify header checksum if this packet 13446 * is coming back from AH/ESP as we already did it. 13447 */ 13448 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 13449 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 13450 goto error; 13451 } 13452 } 13453 /* 13454 * Since there is no SCTP h/w cksum support yet, just 13455 * clear the flag. 13456 */ 13457 DB_CKSUMFLAGS(mp) = 0; 13458 } 13459 13460 /* 13461 * Don't verify header checksum if this packet is coming 13462 * back from AH/ESP as we already did it. 13463 */ 13464 if (!mctl_present) { 13465 UPDATE_IB_PKT_COUNT(ire); 13466 ire->ire_last_used_time = lbolt; 13467 } 13468 13469 /* packet part of fragmented IP packet? */ 13470 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13471 if (u1 & (IPH_MF | IPH_OFFSET)) 13472 goto fragmented; 13473 13474 /* u1 = IP header length (20 bytes) */ 13475 u1 = IP_SIMPLE_HDR_LENGTH; 13476 13477 find_sctp_client: 13478 /* Pullup if we don't have the sctp common header. */ 13479 len = MBLKL(mp); 13480 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 13481 if (mp->b_cont == NULL || 13482 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 13483 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13484 goto error; 13485 } 13486 ipha = (ipha_t *)mp->b_rptr; 13487 len = MBLKL(mp); 13488 } 13489 13490 sctph = (sctp_hdr_t *)(rptr + u1); 13491 #ifdef DEBUG 13492 if (!skip_sctp_cksum) { 13493 #endif 13494 pktsum = sctph->sh_chksum; 13495 sctph->sh_chksum = 0; 13496 calcsum = sctp_cksum(mp, u1); 13497 if (calcsum != pktsum) { 13498 BUMP_MIB(&sctp_mib, sctpChecksumError); 13499 goto error; 13500 } 13501 sctph->sh_chksum = pktsum; 13502 #ifdef DEBUG /* skip_sctp_cksum */ 13503 } 13504 #endif 13505 /* get the ports */ 13506 ports = *(uint32_t *)&sctph->sh_sport; 13507 13508 ipif_seqid = ire->ire_ipif->ipif_seqid; 13509 IRE_REFRELE(ire); 13510 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 13511 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 13512 if ((connp = sctp_fanout(&map_src, &map_dst, ports, ipif_seqid, zoneid, 13513 mp)) == NULL) { 13514 /* Check for raw socket or OOTB handling */ 13515 goto no_conn; 13516 } 13517 13518 /* Found a client; up it goes */ 13519 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 13520 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 13521 return; 13522 13523 no_conn: 13524 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 13525 ports, mctl_present, flags, B_TRUE, ipif_seqid, zoneid); 13526 return; 13527 13528 ipoptions: 13529 DB_CKSUMFLAGS(mp) = 0; 13530 if (!ip_options_cksum(q, ill, first_mp, ipha, ire)) 13531 goto slow_done; 13532 13533 UPDATE_IB_PKT_COUNT(ire); 13534 ire->ire_last_used_time = lbolt; 13535 13536 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 13537 if (u1 & (IPH_MF | IPH_OFFSET)) { 13538 fragmented: 13539 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) 13540 goto slow_done; 13541 /* 13542 * Make sure that first_mp points back to mp as 13543 * the mp we came in with could have changed in 13544 * ip_rput_fragment(). 13545 */ 13546 ASSERT(!mctl_present); 13547 ipha = (ipha_t *)mp->b_rptr; 13548 first_mp = mp; 13549 } 13550 13551 /* Now we have a complete datagram, destined for this machine. */ 13552 u1 = IPH_HDR_LENGTH(ipha); 13553 goto find_sctp_client; 13554 #undef iphs 13555 #undef rptr 13556 13557 error: 13558 freemsg(first_mp); 13559 slow_done: 13560 IRE_REFRELE(ire); 13561 } 13562 13563 #define VER_BITS 0xF0 13564 #define VERSION_6 0x60 13565 13566 static boolean_t 13567 ip_rput_multimblk_ipoptions(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t **iphapp, 13568 ipaddr_t *dstp) 13569 { 13570 uint_t opt_len; 13571 ipha_t *ipha; 13572 ssize_t len; 13573 uint_t pkt_len; 13574 13575 ASSERT(ill != NULL); 13576 IP_STAT(ip_ipoptions); 13577 ipha = *iphapp; 13578 13579 #define rptr ((uchar_t *)ipha) 13580 /* Assume no IPv6 packets arrive over the IPv4 queue */ 13581 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 13582 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); 13583 freemsg(mp); 13584 return (B_FALSE); 13585 } 13586 13587 /* multiple mblk or too short */ 13588 pkt_len = ntohs(ipha->ipha_length); 13589 13590 /* Get the number of words of IP options in the IP header. */ 13591 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 13592 if (opt_len) { 13593 /* IP Options present! Validate and process. */ 13594 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 13595 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13596 goto done; 13597 } 13598 /* 13599 * Recompute complete header length and make sure we 13600 * have access to all of it. 13601 */ 13602 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 13603 if (len > (mp->b_wptr - rptr)) { 13604 if (len > pkt_len) { 13605 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13606 goto done; 13607 } 13608 if (!pullupmsg(mp, len)) { 13609 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13610 goto done; 13611 } 13612 ipha = (ipha_t *)mp->b_rptr; 13613 } 13614 /* 13615 * Go off to ip_rput_options which returns the next hop 13616 * destination address, which may have been affected 13617 * by source routing. 13618 */ 13619 IP_STAT(ip_opt); 13620 if (ip_rput_options(q, mp, ipha, dstp) == -1) { 13621 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13622 return (B_FALSE); 13623 } 13624 } 13625 *iphapp = ipha; 13626 return (B_TRUE); 13627 done: 13628 /* clear b_prev - used by ip_mroute_decap */ 13629 mp->b_prev = NULL; 13630 freemsg(mp); 13631 return (B_FALSE); 13632 #undef rptr 13633 } 13634 13635 /* 13636 * Deal with the fact that there is no ire for the destination. 13637 * The incoming ill (in_ill) is passed in to ip_newroute only 13638 * in the case of packets coming from mobile ip forward tunnel. 13639 * It must be null otherwise. 13640 */ 13641 static ire_t * 13642 ip_rput_noire(queue_t *q, ill_t *in_ill, mblk_t *mp, int ll_multicast, 13643 ipaddr_t dst) 13644 { 13645 ipha_t *ipha; 13646 ill_t *ill; 13647 ire_t *ire; 13648 boolean_t check_multirt = B_FALSE; 13649 13650 ipha = (ipha_t *)mp->b_rptr; 13651 ill = (ill_t *)q->q_ptr; 13652 13653 ASSERT(ill != NULL); 13654 /* 13655 * No IRE for this destination, so it can't be for us. 13656 * Unless we are forwarding, drop the packet. 13657 * We have to let source routed packets through 13658 * since we don't yet know if they are 'ping -l' 13659 * packets i.e. if they will go out over the 13660 * same interface as they came in on. 13661 */ 13662 if (ll_multicast) { 13663 freemsg(mp); 13664 return (NULL); 13665 } 13666 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha)) { 13667 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13668 freemsg(mp); 13669 return (NULL); 13670 } 13671 13672 /* 13673 * Mark this packet as having originated externally. 13674 * 13675 * For non-forwarding code path, ire_send later double 13676 * checks this interface to see if it is still exists 13677 * post-ARP resolution. 13678 * 13679 * Also, IPQOS uses this to differentiate between 13680 * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP 13681 * QOS packet processing in ip_wput_attach_llhdr(). 13682 * The QoS module can mark the b_band for a fastpath message 13683 * or the dl_priority field in a unitdata_req header for 13684 * CoS marking. This info can only be found in 13685 * ip_wput_attach_llhdr(). 13686 */ 13687 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 13688 /* 13689 * Clear the indication that this may have a hardware checksum 13690 * as we are not using it 13691 */ 13692 DB_CKSUMFLAGS(mp) = 0; 13693 13694 if (in_ill != NULL) { 13695 /* 13696 * Now hand the packet to ip_newroute. 13697 */ 13698 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID); 13699 return (NULL); 13700 } 13701 ire = ire_forward(dst, &check_multirt, NULL, NULL, 13702 MBLK_GETLABEL(mp)); 13703 13704 if (ire == NULL && check_multirt) { 13705 /* Let ip_newroute handle CGTP */ 13706 ip_newroute(q, mp, dst, in_ill, NULL, GLOBAL_ZONEID); 13707 return (NULL); 13708 } 13709 13710 if (ire != NULL) 13711 return (ire); 13712 13713 mp->b_prev = mp->b_next = 0; 13714 /* send icmp unreachable */ 13715 q = WR(q); 13716 /* Sent by forwarding path, and router is global zone */ 13717 if (ip_source_routed(ipha)) { 13718 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, 13719 GLOBAL_ZONEID); 13720 } else { 13721 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID); 13722 } 13723 13724 return (NULL); 13725 13726 } 13727 13728 /* 13729 * check ip header length and align it. 13730 */ 13731 static boolean_t 13732 ip_check_and_align_header(queue_t *q, mblk_t *mp) 13733 { 13734 ssize_t len; 13735 ill_t *ill; 13736 ipha_t *ipha; 13737 13738 len = MBLKL(mp); 13739 13740 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13741 ill = (ill_t *)q->q_ptr; 13742 13743 if (!OK_32PTR(mp->b_rptr)) 13744 IP_STAT(ip_notaligned1); 13745 else 13746 IP_STAT(ip_notaligned2); 13747 /* Guard against bogus device drivers */ 13748 if (len < 0) { 13749 /* clear b_prev - used by ip_mroute_decap */ 13750 mp->b_prev = NULL; 13751 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 13752 freemsg(mp); 13753 return (B_FALSE); 13754 } 13755 13756 if (ip_rput_pullups++ == 0) { 13757 ipha = (ipha_t *)mp->b_rptr; 13758 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 13759 "ip_check_and_align_header: %s forced us to " 13760 " pullup pkt, hdr len %ld, hdr addr %p", 13761 ill->ill_name, len, ipha); 13762 } 13763 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 13764 /* clear b_prev - used by ip_mroute_decap */ 13765 mp->b_prev = NULL; 13766 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13767 freemsg(mp); 13768 return (B_FALSE); 13769 } 13770 } 13771 return (B_TRUE); 13772 } 13773 13774 static boolean_t 13775 ip_rput_notforus(queue_t **qp, mblk_t *mp, ire_t *ire, ill_t *ill) 13776 { 13777 ill_group_t *ill_group; 13778 ill_group_t *ire_group; 13779 queue_t *q; 13780 ill_t *ire_ill; 13781 uint_t ill_ifindex; 13782 13783 q = *qp; 13784 /* 13785 * We need to check to make sure the packet came in 13786 * on the queue associated with the destination IRE. 13787 * Note that for multicast packets and broadcast packets sent to 13788 * a broadcast address which is shared between multiple interfaces 13789 * we should not do this since we just got a random broadcast ire. 13790 */ 13791 if (ire->ire_rfq && ire->ire_type != IRE_BROADCAST) { 13792 boolean_t check_multi = B_TRUE; 13793 13794 /* 13795 * This packet came in on an interface other than the 13796 * one associated with the destination address. 13797 * "Gateway" it to the appropriate interface here. 13798 * As long as the ills belong to the same group, 13799 * we don't consider them to arriving on the wrong 13800 * interface. Thus, when the switch is doing inbound 13801 * load spreading, we won't drop packets when we 13802 * are doing strict multihoming checks. Note, the 13803 * same holds true for 'usesrc groups' where the 13804 * destination address may belong to another interface 13805 * to allow multipathing to happen 13806 */ 13807 ill_group = ill->ill_group; 13808 ire_ill = (ill_t *)(ire->ire_rfq)->q_ptr; 13809 ill_ifindex = ill->ill_usesrc_ifindex; 13810 ire_group = ire_ill->ill_group; 13811 13812 /* 13813 * If it's part of the same IPMP group, or if it's a legal 13814 * address on the 'usesrc' interface, then bypass strict 13815 * checks. 13816 */ 13817 if (ill_group != NULL && ill_group == ire_group) { 13818 check_multi = B_FALSE; 13819 } else if (ill_ifindex != 0 && 13820 ill_ifindex == ire_ill->ill_phyint->phyint_ifindex) { 13821 check_multi = B_FALSE; 13822 } 13823 13824 if (check_multi && 13825 ip_strict_dst_multihoming && 13826 ((ill->ill_flags & 13827 ire->ire_ipif->ipif_ill->ill_flags & 13828 ILLF_ROUTER) == 0)) { 13829 /* Drop packet */ 13830 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13831 freemsg(mp); 13832 return (B_TRUE); 13833 } 13834 13835 /* 13836 * Change the queue (for non-virtual destination network 13837 * interfaces) and ip_rput_local will be called with the right 13838 * queue 13839 */ 13840 q = ire->ire_rfq; 13841 } 13842 /* Must be broadcast. We'll take it. */ 13843 *qp = q; 13844 return (B_FALSE); 13845 } 13846 13847 ire_t * 13848 ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) 13849 { 13850 ipha_t *ipha; 13851 ipaddr_t ip_dst, ip_src; 13852 ire_t *src_ire = NULL; 13853 ill_t *stq_ill; 13854 uint_t hlen; 13855 uint_t pkt_len; 13856 uint32_t sum; 13857 queue_t *dev_q; 13858 boolean_t check_multirt = B_FALSE; 13859 13860 13861 ipha = (ipha_t *)mp->b_rptr; 13862 13863 /* 13864 * Martian Address Filtering [RFC 1812, Section 5.3.7] 13865 * The loopback address check for both src and dst has already 13866 * been checked in ip_input 13867 */ 13868 ip_dst = ntohl(dst); 13869 ip_src = ntohl(ipha->ipha_src); 13870 13871 if (ip_dst == INADDR_ANY || IN_BADCLASS(ip_dst) || 13872 IN_CLASSD(ip_src)) { 13873 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13874 goto drop; 13875 } 13876 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 13877 ALL_ZONES, NULL, MATCH_IRE_TYPE); 13878 13879 if (src_ire != NULL) { 13880 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 13881 goto drop; 13882 } 13883 13884 13885 /* No ire cache of nexthop. So first create one */ 13886 if (ire == NULL) { 13887 ire = ire_forward(dst, &check_multirt, NULL, NULL, NULL); 13888 /* 13889 * We only come to ip_fast_forward if ip_cgtp_filter is 13890 * is not set. So upon return from ire_forward 13891 * check_multirt should remain as false. 13892 */ 13893 ASSERT(!check_multirt); 13894 if (ire == NULL) { 13895 /* An attempt was made to forward the packet */ 13896 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 13897 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13898 mp->b_prev = mp->b_next = 0; 13899 /* send icmp unreachable */ 13900 /* Sent by forwarding path, and router is global zone */ 13901 if (ip_source_routed(ipha)) { 13902 icmp_unreachable(ill->ill_wq, mp, 13903 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID); 13904 } else { 13905 icmp_unreachable(ill->ill_wq, mp, 13906 ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID); 13907 } 13908 return (ire); 13909 } 13910 } 13911 13912 /* 13913 * Forwarding fastpath exception case: 13914 * If either of the follwoing case is true, we take 13915 * the slowpath 13916 * o forwarding is not enabled 13917 * o incoming and outgoing interface are the same, or the same 13918 * IPMP group 13919 * o corresponding ire is in incomplete state 13920 * o packet needs fragmentation 13921 * 13922 * The codeflow from here on is thus: 13923 * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 13924 */ 13925 pkt_len = ntohs(ipha->ipha_length); 13926 stq_ill = (ill_t *)ire->ire_stq->q_ptr; 13927 if (!(stq_ill->ill_flags & ILLF_ROUTER) || 13928 !(ill->ill_flags & ILLF_ROUTER) || 13929 (ill == stq_ill) || 13930 (ill->ill_group != NULL && ill->ill_group == stq_ill->ill_group) || 13931 (ire->ire_nce == NULL) || 13932 (ire->ire_nce->nce_state != ND_REACHABLE) || 13933 (pkt_len > ire->ire_max_frag) || 13934 ipha->ipha_ttl <= 1) { 13935 ip_rput_process_forward(ill->ill_rq, mp, ire, 13936 ipha, ill, B_FALSE); 13937 return (ire); 13938 } 13939 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 13940 13941 DTRACE_PROBE4(ip4__forwarding__start, 13942 ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); 13943 13944 FW_HOOKS(ip4_forwarding_event, ipv4firewall_forwarding, 13945 ill, stq_ill, ipha, mp, mp); 13946 13947 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 13948 13949 if (mp == NULL) 13950 goto drop; 13951 13952 mp->b_datap->db_struioun.cksum.flags = 0; 13953 /* Adjust the checksum to reflect the ttl decrement. */ 13954 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 13955 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 13956 ipha->ipha_ttl--; 13957 13958 dev_q = ire->ire_stq->q_next; 13959 if ((dev_q->q_next != NULL || 13960 dev_q->q_first != NULL) && !canput(dev_q)) { 13961 goto indiscard; 13962 } 13963 13964 hlen = ire->ire_nce->nce_fp_mp != NULL ? 13965 MBLKL(ire->ire_nce->nce_fp_mp) : 0; 13966 13967 if (hlen != 0 || ire->ire_nce->nce_res_mp != NULL) { 13968 mblk_t *mpip = mp; 13969 13970 mp = ip_wput_attach_llhdr(mpip, ire, 0, 0); 13971 if (mp != NULL) { 13972 DTRACE_PROBE4(ip4__physical__out__start, 13973 ill_t *, NULL, ill_t *, stq_ill, 13974 ipha_t *, ipha, mblk_t *, mp); 13975 FW_HOOKS(ip4_physical_out_event, 13976 ipv4firewall_physical_out, 13977 NULL, stq_ill, ipha, mp, mpip); 13978 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, 13979 mp); 13980 if (mp == NULL) 13981 goto drop; 13982 13983 UPDATE_IB_PKT_COUNT(ire); 13984 ire->ire_last_used_time = lbolt; 13985 BUMP_MIB(stq_ill->ill_ip_mib, 13986 ipIfStatsHCOutForwDatagrams); 13987 BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 13988 UPDATE_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutOctets, 13989 pkt_len); 13990 putnext(ire->ire_stq, mp); 13991 return (ire); 13992 } 13993 } 13994 13995 indiscard: 13996 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13997 drop: 13998 if (mp != NULL) 13999 freemsg(mp); 14000 if (src_ire != NULL) 14001 ire_refrele(src_ire); 14002 return (ire); 14003 14004 } 14005 14006 /* 14007 * This function is called in the forwarding slowpath, when 14008 * either the ire lacks the link-layer address, or the packet needs 14009 * further processing(eg. fragmentation), before transmission. 14010 */ 14011 14012 static void 14013 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14014 ill_t *ill, boolean_t ll_multicast) 14015 { 14016 ill_group_t *ill_group; 14017 ill_group_t *ire_group; 14018 queue_t *dev_q; 14019 ire_t *src_ire; 14020 14021 ASSERT(ire->ire_stq != NULL); 14022 14023 mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ 14024 mp->b_next = NULL; /* ip_rput_noire sets dst here */ 14025 14026 if (ll_multicast != 0) { 14027 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14028 goto drop_pkt; 14029 } 14030 14031 /* 14032 * check if ipha_src is a broadcast address. Note that this 14033 * check is redundant when we get here from ip_fast_forward() 14034 * which has already done this check. However, since we can 14035 * also get here from ip_rput_process_broadcast() or, for 14036 * for the slow path through ip_fast_forward(), we perform 14037 * the check again for code-reusability 14038 */ 14039 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 14040 ALL_ZONES, NULL, MATCH_IRE_TYPE); 14041 if (src_ire != NULL || ntohl(ipha->ipha_dst) == INADDR_ANY || 14042 IN_BADCLASS(ntohl(ipha->ipha_dst))) { 14043 if (src_ire != NULL) 14044 ire_refrele(src_ire); 14045 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14046 ip2dbg(("ip_rput_process_forward: Received packet with" 14047 " bad src/dst address on %s\n", ill->ill_name)); 14048 goto drop_pkt; 14049 } 14050 14051 ill_group = ill->ill_group; 14052 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 14053 /* 14054 * Check if we want to forward this one at this time. 14055 * We allow source routed packets on a host provided that 14056 * they go out the same interface or same interface group 14057 * as they came in on. 14058 * 14059 * XXX To be quicker, we may wish to not chase pointers to 14060 * get the ILLF_ROUTER flag and instead store the 14061 * forwarding policy in the ire. An unfortunate 14062 * side-effect of that would be requiring an ire flush 14063 * whenever the ILLF_ROUTER flag changes. 14064 */ 14065 if (((ill->ill_flags & 14066 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 14067 ILLF_ROUTER) == 0) && 14068 !(ip_source_routed(ipha) && (ire->ire_rfq == q || 14069 (ill_group != NULL && ill_group == ire_group)))) { 14070 BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); 14071 if (ip_source_routed(ipha)) { 14072 q = WR(q); 14073 /* 14074 * Clear the indication that this may have 14075 * hardware checksum as we are not using it. 14076 */ 14077 DB_CKSUMFLAGS(mp) = 0; 14078 /* Sent by forwarding path, and router is global zone */ 14079 icmp_unreachable(q, mp, 14080 ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID); 14081 return; 14082 } 14083 goto drop_pkt; 14084 } 14085 14086 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); 14087 14088 /* Packet is being forwarded. Turning off hwcksum flag. */ 14089 DB_CKSUMFLAGS(mp) = 0; 14090 if (ip_g_send_redirects) { 14091 /* 14092 * Check whether the incoming interface and outgoing 14093 * interface is part of the same group. If so, 14094 * send redirects. 14095 * 14096 * Check the source address to see if it originated 14097 * on the same logical subnet it is going back out on. 14098 * If so, we should be able to send it a redirect. 14099 * Avoid sending a redirect if the destination 14100 * is directly connected (i.e., ipha_dst is the same 14101 * as ire_gateway_addr or the ire_addr of the 14102 * nexthop IRE_CACHE ), or if the packet was source 14103 * routed out this interface. 14104 */ 14105 ipaddr_t src, nhop; 14106 mblk_t *mp1; 14107 ire_t *nhop_ire = NULL; 14108 14109 /* 14110 * Check whether ire_rfq and q are from the same ill 14111 * or if they are not same, they at least belong 14112 * to the same group. If so, send redirects. 14113 */ 14114 if ((ire->ire_rfq == q || 14115 (ill_group != NULL && ill_group == ire_group)) && 14116 !ip_source_routed(ipha)) { 14117 14118 nhop = (ire->ire_gateway_addr != 0 ? 14119 ire->ire_gateway_addr : ire->ire_addr); 14120 14121 if (ipha->ipha_dst == nhop) { 14122 /* 14123 * We avoid sending a redirect if the 14124 * destination is directly connected 14125 * because it is possible that multiple 14126 * IP subnets may have been configured on 14127 * the link, and the source may not 14128 * be on the same subnet as ip destination, 14129 * even though they are on the same 14130 * physical link. 14131 */ 14132 goto sendit; 14133 } 14134 14135 src = ipha->ipha_src; 14136 14137 /* 14138 * We look up the interface ire for the nexthop, 14139 * to see if ipha_src is in the same subnet 14140 * as the nexthop. 14141 * 14142 * Note that, if, in the future, IRE_CACHE entries 14143 * are obsoleted, this lookup will not be needed, 14144 * as the ire passed to this function will be the 14145 * same as the nhop_ire computed below. 14146 */ 14147 nhop_ire = ire_ftable_lookup(nhop, 0, 0, 14148 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 14149 0, NULL, MATCH_IRE_TYPE); 14150 14151 if (nhop_ire != NULL) { 14152 if ((src & nhop_ire->ire_mask) == 14153 (nhop & nhop_ire->ire_mask)) { 14154 /* 14155 * The source is directly connected. 14156 * Just copy the ip header (which is 14157 * in the first mblk) 14158 */ 14159 mp1 = copyb(mp); 14160 if (mp1 != NULL) { 14161 icmp_send_redirect(WR(q), mp1, 14162 nhop); 14163 } 14164 } 14165 ire_refrele(nhop_ire); 14166 } 14167 } 14168 } 14169 sendit: 14170 dev_q = ire->ire_stq->q_next; 14171 if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { 14172 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14173 freemsg(mp); 14174 return; 14175 } 14176 14177 ip_rput_forward(ire, ipha, mp, ill); 14178 return; 14179 14180 drop_pkt: 14181 ip2dbg(("ip_rput_process_forward: drop pkt\n")); 14182 freemsg(mp); 14183 } 14184 14185 ire_t * 14186 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, 14187 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 14188 { 14189 queue_t *q; 14190 uint16_t hcksumflags; 14191 14192 q = *qp; 14193 14194 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts); 14195 14196 /* 14197 * Clear the indication that this may have hardware 14198 * checksum as we are not using it for forwarding. 14199 */ 14200 hcksumflags = DB_CKSUMFLAGS(mp); 14201 DB_CKSUMFLAGS(mp) = 0; 14202 14203 /* 14204 * Directed broadcast forwarding: if the packet came in over a 14205 * different interface then it is routed out over we can forward it. 14206 */ 14207 if (ipha->ipha_protocol == IPPROTO_TCP) { 14208 ire_refrele(ire); 14209 freemsg(mp); 14210 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14211 return (NULL); 14212 } 14213 /* 14214 * For multicast we have set dst to be INADDR_BROADCAST 14215 * for delivering to all STREAMS. IRE_MARK_NORECV is really 14216 * only for broadcast packets. 14217 */ 14218 if (!CLASSD(ipha->ipha_dst)) { 14219 ire_t *new_ire; 14220 ipif_t *ipif; 14221 /* 14222 * For ill groups, as the switch duplicates broadcasts 14223 * across all the ports, we need to filter out and 14224 * send up only one copy. There is one copy for every 14225 * broadcast address on each ill. Thus, we look for a 14226 * specific IRE on this ill and look at IRE_MARK_NORECV 14227 * later to see whether this ill is eligible to receive 14228 * them or not. ill_nominate_bcast_rcv() nominates only 14229 * one set of IREs for receiving. 14230 */ 14231 14232 ipif = ipif_get_next_ipif(NULL, ill); 14233 if (ipif == NULL) { 14234 ire_refrele(ire); 14235 freemsg(mp); 14236 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14237 return (NULL); 14238 } 14239 new_ire = ire_ctable_lookup(dst, 0, 0, 14240 ipif, ALL_ZONES, NULL, MATCH_IRE_ILL); 14241 ipif_refrele(ipif); 14242 14243 if (new_ire != NULL) { 14244 if (new_ire->ire_marks & IRE_MARK_NORECV) { 14245 ire_refrele(ire); 14246 ire_refrele(new_ire); 14247 freemsg(mp); 14248 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14249 return (NULL); 14250 } 14251 /* 14252 * In the special case of multirouted broadcast 14253 * packets, we unconditionally need to "gateway" 14254 * them to the appropriate interface here. 14255 * In the normal case, this cannot happen, because 14256 * there is no broadcast IRE tagged with the 14257 * RTF_MULTIRT flag. 14258 */ 14259 if (new_ire->ire_flags & RTF_MULTIRT) { 14260 ire_refrele(new_ire); 14261 if (ire->ire_rfq != NULL) { 14262 q = ire->ire_rfq; 14263 *qp = q; 14264 } 14265 } else { 14266 ire_refrele(ire); 14267 ire = new_ire; 14268 } 14269 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 14270 if (!ip_g_forward_directed_bcast) { 14271 /* 14272 * Free the message if 14273 * ip_g_forward_directed_bcast is turned 14274 * off for non-local broadcast. 14275 */ 14276 ire_refrele(ire); 14277 freemsg(mp); 14278 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14279 return (NULL); 14280 } 14281 } else { 14282 /* 14283 * This CGTP packet successfully passed the 14284 * CGTP filter, but the related CGTP 14285 * broadcast IRE has not been found, 14286 * meaning that the redundant ipif is 14287 * probably down. However, if we discarded 14288 * this packet, its duplicate would be 14289 * filtered out by the CGTP filter so none 14290 * of them would get through. So we keep 14291 * going with this one. 14292 */ 14293 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 14294 if (ire->ire_rfq != NULL) { 14295 q = ire->ire_rfq; 14296 *qp = q; 14297 } 14298 } 14299 } 14300 if (ip_g_forward_directed_bcast && ll_multicast == 0) { 14301 /* 14302 * Verify that there are not more then one 14303 * IRE_BROADCAST with this broadcast address which 14304 * has ire_stq set. 14305 * TODO: simplify, loop over all IRE's 14306 */ 14307 ire_t *ire1; 14308 int num_stq = 0; 14309 mblk_t *mp1; 14310 14311 /* Find the first one with ire_stq set */ 14312 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 14313 for (ire1 = ire; ire1 && 14314 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 14315 ire1 = ire1->ire_next) 14316 ; 14317 if (ire1) { 14318 ire_refrele(ire); 14319 ire = ire1; 14320 IRE_REFHOLD(ire); 14321 } 14322 14323 /* Check if there are additional ones with stq set */ 14324 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 14325 if (ire->ire_addr != ire1->ire_addr) 14326 break; 14327 if (ire1->ire_stq) { 14328 num_stq++; 14329 break; 14330 } 14331 } 14332 rw_exit(&ire->ire_bucket->irb_lock); 14333 if (num_stq == 1 && ire->ire_stq != NULL) { 14334 ip1dbg(("ip_rput_process_broadcast: directed " 14335 "broadcast to 0x%x\n", 14336 ntohl(ire->ire_addr))); 14337 mp1 = copymsg(mp); 14338 if (mp1) { 14339 switch (ipha->ipha_protocol) { 14340 case IPPROTO_UDP: 14341 ip_udp_input(q, mp1, ipha, ire, ill); 14342 break; 14343 default: 14344 ip_proto_input(q, mp1, ipha, ire, ill); 14345 break; 14346 } 14347 } 14348 /* 14349 * Adjust ttl to 2 (1+1 - the forward engine 14350 * will decrement it by one. 14351 */ 14352 if (ip_csum_hdr(ipha)) { 14353 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 14354 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 14355 freemsg(mp); 14356 ire_refrele(ire); 14357 return (NULL); 14358 } 14359 ipha->ipha_ttl = ip_broadcast_ttl + 1; 14360 ipha->ipha_hdr_checksum = 0; 14361 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14362 ip_rput_process_forward(q, mp, ire, ipha, 14363 ill, ll_multicast); 14364 ire_refrele(ire); 14365 return (NULL); 14366 } 14367 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 14368 ntohl(ire->ire_addr))); 14369 } 14370 14371 14372 /* Restore any hardware checksum flags */ 14373 DB_CKSUMFLAGS(mp) = hcksumflags; 14374 return (ire); 14375 } 14376 14377 /* ARGSUSED */ 14378 static boolean_t 14379 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 14380 int *ll_multicast, ipaddr_t *dstp) 14381 { 14382 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts); 14383 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, 14384 ntohs(ipha->ipha_length)); 14385 14386 /* 14387 * Forward packets only if we have joined the allmulti 14388 * group on this interface. 14389 */ 14390 if (ip_g_mrouter && ill->ill_join_allmulti) { 14391 int retval; 14392 14393 /* 14394 * Clear the indication that this may have hardware 14395 * checksum as we are not using it. 14396 */ 14397 DB_CKSUMFLAGS(mp) = 0; 14398 retval = ip_mforward(ill, ipha, mp); 14399 /* ip_mforward updates mib variables if needed */ 14400 /* clear b_prev - used by ip_mroute_decap */ 14401 mp->b_prev = NULL; 14402 14403 switch (retval) { 14404 case 0: 14405 /* 14406 * pkt is okay and arrived on phyint. 14407 * 14408 * If we are running as a multicast router 14409 * we need to see all IGMP and/or PIM packets. 14410 */ 14411 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 14412 (ipha->ipha_protocol == IPPROTO_PIM)) { 14413 goto done; 14414 } 14415 break; 14416 case -1: 14417 /* pkt is mal-formed, toss it */ 14418 goto drop_pkt; 14419 case 1: 14420 /* pkt is okay and arrived on a tunnel */ 14421 /* 14422 * If we are running a multicast router 14423 * we need to see all igmp packets. 14424 */ 14425 if (ipha->ipha_protocol == IPPROTO_IGMP) { 14426 *dstp = INADDR_BROADCAST; 14427 *ll_multicast = 1; 14428 return (B_FALSE); 14429 } 14430 14431 goto drop_pkt; 14432 } 14433 } 14434 14435 ILM_WALKER_HOLD(ill); 14436 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 14437 /* 14438 * This might just be caused by the fact that 14439 * multiple IP Multicast addresses map to the same 14440 * link layer multicast - no need to increment counter! 14441 */ 14442 ILM_WALKER_RELE(ill); 14443 freemsg(mp); 14444 return (B_TRUE); 14445 } 14446 ILM_WALKER_RELE(ill); 14447 done: 14448 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 14449 /* 14450 * This assumes the we deliver to all streams for multicast 14451 * and broadcast packets. 14452 */ 14453 *dstp = INADDR_BROADCAST; 14454 *ll_multicast = 1; 14455 return (B_FALSE); 14456 drop_pkt: 14457 ip2dbg(("ip_rput: drop pkt\n")); 14458 freemsg(mp); 14459 return (B_TRUE); 14460 } 14461 14462 static boolean_t 14463 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 14464 int *ll_multicast, mblk_t **mpp) 14465 { 14466 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 14467 boolean_t must_copy = B_FALSE; 14468 struct iocblk *iocp; 14469 ipha_t *ipha; 14470 14471 #define rptr ((uchar_t *)ipha) 14472 14473 first_mp = *first_mpp; 14474 mp = *mpp; 14475 14476 ASSERT(first_mp == mp); 14477 14478 /* 14479 * if db_ref > 1 then copymsg and free original. Packet may be 14480 * changed and do not want other entity who has a reference to this 14481 * message to trip over the changes. This is a blind change because 14482 * trying to catch all places that might change packet is too 14483 * difficult (since it may be a module above this one) 14484 * 14485 * This corresponds to the non-fast path case. We walk down the full 14486 * chain in this case, and check the db_ref count of all the dblks, 14487 * and do a copymsg if required. It is possible that the db_ref counts 14488 * of the data blocks in the mblk chain can be different. 14489 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 14490 * count of 1, followed by a M_DATA block with a ref count of 2, if 14491 * 'snoop' is running. 14492 */ 14493 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 14494 if (mp1->b_datap->db_ref > 1) { 14495 must_copy = B_TRUE; 14496 break; 14497 } 14498 } 14499 14500 if (must_copy) { 14501 mp1 = copymsg(mp); 14502 if (mp1 == NULL) { 14503 for (mp1 = mp; mp1 != NULL; 14504 mp1 = mp1->b_cont) { 14505 mp1->b_next = NULL; 14506 mp1->b_prev = NULL; 14507 } 14508 freemsg(mp); 14509 if (ill != NULL) { 14510 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14511 } else { 14512 BUMP_MIB(&ip_mib, ipIfStatsInDiscards); 14513 } 14514 return (B_TRUE); 14515 } 14516 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 14517 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 14518 /* Copy b_prev - used by ip_mroute_decap */ 14519 to_mp->b_prev = from_mp->b_prev; 14520 from_mp->b_prev = NULL; 14521 } 14522 *first_mpp = first_mp = mp1; 14523 freemsg(mp); 14524 mp = mp1; 14525 *mpp = mp1; 14526 } 14527 14528 ipha = (ipha_t *)mp->b_rptr; 14529 14530 /* 14531 * previous code has a case for M_DATA. 14532 * We want to check how that happens. 14533 */ 14534 ASSERT(first_mp->b_datap->db_type != M_DATA); 14535 switch (first_mp->b_datap->db_type) { 14536 case M_PROTO: 14537 case M_PCPROTO: 14538 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 14539 DL_UNITDATA_IND) { 14540 /* Go handle anything other than data elsewhere. */ 14541 ip_rput_dlpi(q, mp); 14542 return (B_TRUE); 14543 } 14544 *ll_multicast = ((dl_unitdata_ind_t *)rptr)->dl_group_address; 14545 /* Ditch the DLPI header. */ 14546 mp1 = mp->b_cont; 14547 ASSERT(first_mp == mp); 14548 *first_mpp = mp1; 14549 freeb(mp); 14550 *mpp = mp1; 14551 return (B_FALSE); 14552 case M_IOCACK: 14553 ip1dbg(("got iocack ")); 14554 iocp = (struct iocblk *)mp->b_rptr; 14555 switch (iocp->ioc_cmd) { 14556 case DL_IOC_HDR_INFO: 14557 ill = (ill_t *)q->q_ptr; 14558 ill_fastpath_ack(ill, mp); 14559 return (B_TRUE); 14560 case SIOCSTUNPARAM: 14561 case OSIOCSTUNPARAM: 14562 /* Go through qwriter_ip */ 14563 break; 14564 case SIOCGTUNPARAM: 14565 case OSIOCGTUNPARAM: 14566 ip_rput_other(NULL, q, mp, NULL); 14567 return (B_TRUE); 14568 default: 14569 putnext(q, mp); 14570 return (B_TRUE); 14571 } 14572 /* FALLTHRU */ 14573 case M_ERROR: 14574 case M_HANGUP: 14575 /* 14576 * Since this is on the ill stream we unconditionally 14577 * bump up the refcount 14578 */ 14579 ill_refhold(ill); 14580 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, 14581 B_FALSE); 14582 return (B_TRUE); 14583 case M_CTL: 14584 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 14585 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 14586 IPHADA_M_CTL)) { 14587 /* 14588 * It's an IPsec accelerated packet. 14589 * Make sure that the ill from which we received the 14590 * packet has enabled IPsec hardware acceleration. 14591 */ 14592 if (!(ill->ill_capabilities & 14593 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 14594 /* IPsec kstats: bean counter */ 14595 freemsg(mp); 14596 return (B_TRUE); 14597 } 14598 14599 /* 14600 * Make mp point to the mblk following the M_CTL, 14601 * then process according to type of mp. 14602 * After this processing, first_mp will point to 14603 * the data-attributes and mp to the pkt following 14604 * the M_CTL. 14605 */ 14606 mp = first_mp->b_cont; 14607 if (mp == NULL) { 14608 freemsg(first_mp); 14609 return (B_TRUE); 14610 } 14611 /* 14612 * A Hardware Accelerated packet can only be M_DATA 14613 * ESP or AH packet. 14614 */ 14615 if (mp->b_datap->db_type != M_DATA) { 14616 /* non-M_DATA IPsec accelerated packet */ 14617 IPSECHW_DEBUG(IPSECHW_PKT, 14618 ("non-M_DATA IPsec accelerated pkt\n")); 14619 freemsg(first_mp); 14620 return (B_TRUE); 14621 } 14622 ipha = (ipha_t *)mp->b_rptr; 14623 if (ipha->ipha_protocol != IPPROTO_AH && 14624 ipha->ipha_protocol != IPPROTO_ESP) { 14625 IPSECHW_DEBUG(IPSECHW_PKT, 14626 ("non-M_DATA IPsec accelerated pkt\n")); 14627 freemsg(first_mp); 14628 return (B_TRUE); 14629 } 14630 *mpp = mp; 14631 return (B_FALSE); 14632 } 14633 putnext(q, mp); 14634 return (B_TRUE); 14635 case M_FLUSH: 14636 if (*mp->b_rptr & FLUSHW) { 14637 *mp->b_rptr &= ~FLUSHR; 14638 qreply(q, mp); 14639 return (B_TRUE); 14640 } 14641 freemsg(mp); 14642 return (B_TRUE); 14643 case M_IOCNAK: 14644 ip1dbg(("got iocnak ")); 14645 iocp = (struct iocblk *)mp->b_rptr; 14646 switch (iocp->ioc_cmd) { 14647 case DL_IOC_HDR_INFO: 14648 case SIOCSTUNPARAM: 14649 case OSIOCSTUNPARAM: 14650 /* 14651 * Since this is on the ill stream we unconditionally 14652 * bump up the refcount 14653 */ 14654 ill_refhold(ill); 14655 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, 14656 CUR_OP, B_FALSE); 14657 return (B_TRUE); 14658 case SIOCGTUNPARAM: 14659 case OSIOCGTUNPARAM: 14660 ip_rput_other(NULL, q, mp, NULL); 14661 return (B_TRUE); 14662 default: 14663 break; 14664 } 14665 /* FALLTHRU */ 14666 default: 14667 putnext(q, mp); 14668 return (B_TRUE); 14669 } 14670 } 14671 14672 /* Read side put procedure. Packets coming from the wire arrive here. */ 14673 void 14674 ip_rput(queue_t *q, mblk_t *mp) 14675 { 14676 ill_t *ill; 14677 14678 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 14679 14680 ill = (ill_t *)q->q_ptr; 14681 14682 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 14683 union DL_primitives *dl; 14684 14685 /* 14686 * Things are opening or closing. Only accept DLPI control 14687 * messages. In the open case, the ill->ill_ipif has not yet 14688 * been created. In the close case, things hanging off the 14689 * ill could have been freed already. In either case it 14690 * may not be safe to proceed further. 14691 */ 14692 14693 dl = (union DL_primitives *)mp->b_rptr; 14694 if ((mp->b_datap->db_type != M_PCPROTO) || 14695 (dl->dl_primitive == DL_UNITDATA_IND)) { 14696 /* 14697 * Also SIOC[GS]TUN* ioctls can come here. 14698 */ 14699 inet_freemsg(mp); 14700 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14701 "ip_input_end: q %p (%S)", q, "uninit"); 14702 return; 14703 } 14704 } 14705 14706 /* 14707 * if db_ref > 1 then copymsg and free original. Packet may be 14708 * changed and we do not want the other entity who has a reference to 14709 * this message to trip over the changes. This is a blind change because 14710 * trying to catch all places that might change the packet is too 14711 * difficult. 14712 * 14713 * This corresponds to the fast path case, where we have a chain of 14714 * M_DATA mblks. We check the db_ref count of only the 1st data block 14715 * in the mblk chain. There doesn't seem to be a reason why a device 14716 * driver would send up data with varying db_ref counts in the mblk 14717 * chain. In any case the Fast path is a private interface, and our 14718 * drivers don't do such a thing. Given the above assumption, there is 14719 * no need to walk down the entire mblk chain (which could have a 14720 * potential performance problem) 14721 */ 14722 if (mp->b_datap->db_ref > 1) { 14723 mblk_t *mp1; 14724 boolean_t adjusted = B_FALSE; 14725 IP_STAT(ip_db_ref); 14726 14727 /* 14728 * The IP_RECVSLLA option depends on having the link layer 14729 * header. First check that: 14730 * a> the underlying device is of type ether, since this 14731 * option is currently supported only over ethernet. 14732 * b> there is enough room to copy over the link layer header. 14733 * 14734 * Once the checks are done, adjust rptr so that the link layer 14735 * header will be copied via copymsg. Note that, IFT_ETHER may 14736 * be returned by some non-ethernet drivers but in this case the 14737 * second check will fail. 14738 */ 14739 if (ill->ill_type == IFT_ETHER && 14740 (mp->b_rptr - mp->b_datap->db_base) >= 14741 sizeof (struct ether_header)) { 14742 mp->b_rptr -= sizeof (struct ether_header); 14743 adjusted = B_TRUE; 14744 } 14745 mp1 = copymsg(mp); 14746 if (mp1 == NULL) { 14747 mp->b_next = NULL; 14748 /* clear b_prev - used by ip_mroute_decap */ 14749 mp->b_prev = NULL; 14750 freemsg(mp); 14751 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14752 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14753 "ip_rput_end: q %p (%S)", q, "copymsg"); 14754 return; 14755 } 14756 if (adjusted) { 14757 /* 14758 * Copy is done. Restore the pointer in the _new_ mblk 14759 */ 14760 mp1->b_rptr += sizeof (struct ether_header); 14761 } 14762 /* Copy b_prev - used by ip_mroute_decap */ 14763 mp1->b_prev = mp->b_prev; 14764 mp->b_prev = NULL; 14765 freemsg(mp); 14766 mp = mp1; 14767 } 14768 14769 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 14770 "ip_rput_end: q %p (%S)", q, "end"); 14771 14772 ip_input(ill, NULL, mp, NULL); 14773 } 14774 14775 /* 14776 * Direct read side procedure capable of dealing with chains. GLDv3 based 14777 * drivers call this function directly with mblk chains while STREAMS 14778 * read side procedure ip_rput() calls this for single packet with ip_ring 14779 * set to NULL to process one packet at a time. 14780 * 14781 * The ill will always be valid if this function is called directly from 14782 * the driver. 14783 * 14784 * If ip_input() is called from GLDv3: 14785 * 14786 * - This must be a non-VLAN IP stream. 14787 * - 'mp' is either an untagged or a special priority-tagged packet. 14788 * - Any VLAN tag that was in the MAC header has been stripped. 14789 * 14790 * If the IP header in packet is not 32-bit aligned, every message in the 14791 * chain will be aligned before further operations. This is required on SPARC 14792 * platform. 14793 */ 14794 /* ARGSUSED */ 14795 void 14796 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, 14797 struct mac_header_info_s *mhip) 14798 { 14799 ipaddr_t dst = NULL; 14800 ipaddr_t prev_dst; 14801 ire_t *ire = NULL; 14802 ipha_t *ipha; 14803 uint_t pkt_len; 14804 ssize_t len; 14805 uint_t opt_len; 14806 int ll_multicast; 14807 int cgtp_flt_pkt; 14808 queue_t *q = ill->ill_rq; 14809 squeue_t *curr_sqp = NULL; 14810 mblk_t *head = NULL; 14811 mblk_t *tail = NULL; 14812 mblk_t *first_mp; 14813 mblk_t *mp; 14814 mblk_t *dmp; 14815 int cnt = 0; 14816 14817 ASSERT(mp_chain != NULL); 14818 ASSERT(ill != NULL); 14819 14820 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 14821 14822 #define rptr ((uchar_t *)ipha) 14823 14824 while (mp_chain != NULL) { 14825 first_mp = mp = mp_chain; 14826 mp_chain = mp_chain->b_next; 14827 mp->b_next = NULL; 14828 ll_multicast = 0; 14829 14830 /* 14831 * We do ire caching from one iteration to 14832 * another. In the event the packet chain contains 14833 * all packets from the same dst, this caching saves 14834 * an ire_cache_lookup for each of the succeeding 14835 * packets in a packet chain. 14836 */ 14837 prev_dst = dst; 14838 14839 /* 14840 * Check and align the IP header. 14841 */ 14842 if (DB_TYPE(mp) == M_DATA) { 14843 dmp = mp; 14844 } else if (DB_TYPE(mp) == M_PROTO && 14845 *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { 14846 dmp = mp->b_cont; 14847 } else { 14848 dmp = NULL; 14849 } 14850 if (dmp != NULL) { 14851 /* 14852 * IP header ptr not aligned? 14853 * OR IP header not complete in first mblk 14854 */ 14855 if (!OK_32PTR(dmp->b_rptr) || 14856 MBLKL(dmp) < IP_SIMPLE_HDR_LENGTH) { 14857 if (!ip_check_and_align_header(q, dmp)) 14858 continue; 14859 } 14860 } 14861 14862 /* 14863 * ip_input fast path 14864 */ 14865 14866 /* mblk type is not M_DATA */ 14867 if (DB_TYPE(mp) != M_DATA) { 14868 if (ip_rput_process_notdata(q, &first_mp, ill, 14869 &ll_multicast, &mp)) 14870 continue; 14871 } 14872 14873 /* Make sure its an M_DATA and that its aligned */ 14874 ASSERT(DB_TYPE(mp) == M_DATA); 14875 ASSERT(DB_REF(mp) == 1 && OK_32PTR(mp->b_rptr)); 14876 14877 ipha = (ipha_t *)mp->b_rptr; 14878 len = mp->b_wptr - rptr; 14879 pkt_len = ntohs(ipha->ipha_length); 14880 14881 /* 14882 * We must count all incoming packets, even if they end 14883 * up being dropped later on. 14884 */ 14885 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); 14886 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); 14887 14888 /* multiple mblk or too short */ 14889 len -= pkt_len; 14890 if (len != 0) { 14891 /* 14892 * Make sure we have data length consistent 14893 * with the IP header. 14894 */ 14895 if (mp->b_cont == NULL) { 14896 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 14897 BUMP_MIB(ill->ill_ip_mib, 14898 ipIfStatsInHdrErrors); 14899 ip2dbg(("ip_input: drop pkt\n")); 14900 freemsg(mp); 14901 continue; 14902 } 14903 mp->b_wptr = rptr + pkt_len; 14904 } else if ((len += msgdsize(mp->b_cont)) != 0) { 14905 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 14906 BUMP_MIB(ill->ill_ip_mib, 14907 ipIfStatsInHdrErrors); 14908 ip2dbg(("ip_input: drop pkt\n")); 14909 freemsg(mp); 14910 continue; 14911 } 14912 (void) adjmsg(mp, -len); 14913 IP_STAT(ip_multimblk3); 14914 } 14915 } 14916 14917 /* Obtain the dst of the current packet */ 14918 dst = ipha->ipha_dst; 14919 14920 if (IP_LOOPBACK_ADDR(dst) || 14921 IP_LOOPBACK_ADDR(ipha->ipha_src)) { 14922 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors); 14923 cmn_err(CE_CONT, "dst %X src %X\n", 14924 dst, ipha->ipha_src); 14925 freemsg(mp); 14926 continue; 14927 } 14928 14929 /* 14930 * The event for packets being received from a 'physical' 14931 * interface is placed after validation of the source and/or 14932 * destination address as being local so that packets can be 14933 * redirected to loopback addresses using ipnat. 14934 */ 14935 DTRACE_PROBE4(ip4__physical__in__start, 14936 ill_t *, ill, ill_t *, NULL, 14937 ipha_t *, ipha, mblk_t *, first_mp); 14938 14939 FW_HOOKS(ip4_physical_in_event, ipv4firewall_physical_in, 14940 ill, NULL, ipha, first_mp, mp); 14941 14942 DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); 14943 14944 if (first_mp == NULL) { 14945 continue; 14946 } 14947 dst = ipha->ipha_dst; 14948 14949 /* 14950 * Attach any necessary label information to 14951 * this packet 14952 */ 14953 if (is_system_labeled() && 14954 !tsol_get_pkt_label(mp, IPV4_VERSION)) { 14955 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14956 freemsg(mp); 14957 continue; 14958 } 14959 14960 /* 14961 * Reuse the cached ire only if the ipha_dst of the previous 14962 * packet is the same as the current packet AND it is not 14963 * INADDR_ANY. 14964 */ 14965 if (!(dst == prev_dst && dst != INADDR_ANY) && 14966 (ire != NULL)) { 14967 ire_refrele(ire); 14968 ire = NULL; 14969 } 14970 opt_len = ipha->ipha_version_and_hdr_length - 14971 IP_SIMPLE_HDR_VERSION; 14972 14973 /* 14974 * Check to see if we can take the fastpath. 14975 * That is possible if the following conditions are met 14976 * o Tsol disabled 14977 * o CGTP disabled 14978 * o ipp_action_count is 0 14979 * o Mobile IP not running 14980 * o no options in the packet 14981 * o not a RSVP packet 14982 * o not a multicast packet 14983 */ 14984 if (!is_system_labeled() && 14985 !ip_cgtp_filter && ipp_action_count == 0 && 14986 ill->ill_mrtun_refcnt == 0 && ill->ill_srcif_refcnt == 0 && 14987 opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && 14988 !ll_multicast && !CLASSD(dst)) { 14989 if (ire == NULL) 14990 ire = ire_cache_lookup(dst, ALL_ZONES, NULL); 14991 14992 /* incoming packet is for forwarding */ 14993 if (ire == NULL || (ire->ire_type & IRE_CACHE)) { 14994 ire = ip_fast_forward(ire, dst, ill, mp); 14995 continue; 14996 } 14997 /* incoming packet is for local consumption */ 14998 if (ire->ire_type & IRE_LOCAL) 14999 goto local; 15000 } 15001 15002 /* 15003 * Disable ire caching for anything more complex 15004 * than the simple fast path case we checked for above. 15005 */ 15006 if (ire != NULL) { 15007 ire_refrele(ire); 15008 ire = NULL; 15009 } 15010 15011 /* Full-blown slow path */ 15012 if (opt_len != 0) { 15013 if (len != 0) 15014 IP_STAT(ip_multimblk4); 15015 else 15016 IP_STAT(ip_ipoptions); 15017 if (!ip_rput_multimblk_ipoptions(q, ill, mp, &ipha, 15018 &dst)) 15019 continue; 15020 } 15021 15022 /* 15023 * Invoke the CGTP (multirouting) filtering module to process 15024 * the incoming packet. Packets identified as duplicates 15025 * must be discarded. Filtering is active only if the 15026 * the ip_cgtp_filter ndd variable is non-zero. 15027 */ 15028 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 15029 if (ip_cgtp_filter && (ip_cgtp_filter_ops != NULL)) { 15030 cgtp_flt_pkt = 15031 ip_cgtp_filter_ops->cfo_filter(q, mp); 15032 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 15033 freemsg(first_mp); 15034 continue; 15035 } 15036 } 15037 15038 /* 15039 * If rsvpd is running, let RSVP daemon handle its processing 15040 * and forwarding of RSVP multicast/unicast packets. 15041 * If rsvpd is not running but mrouted is running, RSVP 15042 * multicast packets are forwarded as multicast traffic 15043 * and RSVP unicast packets are forwarded by unicast router. 15044 * If neither rsvpd nor mrouted is running, RSVP multicast 15045 * packets are not forwarded, but the unicast packets are 15046 * forwarded like unicast traffic. 15047 */ 15048 if (ipha->ipha_protocol == IPPROTO_RSVP && 15049 ipcl_proto_search(IPPROTO_RSVP) != NULL) { 15050 /* RSVP packet and rsvpd running. Treat as ours */ 15051 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 15052 /* 15053 * This assumes that we deliver to all streams for 15054 * multicast and broadcast packets. 15055 * We have to force ll_multicast to 1 to handle the 15056 * M_DATA messages passed in from ip_mroute_decap. 15057 */ 15058 dst = INADDR_BROADCAST; 15059 ll_multicast = 1; 15060 } else if (CLASSD(dst)) { 15061 /* packet is multicast */ 15062 mp->b_next = NULL; 15063 if (ip_rput_process_multicast(q, mp, ill, ipha, 15064 &ll_multicast, &dst)) 15065 continue; 15066 } 15067 15068 15069 /* 15070 * Check if the packet is coming from the Mobile IP 15071 * forward tunnel interface 15072 */ 15073 if (ill->ill_srcif_refcnt > 0) { 15074 ire = ire_srcif_table_lookup(dst, IRE_INTERFACE, 15075 NULL, ill, MATCH_IRE_TYPE); 15076 if (ire != NULL && ire->ire_nce->nce_res_mp == NULL && 15077 ire->ire_ipif->ipif_net_type == IRE_IF_RESOLVER) { 15078 15079 /* We need to resolve the link layer info */ 15080 ire_refrele(ire); 15081 ire = NULL; 15082 (void) ip_rput_noire(q, (ill_t *)q->q_ptr, mp, 15083 ll_multicast, dst); 15084 continue; 15085 } 15086 } 15087 15088 if (ire == NULL) { 15089 ire = ire_cache_lookup(dst, ALL_ZONES, 15090 MBLK_GETLABEL(mp)); 15091 } 15092 15093 /* 15094 * If mipagent is running and reverse tunnel is created as per 15095 * mobile node request, then any packet coming through the 15096 * incoming interface from the mobile-node, should be reverse 15097 * tunneled to it's home agent except those that are destined 15098 * to foreign agent only. 15099 * This needs source address based ire lookup. The routing 15100 * entries for source address based lookup are only created by 15101 * mipagent program only when a reverse tunnel is created. 15102 * Reference : RFC2002, RFC2344 15103 */ 15104 if (ill->ill_mrtun_refcnt > 0) { 15105 ipaddr_t srcaddr; 15106 ire_t *tmp_ire; 15107 15108 tmp_ire = ire; /* Save, we might need it later */ 15109 if (ire == NULL || (ire->ire_type != IRE_LOCAL && 15110 ire->ire_type != IRE_BROADCAST)) { 15111 srcaddr = ipha->ipha_src; 15112 ire = ire_mrtun_lookup(srcaddr, ill); 15113 if (ire != NULL) { 15114 /* 15115 * Should not be getting iphada packet 15116 * here. we should only get those for 15117 * IRE_LOCAL traffic, excluded above. 15118 * Fail-safe (drop packet) in the event 15119 * hardware is misbehaving. 15120 */ 15121 if (first_mp != mp) { 15122 /* IPsec KSTATS: beancount me */ 15123 freemsg(first_mp); 15124 } else { 15125 /* 15126 * This packet must be forwarded 15127 * to Reverse Tunnel 15128 */ 15129 ip_mrtun_forward(ire, ill, mp); 15130 } 15131 ire_refrele(ire); 15132 ire = NULL; 15133 if (tmp_ire != NULL) { 15134 ire_refrele(tmp_ire); 15135 tmp_ire = NULL; 15136 } 15137 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15138 "ip_input_end: q %p (%S)", 15139 q, "uninit"); 15140 continue; 15141 } 15142 } 15143 /* 15144 * If this packet is from a non-mobilenode or a 15145 * mobile-node which does not request reverse 15146 * tunnel service 15147 */ 15148 ire = tmp_ire; 15149 } 15150 15151 15152 /* 15153 * If we reach here that means the incoming packet satisfies 15154 * one of the following conditions: 15155 * - packet is from a mobile node which does not request 15156 * reverse tunnel 15157 * - packet is from a non-mobile node, which is the most 15158 * common case 15159 * - packet is from a reverse tunnel enabled mobile node 15160 * and destined to foreign agent only 15161 */ 15162 15163 if (ire == NULL) { 15164 /* 15165 * No IRE for this destination, so it can't be for us. 15166 * Unless we are forwarding, drop the packet. 15167 * We have to let source routed packets through 15168 * since we don't yet know if they are 'ping -l' 15169 * packets i.e. if they will go out over the 15170 * same interface as they came in on. 15171 */ 15172 ire = ip_rput_noire(q, NULL, mp, ll_multicast, dst); 15173 if (ire == NULL) 15174 continue; 15175 } 15176 15177 /* 15178 * Broadcast IRE may indicate either broadcast or 15179 * multicast packet 15180 */ 15181 if (ire->ire_type == IRE_BROADCAST) { 15182 /* 15183 * Skip broadcast checks if packet is UDP multicast; 15184 * we'd rather not enter ip_rput_process_broadcast() 15185 * unless the packet is broadcast for real, since 15186 * that routine is a no-op for multicast. 15187 */ 15188 if (ipha->ipha_protocol != IPPROTO_UDP || 15189 !CLASSD(ipha->ipha_dst)) { 15190 ire = ip_rput_process_broadcast(&q, mp, 15191 ire, ipha, ill, dst, cgtp_flt_pkt, 15192 ll_multicast); 15193 if (ire == NULL) 15194 continue; 15195 } 15196 } else if (ire->ire_stq != NULL) { 15197 /* fowarding? */ 15198 ip_rput_process_forward(q, mp, ire, ipha, ill, 15199 ll_multicast); 15200 /* ip_rput_process_forward consumed the packet */ 15201 continue; 15202 } 15203 15204 local: 15205 /* packet not for us */ 15206 if (ire->ire_rfq != q) { 15207 if (ip_rput_notforus(&q, mp, ire, ill)) 15208 continue; 15209 } 15210 15211 switch (ipha->ipha_protocol) { 15212 case IPPROTO_TCP: 15213 ASSERT(first_mp == mp); 15214 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 15215 mp, 0, q, ip_ring)) != NULL) { 15216 if (curr_sqp == NULL) { 15217 curr_sqp = GET_SQUEUE(mp); 15218 ASSERT(cnt == 0); 15219 cnt++; 15220 head = tail = mp; 15221 } else if (curr_sqp == GET_SQUEUE(mp)) { 15222 ASSERT(tail != NULL); 15223 cnt++; 15224 tail->b_next = mp; 15225 tail = mp; 15226 } else { 15227 /* 15228 * A different squeue. Send the 15229 * chain for the previous squeue on 15230 * its way. This shouldn't happen 15231 * often unless interrupt binding 15232 * changes. 15233 */ 15234 IP_STAT(ip_input_multi_squeue); 15235 squeue_enter_chain(curr_sqp, head, 15236 tail, cnt, SQTAG_IP_INPUT); 15237 curr_sqp = GET_SQUEUE(mp); 15238 head = mp; 15239 tail = mp; 15240 cnt = 1; 15241 } 15242 } 15243 continue; 15244 case IPPROTO_UDP: 15245 ASSERT(first_mp == mp); 15246 ip_udp_input(q, mp, ipha, ire, ill); 15247 continue; 15248 case IPPROTO_SCTP: 15249 ASSERT(first_mp == mp); 15250 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 15251 q, dst); 15252 /* ire has been released by ip_sctp_input */ 15253 ire = NULL; 15254 continue; 15255 default: 15256 ip_proto_input(q, first_mp, ipha, ire, ill); 15257 continue; 15258 } 15259 } 15260 15261 if (ire != NULL) 15262 ire_refrele(ire); 15263 15264 if (head != NULL) 15265 squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); 15266 15267 /* 15268 * This code is there just to make netperf/ttcp look good. 15269 * 15270 * Its possible that after being in polling mode (and having cleared 15271 * the backlog), squeues have turned the interrupt frequency higher 15272 * to improve latency at the expense of more CPU utilization (less 15273 * packets per interrupts or more number of interrupts). Workloads 15274 * like ttcp/netperf do manage to tickle polling once in a while 15275 * but for the remaining time, stay in higher interrupt mode since 15276 * their packet arrival rate is pretty uniform and this shows up 15277 * as higher CPU utilization. Since people care about CPU utilization 15278 * while running netperf/ttcp, turn the interrupt frequency back to 15279 * normal/default if polling has not been used in ip_poll_normal_ticks. 15280 */ 15281 if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { 15282 if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { 15283 ip_ring->rr_poll_state &= ~ILL_POLLING; 15284 ip_ring->rr_blank(ip_ring->rr_handle, 15285 ip_ring->rr_normal_blank_time, 15286 ip_ring->rr_normal_pkt_cnt); 15287 } 15288 } 15289 15290 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 15291 "ip_input_end: q %p (%S)", q, "end"); 15292 #undef rptr 15293 } 15294 15295 static void 15296 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 15297 t_uscalar_t err) 15298 { 15299 if (dl_err == DL_SYSERR) { 15300 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15301 "%s: %s failed: DL_SYSERR (errno %u)\n", 15302 ill->ill_name, dlpi_prim_str(prim), err); 15303 return; 15304 } 15305 15306 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 15307 "%s: %s failed: %s\n", ill->ill_name, dlpi_prim_str(prim), 15308 dlpi_err_str(dl_err)); 15309 } 15310 15311 /* 15312 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 15313 * than DL_UNITDATA_IND messages. If we need to process this message 15314 * exclusively, we call qwriter_ip, in which case we also need to call 15315 * ill_refhold before that, since qwriter_ip does an ill_refrele. 15316 */ 15317 void 15318 ip_rput_dlpi(queue_t *q, mblk_t *mp) 15319 { 15320 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15321 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15322 ill_t *ill; 15323 15324 ip1dbg(("ip_rput_dlpi")); 15325 ill = (ill_t *)q->q_ptr; 15326 switch (dloa->dl_primitive) { 15327 case DL_ERROR_ACK: 15328 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK %s (0x%x): " 15329 "%s (0x%x), unix %u\n", ill->ill_name, 15330 dlpi_prim_str(dlea->dl_error_primitive), 15331 dlea->dl_error_primitive, 15332 dlpi_err_str(dlea->dl_errno), 15333 dlea->dl_errno, 15334 dlea->dl_unix_errno)); 15335 switch (dlea->dl_error_primitive) { 15336 case DL_UNBIND_REQ: 15337 mutex_enter(&ill->ill_lock); 15338 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15339 cv_signal(&ill->ill_cv); 15340 mutex_exit(&ill->ill_lock); 15341 /* FALLTHRU */ 15342 case DL_NOTIFY_REQ: 15343 case DL_ATTACH_REQ: 15344 case DL_DETACH_REQ: 15345 case DL_INFO_REQ: 15346 case DL_BIND_REQ: 15347 case DL_ENABMULTI_REQ: 15348 case DL_PHYS_ADDR_REQ: 15349 case DL_CAPABILITY_REQ: 15350 case DL_CONTROL_REQ: 15351 /* 15352 * Refhold the ill to match qwriter_ip which does a 15353 * refrele. Since this is on the ill stream we 15354 * unconditionally bump up the refcount without 15355 * checking for ILL_CAN_LOOKUP 15356 */ 15357 ill_refhold(ill); 15358 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15359 CUR_OP, B_FALSE); 15360 return; 15361 case DL_DISABMULTI_REQ: 15362 freemsg(mp); /* Don't want to pass this up */ 15363 return; 15364 default: 15365 break; 15366 } 15367 ip_dlpi_error(ill, dlea->dl_error_primitive, 15368 dlea->dl_errno, dlea->dl_unix_errno); 15369 freemsg(mp); 15370 return; 15371 case DL_INFO_ACK: 15372 case DL_BIND_ACK: 15373 case DL_PHYS_ADDR_ACK: 15374 case DL_NOTIFY_ACK: 15375 case DL_CAPABILITY_ACK: 15376 case DL_CONTROL_ACK: 15377 /* 15378 * Refhold the ill to match qwriter_ip which does a refrele 15379 * Since this is on the ill stream we unconditionally 15380 * bump up the refcount without doing ILL_CAN_LOOKUP. 15381 */ 15382 ill_refhold(ill); 15383 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15384 CUR_OP, B_FALSE); 15385 return; 15386 case DL_NOTIFY_IND: 15387 ill_refhold(ill); 15388 /* 15389 * The DL_NOTIFY_IND is an asynchronous message that has no 15390 * relation to the current ioctl in progress (if any). Hence we 15391 * pass in NEW_OP in this case. 15392 */ 15393 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15394 NEW_OP, B_FALSE); 15395 return; 15396 case DL_OK_ACK: 15397 ip1dbg(("ip_rput: DL_OK_ACK for %s\n", 15398 dlpi_prim_str((int)dloa->dl_correct_primitive))); 15399 switch (dloa->dl_correct_primitive) { 15400 case DL_UNBIND_REQ: 15401 mutex_enter(&ill->ill_lock); 15402 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 15403 cv_signal(&ill->ill_cv); 15404 mutex_exit(&ill->ill_lock); 15405 /* FALLTHRU */ 15406 case DL_ATTACH_REQ: 15407 case DL_DETACH_REQ: 15408 /* 15409 * Refhold the ill to match qwriter_ip which does a 15410 * refrele. Since this is on the ill stream we 15411 * unconditionally bump up the refcount 15412 */ 15413 ill_refhold(ill); 15414 qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 15415 CUR_OP, B_FALSE); 15416 return; 15417 case DL_ENABMULTI_REQ: 15418 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15419 ill->ill_dlpi_multicast_state = IDS_OK; 15420 break; 15421 15422 } 15423 break; 15424 default: 15425 break; 15426 } 15427 freemsg(mp); 15428 } 15429 15430 /* 15431 * Handling of DLPI messages that require exclusive access to the ipsq. 15432 * 15433 * Need to do ill_pending_mp_release on ioctl completion, which could 15434 * happen here. (along with mi_copy_done) 15435 */ 15436 /* ARGSUSED */ 15437 static void 15438 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 15439 { 15440 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 15441 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 15442 int err = 0; 15443 ill_t *ill; 15444 ipif_t *ipif = NULL; 15445 mblk_t *mp1 = NULL; 15446 conn_t *connp = NULL; 15447 t_uscalar_t paddrreq; 15448 mblk_t *mp_hw; 15449 boolean_t success; 15450 boolean_t ioctl_aborted = B_FALSE; 15451 boolean_t log = B_TRUE; 15452 hook_nic_event_t *info; 15453 15454 ip1dbg(("ip_rput_dlpi_writer ..")); 15455 ill = (ill_t *)q->q_ptr; 15456 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 15457 15458 ASSERT(IAM_WRITER_ILL(ill)); 15459 15460 /* 15461 * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. 15462 * both are null or non-null. However we can assert that only 15463 * after grabbing the ipsq_lock. So we don't make any assertion 15464 * here and in other places in the code. 15465 */ 15466 ipif = ipsq->ipsq_pending_ipif; 15467 /* 15468 * The current ioctl could have been aborted by the user and a new 15469 * ioctl to bring up another ill could have started. We could still 15470 * get a response from the driver later. 15471 */ 15472 if (ipif != NULL && ipif->ipif_ill != ill) 15473 ioctl_aborted = B_TRUE; 15474 15475 switch (dloa->dl_primitive) { 15476 case DL_ERROR_ACK: 15477 switch (dlea->dl_error_primitive) { 15478 case DL_UNBIND_REQ: 15479 case DL_ATTACH_REQ: 15480 case DL_DETACH_REQ: 15481 case DL_INFO_REQ: 15482 ill_dlpi_done(ill, dlea->dl_error_primitive); 15483 break; 15484 case DL_NOTIFY_REQ: 15485 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15486 log = B_FALSE; 15487 break; 15488 case DL_PHYS_ADDR_REQ: 15489 /* 15490 * For IPv6 only, there are two additional 15491 * phys_addr_req's sent to the driver to get the 15492 * IPv6 token and lla. This allows IP to acquire 15493 * the hardware address format for a given interface 15494 * without having built in knowledge of the hardware 15495 * address. ill_phys_addr_pend keeps track of the last 15496 * DL_PAR sent so we know which response we are 15497 * dealing with. ill_dlpi_done will update 15498 * ill_phys_addr_pend when it sends the next req. 15499 * We don't complete the IOCTL until all three DL_PARs 15500 * have been attempted, so set *_len to 0 and break. 15501 */ 15502 paddrreq = ill->ill_phys_addr_pend; 15503 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 15504 if (paddrreq == DL_IPV6_TOKEN) { 15505 ill->ill_token_length = 0; 15506 log = B_FALSE; 15507 break; 15508 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 15509 ill->ill_nd_lla_len = 0; 15510 log = B_FALSE; 15511 break; 15512 } 15513 /* 15514 * Something went wrong with the DL_PHYS_ADDR_REQ. 15515 * We presumably have an IOCTL hanging out waiting 15516 * for completion. Find it and complete the IOCTL 15517 * with the error noted. 15518 * However, ill_dl_phys was called on an ill queue 15519 * (from SIOCSLIFNAME), thus conn_pending_ill is not 15520 * set. But the ioctl is known to be pending on ill_wq. 15521 */ 15522 if (!ill->ill_ifname_pending) 15523 break; 15524 ill->ill_ifname_pending = 0; 15525 if (!ioctl_aborted) 15526 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15527 if (mp1 != NULL) { 15528 /* 15529 * This operation (SIOCSLIFNAME) must have 15530 * happened on the ill. Assert there is no conn 15531 */ 15532 ASSERT(connp == NULL); 15533 q = ill->ill_wq; 15534 } 15535 break; 15536 case DL_BIND_REQ: 15537 ill_dlpi_done(ill, DL_BIND_REQ); 15538 if (ill->ill_ifname_pending) 15539 break; 15540 /* 15541 * Something went wrong with the bind. We presumably 15542 * have an IOCTL hanging out waiting for completion. 15543 * Find it, take down the interface that was coming 15544 * up, and complete the IOCTL with the error noted. 15545 */ 15546 if (!ioctl_aborted) 15547 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15548 if (mp1 != NULL) { 15549 /* 15550 * This operation (SIOCSLIFFLAGS) must have 15551 * happened from a conn. 15552 */ 15553 ASSERT(connp != NULL); 15554 q = CONNP_TO_WQ(connp); 15555 if (ill->ill_move_in_progress) { 15556 ILL_CLEAR_MOVE(ill); 15557 } 15558 (void) ipif_down(ipif, NULL, NULL); 15559 /* error is set below the switch */ 15560 } 15561 break; 15562 case DL_ENABMULTI_REQ: 15563 ip1dbg(("DL_ERROR_ACK to enabmulti\n")); 15564 15565 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 15566 ill->ill_dlpi_multicast_state = IDS_FAILED; 15567 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 15568 ipif_t *ipif; 15569 15570 log = B_FALSE; 15571 printf("ip: joining multicasts failed (%d)" 15572 " on %s - will use link layer " 15573 "broadcasts for multicast\n", 15574 dlea->dl_errno, ill->ill_name); 15575 15576 /* 15577 * Set up the multicast mapping alone. 15578 * writer, so ok to access ill->ill_ipif 15579 * without any lock. 15580 */ 15581 ipif = ill->ill_ipif; 15582 mutex_enter(&ill->ill_phyint->phyint_lock); 15583 ill->ill_phyint->phyint_flags |= 15584 PHYI_MULTI_BCAST; 15585 mutex_exit(&ill->ill_phyint->phyint_lock); 15586 15587 if (!ill->ill_isv6) { 15588 (void) ipif_arp_setup_multicast(ipif, 15589 NULL); 15590 } else { 15591 (void) ipif_ndp_setup_multicast(ipif, 15592 NULL); 15593 } 15594 } 15595 freemsg(mp); /* Don't want to pass this up */ 15596 return; 15597 case DL_CAPABILITY_REQ: 15598 case DL_CONTROL_REQ: 15599 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 15600 "DL_CAPABILITY/CONTROL REQ\n")); 15601 ill_dlpi_done(ill, dlea->dl_error_primitive); 15602 ill->ill_dlpi_capab_state = IDS_FAILED; 15603 freemsg(mp); 15604 return; 15605 } 15606 /* 15607 * Note the error for IOCTL completion (mp1 is set when 15608 * ready to complete ioctl). If ill_ifname_pending_err is 15609 * set, an error occured during plumbing (ill_ifname_pending), 15610 * so we want to report that error. 15611 * 15612 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 15613 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 15614 * expected to get errack'd if the driver doesn't support 15615 * these flags (e.g. ethernet). log will be set to B_FALSE 15616 * if these error conditions are encountered. 15617 */ 15618 if (mp1 != NULL) { 15619 if (ill->ill_ifname_pending_err != 0) { 15620 err = ill->ill_ifname_pending_err; 15621 ill->ill_ifname_pending_err = 0; 15622 } else { 15623 err = dlea->dl_unix_errno ? 15624 dlea->dl_unix_errno : ENXIO; 15625 } 15626 /* 15627 * If we're plumbing an interface and an error hasn't already 15628 * been saved, set ill_ifname_pending_err to the error passed 15629 * up. Ignore the error if log is B_FALSE (see comment above). 15630 */ 15631 } else if (log && ill->ill_ifname_pending && 15632 ill->ill_ifname_pending_err == 0) { 15633 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 15634 dlea->dl_unix_errno : ENXIO; 15635 } 15636 15637 if (log) 15638 ip_dlpi_error(ill, dlea->dl_error_primitive, 15639 dlea->dl_errno, dlea->dl_unix_errno); 15640 break; 15641 case DL_CAPABILITY_ACK: { 15642 boolean_t reneg_flag = B_FALSE; 15643 /* Call a routine to handle this one. */ 15644 ill_dlpi_done(ill, DL_CAPABILITY_REQ); 15645 /* 15646 * Check if the ACK is due to renegotiation case since we 15647 * will need to send a new CAPABILITY_REQ later. 15648 */ 15649 if (ill->ill_dlpi_capab_state == IDS_RENEG) { 15650 /* This is the ack for a renogiation case */ 15651 reneg_flag = B_TRUE; 15652 ill->ill_dlpi_capab_state = IDS_UNKNOWN; 15653 } 15654 ill_capability_ack(ill, mp); 15655 if (reneg_flag) 15656 ill_capability_probe(ill); 15657 break; 15658 } 15659 case DL_CONTROL_ACK: 15660 /* We treat all of these as "fire and forget" */ 15661 ill_dlpi_done(ill, DL_CONTROL_REQ); 15662 break; 15663 case DL_INFO_ACK: 15664 /* Call a routine to handle this one. */ 15665 ill_dlpi_done(ill, DL_INFO_REQ); 15666 ip_ll_subnet_defaults(ill, mp); 15667 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 15668 return; 15669 case DL_BIND_ACK: 15670 /* 15671 * We should have an IOCTL waiting on this unless 15672 * sent by ill_dl_phys, in which case just return 15673 */ 15674 ill_dlpi_done(ill, DL_BIND_REQ); 15675 if (ill->ill_ifname_pending) 15676 break; 15677 15678 if (!ioctl_aborted) 15679 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15680 if (mp1 == NULL) 15681 break; 15682 /* 15683 * Because mp1 was added by ill_dl_up(), and it always 15684 * passes a valid connp, connp must be valid here. 15685 */ 15686 ASSERT(connp != NULL); 15687 q = CONNP_TO_WQ(connp); 15688 15689 /* 15690 * We are exclusive. So nothing can change even after 15691 * we get the pending mp. If need be we can put it back 15692 * and restart, as in calling ipif_arp_up() below. 15693 */ 15694 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 15695 15696 mutex_enter(&ill->ill_lock); 15697 15698 ill->ill_dl_up = 1; 15699 15700 if ((info = ill->ill_nic_event_info) != NULL) { 15701 ip2dbg(("ip_rput_dlpi_writer: unexpected nic event %d " 15702 "attached for %s\n", info->hne_event, 15703 ill->ill_name)); 15704 if (info->hne_data != NULL) 15705 kmem_free(info->hne_data, info->hne_datalen); 15706 kmem_free(info, sizeof (hook_nic_event_t)); 15707 } 15708 15709 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 15710 if (info != NULL) { 15711 info->hne_nic = ill->ill_phyint->phyint_ifindex; 15712 info->hne_lif = 0; 15713 info->hne_event = NE_UP; 15714 info->hne_data = NULL; 15715 info->hne_datalen = 0; 15716 info->hne_family = ill->ill_isv6 ? ipv6 : ipv4; 15717 } else 15718 ip2dbg(("ip_rput_dlpi_writer: could not attach UP nic " 15719 "event information for %s (ENOMEM)\n", 15720 ill->ill_name)); 15721 15722 ill->ill_nic_event_info = info; 15723 15724 mutex_exit(&ill->ill_lock); 15725 15726 /* 15727 * Now bring up the resolver; when that is complete, we'll 15728 * create IREs. Note that we intentionally mirror what 15729 * ipif_up() would have done, because we got here by way of 15730 * ill_dl_up(), which stopped ipif_up()'s processing. 15731 */ 15732 if (ill->ill_isv6) { 15733 /* 15734 * v6 interfaces. 15735 * Unlike ARP which has to do another bind 15736 * and attach, once we get here we are 15737 * done with NDP. Except in the case of 15738 * ILLF_XRESOLV, in which case we send an 15739 * AR_INTERFACE_UP to the external resolver. 15740 * If all goes well, the ioctl will complete 15741 * in ip_rput(). If there's an error, we 15742 * complete it here. 15743 */ 15744 err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr); 15745 if (err == 0) { 15746 if (ill->ill_flags & ILLF_XRESOLV) { 15747 mutex_enter(&connp->conn_lock); 15748 mutex_enter(&ill->ill_lock); 15749 success = ipsq_pending_mp_add( 15750 connp, ipif, q, mp1, 0); 15751 mutex_exit(&ill->ill_lock); 15752 mutex_exit(&connp->conn_lock); 15753 if (success) { 15754 err = ipif_resolver_up(ipif, 15755 Res_act_initial); 15756 if (err == EINPROGRESS) { 15757 freemsg(mp); 15758 return; 15759 } 15760 ASSERT(err != 0); 15761 mp1 = ipsq_pending_mp_get(ipsq, 15762 &connp); 15763 ASSERT(mp1 != NULL); 15764 } else { 15765 /* conn has started closing */ 15766 err = EINTR; 15767 } 15768 } else { /* Non XRESOLV interface */ 15769 (void) ipif_resolver_up(ipif, 15770 Res_act_initial); 15771 err = ipif_up_done_v6(ipif); 15772 } 15773 } 15774 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 15775 /* 15776 * ARP and other v4 external resolvers. 15777 * Leave the pending mblk intact so that 15778 * the ioctl completes in ip_rput(). 15779 */ 15780 mutex_enter(&connp->conn_lock); 15781 mutex_enter(&ill->ill_lock); 15782 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 15783 mutex_exit(&ill->ill_lock); 15784 mutex_exit(&connp->conn_lock); 15785 if (success) { 15786 err = ipif_resolver_up(ipif, Res_act_initial); 15787 if (err == EINPROGRESS) { 15788 freemsg(mp); 15789 return; 15790 } 15791 ASSERT(err != 0); 15792 mp1 = ipsq_pending_mp_get(ipsq, &connp); 15793 } else { 15794 /* The conn has started closing */ 15795 err = EINTR; 15796 } 15797 } else { 15798 /* 15799 * This one is complete. Reply to pending ioctl. 15800 */ 15801 (void) ipif_resolver_up(ipif, Res_act_initial); 15802 err = ipif_up_done(ipif); 15803 } 15804 15805 if ((err == 0) && (ill->ill_up_ipifs)) { 15806 err = ill_up_ipifs(ill, q, mp1); 15807 if (err == EINPROGRESS) { 15808 freemsg(mp); 15809 return; 15810 } 15811 } 15812 15813 if (ill->ill_up_ipifs) { 15814 ill_group_cleanup(ill); 15815 } 15816 15817 break; 15818 case DL_NOTIFY_IND: { 15819 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 15820 ire_t *ire; 15821 boolean_t need_ire_walk_v4 = B_FALSE; 15822 boolean_t need_ire_walk_v6 = B_FALSE; 15823 15824 switch (notify->dl_notification) { 15825 case DL_NOTE_PHYS_ADDR: 15826 err = ill_set_phys_addr(ill, mp); 15827 break; 15828 15829 case DL_NOTE_FASTPATH_FLUSH: 15830 ill_fastpath_flush(ill); 15831 break; 15832 15833 case DL_NOTE_SDU_SIZE: 15834 /* 15835 * Change the MTU size of the interface, of all 15836 * attached ipif's, and of all relevant ire's. The 15837 * new value's a uint32_t at notify->dl_data. 15838 * Mtu change Vs. new ire creation - protocol below. 15839 * 15840 * a Mark the ipif as IPIF_CHANGING. 15841 * b Set the new mtu in the ipif. 15842 * c Change the ire_max_frag on all affected ires 15843 * d Unmark the IPIF_CHANGING 15844 * 15845 * To see how the protocol works, assume an interface 15846 * route is also being added simultaneously by 15847 * ip_rt_add and let 'ipif' be the ipif referenced by 15848 * the ire. If the ire is created before step a, 15849 * it will be cleaned up by step c. If the ire is 15850 * created after step d, it will see the new value of 15851 * ipif_mtu. Any attempt to create the ire between 15852 * steps a to d will fail because of the IPIF_CHANGING 15853 * flag. Note that ire_create() is passed a pointer to 15854 * the ipif_mtu, and not the value. During ire_add 15855 * under the bucket lock, the ire_max_frag of the 15856 * new ire being created is set from the ipif/ire from 15857 * which it is being derived. 15858 */ 15859 mutex_enter(&ill->ill_lock); 15860 ill->ill_max_frag = (uint_t)notify->dl_data; 15861 15862 /* 15863 * If an SIOCSLIFLNKINFO has changed the ill_max_mtu 15864 * leave it alone 15865 */ 15866 if (ill->ill_mtu_userspecified) { 15867 mutex_exit(&ill->ill_lock); 15868 break; 15869 } 15870 ill->ill_max_mtu = ill->ill_max_frag; 15871 if (ill->ill_isv6) { 15872 if (ill->ill_max_mtu < IPV6_MIN_MTU) 15873 ill->ill_max_mtu = IPV6_MIN_MTU; 15874 } else { 15875 if (ill->ill_max_mtu < IP_MIN_MTU) 15876 ill->ill_max_mtu = IP_MIN_MTU; 15877 } 15878 for (ipif = ill->ill_ipif; ipif != NULL; 15879 ipif = ipif->ipif_next) { 15880 /* 15881 * Don't override the mtu if the user 15882 * has explicitly set it. 15883 */ 15884 if (ipif->ipif_flags & IPIF_FIXEDMTU) 15885 continue; 15886 ipif->ipif_mtu = (uint_t)notify->dl_data; 15887 if (ipif->ipif_isv6) 15888 ire = ipif_to_ire_v6(ipif); 15889 else 15890 ire = ipif_to_ire(ipif); 15891 if (ire != NULL) { 15892 ire->ire_max_frag = ipif->ipif_mtu; 15893 ire_refrele(ire); 15894 } 15895 if (ipif->ipif_flags & IPIF_UP) { 15896 if (ill->ill_isv6) 15897 need_ire_walk_v6 = B_TRUE; 15898 else 15899 need_ire_walk_v4 = B_TRUE; 15900 } 15901 } 15902 mutex_exit(&ill->ill_lock); 15903 if (need_ire_walk_v4) 15904 ire_walk_v4(ill_mtu_change, (char *)ill, 15905 ALL_ZONES); 15906 if (need_ire_walk_v6) 15907 ire_walk_v6(ill_mtu_change, (char *)ill, 15908 ALL_ZONES); 15909 break; 15910 case DL_NOTE_LINK_UP: 15911 case DL_NOTE_LINK_DOWN: { 15912 /* 15913 * We are writer. ill / phyint / ipsq assocs stable. 15914 * The RUNNING flag reflects the state of the link. 15915 */ 15916 phyint_t *phyint = ill->ill_phyint; 15917 uint64_t new_phyint_flags; 15918 boolean_t changed = B_FALSE; 15919 boolean_t went_up; 15920 15921 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 15922 mutex_enter(&phyint->phyint_lock); 15923 new_phyint_flags = went_up ? 15924 phyint->phyint_flags | PHYI_RUNNING : 15925 phyint->phyint_flags & ~PHYI_RUNNING; 15926 if (new_phyint_flags != phyint->phyint_flags) { 15927 phyint->phyint_flags = new_phyint_flags; 15928 changed = B_TRUE; 15929 } 15930 mutex_exit(&phyint->phyint_lock); 15931 /* 15932 * ill_restart_dad handles the DAD restart and routing 15933 * socket notification logic. 15934 */ 15935 if (changed) { 15936 ill_restart_dad(phyint->phyint_illv4, went_up); 15937 ill_restart_dad(phyint->phyint_illv6, went_up); 15938 } 15939 break; 15940 } 15941 case DL_NOTE_PROMISC_ON_PHYS: 15942 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 15943 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 15944 mutex_enter(&ill->ill_lock); 15945 ill->ill_promisc_on_phys = B_TRUE; 15946 mutex_exit(&ill->ill_lock); 15947 break; 15948 case DL_NOTE_PROMISC_OFF_PHYS: 15949 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 15950 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 15951 mutex_enter(&ill->ill_lock); 15952 ill->ill_promisc_on_phys = B_FALSE; 15953 mutex_exit(&ill->ill_lock); 15954 break; 15955 case DL_NOTE_CAPAB_RENEG: 15956 /* 15957 * Something changed on the driver side. 15958 * It wants us to renegotiate the capabilities 15959 * on this ill. The most likely cause is the 15960 * aggregation interface under us where a 15961 * port got added or went away. 15962 * 15963 * We reset the capabilities and set the 15964 * state to IDS_RENG so that when the ack 15965 * comes back, we can start the 15966 * renegotiation process. 15967 */ 15968 ill_capability_reset(ill); 15969 ill->ill_dlpi_capab_state = IDS_RENEG; 15970 break; 15971 default: 15972 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 15973 "type 0x%x for DL_NOTIFY_IND\n", 15974 notify->dl_notification)); 15975 break; 15976 } 15977 15978 /* 15979 * As this is an asynchronous operation, we 15980 * should not call ill_dlpi_done 15981 */ 15982 break; 15983 } 15984 case DL_NOTIFY_ACK: { 15985 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 15986 15987 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 15988 ill->ill_note_link = 1; 15989 ill_dlpi_done(ill, DL_NOTIFY_REQ); 15990 break; 15991 } 15992 case DL_PHYS_ADDR_ACK: { 15993 /* 15994 * As part of plumbing the interface via SIOCSLIFNAME, 15995 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, 15996 * whose answers we receive here. As each answer is received, 15997 * we call ill_dlpi_done() to dispatch the next request as 15998 * we're processing the current one. Once all answers have 15999 * been received, we use ipsq_pending_mp_get() to dequeue the 16000 * outstanding IOCTL and reply to it. (Because ill_dl_phys() 16001 * is invoked from an ill queue, conn_oper_pending_ill is not 16002 * available, but we know the ioctl is pending on ill_wq.) 16003 */ 16004 uint_t paddrlen, paddroff; 16005 16006 paddrreq = ill->ill_phys_addr_pend; 16007 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; 16008 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; 16009 16010 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 16011 if (paddrreq == DL_IPV6_TOKEN) { 16012 /* 16013 * bcopy to low-order bits of ill_token 16014 * 16015 * XXX Temporary hack - currently, all known tokens 16016 * are 64 bits, so I'll cheat for the moment. 16017 */ 16018 bcopy(mp->b_rptr + paddroff, 16019 &ill->ill_token.s6_addr32[2], paddrlen); 16020 ill->ill_token_length = paddrlen; 16021 break; 16022 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 16023 ASSERT(ill->ill_nd_lla_mp == NULL); 16024 ill_set_ndmp(ill, mp, paddroff, paddrlen); 16025 mp = NULL; 16026 break; 16027 } 16028 16029 ASSERT(paddrreq == DL_CURR_PHYS_ADDR); 16030 ASSERT(ill->ill_phys_addr_mp == NULL); 16031 if (!ill->ill_ifname_pending) 16032 break; 16033 ill->ill_ifname_pending = 0; 16034 if (!ioctl_aborted) 16035 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16036 if (mp1 != NULL) { 16037 ASSERT(connp == NULL); 16038 q = ill->ill_wq; 16039 } 16040 /* 16041 * If any error acks received during the plumbing sequence, 16042 * ill_ifname_pending_err will be set. Break out and send up 16043 * the error to the pending ioctl. 16044 */ 16045 if (ill->ill_ifname_pending_err != 0) { 16046 err = ill->ill_ifname_pending_err; 16047 ill->ill_ifname_pending_err = 0; 16048 break; 16049 } 16050 16051 ill->ill_phys_addr_mp = mp; 16052 ill->ill_phys_addr = mp->b_rptr + paddroff; 16053 mp = NULL; 16054 16055 /* 16056 * If paddrlen is zero, the DLPI provider doesn't support 16057 * physical addresses. The other two tests were historical 16058 * workarounds for bugs in our former PPP implementation, but 16059 * now other things have grown dependencies on them -- e.g., 16060 * the tun module specifies a dl_addr_length of zero in its 16061 * DL_BIND_ACK, but then specifies an incorrect value in its 16062 * DL_PHYS_ADDR_ACK. These bogus checks need to be removed, 16063 * but only after careful testing ensures that all dependent 16064 * broken DLPI providers have been fixed. 16065 */ 16066 if (paddrlen == 0 || ill->ill_phys_addr_length == 0 || 16067 ill->ill_phys_addr_length == IP_ADDR_LEN) { 16068 ill->ill_phys_addr = NULL; 16069 } else if (paddrlen != ill->ill_phys_addr_length) { 16070 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", 16071 paddrlen, ill->ill_phys_addr_length)); 16072 err = EINVAL; 16073 break; 16074 } 16075 16076 if (ill->ill_nd_lla_mp == NULL) { 16077 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { 16078 err = ENOMEM; 16079 break; 16080 } 16081 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); 16082 } 16083 16084 /* 16085 * Set the interface token. If the zeroth interface address 16086 * is unspecified, then set it to the link local address. 16087 */ 16088 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 16089 (void) ill_setdefaulttoken(ill); 16090 16091 ASSERT(ill->ill_ipif->ipif_id == 0); 16092 if (ipif != NULL && 16093 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) 16094 (void) ipif_setlinklocal(ipif); 16095 break; 16096 } 16097 case DL_OK_ACK: 16098 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 16099 dlpi_prim_str((int)dloa->dl_correct_primitive), 16100 dloa->dl_correct_primitive)); 16101 switch (dloa->dl_correct_primitive) { 16102 case DL_UNBIND_REQ: 16103 case DL_ATTACH_REQ: 16104 case DL_DETACH_REQ: 16105 ill_dlpi_done(ill, dloa->dl_correct_primitive); 16106 break; 16107 } 16108 break; 16109 default: 16110 break; 16111 } 16112 16113 freemsg(mp); 16114 if (mp1 != NULL) { 16115 /* 16116 * The operation must complete without EINPROGRESS 16117 * since ipsq_pending_mp_get() has removed the mblk 16118 * from ipsq_pending_mp. Otherwise, the operation 16119 * will be stuck forever in the ipsq. 16120 */ 16121 ASSERT(err != EINPROGRESS); 16122 16123 switch (ipsq->ipsq_current_ioctl) { 16124 case 0: 16125 ipsq_current_finish(ipsq); 16126 break; 16127 16128 case SIOCLIFADDIF: 16129 case SIOCSLIFNAME: 16130 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 16131 break; 16132 16133 default: 16134 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 16135 break; 16136 } 16137 } 16138 } 16139 16140 /* 16141 * ip_rput_other is called by ip_rput to handle messages modifying the global 16142 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 16143 */ 16144 /* ARGSUSED */ 16145 void 16146 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 16147 { 16148 ill_t *ill; 16149 struct iocblk *iocp; 16150 mblk_t *mp1; 16151 conn_t *connp = NULL; 16152 16153 ip1dbg(("ip_rput_other ")); 16154 ill = (ill_t *)q->q_ptr; 16155 /* 16156 * This routine is not a writer in the case of SIOCGTUNPARAM 16157 * in which case ipsq is NULL. 16158 */ 16159 if (ipsq != NULL) { 16160 ASSERT(IAM_WRITER_IPSQ(ipsq)); 16161 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 16162 } 16163 16164 switch (mp->b_datap->db_type) { 16165 case M_ERROR: 16166 case M_HANGUP: 16167 /* 16168 * The device has a problem. We force the ILL down. It can 16169 * be brought up again manually using SIOCSIFFLAGS (via 16170 * ifconfig or equivalent). 16171 */ 16172 ASSERT(ipsq != NULL); 16173 if (mp->b_rptr < mp->b_wptr) 16174 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 16175 if (ill->ill_error == 0) 16176 ill->ill_error = ENXIO; 16177 if (!ill_down_start(q, mp)) 16178 return; 16179 ipif_all_down_tail(ipsq, q, mp, NULL); 16180 break; 16181 case M_IOCACK: 16182 iocp = (struct iocblk *)mp->b_rptr; 16183 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 16184 switch (iocp->ioc_cmd) { 16185 case SIOCSTUNPARAM: 16186 case OSIOCSTUNPARAM: 16187 ASSERT(ipsq != NULL); 16188 /* 16189 * Finish socket ioctl passed through to tun. 16190 * We should have an IOCTL waiting on this. 16191 */ 16192 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16193 if (ill->ill_isv6) { 16194 struct iftun_req *ta; 16195 16196 /* 16197 * if a source or destination is 16198 * being set, try and set the link 16199 * local address for the tunnel 16200 */ 16201 ta = (struct iftun_req *)mp->b_cont-> 16202 b_cont->b_rptr; 16203 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 16204 ipif_set_tun_llink(ill, ta); 16205 } 16206 } 16207 if (mp1 != NULL) { 16208 /* 16209 * Now copy back the b_next/b_prev used by 16210 * mi code for the mi_copy* functions. 16211 * See ip_sioctl_tunparam() for the reason. 16212 * Also protect against missing b_cont. 16213 */ 16214 if (mp->b_cont != NULL) { 16215 mp->b_cont->b_next = 16216 mp1->b_cont->b_next; 16217 mp->b_cont->b_prev = 16218 mp1->b_cont->b_prev; 16219 } 16220 inet_freemsg(mp1); 16221 ASSERT(connp != NULL); 16222 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16223 iocp->ioc_error, NO_COPYOUT, ipsq); 16224 } else { 16225 ASSERT(connp == NULL); 16226 putnext(q, mp); 16227 } 16228 break; 16229 case SIOCGTUNPARAM: 16230 case OSIOCGTUNPARAM: 16231 /* 16232 * This is really M_IOCDATA from the tunnel driver. 16233 * convert back and complete the ioctl. 16234 * We should have an IOCTL waiting on this. 16235 */ 16236 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 16237 if (mp1) { 16238 /* 16239 * Now copy back the b_next/b_prev used by 16240 * mi code for the mi_copy* functions. 16241 * See ip_sioctl_tunparam() for the reason. 16242 * Also protect against missing b_cont. 16243 */ 16244 if (mp->b_cont != NULL) { 16245 mp->b_cont->b_next = 16246 mp1->b_cont->b_next; 16247 mp->b_cont->b_prev = 16248 mp1->b_cont->b_prev; 16249 } 16250 inet_freemsg(mp1); 16251 if (iocp->ioc_error == 0) 16252 mp->b_datap->db_type = M_IOCDATA; 16253 ASSERT(connp != NULL); 16254 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16255 iocp->ioc_error, COPYOUT, NULL); 16256 } else { 16257 ASSERT(connp == NULL); 16258 putnext(q, mp); 16259 } 16260 break; 16261 default: 16262 break; 16263 } 16264 break; 16265 case M_IOCNAK: 16266 iocp = (struct iocblk *)mp->b_rptr; 16267 16268 switch (iocp->ioc_cmd) { 16269 int mode; 16270 16271 case DL_IOC_HDR_INFO: 16272 /* 16273 * If this was the first attempt turn of the 16274 * fastpath probing. 16275 */ 16276 mutex_enter(&ill->ill_lock); 16277 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 16278 ill->ill_dlpi_fastpath_state = IDS_FAILED; 16279 mutex_exit(&ill->ill_lock); 16280 ill_fastpath_nack(ill); 16281 ip1dbg(("ip_rput: DLPI fastpath off on " 16282 "interface %s\n", 16283 ill->ill_name)); 16284 } else { 16285 mutex_exit(&ill->ill_lock); 16286 } 16287 freemsg(mp); 16288 break; 16289 case SIOCSTUNPARAM: 16290 case OSIOCSTUNPARAM: 16291 ASSERT(ipsq != NULL); 16292 /* 16293 * Finish socket ioctl passed through to tun 16294 * We should have an IOCTL waiting on this. 16295 */ 16296 /* FALLTHRU */ 16297 case SIOCGTUNPARAM: 16298 case OSIOCGTUNPARAM: 16299 /* 16300 * This is really M_IOCDATA from the tunnel driver. 16301 * convert back and complete the ioctl. 16302 * We should have an IOCTL waiting on this. 16303 */ 16304 if (iocp->ioc_cmd == SIOCGTUNPARAM || 16305 iocp->ioc_cmd == OSIOCGTUNPARAM) { 16306 mp1 = ill_pending_mp_get(ill, &connp, 16307 iocp->ioc_id); 16308 mode = COPYOUT; 16309 ipsq = NULL; 16310 } else { 16311 mp1 = ipsq_pending_mp_get(ipsq, &connp); 16312 mode = NO_COPYOUT; 16313 } 16314 if (mp1 != NULL) { 16315 /* 16316 * Now copy back the b_next/b_prev used by 16317 * mi code for the mi_copy* functions. 16318 * See ip_sioctl_tunparam() for the reason. 16319 * Also protect against missing b_cont. 16320 */ 16321 if (mp->b_cont != NULL) { 16322 mp->b_cont->b_next = 16323 mp1->b_cont->b_next; 16324 mp->b_cont->b_prev = 16325 mp1->b_cont->b_prev; 16326 } 16327 inet_freemsg(mp1); 16328 if (iocp->ioc_error == 0) 16329 iocp->ioc_error = EINVAL; 16330 ASSERT(connp != NULL); 16331 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 16332 iocp->ioc_error, mode, ipsq); 16333 } else { 16334 ASSERT(connp == NULL); 16335 putnext(q, mp); 16336 } 16337 break; 16338 default: 16339 break; 16340 } 16341 default: 16342 break; 16343 } 16344 } 16345 16346 /* 16347 * NOTE : This function does not ire_refrele the ire argument passed in. 16348 * 16349 * IPQoS notes 16350 * IP policy is invoked twice for a forwarded packet, once on the read side 16351 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 16352 * enabled. An additional parameter, in_ill, has been added for this purpose. 16353 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 16354 * because ip_mroute drops this information. 16355 * 16356 */ 16357 void 16358 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 16359 { 16360 uint32_t pkt_len; 16361 queue_t *q; 16362 uint32_t sum; 16363 #define rptr ((uchar_t *)ipha) 16364 uint32_t max_frag; 16365 uint32_t ill_index; 16366 ill_t *out_ill; 16367 mib2_ipIfStatsEntry_t *mibptr; 16368 16369 /* Get the ill_index of the incoming ILL */ 16370 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 16371 mibptr = (in_ill != NULL) ? in_ill->ill_ip_mib : &ip_mib; 16372 16373 /* Initiate Read side IPPF processing */ 16374 if (IPP_ENABLED(IPP_FWD_IN)) { 16375 ip_process(IPP_FWD_IN, &mp, ill_index); 16376 if (mp == NULL) { 16377 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 16378 "during IPPF processing\n")); 16379 return; 16380 } 16381 } 16382 16383 pkt_len = ntohs(ipha->ipha_length); 16384 16385 /* Adjust the checksum to reflect the ttl decrement. */ 16386 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 16387 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 16388 16389 if (ipha->ipha_ttl-- <= 1) { 16390 if (ip_csum_hdr(ipha)) { 16391 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16392 goto drop_pkt; 16393 } 16394 /* 16395 * Note: ire_stq this will be NULL for multicast 16396 * datagrams using the long path through arp (the IRE 16397 * is not an IRE_CACHE). This should not cause 16398 * problems since we don't generate ICMP errors for 16399 * multicast packets. 16400 */ 16401 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16402 q = ire->ire_stq; 16403 if (q != NULL) { 16404 /* Sent by forwarding path, and router is global zone */ 16405 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, 16406 GLOBAL_ZONEID); 16407 } else 16408 freemsg(mp); 16409 return; 16410 } 16411 16412 /* 16413 * Don't forward if the interface is down 16414 */ 16415 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 16416 BUMP_MIB(mibptr, ipIfStatsInDiscards); 16417 ip2dbg(("ip_rput_forward:interface is down\n")); 16418 goto drop_pkt; 16419 } 16420 16421 /* Get the ill_index of the outgoing ILL */ 16422 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 16423 16424 out_ill = ire->ire_ipif->ipif_ill; 16425 16426 DTRACE_PROBE4(ip4__forwarding__start, 16427 ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16428 16429 FW_HOOKS(ip4_forwarding_event, ipv4firewall_forwarding, 16430 in_ill, out_ill, ipha, mp, mp); 16431 16432 DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); 16433 16434 if (mp == NULL) 16435 return; 16436 pkt_len = ntohs(ipha->ipha_length); 16437 16438 if (is_system_labeled()) { 16439 mblk_t *mp1; 16440 16441 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 16442 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16443 goto drop_pkt; 16444 } 16445 /* Size may have changed */ 16446 mp = mp1; 16447 ipha = (ipha_t *)mp->b_rptr; 16448 pkt_len = ntohs(ipha->ipha_length); 16449 } 16450 16451 /* Check if there are options to update */ 16452 if (!IS_SIMPLE_IPH(ipha)) { 16453 if (ip_csum_hdr(ipha)) { 16454 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16455 goto drop_pkt; 16456 } 16457 if (ip_rput_forward_options(mp, ipha, ire)) { 16458 BUMP_MIB(mibptr, ipIfStatsForwProhibits); 16459 return; 16460 } 16461 16462 ipha->ipha_hdr_checksum = 0; 16463 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 16464 } 16465 max_frag = ire->ire_max_frag; 16466 if (pkt_len > max_frag) { 16467 /* 16468 * It needs fragging on its way out. We haven't 16469 * verified the header checksum yet. Since we 16470 * are going to put a surely good checksum in the 16471 * outgoing header, we have to make sure that it 16472 * was good coming in. 16473 */ 16474 if (ip_csum_hdr(ipha)) { 16475 BUMP_MIB(mibptr, ipIfStatsInCksumErrs); 16476 goto drop_pkt; 16477 } 16478 /* Initiate Write side IPPF processing */ 16479 if (IPP_ENABLED(IPP_FWD_OUT)) { 16480 ip_process(IPP_FWD_OUT, &mp, ill_index); 16481 if (mp == NULL) { 16482 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 16483 " during IPPF processing\n")); 16484 return; 16485 } 16486 } 16487 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID); 16488 ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); 16489 return; 16490 } 16491 16492 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 16493 ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); 16494 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 16495 NULL, out_ill, ipha, mp, mp); 16496 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 16497 if (mp == NULL) 16498 return; 16499 16500 mp->b_prev = (mblk_t *)IPP_FWD_OUT; 16501 ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); 16502 (void) ip_xmit_v4(mp, ire, NULL, B_FALSE); 16503 /* ip_xmit_v4 always consumes the packet */ 16504 return; 16505 16506 drop_pkt:; 16507 ip1dbg(("ip_rput_forward: drop pkt\n")); 16508 freemsg(mp); 16509 #undef rptr 16510 } 16511 16512 void 16513 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 16514 { 16515 ire_t *ire; 16516 16517 ASSERT(!ipif->ipif_isv6); 16518 /* 16519 * Find an IRE which matches the destination and the outgoing 16520 * queue in the cache table. All we need is an IRE_CACHE which 16521 * is pointing at ipif->ipif_ill. If it is part of some ill group, 16522 * then it is enough to have some IRE_CACHE in the group. 16523 */ 16524 if (ipif->ipif_flags & IPIF_POINTOPOINT) 16525 dst = ipif->ipif_pp_dst_addr; 16526 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MBLK_GETLABEL(mp), 16527 MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR); 16528 if (ire == NULL) { 16529 /* 16530 * Mark this packet to make it be delivered to 16531 * ip_rput_forward after the new ire has been 16532 * created. 16533 */ 16534 mp->b_prev = NULL; 16535 mp->b_next = mp; 16536 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 16537 NULL, 0, GLOBAL_ZONEID, &zero_info); 16538 } else { 16539 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 16540 IRE_REFRELE(ire); 16541 } 16542 } 16543 16544 /* Update any source route, record route or timestamp options */ 16545 static int 16546 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire) 16547 { 16548 ipoptp_t opts; 16549 uchar_t *opt; 16550 uint8_t optval; 16551 uint8_t optlen; 16552 ipaddr_t dst; 16553 uint32_t ts; 16554 ire_t *dst_ire = NULL; 16555 ire_t *tmp_ire = NULL; 16556 timestruc_t now; 16557 16558 ip2dbg(("ip_rput_forward_options\n")); 16559 dst = ipha->ipha_dst; 16560 for (optval = ipoptp_first(&opts, ipha); 16561 optval != IPOPT_EOL; 16562 optval = ipoptp_next(&opts)) { 16563 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 16564 opt = opts.ipoptp_cur; 16565 optlen = opts.ipoptp_len; 16566 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 16567 optval, opts.ipoptp_len)); 16568 switch (optval) { 16569 uint32_t off; 16570 case IPOPT_SSRR: 16571 case IPOPT_LSRR: 16572 /* Check if adminstratively disabled */ 16573 if (!ip_forward_src_routed) { 16574 if (ire->ire_stq != NULL) { 16575 /* 16576 * Sent by forwarding path, and router 16577 * is global zone 16578 */ 16579 icmp_unreachable(ire->ire_stq, mp, 16580 ICMP_SOURCE_ROUTE_FAILED, 16581 GLOBAL_ZONEID); 16582 } else { 16583 ip0dbg(("ip_rput_forward_options: " 16584 "unable to send unreach\n")); 16585 freemsg(mp); 16586 } 16587 return (-1); 16588 } 16589 16590 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16591 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 16592 if (dst_ire == NULL) { 16593 /* 16594 * Must be partial since ip_rput_options 16595 * checked for strict. 16596 */ 16597 break; 16598 } 16599 off = opt[IPOPT_OFFSET]; 16600 off--; 16601 redo_srr: 16602 if (optlen < IP_ADDR_LEN || 16603 off > optlen - IP_ADDR_LEN) { 16604 /* End of source route */ 16605 ip1dbg(( 16606 "ip_rput_forward_options: end of SR\n")); 16607 ire_refrele(dst_ire); 16608 break; 16609 } 16610 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16611 bcopy(&ire->ire_src_addr, (char *)opt + off, 16612 IP_ADDR_LEN); 16613 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 16614 ntohl(dst))); 16615 16616 /* 16617 * Check if our address is present more than 16618 * once as consecutive hops in source route. 16619 */ 16620 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16621 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 16622 if (tmp_ire != NULL) { 16623 ire_refrele(tmp_ire); 16624 off += IP_ADDR_LEN; 16625 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16626 goto redo_srr; 16627 } 16628 ipha->ipha_dst = dst; 16629 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16630 ire_refrele(dst_ire); 16631 break; 16632 case IPOPT_RR: 16633 off = opt[IPOPT_OFFSET]; 16634 off--; 16635 if (optlen < IP_ADDR_LEN || 16636 off > optlen - IP_ADDR_LEN) { 16637 /* No more room - ignore */ 16638 ip1dbg(( 16639 "ip_rput_forward_options: end of RR\n")); 16640 break; 16641 } 16642 bcopy(&ire->ire_src_addr, (char *)opt + off, 16643 IP_ADDR_LEN); 16644 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16645 break; 16646 case IPOPT_TS: 16647 /* Insert timestamp if there is room */ 16648 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16649 case IPOPT_TS_TSONLY: 16650 off = IPOPT_TS_TIMELEN; 16651 break; 16652 case IPOPT_TS_PRESPEC: 16653 case IPOPT_TS_PRESPEC_RFC791: 16654 /* Verify that the address matched */ 16655 off = opt[IPOPT_OFFSET] - 1; 16656 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16657 dst_ire = ire_ctable_lookup(dst, 0, 16658 IRE_LOCAL, NULL, ALL_ZONES, NULL, 16659 MATCH_IRE_TYPE); 16660 16661 if (dst_ire == NULL) { 16662 /* Not for us */ 16663 break; 16664 } 16665 ire_refrele(dst_ire); 16666 /* FALLTHRU */ 16667 case IPOPT_TS_TSANDADDR: 16668 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16669 break; 16670 default: 16671 /* 16672 * ip_*put_options should have already 16673 * dropped this packet. 16674 */ 16675 cmn_err(CE_PANIC, "ip_rput_forward_options: " 16676 "unknown IT - bug in ip_rput_options?\n"); 16677 return (0); /* Keep "lint" happy */ 16678 } 16679 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 16680 /* Increase overflow counter */ 16681 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 16682 opt[IPOPT_POS_OV_FLG] = 16683 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 16684 (off << 4)); 16685 break; 16686 } 16687 off = opt[IPOPT_OFFSET] - 1; 16688 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16689 case IPOPT_TS_PRESPEC: 16690 case IPOPT_TS_PRESPEC_RFC791: 16691 case IPOPT_TS_TSANDADDR: 16692 bcopy(&ire->ire_src_addr, 16693 (char *)opt + off, IP_ADDR_LEN); 16694 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16695 /* FALLTHRU */ 16696 case IPOPT_TS_TSONLY: 16697 off = opt[IPOPT_OFFSET] - 1; 16698 /* Compute # of milliseconds since midnight */ 16699 gethrestime(&now); 16700 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 16701 now.tv_nsec / (NANOSEC / MILLISEC); 16702 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 16703 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 16704 break; 16705 } 16706 break; 16707 } 16708 } 16709 return (0); 16710 } 16711 16712 /* 16713 * This is called after processing at least one of AH/ESP headers. 16714 * 16715 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 16716 * the actual, physical interface on which the packet was received, 16717 * but, when ip_strict_dst_multihoming is set to 1, could be the 16718 * interface which had the ipha_dst configured when the packet went 16719 * through ip_rput. The ill_index corresponding to the recv_ill 16720 * is saved in ipsec_in_rill_index 16721 */ 16722 void 16723 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 16724 { 16725 mblk_t *mp; 16726 ipaddr_t dst; 16727 in6_addr_t *v6dstp; 16728 ipha_t *ipha; 16729 ip6_t *ip6h; 16730 ipsec_in_t *ii; 16731 boolean_t ill_need_rele = B_FALSE; 16732 boolean_t rill_need_rele = B_FALSE; 16733 boolean_t ire_need_rele = B_FALSE; 16734 16735 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 16736 ASSERT(ii->ipsec_in_ill_index != 0); 16737 16738 mp = ipsec_mp->b_cont; 16739 ASSERT(mp != NULL); 16740 16741 16742 if (ill == NULL) { 16743 ASSERT(recv_ill == NULL); 16744 /* 16745 * We need to get the original queue on which ip_rput_local 16746 * or ip_rput_data_v6 was called. 16747 */ 16748 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 16749 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL); 16750 ill_need_rele = B_TRUE; 16751 16752 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 16753 recv_ill = ill_lookup_on_ifindex( 16754 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 16755 NULL, NULL, NULL, NULL); 16756 rill_need_rele = B_TRUE; 16757 } else { 16758 recv_ill = ill; 16759 } 16760 16761 if ((ill == NULL) || (recv_ill == NULL)) { 16762 ip0dbg(("ip_fanout_proto_again: interface " 16763 "disappeared\n")); 16764 if (ill != NULL) 16765 ill_refrele(ill); 16766 if (recv_ill != NULL) 16767 ill_refrele(recv_ill); 16768 freemsg(ipsec_mp); 16769 return; 16770 } 16771 } 16772 16773 ASSERT(ill != NULL && recv_ill != NULL); 16774 16775 if (mp->b_datap->db_type == M_CTL) { 16776 /* 16777 * AH/ESP is returning the ICMP message after 16778 * removing their headers. Fanout again till 16779 * it gets to the right protocol. 16780 */ 16781 if (ii->ipsec_in_v4) { 16782 icmph_t *icmph; 16783 int iph_hdr_length; 16784 int hdr_length; 16785 16786 ipha = (ipha_t *)mp->b_rptr; 16787 iph_hdr_length = IPH_HDR_LENGTH(ipha); 16788 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 16789 ipha = (ipha_t *)&icmph[1]; 16790 hdr_length = IPH_HDR_LENGTH(ipha); 16791 /* 16792 * icmp_inbound_error_fanout may need to do pullupmsg. 16793 * Reset the type to M_DATA. 16794 */ 16795 mp->b_datap->db_type = M_DATA; 16796 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 16797 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 16798 B_FALSE, ill, ii->ipsec_in_zoneid); 16799 } else { 16800 icmp6_t *icmp6; 16801 int hdr_length; 16802 16803 ip6h = (ip6_t *)mp->b_rptr; 16804 /* Don't call hdr_length_v6() unless you have to. */ 16805 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 16806 hdr_length = ip_hdr_length_v6(mp, ip6h); 16807 else 16808 hdr_length = IPV6_HDR_LEN; 16809 16810 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 16811 /* 16812 * icmp_inbound_error_fanout_v6 may need to do 16813 * pullupmsg. Reset the type to M_DATA. 16814 */ 16815 mp->b_datap->db_type = M_DATA; 16816 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 16817 ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); 16818 } 16819 if (ill_need_rele) 16820 ill_refrele(ill); 16821 if (rill_need_rele) 16822 ill_refrele(recv_ill); 16823 return; 16824 } 16825 16826 if (ii->ipsec_in_v4) { 16827 ipha = (ipha_t *)mp->b_rptr; 16828 dst = ipha->ipha_dst; 16829 if (CLASSD(dst)) { 16830 /* 16831 * Multicast has to be delivered to all streams. 16832 */ 16833 dst = INADDR_BROADCAST; 16834 } 16835 16836 if (ire == NULL) { 16837 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, 16838 MBLK_GETLABEL(mp)); 16839 if (ire == NULL) { 16840 if (ill_need_rele) 16841 ill_refrele(ill); 16842 if (rill_need_rele) 16843 ill_refrele(recv_ill); 16844 ip1dbg(("ip_fanout_proto_again: " 16845 "IRE not found")); 16846 freemsg(ipsec_mp); 16847 return; 16848 } 16849 ire_need_rele = B_TRUE; 16850 } 16851 16852 switch (ipha->ipha_protocol) { 16853 case IPPROTO_UDP: 16854 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 16855 recv_ill); 16856 if (ire_need_rele) 16857 ire_refrele(ire); 16858 break; 16859 case IPPROTO_TCP: 16860 if (!ire_need_rele) 16861 IRE_REFHOLD(ire); 16862 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 16863 ire, ipsec_mp, 0, ill->ill_rq, NULL); 16864 IRE_REFRELE(ire); 16865 if (mp != NULL) 16866 squeue_enter_chain(GET_SQUEUE(mp), mp, 16867 mp, 1, SQTAG_IP_PROTO_AGAIN); 16868 break; 16869 case IPPROTO_SCTP: 16870 if (!ire_need_rele) 16871 IRE_REFHOLD(ire); 16872 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 16873 ipsec_mp, 0, ill->ill_rq, dst); 16874 break; 16875 default: 16876 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 16877 recv_ill); 16878 if (ire_need_rele) 16879 ire_refrele(ire); 16880 break; 16881 } 16882 } else { 16883 uint32_t rput_flags = 0; 16884 16885 ip6h = (ip6_t *)mp->b_rptr; 16886 v6dstp = &ip6h->ip6_dst; 16887 /* 16888 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 16889 * address. 16890 * 16891 * Currently, we don't store that state in the IPSEC_IN 16892 * message, and we may need to. 16893 */ 16894 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 16895 IP6_IN_LLMCAST : 0); 16896 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 16897 NULL, NULL); 16898 } 16899 if (ill_need_rele) 16900 ill_refrele(ill); 16901 if (rill_need_rele) 16902 ill_refrele(recv_ill); 16903 } 16904 16905 /* 16906 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 16907 * returns 'true' if there are still fragments left on the queue, in 16908 * which case we restart the timer. 16909 */ 16910 void 16911 ill_frag_timer(void *arg) 16912 { 16913 ill_t *ill = (ill_t *)arg; 16914 boolean_t frag_pending; 16915 16916 mutex_enter(&ill->ill_lock); 16917 ASSERT(!ill->ill_fragtimer_executing); 16918 if (ill->ill_state_flags & ILL_CONDEMNED) { 16919 ill->ill_frag_timer_id = 0; 16920 mutex_exit(&ill->ill_lock); 16921 return; 16922 } 16923 ill->ill_fragtimer_executing = 1; 16924 mutex_exit(&ill->ill_lock); 16925 16926 frag_pending = ill_frag_timeout(ill, ip_g_frag_timeout); 16927 16928 /* 16929 * Restart the timer, if we have fragments pending or if someone 16930 * wanted us to be scheduled again. 16931 */ 16932 mutex_enter(&ill->ill_lock); 16933 ill->ill_fragtimer_executing = 0; 16934 ill->ill_frag_timer_id = 0; 16935 if (frag_pending || ill->ill_fragtimer_needrestart) 16936 ill_frag_timer_start(ill); 16937 mutex_exit(&ill->ill_lock); 16938 } 16939 16940 void 16941 ill_frag_timer_start(ill_t *ill) 16942 { 16943 ASSERT(MUTEX_HELD(&ill->ill_lock)); 16944 16945 /* If the ill is closing or opening don't proceed */ 16946 if (ill->ill_state_flags & ILL_CONDEMNED) 16947 return; 16948 16949 if (ill->ill_fragtimer_executing) { 16950 /* 16951 * ill_frag_timer is currently executing. Just record the 16952 * the fact that we want the timer to be restarted. 16953 * ill_frag_timer will post a timeout before it returns, 16954 * ensuring it will be called again. 16955 */ 16956 ill->ill_fragtimer_needrestart = 1; 16957 return; 16958 } 16959 16960 if (ill->ill_frag_timer_id == 0) { 16961 /* 16962 * The timer is neither running nor is the timeout handler 16963 * executing. Post a timeout so that ill_frag_timer will be 16964 * called 16965 */ 16966 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 16967 MSEC_TO_TICK(ip_g_frag_timo_ms >> 1)); 16968 ill->ill_fragtimer_needrestart = 0; 16969 } 16970 } 16971 16972 /* 16973 * This routine is needed for loopback when forwarding multicasts. 16974 * 16975 * IPQoS Notes: 16976 * IPPF processing is done in fanout routines. 16977 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 16978 * processing for IPSec packets is done when it comes back in clear. 16979 * NOTE : The callers of this function need to do the ire_refrele for the 16980 * ire that is being passed in. 16981 */ 16982 void 16983 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 16984 ill_t *recv_ill) 16985 { 16986 ill_t *ill = (ill_t *)q->q_ptr; 16987 uint32_t sum; 16988 uint32_t u1; 16989 uint32_t u2; 16990 int hdr_length; 16991 boolean_t mctl_present; 16992 mblk_t *first_mp = mp; 16993 mblk_t *hada_mp = NULL; 16994 ipha_t *inner_ipha; 16995 16996 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 16997 "ip_rput_locl_start: q %p", q); 16998 16999 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17000 ASSERT(ill != NULL); 17001 17002 17003 #define rptr ((uchar_t *)ipha) 17004 #define iphs ((uint16_t *)ipha) 17005 17006 /* 17007 * no UDP or TCP packet should come here anymore. 17008 */ 17009 ASSERT((ipha->ipha_protocol != IPPROTO_TCP) && 17010 (ipha->ipha_protocol != IPPROTO_UDP)); 17011 17012 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 17013 if (mctl_present && 17014 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 17015 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 17016 17017 /* 17018 * It's an IPsec accelerated packet. 17019 * Keep a pointer to the data attributes around until 17020 * we allocate the ipsec_info_t. 17021 */ 17022 IPSECHW_DEBUG(IPSECHW_PKT, 17023 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 17024 hada_mp = first_mp; 17025 hada_mp->b_cont = NULL; 17026 /* 17027 * Since it is accelerated, it comes directly from 17028 * the ill and the data attributes is followed by 17029 * the packet data. 17030 */ 17031 ASSERT(mp->b_datap->db_type != M_CTL); 17032 first_mp = mp; 17033 mctl_present = B_FALSE; 17034 } 17035 17036 /* 17037 * IF M_CTL is not present, then ipsec_in_is_secure 17038 * should return B_TRUE. There is a case where loopback 17039 * packets has an M_CTL in the front with all the 17040 * IPSEC options set to IPSEC_PREF_NEVER - which means 17041 * ipsec_in_is_secure will return B_FALSE. As loopback 17042 * packets never comes here, it is safe to ASSERT the 17043 * following. 17044 */ 17045 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 17046 17047 17048 /* u1 is # words of IP options */ 17049 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 17050 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 17051 17052 if (u1) { 17053 if (!ip_options_cksum(q, ill, mp, ipha, ire)) { 17054 if (hada_mp != NULL) 17055 freemsg(hada_mp); 17056 return; 17057 } 17058 } else { 17059 /* Check the IP header checksum. */ 17060 #define uph ((uint16_t *)ipha) 17061 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 17062 uph[6] + uph[7] + uph[8] + uph[9]; 17063 #undef uph 17064 /* finish doing IP checksum */ 17065 sum = (sum & 0xFFFF) + (sum >> 16); 17066 sum = ~(sum + (sum >> 16)) & 0xFFFF; 17067 /* 17068 * Don't verify header checksum if this packet is coming 17069 * back from AH/ESP as we already did it. 17070 */ 17071 if (!mctl_present && (sum && sum != 0xFFFF)) { 17072 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); 17073 goto drop_pkt; 17074 } 17075 } 17076 17077 /* 17078 * Count for SNMP of inbound packets for ire. As ip_proto_input 17079 * might be called more than once for secure packets, count only 17080 * the first time. 17081 */ 17082 if (!mctl_present) { 17083 UPDATE_IB_PKT_COUNT(ire); 17084 ire->ire_last_used_time = lbolt; 17085 } 17086 17087 /* Check for fragmentation offset. */ 17088 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 17089 u1 = u2 & (IPH_MF | IPH_OFFSET); 17090 if (u1) { 17091 /* 17092 * We re-assemble fragments before we do the AH/ESP 17093 * processing. Thus, M_CTL should not be present 17094 * while we are re-assembling. 17095 */ 17096 ASSERT(!mctl_present); 17097 ASSERT(first_mp == mp); 17098 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 17099 return; 17100 } 17101 /* 17102 * Make sure that first_mp points back to mp as 17103 * the mp we came in with could have changed in 17104 * ip_rput_fragment(). 17105 */ 17106 ipha = (ipha_t *)mp->b_rptr; 17107 first_mp = mp; 17108 } 17109 17110 /* 17111 * Clear hardware checksumming flag as it is currently only 17112 * used by TCP and UDP. 17113 */ 17114 DB_CKSUMFLAGS(mp) = 0; 17115 17116 /* Now we have a complete datagram, destined for this machine. */ 17117 u1 = IPH_HDR_LENGTH(ipha); 17118 switch (ipha->ipha_protocol) { 17119 case IPPROTO_ICMP: { 17120 ire_t *ire_zone; 17121 ilm_t *ilm; 17122 mblk_t *mp1; 17123 zoneid_t last_zoneid; 17124 17125 if (CLASSD(ipha->ipha_dst) && 17126 !(recv_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 17127 ASSERT(ire->ire_type == IRE_BROADCAST); 17128 /* 17129 * In the multicast case, applications may have joined 17130 * the group from different zones, so we need to deliver 17131 * the packet to each of them. Loop through the 17132 * multicast memberships structures (ilm) on the receive 17133 * ill and send a copy of the packet up each matching 17134 * one. However, we don't do this for multicasts sent on 17135 * the loopback interface (PHYI_LOOPBACK flag set) as 17136 * they must stay in the sender's zone. 17137 * 17138 * ilm_add_v6() ensures that ilms in the same zone are 17139 * contiguous in the ill_ilm list. We use this property 17140 * to avoid sending duplicates needed when two 17141 * applications in the same zone join the same group on 17142 * different logical interfaces: we ignore the ilm if 17143 * its zoneid is the same as the last matching one. 17144 * In addition, the sending of the packet for 17145 * ire_zoneid is delayed until all of the other ilms 17146 * have been exhausted. 17147 */ 17148 last_zoneid = -1; 17149 ILM_WALKER_HOLD(recv_ill); 17150 for (ilm = recv_ill->ill_ilm; ilm != NULL; 17151 ilm = ilm->ilm_next) { 17152 if ((ilm->ilm_flags & ILM_DELETED) || 17153 ipha->ipha_dst != ilm->ilm_addr || 17154 ilm->ilm_zoneid == last_zoneid || 17155 ilm->ilm_zoneid == ire->ire_zoneid || 17156 ilm->ilm_zoneid == ALL_ZONES || 17157 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 17158 continue; 17159 mp1 = ip_copymsg(first_mp); 17160 if (mp1 == NULL) 17161 continue; 17162 icmp_inbound(q, mp1, B_TRUE, ill, 17163 0, sum, mctl_present, B_TRUE, 17164 recv_ill, ilm->ilm_zoneid); 17165 last_zoneid = ilm->ilm_zoneid; 17166 } 17167 ILM_WALKER_RELE(recv_ill); 17168 } else if (ire->ire_type == IRE_BROADCAST) { 17169 /* 17170 * In the broadcast case, there may be many zones 17171 * which need a copy of the packet delivered to them. 17172 * There is one IRE_BROADCAST per broadcast address 17173 * and per zone; we walk those using a helper function. 17174 * In addition, the sending of the packet for ire is 17175 * delayed until all of the other ires have been 17176 * processed. 17177 */ 17178 IRB_REFHOLD(ire->ire_bucket); 17179 ire_zone = NULL; 17180 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 17181 ire)) != NULL) { 17182 mp1 = ip_copymsg(first_mp); 17183 if (mp1 == NULL) 17184 continue; 17185 17186 UPDATE_IB_PKT_COUNT(ire_zone); 17187 ire_zone->ire_last_used_time = lbolt; 17188 icmp_inbound(q, mp1, B_TRUE, ill, 17189 0, sum, mctl_present, B_TRUE, 17190 recv_ill, ire_zone->ire_zoneid); 17191 } 17192 IRB_REFRELE(ire->ire_bucket); 17193 } 17194 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 17195 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 17196 ire->ire_zoneid); 17197 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17198 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 17199 return; 17200 } 17201 case IPPROTO_IGMP: 17202 /* 17203 * If we are not willing to accept IGMP packets in clear, 17204 * then check with global policy. 17205 */ 17206 if (igmp_accept_clear_messages == 0) { 17207 first_mp = ipsec_check_global_policy(first_mp, NULL, 17208 ipha, NULL, mctl_present); 17209 if (first_mp == NULL) 17210 return; 17211 } 17212 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17213 freemsg(first_mp); 17214 ip1dbg(("ip_proto_input: zone all cannot accept raw")); 17215 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17216 return; 17217 } 17218 if ((mp = igmp_input(q, mp, ill)) == NULL) { 17219 /* Bad packet - discarded by igmp_input */ 17220 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17221 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 17222 if (mctl_present) 17223 freeb(first_mp); 17224 return; 17225 } 17226 /* 17227 * igmp_input() may have returned the pulled up message. 17228 * So first_mp and ipha need to be reinitialized. 17229 */ 17230 ipha = (ipha_t *)mp->b_rptr; 17231 if (mctl_present) 17232 first_mp->b_cont = mp; 17233 else 17234 first_mp = mp; 17235 if (ipcl_proto_search(ipha->ipha_protocol) == NULL) { 17236 /* No user-level listener for IGMP packets */ 17237 goto drop_pkt; 17238 } 17239 /* deliver to local raw users */ 17240 break; 17241 case IPPROTO_PIM: 17242 /* 17243 * If we are not willing to accept PIM packets in clear, 17244 * then check with global policy. 17245 */ 17246 if (pim_accept_clear_messages == 0) { 17247 first_mp = ipsec_check_global_policy(first_mp, NULL, 17248 ipha, NULL, mctl_present); 17249 if (first_mp == NULL) 17250 return; 17251 } 17252 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { 17253 freemsg(first_mp); 17254 ip1dbg(("ip_proto_input: zone all cannot accept PIM")); 17255 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17256 return; 17257 } 17258 if (pim_input(q, mp) != 0) { 17259 /* Bad packet - discarded by pim_input */ 17260 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17261 "ip_rput_locl_end: q %p (%S)", q, "pim"); 17262 if (mctl_present) 17263 freeb(first_mp); 17264 return; 17265 } 17266 17267 /* 17268 * pim_input() may have pulled up the message so ipha needs to 17269 * be reinitialized. 17270 */ 17271 ipha = (ipha_t *)mp->b_rptr; 17272 if (ipcl_proto_search(ipha->ipha_protocol) == NULL) { 17273 /* No user-level listener for PIM packets */ 17274 goto drop_pkt; 17275 } 17276 /* deliver to local raw users */ 17277 break; 17278 case IPPROTO_ENCAP: 17279 /* 17280 * Handle self-encapsulated packets (IP-in-IP where 17281 * the inner addresses == the outer addresses). 17282 */ 17283 hdr_length = IPH_HDR_LENGTH(ipha); 17284 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 17285 mp->b_wptr) { 17286 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 17287 sizeof (ipha_t) - mp->b_rptr)) { 17288 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17289 freemsg(first_mp); 17290 return; 17291 } 17292 ipha = (ipha_t *)mp->b_rptr; 17293 } 17294 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 17295 /* 17296 * Check the sanity of the inner IP header. 17297 */ 17298 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 17299 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17300 freemsg(first_mp); 17301 return; 17302 } 17303 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 17304 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17305 freemsg(first_mp); 17306 return; 17307 } 17308 if (inner_ipha->ipha_src == ipha->ipha_src && 17309 inner_ipha->ipha_dst == ipha->ipha_dst) { 17310 ipsec_in_t *ii; 17311 17312 /* 17313 * Self-encapsulated tunnel packet. Remove 17314 * the outer IP header and fanout again. 17315 * We also need to make sure that the inner 17316 * header is pulled up until options. 17317 */ 17318 mp->b_rptr = (uchar_t *)inner_ipha; 17319 ipha = inner_ipha; 17320 hdr_length = IPH_HDR_LENGTH(ipha); 17321 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 17322 if (!pullupmsg(mp, (uchar_t *)ipha + 17323 + hdr_length - mp->b_rptr)) { 17324 freemsg(first_mp); 17325 return; 17326 } 17327 ipha = (ipha_t *)mp->b_rptr; 17328 } 17329 if (!mctl_present) { 17330 ASSERT(first_mp == mp); 17331 /* 17332 * This means that somebody is sending 17333 * Self-encapsualted packets without AH/ESP. 17334 * If AH/ESP was present, we would have already 17335 * allocated the first_mp. 17336 */ 17337 if ((first_mp = ipsec_in_alloc(B_TRUE)) == 17338 NULL) { 17339 ip1dbg(("ip_proto_input: IPSEC_IN " 17340 "allocation failure.\n")); 17341 BUMP_MIB(ill->ill_ip_mib, 17342 ipIfStatsInDiscards); 17343 freemsg(mp); 17344 return; 17345 } 17346 first_mp->b_cont = mp; 17347 } 17348 /* 17349 * We generally store the ill_index if we need to 17350 * do IPSEC processing as we lose the ill queue when 17351 * we come back. But in this case, we never should 17352 * have to store the ill_index here as it should have 17353 * been stored previously when we processed the 17354 * AH/ESP header in this routine or for non-ipsec 17355 * cases, we still have the queue. But for some bad 17356 * packets from the wire, we can get to IPSEC after 17357 * this and we better store the index for that case. 17358 */ 17359 ill = (ill_t *)q->q_ptr; 17360 ii = (ipsec_in_t *)first_mp->b_rptr; 17361 ii->ipsec_in_ill_index = 17362 ill->ill_phyint->phyint_ifindex; 17363 ii->ipsec_in_rill_index = 17364 recv_ill->ill_phyint->phyint_ifindex; 17365 if (ii->ipsec_in_decaps) { 17366 /* 17367 * This packet is self-encapsulated multiple 17368 * times. We don't want to recurse infinitely. 17369 * To keep it simple, drop the packet. 17370 */ 17371 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17372 freemsg(first_mp); 17373 return; 17374 } 17375 ii->ipsec_in_decaps = B_TRUE; 17376 ip_fanout_proto_again(first_mp, recv_ill, recv_ill, 17377 ire); 17378 return; 17379 } 17380 break; 17381 case IPPROTO_AH: 17382 case IPPROTO_ESP: { 17383 /* 17384 * Fast path for AH/ESP. If this is the first time 17385 * we are sending a datagram to AH/ESP, allocate 17386 * a IPSEC_IN message and prepend it. Otherwise, 17387 * just fanout. 17388 */ 17389 17390 int ipsec_rc; 17391 ipsec_in_t *ii; 17392 17393 IP_STAT(ipsec_proto_ahesp); 17394 if (!mctl_present) { 17395 ASSERT(first_mp == mp); 17396 if ((first_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 17397 ip1dbg(("ip_proto_input: IPSEC_IN " 17398 "allocation failure.\n")); 17399 freemsg(hada_mp); /* okay ifnull */ 17400 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17401 freemsg(mp); 17402 return; 17403 } 17404 /* 17405 * Store the ill_index so that when we come back 17406 * from IPSEC we ride on the same queue. 17407 */ 17408 ill = (ill_t *)q->q_ptr; 17409 ii = (ipsec_in_t *)first_mp->b_rptr; 17410 ii->ipsec_in_ill_index = 17411 ill->ill_phyint->phyint_ifindex; 17412 ii->ipsec_in_rill_index = 17413 recv_ill->ill_phyint->phyint_ifindex; 17414 first_mp->b_cont = mp; 17415 /* 17416 * Cache hardware acceleration info. 17417 */ 17418 if (hada_mp != NULL) { 17419 IPSECHW_DEBUG(IPSECHW_PKT, 17420 ("ip_rput_local: caching data attr.\n")); 17421 ii->ipsec_in_accelerated = B_TRUE; 17422 ii->ipsec_in_da = hada_mp; 17423 hada_mp = NULL; 17424 } 17425 } else { 17426 ii = (ipsec_in_t *)first_mp->b_rptr; 17427 } 17428 17429 if (!ipsec_loaded()) { 17430 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 17431 ire->ire_zoneid); 17432 return; 17433 } 17434 17435 /* select inbound SA and have IPsec process the pkt */ 17436 if (ipha->ipha_protocol == IPPROTO_ESP) { 17437 esph_t *esph = ipsec_inbound_esp_sa(first_mp); 17438 if (esph == NULL) 17439 return; 17440 ASSERT(ii->ipsec_in_esp_sa != NULL); 17441 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 17442 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 17443 first_mp, esph); 17444 } else { 17445 ah_t *ah = ipsec_inbound_ah_sa(first_mp); 17446 if (ah == NULL) 17447 return; 17448 ASSERT(ii->ipsec_in_ah_sa != NULL); 17449 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 17450 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 17451 first_mp, ah); 17452 } 17453 17454 switch (ipsec_rc) { 17455 case IPSEC_STATUS_SUCCESS: 17456 break; 17457 case IPSEC_STATUS_FAILED: 17458 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 17459 /* FALLTHRU */ 17460 case IPSEC_STATUS_PENDING: 17461 return; 17462 } 17463 /* we're done with IPsec processing, send it up */ 17464 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 17465 return; 17466 } 17467 default: 17468 break; 17469 } 17470 if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { 17471 ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", 17472 ire->ire_zoneid)); 17473 goto drop_pkt; 17474 } 17475 /* 17476 * Handle protocols with which IP is less intimate. There 17477 * can be more than one stream bound to a particular 17478 * protocol. When this is the case, each one gets a copy 17479 * of any incoming packets. 17480 */ 17481 ip_fanout_proto(q, first_mp, ill, ipha, 17482 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 17483 B_TRUE, recv_ill, ire->ire_zoneid); 17484 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17485 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 17486 return; 17487 17488 drop_pkt: 17489 freemsg(first_mp); 17490 if (hada_mp != NULL) 17491 freeb(hada_mp); 17492 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 17493 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 17494 #undef rptr 17495 #undef iphs 17496 17497 } 17498 17499 /* 17500 * Update any source route, record route or timestamp options. 17501 * Check that we are at end of strict source route. 17502 * The options have already been checked for sanity in ip_rput_options(). 17503 */ 17504 static boolean_t 17505 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire) 17506 { 17507 ipoptp_t opts; 17508 uchar_t *opt; 17509 uint8_t optval; 17510 uint8_t optlen; 17511 ipaddr_t dst; 17512 uint32_t ts; 17513 ire_t *dst_ire; 17514 timestruc_t now; 17515 zoneid_t zoneid; 17516 ill_t *ill; 17517 17518 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17519 17520 ip2dbg(("ip_rput_local_options\n")); 17521 17522 for (optval = ipoptp_first(&opts, ipha); 17523 optval != IPOPT_EOL; 17524 optval = ipoptp_next(&opts)) { 17525 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 17526 opt = opts.ipoptp_cur; 17527 optlen = opts.ipoptp_len; 17528 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 17529 optval, optlen)); 17530 switch (optval) { 17531 uint32_t off; 17532 case IPOPT_SSRR: 17533 case IPOPT_LSRR: 17534 off = opt[IPOPT_OFFSET]; 17535 off--; 17536 if (optlen < IP_ADDR_LEN || 17537 off > optlen - IP_ADDR_LEN) { 17538 /* End of source route */ 17539 ip1dbg(("ip_rput_local_options: end of SR\n")); 17540 break; 17541 } 17542 /* 17543 * This will only happen if two consecutive entries 17544 * in the source route contains our address or if 17545 * it is a packet with a loose source route which 17546 * reaches us before consuming the whole source route 17547 */ 17548 ip1dbg(("ip_rput_local_options: not end of SR\n")); 17549 if (optval == IPOPT_SSRR) { 17550 goto bad_src_route; 17551 } 17552 /* 17553 * Hack: instead of dropping the packet truncate the 17554 * source route to what has been used by filling the 17555 * rest with IPOPT_NOP. 17556 */ 17557 opt[IPOPT_OLEN] = (uint8_t)off; 17558 while (off < optlen) { 17559 opt[off++] = IPOPT_NOP; 17560 } 17561 break; 17562 case IPOPT_RR: 17563 off = opt[IPOPT_OFFSET]; 17564 off--; 17565 if (optlen < IP_ADDR_LEN || 17566 off > optlen - IP_ADDR_LEN) { 17567 /* No more room - ignore */ 17568 ip1dbg(( 17569 "ip_rput_local_options: end of RR\n")); 17570 break; 17571 } 17572 bcopy(&ire->ire_src_addr, (char *)opt + off, 17573 IP_ADDR_LEN); 17574 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17575 break; 17576 case IPOPT_TS: 17577 /* Insert timestamp if there is romm */ 17578 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17579 case IPOPT_TS_TSONLY: 17580 off = IPOPT_TS_TIMELEN; 17581 break; 17582 case IPOPT_TS_PRESPEC: 17583 case IPOPT_TS_PRESPEC_RFC791: 17584 /* Verify that the address matched */ 17585 off = opt[IPOPT_OFFSET] - 1; 17586 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17587 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 17588 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 17589 if (dst_ire == NULL) { 17590 /* Not for us */ 17591 break; 17592 } 17593 ire_refrele(dst_ire); 17594 /* FALLTHRU */ 17595 case IPOPT_TS_TSANDADDR: 17596 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17597 break; 17598 default: 17599 /* 17600 * ip_*put_options should have already 17601 * dropped this packet. 17602 */ 17603 cmn_err(CE_PANIC, "ip_rput_local_options: " 17604 "unknown IT - bug in ip_rput_options?\n"); 17605 return (B_TRUE); /* Keep "lint" happy */ 17606 } 17607 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 17608 /* Increase overflow counter */ 17609 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 17610 opt[IPOPT_POS_OV_FLG] = 17611 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 17612 (off << 4)); 17613 break; 17614 } 17615 off = opt[IPOPT_OFFSET] - 1; 17616 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17617 case IPOPT_TS_PRESPEC: 17618 case IPOPT_TS_PRESPEC_RFC791: 17619 case IPOPT_TS_TSANDADDR: 17620 bcopy(&ire->ire_src_addr, (char *)opt + off, 17621 IP_ADDR_LEN); 17622 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 17623 /* FALLTHRU */ 17624 case IPOPT_TS_TSONLY: 17625 off = opt[IPOPT_OFFSET] - 1; 17626 /* Compute # of milliseconds since midnight */ 17627 gethrestime(&now); 17628 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 17629 now.tv_nsec / (NANOSEC / MILLISEC); 17630 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 17631 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 17632 break; 17633 } 17634 break; 17635 } 17636 } 17637 return (B_TRUE); 17638 17639 bad_src_route: 17640 q = WR(q); 17641 if (q->q_next != NULL) 17642 ill = q->q_ptr; 17643 else 17644 ill = NULL; 17645 17646 /* make sure we clear any indication of a hardware checksum */ 17647 DB_CKSUMFLAGS(mp) = 0; 17648 zoneid = ipif_lookup_addr_zoneid(ipha->ipha_dst, ill); 17649 if (zoneid == ALL_ZONES) 17650 freemsg(mp); 17651 else 17652 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid); 17653 return (B_FALSE); 17654 17655 } 17656 17657 /* 17658 * Process IP options in an inbound packet. If an option affects the 17659 * effective destination address, return the next hop address via dstp. 17660 * Returns -1 if something fails in which case an ICMP error has been sent 17661 * and mp freed. 17662 */ 17663 static int 17664 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp) 17665 { 17666 ipoptp_t opts; 17667 uchar_t *opt; 17668 uint8_t optval; 17669 uint8_t optlen; 17670 ipaddr_t dst; 17671 intptr_t code = 0; 17672 ire_t *ire = NULL; 17673 zoneid_t zoneid; 17674 ill_t *ill; 17675 17676 ip2dbg(("ip_rput_options\n")); 17677 dst = ipha->ipha_dst; 17678 for (optval = ipoptp_first(&opts, ipha); 17679 optval != IPOPT_EOL; 17680 optval = ipoptp_next(&opts)) { 17681 opt = opts.ipoptp_cur; 17682 optlen = opts.ipoptp_len; 17683 ip2dbg(("ip_rput_options: opt %d, len %d\n", 17684 optval, optlen)); 17685 /* 17686 * Note: we need to verify the checksum before we 17687 * modify anything thus this routine only extracts the next 17688 * hop dst from any source route. 17689 */ 17690 switch (optval) { 17691 uint32_t off; 17692 case IPOPT_SSRR: 17693 case IPOPT_LSRR: 17694 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17695 ALL_ZONES, NULL, MATCH_IRE_TYPE); 17696 if (ire == NULL) { 17697 if (optval == IPOPT_SSRR) { 17698 ip1dbg(("ip_rput_options: not next" 17699 " strict source route 0x%x\n", 17700 ntohl(dst))); 17701 code = (char *)&ipha->ipha_dst - 17702 (char *)ipha; 17703 goto param_prob; /* RouterReq's */ 17704 } 17705 ip2dbg(("ip_rput_options: " 17706 "not next source route 0x%x\n", 17707 ntohl(dst))); 17708 break; 17709 } 17710 ire_refrele(ire); 17711 17712 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17713 ip1dbg(( 17714 "ip_rput_options: bad option offset\n")); 17715 code = (char *)&opt[IPOPT_OLEN] - 17716 (char *)ipha; 17717 goto param_prob; 17718 } 17719 off = opt[IPOPT_OFFSET]; 17720 off--; 17721 redo_srr: 17722 if (optlen < IP_ADDR_LEN || 17723 off > optlen - IP_ADDR_LEN) { 17724 /* End of source route */ 17725 ip1dbg(("ip_rput_options: end of SR\n")); 17726 break; 17727 } 17728 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 17729 ip1dbg(("ip_rput_options: next hop 0x%x\n", 17730 ntohl(dst))); 17731 17732 /* 17733 * Check if our address is present more than 17734 * once as consecutive hops in source route. 17735 * XXX verify per-interface ip_forwarding 17736 * for source route? 17737 */ 17738 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17739 ALL_ZONES, NULL, MATCH_IRE_TYPE); 17740 17741 if (ire != NULL) { 17742 ire_refrele(ire); 17743 off += IP_ADDR_LEN; 17744 goto redo_srr; 17745 } 17746 17747 if (dst == htonl(INADDR_LOOPBACK)) { 17748 ip1dbg(("ip_rput_options: loopback addr in " 17749 "source route!\n")); 17750 goto bad_src_route; 17751 } 17752 /* 17753 * For strict: verify that dst is directly 17754 * reachable. 17755 */ 17756 if (optval == IPOPT_SSRR) { 17757 ire = ire_ftable_lookup(dst, 0, 0, 17758 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 17759 MBLK_GETLABEL(mp), 17760 MATCH_IRE_TYPE | MATCH_IRE_SECATTR); 17761 if (ire == NULL) { 17762 ip1dbg(("ip_rput_options: SSRR not " 17763 "directly reachable: 0x%x\n", 17764 ntohl(dst))); 17765 goto bad_src_route; 17766 } 17767 ire_refrele(ire); 17768 } 17769 /* 17770 * Defer update of the offset and the record route 17771 * until the packet is forwarded. 17772 */ 17773 break; 17774 case IPOPT_RR: 17775 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17776 ip1dbg(( 17777 "ip_rput_options: bad option offset\n")); 17778 code = (char *)&opt[IPOPT_OLEN] - 17779 (char *)ipha; 17780 goto param_prob; 17781 } 17782 break; 17783 case IPOPT_TS: 17784 /* 17785 * Verify that length >= 5 and that there is either 17786 * room for another timestamp or that the overflow 17787 * counter is not maxed out. 17788 */ 17789 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 17790 if (optlen < IPOPT_MINLEN_IT) { 17791 goto param_prob; 17792 } 17793 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 17794 ip1dbg(( 17795 "ip_rput_options: bad option offset\n")); 17796 code = (char *)&opt[IPOPT_OFFSET] - 17797 (char *)ipha; 17798 goto param_prob; 17799 } 17800 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 17801 case IPOPT_TS_TSONLY: 17802 off = IPOPT_TS_TIMELEN; 17803 break; 17804 case IPOPT_TS_TSANDADDR: 17805 case IPOPT_TS_PRESPEC: 17806 case IPOPT_TS_PRESPEC_RFC791: 17807 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 17808 break; 17809 default: 17810 code = (char *)&opt[IPOPT_POS_OV_FLG] - 17811 (char *)ipha; 17812 goto param_prob; 17813 } 17814 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 17815 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 17816 /* 17817 * No room and the overflow counter is 15 17818 * already. 17819 */ 17820 goto param_prob; 17821 } 17822 break; 17823 } 17824 } 17825 17826 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 17827 *dstp = dst; 17828 return (0); 17829 } 17830 17831 ip1dbg(("ip_rput_options: error processing IP options.")); 17832 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 17833 17834 param_prob: 17835 q = WR(q); 17836 if (q->q_next != NULL) 17837 ill = q->q_ptr; 17838 else 17839 ill = NULL; 17840 17841 /* make sure we clear any indication of a hardware checksum */ 17842 DB_CKSUMFLAGS(mp) = 0; 17843 /* Don't know whether this is for non-global or global/forwarding */ 17844 zoneid = ipif_lookup_addr_zoneid(dst, ill); 17845 if (zoneid == ALL_ZONES) 17846 freemsg(mp); 17847 else 17848 icmp_param_problem(q, mp, (uint8_t)code, zoneid); 17849 return (-1); 17850 17851 bad_src_route: 17852 q = WR(q); 17853 if (q->q_next != NULL) 17854 ill = q->q_ptr; 17855 else 17856 ill = NULL; 17857 17858 /* make sure we clear any indication of a hardware checksum */ 17859 DB_CKSUMFLAGS(mp) = 0; 17860 zoneid = ipif_lookup_addr_zoneid(dst, ill); 17861 if (zoneid == ALL_ZONES) 17862 freemsg(mp); 17863 else 17864 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, zoneid); 17865 return (-1); 17866 } 17867 17868 /* 17869 * IP & ICMP info in >=14 msg's ... 17870 * - ip fixed part (mib2_ip_t) 17871 * - icmp fixed part (mib2_icmp_t) 17872 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 17873 * - ipRouteEntryTable (ip 21) all IPv4 IREs 17874 * - ipNetToMediaEntryTable (ip 22) [filled in by the arp module] 17875 * - ipRouteAttributeTable (ip 102) labeled routes 17876 * - ip multicast membership (ip_member_t) 17877 * - ip multicast source filtering (ip_grpsrc_t) 17878 * - igmp fixed part (struct igmpstat) 17879 * - multicast routing stats (struct mrtstat) 17880 * - multicast routing vifs (array of struct vifctl) 17881 * - multicast routing routes (array of struct mfcctl) 17882 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 17883 * One per ill plus one generic 17884 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 17885 * One per ill plus one generic 17886 * - ipv6RouteEntry all IPv6 IREs 17887 * - ipv6RouteAttributeTable (ip6 102) labeled routes 17888 * - ipv6NetToMediaEntry all Neighbor Cache entries 17889 * - ipv6AddrEntry all IPv6 ipifs 17890 * - ipv6 multicast membership (ipv6_member_t) 17891 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 17892 * 17893 * MIB2_IP_MEDIA is filled in by the arp module with ARP cache entries. 17894 * 17895 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 17896 * already filled in by the caller. 17897 * Return value of 0 indicates that no messages were sent and caller 17898 * should free mpctl. 17899 */ 17900 int 17901 ip_snmp_get(queue_t *q, mblk_t *mpctl) 17902 { 17903 if (mpctl == NULL || mpctl->b_cont == NULL) { 17904 return (0); 17905 } 17906 17907 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl)) == NULL) { 17908 return (1); 17909 } 17910 17911 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl)) == NULL) { 17912 return (1); 17913 } 17914 17915 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl)) == NULL) { 17916 return (1); 17917 } 17918 17919 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl)) == NULL) { 17920 return (1); 17921 } 17922 17923 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl)) == NULL) { 17924 return (1); 17925 } 17926 17927 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl)) == NULL) { 17928 return (1); 17929 } 17930 17931 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl)) == NULL) { 17932 return (1); 17933 } 17934 17935 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl)) == NULL) { 17936 return (1); 17937 } 17938 17939 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl)) == NULL) { 17940 return (1); 17941 } 17942 17943 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl)) == NULL) { 17944 return (1); 17945 } 17946 17947 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl)) == NULL) { 17948 return (1); 17949 } 17950 17951 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl)) == NULL) { 17952 return (1); 17953 } 17954 17955 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl)) == NULL) { 17956 return (1); 17957 } 17958 17959 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl)) == NULL) { 17960 return (1); 17961 } 17962 17963 if ((mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl)) == NULL) { 17964 return (1); 17965 } 17966 17967 if ((mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl)) == NULL) { 17968 return (1); 17969 } 17970 17971 if ((mpctl = sctp_snmp_get_mib2(q, mpctl)) == NULL) { 17972 return (1); 17973 } 17974 freemsg(mpctl); 17975 return (1); 17976 } 17977 17978 17979 /* Get global (legacy) IPv4 statistics */ 17980 static mblk_t * 17981 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib) 17982 { 17983 mib2_ip_t old_ip_mib; 17984 struct opthdr *optp; 17985 mblk_t *mp2ctl; 17986 17987 /* 17988 * make a copy of the original message 17989 */ 17990 mp2ctl = copymsg(mpctl); 17991 17992 /* fixed length IP structure... */ 17993 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17994 optp->level = MIB2_IP; 17995 optp->name = 0; 17996 SET_MIB(old_ip_mib.ipForwarding, 17997 (WE_ARE_FORWARDING ? 1 : 2)); 17998 SET_MIB(old_ip_mib.ipDefaultTTL, 17999 (uint32_t)ip_def_ttl); 18000 SET_MIB(old_ip_mib.ipReasmTimeout, 18001 ip_g_frag_timeout); 18002 SET_MIB(old_ip_mib.ipAddrEntrySize, 18003 sizeof (mib2_ipAddrEntry_t)); 18004 SET_MIB(old_ip_mib.ipRouteEntrySize, 18005 sizeof (mib2_ipRouteEntry_t)); 18006 SET_MIB(old_ip_mib.ipNetToMediaEntrySize, 18007 sizeof (mib2_ipNetToMediaEntry_t)); 18008 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 18009 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 18010 SET_MIB(old_ip_mib.ipRouteAttributeSize, 18011 sizeof (mib2_ipAttributeEntry_t)); 18012 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 18013 18014 /* 18015 * Grab the statistics from the new IP MIB 18016 */ 18017 SET_MIB(old_ip_mib.ipInReceives, 18018 (uint32_t)ipmib->ipIfStatsHCInReceives); 18019 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors); 18020 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors); 18021 SET_MIB(old_ip_mib.ipForwDatagrams, 18022 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams); 18023 SET_MIB(old_ip_mib.ipInUnknownProtos, 18024 ipmib->ipIfStatsInUnknownProtos); 18025 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards); 18026 SET_MIB(old_ip_mib.ipInDelivers, 18027 (uint32_t)ipmib->ipIfStatsHCInDelivers); 18028 SET_MIB(old_ip_mib.ipOutRequests, 18029 (uint32_t)ipmib->ipIfStatsHCOutRequests); 18030 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards); 18031 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes); 18032 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds); 18033 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs); 18034 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails); 18035 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs); 18036 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails); 18037 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates); 18038 18039 /* ipRoutingDiscards is not being used */ 18040 SET_MIB(old_ip_mib.ipRoutingDiscards, 0); 18041 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs); 18042 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts); 18043 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs); 18044 SET_MIB(old_ip_mib.ipReasmDuplicates, 18045 ipmib->ipIfStatsReasmDuplicates); 18046 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups); 18047 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits); 18048 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs); 18049 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows); 18050 SET_MIB(old_ip_mib.rawipInOverflows, 18051 ipmib->rawipIfStatsInOverflows); 18052 18053 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded); 18054 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed); 18055 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion); 18056 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion); 18057 SET_MIB(old_ip_mib.ipOutSwitchIPv6, 18058 ipmib->ipIfStatsOutSwitchIPVersion); 18059 18060 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib, 18061 (int)sizeof (old_ip_mib))) { 18062 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 18063 (uint_t)sizeof (old_ip_mib))); 18064 } 18065 18066 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18067 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 18068 (int)optp->level, (int)optp->name, (int)optp->len)); 18069 qreply(q, mpctl); 18070 return (mp2ctl); 18071 } 18072 18073 /* Per interface IPv4 statistics */ 18074 static mblk_t * 18075 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl) 18076 { 18077 struct opthdr *optp; 18078 mblk_t *mp2ctl; 18079 ill_t *ill; 18080 ill_walk_context_t ctx; 18081 mblk_t *mp_tail = NULL; 18082 mib2_ipIfStatsEntry_t global_ip_mib; 18083 18084 /* 18085 * Make a copy of the original message 18086 */ 18087 mp2ctl = copymsg(mpctl); 18088 18089 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18090 optp->level = MIB2_IP; 18091 optp->name = MIB2_IP_TRAFFIC_STATS; 18092 /* Include "unknown interface" ip_mib */ 18093 ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 18094 ip_mib.ipIfStatsIfIndex = MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 18095 SET_MIB(ip_mib.ipIfStatsForwarding, (WE_ARE_FORWARDING ? 1 : 2)); 18096 SET_MIB(ip_mib.ipIfStatsDefaultTTL, (uint32_t)ip_def_ttl); 18097 SET_MIB(ip_mib.ipIfStatsEntrySize, sizeof (mib2_ipIfStatsEntry_t)); 18098 SET_MIB(ip_mib.ipIfStatsAddrEntrySize, sizeof (mib2_ipAddrEntry_t)); 18099 SET_MIB(ip_mib.ipIfStatsRouteEntrySize, sizeof (mib2_ipRouteEntry_t)); 18100 SET_MIB(ip_mib.ipIfStatsNetToMediaEntrySize, 18101 sizeof (mib2_ipNetToMediaEntry_t)); 18102 SET_MIB(ip_mib.ipIfStatsMemberEntrySize, sizeof (ip_member_t)); 18103 SET_MIB(ip_mib.ipIfStatsGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 18104 18105 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, (char *)&ip_mib, 18106 (int)sizeof (ip_mib))) { 18107 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18108 "failed to allocate %u bytes\n", 18109 (uint_t)sizeof (ip_mib))); 18110 } 18111 18112 bcopy(&ip_mib, &global_ip_mib, sizeof (global_ip_mib)); 18113 18114 rw_enter(&ill_g_lock, RW_READER); 18115 ill = ILL_START_WALK_V4(&ctx); 18116 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18117 ill->ill_ip_mib->ipIfStatsIfIndex = 18118 ill->ill_phyint->phyint_ifindex; 18119 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 18120 (WE_ARE_FORWARDING ? 1 : 2)); 18121 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL, 18122 (uint32_t)ip_def_ttl); 18123 18124 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib); 18125 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18126 (char *)ill->ill_ip_mib, 18127 (int)sizeof (*ill->ill_ip_mib))) { 18128 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18129 "failed to allocate %u bytes\n", 18130 (uint_t)sizeof (*ill->ill_ip_mib))); 18131 } 18132 } 18133 rw_exit(&ill_g_lock); 18134 18135 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18136 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 18137 "level %d, name %d, len %d\n", 18138 (int)optp->level, (int)optp->name, (int)optp->len)); 18139 qreply(q, mpctl); 18140 18141 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib)); 18142 } 18143 18144 /* Global IPv4 ICMP statistics */ 18145 static mblk_t * 18146 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl) 18147 { 18148 struct opthdr *optp; 18149 mblk_t *mp2ctl; 18150 18151 /* 18152 * Make a copy of the original message 18153 */ 18154 mp2ctl = copymsg(mpctl); 18155 18156 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18157 optp->level = MIB2_ICMP; 18158 optp->name = 0; 18159 if (!snmp_append_data(mpctl->b_cont, (char *)&icmp_mib, 18160 (int)sizeof (icmp_mib))) { 18161 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 18162 (uint_t)sizeof (icmp_mib))); 18163 } 18164 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18165 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 18166 (int)optp->level, (int)optp->name, (int)optp->len)); 18167 qreply(q, mpctl); 18168 return (mp2ctl); 18169 } 18170 18171 /* Global IPv4 IGMP statistics */ 18172 static mblk_t * 18173 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl) 18174 { 18175 struct opthdr *optp; 18176 mblk_t *mp2ctl; 18177 18178 /* 18179 * make a copy of the original message 18180 */ 18181 mp2ctl = copymsg(mpctl); 18182 18183 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18184 optp->level = EXPER_IGMP; 18185 optp->name = 0; 18186 if (!snmp_append_data(mpctl->b_cont, (char *)&igmpstat, 18187 (int)sizeof (igmpstat))) { 18188 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 18189 (uint_t)sizeof (igmpstat))); 18190 } 18191 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18192 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 18193 (int)optp->level, (int)optp->name, (int)optp->len)); 18194 qreply(q, mpctl); 18195 return (mp2ctl); 18196 } 18197 18198 /* Global IPv4 Multicast Routing statistics */ 18199 static mblk_t * 18200 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl) 18201 { 18202 struct opthdr *optp; 18203 mblk_t *mp2ctl; 18204 18205 /* 18206 * make a copy of the original message 18207 */ 18208 mp2ctl = copymsg(mpctl); 18209 18210 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18211 optp->level = EXPER_DVMRP; 18212 optp->name = 0; 18213 if (!ip_mroute_stats(mpctl->b_cont)) { 18214 ip0dbg(("ip_mroute_stats: failed\n")); 18215 } 18216 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18217 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 18218 (int)optp->level, (int)optp->name, (int)optp->len)); 18219 qreply(q, mpctl); 18220 return (mp2ctl); 18221 } 18222 18223 /* IPv4 address information */ 18224 static mblk_t * 18225 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl) 18226 { 18227 struct opthdr *optp; 18228 mblk_t *mp2ctl; 18229 mblk_t *mp_tail = NULL; 18230 ill_t *ill; 18231 ipif_t *ipif; 18232 uint_t bitval; 18233 mib2_ipAddrEntry_t mae; 18234 zoneid_t zoneid; 18235 ill_walk_context_t ctx; 18236 18237 /* 18238 * make a copy of the original message 18239 */ 18240 mp2ctl = copymsg(mpctl); 18241 18242 /* ipAddrEntryTable */ 18243 18244 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18245 optp->level = MIB2_IP; 18246 optp->name = MIB2_IP_ADDR; 18247 zoneid = Q_TO_CONN(q)->conn_zoneid; 18248 18249 rw_enter(&ill_g_lock, RW_READER); 18250 ill = ILL_START_WALK_V4(&ctx); 18251 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18252 for (ipif = ill->ill_ipif; ipif != NULL; 18253 ipif = ipif->ipif_next) { 18254 if (ipif->ipif_zoneid != zoneid && 18255 ipif->ipif_zoneid != ALL_ZONES) 18256 continue; 18257 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18258 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18259 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18260 18261 (void) ipif_get_name(ipif, 18262 mae.ipAdEntIfIndex.o_bytes, 18263 OCTET_LENGTH); 18264 mae.ipAdEntIfIndex.o_length = 18265 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 18266 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 18267 mae.ipAdEntNetMask = ipif->ipif_net_mask; 18268 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 18269 mae.ipAdEntInfo.ae_subnet_len = 18270 ip_mask_to_plen(ipif->ipif_net_mask); 18271 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 18272 for (bitval = 1; 18273 bitval && 18274 !(bitval & ipif->ipif_brd_addr); 18275 bitval <<= 1) 18276 noop; 18277 mae.ipAdEntBcastAddr = bitval; 18278 mae.ipAdEntReasmMaxSize = IP_MAXPACKET; 18279 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 18280 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 18281 mae.ipAdEntInfo.ae_broadcast_addr = 18282 ipif->ipif_brd_addr; 18283 mae.ipAdEntInfo.ae_pp_dst_addr = 18284 ipif->ipif_pp_dst_addr; 18285 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 18286 ill->ill_flags | ill->ill_phyint->phyint_flags; 18287 mae.ipAdEntRetransmitTime = AR_EQ_DEFAULT_XMIT_INTERVAL; 18288 18289 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18290 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 18291 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 18292 "allocate %u bytes\n", 18293 (uint_t)sizeof (mib2_ipAddrEntry_t))); 18294 } 18295 } 18296 } 18297 rw_exit(&ill_g_lock); 18298 18299 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18300 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 18301 (int)optp->level, (int)optp->name, (int)optp->len)); 18302 qreply(q, mpctl); 18303 return (mp2ctl); 18304 } 18305 18306 /* IPv6 address information */ 18307 static mblk_t * 18308 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl) 18309 { 18310 struct opthdr *optp; 18311 mblk_t *mp2ctl; 18312 mblk_t *mp_tail = NULL; 18313 ill_t *ill; 18314 ipif_t *ipif; 18315 mib2_ipv6AddrEntry_t mae6; 18316 zoneid_t zoneid; 18317 ill_walk_context_t ctx; 18318 18319 /* 18320 * make a copy of the original message 18321 */ 18322 mp2ctl = copymsg(mpctl); 18323 18324 /* ipv6AddrEntryTable */ 18325 18326 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18327 optp->level = MIB2_IP6; 18328 optp->name = MIB2_IP6_ADDR; 18329 zoneid = Q_TO_CONN(q)->conn_zoneid; 18330 18331 rw_enter(&ill_g_lock, RW_READER); 18332 ill = ILL_START_WALK_V6(&ctx); 18333 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18334 for (ipif = ill->ill_ipif; ipif != NULL; 18335 ipif = ipif->ipif_next) { 18336 if (ipif->ipif_zoneid != zoneid && 18337 ipif->ipif_zoneid != ALL_ZONES) 18338 continue; 18339 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 18340 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 18341 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 18342 18343 (void) ipif_get_name(ipif, 18344 mae6.ipv6AddrIfIndex.o_bytes, 18345 OCTET_LENGTH); 18346 mae6.ipv6AddrIfIndex.o_length = 18347 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 18348 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 18349 mae6.ipv6AddrPfxLength = 18350 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 18351 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 18352 mae6.ipv6AddrInfo.ae_subnet_len = 18353 mae6.ipv6AddrPfxLength; 18354 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 18355 18356 /* Type: stateless(1), stateful(2), unknown(3) */ 18357 if (ipif->ipif_flags & IPIF_ADDRCONF) 18358 mae6.ipv6AddrType = 1; 18359 else 18360 mae6.ipv6AddrType = 2; 18361 /* Anycast: true(1), false(2) */ 18362 if (ipif->ipif_flags & IPIF_ANYCAST) 18363 mae6.ipv6AddrAnycastFlag = 1; 18364 else 18365 mae6.ipv6AddrAnycastFlag = 2; 18366 18367 /* 18368 * Address status: preferred(1), deprecated(2), 18369 * invalid(3), inaccessible(4), unknown(5) 18370 */ 18371 if (ipif->ipif_flags & IPIF_NOLOCAL) 18372 mae6.ipv6AddrStatus = 3; 18373 else if (ipif->ipif_flags & IPIF_DEPRECATED) 18374 mae6.ipv6AddrStatus = 2; 18375 else 18376 mae6.ipv6AddrStatus = 1; 18377 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 18378 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 18379 mae6.ipv6AddrInfo.ae_pp_dst_addr = 18380 ipif->ipif_v6pp_dst_addr; 18381 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 18382 ill->ill_flags | ill->ill_phyint->phyint_flags; 18383 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET; 18384 mae6.ipv6AddrIdentifier = ill->ill_token; 18385 mae6.ipv6AddrIdentifierLen = ill->ill_token_length; 18386 mae6.ipv6AddrReachableTime = ill->ill_reachable_time; 18387 mae6.ipv6AddrRetransmitTime = 18388 ill->ill_reachable_retrans_time; 18389 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18390 (char *)&mae6, 18391 (int)sizeof (mib2_ipv6AddrEntry_t))) { 18392 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 18393 "allocate %u bytes\n", 18394 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 18395 } 18396 } 18397 } 18398 rw_exit(&ill_g_lock); 18399 18400 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18401 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 18402 (int)optp->level, (int)optp->name, (int)optp->len)); 18403 qreply(q, mpctl); 18404 return (mp2ctl); 18405 } 18406 18407 /* IPv4 multicast group membership. */ 18408 static mblk_t * 18409 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl) 18410 { 18411 struct opthdr *optp; 18412 mblk_t *mp2ctl; 18413 ill_t *ill; 18414 ipif_t *ipif; 18415 ilm_t *ilm; 18416 ip_member_t ipm; 18417 mblk_t *mp_tail = NULL; 18418 ill_walk_context_t ctx; 18419 zoneid_t zoneid; 18420 18421 /* 18422 * make a copy of the original message 18423 */ 18424 mp2ctl = copymsg(mpctl); 18425 zoneid = Q_TO_CONN(q)->conn_zoneid; 18426 18427 /* ipGroupMember table */ 18428 optp = (struct opthdr *)&mpctl->b_rptr[ 18429 sizeof (struct T_optmgmt_ack)]; 18430 optp->level = MIB2_IP; 18431 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 18432 18433 rw_enter(&ill_g_lock, RW_READER); 18434 ill = ILL_START_WALK_V4(&ctx); 18435 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18436 ILM_WALKER_HOLD(ill); 18437 for (ipif = ill->ill_ipif; ipif != NULL; 18438 ipif = ipif->ipif_next) { 18439 if (ipif->ipif_zoneid != zoneid && 18440 ipif->ipif_zoneid != ALL_ZONES) 18441 continue; /* not this zone */ 18442 (void) ipif_get_name(ipif, 18443 ipm.ipGroupMemberIfIndex.o_bytes, 18444 OCTET_LENGTH); 18445 ipm.ipGroupMemberIfIndex.o_length = 18446 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 18447 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18448 ASSERT(ilm->ilm_ipif != NULL); 18449 ASSERT(ilm->ilm_ill == NULL); 18450 if (ilm->ilm_ipif != ipif) 18451 continue; 18452 ipm.ipGroupMemberAddress = ilm->ilm_addr; 18453 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 18454 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 18455 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18456 (char *)&ipm, (int)sizeof (ipm))) { 18457 ip1dbg(("ip_snmp_get_mib2_ip_group: " 18458 "failed to allocate %u bytes\n", 18459 (uint_t)sizeof (ipm))); 18460 } 18461 } 18462 } 18463 ILM_WALKER_RELE(ill); 18464 } 18465 rw_exit(&ill_g_lock); 18466 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18467 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18468 (int)optp->level, (int)optp->name, (int)optp->len)); 18469 qreply(q, mpctl); 18470 return (mp2ctl); 18471 } 18472 18473 /* IPv6 multicast group membership. */ 18474 static mblk_t * 18475 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl) 18476 { 18477 struct opthdr *optp; 18478 mblk_t *mp2ctl; 18479 ill_t *ill; 18480 ilm_t *ilm; 18481 ipv6_member_t ipm6; 18482 mblk_t *mp_tail = NULL; 18483 ill_walk_context_t ctx; 18484 zoneid_t zoneid; 18485 18486 /* 18487 * make a copy of the original message 18488 */ 18489 mp2ctl = copymsg(mpctl); 18490 zoneid = Q_TO_CONN(q)->conn_zoneid; 18491 18492 /* ip6GroupMember table */ 18493 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18494 optp->level = MIB2_IP6; 18495 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 18496 18497 rw_enter(&ill_g_lock, RW_READER); 18498 ill = ILL_START_WALK_V6(&ctx); 18499 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18500 ILM_WALKER_HOLD(ill); 18501 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 18502 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18503 ASSERT(ilm->ilm_ipif == NULL); 18504 ASSERT(ilm->ilm_ill != NULL); 18505 if (ilm->ilm_zoneid != zoneid) 18506 continue; /* not this zone */ 18507 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 18508 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 18509 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 18510 if (!snmp_append_data2(mpctl->b_cont, 18511 &mp_tail, 18512 (char *)&ipm6, (int)sizeof (ipm6))) { 18513 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 18514 "failed to allocate %u bytes\n", 18515 (uint_t)sizeof (ipm6))); 18516 } 18517 } 18518 ILM_WALKER_RELE(ill); 18519 } 18520 rw_exit(&ill_g_lock); 18521 18522 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18523 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18524 (int)optp->level, (int)optp->name, (int)optp->len)); 18525 qreply(q, mpctl); 18526 return (mp2ctl); 18527 } 18528 18529 /* IP multicast filtered sources */ 18530 static mblk_t * 18531 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl) 18532 { 18533 struct opthdr *optp; 18534 mblk_t *mp2ctl; 18535 ill_t *ill; 18536 ipif_t *ipif; 18537 ilm_t *ilm; 18538 ip_grpsrc_t ips; 18539 mblk_t *mp_tail = NULL; 18540 ill_walk_context_t ctx; 18541 zoneid_t zoneid; 18542 int i; 18543 slist_t *sl; 18544 18545 /* 18546 * make a copy of the original message 18547 */ 18548 mp2ctl = copymsg(mpctl); 18549 zoneid = Q_TO_CONN(q)->conn_zoneid; 18550 18551 /* ipGroupSource table */ 18552 optp = (struct opthdr *)&mpctl->b_rptr[ 18553 sizeof (struct T_optmgmt_ack)]; 18554 optp->level = MIB2_IP; 18555 optp->name = EXPER_IP_GROUP_SOURCES; 18556 18557 rw_enter(&ill_g_lock, RW_READER); 18558 ill = ILL_START_WALK_V4(&ctx); 18559 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18560 ILM_WALKER_HOLD(ill); 18561 for (ipif = ill->ill_ipif; ipif != NULL; 18562 ipif = ipif->ipif_next) { 18563 if (ipif->ipif_zoneid != zoneid) 18564 continue; /* not this zone */ 18565 (void) ipif_get_name(ipif, 18566 ips.ipGroupSourceIfIndex.o_bytes, 18567 OCTET_LENGTH); 18568 ips.ipGroupSourceIfIndex.o_length = 18569 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 18570 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18571 ASSERT(ilm->ilm_ipif != NULL); 18572 ASSERT(ilm->ilm_ill == NULL); 18573 sl = ilm->ilm_filter; 18574 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 18575 continue; 18576 ips.ipGroupSourceGroup = ilm->ilm_addr; 18577 for (i = 0; i < sl->sl_numsrc; i++) { 18578 if (!IN6_IS_ADDR_V4MAPPED( 18579 &sl->sl_addr[i])) 18580 continue; 18581 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 18582 ips.ipGroupSourceAddress); 18583 if (snmp_append_data2(mpctl->b_cont, 18584 &mp_tail, (char *)&ips, 18585 (int)sizeof (ips)) == 0) { 18586 ip1dbg(("ip_snmp_get_mib2_" 18587 "ip_group_src: failed to " 18588 "allocate %u bytes\n", 18589 (uint_t)sizeof (ips))); 18590 } 18591 } 18592 } 18593 } 18594 ILM_WALKER_RELE(ill); 18595 } 18596 rw_exit(&ill_g_lock); 18597 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18598 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18599 (int)optp->level, (int)optp->name, (int)optp->len)); 18600 qreply(q, mpctl); 18601 return (mp2ctl); 18602 } 18603 18604 /* IPv6 multicast filtered sources. */ 18605 static mblk_t * 18606 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl) 18607 { 18608 struct opthdr *optp; 18609 mblk_t *mp2ctl; 18610 ill_t *ill; 18611 ilm_t *ilm; 18612 ipv6_grpsrc_t ips6; 18613 mblk_t *mp_tail = NULL; 18614 ill_walk_context_t ctx; 18615 zoneid_t zoneid; 18616 int i; 18617 slist_t *sl; 18618 18619 /* 18620 * make a copy of the original message 18621 */ 18622 mp2ctl = copymsg(mpctl); 18623 zoneid = Q_TO_CONN(q)->conn_zoneid; 18624 18625 /* ip6GroupMember table */ 18626 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18627 optp->level = MIB2_IP6; 18628 optp->name = EXPER_IP6_GROUP_SOURCES; 18629 18630 rw_enter(&ill_g_lock, RW_READER); 18631 ill = ILL_START_WALK_V6(&ctx); 18632 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18633 ILM_WALKER_HOLD(ill); 18634 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 18635 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 18636 ASSERT(ilm->ilm_ipif == NULL); 18637 ASSERT(ilm->ilm_ill != NULL); 18638 sl = ilm->ilm_filter; 18639 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 18640 continue; 18641 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 18642 for (i = 0; i < sl->sl_numsrc; i++) { 18643 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 18644 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18645 (char *)&ips6, (int)sizeof (ips6))) { 18646 ip1dbg(("ip_snmp_get_mib2_ip6_" 18647 "group_src: failed to allocate " 18648 "%u bytes\n", 18649 (uint_t)sizeof (ips6))); 18650 } 18651 } 18652 } 18653 ILM_WALKER_RELE(ill); 18654 } 18655 rw_exit(&ill_g_lock); 18656 18657 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18658 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 18659 (int)optp->level, (int)optp->name, (int)optp->len)); 18660 qreply(q, mpctl); 18661 return (mp2ctl); 18662 } 18663 18664 /* Multicast routing virtual interface table. */ 18665 static mblk_t * 18666 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl) 18667 { 18668 struct opthdr *optp; 18669 mblk_t *mp2ctl; 18670 18671 /* 18672 * make a copy of the original message 18673 */ 18674 mp2ctl = copymsg(mpctl); 18675 18676 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18677 optp->level = EXPER_DVMRP; 18678 optp->name = EXPER_DVMRP_VIF; 18679 if (!ip_mroute_vif(mpctl->b_cont)) { 18680 ip0dbg(("ip_mroute_vif: failed\n")); 18681 } 18682 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18683 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 18684 (int)optp->level, (int)optp->name, (int)optp->len)); 18685 qreply(q, mpctl); 18686 return (mp2ctl); 18687 } 18688 18689 /* Multicast routing table. */ 18690 static mblk_t * 18691 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl) 18692 { 18693 struct opthdr *optp; 18694 mblk_t *mp2ctl; 18695 18696 /* 18697 * make a copy of the original message 18698 */ 18699 mp2ctl = copymsg(mpctl); 18700 18701 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18702 optp->level = EXPER_DVMRP; 18703 optp->name = EXPER_DVMRP_MRT; 18704 if (!ip_mroute_mrt(mpctl->b_cont)) { 18705 ip0dbg(("ip_mroute_mrt: failed\n")); 18706 } 18707 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18708 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 18709 (int)optp->level, (int)optp->name, (int)optp->len)); 18710 qreply(q, mpctl); 18711 return (mp2ctl); 18712 } 18713 18714 /* 18715 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 18716 * in one IRE walk. 18717 */ 18718 static mblk_t * 18719 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl) 18720 { 18721 struct opthdr *optp; 18722 mblk_t *mp2ctl; /* Returned */ 18723 mblk_t *mp3ctl; /* nettomedia */ 18724 mblk_t *mp4ctl; /* routeattrs */ 18725 iproutedata_t ird; 18726 zoneid_t zoneid; 18727 18728 /* 18729 * make copies of the original message 18730 * - mp2ctl is returned unchanged to the caller for his use 18731 * - mpctl is sent upstream as ipRouteEntryTable 18732 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 18733 * - mp4ctl is sent upstream as ipRouteAttributeTable 18734 */ 18735 mp2ctl = copymsg(mpctl); 18736 mp3ctl = copymsg(mpctl); 18737 mp4ctl = copymsg(mpctl); 18738 if (mp3ctl == NULL || mp4ctl == NULL) { 18739 freemsg(mp4ctl); 18740 freemsg(mp3ctl); 18741 freemsg(mp2ctl); 18742 freemsg(mpctl); 18743 return (NULL); 18744 } 18745 18746 bzero(&ird, sizeof (ird)); 18747 18748 ird.ird_route.lp_head = mpctl->b_cont; 18749 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 18750 ird.ird_attrs.lp_head = mp4ctl->b_cont; 18751 18752 zoneid = Q_TO_CONN(q)->conn_zoneid; 18753 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid); 18754 if (zoneid == GLOBAL_ZONEID) { 18755 /* 18756 * Those IREs are used by Mobile-IP; since mipagent(1M) requires 18757 * the sys_net_config privilege, it can only run in the global 18758 * zone, so we don't display these IREs in the other zones. 18759 */ 18760 ire_walk_srcif_table_v4(ip_snmp_get2_v4, &ird); 18761 ire_walk_ill_mrtun(0, 0, ip_snmp_get2_v4, &ird, NULL); 18762 } 18763 18764 /* ipRouteEntryTable in mpctl */ 18765 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18766 optp->level = MIB2_IP; 18767 optp->name = MIB2_IP_ROUTE; 18768 optp->len = msgdsize(ird.ird_route.lp_head); 18769 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18770 (int)optp->level, (int)optp->name, (int)optp->len)); 18771 qreply(q, mpctl); 18772 18773 /* ipNetToMediaEntryTable in mp3ctl */ 18774 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18775 optp->level = MIB2_IP; 18776 optp->name = MIB2_IP_MEDIA; 18777 optp->len = msgdsize(ird.ird_netmedia.lp_head); 18778 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18779 (int)optp->level, (int)optp->name, (int)optp->len)); 18780 qreply(q, mp3ctl); 18781 18782 /* ipRouteAttributeTable in mp4ctl */ 18783 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18784 optp->level = MIB2_IP; 18785 optp->name = EXPER_IP_RTATTR; 18786 optp->len = msgdsize(ird.ird_attrs.lp_head); 18787 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 18788 (int)optp->level, (int)optp->name, (int)optp->len)); 18789 if (optp->len == 0) 18790 freemsg(mp4ctl); 18791 else 18792 qreply(q, mp4ctl); 18793 18794 return (mp2ctl); 18795 } 18796 18797 /* 18798 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 18799 * ipv6NetToMediaEntryTable in an NDP walk. 18800 */ 18801 static mblk_t * 18802 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl) 18803 { 18804 struct opthdr *optp; 18805 mblk_t *mp2ctl; /* Returned */ 18806 mblk_t *mp3ctl; /* nettomedia */ 18807 mblk_t *mp4ctl; /* routeattrs */ 18808 iproutedata_t ird; 18809 zoneid_t zoneid; 18810 18811 /* 18812 * make copies of the original message 18813 * - mp2ctl is returned unchanged to the caller for his use 18814 * - mpctl is sent upstream as ipv6RouteEntryTable 18815 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 18816 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 18817 */ 18818 mp2ctl = copymsg(mpctl); 18819 mp3ctl = copymsg(mpctl); 18820 mp4ctl = copymsg(mpctl); 18821 if (mp3ctl == NULL || mp4ctl == NULL) { 18822 freemsg(mp4ctl); 18823 freemsg(mp3ctl); 18824 freemsg(mp2ctl); 18825 freemsg(mpctl); 18826 return (NULL); 18827 } 18828 18829 bzero(&ird, sizeof (ird)); 18830 18831 ird.ird_route.lp_head = mpctl->b_cont; 18832 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 18833 ird.ird_attrs.lp_head = mp4ctl->b_cont; 18834 18835 zoneid = Q_TO_CONN(q)->conn_zoneid; 18836 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid); 18837 18838 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18839 optp->level = MIB2_IP6; 18840 optp->name = MIB2_IP6_ROUTE; 18841 optp->len = msgdsize(ird.ird_route.lp_head); 18842 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18843 (int)optp->level, (int)optp->name, (int)optp->len)); 18844 qreply(q, mpctl); 18845 18846 /* ipv6NetToMediaEntryTable in mp3ctl */ 18847 ndp_walk(NULL, ip_snmp_get2_v6_media, &ird); 18848 18849 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18850 optp->level = MIB2_IP6; 18851 optp->name = MIB2_IP6_MEDIA; 18852 optp->len = msgdsize(ird.ird_netmedia.lp_head); 18853 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18854 (int)optp->level, (int)optp->name, (int)optp->len)); 18855 qreply(q, mp3ctl); 18856 18857 /* ipv6RouteAttributeTable in mp4ctl */ 18858 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18859 optp->level = MIB2_IP6; 18860 optp->name = EXPER_IP_RTATTR; 18861 optp->len = msgdsize(ird.ird_attrs.lp_head); 18862 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 18863 (int)optp->level, (int)optp->name, (int)optp->len)); 18864 if (optp->len == 0) 18865 freemsg(mp4ctl); 18866 else 18867 qreply(q, mp4ctl); 18868 18869 return (mp2ctl); 18870 } 18871 18872 /* 18873 * IPv6 mib: One per ill 18874 */ 18875 static mblk_t * 18876 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl) 18877 { 18878 struct opthdr *optp; 18879 mblk_t *mp2ctl; 18880 ill_t *ill; 18881 ill_walk_context_t ctx; 18882 mblk_t *mp_tail = NULL; 18883 18884 /* 18885 * Make a copy of the original message 18886 */ 18887 mp2ctl = copymsg(mpctl); 18888 18889 /* fixed length IPv6 structure ... */ 18890 18891 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18892 optp->level = MIB2_IP6; 18893 optp->name = 0; 18894 /* Include "unknown interface" ip6_mib */ 18895 ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 18896 ip6_mib.ipIfStatsIfIndex = MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 18897 SET_MIB(ip6_mib.ipIfStatsForwarding, ipv6_forward ? 1 : 2); 18898 SET_MIB(ip6_mib.ipIfStatsDefaultHopLimit, ipv6_def_hops); 18899 SET_MIB(ip6_mib.ipIfStatsEntrySize, 18900 sizeof (mib2_ipIfStatsEntry_t)); 18901 SET_MIB(ip6_mib.ipIfStatsAddrEntrySize, sizeof (mib2_ipv6AddrEntry_t)); 18902 SET_MIB(ip6_mib.ipIfStatsRouteEntrySize, 18903 sizeof (mib2_ipv6RouteEntry_t)); 18904 SET_MIB(ip6_mib.ipIfStatsNetToMediaEntrySize, 18905 sizeof (mib2_ipv6NetToMediaEntry_t)); 18906 SET_MIB(ip6_mib.ipIfStatsMemberEntrySize, sizeof (ipv6_member_t)); 18907 SET_MIB(ip6_mib.ipIfStatsGroupSourceEntrySize, sizeof (ipv6_grpsrc_t)); 18908 18909 /* 18910 * Synchronize 64- and 32-bit counters 18911 */ 18912 SYNC32_MIB(&ip6_mib, ipIfStatsInReceives, ipIfStatsHCInReceives); 18913 SYNC32_MIB(&ip6_mib, ipIfStatsInDelivers, ipIfStatsHCInDelivers); 18914 SYNC32_MIB(&ip6_mib, ipIfStatsOutRequests, ipIfStatsHCOutRequests); 18915 SYNC32_MIB(&ip6_mib, ipIfStatsOutForwDatagrams, 18916 ipIfStatsHCOutForwDatagrams); 18917 SYNC32_MIB(&ip6_mib, ipIfStatsOutMcastPkts, ipIfStatsHCOutMcastPkts); 18918 SYNC32_MIB(&ip6_mib, ipIfStatsInMcastPkts, ipIfStatsHCInMcastPkts); 18919 18920 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, (char *)&ip6_mib, 18921 (int)sizeof (ip6_mib))) { 18922 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 18923 (uint_t)sizeof (ip6_mib))); 18924 } 18925 18926 rw_enter(&ill_g_lock, RW_READER); 18927 ill = ILL_START_WALK_V6(&ctx); 18928 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 18929 ill->ill_ip_mib->ipIfStatsIfIndex = 18930 ill->ill_phyint->phyint_ifindex; 18931 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 18932 ipv6_forward ? 1 : 2); 18933 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit, 18934 ill->ill_max_hops); 18935 18936 /* 18937 * Synchronize 64- and 32-bit counters 18938 */ 18939 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives, 18940 ipIfStatsHCInReceives); 18941 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers, 18942 ipIfStatsHCInDelivers); 18943 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests, 18944 ipIfStatsHCOutRequests); 18945 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams, 18946 ipIfStatsHCOutForwDatagrams); 18947 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts, 18948 ipIfStatsHCOutMcastPkts); 18949 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts, 18950 ipIfStatsHCInMcastPkts); 18951 18952 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 18953 (char *)ill->ill_ip_mib, 18954 (int)sizeof (*ill->ill_ip_mib))) { 18955 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 18956 "%u bytes\n", 18957 (uint_t)sizeof (*ill->ill_ip_mib))); 18958 } 18959 } 18960 rw_exit(&ill_g_lock); 18961 18962 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 18963 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 18964 (int)optp->level, (int)optp->name, (int)optp->len)); 18965 qreply(q, mpctl); 18966 return (mp2ctl); 18967 } 18968 18969 /* 18970 * ICMPv6 mib: One per ill 18971 */ 18972 static mblk_t * 18973 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl) 18974 { 18975 struct opthdr *optp; 18976 mblk_t *mp2ctl; 18977 ill_t *ill; 18978 ill_walk_context_t ctx; 18979 mblk_t *mp_tail = NULL; 18980 /* 18981 * Make a copy of the original message 18982 */ 18983 mp2ctl = copymsg(mpctl); 18984 18985 /* fixed length ICMPv6 structure ... */ 18986 18987 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 18988 optp->level = MIB2_ICMP6; 18989 optp->name = 0; 18990 /* Include "unknown interface" icmp6_mib */ 18991 icmp6_mib.ipv6IfIcmpIfIndex = MIB2_UNKNOWN_INTERFACE; /* netstat flag */ 18992 icmp6_mib.ipv6IfIcmpEntrySize = sizeof (mib2_ipv6IfIcmpEntry_t); 18993 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, (char *)&icmp6_mib, 18994 (int)sizeof (icmp6_mib))) { 18995 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 18996 (uint_t)sizeof (icmp6_mib))); 18997 } 18998 18999 rw_enter(&ill_g_lock, RW_READER); 19000 ill = ILL_START_WALK_V6(&ctx); 19001 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19002 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 19003 ill->ill_phyint->phyint_ifindex; 19004 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 19005 (char *)ill->ill_icmp6_mib, 19006 (int)sizeof (*ill->ill_icmp6_mib))) { 19007 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 19008 "%u bytes\n", 19009 (uint_t)sizeof (*ill->ill_icmp6_mib))); 19010 } 19011 } 19012 rw_exit(&ill_g_lock); 19013 19014 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 19015 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 19016 (int)optp->level, (int)optp->name, (int)optp->len)); 19017 qreply(q, mpctl); 19018 return (mp2ctl); 19019 } 19020 19021 /* 19022 * ire_walk routine to create both ipRouteEntryTable and 19023 * ipRouteAttributeTable in one IRE walk 19024 */ 19025 static void 19026 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 19027 { 19028 ill_t *ill; 19029 ipif_t *ipif; 19030 mib2_ipRouteEntry_t *re; 19031 mib2_ipAttributeEntry_t *iae, *iaeptr; 19032 ipaddr_t gw_addr; 19033 tsol_ire_gw_secattr_t *attrp; 19034 tsol_gc_t *gc = NULL; 19035 tsol_gcgrp_t *gcgrp = NULL; 19036 uint_t sacnt = 0; 19037 int i; 19038 19039 ASSERT(ire->ire_ipversion == IPV4_VERSION); 19040 19041 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19042 return; 19043 19044 if ((attrp = ire->ire_gw_secattr) != NULL) { 19045 mutex_enter(&attrp->igsa_lock); 19046 if ((gc = attrp->igsa_gc) != NULL) { 19047 gcgrp = gc->gc_grp; 19048 ASSERT(gcgrp != NULL); 19049 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19050 sacnt = 1; 19051 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19052 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19053 gc = gcgrp->gcgrp_head; 19054 sacnt = gcgrp->gcgrp_count; 19055 } 19056 mutex_exit(&attrp->igsa_lock); 19057 19058 /* do nothing if there's no gc to report */ 19059 if (gc == NULL) { 19060 ASSERT(sacnt == 0); 19061 if (gcgrp != NULL) { 19062 /* we might as well drop the lock now */ 19063 rw_exit(&gcgrp->gcgrp_rwlock); 19064 gcgrp = NULL; 19065 } 19066 attrp = NULL; 19067 } 19068 19069 ASSERT(gc == NULL || (gcgrp != NULL && 19070 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19071 } 19072 ASSERT(sacnt == 0 || gc != NULL); 19073 19074 if (sacnt != 0 && 19075 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19076 kmem_free(re, sizeof (*re)); 19077 rw_exit(&gcgrp->gcgrp_rwlock); 19078 return; 19079 } 19080 19081 /* 19082 * Return all IRE types for route table... let caller pick and choose 19083 */ 19084 re->ipRouteDest = ire->ire_addr; 19085 ipif = ire->ire_ipif; 19086 re->ipRouteIfIndex.o_length = 0; 19087 if (ire->ire_type == IRE_CACHE) { 19088 ill = (ill_t *)ire->ire_stq->q_ptr; 19089 re->ipRouteIfIndex.o_length = 19090 ill->ill_name_length == 0 ? 0 : 19091 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19092 bcopy(ill->ill_name, re->ipRouteIfIndex.o_bytes, 19093 re->ipRouteIfIndex.o_length); 19094 } else if (ipif != NULL) { 19095 (void) ipif_get_name(ipif, re->ipRouteIfIndex.o_bytes, 19096 OCTET_LENGTH); 19097 re->ipRouteIfIndex.o_length = 19098 mi_strlen(re->ipRouteIfIndex.o_bytes); 19099 } 19100 re->ipRouteMetric1 = -1; 19101 re->ipRouteMetric2 = -1; 19102 re->ipRouteMetric3 = -1; 19103 re->ipRouteMetric4 = -1; 19104 19105 gw_addr = ire->ire_gateway_addr; 19106 19107 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 19108 re->ipRouteNextHop = ire->ire_src_addr; 19109 else 19110 re->ipRouteNextHop = gw_addr; 19111 /* indirect(4), direct(3), or invalid(2) */ 19112 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19113 re->ipRouteType = 2; 19114 else 19115 re->ipRouteType = (gw_addr != 0) ? 4 : 3; 19116 re->ipRouteProto = -1; 19117 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 19118 re->ipRouteMask = ire->ire_mask; 19119 re->ipRouteMetric5 = -1; 19120 re->ipRouteInfo.re_max_frag = ire->ire_max_frag; 19121 re->ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 19122 re->ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19123 re->ipRouteInfo.re_ref = ire->ire_refcnt; 19124 re->ipRouteInfo.re_src_addr = ire->ire_src_addr; 19125 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19126 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19127 re->ipRouteInfo.re_flags = ire->ire_flags; 19128 re->ipRouteInfo.re_in_ill.o_length = 0; 19129 19130 if (ire->ire_flags & RTF_DYNAMIC) { 19131 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19132 } else { 19133 re->ipRouteInfo.re_ire_type = ire->ire_type; 19134 } 19135 19136 if (ire->ire_in_ill != NULL) { 19137 re->ipRouteInfo.re_in_ill.o_length = 19138 ire->ire_in_ill->ill_name_length == 0 ? 0 : 19139 MIN(OCTET_LENGTH, ire->ire_in_ill->ill_name_length - 1); 19140 bcopy(ire->ire_in_ill->ill_name, 19141 re->ipRouteInfo.re_in_ill.o_bytes, 19142 re->ipRouteInfo.re_in_ill.o_length); 19143 } 19144 re->ipRouteInfo.re_in_src_addr = ire->ire_in_src_addr; 19145 19146 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19147 (char *)re, (int)sizeof (*re))) { 19148 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19149 (uint_t)sizeof (*re))); 19150 } 19151 19152 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19153 iaeptr->iae_routeidx = ird->ird_idx; 19154 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19155 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19156 } 19157 19158 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19159 (char *)iae, sacnt * sizeof (*iae))) { 19160 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 19161 (unsigned)(sacnt * sizeof (*iae)))); 19162 } 19163 19164 /* bump route index for next pass */ 19165 ird->ird_idx++; 19166 19167 kmem_free(re, sizeof (*re)); 19168 if (sacnt != 0) 19169 kmem_free(iae, sacnt * sizeof (*iae)); 19170 19171 if (gcgrp != NULL) 19172 rw_exit(&gcgrp->gcgrp_rwlock); 19173 } 19174 19175 /* 19176 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 19177 */ 19178 static void 19179 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 19180 { 19181 ill_t *ill; 19182 ipif_t *ipif; 19183 mib2_ipv6RouteEntry_t *re; 19184 mib2_ipAttributeEntry_t *iae, *iaeptr; 19185 in6_addr_t gw_addr_v6; 19186 tsol_ire_gw_secattr_t *attrp; 19187 tsol_gc_t *gc = NULL; 19188 tsol_gcgrp_t *gcgrp = NULL; 19189 uint_t sacnt = 0; 19190 int i; 19191 19192 ASSERT(ire->ire_ipversion == IPV6_VERSION); 19193 19194 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 19195 return; 19196 19197 if ((attrp = ire->ire_gw_secattr) != NULL) { 19198 mutex_enter(&attrp->igsa_lock); 19199 if ((gc = attrp->igsa_gc) != NULL) { 19200 gcgrp = gc->gc_grp; 19201 ASSERT(gcgrp != NULL); 19202 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19203 sacnt = 1; 19204 } else if ((gcgrp = attrp->igsa_gcgrp) != NULL) { 19205 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 19206 gc = gcgrp->gcgrp_head; 19207 sacnt = gcgrp->gcgrp_count; 19208 } 19209 mutex_exit(&attrp->igsa_lock); 19210 19211 /* do nothing if there's no gc to report */ 19212 if (gc == NULL) { 19213 ASSERT(sacnt == 0); 19214 if (gcgrp != NULL) { 19215 /* we might as well drop the lock now */ 19216 rw_exit(&gcgrp->gcgrp_rwlock); 19217 gcgrp = NULL; 19218 } 19219 attrp = NULL; 19220 } 19221 19222 ASSERT(gc == NULL || (gcgrp != NULL && 19223 RW_LOCK_HELD(&gcgrp->gcgrp_rwlock))); 19224 } 19225 ASSERT(sacnt == 0 || gc != NULL); 19226 19227 if (sacnt != 0 && 19228 (iae = kmem_alloc(sacnt * sizeof (*iae), KM_NOSLEEP)) == NULL) { 19229 kmem_free(re, sizeof (*re)); 19230 rw_exit(&gcgrp->gcgrp_rwlock); 19231 return; 19232 } 19233 19234 /* 19235 * Return all IRE types for route table... let caller pick and choose 19236 */ 19237 re->ipv6RouteDest = ire->ire_addr_v6; 19238 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 19239 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 19240 re->ipv6RouteIfIndex.o_length = 0; 19241 ipif = ire->ire_ipif; 19242 if (ire->ire_type == IRE_CACHE) { 19243 ill = (ill_t *)ire->ire_stq->q_ptr; 19244 re->ipv6RouteIfIndex.o_length = 19245 ill->ill_name_length == 0 ? 0 : 19246 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 19247 bcopy(ill->ill_name, re->ipv6RouteIfIndex.o_bytes, 19248 re->ipv6RouteIfIndex.o_length); 19249 } else if (ipif != NULL) { 19250 (void) ipif_get_name(ipif, re->ipv6RouteIfIndex.o_bytes, 19251 OCTET_LENGTH); 19252 re->ipv6RouteIfIndex.o_length = 19253 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 19254 } 19255 19256 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19257 19258 mutex_enter(&ire->ire_lock); 19259 gw_addr_v6 = ire->ire_gateway_addr_v6; 19260 mutex_exit(&ire->ire_lock); 19261 19262 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 19263 re->ipv6RouteNextHop = ire->ire_src_addr_v6; 19264 else 19265 re->ipv6RouteNextHop = gw_addr_v6; 19266 19267 /* remote(4), local(3), or discard(2) */ 19268 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 19269 re->ipv6RouteType = 2; 19270 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 19271 re->ipv6RouteType = 3; 19272 else 19273 re->ipv6RouteType = 4; 19274 19275 re->ipv6RouteProtocol = -1; 19276 re->ipv6RoutePolicy = 0; 19277 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 19278 re->ipv6RouteNextHopRDI = 0; 19279 re->ipv6RouteWeight = 0; 19280 re->ipv6RouteMetric = 0; 19281 re->ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 19282 re->ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 19283 re->ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 19284 re->ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 19285 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 19286 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 19287 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 19288 re->ipv6RouteInfo.re_flags = ire->ire_flags; 19289 19290 if (ire->ire_flags & RTF_DYNAMIC) { 19291 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT; 19292 } else { 19293 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 19294 } 19295 19296 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 19297 (char *)re, (int)sizeof (*re))) { 19298 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19299 (uint_t)sizeof (*re))); 19300 } 19301 19302 for (iaeptr = iae, i = 0; i < sacnt; i++, iaeptr++, gc = gc->gc_next) { 19303 iaeptr->iae_routeidx = ird->ird_idx; 19304 iaeptr->iae_doi = gc->gc_db->gcdb_doi; 19305 iaeptr->iae_slrange = gc->gc_db->gcdb_slrange; 19306 } 19307 19308 if (!snmp_append_data2(ird->ird_attrs.lp_head, &ird->ird_attrs.lp_tail, 19309 (char *)iae, sacnt * sizeof (*iae))) { 19310 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 19311 (unsigned)(sacnt * sizeof (*iae)))); 19312 } 19313 19314 /* bump route index for next pass */ 19315 ird->ird_idx++; 19316 19317 kmem_free(re, sizeof (*re)); 19318 if (sacnt != 0) 19319 kmem_free(iae, sacnt * sizeof (*iae)); 19320 19321 if (gcgrp != NULL) 19322 rw_exit(&gcgrp->gcgrp_rwlock); 19323 } 19324 19325 /* 19326 * ndp_walk routine to create ipv6NetToMediaEntryTable 19327 */ 19328 static int 19329 ip_snmp_get2_v6_media(nce_t *nce, iproutedata_t *ird) 19330 { 19331 ill_t *ill; 19332 mib2_ipv6NetToMediaEntry_t ntme; 19333 dl_unitdata_req_t *dl; 19334 19335 ill = nce->nce_ill; 19336 if (ill->ill_isv6 == B_FALSE) /* skip arpce entry */ 19337 return (0); 19338 19339 /* 19340 * Neighbor cache entry attached to IRE with on-link 19341 * destination. 19342 */ 19343 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 19344 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 19345 if ((ill->ill_flags & ILLF_XRESOLV) && 19346 (nce->nce_res_mp != NULL)) { 19347 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 19348 ntme.ipv6NetToMediaPhysAddress.o_length = 19349 dl->dl_dest_addr_length; 19350 } else { 19351 ntme.ipv6NetToMediaPhysAddress.o_length = 19352 ill->ill_phys_addr_length; 19353 } 19354 if (nce->nce_res_mp != NULL) { 19355 bcopy((char *)nce->nce_res_mp->b_rptr + 19356 NCE_LL_ADDR_OFFSET(ill), 19357 ntme.ipv6NetToMediaPhysAddress.o_bytes, 19358 ntme.ipv6NetToMediaPhysAddress.o_length); 19359 } else { 19360 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 19361 ill->ill_phys_addr_length); 19362 } 19363 /* 19364 * Note: Returns ND_* states. Should be: 19365 * reachable(1), stale(2), delay(3), probe(4), 19366 * invalid(5), unknown(6) 19367 */ 19368 ntme.ipv6NetToMediaState = nce->nce_state; 19369 ntme.ipv6NetToMediaLastUpdated = 0; 19370 19371 /* other(1), dynamic(2), static(3), local(4) */ 19372 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 19373 ntme.ipv6NetToMediaType = 4; 19374 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 19375 ntme.ipv6NetToMediaType = 1; 19376 } else { 19377 ntme.ipv6NetToMediaType = 2; 19378 } 19379 19380 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 19381 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 19382 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 19383 (uint_t)sizeof (ntme))); 19384 } 19385 return (0); 19386 } 19387 19388 /* 19389 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 19390 */ 19391 /* ARGSUSED */ 19392 int 19393 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 19394 { 19395 switch (level) { 19396 case MIB2_IP: 19397 case MIB2_ICMP: 19398 switch (name) { 19399 default: 19400 break; 19401 } 19402 return (1); 19403 default: 19404 return (1); 19405 } 19406 } 19407 19408 /* 19409 * When there exists both a 64- and 32-bit counter of a particular type 19410 * (i.e., InReceives), only the 64-bit counters are added. 19411 */ 19412 void 19413 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2) 19414 { 19415 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors); 19416 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors); 19417 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes); 19418 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors); 19419 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos); 19420 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts); 19421 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards); 19422 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards); 19423 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs); 19424 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails); 19425 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates); 19426 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds); 19427 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs); 19428 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails); 19429 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes); 19430 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates); 19431 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups); 19432 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits); 19433 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs); 19434 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows); 19435 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows); 19436 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion, 19437 o2->ipIfStatsInWrongIPVersion); 19438 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion, 19439 o2->ipIfStatsInWrongIPVersion); 19440 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion, 19441 o2->ipIfStatsOutSwitchIPVersion); 19442 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives); 19443 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets); 19444 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams, 19445 o2->ipIfStatsHCInForwDatagrams); 19446 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers); 19447 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests); 19448 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams, 19449 o2->ipIfStatsHCOutForwDatagrams); 19450 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds); 19451 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits); 19452 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets); 19453 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts); 19454 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets); 19455 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts); 19456 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets, 19457 o2->ipIfStatsHCOutMcastOctets); 19458 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts); 19459 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts); 19460 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded); 19461 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed); 19462 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs); 19463 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs); 19464 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts); 19465 } 19466 19467 void 19468 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2) 19469 { 19470 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs); 19471 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors); 19472 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs); 19473 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs); 19474 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds); 19475 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems); 19476 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs); 19477 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos); 19478 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies); 19479 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits, 19480 o2->ipv6IfIcmpInRouterSolicits); 19481 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements, 19482 o2->ipv6IfIcmpInRouterAdvertisements); 19483 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits, 19484 o2->ipv6IfIcmpInNeighborSolicits); 19485 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements, 19486 o2->ipv6IfIcmpInNeighborAdvertisements); 19487 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects); 19488 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries, 19489 o2->ipv6IfIcmpInGroupMembQueries); 19490 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses, 19491 o2->ipv6IfIcmpInGroupMembResponses); 19492 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions, 19493 o2->ipv6IfIcmpInGroupMembReductions); 19494 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs); 19495 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors); 19496 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs, 19497 o2->ipv6IfIcmpOutDestUnreachs); 19498 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs, 19499 o2->ipv6IfIcmpOutAdminProhibs); 19500 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds); 19501 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems, 19502 o2->ipv6IfIcmpOutParmProblems); 19503 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs); 19504 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos); 19505 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies); 19506 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits, 19507 o2->ipv6IfIcmpOutRouterSolicits); 19508 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements, 19509 o2->ipv6IfIcmpOutRouterAdvertisements); 19510 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits, 19511 o2->ipv6IfIcmpOutNeighborSolicits); 19512 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements, 19513 o2->ipv6IfIcmpOutNeighborAdvertisements); 19514 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects); 19515 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries, 19516 o2->ipv6IfIcmpOutGroupMembQueries); 19517 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses, 19518 o2->ipv6IfIcmpOutGroupMembResponses); 19519 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions, 19520 o2->ipv6IfIcmpOutGroupMembReductions); 19521 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows); 19522 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit); 19523 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements, 19524 o2->ipv6IfIcmpInBadNeighborAdvertisements); 19525 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations, 19526 o2->ipv6IfIcmpInBadNeighborSolicitations); 19527 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects); 19528 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal, 19529 o2->ipv6IfIcmpInGroupMembTotal); 19530 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries, 19531 o2->ipv6IfIcmpInGroupMembBadQueries); 19532 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports, 19533 o2->ipv6IfIcmpInGroupMembBadReports); 19534 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports, 19535 o2->ipv6IfIcmpInGroupMembOurReports); 19536 } 19537 19538 /* 19539 * Called before the options are updated to check if this packet will 19540 * be source routed from here. 19541 * This routine assumes that the options are well formed i.e. that they 19542 * have already been checked. 19543 */ 19544 static boolean_t 19545 ip_source_routed(ipha_t *ipha) 19546 { 19547 ipoptp_t opts; 19548 uchar_t *opt; 19549 uint8_t optval; 19550 uint8_t optlen; 19551 ipaddr_t dst; 19552 ire_t *ire; 19553 19554 if (IS_SIMPLE_IPH(ipha)) { 19555 ip2dbg(("not source routed\n")); 19556 return (B_FALSE); 19557 } 19558 dst = ipha->ipha_dst; 19559 for (optval = ipoptp_first(&opts, ipha); 19560 optval != IPOPT_EOL; 19561 optval = ipoptp_next(&opts)) { 19562 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 19563 opt = opts.ipoptp_cur; 19564 optlen = opts.ipoptp_len; 19565 ip2dbg(("ip_source_routed: opt %d, len %d\n", 19566 optval, optlen)); 19567 switch (optval) { 19568 uint32_t off; 19569 case IPOPT_SSRR: 19570 case IPOPT_LSRR: 19571 /* 19572 * If dst is one of our addresses and there are some 19573 * entries left in the source route return (true). 19574 */ 19575 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 19576 ALL_ZONES, NULL, MATCH_IRE_TYPE); 19577 if (ire == NULL) { 19578 ip2dbg(("ip_source_routed: not next" 19579 " source route 0x%x\n", 19580 ntohl(dst))); 19581 return (B_FALSE); 19582 } 19583 ire_refrele(ire); 19584 off = opt[IPOPT_OFFSET]; 19585 off--; 19586 if (optlen < IP_ADDR_LEN || 19587 off > optlen - IP_ADDR_LEN) { 19588 /* End of source route */ 19589 ip1dbg(("ip_source_routed: end of SR\n")); 19590 return (B_FALSE); 19591 } 19592 return (B_TRUE); 19593 } 19594 } 19595 ip2dbg(("not source routed\n")); 19596 return (B_FALSE); 19597 } 19598 19599 /* 19600 * Check if the packet contains any source route. 19601 */ 19602 static boolean_t 19603 ip_source_route_included(ipha_t *ipha) 19604 { 19605 ipoptp_t opts; 19606 uint8_t optval; 19607 19608 if (IS_SIMPLE_IPH(ipha)) 19609 return (B_FALSE); 19610 for (optval = ipoptp_first(&opts, ipha); 19611 optval != IPOPT_EOL; 19612 optval = ipoptp_next(&opts)) { 19613 switch (optval) { 19614 case IPOPT_SSRR: 19615 case IPOPT_LSRR: 19616 return (B_TRUE); 19617 } 19618 } 19619 return (B_FALSE); 19620 } 19621 19622 /* 19623 * Called when the IRE expiration timer fires. 19624 */ 19625 /* ARGSUSED */ 19626 void 19627 ip_trash_timer_expire(void *args) 19628 { 19629 int flush_flag = 0; 19630 19631 /* 19632 * ip_ire_expire_id is protected by ip_trash_timer_lock. 19633 * This lock makes sure that a new invocation of this function 19634 * that occurs due to an almost immediate timer firing will not 19635 * progress beyond this point until the current invocation is done 19636 */ 19637 mutex_enter(&ip_trash_timer_lock); 19638 ip_ire_expire_id = 0; 19639 mutex_exit(&ip_trash_timer_lock); 19640 19641 /* Periodic timer */ 19642 if (ip_ire_arp_time_elapsed >= ip_ire_arp_interval) { 19643 /* 19644 * Remove all IRE_CACHE entries since they might 19645 * contain arp information. 19646 */ 19647 flush_flag |= FLUSH_ARP_TIME; 19648 ip_ire_arp_time_elapsed = 0; 19649 IP_STAT(ip_ire_arp_timer_expired); 19650 } 19651 if (ip_ire_rd_time_elapsed >= ip_ire_redir_interval) { 19652 /* Remove all redirects */ 19653 flush_flag |= FLUSH_REDIRECT_TIME; 19654 ip_ire_rd_time_elapsed = 0; 19655 IP_STAT(ip_ire_redirect_timer_expired); 19656 } 19657 if (ip_ire_pmtu_time_elapsed >= ip_ire_pathmtu_interval) { 19658 /* Increase path mtu */ 19659 flush_flag |= FLUSH_MTU_TIME; 19660 ip_ire_pmtu_time_elapsed = 0; 19661 IP_STAT(ip_ire_pmtu_timer_expired); 19662 } 19663 19664 /* 19665 * Optimize for the case when there are no redirects in the 19666 * ftable, that is, no need to walk the ftable in that case. 19667 */ 19668 if (flush_flag & (FLUSH_MTU_TIME|FLUSH_ARP_TIME)) { 19669 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_CACHETABLE, ire_expire, 19670 (char *)(uintptr_t)flush_flag, IP_MASK_TABLE_SIZE, 0, NULL, 19671 ip_cache_table_size, ip_cache_table, NULL, ALL_ZONES); 19672 } 19673 if ((flush_flag & FLUSH_REDIRECT_TIME) && ip_redirect_cnt > 0) { 19674 ire_walk_ill_tables(MATCH_IRE_TYPE, IRE_FORWARDTABLE, 19675 ire_expire, (char *)(uintptr_t)flush_flag, 19676 IP_MASK_TABLE_SIZE, 0, NULL, 0, NULL, NULL, ALL_ZONES); 19677 } 19678 if (flush_flag & FLUSH_MTU_TIME) { 19679 /* 19680 * Walk all IPv6 IRE's and update them 19681 * Note that ARP and redirect timers are not 19682 * needed since NUD handles stale entries. 19683 */ 19684 flush_flag = FLUSH_MTU_TIME; 19685 ire_walk_v6(ire_expire, (char *)(uintptr_t)flush_flag, 19686 ALL_ZONES); 19687 } 19688 19689 ip_ire_arp_time_elapsed += ip_timer_interval; 19690 ip_ire_rd_time_elapsed += ip_timer_interval; 19691 ip_ire_pmtu_time_elapsed += ip_timer_interval; 19692 19693 /* 19694 * Hold the lock to serialize timeout calls and prevent 19695 * stale values in ip_ire_expire_id. Otherwise it is possible 19696 * for the timer to fire and a new invocation of this function 19697 * to start before the return value of timeout has been stored 19698 * in ip_ire_expire_id by the current invocation. 19699 */ 19700 mutex_enter(&ip_trash_timer_lock); 19701 ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL, 19702 MSEC_TO_TICK(ip_timer_interval)); 19703 mutex_exit(&ip_trash_timer_lock); 19704 } 19705 19706 /* 19707 * Called by the memory allocator subsystem directly, when the system 19708 * is running low on memory. 19709 */ 19710 /* ARGSUSED */ 19711 void 19712 ip_trash_ire_reclaim(void *args) 19713 { 19714 ire_cache_count_t icc; 19715 ire_cache_reclaim_t icr; 19716 ncc_cache_count_t ncc; 19717 nce_cache_reclaim_t ncr; 19718 uint_t delete_cnt; 19719 /* 19720 * Memory reclaim call back. 19721 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 19722 * Then, with a target of freeing 1/Nth of IRE_CACHE 19723 * entries, determine what fraction to free for 19724 * each category of IRE_CACHE entries giving absolute priority 19725 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 19726 * entry will be freed unless all offlink entries are freed). 19727 */ 19728 icc.icc_total = 0; 19729 icc.icc_unused = 0; 19730 icc.icc_offlink = 0; 19731 icc.icc_pmtu = 0; 19732 icc.icc_onlink = 0; 19733 ire_walk(ire_cache_count, (char *)&icc); 19734 19735 /* 19736 * Free NCEs for IPv6 like the onlink ires. 19737 */ 19738 ncc.ncc_total = 0; 19739 ncc.ncc_host = 0; 19740 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc); 19741 19742 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 19743 icc.icc_pmtu + icc.icc_onlink); 19744 delete_cnt = icc.icc_total/ip_ire_reclaim_fraction; 19745 IP_STAT(ip_trash_ire_reclaim_calls); 19746 if (delete_cnt == 0) 19747 return; 19748 IP_STAT(ip_trash_ire_reclaim_success); 19749 /* Always delete all unused offlink entries */ 19750 icr.icr_unused = 1; 19751 if (delete_cnt <= icc.icc_unused) { 19752 /* 19753 * Only need to free unused entries. In other words, 19754 * there are enough unused entries to free to meet our 19755 * target number of freed ire cache entries. 19756 */ 19757 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 19758 ncr.ncr_host = 0; 19759 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 19760 /* 19761 * Only need to free unused entries, plus a fraction of offlink 19762 * entries. It follows from the first if statement that 19763 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 19764 */ 19765 delete_cnt -= icc.icc_unused; 19766 /* Round up # deleted by truncating fraction */ 19767 icr.icr_offlink = icc.icc_offlink / delete_cnt; 19768 icr.icr_pmtu = icr.icr_onlink = 0; 19769 ncr.ncr_host = 0; 19770 } else if (delete_cnt <= 19771 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 19772 /* 19773 * Free all unused and offlink entries, plus a fraction of 19774 * pmtu entries. It follows from the previous if statement 19775 * that icc_pmtu is non-zero, and that 19776 * delete_cnt != icc_unused + icc_offlink. 19777 */ 19778 icr.icr_offlink = 1; 19779 delete_cnt -= icc.icc_unused + icc.icc_offlink; 19780 /* Round up # deleted by truncating fraction */ 19781 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 19782 icr.icr_onlink = 0; 19783 ncr.ncr_host = 0; 19784 } else { 19785 /* 19786 * Free all unused, offlink, and pmtu entries, plus a fraction 19787 * of onlink entries. If we're here, then we know that 19788 * icc_onlink is non-zero, and that 19789 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 19790 */ 19791 icr.icr_offlink = icr.icr_pmtu = 1; 19792 delete_cnt -= icc.icc_unused + icc.icc_offlink + 19793 icc.icc_pmtu; 19794 /* Round up # deleted by truncating fraction */ 19795 icr.icr_onlink = icc.icc_onlink / delete_cnt; 19796 /* Using the same delete fraction as for onlink IREs */ 19797 ncr.ncr_host = ncc.ncc_host / delete_cnt; 19798 } 19799 #ifdef DEBUG 19800 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 19801 "fractions %d/%d/%d/%d\n", 19802 icc.icc_total/ip_ire_reclaim_fraction, icc.icc_total, 19803 icc.icc_unused, icc.icc_offlink, 19804 icc.icc_pmtu, icc.icc_onlink, 19805 icr.icr_unused, icr.icr_offlink, 19806 icr.icr_pmtu, icr.icr_onlink)); 19807 #endif 19808 ire_walk(ire_cache_reclaim, (char *)&icr); 19809 if (ncr.ncr_host != 0) 19810 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 19811 (uchar_t *)&ncr); 19812 #ifdef DEBUG 19813 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 19814 icc.icc_pmtu = 0; icc.icc_onlink = 0; 19815 ire_walk(ire_cache_count, (char *)&icc); 19816 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 19817 icc.icc_total, icc.icc_unused, icc.icc_offlink, 19818 icc.icc_pmtu, icc.icc_onlink)); 19819 #endif 19820 } 19821 19822 /* 19823 * ip_unbind is called when a copy of an unbind request is received from the 19824 * upper level protocol. We remove this conn from any fanout hash list it is 19825 * on, and zero out the bind information. No reply is expected up above. 19826 */ 19827 mblk_t * 19828 ip_unbind(queue_t *q, mblk_t *mp) 19829 { 19830 conn_t *connp = Q_TO_CONN(q); 19831 19832 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 19833 19834 if (is_system_labeled() && connp->conn_anon_port) { 19835 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 19836 connp->conn_mlp_type, connp->conn_ulp, 19837 ntohs(connp->conn_lport), B_FALSE); 19838 connp->conn_anon_port = 0; 19839 } 19840 connp->conn_mlp_type = mlptSingle; 19841 19842 ipcl_hash_remove(connp); 19843 19844 ASSERT(mp->b_cont == NULL); 19845 /* 19846 * Convert mp into a T_OK_ACK 19847 */ 19848 mp = mi_tpi_ok_ack_alloc(mp); 19849 19850 /* 19851 * should not happen in practice... T_OK_ACK is smaller than the 19852 * original message. 19853 */ 19854 if (mp == NULL) 19855 return (NULL); 19856 19857 /* 19858 * Don't bzero the ports if its TCP since TCP still needs the 19859 * lport to remove it from its own bind hash. TCP will do the 19860 * cleanup. 19861 */ 19862 if (!IPCL_IS_TCP(connp)) 19863 bzero(&connp->u_port, sizeof (connp->u_port)); 19864 19865 return (mp); 19866 } 19867 19868 /* 19869 * Write side put procedure. Outbound data, IOCTLs, responses from 19870 * resolvers, etc, come down through here. 19871 * 19872 * arg2 is always a queue_t *. 19873 * When that queue is an ill_t (i.e. q_next != NULL), then arg must be 19874 * the zoneid. 19875 * When that queue is not an ill_t, then arg must be a conn_t pointer. 19876 */ 19877 void 19878 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 19879 { 19880 ip_output_options(arg, mp, arg2, caller, &zero_info); 19881 } 19882 19883 void 19884 ip_output_options(void *arg, mblk_t *mp, void *arg2, int caller, 19885 ip_opt_info_t *infop) 19886 { 19887 conn_t *connp = NULL; 19888 queue_t *q = (queue_t *)arg2; 19889 ipha_t *ipha; 19890 #define rptr ((uchar_t *)ipha) 19891 ire_t *ire = NULL; 19892 ire_t *sctp_ire = NULL; 19893 uint32_t v_hlen_tos_len; 19894 ipaddr_t dst; 19895 mblk_t *first_mp = NULL; 19896 boolean_t mctl_present; 19897 ipsec_out_t *io; 19898 int match_flags; 19899 ill_t *attach_ill = NULL; 19900 /* Bind to IPIF_NOFAILOVER ill etc. */ 19901 ill_t *xmit_ill = NULL; /* IP_XMIT_IF etc. */ 19902 ipif_t *dst_ipif; 19903 boolean_t multirt_need_resolve = B_FALSE; 19904 mblk_t *copy_mp = NULL; 19905 int err; 19906 zoneid_t zoneid; 19907 int adjust; 19908 uint16_t iplen; 19909 boolean_t need_decref = B_FALSE; 19910 boolean_t ignore_dontroute = B_FALSE; 19911 boolean_t ignore_nexthop = B_FALSE; 19912 boolean_t ip_nexthop = B_FALSE; 19913 ipaddr_t nexthop_addr; 19914 19915 #ifdef _BIG_ENDIAN 19916 #define V_HLEN (v_hlen_tos_len >> 24) 19917 #else 19918 #define V_HLEN (v_hlen_tos_len & 0xFF) 19919 #endif 19920 19921 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 19922 "ip_wput_start: q %p", q); 19923 19924 /* 19925 * ip_wput fast path 19926 */ 19927 19928 /* is packet from ARP ? */ 19929 if (q->q_next != NULL) { 19930 zoneid = (zoneid_t)(uintptr_t)arg; 19931 goto qnext; 19932 } 19933 19934 connp = (conn_t *)arg; 19935 ASSERT(connp != NULL); 19936 zoneid = connp->conn_zoneid; 19937 19938 /* is queue flow controlled? */ 19939 if ((q->q_first != NULL || connp->conn_draining) && 19940 (caller == IP_WPUT)) { 19941 ASSERT(!need_decref); 19942 (void) putq(q, mp); 19943 return; 19944 } 19945 19946 /* Multidata transmit? */ 19947 if (DB_TYPE(mp) == M_MULTIDATA) { 19948 /* 19949 * We should never get here, since all Multidata messages 19950 * originating from tcp should have been directed over to 19951 * tcp_multisend() in the first place. 19952 */ 19953 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 19954 freemsg(mp); 19955 return; 19956 } else if (DB_TYPE(mp) != M_DATA) 19957 goto notdata; 19958 19959 if (mp->b_flag & MSGHASREF) { 19960 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 19961 mp->b_flag &= ~MSGHASREF; 19962 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 19963 need_decref = B_TRUE; 19964 } 19965 ipha = (ipha_t *)mp->b_rptr; 19966 19967 /* is IP header non-aligned or mblk smaller than basic IP header */ 19968 #ifndef SAFETY_BEFORE_SPEED 19969 if (!OK_32PTR(rptr) || 19970 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 19971 goto hdrtoosmall; 19972 #endif 19973 19974 ASSERT(OK_32PTR(ipha)); 19975 19976 /* 19977 * This function assumes that mp points to an IPv4 packet. If it's the 19978 * wrong version, we'll catch it again in ip_output_v6. 19979 * 19980 * Note that this is *only* locally-generated output here, and never 19981 * forwarded data, and that we need to deal only with transports that 19982 * don't know how to label. (TCP, UDP, and ICMP/raw-IP all know how to 19983 * label.) 19984 */ 19985 if (is_system_labeled() && 19986 (ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4) && 19987 !connp->conn_ulp_labeled) { 19988 err = tsol_check_label(BEST_CRED(mp, connp), &mp, &adjust, 19989 connp->conn_mac_exempt); 19990 ipha = (ipha_t *)mp->b_rptr; 19991 if (err != 0) { 19992 first_mp = mp; 19993 if (err == EINVAL) 19994 goto icmp_parameter_problem; 19995 ip2dbg(("ip_wput: label check failed (%d)\n", err)); 19996 goto discard_pkt; 19997 } 19998 iplen = ntohs(ipha->ipha_length) + adjust; 19999 ipha->ipha_length = htons(iplen); 20000 } 20001 20002 ASSERT(infop != NULL); 20003 20004 if (infop->ip_opt_flags & IP_VERIFY_SRC) { 20005 /* 20006 * IP_PKTINFO ancillary option is present. 20007 * IPCL_ZONEID is used to honor IP_ALLZONES option which 20008 * allows using address of any zone as the source address. 20009 */ 20010 ire = ire_ctable_lookup(ipha->ipha_src, 0, 20011 (IRE_LOCAL|IRE_LOOPBACK), NULL, IPCL_ZONEID(connp), 20012 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY); 20013 if (ire == NULL) 20014 goto drop_pkt; 20015 ire_refrele(ire); 20016 ire = NULL; 20017 } 20018 20019 /* 20020 * IP_DONTFAILOVER_IF and IP_XMIT_IF have precedence over 20021 * ill index passed in IP_PKTINFO. 20022 */ 20023 if (infop->ip_opt_ill_index != 0 && 20024 connp->conn_xmit_if_ill == NULL && 20025 connp->conn_nofailover_ill == NULL) { 20026 20027 xmit_ill = ill_lookup_on_ifindex( 20028 infop->ip_opt_ill_index, B_FALSE, NULL, NULL, NULL, NULL); 20029 20030 if (xmit_ill == NULL || IS_VNI(xmit_ill)) 20031 goto drop_pkt; 20032 /* 20033 * check that there is an ipif belonging 20034 * to our zone. IPCL_ZONEID is not used because 20035 * IP_ALLZONES option is valid only when the ill is 20036 * accessible from all zones i.e has a valid ipif in 20037 * all zones. 20038 */ 20039 if (!ipif_lookup_zoneid_group(xmit_ill, zoneid, 0, NULL)) { 20040 goto drop_pkt; 20041 } 20042 } 20043 20044 /* 20045 * If there is a policy, try to attach an ipsec_out in 20046 * the front. At the end, first_mp either points to a 20047 * M_DATA message or IPSEC_OUT message linked to a 20048 * M_DATA message. We have to do it now as we might 20049 * lose the "conn" if we go through ip_newroute. 20050 */ 20051 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 20052 if (((mp = ipsec_attach_ipsec_out(mp, connp, NULL, 20053 ipha->ipha_protocol)) == NULL)) { 20054 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 20055 if (need_decref) 20056 CONN_DEC_REF(connp); 20057 return; 20058 } else { 20059 ASSERT(mp->b_datap->db_type == M_CTL); 20060 first_mp = mp; 20061 mp = mp->b_cont; 20062 mctl_present = B_TRUE; 20063 } 20064 } else { 20065 first_mp = mp; 20066 mctl_present = B_FALSE; 20067 } 20068 20069 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20070 20071 /* is wrong version or IP options present */ 20072 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 20073 goto version_hdrlen_check; 20074 dst = ipha->ipha_dst; 20075 20076 if (connp->conn_nofailover_ill != NULL) { 20077 attach_ill = conn_get_held_ill(connp, 20078 &connp->conn_nofailover_ill, &err); 20079 if (err == ILL_LOOKUP_FAILED) { 20080 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 20081 if (need_decref) 20082 CONN_DEC_REF(connp); 20083 freemsg(first_mp); 20084 return; 20085 } 20086 } 20087 20088 20089 /* is packet multicast? */ 20090 if (CLASSD(dst)) 20091 goto multicast; 20092 20093 /* 20094 * If xmit_ill is set above due to index passed in ip_pkt_info. It 20095 * takes precedence over conn_dontroute and conn_nexthop_set 20096 */ 20097 if (xmit_ill != NULL) { 20098 goto send_from_ill; 20099 } 20100 20101 if ((connp->conn_dontroute) || (connp->conn_xmit_if_ill != NULL) || 20102 (connp->conn_nexthop_set)) { 20103 /* 20104 * If the destination is a broadcast or a loopback 20105 * address, SO_DONTROUTE, IP_XMIT_IF and IP_NEXTHOP go 20106 * through the standard path. But in the case of local 20107 * destination only SO_DONTROUTE and IP_NEXTHOP go through 20108 * the standard path not IP_XMIT_IF. 20109 */ 20110 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 20111 if ((ire == NULL) || ((ire->ire_type != IRE_BROADCAST) && 20112 (ire->ire_type != IRE_LOOPBACK))) { 20113 if ((connp->conn_dontroute || 20114 connp->conn_nexthop_set) && (ire != NULL) && 20115 (ire->ire_type == IRE_LOCAL)) 20116 goto standard_path; 20117 20118 if (ire != NULL) { 20119 ire_refrele(ire); 20120 /* No more access to ire */ 20121 ire = NULL; 20122 } 20123 /* 20124 * bypass routing checks and go directly to 20125 * interface. 20126 */ 20127 if (connp->conn_dontroute) { 20128 goto dontroute; 20129 } else if (connp->conn_nexthop_set) { 20130 ip_nexthop = B_TRUE; 20131 nexthop_addr = connp->conn_nexthop_v4; 20132 goto send_from_ill; 20133 } 20134 20135 /* 20136 * If IP_XMIT_IF socket option is set, 20137 * then we allow unicast and multicast 20138 * packets to go through the ill. It is 20139 * quite possible that the destination 20140 * is not in the ire cache table and we 20141 * do not want to go to ip_newroute() 20142 * instead we call ip_newroute_ipif. 20143 */ 20144 xmit_ill = conn_get_held_ill(connp, 20145 &connp->conn_xmit_if_ill, &err); 20146 if (err == ILL_LOOKUP_FAILED) { 20147 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 20148 if (attach_ill != NULL) 20149 ill_refrele(attach_ill); 20150 if (need_decref) 20151 CONN_DEC_REF(connp); 20152 freemsg(first_mp); 20153 return; 20154 } 20155 goto send_from_ill; 20156 } 20157 standard_path: 20158 /* Must be a broadcast, a loopback or a local ire */ 20159 if (ire != NULL) { 20160 ire_refrele(ire); 20161 /* No more access to ire */ 20162 ire = NULL; 20163 } 20164 } 20165 20166 if (attach_ill != NULL) 20167 goto send_from_ill; 20168 20169 /* 20170 * We cache IRE_CACHEs to avoid lookups. We don't do 20171 * this for the tcp global queue and listen end point 20172 * as it does not really have a real destination to 20173 * talk to. This is also true for SCTP. 20174 */ 20175 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 20176 !connp->conn_fully_bound) { 20177 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 20178 if (ire == NULL) 20179 goto noirefound; 20180 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20181 "ip_wput_end: q %p (%S)", q, "end"); 20182 20183 /* 20184 * Check if the ire has the RTF_MULTIRT flag, inherited 20185 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20186 */ 20187 if (ire->ire_flags & RTF_MULTIRT) { 20188 20189 /* 20190 * Force the TTL of multirouted packets if required. 20191 * The TTL of such packets is bounded by the 20192 * ip_multirt_ttl ndd variable. 20193 */ 20194 if ((ip_multirt_ttl > 0) && 20195 (ipha->ipha_ttl > ip_multirt_ttl)) { 20196 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20197 "(was %d), dst 0x%08x\n", 20198 ip_multirt_ttl, ipha->ipha_ttl, 20199 ntohl(ire->ire_addr))); 20200 ipha->ipha_ttl = ip_multirt_ttl; 20201 } 20202 /* 20203 * We look at this point if there are pending 20204 * unresolved routes. ire_multirt_resolvable() 20205 * checks in O(n) that all IRE_OFFSUBNET ire 20206 * entries for the packet's destination and 20207 * flagged RTF_MULTIRT are currently resolved. 20208 * If some remain unresolved, we make a copy 20209 * of the current message. It will be used 20210 * to initiate additional route resolutions. 20211 */ 20212 multirt_need_resolve = 20213 ire_multirt_need_resolve(ire->ire_addr, 20214 MBLK_GETLABEL(first_mp)); 20215 ip2dbg(("ip_wput[TCP]: ire %p, " 20216 "multirt_need_resolve %d, first_mp %p\n", 20217 (void *)ire, multirt_need_resolve, 20218 (void *)first_mp)); 20219 if (multirt_need_resolve) { 20220 copy_mp = copymsg(first_mp); 20221 if (copy_mp != NULL) { 20222 MULTIRT_DEBUG_TAG(copy_mp); 20223 } 20224 } 20225 } 20226 20227 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20228 20229 /* 20230 * Try to resolve another multiroute if 20231 * ire_multirt_need_resolve() deemed it necessary. 20232 */ 20233 if (copy_mp != NULL) { 20234 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid); 20235 } 20236 if (need_decref) 20237 CONN_DEC_REF(connp); 20238 return; 20239 } 20240 20241 /* 20242 * Access to conn_ire_cache. (protected by conn_lock) 20243 * 20244 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 20245 * the ire bucket lock here to check for CONDEMNED as it is okay to 20246 * send a packet or two with the IRE_CACHE that is going away. 20247 * Access to the ire requires an ire refhold on the ire prior to 20248 * its use since an interface unplumb thread may delete the cached 20249 * ire and release the refhold at any time. 20250 * 20251 * Caching an ire in the conn_ire_cache 20252 * 20253 * o Caching an ire pointer in the conn requires a strict check for 20254 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 20255 * ires before cleaning up the conns. So the caching of an ire pointer 20256 * in the conn is done after making sure under the bucket lock that the 20257 * ire has not yet been marked CONDEMNED. Otherwise we will end up 20258 * caching an ire after the unplumb thread has cleaned up the conn. 20259 * If the conn does not send a packet subsequently the unplumb thread 20260 * will be hanging waiting for the ire count to drop to zero. 20261 * 20262 * o We also need to atomically test for a null conn_ire_cache and 20263 * set the conn_ire_cache under the the protection of the conn_lock 20264 * to avoid races among concurrent threads trying to simultaneously 20265 * cache an ire in the conn_ire_cache. 20266 */ 20267 mutex_enter(&connp->conn_lock); 20268 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 20269 20270 if (ire != NULL && ire->ire_addr == dst && 20271 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20272 20273 IRE_REFHOLD(ire); 20274 mutex_exit(&connp->conn_lock); 20275 20276 } else { 20277 boolean_t cached = B_FALSE; 20278 connp->conn_ire_cache = NULL; 20279 mutex_exit(&connp->conn_lock); 20280 /* Release the old ire */ 20281 if (ire != NULL && sctp_ire == NULL) 20282 IRE_REFRELE_NOTR(ire); 20283 20284 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 20285 if (ire == NULL) 20286 goto noirefound; 20287 IRE_REFHOLD_NOTR(ire); 20288 20289 mutex_enter(&connp->conn_lock); 20290 if (!(connp->conn_state_flags & CONN_CLOSING) && 20291 connp->conn_ire_cache == NULL) { 20292 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 20293 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 20294 connp->conn_ire_cache = ire; 20295 cached = B_TRUE; 20296 } 20297 rw_exit(&ire->ire_bucket->irb_lock); 20298 } 20299 mutex_exit(&connp->conn_lock); 20300 20301 /* 20302 * We can continue to use the ire but since it was 20303 * not cached, we should drop the extra reference. 20304 */ 20305 if (!cached) 20306 IRE_REFRELE_NOTR(ire); 20307 } 20308 20309 20310 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20311 "ip_wput_end: q %p (%S)", q, "end"); 20312 20313 /* 20314 * Check if the ire has the RTF_MULTIRT flag, inherited 20315 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 20316 */ 20317 if (ire->ire_flags & RTF_MULTIRT) { 20318 20319 /* 20320 * Force the TTL of multirouted packets if required. 20321 * The TTL of such packets is bounded by the 20322 * ip_multirt_ttl ndd variable. 20323 */ 20324 if ((ip_multirt_ttl > 0) && 20325 (ipha->ipha_ttl > ip_multirt_ttl)) { 20326 ip2dbg(("ip_wput: forcing multirt TTL to %d " 20327 "(was %d), dst 0x%08x\n", 20328 ip_multirt_ttl, ipha->ipha_ttl, 20329 ntohl(ire->ire_addr))); 20330 ipha->ipha_ttl = ip_multirt_ttl; 20331 } 20332 20333 /* 20334 * At this point, we check to see if there are any pending 20335 * unresolved routes. ire_multirt_resolvable() 20336 * checks in O(n) that all IRE_OFFSUBNET ire 20337 * entries for the packet's destination and 20338 * flagged RTF_MULTIRT are currently resolved. 20339 * If some remain unresolved, we make a copy 20340 * of the current message. It will be used 20341 * to initiate additional route resolutions. 20342 */ 20343 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 20344 MBLK_GETLABEL(first_mp)); 20345 ip2dbg(("ip_wput[not TCP]: ire %p, " 20346 "multirt_need_resolve %d, first_mp %p\n", 20347 (void *)ire, multirt_need_resolve, (void *)first_mp)); 20348 if (multirt_need_resolve) { 20349 copy_mp = copymsg(first_mp); 20350 if (copy_mp != NULL) { 20351 MULTIRT_DEBUG_TAG(copy_mp); 20352 } 20353 } 20354 } 20355 20356 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 20357 20358 /* 20359 * Try to resolve another multiroute if 20360 * ire_multirt_resolvable() deemed it necessary 20361 */ 20362 if (copy_mp != NULL) { 20363 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid); 20364 } 20365 if (need_decref) 20366 CONN_DEC_REF(connp); 20367 return; 20368 20369 qnext: 20370 /* 20371 * Upper Level Protocols pass down complete IP datagrams 20372 * as M_DATA messages. Everything else is a sideshow. 20373 * 20374 * 1) We could be re-entering ip_wput because of ip_neworute 20375 * in which case we could have a IPSEC_OUT message. We 20376 * need to pass through ip_wput like other datagrams and 20377 * hence cannot branch to ip_wput_nondata. 20378 * 20379 * 2) ARP, AH, ESP, and other clients who are on the module 20380 * instance of IP stream, give us something to deal with. 20381 * We will handle AH and ESP here and rest in ip_wput_nondata. 20382 * 20383 * 3) ICMP replies also could come here. 20384 */ 20385 if (DB_TYPE(mp) != M_DATA) { 20386 notdata: 20387 if (DB_TYPE(mp) == M_CTL) { 20388 /* 20389 * M_CTL messages are used by ARP, AH and ESP to 20390 * communicate with IP. We deal with IPSEC_IN and 20391 * IPSEC_OUT here. ip_wput_nondata handles other 20392 * cases. 20393 */ 20394 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 20395 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 20396 first_mp = mp->b_cont; 20397 first_mp->b_flag &= ~MSGHASREF; 20398 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 20399 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 20400 CONN_DEC_REF(connp); 20401 connp = NULL; 20402 } 20403 if (ii->ipsec_info_type == IPSEC_IN) { 20404 /* 20405 * Either this message goes back to 20406 * IPSEC for further processing or to 20407 * ULP after policy checks. 20408 */ 20409 ip_fanout_proto_again(mp, NULL, NULL, NULL); 20410 return; 20411 } else if (ii->ipsec_info_type == IPSEC_OUT) { 20412 io = (ipsec_out_t *)ii; 20413 if (io->ipsec_out_proc_begin) { 20414 /* 20415 * IPSEC processing has already started. 20416 * Complete it. 20417 * IPQoS notes: We don't care what is 20418 * in ipsec_out_ill_index since this 20419 * won't be processed for IPQoS policies 20420 * in ipsec_out_process. 20421 */ 20422 ipsec_out_process(q, mp, NULL, 20423 io->ipsec_out_ill_index); 20424 return; 20425 } else { 20426 connp = (q->q_next != NULL) ? 20427 NULL : Q_TO_CONN(q); 20428 first_mp = mp; 20429 mp = mp->b_cont; 20430 mctl_present = B_TRUE; 20431 } 20432 zoneid = io->ipsec_out_zoneid; 20433 ASSERT(zoneid != ALL_ZONES); 20434 } else if (ii->ipsec_info_type == IPSEC_CTL) { 20435 /* 20436 * It's an IPsec control message requesting 20437 * an SADB update to be sent to the IPsec 20438 * hardware acceleration capable ills. 20439 */ 20440 ipsec_ctl_t *ipsec_ctl = 20441 (ipsec_ctl_t *)mp->b_rptr; 20442 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 20443 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 20444 mblk_t *cmp = mp->b_cont; 20445 20446 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 20447 ASSERT(cmp != NULL); 20448 20449 freeb(mp); 20450 ill_ipsec_capab_send_all(satype, cmp, sa); 20451 return; 20452 } else { 20453 /* 20454 * This must be ARP or special TSOL signaling. 20455 */ 20456 ip_wput_nondata(NULL, q, mp, NULL); 20457 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20458 "ip_wput_end: q %p (%S)", q, "nondata"); 20459 return; 20460 } 20461 } else { 20462 /* 20463 * This must be non-(ARP/AH/ESP) messages. 20464 */ 20465 ASSERT(!need_decref); 20466 ip_wput_nondata(NULL, q, mp, NULL); 20467 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20468 "ip_wput_end: q %p (%S)", q, "nondata"); 20469 return; 20470 } 20471 } else { 20472 first_mp = mp; 20473 mctl_present = B_FALSE; 20474 } 20475 20476 ASSERT(first_mp != NULL); 20477 /* 20478 * ICMP echo replies attach an ipsec_out and set ipsec_out_attach_if 20479 * to make sure that this packet goes out on the same interface it 20480 * came in. We handle that here. 20481 */ 20482 if (mctl_present) { 20483 uint_t ifindex; 20484 20485 io = (ipsec_out_t *)first_mp->b_rptr; 20486 if (io->ipsec_out_attach_if || 20487 io->ipsec_out_xmit_if || 20488 io->ipsec_out_ip_nexthop) { 20489 ill_t *ill; 20490 20491 /* 20492 * We may have lost the conn context if we are 20493 * coming here from ip_newroute(). Copy the 20494 * nexthop information. 20495 */ 20496 if (io->ipsec_out_ip_nexthop) { 20497 ip_nexthop = B_TRUE; 20498 nexthop_addr = io->ipsec_out_nexthop_addr; 20499 20500 ipha = (ipha_t *)mp->b_rptr; 20501 dst = ipha->ipha_dst; 20502 goto send_from_ill; 20503 } else { 20504 ASSERT(io->ipsec_out_ill_index != 0); 20505 ifindex = io->ipsec_out_ill_index; 20506 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 20507 NULL, NULL, NULL, NULL); 20508 /* 20509 * ipsec_out_xmit_if bit is used to tell 20510 * ip_wput to use the ill to send outgoing data 20511 * as we have no conn when data comes from ICMP 20512 * error msg routines. Currently this feature is 20513 * only used by ip_mrtun_forward routine. 20514 */ 20515 if (io->ipsec_out_xmit_if) { 20516 xmit_ill = ill; 20517 if (xmit_ill == NULL) { 20518 ip1dbg(("ip_output:bad ifindex " 20519 "for xmit_ill %d\n", 20520 ifindex)); 20521 freemsg(first_mp); 20522 BUMP_MIB(&ip_mib, 20523 ipIfStatsOutDiscards); 20524 ASSERT(!need_decref); 20525 return; 20526 } 20527 /* Free up the ipsec_out_t mblk */ 20528 ASSERT(first_mp->b_cont == mp); 20529 first_mp->b_cont = NULL; 20530 freeb(first_mp); 20531 /* Just send the IP header+ICMP+data */ 20532 first_mp = mp; 20533 ipha = (ipha_t *)mp->b_rptr; 20534 dst = ipha->ipha_dst; 20535 goto send_from_ill; 20536 } else { 20537 attach_ill = ill; 20538 } 20539 20540 if (attach_ill == NULL) { 20541 ASSERT(xmit_ill == NULL); 20542 ip1dbg(("ip_output: bad ifindex for " 20543 "(BIND TO IPIF_NOFAILOVER) %d\n", 20544 ifindex)); 20545 freemsg(first_mp); 20546 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 20547 ASSERT(!need_decref); 20548 return; 20549 } 20550 } 20551 } 20552 } 20553 20554 ASSERT(xmit_ill == NULL); 20555 20556 /* We have a complete IP datagram heading outbound. */ 20557 ipha = (ipha_t *)mp->b_rptr; 20558 20559 #ifndef SPEED_BEFORE_SAFETY 20560 /* 20561 * Make sure we have a full-word aligned message and that at least 20562 * a simple IP header is accessible in the first message. If not, 20563 * try a pullup. 20564 */ 20565 if (!OK_32PTR(rptr) || 20566 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) { 20567 hdrtoosmall: 20568 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 20569 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20570 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 20571 if (first_mp == NULL) 20572 first_mp = mp; 20573 goto discard_pkt; 20574 } 20575 20576 /* This function assumes that mp points to an IPv4 packet. */ 20577 if (is_system_labeled() && q->q_next == NULL && 20578 (*mp->b_rptr & 0xf0) == (IPV4_VERSION << 4) && 20579 !connp->conn_ulp_labeled) { 20580 err = tsol_check_label(BEST_CRED(mp, connp), &mp, 20581 &adjust, connp->conn_mac_exempt); 20582 ipha = (ipha_t *)mp->b_rptr; 20583 if (first_mp != NULL) 20584 first_mp->b_cont = mp; 20585 if (err != 0) { 20586 if (first_mp == NULL) 20587 first_mp = mp; 20588 if (err == EINVAL) 20589 goto icmp_parameter_problem; 20590 ip2dbg(("ip_wput: label check failed (%d)\n", 20591 err)); 20592 goto discard_pkt; 20593 } 20594 iplen = ntohs(ipha->ipha_length) + adjust; 20595 ipha->ipha_length = htons(iplen); 20596 } 20597 20598 ipha = (ipha_t *)mp->b_rptr; 20599 if (first_mp == NULL) { 20600 ASSERT(attach_ill == NULL && xmit_ill == NULL); 20601 /* 20602 * If we got here because of "goto hdrtoosmall" 20603 * We need to attach a IPSEC_OUT. 20604 */ 20605 if (connp->conn_out_enforce_policy) { 20606 if (((mp = ipsec_attach_ipsec_out(mp, connp, 20607 NULL, ipha->ipha_protocol)) == NULL)) { 20608 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 20609 if (need_decref) 20610 CONN_DEC_REF(connp); 20611 return; 20612 } else { 20613 ASSERT(mp->b_datap->db_type == M_CTL); 20614 first_mp = mp; 20615 mp = mp->b_cont; 20616 mctl_present = B_TRUE; 20617 } 20618 } else { 20619 first_mp = mp; 20620 mctl_present = B_FALSE; 20621 } 20622 } 20623 } 20624 #endif 20625 20626 /* Most of the code below is written for speed, not readability */ 20627 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 20628 20629 /* 20630 * If ip_newroute() fails, we're going to need a full 20631 * header for the icmp wraparound. 20632 */ 20633 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 20634 uint_t v_hlen; 20635 version_hdrlen_check: 20636 ASSERT(first_mp != NULL); 20637 v_hlen = V_HLEN; 20638 /* 20639 * siphon off IPv6 packets coming down from transport 20640 * layer modules here. 20641 * Note: high-order bit carries NUD reachability confirmation 20642 */ 20643 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 20644 /* 20645 * XXX implement a IPv4 and IPv6 packet counter per 20646 * conn and switch when ratio exceeds e.g. 10:1 20647 */ 20648 #ifdef notyet 20649 if (q->q_next == NULL) /* Avoid ill queue */ 20650 ip_setqinfo(RD(q), B_TRUE, B_TRUE); 20651 #endif 20652 BUMP_MIB(&ip_mib, ipIfStatsOutWrongIPVersion); 20653 ASSERT(xmit_ill == NULL); 20654 if (attach_ill != NULL) 20655 ill_refrele(attach_ill); 20656 if (need_decref) 20657 mp->b_flag |= MSGHASREF; 20658 (void) ip_output_v6(arg, first_mp, arg2, caller); 20659 return; 20660 } 20661 20662 if ((v_hlen >> 4) != IP_VERSION) { 20663 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20664 "ip_wput_end: q %p (%S)", q, "badvers"); 20665 goto discard_pkt; 20666 } 20667 /* 20668 * Is the header length at least 20 bytes? 20669 * 20670 * Are there enough bytes accessible in the header? If 20671 * not, try a pullup. 20672 */ 20673 v_hlen &= 0xF; 20674 v_hlen <<= 2; 20675 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 20676 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20677 "ip_wput_end: q %p (%S)", q, "badlen"); 20678 goto discard_pkt; 20679 } 20680 if (v_hlen > (mp->b_wptr - rptr)) { 20681 if (!pullupmsg(mp, v_hlen)) { 20682 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20683 "ip_wput_end: q %p (%S)", q, "badpullup2"); 20684 goto discard_pkt; 20685 } 20686 ipha = (ipha_t *)mp->b_rptr; 20687 } 20688 /* 20689 * Move first entry from any source route into ipha_dst and 20690 * verify the options 20691 */ 20692 if (ip_wput_options(q, first_mp, ipha, mctl_present, zoneid)) { 20693 ASSERT(xmit_ill == NULL); 20694 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 20695 if (attach_ill != NULL) 20696 ill_refrele(attach_ill); 20697 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20698 "ip_wput_end: q %p (%S)", q, "badopts"); 20699 if (need_decref) 20700 CONN_DEC_REF(connp); 20701 return; 20702 } 20703 } 20704 dst = ipha->ipha_dst; 20705 20706 /* 20707 * Try to get an IRE_CACHE for the destination address. If we can't, 20708 * we have to run the packet through ip_newroute which will take 20709 * the appropriate action to arrange for an IRE_CACHE, such as querying 20710 * a resolver, or assigning a default gateway, etc. 20711 */ 20712 if (CLASSD(dst)) { 20713 ipif_t *ipif; 20714 uint32_t setsrc = 0; 20715 20716 multicast: 20717 ASSERT(first_mp != NULL); 20718 ip2dbg(("ip_wput: CLASSD\n")); 20719 if (connp == NULL) { 20720 /* 20721 * Use the first good ipif on the ill. 20722 * XXX Should this ever happen? (Appears 20723 * to show up with just ppp and no ethernet due 20724 * to in.rdisc.) 20725 * However, ire_send should be able to 20726 * call ip_wput_ire directly. 20727 * 20728 * XXX Also, this can happen for ICMP and other packets 20729 * with multicast source addresses. Perhaps we should 20730 * fix things so that we drop the packet in question, 20731 * but for now, just run with it. 20732 */ 20733 ill_t *ill = (ill_t *)q->q_ptr; 20734 20735 /* 20736 * Don't honor attach_if for this case. If ill 20737 * is part of the group, ipif could belong to 20738 * any ill and we cannot maintain attach_ill 20739 * and ipif_ill same anymore and the assert 20740 * below would fail. 20741 */ 20742 if (mctl_present && io->ipsec_out_attach_if) { 20743 io->ipsec_out_ill_index = 0; 20744 io->ipsec_out_attach_if = B_FALSE; 20745 ASSERT(attach_ill != NULL); 20746 ill_refrele(attach_ill); 20747 attach_ill = NULL; 20748 } 20749 20750 ASSERT(attach_ill == NULL); 20751 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 20752 if (ipif == NULL) { 20753 if (need_decref) 20754 CONN_DEC_REF(connp); 20755 freemsg(first_mp); 20756 return; 20757 } 20758 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 20759 ntohl(dst), ill->ill_name)); 20760 } else { 20761 /* 20762 * The order of precedence is IP_XMIT_IF, IP_PKTINFO 20763 * and IP_MULTICAST_IF. 20764 * Block comment above this function explains the 20765 * locking mechanism used here 20766 */ 20767 if (xmit_ill == NULL) { 20768 xmit_ill = conn_get_held_ill(connp, 20769 &connp->conn_xmit_if_ill, &err); 20770 if (err == ILL_LOOKUP_FAILED) { 20771 ip1dbg(("ip_wput: No ill for " 20772 "IP_XMIT_IF\n")); 20773 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 20774 goto drop_pkt; 20775 } 20776 } 20777 20778 if (xmit_ill == NULL) { 20779 ipif = conn_get_held_ipif(connp, 20780 &connp->conn_multicast_ipif, &err); 20781 if (err == IPIF_LOOKUP_FAILED) { 20782 ip1dbg(("ip_wput: No ipif for " 20783 "multicast\n")); 20784 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 20785 goto drop_pkt; 20786 } 20787 } 20788 if (xmit_ill != NULL) { 20789 ipif = ipif_get_next_ipif(NULL, xmit_ill); 20790 if (ipif == NULL) { 20791 ip1dbg(("ip_wput: No ipif for " 20792 "IP_XMIT_IF\n")); 20793 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 20794 goto drop_pkt; 20795 } 20796 } else if (ipif == NULL || ipif->ipif_isv6) { 20797 /* 20798 * We must do this ipif determination here 20799 * else we could pass through ip_newroute 20800 * and come back here without the conn context. 20801 * 20802 * Note: we do late binding i.e. we bind to 20803 * the interface when the first packet is sent. 20804 * For performance reasons we do not rebind on 20805 * each packet but keep the binding until the 20806 * next IP_MULTICAST_IF option. 20807 * 20808 * conn_multicast_{ipif,ill} are shared between 20809 * IPv4 and IPv6 and AF_INET6 sockets can 20810 * send both IPv4 and IPv6 packets. Hence 20811 * we have to check that "isv6" matches above. 20812 */ 20813 if (ipif != NULL) 20814 ipif_refrele(ipif); 20815 ipif = ipif_lookup_group(dst, zoneid); 20816 if (ipif == NULL) { 20817 ip1dbg(("ip_wput: No ipif for " 20818 "multicast\n")); 20819 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 20820 goto drop_pkt; 20821 } 20822 err = conn_set_held_ipif(connp, 20823 &connp->conn_multicast_ipif, ipif); 20824 if (err == IPIF_LOOKUP_FAILED) { 20825 ipif_refrele(ipif); 20826 ip1dbg(("ip_wput: No ipif for " 20827 "multicast\n")); 20828 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 20829 goto drop_pkt; 20830 } 20831 } 20832 } 20833 ASSERT(!ipif->ipif_isv6); 20834 /* 20835 * As we may lose the conn by the time we reach ip_wput_ire, 20836 * we copy conn_multicast_loop and conn_dontroute on to an 20837 * ipsec_out. In case if this datagram goes out secure, 20838 * we need the ill_index also. Copy that also into the 20839 * ipsec_out. 20840 */ 20841 if (mctl_present) { 20842 io = (ipsec_out_t *)first_mp->b_rptr; 20843 ASSERT(first_mp->b_datap->db_type == M_CTL); 20844 ASSERT(io->ipsec_out_type == IPSEC_OUT); 20845 } else { 20846 ASSERT(mp == first_mp); 20847 if ((first_mp = allocb(sizeof (ipsec_info_t), 20848 BPRI_HI)) == NULL) { 20849 ipif_refrele(ipif); 20850 first_mp = mp; 20851 goto discard_pkt; 20852 } 20853 first_mp->b_datap->db_type = M_CTL; 20854 first_mp->b_wptr += sizeof (ipsec_info_t); 20855 /* ipsec_out_secure is B_FALSE now */ 20856 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 20857 io = (ipsec_out_t *)first_mp->b_rptr; 20858 io->ipsec_out_type = IPSEC_OUT; 20859 io->ipsec_out_len = sizeof (ipsec_out_t); 20860 io->ipsec_out_use_global_policy = B_TRUE; 20861 first_mp->b_cont = mp; 20862 mctl_present = B_TRUE; 20863 } 20864 if (attach_ill != NULL) { 20865 ASSERT(attach_ill == ipif->ipif_ill); 20866 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 20867 20868 /* 20869 * Check if we need an ire that will not be 20870 * looked up by anybody else i.e. HIDDEN. 20871 */ 20872 if (ill_is_probeonly(attach_ill)) { 20873 match_flags |= MATCH_IRE_MARK_HIDDEN; 20874 } 20875 io->ipsec_out_ill_index = 20876 attach_ill->ill_phyint->phyint_ifindex; 20877 io->ipsec_out_attach_if = B_TRUE; 20878 } else { 20879 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 20880 io->ipsec_out_ill_index = 20881 ipif->ipif_ill->ill_phyint->phyint_ifindex; 20882 } 20883 if (connp != NULL) { 20884 io->ipsec_out_multicast_loop = 20885 connp->conn_multicast_loop; 20886 io->ipsec_out_dontroute = connp->conn_dontroute; 20887 io->ipsec_out_zoneid = connp->conn_zoneid; 20888 } 20889 /* 20890 * If the application uses IP_MULTICAST_IF with 20891 * different logical addresses of the same ILL, we 20892 * need to make sure that the soruce address of 20893 * the packet matches the logical IP address used 20894 * in the option. We do it by initializing ipha_src 20895 * here. This should keep IPSEC also happy as 20896 * when we return from IPSEC processing, we don't 20897 * have to worry about getting the right address on 20898 * the packet. Thus it is sufficient to look for 20899 * IRE_CACHE using MATCH_IRE_ILL rathen than 20900 * MATCH_IRE_IPIF. 20901 * 20902 * NOTE : We need to do it for non-secure case also as 20903 * this might go out secure if there is a global policy 20904 * match in ip_wput_ire. For bind to IPIF_NOFAILOVER 20905 * address, the source should be initialized already and 20906 * hence we won't be initializing here. 20907 * 20908 * As we do not have the ire yet, it is possible that 20909 * we set the source address here and then later discover 20910 * that the ire implies the source address to be assigned 20911 * through the RTF_SETSRC flag. 20912 * In that case, the setsrc variable will remind us 20913 * that overwritting the source address by the one 20914 * of the RTF_SETSRC-flagged ire is allowed. 20915 */ 20916 if (ipha->ipha_src == INADDR_ANY && 20917 (connp == NULL || !connp->conn_unspec_src)) { 20918 ipha->ipha_src = ipif->ipif_src_addr; 20919 setsrc = RTF_SETSRC; 20920 } 20921 /* 20922 * Find an IRE which matches the destination and the outgoing 20923 * queue (i.e. the outgoing interface.) 20924 * For loopback use a unicast IP address for 20925 * the ire lookup. 20926 */ 20927 if (ipif->ipif_ill->ill_phyint->phyint_flags & 20928 PHYI_LOOPBACK) { 20929 dst = ipif->ipif_lcl_addr; 20930 } 20931 /* 20932 * If IP_XMIT_IF is set, we branch out to ip_newroute_ipif. 20933 * We don't need to lookup ire in ctable as the packet 20934 * needs to be sent to the destination through the specified 20935 * ill irrespective of ires in the cache table. 20936 */ 20937 ire = NULL; 20938 if (xmit_ill == NULL) { 20939 ire = ire_ctable_lookup(dst, 0, 0, ipif, 20940 zoneid, MBLK_GETLABEL(mp), match_flags); 20941 } 20942 20943 /* 20944 * refrele attach_ill as its not needed anymore. 20945 */ 20946 if (attach_ill != NULL) { 20947 ill_refrele(attach_ill); 20948 attach_ill = NULL; 20949 } 20950 20951 if (ire == NULL) { 20952 /* 20953 * Multicast loopback and multicast forwarding is 20954 * done in ip_wput_ire. 20955 * 20956 * Mark this packet to make it be delivered to 20957 * ip_wput_ire after the new ire has been 20958 * created. 20959 * 20960 * The call to ip_newroute_ipif takes into account 20961 * the setsrc reminder. In any case, we take care 20962 * of the RTF_MULTIRT flag. 20963 */ 20964 mp->b_prev = mp->b_next = NULL; 20965 if (xmit_ill == NULL || 20966 xmit_ill->ill_ipif_up_count > 0) { 20967 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 20968 setsrc | RTF_MULTIRT, zoneid, infop); 20969 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 20970 "ip_wput_end: q %p (%S)", q, "noire"); 20971 } else { 20972 freemsg(first_mp); 20973 } 20974 ipif_refrele(ipif); 20975 if (xmit_ill != NULL) 20976 ill_refrele(xmit_ill); 20977 if (need_decref) 20978 CONN_DEC_REF(connp); 20979 return; 20980 } 20981 20982 ipif_refrele(ipif); 20983 ipif = NULL; 20984 ASSERT(xmit_ill == NULL); 20985 20986 /* 20987 * Honor the RTF_SETSRC flag for multicast packets, 20988 * if allowed by the setsrc reminder. 20989 */ 20990 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 20991 ipha->ipha_src = ire->ire_src_addr; 20992 } 20993 20994 /* 20995 * Unconditionally force the TTL to 1 for 20996 * multirouted multicast packets: 20997 * multirouted multicast should not cross 20998 * multicast routers. 20999 */ 21000 if (ire->ire_flags & RTF_MULTIRT) { 21001 if (ipha->ipha_ttl > 1) { 21002 ip2dbg(("ip_wput: forcing multicast " 21003 "multirt TTL to 1 (was %d), dst 0x%08x\n", 21004 ipha->ipha_ttl, ntohl(ire->ire_addr))); 21005 ipha->ipha_ttl = 1; 21006 } 21007 } 21008 } else { 21009 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 21010 if ((ire != NULL) && (ire->ire_type & 21011 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 21012 ignore_dontroute = B_TRUE; 21013 ignore_nexthop = B_TRUE; 21014 } 21015 if (ire != NULL) { 21016 ire_refrele(ire); 21017 ire = NULL; 21018 } 21019 /* 21020 * Guard against coming in from arp in which case conn is NULL. 21021 * Also guard against non M_DATA with dontroute set but 21022 * destined to local, loopback or broadcast addresses. 21023 */ 21024 if (connp != NULL && connp->conn_dontroute && 21025 !ignore_dontroute) { 21026 dontroute: 21027 /* 21028 * Set TTL to 1 if SO_DONTROUTE is set to prevent 21029 * routing protocols from seeing false direct 21030 * connectivity. 21031 */ 21032 ipha->ipha_ttl = 1; 21033 /* 21034 * If IP_XMIT_IF is also set (conn_xmit_if_ill != NULL) 21035 * along with SO_DONTROUTE, higher precedence is 21036 * given to IP_XMIT_IF and the IP_XMIT_IF ipif is used. 21037 */ 21038 if (connp->conn_xmit_if_ill == NULL) { 21039 /* If suitable ipif not found, drop packet */ 21040 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid); 21041 if (dst_ipif == NULL) { 21042 ip1dbg(("ip_wput: no route for " 21043 "dst using SO_DONTROUTE\n")); 21044 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 21045 mp->b_prev = mp->b_next = NULL; 21046 if (first_mp == NULL) 21047 first_mp = mp; 21048 goto drop_pkt; 21049 } else { 21050 /* 21051 * If suitable ipif has been found, set 21052 * xmit_ill to the corresponding 21053 * ipif_ill because we'll be following 21054 * the IP_XMIT_IF logic. 21055 */ 21056 ASSERT(xmit_ill == NULL); 21057 xmit_ill = dst_ipif->ipif_ill; 21058 mutex_enter(&xmit_ill->ill_lock); 21059 if (!ILL_CAN_LOOKUP(xmit_ill)) { 21060 mutex_exit(&xmit_ill->ill_lock); 21061 xmit_ill = NULL; 21062 ipif_refrele(dst_ipif); 21063 ip1dbg(("ip_wput: no route for" 21064 " dst using" 21065 " SO_DONTROUTE\n")); 21066 BUMP_MIB(&ip_mib, 21067 ipIfStatsOutNoRoutes); 21068 mp->b_prev = mp->b_next = NULL; 21069 if (first_mp == NULL) 21070 first_mp = mp; 21071 goto drop_pkt; 21072 } 21073 ill_refhold_locked(xmit_ill); 21074 mutex_exit(&xmit_ill->ill_lock); 21075 ipif_refrele(dst_ipif); 21076 } 21077 } 21078 21079 } 21080 /* 21081 * If we are bound to IPIF_NOFAILOVER address, look for 21082 * an IRE_CACHE matching the ill. 21083 */ 21084 send_from_ill: 21085 if (attach_ill != NULL) { 21086 ipif_t *attach_ipif; 21087 21088 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 21089 21090 /* 21091 * Check if we need an ire that will not be 21092 * looked up by anybody else i.e. HIDDEN. 21093 */ 21094 if (ill_is_probeonly(attach_ill)) { 21095 match_flags |= MATCH_IRE_MARK_HIDDEN; 21096 } 21097 21098 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 21099 if (attach_ipif == NULL) { 21100 ip1dbg(("ip_wput: No ipif for attach_ill\n")); 21101 goto discard_pkt; 21102 } 21103 ire = ire_ctable_lookup(dst, 0, 0, attach_ipif, 21104 zoneid, MBLK_GETLABEL(mp), match_flags); 21105 ipif_refrele(attach_ipif); 21106 } else if (xmit_ill != NULL || (connp != NULL && 21107 connp->conn_xmit_if_ill != NULL)) { 21108 /* 21109 * Mark this packet as originated locally 21110 */ 21111 mp->b_prev = mp->b_next = NULL; 21112 /* 21113 * xmit_ill could be NULL if SO_DONTROUTE 21114 * is also set. 21115 */ 21116 if (xmit_ill == NULL) { 21117 xmit_ill = conn_get_held_ill(connp, 21118 &connp->conn_xmit_if_ill, &err); 21119 if (err == ILL_LOOKUP_FAILED) { 21120 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 21121 if (need_decref) 21122 CONN_DEC_REF(connp); 21123 freemsg(first_mp); 21124 return; 21125 } 21126 if (xmit_ill == NULL) { 21127 if (connp->conn_dontroute) 21128 goto dontroute; 21129 goto send_from_ill; 21130 } 21131 } 21132 /* 21133 * Could be SO_DONTROUTE case also. 21134 * check at least one interface is UP as 21135 * specified by this ILL 21136 */ 21137 if (xmit_ill->ill_ipif_up_count > 0) { 21138 ipif_t *ipif; 21139 21140 ipif = ipif_get_next_ipif(NULL, xmit_ill); 21141 if (ipif == NULL) { 21142 ip1dbg(("ip_output: " 21143 "xmit_ill NULL ipif\n")); 21144 goto drop_pkt; 21145 } 21146 /* 21147 * Look for a ire that is part of the group, 21148 * if found use it else call ip_newroute_ipif. 21149 * IPCL_ZONEID is not used for matching because 21150 * IP_ALLZONES option is valid only when the 21151 * ill is accessible from all zones i.e has a 21152 * valid ipif in all zones. 21153 */ 21154 match_flags = MATCH_IRE_ILL_GROUP | 21155 MATCH_IRE_SECATTR; 21156 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 21157 MBLK_GETLABEL(mp), match_flags); 21158 /* 21159 * If an ire exists use it or else create 21160 * an ire but don't add it to the cache. 21161 * Adding an ire may cause issues with 21162 * asymmetric routing. 21163 * In case of multiroute always act as if 21164 * ire does not exist. 21165 */ 21166 if (ire == NULL || 21167 ire->ire_flags & RTF_MULTIRT) { 21168 if (ire != NULL) 21169 ire_refrele(ire); 21170 ip_newroute_ipif(q, first_mp, ipif, 21171 dst, connp, 0, zoneid, infop); 21172 ipif_refrele(ipif); 21173 ip1dbg(("ip_wput: ip_unicast_if\n")); 21174 ill_refrele(xmit_ill); 21175 if (need_decref) 21176 CONN_DEC_REF(connp); 21177 return; 21178 } 21179 ipif_refrele(ipif); 21180 } else { 21181 goto drop_pkt; 21182 } 21183 } else if (ip_nexthop || (connp != NULL && 21184 (connp->conn_nexthop_set)) && !ignore_nexthop) { 21185 if (!ip_nexthop) { 21186 ip_nexthop = B_TRUE; 21187 nexthop_addr = connp->conn_nexthop_v4; 21188 } 21189 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 21190 MATCH_IRE_GW; 21191 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 21192 NULL, zoneid, MBLK_GETLABEL(mp), match_flags); 21193 } else { 21194 ire = ire_cache_lookup(dst, zoneid, MBLK_GETLABEL(mp)); 21195 } 21196 if (!ire) { 21197 /* 21198 * Make sure we don't load spread if this 21199 * is IPIF_NOFAILOVER case. 21200 */ 21201 if ((attach_ill != NULL) || 21202 (ip_nexthop && !ignore_nexthop)) { 21203 if (mctl_present) { 21204 io = (ipsec_out_t *)first_mp->b_rptr; 21205 ASSERT(first_mp->b_datap->db_type == 21206 M_CTL); 21207 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21208 } else { 21209 ASSERT(mp == first_mp); 21210 first_mp = allocb( 21211 sizeof (ipsec_info_t), BPRI_HI); 21212 if (first_mp == NULL) { 21213 first_mp = mp; 21214 goto discard_pkt; 21215 } 21216 first_mp->b_datap->db_type = M_CTL; 21217 first_mp->b_wptr += 21218 sizeof (ipsec_info_t); 21219 /* ipsec_out_secure is B_FALSE now */ 21220 bzero(first_mp->b_rptr, 21221 sizeof (ipsec_info_t)); 21222 io = (ipsec_out_t *)first_mp->b_rptr; 21223 io->ipsec_out_type = IPSEC_OUT; 21224 io->ipsec_out_len = 21225 sizeof (ipsec_out_t); 21226 io->ipsec_out_use_global_policy = 21227 B_TRUE; 21228 first_mp->b_cont = mp; 21229 mctl_present = B_TRUE; 21230 } 21231 if (attach_ill != NULL) { 21232 io->ipsec_out_ill_index = attach_ill-> 21233 ill_phyint->phyint_ifindex; 21234 io->ipsec_out_attach_if = B_TRUE; 21235 } else { 21236 io->ipsec_out_ip_nexthop = ip_nexthop; 21237 io->ipsec_out_nexthop_addr = 21238 nexthop_addr; 21239 } 21240 } 21241 noirefound: 21242 /* 21243 * Mark this packet as having originated on 21244 * this machine. This will be noted in 21245 * ire_add_then_send, which needs to know 21246 * whether to run it back through ip_wput or 21247 * ip_rput following successful resolution. 21248 */ 21249 mp->b_prev = NULL; 21250 mp->b_next = NULL; 21251 ip_newroute(q, first_mp, dst, NULL, connp, zoneid); 21252 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21253 "ip_wput_end: q %p (%S)", q, "newroute"); 21254 if (attach_ill != NULL) 21255 ill_refrele(attach_ill); 21256 if (xmit_ill != NULL) 21257 ill_refrele(xmit_ill); 21258 if (need_decref) 21259 CONN_DEC_REF(connp); 21260 return; 21261 } 21262 } 21263 21264 /* We now know where we are going with it. */ 21265 21266 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21267 "ip_wput_end: q %p (%S)", q, "end"); 21268 21269 /* 21270 * Check if the ire has the RTF_MULTIRT flag, inherited 21271 * from an IRE_OFFSUBNET ire entry in ip_newroute. 21272 */ 21273 if (ire->ire_flags & RTF_MULTIRT) { 21274 /* 21275 * Force the TTL of multirouted packets if required. 21276 * The TTL of such packets is bounded by the 21277 * ip_multirt_ttl ndd variable. 21278 */ 21279 if ((ip_multirt_ttl > 0) && 21280 (ipha->ipha_ttl > ip_multirt_ttl)) { 21281 ip2dbg(("ip_wput: forcing multirt TTL to %d " 21282 "(was %d), dst 0x%08x\n", 21283 ip_multirt_ttl, ipha->ipha_ttl, 21284 ntohl(ire->ire_addr))); 21285 ipha->ipha_ttl = ip_multirt_ttl; 21286 } 21287 /* 21288 * At this point, we check to see if there are any pending 21289 * unresolved routes. ire_multirt_resolvable() 21290 * checks in O(n) that all IRE_OFFSUBNET ire 21291 * entries for the packet's destination and 21292 * flagged RTF_MULTIRT are currently resolved. 21293 * If some remain unresolved, we make a copy 21294 * of the current message. It will be used 21295 * to initiate additional route resolutions. 21296 */ 21297 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr, 21298 MBLK_GETLABEL(first_mp)); 21299 ip2dbg(("ip_wput[noirefound]: ire %p, " 21300 "multirt_need_resolve %d, first_mp %p\n", 21301 (void *)ire, multirt_need_resolve, (void *)first_mp)); 21302 if (multirt_need_resolve) { 21303 copy_mp = copymsg(first_mp); 21304 if (copy_mp != NULL) { 21305 MULTIRT_DEBUG_TAG(copy_mp); 21306 } 21307 } 21308 } 21309 21310 ip_wput_ire(q, first_mp, ire, connp, caller, zoneid); 21311 /* 21312 * Try to resolve another multiroute if 21313 * ire_multirt_resolvable() deemed it necessary. 21314 * At this point, we need to distinguish 21315 * multicasts from other packets. For multicasts, 21316 * we call ip_newroute_ipif() and request that both 21317 * multirouting and setsrc flags are checked. 21318 */ 21319 if (copy_mp != NULL) { 21320 if (CLASSD(dst)) { 21321 ipif_t *ipif = ipif_lookup_group(dst, zoneid); 21322 if (ipif) { 21323 ASSERT(infop->ip_opt_ill_index == 0); 21324 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 21325 RTF_SETSRC | RTF_MULTIRT, zoneid, infop); 21326 ipif_refrele(ipif); 21327 } else { 21328 MULTIRT_DEBUG_UNTAG(copy_mp); 21329 freemsg(copy_mp); 21330 copy_mp = NULL; 21331 } 21332 } else { 21333 ip_newroute(q, copy_mp, dst, NULL, connp, zoneid); 21334 } 21335 } 21336 if (attach_ill != NULL) 21337 ill_refrele(attach_ill); 21338 if (xmit_ill != NULL) 21339 ill_refrele(xmit_ill); 21340 if (need_decref) 21341 CONN_DEC_REF(connp); 21342 return; 21343 21344 icmp_parameter_problem: 21345 /* could not have originated externally */ 21346 ASSERT(mp->b_prev == NULL); 21347 if (ip_hdr_complete(ipha, zoneid) == 0) { 21348 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 21349 /* it's the IP header length that's in trouble */ 21350 icmp_param_problem(q, first_mp, 0, zoneid); 21351 first_mp = NULL; 21352 } 21353 21354 discard_pkt: 21355 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 21356 drop_pkt: 21357 ip1dbg(("ip_wput: dropped packet\n")); 21358 if (ire != NULL) 21359 ire_refrele(ire); 21360 if (need_decref) 21361 CONN_DEC_REF(connp); 21362 freemsg(first_mp); 21363 if (attach_ill != NULL) 21364 ill_refrele(attach_ill); 21365 if (xmit_ill != NULL) 21366 ill_refrele(xmit_ill); 21367 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 21368 "ip_wput_end: q %p (%S)", q, "droppkt"); 21369 } 21370 21371 /* 21372 * If this is a conn_t queue, then we pass in the conn. This includes the 21373 * zoneid. 21374 * Otherwise, this is a message coming back from ARP or for an ill_t queue, 21375 * in which case we use the global zoneid since those are all part of 21376 * the global zone. 21377 */ 21378 void 21379 ip_wput(queue_t *q, mblk_t *mp) 21380 { 21381 if (CONN_Q(q)) 21382 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 21383 else 21384 ip_output(GLOBAL_ZONEID, mp, q, IP_WPUT); 21385 } 21386 21387 /* 21388 * 21389 * The following rules must be observed when accessing any ipif or ill 21390 * that has been cached in the conn. Typically conn_nofailover_ill, 21391 * conn_xmit_if_ill, conn_multicast_ipif and conn_multicast_ill. 21392 * 21393 * Access: The ipif or ill pointed to from the conn can be accessed under 21394 * the protection of the conn_lock or after it has been refheld under the 21395 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 21396 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 21397 * The reason for this is that a concurrent unplumb could actually be 21398 * cleaning up these cached pointers by walking the conns and might have 21399 * finished cleaning up the conn in question. The macros check that an 21400 * unplumb has not yet started on the ipif or ill. 21401 * 21402 * Caching: An ipif or ill pointer may be cached in the conn only after 21403 * making sure that an unplumb has not started. So the caching is done 21404 * while holding both the conn_lock and the ill_lock and after using the 21405 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 21406 * flag before starting the cleanup of conns. 21407 * 21408 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 21409 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 21410 * or a reference to the ipif or a reference to an ire that references the 21411 * ipif. An ipif does not change its ill except for failover/failback. Since 21412 * failover/failback happens only after bringing down the ipif and making sure 21413 * the ipif refcnt has gone to zero and holding the ill_g_lock and ill_lock 21414 * the above holds. 21415 */ 21416 ipif_t * 21417 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 21418 { 21419 ipif_t *ipif; 21420 ill_t *ill; 21421 21422 *err = 0; 21423 rw_enter(&ill_g_lock, RW_READER); 21424 mutex_enter(&connp->conn_lock); 21425 ipif = *ipifp; 21426 if (ipif != NULL) { 21427 ill = ipif->ipif_ill; 21428 mutex_enter(&ill->ill_lock); 21429 if (IPIF_CAN_LOOKUP(ipif)) { 21430 ipif_refhold_locked(ipif); 21431 mutex_exit(&ill->ill_lock); 21432 mutex_exit(&connp->conn_lock); 21433 rw_exit(&ill_g_lock); 21434 return (ipif); 21435 } else { 21436 *err = IPIF_LOOKUP_FAILED; 21437 } 21438 mutex_exit(&ill->ill_lock); 21439 } 21440 mutex_exit(&connp->conn_lock); 21441 rw_exit(&ill_g_lock); 21442 return (NULL); 21443 } 21444 21445 ill_t * 21446 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 21447 { 21448 ill_t *ill; 21449 21450 *err = 0; 21451 mutex_enter(&connp->conn_lock); 21452 ill = *illp; 21453 if (ill != NULL) { 21454 mutex_enter(&ill->ill_lock); 21455 if (ILL_CAN_LOOKUP(ill)) { 21456 ill_refhold_locked(ill); 21457 mutex_exit(&ill->ill_lock); 21458 mutex_exit(&connp->conn_lock); 21459 return (ill); 21460 } else { 21461 *err = ILL_LOOKUP_FAILED; 21462 } 21463 mutex_exit(&ill->ill_lock); 21464 } 21465 mutex_exit(&connp->conn_lock); 21466 return (NULL); 21467 } 21468 21469 static int 21470 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 21471 { 21472 ill_t *ill; 21473 21474 ill = ipif->ipif_ill; 21475 mutex_enter(&connp->conn_lock); 21476 mutex_enter(&ill->ill_lock); 21477 if (IPIF_CAN_LOOKUP(ipif)) { 21478 *ipifp = ipif; 21479 mutex_exit(&ill->ill_lock); 21480 mutex_exit(&connp->conn_lock); 21481 return (0); 21482 } 21483 mutex_exit(&ill->ill_lock); 21484 mutex_exit(&connp->conn_lock); 21485 return (IPIF_LOOKUP_FAILED); 21486 } 21487 21488 /* 21489 * This is called if the outbound datagram needs fragmentation. 21490 * 21491 * NOTE : This function does not ire_refrele the ire argument passed in. 21492 */ 21493 static void 21494 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire, zoneid_t zoneid) 21495 { 21496 ipha_t *ipha; 21497 mblk_t *mp; 21498 uint32_t v_hlen_tos_len; 21499 uint32_t max_frag; 21500 uint32_t frag_flag; 21501 boolean_t dont_use; 21502 21503 if (ipsec_mp->b_datap->db_type == M_CTL) { 21504 mp = ipsec_mp->b_cont; 21505 } else { 21506 mp = ipsec_mp; 21507 } 21508 21509 ipha = (ipha_t *)mp->b_rptr; 21510 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21511 21512 #ifdef _BIG_ENDIAN 21513 #define V_HLEN (v_hlen_tos_len >> 24) 21514 #define LENGTH (v_hlen_tos_len & 0xFFFF) 21515 #else 21516 #define V_HLEN (v_hlen_tos_len & 0xFF) 21517 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 21518 #endif 21519 21520 #ifndef SPEED_BEFORE_SAFETY 21521 /* 21522 * Check that ipha_length is consistent with 21523 * the mblk length 21524 */ 21525 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 21526 ip0dbg(("Packet length mismatch: %d, %ld\n", 21527 LENGTH, msgdsize(mp))); 21528 freemsg(ipsec_mp); 21529 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 21530 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 21531 "packet length mismatch"); 21532 return; 21533 } 21534 #endif 21535 /* 21536 * Don't use frag_flag if pre-built packet or source 21537 * routed or if multicast (since multicast packets do not solicit 21538 * ICMP "packet too big" messages). Get the values of 21539 * max_frag and frag_flag atomically by acquiring the 21540 * ire_lock. 21541 */ 21542 mutex_enter(&ire->ire_lock); 21543 max_frag = ire->ire_max_frag; 21544 frag_flag = ire->ire_frag_flag; 21545 mutex_exit(&ire->ire_lock); 21546 21547 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 21548 (V_HLEN != IP_SIMPLE_HDR_VERSION && 21549 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 21550 21551 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 21552 (dont_use ? 0 : frag_flag), zoneid); 21553 } 21554 21555 /* 21556 * Used for deciding the MSS size for the upper layer. Thus 21557 * we need to check the outbound policy values in the conn. 21558 */ 21559 int 21560 conn_ipsec_length(conn_t *connp) 21561 { 21562 ipsec_latch_t *ipl; 21563 21564 ipl = connp->conn_latch; 21565 if (ipl == NULL) 21566 return (0); 21567 21568 if (ipl->ipl_out_policy == NULL) 21569 return (0); 21570 21571 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 21572 } 21573 21574 /* 21575 * Returns an estimate of the IPSEC headers size. This is used if 21576 * we don't want to call into IPSEC to get the exact size. 21577 */ 21578 int 21579 ipsec_out_extra_length(mblk_t *ipsec_mp) 21580 { 21581 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 21582 ipsec_action_t *a; 21583 21584 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21585 if (!io->ipsec_out_secure) 21586 return (0); 21587 21588 a = io->ipsec_out_act; 21589 21590 if (a == NULL) { 21591 ASSERT(io->ipsec_out_policy != NULL); 21592 a = io->ipsec_out_policy->ipsp_act; 21593 } 21594 ASSERT(a != NULL); 21595 21596 return (a->ipa_ovhd); 21597 } 21598 21599 /* 21600 * Returns an estimate of the IPSEC headers size. This is used if 21601 * we don't want to call into IPSEC to get the exact size. 21602 */ 21603 int 21604 ipsec_in_extra_length(mblk_t *ipsec_mp) 21605 { 21606 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21607 ipsec_action_t *a; 21608 21609 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21610 21611 a = ii->ipsec_in_action; 21612 return (a == NULL ? 0 : a->ipa_ovhd); 21613 } 21614 21615 /* 21616 * If there are any source route options, return the true final 21617 * destination. Otherwise, return the destination. 21618 */ 21619 ipaddr_t 21620 ip_get_dst(ipha_t *ipha) 21621 { 21622 ipoptp_t opts; 21623 uchar_t *opt; 21624 uint8_t optval; 21625 uint8_t optlen; 21626 ipaddr_t dst; 21627 uint32_t off; 21628 21629 dst = ipha->ipha_dst; 21630 21631 if (IS_SIMPLE_IPH(ipha)) 21632 return (dst); 21633 21634 for (optval = ipoptp_first(&opts, ipha); 21635 optval != IPOPT_EOL; 21636 optval = ipoptp_next(&opts)) { 21637 opt = opts.ipoptp_cur; 21638 optlen = opts.ipoptp_len; 21639 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 21640 switch (optval) { 21641 case IPOPT_SSRR: 21642 case IPOPT_LSRR: 21643 off = opt[IPOPT_OFFSET]; 21644 /* 21645 * If one of the conditions is true, it means 21646 * end of options and dst already has the right 21647 * value. 21648 */ 21649 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 21650 off = optlen - IP_ADDR_LEN; 21651 bcopy(&opt[off], &dst, IP_ADDR_LEN); 21652 } 21653 return (dst); 21654 default: 21655 break; 21656 } 21657 } 21658 21659 return (dst); 21660 } 21661 21662 mblk_t * 21663 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 21664 conn_t *connp, boolean_t unspec_src, zoneid_t zoneid) 21665 { 21666 ipsec_out_t *io; 21667 mblk_t *first_mp; 21668 boolean_t policy_present; 21669 21670 first_mp = mp; 21671 if (mp->b_datap->db_type == M_CTL) { 21672 io = (ipsec_out_t *)first_mp->b_rptr; 21673 /* 21674 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 21675 * 21676 * 1) There is per-socket policy (including cached global 21677 * policy) or a policy on the IP-in-IP tunnel. 21678 * 2) There is no per-socket policy, but it is 21679 * a multicast packet that needs to go out 21680 * on a specific interface. This is the case 21681 * where (ip_wput and ip_wput_multicast) attaches 21682 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 21683 * 21684 * In case (2) we check with global policy to 21685 * see if there is a match and set the ill_index 21686 * appropriately so that we can lookup the ire 21687 * properly in ip_wput_ipsec_out. 21688 */ 21689 21690 /* 21691 * ipsec_out_use_global_policy is set to B_FALSE 21692 * in ipsec_in_to_out(). Refer to that function for 21693 * details. 21694 */ 21695 if ((io->ipsec_out_latch == NULL) && 21696 (io->ipsec_out_use_global_policy)) { 21697 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 21698 ire, connp, unspec_src, zoneid)); 21699 } 21700 if (!io->ipsec_out_secure) { 21701 /* 21702 * If this is not a secure packet, drop 21703 * the IPSEC_OUT mp and treat it as a clear 21704 * packet. This happens when we are sending 21705 * a ICMP reply back to a clear packet. See 21706 * ipsec_in_to_out() for details. 21707 */ 21708 mp = first_mp->b_cont; 21709 freeb(first_mp); 21710 } 21711 return (mp); 21712 } 21713 /* 21714 * See whether we need to attach a global policy here. We 21715 * don't depend on the conn (as it could be null) for deciding 21716 * what policy this datagram should go through because it 21717 * should have happened in ip_wput if there was some 21718 * policy. This normally happens for connections which are not 21719 * fully bound preventing us from caching policies in 21720 * ip_bind. Packets coming from the TCP listener/global queue 21721 * - which are non-hard_bound - could also be affected by 21722 * applying policy here. 21723 * 21724 * If this packet is coming from tcp global queue or listener, 21725 * we will be applying policy here. This may not be *right* 21726 * if these packets are coming from the detached connection as 21727 * it could have gone in clear before. This happens only if a 21728 * TCP connection started when there is no policy and somebody 21729 * added policy before it became detached. Thus packets of the 21730 * detached connection could go out secure and the other end 21731 * would drop it because it will be expecting in clear. The 21732 * converse is not true i.e if somebody starts a TCP 21733 * connection and deletes the policy, all the packets will 21734 * still go out with the policy that existed before deleting 21735 * because ip_unbind sends up policy information which is used 21736 * by TCP on subsequent ip_wputs. The right solution is to fix 21737 * TCP to attach a dummy IPSEC_OUT and set 21738 * ipsec_out_use_global_policy to B_FALSE. As this might 21739 * affect performance for normal cases, we are not doing it. 21740 * Thus, set policy before starting any TCP connections. 21741 * 21742 * NOTE - We might apply policy even for a hard bound connection 21743 * - for which we cached policy in ip_bind - if somebody added 21744 * global policy after we inherited the policy in ip_bind. 21745 * This means that the packets that were going out in clear 21746 * previously would start going secure and hence get dropped 21747 * on the other side. To fix this, TCP attaches a dummy 21748 * ipsec_out and make sure that we don't apply global policy. 21749 */ 21750 if (ipha != NULL) 21751 policy_present = ipsec_outbound_v4_policy_present; 21752 else 21753 policy_present = ipsec_outbound_v6_policy_present; 21754 if (!policy_present) 21755 return (mp); 21756 21757 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src, 21758 zoneid)); 21759 } 21760 21761 ire_t * 21762 conn_set_outgoing_ill(conn_t *connp, ire_t *ire, ill_t **conn_outgoing_ill) 21763 { 21764 ipaddr_t addr; 21765 ire_t *save_ire; 21766 irb_t *irb; 21767 ill_group_t *illgrp; 21768 int err; 21769 21770 save_ire = ire; 21771 addr = ire->ire_addr; 21772 21773 ASSERT(ire->ire_type == IRE_BROADCAST); 21774 21775 illgrp = connp->conn_outgoing_ill->ill_group; 21776 if (illgrp == NULL) { 21777 *conn_outgoing_ill = conn_get_held_ill(connp, 21778 &connp->conn_outgoing_ill, &err); 21779 if (err == ILL_LOOKUP_FAILED) { 21780 ire_refrele(save_ire); 21781 return (NULL); 21782 } 21783 return (save_ire); 21784 } 21785 /* 21786 * If IP_BOUND_IF has been done, conn_outgoing_ill will be set. 21787 * If it is part of the group, we need to send on the ire 21788 * that has been cleared of IRE_MARK_NORECV and that belongs 21789 * to this group. This is okay as IP_BOUND_IF really means 21790 * any ill in the group. We depend on the fact that the 21791 * first ire in the group is always cleared of IRE_MARK_NORECV 21792 * if such an ire exists. This is possible only if you have 21793 * at least one ill in the group that has not failed. 21794 * 21795 * First get to the ire that matches the address and group. 21796 * 21797 * We don't look for an ire with a matching zoneid because a given zone 21798 * won't always have broadcast ires on all ills in the group. 21799 */ 21800 irb = ire->ire_bucket; 21801 rw_enter(&irb->irb_lock, RW_READER); 21802 if (ire->ire_marks & IRE_MARK_NORECV) { 21803 /* 21804 * If the current zone only has an ire broadcast for this 21805 * address marked NORECV, the ire we want is ahead in the 21806 * bucket, so we look it up deliberately ignoring the zoneid. 21807 */ 21808 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 21809 if (ire->ire_addr != addr) 21810 continue; 21811 /* skip over deleted ires */ 21812 if (ire->ire_marks & IRE_MARK_CONDEMNED) 21813 continue; 21814 } 21815 } 21816 while (ire != NULL) { 21817 /* 21818 * If a new interface is coming up, we could end up 21819 * seeing the loopback ire and the non-loopback ire 21820 * may not have been added yet. So check for ire_stq 21821 */ 21822 if (ire->ire_stq != NULL && (ire->ire_addr != addr || 21823 ire->ire_ipif->ipif_ill->ill_group == illgrp)) { 21824 break; 21825 } 21826 ire = ire->ire_next; 21827 } 21828 if (ire != NULL && ire->ire_addr == addr && 21829 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 21830 IRE_REFHOLD(ire); 21831 rw_exit(&irb->irb_lock); 21832 ire_refrele(save_ire); 21833 *conn_outgoing_ill = ire_to_ill(ire); 21834 /* 21835 * Refhold the ill to make the conn_outgoing_ill 21836 * independent of the ire. ip_wput_ire goes in a loop 21837 * and may refrele the ire. Since we have an ire at this 21838 * point we don't need to use ILL_CAN_LOOKUP on the ill. 21839 */ 21840 ill_refhold(*conn_outgoing_ill); 21841 return (ire); 21842 } 21843 rw_exit(&irb->irb_lock); 21844 ip1dbg(("conn_set_outgoing_ill: No matching ire\n")); 21845 /* 21846 * If we can't find a suitable ire, return the original ire. 21847 */ 21848 return (save_ire); 21849 } 21850 21851 /* 21852 * This function does the ire_refrele of the ire passed in as the 21853 * argument. As this function looks up more ires i.e broadcast ires, 21854 * it needs to REFRELE them. Currently, for simplicity we don't 21855 * differentiate the one passed in and looked up here. We always 21856 * REFRELE. 21857 * IPQoS Notes: 21858 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 21859 * IPSec packets are done in ipsec_out_process. 21860 * 21861 */ 21862 void 21863 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller, 21864 zoneid_t zoneid) 21865 { 21866 ipha_t *ipha; 21867 #define rptr ((uchar_t *)ipha) 21868 queue_t *stq; 21869 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 21870 uint32_t v_hlen_tos_len; 21871 uint32_t ttl_protocol; 21872 ipaddr_t src; 21873 ipaddr_t dst; 21874 uint32_t cksum; 21875 ipaddr_t orig_src; 21876 ire_t *ire1; 21877 mblk_t *next_mp; 21878 uint_t hlen; 21879 uint16_t *up; 21880 uint32_t max_frag = ire->ire_max_frag; 21881 ill_t *ill = ire_to_ill(ire); 21882 int clusterwide; 21883 uint16_t ip_hdr_included; /* IP header included by ULP? */ 21884 int ipsec_len; 21885 mblk_t *first_mp; 21886 ipsec_out_t *io; 21887 boolean_t conn_dontroute; /* conn value for multicast */ 21888 boolean_t conn_multicast_loop; /* conn value for multicast */ 21889 boolean_t multicast_forward; /* Should we forward ? */ 21890 boolean_t unspec_src; 21891 ill_t *conn_outgoing_ill = NULL; 21892 ill_t *ire_ill; 21893 ill_t *ire1_ill; 21894 ill_t *out_ill; 21895 uint32_t ill_index = 0; 21896 boolean_t multirt_send = B_FALSE; 21897 int err; 21898 ipxmit_state_t pktxmit_state; 21899 21900 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 21901 "ip_wput_ire_start: q %p", q); 21902 21903 multicast_forward = B_FALSE; 21904 unspec_src = (connp != NULL && connp->conn_unspec_src); 21905 21906 if (ire->ire_flags & RTF_MULTIRT) { 21907 /* 21908 * Multirouting case. The bucket where ire is stored 21909 * probably holds other RTF_MULTIRT flagged ire 21910 * to the destination. In this call to ip_wput_ire, 21911 * we attempt to send the packet through all 21912 * those ires. Thus, we first ensure that ire is the 21913 * first RTF_MULTIRT ire in the bucket, 21914 * before walking the ire list. 21915 */ 21916 ire_t *first_ire; 21917 irb_t *irb = ire->ire_bucket; 21918 ASSERT(irb != NULL); 21919 21920 /* Make sure we do not omit any multiroute ire. */ 21921 IRB_REFHOLD(irb); 21922 for (first_ire = irb->irb_ire; 21923 first_ire != NULL; 21924 first_ire = first_ire->ire_next) { 21925 if ((first_ire->ire_flags & RTF_MULTIRT) && 21926 (first_ire->ire_addr == ire->ire_addr) && 21927 !(first_ire->ire_marks & 21928 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 21929 break; 21930 } 21931 21932 if ((first_ire != NULL) && (first_ire != ire)) { 21933 IRE_REFHOLD(first_ire); 21934 ire_refrele(ire); 21935 ire = first_ire; 21936 ill = ire_to_ill(ire); 21937 } 21938 IRB_REFRELE(irb); 21939 } 21940 21941 /* 21942 * conn_outgoing_ill is used only in the broadcast loop. 21943 * for performance we don't grab the mutexs in the fastpath 21944 */ 21945 if ((connp != NULL) && 21946 (connp->conn_xmit_if_ill == NULL) && 21947 (ire->ire_type == IRE_BROADCAST) && 21948 ((connp->conn_nofailover_ill != NULL) || 21949 (connp->conn_outgoing_ill != NULL))) { 21950 /* 21951 * Bind to IPIF_NOFAILOVER address overrides IP_BOUND_IF 21952 * option. So, see if this endpoint is bound to a 21953 * IPIF_NOFAILOVER address. If so, honor it. This implies 21954 * that if the interface is failed, we will still send 21955 * the packet on the same ill which is what we want. 21956 */ 21957 conn_outgoing_ill = conn_get_held_ill(connp, 21958 &connp->conn_nofailover_ill, &err); 21959 if (err == ILL_LOOKUP_FAILED) { 21960 ire_refrele(ire); 21961 freemsg(mp); 21962 return; 21963 } 21964 if (conn_outgoing_ill == NULL) { 21965 /* 21966 * Choose a good ill in the group to send the 21967 * packets on. 21968 */ 21969 ire = conn_set_outgoing_ill(connp, ire, 21970 &conn_outgoing_ill); 21971 if (ire == NULL) { 21972 freemsg(mp); 21973 return; 21974 } 21975 } 21976 } 21977 21978 if (mp->b_datap->db_type != M_CTL) { 21979 ipha = (ipha_t *)mp->b_rptr; 21980 } else { 21981 io = (ipsec_out_t *)mp->b_rptr; 21982 ASSERT(io->ipsec_out_type == IPSEC_OUT); 21983 ASSERT(zoneid == io->ipsec_out_zoneid); 21984 ASSERT(zoneid != ALL_ZONES); 21985 ipha = (ipha_t *)mp->b_cont->b_rptr; 21986 dst = ipha->ipha_dst; 21987 /* 21988 * For the multicast case, ipsec_out carries conn_dontroute and 21989 * conn_multicast_loop as conn may not be available here. We 21990 * need this for multicast loopback and forwarding which is done 21991 * later in the code. 21992 */ 21993 if (CLASSD(dst)) { 21994 conn_dontroute = io->ipsec_out_dontroute; 21995 conn_multicast_loop = io->ipsec_out_multicast_loop; 21996 /* 21997 * If conn_dontroute is not set or conn_multicast_loop 21998 * is set, we need to do forwarding/loopback. For 21999 * datagrams from ip_wput_multicast, conn_dontroute is 22000 * set to B_TRUE and conn_multicast_loop is set to 22001 * B_FALSE so that we neither do forwarding nor 22002 * loopback. 22003 */ 22004 if (!conn_dontroute || conn_multicast_loop) 22005 multicast_forward = B_TRUE; 22006 } 22007 } 22008 22009 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 22010 ire->ire_zoneid != ALL_ZONES) { 22011 /* 22012 * When a zone sends a packet to another zone, we try to deliver 22013 * the packet under the same conditions as if the destination 22014 * was a real node on the network. To do so, we look for a 22015 * matching route in the forwarding table. 22016 * RTF_REJECT and RTF_BLACKHOLE are handled just like 22017 * ip_newroute() does. 22018 * Note that IRE_LOCAL are special, since they are used 22019 * when the zoneid doesn't match in some cases. This means that 22020 * we need to handle ipha_src differently since ire_src_addr 22021 * belongs to the receiving zone instead of the sending zone. 22022 * When ip_restrict_interzone_loopback is set, then 22023 * ire_cache_lookup() ensures that IRE_LOCAL are only used 22024 * for loopback between zones when the logical "Ethernet" would 22025 * have looped them back. 22026 */ 22027 ire_t *src_ire; 22028 22029 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 22030 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 22031 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE)); 22032 if (src_ire != NULL && 22033 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) && 22034 (!ip_restrict_interzone_loopback || 22035 ire_local_same_ill_group(ire, src_ire))) { 22036 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 22037 ipha->ipha_src = src_ire->ire_src_addr; 22038 ire_refrele(src_ire); 22039 } else { 22040 ire_refrele(ire); 22041 if (conn_outgoing_ill != NULL) 22042 ill_refrele(conn_outgoing_ill); 22043 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 22044 if (src_ire != NULL) { 22045 if (src_ire->ire_flags & RTF_BLACKHOLE) { 22046 ire_refrele(src_ire); 22047 freemsg(mp); 22048 return; 22049 } 22050 ire_refrele(src_ire); 22051 } 22052 if (ip_hdr_complete(ipha, zoneid)) { 22053 /* Failed */ 22054 freemsg(mp); 22055 return; 22056 } 22057 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, zoneid); 22058 return; 22059 } 22060 } 22061 22062 if (mp->b_datap->db_type == M_CTL || 22063 ipsec_outbound_v4_policy_present) { 22064 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 22065 unspec_src, zoneid); 22066 if (mp == NULL) { 22067 ire_refrele(ire); 22068 if (conn_outgoing_ill != NULL) 22069 ill_refrele(conn_outgoing_ill); 22070 return; 22071 } 22072 } 22073 22074 first_mp = mp; 22075 ipsec_len = 0; 22076 22077 if (first_mp->b_datap->db_type == M_CTL) { 22078 io = (ipsec_out_t *)first_mp->b_rptr; 22079 ASSERT(io->ipsec_out_type == IPSEC_OUT); 22080 mp = first_mp->b_cont; 22081 ipsec_len = ipsec_out_extra_length(first_mp); 22082 ASSERT(ipsec_len >= 0); 22083 /* We already picked up the zoneid from the M_CTL above */ 22084 ASSERT(zoneid == io->ipsec_out_zoneid); 22085 ASSERT(zoneid != ALL_ZONES); 22086 22087 /* 22088 * Drop M_CTL here if IPsec processing is not needed. 22089 * (Non-IPsec use of M_CTL extracted any information it 22090 * needed above). 22091 */ 22092 if (ipsec_len == 0) { 22093 freeb(first_mp); 22094 first_mp = mp; 22095 } 22096 } 22097 22098 /* 22099 * Fast path for ip_wput_ire 22100 */ 22101 22102 ipha = (ipha_t *)mp->b_rptr; 22103 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 22104 dst = ipha->ipha_dst; 22105 22106 /* 22107 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 22108 * if the socket is a SOCK_RAW type. The transport checksum should 22109 * be provided in the pre-built packet, so we don't need to compute it. 22110 * Also, other application set flags, like DF, should not be altered. 22111 * Other transport MUST pass down zero. 22112 */ 22113 ip_hdr_included = ipha->ipha_ident; 22114 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 22115 22116 if (CLASSD(dst)) { 22117 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 22118 ntohl(dst), 22119 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 22120 ntohl(ire->ire_addr))); 22121 } 22122 22123 /* Macros to extract header fields from data already in registers */ 22124 #ifdef _BIG_ENDIAN 22125 #define V_HLEN (v_hlen_tos_len >> 24) 22126 #define LENGTH (v_hlen_tos_len & 0xFFFF) 22127 #define PROTO (ttl_protocol & 0xFF) 22128 #else 22129 #define V_HLEN (v_hlen_tos_len & 0xFF) 22130 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 22131 #define PROTO (ttl_protocol >> 8) 22132 #endif 22133 22134 22135 orig_src = src = ipha->ipha_src; 22136 /* (The loop back to "another" is explained down below.) */ 22137 another:; 22138 /* 22139 * Assign an ident value for this packet. We assign idents on 22140 * a per destination basis out of the IRE. There could be 22141 * other threads targeting the same destination, so we have to 22142 * arrange for a atomic increment. Note that we use a 32-bit 22143 * atomic add because it has better performance than its 22144 * 16-bit sibling. 22145 * 22146 * If running in cluster mode and if the source address 22147 * belongs to a replicated service then vector through 22148 * cl_inet_ipident vector to allocate ip identifier 22149 * NOTE: This is a contract private interface with the 22150 * clustering group. 22151 */ 22152 clusterwide = 0; 22153 if (cl_inet_ipident) { 22154 ASSERT(cl_inet_isclusterwide); 22155 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 22156 AF_INET, (uint8_t *)(uintptr_t)src)) { 22157 ipha->ipha_ident = (*cl_inet_ipident)(IPPROTO_IP, 22158 AF_INET, (uint8_t *)(uintptr_t)src, 22159 (uint8_t *)(uintptr_t)dst); 22160 clusterwide = 1; 22161 } 22162 } 22163 if (!clusterwide) { 22164 ipha->ipha_ident = 22165 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 22166 } 22167 22168 #ifndef _BIG_ENDIAN 22169 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 22170 #endif 22171 22172 /* 22173 * Set source address unless sent on an ill or conn_unspec_src is set. 22174 * This is needed to obey conn_unspec_src when packets go through 22175 * ip_newroute + arp. 22176 * Assumes ip_newroute{,_multi} sets the source address as well. 22177 */ 22178 if (src == INADDR_ANY && !unspec_src) { 22179 /* 22180 * Assign the appropriate source address from the IRE if none 22181 * was specified. 22182 */ 22183 ASSERT(ire->ire_ipversion == IPV4_VERSION); 22184 22185 /* 22186 * With IP multipathing, broadcast packets are sent on the ire 22187 * that has been cleared of IRE_MARK_NORECV and that belongs to 22188 * the group. However, this ire might not be in the same zone so 22189 * we can't always use its source address. We look for a 22190 * broadcast ire in the same group and in the right zone. 22191 */ 22192 if (ire->ire_type == IRE_BROADCAST && 22193 ire->ire_zoneid != zoneid) { 22194 ire_t *src_ire = ire_ctable_lookup(dst, 0, 22195 IRE_BROADCAST, ire->ire_ipif, zoneid, NULL, 22196 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP)); 22197 if (src_ire != NULL) { 22198 src = src_ire->ire_src_addr; 22199 ire_refrele(src_ire); 22200 } else { 22201 ire_refrele(ire); 22202 if (conn_outgoing_ill != NULL) 22203 ill_refrele(conn_outgoing_ill); 22204 freemsg(first_mp); 22205 if (ill != NULL) { 22206 BUMP_MIB(ill->ill_ip_mib, 22207 ipIfStatsOutDiscards); 22208 } else { 22209 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 22210 } 22211 return; 22212 } 22213 } else { 22214 src = ire->ire_src_addr; 22215 } 22216 22217 if (connp == NULL) { 22218 ip1dbg(("ip_wput_ire: no connp and no src " 22219 "address for dst 0x%x, using src 0x%x\n", 22220 ntohl(dst), 22221 ntohl(src))); 22222 } 22223 ipha->ipha_src = src; 22224 } 22225 stq = ire->ire_stq; 22226 22227 /* 22228 * We only allow ire chains for broadcasts since there will 22229 * be multiple IRE_CACHE entries for the same multicast 22230 * address (one per ipif). 22231 */ 22232 next_mp = NULL; 22233 22234 /* broadcast packet */ 22235 if (ire->ire_type == IRE_BROADCAST) 22236 goto broadcast; 22237 22238 /* loopback ? */ 22239 if (stq == NULL) 22240 goto nullstq; 22241 22242 /* The ill_index for outbound ILL */ 22243 ill_index = Q_TO_INDEX(stq); 22244 22245 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 22246 ttl_protocol = ((uint16_t *)ipha)[4]; 22247 22248 /* pseudo checksum (do it in parts for IP header checksum) */ 22249 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 22250 22251 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 22252 queue_t *dev_q = stq->q_next; 22253 22254 /* flow controlled */ 22255 if ((dev_q->q_next || dev_q->q_first) && 22256 !canput(dev_q)) 22257 goto blocked; 22258 if ((PROTO == IPPROTO_UDP) && 22259 (ip_hdr_included != IP_HDR_INCLUDED)) { 22260 hlen = (V_HLEN & 0xF) << 2; 22261 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22262 if (*up != 0) { 22263 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 22264 hlen, LENGTH, max_frag, ipsec_len, cksum); 22265 /* Software checksum? */ 22266 if (DB_CKSUMFLAGS(mp) == 0) { 22267 IP_STAT(ip_out_sw_cksum); 22268 IP_STAT_UPDATE( 22269 ip_udp_out_sw_cksum_bytes, 22270 LENGTH - hlen); 22271 } 22272 } 22273 } 22274 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 22275 hlen = (V_HLEN & 0xF) << 2; 22276 if (PROTO == IPPROTO_TCP) { 22277 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22278 /* 22279 * The packet header is processed once and for all, even 22280 * in the multirouting case. We disable hardware 22281 * checksum if the packet is multirouted, as it will be 22282 * replicated via several interfaces, and not all of 22283 * them may have this capability. 22284 */ 22285 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 22286 LENGTH, max_frag, ipsec_len, cksum); 22287 /* Software checksum? */ 22288 if (DB_CKSUMFLAGS(mp) == 0) { 22289 IP_STAT(ip_out_sw_cksum); 22290 IP_STAT_UPDATE(ip_tcp_out_sw_cksum_bytes, 22291 LENGTH - hlen); 22292 } 22293 } else { 22294 sctp_hdr_t *sctph; 22295 22296 ASSERT(PROTO == IPPROTO_SCTP); 22297 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22298 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22299 /* 22300 * Zero out the checksum field to ensure proper 22301 * checksum calculation. 22302 */ 22303 sctph->sh_chksum = 0; 22304 #ifdef DEBUG 22305 if (!skip_sctp_cksum) 22306 #endif 22307 sctph->sh_chksum = sctp_cksum(mp, hlen); 22308 } 22309 } 22310 22311 /* 22312 * If this is a multicast packet and originated from ip_wput 22313 * we need to do loopback and forwarding checks. If it comes 22314 * from ip_wput_multicast, we SHOULD not do this. 22315 */ 22316 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 22317 22318 /* checksum */ 22319 cksum += ttl_protocol; 22320 22321 /* fragment the packet */ 22322 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 22323 goto fragmentit; 22324 /* 22325 * Don't use frag_flag if packet is pre-built or source 22326 * routed or if multicast (since multicast packets do 22327 * not solicit ICMP "packet too big" messages). 22328 */ 22329 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22330 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22331 !ip_source_route_included(ipha)) && 22332 !CLASSD(ipha->ipha_dst)) 22333 ipha->ipha_fragment_offset_and_flags |= 22334 htons(ire->ire_frag_flag); 22335 22336 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22337 /* calculate IP header checksum */ 22338 cksum += ipha->ipha_ident; 22339 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 22340 cksum += ipha->ipha_fragment_offset_and_flags; 22341 22342 /* IP options present */ 22343 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22344 if (hlen) 22345 goto checksumoptions; 22346 22347 /* calculate hdr checksum */ 22348 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22349 cksum = ~(cksum + (cksum >> 16)); 22350 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22351 } 22352 if (ipsec_len != 0) { 22353 /* 22354 * We will do the rest of the processing after 22355 * we come back from IPSEC in ip_wput_ipsec_out(). 22356 */ 22357 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 22358 22359 io = (ipsec_out_t *)first_mp->b_rptr; 22360 io->ipsec_out_ill_index = ((ill_t *)stq->q_ptr)-> 22361 ill_phyint->phyint_ifindex; 22362 22363 ipsec_out_process(q, first_mp, ire, ill_index); 22364 ire_refrele(ire); 22365 if (conn_outgoing_ill != NULL) 22366 ill_refrele(conn_outgoing_ill); 22367 return; 22368 } 22369 22370 /* 22371 * In most cases, the emission loop below is entered only 22372 * once. Only in the case where the ire holds the 22373 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 22374 * flagged ires in the bucket, and send the packet 22375 * through all crossed RTF_MULTIRT routes. 22376 */ 22377 if (ire->ire_flags & RTF_MULTIRT) { 22378 multirt_send = B_TRUE; 22379 } 22380 do { 22381 if (multirt_send) { 22382 irb_t *irb; 22383 /* 22384 * We are in a multiple send case, need to get 22385 * the next ire and make a duplicate of the packet. 22386 * ire1 holds here the next ire to process in the 22387 * bucket. If multirouting is expected, 22388 * any non-RTF_MULTIRT ire that has the 22389 * right destination address is ignored. 22390 */ 22391 irb = ire->ire_bucket; 22392 ASSERT(irb != NULL); 22393 22394 IRB_REFHOLD(irb); 22395 for (ire1 = ire->ire_next; 22396 ire1 != NULL; 22397 ire1 = ire1->ire_next) { 22398 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 22399 continue; 22400 if (ire1->ire_addr != ire->ire_addr) 22401 continue; 22402 if (ire1->ire_marks & 22403 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 22404 continue; 22405 22406 /* Got one */ 22407 IRE_REFHOLD(ire1); 22408 break; 22409 } 22410 IRB_REFRELE(irb); 22411 22412 if (ire1 != NULL) { 22413 next_mp = copyb(mp); 22414 if ((next_mp == NULL) || 22415 ((mp->b_cont != NULL) && 22416 ((next_mp->b_cont = 22417 dupmsg(mp->b_cont)) == NULL))) { 22418 freemsg(next_mp); 22419 next_mp = NULL; 22420 ire_refrele(ire1); 22421 ire1 = NULL; 22422 } 22423 } 22424 22425 /* Last multiroute ire; don't loop anymore. */ 22426 if (ire1 == NULL) { 22427 multirt_send = B_FALSE; 22428 } 22429 } 22430 22431 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 22432 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha, 22433 mblk_t *, mp); 22434 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 22435 NULL, ire->ire_ipif->ipif_ill, ipha, mp, mp); 22436 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 22437 if (mp == NULL) 22438 goto release_ire_and_ill; 22439 22440 mp->b_prev = SET_BPREV_FLAG(IPP_LOCAL_OUT); 22441 DTRACE_PROBE2(ip__xmit__1, mblk_t *, mp, ire_t *, ire); 22442 pktxmit_state = ip_xmit_v4(mp, ire, NULL, B_TRUE); 22443 if ((pktxmit_state == SEND_FAILED) || 22444 (pktxmit_state == LLHDR_RESLV_FAILED)) { 22445 ip2dbg(("ip_wput_ire: ip_xmit_v4 failed" 22446 "- packet dropped\n")); 22447 release_ire_and_ill: 22448 ire_refrele(ire); 22449 if (next_mp != NULL) { 22450 freemsg(next_mp); 22451 ire_refrele(ire1); 22452 } 22453 if (conn_outgoing_ill != NULL) 22454 ill_refrele(conn_outgoing_ill); 22455 return; 22456 } 22457 22458 if (CLASSD(dst)) { 22459 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastPkts); 22460 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutMcastOctets, 22461 ntohs(ipha->ipha_length)); 22462 } 22463 22464 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22465 "ip_wput_ire_end: q %p (%S)", 22466 q, "last copy out"); 22467 IRE_REFRELE(ire); 22468 22469 if (multirt_send) { 22470 ASSERT(ire1); 22471 /* 22472 * Proceed with the next RTF_MULTIRT ire, 22473 * Also set up the send-to queue accordingly. 22474 */ 22475 ire = ire1; 22476 ire1 = NULL; 22477 stq = ire->ire_stq; 22478 mp = next_mp; 22479 next_mp = NULL; 22480 ipha = (ipha_t *)mp->b_rptr; 22481 ill_index = Q_TO_INDEX(stq); 22482 ill = (ill_t *)stq->q_ptr; 22483 } 22484 } while (multirt_send); 22485 if (conn_outgoing_ill != NULL) 22486 ill_refrele(conn_outgoing_ill); 22487 return; 22488 22489 /* 22490 * ire->ire_type == IRE_BROADCAST (minimize diffs) 22491 */ 22492 broadcast: 22493 { 22494 /* 22495 * Avoid broadcast storms by setting the ttl to 1 22496 * for broadcasts. This parameter can be set 22497 * via ndd, so make sure that for the SO_DONTROUTE 22498 * case that ipha_ttl is always set to 1. 22499 * In the event that we are replying to incoming 22500 * ICMP packets, conn could be NULL. 22501 */ 22502 if ((connp != NULL) && connp->conn_dontroute) 22503 ipha->ipha_ttl = 1; 22504 else 22505 ipha->ipha_ttl = ip_broadcast_ttl; 22506 22507 /* 22508 * Note that we are not doing a IRB_REFHOLD here. 22509 * Actually we don't care if the list changes i.e 22510 * if somebody deletes an IRE from the list while 22511 * we drop the lock, the next time we come around 22512 * ire_next will be NULL and hence we won't send 22513 * out multiple copies which is fine. 22514 */ 22515 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 22516 ire1 = ire->ire_next; 22517 if (conn_outgoing_ill != NULL) { 22518 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 22519 ASSERT(ire1 == ire->ire_next); 22520 if (ire1 != NULL && ire1->ire_addr == dst) { 22521 ire_refrele(ire); 22522 ire = ire1; 22523 IRE_REFHOLD(ire); 22524 ire1 = ire->ire_next; 22525 continue; 22526 } 22527 rw_exit(&ire->ire_bucket->irb_lock); 22528 /* Did not find a matching ill */ 22529 ip1dbg(("ip_wput_ire: broadcast with no " 22530 "matching IP_BOUND_IF ill %s\n", 22531 conn_outgoing_ill->ill_name)); 22532 freemsg(first_mp); 22533 if (ire != NULL) 22534 ire_refrele(ire); 22535 ill_refrele(conn_outgoing_ill); 22536 return; 22537 } 22538 } else if (ire1 != NULL && ire1->ire_addr == dst) { 22539 /* 22540 * If the next IRE has the same address and is not one 22541 * of the two copies that we need to send, try to see 22542 * whether this copy should be sent at all. This 22543 * assumes that we insert loopbacks first and then 22544 * non-loopbacks. This is acheived by inserting the 22545 * loopback always before non-loopback. 22546 * This is used to send a single copy of a broadcast 22547 * packet out all physical interfaces that have an 22548 * matching IRE_BROADCAST while also looping 22549 * back one copy (to ip_wput_local) for each 22550 * matching physical interface. However, we avoid 22551 * sending packets out different logical that match by 22552 * having ipif_up/ipif_down supress duplicate 22553 * IRE_BROADCASTS. 22554 * 22555 * This feature is currently used to get broadcasts 22556 * sent to multiple interfaces, when the broadcast 22557 * address being used applies to multiple interfaces. 22558 * For example, a whole net broadcast will be 22559 * replicated on every connected subnet of 22560 * the target net. 22561 * 22562 * Each zone has its own set of IRE_BROADCASTs, so that 22563 * we're able to distribute inbound packets to multiple 22564 * zones who share a broadcast address. We avoid looping 22565 * back outbound packets in different zones but on the 22566 * same ill, as the application would see duplicates. 22567 * 22568 * If the interfaces are part of the same group, 22569 * we would want to send only one copy out for 22570 * whole group. 22571 * 22572 * This logic assumes that ire_add_v4() groups the 22573 * IRE_BROADCAST entries so that those with the same 22574 * ire_addr and ill_group are kept together. 22575 */ 22576 ire_ill = ire->ire_ipif->ipif_ill; 22577 if (ire->ire_stq == NULL && ire1->ire_stq != NULL) { 22578 if (ire_ill->ill_group != NULL && 22579 (ire->ire_marks & IRE_MARK_NORECV)) { 22580 /* 22581 * If the current zone only has an ire 22582 * broadcast for this address marked 22583 * NORECV, the ire we want is ahead in 22584 * the bucket, so we look it up 22585 * deliberately ignoring the zoneid. 22586 */ 22587 for (ire1 = ire->ire_bucket->irb_ire; 22588 ire1 != NULL; 22589 ire1 = ire1->ire_next) { 22590 ire1_ill = 22591 ire1->ire_ipif->ipif_ill; 22592 if (ire1->ire_addr != dst) 22593 continue; 22594 /* skip over the current ire */ 22595 if (ire1 == ire) 22596 continue; 22597 /* skip over deleted ires */ 22598 if (ire1->ire_marks & 22599 IRE_MARK_CONDEMNED) 22600 continue; 22601 /* 22602 * non-loopback ire in our 22603 * group: use it for the next 22604 * pass in the loop 22605 */ 22606 if (ire1->ire_stq != NULL && 22607 ire1_ill->ill_group == 22608 ire_ill->ill_group) 22609 break; 22610 } 22611 } 22612 } else { 22613 while (ire1 != NULL && ire1->ire_addr == dst) { 22614 ire1_ill = ire1->ire_ipif->ipif_ill; 22615 /* 22616 * We can have two broadcast ires on the 22617 * same ill in different zones; here 22618 * we'll send a copy of the packet on 22619 * each ill and the fanout code will 22620 * call conn_wantpacket() to check that 22621 * the zone has the broadcast address 22622 * configured on the ill. If the two 22623 * ires are in the same group we only 22624 * send one copy up. 22625 */ 22626 if (ire1_ill != ire_ill && 22627 (ire1_ill->ill_group == NULL || 22628 ire_ill->ill_group == NULL || 22629 ire1_ill->ill_group != 22630 ire_ill->ill_group)) { 22631 break; 22632 } 22633 ire1 = ire1->ire_next; 22634 } 22635 } 22636 } 22637 ASSERT(multirt_send == B_FALSE); 22638 if (ire1 != NULL && ire1->ire_addr == dst) { 22639 if ((ire->ire_flags & RTF_MULTIRT) && 22640 (ire1->ire_flags & RTF_MULTIRT)) { 22641 /* 22642 * We are in the multirouting case. 22643 * The message must be sent at least 22644 * on both ires. These ires have been 22645 * inserted AFTER the standard ones 22646 * in ip_rt_add(). There are thus no 22647 * other ire entries for the destination 22648 * address in the rest of the bucket 22649 * that do not have the RTF_MULTIRT 22650 * flag. We don't process a copy 22651 * of the message here. This will be 22652 * done in the final sending loop. 22653 */ 22654 multirt_send = B_TRUE; 22655 } else { 22656 next_mp = ip_copymsg(first_mp); 22657 if (next_mp != NULL) 22658 IRE_REFHOLD(ire1); 22659 } 22660 } 22661 rw_exit(&ire->ire_bucket->irb_lock); 22662 } 22663 22664 if (stq) { 22665 /* 22666 * A non-NULL send-to queue means this packet is going 22667 * out of this machine. 22668 */ 22669 out_ill = (ill_t *)stq->q_ptr; 22670 22671 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutRequests); 22672 ttl_protocol = ((uint16_t *)ipha)[4]; 22673 /* 22674 * We accumulate the pseudo header checksum in cksum. 22675 * This is pretty hairy code, so watch close. One 22676 * thing to keep in mind is that UDP and TCP have 22677 * stored their respective datagram lengths in their 22678 * checksum fields. This lines things up real nice. 22679 */ 22680 cksum = (dst >> 16) + (dst & 0xFFFF) + 22681 (src >> 16) + (src & 0xFFFF); 22682 /* 22683 * We assume the udp checksum field contains the 22684 * length, so to compute the pseudo header checksum, 22685 * all we need is the protocol number and src/dst. 22686 */ 22687 /* Provide the checksums for UDP and TCP. */ 22688 if ((PROTO == IPPROTO_TCP) && 22689 (ip_hdr_included != IP_HDR_INCLUDED)) { 22690 /* hlen gets the number of uchar_ts in the IP header */ 22691 hlen = (V_HLEN & 0xF) << 2; 22692 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 22693 IP_STAT(ip_out_sw_cksum); 22694 IP_STAT_UPDATE(ip_tcp_out_sw_cksum_bytes, 22695 LENGTH - hlen); 22696 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 22697 if (*up == 0) 22698 *up = 0xFFFF; 22699 } else if (PROTO == IPPROTO_SCTP && 22700 (ip_hdr_included != IP_HDR_INCLUDED)) { 22701 sctp_hdr_t *sctph; 22702 22703 hlen = (V_HLEN & 0xF) << 2; 22704 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 22705 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 22706 sctph->sh_chksum = 0; 22707 #ifdef DEBUG 22708 if (!skip_sctp_cksum) 22709 #endif 22710 sctph->sh_chksum = sctp_cksum(mp, hlen); 22711 } else { 22712 queue_t *dev_q = stq->q_next; 22713 22714 if ((dev_q->q_next || dev_q->q_first) && 22715 !canput(dev_q)) { 22716 blocked: 22717 ipha->ipha_ident = ip_hdr_included; 22718 /* 22719 * If we don't have a conn to apply 22720 * backpressure, free the message. 22721 * In the ire_send path, we don't know 22722 * the position to requeue the packet. Rather 22723 * than reorder packets, we just drop this 22724 * packet. 22725 */ 22726 if (ip_output_queue && connp != NULL && 22727 caller != IRE_SEND) { 22728 if (caller == IP_WSRV) { 22729 connp->conn_did_putbq = 1; 22730 (void) putbq(connp->conn_wq, 22731 first_mp); 22732 conn_drain_insert(connp); 22733 /* 22734 * This is the service thread, 22735 * and the queue is already 22736 * noenabled. The check for 22737 * canput and the putbq is not 22738 * atomic. So we need to check 22739 * again. 22740 */ 22741 if (canput(stq->q_next)) 22742 connp->conn_did_putbq 22743 = 0; 22744 IP_STAT(ip_conn_flputbq); 22745 } else { 22746 /* 22747 * We are not the service proc. 22748 * ip_wsrv will be scheduled or 22749 * is already running. 22750 */ 22751 (void) putq(connp->conn_wq, 22752 first_mp); 22753 } 22754 } else { 22755 out_ill = (ill_t *)stq->q_ptr; 22756 BUMP_MIB(out_ill->ill_ip_mib, 22757 ipIfStatsOutDiscards); 22758 freemsg(first_mp); 22759 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22760 "ip_wput_ire_end: q %p (%S)", 22761 q, "discard"); 22762 } 22763 ire_refrele(ire); 22764 if (next_mp) { 22765 ire_refrele(ire1); 22766 freemsg(next_mp); 22767 } 22768 if (conn_outgoing_ill != NULL) 22769 ill_refrele(conn_outgoing_ill); 22770 return; 22771 } 22772 if ((PROTO == IPPROTO_UDP) && 22773 (ip_hdr_included != IP_HDR_INCLUDED)) { 22774 /* 22775 * hlen gets the number of uchar_ts in the 22776 * IP header 22777 */ 22778 hlen = (V_HLEN & 0xF) << 2; 22779 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 22780 max_frag = ire->ire_max_frag; 22781 if (*up != 0) { 22782 IP_CKSUM_XMIT(ire_ill, ire, mp, ipha, 22783 up, PROTO, hlen, LENGTH, max_frag, 22784 ipsec_len, cksum); 22785 /* Software checksum? */ 22786 if (DB_CKSUMFLAGS(mp) == 0) { 22787 IP_STAT(ip_out_sw_cksum); 22788 IP_STAT_UPDATE( 22789 ip_udp_out_sw_cksum_bytes, 22790 LENGTH - hlen); 22791 } 22792 } 22793 } 22794 } 22795 /* 22796 * Need to do this even when fragmenting. The local 22797 * loopback can be done without computing checksums 22798 * but forwarding out other interface must be done 22799 * after the IP checksum (and ULP checksums) have been 22800 * computed. 22801 * 22802 * NOTE : multicast_forward is set only if this packet 22803 * originated from ip_wput. For packets originating from 22804 * ip_wput_multicast, it is not set. 22805 */ 22806 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 22807 multi_loopback: 22808 ip2dbg(("ip_wput: multicast, loop %d\n", 22809 conn_multicast_loop)); 22810 22811 /* Forget header checksum offload */ 22812 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 22813 22814 /* 22815 * Local loopback of multicasts? Check the 22816 * ill. 22817 * 22818 * Note that the loopback function will not come 22819 * in through ip_rput - it will only do the 22820 * client fanout thus we need to do an mforward 22821 * as well. The is different from the BSD 22822 * logic. 22823 */ 22824 if (ill != NULL) { 22825 ilm_t *ilm; 22826 22827 ILM_WALKER_HOLD(ill); 22828 ilm = ilm_lookup_ill(ill, ipha->ipha_dst, 22829 ALL_ZONES); 22830 ILM_WALKER_RELE(ill); 22831 if (ilm != NULL) { 22832 /* 22833 * Pass along the virtual output q. 22834 * ip_wput_local() will distribute the 22835 * packet to all the matching zones, 22836 * except the sending zone when 22837 * IP_MULTICAST_LOOP is false. 22838 */ 22839 ip_multicast_loopback(q, ill, first_mp, 22840 conn_multicast_loop ? 0 : 22841 IP_FF_NO_MCAST_LOOP, zoneid); 22842 } 22843 } 22844 if (ipha->ipha_ttl == 0) { 22845 /* 22846 * 0 => only to this host i.e. we are 22847 * done. We are also done if this was the 22848 * loopback interface since it is sufficient 22849 * to loopback one copy of a multicast packet. 22850 */ 22851 freemsg(first_mp); 22852 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22853 "ip_wput_ire_end: q %p (%S)", 22854 q, "loopback"); 22855 ire_refrele(ire); 22856 if (conn_outgoing_ill != NULL) 22857 ill_refrele(conn_outgoing_ill); 22858 return; 22859 } 22860 /* 22861 * ILLF_MULTICAST is checked in ip_newroute 22862 * i.e. we don't need to check it here since 22863 * all IRE_CACHEs come from ip_newroute. 22864 * For multicast traffic, SO_DONTROUTE is interpreted 22865 * to mean only send the packet out the interface 22866 * (optionally specified with IP_MULTICAST_IF) 22867 * and do not forward it out additional interfaces. 22868 * RSVP and the rsvp daemon is an example of a 22869 * protocol and user level process that 22870 * handles it's own routing. Hence, it uses the 22871 * SO_DONTROUTE option to accomplish this. 22872 */ 22873 22874 if (ip_g_mrouter && !conn_dontroute && ill != NULL) { 22875 /* Unconditionally redo the checksum */ 22876 ipha->ipha_hdr_checksum = 0; 22877 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 22878 22879 /* 22880 * If this needs to go out secure, we need 22881 * to wait till we finish the IPSEC 22882 * processing. 22883 */ 22884 if (ipsec_len == 0 && 22885 ip_mforward(ill, ipha, mp)) { 22886 freemsg(first_mp); 22887 ip1dbg(("ip_wput: mforward failed\n")); 22888 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 22889 "ip_wput_ire_end: q %p (%S)", 22890 q, "mforward failed"); 22891 ire_refrele(ire); 22892 if (conn_outgoing_ill != NULL) 22893 ill_refrele(conn_outgoing_ill); 22894 return; 22895 } 22896 } 22897 } 22898 max_frag = ire->ire_max_frag; 22899 cksum += ttl_protocol; 22900 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 22901 /* No fragmentation required for this one. */ 22902 /* 22903 * Don't use frag_flag if packet is pre-built or source 22904 * routed or if multicast (since multicast packets do 22905 * not solicit ICMP "packet too big" messages). 22906 */ 22907 if ((ip_hdr_included != IP_HDR_INCLUDED) && 22908 (V_HLEN == IP_SIMPLE_HDR_VERSION || 22909 !ip_source_route_included(ipha)) && 22910 !CLASSD(ipha->ipha_dst)) 22911 ipha->ipha_fragment_offset_and_flags |= 22912 htons(ire->ire_frag_flag); 22913 22914 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 22915 /* Complete the IP header checksum. */ 22916 cksum += ipha->ipha_ident; 22917 cksum += (v_hlen_tos_len >> 16)+ 22918 (v_hlen_tos_len & 0xFFFF); 22919 cksum += ipha->ipha_fragment_offset_and_flags; 22920 hlen = (V_HLEN & 0xF) - 22921 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 22922 if (hlen) { 22923 checksumoptions: 22924 /* 22925 * Account for the IP Options in the IP 22926 * header checksum. 22927 */ 22928 up = (uint16_t *)(rptr+ 22929 IP_SIMPLE_HDR_LENGTH); 22930 do { 22931 cksum += up[0]; 22932 cksum += up[1]; 22933 up += 2; 22934 } while (--hlen); 22935 } 22936 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 22937 cksum = ~(cksum + (cksum >> 16)); 22938 ipha->ipha_hdr_checksum = (uint16_t)cksum; 22939 } 22940 if (ipsec_len != 0) { 22941 ipsec_out_process(q, first_mp, ire, ill_index); 22942 if (!next_mp) { 22943 ire_refrele(ire); 22944 if (conn_outgoing_ill != NULL) 22945 ill_refrele(conn_outgoing_ill); 22946 return; 22947 } 22948 goto next; 22949 } 22950 22951 /* 22952 * multirt_send has already been handled 22953 * for broadcast, but not yet for multicast 22954 * or IP options. 22955 */ 22956 if (next_mp == NULL) { 22957 if (ire->ire_flags & RTF_MULTIRT) { 22958 multirt_send = B_TRUE; 22959 } 22960 } 22961 22962 /* 22963 * In most cases, the emission loop below is 22964 * entered only once. Only in the case where 22965 * the ire holds the RTF_MULTIRT flag, do we loop 22966 * to process all RTF_MULTIRT ires in the bucket, 22967 * and send the packet through all crossed 22968 * RTF_MULTIRT routes. 22969 */ 22970 do { 22971 if (multirt_send) { 22972 irb_t *irb; 22973 22974 irb = ire->ire_bucket; 22975 ASSERT(irb != NULL); 22976 /* 22977 * We are in a multiple send case, 22978 * need to get the next IRE and make 22979 * a duplicate of the packet. 22980 */ 22981 IRB_REFHOLD(irb); 22982 for (ire1 = ire->ire_next; 22983 ire1 != NULL; 22984 ire1 = ire1->ire_next) { 22985 if (!(ire1->ire_flags & 22986 RTF_MULTIRT)) 22987 continue; 22988 if (ire1->ire_addr != 22989 ire->ire_addr) 22990 continue; 22991 if (ire1->ire_marks & 22992 (IRE_MARK_CONDEMNED| 22993 IRE_MARK_HIDDEN)) 22994 continue; 22995 22996 /* Got one */ 22997 IRE_REFHOLD(ire1); 22998 break; 22999 } 23000 IRB_REFRELE(irb); 23001 23002 if (ire1 != NULL) { 23003 next_mp = copyb(mp); 23004 if ((next_mp == NULL) || 23005 ((mp->b_cont != NULL) && 23006 ((next_mp->b_cont = 23007 dupmsg(mp->b_cont)) 23008 == NULL))) { 23009 freemsg(next_mp); 23010 next_mp = NULL; 23011 ire_refrele(ire1); 23012 ire1 = NULL; 23013 } 23014 } 23015 23016 /* 23017 * Last multiroute ire; don't loop 23018 * anymore. The emission is over 23019 * and next_mp is NULL. 23020 */ 23021 if (ire1 == NULL) { 23022 multirt_send = B_FALSE; 23023 } 23024 } 23025 23026 out_ill = ire->ire_ipif->ipif_ill; 23027 DTRACE_PROBE4(ip4__physical__out__start, 23028 ill_t *, NULL, 23029 ill_t *, out_ill, 23030 ipha_t *, ipha, mblk_t *, mp); 23031 FW_HOOKS(ip4_physical_out_event, 23032 ipv4firewall_physical_out, 23033 NULL, out_ill, ipha, mp, mp); 23034 DTRACE_PROBE1(ip4__physical__out__end, 23035 mblk_t *, mp); 23036 if (mp == NULL) 23037 goto release_ire_and_ill_2; 23038 23039 ASSERT(ipsec_len == 0); 23040 mp->b_prev = 23041 SET_BPREV_FLAG(IPP_LOCAL_OUT); 23042 DTRACE_PROBE2(ip__xmit__2, 23043 mblk_t *, mp, ire_t *, ire); 23044 pktxmit_state = ip_xmit_v4(mp, ire, 23045 NULL, B_TRUE); 23046 if ((pktxmit_state == SEND_FAILED) || 23047 (pktxmit_state == LLHDR_RESLV_FAILED)) { 23048 release_ire_and_ill_2: 23049 if (next_mp) { 23050 freemsg(next_mp); 23051 ire_refrele(ire1); 23052 } 23053 ire_refrele(ire); 23054 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23055 "ip_wput_ire_end: q %p (%S)", 23056 q, "discard MDATA"); 23057 if (conn_outgoing_ill != NULL) 23058 ill_refrele(conn_outgoing_ill); 23059 return; 23060 } 23061 23062 if (CLASSD(dst)) { 23063 BUMP_MIB(out_ill->ill_ip_mib, 23064 ipIfStatsHCOutMcastPkts); 23065 UPDATE_MIB(out_ill->ill_ip_mib, 23066 ipIfStatsHCOutMcastOctets, 23067 ntohs(ipha->ipha_length)); 23068 } else if (ire->ire_type == IRE_BROADCAST) { 23069 BUMP_MIB(out_ill->ill_ip_mib, 23070 ipIfStatsHCOutBcastPkts); 23071 } 23072 23073 if (multirt_send) { 23074 /* 23075 * We are in a multiple send case, 23076 * need to re-enter the sending loop 23077 * using the next ire. 23078 */ 23079 ire_refrele(ire); 23080 ire = ire1; 23081 stq = ire->ire_stq; 23082 mp = next_mp; 23083 next_mp = NULL; 23084 ipha = (ipha_t *)mp->b_rptr; 23085 ill_index = Q_TO_INDEX(stq); 23086 } 23087 } while (multirt_send); 23088 23089 if (!next_mp) { 23090 /* 23091 * Last copy going out (the ultra-common 23092 * case). Note that we intentionally replicate 23093 * the putnext rather than calling it before 23094 * the next_mp check in hopes of a little 23095 * tail-call action out of the compiler. 23096 */ 23097 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23098 "ip_wput_ire_end: q %p (%S)", 23099 q, "last copy out(1)"); 23100 ire_refrele(ire); 23101 if (conn_outgoing_ill != NULL) 23102 ill_refrele(conn_outgoing_ill); 23103 return; 23104 } 23105 /* More copies going out below. */ 23106 } else { 23107 int offset; 23108 fragmentit: 23109 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23110 /* 23111 * If this would generate a icmp_frag_needed message, 23112 * we need to handle it before we do the IPSEC 23113 * processing. Otherwise, we need to strip the IPSEC 23114 * headers before we send up the message to the ULPs 23115 * which becomes messy and difficult. 23116 */ 23117 if (ipsec_len != 0) { 23118 if ((max_frag < (unsigned int)(LENGTH + 23119 ipsec_len)) && (offset & IPH_DF)) { 23120 out_ill = (ill_t *)stq->q_ptr; 23121 BUMP_MIB(out_ill->ill_ip_mib, 23122 ipIfStatsOutFragFails); 23123 BUMP_MIB(out_ill->ill_ip_mib, 23124 ipIfStatsOutFragReqds); 23125 ipha->ipha_hdr_checksum = 0; 23126 ipha->ipha_hdr_checksum = 23127 (uint16_t)ip_csum_hdr(ipha); 23128 icmp_frag_needed(ire->ire_stq, first_mp, 23129 max_frag, zoneid); 23130 if (!next_mp) { 23131 ire_refrele(ire); 23132 if (conn_outgoing_ill != NULL) { 23133 ill_refrele( 23134 conn_outgoing_ill); 23135 } 23136 return; 23137 } 23138 } else { 23139 /* 23140 * This won't cause a icmp_frag_needed 23141 * message. to be generated. Send it on 23142 * the wire. Note that this could still 23143 * cause fragmentation and all we 23144 * do is the generation of the message 23145 * to the ULP if needed before IPSEC. 23146 */ 23147 if (!next_mp) { 23148 ipsec_out_process(q, first_mp, 23149 ire, ill_index); 23150 TRACE_2(TR_FAC_IP, 23151 TR_IP_WPUT_IRE_END, 23152 "ip_wput_ire_end: q %p " 23153 "(%S)", q, 23154 "last ipsec_out_process"); 23155 ire_refrele(ire); 23156 if (conn_outgoing_ill != NULL) { 23157 ill_refrele( 23158 conn_outgoing_ill); 23159 } 23160 return; 23161 } 23162 ipsec_out_process(q, first_mp, 23163 ire, ill_index); 23164 } 23165 } else { 23166 /* 23167 * Initiate IPPF processing. For 23168 * fragmentable packets we finish 23169 * all QOS packet processing before 23170 * calling: 23171 * ip_wput_ire_fragmentit->ip_wput_frag 23172 */ 23173 23174 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 23175 ip_process(IPP_LOCAL_OUT, &mp, 23176 ill_index); 23177 if (mp == NULL) { 23178 out_ill = (ill_t *)stq->q_ptr; 23179 BUMP_MIB(out_ill->ill_ip_mib, 23180 ipIfStatsOutDiscards); 23181 if (next_mp != NULL) { 23182 freemsg(next_mp); 23183 ire_refrele(ire1); 23184 } 23185 ire_refrele(ire); 23186 TRACE_2(TR_FAC_IP, 23187 TR_IP_WPUT_IRE_END, 23188 "ip_wput_ire: q %p (%S)", 23189 q, "discard MDATA"); 23190 if (conn_outgoing_ill != NULL) { 23191 ill_refrele( 23192 conn_outgoing_ill); 23193 } 23194 return; 23195 } 23196 } 23197 if (!next_mp) { 23198 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23199 "ip_wput_ire_end: q %p (%S)", 23200 q, "last fragmentation"); 23201 ip_wput_ire_fragmentit(mp, ire, 23202 zoneid); 23203 ire_refrele(ire); 23204 if (conn_outgoing_ill != NULL) 23205 ill_refrele(conn_outgoing_ill); 23206 return; 23207 } 23208 ip_wput_ire_fragmentit(mp, ire, zoneid); 23209 } 23210 } 23211 } else { 23212 nullstq: 23213 /* A NULL stq means the destination address is local. */ 23214 UPDATE_OB_PKT_COUNT(ire); 23215 ire->ire_last_used_time = lbolt; 23216 ASSERT(ire->ire_ipif != NULL); 23217 if (!next_mp) { 23218 /* 23219 * Is there an "in" and "out" for traffic local 23220 * to a host (loopback)? The code in Solaris doesn't 23221 * explicitly draw a line in its code for in vs out, 23222 * so we've had to draw a line in the sand: ip_wput_ire 23223 * is considered to be the "output" side and 23224 * ip_wput_local to be the "input" side. 23225 */ 23226 out_ill = ire->ire_ipif->ipif_ill; 23227 23228 DTRACE_PROBE4(ip4__loopback__out__start, 23229 ill_t *, NULL, ill_t *, out_ill, 23230 ipha_t *, ipha, mblk_t *, first_mp); 23231 23232 FW_HOOKS(ip4_loopback_out_event, 23233 ipv4firewall_loopback_out, 23234 NULL, out_ill, ipha, first_mp, mp); 23235 23236 DTRACE_PROBE1(ip4__loopback__out_end, 23237 mblk_t *, first_mp); 23238 23239 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 23240 "ip_wput_ire_end: q %p (%S)", 23241 q, "local address"); 23242 23243 if (first_mp != NULL) 23244 ip_wput_local(q, out_ill, ipha, 23245 first_mp, ire, 0, ire->ire_zoneid); 23246 ire_refrele(ire); 23247 if (conn_outgoing_ill != NULL) 23248 ill_refrele(conn_outgoing_ill); 23249 return; 23250 } 23251 23252 out_ill = ire->ire_ipif->ipif_ill; 23253 23254 DTRACE_PROBE4(ip4__loopback__out__start, 23255 ill_t *, NULL, ill_t *, out_ill, 23256 ipha_t *, ipha, mblk_t *, first_mp); 23257 23258 FW_HOOKS(ip4_loopback_out_event, ipv4firewall_loopback_out, 23259 NULL, out_ill, ipha, first_mp, mp); 23260 23261 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, first_mp); 23262 23263 if (first_mp != NULL) 23264 ip_wput_local(q, out_ill, ipha, 23265 first_mp, ire, 0, ire->ire_zoneid); 23266 } 23267 next: 23268 /* 23269 * More copies going out to additional interfaces. 23270 * ire1 has already been held. We don't need the 23271 * "ire" anymore. 23272 */ 23273 ire_refrele(ire); 23274 ire = ire1; 23275 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 23276 mp = next_mp; 23277 ASSERT(ire->ire_ipversion == IPV4_VERSION); 23278 ill = ire_to_ill(ire); 23279 first_mp = mp; 23280 if (ipsec_len != 0) { 23281 ASSERT(first_mp->b_datap->db_type == M_CTL); 23282 mp = mp->b_cont; 23283 } 23284 dst = ire->ire_addr; 23285 ipha = (ipha_t *)mp->b_rptr; 23286 /* 23287 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 23288 * Restore ipha_ident "no checksum" flag. 23289 */ 23290 src = orig_src; 23291 ipha->ipha_ident = ip_hdr_included; 23292 goto another; 23293 23294 #undef rptr 23295 #undef Q_TO_INDEX 23296 } 23297 23298 /* 23299 * Routine to allocate a message that is used to notify the ULP about MDT. 23300 * The caller may provide a pointer to the link-layer MDT capabilities, 23301 * or NULL if MDT is to be disabled on the stream. 23302 */ 23303 mblk_t * 23304 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 23305 { 23306 mblk_t *mp; 23307 ip_mdt_info_t *mdti; 23308 ill_mdt_capab_t *idst; 23309 23310 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 23311 DB_TYPE(mp) = M_CTL; 23312 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 23313 mdti = (ip_mdt_info_t *)mp->b_rptr; 23314 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 23315 idst = &(mdti->mdt_capab); 23316 23317 /* 23318 * If the caller provides us with the capability, copy 23319 * it over into our notification message; otherwise 23320 * we zero out the capability portion. 23321 */ 23322 if (isrc != NULL) 23323 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23324 else 23325 bzero((caddr_t)idst, sizeof (*idst)); 23326 } 23327 return (mp); 23328 } 23329 23330 /* 23331 * Routine which determines whether MDT can be enabled on the destination 23332 * IRE and IPC combination, and if so, allocates and returns the MDT 23333 * notification mblk that may be used by ULP. We also check if we need to 23334 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 23335 * MDT usage in the past have been lifted. This gets called during IP 23336 * and ULP binding. 23337 */ 23338 mblk_t * 23339 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23340 ill_mdt_capab_t *mdt_cap) 23341 { 23342 mblk_t *mp; 23343 boolean_t rc = B_FALSE; 23344 23345 ASSERT(dst_ire != NULL); 23346 ASSERT(connp != NULL); 23347 ASSERT(mdt_cap != NULL); 23348 23349 /* 23350 * Currently, we only support simple TCP/{IPv4,IPv6} with 23351 * Multidata, which is handled in tcp_multisend(). This 23352 * is the reason why we do all these checks here, to ensure 23353 * that we don't enable Multidata for the cases which we 23354 * can't handle at the moment. 23355 */ 23356 do { 23357 /* Only do TCP at the moment */ 23358 if (connp->conn_ulp != IPPROTO_TCP) 23359 break; 23360 23361 /* 23362 * IPSEC outbound policy present? Note that we get here 23363 * after calling ipsec_conn_cache_policy() where the global 23364 * policy checking is performed. conn_latch will be 23365 * non-NULL as long as there's a policy defined, 23366 * i.e. conn_out_enforce_policy may be NULL in such case 23367 * when the connection is non-secure, and hence we check 23368 * further if the latch refers to an outbound policy. 23369 */ 23370 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 23371 break; 23372 23373 /* CGTP (multiroute) is enabled? */ 23374 if (dst_ire->ire_flags & RTF_MULTIRT) 23375 break; 23376 23377 /* Outbound IPQoS enabled? */ 23378 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 23379 /* 23380 * In this case, we disable MDT for this and all 23381 * future connections going over the interface. 23382 */ 23383 mdt_cap->ill_mdt_on = 0; 23384 break; 23385 } 23386 23387 /* socket option(s) present? */ 23388 if (!CONN_IS_LSO_MD_FASTPATH(connp)) 23389 break; 23390 23391 rc = B_TRUE; 23392 /* CONSTCOND */ 23393 } while (0); 23394 23395 /* Remember the result */ 23396 connp->conn_mdt_ok = rc; 23397 23398 if (!rc) 23399 return (NULL); 23400 else if (!mdt_cap->ill_mdt_on) { 23401 /* 23402 * If MDT has been previously turned off in the past, and we 23403 * currently can do MDT (due to IPQoS policy removal, etc.) 23404 * then enable it for this interface. 23405 */ 23406 mdt_cap->ill_mdt_on = 1; 23407 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 23408 "interface %s\n", ill_name)); 23409 } 23410 23411 /* Allocate the MDT info mblk */ 23412 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 23413 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 23414 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23415 return (NULL); 23416 } 23417 return (mp); 23418 } 23419 23420 /* 23421 * Routine to allocate a message that is used to notify the ULP about LSO. 23422 * The caller may provide a pointer to the link-layer LSO capabilities, 23423 * or NULL if LSO is to be disabled on the stream. 23424 */ 23425 mblk_t * 23426 ip_lsoinfo_alloc(ill_lso_capab_t *isrc) 23427 { 23428 mblk_t *mp; 23429 ip_lso_info_t *lsoi; 23430 ill_lso_capab_t *idst; 23431 23432 if ((mp = allocb(sizeof (*lsoi), BPRI_HI)) != NULL) { 23433 DB_TYPE(mp) = M_CTL; 23434 mp->b_wptr = mp->b_rptr + sizeof (*lsoi); 23435 lsoi = (ip_lso_info_t *)mp->b_rptr; 23436 lsoi->lso_info_id = LSO_IOC_INFO_UPDATE; 23437 idst = &(lsoi->lso_capab); 23438 23439 /* 23440 * If the caller provides us with the capability, copy 23441 * it over into our notification message; otherwise 23442 * we zero out the capability portion. 23443 */ 23444 if (isrc != NULL) 23445 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 23446 else 23447 bzero((caddr_t)idst, sizeof (*idst)); 23448 } 23449 return (mp); 23450 } 23451 23452 /* 23453 * Routine which determines whether LSO can be enabled on the destination 23454 * IRE and IPC combination, and if so, allocates and returns the LSO 23455 * notification mblk that may be used by ULP. We also check if we need to 23456 * turn LSO back to 'on' when certain restrictions prohibiting us to allow 23457 * LSO usage in the past have been lifted. This gets called during IP 23458 * and ULP binding. 23459 */ 23460 mblk_t * 23461 ip_lsoinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 23462 ill_lso_capab_t *lso_cap) 23463 { 23464 mblk_t *mp; 23465 23466 ASSERT(dst_ire != NULL); 23467 ASSERT(connp != NULL); 23468 ASSERT(lso_cap != NULL); 23469 23470 connp->conn_lso_ok = B_TRUE; 23471 23472 if ((connp->conn_ulp != IPPROTO_TCP) || 23473 CONN_IPSEC_OUT_ENCAPSULATED(connp) || 23474 (dst_ire->ire_flags & RTF_MULTIRT) || 23475 !CONN_IS_LSO_MD_FASTPATH(connp) || 23476 (IPP_ENABLED(IPP_LOCAL_OUT))) { 23477 connp->conn_lso_ok = B_FALSE; 23478 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 23479 /* 23480 * Disable LSO for this and all future connections going 23481 * over the interface. 23482 */ 23483 lso_cap->ill_lso_on = 0; 23484 } 23485 } 23486 23487 if (!connp->conn_lso_ok) 23488 return (NULL); 23489 else if (!lso_cap->ill_lso_on) { 23490 /* 23491 * If LSO has been previously turned off in the past, and we 23492 * currently can do LSO (due to IPQoS policy removal, etc.) 23493 * then enable it for this interface. 23494 */ 23495 lso_cap->ill_lso_on = 1; 23496 ip1dbg(("ip_mdinfo_return: reenabling LSO for interface %s\n", 23497 ill_name)); 23498 } 23499 23500 /* Allocate the LSO info mblk */ 23501 if ((mp = ip_lsoinfo_alloc(lso_cap)) == NULL) 23502 ip0dbg(("ip_lsoinfo_return: can't enable LSO for " 23503 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 23504 23505 return (mp); 23506 } 23507 23508 /* 23509 * Create destination address attribute, and fill it with the physical 23510 * destination address and SAP taken from the template DL_UNITDATA_REQ 23511 * message block. 23512 */ 23513 boolean_t 23514 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 23515 { 23516 dl_unitdata_req_t *dlurp; 23517 pattr_t *pa; 23518 pattrinfo_t pa_info; 23519 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 23520 uint_t das_len, das_off; 23521 23522 ASSERT(dlmp != NULL); 23523 23524 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 23525 das_len = dlurp->dl_dest_addr_length; 23526 das_off = dlurp->dl_dest_addr_offset; 23527 23528 pa_info.type = PATTR_DSTADDRSAP; 23529 pa_info.len = sizeof (**das) + das_len - 1; 23530 23531 /* create and associate the attribute */ 23532 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23533 if (pa != NULL) { 23534 ASSERT(*das != NULL); 23535 (*das)->addr_is_group = 0; 23536 (*das)->addr_len = (uint8_t)das_len; 23537 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 23538 } 23539 23540 return (pa != NULL); 23541 } 23542 23543 /* 23544 * Create hardware checksum attribute and fill it with the values passed. 23545 */ 23546 boolean_t 23547 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 23548 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 23549 { 23550 pattr_t *pa; 23551 pattrinfo_t pa_info; 23552 23553 ASSERT(mmd != NULL); 23554 23555 pa_info.type = PATTR_HCKSUM; 23556 pa_info.len = sizeof (pattr_hcksum_t); 23557 23558 /* create and associate the attribute */ 23559 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23560 if (pa != NULL) { 23561 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 23562 23563 hck->hcksum_start_offset = start_offset; 23564 hck->hcksum_stuff_offset = stuff_offset; 23565 hck->hcksum_end_offset = end_offset; 23566 hck->hcksum_flags = flags; 23567 } 23568 return (pa != NULL); 23569 } 23570 23571 /* 23572 * Create zerocopy attribute and fill it with the specified flags 23573 */ 23574 boolean_t 23575 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 23576 { 23577 pattr_t *pa; 23578 pattrinfo_t pa_info; 23579 23580 ASSERT(mmd != NULL); 23581 pa_info.type = PATTR_ZCOPY; 23582 pa_info.len = sizeof (pattr_zcopy_t); 23583 23584 /* create and associate the attribute */ 23585 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 23586 if (pa != NULL) { 23587 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 23588 23589 zcopy->zcopy_flags = flags; 23590 } 23591 return (pa != NULL); 23592 } 23593 23594 /* 23595 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 23596 * block chain. We could rewrite to handle arbitrary message block chains but 23597 * that would make the code complicated and slow. Right now there three 23598 * restrictions: 23599 * 23600 * 1. The first message block must contain the complete IP header and 23601 * at least 1 byte of payload data. 23602 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 23603 * so that we can use a single Multidata message. 23604 * 3. No frag must be distributed over two or more message blocks so 23605 * that we don't need more than two packet descriptors per frag. 23606 * 23607 * The above restrictions allow us to support userland applications (which 23608 * will send down a single message block) and NFS over UDP (which will 23609 * send down a chain of at most three message blocks). 23610 * 23611 * We also don't use MDT for payloads with less than or equal to 23612 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 23613 */ 23614 boolean_t 23615 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 23616 { 23617 int blocks; 23618 ssize_t total, missing, size; 23619 23620 ASSERT(mp != NULL); 23621 ASSERT(hdr_len > 0); 23622 23623 size = MBLKL(mp) - hdr_len; 23624 if (size <= 0) 23625 return (B_FALSE); 23626 23627 /* The first mblk contains the header and some payload. */ 23628 blocks = 1; 23629 total = size; 23630 size %= len; 23631 missing = (size == 0) ? 0 : (len - size); 23632 mp = mp->b_cont; 23633 23634 while (mp != NULL) { 23635 /* 23636 * Give up if we encounter a zero length message block. 23637 * In practice, this should rarely happen and therefore 23638 * not worth the trouble of freeing and re-linking the 23639 * mblk from the chain to handle such case. 23640 */ 23641 if ((size = MBLKL(mp)) == 0) 23642 return (B_FALSE); 23643 23644 /* Too many payload buffers for a single Multidata message? */ 23645 if (++blocks > MULTIDATA_MAX_PBUFS) 23646 return (B_FALSE); 23647 23648 total += size; 23649 /* Is a frag distributed over two or more message blocks? */ 23650 if (missing > size) 23651 return (B_FALSE); 23652 size -= missing; 23653 23654 size %= len; 23655 missing = (size == 0) ? 0 : (len - size); 23656 23657 mp = mp->b_cont; 23658 } 23659 23660 return (total > ip_wput_frag_mdt_min); 23661 } 23662 23663 /* 23664 * Outbound IPv4 fragmentation routine using MDT. 23665 */ 23666 static void 23667 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 23668 uint32_t frag_flag, int offset) 23669 { 23670 ipha_t *ipha_orig; 23671 int i1, ip_data_end; 23672 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 23673 mblk_t *hdr_mp, *md_mp = NULL; 23674 unsigned char *hdr_ptr, *pld_ptr; 23675 multidata_t *mmd; 23676 ip_pdescinfo_t pdi; 23677 ill_t *ill; 23678 23679 ASSERT(DB_TYPE(mp) == M_DATA); 23680 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 23681 23682 ill = ire_to_ill(ire); 23683 ASSERT(ill != NULL); 23684 23685 ipha_orig = (ipha_t *)mp->b_rptr; 23686 mp->b_rptr += sizeof (ipha_t); 23687 23688 /* Calculate how many packets we will send out */ 23689 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 23690 pkts = (i1 + len - 1) / len; 23691 ASSERT(pkts > 1); 23692 23693 /* Allocate a message block which will hold all the IP Headers. */ 23694 wroff = ip_wroff_extra; 23695 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 23696 23697 i1 = pkts * hdr_chunk_len; 23698 /* 23699 * Create the header buffer, Multidata and destination address 23700 * and SAP attribute that should be associated with it. 23701 */ 23702 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 23703 ((hdr_mp->b_wptr += i1), 23704 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 23705 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 23706 freemsg(mp); 23707 if (md_mp == NULL) { 23708 freemsg(hdr_mp); 23709 } else { 23710 free_mmd: IP_STAT(ip_frag_mdt_discarded); 23711 freemsg(md_mp); 23712 } 23713 IP_STAT(ip_frag_mdt_allocfail); 23714 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 23715 return; 23716 } 23717 IP_STAT(ip_frag_mdt_allocd); 23718 23719 /* 23720 * Add a payload buffer to the Multidata; this operation must not 23721 * fail, or otherwise our logic in this routine is broken. There 23722 * is no memory allocation done by the routine, so any returned 23723 * failure simply tells us that we've done something wrong. 23724 * 23725 * A failure tells us that either we're adding the same payload 23726 * buffer more than once, or we're trying to add more buffers than 23727 * allowed. None of the above cases should happen, and we panic 23728 * because either there's horrible heap corruption, and/or 23729 * programming mistake. 23730 */ 23731 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23732 goto pbuf_panic; 23733 23734 hdr_ptr = hdr_mp->b_rptr; 23735 pld_ptr = mp->b_rptr; 23736 23737 /* Establish the ending byte offset, based on the starting offset. */ 23738 offset <<= 3; 23739 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 23740 IP_SIMPLE_HDR_LENGTH; 23741 23742 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 23743 23744 while (pld_ptr < mp->b_wptr) { 23745 ipha_t *ipha; 23746 uint16_t offset_and_flags; 23747 uint16_t ip_len; 23748 int error; 23749 23750 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 23751 ipha = (ipha_t *)(hdr_ptr + wroff); 23752 ASSERT(OK_32PTR(ipha)); 23753 *ipha = *ipha_orig; 23754 23755 if (ip_data_end - offset > len) { 23756 offset_and_flags = IPH_MF; 23757 } else { 23758 /* 23759 * Last frag. Set len to the length of this last piece. 23760 */ 23761 len = ip_data_end - offset; 23762 /* A frag of a frag might have IPH_MF non-zero */ 23763 offset_and_flags = 23764 ntohs(ipha->ipha_fragment_offset_and_flags) & 23765 IPH_MF; 23766 } 23767 offset_and_flags |= (uint16_t)(offset >> 3); 23768 offset_and_flags |= (uint16_t)frag_flag; 23769 /* Store the offset and flags in the IP header. */ 23770 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 23771 23772 /* Store the length in the IP header. */ 23773 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 23774 ipha->ipha_length = htons(ip_len); 23775 23776 /* 23777 * Set the IP header checksum. Note that mp is just 23778 * the header, so this is easy to pass to ip_csum. 23779 */ 23780 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23781 23782 /* 23783 * Record offset and size of header and data of the next packet 23784 * in the multidata message. 23785 */ 23786 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 23787 PDESC_PLD_INIT(&pdi); 23788 i1 = MIN(mp->b_wptr - pld_ptr, len); 23789 ASSERT(i1 > 0); 23790 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 23791 if (i1 == len) { 23792 pld_ptr += len; 23793 } else { 23794 i1 = len - i1; 23795 mp = mp->b_cont; 23796 ASSERT(mp != NULL); 23797 ASSERT(MBLKL(mp) >= i1); 23798 /* 23799 * Attach the next payload message block to the 23800 * multidata message. 23801 */ 23802 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23803 goto pbuf_panic; 23804 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 23805 pld_ptr = mp->b_rptr + i1; 23806 } 23807 23808 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 23809 KM_NOSLEEP)) == NULL) { 23810 /* 23811 * Any failure other than ENOMEM indicates that we 23812 * have passed in invalid pdesc info or parameters 23813 * to mmd_addpdesc, which must not happen. 23814 * 23815 * EINVAL is a result of failure on boundary checks 23816 * against the pdesc info contents. It should not 23817 * happen, and we panic because either there's 23818 * horrible heap corruption, and/or programming 23819 * mistake. 23820 */ 23821 if (error != ENOMEM) { 23822 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 23823 "pdesc logic error detected for " 23824 "mmd %p pinfo %p (%d)\n", 23825 (void *)mmd, (void *)&pdi, error); 23826 /* NOTREACHED */ 23827 } 23828 IP_STAT(ip_frag_mdt_addpdescfail); 23829 /* Free unattached payload message blocks as well */ 23830 md_mp->b_cont = mp->b_cont; 23831 goto free_mmd; 23832 } 23833 23834 /* Advance fragment offset. */ 23835 offset += len; 23836 23837 /* Advance to location for next header in the buffer. */ 23838 hdr_ptr += hdr_chunk_len; 23839 23840 /* Did we reach the next payload message block? */ 23841 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 23842 mp = mp->b_cont; 23843 /* 23844 * Attach the next message block with payload 23845 * data to the multidata message. 23846 */ 23847 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 23848 goto pbuf_panic; 23849 pld_ptr = mp->b_rptr; 23850 } 23851 } 23852 23853 ASSERT(hdr_mp->b_wptr == hdr_ptr); 23854 ASSERT(mp->b_wptr == pld_ptr); 23855 23856 /* Update IP statistics */ 23857 IP_STAT_UPDATE(ip_frag_mdt_pkt_out, pkts); 23858 23859 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates, pkts); 23860 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs); 23861 23862 len = ntohs(ipha_orig->ipha_length) + (pkts - 1) * IP_SIMPLE_HDR_LENGTH; 23863 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, pkts); 23864 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, len); 23865 23866 if (pkt_type == OB_PKT) { 23867 ire->ire_ob_pkt_count += pkts; 23868 if (ire->ire_ipif != NULL) 23869 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 23870 } else { 23871 /* 23872 * The type is IB_PKT in the forwarding path and in 23873 * the mobile IP case when the packet is being reverse- 23874 * tunneled to the home agent. 23875 */ 23876 ire->ire_ib_pkt_count += pkts; 23877 ASSERT(!IRE_IS_LOCAL(ire)); 23878 if (ire->ire_type & IRE_BROADCAST) { 23879 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 23880 } else { 23881 UPDATE_MIB(ill->ill_ip_mib, 23882 ipIfStatsHCOutForwDatagrams, pkts); 23883 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 23884 } 23885 } 23886 ire->ire_last_used_time = lbolt; 23887 /* Send it down */ 23888 putnext(ire->ire_stq, md_mp); 23889 return; 23890 23891 pbuf_panic: 23892 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 23893 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 23894 pbuf_idx); 23895 /* NOTREACHED */ 23896 } 23897 23898 /* 23899 * Outbound IP fragmentation routine. 23900 * 23901 * NOTE : This routine does not ire_refrele the ire that is passed in 23902 * as the argument. 23903 */ 23904 static void 23905 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 23906 uint32_t frag_flag, zoneid_t zoneid) 23907 { 23908 int i1; 23909 mblk_t *ll_hdr_mp; 23910 int ll_hdr_len; 23911 int hdr_len; 23912 mblk_t *hdr_mp; 23913 ipha_t *ipha; 23914 int ip_data_end; 23915 int len; 23916 mblk_t *mp = mp_orig, *mp1; 23917 int offset; 23918 queue_t *q; 23919 uint32_t v_hlen_tos_len; 23920 mblk_t *first_mp; 23921 boolean_t mctl_present; 23922 ill_t *ill; 23923 ill_t *out_ill; 23924 mblk_t *xmit_mp; 23925 mblk_t *carve_mp; 23926 ire_t *ire1 = NULL; 23927 ire_t *save_ire = NULL; 23928 mblk_t *next_mp = NULL; 23929 boolean_t last_frag = B_FALSE; 23930 boolean_t multirt_send = B_FALSE; 23931 ire_t *first_ire = NULL; 23932 irb_t *irb = NULL; 23933 mib2_ipIfStatsEntry_t *mibptr = NULL; 23934 23935 ill = ire_to_ill(ire); 23936 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ip_mib; 23937 23938 BUMP_MIB(mibptr, ipIfStatsOutFragReqds); 23939 23940 /* 23941 * IPSEC does not allow hw accelerated packets to be fragmented 23942 * This check is made in ip_wput_ipsec_out prior to coming here 23943 * via ip_wput_ire_fragmentit. 23944 * 23945 * If at this point we have an ire whose ARP request has not 23946 * been sent out, we call ip_xmit_v4->ire_arpresolve to trigger 23947 * sending of ARP query and change ire's state to ND_INCOMPLETE. 23948 * This packet and all fragmentable packets for this ire will 23949 * continue to get dropped while ire_nce->nce_state remains in 23950 * ND_INCOMPLETE. Post-ARP resolution, after ire's nce_state changes to 23951 * ND_REACHABLE, all subsquent large packets for this ire will 23952 * get fragemented and sent out by this function. 23953 */ 23954 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 23955 /* If nce_state is ND_INITIAL, trigger ARP query */ 23956 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 23957 ip1dbg(("ip_wput_frag: mac address for ire is unresolved" 23958 " - dropping packet\n")); 23959 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 23960 freemsg(mp); 23961 return; 23962 } 23963 23964 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 23965 "ip_wput_frag_start:"); 23966 23967 if (mp->b_datap->db_type == M_CTL) { 23968 first_mp = mp; 23969 mp_orig = mp = mp->b_cont; 23970 mctl_present = B_TRUE; 23971 } else { 23972 first_mp = mp; 23973 mctl_present = B_FALSE; 23974 } 23975 23976 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 23977 ipha = (ipha_t *)mp->b_rptr; 23978 23979 /* 23980 * If the Don't Fragment flag is on, generate an ICMP destination 23981 * unreachable, fragmentation needed. 23982 */ 23983 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 23984 if (offset & IPH_DF) { 23985 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 23986 /* 23987 * Need to compute hdr checksum if called from ip_wput_ire. 23988 * Note that ip_rput_forward verifies the checksum before 23989 * calling this routine so in that case this is a noop. 23990 */ 23991 ipha->ipha_hdr_checksum = 0; 23992 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 23993 icmp_frag_needed(ire->ire_stq, first_mp, max_frag, zoneid); 23994 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 23995 "ip_wput_frag_end:(%S)", 23996 "don't fragment"); 23997 return; 23998 } 23999 if (mctl_present) 24000 freeb(first_mp); 24001 /* 24002 * Establish the starting offset. May not be zero if we are fragging 24003 * a fragment that is being forwarded. 24004 */ 24005 offset = offset & IPH_OFFSET; 24006 24007 /* TODO why is this test needed? */ 24008 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 24009 if (((max_frag - LENGTH) & ~7) < 8) { 24010 /* TODO: notify ulp somehow */ 24011 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24012 freemsg(mp); 24013 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24014 "ip_wput_frag_end:(%S)", 24015 "len < 8"); 24016 return; 24017 } 24018 24019 hdr_len = (V_HLEN & 0xF) << 2; 24020 24021 ipha->ipha_hdr_checksum = 0; 24022 24023 /* 24024 * Establish the number of bytes maximum per frag, after putting 24025 * in the header. 24026 */ 24027 len = (max_frag - hdr_len) & ~7; 24028 24029 /* Check if we can use MDT to send out the frags. */ 24030 ASSERT(!IRE_IS_LOCAL(ire)); 24031 if (hdr_len == IP_SIMPLE_HDR_LENGTH && ip_multidata_outbound && 24032 !(ire->ire_flags & RTF_MULTIRT) && !IPP_ENABLED(IPP_LOCAL_OUT) && 24033 ill != NULL && ILL_MDT_CAPABLE(ill) && 24034 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 24035 ASSERT(ill->ill_mdt_capab != NULL); 24036 if (!ill->ill_mdt_capab->ill_mdt_on) { 24037 /* 24038 * If MDT has been previously turned off in the past, 24039 * and we currently can do MDT (due to IPQoS policy 24040 * removal, etc.) then enable it for this interface. 24041 */ 24042 ill->ill_mdt_capab->ill_mdt_on = 1; 24043 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 24044 ill->ill_name)); 24045 } 24046 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 24047 offset); 24048 return; 24049 } 24050 24051 /* Get a copy of the header for the trailing frags */ 24052 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset); 24053 if (!hdr_mp) { 24054 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24055 freemsg(mp); 24056 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24057 "ip_wput_frag_end:(%S)", 24058 "couldn't copy hdr"); 24059 return; 24060 } 24061 if (DB_CRED(mp) != NULL) 24062 mblk_setcred(hdr_mp, DB_CRED(mp)); 24063 24064 /* Store the starting offset, with the MoreFrags flag. */ 24065 i1 = offset | IPH_MF | frag_flag; 24066 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 24067 24068 /* Establish the ending byte offset, based on the starting offset. */ 24069 offset <<= 3; 24070 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 24071 24072 /* Store the length of the first fragment in the IP header. */ 24073 i1 = len + hdr_len; 24074 ASSERT(i1 <= IP_MAXPACKET); 24075 ipha->ipha_length = htons((uint16_t)i1); 24076 24077 /* 24078 * Compute the IP header checksum for the first frag. We have to 24079 * watch out that we stop at the end of the header. 24080 */ 24081 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24082 24083 /* 24084 * Now carve off the first frag. Note that this will include the 24085 * original IP header. 24086 */ 24087 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 24088 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24089 freeb(hdr_mp); 24090 freemsg(mp_orig); 24091 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24092 "ip_wput_frag_end:(%S)", 24093 "couldn't carve first"); 24094 return; 24095 } 24096 24097 /* 24098 * Multirouting case. Each fragment is replicated 24099 * via all non-condemned RTF_MULTIRT routes 24100 * currently resolved. 24101 * We ensure that first_ire is the first RTF_MULTIRT 24102 * ire in the bucket. 24103 */ 24104 if (ire->ire_flags & RTF_MULTIRT) { 24105 irb = ire->ire_bucket; 24106 ASSERT(irb != NULL); 24107 24108 multirt_send = B_TRUE; 24109 24110 /* Make sure we do not omit any multiroute ire. */ 24111 IRB_REFHOLD(irb); 24112 for (first_ire = irb->irb_ire; 24113 first_ire != NULL; 24114 first_ire = first_ire->ire_next) { 24115 if ((first_ire->ire_flags & RTF_MULTIRT) && 24116 (first_ire->ire_addr == ire->ire_addr) && 24117 !(first_ire->ire_marks & 24118 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 24119 break; 24120 } 24121 24122 if (first_ire != NULL) { 24123 if (first_ire != ire) { 24124 IRE_REFHOLD(first_ire); 24125 /* 24126 * Do not release the ire passed in 24127 * as the argument. 24128 */ 24129 ire = first_ire; 24130 } else { 24131 first_ire = NULL; 24132 } 24133 } 24134 IRB_REFRELE(irb); 24135 24136 /* 24137 * Save the first ire; we will need to restore it 24138 * for the trailing frags. 24139 * We REFHOLD save_ire, as each iterated ire will be 24140 * REFRELEd. 24141 */ 24142 save_ire = ire; 24143 IRE_REFHOLD(save_ire); 24144 } 24145 24146 /* 24147 * First fragment emission loop. 24148 * In most cases, the emission loop below is entered only 24149 * once. Only in the case where the ire holds the RTF_MULTIRT 24150 * flag, do we loop to process all RTF_MULTIRT ires in the 24151 * bucket, and send the fragment through all crossed 24152 * RTF_MULTIRT routes. 24153 */ 24154 do { 24155 if (ire->ire_flags & RTF_MULTIRT) { 24156 /* 24157 * We are in a multiple send case, need to get 24158 * the next ire and make a copy of the packet. 24159 * ire1 holds here the next ire to process in the 24160 * bucket. If multirouting is expected, 24161 * any non-RTF_MULTIRT ire that has the 24162 * right destination address is ignored. 24163 * 24164 * We have to take into account the MTU of 24165 * each walked ire. max_frag is set by the 24166 * the caller and generally refers to 24167 * the primary ire entry. Here we ensure that 24168 * no route with a lower MTU will be used, as 24169 * fragments are carved once for all ires, 24170 * then replicated. 24171 */ 24172 ASSERT(irb != NULL); 24173 IRB_REFHOLD(irb); 24174 for (ire1 = ire->ire_next; 24175 ire1 != NULL; 24176 ire1 = ire1->ire_next) { 24177 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 24178 continue; 24179 if (ire1->ire_addr != ire->ire_addr) 24180 continue; 24181 if (ire1->ire_marks & 24182 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 24183 continue; 24184 /* 24185 * Ensure we do not exceed the MTU 24186 * of the next route. 24187 */ 24188 if (ire1->ire_max_frag < max_frag) { 24189 ip_multirt_bad_mtu(ire1, max_frag); 24190 continue; 24191 } 24192 24193 /* Got one. */ 24194 IRE_REFHOLD(ire1); 24195 break; 24196 } 24197 IRB_REFRELE(irb); 24198 24199 if (ire1 != NULL) { 24200 next_mp = copyb(mp); 24201 if ((next_mp == NULL) || 24202 ((mp->b_cont != NULL) && 24203 ((next_mp->b_cont = 24204 dupmsg(mp->b_cont)) == NULL))) { 24205 freemsg(next_mp); 24206 next_mp = NULL; 24207 ire_refrele(ire1); 24208 ire1 = NULL; 24209 } 24210 } 24211 24212 /* Last multiroute ire; don't loop anymore. */ 24213 if (ire1 == NULL) { 24214 multirt_send = B_FALSE; 24215 } 24216 } 24217 24218 ll_hdr_len = 0; 24219 LOCK_IRE_FP_MP(ire); 24220 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24221 if (ll_hdr_mp != NULL) { 24222 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24223 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 24224 } else { 24225 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24226 } 24227 24228 /* If there is a transmit header, get a copy for this frag. */ 24229 /* 24230 * TODO: should check db_ref before calling ip_carve_mp since 24231 * it might give us a dup. 24232 */ 24233 if (!ll_hdr_mp) { 24234 /* No xmit header. */ 24235 xmit_mp = mp; 24236 24237 /* We have a link-layer header that can fit in our mblk. */ 24238 } else if (mp->b_datap->db_ref == 1 && 24239 ll_hdr_len != 0 && 24240 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24241 /* M_DATA fastpath */ 24242 mp->b_rptr -= ll_hdr_len; 24243 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 24244 xmit_mp = mp; 24245 24246 /* Corner case if copyb has failed */ 24247 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 24248 UNLOCK_IRE_FP_MP(ire); 24249 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24250 freeb(hdr_mp); 24251 freemsg(mp); 24252 freemsg(mp_orig); 24253 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24254 "ip_wput_frag_end:(%S)", 24255 "discard"); 24256 24257 if (multirt_send) { 24258 ASSERT(ire1); 24259 ASSERT(next_mp); 24260 24261 freemsg(next_mp); 24262 ire_refrele(ire1); 24263 } 24264 if (save_ire != NULL) 24265 IRE_REFRELE(save_ire); 24266 24267 if (first_ire != NULL) 24268 ire_refrele(first_ire); 24269 return; 24270 24271 /* 24272 * Case of res_mp OR the fastpath mp can't fit 24273 * in the mblk 24274 */ 24275 } else { 24276 xmit_mp->b_cont = mp; 24277 if (DB_CRED(mp) != NULL) 24278 mblk_setcred(xmit_mp, DB_CRED(mp)); 24279 /* 24280 * Get priority marking, if any. 24281 * We propagate the CoS marking from the 24282 * original packet that went to QoS processing 24283 * in ip_wput_ire to the newly carved mp. 24284 */ 24285 if (DB_TYPE(xmit_mp) == M_DATA) 24286 xmit_mp->b_band = mp->b_band; 24287 } 24288 UNLOCK_IRE_FP_MP(ire); 24289 24290 q = ire->ire_stq; 24291 out_ill = (ill_t *)q->q_ptr; 24292 24293 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24294 24295 DTRACE_PROBE4(ip4__physical__out__start, 24296 ill_t *, NULL, ill_t *, out_ill, 24297 ipha_t *, ipha, mblk_t *, xmit_mp); 24298 24299 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 24300 NULL, out_ill, ipha, xmit_mp, mp); 24301 24302 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, xmit_mp); 24303 24304 if (xmit_mp != NULL) { 24305 putnext(q, xmit_mp); 24306 24307 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutTransmits); 24308 UPDATE_MIB(out_ill->ill_ip_mib, 24309 ipIfStatsHCOutOctets, i1); 24310 24311 if (pkt_type != OB_PKT) { 24312 /* 24313 * Update the packet count and MIB stats 24314 * of trailing RTF_MULTIRT ires. 24315 */ 24316 UPDATE_OB_PKT_COUNT(ire); 24317 BUMP_MIB(out_ill->ill_ip_mib, 24318 ipIfStatsOutFragReqds); 24319 } 24320 } 24321 24322 if (multirt_send) { 24323 /* 24324 * We are in a multiple send case; look for 24325 * the next ire and re-enter the loop. 24326 */ 24327 ASSERT(ire1); 24328 ASSERT(next_mp); 24329 /* REFRELE the current ire before looping */ 24330 ire_refrele(ire); 24331 ire = ire1; 24332 ire1 = NULL; 24333 mp = next_mp; 24334 next_mp = NULL; 24335 } 24336 } while (multirt_send); 24337 24338 ASSERT(ire1 == NULL); 24339 24340 /* Restore the original ire; we need it for the trailing frags */ 24341 if (save_ire != NULL) { 24342 /* REFRELE the last iterated ire */ 24343 ire_refrele(ire); 24344 /* save_ire has been REFHOLDed */ 24345 ire = save_ire; 24346 save_ire = NULL; 24347 q = ire->ire_stq; 24348 } 24349 24350 if (pkt_type == OB_PKT) { 24351 UPDATE_OB_PKT_COUNT(ire); 24352 } else { 24353 out_ill = (ill_t *)q->q_ptr; 24354 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); 24355 UPDATE_IB_PKT_COUNT(ire); 24356 } 24357 24358 /* Advance the offset to the second frag starting point. */ 24359 offset += len; 24360 /* 24361 * Update hdr_len from the copied header - there might be less options 24362 * in the later fragments. 24363 */ 24364 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 24365 /* Loop until done. */ 24366 for (;;) { 24367 uint16_t offset_and_flags; 24368 uint16_t ip_len; 24369 24370 if (ip_data_end - offset > len) { 24371 /* 24372 * Carve off the appropriate amount from the original 24373 * datagram. 24374 */ 24375 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24376 mp = NULL; 24377 break; 24378 } 24379 /* 24380 * More frags after this one. Get another copy 24381 * of the header. 24382 */ 24383 if (carve_mp->b_datap->db_ref == 1 && 24384 hdr_mp->b_wptr - hdr_mp->b_rptr < 24385 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24386 /* Inline IP header */ 24387 carve_mp->b_rptr -= hdr_mp->b_wptr - 24388 hdr_mp->b_rptr; 24389 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24390 hdr_mp->b_wptr - hdr_mp->b_rptr); 24391 mp = carve_mp; 24392 } else { 24393 if (!(mp = copyb(hdr_mp))) { 24394 freemsg(carve_mp); 24395 break; 24396 } 24397 /* Get priority marking, if any. */ 24398 mp->b_band = carve_mp->b_band; 24399 mp->b_cont = carve_mp; 24400 } 24401 ipha = (ipha_t *)mp->b_rptr; 24402 offset_and_flags = IPH_MF; 24403 } else { 24404 /* 24405 * Last frag. Consume the header. Set len to 24406 * the length of this last piece. 24407 */ 24408 len = ip_data_end - offset; 24409 24410 /* 24411 * Carve off the appropriate amount from the original 24412 * datagram. 24413 */ 24414 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 24415 mp = NULL; 24416 break; 24417 } 24418 if (carve_mp->b_datap->db_ref == 1 && 24419 hdr_mp->b_wptr - hdr_mp->b_rptr < 24420 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 24421 /* Inline IP header */ 24422 carve_mp->b_rptr -= hdr_mp->b_wptr - 24423 hdr_mp->b_rptr; 24424 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 24425 hdr_mp->b_wptr - hdr_mp->b_rptr); 24426 mp = carve_mp; 24427 freeb(hdr_mp); 24428 hdr_mp = mp; 24429 } else { 24430 mp = hdr_mp; 24431 /* Get priority marking, if any. */ 24432 mp->b_band = carve_mp->b_band; 24433 mp->b_cont = carve_mp; 24434 } 24435 ipha = (ipha_t *)mp->b_rptr; 24436 /* A frag of a frag might have IPH_MF non-zero */ 24437 offset_and_flags = 24438 ntohs(ipha->ipha_fragment_offset_and_flags) & 24439 IPH_MF; 24440 } 24441 offset_and_flags |= (uint16_t)(offset >> 3); 24442 offset_and_flags |= (uint16_t)frag_flag; 24443 /* Store the offset and flags in the IP header. */ 24444 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 24445 24446 /* Store the length in the IP header. */ 24447 ip_len = (uint16_t)(len + hdr_len); 24448 ipha->ipha_length = htons(ip_len); 24449 24450 /* 24451 * Set the IP header checksum. Note that mp is just 24452 * the header, so this is easy to pass to ip_csum. 24453 */ 24454 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 24455 24456 /* Attach a transmit header, if any, and ship it. */ 24457 if (pkt_type == OB_PKT) { 24458 UPDATE_OB_PKT_COUNT(ire); 24459 } else { 24460 out_ill = (ill_t *)q->q_ptr; 24461 BUMP_MIB(out_ill->ill_ip_mib, 24462 ipIfStatsHCOutForwDatagrams); 24463 UPDATE_IB_PKT_COUNT(ire); 24464 } 24465 24466 if (ire->ire_flags & RTF_MULTIRT) { 24467 irb = ire->ire_bucket; 24468 ASSERT(irb != NULL); 24469 24470 multirt_send = B_TRUE; 24471 24472 /* 24473 * Save the original ire; we will need to restore it 24474 * for the tailing frags. 24475 */ 24476 save_ire = ire; 24477 IRE_REFHOLD(save_ire); 24478 } 24479 /* 24480 * Emission loop for this fragment, similar 24481 * to what is done for the first fragment. 24482 */ 24483 do { 24484 if (multirt_send) { 24485 /* 24486 * We are in a multiple send case, need to get 24487 * the next ire and make a copy of the packet. 24488 */ 24489 ASSERT(irb != NULL); 24490 IRB_REFHOLD(irb); 24491 for (ire1 = ire->ire_next; 24492 ire1 != NULL; 24493 ire1 = ire1->ire_next) { 24494 if (!(ire1->ire_flags & RTF_MULTIRT)) 24495 continue; 24496 if (ire1->ire_addr != ire->ire_addr) 24497 continue; 24498 if (ire1->ire_marks & 24499 (IRE_MARK_CONDEMNED| 24500 IRE_MARK_HIDDEN)) 24501 continue; 24502 /* 24503 * Ensure we do not exceed the MTU 24504 * of the next route. 24505 */ 24506 if (ire1->ire_max_frag < max_frag) { 24507 ip_multirt_bad_mtu(ire1, 24508 max_frag); 24509 continue; 24510 } 24511 24512 /* Got one. */ 24513 IRE_REFHOLD(ire1); 24514 break; 24515 } 24516 IRB_REFRELE(irb); 24517 24518 if (ire1 != NULL) { 24519 next_mp = copyb(mp); 24520 if ((next_mp == NULL) || 24521 ((mp->b_cont != NULL) && 24522 ((next_mp->b_cont = 24523 dupmsg(mp->b_cont)) == NULL))) { 24524 freemsg(next_mp); 24525 next_mp = NULL; 24526 ire_refrele(ire1); 24527 ire1 = NULL; 24528 } 24529 } 24530 24531 /* Last multiroute ire; don't loop anymore. */ 24532 if (ire1 == NULL) { 24533 multirt_send = B_FALSE; 24534 } 24535 } 24536 24537 /* Update transmit header */ 24538 ll_hdr_len = 0; 24539 LOCK_IRE_FP_MP(ire); 24540 ll_hdr_mp = ire->ire_nce->nce_fp_mp; 24541 if (ll_hdr_mp != NULL) { 24542 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 24543 ll_hdr_len = MBLKL(ll_hdr_mp); 24544 } else { 24545 ll_hdr_mp = ire->ire_nce->nce_res_mp; 24546 } 24547 24548 if (!ll_hdr_mp) { 24549 xmit_mp = mp; 24550 24551 /* 24552 * We have link-layer header that can fit in 24553 * our mblk. 24554 */ 24555 } else if (mp->b_datap->db_ref == 1 && 24556 ll_hdr_len != 0 && 24557 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 24558 /* M_DATA fastpath */ 24559 mp->b_rptr -= ll_hdr_len; 24560 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 24561 ll_hdr_len); 24562 xmit_mp = mp; 24563 24564 /* 24565 * Case of res_mp OR the fastpath mp can't fit 24566 * in the mblk 24567 */ 24568 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 24569 xmit_mp->b_cont = mp; 24570 if (DB_CRED(mp) != NULL) 24571 mblk_setcred(xmit_mp, DB_CRED(mp)); 24572 /* Get priority marking, if any. */ 24573 if (DB_TYPE(xmit_mp) == M_DATA) 24574 xmit_mp->b_band = mp->b_band; 24575 24576 /* Corner case if copyb failed */ 24577 } else { 24578 /* 24579 * Exit both the replication and 24580 * fragmentation loops. 24581 */ 24582 UNLOCK_IRE_FP_MP(ire); 24583 goto drop_pkt; 24584 } 24585 UNLOCK_IRE_FP_MP(ire); 24586 24587 mp1 = mp; 24588 out_ill = (ill_t *)q->q_ptr; 24589 24590 BUMP_MIB(out_ill->ill_ip_mib, ipIfStatsOutFragCreates); 24591 24592 DTRACE_PROBE4(ip4__physical__out__start, 24593 ill_t *, NULL, ill_t *, out_ill, 24594 ipha_t *, ipha, mblk_t *, xmit_mp); 24595 24596 FW_HOOKS(ip4_physical_out_event, 24597 ipv4firewall_physical_out, 24598 NULL, out_ill, ipha, xmit_mp, mp); 24599 24600 DTRACE_PROBE1(ip4__physical__out__end, 24601 mblk_t *, xmit_mp); 24602 24603 if (mp != mp1 && hdr_mp == mp1) 24604 hdr_mp = mp; 24605 if (mp != mp1 && mp_orig == mp1) 24606 mp_orig = mp; 24607 24608 if (xmit_mp != NULL) { 24609 putnext(q, xmit_mp); 24610 24611 BUMP_MIB(out_ill->ill_ip_mib, 24612 ipIfStatsHCOutTransmits); 24613 UPDATE_MIB(out_ill->ill_ip_mib, 24614 ipIfStatsHCOutOctets, ip_len); 24615 24616 if (pkt_type != OB_PKT) { 24617 /* 24618 * Update the packet count of trailing 24619 * RTF_MULTIRT ires. 24620 */ 24621 UPDATE_OB_PKT_COUNT(ire); 24622 } 24623 } 24624 24625 /* All done if we just consumed the hdr_mp. */ 24626 if (mp == hdr_mp) { 24627 last_frag = B_TRUE; 24628 BUMP_MIB(out_ill->ill_ip_mib, 24629 ipIfStatsOutFragOKs); 24630 } 24631 24632 if (multirt_send) { 24633 /* 24634 * We are in a multiple send case; look for 24635 * the next ire and re-enter the loop. 24636 */ 24637 ASSERT(ire1); 24638 ASSERT(next_mp); 24639 /* REFRELE the current ire before looping */ 24640 ire_refrele(ire); 24641 ire = ire1; 24642 ire1 = NULL; 24643 q = ire->ire_stq; 24644 mp = next_mp; 24645 next_mp = NULL; 24646 } 24647 } while (multirt_send); 24648 /* 24649 * Restore the original ire; we need it for the 24650 * trailing frags 24651 */ 24652 if (save_ire != NULL) { 24653 ASSERT(ire1 == NULL); 24654 /* REFRELE the last iterated ire */ 24655 ire_refrele(ire); 24656 /* save_ire has been REFHOLDed */ 24657 ire = save_ire; 24658 q = ire->ire_stq; 24659 save_ire = NULL; 24660 } 24661 24662 if (last_frag) { 24663 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24664 "ip_wput_frag_end:(%S)", 24665 "consumed hdr_mp"); 24666 24667 if (first_ire != NULL) 24668 ire_refrele(first_ire); 24669 return; 24670 } 24671 /* Otherwise, advance and loop. */ 24672 offset += len; 24673 } 24674 24675 drop_pkt: 24676 /* Clean up following allocation failure. */ 24677 BUMP_MIB(mibptr, ipIfStatsOutFragFails); 24678 freemsg(mp); 24679 if (mp != hdr_mp) 24680 freeb(hdr_mp); 24681 if (mp != mp_orig) 24682 freemsg(mp_orig); 24683 24684 if (save_ire != NULL) 24685 IRE_REFRELE(save_ire); 24686 if (first_ire != NULL) 24687 ire_refrele(first_ire); 24688 24689 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 24690 "ip_wput_frag_end:(%S)", 24691 "end--alloc failure"); 24692 } 24693 24694 /* 24695 * Copy the header plus those options which have the copy bit set 24696 */ 24697 static mblk_t * 24698 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset) 24699 { 24700 mblk_t *mp; 24701 uchar_t *up; 24702 24703 /* 24704 * Quick check if we need to look for options without the copy bit 24705 * set 24706 */ 24707 mp = allocb(ip_wroff_extra + hdr_len, BPRI_HI); 24708 if (!mp) 24709 return (mp); 24710 mp->b_rptr += ip_wroff_extra; 24711 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 24712 bcopy(rptr, mp->b_rptr, hdr_len); 24713 mp->b_wptr += hdr_len + ip_wroff_extra; 24714 return (mp); 24715 } 24716 up = mp->b_rptr; 24717 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 24718 up += IP_SIMPLE_HDR_LENGTH; 24719 rptr += IP_SIMPLE_HDR_LENGTH; 24720 hdr_len -= IP_SIMPLE_HDR_LENGTH; 24721 while (hdr_len > 0) { 24722 uint32_t optval; 24723 uint32_t optlen; 24724 24725 optval = *rptr; 24726 if (optval == IPOPT_EOL) 24727 break; 24728 if (optval == IPOPT_NOP) 24729 optlen = 1; 24730 else 24731 optlen = rptr[1]; 24732 if (optval & IPOPT_COPY) { 24733 bcopy(rptr, up, optlen); 24734 up += optlen; 24735 } 24736 rptr += optlen; 24737 hdr_len -= optlen; 24738 } 24739 /* 24740 * Make sure that we drop an even number of words by filling 24741 * with EOL to the next word boundary. 24742 */ 24743 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 24744 hdr_len & 0x3; hdr_len++) 24745 *up++ = IPOPT_EOL; 24746 mp->b_wptr = up; 24747 /* Update header length */ 24748 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 24749 return (mp); 24750 } 24751 24752 /* 24753 * Delivery to local recipients including fanout to multiple recipients. 24754 * Does not do checksumming of UDP/TCP. 24755 * Note: q should be the read side queue for either the ill or conn. 24756 * Note: rq should be the read side q for the lower (ill) stream. 24757 * We don't send packets to IPPF processing, thus the last argument 24758 * to all the fanout calls are B_FALSE. 24759 */ 24760 void 24761 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 24762 int fanout_flags, zoneid_t zoneid) 24763 { 24764 uint32_t protocol; 24765 mblk_t *first_mp; 24766 boolean_t mctl_present; 24767 int ire_type; 24768 #define rptr ((uchar_t *)ipha) 24769 24770 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 24771 "ip_wput_local_start: q %p", q); 24772 24773 if (ire != NULL) { 24774 ire_type = ire->ire_type; 24775 } else { 24776 /* 24777 * Only ip_multicast_loopback() calls us with a NULL ire. If the 24778 * packet is not multicast, we can't tell the ire type. 24779 */ 24780 ASSERT(CLASSD(ipha->ipha_dst)); 24781 ire_type = IRE_BROADCAST; 24782 } 24783 24784 first_mp = mp; 24785 if (first_mp->b_datap->db_type == M_CTL) { 24786 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 24787 if (!io->ipsec_out_secure) { 24788 /* 24789 * This ipsec_out_t was allocated in ip_wput 24790 * for multicast packets to store the ill_index. 24791 * As this is being delivered locally, we don't 24792 * need this anymore. 24793 */ 24794 mp = first_mp->b_cont; 24795 freeb(first_mp); 24796 first_mp = mp; 24797 mctl_present = B_FALSE; 24798 } else { 24799 /* 24800 * Convert IPSEC_OUT to IPSEC_IN, preserving all 24801 * security properties for the looped-back packet. 24802 */ 24803 mctl_present = B_TRUE; 24804 mp = first_mp->b_cont; 24805 ASSERT(mp != NULL); 24806 ipsec_out_to_in(first_mp); 24807 } 24808 } else { 24809 mctl_present = B_FALSE; 24810 } 24811 24812 DTRACE_PROBE4(ip4__loopback__in__start, 24813 ill_t *, ill, ill_t *, NULL, 24814 ipha_t *, ipha, mblk_t *, first_mp); 24815 24816 FW_HOOKS(ip4_loopback_in_event, ipv4firewall_loopback_in, 24817 ill, NULL, ipha, first_mp, mp); 24818 24819 DTRACE_PROBE1(ip4__loopback__in__end, mblk_t *, first_mp); 24820 24821 if (first_mp == NULL) 24822 return; 24823 24824 loopback_packets++; 24825 24826 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 24827 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 24828 if (!IS_SIMPLE_IPH(ipha)) { 24829 ip_wput_local_options(ipha); 24830 } 24831 24832 protocol = ipha->ipha_protocol; 24833 switch (protocol) { 24834 case IPPROTO_ICMP: { 24835 ire_t *ire_zone; 24836 ilm_t *ilm; 24837 mblk_t *mp1; 24838 zoneid_t last_zoneid; 24839 24840 if (CLASSD(ipha->ipha_dst) && 24841 !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 24842 ASSERT(ire_type == IRE_BROADCAST); 24843 /* 24844 * In the multicast case, applications may have joined 24845 * the group from different zones, so we need to deliver 24846 * the packet to each of them. Loop through the 24847 * multicast memberships structures (ilm) on the receive 24848 * ill and send a copy of the packet up each matching 24849 * one. However, we don't do this for multicasts sent on 24850 * the loopback interface (PHYI_LOOPBACK flag set) as 24851 * they must stay in the sender's zone. 24852 * 24853 * ilm_add_v6() ensures that ilms in the same zone are 24854 * contiguous in the ill_ilm list. We use this property 24855 * to avoid sending duplicates needed when two 24856 * applications in the same zone join the same group on 24857 * different logical interfaces: we ignore the ilm if 24858 * it's zoneid is the same as the last matching one. 24859 * In addition, the sending of the packet for 24860 * ire_zoneid is delayed until all of the other ilms 24861 * have been exhausted. 24862 */ 24863 last_zoneid = -1; 24864 ILM_WALKER_HOLD(ill); 24865 for (ilm = ill->ill_ilm; ilm != NULL; 24866 ilm = ilm->ilm_next) { 24867 if ((ilm->ilm_flags & ILM_DELETED) || 24868 ipha->ipha_dst != ilm->ilm_addr || 24869 ilm->ilm_zoneid == last_zoneid || 24870 ilm->ilm_zoneid == zoneid || 24871 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 24872 continue; 24873 mp1 = ip_copymsg(first_mp); 24874 if (mp1 == NULL) 24875 continue; 24876 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 24877 mctl_present, B_FALSE, ill, 24878 ilm->ilm_zoneid); 24879 last_zoneid = ilm->ilm_zoneid; 24880 } 24881 ILM_WALKER_RELE(ill); 24882 /* 24883 * Loopback case: the sending endpoint has 24884 * IP_MULTICAST_LOOP disabled, therefore we don't 24885 * dispatch the multicast packet to the sending zone. 24886 */ 24887 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 24888 freemsg(first_mp); 24889 return; 24890 } 24891 } else if (ire_type == IRE_BROADCAST) { 24892 /* 24893 * In the broadcast case, there may be many zones 24894 * which need a copy of the packet delivered to them. 24895 * There is one IRE_BROADCAST per broadcast address 24896 * and per zone; we walk those using a helper function. 24897 * In addition, the sending of the packet for zoneid is 24898 * delayed until all of the other ires have been 24899 * processed. 24900 */ 24901 IRB_REFHOLD(ire->ire_bucket); 24902 ire_zone = NULL; 24903 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 24904 ire)) != NULL) { 24905 mp1 = ip_copymsg(first_mp); 24906 if (mp1 == NULL) 24907 continue; 24908 24909 UPDATE_IB_PKT_COUNT(ire_zone); 24910 ire_zone->ire_last_used_time = lbolt; 24911 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 24912 mctl_present, B_FALSE, ill, 24913 ire_zone->ire_zoneid); 24914 } 24915 IRB_REFRELE(ire->ire_bucket); 24916 } 24917 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 24918 0, mctl_present, B_FALSE, ill, zoneid); 24919 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24920 "ip_wput_local_end: q %p (%S)", 24921 q, "icmp"); 24922 return; 24923 } 24924 case IPPROTO_IGMP: 24925 if ((mp = igmp_input(q, mp, ill)) == NULL) { 24926 /* Bad packet - discarded by igmp_input */ 24927 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24928 "ip_wput_local_end: q %p (%S)", 24929 q, "igmp_input--bad packet"); 24930 if (mctl_present) 24931 freeb(first_mp); 24932 return; 24933 } 24934 /* 24935 * igmp_input() may have returned the pulled up message. 24936 * So first_mp and ipha need to be reinitialized. 24937 */ 24938 ipha = (ipha_t *)mp->b_rptr; 24939 if (mctl_present) 24940 first_mp->b_cont = mp; 24941 else 24942 first_mp = mp; 24943 /* deliver to local raw users */ 24944 break; 24945 case IPPROTO_ENCAP: 24946 /* 24947 * This case is covered by either ip_fanout_proto, or by 24948 * the above security processing for self-tunneled packets. 24949 */ 24950 break; 24951 case IPPROTO_UDP: { 24952 uint16_t *up; 24953 uint32_t ports; 24954 24955 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 24956 UDP_PORTS_OFFSET); 24957 /* Force a 'valid' checksum. */ 24958 up[3] = 0; 24959 24960 ports = *(uint32_t *)up; 24961 ip_fanout_udp(q, first_mp, ill, ipha, ports, 24962 (ire_type == IRE_BROADCAST), 24963 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 24964 IP_FF_SEND_SLLA | IP_FF_IPINFO, mctl_present, B_FALSE, 24965 ill, zoneid); 24966 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24967 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 24968 return; 24969 } 24970 case IPPROTO_TCP: { 24971 24972 /* 24973 * For TCP, discard broadcast packets. 24974 */ 24975 if ((ushort_t)ire_type == IRE_BROADCAST) { 24976 freemsg(first_mp); 24977 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 24978 ip2dbg(("ip_wput_local: discard broadcast\n")); 24979 return; 24980 } 24981 24982 if (mp->b_datap->db_type == M_DATA) { 24983 /* 24984 * M_DATA mblk, so init mblk (chain) for no struio(). 24985 */ 24986 mblk_t *mp1 = mp; 24987 24988 do 24989 mp1->b_datap->db_struioflag = 0; 24990 while ((mp1 = mp1->b_cont) != NULL); 24991 } 24992 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 24993 <= mp->b_wptr); 24994 ip_fanout_tcp(q, first_mp, ill, ipha, 24995 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 24996 IP_FF_SYN_ADDIRE | IP_FF_IPINFO, 24997 mctl_present, B_FALSE, zoneid); 24998 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 24999 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 25000 return; 25001 } 25002 case IPPROTO_SCTP: 25003 { 25004 uint32_t ports; 25005 25006 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 25007 ip_fanout_sctp(first_mp, ill, ipha, ports, 25008 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 25009 IP_FF_IPINFO, 25010 mctl_present, B_FALSE, 0, zoneid); 25011 return; 25012 } 25013 25014 default: 25015 break; 25016 } 25017 /* 25018 * Find a client for some other protocol. We give 25019 * copies to multiple clients, if more than one is 25020 * bound. 25021 */ 25022 ip_fanout_proto(q, first_mp, ill, ipha, 25023 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 25024 mctl_present, B_FALSE, ill, zoneid); 25025 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 25026 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 25027 #undef rptr 25028 } 25029 25030 /* 25031 * Update any source route, record route, or timestamp options. 25032 * Check that we are at end of strict source route. 25033 * The options have been sanity checked by ip_wput_options(). 25034 */ 25035 static void 25036 ip_wput_local_options(ipha_t *ipha) 25037 { 25038 ipoptp_t opts; 25039 uchar_t *opt; 25040 uint8_t optval; 25041 uint8_t optlen; 25042 ipaddr_t dst; 25043 uint32_t ts; 25044 ire_t *ire; 25045 timestruc_t now; 25046 25047 ip2dbg(("ip_wput_local_options\n")); 25048 for (optval = ipoptp_first(&opts, ipha); 25049 optval != IPOPT_EOL; 25050 optval = ipoptp_next(&opts)) { 25051 opt = opts.ipoptp_cur; 25052 optlen = opts.ipoptp_len; 25053 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 25054 switch (optval) { 25055 uint32_t off; 25056 case IPOPT_SSRR: 25057 case IPOPT_LSRR: 25058 off = opt[IPOPT_OFFSET]; 25059 off--; 25060 if (optlen < IP_ADDR_LEN || 25061 off > optlen - IP_ADDR_LEN) { 25062 /* End of source route */ 25063 break; 25064 } 25065 /* 25066 * This will only happen if two consecutive entries 25067 * in the source route contains our address or if 25068 * it is a packet with a loose source route which 25069 * reaches us before consuming the whole source route 25070 */ 25071 ip1dbg(("ip_wput_local_options: not end of SR\n")); 25072 if (optval == IPOPT_SSRR) { 25073 return; 25074 } 25075 /* 25076 * Hack: instead of dropping the packet truncate the 25077 * source route to what has been used by filling the 25078 * rest with IPOPT_NOP. 25079 */ 25080 opt[IPOPT_OLEN] = (uint8_t)off; 25081 while (off < optlen) { 25082 opt[off++] = IPOPT_NOP; 25083 } 25084 break; 25085 case IPOPT_RR: 25086 off = opt[IPOPT_OFFSET]; 25087 off--; 25088 if (optlen < IP_ADDR_LEN || 25089 off > optlen - IP_ADDR_LEN) { 25090 /* No more room - ignore */ 25091 ip1dbg(( 25092 "ip_wput_forward_options: end of RR\n")); 25093 break; 25094 } 25095 dst = htonl(INADDR_LOOPBACK); 25096 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25097 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25098 break; 25099 case IPOPT_TS: 25100 /* Insert timestamp if there is romm */ 25101 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25102 case IPOPT_TS_TSONLY: 25103 off = IPOPT_TS_TIMELEN; 25104 break; 25105 case IPOPT_TS_PRESPEC: 25106 case IPOPT_TS_PRESPEC_RFC791: 25107 /* Verify that the address matched */ 25108 off = opt[IPOPT_OFFSET] - 1; 25109 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 25110 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 25111 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE); 25112 if (ire == NULL) { 25113 /* Not for us */ 25114 break; 25115 } 25116 ire_refrele(ire); 25117 /* FALLTHRU */ 25118 case IPOPT_TS_TSANDADDR: 25119 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 25120 break; 25121 default: 25122 /* 25123 * ip_*put_options should have already 25124 * dropped this packet. 25125 */ 25126 cmn_err(CE_PANIC, "ip_wput_local_options: " 25127 "unknown IT - bug in ip_wput_options?\n"); 25128 return; /* Keep "lint" happy */ 25129 } 25130 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 25131 /* Increase overflow counter */ 25132 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 25133 opt[IPOPT_POS_OV_FLG] = (uint8_t) 25134 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 25135 (off << 4); 25136 break; 25137 } 25138 off = opt[IPOPT_OFFSET] - 1; 25139 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 25140 case IPOPT_TS_PRESPEC: 25141 case IPOPT_TS_PRESPEC_RFC791: 25142 case IPOPT_TS_TSANDADDR: 25143 dst = htonl(INADDR_LOOPBACK); 25144 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 25145 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 25146 /* FALLTHRU */ 25147 case IPOPT_TS_TSONLY: 25148 off = opt[IPOPT_OFFSET] - 1; 25149 /* Compute # of milliseconds since midnight */ 25150 gethrestime(&now); 25151 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 25152 now.tv_nsec / (NANOSEC / MILLISEC); 25153 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 25154 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 25155 break; 25156 } 25157 break; 25158 } 25159 } 25160 } 25161 25162 /* 25163 * Send out a multicast packet on interface ipif. 25164 * The sender does not have an conn. 25165 * Caller verifies that this isn't a PHYI_LOOPBACK. 25166 */ 25167 void 25168 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif, zoneid_t zoneid) 25169 { 25170 ipha_t *ipha; 25171 ire_t *ire; 25172 ipaddr_t dst; 25173 mblk_t *first_mp; 25174 25175 /* igmp_sendpkt always allocates a ipsec_out_t */ 25176 ASSERT(mp->b_datap->db_type == M_CTL); 25177 ASSERT(!ipif->ipif_isv6); 25178 ASSERT(!(ipif->ipif_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)); 25179 25180 first_mp = mp; 25181 mp = first_mp->b_cont; 25182 ASSERT(mp->b_datap->db_type == M_DATA); 25183 ipha = (ipha_t *)mp->b_rptr; 25184 25185 /* 25186 * Find an IRE which matches the destination and the outgoing 25187 * queue (i.e. the outgoing interface.) 25188 */ 25189 if (ipif->ipif_flags & IPIF_POINTOPOINT) 25190 dst = ipif->ipif_pp_dst_addr; 25191 else 25192 dst = ipha->ipha_dst; 25193 /* 25194 * The source address has already been initialized by the 25195 * caller and hence matching on ILL (MATCH_IRE_ILL) would 25196 * be sufficient rather than MATCH_IRE_IPIF. 25197 * 25198 * This function is used for sending IGMP packets. We need 25199 * to make sure that we send the packet out of the interface 25200 * (ipif->ipif_ill) where we joined the group. This is to 25201 * prevent from switches doing IGMP snooping to send us multicast 25202 * packets for a given group on the interface we have joined. 25203 * If we can't find an ire, igmp_sendpkt has already initialized 25204 * ipsec_out_attach_if so that this will not be load spread in 25205 * ip_newroute_ipif. 25206 */ 25207 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, NULL, 25208 MATCH_IRE_ILL); 25209 if (!ire) { 25210 /* 25211 * Mark this packet to make it be delivered to 25212 * ip_wput_ire after the new ire has been 25213 * created. 25214 */ 25215 mp->b_prev = NULL; 25216 mp->b_next = NULL; 25217 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC, 25218 zoneid, &zero_info); 25219 return; 25220 } 25221 25222 /* 25223 * Honor the RTF_SETSRC flag; this is the only case 25224 * where we force this addr whatever the current src addr is, 25225 * because this address is set by igmp_sendpkt(), and 25226 * cannot be specified by any user. 25227 */ 25228 if (ire->ire_flags & RTF_SETSRC) { 25229 ipha->ipha_src = ire->ire_src_addr; 25230 } 25231 25232 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE, zoneid); 25233 } 25234 25235 /* 25236 * NOTE : This function does not ire_refrele the ire argument passed in. 25237 * 25238 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 25239 * failure. The nce_fp_mp can vanish any time in the case of IRE_MIPRTUN 25240 * and IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 25241 * the ire_lock to access the nce_fp_mp in this case. 25242 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 25243 * prepending a fastpath message IPQoS processing must precede it, we also set 25244 * the b_band of the fastpath message to that of the mblk returned by IPQoS 25245 * (IPQoS might have set the b_band for CoS marking). 25246 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 25247 * must follow it so that IPQoS can mark the dl_priority field for CoS 25248 * marking, if needed. 25249 */ 25250 static mblk_t * 25251 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, uint32_t ill_index) 25252 { 25253 uint_t hlen; 25254 ipha_t *ipha; 25255 mblk_t *mp1; 25256 boolean_t qos_done = B_FALSE; 25257 uchar_t *ll_hdr; 25258 25259 #define rptr ((uchar_t *)ipha) 25260 25261 ipha = (ipha_t *)mp->b_rptr; 25262 hlen = 0; 25263 LOCK_IRE_FP_MP(ire); 25264 if ((mp1 = ire->ire_nce->nce_fp_mp) != NULL) { 25265 ASSERT(DB_TYPE(mp1) == M_DATA); 25266 /* Initiate IPPF processing */ 25267 if ((proc != 0) && IPP_ENABLED(proc)) { 25268 UNLOCK_IRE_FP_MP(ire); 25269 ip_process(proc, &mp, ill_index); 25270 if (mp == NULL) 25271 return (NULL); 25272 25273 ipha = (ipha_t *)mp->b_rptr; 25274 LOCK_IRE_FP_MP(ire); 25275 if ((mp1 = ire->ire_nce->nce_fp_mp) == NULL) { 25276 qos_done = B_TRUE; 25277 goto no_fp_mp; 25278 } 25279 ASSERT(DB_TYPE(mp1) == M_DATA); 25280 } 25281 hlen = MBLKL(mp1); 25282 /* 25283 * Check if we have enough room to prepend fastpath 25284 * header 25285 */ 25286 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 25287 ll_hdr = rptr - hlen; 25288 bcopy(mp1->b_rptr, ll_hdr, hlen); 25289 /* 25290 * Set the b_rptr to the start of the link layer 25291 * header 25292 */ 25293 mp->b_rptr = ll_hdr; 25294 mp1 = mp; 25295 } else { 25296 mp1 = copyb(mp1); 25297 if (mp1 == NULL) 25298 goto unlock_err; 25299 mp1->b_band = mp->b_band; 25300 mp1->b_cont = mp; 25301 /* 25302 * certain system generated traffic may not 25303 * have cred/label in ip header block. This 25304 * is true even for a labeled system. But for 25305 * labeled traffic, inherit the label in the 25306 * new header. 25307 */ 25308 if (DB_CRED(mp) != NULL) 25309 mblk_setcred(mp1, DB_CRED(mp)); 25310 /* 25311 * XXX disable ICK_VALID and compute checksum 25312 * here; can happen if nce_fp_mp changes and 25313 * it can't be copied now due to insufficient 25314 * space. (unlikely, fp mp can change, but it 25315 * does not increase in length) 25316 */ 25317 } 25318 UNLOCK_IRE_FP_MP(ire); 25319 } else { 25320 no_fp_mp: 25321 mp1 = copyb(ire->ire_nce->nce_res_mp); 25322 if (mp1 == NULL) { 25323 unlock_err: 25324 UNLOCK_IRE_FP_MP(ire); 25325 freemsg(mp); 25326 return (NULL); 25327 } 25328 UNLOCK_IRE_FP_MP(ire); 25329 mp1->b_cont = mp; 25330 /* 25331 * certain system generated traffic may not 25332 * have cred/label in ip header block. This 25333 * is true even for a labeled system. But for 25334 * labeled traffic, inherit the label in the 25335 * new header. 25336 */ 25337 if (DB_CRED(mp) != NULL) 25338 mblk_setcred(mp1, DB_CRED(mp)); 25339 if (!qos_done && (proc != 0) && IPP_ENABLED(proc)) { 25340 ip_process(proc, &mp1, ill_index); 25341 if (mp1 == NULL) 25342 return (NULL); 25343 } 25344 } 25345 return (mp1); 25346 #undef rptr 25347 } 25348 25349 /* 25350 * Finish the outbound IPsec processing for an IPv6 packet. This function 25351 * is called from ipsec_out_process() if the IPsec packet was processed 25352 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25353 * asynchronously. 25354 */ 25355 void 25356 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 25357 ire_t *ire_arg) 25358 { 25359 in6_addr_t *v6dstp; 25360 ire_t *ire; 25361 mblk_t *mp; 25362 ip6_t *ip6h1; 25363 uint_t ill_index; 25364 ipsec_out_t *io; 25365 boolean_t attach_if, hwaccel; 25366 uint32_t flags = IP6_NO_IPPOLICY; 25367 int match_flags; 25368 zoneid_t zoneid; 25369 boolean_t ill_need_rele = B_FALSE; 25370 boolean_t ire_need_rele = B_FALSE; 25371 25372 mp = ipsec_mp->b_cont; 25373 ip6h1 = (ip6_t *)mp->b_rptr; 25374 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25375 ill_index = io->ipsec_out_ill_index; 25376 if (io->ipsec_out_reachable) { 25377 flags |= IPV6_REACHABILITY_CONFIRMATION; 25378 } 25379 attach_if = io->ipsec_out_attach_if; 25380 hwaccel = io->ipsec_out_accelerated; 25381 zoneid = io->ipsec_out_zoneid; 25382 ASSERT(zoneid != ALL_ZONES); 25383 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25384 /* Multicast addresses should have non-zero ill_index. */ 25385 v6dstp = &ip6h->ip6_dst; 25386 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 25387 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 25388 ASSERT(!attach_if || ill_index != 0); 25389 if (ill_index != 0) { 25390 if (ill == NULL) { 25391 ill = ip_grab_attach_ill(NULL, ipsec_mp, ill_index, 25392 B_TRUE); 25393 25394 /* Failure case frees things for us. */ 25395 if (ill == NULL) 25396 return; 25397 25398 ill_need_rele = B_TRUE; 25399 } 25400 /* 25401 * If this packet needs to go out on a particular interface 25402 * honor it. 25403 */ 25404 if (attach_if) { 25405 match_flags = MATCH_IRE_ILL; 25406 25407 /* 25408 * Check if we need an ire that will not be 25409 * looked up by anybody else i.e. HIDDEN. 25410 */ 25411 if (ill_is_probeonly(ill)) { 25412 match_flags |= MATCH_IRE_MARK_HIDDEN; 25413 } 25414 } 25415 } 25416 ASSERT(mp != NULL); 25417 25418 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 25419 boolean_t unspec_src; 25420 ipif_t *ipif; 25421 25422 /* 25423 * Use the ill_index to get the right ill. 25424 */ 25425 unspec_src = io->ipsec_out_unspec_src; 25426 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25427 if (ipif == NULL) { 25428 if (ill_need_rele) 25429 ill_refrele(ill); 25430 freemsg(ipsec_mp); 25431 return; 25432 } 25433 25434 if (ire_arg != NULL) { 25435 ire = ire_arg; 25436 } else { 25437 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25438 zoneid, MBLK_GETLABEL(mp), match_flags); 25439 ire_need_rele = B_TRUE; 25440 } 25441 if (ire != NULL) { 25442 ipif_refrele(ipif); 25443 /* 25444 * XXX Do the multicast forwarding now, as the IPSEC 25445 * processing has been done. 25446 */ 25447 goto send; 25448 } 25449 25450 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 25451 mp->b_prev = NULL; 25452 mp->b_next = NULL; 25453 25454 /* 25455 * If the IPsec packet was processed asynchronously, 25456 * drop it now. 25457 */ 25458 if (q == NULL) { 25459 if (ill_need_rele) 25460 ill_refrele(ill); 25461 freemsg(ipsec_mp); 25462 return; 25463 } 25464 25465 ip_newroute_ipif_v6(q, ipsec_mp, ipif, *v6dstp, 25466 unspec_src, zoneid); 25467 ipif_refrele(ipif); 25468 } else { 25469 if (attach_if) { 25470 ipif_t *ipif; 25471 25472 ipif = ipif_get_next_ipif(NULL, ill); 25473 if (ipif == NULL) { 25474 if (ill_need_rele) 25475 ill_refrele(ill); 25476 freemsg(ipsec_mp); 25477 return; 25478 } 25479 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 25480 zoneid, MBLK_GETLABEL(mp), match_flags); 25481 ire_need_rele = B_TRUE; 25482 ipif_refrele(ipif); 25483 } else { 25484 if (ire_arg != NULL) { 25485 ire = ire_arg; 25486 } else { 25487 ire = ire_cache_lookup_v6(v6dstp, zoneid, NULL); 25488 ire_need_rele = B_TRUE; 25489 } 25490 } 25491 if (ire != NULL) 25492 goto send; 25493 /* 25494 * ire disappeared underneath. 25495 * 25496 * What we need to do here is the ip_newroute 25497 * logic to get the ire without doing the IPSEC 25498 * processing. Follow the same old path. But this 25499 * time, ip_wput or ire_add_then_send will call us 25500 * directly as all the IPSEC operations are done. 25501 */ 25502 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 25503 mp->b_prev = NULL; 25504 mp->b_next = NULL; 25505 25506 /* 25507 * If the IPsec packet was processed asynchronously, 25508 * drop it now. 25509 */ 25510 if (q == NULL) { 25511 if (ill_need_rele) 25512 ill_refrele(ill); 25513 freemsg(ipsec_mp); 25514 return; 25515 } 25516 25517 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 25518 zoneid); 25519 } 25520 if (ill != NULL && ill_need_rele) 25521 ill_refrele(ill); 25522 return; 25523 send: 25524 if (ill != NULL && ill_need_rele) 25525 ill_refrele(ill); 25526 25527 /* Local delivery */ 25528 if (ire->ire_stq == NULL) { 25529 ill_t *out_ill; 25530 ASSERT(q != NULL); 25531 25532 /* PFHooks: LOOPBACK_OUT */ 25533 out_ill = ire->ire_ipif->ipif_ill; 25534 25535 DTRACE_PROBE4(ip6__loopback__out__start, 25536 ill_t *, NULL, ill_t *, out_ill, 25537 ip6_t *, ip6h1, mblk_t *, ipsec_mp); 25538 25539 FW_HOOKS6(ip6_loopback_out_event, ipv6firewall_loopback_out, 25540 NULL, out_ill, ip6h1, ipsec_mp, mp); 25541 25542 DTRACE_PROBE1(ip6__loopback__out__end, mblk_t *, ipsec_mp); 25543 25544 if (ipsec_mp != NULL) 25545 ip_wput_local_v6(RD(q), out_ill, 25546 ip6h, ipsec_mp, ire, 0); 25547 if (ire_need_rele) 25548 ire_refrele(ire); 25549 return; 25550 } 25551 /* 25552 * Everything is done. Send it out on the wire. 25553 * We force the insertion of a fragment header using the 25554 * IPH_FRAG_HDR flag in two cases: 25555 * - after reception of an ICMPv6 "packet too big" message 25556 * with a MTU < 1280 (cf. RFC 2460 section 5) 25557 * - for multirouted IPv6 packets, so that the receiver can 25558 * discard duplicates according to their fragment identifier 25559 */ 25560 /* XXX fix flow control problems. */ 25561 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 25562 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 25563 if (hwaccel) { 25564 /* 25565 * hardware acceleration does not handle these 25566 * "slow path" cases. 25567 */ 25568 /* IPsec KSTATS: should bump bean counter here. */ 25569 if (ire_need_rele) 25570 ire_refrele(ire); 25571 freemsg(ipsec_mp); 25572 return; 25573 } 25574 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 25575 (mp->b_cont ? msgdsize(mp) : 25576 mp->b_wptr - (uchar_t *)ip6h)) { 25577 /* IPsec KSTATS: should bump bean counter here. */ 25578 ip0dbg(("Packet length mismatch: %d, %ld\n", 25579 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 25580 msgdsize(mp))); 25581 if (ire_need_rele) 25582 ire_refrele(ire); 25583 freemsg(ipsec_mp); 25584 return; 25585 } 25586 ASSERT(mp->b_prev == NULL); 25587 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 25588 ntohs(ip6h->ip6_plen) + 25589 IPV6_HDR_LEN, ire->ire_max_frag)); 25590 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 25591 ire->ire_max_frag); 25592 } else { 25593 UPDATE_OB_PKT_COUNT(ire); 25594 ire->ire_last_used_time = lbolt; 25595 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 25596 } 25597 if (ire_need_rele) 25598 ire_refrele(ire); 25599 freeb(ipsec_mp); 25600 } 25601 25602 void 25603 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 25604 { 25605 mblk_t *hada_mp; /* attributes M_CTL mblk */ 25606 da_ipsec_t *hada; /* data attributes */ 25607 ill_t *ill = (ill_t *)q->q_ptr; 25608 25609 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 25610 25611 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 25612 /* IPsec KSTATS: Bump lose counter here! */ 25613 freemsg(mp); 25614 return; 25615 } 25616 25617 /* 25618 * It's an IPsec packet that must be 25619 * accelerated by the Provider, and the 25620 * outbound ill is IPsec acceleration capable. 25621 * Prepends the mblk with an IPHADA_M_CTL, and ship it 25622 * to the ill. 25623 * IPsec KSTATS: should bump packet counter here. 25624 */ 25625 25626 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 25627 if (hada_mp == NULL) { 25628 /* IPsec KSTATS: should bump packet counter here. */ 25629 freemsg(mp); 25630 return; 25631 } 25632 25633 hada_mp->b_datap->db_type = M_CTL; 25634 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 25635 hada_mp->b_cont = mp; 25636 25637 hada = (da_ipsec_t *)hada_mp->b_rptr; 25638 bzero(hada, sizeof (da_ipsec_t)); 25639 hada->da_type = IPHADA_M_CTL; 25640 25641 putnext(q, hada_mp); 25642 } 25643 25644 /* 25645 * Finish the outbound IPsec processing. This function is called from 25646 * ipsec_out_process() if the IPsec packet was processed 25647 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 25648 * asynchronously. 25649 */ 25650 void 25651 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 25652 ire_t *ire_arg) 25653 { 25654 uint32_t v_hlen_tos_len; 25655 ipaddr_t dst; 25656 ipif_t *ipif = NULL; 25657 ire_t *ire; 25658 ire_t *ire1 = NULL; 25659 mblk_t *next_mp = NULL; 25660 uint32_t max_frag; 25661 boolean_t multirt_send = B_FALSE; 25662 mblk_t *mp; 25663 mblk_t *mp1; 25664 ipha_t *ipha1; 25665 uint_t ill_index; 25666 ipsec_out_t *io; 25667 boolean_t attach_if; 25668 int match_flags, offset; 25669 irb_t *irb = NULL; 25670 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 25671 zoneid_t zoneid; 25672 uint32_t cksum; 25673 uint16_t *up; 25674 ipxmit_state_t pktxmit_state; 25675 #ifdef _BIG_ENDIAN 25676 #define LENGTH (v_hlen_tos_len & 0xFFFF) 25677 #else 25678 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 25679 #endif 25680 25681 mp = ipsec_mp->b_cont; 25682 ipha1 = (ipha_t *)mp->b_rptr; 25683 ASSERT(mp != NULL); 25684 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 25685 dst = ipha->ipha_dst; 25686 25687 io = (ipsec_out_t *)ipsec_mp->b_rptr; 25688 ill_index = io->ipsec_out_ill_index; 25689 attach_if = io->ipsec_out_attach_if; 25690 zoneid = io->ipsec_out_zoneid; 25691 ASSERT(zoneid != ALL_ZONES); 25692 match_flags = MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR; 25693 if (ill_index != 0) { 25694 if (ill == NULL) { 25695 ill = ip_grab_attach_ill(NULL, ipsec_mp, 25696 ill_index, B_FALSE); 25697 25698 /* Failure case frees things for us. */ 25699 if (ill == NULL) 25700 return; 25701 25702 ill_need_rele = B_TRUE; 25703 } 25704 /* 25705 * If this packet needs to go out on a particular interface 25706 * honor it. 25707 */ 25708 if (attach_if) { 25709 match_flags = MATCH_IRE_ILL | MATCH_IRE_SECATTR; 25710 25711 /* 25712 * Check if we need an ire that will not be 25713 * looked up by anybody else i.e. HIDDEN. 25714 */ 25715 if (ill_is_probeonly(ill)) { 25716 match_flags |= MATCH_IRE_MARK_HIDDEN; 25717 } 25718 } 25719 } 25720 25721 if (CLASSD(dst)) { 25722 boolean_t conn_dontroute; 25723 /* 25724 * Use the ill_index to get the right ipif. 25725 */ 25726 conn_dontroute = io->ipsec_out_dontroute; 25727 if (ill_index == 0) 25728 ipif = ipif_lookup_group(dst, zoneid); 25729 else 25730 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 25731 if (ipif == NULL) { 25732 ip1dbg(("ip_wput_ipsec_out: No ipif for" 25733 " multicast\n")); 25734 BUMP_MIB(&ip_mib, ipIfStatsOutNoRoutes); 25735 freemsg(ipsec_mp); 25736 goto done; 25737 } 25738 /* 25739 * ipha_src has already been intialized with the 25740 * value of the ipif in ip_wput. All we need now is 25741 * an ire to send this downstream. 25742 */ 25743 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, 25744 MBLK_GETLABEL(mp), match_flags); 25745 if (ire != NULL) { 25746 ill_t *ill1; 25747 /* 25748 * Do the multicast forwarding now, as the IPSEC 25749 * processing has been done. 25750 */ 25751 if (ip_g_mrouter && !conn_dontroute && 25752 (ill1 = ire_to_ill(ire))) { 25753 if (ip_mforward(ill1, ipha, mp)) { 25754 freemsg(ipsec_mp); 25755 ip1dbg(("ip_wput_ipsec_out: mforward " 25756 "failed\n")); 25757 ire_refrele(ire); 25758 goto done; 25759 } 25760 } 25761 goto send; 25762 } 25763 25764 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 25765 mp->b_prev = NULL; 25766 mp->b_next = NULL; 25767 25768 /* 25769 * If the IPsec packet was processed asynchronously, 25770 * drop it now. 25771 */ 25772 if (q == NULL) { 25773 freemsg(ipsec_mp); 25774 goto done; 25775 } 25776 25777 /* 25778 * We may be using a wrong ipif to create the ire. 25779 * But it is okay as the source address is assigned 25780 * for the packet already. Next outbound packet would 25781 * create the IRE with the right IPIF in ip_wput. 25782 * 25783 * Also handle RTF_MULTIRT routes. 25784 */ 25785 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT, 25786 zoneid, &zero_info); 25787 } else { 25788 if (attach_if) { 25789 ire = ire_ctable_lookup(dst, 0, 0, ill->ill_ipif, 25790 zoneid, MBLK_GETLABEL(mp), match_flags); 25791 } else { 25792 if (ire_arg != NULL) { 25793 ire = ire_arg; 25794 ire_need_rele = B_FALSE; 25795 } else { 25796 ire = ire_cache_lookup(dst, zoneid, 25797 MBLK_GETLABEL(mp)); 25798 } 25799 } 25800 if (ire != NULL) { 25801 goto send; 25802 } 25803 25804 /* 25805 * ire disappeared underneath. 25806 * 25807 * What we need to do here is the ip_newroute 25808 * logic to get the ire without doing the IPSEC 25809 * processing. Follow the same old path. But this 25810 * time, ip_wput or ire_add_then_put will call us 25811 * directly as all the IPSEC operations are done. 25812 */ 25813 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 25814 mp->b_prev = NULL; 25815 mp->b_next = NULL; 25816 25817 /* 25818 * If the IPsec packet was processed asynchronously, 25819 * drop it now. 25820 */ 25821 if (q == NULL) { 25822 freemsg(ipsec_mp); 25823 goto done; 25824 } 25825 25826 /* 25827 * Since we're going through ip_newroute() again, we 25828 * need to make sure we don't: 25829 * 25830 * 1.) Trigger the ASSERT() with the ipha_ident 25831 * overloading. 25832 * 2.) Redo transport-layer checksumming, since we've 25833 * already done all that to get this far. 25834 * 25835 * The easiest way not do either of the above is to set 25836 * the ipha_ident field to IP_HDR_INCLUDED. 25837 */ 25838 ipha->ipha_ident = IP_HDR_INCLUDED; 25839 ip_newroute(q, ipsec_mp, dst, NULL, 25840 (CONN_Q(q) ? Q_TO_CONN(q) : NULL), zoneid); 25841 } 25842 goto done; 25843 send: 25844 if (ipha->ipha_protocol == IPPROTO_UDP && udp_compute_checksum()) { 25845 /* 25846 * ESP NAT-Traversal packet. 25847 * 25848 * Just do software checksum for now. 25849 */ 25850 25851 offset = IP_SIMPLE_HDR_LENGTH + UDP_CHECKSUM_OFFSET; 25852 IP_STAT(ip_out_sw_cksum); 25853 IP_STAT_UPDATE(ip_udp_out_sw_cksum_bytes, 25854 ntohs(htons(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH)); 25855 #define iphs ((uint16_t *)ipha) 25856 cksum = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 25857 iphs[9] + ntohs(htons(ipha->ipha_length) - 25858 IP_SIMPLE_HDR_LENGTH); 25859 #undef iphs 25860 if ((cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH, cksum)) == 0) 25861 cksum = 0xFFFF; 25862 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) 25863 if (mp1->b_wptr - mp1->b_rptr >= 25864 offset + sizeof (uint16_t)) { 25865 up = (uint16_t *)(mp1->b_rptr + offset); 25866 *up = cksum; 25867 break; /* out of for loop */ 25868 } else { 25869 offset -= (mp->b_wptr - mp->b_rptr); 25870 } 25871 } /* Otherwise, just keep the all-zero checksum. */ 25872 25873 if (ire->ire_stq == NULL) { 25874 ill_t *out_ill; 25875 /* 25876 * Loopbacks go through ip_wput_local except for one case. 25877 * We come here if we generate a icmp_frag_needed message 25878 * after IPSEC processing is over. When this function calls 25879 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 25880 * icmp_frag_needed. The message generated comes back here 25881 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 25882 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 25883 * source address as it is usually set in ip_wput_ire. As 25884 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 25885 * and we end up here. We can't enter ip_wput_ire once the 25886 * IPSEC processing is over and hence we need to do it here. 25887 */ 25888 ASSERT(q != NULL); 25889 UPDATE_OB_PKT_COUNT(ire); 25890 ire->ire_last_used_time = lbolt; 25891 if (ipha->ipha_src == 0) 25892 ipha->ipha_src = ire->ire_src_addr; 25893 25894 /* PFHooks: LOOPBACK_OUT */ 25895 out_ill = ire->ire_ipif->ipif_ill; 25896 25897 DTRACE_PROBE4(ip4__loopback__out__start, 25898 ill_t *, NULL, ill_t *, out_ill, 25899 ipha_t *, ipha1, mblk_t *, ipsec_mp); 25900 25901 FW_HOOKS(ip4_loopback_out_event, ipv4firewall_loopback_out, 25902 NULL, out_ill, ipha1, ipsec_mp, mp); 25903 25904 DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp); 25905 25906 if (ipsec_mp != NULL) 25907 ip_wput_local(RD(q), out_ill, 25908 ipha, ipsec_mp, ire, 0, zoneid); 25909 if (ire_need_rele) 25910 ire_refrele(ire); 25911 goto done; 25912 } 25913 25914 if (ire->ire_max_frag < (unsigned int)LENGTH) { 25915 /* 25916 * We are through with IPSEC processing. 25917 * Fragment this and send it on the wire. 25918 */ 25919 if (io->ipsec_out_accelerated) { 25920 /* 25921 * The packet has been accelerated but must 25922 * be fragmented. This should not happen 25923 * since AH and ESP must not accelerate 25924 * packets that need fragmentation, however 25925 * the configuration could have changed 25926 * since the AH or ESP processing. 25927 * Drop packet. 25928 * IPsec KSTATS: bump bean counter here. 25929 */ 25930 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 25931 "fragmented accelerated packet!\n")); 25932 freemsg(ipsec_mp); 25933 } else { 25934 ip_wput_ire_fragmentit(ipsec_mp, ire, zoneid); 25935 } 25936 if (ire_need_rele) 25937 ire_refrele(ire); 25938 goto done; 25939 } 25940 25941 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 25942 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 25943 (void *)ire->ire_ipif, (void *)ipif)); 25944 25945 /* 25946 * Multiroute the secured packet, unless IPsec really 25947 * requires the packet to go out only through a particular 25948 * interface. 25949 */ 25950 if ((ire->ire_flags & RTF_MULTIRT) && !attach_if) { 25951 ire_t *first_ire; 25952 irb = ire->ire_bucket; 25953 ASSERT(irb != NULL); 25954 /* 25955 * This ire has been looked up as the one that 25956 * goes through the given ipif; 25957 * make sure we do not omit any other multiroute ire 25958 * that may be present in the bucket before this one. 25959 */ 25960 IRB_REFHOLD(irb); 25961 for (first_ire = irb->irb_ire; 25962 first_ire != NULL; 25963 first_ire = first_ire->ire_next) { 25964 if ((first_ire->ire_flags & RTF_MULTIRT) && 25965 (first_ire->ire_addr == ire->ire_addr) && 25966 !(first_ire->ire_marks & 25967 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 25968 break; 25969 } 25970 25971 if ((first_ire != NULL) && (first_ire != ire)) { 25972 /* 25973 * Don't change the ire if the packet must 25974 * be fragmented if sent via this new one. 25975 */ 25976 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 25977 IRE_REFHOLD(first_ire); 25978 if (ire_need_rele) 25979 ire_refrele(ire); 25980 else 25981 ire_need_rele = B_TRUE; 25982 ire = first_ire; 25983 } 25984 } 25985 IRB_REFRELE(irb); 25986 25987 multirt_send = B_TRUE; 25988 max_frag = ire->ire_max_frag; 25989 } else { 25990 if ((ire->ire_flags & RTF_MULTIRT) && attach_if) { 25991 ip1dbg(("ip_wput_ipsec_out: ignoring multirouting " 25992 "flag, attach_if %d\n", attach_if)); 25993 } 25994 } 25995 25996 /* 25997 * In most cases, the emission loop below is entered only once. 25998 * Only in the case where the ire holds the RTF_MULTIRT 25999 * flag, we loop to process all RTF_MULTIRT ires in the 26000 * bucket, and send the packet through all crossed 26001 * RTF_MULTIRT routes. 26002 */ 26003 do { 26004 if (multirt_send) { 26005 /* 26006 * ire1 holds here the next ire to process in the 26007 * bucket. If multirouting is expected, 26008 * any non-RTF_MULTIRT ire that has the 26009 * right destination address is ignored. 26010 */ 26011 ASSERT(irb != NULL); 26012 IRB_REFHOLD(irb); 26013 for (ire1 = ire->ire_next; 26014 ire1 != NULL; 26015 ire1 = ire1->ire_next) { 26016 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 26017 continue; 26018 if (ire1->ire_addr != ire->ire_addr) 26019 continue; 26020 if (ire1->ire_marks & 26021 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 26022 continue; 26023 /* No loopback here */ 26024 if (ire1->ire_stq == NULL) 26025 continue; 26026 /* 26027 * Ensure we do not exceed the MTU 26028 * of the next route. 26029 */ 26030 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 26031 ip_multirt_bad_mtu(ire1, max_frag); 26032 continue; 26033 } 26034 26035 IRE_REFHOLD(ire1); 26036 break; 26037 } 26038 IRB_REFRELE(irb); 26039 if (ire1 != NULL) { 26040 /* 26041 * We are in a multiple send case, need to 26042 * make a copy of the packet. 26043 */ 26044 next_mp = copymsg(ipsec_mp); 26045 if (next_mp == NULL) { 26046 ire_refrele(ire1); 26047 ire1 = NULL; 26048 } 26049 } 26050 } 26051 /* 26052 * Everything is done. Send it out on the wire 26053 * 26054 * ip_xmit_v4 will call ip_wput_attach_llhdr and then 26055 * either send it on the wire or, in the case of 26056 * HW acceleration, call ipsec_hw_putnext. 26057 */ 26058 if (ire->ire_nce && 26059 ire->ire_nce->nce_state != ND_REACHABLE) { 26060 DTRACE_PROBE2(ip__wput__ipsec__bail, 26061 (ire_t *), ire, (mblk_t *), ipsec_mp); 26062 /* 26063 * If ire's link-layer is unresolved (this 26064 * would only happen if the incomplete ire 26065 * was added to cachetable via forwarding path) 26066 * don't bother going to ip_xmit_v4. Just drop the 26067 * packet. 26068 * There is a slight risk here, in that, if we 26069 * have the forwarding path create an incomplete 26070 * IRE, then until the IRE is completed, any 26071 * transmitted IPSEC packets will be dropped 26072 * instead of being queued waiting for resolution. 26073 * 26074 * But the likelihood of a forwarding packet and a wput 26075 * packet sending to the same dst at the same time 26076 * and there not yet be an ARP entry for it is small. 26077 * Furthermore, if this actually happens, it might 26078 * be likely that wput would generate multiple 26079 * packets (and forwarding would also have a train 26080 * of packets) for that destination. If this is 26081 * the case, some of them would have been dropped 26082 * anyway, since ARP only queues a few packets while 26083 * waiting for resolution 26084 * 26085 * NOTE: We should really call ip_xmit_v4, 26086 * and let it queue the packet and send the 26087 * ARP query and have ARP come back thus: 26088 * <ARP> ip_wput->ip_output->ip-wput_nondata-> 26089 * ip_xmit_v4->ip_wput_attach_llhdr + ipsec 26090 * hw accel work. But it's too complex to get 26091 * the IPsec hw acceleration approach to fit 26092 * well with ip_xmit_v4 doing ARP without 26093 * doing IPSEC simplification. For now, we just 26094 * poke ip_xmit_v4 to trigger the arp resolve, so 26095 * that we can continue with the send on the next 26096 * attempt. 26097 * 26098 * XXX THis should be revisited, when 26099 * the IPsec/IP interaction is cleaned up 26100 */ 26101 ip1dbg(("ip_wput_ipsec_out: ire is incomplete" 26102 " - dropping packet\n")); 26103 freemsg(ipsec_mp); 26104 /* 26105 * Call ip_xmit_v4() to trigger ARP query 26106 * in case the nce_state is ND_INITIAL 26107 */ 26108 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 26109 goto drop_pkt; 26110 } 26111 26112 DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, 26113 ill_t *, ire->ire_ipif->ipif_ill, ipha_t *, ipha1, 26114 mblk_t *, mp); 26115 FW_HOOKS(ip4_physical_out_event, ipv4firewall_physical_out, 26116 NULL, ire->ire_ipif->ipif_ill, ipha1, mp, mp); 26117 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 26118 if (mp == NULL) 26119 goto drop_pkt; 26120 26121 ip1dbg(("ip_wput_ipsec_out: calling ip_xmit_v4\n")); 26122 pktxmit_state = ip_xmit_v4(mp, ire, 26123 (io->ipsec_out_accelerated ? io : NULL), B_FALSE); 26124 26125 if ((pktxmit_state == SEND_FAILED) || 26126 (pktxmit_state == LLHDR_RESLV_FAILED)) { 26127 26128 freeb(ipsec_mp); /* ip_xmit_v4 frees the mp */ 26129 drop_pkt: 26130 BUMP_MIB(((ill_t *)ire->ire_stq->q_ptr)->ill_ip_mib, 26131 ipIfStatsOutDiscards); 26132 if (ire_need_rele) 26133 ire_refrele(ire); 26134 if (ire1 != NULL) { 26135 ire_refrele(ire1); 26136 freemsg(next_mp); 26137 } 26138 goto done; 26139 } 26140 26141 freeb(ipsec_mp); 26142 if (ire_need_rele) 26143 ire_refrele(ire); 26144 26145 if (ire1 != NULL) { 26146 ire = ire1; 26147 ire_need_rele = B_TRUE; 26148 ASSERT(next_mp); 26149 ipsec_mp = next_mp; 26150 mp = ipsec_mp->b_cont; 26151 ire1 = NULL; 26152 next_mp = NULL; 26153 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26154 } else { 26155 multirt_send = B_FALSE; 26156 } 26157 } while (multirt_send); 26158 done: 26159 if (ill != NULL && ill_need_rele) 26160 ill_refrele(ill); 26161 if (ipif != NULL) 26162 ipif_refrele(ipif); 26163 } 26164 26165 /* 26166 * Get the ill corresponding to the specified ire, and compare its 26167 * capabilities with the protocol and algorithms specified by the 26168 * the SA obtained from ipsec_out. If they match, annotate the 26169 * ipsec_out structure to indicate that the packet needs acceleration. 26170 * 26171 * 26172 * A packet is eligible for outbound hardware acceleration if the 26173 * following conditions are satisfied: 26174 * 26175 * 1. the packet will not be fragmented 26176 * 2. the provider supports the algorithm 26177 * 3. there is no pending control message being exchanged 26178 * 4. snoop is not attached 26179 * 5. the destination address is not a broadcast or multicast address. 26180 * 26181 * Rationale: 26182 * - Hardware drivers do not support fragmentation with 26183 * the current interface. 26184 * - snoop, multicast, and broadcast may result in exposure of 26185 * a cleartext datagram. 26186 * We check all five of these conditions here. 26187 * 26188 * XXX would like to nuke "ire_t *" parameter here; problem is that 26189 * IRE is only way to figure out if a v4 address is a broadcast and 26190 * thus ineligible for acceleration... 26191 */ 26192 static void 26193 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 26194 { 26195 ipsec_out_t *io; 26196 mblk_t *data_mp; 26197 uint_t plen, overhead; 26198 26199 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 26200 return; 26201 26202 if (ill == NULL) 26203 return; 26204 26205 /* 26206 * Destination address is a broadcast or multicast. Punt. 26207 */ 26208 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 26209 IRE_LOCAL))) 26210 return; 26211 26212 data_mp = ipsec_mp->b_cont; 26213 26214 if (ill->ill_isv6) { 26215 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 26216 26217 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 26218 return; 26219 26220 plen = ip6h->ip6_plen; 26221 } else { 26222 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 26223 26224 if (CLASSD(ipha->ipha_dst)) 26225 return; 26226 26227 plen = ipha->ipha_length; 26228 } 26229 /* 26230 * Is there a pending DLPI control message being exchanged 26231 * between IP/IPsec and the DLS Provider? If there is, it 26232 * could be a SADB update, and the state of the DLS Provider 26233 * SADB might not be in sync with the SADB maintained by 26234 * IPsec. To avoid dropping packets or using the wrong keying 26235 * material, we do not accelerate this packet. 26236 */ 26237 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 26238 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26239 "ill_dlpi_pending! don't accelerate packet\n")); 26240 return; 26241 } 26242 26243 /* 26244 * Is the Provider in promiscous mode? If it does, we don't 26245 * accelerate the packet since it will bounce back up to the 26246 * listeners in the clear. 26247 */ 26248 if (ill->ill_promisc_on_phys) { 26249 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 26250 "ill in promiscous mode, don't accelerate packet\n")); 26251 return; 26252 } 26253 26254 /* 26255 * Will the packet require fragmentation? 26256 */ 26257 26258 /* 26259 * IPsec ESP note: this is a pessimistic estimate, but the same 26260 * as is used elsewhere. 26261 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 26262 * + 2-byte trailer 26263 */ 26264 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 26265 IPSEC_BASE_ESP_HDR_SIZE(sa); 26266 26267 if ((plen + overhead) > ill->ill_max_mtu) 26268 return; 26269 26270 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26271 26272 /* 26273 * Can the ill accelerate this IPsec protocol and algorithm 26274 * specified by the SA? 26275 */ 26276 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 26277 ill->ill_isv6, sa)) { 26278 return; 26279 } 26280 26281 /* 26282 * Tell AH or ESP that the outbound ill is capable of 26283 * accelerating this packet. 26284 */ 26285 io->ipsec_out_is_capab_ill = B_TRUE; 26286 } 26287 26288 /* 26289 * Select which AH & ESP SA's to use (if any) for the outbound packet. 26290 * 26291 * If this function returns B_TRUE, the requested SA's have been filled 26292 * into the ipsec_out_*_sa pointers. 26293 * 26294 * If the function returns B_FALSE, the packet has been "consumed", most 26295 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 26296 * 26297 * The SA references created by the protocol-specific "select" 26298 * function will be released when the ipsec_mp is freed, thanks to the 26299 * ipsec_out_free destructor -- see spd.c. 26300 */ 26301 static boolean_t 26302 ipsec_out_select_sa(mblk_t *ipsec_mp) 26303 { 26304 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 26305 ipsec_out_t *io; 26306 ipsec_policy_t *pp; 26307 ipsec_action_t *ap; 26308 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26309 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26310 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26311 26312 if (!io->ipsec_out_secure) { 26313 /* 26314 * We came here by mistake. 26315 * Don't bother with ipsec processing 26316 * We should "discourage" this path in the future. 26317 */ 26318 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26319 return (B_FALSE); 26320 } 26321 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26322 ASSERT((io->ipsec_out_policy != NULL) || 26323 (io->ipsec_out_act != NULL)); 26324 26325 ASSERT(io->ipsec_out_failed == B_FALSE); 26326 26327 /* 26328 * IPSEC processing has started. 26329 */ 26330 io->ipsec_out_proc_begin = B_TRUE; 26331 ap = io->ipsec_out_act; 26332 if (ap == NULL) { 26333 pp = io->ipsec_out_policy; 26334 ASSERT(pp != NULL); 26335 ap = pp->ipsp_act; 26336 ASSERT(ap != NULL); 26337 } 26338 26339 /* 26340 * We have an action. now, let's select SA's. 26341 * (In the future, we can cache this in the conn_t..) 26342 */ 26343 if (ap->ipa_want_esp) { 26344 if (io->ipsec_out_esp_sa == NULL) { 26345 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 26346 IPPROTO_ESP); 26347 } 26348 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 26349 } 26350 26351 if (ap->ipa_want_ah) { 26352 if (io->ipsec_out_ah_sa == NULL) { 26353 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 26354 IPPROTO_AH); 26355 } 26356 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 26357 /* 26358 * The ESP and AH processing order needs to be preserved 26359 * when both protocols are required (ESP should be applied 26360 * before AH for an outbound packet). Force an ESP ACQUIRE 26361 * when both ESP and AH are required, and an AH ACQUIRE 26362 * is needed. 26363 */ 26364 if (ap->ipa_want_esp && need_ah_acquire) 26365 need_esp_acquire = B_TRUE; 26366 } 26367 26368 /* 26369 * Send an ACQUIRE (extended, regular, or both) if we need one. 26370 * Release SAs that got referenced, but will not be used until we 26371 * acquire _all_ of the SAs we need. 26372 */ 26373 if (need_ah_acquire || need_esp_acquire) { 26374 if (io->ipsec_out_ah_sa != NULL) { 26375 IPSA_REFRELE(io->ipsec_out_ah_sa); 26376 io->ipsec_out_ah_sa = NULL; 26377 } 26378 if (io->ipsec_out_esp_sa != NULL) { 26379 IPSA_REFRELE(io->ipsec_out_esp_sa); 26380 io->ipsec_out_esp_sa = NULL; 26381 } 26382 26383 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 26384 return (B_FALSE); 26385 } 26386 26387 return (B_TRUE); 26388 } 26389 26390 /* 26391 * Process an IPSEC_OUT message and see what you can 26392 * do with it. 26393 * IPQoS Notes: 26394 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 26395 * IPSec. 26396 * XXX would like to nuke ire_t. 26397 * XXX ill_index better be "real" 26398 */ 26399 void 26400 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 26401 { 26402 ipsec_out_t *io; 26403 ipsec_policy_t *pp; 26404 ipsec_action_t *ap; 26405 ipha_t *ipha; 26406 ip6_t *ip6h; 26407 mblk_t *mp; 26408 ill_t *ill; 26409 zoneid_t zoneid; 26410 ipsec_status_t ipsec_rc; 26411 boolean_t ill_need_rele = B_FALSE; 26412 26413 io = (ipsec_out_t *)ipsec_mp->b_rptr; 26414 ASSERT(io->ipsec_out_type == IPSEC_OUT); 26415 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 26416 mp = ipsec_mp->b_cont; 26417 26418 /* 26419 * Initiate IPPF processing. We do it here to account for packets 26420 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 26421 * We can check for ipsec_out_proc_begin even for such packets, as 26422 * they will always be false (asserted below). 26423 */ 26424 if (IPP_ENABLED(IPP_LOCAL_OUT) && !io->ipsec_out_proc_begin) { 26425 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 26426 io->ipsec_out_ill_index : ill_index); 26427 if (mp == NULL) { 26428 ip2dbg(("ipsec_out_process: packet dropped "\ 26429 "during IPPF processing\n")); 26430 freeb(ipsec_mp); 26431 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 26432 return; 26433 } 26434 } 26435 26436 if (!io->ipsec_out_secure) { 26437 /* 26438 * We came here by mistake. 26439 * Don't bother with ipsec processing 26440 * Should "discourage" this path in the future. 26441 */ 26442 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 26443 goto done; 26444 } 26445 ASSERT(io->ipsec_out_need_policy == B_FALSE); 26446 ASSERT((io->ipsec_out_policy != NULL) || 26447 (io->ipsec_out_act != NULL)); 26448 ASSERT(io->ipsec_out_failed == B_FALSE); 26449 26450 if (!ipsec_loaded()) { 26451 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 26452 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26453 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 26454 } else { 26455 BUMP_MIB(&ip6_mib, ipIfStatsOutDiscards); 26456 } 26457 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 26458 &ipdrops_ip_ipsec_not_loaded, &ip_dropper); 26459 return; 26460 } 26461 26462 /* 26463 * IPSEC processing has started. 26464 */ 26465 io->ipsec_out_proc_begin = B_TRUE; 26466 ap = io->ipsec_out_act; 26467 if (ap == NULL) { 26468 pp = io->ipsec_out_policy; 26469 ASSERT(pp != NULL); 26470 ap = pp->ipsp_act; 26471 ASSERT(ap != NULL); 26472 } 26473 26474 /* 26475 * Save the outbound ill index. When the packet comes back 26476 * from IPsec, we make sure the ill hasn't changed or disappeared 26477 * before sending it the accelerated packet. 26478 */ 26479 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 26480 int ifindex; 26481 ill = ire_to_ill(ire); 26482 ifindex = ill->ill_phyint->phyint_ifindex; 26483 io->ipsec_out_capab_ill_index = ifindex; 26484 } 26485 26486 /* 26487 * The order of processing is first insert a IP header if needed. 26488 * Then insert the ESP header and then the AH header. 26489 */ 26490 if ((io->ipsec_out_se_done == B_FALSE) && 26491 (ap->ipa_want_se)) { 26492 /* 26493 * First get the outer IP header before sending 26494 * it to ESP. 26495 */ 26496 ipha_t *oipha, *iipha; 26497 mblk_t *outer_mp, *inner_mp; 26498 26499 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 26500 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 26501 "ipsec_out_process: " 26502 "Self-Encapsulation failed: Out of memory\n"); 26503 freemsg(ipsec_mp); 26504 if (ill != NULL) { 26505 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26506 } else { 26507 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 26508 } 26509 return; 26510 } 26511 inner_mp = ipsec_mp->b_cont; 26512 ASSERT(inner_mp->b_datap->db_type == M_DATA); 26513 oipha = (ipha_t *)outer_mp->b_rptr; 26514 iipha = (ipha_t *)inner_mp->b_rptr; 26515 *oipha = *iipha; 26516 outer_mp->b_wptr += sizeof (ipha_t); 26517 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 26518 sizeof (ipha_t)); 26519 oipha->ipha_protocol = IPPROTO_ENCAP; 26520 oipha->ipha_version_and_hdr_length = 26521 IP_SIMPLE_HDR_VERSION; 26522 oipha->ipha_hdr_checksum = 0; 26523 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 26524 outer_mp->b_cont = inner_mp; 26525 ipsec_mp->b_cont = outer_mp; 26526 26527 io->ipsec_out_se_done = B_TRUE; 26528 io->ipsec_out_tunnel = B_TRUE; 26529 } 26530 26531 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 26532 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 26533 !ipsec_out_select_sa(ipsec_mp)) 26534 return; 26535 26536 /* 26537 * By now, we know what SA's to use. Toss over to ESP & AH 26538 * to do the heavy lifting. 26539 */ 26540 zoneid = io->ipsec_out_zoneid; 26541 ASSERT(zoneid != ALL_ZONES); 26542 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 26543 ASSERT(io->ipsec_out_esp_sa != NULL); 26544 io->ipsec_out_esp_done = B_TRUE; 26545 /* 26546 * Note that since hw accel can only apply one transform, 26547 * not two, we skip hw accel for ESP if we also have AH 26548 * This is an design limitation of the interface 26549 * which should be revisited. 26550 */ 26551 ASSERT(ire != NULL); 26552 if (io->ipsec_out_ah_sa == NULL) { 26553 ill = (ill_t *)ire->ire_stq->q_ptr; 26554 ipsec_out_is_accelerated(ipsec_mp, 26555 io->ipsec_out_esp_sa, ill, ire); 26556 } 26557 26558 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 26559 switch (ipsec_rc) { 26560 case IPSEC_STATUS_SUCCESS: 26561 break; 26562 case IPSEC_STATUS_FAILED: 26563 if (ill != NULL) { 26564 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26565 } else { 26566 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 26567 } 26568 /* FALLTHRU */ 26569 case IPSEC_STATUS_PENDING: 26570 return; 26571 } 26572 } 26573 26574 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 26575 ASSERT(io->ipsec_out_ah_sa != NULL); 26576 io->ipsec_out_ah_done = B_TRUE; 26577 if (ire == NULL) { 26578 int idx = io->ipsec_out_capab_ill_index; 26579 ill = ill_lookup_on_ifindex(idx, B_FALSE, 26580 NULL, NULL, NULL, NULL); 26581 ill_need_rele = B_TRUE; 26582 } else { 26583 ill = (ill_t *)ire->ire_stq->q_ptr; 26584 } 26585 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 26586 ire); 26587 26588 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 26589 switch (ipsec_rc) { 26590 case IPSEC_STATUS_SUCCESS: 26591 break; 26592 case IPSEC_STATUS_FAILED: 26593 if (ill != NULL) { 26594 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 26595 } else { 26596 BUMP_MIB(&ip_mib, ipIfStatsOutDiscards); 26597 } 26598 /* FALLTHRU */ 26599 case IPSEC_STATUS_PENDING: 26600 if (ill != NULL && ill_need_rele) 26601 ill_refrele(ill); 26602 return; 26603 } 26604 } 26605 /* 26606 * We are done with IPSEC processing. Send it over 26607 * the wire. 26608 */ 26609 done: 26610 mp = ipsec_mp->b_cont; 26611 ipha = (ipha_t *)mp->b_rptr; 26612 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 26613 ip_wput_ipsec_out(q, ipsec_mp, ipha, ill, ire); 26614 } else { 26615 ip6h = (ip6_t *)ipha; 26616 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ill, ire); 26617 } 26618 if (ill != NULL && ill_need_rele) 26619 ill_refrele(ill); 26620 } 26621 26622 /* ARGSUSED */ 26623 void 26624 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 26625 { 26626 opt_restart_t *or; 26627 int err; 26628 conn_t *connp; 26629 26630 ASSERT(CONN_Q(q)); 26631 connp = Q_TO_CONN(q); 26632 26633 ASSERT(first_mp->b_datap->db_type == M_CTL); 26634 or = (opt_restart_t *)first_mp->b_rptr; 26635 /* 26636 * We don't need to pass any credentials here since this is just 26637 * a restart. The credentials are passed in when svr4_optcom_req 26638 * is called the first time (from ip_wput_nondata). 26639 */ 26640 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 26641 err = svr4_optcom_req(q, first_mp, NULL, 26642 &ip_opt_obj); 26643 } else { 26644 ASSERT(or->or_type == T_OPTMGMT_REQ); 26645 err = tpi_optcom_req(q, first_mp, NULL, 26646 &ip_opt_obj); 26647 } 26648 if (err != EINPROGRESS) { 26649 /* operation is done */ 26650 CONN_OPER_PENDING_DONE(connp); 26651 } 26652 } 26653 26654 /* 26655 * ioctls that go through a down/up sequence may need to wait for the down 26656 * to complete. This involves waiting for the ire and ipif refcnts to go down 26657 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 26658 */ 26659 /* ARGSUSED */ 26660 void 26661 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 26662 { 26663 struct iocblk *iocp; 26664 mblk_t *mp1; 26665 ip_ioctl_cmd_t *ipip; 26666 int err; 26667 sin_t *sin; 26668 struct lifreq *lifr; 26669 struct ifreq *ifr; 26670 26671 iocp = (struct iocblk *)mp->b_rptr; 26672 ASSERT(ipsq != NULL); 26673 /* Existence of mp1 verified in ip_wput_nondata */ 26674 mp1 = mp->b_cont->b_cont; 26675 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26676 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 26677 /* 26678 * Special case where ipsq_current_ipif is not set: 26679 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 26680 * ill could also have become part of a ipmp group in the 26681 * process, we are here as were not able to complete the 26682 * operation in ipif_set_values because we could not become 26683 * exclusive on the new ipsq, In such a case ipsq_current_ipif 26684 * will not be set so we need to set it. 26685 */ 26686 ill_t *ill = q->q_ptr; 26687 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd); 26688 } 26689 ASSERT(ipsq->ipsq_current_ipif != NULL); 26690 26691 if (ipip->ipi_cmd_type == IF_CMD) { 26692 /* This a old style SIOC[GS]IF* command */ 26693 ifr = (struct ifreq *)mp1->b_rptr; 26694 sin = (sin_t *)&ifr->ifr_addr; 26695 } else if (ipip->ipi_cmd_type == LIF_CMD) { 26696 /* This a new style SIOC[GS]LIF* command */ 26697 lifr = (struct lifreq *)mp1->b_rptr; 26698 sin = (sin_t *)&lifr->lifr_addr; 26699 } else { 26700 sin = NULL; 26701 } 26702 26703 err = (*ipip->ipi_func_restart)(ipsq->ipsq_current_ipif, sin, q, mp, 26704 ipip, mp1->b_rptr); 26705 26706 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 26707 } 26708 26709 /* 26710 * ioctl processing 26711 * 26712 * ioctl processing starts with ip_sioctl_copyin_setup which looks up 26713 * the ioctl command in the ioctl tables and determines the copyin data size 26714 * from the ioctl property ipi_copyin_size, and does an mi_copyin() of that 26715 * size. 26716 * 26717 * ioctl processing then continues when the M_IOCDATA makes its way down. 26718 * Now the ioctl is looked up again in the ioctl table, and its properties are 26719 * extracted. The associated 'conn' is then refheld till the end of the ioctl 26720 * and the general ioctl processing function ip_process_ioctl is called. 26721 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 26722 * so goes thru the serialization primitive ipsq_try_enter. Then the 26723 * appropriate function to handle the ioctl is called based on the entry in 26724 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 26725 * which also refreleases the 'conn' that was refheld at the start of the 26726 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 26727 * ip_extract_lifreq_cmn extracts the interface name from the lifreq/ifreq 26728 * struct and looks up the ipif. ip_extract_tunreq handles the case of tunnel. 26729 * 26730 * Many exclusive ioctls go thru an internal down up sequence as part of 26731 * the operation. For example an attempt to change the IP address of an 26732 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 26733 * does all the cleanup such as deleting all ires that use this address. 26734 * Then we need to wait till all references to the interface go away. 26735 */ 26736 void 26737 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 26738 { 26739 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 26740 ip_ioctl_cmd_t *ipip = (ip_ioctl_cmd_t *)arg; 26741 cmd_info_t ci; 26742 int err; 26743 boolean_t entered_ipsq = B_FALSE; 26744 26745 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 26746 26747 if (ipip == NULL) 26748 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 26749 26750 /* 26751 * SIOCLIFADDIF needs to go thru a special path since the 26752 * ill may not exist yet. This happens in the case of lo0 26753 * which is created using this ioctl. 26754 */ 26755 if (ipip->ipi_cmd == SIOCLIFADDIF) { 26756 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 26757 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26758 return; 26759 } 26760 26761 ci.ci_ipif = NULL; 26762 switch (ipip->ipi_cmd_type) { 26763 case IF_CMD: 26764 case LIF_CMD: 26765 /* 26766 * ioctls that pass in a [l]ifreq appear here. 26767 * ip_extract_lifreq_cmn returns a refheld ipif in 26768 * ci.ci_ipif 26769 */ 26770 err = ip_extract_lifreq_cmn(q, mp, ipip->ipi_cmd_type, 26771 ipip->ipi_flags, &ci, ip_process_ioctl); 26772 if (err != 0) { 26773 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26774 return; 26775 } 26776 ASSERT(ci.ci_ipif != NULL); 26777 break; 26778 26779 case TUN_CMD: 26780 /* 26781 * SIOC[GS]TUNPARAM appear here. ip_extract_tunreq returns 26782 * a refheld ipif in ci.ci_ipif 26783 */ 26784 err = ip_extract_tunreq(q, mp, &ci.ci_ipif, ip_process_ioctl); 26785 if (err != 0) { 26786 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26787 return; 26788 } 26789 ASSERT(ci.ci_ipif != NULL); 26790 break; 26791 26792 case MISC_CMD: 26793 /* 26794 * ioctls that neither pass in [l]ifreq or iftun_req come here 26795 * For eg. SIOCGLIFCONF will appear here. 26796 */ 26797 switch (ipip->ipi_cmd) { 26798 case IF_UNITSEL: 26799 /* ioctl comes down the ill */ 26800 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 26801 ipif_refhold(ci.ci_ipif); 26802 break; 26803 case SIOCGMSFILTER: 26804 case SIOCSMSFILTER: 26805 case SIOCGIPMSFILTER: 26806 case SIOCSIPMSFILTER: 26807 err = ip_extract_msfilter(q, mp, &ci.ci_ipif, 26808 ip_process_ioctl); 26809 if (err != 0) { 26810 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), 26811 NULL); 26812 } 26813 break; 26814 } 26815 err = 0; 26816 ci.ci_sin = NULL; 26817 ci.ci_sin6 = NULL; 26818 ci.ci_lifr = NULL; 26819 break; 26820 } 26821 26822 /* 26823 * If ipsq is non-null, we are already being called exclusively 26824 */ 26825 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 26826 if (!(ipip->ipi_flags & IPI_WR)) { 26827 /* 26828 * A return value of EINPROGRESS means the ioctl is 26829 * either queued and waiting for some reason or has 26830 * already completed. 26831 */ 26832 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 26833 ci.ci_lifr); 26834 if (ci.ci_ipif != NULL) 26835 ipif_refrele(ci.ci_ipif); 26836 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 26837 return; 26838 } 26839 26840 ASSERT(ci.ci_ipif != NULL); 26841 26842 if (ipsq == NULL) { 26843 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, 26844 ip_process_ioctl, NEW_OP, B_TRUE); 26845 entered_ipsq = B_TRUE; 26846 } 26847 /* 26848 * Release the ipif so that ipif_down and friends that wait for 26849 * references to go away are not misled about the current ipif_refcnt 26850 * values. We are writer so we can access the ipif even after releasing 26851 * the ipif. 26852 */ 26853 ipif_refrele(ci.ci_ipif); 26854 if (ipsq == NULL) 26855 return; 26856 26857 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd); 26858 26859 /* 26860 * For most set ioctls that come here, this serves as a single point 26861 * where we set the IPIF_CHANGING flag. This ensures that there won't 26862 * be any new references to the ipif. This helps functions that go 26863 * through this path and end up trying to wait for the refcnts 26864 * associated with the ipif to go down to zero. Some exceptions are 26865 * Failover, Failback, and Groupname commands that operate on more than 26866 * just the ci.ci_ipif. These commands internally determine the 26867 * set of ipif's they operate on and set and clear the IPIF_CHANGING 26868 * flags on that set. Another exception is the Removeif command that 26869 * sets the IPIF_CONDEMNED flag internally after identifying the right 26870 * ipif to operate on. 26871 */ 26872 mutex_enter(&(ci.ci_ipif)->ipif_ill->ill_lock); 26873 if (ipip->ipi_cmd != SIOCLIFREMOVEIF && 26874 ipip->ipi_cmd != SIOCLIFFAILOVER && 26875 ipip->ipi_cmd != SIOCLIFFAILBACK && 26876 ipip->ipi_cmd != SIOCSLIFGROUPNAME) 26877 (ci.ci_ipif)->ipif_state_flags |= IPIF_CHANGING; 26878 mutex_exit(&(ci.ci_ipif)->ipif_ill->ill_lock); 26879 26880 /* 26881 * A return value of EINPROGRESS means the ioctl is 26882 * either queued and waiting for some reason or has 26883 * already completed. 26884 */ 26885 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr); 26886 26887 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 26888 26889 if (entered_ipsq) 26890 ipsq_exit(ipsq, B_TRUE, B_TRUE); 26891 } 26892 26893 /* 26894 * Complete the ioctl. Typically ioctls use the mi package and need to 26895 * do mi_copyout/mi_copy_done. 26896 */ 26897 void 26898 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq) 26899 { 26900 conn_t *connp = NULL; 26901 26902 if (err == EINPROGRESS) 26903 return; 26904 26905 if (CONN_Q(q)) { 26906 connp = Q_TO_CONN(q); 26907 ASSERT(connp->conn_ref >= 2); 26908 } 26909 26910 switch (mode) { 26911 case COPYOUT: 26912 if (err == 0) 26913 mi_copyout(q, mp); 26914 else 26915 mi_copy_done(q, mp, err); 26916 break; 26917 26918 case NO_COPYOUT: 26919 mi_copy_done(q, mp, err); 26920 break; 26921 26922 default: 26923 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */ 26924 break; 26925 } 26926 26927 /* 26928 * The refhold placed at the start of the ioctl is released here. 26929 */ 26930 if (connp != NULL) 26931 CONN_OPER_PENDING_DONE(connp); 26932 26933 if (ipsq != NULL) 26934 ipsq_current_finish(ipsq); 26935 } 26936 26937 /* 26938 * This is called from ip_wput_nondata to resume a deferred TCP bind. 26939 */ 26940 /* ARGSUSED */ 26941 void 26942 ip_resume_tcp_bind(void *arg, mblk_t *mp, void *arg2) 26943 { 26944 conn_t *connp = arg; 26945 tcp_t *tcp; 26946 26947 ASSERT(connp != NULL && IPCL_IS_TCP(connp) && connp->conn_tcp != NULL); 26948 tcp = connp->conn_tcp; 26949 26950 if (connp->conn_tcp->tcp_state == TCPS_CLOSED) 26951 freemsg(mp); 26952 else 26953 tcp_rput_other(tcp, mp); 26954 CONN_OPER_PENDING_DONE(connp); 26955 } 26956 26957 /* Called from ip_wput for all non data messages */ 26958 /* ARGSUSED */ 26959 void 26960 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 26961 { 26962 mblk_t *mp1; 26963 ire_t *ire, *fake_ire; 26964 ill_t *ill; 26965 struct iocblk *iocp; 26966 ip_ioctl_cmd_t *ipip; 26967 cred_t *cr; 26968 conn_t *connp = NULL; 26969 int cmd, err; 26970 nce_t *nce; 26971 ipif_t *ipif; 26972 26973 if (CONN_Q(q)) 26974 connp = Q_TO_CONN(q); 26975 26976 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 26977 26978 /* Check if it is a queue to /dev/sctp. */ 26979 if (connp != NULL && connp->conn_ulp == IPPROTO_SCTP && 26980 connp->conn_rq == NULL) { 26981 sctp_wput(q, mp); 26982 return; 26983 } 26984 26985 switch (DB_TYPE(mp)) { 26986 case M_IOCTL: 26987 /* 26988 * IOCTL processing begins in ip_sioctl_copyin_setup which 26989 * will arrange to copy in associated control structures. 26990 */ 26991 ip_sioctl_copyin_setup(q, mp); 26992 return; 26993 case M_IOCDATA: 26994 /* 26995 * Ensure that this is associated with one of our trans- 26996 * parent ioctls. If it's not ours, discard it if we're 26997 * running as a driver, or pass it on if we're a module. 26998 */ 26999 iocp = (struct iocblk *)mp->b_rptr; 27000 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 27001 if (ipip == NULL) { 27002 if (q->q_next == NULL) { 27003 goto nak; 27004 } else { 27005 putnext(q, mp); 27006 } 27007 return; 27008 } else if ((q->q_next != NULL) && 27009 !(ipip->ipi_flags & IPI_MODOK)) { 27010 /* 27011 * the ioctl is one we recognise, but is not 27012 * consumed by IP as a module, pass M_IOCDATA 27013 * for processing downstream, but only for 27014 * common Streams ioctls. 27015 */ 27016 if (ipip->ipi_flags & IPI_PASS_DOWN) { 27017 putnext(q, mp); 27018 return; 27019 } else { 27020 goto nak; 27021 } 27022 } 27023 27024 /* IOCTL continuation following copyin or copyout. */ 27025 if (mi_copy_state(q, mp, NULL) == -1) { 27026 /* 27027 * The copy operation failed. mi_copy_state already 27028 * cleaned up, so we're out of here. 27029 */ 27030 return; 27031 } 27032 /* 27033 * If we just completed a copy in, we become writer and 27034 * continue processing in ip_sioctl_copyin_done. If it 27035 * was a copy out, we call mi_copyout again. If there is 27036 * nothing more to copy out, it will complete the IOCTL. 27037 */ 27038 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 27039 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 27040 mi_copy_done(q, mp, EPROTO); 27041 return; 27042 } 27043 /* 27044 * Check for cases that need more copying. A return 27045 * value of 0 means a second copyin has been started, 27046 * so we return; a return value of 1 means no more 27047 * copying is needed, so we continue. 27048 */ 27049 cmd = iocp->ioc_cmd; 27050 if ((cmd == SIOCGMSFILTER || cmd == SIOCSMSFILTER || 27051 cmd == SIOCGIPMSFILTER || cmd == SIOCSIPMSFILTER) && 27052 MI_COPY_COUNT(mp) == 1) { 27053 if (ip_copyin_msfilter(q, mp) == 0) 27054 return; 27055 } 27056 /* 27057 * Refhold the conn, till the ioctl completes. This is 27058 * needed in case the ioctl ends up in the pending mp 27059 * list. Every mp in the ill_pending_mp list and 27060 * the ipsq_pending_mp must have a refhold on the conn 27061 * to resume processing. The refhold is released when 27062 * the ioctl completes. (normally or abnormally) 27063 * In all cases ip_ioctl_finish is called to finish 27064 * the ioctl. 27065 */ 27066 if (connp != NULL) { 27067 /* This is not a reentry */ 27068 ASSERT(ipsq == NULL); 27069 CONN_INC_REF(connp); 27070 } else { 27071 if (!(ipip->ipi_flags & IPI_MODOK)) { 27072 mi_copy_done(q, mp, EINVAL); 27073 return; 27074 } 27075 } 27076 27077 ip_process_ioctl(ipsq, q, mp, ipip); 27078 27079 } else { 27080 mi_copyout(q, mp); 27081 } 27082 return; 27083 nak: 27084 iocp->ioc_error = EINVAL; 27085 mp->b_datap->db_type = M_IOCNAK; 27086 iocp->ioc_count = 0; 27087 qreply(q, mp); 27088 return; 27089 27090 case M_IOCNAK: 27091 /* 27092 * The only way we could get here is if a resolver didn't like 27093 * an IOCTL we sent it. This shouldn't happen. 27094 */ 27095 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 27096 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 27097 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 27098 freemsg(mp); 27099 return; 27100 case M_IOCACK: 27101 /* Finish socket ioctls passed through to ARP. */ 27102 ip_sioctl_iocack(q, mp); 27103 return; 27104 case M_FLUSH: 27105 if (*mp->b_rptr & FLUSHW) 27106 flushq(q, FLUSHALL); 27107 if (q->q_next) { 27108 /* 27109 * M_FLUSH is sent up to IP by some drivers during 27110 * unbind. ip_rput has already replied to it. We are 27111 * here for the M_FLUSH that we originated in IP 27112 * before sending the unbind request to the driver. 27113 * Just free it as we don't queue packets in IP 27114 * on the write side of the device instance. 27115 */ 27116 freemsg(mp); 27117 return; 27118 } 27119 if (*mp->b_rptr & FLUSHR) { 27120 *mp->b_rptr &= ~FLUSHW; 27121 qreply(q, mp); 27122 return; 27123 } 27124 freemsg(mp); 27125 return; 27126 case IRE_DB_REQ_TYPE: 27127 /* An Upper Level Protocol wants a copy of an IRE. */ 27128 ip_ire_req(q, mp); 27129 return; 27130 case M_CTL: 27131 if (mp->b_wptr - mp->b_rptr < sizeof (uint32_t)) 27132 break; 27133 27134 if (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == 27135 TUN_HELLO) { 27136 ASSERT(connp != NULL); 27137 connp->conn_flags |= IPCL_IPTUN; 27138 freeb(mp); 27139 return; 27140 } 27141 27142 if (connp != NULL && *(uint32_t *)mp->b_rptr == 27143 IP_ULP_OUT_LABELED) { 27144 out_labeled_t *olp; 27145 27146 if (mp->b_wptr - mp->b_rptr != sizeof (*olp)) 27147 break; 27148 olp = (out_labeled_t *)mp->b_rptr; 27149 connp->conn_ulp_labeled = olp->out_qnext == q; 27150 freemsg(mp); 27151 return; 27152 } 27153 27154 /* M_CTL messages are used by ARP to tell us things. */ 27155 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 27156 break; 27157 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 27158 case AR_ENTRY_SQUERY: 27159 ip_wput_ctl(q, mp); 27160 return; 27161 case AR_CLIENT_NOTIFY: 27162 ip_arp_news(q, mp); 27163 return; 27164 case AR_DLPIOP_DONE: 27165 ASSERT(q->q_next != NULL); 27166 ill = (ill_t *)q->q_ptr; 27167 /* qwriter_ip releases the refhold */ 27168 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 27169 ill_refhold(ill); 27170 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_done, 27171 CUR_OP, B_FALSE); 27172 return; 27173 case AR_ARP_CLOSING: 27174 /* 27175 * ARP (above us) is closing. If no ARP bringup is 27176 * currently pending, ack the message so that ARP 27177 * can complete its close. Also mark ill_arp_closing 27178 * so that new ARP bringups will fail. If any 27179 * ARP bringup is currently in progress, we will 27180 * ack this when the current ARP bringup completes. 27181 */ 27182 ASSERT(q->q_next != NULL); 27183 ill = (ill_t *)q->q_ptr; 27184 mutex_enter(&ill->ill_lock); 27185 ill->ill_arp_closing = 1; 27186 if (!ill->ill_arp_bringup_pending) { 27187 mutex_exit(&ill->ill_lock); 27188 qreply(q, mp); 27189 } else { 27190 mutex_exit(&ill->ill_lock); 27191 freemsg(mp); 27192 } 27193 return; 27194 case AR_ARP_EXTEND: 27195 /* 27196 * The ARP module above us is capable of duplicate 27197 * address detection. Old ATM drivers will not send 27198 * this message. 27199 */ 27200 ASSERT(q->q_next != NULL); 27201 ill = (ill_t *)q->q_ptr; 27202 ill->ill_arp_extend = B_TRUE; 27203 freemsg(mp); 27204 return; 27205 default: 27206 break; 27207 } 27208 break; 27209 case M_PROTO: 27210 case M_PCPROTO: 27211 /* 27212 * The only PROTO messages we expect are ULP binds and 27213 * copies of option negotiation acknowledgements. 27214 */ 27215 switch (((union T_primitives *)mp->b_rptr)->type) { 27216 case O_T_BIND_REQ: 27217 case T_BIND_REQ: { 27218 /* Request can get queued in bind */ 27219 ASSERT(connp != NULL); 27220 /* 27221 * Both TCP and UDP call ip_bind_{v4,v6}() directly 27222 * instead of going through this path. We only get 27223 * here in the following cases: 27224 * 27225 * a. Bind retries, where ipsq is non-NULL. 27226 * b. T_BIND_REQ is issued from non TCP/UDP 27227 * transport, e.g. icmp for raw socket, 27228 * in which case ipsq will be NULL. 27229 */ 27230 ASSERT(ipsq != NULL || 27231 (!IPCL_IS_TCP(connp) && !IPCL_IS_UDP(connp))); 27232 27233 /* Don't increment refcnt if this is a re-entry */ 27234 if (ipsq == NULL) 27235 CONN_INC_REF(connp); 27236 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 27237 connp, NULL) : ip_bind_v4(q, mp, connp); 27238 if (mp == NULL) 27239 return; 27240 if (IPCL_IS_TCP(connp)) { 27241 /* 27242 * In the case of TCP endpoint we 27243 * come here only for bind retries 27244 */ 27245 ASSERT(ipsq != NULL); 27246 CONN_INC_REF(connp); 27247 squeue_fill(connp->conn_sqp, mp, 27248 ip_resume_tcp_bind, connp, 27249 SQTAG_BIND_RETRY); 27250 return; 27251 } else if (IPCL_IS_UDP(connp)) { 27252 /* 27253 * In the case of UDP endpoint we 27254 * come here only for bind retries 27255 */ 27256 ASSERT(ipsq != NULL); 27257 udp_resume_bind(connp, mp); 27258 return; 27259 } 27260 qreply(q, mp); 27261 CONN_OPER_PENDING_DONE(connp); 27262 return; 27263 } 27264 case T_SVR4_OPTMGMT_REQ: 27265 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 27266 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 27267 27268 ASSERT(connp != NULL); 27269 if (!snmpcom_req(q, mp, ip_snmp_set, 27270 ip_snmp_get, cr)) { 27271 /* 27272 * Call svr4_optcom_req so that it can 27273 * generate the ack. We don't come here 27274 * if this operation is being restarted. 27275 * ip_restart_optmgmt will drop the conn ref. 27276 * In the case of ipsec option after the ipsec 27277 * load is complete conn_restart_ipsec_waiter 27278 * drops the conn ref. 27279 */ 27280 ASSERT(ipsq == NULL); 27281 CONN_INC_REF(connp); 27282 if (ip_check_for_ipsec_opt(q, mp)) 27283 return; 27284 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj); 27285 if (err != EINPROGRESS) { 27286 /* Operation is done */ 27287 CONN_OPER_PENDING_DONE(connp); 27288 } 27289 } 27290 return; 27291 case T_OPTMGMT_REQ: 27292 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 27293 /* 27294 * Note: No snmpcom_req support through new 27295 * T_OPTMGMT_REQ. 27296 * Call tpi_optcom_req so that it can 27297 * generate the ack. 27298 */ 27299 ASSERT(connp != NULL); 27300 ASSERT(ipsq == NULL); 27301 /* 27302 * We don't come here for restart. ip_restart_optmgmt 27303 * will drop the conn ref. In the case of ipsec option 27304 * after the ipsec load is complete 27305 * conn_restart_ipsec_waiter drops the conn ref. 27306 */ 27307 CONN_INC_REF(connp); 27308 if (ip_check_for_ipsec_opt(q, mp)) 27309 return; 27310 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj); 27311 if (err != EINPROGRESS) { 27312 /* Operation is done */ 27313 CONN_OPER_PENDING_DONE(connp); 27314 } 27315 return; 27316 case T_UNBIND_REQ: 27317 mp = ip_unbind(q, mp); 27318 qreply(q, mp); 27319 return; 27320 default: 27321 /* 27322 * Have to drop any DLPI messages coming down from 27323 * arp (such as an info_req which would cause ip 27324 * to receive an extra info_ack if it was passed 27325 * through. 27326 */ 27327 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 27328 (int)*(uint_t *)mp->b_rptr)); 27329 freemsg(mp); 27330 return; 27331 } 27332 /* NOTREACHED */ 27333 case IRE_DB_TYPE: { 27334 nce_t *nce; 27335 ill_t *ill; 27336 in6_addr_t gw_addr_v6; 27337 27338 27339 /* 27340 * This is a response back from a resolver. It 27341 * consists of a message chain containing: 27342 * IRE_MBLK-->LL_HDR_MBLK->pkt 27343 * The IRE_MBLK is the one we allocated in ip_newroute. 27344 * The LL_HDR_MBLK is the DLPI header to use to get 27345 * the attached packet, and subsequent ones for the 27346 * same destination, transmitted. 27347 */ 27348 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 27349 break; 27350 /* 27351 * First, check to make sure the resolution succeeded. 27352 * If it failed, the second mblk will be empty. 27353 * If it is, free the chain, dropping the packet. 27354 * (We must ire_delete the ire; that frees the ire mblk) 27355 * We're doing this now to support PVCs for ATM; it's 27356 * a partial xresolv implementation. When we fully implement 27357 * xresolv interfaces, instead of freeing everything here 27358 * we'll initiate neighbor discovery. 27359 * 27360 * For v4 (ARP and other external resolvers) the resolver 27361 * frees the message, so no check is needed. This check 27362 * is required, though, for a full xresolve implementation. 27363 * Including this code here now both shows how external 27364 * resolvers can NACK a resolution request using an 27365 * existing design that has no specific provisions for NACKs, 27366 * and also takes into account that the current non-ARP 27367 * external resolver has been coded to use this method of 27368 * NACKing for all IPv6 (xresolv) cases, 27369 * whether our xresolv implementation is complete or not. 27370 * 27371 */ 27372 ire = (ire_t *)mp->b_rptr; 27373 ill = ire_to_ill(ire); 27374 mp1 = mp->b_cont; /* dl_unitdata_req */ 27375 if (mp1->b_rptr == mp1->b_wptr) { 27376 if (ire->ire_ipversion == IPV6_VERSION) { 27377 /* 27378 * XRESOLV interface. 27379 */ 27380 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27381 mutex_enter(&ire->ire_lock); 27382 gw_addr_v6 = ire->ire_gateway_addr_v6; 27383 mutex_exit(&ire->ire_lock); 27384 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27385 nce = ndp_lookup_v6(ill, 27386 &ire->ire_addr_v6, B_FALSE); 27387 } else { 27388 nce = ndp_lookup_v6(ill, &gw_addr_v6, 27389 B_FALSE); 27390 } 27391 if (nce != NULL) { 27392 nce_resolv_failed(nce); 27393 ndp_delete(nce); 27394 NCE_REFRELE(nce); 27395 } 27396 } 27397 mp->b_cont = NULL; 27398 freemsg(mp1); /* frees the pkt as well */ 27399 ASSERT(ire->ire_nce == NULL); 27400 ire_delete((ire_t *)mp->b_rptr); 27401 return; 27402 } 27403 27404 /* 27405 * Split them into IRE_MBLK and pkt and feed it into 27406 * ire_add_then_send. Then in ire_add_then_send 27407 * the IRE will be added, and then the packet will be 27408 * run back through ip_wput. This time it will make 27409 * it to the wire. 27410 */ 27411 mp->b_cont = NULL; 27412 mp = mp1->b_cont; /* now, mp points to pkt */ 27413 mp1->b_cont = NULL; 27414 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 27415 if (ire->ire_ipversion == IPV6_VERSION) { 27416 /* 27417 * XRESOLV interface. Find the nce and put a copy 27418 * of the dl_unitdata_req in nce_res_mp 27419 */ 27420 ASSERT(ill->ill_flags & ILLF_XRESOLV); 27421 mutex_enter(&ire->ire_lock); 27422 gw_addr_v6 = ire->ire_gateway_addr_v6; 27423 mutex_exit(&ire->ire_lock); 27424 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 27425 nce = ndp_lookup_v6(ill, &ire->ire_addr_v6, 27426 B_FALSE); 27427 } else { 27428 nce = ndp_lookup_v6(ill, &gw_addr_v6, B_FALSE); 27429 } 27430 if (nce != NULL) { 27431 /* 27432 * We have to protect nce_res_mp here 27433 * from being accessed by other threads 27434 * while we change the mblk pointer. 27435 * Other functions will also lock the nce when 27436 * accessing nce_res_mp. 27437 * 27438 * The reason we change the mblk pointer 27439 * here rather than copying the resolved address 27440 * into the template is that, unlike with 27441 * ethernet, we have no guarantee that the 27442 * resolved address length will be 27443 * smaller than or equal to the lla length 27444 * with which the template was allocated, 27445 * (for ethernet, they're equal) 27446 * so we have to use the actual resolved 27447 * address mblk - which holds the real 27448 * dl_unitdata_req with the resolved address. 27449 * 27450 * Doing this is the same behavior as was 27451 * previously used in the v4 ARP case. 27452 */ 27453 mutex_enter(&nce->nce_lock); 27454 if (nce->nce_res_mp != NULL) 27455 freemsg(nce->nce_res_mp); 27456 nce->nce_res_mp = mp1; 27457 mutex_exit(&nce->nce_lock); 27458 /* 27459 * We do a fastpath probe here because 27460 * we have resolved the address without 27461 * using Neighbor Discovery. 27462 * In the non-XRESOLV v6 case, the fastpath 27463 * probe is done right after neighbor 27464 * discovery completes. 27465 */ 27466 if (nce->nce_res_mp != NULL) { 27467 int res; 27468 nce_fastpath_list_add(nce); 27469 res = ill_fastpath_probe(ill, 27470 nce->nce_res_mp); 27471 if (res != 0 && res != EAGAIN) 27472 nce_fastpath_list_delete(nce); 27473 } 27474 27475 ire_add_then_send(q, ire, mp); 27476 /* 27477 * Now we have to clean out any packets 27478 * that may have been queued on the nce 27479 * while it was waiting for address resolution 27480 * to complete. 27481 */ 27482 mutex_enter(&nce->nce_lock); 27483 mp1 = nce->nce_qd_mp; 27484 nce->nce_qd_mp = NULL; 27485 mutex_exit(&nce->nce_lock); 27486 while (mp1 != NULL) { 27487 mblk_t *nxt_mp; 27488 queue_t *fwdq = NULL; 27489 ill_t *inbound_ill; 27490 uint_t ifindex; 27491 27492 nxt_mp = mp1->b_next; 27493 mp1->b_next = NULL; 27494 /* 27495 * Retrieve ifindex stored in 27496 * ip_rput_data_v6() 27497 */ 27498 ifindex = 27499 (uint_t)(uintptr_t)mp1->b_prev; 27500 inbound_ill = 27501 ill_lookup_on_ifindex(ifindex, 27502 B_TRUE, NULL, NULL, NULL, 27503 NULL); 27504 mp1->b_prev = NULL; 27505 if (inbound_ill != NULL) 27506 fwdq = inbound_ill->ill_rq; 27507 27508 if (fwdq != NULL) { 27509 put(fwdq, mp1); 27510 ill_refrele(inbound_ill); 27511 } else 27512 put(WR(ill->ill_rq), mp1); 27513 mp1 = nxt_mp; 27514 } 27515 NCE_REFRELE(nce); 27516 } else { /* nce is NULL; clean up */ 27517 ire_delete(ire); 27518 freemsg(mp); 27519 freemsg(mp1); 27520 return; 27521 } 27522 } else { 27523 nce_t *arpce; 27524 /* 27525 * Link layer resolution succeeded. Recompute the 27526 * ire_nce. 27527 */ 27528 ASSERT(ire->ire_type & (IRE_CACHE|IRE_BROADCAST)); 27529 if ((arpce = ndp_lookup_v4(ill, 27530 (ire->ire_gateway_addr != INADDR_ANY ? 27531 &ire->ire_gateway_addr : &ire->ire_addr), 27532 B_FALSE)) == NULL) { 27533 freeb(ire->ire_mp); 27534 freeb(mp1); 27535 freemsg(mp); 27536 return; 27537 } 27538 mutex_enter(&arpce->nce_lock); 27539 arpce->nce_last = TICK_TO_MSEC(lbolt64); 27540 if (arpce->nce_state == ND_REACHABLE) { 27541 /* 27542 * Someone resolved this before us; 27543 * cleanup the res_mp. Since ire has 27544 * not been added yet, the call to ire_add_v4 27545 * from ire_add_then_send (when a dup is 27546 * detected) will clean up the ire. 27547 */ 27548 freeb(mp1); 27549 } else { 27550 if (arpce->nce_res_mp != NULL) 27551 freemsg(arpce->nce_res_mp); 27552 arpce->nce_res_mp = mp1; 27553 arpce->nce_state = ND_REACHABLE; 27554 } 27555 mutex_exit(&arpce->nce_lock); 27556 if (ire->ire_marks & IRE_MARK_NOADD) { 27557 /* 27558 * this ire will not be added to the ire 27559 * cache table, so we can set the ire_nce 27560 * here, as there are no atomicity constraints. 27561 */ 27562 ire->ire_nce = arpce; 27563 /* 27564 * We are associating this nce with the ire 27565 * so change the nce ref taken in 27566 * ndp_lookup_v4() from 27567 * NCE_REFHOLD to NCE_REFHOLD_NOTR 27568 */ 27569 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); 27570 } else { 27571 NCE_REFRELE(arpce); 27572 } 27573 ire_add_then_send(q, ire, mp); 27574 } 27575 return; /* All is well, the packet has been sent. */ 27576 } 27577 case IRE_ARPRESOLVE_TYPE: { 27578 27579 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* fake_ire */ 27580 break; 27581 mp1 = mp->b_cont; /* dl_unitdata_req */ 27582 mp->b_cont = NULL; 27583 /* 27584 * First, check to make sure the resolution succeeded. 27585 * If it failed, the second mblk will be empty. 27586 */ 27587 if (mp1->b_rptr == mp1->b_wptr) { 27588 /* cleanup the incomplete ire, free queued packets */ 27589 freemsg(mp); /* fake ire */ 27590 freeb(mp1); /* dl_unitdata response */ 27591 return; 27592 } 27593 27594 /* 27595 * update any incomplete nce_t found. we lookup the ctable 27596 * and find the nce from the ire->ire_nce because we need 27597 * to pass the ire to ip_xmit_v4 later, and can find both 27598 * ire and nce in one lookup from the ctable. 27599 */ 27600 fake_ire = (ire_t *)mp->b_rptr; 27601 /* 27602 * By the time we come back here from ARP 27603 * the logical outgoing interface of the incomplete ire 27604 * we added in ire_forward could have disappeared, 27605 * causing the incomplete ire to also have 27606 * dissapeared. So we need to retreive the 27607 * proper ipif for the ire before looking 27608 * in ctable; do the ctablelookup based on ire_ipif_seqid 27609 */ 27610 ill = q->q_ptr; 27611 27612 /* Get the outgoing ipif */ 27613 mutex_enter(&ill->ill_lock); 27614 if (ill->ill_state_flags & ILL_CONDEMNED) { 27615 mutex_exit(&ill->ill_lock); 27616 freemsg(mp); /* fake ire */ 27617 freeb(mp1); /* dl_unitdata response */ 27618 return; 27619 } 27620 ipif = ipif_lookup_seqid(ill, fake_ire->ire_ipif_seqid); 27621 27622 if (ipif == NULL) { 27623 mutex_exit(&ill->ill_lock); 27624 ip1dbg(("logical intrf to incomplete ire vanished\n")); 27625 freemsg(mp); 27626 freeb(mp1); 27627 return; 27628 } 27629 ipif_refhold_locked(ipif); 27630 mutex_exit(&ill->ill_lock); 27631 ire = ire_ctable_lookup(fake_ire->ire_addr, 27632 fake_ire->ire_gateway_addr, IRE_CACHE, 27633 ipif, fake_ire->ire_zoneid, NULL, 27634 (MATCH_IRE_GW|MATCH_IRE_IPIF|MATCH_IRE_ZONEONLY)); 27635 ipif_refrele(ipif); 27636 if (ire == NULL) { 27637 /* 27638 * no ire was found; check if there is an nce 27639 * for this lookup; if it has no ire's pointing at it 27640 * cleanup. 27641 */ 27642 if ((nce = ndp_lookup_v4(ill, 27643 (fake_ire->ire_gateway_addr != INADDR_ANY ? 27644 &fake_ire->ire_gateway_addr : &fake_ire->ire_addr), 27645 B_FALSE)) != NULL) { 27646 /* 27647 * cleanup: just reset nce. 27648 * We check for refcnt 2 (one for the nce 27649 * hash list + 1 for the ref taken by 27650 * ndp_lookup_v4) to ensure that there are 27651 * no ire's pointing at the nce. 27652 */ 27653 if (nce->nce_refcnt == 2) { 27654 nce = nce_reinit(nce); 27655 } 27656 if (nce != NULL) 27657 NCE_REFRELE(nce); 27658 } 27659 freeb(mp1); /* dl_unitdata response */ 27660 freemsg(mp); /* fake ire */ 27661 return; 27662 } 27663 nce = ire->ire_nce; 27664 DTRACE_PROBE2(ire__arpresolve__type, 27665 ire_t *, ire, nce_t *, nce); 27666 ASSERT(nce->nce_state != ND_INITIAL); 27667 mutex_enter(&nce->nce_lock); 27668 nce->nce_last = TICK_TO_MSEC(lbolt64); 27669 if (nce->nce_state == ND_REACHABLE) { 27670 /* 27671 * Someone resolved this before us; 27672 * our response is not needed any more. 27673 */ 27674 mutex_exit(&nce->nce_lock); 27675 freeb(mp1); /* dl_unitdata response */ 27676 } else { 27677 if (nce->nce_res_mp != NULL) { 27678 freemsg(nce->nce_res_mp); 27679 /* existing dl_unitdata template */ 27680 } 27681 nce->nce_res_mp = mp1; 27682 nce->nce_state = ND_REACHABLE; 27683 mutex_exit(&nce->nce_lock); 27684 nce_fastpath(nce); 27685 } 27686 /* 27687 * The cached nce_t has been updated to be reachable; 27688 * Set the IRE_MARK_UNCACHED flag and free the fake_ire. 27689 */ 27690 fake_ire->ire_marks &= ~IRE_MARK_UNCACHED; 27691 freemsg(mp); 27692 /* 27693 * send out queued packets. 27694 */ 27695 (void) ip_xmit_v4(NULL, ire, NULL, B_FALSE); 27696 27697 IRE_REFRELE(ire); 27698 return; 27699 } 27700 default: 27701 break; 27702 } 27703 if (q->q_next) { 27704 putnext(q, mp); 27705 } else 27706 freemsg(mp); 27707 } 27708 27709 /* 27710 * Process IP options in an outbound packet. Modify the destination if there 27711 * is a source route option. 27712 * Returns non-zero if something fails in which case an ICMP error has been 27713 * sent and mp freed. 27714 */ 27715 static int 27716 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 27717 boolean_t mctl_present, zoneid_t zoneid) 27718 { 27719 ipoptp_t opts; 27720 uchar_t *opt; 27721 uint8_t optval; 27722 uint8_t optlen; 27723 ipaddr_t dst; 27724 intptr_t code = 0; 27725 mblk_t *mp; 27726 ire_t *ire = NULL; 27727 27728 ip2dbg(("ip_wput_options\n")); 27729 mp = ipsec_mp; 27730 if (mctl_present) { 27731 mp = ipsec_mp->b_cont; 27732 } 27733 27734 dst = ipha->ipha_dst; 27735 for (optval = ipoptp_first(&opts, ipha); 27736 optval != IPOPT_EOL; 27737 optval = ipoptp_next(&opts)) { 27738 opt = opts.ipoptp_cur; 27739 optlen = opts.ipoptp_len; 27740 ip2dbg(("ip_wput_options: opt %d, len %d\n", 27741 optval, optlen)); 27742 switch (optval) { 27743 uint32_t off; 27744 case IPOPT_SSRR: 27745 case IPOPT_LSRR: 27746 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27747 ip1dbg(( 27748 "ip_wput_options: bad option offset\n")); 27749 code = (char *)&opt[IPOPT_OLEN] - 27750 (char *)ipha; 27751 goto param_prob; 27752 } 27753 off = opt[IPOPT_OFFSET]; 27754 ip1dbg(("ip_wput_options: next hop 0x%x\n", 27755 ntohl(dst))); 27756 /* 27757 * For strict: verify that dst is directly 27758 * reachable. 27759 */ 27760 if (optval == IPOPT_SSRR) { 27761 ire = ire_ftable_lookup(dst, 0, 0, 27762 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 27763 MBLK_GETLABEL(mp), 27764 MATCH_IRE_TYPE | MATCH_IRE_SECATTR); 27765 if (ire == NULL) { 27766 ip1dbg(("ip_wput_options: SSRR not" 27767 " directly reachable: 0x%x\n", 27768 ntohl(dst))); 27769 goto bad_src_route; 27770 } 27771 ire_refrele(ire); 27772 } 27773 break; 27774 case IPOPT_RR: 27775 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27776 ip1dbg(( 27777 "ip_wput_options: bad option offset\n")); 27778 code = (char *)&opt[IPOPT_OLEN] - 27779 (char *)ipha; 27780 goto param_prob; 27781 } 27782 break; 27783 case IPOPT_TS: 27784 /* 27785 * Verify that length >=5 and that there is either 27786 * room for another timestamp or that the overflow 27787 * counter is not maxed out. 27788 */ 27789 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 27790 if (optlen < IPOPT_MINLEN_IT) { 27791 goto param_prob; 27792 } 27793 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 27794 ip1dbg(( 27795 "ip_wput_options: bad option offset\n")); 27796 code = (char *)&opt[IPOPT_OFFSET] - 27797 (char *)ipha; 27798 goto param_prob; 27799 } 27800 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 27801 case IPOPT_TS_TSONLY: 27802 off = IPOPT_TS_TIMELEN; 27803 break; 27804 case IPOPT_TS_TSANDADDR: 27805 case IPOPT_TS_PRESPEC: 27806 case IPOPT_TS_PRESPEC_RFC791: 27807 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 27808 break; 27809 default: 27810 code = (char *)&opt[IPOPT_POS_OV_FLG] - 27811 (char *)ipha; 27812 goto param_prob; 27813 } 27814 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 27815 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 27816 /* 27817 * No room and the overflow counter is 15 27818 * already. 27819 */ 27820 goto param_prob; 27821 } 27822 break; 27823 } 27824 } 27825 27826 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 27827 return (0); 27828 27829 ip1dbg(("ip_wput_options: error processing IP options.")); 27830 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 27831 27832 param_prob: 27833 /* 27834 * Since ip_wput() isn't close to finished, we fill 27835 * in enough of the header for credible error reporting. 27836 */ 27837 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 27838 /* Failed */ 27839 freemsg(ipsec_mp); 27840 return (-1); 27841 } 27842 icmp_param_problem(q, ipsec_mp, (uint8_t)code, zoneid); 27843 return (-1); 27844 27845 bad_src_route: 27846 /* 27847 * Since ip_wput() isn't close to finished, we fill 27848 * in enough of the header for credible error reporting. 27849 */ 27850 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 27851 /* Failed */ 27852 freemsg(ipsec_mp); 27853 return (-1); 27854 } 27855 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid); 27856 return (-1); 27857 } 27858 27859 /* 27860 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 27861 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 27862 * thru /etc/system. 27863 */ 27864 #define CONN_MAXDRAINCNT 64 27865 27866 static void 27867 conn_drain_init(void) 27868 { 27869 int i; 27870 27871 conn_drain_list_cnt = conn_drain_nthreads; 27872 27873 if ((conn_drain_list_cnt == 0) || 27874 (conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 27875 /* 27876 * Default value of the number of drainers is the 27877 * number of cpus, subject to maximum of 8 drainers. 27878 */ 27879 if (boot_max_ncpus != -1) 27880 conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 27881 else 27882 conn_drain_list_cnt = MIN(max_ncpus, 8); 27883 } 27884 27885 conn_drain_list = kmem_zalloc(conn_drain_list_cnt * sizeof (idl_t), 27886 KM_SLEEP); 27887 27888 for (i = 0; i < conn_drain_list_cnt; i++) { 27889 mutex_init(&conn_drain_list[i].idl_lock, NULL, 27890 MUTEX_DEFAULT, NULL); 27891 } 27892 } 27893 27894 static void 27895 conn_drain_fini(void) 27896 { 27897 int i; 27898 27899 for (i = 0; i < conn_drain_list_cnt; i++) 27900 mutex_destroy(&conn_drain_list[i].idl_lock); 27901 kmem_free(conn_drain_list, conn_drain_list_cnt * sizeof (idl_t)); 27902 conn_drain_list = NULL; 27903 } 27904 27905 /* 27906 * Note: For an overview of how flowcontrol is handled in IP please see the 27907 * IP Flowcontrol notes at the top of this file. 27908 * 27909 * Flow control has blocked us from proceeding. Insert the given conn in one 27910 * of the conn drain lists. These conn wq's will be qenabled later on when 27911 * STREAMS flow control does a backenable. conn_walk_drain will enable 27912 * the first conn in each of these drain lists. Each of these qenabled conns 27913 * in turn enables the next in the list, after it runs, or when it closes, 27914 * thus sustaining the drain process. 27915 * 27916 * The only possible calling sequence is ip_wsrv (on conn) -> ip_wput -> 27917 * conn_drain_insert. Thus there can be only 1 instance of conn_drain_insert 27918 * running at any time, on a given conn, since there can be only 1 service proc 27919 * running on a queue at any time. 27920 */ 27921 void 27922 conn_drain_insert(conn_t *connp) 27923 { 27924 idl_t *idl; 27925 uint_t index; 27926 27927 mutex_enter(&connp->conn_lock); 27928 if (connp->conn_state_flags & CONN_CLOSING) { 27929 /* 27930 * The conn is closing as a result of which CONN_CLOSING 27931 * is set. Return. 27932 */ 27933 mutex_exit(&connp->conn_lock); 27934 return; 27935 } else if (connp->conn_idl == NULL) { 27936 /* 27937 * Assign the next drain list round robin. We dont' use 27938 * a lock, and thus it may not be strictly round robin. 27939 * Atomicity of load/stores is enough to make sure that 27940 * conn_drain_list_index is always within bounds. 27941 */ 27942 index = conn_drain_list_index; 27943 ASSERT(index < conn_drain_list_cnt); 27944 connp->conn_idl = &conn_drain_list[index]; 27945 index++; 27946 if (index == conn_drain_list_cnt) 27947 index = 0; 27948 conn_drain_list_index = index; 27949 } 27950 mutex_exit(&connp->conn_lock); 27951 27952 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 27953 if ((connp->conn_drain_prev != NULL) || 27954 (connp->conn_state_flags & CONN_CLOSING)) { 27955 /* 27956 * The conn is already in the drain list, OR 27957 * the conn is closing. We need to check again for 27958 * the closing case again since close can happen 27959 * after we drop the conn_lock, and before we 27960 * acquire the CONN_DRAIN_LIST_LOCK. 27961 */ 27962 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 27963 return; 27964 } else { 27965 idl = connp->conn_idl; 27966 } 27967 27968 /* 27969 * The conn is not in the drain list. Insert it at the 27970 * tail of the drain list. The drain list is circular 27971 * and doubly linked. idl_conn points to the 1st element 27972 * in the list. 27973 */ 27974 if (idl->idl_conn == NULL) { 27975 idl->idl_conn = connp; 27976 connp->conn_drain_next = connp; 27977 connp->conn_drain_prev = connp; 27978 } else { 27979 conn_t *head = idl->idl_conn; 27980 27981 connp->conn_drain_next = head; 27982 connp->conn_drain_prev = head->conn_drain_prev; 27983 head->conn_drain_prev->conn_drain_next = connp; 27984 head->conn_drain_prev = connp; 27985 } 27986 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 27987 } 27988 27989 /* 27990 * This conn is closing, and we are called from ip_close. OR 27991 * This conn has been serviced by ip_wsrv, and we need to do the tail 27992 * processing. 27993 * If this conn is part of the drain list, we may need to sustain the drain 27994 * process by qenabling the next conn in the drain list. We may also need to 27995 * remove this conn from the list, if it is done. 27996 */ 27997 static void 27998 conn_drain_tail(conn_t *connp, boolean_t closing) 27999 { 28000 idl_t *idl; 28001 28002 /* 28003 * connp->conn_idl is stable at this point, and no lock is needed 28004 * to check it. If we are called from ip_close, close has already 28005 * set CONN_CLOSING, thus freezing the value of conn_idl, and 28006 * called us only because conn_idl is non-null. If we are called thru 28007 * service, conn_idl could be null, but it cannot change because 28008 * service is single-threaded per queue, and there cannot be another 28009 * instance of service trying to call conn_drain_insert on this conn 28010 * now. 28011 */ 28012 ASSERT(!closing || (connp->conn_idl != NULL)); 28013 28014 /* 28015 * If connp->conn_idl is null, the conn has not been inserted into any 28016 * drain list even once since creation of the conn. Just return. 28017 */ 28018 if (connp->conn_idl == NULL) 28019 return; 28020 28021 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 28022 28023 if (connp->conn_drain_prev == NULL) { 28024 /* This conn is currently not in the drain list. */ 28025 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28026 return; 28027 } 28028 idl = connp->conn_idl; 28029 if (idl->idl_conn_draining == connp) { 28030 /* 28031 * This conn is the current drainer. If this is the last conn 28032 * in the drain list, we need to do more checks, in the 'if' 28033 * below. Otherwwise we need to just qenable the next conn, 28034 * to sustain the draining, and is handled in the 'else' 28035 * below. 28036 */ 28037 if (connp->conn_drain_next == idl->idl_conn) { 28038 /* 28039 * This conn is the last in this list. This round 28040 * of draining is complete. If idl_repeat is set, 28041 * it means another flow enabling has happened from 28042 * the driver/streams and we need to another round 28043 * of draining. 28044 * If there are more than 2 conns in the drain list, 28045 * do a left rotate by 1, so that all conns except the 28046 * conn at the head move towards the head by 1, and the 28047 * the conn at the head goes to the tail. This attempts 28048 * a more even share for all queues that are being 28049 * drained. 28050 */ 28051 if ((connp->conn_drain_next != connp) && 28052 (idl->idl_conn->conn_drain_next != connp)) { 28053 idl->idl_conn = idl->idl_conn->conn_drain_next; 28054 } 28055 if (idl->idl_repeat) { 28056 qenable(idl->idl_conn->conn_wq); 28057 idl->idl_conn_draining = idl->idl_conn; 28058 idl->idl_repeat = 0; 28059 } else { 28060 idl->idl_conn_draining = NULL; 28061 } 28062 } else { 28063 /* 28064 * If the next queue that we are now qenable'ing, 28065 * is closing, it will remove itself from this list 28066 * and qenable the subsequent queue in ip_close(). 28067 * Serialization is acheived thru idl_lock. 28068 */ 28069 qenable(connp->conn_drain_next->conn_wq); 28070 idl->idl_conn_draining = connp->conn_drain_next; 28071 } 28072 } 28073 if (!connp->conn_did_putbq || closing) { 28074 /* 28075 * Remove ourself from the drain list, if we did not do 28076 * a putbq, or if the conn is closing. 28077 * Note: It is possible that q->q_first is non-null. It means 28078 * that these messages landed after we did a enableok() in 28079 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 28080 * service them. 28081 */ 28082 if (connp->conn_drain_next == connp) { 28083 /* Singleton in the list */ 28084 ASSERT(connp->conn_drain_prev == connp); 28085 idl->idl_conn = NULL; 28086 idl->idl_conn_draining = NULL; 28087 } else { 28088 connp->conn_drain_prev->conn_drain_next = 28089 connp->conn_drain_next; 28090 connp->conn_drain_next->conn_drain_prev = 28091 connp->conn_drain_prev; 28092 if (idl->idl_conn == connp) 28093 idl->idl_conn = connp->conn_drain_next; 28094 ASSERT(idl->idl_conn_draining != connp); 28095 28096 } 28097 connp->conn_drain_next = NULL; 28098 connp->conn_drain_prev = NULL; 28099 } 28100 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 28101 } 28102 28103 /* 28104 * Write service routine. Shared perimeter entry point. 28105 * ip_wsrv can be called in any of the following ways. 28106 * 1. The device queue's messages has fallen below the low water mark 28107 * and STREAMS has backenabled the ill_wq. We walk thru all the 28108 * the drain lists and backenable the first conn in each list. 28109 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 28110 * qenabled non-tcp upper layers. We start dequeing messages and call 28111 * ip_wput for each message. 28112 */ 28113 28114 void 28115 ip_wsrv(queue_t *q) 28116 { 28117 conn_t *connp; 28118 ill_t *ill; 28119 mblk_t *mp; 28120 28121 if (q->q_next) { 28122 ill = (ill_t *)q->q_ptr; 28123 if (ill->ill_state_flags == 0) { 28124 /* 28125 * The device flow control has opened up. 28126 * Walk through conn drain lists and qenable the 28127 * first conn in each list. This makes sense only 28128 * if the stream is fully plumbed and setup. 28129 * Hence the if check above. 28130 */ 28131 ip1dbg(("ip_wsrv: walking\n")); 28132 conn_walk_drain(); 28133 } 28134 return; 28135 } 28136 28137 connp = Q_TO_CONN(q); 28138 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 28139 28140 /* 28141 * 1. Set conn_draining flag to signal that service is active. 28142 * 28143 * 2. ip_output determines whether it has been called from service, 28144 * based on the last parameter. If it is IP_WSRV it concludes it 28145 * has been called from service. 28146 * 28147 * 3. Message ordering is preserved by the following logic. 28148 * i. A directly called ip_output (i.e. not thru service) will queue 28149 * the message at the tail, if conn_draining is set (i.e. service 28150 * is running) or if q->q_first is non-null. 28151 * 28152 * ii. If ip_output is called from service, and if ip_output cannot 28153 * putnext due to flow control, it does a putbq. 28154 * 28155 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 28156 * (causing an infinite loop). 28157 */ 28158 ASSERT(!connp->conn_did_putbq); 28159 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 28160 connp->conn_draining = 1; 28161 noenable(q); 28162 while ((mp = getq(q)) != NULL) { 28163 ASSERT(CONN_Q(q)); 28164 28165 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 28166 if (connp->conn_did_putbq) { 28167 /* ip_wput did a putbq */ 28168 break; 28169 } 28170 } 28171 /* 28172 * At this point, a thread coming down from top, calling 28173 * ip_wput, may end up queueing the message. We have not yet 28174 * enabled the queue, so ip_wsrv won't be called again. 28175 * To avoid this race, check q->q_first again (in the loop) 28176 * If the other thread queued the message before we call 28177 * enableok(), we will catch it in the q->q_first check. 28178 * If the other thread queues the message after we call 28179 * enableok(), ip_wsrv will be called again by STREAMS. 28180 */ 28181 connp->conn_draining = 0; 28182 enableok(q); 28183 } 28184 28185 /* Enable the next conn for draining */ 28186 conn_drain_tail(connp, B_FALSE); 28187 28188 connp->conn_did_putbq = 0; 28189 } 28190 28191 /* 28192 * Walk the list of all conn's calling the function provided with the 28193 * specified argument for each. Note that this only walks conn's that 28194 * have been bound. 28195 * Applies to both IPv4 and IPv6. 28196 */ 28197 static void 28198 conn_walk_fanout(pfv_t func, void *arg, zoneid_t zoneid) 28199 { 28200 conn_walk_fanout_table(ipcl_udp_fanout, ipcl_udp_fanout_size, 28201 func, arg, zoneid); 28202 conn_walk_fanout_table(ipcl_conn_fanout, ipcl_conn_fanout_size, 28203 func, arg, zoneid); 28204 conn_walk_fanout_table(ipcl_bind_fanout, ipcl_bind_fanout_size, 28205 func, arg, zoneid); 28206 conn_walk_fanout_table(ipcl_proto_fanout, 28207 A_CNT(ipcl_proto_fanout), func, arg, zoneid); 28208 conn_walk_fanout_table(ipcl_proto_fanout_v6, 28209 A_CNT(ipcl_proto_fanout_v6), func, arg, zoneid); 28210 } 28211 28212 /* 28213 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 28214 * of conns that need to be drained, check if drain is already in progress. 28215 * If so set the idl_repeat bit, indicating that the last conn in the list 28216 * needs to reinitiate the drain once again, for the list. If drain is not 28217 * in progress for the list, initiate the draining, by qenabling the 1st 28218 * conn in the list. The drain is self-sustaining, each qenabled conn will 28219 * in turn qenable the next conn, when it is done/blocked/closing. 28220 */ 28221 static void 28222 conn_walk_drain(void) 28223 { 28224 int i; 28225 idl_t *idl; 28226 28227 IP_STAT(ip_conn_walk_drain); 28228 28229 for (i = 0; i < conn_drain_list_cnt; i++) { 28230 idl = &conn_drain_list[i]; 28231 mutex_enter(&idl->idl_lock); 28232 if (idl->idl_conn == NULL) { 28233 mutex_exit(&idl->idl_lock); 28234 continue; 28235 } 28236 /* 28237 * If this list is not being drained currently by 28238 * an ip_wsrv thread, start the process. 28239 */ 28240 if (idl->idl_conn_draining == NULL) { 28241 ASSERT(idl->idl_repeat == 0); 28242 qenable(idl->idl_conn->conn_wq); 28243 idl->idl_conn_draining = idl->idl_conn; 28244 } else { 28245 idl->idl_repeat = 1; 28246 } 28247 mutex_exit(&idl->idl_lock); 28248 } 28249 } 28250 28251 /* 28252 * Walk an conn hash table of `count' buckets, calling func for each entry. 28253 */ 28254 static void 28255 conn_walk_fanout_table(connf_t *connfp, uint_t count, pfv_t func, void *arg, 28256 zoneid_t zoneid) 28257 { 28258 conn_t *connp; 28259 28260 while (count-- > 0) { 28261 mutex_enter(&connfp->connf_lock); 28262 for (connp = connfp->connf_head; connp != NULL; 28263 connp = connp->conn_next) { 28264 if (zoneid == GLOBAL_ZONEID || 28265 zoneid == connp->conn_zoneid) { 28266 CONN_INC_REF(connp); 28267 mutex_exit(&connfp->connf_lock); 28268 (*func)(connp, arg); 28269 mutex_enter(&connfp->connf_lock); 28270 CONN_DEC_REF(connp); 28271 } 28272 } 28273 mutex_exit(&connfp->connf_lock); 28274 connfp++; 28275 } 28276 } 28277 28278 /* ipcl_walk routine invoked for ip_conn_report for each conn. */ 28279 static void 28280 conn_report1(conn_t *connp, void *mp) 28281 { 28282 char buf1[INET6_ADDRSTRLEN]; 28283 char buf2[INET6_ADDRSTRLEN]; 28284 uint_t print_len, buf_len; 28285 28286 ASSERT(connp != NULL); 28287 28288 buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr; 28289 if (buf_len <= 0) 28290 return; 28291 (void) inet_ntop(AF_INET6, &connp->conn_srcv6, buf1, sizeof (buf1)), 28292 (void) inet_ntop(AF_INET6, &connp->conn_remv6, buf2, sizeof (buf2)), 28293 print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len, 28294 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 28295 "%5d %s/%05d %s/%05d\n", 28296 (void *)connp, (void *)CONNP_TO_RQ(connp), 28297 (void *)CONNP_TO_WQ(connp), connp->conn_zoneid, 28298 buf1, connp->conn_lport, 28299 buf2, connp->conn_fport); 28300 if (print_len < buf_len) { 28301 ((mblk_t *)mp)->b_wptr += print_len; 28302 } else { 28303 ((mblk_t *)mp)->b_wptr += buf_len; 28304 } 28305 } 28306 28307 /* 28308 * Named Dispatch routine to produce a formatted report on all conns 28309 * that are listed in one of the fanout tables. 28310 * This report is accessed by using the ndd utility to "get" ND variable 28311 * "ip_conn_status". 28312 */ 28313 /* ARGSUSED */ 28314 static int 28315 ip_conn_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 28316 { 28317 (void) mi_mpprintf(mp, 28318 "CONN " MI_COL_HDRPAD_STR 28319 "rfq " MI_COL_HDRPAD_STR 28320 "stq " MI_COL_HDRPAD_STR 28321 " zone local remote"); 28322 28323 /* 28324 * Because of the ndd constraint, at most we can have 64K buffer 28325 * to put in all conn info. So to be more efficient, just 28326 * allocate a 64K buffer here, assuming we need that large buffer. 28327 * This should be OK as only privileged processes can do ndd /dev/ip. 28328 */ 28329 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 28330 /* The following may work even if we cannot get a large buf. */ 28331 (void) mi_mpprintf(mp, "<< Out of buffer >>\n"); 28332 return (0); 28333 } 28334 28335 conn_walk_fanout(conn_report1, mp->b_cont, Q_TO_CONN(q)->conn_zoneid); 28336 return (0); 28337 } 28338 28339 /* 28340 * Determine if the ill and multicast aspects of that packets 28341 * "matches" the conn. 28342 */ 28343 boolean_t 28344 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 28345 zoneid_t zoneid) 28346 { 28347 ill_t *in_ill; 28348 boolean_t found; 28349 ipif_t *ipif; 28350 ire_t *ire; 28351 ipaddr_t dst, src; 28352 28353 dst = ipha->ipha_dst; 28354 src = ipha->ipha_src; 28355 28356 /* 28357 * conn_incoming_ill is set by IP_BOUND_IF which limits 28358 * unicast, broadcast and multicast reception to 28359 * conn_incoming_ill. conn_wantpacket itself is called 28360 * only for BROADCAST and multicast. 28361 * 28362 * 1) ip_rput supresses duplicate broadcasts if the ill 28363 * is part of a group. Hence, we should be receiving 28364 * just one copy of broadcast for the whole group. 28365 * Thus, if it is part of the group the packet could 28366 * come on any ill of the group and hence we need a 28367 * match on the group. Otherwise, match on ill should 28368 * be sufficient. 28369 * 28370 * 2) ip_rput does not suppress duplicate multicast packets. 28371 * If there are two interfaces in a ill group and we have 28372 * 2 applications (conns) joined a multicast group G on 28373 * both the interfaces, ilm_lookup_ill filter in ip_rput 28374 * will give us two packets because we join G on both the 28375 * interfaces rather than nominating just one interface 28376 * for receiving multicast like broadcast above. So, 28377 * we have to call ilg_lookup_ill to filter out duplicate 28378 * copies, if ill is part of a group. 28379 */ 28380 in_ill = connp->conn_incoming_ill; 28381 if (in_ill != NULL) { 28382 if (in_ill->ill_group == NULL) { 28383 if (in_ill != ill) 28384 return (B_FALSE); 28385 } else if (in_ill->ill_group != ill->ill_group) { 28386 return (B_FALSE); 28387 } 28388 } 28389 28390 if (!CLASSD(dst)) { 28391 if (IPCL_ZONE_MATCH(connp, zoneid)) 28392 return (B_TRUE); 28393 /* 28394 * The conn is in a different zone; we need to check that this 28395 * broadcast address is configured in the application's zone and 28396 * on one ill in the group. 28397 */ 28398 ipif = ipif_get_next_ipif(NULL, ill); 28399 if (ipif == NULL) 28400 return (B_FALSE); 28401 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 28402 connp->conn_zoneid, NULL, 28403 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP)); 28404 ipif_refrele(ipif); 28405 if (ire != NULL) { 28406 ire_refrele(ire); 28407 return (B_TRUE); 28408 } else { 28409 return (B_FALSE); 28410 } 28411 } 28412 28413 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 28414 connp->conn_zoneid == zoneid) { 28415 /* 28416 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 28417 * disabled, therefore we don't dispatch the multicast packet to 28418 * the sending zone. 28419 */ 28420 return (B_FALSE); 28421 } 28422 28423 if ((ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) && 28424 connp->conn_zoneid != zoneid) { 28425 /* 28426 * Multicast packet on the loopback interface: we only match 28427 * conns who joined the group in the specified zone. 28428 */ 28429 return (B_FALSE); 28430 } 28431 28432 if (connp->conn_multi_router) { 28433 /* multicast packet and multicast router socket: send up */ 28434 return (B_TRUE); 28435 } 28436 28437 mutex_enter(&connp->conn_lock); 28438 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 28439 mutex_exit(&connp->conn_lock); 28440 return (found); 28441 } 28442 28443 /* 28444 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 28445 */ 28446 /* ARGSUSED */ 28447 static void 28448 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 28449 { 28450 ill_t *ill = (ill_t *)q->q_ptr; 28451 mblk_t *mp1, *mp2; 28452 ipif_t *ipif; 28453 int err = 0; 28454 conn_t *connp = NULL; 28455 ipsq_t *ipsq; 28456 arc_t *arc; 28457 28458 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 28459 28460 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 28461 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 28462 28463 ASSERT(IAM_WRITER_ILL(ill)); 28464 mp2 = mp->b_cont; 28465 mp->b_cont = NULL; 28466 28467 /* 28468 * We have now received the arp bringup completion message 28469 * from ARP. Mark the arp bringup as done. Also if the arp 28470 * stream has already started closing, send up the AR_ARP_CLOSING 28471 * ack now since ARP is waiting in close for this ack. 28472 */ 28473 mutex_enter(&ill->ill_lock); 28474 ill->ill_arp_bringup_pending = 0; 28475 if (ill->ill_arp_closing) { 28476 mutex_exit(&ill->ill_lock); 28477 /* Let's reuse the mp for sending the ack */ 28478 arc = (arc_t *)mp->b_rptr; 28479 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 28480 arc->arc_cmd = AR_ARP_CLOSING; 28481 qreply(q, mp); 28482 } else { 28483 mutex_exit(&ill->ill_lock); 28484 freeb(mp); 28485 } 28486 28487 ipsq = ill->ill_phyint->phyint_ipsq; 28488 ipif = ipsq->ipsq_pending_ipif; 28489 mp1 = ipsq_pending_mp_get(ipsq, &connp); 28490 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 28491 if (mp1 == NULL) { 28492 /* bringup was aborted by the user */ 28493 freemsg(mp2); 28494 return; 28495 } 28496 28497 /* 28498 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we 28499 * must have an associated conn_t. Otherwise, we're bringing this 28500 * interface back up as part of handling an asynchronous event (e.g., 28501 * physical address change). 28502 */ 28503 if (ipsq->ipsq_current_ioctl != 0) { 28504 ASSERT(connp != NULL); 28505 q = CONNP_TO_WQ(connp); 28506 } else { 28507 ASSERT(connp == NULL); 28508 q = ill->ill_rq; 28509 } 28510 28511 /* 28512 * If the DL_BIND_REQ fails, it is noted 28513 * in arc_name_offset. 28514 */ 28515 err = *((int *)mp2->b_rptr); 28516 if (err == 0) { 28517 if (ipif->ipif_isv6) { 28518 if ((err = ipif_up_done_v6(ipif)) != 0) 28519 ip0dbg(("ip_arp_done: init failed\n")); 28520 } else { 28521 if ((err = ipif_up_done(ipif)) != 0) 28522 ip0dbg(("ip_arp_done: init failed\n")); 28523 } 28524 } else { 28525 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 28526 } 28527 28528 freemsg(mp2); 28529 28530 if ((err == 0) && (ill->ill_up_ipifs)) { 28531 err = ill_up_ipifs(ill, q, mp1); 28532 if (err == EINPROGRESS) 28533 return; 28534 } 28535 28536 if (ill->ill_up_ipifs) 28537 ill_group_cleanup(ill); 28538 28539 /* 28540 * The operation must complete without EINPROGRESS since 28541 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp. 28542 * Otherwise, the operation will be stuck forever in the ipsq. 28543 */ 28544 ASSERT(err != EINPROGRESS); 28545 if (ipsq->ipsq_current_ioctl != 0) 28546 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 28547 else 28548 ipsq_current_finish(ipsq); 28549 } 28550 28551 /* Allocate the private structure */ 28552 static int 28553 ip_priv_alloc(void **bufp) 28554 { 28555 void *buf; 28556 28557 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 28558 return (ENOMEM); 28559 28560 *bufp = buf; 28561 return (0); 28562 } 28563 28564 /* Function to delete the private structure */ 28565 void 28566 ip_priv_free(void *buf) 28567 { 28568 ASSERT(buf != NULL); 28569 kmem_free(buf, sizeof (ip_priv_t)); 28570 } 28571 28572 /* 28573 * The entry point for IPPF processing. 28574 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 28575 * routine just returns. 28576 * 28577 * When called, ip_process generates an ipp_packet_t structure 28578 * which holds the state information for this packet and invokes the 28579 * the classifier (via ipp_packet_process). The classification, depending on 28580 * configured filters, results in a list of actions for this packet. Invoking 28581 * an action may cause the packet to be dropped, in which case the resulting 28582 * mblk (*mpp) is NULL. proc indicates the callout position for 28583 * this packet and ill_index is the interface this packet on or will leave 28584 * on (inbound and outbound resp.). 28585 */ 28586 void 28587 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 28588 { 28589 mblk_t *mp; 28590 ip_priv_t *priv; 28591 ipp_action_id_t aid; 28592 int rc = 0; 28593 ipp_packet_t *pp; 28594 #define IP_CLASS "ip" 28595 28596 /* If the classifier is not loaded, return */ 28597 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 28598 return; 28599 } 28600 28601 mp = *mpp; 28602 ASSERT(mp != NULL); 28603 28604 /* Allocate the packet structure */ 28605 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 28606 if (rc != 0) { 28607 *mpp = NULL; 28608 freemsg(mp); 28609 return; 28610 } 28611 28612 /* Allocate the private structure */ 28613 rc = ip_priv_alloc((void **)&priv); 28614 if (rc != 0) { 28615 *mpp = NULL; 28616 freemsg(mp); 28617 ipp_packet_free(pp); 28618 return; 28619 } 28620 priv->proc = proc; 28621 priv->ill_index = ill_index; 28622 ipp_packet_set_private(pp, priv, ip_priv_free); 28623 ipp_packet_set_data(pp, mp); 28624 28625 /* Invoke the classifier */ 28626 rc = ipp_packet_process(&pp); 28627 if (pp != NULL) { 28628 mp = ipp_packet_get_data(pp); 28629 ipp_packet_free(pp); 28630 if (rc != 0) { 28631 freemsg(mp); 28632 *mpp = NULL; 28633 } 28634 } else { 28635 *mpp = NULL; 28636 } 28637 #undef IP_CLASS 28638 } 28639 28640 /* 28641 * Propagate a multicast group membership operation (add/drop) on 28642 * all the interfaces crossed by the related multirt routes. 28643 * The call is considered successful if the operation succeeds 28644 * on at least one interface. 28645 */ 28646 static int 28647 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 28648 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 28649 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 28650 mblk_t *first_mp) 28651 { 28652 ire_t *ire_gw; 28653 irb_t *irb; 28654 int error = 0; 28655 opt_restart_t *or; 28656 28657 irb = ire->ire_bucket; 28658 ASSERT(irb != NULL); 28659 28660 ASSERT(DB_TYPE(first_mp) == M_CTL); 28661 28662 or = (opt_restart_t *)first_mp->b_rptr; 28663 IRB_REFHOLD(irb); 28664 for (; ire != NULL; ire = ire->ire_next) { 28665 if ((ire->ire_flags & RTF_MULTIRT) == 0) 28666 continue; 28667 if (ire->ire_addr != group) 28668 continue; 28669 28670 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 28671 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 28672 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE); 28673 /* No resolver exists for the gateway; skip this ire. */ 28674 if (ire_gw == NULL) 28675 continue; 28676 28677 /* 28678 * This function can return EINPROGRESS. If so the operation 28679 * will be restarted from ip_restart_optmgmt which will 28680 * call ip_opt_set and option processing will restart for 28681 * this option. So we may end up calling 'fn' more than once. 28682 * This requires that 'fn' is idempotent except for the 28683 * return value. The operation is considered a success if 28684 * it succeeds at least once on any one interface. 28685 */ 28686 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 28687 NULL, fmode, src, first_mp); 28688 if (error == 0) 28689 or->or_private = CGTP_MCAST_SUCCESS; 28690 28691 if (ip_debug > 0) { 28692 ulong_t off; 28693 char *ksym; 28694 ksym = kobj_getsymname((uintptr_t)fn, &off); 28695 ip2dbg(("ip_multirt_apply_membership: " 28696 "called %s, multirt group 0x%08x via itf 0x%08x, " 28697 "error %d [success %u]\n", 28698 ksym ? ksym : "?", 28699 ntohl(group), ntohl(ire_gw->ire_src_addr), 28700 error, or->or_private)); 28701 } 28702 28703 ire_refrele(ire_gw); 28704 if (error == EINPROGRESS) { 28705 IRB_REFRELE(irb); 28706 return (error); 28707 } 28708 } 28709 IRB_REFRELE(irb); 28710 /* 28711 * Consider the call as successful if we succeeded on at least 28712 * one interface. Otherwise, return the last encountered error. 28713 */ 28714 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 28715 } 28716 28717 28718 /* 28719 * Issue a warning regarding a route crossing an interface with an 28720 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 28721 * amount of time is logged. 28722 */ 28723 static void 28724 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 28725 { 28726 hrtime_t current = gethrtime(); 28727 char buf[INET_ADDRSTRLEN]; 28728 28729 /* Convert interval in ms to hrtime in ns */ 28730 if (multirt_bad_mtu_last_time + 28731 ((hrtime_t)ip_multirt_log_interval * (hrtime_t)1000000) <= 28732 current) { 28733 cmn_err(CE_WARN, "ip: ignoring multiroute " 28734 "to %s, incorrect MTU %u (expected %u)\n", 28735 ip_dot_addr(ire->ire_addr, buf), 28736 ire->ire_max_frag, max_frag); 28737 28738 multirt_bad_mtu_last_time = current; 28739 } 28740 } 28741 28742 28743 /* 28744 * Get the CGTP (multirouting) filtering status. 28745 * If 0, the CGTP hooks are transparent. 28746 */ 28747 /* ARGSUSED */ 28748 static int 28749 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 28750 { 28751 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28752 28753 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 28754 return (0); 28755 } 28756 28757 28758 /* 28759 * Set the CGTP (multirouting) filtering status. 28760 * If the status is changed from active to transparent 28761 * or from transparent to active, forward the new status 28762 * to the filtering module (if loaded). 28763 */ 28764 /* ARGSUSED */ 28765 static int 28766 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 28767 cred_t *ioc_cr) 28768 { 28769 long new_value; 28770 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 28771 28772 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 28773 new_value < 0 || new_value > 1) { 28774 return (EINVAL); 28775 } 28776 28777 /* 28778 * Do not enable CGTP filtering - thus preventing the hooks 28779 * from being invoked - if the version number of the 28780 * filtering module hooks does not match. 28781 */ 28782 if ((ip_cgtp_filter_ops != NULL) && 28783 (ip_cgtp_filter_ops->cfo_filter_rev != CGTP_FILTER_REV)) { 28784 cmn_err(CE_WARN, "IP: CGTP filtering version mismatch " 28785 "(module hooks version %d, expecting %d)\n", 28786 ip_cgtp_filter_ops->cfo_filter_rev, CGTP_FILTER_REV); 28787 return (ENOTSUP); 28788 } 28789 28790 if ((!*ip_cgtp_filter_value) && new_value) { 28791 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 28792 ip_cgtp_filter_ops == NULL ? 28793 " (module not loaded)" : ""); 28794 } 28795 if (*ip_cgtp_filter_value && (!new_value)) { 28796 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 28797 ip_cgtp_filter_ops == NULL ? 28798 " (module not loaded)" : ""); 28799 } 28800 28801 if (ip_cgtp_filter_ops != NULL) { 28802 int res; 28803 if ((res = ip_cgtp_filter_ops->cfo_change_state(new_value))) { 28804 return (res); 28805 } 28806 } 28807 28808 *ip_cgtp_filter_value = (boolean_t)new_value; 28809 28810 return (0); 28811 } 28812 28813 28814 /* 28815 * Return the expected CGTP hooks version number. 28816 */ 28817 int 28818 ip_cgtp_filter_supported(void) 28819 { 28820 return (ip_cgtp_filter_rev); 28821 } 28822 28823 28824 /* 28825 * CGTP hooks can be registered by directly touching ip_cgtp_filter_ops 28826 * or by invoking this function. In the first case, the version number 28827 * of the registered structure is checked at hooks activation time 28828 * in ip_cgtp_filter_set(). 28829 */ 28830 int 28831 ip_cgtp_filter_register(cgtp_filter_ops_t *ops) 28832 { 28833 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 28834 return (ENOTSUP); 28835 28836 ip_cgtp_filter_ops = ops; 28837 return (0); 28838 } 28839 28840 static squeue_func_t 28841 ip_squeue_switch(int val) 28842 { 28843 squeue_func_t rval = squeue_fill; 28844 28845 switch (val) { 28846 case IP_SQUEUE_ENTER_NODRAIN: 28847 rval = squeue_enter_nodrain; 28848 break; 28849 case IP_SQUEUE_ENTER: 28850 rval = squeue_enter; 28851 break; 28852 default: 28853 break; 28854 } 28855 return (rval); 28856 } 28857 28858 /* ARGSUSED */ 28859 static int 28860 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 28861 caddr_t addr, cred_t *cr) 28862 { 28863 int *v = (int *)addr; 28864 long new_value; 28865 28866 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 28867 return (EINVAL); 28868 28869 ip_input_proc = ip_squeue_switch(new_value); 28870 *v = new_value; 28871 return (0); 28872 } 28873 28874 /* ARGSUSED */ 28875 static int 28876 ip_int_set(queue_t *q, mblk_t *mp, char *value, 28877 caddr_t addr, cred_t *cr) 28878 { 28879 int *v = (int *)addr; 28880 long new_value; 28881 28882 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 28883 return (EINVAL); 28884 28885 *v = new_value; 28886 return (0); 28887 } 28888 28889 static void 28890 ip_kstat_init(void) 28891 { 28892 ip_named_kstat_t template = { 28893 { "forwarding", KSTAT_DATA_UINT32, 0 }, 28894 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 28895 { "inReceives", KSTAT_DATA_UINT64, 0 }, 28896 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 28897 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 28898 { "forwDatagrams", KSTAT_DATA_UINT64, 0 }, 28899 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 28900 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 28901 { "inDelivers", KSTAT_DATA_UINT64, 0 }, 28902 { "outRequests", KSTAT_DATA_UINT64, 0 }, 28903 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 28904 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 28905 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 28906 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 28907 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 28908 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 28909 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 28910 { "fragFails", KSTAT_DATA_UINT32, 0 }, 28911 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 28912 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 28913 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 28914 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 28915 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 28916 { "inErrs", KSTAT_DATA_UINT32, 0 }, 28917 { "noPorts", KSTAT_DATA_UINT32, 0 }, 28918 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 28919 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 28920 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 28921 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 28922 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 28923 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 28924 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 28925 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 28926 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 28927 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 28928 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 28929 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 28930 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 28931 }; 28932 28933 ip_mibkp = kstat_create("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 28934 NUM_OF_FIELDS(ip_named_kstat_t), 28935 0); 28936 if (!ip_mibkp) 28937 return; 28938 28939 template.forwarding.value.ui32 = WE_ARE_FORWARDING ? 1:2; 28940 template.defaultTTL.value.ui32 = (uint32_t)ip_def_ttl; 28941 template.reasmTimeout.value.ui32 = ip_g_frag_timeout; 28942 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 28943 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 28944 28945 template.netToMediaEntrySize.value.i32 = 28946 sizeof (mib2_ipNetToMediaEntry_t); 28947 28948 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 28949 28950 bcopy(&template, ip_mibkp->ks_data, sizeof (template)); 28951 28952 ip_mibkp->ks_update = ip_kstat_update; 28953 28954 kstat_install(ip_mibkp); 28955 } 28956 28957 static void 28958 ip_kstat_fini(void) 28959 { 28960 28961 if (ip_mibkp != NULL) { 28962 kstat_delete(ip_mibkp); 28963 ip_mibkp = NULL; 28964 } 28965 } 28966 28967 static int 28968 ip_kstat_update(kstat_t *kp, int rw) 28969 { 28970 ip_named_kstat_t *ipkp; 28971 mib2_ipIfStatsEntry_t ipmib; 28972 ill_walk_context_t ctx; 28973 ill_t *ill; 28974 28975 if (!kp || !kp->ks_data) 28976 return (EIO); 28977 28978 if (rw == KSTAT_WRITE) 28979 return (EACCES); 28980 28981 ipkp = (ip_named_kstat_t *)kp->ks_data; 28982 28983 bcopy(&ip_mib, &ipmib, sizeof (ipmib)); 28984 rw_enter(&ill_g_lock, RW_READER); 28985 ill = ILL_START_WALK_V4(&ctx); 28986 for (; ill != NULL; ill = ill_next(&ctx, ill)) 28987 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib); 28988 rw_exit(&ill_g_lock); 28989 28990 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding; 28991 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL; 28992 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives; 28993 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors; 28994 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors; 28995 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams; 28996 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos; 28997 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards; 28998 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers; 28999 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests; 29000 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards; 29001 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes; 29002 ipkp->reasmTimeout.value.ui32 = ip_g_frag_timeout; 29003 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds; 29004 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs; 29005 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails; 29006 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs; 29007 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails; 29008 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates; 29009 29010 ipkp->routingDiscards.value.ui32 = 0; 29011 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs; 29012 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts; 29013 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs; 29014 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates; 29015 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups; 29016 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits; 29017 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs; 29018 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows; 29019 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows; 29020 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded; 29021 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed; 29022 29023 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion; 29024 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion; 29025 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion; 29026 29027 return (0); 29028 } 29029 29030 static void 29031 icmp_kstat_init(void) 29032 { 29033 icmp_named_kstat_t template = { 29034 { "inMsgs", KSTAT_DATA_UINT32 }, 29035 { "inErrors", KSTAT_DATA_UINT32 }, 29036 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 29037 { "inTimeExcds", KSTAT_DATA_UINT32 }, 29038 { "inParmProbs", KSTAT_DATA_UINT32 }, 29039 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 29040 { "inRedirects", KSTAT_DATA_UINT32 }, 29041 { "inEchos", KSTAT_DATA_UINT32 }, 29042 { "inEchoReps", KSTAT_DATA_UINT32 }, 29043 { "inTimestamps", KSTAT_DATA_UINT32 }, 29044 { "inTimestampReps", KSTAT_DATA_UINT32 }, 29045 { "inAddrMasks", KSTAT_DATA_UINT32 }, 29046 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 29047 { "outMsgs", KSTAT_DATA_UINT32 }, 29048 { "outErrors", KSTAT_DATA_UINT32 }, 29049 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 29050 { "outTimeExcds", KSTAT_DATA_UINT32 }, 29051 { "outParmProbs", KSTAT_DATA_UINT32 }, 29052 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 29053 { "outRedirects", KSTAT_DATA_UINT32 }, 29054 { "outEchos", KSTAT_DATA_UINT32 }, 29055 { "outEchoReps", KSTAT_DATA_UINT32 }, 29056 { "outTimestamps", KSTAT_DATA_UINT32 }, 29057 { "outTimestampReps", KSTAT_DATA_UINT32 }, 29058 { "outAddrMasks", KSTAT_DATA_UINT32 }, 29059 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 29060 { "inChksumErrs", KSTAT_DATA_UINT32 }, 29061 { "inUnknowns", KSTAT_DATA_UINT32 }, 29062 { "inFragNeeded", KSTAT_DATA_UINT32 }, 29063 { "outFragNeeded", KSTAT_DATA_UINT32 }, 29064 { "outDrops", KSTAT_DATA_UINT32 }, 29065 { "inOverFlows", KSTAT_DATA_UINT32 }, 29066 { "inBadRedirects", KSTAT_DATA_UINT32 }, 29067 }; 29068 29069 icmp_mibkp = kstat_create("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 29070 NUM_OF_FIELDS(icmp_named_kstat_t), 29071 0); 29072 if (icmp_mibkp == NULL) 29073 return; 29074 29075 bcopy(&template, icmp_mibkp->ks_data, sizeof (template)); 29076 29077 icmp_mibkp->ks_update = icmp_kstat_update; 29078 29079 kstat_install(icmp_mibkp); 29080 } 29081 29082 static void 29083 icmp_kstat_fini(void) 29084 { 29085 29086 if (icmp_mibkp != NULL) { 29087 kstat_delete(icmp_mibkp); 29088 icmp_mibkp = NULL; 29089 } 29090 } 29091 29092 static int 29093 icmp_kstat_update(kstat_t *kp, int rw) 29094 { 29095 icmp_named_kstat_t *icmpkp; 29096 29097 if ((kp == NULL) || (kp->ks_data == NULL)) 29098 return (EIO); 29099 29100 if (rw == KSTAT_WRITE) 29101 return (EACCES); 29102 29103 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 29104 29105 icmpkp->inMsgs.value.ui32 = icmp_mib.icmpInMsgs; 29106 icmpkp->inErrors.value.ui32 = icmp_mib.icmpInErrors; 29107 icmpkp->inDestUnreachs.value.ui32 = icmp_mib.icmpInDestUnreachs; 29108 icmpkp->inTimeExcds.value.ui32 = icmp_mib.icmpInTimeExcds; 29109 icmpkp->inParmProbs.value.ui32 = icmp_mib.icmpInParmProbs; 29110 icmpkp->inSrcQuenchs.value.ui32 = icmp_mib.icmpInSrcQuenchs; 29111 icmpkp->inRedirects.value.ui32 = icmp_mib.icmpInRedirects; 29112 icmpkp->inEchos.value.ui32 = icmp_mib.icmpInEchos; 29113 icmpkp->inEchoReps.value.ui32 = icmp_mib.icmpInEchoReps; 29114 icmpkp->inTimestamps.value.ui32 = icmp_mib.icmpInTimestamps; 29115 icmpkp->inTimestampReps.value.ui32 = icmp_mib.icmpInTimestampReps; 29116 icmpkp->inAddrMasks.value.ui32 = icmp_mib.icmpInAddrMasks; 29117 icmpkp->inAddrMaskReps.value.ui32 = icmp_mib.icmpInAddrMaskReps; 29118 icmpkp->outMsgs.value.ui32 = icmp_mib.icmpOutMsgs; 29119 icmpkp->outErrors.value.ui32 = icmp_mib.icmpOutErrors; 29120 icmpkp->outDestUnreachs.value.ui32 = icmp_mib.icmpOutDestUnreachs; 29121 icmpkp->outTimeExcds.value.ui32 = icmp_mib.icmpOutTimeExcds; 29122 icmpkp->outParmProbs.value.ui32 = icmp_mib.icmpOutParmProbs; 29123 icmpkp->outSrcQuenchs.value.ui32 = icmp_mib.icmpOutSrcQuenchs; 29124 icmpkp->outRedirects.value.ui32 = icmp_mib.icmpOutRedirects; 29125 icmpkp->outEchos.value.ui32 = icmp_mib.icmpOutEchos; 29126 icmpkp->outEchoReps.value.ui32 = icmp_mib.icmpOutEchoReps; 29127 icmpkp->outTimestamps.value.ui32 = icmp_mib.icmpOutTimestamps; 29128 icmpkp->outTimestampReps.value.ui32 = icmp_mib.icmpOutTimestampReps; 29129 icmpkp->outAddrMasks.value.ui32 = icmp_mib.icmpOutAddrMasks; 29130 icmpkp->outAddrMaskReps.value.ui32 = icmp_mib.icmpOutAddrMaskReps; 29131 icmpkp->inCksumErrs.value.ui32 = icmp_mib.icmpInCksumErrs; 29132 icmpkp->inUnknowns.value.ui32 = icmp_mib.icmpInUnknowns; 29133 icmpkp->inFragNeeded.value.ui32 = icmp_mib.icmpInFragNeeded; 29134 icmpkp->outFragNeeded.value.ui32 = icmp_mib.icmpOutFragNeeded; 29135 icmpkp->outDrops.value.ui32 = icmp_mib.icmpOutDrops; 29136 icmpkp->inOverflows.value.ui32 = icmp_mib.icmpInOverflows; 29137 icmpkp->inBadRedirects.value.ui32 = icmp_mib.icmpInBadRedirects; 29138 29139 return (0); 29140 } 29141 29142 /* 29143 * This is the fanout function for raw socket opened for SCTP. Note 29144 * that it is called after SCTP checks that there is no socket which 29145 * wants a packet. Then before SCTP handles this out of the blue packet, 29146 * this function is called to see if there is any raw socket for SCTP. 29147 * If there is and it is bound to the correct address, the packet will 29148 * be sent to that socket. Note that only one raw socket can be bound to 29149 * a port. This is assured in ipcl_sctp_hash_insert(); 29150 */ 29151 void 29152 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 29153 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 29154 uint_t ipif_seqid, zoneid_t zoneid) 29155 { 29156 conn_t *connp; 29157 queue_t *rq; 29158 mblk_t *first_mp; 29159 boolean_t secure; 29160 ip6_t *ip6h; 29161 29162 first_mp = mp; 29163 if (mctl_present) { 29164 mp = first_mp->b_cont; 29165 secure = ipsec_in_is_secure(first_mp); 29166 ASSERT(mp != NULL); 29167 } else { 29168 secure = B_FALSE; 29169 } 29170 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 29171 29172 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha); 29173 if (connp == NULL) { 29174 sctp_ootb_input(first_mp, recv_ill, ipif_seqid, zoneid, 29175 mctl_present); 29176 return; 29177 } 29178 rq = connp->conn_rq; 29179 if (!canputnext(rq)) { 29180 CONN_DEC_REF(connp); 29181 BUMP_MIB(recv_ill->ill_ip_mib, rawipIfStatsInOverflows); 29182 freemsg(first_mp); 29183 return; 29184 } 29185 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp) : 29186 CONN_INBOUND_POLICY_PRESENT_V6(connp)) || secure) { 29187 first_mp = ipsec_check_inbound_policy(first_mp, connp, 29188 (isv4 ? ipha : NULL), ip6h, mctl_present); 29189 if (first_mp == NULL) { 29190 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); 29191 CONN_DEC_REF(connp); 29192 return; 29193 } 29194 } 29195 /* 29196 * We probably should not send M_CTL message up to 29197 * raw socket. 29198 */ 29199 if (mctl_present) 29200 freeb(first_mp); 29201 29202 /* Initiate IPPF processing here if needed. */ 29203 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) || 29204 (!isv4 && IP6_IN_IPP(flags))) { 29205 ip_process(IPP_LOCAL_IN, &mp, 29206 recv_ill->ill_phyint->phyint_ifindex); 29207 if (mp == NULL) { 29208 CONN_DEC_REF(connp); 29209 return; 29210 } 29211 } 29212 29213 if (connp->conn_recvif || connp->conn_recvslla || 29214 ((connp->conn_ip_recvpktinfo || 29215 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 29216 (flags & IP_FF_IPINFO))) { 29217 int in_flags = 0; 29218 29219 /* 29220 * Since sctp does not support IP_RECVPKTINFO for v4, only pass 29221 * IPF_RECVIF. 29222 */ 29223 if (connp->conn_recvif || connp->conn_ip_recvpktinfo) { 29224 in_flags = IPF_RECVIF; 29225 } 29226 if (connp->conn_recvslla) { 29227 in_flags |= IPF_RECVSLLA; 29228 } 29229 if (isv4) { 29230 mp = ip_add_info(mp, recv_ill, in_flags, 29231 IPCL_ZONEID(connp)); 29232 } else { 29233 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 29234 if (mp == NULL) { 29235 BUMP_MIB(recv_ill->ill_ip_mib, 29236 ipIfStatsInDiscards); 29237 CONN_DEC_REF(connp); 29238 return; 29239 } 29240 } 29241 } 29242 29243 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); 29244 /* 29245 * We are sending the IPSEC_IN message also up. Refer 29246 * to comments above this function. 29247 */ 29248 putnext(rq, mp); 29249 CONN_DEC_REF(connp); 29250 } 29251 29252 #define UPDATE_IP_MIB_OB_COUNTERS(ill, len) \ 29253 { \ 29254 BUMP_MIB((ill)->ill_ip_mib, ipIfStatsHCOutTransmits); \ 29255 UPDATE_MIB((ill)->ill_ip_mib, ipIfStatsHCOutOctets, (len)); \ 29256 } 29257 /* 29258 * This function should be called only if all packet processing 29259 * including fragmentation is complete. Callers of this function 29260 * must set mp->b_prev to one of these values: 29261 * {0, IPP_FWD_OUT, IPP_LOCAL_OUT} 29262 * prior to handing over the mp as first argument to this function. 29263 * 29264 * If the ire passed by caller is incomplete, this function 29265 * queues the packet and if necessary, sends ARP request and bails. 29266 * If the ire passed is fully resolved, we simply prepend 29267 * the link-layer header to the packet, do ipsec hw acceleration 29268 * work if necessary, and send the packet out on the wire. 29269 * 29270 * NOTE: IPSEC will only call this function with fully resolved 29271 * ires if hw acceleration is involved. 29272 * TODO list : 29273 * a Handle M_MULTIDATA so that 29274 * tcp_multisend->tcp_multisend_data can 29275 * call ip_xmit_v4 directly 29276 * b Handle post-ARP work for fragments so that 29277 * ip_wput_frag can call this function. 29278 */ 29279 ipxmit_state_t 29280 ip_xmit_v4(mblk_t *mp, ire_t *ire, ipsec_out_t *io, boolean_t flow_ctl_enabled) 29281 { 29282 nce_t *arpce; 29283 queue_t *q; 29284 int ill_index; 29285 mblk_t *nxt_mp, *first_mp; 29286 boolean_t xmit_drop = B_FALSE; 29287 ip_proc_t proc; 29288 ill_t *out_ill; 29289 int pkt_len; 29290 29291 arpce = ire->ire_nce; 29292 ASSERT(arpce != NULL); 29293 29294 DTRACE_PROBE2(ip__xmit__v4, ire_t *, ire, nce_t *, arpce); 29295 29296 mutex_enter(&arpce->nce_lock); 29297 switch (arpce->nce_state) { 29298 case ND_REACHABLE: 29299 /* If there are other queued packets, queue this packet */ 29300 if (arpce->nce_qd_mp != NULL) { 29301 if (mp != NULL) 29302 nce_queue_mp_common(arpce, mp, B_FALSE); 29303 mp = arpce->nce_qd_mp; 29304 } 29305 arpce->nce_qd_mp = NULL; 29306 mutex_exit(&arpce->nce_lock); 29307 29308 /* 29309 * Flush the queue. In the common case, where the 29310 * ARP is already resolved, it will go through the 29311 * while loop only once. 29312 */ 29313 while (mp != NULL) { 29314 29315 nxt_mp = mp->b_next; 29316 mp->b_next = NULL; 29317 ASSERT(mp->b_datap->db_type != M_CTL); 29318 pkt_len = ntohs(((ipha_t *)mp->b_rptr)->ipha_length); 29319 /* 29320 * This info is needed for IPQOS to do COS marking 29321 * in ip_wput_attach_llhdr->ip_process. 29322 */ 29323 proc = (ip_proc_t)(uintptr_t)mp->b_prev; 29324 mp->b_prev = NULL; 29325 29326 /* set up ill index for outbound qos processing */ 29327 out_ill = ire->ire_ipif->ipif_ill; 29328 ill_index = out_ill->ill_phyint->phyint_ifindex; 29329 first_mp = ip_wput_attach_llhdr(mp, ire, proc, 29330 ill_index); 29331 if (first_mp == NULL) { 29332 xmit_drop = B_TRUE; 29333 BUMP_MIB(out_ill->ill_ip_mib, 29334 ipIfStatsOutDiscards); 29335 goto next_mp; 29336 } 29337 /* non-ipsec hw accel case */ 29338 if (io == NULL || !io->ipsec_out_accelerated) { 29339 /* send it */ 29340 q = ire->ire_stq; 29341 if (proc == IPP_FWD_OUT) { 29342 UPDATE_IB_PKT_COUNT(ire); 29343 } else { 29344 UPDATE_OB_PKT_COUNT(ire); 29345 } 29346 ire->ire_last_used_time = lbolt; 29347 29348 if (flow_ctl_enabled || canputnext(q)) { 29349 if (proc == IPP_FWD_OUT) { 29350 BUMP_MIB(out_ill->ill_ip_mib, 29351 ipIfStatsHCOutForwDatagrams); 29352 } 29353 UPDATE_IP_MIB_OB_COUNTERS(out_ill, 29354 pkt_len); 29355 29356 putnext(q, first_mp); 29357 } else { 29358 BUMP_MIB(out_ill->ill_ip_mib, 29359 ipIfStatsOutDiscards); 29360 xmit_drop = B_TRUE; 29361 freemsg(first_mp); 29362 } 29363 } else { 29364 /* 29365 * Safety Pup says: make sure this 29366 * is going to the right interface! 29367 */ 29368 ill_t *ill1 = 29369 (ill_t *)ire->ire_stq->q_ptr; 29370 int ifindex = 29371 ill1->ill_phyint->phyint_ifindex; 29372 if (ifindex != 29373 io->ipsec_out_capab_ill_index) { 29374 xmit_drop = B_TRUE; 29375 freemsg(mp); 29376 } else { 29377 UPDATE_IP_MIB_OB_COUNTERS(ill1, 29378 pkt_len); 29379 ipsec_hw_putnext(ire->ire_stq, mp); 29380 } 29381 } 29382 next_mp: 29383 mp = nxt_mp; 29384 } /* while (mp != NULL) */ 29385 if (xmit_drop) 29386 return (SEND_FAILED); 29387 else 29388 return (SEND_PASSED); 29389 29390 case ND_INITIAL: 29391 case ND_INCOMPLETE: 29392 29393 /* 29394 * While we do send off packets to dests that 29395 * use fully-resolved CGTP routes, we do not 29396 * handle unresolved CGTP routes. 29397 */ 29398 ASSERT(!(ire->ire_flags & RTF_MULTIRT)); 29399 ASSERT(io == NULL || !io->ipsec_out_accelerated); 29400 29401 if (mp != NULL) { 29402 /* queue the packet */ 29403 nce_queue_mp_common(arpce, mp, B_FALSE); 29404 } 29405 29406 if (arpce->nce_state == ND_INCOMPLETE) { 29407 mutex_exit(&arpce->nce_lock); 29408 DTRACE_PROBE3(ip__xmit__incomplete, 29409 (ire_t *), ire, (mblk_t *), mp, 29410 (ipsec_out_t *), io); 29411 return (LOOKUP_IN_PROGRESS); 29412 } 29413 29414 arpce->nce_state = ND_INCOMPLETE; 29415 mutex_exit(&arpce->nce_lock); 29416 /* 29417 * Note that ire_add() (called from ire_forward()) 29418 * holds a ref on the ire until ARP is completed. 29419 */ 29420 29421 ire_arpresolve(ire, ire_to_ill(ire)); 29422 return (LOOKUP_IN_PROGRESS); 29423 default: 29424 ASSERT(0); 29425 mutex_exit(&arpce->nce_lock); 29426 return (LLHDR_RESLV_FAILED); 29427 } 29428 } 29429 29430 #undef UPDATE_IP_MIB_OB_COUNTERS 29431 29432 /* 29433 * Return B_TRUE if the buffers differ in length or content. 29434 * This is used for comparing extension header buffers. 29435 * Note that an extension header would be declared different 29436 * even if all that changed was the next header value in that header i.e. 29437 * what really changed is the next extension header. 29438 */ 29439 boolean_t 29440 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 29441 uint_t blen) 29442 { 29443 if (!b_valid) 29444 blen = 0; 29445 29446 if (alen != blen) 29447 return (B_TRUE); 29448 if (alen == 0) 29449 return (B_FALSE); /* Both zero length */ 29450 return (bcmp(abuf, bbuf, alen)); 29451 } 29452 29453 /* 29454 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 29455 * Return B_FALSE if memory allocation fails - don't change any state! 29456 */ 29457 boolean_t 29458 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29459 const void *src, uint_t srclen) 29460 { 29461 void *dst; 29462 29463 if (!src_valid) 29464 srclen = 0; 29465 29466 ASSERT(*dstlenp == 0); 29467 if (src != NULL && srclen != 0) { 29468 dst = mi_alloc(srclen, BPRI_MED); 29469 if (dst == NULL) 29470 return (B_FALSE); 29471 } else { 29472 dst = NULL; 29473 } 29474 if (*dstp != NULL) 29475 mi_free(*dstp); 29476 *dstp = dst; 29477 *dstlenp = dst == NULL ? 0 : srclen; 29478 return (B_TRUE); 29479 } 29480 29481 /* 29482 * Replace what is in *dst, *dstlen with the source. 29483 * Assumes ip_allocbuf has already been called. 29484 */ 29485 void 29486 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 29487 const void *src, uint_t srclen) 29488 { 29489 if (!src_valid) 29490 srclen = 0; 29491 29492 ASSERT(*dstlenp == srclen); 29493 if (src != NULL && srclen != 0) 29494 bcopy(src, *dstp, srclen); 29495 } 29496 29497 /* 29498 * Free the storage pointed to by the members of an ip6_pkt_t. 29499 */ 29500 void 29501 ip6_pkt_free(ip6_pkt_t *ipp) 29502 { 29503 ASSERT(ipp->ipp_pathmtu == NULL && !(ipp->ipp_fields & IPPF_PATHMTU)); 29504 29505 if (ipp->ipp_fields & IPPF_HOPOPTS) { 29506 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 29507 ipp->ipp_hopopts = NULL; 29508 ipp->ipp_hopoptslen = 0; 29509 } 29510 if (ipp->ipp_fields & IPPF_RTDSTOPTS) { 29511 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 29512 ipp->ipp_rtdstopts = NULL; 29513 ipp->ipp_rtdstoptslen = 0; 29514 } 29515 if (ipp->ipp_fields & IPPF_DSTOPTS) { 29516 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 29517 ipp->ipp_dstopts = NULL; 29518 ipp->ipp_dstoptslen = 0; 29519 } 29520 if (ipp->ipp_fields & IPPF_RTHDR) { 29521 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 29522 ipp->ipp_rthdr = NULL; 29523 ipp->ipp_rthdrlen = 0; 29524 } 29525 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 29526 IPPF_RTHDR); 29527 } 29528