1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* Copyright (c) 1990 Mentat Inc. */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/stream.h> 31 #include <sys/dlpi.h> 32 #include <sys/stropts.h> 33 #include <sys/sysmacros.h> 34 #include <sys/strsubr.h> 35 #include <sys/strlog.h> 36 #include <sys/strsun.h> 37 #include <sys/zone.h> 38 #define _SUN_TPI_VERSION 2 39 #include <sys/tihdr.h> 40 #include <sys/xti_inet.h> 41 #include <sys/ddi.h> 42 #include <sys/sunddi.h> 43 #include <sys/cmn_err.h> 44 #include <sys/debug.h> 45 #include <sys/kobj.h> 46 #include <sys/modctl.h> 47 #include <sys/atomic.h> 48 #include <sys/policy.h> 49 50 #include <sys/systm.h> 51 #include <sys/param.h> 52 #include <sys/kmem.h> 53 #include <sys/socket.h> 54 #include <sys/vtrace.h> 55 #include <sys/isa_defs.h> 56 #include <net/if.h> 57 #include <net/if_arp.h> 58 #include <net/route.h> 59 #include <sys/sockio.h> 60 #include <netinet/in.h> 61 #include <net/if_dl.h> 62 63 #include <inet/common.h> 64 #include <inet/mi.h> 65 #include <inet/mib2.h> 66 #include <inet/nd.h> 67 #include <inet/arp.h> 68 #include <inet/snmpcom.h> 69 #include <inet/kstatcom.h> 70 71 #include <netinet/igmp_var.h> 72 #include <netinet/ip6.h> 73 #include <netinet/icmp6.h> 74 #include <netinet/sctp.h> 75 76 #include <inet/ip.h> 77 #include <inet/ip_impl.h> 78 #include <inet/ip6.h> 79 #include <inet/ip6_asp.h> 80 #include <inet/tcp.h> 81 #include <inet/tcp_impl.h> 82 #include <inet/ip_multi.h> 83 #include <inet/ip_if.h> 84 #include <inet/ip_ire.h> 85 #include <inet/ip_rts.h> 86 #include <inet/optcom.h> 87 #include <inet/ip_ndp.h> 88 #include <inet/ip_listutils.h> 89 #include <netinet/igmp.h> 90 #include <netinet/ip_mroute.h> 91 #include <inet/ipp_common.h> 92 93 #include <net/pfkeyv2.h> 94 #include <inet/ipsec_info.h> 95 #include <inet/sadb.h> 96 #include <inet/ipsec_impl.h> 97 #include <sys/iphada.h> 98 #include <inet/tun.h> 99 #include <inet/ipdrop.h> 100 101 #include <sys/ethernet.h> 102 #include <net/if_types.h> 103 #include <sys/cpuvar.h> 104 105 #include <ipp/ipp.h> 106 #include <ipp/ipp_impl.h> 107 #include <ipp/ipgpc/ipgpc.h> 108 109 #include <sys/multidata.h> 110 #include <sys/pattr.h> 111 112 #include <inet/ipclassifier.h> 113 #include <inet/sctp_ip.h> 114 #include <inet/udp_impl.h> 115 116 /* 117 * Values for squeue switch: 118 * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain 119 * IP_SQUEUE_ENTER: squeue_enter 120 * IP_SQUEUE_FILL: squeue_fill 121 */ 122 int ip_squeue_enter = 2; 123 squeue_func_t ip_input_proc; 124 /* 125 * IP statistics. 126 */ 127 #define IP_STAT(x) (ip_statistics.x.value.ui64++) 128 #define IP_STAT_UPDATE(x, n) (ip_statistics.x.value.ui64 += (n)) 129 130 typedef struct ip_stat { 131 kstat_named_t ipsec_fanout_proto; 132 kstat_named_t ip_udp_fannorm; 133 kstat_named_t ip_udp_fanmb; 134 kstat_named_t ip_udp_fanothers; 135 kstat_named_t ip_udp_fast_path; 136 kstat_named_t ip_udp_slow_path; 137 kstat_named_t ip_udp_input_err; 138 kstat_named_t ip_tcppullup; 139 kstat_named_t ip_tcpoptions; 140 kstat_named_t ip_multipkttcp; 141 kstat_named_t ip_tcp_fast_path; 142 kstat_named_t ip_tcp_slow_path; 143 kstat_named_t ip_tcp_input_error; 144 kstat_named_t ip_db_ref; 145 kstat_named_t ip_notaligned1; 146 kstat_named_t ip_notaligned2; 147 kstat_named_t ip_multimblk3; 148 kstat_named_t ip_multimblk4; 149 kstat_named_t ip_ipoptions; 150 kstat_named_t ip_classify_fail; 151 kstat_named_t ip_opt; 152 kstat_named_t ip_udp_rput_local; 153 kstat_named_t ipsec_proto_ahesp; 154 kstat_named_t ip_conn_flputbq; 155 kstat_named_t ip_conn_walk_drain; 156 kstat_named_t ip_out_sw_cksum; 157 kstat_named_t ip_in_sw_cksum; 158 kstat_named_t ip_trash_ire_reclaim_calls; 159 kstat_named_t ip_trash_ire_reclaim_success; 160 kstat_named_t ip_ire_arp_timer_expired; 161 kstat_named_t ip_ire_redirect_timer_expired; 162 kstat_named_t ip_ire_pmtu_timer_expired; 163 kstat_named_t ip_input_multi_squeue; 164 kstat_named_t ip_tcp_in_full_hw_cksum_err; 165 kstat_named_t ip_tcp_in_part_hw_cksum_err; 166 kstat_named_t ip_tcp_in_sw_cksum_err; 167 kstat_named_t ip_tcp_out_sw_cksum_bytes; 168 kstat_named_t ip_udp_in_full_hw_cksum_err; 169 kstat_named_t ip_udp_in_part_hw_cksum_err; 170 kstat_named_t ip_udp_in_sw_cksum_err; 171 kstat_named_t ip_udp_out_sw_cksum_bytes; 172 kstat_named_t ip_frag_mdt_pkt_out; 173 kstat_named_t ip_frag_mdt_discarded; 174 kstat_named_t ip_frag_mdt_allocfail; 175 kstat_named_t ip_frag_mdt_addpdescfail; 176 kstat_named_t ip_frag_mdt_allocd; 177 } ip_stat_t; 178 179 static ip_stat_t ip_statistics = { 180 { "ipsec_fanout_proto", KSTAT_DATA_UINT64 }, 181 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 182 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 183 { "ip_udp_fanothers", KSTAT_DATA_UINT64 }, 184 { "ip_udp_fast_path", KSTAT_DATA_UINT64 }, 185 { "ip_udp_slow_path", KSTAT_DATA_UINT64 }, 186 { "ip_udp_input_err", KSTAT_DATA_UINT64 }, 187 { "ip_tcppullup", KSTAT_DATA_UINT64 }, 188 { "ip_tcpoptions", KSTAT_DATA_UINT64 }, 189 { "ip_multipkttcp", KSTAT_DATA_UINT64 }, 190 { "ip_tcp_fast_path", KSTAT_DATA_UINT64 }, 191 { "ip_tcp_slow_path", KSTAT_DATA_UINT64 }, 192 { "ip_tcp_input_error", KSTAT_DATA_UINT64 }, 193 { "ip_db_ref", KSTAT_DATA_UINT64 }, 194 { "ip_notaligned1", KSTAT_DATA_UINT64 }, 195 { "ip_notaligned2", KSTAT_DATA_UINT64 }, 196 { "ip_multimblk3", KSTAT_DATA_UINT64 }, 197 { "ip_multimblk4", KSTAT_DATA_UINT64 }, 198 { "ip_ipoptions", KSTAT_DATA_UINT64 }, 199 { "ip_classify_fail", KSTAT_DATA_UINT64 }, 200 { "ip_opt", KSTAT_DATA_UINT64 }, 201 { "ip_udp_rput_local", KSTAT_DATA_UINT64 }, 202 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 203 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 204 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 205 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 206 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 207 { "ip_trash_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 208 { "ip_trash_ire_reclaim_success", KSTAT_DATA_UINT64 }, 209 { "ip_ire_arp_timer_expired", KSTAT_DATA_UINT64 }, 210 { "ip_ire_redirect_timer_expired", KSTAT_DATA_UINT64 }, 211 { "ip_ire_pmtu_timer_expired", KSTAT_DATA_UINT64 }, 212 { "ip_input_multi_squeue", KSTAT_DATA_UINT64 }, 213 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 214 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 215 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 216 { "ip_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 217 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 218 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 219 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 220 { "ip_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 221 { "ip_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 222 { "ip_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 223 { "ip_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 224 { "ip_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 225 { "ip_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 226 }; 227 228 static kstat_t *ip_kstat; 229 230 #define TCP6 "tcp6" 231 #define TCP "tcp" 232 #define SCTP "sctp" 233 #define SCTP6 "sctp6" 234 235 major_t TCP6_MAJ; 236 major_t TCP_MAJ; 237 major_t SCTP_MAJ; 238 major_t SCTP6_MAJ; 239 240 int ip_poll_normal_ms = 100; 241 int ip_poll_normal_ticks = 0; 242 243 /* 244 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 245 */ 246 247 struct listptr_s { 248 mblk_t *lp_head; /* pointer to the head of the list */ 249 mblk_t *lp_tail; /* pointer to the tail of the list */ 250 }; 251 252 typedef struct listptr_s listptr_t; 253 254 /* 255 * Cluster specific hooks. These should be NULL when booted as a non-cluster 256 */ 257 258 /* 259 * Hook functions to enable cluster networking 260 * On non-clustered systems these vectors must always be NULL. 261 * 262 * Hook function to Check ip specified ip address is a shared ip address 263 * in the cluster 264 * 265 */ 266 int (*cl_inet_isclusterwide)(uint8_t protocol, 267 sa_family_t addr_family, uint8_t *laddrp) = NULL; 268 269 /* 270 * Hook function to generate cluster wide ip fragment identifier 271 */ 272 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 273 uint8_t *laddrp, uint8_t *faddrp) = NULL; 274 275 /* 276 * Synchronization notes: 277 * 278 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 279 * MT level protection given by STREAMS. IP uses a combination of its own 280 * internal serialization mechanism and standard Solaris locking techniques. 281 * The internal serialization is per phyint (no IPMP) or per IPMP group. 282 * This is used to serialize plumbing operations, IPMP operations, certain 283 * multicast operations, most set ioctls, igmp/mld timers etc. 284 * 285 * Plumbing is a long sequence of operations involving message 286 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 287 * involved in plumbing operations. A natural model is to serialize these 288 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 289 * parallel without any interference. But various set ioctls on hme0 are best 290 * serialized. However if the system uses IPMP, the operations are easier if 291 * they are serialized on a per IPMP group basis since IPMP operations 292 * happen across ill's of a group. Thus the lowest common denominator is to 293 * serialize most set ioctls, multicast join/leave operations, IPMP operations 294 * igmp/mld timer operations, and processing of DLPI control messages received 295 * from drivers on a per IPMP group basis. If the system does not employ 296 * IPMP the serialization is on a per phyint basis. This serialization is 297 * provided by the ipsq_t and primitives operating on this. Details can 298 * be found in ip_if.c above the core primitives operating on ipsq_t. 299 * 300 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 301 * Simiarly lookup of an ire by a thread also returns a refheld ire. 302 * In addition ipif's and ill's referenced by the ire are also indirectly 303 * refheld. Thus no ipif or ill can vanish nor can critical parameters like 304 * the ipif's address or netmask change as long as an ipif is refheld 305 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the 306 * address of an ipif has to go through the ipsq_t. This ensures that only 307 * 1 such exclusive operation proceeds at any time on the ipif. It then 308 * deletes all ires associated with this ipif, and waits for all refcnts 309 * associated with this ipif to come down to zero. The address is changed 310 * only after the ipif has been quiesced. Then the ipif is brought up again. 311 * More details are described above the comment in ip_sioctl_flags. 312 * 313 * Packet processing is based mostly on IREs and are fully multi-threaded 314 * using standard Solaris MT techniques. 315 * 316 * There are explicit locks in IP to handle: 317 * - The ip_g_head list maintained by mi_open_link() and friends. 318 * 319 * - The reassembly data structures (one lock per hash bucket) 320 * 321 * - conn_lock is meant to protect conn_t fields. The fields actually 322 * protected by conn_lock are documented in the conn_t definition. 323 * 324 * - ire_lock to protect some of the fields of the ire, IRE tables 325 * (one lock per hash bucket). Refer to ip_ire.c for details. 326 * 327 * - ndp_g_lock and nce_lock for protecting NCEs. 328 * 329 * - ill_lock protects fields of the ill and ipif. Details in ip.h 330 * 331 * - ill_g_lock: This is a global reader/writer lock. Protects the following 332 * * The AVL tree based global multi list of all ills. 333 * * The linked list of all ipifs of an ill 334 * * The <ill-ipsq> mapping 335 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next 336 * * The illgroup list threaded by ill_group_next. 337 * * <ill-phyint> association 338 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 339 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion 340 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill 341 * will all have to hold the ill_g_lock as writer for the actual duration 342 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping 343 * may be found in the IPMP section. 344 * 345 * - ill_lock: This is a per ill mutex. 346 * It protects some members of the ill and is documented below. 347 * It also protects the <ill-ipsq> mapping 348 * It also protects the illgroup list threaded by ill_group_next. 349 * It also protects the <ill-phyint> assoc. 350 * It also protects the list of ipifs hanging off the ill. 351 * 352 * - ipsq_lock: This is a per ipsq_t mutex lock. 353 * This protects all the other members of the ipsq struct except 354 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock 355 * 356 * - illgrp_lock: This is a per ill_group mutex lock. 357 * The only thing it protects is the illgrp_ill_schednext member of ill_group 358 * which dictates which is the next ill in an ill_group that is to be chosen 359 * for sending outgoing packets, through creation of an IRE_CACHE that 360 * references this ill. 361 * 362 * - phyint_lock: This is a per phyint mutex lock. Protects just the 363 * phyint_flags 364 * 365 * - ip_g_nd_lock: This is a global reader/writer lock. 366 * Any call to nd_load to load a new parameter to the ND table must hold the 367 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock 368 * as reader. 369 * 370 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 371 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 372 * uniqueness check also done atomically. 373 * 374 * - ipsec_capab_ills_lock: This readers/writer lock protects the global 375 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken 376 * as a writer when adding or deleting elements from these lists, and 377 * as a reader when walking these lists to send a SADB update to the 378 * IPsec capable ills. 379 * 380 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 381 * group list linked by ill_usesrc_grp_next. It also protects the 382 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 383 * group is being added or deleted. This lock is taken as a reader when 384 * walking the list/group(eg: to get the number of members in a usesrc group). 385 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 386 * field is changing state i.e from NULL to non-NULL or vice-versa. For 387 * example, it is not necessary to take this lock in the initial portion 388 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and 389 * ip_sioctl_flags since the these operations are executed exclusively and 390 * that ensures that the "usesrc group state" cannot change. The "usesrc 391 * group state" change can happen only in the latter part of 392 * ip_sioctl_slifusesrc and in ill_delete. 393 * 394 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications. 395 * 396 * To change the <ill-phyint> association, the ill_g_lock must be held 397 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 398 * must be held. 399 * 400 * To change the <ill-ipsq> association the ill_g_lock must be held as writer 401 * and the ill_lock of the ill in question must be held. 402 * 403 * To change the <ill-illgroup> association the ill_g_lock must be held as 404 * writer and the ill_lock of the ill in question must be held. 405 * 406 * To add or delete an ipif from the list of ipifs hanging off the ill, 407 * ill_g_lock (writer) and ill_lock must be held and the thread must be 408 * a writer on the associated ipsq,. 409 * 410 * To add or delete an ill to the system, the ill_g_lock must be held as 411 * writer and the thread must be a writer on the associated ipsq. 412 * 413 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 414 * must be a writer on the associated ipsq. 415 * 416 * Lock hierarchy 417 * 418 * Some lock hierarchy scenarios are listed below. 419 * 420 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 421 * ill_g_lock -> illgrp_lock -> ill_lock 422 * ill_g_lock -> ill_lock(s) -> phyint_lock 423 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock 424 * ill_g_lock -> ip_addr_avail_lock 425 * conn_lock -> irb_lock -> ill_lock -> ire_lock 426 * ipsa_lock -> ill_g_lock -> ill_lock 427 * ill_g_lock -> ip_g_nd_lock 428 * irb_lock -> ill_lock -> ire_mrtun_lock 429 * irb_lock -> ill_lock -> ire_srcif_table_lock 430 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock 431 * ipsec_capab_ills_lock -> ipsa_lock 432 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 433 * 434 * When more than 1 ill lock is needed to be held, all ill lock addresses 435 * are sorted on address and locked starting from highest addressed lock 436 * downward. 437 * 438 * IPSEC notes : 439 * 440 * IP interacts with the IPSEC code (AH/ESP) by tagging a M_CTL message 441 * in front of the actual packet. For outbound datagrams, the M_CTL 442 * contains a ipsec_out_t (defined in ipsec_info.h), which has the 443 * information used by the IPSEC code for applying the right level of 444 * protection. The information initialized by IP in the ipsec_out_t 445 * is determined by the per-socket policy or global policy in the system. 446 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in 447 * ipsec_info.h) which starts out with nothing in it. It gets filled 448 * with the right information if it goes through the AH/ESP code, which 449 * happens if the incoming packet is secure. The information initialized 450 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether 451 * the policy requirements needed by per-socket policy or global policy 452 * is met or not. 453 * 454 * If there is both per-socket policy (set using setsockopt) and there 455 * is also global policy match for the 5 tuples of the socket, 456 * ipsec_override_policy() makes the decision of which one to use. 457 * 458 * For fully connected sockets i.e dst, src [addr, port] is known, 459 * conn_policy_cached is set indicating that policy has been cached. 460 * conn_in_enforce_policy may or may not be set depending on whether 461 * there is a global policy match or per-socket policy match. 462 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. 463 * Once the right policy is set on the conn_t, policy cannot change for 464 * this socket. This makes life simpler for TCP (UDP ?) where 465 * re-transmissions go out with the same policy. For symmetry, policy 466 * is cached for fully connected UDP sockets also. Thus if policy is cached, 467 * it also implies that policy is latched i.e policy cannot change 468 * on these sockets. As we have the right policy on the conn, we don't 469 * have to lookup global policy for every outbound and inbound datagram 470 * and thus serving as an optimization. Note that a global policy change 471 * does not affect fully connected sockets if they have policy. If fully 472 * connected sockets did not have any policy associated with it, global 473 * policy change may affect them. 474 * 475 * IP Flow control notes: 476 * 477 * Non-TCP streams are flow controlled by IP. On the send side, if the packet 478 * cannot be sent down to the driver by IP, because of a canput failure, IP 479 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. 480 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained 481 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the 482 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the 483 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains 484 * the queued messages, and removes the conn from the drain list, if all 485 * messages were drained. It also qenables the next conn in the drain list to 486 * continue the drain process. 487 * 488 * In reality the drain list is not a single list, but a configurable number 489 * of lists. The ip_wsrv on the IP module, qenables the first conn in each 490 * list. If the ip_wsrv of the next qenabled conn does not run, because the 491 * stream closes, ip_close takes responsibility to qenable the next conn in 492 * the drain list. The directly called ip_wput path always does a putq, if 493 * it cannot putnext. Thus synchronization problems are handled between 494 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only 495 * functions that manipulate this drain list. Furthermore conn_drain_insert 496 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv 497 * running on a queue at any time. conn_drain_tail can be simultaneously called 498 * from both ip_wsrv and ip_close. 499 * 500 * IPQOS notes: 501 * 502 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 503 * and IPQoS modules. IPPF includes hooks in IP at different control points 504 * (callout positions) which direct packets to IPQoS modules for policy 505 * processing. Policies, if present, are global. 506 * 507 * The callout positions are located in the following paths: 508 * o local_in (packets destined for this host) 509 * o local_out (packets orginating from this host ) 510 * o fwd_in (packets forwarded by this m/c - inbound) 511 * o fwd_out (packets forwarded by this m/c - outbound) 512 * Hooks at these callout points can be enabled/disabled using the ndd variable 513 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 514 * By default all the callout positions are enabled. 515 * 516 * Outbound (local_out) 517 * Hooks are placed in ip_wput_ire and ipsec_out_process. 518 * 519 * Inbound (local_in) 520 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and 521 * TCP and UDP fanout routines. 522 * 523 * Forwarding (in and out) 524 * Hooks are placed in ip_rput_forward and ip_mrtun_forward. 525 * 526 * IP Policy Framework processing (IPPF processing) 527 * Policy processing for a packet is initiated by ip_process, which ascertains 528 * that the classifier (ipgpc) is loaded and configured, failing which the 529 * packet resumes normal processing in IP. If the clasifier is present, the 530 * packet is acted upon by one or more IPQoS modules (action instances), per 531 * filters configured in ipgpc and resumes normal IP processing thereafter. 532 * An action instance can drop a packet in course of its processing. 533 * 534 * A boolean variable, ip_policy, is used in all the fanout routines that can 535 * invoke ip_process for a packet. This variable indicates if the packet should 536 * to be sent for policy processing. The variable is set to B_TRUE by default, 537 * i.e. when the routines are invoked in the normal ip procesing path for a 538 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; 539 * ip_policy is set to B_FALSE for all the routines called in these two 540 * functions because, in the former case, we don't process loopback traffic 541 * currently while in the latter, the packets have already been processed in 542 * icmp_inbound. 543 * 544 * Zones notes: 545 * 546 * The partitioning rules for networking are as follows: 547 * 1) Packets coming from a zone must have a source address belonging to that 548 * zone. 549 * 2) Packets coming from a zone can only be sent on a physical interface on 550 * which the zone has an IP address. 551 * 3) Between two zones on the same machine, packet delivery is only allowed if 552 * there's a matching route for the destination and zone in the forwarding 553 * table. 554 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 555 * different zones can bind to the same port with the wildcard address 556 * (INADDR_ANY). 557 * 558 * The granularity of interface partitioning is at the logical interface level. 559 * Therefore, every zone has its own IP addresses, and incoming packets can be 560 * attributed to a zone unambiguously. A logical interface is placed into a zone 561 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 562 * structure. Rule (1) is implemented by modifying the source address selection 563 * algorithm so that the list of eligible addresses is filtered based on the 564 * sending process zone. 565 * 566 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 567 * across all zones, depending on their type. Here is the break-up: 568 * 569 * IRE type Shared/exclusive 570 * -------- ---------------- 571 * IRE_BROADCAST Exclusive 572 * IRE_DEFAULT (default routes) Shared (*) 573 * IRE_LOCAL Exclusive 574 * IRE_LOOPBACK Exclusive 575 * IRE_PREFIX (net routes) Shared (*) 576 * IRE_CACHE Exclusive 577 * IRE_IF_NORESOLVER (interface routes) Exclusive 578 * IRE_IF_RESOLVER (interface routes) Exclusive 579 * IRE_HOST (host routes) Shared (*) 580 * 581 * (*) A zone can only use a default or off-subnet route if the gateway is 582 * directly reachable from the zone, that is, if the gateway's address matches 583 * one of the zone's logical interfaces. 584 * 585 * Multiple zones can share a common broadcast address; typically all zones 586 * share the 255.255.255.255 address. Incoming as well as locally originated 587 * broadcast packets must be dispatched to all the zones on the broadcast 588 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 589 * since some zones may not be on the 10.16.72/24 network. To handle this, each 590 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 591 * sent to every zone that has an IRE_BROADCAST entry for the destination 592 * address on the input ill, see conn_wantpacket(). 593 * 594 * Applications in different zones can join the same multicast group address. 595 * For IPv4, group memberships are per-logical interface, so they're already 596 * inherently part of a zone. For IPv6, group memberships are per-physical 597 * interface, so we distinguish IPv6 group memberships based on group address, 598 * interface and zoneid. In both cases, received multicast packets are sent to 599 * every zone for which a group membership entry exists. On IPv6 we need to 600 * check that the target zone still has an address on the receiving physical 601 * interface; it could have been removed since the application issued the 602 * IPV6_JOIN_GROUP. 603 */ 604 605 /* 606 * Squeue Fanout flags: 607 * 0: No fanout. 608 * 1: Fanout across all squeues 609 */ 610 boolean_t ip_squeue_fanout = 0; 611 612 /* 613 * Maximum dups allowed per packet. 614 */ 615 uint_t ip_max_frag_dups = 10; 616 617 #define IS_SIMPLE_IPH(ipha) \ 618 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) 619 620 /* RFC1122 Conformance */ 621 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER 622 623 #define ILL_MAX_NAMELEN LIFNAMSIZ 624 625 /* Leave room for ip_newroute to tack on the src and target addresses */ 626 #define OK_RESOLVER_MP(mp) \ 627 ((mp) && ((mp)->b_wptr - (mp)->b_rptr) >= (2 * IP_ADDR_LEN)) 628 629 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); 630 631 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t); 632 static void ip_ipsec_out_prepend(mblk_t *, mblk_t *, ill_t *); 633 634 static void icmp_frag_needed(queue_t *, mblk_t *, int); 635 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, 636 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); 637 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *); 638 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, 639 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, 640 ill_t *, zoneid_t); 641 static void icmp_options_update(ipha_t *); 642 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t); 643 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t); 644 static mblk_t *icmp_pkt_err_ok(mblk_t *); 645 static void icmp_redirect(mblk_t *); 646 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t); 647 648 static void ip_arp_news(queue_t *, mblk_t *); 649 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *); 650 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 651 char *ip_dot_addr(ipaddr_t, char *); 652 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 653 int ip_close(queue_t *, int); 654 static char *ip_dot_saddr(uchar_t *, char *); 655 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 656 boolean_t, boolean_t, ill_t *, zoneid_t); 657 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, 658 boolean_t, boolean_t, zoneid_t); 659 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, 660 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); 661 static void ip_lrput(queue_t *, mblk_t *); 662 ipaddr_t ip_massage_options(ipha_t *); 663 static void ip_mrtun_forward(ire_t *, ill_t *, mblk_t *); 664 ipaddr_t ip_net_mask(ipaddr_t); 665 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, ill_t *, conn_t *); 666 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, 667 conn_t *, uint32_t); 668 static int ip_hdr_complete(ipha_t *, zoneid_t); 669 char *ip_nv_lookup(nv_t *, int); 670 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); 671 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); 672 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); 673 static boolean_t ip_param_register(ipparam_t *, size_t, ipndp_t *, 674 size_t); 675 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 676 void ip_rput(queue_t *, mblk_t *); 677 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 678 void *dummy_arg); 679 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); 680 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *); 681 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, 682 ire_t *); 683 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *); 684 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, 685 uint16_t *); 686 int ip_snmp_get(queue_t *, mblk_t *); 687 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *); 688 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *); 689 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *); 690 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *); 691 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *); 692 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *); 693 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *); 694 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *); 695 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *); 696 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *); 697 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *); 698 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *); 699 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *); 700 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *); 701 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *); 702 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *); 703 static void ip_snmp_get2_v4(ire_t *, listptr_t []); 704 static void ip_snmp_get2_v6_route(ire_t *, listptr_t *); 705 static int ip_snmp_get2_v6_media(nce_t *, listptr_t *); 706 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 707 static boolean_t ip_source_routed(ipha_t *); 708 static boolean_t ip_source_route_included(ipha_t *); 709 710 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t); 711 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int); 712 static void ip_wput_local_options(ipha_t *); 713 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, 714 zoneid_t); 715 716 static void conn_drain_init(void); 717 static void conn_drain_fini(void); 718 static void conn_drain_tail(conn_t *connp, boolean_t closing); 719 720 static void conn_walk_drain(void); 721 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, 722 zoneid_t); 723 724 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, 725 zoneid_t); 726 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 727 void *dummy_arg); 728 729 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); 730 731 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 732 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, 733 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); 734 static void ip_multirt_bad_mtu(ire_t *, uint32_t); 735 736 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); 737 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, 738 caddr_t, cred_t *); 739 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, 740 caddr_t cp, cred_t *cr); 741 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, 742 cred_t *); 743 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 744 caddr_t cp, cred_t *cr); 745 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, 746 cred_t *); 747 static squeue_func_t ip_squeue_switch(int); 748 749 static void ip_kstat_init(void); 750 static void ip_kstat_fini(void); 751 static int ip_kstat_update(kstat_t *kp, int rw); 752 static void icmp_kstat_init(void); 753 static void icmp_kstat_fini(void); 754 static int icmp_kstat_update(kstat_t *kp, int rw); 755 756 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); 757 758 static boolean_t ip_no_forward(ipha_t *, ill_t *); 759 static boolean_t ip_loopback_src_or_dst(ipha_t *, ill_t *); 760 761 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, 762 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); 763 764 void ip_input(ill_t *, ill_rx_ring_t *, mblk_t *, size_t); 765 766 timeout_id_t ip_ire_expire_id; /* IRE expiration timer. */ 767 static clock_t ip_ire_arp_time_elapsed; /* Time since IRE cache last flushed */ 768 static clock_t ip_ire_rd_time_elapsed; /* ... redirect IREs last flushed */ 769 static clock_t ip_ire_pmtu_time_elapsed; /* Time since path mtu increase */ 770 771 uint_t ip_ire_default_count; /* Number of IPv4 IRE_DEFAULT entries. */ 772 uint_t ip_ire_default_index; /* Walking index used to mod in */ 773 774 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 775 clock_t icmp_pkt_err_last = 0; /* Time since last icmp_pkt_err */ 776 uint_t icmp_pkt_err_sent = 0; /* Number of packets sent in burst */ 777 778 /* How long, in seconds, we allow frags to hang around. */ 779 #define IP_FRAG_TIMEOUT 60 780 781 time_t ip_g_frag_timeout = IP_FRAG_TIMEOUT; 782 clock_t ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; 783 784 /* 785 * Threshold which determines whether MDT should be used when 786 * generating IP fragments; payload size must be greater than 787 * this threshold for MDT to take place. 788 */ 789 #define IP_WPUT_FRAG_MDT_MIN 32768 790 791 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; 792 793 /* Protected by ip_mi_lock */ 794 static void *ip_g_head; /* Instance Data List Head */ 795 kmutex_t ip_mi_lock; /* Lock for list of instances */ 796 797 /* Only modified during _init and _fini thus no locking is needed. */ 798 caddr_t ip_g_nd; /* Named Dispatch List Head */ 799 800 801 static long ip_rput_pullups; 802 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 803 804 vmem_t *ip_minor_arena; 805 806 /* 807 * MIB-2 stuff for SNMP (both IP and ICMP) 808 */ 809 mib2_ip_t ip_mib; 810 mib2_icmp_t icmp_mib; 811 812 #ifdef DEBUG 813 uint32_t ipsechw_debug = 0; 814 #endif 815 816 kstat_t *ip_mibkp; /* kstat exporting ip_mib data */ 817 kstat_t *icmp_mibkp; /* kstat exporting icmp_mib data */ 818 819 uint_t loopback_packets = 0; 820 821 /* 822 * Multirouting/CGTP stuff 823 */ 824 cgtp_filter_ops_t *ip_cgtp_filter_ops; /* CGTP hooks */ 825 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 826 boolean_t ip_cgtp_filter; /* Enable/disable CGTP hooks */ 827 /* Interval (in ms) between consecutive 'bad MTU' warnings */ 828 hrtime_t ip_multirt_log_interval = 1000; 829 /* Time since last warning issued. */ 830 static hrtime_t multirt_bad_mtu_last_time = 0; 831 832 kmutex_t ip_trash_timer_lock; 833 krwlock_t ip_g_nd_lock; 834 835 /* 836 * XXX following really should only be in a header. Would need more 837 * header and .c clean up first. 838 */ 839 extern optdb_obj_t ip_opt_obj; 840 841 ulong_t ip_squeue_enter_unbound = 0; 842 843 /* 844 * Named Dispatch Parameter Table. 845 * All of these are alterable, within the min/max values given, at run time. 846 */ 847 static ipparam_t lcl_param_arr[] = { 848 /* min max value name */ 849 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, 850 { 0, 1, 1, "ip_respond_to_echo_broadcast"}, 851 { 0, 1, 1, "ip_respond_to_echo_multicast"}, 852 { 0, 1, 0, "ip_respond_to_timestamp"}, 853 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, 854 { 0, 1, 1, "ip_send_redirects"}, 855 { 0, 1, 0, "ip_forward_directed_broadcasts"}, 856 { 0, 10, 0, "ip_debug"}, 857 { 0, 10, 0, "ip_mrtdebug"}, 858 { 5000, 999999999, 60000, "ip_ire_timer_interval" }, 859 { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, 860 { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, 861 { 1, 255, 255, "ip_def_ttl" }, 862 { 0, 1, 0, "ip_forward_src_routed"}, 863 { 0, 256, 32, "ip_wroff_extra" }, 864 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, 865 { 8, 65536, 64, "ip_icmp_return_data_bytes" }, 866 { 0, 1, 1, "ip_path_mtu_discovery" }, 867 { 0, 240, 30, "ip_ignore_delete_time" }, 868 { 0, 1, 0, "ip_ignore_redirect" }, 869 { 0, 1, 1, "ip_output_queue" }, 870 { 1, 254, 1, "ip_broadcast_ttl" }, 871 { 0, 99999, 100, "ip_icmp_err_interval" }, 872 { 1, 99999, 10, "ip_icmp_err_burst" }, 873 { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, 874 { 0, 1, 0, "ip_strict_dst_multihoming" }, 875 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, 876 { 0, 1, 0, "ipsec_override_persocket_policy" }, 877 { 0, 1, 1, "icmp_accept_clear_messages" }, 878 { 0, 1, 1, "igmp_accept_clear_messages" }, 879 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, 880 "ip_ndp_delay_first_probe_time"}, 881 { 1, 999999999, ND_MAX_UNICAST_SOLICIT, 882 "ip_ndp_max_unicast_solicit"}, 883 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, 884 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, 885 { 0, 1, 0, "ip6_forward_src_routed"}, 886 { 0, 1, 1, "ip6_respond_to_echo_multicast"}, 887 { 0, 1, 1, "ip6_send_redirects"}, 888 { 0, 1, 0, "ip6_ignore_redirect" }, 889 { 0, 1, 0, "ip6_strict_dst_multihoming" }, 890 891 { 1, 8, 3, "ip_ire_reclaim_fraction" }, 892 893 { 0, 999999, 1000, "ipsec_policy_log_interval" }, 894 895 { 0, 1, 1, "pim_accept_clear_messages" }, 896 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, 897 { 1, 20, 3, "ip_ndp_unsolicit_count" }, 898 { 0, 1, 1, "ip6_ignore_home_address_opt" }, 899 { 0, 15, 0, "ip_policy_mask" }, 900 { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, 901 { 0, 255, 1, "ip_multirt_ttl" }, 902 { 0, 1, 1, "ip_multidata_outbound" }, 903 #ifdef DEBUG 904 { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, 905 #endif 906 }; 907 908 ipparam_t *ip_param_arr = lcl_param_arr; 909 910 /* Extended NDP table */ 911 static ipndp_t lcl_ndp_arr[] = { 912 /* getf setf data name */ 913 { ip_param_generic_get, ip_forward_set, (caddr_t)&ip_g_forward, 914 "ip_forwarding" }, 915 { ip_param_generic_get, ip_forward_set, (caddr_t)&ipv6_forward, 916 "ip6_forwarding" }, 917 { ip_ill_report, NULL, NULL, 918 "ip_ill_status" }, 919 { ip_ipif_report, NULL, NULL, 920 "ip_ipif_status" }, 921 { ip_ire_report, NULL, NULL, 922 "ipv4_ire_status" }, 923 { ip_ire_report_mrtun, NULL, NULL, 924 "ipv4_mrtun_ire_status" }, 925 { ip_ire_report_srcif, NULL, NULL, 926 "ipv4_srcif_ire_status" }, 927 { ip_ire_report_v6, NULL, NULL, 928 "ipv6_ire_status" }, 929 { ip_conn_report, NULL, NULL, 930 "ip_conn_status" }, 931 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, 932 "ip_rput_pullups" }, 933 { ndp_report, NULL, NULL, 934 "ip_ndp_cache_report" }, 935 { ip_srcid_report, NULL, NULL, 936 "ip_srcid_status" }, 937 { ip_param_generic_get, ip_squeue_profile_set, 938 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, 939 { ip_param_generic_get, ip_squeue_bind_set, 940 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, 941 { ip_param_generic_get, ip_input_proc_set, 942 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, 943 { ip_param_generic_get, ip_int_set, 944 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, 945 { ip_cgtp_filter_get, ip_cgtp_filter_set, (caddr_t)&ip_cgtp_filter, 946 "ip_cgtp_filter" }, 947 { ip_param_generic_get, ip_int_set, 948 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" } 949 }; 950 951 /* 952 * ip_g_forward controls IP forwarding. It takes two values: 953 * 0: IP_FORWARD_NEVER Don't forward packets ever. 954 * 1: IP_FORWARD_ALWAYS Forward packets for elsewhere. 955 * 956 * RFC1122 says there must be a configuration switch to control forwarding, 957 * but that the default MUST be to not forward packets ever. Implicit 958 * control based on configuration of multiple interfaces MUST NOT be 959 * implemented (Section 3.1). SunOS 4.1 did provide the "automatic" capability 960 * and, in fact, it was the default. That capability is now provided in the 961 * /etc/rc2.d/S69inet script. 962 */ 963 int ip_g_forward = IP_FORWARD_DEFAULT; 964 965 /* It also has an IPv6 counterpart. */ 966 967 int ipv6_forward = IP_FORWARD_DEFAULT; 968 969 /* Following line is external, and in ip.h. Normally marked with * *. */ 970 #define ip_respond_to_address_mask_broadcast ip_param_arr[0].ip_param_value 971 #define ip_g_resp_to_echo_bcast ip_param_arr[1].ip_param_value 972 #define ip_g_resp_to_echo_mcast ip_param_arr[2].ip_param_value 973 #define ip_g_resp_to_timestamp ip_param_arr[3].ip_param_value 974 #define ip_g_resp_to_timestamp_bcast ip_param_arr[4].ip_param_value 975 #define ip_g_send_redirects ip_param_arr[5].ip_param_value 976 #define ip_g_forward_directed_bcast ip_param_arr[6].ip_param_value 977 #define ip_debug ip_param_arr[7].ip_param_value /* */ 978 #define ip_mrtdebug ip_param_arr[8].ip_param_value /* */ 979 #define ip_timer_interval ip_param_arr[9].ip_param_value /* */ 980 #define ip_ire_arp_interval ip_param_arr[10].ip_param_value /* */ 981 #define ip_ire_redir_interval ip_param_arr[11].ip_param_value 982 #define ip_def_ttl ip_param_arr[12].ip_param_value 983 #define ip_forward_src_routed ip_param_arr[13].ip_param_value 984 #define ip_wroff_extra ip_param_arr[14].ip_param_value 985 #define ip_ire_pathmtu_interval ip_param_arr[15].ip_param_value 986 #define ip_icmp_return ip_param_arr[16].ip_param_value 987 #define ip_path_mtu_discovery ip_param_arr[17].ip_param_value /* */ 988 #define ip_ignore_delete_time ip_param_arr[18].ip_param_value /* */ 989 #define ip_ignore_redirect ip_param_arr[19].ip_param_value 990 #define ip_output_queue ip_param_arr[20].ip_param_value 991 #define ip_broadcast_ttl ip_param_arr[21].ip_param_value 992 #define ip_icmp_err_interval ip_param_arr[22].ip_param_value 993 #define ip_icmp_err_burst ip_param_arr[23].ip_param_value 994 #define ip_reass_queue_bytes ip_param_arr[24].ip_param_value 995 #define ip_strict_dst_multihoming ip_param_arr[25].ip_param_value 996 #define ip_addrs_per_if ip_param_arr[26].ip_param_value 997 #define ipsec_override_persocket_policy ip_param_arr[27].ip_param_value /* */ 998 #define icmp_accept_clear_messages ip_param_arr[28].ip_param_value 999 #define igmp_accept_clear_messages ip_param_arr[29].ip_param_value 1000 1001 /* IPv6 configuration knobs */ 1002 #define delay_first_probe_time ip_param_arr[30].ip_param_value 1003 #define max_unicast_solicit ip_param_arr[31].ip_param_value 1004 #define ipv6_def_hops ip_param_arr[32].ip_param_value 1005 #define ipv6_icmp_return ip_param_arr[33].ip_param_value 1006 #define ipv6_forward_src_routed ip_param_arr[34].ip_param_value 1007 #define ipv6_resp_echo_mcast ip_param_arr[35].ip_param_value 1008 #define ipv6_send_redirects ip_param_arr[36].ip_param_value 1009 #define ipv6_ignore_redirect ip_param_arr[37].ip_param_value 1010 #define ipv6_strict_dst_multihoming ip_param_arr[38].ip_param_value 1011 #define ip_ire_reclaim_fraction ip_param_arr[39].ip_param_value 1012 #define ipsec_policy_log_interval ip_param_arr[40].ip_param_value 1013 #define pim_accept_clear_messages ip_param_arr[41].ip_param_value 1014 #define ip_ndp_unsolicit_interval ip_param_arr[42].ip_param_value 1015 #define ip_ndp_unsolicit_count ip_param_arr[43].ip_param_value 1016 #define ipv6_ignore_home_address_opt ip_param_arr[44].ip_param_value 1017 #define ip_policy_mask ip_param_arr[45].ip_param_value 1018 #define ip_multirt_resolution_interval ip_param_arr[46].ip_param_value 1019 #define ip_multirt_ttl ip_param_arr[47].ip_param_value 1020 #define ip_multidata_outbound ip_param_arr[48].ip_param_value 1021 #ifdef DEBUG 1022 #define ipv6_drop_inbound_icmpv6 ip_param_arr[49].ip_param_value 1023 #else 1024 #define ipv6_drop_inbound_icmpv6 0 1025 #endif 1026 1027 1028 /* 1029 * Table of IP ioctls encoding the various properties of the ioctl and 1030 * indexed based on the last byte of the ioctl command. Occasionally there 1031 * is a clash, and there is more than 1 ioctl with the same last byte. 1032 * In such a case 1 ioctl is encoded in the ndx table and the remaining 1033 * ioctls are encoded in the misc table. An entry in the ndx table is 1034 * retrieved by indexing on the last byte of the ioctl command and comparing 1035 * the ioctl command with the value in the ndx table. In the event of a 1036 * mismatch the misc table is then searched sequentially for the desired 1037 * ioctl command. 1038 * 1039 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 1040 */ 1041 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 1042 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1043 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1044 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1045 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1046 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1047 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1048 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1049 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1050 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1051 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1052 1053 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 1054 MISC_CMD, ip_siocaddrt, NULL }, 1055 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 1056 MISC_CMD, ip_siocdelrt, NULL }, 1057 1058 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1059 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1060 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1061 IF_CMD, ip_sioctl_get_addr, NULL }, 1062 1063 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1064 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1065 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 1066 IPI_GET_CMD | IPI_REPL, 1067 IF_CMD, ip_sioctl_get_dstaddr, NULL }, 1068 1069 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 1070 IPI_PRIV | IPI_WR | IPI_REPL, 1071 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1072 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 1073 IPI_MODOK | IPI_GET_CMD | IPI_REPL, 1074 IF_CMD, ip_sioctl_get_flags, NULL }, 1075 1076 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1077 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1078 1079 /* copyin size cannot be coded for SIOCGIFCONF */ 1080 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1081 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1082 1083 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1084 IF_CMD, ip_sioctl_mtu, NULL }, 1085 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1086 IF_CMD, ip_sioctl_get_mtu, NULL }, 1087 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 1088 IPI_GET_CMD | IPI_REPL, 1089 IF_CMD, ip_sioctl_get_brdaddr, NULL }, 1090 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1091 IF_CMD, ip_sioctl_brdaddr, NULL }, 1092 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 1093 IPI_GET_CMD | IPI_REPL, 1094 IF_CMD, ip_sioctl_get_netmask, NULL }, 1095 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 1096 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1097 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 1098 IPI_GET_CMD | IPI_REPL, 1099 IF_CMD, ip_sioctl_get_metric, NULL }, 1100 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 1101 IF_CMD, ip_sioctl_metric, NULL }, 1102 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1103 1104 /* See 166-168 below for extended SIOC*XARP ioctls */ 1105 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV, 1106 MISC_CMD, ip_sioctl_arp, NULL }, 1107 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, 1108 MISC_CMD, ip_sioctl_arp, NULL }, 1109 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, 1110 MISC_CMD, ip_sioctl_arp, NULL }, 1111 1112 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1113 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1114 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1115 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1116 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1117 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1118 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1119 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1120 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1121 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1122 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1123 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1124 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1125 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1126 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1127 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1128 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1129 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1130 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1131 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1132 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1133 1134 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 1135 MISC_CMD, if_unitsel, if_unitsel_restart }, 1136 1137 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1138 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1139 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1140 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1141 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1142 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1143 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1144 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1145 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1146 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1147 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1148 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1149 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1150 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1151 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1152 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1153 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1154 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1155 1156 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 1157 IPI_PRIV | IPI_WR | IPI_MODOK, 1158 IF_CMD, ip_sioctl_sifname, NULL }, 1159 1160 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1161 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1162 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1163 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1164 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1165 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1166 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1167 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1168 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1169 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1170 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1171 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1172 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1173 1174 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL, 1175 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 1176 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1177 IF_CMD, ip_sioctl_get_muxid, NULL }, 1178 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 1179 IPI_PRIV | IPI_WR | IPI_REPL, 1180 IF_CMD, ip_sioctl_muxid, NULL }, 1181 1182 /* Both if and lif variants share same func */ 1183 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, 1184 IF_CMD, ip_sioctl_get_lifindex, NULL }, 1185 /* Both if and lif variants share same func */ 1186 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 1187 IPI_PRIV | IPI_WR | IPI_REPL, 1188 IF_CMD, ip_sioctl_slifindex, NULL }, 1189 1190 /* copyin size cannot be coded for SIOCGIFCONF */ 1191 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD | IPI_REPL, 1192 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 1193 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1194 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1195 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1196 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1197 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1198 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1199 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1200 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1201 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1202 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1203 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1204 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1205 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1206 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1207 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1208 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1209 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1210 1211 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 1212 IPI_PRIV | IPI_WR | IPI_REPL, 1213 LIF_CMD, ip_sioctl_removeif, 1214 ip_sioctl_removeif_restart }, 1215 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 1216 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL, 1217 LIF_CMD, ip_sioctl_addif, NULL }, 1218 #define SIOCLIFADDR_NDX 112 1219 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1220 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 1221 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 1222 IPI_GET_CMD | IPI_REPL, 1223 LIF_CMD, ip_sioctl_get_addr, NULL }, 1224 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1225 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 1226 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 1227 IPI_GET_CMD | IPI_REPL, 1228 LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 1229 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 1230 IPI_PRIV | IPI_WR | IPI_REPL, 1231 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 1232 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 1233 IPI_GET_CMD | IPI_MODOK | IPI_REPL, 1234 LIF_CMD, ip_sioctl_get_flags, NULL }, 1235 1236 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1237 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1238 1239 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1240 ip_sioctl_get_lifconf, NULL }, 1241 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1242 LIF_CMD, ip_sioctl_mtu, NULL }, 1243 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, 1244 LIF_CMD, ip_sioctl_get_mtu, NULL }, 1245 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 1246 IPI_GET_CMD | IPI_REPL, 1247 LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 1248 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1249 LIF_CMD, ip_sioctl_brdaddr, NULL }, 1250 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 1251 IPI_GET_CMD | IPI_REPL, 1252 LIF_CMD, ip_sioctl_get_netmask, NULL }, 1253 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1254 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 1255 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 1256 IPI_GET_CMD | IPI_REPL, 1257 LIF_CMD, ip_sioctl_get_metric, NULL }, 1258 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1259 LIF_CMD, ip_sioctl_metric, NULL }, 1260 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1261 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL, 1262 LIF_CMD, ip_sioctl_slifname, 1263 ip_sioctl_slifname_restart }, 1264 1265 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL, 1266 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1267 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1268 IPI_GET_CMD | IPI_REPL, 1269 LIF_CMD, ip_sioctl_get_muxid, NULL }, 1270 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1271 IPI_PRIV | IPI_WR | IPI_REPL, 1272 LIF_CMD, ip_sioctl_muxid, NULL }, 1273 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1274 IPI_GET_CMD | IPI_REPL, 1275 LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1276 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1277 IPI_PRIV | IPI_WR | IPI_REPL, 1278 LIF_CMD, ip_sioctl_slifindex, 0 }, 1279 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1280 LIF_CMD, ip_sioctl_token, NULL }, 1281 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1282 IPI_GET_CMD | IPI_REPL, 1283 LIF_CMD, ip_sioctl_get_token, NULL }, 1284 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1285 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1286 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1287 IPI_GET_CMD | IPI_REPL, 1288 LIF_CMD, ip_sioctl_get_subnet, NULL }, 1289 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1290 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1291 1292 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1293 IPI_GET_CMD | IPI_REPL, 1294 LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1295 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1296 LIF_CMD, ip_siocdelndp_v6, NULL }, 1297 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1298 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1299 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1300 LIF_CMD, ip_siocsetndp_v6, NULL }, 1301 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1302 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1303 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1304 MISC_CMD, ip_sioctl_tonlink, NULL }, 1305 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1306 MISC_CMD, ip_sioctl_tmysite, NULL }, 1307 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL, 1308 TUN_CMD, ip_sioctl_tunparam, NULL }, 1309 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), 1310 IPI_PRIV | IPI_WR, 1311 TUN_CMD, ip_sioctl_tunparam, NULL }, 1312 1313 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1314 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1315 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1316 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1317 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1318 1319 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq), 1320 IPI_PRIV | IPI_WR | IPI_REPL, 1321 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1322 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq), 1323 IPI_PRIV | IPI_WR | IPI_REPL, 1324 LIF_CMD, ip_sioctl_move, ip_sioctl_move }, 1325 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1326 IPI_PRIV | IPI_WR, 1327 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1328 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1329 IPI_GET_CMD | IPI_REPL, 1330 LIF_CMD, ip_sioctl_get_groupname, NULL }, 1331 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq), 1332 IPI_GET_CMD | IPI_REPL, 1333 LIF_CMD, ip_sioctl_get_oindex, NULL }, 1334 1335 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1336 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1337 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1338 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1339 1340 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1341 LIF_CMD, ip_sioctl_slifoindex, NULL }, 1342 1343 /* These are handled in ip_sioctl_copyin_setup itself */ 1344 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1345 MISC_CMD, NULL, NULL }, 1346 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1347 MISC_CMD, NULL, NULL }, 1348 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1349 1350 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD | IPI_REPL, 1351 ip_sioctl_get_lifconf, NULL }, 1352 1353 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, 1354 MISC_CMD, ip_sioctl_xarp, NULL }, 1355 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, 1356 MISC_CMD, ip_sioctl_xarp, NULL }, 1357 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, 1358 MISC_CMD, ip_sioctl_xarp, NULL }, 1359 1360 /* SIOCPOPSOCKFS is not handled by IP */ 1361 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1362 1363 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1364 IPI_GET_CMD | IPI_REPL, 1365 LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1366 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1367 IPI_PRIV | IPI_WR | IPI_REPL, 1368 LIF_CMD, ip_sioctl_slifzone, 1369 ip_sioctl_slifzone_restart }, 1370 /* 172-174 are SCTP ioctls and not handled by IP */ 1371 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1372 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1373 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1374 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1375 IPI_GET_CMD, LIF_CMD, 1376 ip_sioctl_get_lifusesrc, 0 }, 1377 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1378 IPI_PRIV | IPI_WR, 1379 LIF_CMD, ip_sioctl_slifusesrc, 1380 NULL }, 1381 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1382 ip_sioctl_get_lifsrcof, NULL }, 1383 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1384 MISC_CMD, ip_sioctl_msfilter, NULL }, 1385 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, 1386 MISC_CMD, ip_sioctl_msfilter, NULL }, 1387 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1388 MISC_CMD, ip_sioctl_msfilter, NULL }, 1389 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, 1390 MISC_CMD, ip_sioctl_msfilter, NULL }, 1391 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD, 1392 ip_sioctl_set_ipmpfailback, NULL } 1393 }; 1394 1395 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1396 1397 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1398 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), 1399 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, 1400 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, 1401 TUN_CMD, ip_sioctl_tunparam, NULL }, 1402 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1403 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1404 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1405 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1406 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, 1407 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, 1408 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1409 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD, 1410 MISC_CMD, mrt_ioctl}, 1411 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD, 1412 MISC_CMD, mrt_ioctl}, 1413 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD, 1414 MISC_CMD, mrt_ioctl} 1415 }; 1416 1417 int ip_misc_ioctl_count = 1418 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1419 1420 static idl_t *conn_drain_list; /* The array of conn drain lists */ 1421 static uint_t conn_drain_list_cnt; /* Total count of conn_drain_list */ 1422 static int conn_drain_list_index; /* Next drain_list to be used */ 1423 int conn_drain_nthreads; /* Number of drainers reqd. */ 1424 /* Settable in /etc/system */ 1425 1426 /* Defined in ip_ire.c */ 1427 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1428 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1429 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1430 1431 static nv_t ire_nv_arr[] = { 1432 { IRE_BROADCAST, "BROADCAST" }, 1433 { IRE_LOCAL, "LOCAL" }, 1434 { IRE_LOOPBACK, "LOOPBACK" }, 1435 { IRE_CACHE, "CACHE" }, 1436 { IRE_DEFAULT, "DEFAULT" }, 1437 { IRE_PREFIX, "PREFIX" }, 1438 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1439 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1440 { IRE_HOST, "HOST" }, 1441 { IRE_HOST_REDIRECT, "HOST_REDIRECT" }, 1442 { 0 } 1443 }; 1444 1445 nv_t *ire_nv_tbl = ire_nv_arr; 1446 1447 /* Defined in ip_if.c, protect the list of IPsec capable ills */ 1448 extern krwlock_t ipsec_capab_ills_lock; 1449 1450 /* Packet dropper for IP IPsec processing failures */ 1451 ipdropper_t ip_dropper; 1452 1453 /* Simple ICMP IP Header Template */ 1454 static ipha_t icmp_ipha = { 1455 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1456 }; 1457 1458 struct module_info ip_mod_info = { 1459 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024 1460 }; 1461 1462 static struct qinit rinit = { 1463 (pfi_t)ip_rput, NULL, ip_open, ip_close, NULL, 1464 &ip_mod_info 1465 }; 1466 1467 static struct qinit winit = { 1468 (pfi_t)ip_wput, (pfi_t)ip_wsrv, ip_open, ip_close, NULL, 1469 &ip_mod_info 1470 }; 1471 1472 static struct qinit lrinit = { 1473 (pfi_t)ip_lrput, NULL, ip_open, ip_close, NULL, 1474 &ip_mod_info 1475 }; 1476 1477 static struct qinit lwinit = { 1478 (pfi_t)ip_lwput, NULL, ip_open, ip_close, NULL, 1479 &ip_mod_info 1480 }; 1481 1482 struct streamtab ipinfo = { 1483 &rinit, &winit, &lrinit, &lwinit 1484 }; 1485 1486 #ifdef DEBUG 1487 static boolean_t skip_sctp_cksum = B_FALSE; 1488 #endif 1489 /* 1490 * Copy an M_CTL-tagged message, preserving reference counts appropriately. 1491 */ 1492 mblk_t * 1493 ip_copymsg(mblk_t *mp) 1494 { 1495 mblk_t *nmp; 1496 ipsec_info_t *in; 1497 1498 if (mp->b_datap->db_type != M_CTL) 1499 return (copymsg(mp)); 1500 1501 in = (ipsec_info_t *)mp->b_rptr; 1502 1503 /* 1504 * Note that M_CTL is also used for delivering ICMP error messages 1505 * upstream to transport layers. 1506 */ 1507 if (in->ipsec_info_type != IPSEC_OUT && 1508 in->ipsec_info_type != IPSEC_IN) 1509 return (copymsg(mp)); 1510 1511 nmp = copymsg(mp->b_cont); 1512 1513 if (in->ipsec_info_type == IPSEC_OUT) 1514 return (ipsec_out_tag(mp, nmp)); 1515 else 1516 return (ipsec_in_tag(mp, nmp)); 1517 } 1518 1519 /* Generate an ICMP fragmentation needed message. */ 1520 static void 1521 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu) 1522 { 1523 icmph_t icmph; 1524 mblk_t *first_mp; 1525 boolean_t mctl_present; 1526 1527 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 1528 1529 if (!(mp = icmp_pkt_err_ok(mp))) { 1530 if (mctl_present) 1531 freeb(first_mp); 1532 return; 1533 } 1534 1535 bzero(&icmph, sizeof (icmph_t)); 1536 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1537 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1538 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1539 BUMP_MIB(&icmp_mib, icmpOutFragNeeded); 1540 BUMP_MIB(&icmp_mib, icmpOutDestUnreachs); 1541 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present); 1542 } 1543 1544 /* 1545 * icmp_inbound deals with ICMP messages in the following ways. 1546 * 1547 * 1) It needs to send a reply back and possibly delivering it 1548 * to the "interested" upper clients. 1549 * 2) It needs to send it to the upper clients only. 1550 * 3) It needs to change some values in IP only. 1551 * 4) It needs to change some values in IP and upper layers e.g TCP. 1552 * 1553 * We need to accomodate icmp messages coming in clear until we get 1554 * everything secure from the wire. If icmp_accept_clear_messages 1555 * is zero we check with the global policy and act accordingly. If 1556 * it is non-zero, we accept the message without any checks. But 1557 * *this does not mean* that this will be delivered to the upper 1558 * clients. By accepting we might send replies back, change our MTU 1559 * value etc. but delivery to the ULP/clients depends on their policy 1560 * dispositions. 1561 * 1562 * We handle the above 4 cases in the context of IPSEC in the 1563 * following way : 1564 * 1565 * 1) Send the reply back in the same way as the request came in. 1566 * If it came in encrypted, it goes out encrypted. If it came in 1567 * clear, it goes out in clear. Thus, this will prevent chosen 1568 * plain text attack. 1569 * 2) The client may or may not expect things to come in secure. 1570 * If it comes in secure, the policy constraints are checked 1571 * before delivering it to the upper layers. If it comes in 1572 * clear, ipsec_inbound_accept_clear will decide whether to 1573 * accept this in clear or not. In both the cases, if the returned 1574 * message (IP header + 8 bytes) that caused the icmp message has 1575 * AH/ESP headers, it is sent up to AH/ESP for validation before 1576 * sending up. If there are only 8 bytes of returned message, then 1577 * upper client will not be notified. 1578 * 3) Check with global policy to see whether it matches the constaints. 1579 * But this will be done only if icmp_accept_messages_in_clear is 1580 * zero. 1581 * 4) If we need to change both in IP and ULP, then the decision taken 1582 * while affecting the values in IP and while delivering up to TCP 1583 * should be the same. 1584 * 1585 * There are two cases. 1586 * 1587 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1588 * failed), we will not deliver it to the ULP, even though they 1589 * are *willing* to accept in *clear*. This is fine as our global 1590 * disposition to icmp messages asks us reject the datagram. 1591 * 1592 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1593 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1594 * to deliver it to ULP (policy failed), it can lead to 1595 * consistency problems. The cases known at this time are 1596 * ICMP_DESTINATION_UNREACHABLE messages with following code 1597 * values : 1598 * 1599 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1600 * and Upper layer rejects. Then the communication will 1601 * come to a stop. This is solved by making similar decisions 1602 * at both levels. Currently, when we are unable to deliver 1603 * to the Upper Layer (due to policy failures) while IP has 1604 * adjusted ire_max_frag, the next outbound datagram would 1605 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1606 * will be with the right level of protection. Thus the right 1607 * value will be communicated even if we are not able to 1608 * communicate when we get from the wire initially. But this 1609 * assumes there would be at least one outbound datagram after 1610 * IP has adjusted its ire_max_frag value. To make things 1611 * simpler, we accept in clear after the validation of 1612 * AH/ESP headers. 1613 * 1614 * - Other ICMP ERRORS : We may not be able to deliver it to the 1615 * upper layer depending on the level of protection the upper 1616 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1617 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1618 * should be accepted in clear when the Upper layer expects secure. 1619 * Thus the communication may get aborted by some bad ICMP 1620 * packets. 1621 * 1622 * IPQoS Notes: 1623 * The only instance when a packet is sent for processing is when there 1624 * isn't an ICMP client and if we are interested in it. 1625 * If there is a client, IPPF processing will take place in the 1626 * ip_fanout_proto routine. 1627 * 1628 * Zones notes: 1629 * The packet is only processed in the context of the specified zone: typically 1630 * only this zone will reply to an echo request, and only interested clients in 1631 * this zone will receive a copy of the packet. This means that the caller must 1632 * call icmp_inbound() for each relevant zone. 1633 */ 1634 static void 1635 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, 1636 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, 1637 ill_t *recv_ill, zoneid_t zoneid) 1638 { 1639 icmph_t *icmph; 1640 ipha_t *ipha; 1641 int iph_hdr_length; 1642 int hdr_length; 1643 boolean_t interested; 1644 uint32_t ts; 1645 uchar_t *wptr; 1646 ipif_t *ipif; 1647 mblk_t *first_mp; 1648 ipsec_in_t *ii; 1649 ire_t *src_ire; 1650 boolean_t onlink; 1651 timestruc_t now; 1652 uint32_t ill_index; 1653 1654 ASSERT(ill != NULL); 1655 1656 first_mp = mp; 1657 if (mctl_present) { 1658 mp = first_mp->b_cont; 1659 ASSERT(mp != NULL); 1660 } 1661 1662 ipha = (ipha_t *)mp->b_rptr; 1663 if (icmp_accept_clear_messages == 0) { 1664 first_mp = ipsec_check_global_policy(first_mp, NULL, 1665 ipha, NULL, mctl_present); 1666 if (first_mp == NULL) 1667 return; 1668 } 1669 /* 1670 * We have accepted the ICMP message. It means that we will 1671 * respond to the packet if needed. It may not be delivered 1672 * to the upper client depending on the policy constraints 1673 * and the disposition in ipsec_inbound_accept_clear. 1674 */ 1675 1676 ASSERT(ill != NULL); 1677 1678 BUMP_MIB(&icmp_mib, icmpInMsgs); 1679 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1680 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { 1681 /* Last chance to get real. */ 1682 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { 1683 BUMP_MIB(&icmp_mib, icmpInErrors); 1684 freemsg(first_mp); 1685 return; 1686 } 1687 /* Refresh iph following the pullup. */ 1688 ipha = (ipha_t *)mp->b_rptr; 1689 } 1690 /* ICMP header checksum, including checksum field, should be zero. */ 1691 if (sum_valid ? (sum != 0 && sum != 0xFFFF) : 1692 IP_CSUM(mp, iph_hdr_length, 0)) { 1693 BUMP_MIB(&icmp_mib, icmpInCksumErrs); 1694 freemsg(first_mp); 1695 return; 1696 } 1697 /* The IP header will always be a multiple of four bytes */ 1698 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1699 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, 1700 icmph->icmph_code)); 1701 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1702 /* We will set "interested" to "true" if we want a copy */ 1703 interested = B_FALSE; 1704 switch (icmph->icmph_type) { 1705 case ICMP_ECHO_REPLY: 1706 BUMP_MIB(&icmp_mib, icmpInEchoReps); 1707 break; 1708 case ICMP_DEST_UNREACHABLE: 1709 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1710 BUMP_MIB(&icmp_mib, icmpInFragNeeded); 1711 interested = B_TRUE; /* Pass up to transport */ 1712 BUMP_MIB(&icmp_mib, icmpInDestUnreachs); 1713 break; 1714 case ICMP_SOURCE_QUENCH: 1715 interested = B_TRUE; /* Pass up to transport */ 1716 BUMP_MIB(&icmp_mib, icmpInSrcQuenchs); 1717 break; 1718 case ICMP_REDIRECT: 1719 if (!ip_ignore_redirect) 1720 interested = B_TRUE; 1721 BUMP_MIB(&icmp_mib, icmpInRedirects); 1722 break; 1723 case ICMP_ECHO_REQUEST: 1724 /* 1725 * Whether to respond to echo requests that come in as IP 1726 * broadcasts or as IP multicast is subject to debate 1727 * (what isn't?). We aim to please, you pick it. 1728 * Default is do it. 1729 */ 1730 if (!broadcast && !CLASSD(ipha->ipha_dst)) { 1731 /* unicast: always respond */ 1732 interested = B_TRUE; 1733 } else if (CLASSD(ipha->ipha_dst)) { 1734 /* multicast: respond based on tunable */ 1735 interested = ip_g_resp_to_echo_mcast; 1736 } else if (broadcast) { 1737 /* broadcast: respond based on tunable */ 1738 interested = ip_g_resp_to_echo_bcast; 1739 } 1740 BUMP_MIB(&icmp_mib, icmpInEchos); 1741 break; 1742 case ICMP_ROUTER_ADVERTISEMENT: 1743 case ICMP_ROUTER_SOLICITATION: 1744 break; 1745 case ICMP_TIME_EXCEEDED: 1746 interested = B_TRUE; /* Pass up to transport */ 1747 BUMP_MIB(&icmp_mib, icmpInTimeExcds); 1748 break; 1749 case ICMP_PARAM_PROBLEM: 1750 interested = B_TRUE; /* Pass up to transport */ 1751 BUMP_MIB(&icmp_mib, icmpInParmProbs); 1752 break; 1753 case ICMP_TIME_STAMP_REQUEST: 1754 /* Response to Time Stamp Requests is local policy. */ 1755 if (ip_g_resp_to_timestamp && 1756 /* So is whether to respond if it was an IP broadcast. */ 1757 (!broadcast || ip_g_resp_to_timestamp_bcast)) { 1758 int tstamp_len = 3 * sizeof (uint32_t); 1759 1760 if (wptr + tstamp_len > mp->b_wptr) { 1761 if (!pullupmsg(mp, wptr + tstamp_len - 1762 mp->b_rptr)) { 1763 BUMP_MIB(&ip_mib, ipInDiscards); 1764 freemsg(first_mp); 1765 return; 1766 } 1767 /* Refresh ipha following the pullup. */ 1768 ipha = (ipha_t *)mp->b_rptr; 1769 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1770 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1771 } 1772 interested = B_TRUE; 1773 } 1774 BUMP_MIB(&icmp_mib, icmpInTimestamps); 1775 break; 1776 case ICMP_TIME_STAMP_REPLY: 1777 BUMP_MIB(&icmp_mib, icmpInTimestampReps); 1778 break; 1779 case ICMP_INFO_REQUEST: 1780 /* Per RFC 1122 3.2.2.7, ignore this. */ 1781 case ICMP_INFO_REPLY: 1782 break; 1783 case ICMP_ADDRESS_MASK_REQUEST: 1784 if ((ip_respond_to_address_mask_broadcast || !broadcast) && 1785 /* TODO m_pullup of complete header? */ 1786 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) 1787 interested = B_TRUE; 1788 BUMP_MIB(&icmp_mib, icmpInAddrMasks); 1789 break; 1790 case ICMP_ADDRESS_MASK_REPLY: 1791 BUMP_MIB(&icmp_mib, icmpInAddrMaskReps); 1792 break; 1793 default: 1794 interested = B_TRUE; /* Pass up to transport */ 1795 BUMP_MIB(&icmp_mib, icmpInUnknowns); 1796 break; 1797 } 1798 /* See if there is an ICMP client. */ 1799 if (ipcl_proto_search(IPPROTO_ICMP) != NULL) { 1800 /* If there is an ICMP client and we want one too, copy it. */ 1801 mblk_t *first_mp1; 1802 1803 if (!interested) { 1804 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, 1805 ip_policy, recv_ill, zoneid); 1806 return; 1807 } 1808 first_mp1 = ip_copymsg(first_mp); 1809 if (first_mp1 != NULL) { 1810 ip_fanout_proto(q, first_mp1, ill, ipha, 1811 0, mctl_present, ip_policy, recv_ill, zoneid); 1812 } 1813 } else if (!interested) { 1814 freemsg(first_mp); 1815 return; 1816 } else { 1817 /* 1818 * Initiate policy processing for this packet if ip_policy 1819 * is true. 1820 */ 1821 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 1822 ill_index = ill->ill_phyint->phyint_ifindex; 1823 ip_process(IPP_LOCAL_IN, &mp, ill_index); 1824 if (mp == NULL) { 1825 if (mctl_present) { 1826 freeb(first_mp); 1827 } 1828 BUMP_MIB(&icmp_mib, icmpInErrors); 1829 return; 1830 } 1831 } 1832 } 1833 /* We want to do something with it. */ 1834 /* Check db_ref to make sure we can modify the packet. */ 1835 if (mp->b_datap->db_ref > 1) { 1836 mblk_t *first_mp1; 1837 1838 first_mp1 = ip_copymsg(first_mp); 1839 freemsg(first_mp); 1840 if (!first_mp1) { 1841 BUMP_MIB(&icmp_mib, icmpOutDrops); 1842 return; 1843 } 1844 first_mp = first_mp1; 1845 if (mctl_present) { 1846 mp = first_mp->b_cont; 1847 ASSERT(mp != NULL); 1848 } else { 1849 mp = first_mp; 1850 } 1851 ipha = (ipha_t *)mp->b_rptr; 1852 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1853 wptr = (uchar_t *)icmph + ICMPH_SIZE; 1854 } 1855 switch (icmph->icmph_type) { 1856 case ICMP_ADDRESS_MASK_REQUEST: 1857 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1858 if (ipif == NULL) { 1859 freemsg(first_mp); 1860 return; 1861 } 1862 /* 1863 * outging interface must be IPv4 1864 */ 1865 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1866 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1867 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); 1868 ipif_refrele(ipif); 1869 BUMP_MIB(&icmp_mib, icmpOutAddrMaskReps); 1870 break; 1871 case ICMP_ECHO_REQUEST: 1872 icmph->icmph_type = ICMP_ECHO_REPLY; 1873 BUMP_MIB(&icmp_mib, icmpOutEchoReps); 1874 break; 1875 case ICMP_TIME_STAMP_REQUEST: { 1876 uint32_t *tsp; 1877 1878 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1879 tsp = (uint32_t *)wptr; 1880 tsp++; /* Skip past 'originate time' */ 1881 /* Compute # of milliseconds since midnight */ 1882 gethrestime(&now); 1883 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1884 now.tv_nsec / (NANOSEC / MILLISEC); 1885 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1886 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1887 BUMP_MIB(&icmp_mib, icmpOutTimestampReps); 1888 break; 1889 } 1890 default: 1891 ipha = (ipha_t *)&icmph[1]; 1892 if ((uchar_t *)&ipha[1] > mp->b_wptr) { 1893 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { 1894 BUMP_MIB(&ip_mib, ipInDiscards); 1895 freemsg(first_mp); 1896 return; 1897 } 1898 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1899 ipha = (ipha_t *)&icmph[1]; 1900 } 1901 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { 1902 BUMP_MIB(&ip_mib, ipInDiscards); 1903 freemsg(first_mp); 1904 return; 1905 } 1906 hdr_length = IPH_HDR_LENGTH(ipha); 1907 if (hdr_length < sizeof (ipha_t)) { 1908 BUMP_MIB(&ip_mib, ipInDiscards); 1909 freemsg(first_mp); 1910 return; 1911 } 1912 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 1913 if (!pullupmsg(mp, 1914 (uchar_t *)ipha + hdr_length - mp->b_rptr)) { 1915 BUMP_MIB(&ip_mib, ipInDiscards); 1916 freemsg(first_mp); 1917 return; 1918 } 1919 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1920 ipha = (ipha_t *)&icmph[1]; 1921 } 1922 switch (icmph->icmph_type) { 1923 case ICMP_REDIRECT: 1924 /* 1925 * As there is no upper client to deliver, we don't 1926 * need the first_mp any more. 1927 */ 1928 if (mctl_present) { 1929 freeb(first_mp); 1930 } 1931 icmp_redirect(mp); 1932 return; 1933 case ICMP_DEST_UNREACHABLE: 1934 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 1935 if (!icmp_inbound_too_big(icmph, ipha)) { 1936 freemsg(first_mp); 1937 return; 1938 } 1939 } 1940 /* FALLTHRU */ 1941 default : 1942 /* 1943 * IPQoS notes: Since we have already done IPQoS 1944 * processing we don't want to do it again in 1945 * the fanout routines called by 1946 * icmp_inbound_error_fanout, hence the last 1947 * argument, ip_policy, is B_FALSE. 1948 */ 1949 icmp_inbound_error_fanout(q, ill, first_mp, icmph, 1950 ipha, iph_hdr_length, hdr_length, mctl_present, 1951 B_FALSE, recv_ill, zoneid); 1952 } 1953 return; 1954 } 1955 /* Send out an ICMP packet */ 1956 icmph->icmph_checksum = 0; 1957 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); 1958 if (icmph->icmph_checksum == 0) 1959 icmph->icmph_checksum = 0xFFFF; 1960 if (broadcast || CLASSD(ipha->ipha_dst)) { 1961 ipif_t *ipif_chosen; 1962 /* 1963 * Make it look like it was directed to us, so we don't look 1964 * like a fool with a broadcast or multicast source address. 1965 */ 1966 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1967 /* 1968 * Make sure that we haven't grabbed an interface that's DOWN. 1969 */ 1970 if (ipif != NULL) { 1971 ipif_chosen = ipif_select_source(ipif->ipif_ill, 1972 ipha->ipha_src, zoneid); 1973 if (ipif_chosen != NULL) { 1974 ipif_refrele(ipif); 1975 ipif = ipif_chosen; 1976 } 1977 } 1978 if (ipif == NULL) { 1979 ip0dbg(("icmp_inbound: " 1980 "No source for broadcast/multicast:\n" 1981 "\tsrc 0x%x dst 0x%x ill %p " 1982 "ipif_lcl_addr 0x%x\n", 1983 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), 1984 (void *)ill, 1985 ill->ill_ipif->ipif_lcl_addr)); 1986 freemsg(first_mp); 1987 return; 1988 } 1989 ASSERT(ipif != NULL && !ipif->ipif_isv6); 1990 ipha->ipha_dst = ipif->ipif_src_addr; 1991 ipif_refrele(ipif); 1992 } 1993 /* Reset time to live. */ 1994 ipha->ipha_ttl = ip_def_ttl; 1995 { 1996 /* Swap source and destination addresses */ 1997 ipaddr_t tmp; 1998 1999 tmp = ipha->ipha_src; 2000 ipha->ipha_src = ipha->ipha_dst; 2001 ipha->ipha_dst = tmp; 2002 } 2003 ipha->ipha_ident = 0; 2004 if (!IS_SIMPLE_IPH(ipha)) 2005 icmp_options_update(ipha); 2006 2007 /* 2008 * ICMP echo replies should go out on the same interface 2009 * the request came on as probes used by in.mpathd for detecting 2010 * NIC failures are ECHO packets. We turn-off load spreading 2011 * by setting ipsec_in_attach_if to B_TRUE, which is copied 2012 * to ipsec_out_attach_if by ipsec_in_to_out called later in this 2013 * function. This is in turn handled by ip_wput and ip_newroute 2014 * to make sure that the packet goes out on the interface it came 2015 * in on. If we don't turnoff load spreading, the packets might get 2016 * dropped if there are no non-FAILED/INACTIVE interfaces for it 2017 * to go out and in.mpathd would wrongly detect a failure or 2018 * mis-detect a NIC failure for link failure. As load spreading 2019 * can happen only if ill_group is not NULL, we do only for 2020 * that case and this does not affect the normal case. 2021 * 2022 * We turn off load spreading only on echo packets that came from 2023 * on-link hosts. If the interface route has been deleted, this will 2024 * not be enforced as we can't do much. For off-link hosts, as the 2025 * default routes in IPv4 does not typically have an ire_ipif 2026 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. 2027 * Moreover, expecting a default route through this interface may 2028 * not be correct. We use ipha_dst because of the swap above. 2029 */ 2030 onlink = B_FALSE; 2031 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { 2032 /* 2033 * First, we need to make sure that it is not one of our 2034 * local addresses. If we set onlink when it is one of 2035 * our local addresses, we will end up creating IRE_CACHES 2036 * for one of our local addresses. Then, we will never 2037 * accept packets for them afterwards. 2038 */ 2039 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, 2040 NULL, ALL_ZONES, MATCH_IRE_TYPE); 2041 if (src_ire == NULL) { 2042 ipif = ipif_get_next_ipif(NULL, ill); 2043 if (ipif == NULL) { 2044 BUMP_MIB(&ip_mib, ipInDiscards); 2045 freemsg(mp); 2046 return; 2047 } 2048 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 2049 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 2050 MATCH_IRE_ILL | MATCH_IRE_TYPE); 2051 ipif_refrele(ipif); 2052 if (src_ire != NULL) { 2053 onlink = B_TRUE; 2054 ire_refrele(src_ire); 2055 } 2056 } else { 2057 ire_refrele(src_ire); 2058 } 2059 } 2060 if (!mctl_present) { 2061 /* 2062 * This packet should go out the same way as it 2063 * came in i.e in clear. To make sure that global 2064 * policy will not be applied to this in ip_wput_ire, 2065 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 2066 */ 2067 ASSERT(first_mp == mp); 2068 if ((first_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 2069 BUMP_MIB(&ip_mib, ipInDiscards); 2070 freemsg(mp); 2071 return; 2072 } 2073 ii = (ipsec_in_t *)first_mp->b_rptr; 2074 2075 /* This is not a secure packet */ 2076 ii->ipsec_in_secure = B_FALSE; 2077 if (onlink) { 2078 ii->ipsec_in_attach_if = B_TRUE; 2079 ii->ipsec_in_ill_index = 2080 ill->ill_phyint->phyint_ifindex; 2081 ii->ipsec_in_rill_index = 2082 recv_ill->ill_phyint->phyint_ifindex; 2083 } 2084 first_mp->b_cont = mp; 2085 } else if (onlink) { 2086 ii = (ipsec_in_t *)first_mp->b_rptr; 2087 ii->ipsec_in_attach_if = B_TRUE; 2088 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 2089 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; 2090 } else { 2091 ii = (ipsec_in_t *)first_mp->b_rptr; 2092 } 2093 ii->ipsec_in_zoneid = zoneid; 2094 if (!ipsec_in_to_out(first_mp, ipha, NULL)) { 2095 BUMP_MIB(&ip_mib, ipInDiscards); 2096 return; 2097 } 2098 BUMP_MIB(&icmp_mib, icmpOutMsgs); 2099 put(WR(q), first_mp); 2100 } 2101 2102 /* Table from RFC 1191 */ 2103 static int icmp_frag_size_table[] = 2104 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 2105 2106 /* 2107 * Process received ICMP Packet too big. 2108 * After updating any IRE it does the fanout to any matching transport streams. 2109 * Assumes the message has been pulled up till the IP header that caused 2110 * the error. 2111 * 2112 * Returns B_FALSE on failure and B_TRUE on success. 2113 */ 2114 static boolean_t 2115 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha) 2116 { 2117 ire_t *ire, *first_ire; 2118 int mtu; 2119 int hdr_length; 2120 2121 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 2122 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 2123 2124 hdr_length = IPH_HDR_LENGTH(ipha); 2125 2126 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, NULL, 2127 ALL_ZONES, MATCH_IRE_TYPE); 2128 2129 if (!first_ire) { 2130 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", 2131 ntohl(ipha->ipha_dst))); 2132 return (B_FALSE); 2133 } 2134 /* Drop if the original packet contained a source route */ 2135 if (ip_source_route_included(ipha)) { 2136 ire_refrele(first_ire); 2137 return (B_FALSE); 2138 } 2139 /* Check for MTU discovery advice as described in RFC 1191 */ 2140 mtu = ntohs(icmph->icmph_du_mtu); 2141 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 2142 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; 2143 ire = ire->ire_next) { 2144 mutex_enter(&ire->ire_lock); 2145 if (icmph->icmph_du_zero == 0 && mtu > 68) { 2146 /* Reduce the IRE max frag value as advised. */ 2147 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2148 ip1dbg(("Received mtu from router: %d\n", mtu)); 2149 } else { 2150 uint32_t length; 2151 int i; 2152 2153 /* 2154 * Use the table from RFC 1191 to figure out 2155 * the next "plateau" based on the length in 2156 * the original IP packet. 2157 */ 2158 length = ntohs(ipha->ipha_length); 2159 if (ire->ire_max_frag <= length && 2160 ire->ire_max_frag >= length - hdr_length) { 2161 /* 2162 * Handle broken BSD 4.2 systems that 2163 * return the wrong iph_length in ICMP 2164 * errors. 2165 */ 2166 ip1dbg(("Wrong mtu: sent %d, ire %d\n", 2167 length, ire->ire_max_frag)); 2168 length -= hdr_length; 2169 } 2170 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 2171 if (length > icmp_frag_size_table[i]) 2172 break; 2173 } 2174 if (i == A_CNT(icmp_frag_size_table)) { 2175 /* Smaller than 68! */ 2176 ip1dbg(("Too big for packet size %d\n", 2177 length)); 2178 ire->ire_max_frag = MIN(ire->ire_max_frag, 576); 2179 ire->ire_frag_flag = 0; 2180 } else { 2181 mtu = icmp_frag_size_table[i]; 2182 ip1dbg(("Calculated mtu %d, packet size %d, " 2183 "before %d", mtu, length, 2184 ire->ire_max_frag)); 2185 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 2186 ip1dbg((", after %d\n", ire->ire_max_frag)); 2187 } 2188 /* Record the new max frag size for the ULP. */ 2189 icmph->icmph_du_zero = 0; 2190 icmph->icmph_du_mtu = 2191 htons((uint16_t)ire->ire_max_frag); 2192 } 2193 mutex_exit(&ire->ire_lock); 2194 } 2195 rw_exit(&first_ire->ire_bucket->irb_lock); 2196 ire_refrele(first_ire); 2197 return (B_TRUE); 2198 } 2199 2200 /* 2201 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout 2202 * calls this function. 2203 */ 2204 static mblk_t * 2205 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) 2206 { 2207 ipha_t *ipha; 2208 icmph_t *icmph; 2209 ipha_t *in_ipha; 2210 int length; 2211 2212 ASSERT(mp->b_datap->db_type == M_DATA); 2213 2214 /* 2215 * For Self-encapsulated packets, we added an extra IP header 2216 * without the options. Inner IP header is the one from which 2217 * the outer IP header was formed. Thus, we need to remove the 2218 * outer IP header. To do this, we pullup the whole message 2219 * and overlay whatever follows the outer IP header over the 2220 * outer IP header. 2221 */ 2222 2223 if (!pullupmsg(mp, -1)) { 2224 BUMP_MIB(&ip_mib, ipInDiscards); 2225 return (NULL); 2226 } 2227 2228 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2229 ipha = (ipha_t *)&icmph[1]; 2230 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2231 2232 /* 2233 * The length that we want to overlay is following the inner 2234 * IP header. Subtracting the IP header + icmp header + outer 2235 * IP header's length should give us the length that we want to 2236 * overlay. 2237 */ 2238 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - 2239 hdr_length; 2240 /* 2241 * Overlay whatever follows the inner header over the 2242 * outer header. 2243 */ 2244 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2245 2246 /* Set the wptr to account for the outer header */ 2247 mp->b_wptr -= hdr_length; 2248 return (mp); 2249 } 2250 2251 /* 2252 * Try to pass the ICMP message upstream in case the ULP cares. 2253 * 2254 * If the packet that caused the ICMP error is secure, we send 2255 * it to AH/ESP to make sure that the attached packet has a 2256 * valid association. ipha in the code below points to the 2257 * IP header of the packet that caused the error. 2258 * 2259 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently 2260 * in the context of IPSEC. Normally we tell the upper layer 2261 * whenever we send the ire (including ip_bind), the IPSEC header 2262 * length in ire_ipsec_overhead. TCP can deduce the MSS as it 2263 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. 2264 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the 2265 * same thing. As TCP has the IPSEC options size that needs to be 2266 * adjusted, we just pass the MTU unchanged. 2267 * 2268 * IFN could have been generated locally or by some router. 2269 * 2270 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. 2271 * This happens because IP adjusted its value of MTU on an 2272 * earlier IFN message and could not tell the upper layer, 2273 * the new adjusted value of MTU e.g. Packet was encrypted 2274 * or there was not enough information to fanout to upper 2275 * layers. Thus on the next outbound datagram, ip_wput_ire 2276 * generates the IFN, where IPSEC processing has *not* been 2277 * done. 2278 * 2279 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed 2280 * could have generated this. This happens because ire_max_frag 2281 * value in IP was set to a new value, while the IPSEC processing 2282 * was being done and after we made the fragmentation check in 2283 * ip_wput_ire. Thus on return from IPSEC processing, 2284 * ip_wput_ipsec_out finds that the new length is > ire_max_frag 2285 * and generates the IFN. As IPSEC processing is over, we fanout 2286 * to AH/ESP to remove the header. 2287 * 2288 * In both these cases, ipsec_in_loopback will be set indicating 2289 * that IFN was generated locally. 2290 * 2291 * ROUTER : IFN could be secure or non-secure. 2292 * 2293 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2294 * packet in error has AH/ESP headers to validate the AH/ESP 2295 * headers. AH/ESP will verify whether there is a valid SA or 2296 * not and send it back. We will fanout again if we have more 2297 * data in the packet. 2298 * 2299 * If the packet in error does not have AH/ESP, we handle it 2300 * like any other case. 2301 * 2302 * * NON_SECURE : If the packet in error has AH/ESP headers, 2303 * we attach a dummy ipsec_in and send it up to AH/ESP 2304 * for validation. AH/ESP will verify whether there is a 2305 * valid SA or not and send it back. We will fanout again if 2306 * we have more data in the packet. 2307 * 2308 * If the packet in error does not have AH/ESP, we handle it 2309 * like any other case. 2310 */ 2311 static void 2312 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, 2313 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, 2314 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 2315 zoneid_t zoneid) 2316 { 2317 uint16_t *up; /* Pointer to ports in ULP header */ 2318 uint32_t ports; /* reversed ports for fanout */ 2319 ipha_t ripha; /* With reversed addresses */ 2320 mblk_t *first_mp; 2321 ipsec_in_t *ii; 2322 tcph_t *tcph; 2323 conn_t *connp; 2324 2325 first_mp = mp; 2326 if (mctl_present) { 2327 mp = first_mp->b_cont; 2328 ASSERT(mp != NULL); 2329 2330 ii = (ipsec_in_t *)first_mp->b_rptr; 2331 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2332 } else { 2333 ii = NULL; 2334 } 2335 2336 switch (ipha->ipha_protocol) { 2337 case IPPROTO_UDP: 2338 /* 2339 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2340 * transport header. 2341 */ 2342 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2343 mp->b_wptr) { 2344 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2345 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2346 BUMP_MIB(&ip_mib, ipInDiscards); 2347 goto drop_pkt; 2348 } 2349 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2350 ipha = (ipha_t *)&icmph[1]; 2351 } 2352 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2353 2354 /* 2355 * Attempt to find a client stream based on port. 2356 * Note that we do a reverse lookup since the header is 2357 * in the form we sent it out. 2358 * The ripha header is only used for the IP_UDP_MATCH and we 2359 * only set the src and dst addresses and protocol. 2360 */ 2361 ripha.ipha_src = ipha->ipha_dst; 2362 ripha.ipha_dst = ipha->ipha_src; 2363 ripha.ipha_protocol = ipha->ipha_protocol; 2364 ((uint16_t *)&ports)[0] = up[1]; 2365 ((uint16_t *)&ports)[1] = up[0]; 2366 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", 2367 ntohl(ipha->ipha_src), ntohs(up[0]), 2368 ntohl(ipha->ipha_dst), ntohs(up[1]), 2369 icmph->icmph_type, icmph->icmph_code)); 2370 2371 /* Have to change db_type after any pullupmsg */ 2372 DB_TYPE(mp) = M_CTL; 2373 2374 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, 2375 mctl_present, ip_policy, recv_ill, zoneid); 2376 return; 2377 2378 case IPPROTO_TCP: 2379 /* 2380 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2381 * transport header. 2382 */ 2383 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2384 mp->b_wptr) { 2385 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2386 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2387 BUMP_MIB(&ip_mib, ipInDiscards); 2388 goto drop_pkt; 2389 } 2390 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2391 ipha = (ipha_t *)&icmph[1]; 2392 } 2393 /* 2394 * Find a TCP client stream for this packet. 2395 * Note that we do a reverse lookup since the header is 2396 * in the form we sent it out. 2397 */ 2398 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); 2399 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN); 2400 if (connp == NULL) { 2401 BUMP_MIB(&ip_mib, ipInDiscards); 2402 goto drop_pkt; 2403 } 2404 2405 /* Have to change db_type after any pullupmsg */ 2406 DB_TYPE(mp) = M_CTL; 2407 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 2408 connp, SQTAG_TCP_INPUT_ICMP_ERR); 2409 return; 2410 2411 case IPPROTO_SCTP: 2412 /* 2413 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 2414 * transport header. 2415 */ 2416 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 2417 mp->b_wptr) { 2418 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 2419 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { 2420 BUMP_MIB(&ip_mib, ipInDiscards); 2421 goto drop_pkt; 2422 } 2423 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2424 ipha = (ipha_t *)&icmph[1]; 2425 } 2426 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2427 /* 2428 * Find a SCTP client stream for this packet. 2429 * Note that we do a reverse lookup since the header is 2430 * in the form we sent it out. 2431 * The ripha header is only used for the matching and we 2432 * only set the src and dst addresses, protocol, and version. 2433 */ 2434 ripha.ipha_src = ipha->ipha_dst; 2435 ripha.ipha_dst = ipha->ipha_src; 2436 ripha.ipha_protocol = ipha->ipha_protocol; 2437 ripha.ipha_version_and_hdr_length = 2438 ipha->ipha_version_and_hdr_length; 2439 ((uint16_t *)&ports)[0] = up[1]; 2440 ((uint16_t *)&ports)[1] = up[0]; 2441 2442 /* Have to change db_type after any pullupmsg */ 2443 DB_TYPE(mp) = M_CTL; 2444 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, 2445 mctl_present, ip_policy, 0, zoneid); 2446 return; 2447 2448 case IPPROTO_ESP: 2449 case IPPROTO_AH: { 2450 int ipsec_rc; 2451 2452 /* 2453 * We need a IPSEC_IN in the front to fanout to AH/ESP. 2454 * We will re-use the IPSEC_IN if it is already present as 2455 * AH/ESP will not affect any fields in the IPSEC_IN for 2456 * ICMP errors. If there is no IPSEC_IN, allocate a new 2457 * one and attach it in the front. 2458 */ 2459 if (ii != NULL) { 2460 /* 2461 * ip_fanout_proto_again converts the ICMP errors 2462 * that come back from AH/ESP to M_DATA so that 2463 * if it is non-AH/ESP and we do a pullupmsg in 2464 * this function, it would work. Convert it back 2465 * to M_CTL before we send up as this is a ICMP 2466 * error. This could have been generated locally or 2467 * by some router. Validate the inner IPSEC 2468 * headers. 2469 * 2470 * NOTE : ill_index is used by ip_fanout_proto_again 2471 * to locate the ill. 2472 */ 2473 ASSERT(ill != NULL); 2474 ii->ipsec_in_ill_index = 2475 ill->ill_phyint->phyint_ifindex; 2476 ii->ipsec_in_rill_index = 2477 recv_ill->ill_phyint->phyint_ifindex; 2478 DB_TYPE(first_mp->b_cont) = M_CTL; 2479 } else { 2480 /* 2481 * IPSEC_IN is not present. We attach a ipsec_in 2482 * message and send up to IPSEC for validating 2483 * and removing the IPSEC headers. Clear 2484 * ipsec_in_secure so that when we return 2485 * from IPSEC, we don't mistakenly think that this 2486 * is a secure packet came from the network. 2487 * 2488 * NOTE : ill_index is used by ip_fanout_proto_again 2489 * to locate the ill. 2490 */ 2491 ASSERT(first_mp == mp); 2492 first_mp = ipsec_in_alloc(B_TRUE); 2493 if (first_mp == NULL) { 2494 freemsg(mp); 2495 BUMP_MIB(&ip_mib, ipInDiscards); 2496 return; 2497 } 2498 ii = (ipsec_in_t *)first_mp->b_rptr; 2499 2500 /* This is not a secure packet */ 2501 ii->ipsec_in_secure = B_FALSE; 2502 first_mp->b_cont = mp; 2503 DB_TYPE(mp) = M_CTL; 2504 ASSERT(ill != NULL); 2505 ii->ipsec_in_ill_index = 2506 ill->ill_phyint->phyint_ifindex; 2507 ii->ipsec_in_rill_index = 2508 recv_ill->ill_phyint->phyint_ifindex; 2509 } 2510 ip2dbg(("icmp_inbound_error: ipsec\n")); 2511 2512 if (!ipsec_loaded()) { 2513 ip_proto_not_sup(q, first_mp, 0, zoneid); 2514 return; 2515 } 2516 2517 if (ipha->ipha_protocol == IPPROTO_ESP) 2518 ipsec_rc = ipsecesp_icmp_error(first_mp); 2519 else 2520 ipsec_rc = ipsecah_icmp_error(first_mp); 2521 if (ipsec_rc == IPSEC_STATUS_FAILED) 2522 return; 2523 2524 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); 2525 return; 2526 } 2527 default: 2528 /* 2529 * The ripha header is only used for the lookup and we 2530 * only set the src and dst addresses and protocol. 2531 */ 2532 ripha.ipha_src = ipha->ipha_dst; 2533 ripha.ipha_dst = ipha->ipha_src; 2534 ripha.ipha_protocol = ipha->ipha_protocol; 2535 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", 2536 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2537 ntohl(ipha->ipha_dst), 2538 icmph->icmph_type, icmph->icmph_code)); 2539 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2540 ipha_t *in_ipha; 2541 2542 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 2543 mp->b_wptr) { 2544 if (!pullupmsg(mp, (uchar_t *)ipha + 2545 hdr_length + sizeof (ipha_t) - 2546 mp->b_rptr)) { 2547 2548 BUMP_MIB(&ip_mib, ipInDiscards); 2549 goto drop_pkt; 2550 } 2551 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2552 ipha = (ipha_t *)&icmph[1]; 2553 } 2554 /* 2555 * Caller has verified that length has to be 2556 * at least the size of IP header. 2557 */ 2558 ASSERT(hdr_length >= sizeof (ipha_t)); 2559 /* 2560 * Check the sanity of the inner IP header like 2561 * we did for the outer header. 2562 */ 2563 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2564 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2565 BUMP_MIB(&ip_mib, ipInDiscards); 2566 goto drop_pkt; 2567 } 2568 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2569 BUMP_MIB(&ip_mib, ipInDiscards); 2570 goto drop_pkt; 2571 } 2572 /* Check for Self-encapsulated tunnels */ 2573 if (in_ipha->ipha_src == ipha->ipha_src && 2574 in_ipha->ipha_dst == ipha->ipha_dst) { 2575 2576 mp = icmp_inbound_self_encap_error(mp, 2577 iph_hdr_length, hdr_length); 2578 if (mp == NULL) 2579 goto drop_pkt; 2580 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2581 ipha = (ipha_t *)&icmph[1]; 2582 hdr_length = IPH_HDR_LENGTH(ipha); 2583 /* 2584 * The packet in error is self-encapsualted. 2585 * And we are finding it further encapsulated 2586 * which we could not have possibly generated. 2587 */ 2588 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2589 BUMP_MIB(&ip_mib, ipInDiscards); 2590 goto drop_pkt; 2591 } 2592 icmp_inbound_error_fanout(q, ill, first_mp, 2593 icmph, ipha, iph_hdr_length, hdr_length, 2594 mctl_present, ip_policy, recv_ill, zoneid); 2595 return; 2596 } 2597 } 2598 if ((ipha->ipha_protocol == IPPROTO_ENCAP || 2599 ipha->ipha_protocol == IPPROTO_IPV6) && 2600 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && 2601 ii != NULL && 2602 ii->ipsec_in_loopback && 2603 ii->ipsec_in_secure) { 2604 /* 2605 * For IP tunnels that get a looped-back 2606 * ICMP_FRAGMENTATION_NEEDED message, adjust the 2607 * reported new MTU to take into account the IPsec 2608 * headers protecting this configured tunnel. 2609 * 2610 * This allows the tunnel module (tun.c) to blindly 2611 * accept the MTU reported in an ICMP "too big" 2612 * message. 2613 * 2614 * Non-looped back ICMP messages will just be 2615 * handled by the security protocols (if needed), 2616 * and the first subsequent packet will hit this 2617 * path. 2618 */ 2619 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - 2620 ipsec_in_extra_length(first_mp)); 2621 } 2622 /* Have to change db_type after any pullupmsg */ 2623 DB_TYPE(mp) = M_CTL; 2624 2625 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, 2626 ip_policy, recv_ill, zoneid); 2627 return; 2628 } 2629 /* NOTREACHED */ 2630 drop_pkt:; 2631 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); 2632 freemsg(first_mp); 2633 } 2634 2635 /* 2636 * Common IP options parser. 2637 * 2638 * Setup routine: fill in *optp with options-parsing state, then 2639 * tail-call ipoptp_next to return the first option. 2640 */ 2641 uint8_t 2642 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2643 { 2644 uint32_t totallen; /* total length of all options */ 2645 2646 totallen = ipha->ipha_version_and_hdr_length - 2647 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2648 totallen <<= 2; 2649 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2650 optp->ipoptp_end = optp->ipoptp_next + totallen; 2651 optp->ipoptp_flags = 0; 2652 return (ipoptp_next(optp)); 2653 } 2654 2655 /* 2656 * Common IP options parser: extract next option. 2657 */ 2658 uint8_t 2659 ipoptp_next(ipoptp_t *optp) 2660 { 2661 uint8_t *end = optp->ipoptp_end; 2662 uint8_t *cur = optp->ipoptp_next; 2663 uint8_t opt, len, pointer; 2664 2665 /* 2666 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2667 * has been corrupted. 2668 */ 2669 ASSERT(cur <= end); 2670 2671 if (cur == end) 2672 return (IPOPT_EOL); 2673 2674 opt = cur[IPOPT_OPTVAL]; 2675 2676 /* 2677 * Skip any NOP options. 2678 */ 2679 while (opt == IPOPT_NOP) { 2680 cur++; 2681 if (cur == end) 2682 return (IPOPT_EOL); 2683 opt = cur[IPOPT_OPTVAL]; 2684 } 2685 2686 if (opt == IPOPT_EOL) 2687 return (IPOPT_EOL); 2688 2689 /* 2690 * Option requiring a length. 2691 */ 2692 if ((cur + 1) >= end) { 2693 optp->ipoptp_flags |= IPOPTP_ERROR; 2694 return (IPOPT_EOL); 2695 } 2696 len = cur[IPOPT_OLEN]; 2697 if (len < 2) { 2698 optp->ipoptp_flags |= IPOPTP_ERROR; 2699 return (IPOPT_EOL); 2700 } 2701 optp->ipoptp_cur = cur; 2702 optp->ipoptp_len = len; 2703 optp->ipoptp_next = cur + len; 2704 if (cur + len > end) { 2705 optp->ipoptp_flags |= IPOPTP_ERROR; 2706 return (IPOPT_EOL); 2707 } 2708 2709 /* 2710 * For the options which require a pointer field, make sure 2711 * its there, and make sure it points to either something 2712 * inside this option, or the end of the option. 2713 */ 2714 switch (opt) { 2715 case IPOPT_RR: 2716 case IPOPT_TS: 2717 case IPOPT_LSRR: 2718 case IPOPT_SSRR: 2719 if (len <= IPOPT_OFFSET) { 2720 optp->ipoptp_flags |= IPOPTP_ERROR; 2721 return (opt); 2722 } 2723 pointer = cur[IPOPT_OFFSET]; 2724 if (pointer - 1 > len) { 2725 optp->ipoptp_flags |= IPOPTP_ERROR; 2726 return (opt); 2727 } 2728 break; 2729 } 2730 2731 /* 2732 * Sanity check the pointer field based on the type of the 2733 * option. 2734 */ 2735 switch (opt) { 2736 case IPOPT_RR: 2737 case IPOPT_SSRR: 2738 case IPOPT_LSRR: 2739 if (pointer < IPOPT_MINOFF_SR) 2740 optp->ipoptp_flags |= IPOPTP_ERROR; 2741 break; 2742 case IPOPT_TS: 2743 if (pointer < IPOPT_MINOFF_IT) 2744 optp->ipoptp_flags |= IPOPTP_ERROR; 2745 /* 2746 * Note that the Internet Timestamp option also 2747 * contains two four bit fields (the Overflow field, 2748 * and the Flag field), which follow the pointer 2749 * field. We don't need to check that these fields 2750 * fall within the length of the option because this 2751 * was implicitely done above. We've checked that the 2752 * pointer value is at least IPOPT_MINOFF_IT, and that 2753 * it falls within the option. Since IPOPT_MINOFF_IT > 2754 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2755 */ 2756 ASSERT(len > IPOPT_POS_OV_FLG); 2757 break; 2758 } 2759 2760 return (opt); 2761 } 2762 2763 /* 2764 * Update any record route or timestamp options to include this host. 2765 * Reverse any source route option. 2766 * This routine assumes that the options are well formed i.e. that they 2767 * have already been checked. 2768 */ 2769 static void 2770 icmp_options_update(ipha_t *ipha) 2771 { 2772 ipoptp_t opts; 2773 uchar_t *opt; 2774 uint8_t optval; 2775 ipaddr_t src; /* Our local address */ 2776 ipaddr_t dst; 2777 2778 ip2dbg(("icmp_options_update\n")); 2779 src = ipha->ipha_src; 2780 dst = ipha->ipha_dst; 2781 2782 for (optval = ipoptp_first(&opts, ipha); 2783 optval != IPOPT_EOL; 2784 optval = ipoptp_next(&opts)) { 2785 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 2786 opt = opts.ipoptp_cur; 2787 ip2dbg(("icmp_options_update: opt %d, len %d\n", 2788 optval, opts.ipoptp_len)); 2789 switch (optval) { 2790 int off1, off2; 2791 case IPOPT_SSRR: 2792 case IPOPT_LSRR: 2793 /* 2794 * Reverse the source route. The first entry 2795 * should be the next to last one in the current 2796 * source route (the last entry is our address). 2797 * The last entry should be the final destination. 2798 */ 2799 off1 = IPOPT_MINOFF_SR - 1; 2800 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 2801 if (off2 < 0) { 2802 /* No entries in source route */ 2803 ip1dbg(( 2804 "icmp_options_update: bad src route\n")); 2805 break; 2806 } 2807 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 2808 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 2809 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 2810 off2 -= IP_ADDR_LEN; 2811 2812 while (off1 < off2) { 2813 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 2814 bcopy((char *)opt + off2, (char *)opt + off1, 2815 IP_ADDR_LEN); 2816 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 2817 off1 += IP_ADDR_LEN; 2818 off2 -= IP_ADDR_LEN; 2819 } 2820 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 2821 break; 2822 } 2823 } 2824 } 2825 2826 /* 2827 * Process received ICMP Redirect messages. 2828 */ 2829 /* ARGSUSED */ 2830 static void 2831 icmp_redirect(mblk_t *mp) 2832 { 2833 ipha_t *ipha; 2834 int iph_hdr_length; 2835 icmph_t *icmph; 2836 ipha_t *ipha_err; 2837 ire_t *ire; 2838 ire_t *prev_ire; 2839 ire_t *save_ire; 2840 ipaddr_t src, dst, gateway; 2841 iulp_t ulp_info = { 0 }; 2842 int error; 2843 2844 ipha = (ipha_t *)mp->b_rptr; 2845 iph_hdr_length = IPH_HDR_LENGTH(ipha); 2846 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < 2847 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { 2848 BUMP_MIB(&icmp_mib, icmpInErrors); 2849 freemsg(mp); 2850 return; 2851 } 2852 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2853 ipha_err = (ipha_t *)&icmph[1]; 2854 src = ipha->ipha_src; 2855 dst = ipha_err->ipha_dst; 2856 gateway = icmph->icmph_rd_gateway; 2857 /* Make sure the new gateway is reachable somehow. */ 2858 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, 2859 ALL_ZONES, MATCH_IRE_TYPE); 2860 /* 2861 * Make sure we had a route for the dest in question and that 2862 * that route was pointing to the old gateway (the source of the 2863 * redirect packet.) 2864 */ 2865 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, 2866 MATCH_IRE_GW); 2867 /* 2868 * Check that 2869 * the redirect was not from ourselves 2870 * the new gateway and the old gateway are directly reachable 2871 */ 2872 if (!prev_ire || 2873 !ire || 2874 ire->ire_type == IRE_LOCAL) { 2875 BUMP_MIB(&icmp_mib, icmpInBadRedirects); 2876 freemsg(mp); 2877 if (ire != NULL) 2878 ire_refrele(ire); 2879 if (prev_ire != NULL) 2880 ire_refrele(prev_ire); 2881 return; 2882 } 2883 2884 /* 2885 * Should we use the old ULP info to create the new gateway? From 2886 * a user's perspective, we should inherit the info so that it 2887 * is a "smooth" transition. If we do not do that, then new 2888 * connections going thru the new gateway will have no route metrics, 2889 * which is counter-intuitive to user. From a network point of 2890 * view, this may or may not make sense even though the new gateway 2891 * is still directly connected to us so the route metrics should not 2892 * change much. 2893 * 2894 * But if the old ire_uinfo is not initialized, we do another 2895 * recursive lookup on the dest using the new gateway. There may 2896 * be a route to that. If so, use it to initialize the redirect 2897 * route. 2898 */ 2899 if (prev_ire->ire_uinfo.iulp_set) { 2900 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 2901 } else { 2902 ire_t *tmp_ire; 2903 ire_t *sire; 2904 2905 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, 2906 ALL_ZONES, 0, 2907 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT)); 2908 if (sire != NULL) { 2909 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 2910 /* 2911 * If sire != NULL, ire_ftable_lookup() should not 2912 * return a NULL value. 2913 */ 2914 ASSERT(tmp_ire != NULL); 2915 ire_refrele(tmp_ire); 2916 ire_refrele(sire); 2917 } else if (tmp_ire != NULL) { 2918 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 2919 sizeof (iulp_t)); 2920 ire_refrele(tmp_ire); 2921 } 2922 } 2923 if (prev_ire->ire_type == IRE_CACHE) 2924 ire_delete(prev_ire); 2925 ire_refrele(prev_ire); 2926 /* 2927 * TODO: more precise handling for cases 0, 2, 3, the latter two 2928 * require TOS routing 2929 */ 2930 switch (icmph->icmph_code) { 2931 case 0: 2932 case 1: 2933 /* TODO: TOS specificity for cases 2 and 3 */ 2934 case 2: 2935 case 3: 2936 break; 2937 default: 2938 freemsg(mp); 2939 BUMP_MIB(&icmp_mib, icmpInBadRedirects); 2940 ire_refrele(ire); 2941 return; 2942 } 2943 /* 2944 * Create a Route Association. This will allow us to remember that 2945 * someone we believe told us to use the particular gateway. 2946 */ 2947 save_ire = ire; 2948 ire = ire_create( 2949 (uchar_t *)&dst, /* dest addr */ 2950 (uchar_t *)&ip_g_all_ones, /* mask */ 2951 (uchar_t *)&save_ire->ire_src_addr, /* source addr */ 2952 (uchar_t *)&gateway, /* gateway addr */ 2953 NULL, /* no in_srcaddr */ 2954 &save_ire->ire_max_frag, /* max frag */ 2955 NULL, /* Fast Path header */ 2956 NULL, /* no rfq */ 2957 NULL, /* no stq */ 2958 IRE_HOST_REDIRECT, 2959 NULL, 2960 NULL, 2961 NULL, 2962 0, 2963 0, 2964 0, 2965 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 2966 &ulp_info); 2967 2968 if (ire == NULL) { 2969 freemsg(mp); 2970 ire_refrele(save_ire); 2971 return; 2972 } 2973 error = ire_add(&ire, NULL, NULL, NULL); 2974 ire_refrele(save_ire); 2975 if (error == 0) { 2976 ire_refrele(ire); /* Held in ire_add_v4 */ 2977 /* tell routing sockets that we received a redirect */ 2978 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 2979 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 2980 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR)); 2981 } 2982 2983 /* 2984 * Delete any existing IRE_HOST_REDIRECT for this destination. 2985 * This together with the added IRE has the effect of 2986 * modifying an existing redirect. 2987 */ 2988 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST_REDIRECT, NULL, NULL, 2989 ALL_ZONES, 0, (MATCH_IRE_GW | MATCH_IRE_TYPE)); 2990 if (prev_ire) { 2991 ire_delete(prev_ire); 2992 ire_refrele(prev_ire); 2993 } 2994 2995 freemsg(mp); 2996 } 2997 2998 /* 2999 * Generate an ICMP parameter problem message. 3000 */ 3001 static void 3002 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr) 3003 { 3004 icmph_t icmph; 3005 boolean_t mctl_present; 3006 mblk_t *first_mp; 3007 3008 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3009 3010 if (!(mp = icmp_pkt_err_ok(mp))) { 3011 if (mctl_present) 3012 freeb(first_mp); 3013 return; 3014 } 3015 3016 bzero(&icmph, sizeof (icmph_t)); 3017 icmph.icmph_type = ICMP_PARAM_PROBLEM; 3018 icmph.icmph_pp_ptr = ptr; 3019 BUMP_MIB(&icmp_mib, icmpOutParmProbs); 3020 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present); 3021 } 3022 3023 /* 3024 * Build and ship an IPv4 ICMP message using the packet data in mp, and 3025 * the ICMP header pointed to by "stuff". (May be called as writer.) 3026 * Note: assumes that icmp_pkt_err_ok has been called to verify that 3027 * an icmp error packet can be sent. 3028 * Assigns an appropriate source address to the packet. If ipha_dst is 3029 * one of our addresses use it for source. Otherwise pick a source based 3030 * on a route lookup back to ipha_src. 3031 * Note that ipha_src must be set here since the 3032 * packet is likely to arrive on an ill queue in ip_wput() which will 3033 * not set a source address. 3034 */ 3035 static void 3036 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, 3037 boolean_t mctl_present) 3038 { 3039 ipaddr_t dst; 3040 icmph_t *icmph; 3041 ipha_t *ipha; 3042 uint_t len_needed; 3043 size_t msg_len; 3044 mblk_t *mp1; 3045 ipaddr_t src; 3046 ire_t *ire; 3047 mblk_t *ipsec_mp; 3048 ipsec_out_t *io = NULL; 3049 boolean_t xmit_if_on = B_FALSE; 3050 zoneid_t zoneid; 3051 3052 if (mctl_present) { 3053 /* 3054 * If it is : 3055 * 3056 * 1) a IPSEC_OUT, then this is caused by outbound 3057 * datagram originating on this host. IPSEC processing 3058 * may or may not have been done. Refer to comments above 3059 * icmp_inbound_error_fanout for details. 3060 * 3061 * 2) a IPSEC_IN if we are generating a icmp_message 3062 * for an incoming datagram destined for us i.e called 3063 * from ip_fanout_send_icmp. 3064 */ 3065 ipsec_info_t *in; 3066 ipsec_mp = mp; 3067 mp = ipsec_mp->b_cont; 3068 3069 in = (ipsec_info_t *)ipsec_mp->b_rptr; 3070 ipha = (ipha_t *)mp->b_rptr; 3071 3072 ASSERT(in->ipsec_info_type == IPSEC_OUT || 3073 in->ipsec_info_type == IPSEC_IN); 3074 3075 if (in->ipsec_info_type == IPSEC_IN) { 3076 /* 3077 * Convert the IPSEC_IN to IPSEC_OUT. 3078 */ 3079 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3080 BUMP_MIB(&ip_mib, ipOutDiscards); 3081 return; 3082 } 3083 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3084 } else { 3085 ASSERT(in->ipsec_info_type == IPSEC_OUT); 3086 io = (ipsec_out_t *)in; 3087 if (io->ipsec_out_xmit_if) 3088 xmit_if_on = B_TRUE; 3089 /* 3090 * Clear out ipsec_out_proc_begin, so we do a fresh 3091 * ire lookup. 3092 */ 3093 io->ipsec_out_proc_begin = B_FALSE; 3094 } 3095 zoneid = io->ipsec_out_zoneid; 3096 ASSERT(zoneid != ALL_ZONES); 3097 } else { 3098 /* 3099 * This is in clear. The icmp message we are building 3100 * here should go out in clear. 3101 * 3102 * Pardon the convolution of it all, but it's easier to 3103 * allocate a "use cleartext" IPSEC_IN message and convert 3104 * it than it is to allocate a new one. 3105 */ 3106 ipsec_in_t *ii; 3107 ASSERT(DB_TYPE(mp) == M_DATA); 3108 if ((ipsec_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 3109 freemsg(mp); 3110 BUMP_MIB(&ip_mib, ipOutDiscards); 3111 return; 3112 } 3113 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 3114 3115 /* This is not a secure packet */ 3116 ii->ipsec_in_secure = B_FALSE; 3117 if (CONN_Q(q)) { 3118 zoneid = Q_TO_CONN(q)->conn_zoneid; 3119 } else { 3120 zoneid = GLOBAL_ZONEID; 3121 } 3122 ii->ipsec_in_zoneid = zoneid; 3123 ipsec_mp->b_cont = mp; 3124 ipha = (ipha_t *)mp->b_rptr; 3125 /* 3126 * Convert the IPSEC_IN to IPSEC_OUT. 3127 */ 3128 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { 3129 BUMP_MIB(&ip_mib, ipOutDiscards); 3130 return; 3131 } 3132 io = (ipsec_out_t *)ipsec_mp->b_rptr; 3133 } 3134 3135 /* Remember our eventual destination */ 3136 dst = ipha->ipha_src; 3137 3138 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 3139 NULL, NULL, zoneid, MATCH_IRE_TYPE); 3140 if (ire != NULL && ire->ire_zoneid == zoneid) { 3141 src = ipha->ipha_dst; 3142 } else if (!xmit_if_on) { 3143 if (ire != NULL) 3144 ire_refrele(ire); 3145 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, 3146 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY)); 3147 if (ire == NULL) { 3148 BUMP_MIB(&ip_mib, ipOutNoRoutes); 3149 freemsg(ipsec_mp); 3150 return; 3151 } 3152 src = ire->ire_src_addr; 3153 } else { 3154 ipif_t *ipif = NULL; 3155 ill_t *ill; 3156 /* 3157 * This must be an ICMP error coming from 3158 * ip_mrtun_forward(). The src addr should 3159 * be equal to the IP-addr of the outgoing 3160 * interface. 3161 */ 3162 if (io == NULL) { 3163 /* This is not a IPSEC_OUT type control msg */ 3164 BUMP_MIB(&ip_mib, ipOutNoRoutes); 3165 freemsg(ipsec_mp); 3166 return; 3167 } 3168 ill = ill_lookup_on_ifindex(io->ipsec_out_ill_index, B_FALSE, 3169 NULL, NULL, NULL, NULL); 3170 if (ill != NULL) { 3171 ipif = ipif_get_next_ipif(NULL, ill); 3172 ill_refrele(ill); 3173 } 3174 if (ipif == NULL) { 3175 BUMP_MIB(&ip_mib, ipOutNoRoutes); 3176 freemsg(ipsec_mp); 3177 return; 3178 } 3179 src = ipif->ipif_src_addr; 3180 ipif_refrele(ipif); 3181 } 3182 3183 if (ire != NULL) 3184 ire_refrele(ire); 3185 3186 /* 3187 * Check if we can send back more then 8 bytes in addition 3188 * to the IP header. We will include as much as 64 bytes. 3189 */ 3190 len_needed = IPH_HDR_LENGTH(ipha) + ip_icmp_return; 3191 msg_len = msgdsize(mp); 3192 if (msg_len > len_needed) { 3193 (void) adjmsg(mp, len_needed - msg_len); 3194 msg_len = len_needed; 3195 } 3196 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_HI); 3197 if (!mp1) { 3198 BUMP_MIB(&icmp_mib, icmpOutErrors); 3199 freemsg(ipsec_mp); 3200 return; 3201 } 3202 mp1->b_cont = mp; 3203 mp = mp1; 3204 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 3205 ipsec_mp->b_rptr == (uint8_t *)io && 3206 io->ipsec_out_type == IPSEC_OUT); 3207 ipsec_mp->b_cont = mp; 3208 3209 /* 3210 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 3211 * node generates be accepted in peace by all on-host destinations. 3212 * If we do NOT assume that all on-host destinations trust 3213 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 3214 * (Look for ipsec_out_icmp_loopback). 3215 */ 3216 io->ipsec_out_icmp_loopback = B_TRUE; 3217 3218 ipha = (ipha_t *)mp->b_rptr; 3219 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 3220 *ipha = icmp_ipha; 3221 ipha->ipha_src = src; 3222 ipha->ipha_dst = dst; 3223 ipha->ipha_ttl = ip_def_ttl; 3224 msg_len += sizeof (icmp_ipha) + len; 3225 if (msg_len > IP_MAXPACKET) { 3226 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 3227 msg_len = IP_MAXPACKET; 3228 } 3229 ipha->ipha_length = htons((uint16_t)msg_len); 3230 icmph = (icmph_t *)&ipha[1]; 3231 bcopy(stuff, icmph, len); 3232 icmph->icmph_checksum = 0; 3233 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 3234 if (icmph->icmph_checksum == 0) 3235 icmph->icmph_checksum = 0xFFFF; 3236 BUMP_MIB(&icmp_mib, icmpOutMsgs); 3237 put(q, ipsec_mp); 3238 } 3239 3240 /* 3241 * Determine if an ICMP error packet can be sent given the rate limit. 3242 * The limit consists of an average frequency (icmp_pkt_err_interval measured 3243 * in milliseconds) and a burst size. Burst size number of packets can 3244 * be sent arbitrarely closely spaced. 3245 * The state is tracked using two variables to implement an approximate 3246 * token bucket filter: 3247 * icmp_pkt_err_last - lbolt value when the last burst started 3248 * icmp_pkt_err_sent - number of packets sent in current burst 3249 */ 3250 boolean_t 3251 icmp_err_rate_limit(void) 3252 { 3253 clock_t now = TICK_TO_MSEC(lbolt); 3254 uint_t refilled; /* Number of packets refilled in tbf since last */ 3255 uint_t err_interval = ip_icmp_err_interval; /* Guard against changes */ 3256 3257 if (err_interval == 0) 3258 return (B_FALSE); 3259 3260 if (icmp_pkt_err_last > now) { 3261 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 3262 icmp_pkt_err_last = 0; 3263 icmp_pkt_err_sent = 0; 3264 } 3265 /* 3266 * If we are in a burst update the token bucket filter. 3267 * Update the "last" time to be close to "now" but make sure 3268 * we don't loose precision. 3269 */ 3270 if (icmp_pkt_err_sent != 0) { 3271 refilled = (now - icmp_pkt_err_last)/err_interval; 3272 if (refilled > icmp_pkt_err_sent) { 3273 icmp_pkt_err_sent = 0; 3274 } else { 3275 icmp_pkt_err_sent -= refilled; 3276 icmp_pkt_err_last += refilled * err_interval; 3277 } 3278 } 3279 if (icmp_pkt_err_sent == 0) { 3280 /* Start of new burst */ 3281 icmp_pkt_err_last = now; 3282 } 3283 if (icmp_pkt_err_sent < ip_icmp_err_burst) { 3284 icmp_pkt_err_sent++; 3285 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 3286 icmp_pkt_err_sent)); 3287 return (B_FALSE); 3288 } 3289 ip1dbg(("icmp_err_rate_limit: dropped\n")); 3290 return (B_TRUE); 3291 } 3292 3293 /* 3294 * Check if it is ok to send an IPv4 ICMP error packet in 3295 * response to the IPv4 packet in mp. 3296 * Free the message and return null if no 3297 * ICMP error packet should be sent. 3298 */ 3299 static mblk_t * 3300 icmp_pkt_err_ok(mblk_t *mp) 3301 { 3302 icmph_t *icmph; 3303 ipha_t *ipha; 3304 uint_t len_needed; 3305 ire_t *src_ire; 3306 ire_t *dst_ire; 3307 3308 if (!mp) 3309 return (NULL); 3310 ipha = (ipha_t *)mp->b_rptr; 3311 if (ip_csum_hdr(ipha)) { 3312 BUMP_MIB(&ip_mib, ipInCksumErrs); 3313 freemsg(mp); 3314 return (NULL); 3315 } 3316 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, 3317 NULL, ALL_ZONES, MATCH_IRE_TYPE); 3318 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, 3319 NULL, ALL_ZONES, MATCH_IRE_TYPE); 3320 if (src_ire != NULL || dst_ire != NULL || 3321 CLASSD(ipha->ipha_dst) || 3322 CLASSD(ipha->ipha_src) || 3323 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3324 /* Note: only errors to the fragment with offset 0 */ 3325 BUMP_MIB(&icmp_mib, icmpOutDrops); 3326 freemsg(mp); 3327 if (src_ire != NULL) 3328 ire_refrele(src_ire); 3329 if (dst_ire != NULL) 3330 ire_refrele(dst_ire); 3331 return (NULL); 3332 } 3333 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3334 /* 3335 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3336 * errors in response to any ICMP errors. 3337 */ 3338 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3339 if (mp->b_wptr - mp->b_rptr < len_needed) { 3340 if (!pullupmsg(mp, len_needed)) { 3341 BUMP_MIB(&icmp_mib, icmpInErrors); 3342 freemsg(mp); 3343 return (NULL); 3344 } 3345 ipha = (ipha_t *)mp->b_rptr; 3346 } 3347 icmph = (icmph_t *) 3348 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3349 switch (icmph->icmph_type) { 3350 case ICMP_DEST_UNREACHABLE: 3351 case ICMP_SOURCE_QUENCH: 3352 case ICMP_TIME_EXCEEDED: 3353 case ICMP_PARAM_PROBLEM: 3354 case ICMP_REDIRECT: 3355 BUMP_MIB(&icmp_mib, icmpOutDrops); 3356 freemsg(mp); 3357 return (NULL); 3358 default: 3359 break; 3360 } 3361 } 3362 if (icmp_err_rate_limit()) { 3363 /* 3364 * Only send ICMP error packets every so often. 3365 * This should be done on a per port/source basis, 3366 * but for now this will suffice. 3367 */ 3368 freemsg(mp); 3369 return (NULL); 3370 } 3371 return (mp); 3372 } 3373 3374 /* 3375 * Generate an ICMP redirect message. 3376 */ 3377 static void 3378 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway) 3379 { 3380 icmph_t icmph; 3381 3382 /* 3383 * We are called from ip_rput where we could 3384 * not have attached an IPSEC_IN. 3385 */ 3386 ASSERT(mp->b_datap->db_type == M_DATA); 3387 3388 if (!(mp = icmp_pkt_err_ok(mp))) { 3389 return; 3390 } 3391 3392 bzero(&icmph, sizeof (icmph_t)); 3393 icmph.icmph_type = ICMP_REDIRECT; 3394 icmph.icmph_code = 1; 3395 icmph.icmph_rd_gateway = gateway; 3396 BUMP_MIB(&icmp_mib, icmpOutRedirects); 3397 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE); 3398 } 3399 3400 /* 3401 * Generate an ICMP time exceeded message. 3402 */ 3403 void 3404 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code) 3405 { 3406 icmph_t icmph; 3407 boolean_t mctl_present; 3408 mblk_t *first_mp; 3409 3410 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3411 3412 if (!(mp = icmp_pkt_err_ok(mp))) { 3413 if (mctl_present) 3414 freeb(first_mp); 3415 return; 3416 } 3417 3418 bzero(&icmph, sizeof (icmph_t)); 3419 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3420 icmph.icmph_code = code; 3421 BUMP_MIB(&icmp_mib, icmpOutTimeExcds); 3422 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present); 3423 } 3424 3425 /* 3426 * Generate an ICMP unreachable message. 3427 */ 3428 void 3429 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code) 3430 { 3431 icmph_t icmph; 3432 mblk_t *first_mp; 3433 boolean_t mctl_present; 3434 3435 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 3436 3437 if (!(mp = icmp_pkt_err_ok(mp))) { 3438 if (mctl_present) 3439 freeb(first_mp); 3440 return; 3441 } 3442 3443 bzero(&icmph, sizeof (icmph_t)); 3444 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3445 icmph.icmph_code = code; 3446 BUMP_MIB(&icmp_mib, icmpOutDestUnreachs); 3447 ip2dbg(("send icmp destination unreachable code %d\n", code)); 3448 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present); 3449 } 3450 3451 /* 3452 * News from ARP. ARP sends notification of interesting events down 3453 * to its clients using M_CTL messages with the interesting ARP packet 3454 * attached via b_cont. 3455 * The interesting event from a device comes up the corresponding ARP-IP-DEV 3456 * queue as opposed to ARP sending the message to all the clients, i.e. all 3457 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache 3458 * table if a cache IRE is found to delete all the entries for the address in 3459 * the packet. 3460 */ 3461 static void 3462 ip_arp_news(queue_t *q, mblk_t *mp) 3463 { 3464 arcn_t *arcn; 3465 arh_t *arh; 3466 char *cp1; 3467 uchar_t *cp2; 3468 ire_t *ire = NULL; 3469 int i1; 3470 char hbuf[128]; 3471 char sbuf[16]; 3472 ipaddr_t src; 3473 in6_addr_t v6src; 3474 boolean_t isv6 = B_FALSE; 3475 3476 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { 3477 if (q->q_next) { 3478 putnext(q, mp); 3479 } else 3480 freemsg(mp); 3481 return; 3482 } 3483 arh = (arh_t *)mp->b_cont->b_rptr; 3484 /* Is it one we are interested in? */ 3485 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { 3486 isv6 = B_TRUE; 3487 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, 3488 IPV6_ADDR_LEN); 3489 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { 3490 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, 3491 IP_ADDR_LEN); 3492 } else { 3493 freemsg(mp); 3494 return; 3495 } 3496 3497 arcn = (arcn_t *)mp->b_rptr; 3498 switch (arcn->arcn_code) { 3499 case AR_CN_BOGON: 3500 /* 3501 * Someone is sending ARP packets with a source protocol 3502 * address which we have published. Either they are 3503 * pretending to be us, or we have been asked to proxy 3504 * for a machine that can do fine for itself, or two 3505 * different machines are providing proxy service for the 3506 * same protocol address, or something. We try and do 3507 * something appropriate here. 3508 */ 3509 cp2 = (uchar_t *)&arh[1]; 3510 cp1 = hbuf; 3511 *cp1 = '\0'; 3512 for (i1 = arh->arh_hlen; i1--; cp1 += 3) 3513 (void) sprintf(cp1, "%02x:", *cp2++ & 0xff); 3514 if (cp1 != hbuf) 3515 cp1[-1] = '\0'; 3516 (void) ip_dot_addr(src, sbuf); 3517 if (isv6) 3518 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES); 3519 else 3520 ire = ire_cache_lookup(src, ALL_ZONES); 3521 3522 if (ire != NULL && IRE_IS_LOCAL(ire)) { 3523 cmn_err(CE_WARN, 3524 "IP: Hardware address '%s' trying" 3525 " to be our address %s!", 3526 hbuf, sbuf); 3527 } else { 3528 cmn_err(CE_WARN, 3529 "IP: Proxy ARP problem? " 3530 "Hardware address '%s' thinks it is %s", 3531 hbuf, sbuf); 3532 } 3533 if (ire != NULL) 3534 ire_refrele(ire); 3535 break; 3536 case AR_CN_ANNOUNCE: 3537 if (isv6) { 3538 /* 3539 * For XRESOLV interfaces. 3540 * Delete the IRE cache entry and NCE for this 3541 * v6 address 3542 */ 3543 ip_ire_clookup_and_delete_v6(&v6src); 3544 /* 3545 * If v6src is a non-zero, it's a router address 3546 * as below. Do the same sort of thing to clean 3547 * out off-net IRE_CACHE entries that go through 3548 * the router. 3549 */ 3550 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { 3551 ire_walk_v6(ire_delete_cache_gw_v6, 3552 (char *)&v6src, ALL_ZONES); 3553 } 3554 break; 3555 } 3556 /* 3557 * ARP gives us a copy of any broadcast packet with identical 3558 * sender and receiver protocol address, in 3559 * case we want to intuit something from it. Such a packet 3560 * usually means that a machine has just come up on the net. 3561 * If we have an IRE_CACHE, we blow it away. This way we will 3562 * immediately pick up the rare case of a host changing 3563 * hardware address. ip_ire_clookup_and_delete achieves this. 3564 * 3565 * The address in "src" may be an entry for a router. 3566 * (Default router, or non-default router.) If 3567 * that's true, then any off-net IRE_CACHE entries 3568 * that go through the router with address "src" 3569 * must be clobbered. Use ire_walk to achieve this 3570 * goal. 3571 * 3572 * It should be possible to determine if the address 3573 * in src is or is not for a router. This way, 3574 * the ire_walk() isn't called all of the time here. 3575 * Do not pass 'src' value of 0 to ire_delete_cache_gw, 3576 * as it would remove all IRE_CACHE entries for onlink 3577 * destinations. All onlink destinations have 3578 * ire_gateway_addr == 0. 3579 */ 3580 if ((ip_ire_clookup_and_delete(src, NULL) || 3581 (ire = ire_ftable_lookup(src, 0, 0, 0, NULL, NULL, NULL, 3582 0, MATCH_IRE_DSTONLY)) != NULL) && src != 0) { 3583 ire_walk_v4(ire_delete_cache_gw, (char *)&src, 3584 ALL_ZONES); 3585 } 3586 /* From ire_ftable_lookup */ 3587 if (ire != NULL) 3588 ire_refrele(ire); 3589 break; 3590 default: 3591 if (ire != NULL) 3592 ire_refrele(ire); 3593 break; 3594 } 3595 freemsg(mp); 3596 } 3597 3598 /* 3599 * Create a mblk suitable for carrying the interface index and/or source link 3600 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used 3601 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user 3602 * application. 3603 */ 3604 mblk_t * 3605 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags) 3606 { 3607 mblk_t *mp; 3608 in_pktinfo_t *pinfo; 3609 ipha_t *ipha; 3610 struct ether_header *pether; 3611 3612 mp = allocb(sizeof (in_pktinfo_t), BPRI_MED); 3613 if (mp == NULL) { 3614 ip1dbg(("ip_add_info: allocation failure.\n")); 3615 return (data_mp); 3616 } 3617 3618 ipha = (ipha_t *)data_mp->b_rptr; 3619 pinfo = (in_pktinfo_t *)mp->b_rptr; 3620 bzero(pinfo, sizeof (in_pktinfo_t)); 3621 pinfo->in_pkt_flags = (uchar_t)flags; 3622 pinfo->in_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ 3623 3624 if (flags & IPF_RECVIF) 3625 pinfo->in_pkt_ifindex = ill->ill_phyint->phyint_ifindex; 3626 3627 pether = (struct ether_header *)((char *)ipha 3628 - sizeof (struct ether_header)); 3629 /* 3630 * Make sure the interface is an ethernet type, since this option 3631 * is currently supported only on this type of interface. Also make 3632 * sure we are pointing correctly above db_base. 3633 */ 3634 3635 if ((flags & IPF_RECVSLLA) && 3636 ((uchar_t *)pether >= data_mp->b_datap->db_base) && 3637 (ill->ill_type == IFT_ETHER) && 3638 (ill->ill_net_type == IRE_IF_RESOLVER)) { 3639 3640 pinfo->in_pkt_slla.sdl_type = IFT_ETHER; 3641 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, 3642 (uchar_t *)pinfo->in_pkt_slla.sdl_data, ETHERADDRL); 3643 } else { 3644 /* 3645 * Clear the bit. Indicate to upper layer that IP is not 3646 * sending this ancillary info. 3647 */ 3648 pinfo->in_pkt_flags = pinfo->in_pkt_flags & ~IPF_RECVSLLA; 3649 } 3650 3651 mp->b_datap->db_type = M_CTL; 3652 mp->b_wptr += sizeof (in_pktinfo_t); 3653 mp->b_cont = data_mp; 3654 3655 return (mp); 3656 } 3657 3658 /* 3659 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as 3660 * part of the bind request. 3661 */ 3662 3663 boolean_t 3664 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) 3665 { 3666 ipsec_in_t *ii; 3667 3668 ASSERT(policy_mp != NULL); 3669 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); 3670 3671 ii = (ipsec_in_t *)policy_mp->b_rptr; 3672 ASSERT(ii->ipsec_in_type == IPSEC_IN); 3673 3674 connp->conn_policy = ii->ipsec_in_policy; 3675 ii->ipsec_in_policy = NULL; 3676 3677 if (ii->ipsec_in_action != NULL) { 3678 if (connp->conn_latch == NULL) { 3679 connp->conn_latch = iplatch_create(); 3680 if (connp->conn_latch == NULL) 3681 return (B_FALSE); 3682 } 3683 ipsec_latch_inbound(connp->conn_latch, ii); 3684 } 3685 return (B_TRUE); 3686 } 3687 3688 /* 3689 * Upper level protocols (ULP) pass through bind requests to IP for inspection 3690 * and to arrange for power-fanout assist. The ULP is identified by 3691 * adding a single byte at the end of the original bind message. 3692 * A ULP other than UDP or TCP that wishes to be recognized passes 3693 * down a bind with a zero length address. 3694 * 3695 * The binding works as follows: 3696 * - A zero byte address means just bind to the protocol. 3697 * - A four byte address is treated as a request to validate 3698 * that the address is a valid local address, appropriate for 3699 * an application to bind to. This does not affect any fanout 3700 * information in IP. 3701 * - A sizeof sin_t byte address is used to bind to only the local address 3702 * and port. 3703 * - A sizeof ipa_conn_t byte address contains complete fanout information 3704 * consisting of local and remote addresses and ports. In 3705 * this case, the addresses are both validated as appropriate 3706 * for this operation, and, if so, the information is retained 3707 * for use in the inbound fanout. 3708 * 3709 * The ULP (except in the zero-length bind) can append an 3710 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the 3711 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants 3712 * a copy of the source or destination IRE (source for local bind; 3713 * destination for complete bind). IPSEC_POLICY_SET indicates that the 3714 * policy information contained should be copied on to the conn. 3715 * 3716 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. 3717 */ 3718 mblk_t * 3719 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) 3720 { 3721 ssize_t len; 3722 struct T_bind_req *tbr; 3723 sin_t *sin; 3724 ipa_conn_t *ac; 3725 uchar_t *ucp; 3726 mblk_t *mp1; 3727 boolean_t ire_requested; 3728 boolean_t ipsec_policy_set = B_FALSE; 3729 int error = 0; 3730 int protocol; 3731 ipa_conn_x_t *acx; 3732 3733 ASSERT(!connp->conn_af_isv6); 3734 connp->conn_pkt_isv6 = B_FALSE; 3735 3736 len = MBLKL(mp); 3737 if (len < (sizeof (*tbr) + 1)) { 3738 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 3739 "ip_bind: bogus msg, len %ld", len); 3740 /* XXX: Need to return something better */ 3741 goto bad_addr; 3742 } 3743 /* Back up and extract the protocol identifier. */ 3744 mp->b_wptr--; 3745 protocol = *mp->b_wptr & 0xFF; 3746 tbr = (struct T_bind_req *)mp->b_rptr; 3747 /* Reset the message type in preparation for shipping it back. */ 3748 DB_TYPE(mp) = M_PCPROTO; 3749 3750 connp->conn_ulp = (uint8_t)protocol; 3751 3752 /* 3753 * Check for a zero length address. This is from a protocol that 3754 * wants to register to receive all packets of its type. 3755 */ 3756 if (tbr->ADDR_length == 0) { 3757 /* 3758 * These protocols are now intercepted in ip_bind_v6(). 3759 * Reject protocol-level binds here for now. 3760 * 3761 * For SCTP raw socket, ICMP sends down a bind with sin_t 3762 * so that the protocol type cannot be SCTP. 3763 */ 3764 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || 3765 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { 3766 goto bad_addr; 3767 } 3768 3769 /* No hash here really. The table is big enough. */ 3770 connp->conn_srcv6 = ipv6_all_zeros; 3771 3772 ipcl_proto_insert(connp, protocol); 3773 3774 tbr->PRIM_type = T_BIND_ACK; 3775 return (mp); 3776 } 3777 3778 /* Extract the address pointer from the message. */ 3779 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 3780 tbr->ADDR_length); 3781 if (ucp == NULL) { 3782 ip1dbg(("ip_bind: no address\n")); 3783 goto bad_addr; 3784 } 3785 if (!OK_32PTR(ucp)) { 3786 ip1dbg(("ip_bind: unaligned address\n")); 3787 goto bad_addr; 3788 } 3789 /* 3790 * Check for trailing mps. 3791 */ 3792 3793 mp1 = mp->b_cont; 3794 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); 3795 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); 3796 3797 switch (tbr->ADDR_length) { 3798 default: 3799 ip1dbg(("ip_bind: bad address length %d\n", 3800 (int)tbr->ADDR_length)); 3801 goto bad_addr; 3802 3803 case IP_ADDR_LEN: 3804 /* Verification of local address only */ 3805 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, 3806 ire_requested, ipsec_policy_set, B_FALSE); 3807 break; 3808 3809 case sizeof (sin_t): 3810 sin = (sin_t *)ucp; 3811 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, 3812 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); 3813 if (protocol == IPPROTO_TCP) 3814 connp->conn_recv = tcp_conn_request; 3815 break; 3816 3817 case sizeof (ipa_conn_t): 3818 ac = (ipa_conn_t *)ucp; 3819 /* For raw socket, the local port is not set. */ 3820 if (ac->ac_lport == 0) 3821 ac->ac_lport = connp->conn_lport; 3822 /* Always verify destination reachability. */ 3823 error = ip_bind_connected(connp, mp, &ac->ac_laddr, 3824 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, 3825 ipsec_policy_set, B_TRUE, B_TRUE); 3826 if (protocol == IPPROTO_TCP) 3827 connp->conn_recv = tcp_input; 3828 break; 3829 3830 case sizeof (ipa_conn_x_t): 3831 acx = (ipa_conn_x_t *)ucp; 3832 /* 3833 * Whether or not to verify destination reachability depends 3834 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. 3835 */ 3836 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, 3837 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, 3838 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, 3839 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); 3840 if (protocol == IPPROTO_TCP) 3841 connp->conn_recv = tcp_input; 3842 break; 3843 } 3844 if (error == EINPROGRESS) 3845 return (NULL); 3846 else if (error != 0) 3847 goto bad_addr; 3848 /* 3849 * Pass the IPSEC headers size in ire_ipsec_overhead. 3850 * We can't do this in ip_bind_insert_ire because the policy 3851 * may not have been inherited at that point in time and hence 3852 * conn_out_enforce_policy may not be set. 3853 */ 3854 mp1 = mp->b_cont; 3855 if (ire_requested && connp->conn_out_enforce_policy && 3856 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 3857 ire_t *ire = (ire_t *)mp1->b_rptr; 3858 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 3859 ire->ire_ipsec_overhead = conn_ipsec_length(connp); 3860 } 3861 3862 /* Send it home. */ 3863 mp->b_datap->db_type = M_PCPROTO; 3864 tbr->PRIM_type = T_BIND_ACK; 3865 return (mp); 3866 3867 bad_addr: 3868 /* 3869 * If error = -1 then we generate a TBADADDR - otherwise error is 3870 * a unix errno. 3871 */ 3872 if (error > 0) 3873 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 3874 else 3875 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 3876 return (mp); 3877 } 3878 3879 /* 3880 * Here address is verified to be a valid local address. 3881 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast 3882 * address is also considered a valid local address. 3883 * In the case of a broadcast/multicast address, however, the 3884 * upper protocol is expected to reset the src address 3885 * to 0 if it sees a IRE_BROADCAST type returned so that 3886 * no packets are emitted with broadcast/multicast address as 3887 * source address (that violates hosts requirements RFC1122) 3888 * The addresses valid for bind are: 3889 * (1) - INADDR_ANY (0) 3890 * (2) - IP address of an UP interface 3891 * (3) - IP address of a DOWN interface 3892 * (4) - valid local IP broadcast addresses. In this case 3893 * the conn will only receive packets destined to 3894 * the specified broadcast address. 3895 * (5) - a multicast address. In this case 3896 * the conn will only receive packets destined to 3897 * the specified multicast address. Note: the 3898 * application still has to issue an 3899 * IP_ADD_MEMBERSHIP socket option. 3900 * 3901 * On error, return -1 for TBADADDR otherwise pass the 3902 * errno with TSYSERR reply. 3903 * 3904 * In all the above cases, the bound address must be valid in the current zone. 3905 * When the address is loopback, multicast or broadcast, there might be many 3906 * matching IREs so bind has to look up based on the zone. 3907 */ 3908 int 3909 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, 3910 boolean_t ire_requested, boolean_t ipsec_policy_set, 3911 boolean_t fanout_insert) 3912 { 3913 int error = 0; 3914 ire_t *src_ire; 3915 mblk_t *policy_mp; 3916 ipif_t *ipif; 3917 zoneid_t zoneid; 3918 3919 if (ipsec_policy_set) { 3920 policy_mp = mp->b_cont; 3921 } 3922 3923 /* 3924 * If it was previously connected, conn_fully_bound would have 3925 * been set. 3926 */ 3927 connp->conn_fully_bound = B_FALSE; 3928 3929 src_ire = NULL; 3930 ipif = NULL; 3931 3932 zoneid = connp->conn_zoneid; 3933 3934 if (src_addr) { 3935 src_ire = ire_route_lookup(src_addr, 0, 0, 0, 3936 NULL, NULL, zoneid, MATCH_IRE_ZONEONLY); 3937 /* 3938 * If an address other than 0.0.0.0 is requested, 3939 * we verify that it is a valid address for bind 3940 * Note: Following code is in if-else-if form for 3941 * readability compared to a condition check. 3942 */ 3943 /* LINTED - statement has no consequent */ 3944 if (IRE_IS_LOCAL(src_ire)) { 3945 /* 3946 * (2) Bind to address of local UP interface 3947 */ 3948 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { 3949 /* 3950 * (4) Bind to broadcast address 3951 * Note: permitted only from transports that 3952 * request IRE 3953 */ 3954 if (!ire_requested) 3955 error = EADDRNOTAVAIL; 3956 } else { 3957 /* 3958 * (3) Bind to address of local DOWN interface 3959 * (ipif_lookup_addr() looks up all interfaces 3960 * but we do not get here for UP interfaces 3961 * - case (2) above) 3962 * We put the protocol byte back into the mblk 3963 * since we may come back via ip_wput_nondata() 3964 * later with this mblk if ipif_lookup_addr chooses 3965 * to defer processing. 3966 */ 3967 *mp->b_wptr++ = (char)connp->conn_ulp; 3968 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, 3969 CONNP_TO_WQ(connp), mp, ip_wput_nondata, 3970 &error)) != NULL) { 3971 ipif_refrele(ipif); 3972 } else if (error == EINPROGRESS) { 3973 if (src_ire != NULL) 3974 ire_refrele(src_ire); 3975 return (EINPROGRESS); 3976 } else if (CLASSD(src_addr)) { 3977 error = 0; 3978 if (src_ire != NULL) 3979 ire_refrele(src_ire); 3980 /* 3981 * (5) bind to multicast address. 3982 * Fake out the IRE returned to upper 3983 * layer to be a broadcast IRE. 3984 */ 3985 src_ire = ire_ctable_lookup( 3986 INADDR_BROADCAST, INADDR_ANY, 3987 IRE_BROADCAST, NULL, zoneid, 3988 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY)); 3989 if (src_ire == NULL || !ire_requested) 3990 error = EADDRNOTAVAIL; 3991 } else { 3992 /* 3993 * Not a valid address for bind 3994 */ 3995 error = EADDRNOTAVAIL; 3996 } 3997 /* 3998 * Just to keep it consistent with the processing in 3999 * ip_bind_v4() 4000 */ 4001 mp->b_wptr--; 4002 } 4003 if (error) { 4004 /* Red Alert! Attempting to be a bogon! */ 4005 ip1dbg(("ip_bind: bad src address 0x%x\n", 4006 ntohl(src_addr))); 4007 goto bad_addr; 4008 } 4009 } 4010 4011 /* 4012 * Allow setting new policies. For example, disconnects come 4013 * down as ipa_t bind. As we would have set conn_policy_cached 4014 * to B_TRUE before, we should set it to B_FALSE, so that policy 4015 * can change after the disconnect. 4016 */ 4017 connp->conn_policy_cached = B_FALSE; 4018 4019 /* 4020 * If not fanout_insert this was just an address verification 4021 */ 4022 if (fanout_insert) { 4023 /* 4024 * The addresses have been verified. Time to insert in 4025 * the correct fanout list. 4026 */ 4027 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4028 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); 4029 connp->conn_lport = lport; 4030 connp->conn_fport = 0; 4031 /* 4032 * Do we need to add a check to reject Multicast packets 4033 */ 4034 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); 4035 } 4036 done: 4037 if (error == 0) { 4038 if (ire_requested) { 4039 if (!ip_bind_insert_ire(mp, src_ire, NULL)) { 4040 error = -1; 4041 /* Falls through to bad_addr */ 4042 } 4043 } else if (ipsec_policy_set) { 4044 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4045 error = -1; 4046 /* Falls through to bad_addr */ 4047 } 4048 } 4049 } 4050 bad_addr: 4051 if (src_ire != NULL) 4052 IRE_REFRELE(src_ire); 4053 if (ipsec_policy_set) { 4054 ASSERT(policy_mp == mp->b_cont); 4055 ASSERT(policy_mp != NULL); 4056 freeb(policy_mp); 4057 /* 4058 * As of now assume that nothing else accompanies 4059 * IPSEC_POLICY_SET. 4060 */ 4061 mp->b_cont = NULL; 4062 } 4063 return (error); 4064 } 4065 4066 /* 4067 * Verify that both the source and destination addresses 4068 * are valid. If verify_dst is false, then the destination address may be 4069 * unreachable, i.e. have no route to it. Protocols like TCP want to verify 4070 * destination reachability, while tunnels do not. 4071 * Note that we allow connect to broadcast and multicast 4072 * addresses when ire_requested is set. Thus the ULP 4073 * has to check for IRE_BROADCAST and multicast. 4074 * 4075 * Returns zero if ok. 4076 * On error: returns -1 to mean TBADADDR otherwise returns an errno 4077 * (for use with TSYSERR reply). 4078 */ 4079 int 4080 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, 4081 uint16_t lport, ipaddr_t dst_addr, uint16_t fport, 4082 boolean_t ire_requested, boolean_t ipsec_policy_set, 4083 boolean_t fanout_insert, boolean_t verify_dst) 4084 { 4085 ire_t *src_ire; 4086 ire_t *dst_ire; 4087 int error = 0; 4088 int protocol; 4089 mblk_t *policy_mp; 4090 ire_t *sire = NULL; 4091 ire_t *md_dst_ire = NULL; 4092 ill_t *md_ill = NULL; 4093 zoneid_t zoneid; 4094 ipaddr_t src_addr = *src_addrp; 4095 4096 src_ire = dst_ire = NULL; 4097 protocol = *mp->b_wptr & 0xFF; 4098 4099 /* 4100 * If we never got a disconnect before, clear it now. 4101 */ 4102 connp->conn_fully_bound = B_FALSE; 4103 4104 if (ipsec_policy_set) { 4105 policy_mp = mp->b_cont; 4106 } 4107 4108 zoneid = connp->conn_zoneid; 4109 4110 if (CLASSD(dst_addr)) { 4111 /* Pick up an IRE_BROADCAST */ 4112 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, 4113 NULL, zoneid, (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4114 MATCH_IRE_RJ_BHOLE)); 4115 } else { 4116 /* 4117 * If conn_dontroute is set or if conn_nexthop_set is set, 4118 * and onlink ipif is not found set ENETUNREACH error. 4119 */ 4120 if (connp->conn_dontroute || connp->conn_nexthop_set) { 4121 ipif_t *ipif; 4122 4123 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? 4124 dst_addr : connp->conn_nexthop_v4, zoneid); 4125 if (ipif == NULL) { 4126 error = ENETUNREACH; 4127 goto bad_addr; 4128 } 4129 ipif_refrele(ipif); 4130 } 4131 4132 if (connp->conn_nexthop_set) { 4133 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 4134 0, 0, NULL, NULL, zoneid, 0); 4135 } else { 4136 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, 4137 &sire, zoneid, 4138 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4139 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE)); 4140 } 4141 } 4142 /* 4143 * dst_ire can't be a broadcast when not ire_requested. 4144 * We also prevent ire's with src address INADDR_ANY to 4145 * be used, which are created temporarily for 4146 * sending out packets from endpoints that have 4147 * conn_unspec_src set. If verify_dst is true, the destination must be 4148 * reachable. If verify_dst is false, the destination needn't be 4149 * reachable. 4150 * 4151 * If we match on a reject or black hole, then we've got a 4152 * local failure. May as well fail out the connect() attempt, 4153 * since it's never going to succeed. 4154 */ 4155 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || 4156 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 4157 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { 4158 /* 4159 * If we're verifying destination reachability, we always want 4160 * to complain here. 4161 * 4162 * If we're not verifying destination reachability but the 4163 * destination has a route, we still want to fail on the 4164 * temporary address and broadcast address tests. 4165 */ 4166 if (verify_dst || (dst_ire != NULL)) { 4167 if (ip_debug > 2) { 4168 pr_addr_dbg("ip_bind_connected: bad connected " 4169 "dst %s\n", AF_INET, &dst_addr); 4170 } 4171 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) 4172 error = ENETUNREACH; 4173 else 4174 error = EHOSTUNREACH; 4175 goto bad_addr; 4176 } 4177 } 4178 /* 4179 * If the app does a connect(), it means that it will most likely 4180 * send more than 1 packet to the destination. It makes sense 4181 * to clear the temporary flag. 4182 */ 4183 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 4184 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 4185 irb_t *irb = dst_ire->ire_bucket; 4186 4187 rw_enter(&irb->irb_lock, RW_WRITER); 4188 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 4189 irb->irb_tmp_ire_cnt--; 4190 rw_exit(&irb->irb_lock); 4191 } 4192 4193 /* 4194 * See if we should notify ULP about MDT; we do this whether or not 4195 * ire_requested is TRUE, in order to handle active connects; MDT 4196 * eligibility tests for passive connects are handled separately 4197 * through tcp_adapt_ire(). We do this before the source address 4198 * selection, because dst_ire may change after a call to 4199 * ipif_select_source(). This is a best-effort check, as the 4200 * packet for this connection may not actually go through 4201 * dst_ire->ire_stq, and the exact IRE can only be known after 4202 * calling ip_newroute(). This is why we further check on the 4203 * IRE during Multidata packet transmission in tcp_multisend(). 4204 */ 4205 if (ip_multidata_outbound && !ipsec_policy_set && dst_ire != NULL && 4206 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 4207 (md_ill = ire_to_ill(dst_ire), md_ill != NULL) && 4208 ILL_MDT_CAPABLE(md_ill)) { 4209 md_dst_ire = dst_ire; 4210 IRE_REFHOLD(md_dst_ire); 4211 } 4212 4213 if (dst_ire != NULL && 4214 dst_ire->ire_type == IRE_LOCAL && 4215 dst_ire->ire_zoneid != zoneid) { 4216 /* 4217 * If the IRE belongs to a different zone, look for a matching 4218 * route in the forwarding table and use the source address from 4219 * that route. 4220 */ 4221 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, 4222 zoneid, 0, 4223 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4224 MATCH_IRE_RJ_BHOLE); 4225 if (src_ire == NULL) { 4226 error = EHOSTUNREACH; 4227 goto bad_addr; 4228 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 4229 if (!(src_ire->ire_type & IRE_HOST)) 4230 error = ENETUNREACH; 4231 else 4232 error = EHOSTUNREACH; 4233 goto bad_addr; 4234 } 4235 if (src_addr == INADDR_ANY) 4236 src_addr = src_ire->ire_src_addr; 4237 ire_refrele(src_ire); 4238 src_ire = NULL; 4239 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { 4240 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 4241 src_addr = sire->ire_src_addr; 4242 ire_refrele(dst_ire); 4243 dst_ire = sire; 4244 sire = NULL; 4245 } else { 4246 /* 4247 * Pick a source address so that a proper inbound 4248 * load spreading would happen. 4249 */ 4250 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; 4251 ipif_t *src_ipif = NULL; 4252 ire_t *ipif_ire; 4253 4254 /* 4255 * Supply a local source address such that inbound 4256 * load spreading happens. 4257 * 4258 * Determine the best source address on this ill for 4259 * the destination. 4260 * 4261 * 1) For broadcast, we should return a broadcast ire 4262 * found above so that upper layers know that the 4263 * destination address is a broadcast address. 4264 * 4265 * 2) If this is part of a group, select a better 4266 * source address so that better inbound load 4267 * balancing happens. Do the same if the ipif 4268 * is DEPRECATED. 4269 * 4270 * 3) If the outgoing interface is part of a usesrc 4271 * group, then try selecting a source address from 4272 * the usesrc ILL. 4273 */ 4274 if (!(dst_ire->ire_type & IRE_BROADCAST) && 4275 ((dst_ill->ill_group != NULL) || 4276 (dst_ire->ire_ipif->ipif_flags & 4277 IPIF_DEPRECATED) || 4278 (dst_ill->ill_usesrc_ifindex != 0))) { 4279 /* 4280 * If the destination is reachable via a 4281 * given gateway, the selected source address 4282 * should be in the same subnet as the gateway. 4283 * Otherwise, the destination is not reachable. 4284 * 4285 * If there are no interfaces on the same subnet 4286 * as the destination, ipif_select_source gives 4287 * first non-deprecated interface which might be 4288 * on a different subnet than the gateway. 4289 * This is not desirable. Hence pass the dst_ire 4290 * source address to ipif_select_source. 4291 * It is sure that the destination is reachable 4292 * with the dst_ire source address subnet. 4293 * So passing dst_ire source address to 4294 * ipif_select_source will make sure that the 4295 * selected source will be on the same subnet 4296 * as dst_ire source address. 4297 */ 4298 ipaddr_t saddr = 4299 dst_ire->ire_ipif->ipif_src_addr; 4300 src_ipif = ipif_select_source(dst_ill, 4301 saddr, zoneid); 4302 if (src_ipif != NULL) { 4303 if (IS_VNI(src_ipif->ipif_ill)) { 4304 /* 4305 * For VNI there is no 4306 * interface route 4307 */ 4308 src_addr = 4309 src_ipif->ipif_src_addr; 4310 } else { 4311 ipif_ire = 4312 ipif_to_ire(src_ipif); 4313 if (ipif_ire != NULL) { 4314 IRE_REFRELE(dst_ire); 4315 dst_ire = ipif_ire; 4316 } 4317 src_addr = 4318 dst_ire->ire_src_addr; 4319 } 4320 ipif_refrele(src_ipif); 4321 } else { 4322 src_addr = dst_ire->ire_src_addr; 4323 } 4324 } else { 4325 src_addr = dst_ire->ire_src_addr; 4326 } 4327 } 4328 } 4329 4330 /* 4331 * We do ire_route_lookup() here (and not 4332 * interface lookup as we assert that 4333 * src_addr should only come from an 4334 * UP interface for hard binding. 4335 */ 4336 ASSERT(src_ire == NULL); 4337 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, 4338 NULL, zoneid, MATCH_IRE_ZONEONLY); 4339 /* src_ire must be a local|loopback */ 4340 if (!IRE_IS_LOCAL(src_ire)) { 4341 if (ip_debug > 2) { 4342 pr_addr_dbg("ip_bind_connected: bad connected " 4343 "src %s\n", AF_INET, &src_addr); 4344 } 4345 error = EADDRNOTAVAIL; 4346 goto bad_addr; 4347 } 4348 4349 /* 4350 * If the source address is a loopback address, the 4351 * destination had best be local or multicast. 4352 * The transports that can't handle multicast will reject 4353 * those addresses. 4354 */ 4355 if (src_ire->ire_type == IRE_LOOPBACK && 4356 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { 4357 ip1dbg(("ip_bind_connected: bad connected loopback\n")); 4358 error = -1; 4359 goto bad_addr; 4360 } 4361 4362 /* 4363 * Allow setting new policies. For example, disconnects come 4364 * down as ipa_t bind. As we would have set conn_policy_cached 4365 * to B_TRUE before, we should set it to B_FALSE, so that policy 4366 * can change after the disconnect. 4367 */ 4368 connp->conn_policy_cached = B_FALSE; 4369 4370 /* 4371 * Set the conn addresses/ports immediately, so the IPsec policy calls 4372 * can handle their passed-in conn's. 4373 */ 4374 4375 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); 4376 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); 4377 connp->conn_lport = lport; 4378 connp->conn_fport = fport; 4379 *src_addrp = src_addr; 4380 4381 ASSERT(!(ipsec_policy_set && ire_requested)); 4382 if (ire_requested) { 4383 iulp_t *ulp_info = NULL; 4384 4385 /* 4386 * Note that sire will not be NULL if this is an off-link 4387 * connection and there is not cache for that dest yet. 4388 * 4389 * XXX Because of an existing bug, if there are multiple 4390 * default routes, the IRE returned now may not be the actual 4391 * default route used (default routes are chosen in a 4392 * round robin fashion). So if the metrics for different 4393 * default routes are different, we may return the wrong 4394 * metrics. This will not be a problem if the existing 4395 * bug is fixed. 4396 */ 4397 if (sire != NULL) { 4398 ulp_info = &(sire->ire_uinfo); 4399 } 4400 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info)) { 4401 error = -1; 4402 goto bad_addr; 4403 } 4404 } else if (ipsec_policy_set) { 4405 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 4406 error = -1; 4407 goto bad_addr; 4408 } 4409 } 4410 4411 /* 4412 * Cache IPsec policy in this conn. If we have per-socket policy, 4413 * we'll cache that. If we don't, we'll inherit global policy. 4414 * 4415 * We can't insert until the conn reflects the policy. Note that 4416 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 4417 * connections where we don't have a policy. This is to prevent 4418 * global policy lookups in the inbound path. 4419 * 4420 * If we insert before we set conn_policy_cached, 4421 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true 4422 * because global policy cound be non-empty. We normally call 4423 * ipsec_check_policy() for conn_policy_cached connections only if 4424 * ipc_in_enforce_policy is set. But in this case, 4425 * conn_policy_cached can get set anytime since we made the 4426 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is 4427 * called, which will make the above assumption false. Thus, we 4428 * need to insert after we set conn_policy_cached. 4429 */ 4430 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) 4431 goto bad_addr; 4432 4433 if (fanout_insert) { 4434 /* 4435 * The addresses have been verified. Time to insert in 4436 * the correct fanout list. 4437 */ 4438 error = ipcl_conn_insert(connp, protocol, src_addr, 4439 dst_addr, connp->conn_ports); 4440 } 4441 4442 if (error == 0) { 4443 connp->conn_fully_bound = B_TRUE; 4444 /* 4445 * Our initial checks for MDT have passed; the IRE is not 4446 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 4447 * be supporting MDT. Pass the IRE, IPC and ILL into 4448 * ip_mdinfo_return(), which performs further checks 4449 * against them and upon success, returns the MDT info 4450 * mblk which we will attach to the bind acknowledgment. 4451 */ 4452 if (md_dst_ire != NULL) { 4453 mblk_t *mdinfo_mp; 4454 4455 ASSERT(md_ill != NULL); 4456 ASSERT(md_ill->ill_mdt_capab != NULL); 4457 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 4458 md_ill->ill_name, md_ill->ill_mdt_capab)) != NULL) 4459 linkb(mp, mdinfo_mp); 4460 } 4461 } 4462 bad_addr: 4463 if (ipsec_policy_set) { 4464 ASSERT(policy_mp == mp->b_cont); 4465 ASSERT(policy_mp != NULL); 4466 freeb(policy_mp); 4467 /* 4468 * As of now assume that nothing else accompanies 4469 * IPSEC_POLICY_SET. 4470 */ 4471 mp->b_cont = NULL; 4472 } 4473 if (src_ire != NULL) 4474 IRE_REFRELE(src_ire); 4475 if (dst_ire != NULL) 4476 IRE_REFRELE(dst_ire); 4477 if (sire != NULL) 4478 IRE_REFRELE(sire); 4479 if (md_dst_ire != NULL) 4480 IRE_REFRELE(md_dst_ire); 4481 return (error); 4482 } 4483 4484 /* 4485 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 4486 * Prefers dst_ire over src_ire. 4487 */ 4488 static boolean_t 4489 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info) 4490 { 4491 mblk_t *mp1; 4492 ire_t *ret_ire = NULL; 4493 4494 mp1 = mp->b_cont; 4495 ASSERT(mp1 != NULL); 4496 4497 if (ire != NULL) { 4498 /* 4499 * mp1 initialized above to IRE_DB_REQ_TYPE 4500 * appended mblk. Its <upper protocol>'s 4501 * job to make sure there is room. 4502 */ 4503 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 4504 return (0); 4505 4506 mp1->b_datap->db_type = IRE_DB_TYPE; 4507 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 4508 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 4509 ret_ire = (ire_t *)mp1->b_rptr; 4510 /* 4511 * Pass the latest setting of the ip_path_mtu_discovery and 4512 * copy the ulp info if any. 4513 */ 4514 ret_ire->ire_frag_flag |= (ip_path_mtu_discovery) ? 4515 IPH_DF : 0; 4516 if (ulp_info != NULL) { 4517 bcopy(ulp_info, &(ret_ire->ire_uinfo), 4518 sizeof (iulp_t)); 4519 } 4520 ret_ire->ire_mp = mp1; 4521 } else { 4522 /* 4523 * No IRE was found. Remove IRE mblk. 4524 */ 4525 mp->b_cont = mp1->b_cont; 4526 freeb(mp1); 4527 } 4528 4529 return (1); 4530 } 4531 4532 /* 4533 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 4534 * the final piece where we don't. Return a pointer to the first mblk in the 4535 * result, and update the pointer to the next mblk to chew on. If anything 4536 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 4537 * NULL pointer. 4538 */ 4539 mblk_t * 4540 ip_carve_mp(mblk_t **mpp, ssize_t len) 4541 { 4542 mblk_t *mp0; 4543 mblk_t *mp1; 4544 mblk_t *mp2; 4545 4546 if (!len || !mpp || !(mp0 = *mpp)) 4547 return (NULL); 4548 /* If we aren't going to consume the first mblk, we need a dup. */ 4549 if (mp0->b_wptr - mp0->b_rptr > len) { 4550 mp1 = dupb(mp0); 4551 if (mp1) { 4552 /* Partition the data between the two mblks. */ 4553 mp1->b_wptr = mp1->b_rptr + len; 4554 mp0->b_rptr = mp1->b_wptr; 4555 /* 4556 * after adjustments if mblk not consumed is now 4557 * unaligned, try to align it. If this fails free 4558 * all messages and let upper layer recover. 4559 */ 4560 if (!OK_32PTR(mp0->b_rptr)) { 4561 if (!pullupmsg(mp0, -1)) { 4562 freemsg(mp0); 4563 freemsg(mp1); 4564 *mpp = NULL; 4565 return (NULL); 4566 } 4567 } 4568 } 4569 return (mp1); 4570 } 4571 /* Eat through as many mblks as we need to get len bytes. */ 4572 len -= mp0->b_wptr - mp0->b_rptr; 4573 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 4574 if (mp2->b_wptr - mp2->b_rptr > len) { 4575 /* 4576 * We won't consume the entire last mblk. Like 4577 * above, dup and partition it. 4578 */ 4579 mp1->b_cont = dupb(mp2); 4580 mp1 = mp1->b_cont; 4581 if (!mp1) { 4582 /* 4583 * Trouble. Rather than go to a lot of 4584 * trouble to clean up, we free the messages. 4585 * This won't be any worse than losing it on 4586 * the wire. 4587 */ 4588 freemsg(mp0); 4589 freemsg(mp2); 4590 *mpp = NULL; 4591 return (NULL); 4592 } 4593 mp1->b_wptr = mp1->b_rptr + len; 4594 mp2->b_rptr = mp1->b_wptr; 4595 /* 4596 * after adjustments if mblk not consumed is now 4597 * unaligned, try to align it. If this fails free 4598 * all messages and let upper layer recover. 4599 */ 4600 if (!OK_32PTR(mp2->b_rptr)) { 4601 if (!pullupmsg(mp2, -1)) { 4602 freemsg(mp0); 4603 freemsg(mp2); 4604 *mpp = NULL; 4605 return (NULL); 4606 } 4607 } 4608 *mpp = mp2; 4609 return (mp0); 4610 } 4611 /* Decrement len by the amount we just got. */ 4612 len -= mp2->b_wptr - mp2->b_rptr; 4613 } 4614 /* 4615 * len should be reduced to zero now. If not our caller has 4616 * screwed up. 4617 */ 4618 if (len) { 4619 /* Shouldn't happen! */ 4620 freemsg(mp0); 4621 *mpp = NULL; 4622 return (NULL); 4623 } 4624 /* 4625 * We consumed up to exactly the end of an mblk. Detach the part 4626 * we are returning from the rest of the chain. 4627 */ 4628 mp1->b_cont = NULL; 4629 *mpp = mp2; 4630 return (mp0); 4631 } 4632 4633 /* The ill stream is being unplumbed. Called from ip_close */ 4634 int 4635 ip_modclose(ill_t *ill) 4636 { 4637 4638 boolean_t success; 4639 ipsq_t *ipsq; 4640 ipif_t *ipif; 4641 queue_t *q = ill->ill_rq; 4642 4643 /* 4644 * Forcibly enter the ipsq after some delay. This is to take 4645 * care of the case when some ioctl does not complete because 4646 * we sent a control message to the driver and it did not 4647 * send us a reply. We want to be able to at least unplumb 4648 * and replumb rather than force the user to reboot the system. 4649 */ 4650 success = ipsq_enter(ill, B_FALSE); 4651 4652 /* 4653 * Open/close/push/pop is guaranteed to be single threaded 4654 * per stream by STREAMS. FS guarantees that all references 4655 * from top are gone before close is called. So there can't 4656 * be another close thread that has set CONDEMNED on this ill. 4657 * and cause ipsq_enter to return failure. 4658 */ 4659 ASSERT(success); 4660 ipsq = ill->ill_phyint->phyint_ipsq; 4661 4662 /* 4663 * Mark it condemned. No new reference will be made to this ill. 4664 * Lookup functions will return an error. Threads that try to 4665 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 4666 * that the refcnt will drop down to zero. 4667 */ 4668 mutex_enter(&ill->ill_lock); 4669 ill->ill_state_flags |= ILL_CONDEMNED; 4670 for (ipif = ill->ill_ipif; ipif != NULL; 4671 ipif = ipif->ipif_next) { 4672 ipif->ipif_state_flags |= IPIF_CONDEMNED; 4673 } 4674 /* 4675 * Wake up anybody waiting to enter the ipsq. ipsq_enter 4676 * returns error if ILL_CONDEMNED is set 4677 */ 4678 cv_broadcast(&ill->ill_cv); 4679 mutex_exit(&ill->ill_lock); 4680 4681 /* 4682 * Shut down fragmentation reassembly. 4683 * ill_frag_timer won't start a timer again. 4684 * Now cancel any existing timer 4685 */ 4686 (void) untimeout(ill->ill_frag_timer_id); 4687 (void) ill_frag_timeout(ill, 0); 4688 4689 /* 4690 * If MOVE was in progress, clear the 4691 * move_in_progress fields also. 4692 */ 4693 if (ill->ill_move_in_progress) { 4694 ILL_CLEAR_MOVE(ill); 4695 } 4696 4697 /* 4698 * Call ill_delete to bring down the ipifs, ilms and ill on 4699 * this ill. Then wait for the refcnts to drop to zero. 4700 * ill_is_quiescent checks whether the ill is really quiescent. 4701 * Then make sure that threads that are waiting to enter the 4702 * ipsq have seen the error returned by ipsq_enter and have 4703 * gone away. Then we call ill_delete_tail which does the 4704 * DL_UNBIND and DL_DETACH with the driver and then qprocsoff. 4705 */ 4706 ill_delete(ill); 4707 mutex_enter(&ill->ill_lock); 4708 while (!ill_is_quiescent(ill)) 4709 cv_wait(&ill->ill_cv, &ill->ill_lock); 4710 while (ill->ill_waiters) 4711 cv_wait(&ill->ill_cv, &ill->ill_lock); 4712 4713 mutex_exit(&ill->ill_lock); 4714 4715 /* qprocsoff is called in ill_delete_tail */ 4716 ill_delete_tail(ill); 4717 4718 /* 4719 * Walk through all upper (conn) streams and qenable 4720 * those that have queued data. 4721 * close synchronization needs this to 4722 * be done to ensure that all upper layers blocked 4723 * due to flow control to the closing device 4724 * get unblocked. 4725 */ 4726 ip1dbg(("ip_wsrv: walking\n")); 4727 conn_walk_drain(); 4728 4729 mutex_enter(&ip_mi_lock); 4730 mi_close_unlink(&ip_g_head, (IDP)ill); 4731 mutex_exit(&ip_mi_lock); 4732 4733 /* 4734 * credp could be null if the open didn't succeed and ip_modopen 4735 * itself calls ip_close. 4736 */ 4737 if (ill->ill_credp != NULL) 4738 crfree(ill->ill_credp); 4739 4740 mi_close_free((IDP)ill); 4741 q->q_ptr = WR(q)->q_ptr = NULL; 4742 4743 ipsq_exit(ipsq, B_TRUE, B_TRUE); 4744 4745 return (0); 4746 } 4747 4748 /* 4749 * This is called as part of close() for both IP and UDP 4750 * in order to quiesce the conn. 4751 */ 4752 void 4753 ip_quiesce_conn(conn_t *connp) 4754 { 4755 boolean_t drain_cleanup_reqd = B_FALSE; 4756 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4757 boolean_t ilg_cleanup_reqd = B_FALSE; 4758 4759 ASSERT(!IPCL_IS_TCP(connp)); 4760 4761 /* 4762 * Mark the conn as closing, and this conn must not be 4763 * inserted in future into any list. Eg. conn_drain_insert(), 4764 * won't insert this conn into the conn_drain_list. 4765 * Similarly ill_pending_mp_add() will not add any mp to 4766 * the pending mp list, after this conn has started closing. 4767 * 4768 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg 4769 * cannot get set henceforth. 4770 */ 4771 mutex_enter(&connp->conn_lock); 4772 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 4773 connp->conn_state_flags |= CONN_CLOSING; 4774 if (connp->conn_idl != NULL) 4775 drain_cleanup_reqd = B_TRUE; 4776 if (connp->conn_oper_pending_ill != NULL) 4777 conn_ioctl_cleanup_reqd = B_TRUE; 4778 if (connp->conn_ilg_inuse != 0) 4779 ilg_cleanup_reqd = B_TRUE; 4780 mutex_exit(&connp->conn_lock); 4781 4782 if (IPCL_IS_UDP(connp)) 4783 udp_quiesce_conn(connp); 4784 4785 if (conn_ioctl_cleanup_reqd) 4786 conn_ioctl_cleanup(connp); 4787 4788 /* 4789 * Remove this conn from any fanout list it is on. 4790 * and then wait for any threads currently operating 4791 * on this endpoint to finish 4792 */ 4793 ipcl_hash_remove(connp); 4794 4795 /* 4796 * Remove this conn from the drain list, and do 4797 * any other cleanup that may be required. 4798 * (Only non-tcp streams may have a non-null conn_idl. 4799 * TCP streams are never flow controlled, and 4800 * conn_idl will be null) 4801 */ 4802 if (drain_cleanup_reqd) 4803 conn_drain_tail(connp, B_TRUE); 4804 4805 if (connp->conn_rq == ip_g_mrouter || connp->conn_wq == ip_g_mrouter) 4806 (void) ip_mrouter_done(NULL); 4807 4808 if (ilg_cleanup_reqd) 4809 ilg_delete_all(connp); 4810 4811 conn_delete_ire(connp, NULL); 4812 4813 /* 4814 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 4815 * callers from write side can't be there now because close 4816 * is in progress. The only other caller is ipcl_walk 4817 * which checks for the condemned flag. 4818 */ 4819 mutex_enter(&connp->conn_lock); 4820 connp->conn_state_flags |= CONN_CONDEMNED; 4821 while (connp->conn_ref != 1) 4822 cv_wait(&connp->conn_cv, &connp->conn_lock); 4823 connp->conn_state_flags |= CONN_QUIESCED; 4824 mutex_exit(&connp->conn_lock); 4825 } 4826 4827 /* ARGSUSED */ 4828 int 4829 ip_close(queue_t *q, int flags) 4830 { 4831 conn_t *connp; 4832 4833 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); 4834 4835 /* 4836 * Call the appropriate delete routine depending on whether this is 4837 * a module or device. 4838 */ 4839 if (WR(q)->q_next != NULL) { 4840 /* This is a module close */ 4841 return (ip_modclose((ill_t *)q->q_ptr)); 4842 } 4843 4844 connp = q->q_ptr; 4845 ip_quiesce_conn(connp); 4846 4847 qprocsoff(q); 4848 4849 /* 4850 * Now we are truly single threaded on this stream, and can 4851 * delete the things hanging off the connp, and finally the connp. 4852 * We removed this connp from the fanout list, it cannot be 4853 * accessed thru the fanouts, and we already waited for the 4854 * conn_ref to drop to 0. We are already in close, so 4855 * there cannot be any other thread from the top. qprocsoff 4856 * has completed, and service has completed or won't run in 4857 * future. 4858 */ 4859 ASSERT(connp->conn_ref == 1); 4860 4861 /* 4862 * A conn which was previously marked as IPCL_UDP cannot 4863 * retain the flag because it would have been cleared by 4864 * udp_close(). 4865 */ 4866 ASSERT(!IPCL_IS_UDP(connp)); 4867 4868 if (connp->conn_latch != NULL) { 4869 IPLATCH_REFRELE(connp->conn_latch); 4870 connp->conn_latch = NULL; 4871 } 4872 if (connp->conn_policy != NULL) { 4873 IPPH_REFRELE(connp->conn_policy); 4874 connp->conn_policy = NULL; 4875 } 4876 if (connp->conn_ipsec_opt_mp != NULL) { 4877 freemsg(connp->conn_ipsec_opt_mp); 4878 connp->conn_ipsec_opt_mp = NULL; 4879 } 4880 if (connp->conn_cred != NULL) { 4881 crfree(connp->conn_cred); 4882 connp->conn_cred = NULL; 4883 } 4884 4885 inet_minor_free(ip_minor_arena, connp->conn_dev); 4886 4887 connp->conn_ref--; 4888 ipcl_conn_destroy(connp); 4889 4890 q->q_ptr = WR(q)->q_ptr = NULL; 4891 return (0); 4892 } 4893 4894 int 4895 ip_snmpmod_close(queue_t *q) 4896 { 4897 conn_t *connp = Q_TO_CONN(q); 4898 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 4899 4900 qprocsoff(q); 4901 4902 if (connp->conn_flags & IPCL_UDPMOD) 4903 udp_close_free(connp); 4904 4905 if (connp->conn_cred != NULL) { 4906 crfree(connp->conn_cred); 4907 connp->conn_cred = NULL; 4908 } 4909 CONN_DEC_REF(connp); 4910 q->q_ptr = WR(q)->q_ptr = NULL; 4911 return (0); 4912 } 4913 4914 /* 4915 * Write side put procedure for TCP module or UDP module instance. TCP/UDP 4916 * as a module is only used for MIB browsers that push TCP/UDP over IP or ARP. 4917 * The only supported primitives are T_SVR4_OPTMGMT_REQ and T_OPTMGMT_REQ. 4918 * M_FLUSH messages and ioctls are only passed downstream; we don't flush our 4919 * queues as we never enqueue messages there and we don't handle any ioctls. 4920 * Everything else is freed. 4921 */ 4922 void 4923 ip_snmpmod_wput(queue_t *q, mblk_t *mp) 4924 { 4925 conn_t *connp = q->q_ptr; 4926 pfi_t setfn; 4927 pfi_t getfn; 4928 4929 ASSERT(connp->conn_flags & (IPCL_TCPMOD | IPCL_UDPMOD)); 4930 4931 switch (DB_TYPE(mp)) { 4932 case M_PROTO: 4933 case M_PCPROTO: 4934 if ((MBLKL(mp) >= sizeof (t_scalar_t)) && 4935 ((((union T_primitives *)mp->b_rptr)->type == 4936 T_SVR4_OPTMGMT_REQ) || 4937 (((union T_primitives *)mp->b_rptr)->type == 4938 T_OPTMGMT_REQ))) { 4939 /* 4940 * This is the only TPI primitive supported. Its 4941 * handling does not require tcp_t, but it does require 4942 * conn_t to check permissions. 4943 */ 4944 cred_t *cr = DB_CREDDEF(mp, connp->conn_cred); 4945 4946 if (connp->conn_flags & IPCL_TCPMOD) { 4947 setfn = tcp_snmp_set; 4948 getfn = tcp_snmp_get; 4949 } else { 4950 setfn = udp_snmp_set; 4951 getfn = udp_snmp_get; 4952 } 4953 if (!snmpcom_req(q, mp, setfn, getfn, cr)) { 4954 freemsg(mp); 4955 return; 4956 } 4957 } else if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, ENOTSUP)) 4958 != NULL) 4959 qreply(q, mp); 4960 break; 4961 case M_FLUSH: 4962 case M_IOCTL: 4963 putnext(q, mp); 4964 break; 4965 default: 4966 freemsg(mp); 4967 break; 4968 } 4969 } 4970 4971 /* Return the IP checksum for the IP header at "iph". */ 4972 uint16_t 4973 ip_csum_hdr(ipha_t *ipha) 4974 { 4975 uint16_t *uph; 4976 uint32_t sum; 4977 int opt_len; 4978 4979 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - 4980 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 4981 uph = (uint16_t *)ipha; 4982 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 4983 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 4984 if (opt_len > 0) { 4985 do { 4986 sum += uph[10]; 4987 sum += uph[11]; 4988 uph += 2; 4989 } while (--opt_len); 4990 } 4991 sum = (sum & 0xFFFF) + (sum >> 16); 4992 sum = ~(sum + (sum >> 16)) & 0xFFFF; 4993 if (sum == 0xffff) 4994 sum = 0; 4995 return ((uint16_t)sum); 4996 } 4997 4998 void 4999 ip_ddi_destroy(void) 5000 { 5001 tcp_ddi_destroy(); 5002 sctp_ddi_destroy(); 5003 ipsec_loader_destroy(); 5004 ipsec_policy_destroy(); 5005 ipsec_kstat_destroy(); 5006 nd_free(&ip_g_nd); 5007 mutex_destroy(&igmp_timer_lock); 5008 mutex_destroy(&mld_timer_lock); 5009 mutex_destroy(&igmp_slowtimeout_lock); 5010 mutex_destroy(&mld_slowtimeout_lock); 5011 mutex_destroy(&ip_mi_lock); 5012 mutex_destroy(&rts_clients.connf_lock); 5013 ip_ire_fini(); 5014 ip6_asp_free(); 5015 conn_drain_fini(); 5016 ipcl_destroy(); 5017 inet_minor_destroy(ip_minor_arena); 5018 icmp_kstat_fini(); 5019 ip_kstat_fini(); 5020 rw_destroy(&ipsec_capab_ills_lock); 5021 rw_destroy(&ill_g_usesrc_lock); 5022 ip_drop_unregister(&ip_dropper); 5023 } 5024 5025 5026 void 5027 ip_ddi_init(void) 5028 { 5029 TCP6_MAJ = ddi_name_to_major(TCP6); 5030 TCP_MAJ = ddi_name_to_major(TCP); 5031 SCTP_MAJ = ddi_name_to_major(SCTP); 5032 SCTP6_MAJ = ddi_name_to_major(SCTP6); 5033 5034 ip_input_proc = ip_squeue_switch(ip_squeue_enter); 5035 5036 /* IP's IPsec code calls the packet dropper */ 5037 ip_drop_register(&ip_dropper, "IP IPsec processing"); 5038 5039 if (!ip_g_nd) { 5040 if (!ip_param_register(lcl_param_arr, A_CNT(lcl_param_arr), 5041 lcl_ndp_arr, A_CNT(lcl_ndp_arr))) { 5042 nd_free(&ip_g_nd); 5043 } 5044 } 5045 5046 ipsec_loader_init(); 5047 ipsec_policy_init(); 5048 ipsec_kstat_init(); 5049 rw_init(&ip_g_nd_lock, NULL, RW_DEFAULT, NULL); 5050 mutex_init(&igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5051 mutex_init(&mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 5052 mutex_init(&igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5053 mutex_init(&mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 5054 mutex_init(&ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 5055 mutex_init(&ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 5056 rw_init(&ill_g_lock, NULL, RW_DEFAULT, NULL); 5057 rw_init(&ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); 5058 rw_init(&ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 5059 5060 /* 5061 * For IP and TCP the minor numbers should start from 2 since we have 4 5062 * initial devices: ip, ip6, tcp, tcp6. 5063 */ 5064 if ((ip_minor_arena = inet_minor_create("ip_minor_arena", 5065 INET_MIN_DEV + 2, KM_SLEEP)) == NULL) { 5066 cmn_err(CE_PANIC, 5067 "ip_ddi_init: ip_minor_arena creation failed\n"); 5068 } 5069 5070 ipcl_init(); 5071 mutex_init(&rts_clients.connf_lock, NULL, MUTEX_DEFAULT, NULL); 5072 ip_ire_init(); 5073 ip6_asp_init(); 5074 ipif_init(); 5075 conn_drain_init(); 5076 tcp_ddi_init(); 5077 sctp_ddi_init(); 5078 5079 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 5080 5081 if ((ip_kstat = kstat_create("ip", 0, "ipstat", 5082 "net", KSTAT_TYPE_NAMED, 5083 sizeof (ip_statistics) / sizeof (kstat_named_t), 5084 KSTAT_FLAG_VIRTUAL)) != NULL) { 5085 ip_kstat->ks_data = &ip_statistics; 5086 kstat_install(ip_kstat); 5087 } 5088 ip_kstat_init(); 5089 ip6_kstat_init(); 5090 icmp_kstat_init(); 5091 5092 ipsec_loader_start(); 5093 } 5094 5095 /* 5096 * Allocate and initialize a DLPI template of the specified length. (May be 5097 * called as writer.) 5098 */ 5099 mblk_t * 5100 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 5101 { 5102 mblk_t *mp; 5103 5104 mp = allocb(len, BPRI_MED); 5105 if (!mp) 5106 return (NULL); 5107 5108 /* 5109 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 5110 * of which we don't seem to use) are sent with M_PCPROTO, and 5111 * that other DLPI are M_PROTO. 5112 */ 5113 if (prim == DL_INFO_REQ) { 5114 mp->b_datap->db_type = M_PCPROTO; 5115 } else { 5116 mp->b_datap->db_type = M_PROTO; 5117 } 5118 5119 mp->b_wptr = mp->b_rptr + len; 5120 bzero(mp->b_rptr, len); 5121 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 5122 return (mp); 5123 } 5124 5125 const char * 5126 dlpi_prim_str(int prim) 5127 { 5128 switch (prim) { 5129 case DL_INFO_REQ: return ("DL_INFO_REQ"); 5130 case DL_INFO_ACK: return ("DL_INFO_ACK"); 5131 case DL_ATTACH_REQ: return ("DL_ATTACH_REQ"); 5132 case DL_DETACH_REQ: return ("DL_DETACH_REQ"); 5133 case DL_BIND_REQ: return ("DL_BIND_REQ"); 5134 case DL_BIND_ACK: return ("DL_BIND_ACK"); 5135 case DL_UNBIND_REQ: return ("DL_UNBIND_REQ"); 5136 case DL_OK_ACK: return ("DL_OK_ACK"); 5137 case DL_ERROR_ACK: return ("DL_ERROR_ACK"); 5138 case DL_ENABMULTI_REQ: return ("DL_ENABMULTI_REQ"); 5139 case DL_DISABMULTI_REQ: return ("DL_DISABMULTI_REQ"); 5140 case DL_PROMISCON_REQ: return ("DL_PROMISCON_REQ"); 5141 case DL_PROMISCOFF_REQ: return ("DL_PROMISCOFF_REQ"); 5142 case DL_UNITDATA_REQ: return ("DL_UNITDATA_REQ"); 5143 case DL_UNITDATA_IND: return ("DL_UNITDATA_IND"); 5144 case DL_UDERROR_IND: return ("DL_UDERROR_IND"); 5145 case DL_PHYS_ADDR_REQ: return ("DL_PHYS_ADDR_REQ"); 5146 case DL_PHYS_ADDR_ACK: return ("DL_PHYS_ADDR_ACK"); 5147 case DL_SET_PHYS_ADDR_REQ: return ("DL_SET_PHYS_ADDR_REQ"); 5148 case DL_NOTIFY_REQ: return ("DL_NOTIFY_REQ"); 5149 case DL_NOTIFY_ACK: return ("DL_NOTIFY_ACK"); 5150 case DL_NOTIFY_IND: return ("DL_NOTIFY_IND"); 5151 case DL_CAPABILITY_REQ: return ("DL_CAPABILITY_REQ"); 5152 case DL_CAPABILITY_ACK: return ("DL_CAPABILITY_ACK"); 5153 case DL_CONTROL_REQ: return ("DL_CONTROL_REQ"); 5154 case DL_CONTROL_ACK: return ("DL_CONTROL_ACK"); 5155 default: return ("<unknown primitive>"); 5156 } 5157 } 5158 5159 const char * 5160 dlpi_err_str(int err) 5161 { 5162 switch (err) { 5163 case DL_ACCESS: return ("DL_ACCESS"); 5164 case DL_BADADDR: return ("DL_BADADDR"); 5165 case DL_BADCORR: return ("DL_BADCORR"); 5166 case DL_BADDATA: return ("DL_BADDATA"); 5167 case DL_BADPPA: return ("DL_BADPPA"); 5168 case DL_BADPRIM: return ("DL_BADPRIM"); 5169 case DL_BADQOSPARAM: return ("DL_BADQOSPARAM"); 5170 case DL_BADQOSTYPE: return ("DL_BADQOSTYPE"); 5171 case DL_BADSAP: return ("DL_BADSAP"); 5172 case DL_BADTOKEN: return ("DL_BADTOKEN"); 5173 case DL_BOUND: return ("DL_BOUND"); 5174 case DL_INITFAILED: return ("DL_INITFAILED"); 5175 case DL_NOADDR: return ("DL_NOADDR"); 5176 case DL_NOTINIT: return ("DL_NOTINIT"); 5177 case DL_OUTSTATE: return ("DL_OUTSTATE"); 5178 case DL_SYSERR: return ("DL_SYSERR"); 5179 case DL_UNSUPPORTED: return ("DL_UNSUPPORTED"); 5180 case DL_UNDELIVERABLE: return ("DL_UNDELIVERABLE"); 5181 case DL_NOTSUPPORTED : return ("DL_NOTSUPPORTED "); 5182 case DL_TOOMANY: return ("DL_TOOMANY"); 5183 case DL_NOTENAB: return ("DL_NOTENAB"); 5184 case DL_BUSY: return ("DL_BUSY"); 5185 case DL_NOAUTO: return ("DL_NOAUTO"); 5186 case DL_NOXIDAUTO: return ("DL_NOXIDAUTO"); 5187 case DL_NOTESTAUTO: return ("DL_NOTESTAUTO"); 5188 case DL_XIDAUTO: return ("DL_XIDAUTO"); 5189 case DL_TESTAUTO: return ("DL_TESTAUTO"); 5190 case DL_PENDING: return ("DL_PENDING"); 5191 default: return ("<unknown error>"); 5192 } 5193 } 5194 5195 /* 5196 * Debug formatting routine. Returns a character string representation of the 5197 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 5198 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 5199 */ 5200 char * 5201 ip_dot_addr(ipaddr_t addr, char *buf) 5202 { 5203 return (ip_dot_saddr((uchar_t *)&addr, buf)); 5204 } 5205 5206 /* 5207 * Debug formatting routine. Returns a character string representation of the 5208 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 5209 * as a pointer. The "xxx" parts including left zero padding so the final 5210 * string will fit easily in tables. It would be nice to take a padding 5211 * length argument instead. 5212 */ 5213 static char * 5214 ip_dot_saddr(uchar_t *addr, char *buf) 5215 { 5216 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 5217 addr[0] & 0xFF, addr[1] & 0xFF, addr[2] & 0xFF, addr[3] & 0xFF); 5218 return (buf); 5219 } 5220 5221 /* 5222 * Send an ICMP error after patching up the packet appropriately. Returns 5223 * non-zero if the appropriate MIB should be bumped; zero otherwise. 5224 */ 5225 static boolean_t 5226 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, 5227 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, zoneid_t zoneid) 5228 { 5229 ipha_t *ipha; 5230 mblk_t *first_mp; 5231 boolean_t secure; 5232 unsigned char db_type; 5233 5234 first_mp = mp; 5235 if (mctl_present) { 5236 mp = mp->b_cont; 5237 secure = ipsec_in_is_secure(first_mp); 5238 ASSERT(mp != NULL); 5239 } else { 5240 /* 5241 * If this is an ICMP error being reported - which goes 5242 * up as M_CTLs, we need to convert them to M_DATA till 5243 * we finish checking with global policy because 5244 * ipsec_check_global_policy() assumes M_DATA as clear 5245 * and M_CTL as secure. 5246 */ 5247 db_type = DB_TYPE(mp); 5248 DB_TYPE(mp) = M_DATA; 5249 secure = B_FALSE; 5250 } 5251 /* 5252 * We are generating an icmp error for some inbound packet. 5253 * Called from all ip_fanout_(udp, tcp, proto) functions. 5254 * Before we generate an error, check with global policy 5255 * to see whether this is allowed to enter the system. As 5256 * there is no "conn", we are checking with global policy. 5257 */ 5258 ipha = (ipha_t *)mp->b_rptr; 5259 if (secure || ipsec_inbound_v4_policy_present) { 5260 first_mp = ipsec_check_global_policy(first_mp, NULL, 5261 ipha, NULL, mctl_present); 5262 if (first_mp == NULL) 5263 return (B_FALSE); 5264 } 5265 5266 if (!mctl_present) 5267 DB_TYPE(mp) = db_type; 5268 5269 if (flags & IP_FF_SEND_ICMP) { 5270 if (flags & IP_FF_HDR_COMPLETE) { 5271 if (ip_hdr_complete(ipha, zoneid)) { 5272 freemsg(first_mp); 5273 return (B_TRUE); 5274 } 5275 } 5276 if (flags & IP_FF_CKSUM) { 5277 /* 5278 * Have to correct checksum since 5279 * the packet might have been 5280 * fragmented and the reassembly code in ip_rput 5281 * does not restore the IP checksum. 5282 */ 5283 ipha->ipha_hdr_checksum = 0; 5284 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 5285 } 5286 switch (icmp_type) { 5287 case ICMP_DEST_UNREACHABLE: 5288 icmp_unreachable(WR(q), first_mp, icmp_code); 5289 break; 5290 default: 5291 freemsg(first_mp); 5292 break; 5293 } 5294 } else { 5295 freemsg(first_mp); 5296 return (B_FALSE); 5297 } 5298 5299 return (B_TRUE); 5300 } 5301 5302 #ifdef DEBUG 5303 /* 5304 * Copy the header into the IPSEC_IN message. 5305 */ 5306 static void 5307 ipsec_inbound_debug_tag(mblk_t *ipsec_mp) 5308 { 5309 mblk_t *data_mp = ipsec_mp->b_cont; 5310 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 5311 ipha_t *ipha; 5312 5313 if (ii->ipsec_in_type != IPSEC_IN) 5314 return; 5315 ASSERT(data_mp != NULL); 5316 5317 ipha = (ipha_t *)data_mp->b_rptr; 5318 bcopy(ipha, ii->ipsec_in_saved_hdr, 5319 (IPH_HDR_VERSION(ipha) == IP_VERSION) ? 5320 sizeof (ipha_t) : sizeof (ip6_t)); 5321 } 5322 #else 5323 #define ipsec_inbound_debug_tag(x) /* NOP */ 5324 #endif /* DEBUG */ 5325 5326 /* 5327 * Used to send an ICMP error message when a packet is received for 5328 * a protocol that is not supported. The mblk passed as argument 5329 * is consumed by this function. 5330 */ 5331 void 5332 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid) 5333 { 5334 mblk_t *mp; 5335 ipha_t *ipha; 5336 ill_t *ill; 5337 ipsec_in_t *ii; 5338 5339 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 5340 ASSERT(ii->ipsec_in_type == IPSEC_IN); 5341 5342 mp = ipsec_mp->b_cont; 5343 ipsec_mp->b_cont = NULL; 5344 ipha = (ipha_t *)mp->b_rptr; 5345 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 5346 if (ip_fanout_send_icmp(q, mp, flags, ICMP_DEST_UNREACHABLE, 5347 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid)) { 5348 BUMP_MIB(&ip_mib, ipInUnknownProtos); 5349 } 5350 } else { 5351 /* Get ill from index in ipsec_in_t. */ 5352 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 5353 B_TRUE, NULL, NULL, NULL, NULL); 5354 if (ill != NULL) { 5355 if (ip_fanout_send_icmp_v6(q, mp, flags, 5356 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 5357 0, B_FALSE, zoneid)) { 5358 BUMP_MIB(ill->ill_ip6_mib, ipv6InUnknownProtos); 5359 } 5360 5361 ill_refrele(ill); 5362 } else { /* re-link for the freemsg() below. */ 5363 ipsec_mp->b_cont = mp; 5364 } 5365 } 5366 5367 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ 5368 freemsg(ipsec_mp); 5369 } 5370 5371 /* 5372 * See if the inbound datagram has had IPsec processing applied to it. 5373 */ 5374 boolean_t 5375 ipsec_in_is_secure(mblk_t *ipsec_mp) 5376 { 5377 ipsec_in_t *ii; 5378 5379 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 5380 ASSERT(ii->ipsec_in_type == IPSEC_IN); 5381 5382 if (ii->ipsec_in_loopback) { 5383 return (ii->ipsec_in_secure); 5384 } else { 5385 return (ii->ipsec_in_ah_sa != NULL || 5386 ii->ipsec_in_esp_sa != NULL || 5387 ii->ipsec_in_decaps); 5388 } 5389 } 5390 5391 /* 5392 * Handle protocols with which IP is less intimate. There 5393 * can be more than one stream bound to a particular 5394 * protocol. When this is the case, normally each one gets a copy 5395 * of any incoming packets. 5396 * 5397 * IPSEC NOTE : 5398 * 5399 * Don't allow a secure packet going up a non-secure connection. 5400 * We don't allow this because 5401 * 5402 * 1) Reply might go out in clear which will be dropped at 5403 * the sending side. 5404 * 2) If the reply goes out in clear it will give the 5405 * adversary enough information for getting the key in 5406 * most of the cases. 5407 * 5408 * Moreover getting a secure packet when we expect clear 5409 * implies that SA's were added without checking for 5410 * policy on both ends. This should not happen once ISAKMP 5411 * is used to negotiate SAs as SAs will be added only after 5412 * verifying the policy. 5413 * 5414 * NOTE : If the packet was tunneled and not multicast we only send 5415 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall 5416 * back to delivering packets to AF_INET6 raw sockets. 5417 * 5418 * IPQoS Notes: 5419 * Once we have determined the client, invoke IPPF processing. 5420 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 5421 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 5422 * ip_policy will be false. 5423 * 5424 * Zones notes: 5425 * Currently only applications in the global zone can create raw sockets for 5426 * protocols other than ICMP. So unlike the broadcast / multicast case of 5427 * ip_fanout_udp(), we only send a copy of the packet to streams in the 5428 * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). 5429 */ 5430 static void 5431 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, 5432 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, 5433 zoneid_t zoneid) 5434 { 5435 queue_t *rq; 5436 mblk_t *mp1, *first_mp1; 5437 uint_t protocol = ipha->ipha_protocol; 5438 ipaddr_t dst; 5439 boolean_t one_only; 5440 mblk_t *first_mp = mp; 5441 boolean_t secure; 5442 uint32_t ill_index; 5443 conn_t *connp, *first_connp, *next_connp; 5444 connf_t *connfp; 5445 5446 if (mctl_present) { 5447 mp = first_mp->b_cont; 5448 secure = ipsec_in_is_secure(first_mp); 5449 ASSERT(mp != NULL); 5450 } else { 5451 secure = B_FALSE; 5452 } 5453 dst = ipha->ipha_dst; 5454 /* 5455 * If the packet was tunneled and not multicast we only send to it 5456 * the first match. 5457 */ 5458 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && 5459 !CLASSD(dst)); 5460 5461 connfp = &ipcl_proto_fanout[protocol]; 5462 mutex_enter(&connfp->connf_lock); 5463 connp = connfp->connf_head; 5464 for (connp = connfp->connf_head; connp != NULL; 5465 connp = connp->conn_next) { 5466 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, zoneid)) 5467 break; 5468 } 5469 5470 if (connp == NULL || connp->conn_upq == NULL) { 5471 /* 5472 * No one bound to these addresses. Is 5473 * there a client that wants all 5474 * unclaimed datagrams? 5475 */ 5476 mutex_exit(&connfp->connf_lock); 5477 /* 5478 * Check for IPPROTO_ENCAP... 5479 */ 5480 if (protocol == IPPROTO_ENCAP && ip_g_mrouter) { 5481 /* 5482 * XXX If an IPsec mblk is here on a multicast 5483 * tunnel (using ip_mroute stuff), what should 5484 * I do? 5485 * 5486 * For now, just free the IPsec mblk before 5487 * passing it up to the multicast routing 5488 * stuff. 5489 * 5490 * BTW, If I match a configured IP-in-IP 5491 * tunnel, ip_mroute_decap will never be 5492 * called. 5493 */ 5494 if (mp != first_mp) 5495 freeb(first_mp); 5496 ip_mroute_decap(q, mp); 5497 } else { 5498 /* 5499 * Otherwise send an ICMP protocol unreachable. 5500 */ 5501 if (ip_fanout_send_icmp(q, first_mp, flags, 5502 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, 5503 mctl_present, zoneid)) { 5504 BUMP_MIB(&ip_mib, ipInUnknownProtos); 5505 } 5506 } 5507 return; 5508 } 5509 CONN_INC_REF(connp); 5510 first_connp = connp; 5511 5512 /* 5513 * Only send message to one tunnel driver by immediately 5514 * terminating the loop. 5515 */ 5516 connp = one_only ? NULL : connp->conn_next; 5517 5518 for (;;) { 5519 while (connp != NULL) { 5520 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, 5521 flags, zoneid)) 5522 break; 5523 connp = connp->conn_next; 5524 } 5525 5526 /* 5527 * Copy the packet. 5528 */ 5529 if (connp == NULL || connp->conn_upq == NULL || 5530 (((first_mp1 = dupmsg(first_mp)) == NULL) && 5531 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 5532 /* 5533 * No more interested clients or memory 5534 * allocation failed 5535 */ 5536 connp = first_connp; 5537 break; 5538 } 5539 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 5540 CONN_INC_REF(connp); 5541 mutex_exit(&connfp->connf_lock); 5542 rq = connp->conn_rq; 5543 if (!canputnext(rq)) { 5544 if (flags & IP_FF_RAWIP) { 5545 BUMP_MIB(&ip_mib, rawipInOverflows); 5546 } else { 5547 BUMP_MIB(&icmp_mib, icmpInOverflows); 5548 } 5549 5550 freemsg(first_mp1); 5551 } else { 5552 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 5553 first_mp1 = ipsec_check_inbound_policy 5554 (first_mp1, connp, ipha, NULL, 5555 mctl_present); 5556 } 5557 if (first_mp1 != NULL) { 5558 /* 5559 * ip_fanout_proto also gets called from 5560 * icmp_inbound_error_fanout, in which case 5561 * the msg type is M_CTL. Don't add info 5562 * in this case for the time being. In future 5563 * when there is a need for knowing the 5564 * inbound iface index for ICMP error msgs, 5565 * then this can be changed. 5566 */ 5567 if ((connp->conn_recvif != 0) && 5568 (mp->b_datap->db_type != M_CTL)) { 5569 /* 5570 * the actual data will be 5571 * contained in b_cont upon 5572 * successful return of the 5573 * following call else 5574 * original mblk is returned 5575 */ 5576 ASSERT(recv_ill != NULL); 5577 mp1 = ip_add_info(mp1, recv_ill, 5578 IPF_RECVIF); 5579 } 5580 BUMP_MIB(&ip_mib, ipInDelivers); 5581 if (mctl_present) 5582 freeb(first_mp1); 5583 putnext(rq, mp1); 5584 } 5585 } 5586 mutex_enter(&connfp->connf_lock); 5587 /* Follow the next pointer before releasing the conn. */ 5588 next_connp = connp->conn_next; 5589 CONN_DEC_REF(connp); 5590 connp = next_connp; 5591 } 5592 5593 /* Last one. Send it upstream. */ 5594 mutex_exit(&connfp->connf_lock); 5595 5596 /* 5597 * If this packet is coming from icmp_inbound_error_fanout ip_policy 5598 * will be set to false. 5599 */ 5600 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 5601 ill_index = ill->ill_phyint->phyint_ifindex; 5602 ip_process(IPP_LOCAL_IN, &mp, ill_index); 5603 if (mp == NULL) { 5604 CONN_DEC_REF(connp); 5605 if (mctl_present) { 5606 freeb(first_mp); 5607 } 5608 return; 5609 } 5610 } 5611 5612 rq = connp->conn_rq; 5613 if (!canputnext(rq)) { 5614 if (flags & IP_FF_RAWIP) { 5615 BUMP_MIB(&ip_mib, rawipInOverflows); 5616 } else { 5617 BUMP_MIB(&icmp_mib, icmpInOverflows); 5618 } 5619 5620 freemsg(first_mp); 5621 } else { 5622 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 5623 first_mp = ipsec_check_inbound_policy(first_mp, connp, 5624 ipha, NULL, mctl_present); 5625 } 5626 if (first_mp != NULL) { 5627 /* 5628 * ip_fanout_proto also gets called 5629 * from icmp_inbound_error_fanout, in 5630 * which case the msg type is M_CTL. 5631 * Don't add info in this case for time 5632 * being. In future when there is a 5633 * need for knowing the inbound iface 5634 * index for ICMP error msgs, then this 5635 * can be changed 5636 */ 5637 if ((connp->conn_recvif != 0) && 5638 (mp->b_datap->db_type != M_CTL)) { 5639 /* 5640 * the actual data will be contained in 5641 * b_cont upon successful return 5642 * of the following call else original 5643 * mblk is returned 5644 */ 5645 ASSERT(recv_ill != NULL); 5646 mp = ip_add_info(mp, recv_ill, IPF_RECVIF); 5647 } 5648 BUMP_MIB(&ip_mib, ipInDelivers); 5649 putnext(rq, mp); 5650 if (mctl_present) 5651 freeb(first_mp); 5652 } 5653 } 5654 CONN_DEC_REF(connp); 5655 } 5656 5657 /* 5658 * Fanout for TCP packets 5659 * The caller puts <fport, lport> in the ports parameter. 5660 * 5661 * IPQoS Notes 5662 * Before sending it to the client, invoke IPPF processing. 5663 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, 5664 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local 5665 * ip_policy is false. 5666 */ 5667 static void 5668 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, 5669 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) 5670 { 5671 mblk_t *first_mp; 5672 boolean_t secure; 5673 uint32_t ill_index; 5674 int ip_hdr_len; 5675 tcph_t *tcph; 5676 boolean_t syn_present = B_FALSE; 5677 conn_t *connp; 5678 5679 first_mp = mp; 5680 if (mctl_present) { 5681 ASSERT(first_mp->b_datap->db_type == M_CTL); 5682 mp = first_mp->b_cont; 5683 secure = ipsec_in_is_secure(first_mp); 5684 ASSERT(mp != NULL); 5685 } else { 5686 secure = B_FALSE; 5687 } 5688 5689 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); 5690 5691 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid)) == 5692 NULL) { 5693 /* 5694 * No connected connection or listener. Send a 5695 * TH_RST via tcp_xmit_listeners_reset. 5696 */ 5697 5698 /* Initiate IPPf processing, if needed. */ 5699 if (IPP_ENABLED(IPP_LOCAL_IN)) { 5700 uint32_t ill_index; 5701 ill_index = recv_ill->ill_phyint->phyint_ifindex; 5702 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 5703 if (first_mp == NULL) 5704 return; 5705 } 5706 BUMP_MIB(&ip_mib, ipInDelivers); 5707 tcp_xmit_listeners_reset(first_mp, ip_hdr_len); 5708 return; 5709 } 5710 5711 /* 5712 * Allocate the SYN for the TCP connection here itself 5713 */ 5714 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5715 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 5716 if (IPCL_IS_TCP(connp)) { 5717 squeue_t *sqp; 5718 5719 /* 5720 * For fused tcp loopback, assign the eager's 5721 * squeue to be that of the active connect's. 5722 * Note that we don't check for IP_FF_LOOPBACK 5723 * here since this routine gets called only 5724 * for loopback (unlike the IPv6 counterpart). 5725 */ 5726 ASSERT(Q_TO_CONN(q) != NULL); 5727 if (do_tcp_fusion && 5728 !CONN_INBOUND_POLICY_PRESENT(connp) && !secure && 5729 !IPP_ENABLED(IPP_LOCAL_IN) && !ip_policy && 5730 IPCL_IS_TCP(Q_TO_CONN(q))) { 5731 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); 5732 sqp = Q_TO_CONN(q)->conn_sqp; 5733 } else { 5734 sqp = IP_SQUEUE_GET(lbolt); 5735 } 5736 5737 mp->b_datap->db_struioflag |= STRUIO_EAGER; 5738 DB_CKSUMSTART(mp) = (intptr_t)sqp; 5739 syn_present = B_TRUE; 5740 } 5741 } 5742 5743 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 5744 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 5745 if ((flags & TH_RST) || (flags & TH_URG)) { 5746 CONN_DEC_REF(connp); 5747 freemsg(first_mp); 5748 return; 5749 } 5750 if (flags & TH_ACK) { 5751 tcp_xmit_listeners_reset(first_mp, ip_hdr_len); 5752 CONN_DEC_REF(connp); 5753 return; 5754 } 5755 5756 CONN_DEC_REF(connp); 5757 freemsg(first_mp); 5758 return; 5759 } 5760 5761 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 5762 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 5763 NULL, mctl_present); 5764 if (first_mp == NULL) { 5765 CONN_DEC_REF(connp); 5766 return; 5767 } 5768 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 5769 ASSERT(syn_present); 5770 if (mctl_present) { 5771 ASSERT(first_mp != mp); 5772 first_mp->b_datap->db_struioflag |= 5773 STRUIO_POLICY; 5774 } else { 5775 ASSERT(first_mp == mp); 5776 mp->b_datap->db_struioflag &= 5777 ~STRUIO_EAGER; 5778 mp->b_datap->db_struioflag |= 5779 STRUIO_POLICY; 5780 } 5781 } else { 5782 /* 5783 * Discard first_mp early since we're dealing with a 5784 * fully-connected conn_t and tcp doesn't do policy in 5785 * this case. 5786 */ 5787 if (mctl_present) { 5788 freeb(first_mp); 5789 mctl_present = B_FALSE; 5790 } 5791 first_mp = mp; 5792 } 5793 } 5794 5795 /* 5796 * Initiate policy processing here if needed. If we get here from 5797 * icmp_inbound_error_fanout, ip_policy is false. 5798 */ 5799 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 5800 ill_index = recv_ill->ill_phyint->phyint_ifindex; 5801 ip_process(IPP_LOCAL_IN, &mp, ill_index); 5802 if (mp == NULL) { 5803 CONN_DEC_REF(connp); 5804 if (mctl_present) 5805 freeb(first_mp); 5806 return; 5807 } else if (mctl_present) { 5808 ASSERT(first_mp != mp); 5809 first_mp->b_cont = mp; 5810 } else { 5811 first_mp = mp; 5812 } 5813 } 5814 5815 5816 5817 /* Handle IPv6 socket options. */ 5818 if (!syn_present && 5819 connp->conn_ipv6_recvpktinfo && (flags & IP_FF_IP6INFO)) { 5820 /* Add header */ 5821 ASSERT(recv_ill != NULL); 5822 mp = ip_add_info(mp, recv_ill, IPF_RECVIF); 5823 if (mp == NULL) { 5824 CONN_DEC_REF(connp); 5825 if (mctl_present) 5826 freeb(first_mp); 5827 return; 5828 } else if (mctl_present) { 5829 /* 5830 * ip_add_info might return a new mp. 5831 */ 5832 ASSERT(first_mp != mp); 5833 first_mp->b_cont = mp; 5834 } else { 5835 first_mp = mp; 5836 } 5837 } 5838 5839 BUMP_MIB(&ip_mib, ipInDelivers); 5840 if (IPCL_IS_TCP(connp)) { 5841 (*ip_input_proc)(connp->conn_sqp, first_mp, 5842 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); 5843 } else { 5844 putnext(connp->conn_rq, first_mp); 5845 CONN_DEC_REF(connp); 5846 } 5847 } 5848 5849 /* 5850 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 5851 * We are responsible for disposing of mp, such as by freemsg() or putnext() 5852 * Caller is responsible for dropping references to the conn, and freeing 5853 * first_mp. 5854 * 5855 * IPQoS Notes 5856 * Before sending it to the client, invoke IPPF processing. Policy processing 5857 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and 5858 * ip_policy is true. If we get here from icmp_inbound_error_fanout or 5859 * ip_wput_local, ip_policy is false. 5860 */ 5861 static void 5862 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, 5863 boolean_t secure, ipha_t *ipha, uint_t flags, ill_t *recv_ill, 5864 boolean_t ip_policy) 5865 { 5866 boolean_t mctl_present = (first_mp != NULL); 5867 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ 5868 uint32_t ill_index; 5869 5870 if (mctl_present) 5871 first_mp->b_cont = mp; 5872 else 5873 first_mp = mp; 5874 5875 if (CONN_UDP_FLOWCTLD(connp)) { 5876 BUMP_MIB(&ip_mib, udpInOverflows); 5877 freemsg(first_mp); 5878 return; 5879 } 5880 5881 if (CONN_INBOUND_POLICY_PRESENT(connp) || secure) { 5882 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, 5883 NULL, mctl_present); 5884 if (first_mp == NULL) 5885 return; /* Freed by ipsec_check_inbound_policy(). */ 5886 } 5887 if (mctl_present) 5888 freeb(first_mp); 5889 5890 if (connp->conn_recvif) 5891 in_flags = IPF_RECVIF; 5892 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) 5893 in_flags |= IPF_RECVSLLA; 5894 5895 /* Handle IPv6 options. */ 5896 if (connp->conn_ipv6_recvpktinfo && (flags & IP_FF_IP6INFO)) 5897 in_flags |= IPF_RECVIF; 5898 5899 /* 5900 * Initiate IPPF processing here, if needed. Note first_mp won't be 5901 * freed if the packet is dropped. The caller will do so. 5902 */ 5903 if (IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) { 5904 ill_index = recv_ill->ill_phyint->phyint_ifindex; 5905 ip_process(IPP_LOCAL_IN, &mp, ill_index); 5906 if (mp == NULL) { 5907 return; 5908 } 5909 } 5910 if ((in_flags != 0) && 5911 (mp->b_datap->db_type != M_CTL)) { 5912 /* 5913 * The actual data will be contained in b_cont 5914 * upon successful return of the following call 5915 * else original mblk is returned 5916 */ 5917 ASSERT(recv_ill != NULL); 5918 mp = ip_add_info(mp, recv_ill, in_flags); 5919 } 5920 BUMP_MIB(&ip_mib, ipInDelivers); 5921 5922 /* Send it upstream */ 5923 CONN_UDP_RECV(connp, mp); 5924 } 5925 5926 /* 5927 * Fanout for UDP packets. 5928 * The caller puts <fport, lport> in the ports parameter. 5929 * 5930 * If SO_REUSEADDR is set all multicast and broadcast packets 5931 * will be delivered to all streams bound to the same port. 5932 * 5933 * Zones notes: 5934 * Multicast and broadcast packets will be distributed to streams in all zones. 5935 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 5936 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 5937 * packets. To maintain this behavior with multiple zones, the conns are grouped 5938 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in 5939 * each zone. If unset, all the following conns in the same zone are skipped. 5940 */ 5941 static void 5942 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 5943 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, 5944 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) 5945 { 5946 uint32_t dstport, srcport; 5947 ipaddr_t dst; 5948 mblk_t *first_mp; 5949 boolean_t secure; 5950 in6_addr_t v6src; 5951 conn_t *connp; 5952 connf_t *connfp; 5953 conn_t *first_connp; 5954 conn_t *next_connp; 5955 mblk_t *mp1, *first_mp1; 5956 ipaddr_t src; 5957 zoneid_t last_zoneid; 5958 boolean_t reuseaddr; 5959 5960 first_mp = mp; 5961 if (mctl_present) { 5962 mp = first_mp->b_cont; 5963 first_mp->b_cont = NULL; 5964 secure = ipsec_in_is_secure(first_mp); 5965 ASSERT(mp != NULL); 5966 } else { 5967 first_mp = NULL; 5968 secure = B_FALSE; 5969 } 5970 5971 /* Extract ports in net byte order */ 5972 dstport = htons(ntohl(ports) & 0xFFFF); 5973 srcport = htons(ntohl(ports) >> 16); 5974 dst = ipha->ipha_dst; 5975 src = ipha->ipha_src; 5976 5977 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 5978 mutex_enter(&connfp->connf_lock); 5979 connp = connfp->connf_head; 5980 if (!broadcast && !CLASSD(dst)) { 5981 /* 5982 * Not broadcast or multicast. Send to the one (first) 5983 * client we find. No need to check conn_wantpacket() 5984 * since IP_BOUND_IF/conn_incoming_ill does not apply to 5985 * IPv4 unicast packets. 5986 */ 5987 while ((connp != NULL) && 5988 (!IPCL_UDP_MATCH(connp, dstport, dst, 5989 srcport, src) || connp->conn_zoneid != zoneid)) { 5990 connp = connp->conn_next; 5991 } 5992 5993 if (connp == NULL || connp->conn_upq == NULL) 5994 goto notfound; 5995 CONN_INC_REF(connp); 5996 mutex_exit(&connfp->connf_lock); 5997 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, 5998 recv_ill, ip_policy); 5999 IP_STAT(ip_udp_fannorm); 6000 CONN_DEC_REF(connp); 6001 return; 6002 } 6003 6004 /* 6005 * Broadcast and multicast case 6006 * 6007 * Need to check conn_wantpacket(). 6008 * If SO_REUSEADDR has been set on the first we send the 6009 * packet to all clients that have joined the group and 6010 * match the port. 6011 */ 6012 6013 while (connp != NULL) { 6014 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && 6015 conn_wantpacket(connp, ill, ipha, flags, zoneid)) 6016 break; 6017 connp = connp->conn_next; 6018 } 6019 6020 if (connp == NULL || connp->conn_upq == NULL) 6021 goto notfound; 6022 6023 first_connp = connp; 6024 /* 6025 * When SO_REUSEADDR is not set, send the packet only to the first 6026 * matching connection in its zone by keeping track of the zoneid. 6027 */ 6028 reuseaddr = first_connp->conn_reuseaddr; 6029 last_zoneid = first_connp->conn_zoneid; 6030 6031 CONN_INC_REF(connp); 6032 connp = connp->conn_next; 6033 for (;;) { 6034 while (connp != NULL) { 6035 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && 6036 (reuseaddr || connp->conn_zoneid != last_zoneid) && 6037 conn_wantpacket(connp, ill, ipha, flags, zoneid)) 6038 break; 6039 connp = connp->conn_next; 6040 } 6041 /* 6042 * Just copy the data part alone. The mctl part is 6043 * needed just for verifying policy and it is never 6044 * sent up. 6045 */ 6046 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 6047 ((mp1 = copymsg(mp)) == NULL))) { 6048 /* 6049 * No more interested clients or memory 6050 * allocation failed 6051 */ 6052 connp = first_connp; 6053 break; 6054 } 6055 if (connp->conn_zoneid != last_zoneid) { 6056 /* 6057 * Update the zoneid so that the packet isn't sent to 6058 * any more conns in the same zone unless SO_REUSEADDR 6059 * is set. 6060 */ 6061 reuseaddr = connp->conn_reuseaddr; 6062 last_zoneid = connp->conn_zoneid; 6063 } 6064 if (first_mp != NULL) { 6065 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 6066 ipsec_info_type == IPSEC_IN); 6067 first_mp1 = ipsec_in_tag(first_mp, NULL); 6068 if (first_mp1 == NULL) { 6069 freemsg(mp1); 6070 connp = first_connp; 6071 break; 6072 } 6073 } else { 6074 first_mp1 = NULL; 6075 } 6076 CONN_INC_REF(connp); 6077 mutex_exit(&connfp->connf_lock); 6078 /* 6079 * IPQoS notes: We don't send the packet for policy 6080 * processing here, will do it for the last one (below). 6081 * i.e. we do it per-packet now, but if we do policy 6082 * processing per-conn, then we would need to do it 6083 * here too. 6084 */ 6085 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, 6086 ipha, flags, recv_ill, B_FALSE); 6087 mutex_enter(&connfp->connf_lock); 6088 /* Follow the next pointer before releasing the conn. */ 6089 next_connp = connp->conn_next; 6090 IP_STAT(ip_udp_fanmb); 6091 CONN_DEC_REF(connp); 6092 connp = next_connp; 6093 } 6094 6095 /* Last one. Send it upstream. */ 6096 mutex_exit(&connfp->connf_lock); 6097 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, recv_ill, 6098 ip_policy); 6099 IP_STAT(ip_udp_fanmb); 6100 CONN_DEC_REF(connp); 6101 return; 6102 6103 notfound: 6104 6105 mutex_exit(&connfp->connf_lock); 6106 IP_STAT(ip_udp_fanothers); 6107 /* 6108 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses 6109 * have already been matched above, since they live in the IPv4 6110 * fanout tables. This implies we only need to 6111 * check for IPv6 in6addr_any endpoints here. 6112 * Thus we compare using ipv6_all_zeros instead of the destination 6113 * address, except for the multicast group membership lookup which 6114 * uses the IPv4 destination. 6115 */ 6116 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); 6117 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 6118 mutex_enter(&connfp->connf_lock); 6119 connp = connfp->connf_head; 6120 if (!broadcast && !CLASSD(dst)) { 6121 while (connp != NULL) { 6122 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 6123 srcport, v6src) && connp->conn_zoneid == zoneid && 6124 conn_wantpacket(connp, ill, ipha, flags, zoneid) && 6125 !connp->conn_ipv6_v6only) 6126 break; 6127 connp = connp->conn_next; 6128 } 6129 6130 if (connp == NULL || connp->conn_upq == NULL) { 6131 /* 6132 * No one bound to this port. Is 6133 * there a client that wants all 6134 * unclaimed datagrams? 6135 */ 6136 mutex_exit(&connfp->connf_lock); 6137 6138 if (mctl_present) 6139 first_mp->b_cont = mp; 6140 else 6141 first_mp = mp; 6142 if (ipcl_proto_search(IPPROTO_UDP) != NULL) { 6143 ip_fanout_proto(q, first_mp, ill, ipha, 6144 flags | IP_FF_RAWIP, mctl_present, 6145 ip_policy, recv_ill, zoneid); 6146 } else { 6147 if (ip_fanout_send_icmp(q, first_mp, flags, 6148 ICMP_DEST_UNREACHABLE, 6149 ICMP_PORT_UNREACHABLE, 6150 mctl_present, zoneid)) { 6151 BUMP_MIB(&ip_mib, udpNoPorts); 6152 } 6153 } 6154 return; 6155 } 6156 CONN_INC_REF(connp); 6157 mutex_exit(&connfp->connf_lock); 6158 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, 6159 recv_ill, ip_policy); 6160 CONN_DEC_REF(connp); 6161 return; 6162 } 6163 /* 6164 * IPv4 multicast packet being delivered to an AF_INET6 6165 * in6addr_any endpoint. 6166 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 6167 * and not conn_wantpacket_v6() since any multicast membership is 6168 * for an IPv4-mapped multicast address. 6169 * The packet is sent to all clients in all zones that have joined the 6170 * group and match the port. 6171 */ 6172 while (connp != NULL) { 6173 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, 6174 srcport, v6src) && 6175 conn_wantpacket(connp, ill, ipha, flags, zoneid)) 6176 break; 6177 connp = connp->conn_next; 6178 } 6179 6180 if (connp == NULL || connp->conn_upq == NULL) { 6181 /* 6182 * No one bound to this port. Is 6183 * there a client that wants all 6184 * unclaimed datagrams? 6185 */ 6186 mutex_exit(&connfp->connf_lock); 6187 6188 if (mctl_present) 6189 first_mp->b_cont = mp; 6190 else 6191 first_mp = mp; 6192 if (ipcl_proto_search(IPPROTO_UDP) != NULL) { 6193 ip_fanout_proto(q, first_mp, ill, ipha, 6194 flags | IP_FF_RAWIP, mctl_present, ip_policy, 6195 recv_ill, zoneid); 6196 } else { 6197 /* 6198 * We used to attempt to send an icmp error here, but 6199 * since this is known to be a multicast packet 6200 * and we don't send icmp errors in response to 6201 * multicast, just drop the packet and give up sooner. 6202 */ 6203 BUMP_MIB(&ip_mib, udpNoPorts); 6204 freemsg(first_mp); 6205 } 6206 return; 6207 } 6208 6209 first_connp = connp; 6210 6211 CONN_INC_REF(connp); 6212 connp = connp->conn_next; 6213 for (;;) { 6214 while (connp != NULL) { 6215 if (IPCL_UDP_MATCH_V6(connp, dstport, 6216 ipv6_all_zeros, srcport, v6src) && 6217 conn_wantpacket(connp, ill, ipha, flags, zoneid)) 6218 break; 6219 connp = connp->conn_next; 6220 } 6221 /* 6222 * Just copy the data part alone. The mctl part is 6223 * needed just for verifying policy and it is never 6224 * sent up. 6225 */ 6226 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && 6227 ((mp1 = copymsg(mp)) == NULL))) { 6228 /* 6229 * No more intested clients or memory 6230 * allocation failed 6231 */ 6232 connp = first_connp; 6233 break; 6234 } 6235 if (first_mp != NULL) { 6236 ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> 6237 ipsec_info_type == IPSEC_IN); 6238 first_mp1 = ipsec_in_tag(first_mp, NULL); 6239 if (first_mp1 == NULL) { 6240 freemsg(mp1); 6241 connp = first_connp; 6242 break; 6243 } 6244 } else { 6245 first_mp1 = NULL; 6246 } 6247 CONN_INC_REF(connp); 6248 mutex_exit(&connfp->connf_lock); 6249 /* 6250 * IPQoS notes: We don't send the packet for policy 6251 * processing here, will do it for the last one (below). 6252 * i.e. we do it per-packet now, but if we do policy 6253 * processing per-conn, then we would need to do it 6254 * here too. 6255 */ 6256 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, 6257 ipha, flags, recv_ill, B_FALSE); 6258 mutex_enter(&connfp->connf_lock); 6259 /* Follow the next pointer before releasing the conn. */ 6260 next_connp = connp->conn_next; 6261 CONN_DEC_REF(connp); 6262 connp = next_connp; 6263 } 6264 6265 /* Last one. Send it upstream. */ 6266 mutex_exit(&connfp->connf_lock); 6267 ip_fanout_udp_conn(connp, first_mp, mp, secure, ipha, flags, recv_ill, 6268 ip_policy); 6269 CONN_DEC_REF(connp); 6270 } 6271 6272 /* 6273 * Complete the ip_wput header so that it 6274 * is possible to generate ICMP 6275 * errors. 6276 */ 6277 static int 6278 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid) 6279 { 6280 ire_t *ire; 6281 6282 if (ipha->ipha_src == INADDR_ANY) { 6283 ire = ire_lookup_local(zoneid); 6284 if (ire == NULL) { 6285 ip1dbg(("ip_hdr_complete: no source IRE\n")); 6286 return (1); 6287 } 6288 ipha->ipha_src = ire->ire_addr; 6289 ire_refrele(ire); 6290 } 6291 ipha->ipha_ttl = ip_def_ttl; 6292 ipha->ipha_hdr_checksum = 0; 6293 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 6294 return (0); 6295 } 6296 6297 /* 6298 * Nobody should be sending 6299 * packets up this stream 6300 */ 6301 static void 6302 ip_lrput(queue_t *q, mblk_t *mp) 6303 { 6304 mblk_t *mp1; 6305 6306 switch (mp->b_datap->db_type) { 6307 case M_FLUSH: 6308 /* Turn around */ 6309 if (*mp->b_rptr & FLUSHW) { 6310 *mp->b_rptr &= ~FLUSHR; 6311 qreply(q, mp); 6312 return; 6313 } 6314 break; 6315 } 6316 /* Could receive messages that passed through ar_rput */ 6317 for (mp1 = mp; mp1; mp1 = mp1->b_cont) 6318 mp1->b_prev = mp1->b_next = NULL; 6319 freemsg(mp); 6320 } 6321 6322 /* Nobody should be sending packets down this stream */ 6323 /* ARGSUSED */ 6324 void 6325 ip_lwput(queue_t *q, mblk_t *mp) 6326 { 6327 freemsg(mp); 6328 } 6329 6330 /* 6331 * Move the first hop in any source route to ipha_dst and remove that part of 6332 * the source route. Called by other protocols. Errors in option formatting 6333 * are ignored - will be handled by ip_wput_options Return the final 6334 * destination (either ipha_dst or the last entry in a source route.) 6335 */ 6336 ipaddr_t 6337 ip_massage_options(ipha_t *ipha) 6338 { 6339 ipoptp_t opts; 6340 uchar_t *opt; 6341 uint8_t optval; 6342 uint8_t optlen; 6343 ipaddr_t dst; 6344 int i; 6345 ire_t *ire; 6346 6347 ip2dbg(("ip_massage_options\n")); 6348 dst = ipha->ipha_dst; 6349 for (optval = ipoptp_first(&opts, ipha); 6350 optval != IPOPT_EOL; 6351 optval = ipoptp_next(&opts)) { 6352 opt = opts.ipoptp_cur; 6353 switch (optval) { 6354 uint8_t off; 6355 case IPOPT_SSRR: 6356 case IPOPT_LSRR: 6357 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 6358 ip1dbg(("ip_massage_options: bad src route\n")); 6359 break; 6360 } 6361 optlen = opts.ipoptp_len; 6362 off = opt[IPOPT_OFFSET]; 6363 off--; 6364 redo_srr: 6365 if (optlen < IP_ADDR_LEN || 6366 off > optlen - IP_ADDR_LEN) { 6367 /* End of source route */ 6368 ip1dbg(("ip_massage_options: end of SR\n")); 6369 break; 6370 } 6371 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 6372 ip1dbg(("ip_massage_options: next hop 0x%x\n", 6373 ntohl(dst))); 6374 /* 6375 * Check if our address is present more than 6376 * once as consecutive hops in source route. 6377 * XXX verify per-interface ip_forwarding 6378 * for source route? 6379 */ 6380 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 6381 ALL_ZONES, MATCH_IRE_TYPE); 6382 if (ire != NULL) { 6383 ire_refrele(ire); 6384 off += IP_ADDR_LEN; 6385 goto redo_srr; 6386 } 6387 if (dst == htonl(INADDR_LOOPBACK)) { 6388 ip1dbg(("ip_massage_options: loopback addr in " 6389 "source route!\n")); 6390 break; 6391 } 6392 /* 6393 * Update ipha_dst to be the first hop and remove the 6394 * first hop from the source route (by overwriting 6395 * part of the option with NOP options). 6396 */ 6397 ipha->ipha_dst = dst; 6398 /* Put the last entry in dst */ 6399 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 6400 3; 6401 bcopy(&opt[off], &dst, IP_ADDR_LEN); 6402 6403 ip1dbg(("ip_massage_options: last hop 0x%x\n", 6404 ntohl(dst))); 6405 /* Move down and overwrite */ 6406 opt[IP_ADDR_LEN] = opt[0]; 6407 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 6408 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 6409 for (i = 0; i < IP_ADDR_LEN; i++) 6410 opt[i] = IPOPT_NOP; 6411 break; 6412 } 6413 } 6414 return (dst); 6415 } 6416 6417 /* 6418 * This function's job is to forward data to the reverse tunnel (FA->HA) 6419 * after doing a few checks. It is assumed that the incoming interface 6420 * of the packet is always different than the outgoing interface and the 6421 * ire_type of the found ire has to be a non-resolver type. 6422 * 6423 * IPQoS notes 6424 * IP policy is invoked twice for a forwarded packet, once on the read side 6425 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 6426 * enabled. 6427 */ 6428 static void 6429 ip_mrtun_forward(ire_t *ire, ill_t *in_ill, mblk_t *mp) 6430 { 6431 ipha_t *ipha; 6432 queue_t *q; 6433 uint32_t pkt_len; 6434 #define rptr ((uchar_t *)ipha) 6435 uint32_t sum; 6436 uint32_t max_frag; 6437 mblk_t *first_mp; 6438 uint32_t ill_index; 6439 6440 ASSERT(ire != NULL); 6441 ASSERT(ire->ire_ipif->ipif_net_type == IRE_IF_NORESOLVER); 6442 ASSERT(ire->ire_stq != NULL); 6443 6444 /* Initiate read side IPPF processing */ 6445 if (IPP_ENABLED(IPP_FWD_IN)) { 6446 ill_index = in_ill->ill_phyint->phyint_ifindex; 6447 ip_process(IPP_FWD_IN, &mp, ill_index); 6448 if (mp == NULL) { 6449 ip2dbg(("ip_mrtun_forward: inbound pkt " 6450 "dropped during IPPF processing\n")); 6451 return; 6452 } 6453 } 6454 6455 if (((in_ill->ill_flags & ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 6456 ILLF_ROUTER) == 0) || 6457 (in_ill == (ill_t *)ire->ire_stq->q_ptr)) { 6458 BUMP_MIB(&ip_mib, ipForwProhibits); 6459 ip0dbg(("ip_mrtun_forward: Can't forward :" 6460 "forwarding is not turned on\n")); 6461 goto drop_pkt; 6462 } 6463 6464 /* 6465 * Don't forward if the interface is down 6466 */ 6467 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 6468 BUMP_MIB(&ip_mib, ipInDiscards); 6469 goto drop_pkt; 6470 } 6471 6472 ipha = (ipha_t *)mp->b_rptr; 6473 pkt_len = ntohs(ipha->ipha_length); 6474 /* Adjust the checksum to reflect the ttl decrement. */ 6475 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 6476 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 6477 if (ipha->ipha_ttl-- <= 1) { 6478 if (ip_csum_hdr(ipha)) { 6479 BUMP_MIB(&ip_mib, ipInCksumErrs); 6480 goto drop_pkt; 6481 } 6482 q = ire->ire_stq; 6483 if ((first_mp = allocb(sizeof (ipsec_info_t), 6484 BPRI_HI)) == NULL) { 6485 goto drop_pkt; 6486 } 6487 ip_ipsec_out_prepend(first_mp, mp, in_ill); 6488 icmp_time_exceeded(q, first_mp, ICMP_TTL_EXCEEDED); 6489 6490 return; 6491 } 6492 6493 /* Get the ill_index of the ILL */ 6494 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 6495 6496 /* 6497 * ip_mrtun_forward is only used by foreign agent to reverse 6498 * tunnel the incoming packet. So it does not do any option 6499 * processing for source routing. 6500 */ 6501 max_frag = ire->ire_max_frag; 6502 if (pkt_len > max_frag) { 6503 /* 6504 * It needs fragging on its way out. We haven't 6505 * verified the header checksum yet. Since we 6506 * are going to put a surely good checksum in the 6507 * outgoing header, we have to make sure that it 6508 * was good coming in. 6509 */ 6510 if (ip_csum_hdr(ipha)) { 6511 BUMP_MIB(&ip_mib, ipInCksumErrs); 6512 goto drop_pkt; 6513 } 6514 6515 /* Initiate write side IPPF processing */ 6516 if (IPP_ENABLED(IPP_FWD_OUT)) { 6517 ip_process(IPP_FWD_OUT, &mp, ill_index); 6518 if (mp == NULL) { 6519 ip2dbg(("ip_mrtun_forward: outbound pkt "\ 6520 "dropped/deferred during ip policy "\ 6521 "processing\n")); 6522 return; 6523 } 6524 } 6525 if ((first_mp = allocb(sizeof (ipsec_info_t), 6526 BPRI_HI)) == NULL) { 6527 goto drop_pkt; 6528 } 6529 ip_ipsec_out_prepend(first_mp, mp, in_ill); 6530 mp = first_mp; 6531 6532 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0); 6533 return; 6534 } 6535 6536 ip2dbg(("ip_mrtun_forward: ire type (%d)\n", ire->ire_type)); 6537 6538 ASSERT(ire->ire_ipif != NULL); 6539 6540 mp = ip_wput_attach_llhdr(mp, ire, IPP_FWD_OUT, ill_index); 6541 if (mp == NULL) { 6542 BUMP_MIB(&ip_mib, ipInDiscards); 6543 return; 6544 } 6545 6546 /* Now send the packet to the tunnel interface */ 6547 q = ire->ire_stq; 6548 UPDATE_IB_PKT_COUNT(ire); 6549 ire->ire_last_used_time = lbolt; 6550 BUMP_MIB(&ip_mib, ipForwDatagrams); 6551 putnext(q, mp); 6552 ip2dbg(("ip_mrtun_forward: sent packet to ill %p\n", q->q_ptr)); 6553 return; 6554 6555 drop_pkt:; 6556 ip2dbg(("ip_mrtun_forward: dropping pkt\n")); 6557 freemsg(mp); 6558 #undef rptr 6559 } 6560 6561 /* 6562 * Fills the ipsec_out_t data structure with appropriate fields and 6563 * prepends it to mp which contains the IP hdr + data that was meant 6564 * to be forwarded. Please note that ipsec_out_info data structure 6565 * is used here to communicate the outgoing ill path at ip_wput() 6566 * for the ICMP error packet. This has nothing to do with ipsec IP 6567 * security. ipsec_out_t is really used to pass the info to the module 6568 * IP where this information cannot be extracted from conn. 6569 * This functions is called by ip_mrtun_forward(). 6570 */ 6571 void 6572 ip_ipsec_out_prepend(mblk_t *first_mp, mblk_t *mp, ill_t *xmit_ill) 6573 { 6574 ipsec_out_t *io; 6575 6576 ASSERT(xmit_ill != NULL); 6577 first_mp->b_datap->db_type = M_CTL; 6578 first_mp->b_wptr += sizeof (ipsec_info_t); 6579 /* 6580 * This is to pass info to ip_wput in absence of conn. 6581 * ipsec_out_secure will be B_FALSE because of this. 6582 * Thus ipsec_out_secure being B_FALSE indicates that 6583 * this is not IPSEC security related information. 6584 */ 6585 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 6586 io = (ipsec_out_t *)first_mp->b_rptr; 6587 io->ipsec_out_type = IPSEC_OUT; 6588 io->ipsec_out_len = sizeof (ipsec_out_t); 6589 first_mp->b_cont = mp; 6590 io->ipsec_out_ill_index = 6591 xmit_ill->ill_phyint->phyint_ifindex; 6592 io->ipsec_out_xmit_if = B_TRUE; 6593 } 6594 6595 /* 6596 * Return the network mask 6597 * associated with the specified address. 6598 */ 6599 ipaddr_t 6600 ip_net_mask(ipaddr_t addr) 6601 { 6602 uchar_t *up = (uchar_t *)&addr; 6603 ipaddr_t mask = 0; 6604 uchar_t *maskp = (uchar_t *)&mask; 6605 6606 #if defined(__i386) || defined(__amd64) 6607 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 6608 #endif 6609 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 6610 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 6611 #endif 6612 if (CLASSD(addr)) { 6613 maskp[0] = 0xF0; 6614 return (mask); 6615 } 6616 if (addr == 0) 6617 return (0); 6618 maskp[0] = 0xFF; 6619 if ((up[0] & 0x80) == 0) 6620 return (mask); 6621 6622 maskp[1] = 0xFF; 6623 if ((up[0] & 0xC0) == 0x80) 6624 return (mask); 6625 6626 maskp[2] = 0xFF; 6627 if ((up[0] & 0xE0) == 0xC0) 6628 return (mask); 6629 6630 /* Must be experimental or multicast, indicate as much */ 6631 return ((ipaddr_t)0); 6632 } 6633 6634 /* 6635 * Select an ill for the packet by considering load spreading across 6636 * a different ill in the group if dst_ill is part of some group. 6637 */ 6638 static ill_t * 6639 ip_newroute_get_dst_ill(ill_t *dst_ill) 6640 { 6641 ill_t *ill; 6642 6643 /* 6644 * We schedule irrespective of whether the source address is 6645 * INADDR_ANY or not. illgrp_scheduler returns a held ill. 6646 */ 6647 ill = illgrp_scheduler(dst_ill); 6648 if (ill == NULL) 6649 return (NULL); 6650 6651 /* 6652 * For groups with names ip_sioctl_groupname ensures that all 6653 * ills are of same type. For groups without names, ifgrp_insert 6654 * ensures this. 6655 */ 6656 ASSERT(dst_ill->ill_type == ill->ill_type); 6657 6658 return (ill); 6659 } 6660 6661 /* 6662 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. 6663 */ 6664 ill_t * 6665 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6) 6666 { 6667 ill_t *ret_ill; 6668 6669 ASSERT(ifindex != 0); 6670 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL); 6671 if (ret_ill == NULL || 6672 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { 6673 if (isv6) { 6674 if (ill != NULL) { 6675 BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards); 6676 } else { 6677 BUMP_MIB(&ip6_mib, ipv6OutDiscards); 6678 } 6679 ip1dbg(("ip_grab_attach_ill (IPv6): " 6680 "bad ifindex %d.\n", ifindex)); 6681 } else { 6682 BUMP_MIB(&ip_mib, ipOutDiscards); 6683 ip1dbg(("ip_grab_attach_ill (IPv4): " 6684 "bad ifindex %d.\n", ifindex)); 6685 } 6686 if (ret_ill != NULL) 6687 ill_refrele(ret_ill); 6688 freemsg(first_mp); 6689 return (NULL); 6690 } 6691 6692 return (ret_ill); 6693 } 6694 6695 /* 6696 * IPv4 - 6697 * ip_newroute is called by ip_rput or ip_wput whenever we need to send 6698 * out a packet to a destination address for which we do not have specific 6699 * (or sufficient) routing information. 6700 * 6701 * NOTE : These are the scopes of some of the variables that point at IRE, 6702 * which needs to be followed while making any future modifications 6703 * to avoid memory leaks. 6704 * 6705 * - ire and sire are the entries looked up initially by 6706 * ire_ftable_lookup. 6707 * - ipif_ire is used to hold the interface ire associated with 6708 * the new cache ire. But it's scope is limited, so we always REFRELE 6709 * it before branching out to error paths. 6710 * - save_ire is initialized before ire_create, so that ire returned 6711 * by ire_create will not over-write the ire. We REFRELE save_ire 6712 * before breaking out of the switch. 6713 * 6714 * Thus on failures, we have to REFRELE only ire and sire, if they 6715 * are not NULL. 6716 */ 6717 void 6718 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, ill_t *in_ill, conn_t *connp) 6719 { 6720 areq_t *areq; 6721 ipaddr_t gw = 0; 6722 ire_t *ire = NULL; 6723 mblk_t *res_mp; 6724 ipaddr_t *addrp; 6725 ipaddr_t nexthop_addr; 6726 ipif_t *src_ipif = NULL; 6727 ill_t *dst_ill = NULL; 6728 ipha_t *ipha; 6729 ire_t *sire = NULL; 6730 mblk_t *first_mp; 6731 ire_t *save_ire; 6732 mblk_t *dlureq_mp; 6733 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ 6734 ushort_t ire_marks = 0; 6735 boolean_t mctl_present; 6736 ipsec_out_t *io; 6737 mblk_t *saved_mp; 6738 ire_t *first_sire = NULL; 6739 mblk_t *copy_mp = NULL; 6740 mblk_t *xmit_mp = NULL; 6741 ipaddr_t save_dst; 6742 uint32_t multirt_flags = 6743 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 6744 boolean_t multirt_is_resolvable; 6745 boolean_t multirt_resolve_next; 6746 boolean_t do_attach_ill = B_FALSE; 6747 boolean_t ip_nexthop = B_FALSE; 6748 zoneid_t zoneid; 6749 6750 if (ip_debug > 2) { 6751 /* ip1dbg */ 6752 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); 6753 } 6754 6755 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 6756 if (mctl_present) { 6757 io = (ipsec_out_t *)first_mp->b_rptr; 6758 zoneid = io->ipsec_out_zoneid; 6759 ASSERT(zoneid != ALL_ZONES); 6760 } else if (connp != NULL) { 6761 zoneid = connp->conn_zoneid; 6762 } else { 6763 zoneid = GLOBAL_ZONEID; 6764 } 6765 6766 ipha = (ipha_t *)mp->b_rptr; 6767 6768 /* All multicast lookups come through ip_newroute_ipif() */ 6769 if (CLASSD(dst)) { 6770 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", 6771 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); 6772 freemsg(first_mp); 6773 return; 6774 } 6775 6776 if (ip_loopback_src_or_dst(ipha, NULL)) { 6777 goto icmp_err_ret; 6778 } 6779 6780 if (mctl_present && io->ipsec_out_attach_if) { 6781 /* ip_grab_attach_ill returns a held ill */ 6782 attach_ill = ip_grab_attach_ill(NULL, first_mp, 6783 io->ipsec_out_ill_index, B_FALSE); 6784 6785 /* Failure case frees things for us. */ 6786 if (attach_ill == NULL) 6787 return; 6788 6789 /* 6790 * Check if we need an ire that will not be 6791 * looked up by anybody else i.e. HIDDEN. 6792 */ 6793 if (ill_is_probeonly(attach_ill)) 6794 ire_marks = IRE_MARK_HIDDEN; 6795 } 6796 if (mctl_present && io->ipsec_out_ip_nexthop) { 6797 ip_nexthop = B_TRUE; 6798 nexthop_addr = io->ipsec_out_nexthop_addr; 6799 } 6800 /* 6801 * If this IRE is created for forwarding or it is not for 6802 * traffic for congestion controlled protocols, mark it as temporary. 6803 */ 6804 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) 6805 ire_marks |= IRE_MARK_TEMPORARY; 6806 6807 /* 6808 * Get what we can from ire_ftable_lookup which will follow an IRE 6809 * chain until it gets the most specific information available. 6810 * For example, we know that there is no IRE_CACHE for this dest, 6811 * but there may be an IRE_OFFSUBNET which specifies a gateway. 6812 * ire_ftable_lookup will look up the gateway, etc. 6813 * Check if in_ill != NULL. If it is true, the packet must be 6814 * from an incoming interface where RTA_SRCIFP is set. 6815 * Otherwise, given ire_ftable_lookup algorithm, only one among routes 6816 * to the destination, of equal netmask length in the forward table, 6817 * will be recursively explored. If no information is available 6818 * for the final gateway of that route, we force the returned ire 6819 * to be equal to sire using MATCH_IRE_PARENT. 6820 * At least, in this case we have a starting point (in the buckets) 6821 * to look for other routes to the destination in the forward table. 6822 * This is actually used only for multirouting, where a list 6823 * of routes has to be processed in sequence. 6824 */ 6825 if (in_ill != NULL) { 6826 ire = ire_srcif_table_lookup(dst, IRE_IF_RESOLVER, NULL, 6827 in_ill, MATCH_IRE_TYPE); 6828 } else if (ip_nexthop) { 6829 /* 6830 * The first time we come here, we look for an IRE_INTERFACE 6831 * entry for the specified nexthop, set the dst to be the 6832 * nexthop address and create an IRE_CACHE entry for the 6833 * nexthop. The next time around, we are able to find an 6834 * IRE_CACHE entry for the nexthop, set the gateway to be the 6835 * nexthop address and create an IRE_CACHE entry for the 6836 * destination address via the specified nexthop. 6837 */ 6838 ire = ire_cache_lookup(nexthop_addr, zoneid); 6839 if (ire != NULL) { 6840 gw = nexthop_addr; 6841 ire_marks |= IRE_MARK_PRIVATE_ADDR; 6842 } else { 6843 ire = ire_ftable_lookup(nexthop_addr, 0, 0, 6844 IRE_INTERFACE, NULL, NULL, zoneid, 0, 6845 MATCH_IRE_TYPE); 6846 if (ire != NULL) { 6847 dst = nexthop_addr; 6848 } 6849 } 6850 } else if (attach_ill == NULL) { 6851 ire = ire_ftable_lookup(dst, 0, 0, 0, 6852 NULL, &sire, zoneid, 0, 6853 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 6854 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT); 6855 } else { 6856 /* 6857 * attach_ill is set only for communicating with 6858 * on-link hosts. So, don't look for DEFAULT. 6859 */ 6860 ipif_t *attach_ipif; 6861 6862 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 6863 if (attach_ipif == NULL) { 6864 ill_refrele(attach_ill); 6865 goto icmp_err_ret; 6866 } 6867 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, 6868 &sire, zoneid, 0, 6869 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL); 6870 ipif_refrele(attach_ipif); 6871 } 6872 ip3dbg(("ip_newroute: ire_ftable_lookup() " 6873 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 6874 6875 /* 6876 * This loop is run only once in most cases. 6877 * We loop to resolve further routes only when the destination 6878 * can be reached through multiple RTF_MULTIRT-flagged ires. 6879 */ 6880 do { 6881 /* Clear the previous iteration's values */ 6882 if (src_ipif != NULL) { 6883 ipif_refrele(src_ipif); 6884 src_ipif = NULL; 6885 } 6886 if (dst_ill != NULL) { 6887 ill_refrele(dst_ill); 6888 dst_ill = NULL; 6889 } 6890 6891 multirt_resolve_next = B_FALSE; 6892 /* 6893 * We check if packets have to be multirouted. 6894 * In this case, given the current <ire, sire> couple, 6895 * we look for the next suitable <ire, sire>. 6896 * This check is done in ire_multirt_lookup(), 6897 * which applies various criteria to find the next route 6898 * to resolve. ire_multirt_lookup() leaves <ire, sire> 6899 * unchanged if it detects it has not been tried yet. 6900 */ 6901 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 6902 ip3dbg(("ip_newroute: starting next_resolution " 6903 "with first_mp %p, tag %d\n", 6904 (void *)first_mp, 6905 MULTIRT_DEBUG_TAGGED(first_mp))); 6906 6907 ASSERT(sire != NULL); 6908 multirt_is_resolvable = 6909 ire_multirt_lookup(&ire, &sire, multirt_flags); 6910 6911 ip3dbg(("ip_newroute: multirt_is_resolvable %d, " 6912 "ire %p, sire %p\n", 6913 multirt_is_resolvable, 6914 (void *)ire, (void *)sire)); 6915 6916 if (!multirt_is_resolvable) { 6917 /* 6918 * No more multirt route to resolve; give up 6919 * (all routes resolved or no more 6920 * resolvable routes). 6921 */ 6922 if (ire != NULL) { 6923 ire_refrele(ire); 6924 ire = NULL; 6925 } 6926 } else { 6927 ASSERT(sire != NULL); 6928 ASSERT(ire != NULL); 6929 /* 6930 * We simply use first_sire as a flag that 6931 * indicates if a resolvable multirt route 6932 * has already been found. 6933 * If it is not the case, we may have to send 6934 * an ICMP error to report that the 6935 * destination is unreachable. 6936 * We do not IRE_REFHOLD first_sire. 6937 */ 6938 if (first_sire == NULL) { 6939 first_sire = sire; 6940 } 6941 } 6942 } 6943 if (ire == NULL) { 6944 if (ip_debug > 3) { 6945 /* ip2dbg */ 6946 pr_addr_dbg("ip_newroute: " 6947 "can't resolve %s\n", AF_INET, &dst); 6948 } 6949 ip3dbg(("ip_newroute: " 6950 "ire %p, sire %p, first_sire %p\n", 6951 (void *)ire, (void *)sire, (void *)first_sire)); 6952 6953 if (sire != NULL) { 6954 ire_refrele(sire); 6955 sire = NULL; 6956 } 6957 6958 if (first_sire != NULL) { 6959 /* 6960 * At least one multirt route has been found 6961 * in the same call to ip_newroute(); 6962 * there is no need to report an ICMP error. 6963 * first_sire was not IRE_REFHOLDed. 6964 */ 6965 MULTIRT_DEBUG_UNTAG(first_mp); 6966 freemsg(first_mp); 6967 return; 6968 } 6969 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, 6970 RTA_DST); 6971 if (attach_ill != NULL) 6972 ill_refrele(attach_ill); 6973 goto icmp_err_ret; 6974 } 6975 6976 /* 6977 * When RTA_SRCIFP is used to add a route, then an interface 6978 * route is added in the source interface's routing table. 6979 * If the outgoing interface of this route is of type 6980 * IRE_IF_RESOLVER, then upon creation of the ire, 6981 * ire_dlureq_mp is set to NULL. Later, when this route is 6982 * first used for forwarding packet, ip_newroute() is called 6983 * to resolve the hardware address of the outgoing ipif. 6984 * We do not come here for IRE_IF_NORESOLVER entries in the 6985 * source interface based table. We only come here if the 6986 * outgoing interface is a resolver interface and we don't 6987 * have the ire_dlureq_mp information yet. 6988 * If in_ill is not null that means it is called from 6989 * ip_rput. 6990 */ 6991 6992 ASSERT(ire->ire_in_ill == NULL || 6993 (ire->ire_type == IRE_IF_RESOLVER && 6994 ire->ire_dlureq_mp == NULL)); 6995 6996 /* 6997 * Verify that the returned IRE does not have either 6998 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 6999 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 7000 */ 7001 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 7002 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { 7003 if (attach_ill != NULL) 7004 ill_refrele(attach_ill); 7005 goto icmp_err_ret; 7006 } 7007 /* 7008 * Increment the ire_ob_pkt_count field for ire if it is an 7009 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 7010 * increment the same for the parent IRE, sire, if it is some 7011 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST 7012 * and HOST_REDIRECT). 7013 */ 7014 if ((ire->ire_type & IRE_INTERFACE) != 0) { 7015 UPDATE_OB_PKT_COUNT(ire); 7016 ire->ire_last_used_time = lbolt; 7017 } 7018 7019 if (sire != NULL) { 7020 gw = sire->ire_gateway_addr; 7021 ASSERT((sire->ire_type & (IRE_CACHETABLE | 7022 IRE_INTERFACE)) == 0); 7023 UPDATE_OB_PKT_COUNT(sire); 7024 sire->ire_last_used_time = lbolt; 7025 } 7026 /* 7027 * We have a route to reach the destination. 7028 * 7029 * 1) If the interface is part of ill group, try to get a new 7030 * ill taking load spreading into account. 7031 * 7032 * 2) After selecting the ill, get a source address that 7033 * might create good inbound load spreading. 7034 * ipif_select_source does this for us. 7035 * 7036 * If the application specified the ill (ifindex), we still 7037 * load spread. Only if the packets needs to go out 7038 * specifically on a given ill e.g. binding to 7039 * IPIF_NOFAILOVER address, then we don't try to use a 7040 * different ill for load spreading. 7041 */ 7042 if (attach_ill == NULL) { 7043 /* 7044 * Don't perform outbound load spreading in the 7045 * case of an RTF_MULTIRT route, as we actually 7046 * typically want to replicate outgoing packets 7047 * through particular interfaces. 7048 */ 7049 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 7050 dst_ill = ire->ire_ipif->ipif_ill; 7051 /* for uniformity */ 7052 ill_refhold(dst_ill); 7053 } else { 7054 /* 7055 * If we are here trying to create an IRE_CACHE 7056 * for an offlink destination and have the 7057 * IRE_CACHE for the next hop and the latter is 7058 * using virtual IP source address selection i.e 7059 * it's ire->ire_ipif is pointing to a virtual 7060 * network interface (vni) then 7061 * ip_newroute_get_dst_ll() will return the vni 7062 * interface as the dst_ill. Since the vni is 7063 * virtual i.e not associated with any physical 7064 * interface, it cannot be the dst_ill, hence 7065 * in such a case call ip_newroute_get_dst_ll() 7066 * with the stq_ill instead of the ire_ipif ILL. 7067 * The function returns a refheld ill. 7068 */ 7069 if ((ire->ire_type == IRE_CACHE) && 7070 IS_VNI(ire->ire_ipif->ipif_ill)) 7071 dst_ill = ip_newroute_get_dst_ill( 7072 ire->ire_stq->q_ptr); 7073 else 7074 dst_ill = ip_newroute_get_dst_ill( 7075 ire->ire_ipif->ipif_ill); 7076 } 7077 if (dst_ill == NULL) { 7078 if (ip_debug > 2) { 7079 pr_addr_dbg("ip_newroute: " 7080 "no dst ill for dst" 7081 " %s\n", AF_INET, &dst); 7082 } 7083 goto icmp_err_ret; 7084 } 7085 } else { 7086 dst_ill = ire->ire_ipif->ipif_ill; 7087 /* for uniformity */ 7088 ill_refhold(dst_ill); 7089 /* 7090 * We should have found a route matching ill as we 7091 * called ire_ftable_lookup with MATCH_IRE_ILL. 7092 * Rather than asserting, when there is a mismatch, 7093 * we just drop the packet. 7094 */ 7095 if (dst_ill != attach_ill) { 7096 ip0dbg(("ip_newroute: Packet dropped as " 7097 "IPIF_NOFAILOVER ill is %s, " 7098 "ire->ire_ipif->ipif_ill is %s\n", 7099 attach_ill->ill_name, 7100 dst_ill->ill_name)); 7101 ill_refrele(attach_ill); 7102 goto icmp_err_ret; 7103 } 7104 } 7105 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ 7106 if (attach_ill != NULL) { 7107 ill_refrele(attach_ill); 7108 attach_ill = NULL; 7109 do_attach_ill = B_TRUE; 7110 } 7111 ASSERT(dst_ill != NULL); 7112 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); 7113 7114 /* 7115 * Pick the best source address from dst_ill. 7116 * 7117 * 1) If it is part of a multipathing group, we would 7118 * like to spread the inbound packets across different 7119 * interfaces. ipif_select_source picks a random source 7120 * across the different ills in the group. 7121 * 7122 * 2) If it is not part of a multipathing group, we try 7123 * to pick the source address from the destination 7124 * route. Clustering assumes that when we have multiple 7125 * prefixes hosted on an interface, the prefix of the 7126 * source address matches the prefix of the destination 7127 * route. We do this only if the address is not 7128 * DEPRECATED. 7129 * 7130 * 3) If the conn is in a different zone than the ire, we 7131 * need to pick a source address from the right zone. 7132 * 7133 * NOTE : If we hit case (1) above, the prefix of the source 7134 * address picked may not match the prefix of the 7135 * destination routes prefix as ipif_select_source 7136 * does not look at "dst" while picking a source 7137 * address. 7138 * If we want the same behavior as (2), we will need 7139 * to change the behavior of ipif_select_source. 7140 */ 7141 ASSERT(src_ipif == NULL); 7142 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 7143 /* 7144 * The RTF_SETSRC flag is set in the parent ire (sire). 7145 * Check that the ipif matching the requested source 7146 * address still exists. 7147 */ 7148 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, 7149 zoneid, NULL, NULL, NULL, NULL); 7150 } 7151 if (src_ipif == NULL) { 7152 ire_marks |= IRE_MARK_USESRC_CHECK; 7153 if ((dst_ill->ill_group != NULL) || 7154 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || 7155 (connp != NULL && ire->ire_zoneid != zoneid) || 7156 (dst_ill->ill_usesrc_ifindex != 0)) { 7157 /* 7158 * If the destination is reachable via a 7159 * given gateway, the selected source address 7160 * should be in the same subnet as the gateway. 7161 * Otherwise, the destination is not reachable. 7162 * 7163 * If there are no interfaces on the same subnet 7164 * as the destination, ipif_select_source gives 7165 * first non-deprecated interface which might be 7166 * on a different subnet than the gateway. 7167 * This is not desirable. Hence pass the dst_ire 7168 * source address to ipif_select_source. 7169 * It is sure that the destination is reachable 7170 * with the dst_ire source address subnet. 7171 * So passing dst_ire source address to 7172 * ipif_select_source will make sure that the 7173 * selected source will be on the same subnet 7174 * as dst_ire source address. 7175 */ 7176 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; 7177 src_ipif = ipif_select_source(dst_ill, saddr, 7178 zoneid); 7179 if (src_ipif == NULL) { 7180 if (ip_debug > 2) { 7181 pr_addr_dbg("ip_newroute: " 7182 "no src for dst %s ", 7183 AF_INET, &dst); 7184 printf("through interface %s\n", 7185 dst_ill->ill_name); 7186 } 7187 goto icmp_err_ret; 7188 } 7189 } else { 7190 src_ipif = ire->ire_ipif; 7191 ASSERT(src_ipif != NULL); 7192 /* hold src_ipif for uniformity */ 7193 ipif_refhold(src_ipif); 7194 } 7195 } 7196 7197 /* 7198 * Assign a source address while we have the conn. 7199 * We can't have ip_wput_ire pick a source address when the 7200 * packet returns from arp since we need to look at 7201 * conn_unspec_src and conn_zoneid, and we lose the conn when 7202 * going through arp. 7203 * 7204 * NOTE : ip_newroute_v6 does not have this piece of code as 7205 * it uses ip6i to store this information. 7206 */ 7207 if (ipha->ipha_src == INADDR_ANY && 7208 (connp == NULL || !connp->conn_unspec_src)) { 7209 ipha->ipha_src = src_ipif->ipif_src_addr; 7210 } 7211 if (ip_debug > 3) { 7212 /* ip2dbg */ 7213 pr_addr_dbg("ip_newroute: first hop %s\n", 7214 AF_INET, &gw); 7215 } 7216 ip2dbg(("\tire type %s (%d)\n", 7217 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 7218 7219 /* 7220 * The TTL of multirouted packets is bounded by the 7221 * ip_multirt_ttl ndd variable. 7222 */ 7223 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 7224 /* Force TTL of multirouted packets */ 7225 if ((ip_multirt_ttl > 0) && 7226 (ipha->ipha_ttl > ip_multirt_ttl)) { 7227 ip2dbg(("ip_newroute: forcing multirt TTL " 7228 "to %d (was %d), dst 0x%08x\n", 7229 ip_multirt_ttl, ipha->ipha_ttl, 7230 ntohl(sire->ire_addr))); 7231 ipha->ipha_ttl = ip_multirt_ttl; 7232 } 7233 } 7234 /* 7235 * At this point in ip_newroute(), ire is either the 7236 * IRE_CACHE of the next-hop gateway for an off-subnet 7237 * destination or an IRE_INTERFACE type that should be used 7238 * to resolve an on-subnet destination or an on-subnet 7239 * next-hop gateway. 7240 * 7241 * In the IRE_CACHE case, we have the following : 7242 * 7243 * 1) src_ipif - used for getting a source address. 7244 * 7245 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 7246 * means packets using this IRE_CACHE will go out on 7247 * dst_ill. 7248 * 7249 * 3) The IRE sire will point to the prefix that is the 7250 * longest matching route for the destination. These 7251 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST, 7252 * and IRE_HOST_REDIRECT. 7253 * 7254 * The newly created IRE_CACHE entry for the off-subnet 7255 * destination is tied to both the prefix route and the 7256 * interface route used to resolve the next-hop gateway 7257 * via the ire_phandle and ire_ihandle fields, 7258 * respectively. 7259 * 7260 * In the IRE_INTERFACE case, we have the following : 7261 * 7262 * 1) src_ipif - used for getting a source address. 7263 * 7264 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 7265 * means packets using the IRE_CACHE that we will build 7266 * here will go out on dst_ill. 7267 * 7268 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 7269 * to be created will only be tied to the IRE_INTERFACE 7270 * that was derived from the ire_ihandle field. 7271 * 7272 * If sire is non-NULL, it means the destination is 7273 * off-link and we will first create the IRE_CACHE for the 7274 * gateway. Next time through ip_newroute, we will create 7275 * the IRE_CACHE for the final destination as described 7276 * above. 7277 * 7278 * In both cases, after the current resolution has been 7279 * completed (or possibly initialised, in the IRE_INTERFACE 7280 * case), the loop may be re-entered to attempt the resolution 7281 * of another RTF_MULTIRT route. 7282 * 7283 * When an IRE_CACHE entry for the off-subnet destination is 7284 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, 7285 * for further processing in emission loops. 7286 */ 7287 save_ire = ire; 7288 switch (ire->ire_type) { 7289 case IRE_CACHE: { 7290 ire_t *ipif_ire; 7291 mblk_t *ire_fp_mp; 7292 7293 if (gw == 0) 7294 gw = ire->ire_gateway_addr; 7295 /* 7296 * We need 3 ire's to create a new cache ire for an 7297 * off-link destination from the cache ire of the 7298 * gateway. 7299 * 7300 * 1. The prefix ire 'sire' (Note that this does 7301 * not apply to the conn_nexthop_set case) 7302 * 2. The cache ire of the gateway 'ire' 7303 * 3. The interface ire 'ipif_ire' 7304 * 7305 * We have (1) and (2). We lookup (3) below. 7306 * 7307 * If there is no interface route to the gateway, 7308 * it is a race condition, where we found the cache 7309 * but the interface route has been deleted. 7310 */ 7311 if (ip_nexthop) { 7312 ipif_ire = ire_ihandle_lookup_onlink(ire); 7313 } else { 7314 ipif_ire = 7315 ire_ihandle_lookup_offlink(ire, sire); 7316 } 7317 if (ipif_ire == NULL) { 7318 ip1dbg(("ip_newroute: " 7319 "ire_ihandle_lookup_offlink failed\n")); 7320 goto icmp_err_ret; 7321 } 7322 /* 7323 * XXX We are using the same dlureq_mp 7324 * (DL_UNITDATA_REQ) though the save_ire is not 7325 * pointing at the same ill. 7326 * This is incorrect. We need to send it up to the 7327 * resolver to get the right dlureq_mp. For ethernets 7328 * this may be okay (ill_type == DL_ETHER). 7329 */ 7330 dlureq_mp = save_ire->ire_dlureq_mp; 7331 ire_fp_mp = NULL; 7332 /* 7333 * save_ire's ire_fp_mp can't change since it is 7334 * not an IRE_MIPRTUN or IRE_BROADCAST 7335 * LOCK_IRE_FP_MP does not do any useful work in 7336 * the case of IRE_CACHE. So we don't use it below. 7337 */ 7338 if (save_ire->ire_stq == dst_ill->ill_wq) 7339 ire_fp_mp = save_ire->ire_fp_mp; 7340 7341 ire = ire_create( 7342 (uchar_t *)&dst, /* dest address */ 7343 (uchar_t *)&ip_g_all_ones, /* mask */ 7344 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 7345 (uchar_t *)&gw, /* gateway address */ 7346 NULL, 7347 &save_ire->ire_max_frag, 7348 ire_fp_mp, /* Fast Path header */ 7349 dst_ill->ill_rq, /* recv-from queue */ 7350 dst_ill->ill_wq, /* send-to queue */ 7351 IRE_CACHE, /* IRE type */ 7352 save_ire->ire_dlureq_mp, 7353 src_ipif, 7354 in_ill, /* incoming ill */ 7355 (sire != NULL) ? 7356 sire->ire_mask : 0, /* Parent mask */ 7357 (sire != NULL) ? 7358 sire->ire_phandle : 0, /* Parent handle */ 7359 ipif_ire->ire_ihandle, /* Interface handle */ 7360 (sire != NULL) ? (sire->ire_flags & 7361 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ 7362 (sire != NULL) ? 7363 &(sire->ire_uinfo) : &(save_ire->ire_uinfo)); 7364 7365 if (ire == NULL) { 7366 ire_refrele(ipif_ire); 7367 ire_refrele(save_ire); 7368 break; 7369 } 7370 ire->ire_marks |= ire_marks; 7371 7372 /* 7373 * Prevent sire and ipif_ire from getting deleted. 7374 * The newly created ire is tied to both of them via 7375 * the phandle and ihandle respectively. 7376 */ 7377 if (sire != NULL) { 7378 IRB_REFHOLD(sire->ire_bucket); 7379 /* Has it been removed already ? */ 7380 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 7381 IRB_REFRELE(sire->ire_bucket); 7382 ire_refrele(ipif_ire); 7383 ire_refrele(save_ire); 7384 break; 7385 } 7386 } 7387 7388 IRB_REFHOLD(ipif_ire->ire_bucket); 7389 /* Has it been removed already ? */ 7390 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 7391 IRB_REFRELE(ipif_ire->ire_bucket); 7392 if (sire != NULL) 7393 IRB_REFRELE(sire->ire_bucket); 7394 ire_refrele(ipif_ire); 7395 ire_refrele(save_ire); 7396 break; 7397 } 7398 7399 xmit_mp = first_mp; 7400 /* 7401 * In the case of multirouting, a copy 7402 * of the packet is done before its sending. 7403 * The copy is used to attempt another 7404 * route resolution, in a next loop. 7405 */ 7406 if (ire->ire_flags & RTF_MULTIRT) { 7407 copy_mp = copymsg(first_mp); 7408 if (copy_mp != NULL) { 7409 xmit_mp = copy_mp; 7410 MULTIRT_DEBUG_TAG(first_mp); 7411 } 7412 } 7413 ire_add_then_send(q, ire, xmit_mp); 7414 ire_refrele(save_ire); 7415 7416 /* Assert that sire is not deleted yet. */ 7417 if (sire != NULL) { 7418 ASSERT(sire->ire_ptpn != NULL); 7419 IRB_REFRELE(sire->ire_bucket); 7420 } 7421 7422 /* Assert that ipif_ire is not deleted yet. */ 7423 ASSERT(ipif_ire->ire_ptpn != NULL); 7424 IRB_REFRELE(ipif_ire->ire_bucket); 7425 ire_refrele(ipif_ire); 7426 7427 /* 7428 * If copy_mp is not NULL, multirouting was 7429 * requested. We loop to initiate a next 7430 * route resolution attempt, starting from sire. 7431 */ 7432 if (copy_mp != NULL) { 7433 /* 7434 * Search for the next unresolved 7435 * multirt route. 7436 */ 7437 copy_mp = NULL; 7438 ipif_ire = NULL; 7439 ire = NULL; 7440 multirt_resolve_next = B_TRUE; 7441 continue; 7442 } 7443 if (sire != NULL) 7444 ire_refrele(sire); 7445 ipif_refrele(src_ipif); 7446 ill_refrele(dst_ill); 7447 return; 7448 } 7449 case IRE_IF_NORESOLVER: { 7450 /* 7451 * We have what we need to build an IRE_CACHE. 7452 * 7453 * Create a new dlureq_mp with the IP gateway address 7454 * in destination address in the DLPI hdr if the 7455 * physical length is exactly 4 bytes. 7456 */ 7457 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 7458 uchar_t *addr; 7459 7460 if (gw) 7461 addr = (uchar_t *)&gw; 7462 else 7463 addr = (uchar_t *)&dst; 7464 7465 dlureq_mp = ill_dlur_gen(addr, 7466 dst_ill->ill_phys_addr_length, 7467 dst_ill->ill_sap, 7468 dst_ill->ill_sap_length); 7469 } else { 7470 dlureq_mp = ire->ire_dlureq_mp; 7471 } 7472 7473 if (dlureq_mp == NULL) { 7474 ip1dbg(("ip_newroute: dlureq_mp NULL\n")); 7475 break; 7476 } 7477 7478 ire = ire_create( 7479 (uchar_t *)&dst, /* dest address */ 7480 (uchar_t *)&ip_g_all_ones, /* mask */ 7481 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 7482 (uchar_t *)&gw, /* gateway address */ 7483 NULL, 7484 &save_ire->ire_max_frag, 7485 NULL, /* Fast Path header */ 7486 dst_ill->ill_rq, /* recv-from queue */ 7487 dst_ill->ill_wq, /* send-to queue */ 7488 IRE_CACHE, 7489 dlureq_mp, 7490 src_ipif, 7491 in_ill, /* Incoming ill */ 7492 save_ire->ire_mask, /* Parent mask */ 7493 (sire != NULL) ? /* Parent handle */ 7494 sire->ire_phandle : 0, 7495 save_ire->ire_ihandle, /* Interface handle */ 7496 (sire != NULL) ? sire->ire_flags & 7497 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ 7498 &(save_ire->ire_uinfo)); 7499 7500 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) 7501 freeb(dlureq_mp); 7502 7503 if (ire == NULL) { 7504 ire_refrele(save_ire); 7505 break; 7506 } 7507 7508 ire->ire_marks |= ire_marks; 7509 7510 /* Prevent save_ire from getting deleted */ 7511 IRB_REFHOLD(save_ire->ire_bucket); 7512 /* Has it been removed already ? */ 7513 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 7514 IRB_REFRELE(save_ire->ire_bucket); 7515 ire_refrele(save_ire); 7516 break; 7517 } 7518 7519 /* 7520 * In the case of multirouting, a copy 7521 * of the packet is made before it is sent. 7522 * The copy is used in the next 7523 * loop to attempt another resolution. 7524 */ 7525 xmit_mp = first_mp; 7526 if ((sire != NULL) && 7527 (sire->ire_flags & RTF_MULTIRT)) { 7528 copy_mp = copymsg(first_mp); 7529 if (copy_mp != NULL) { 7530 xmit_mp = copy_mp; 7531 MULTIRT_DEBUG_TAG(first_mp); 7532 } 7533 } 7534 ire_add_then_send(q, ire, xmit_mp); 7535 7536 /* Assert that it is not deleted yet. */ 7537 ASSERT(save_ire->ire_ptpn != NULL); 7538 IRB_REFRELE(save_ire->ire_bucket); 7539 ire_refrele(save_ire); 7540 7541 if (copy_mp != NULL) { 7542 /* 7543 * If we found a (no)resolver, we ignore any 7544 * trailing top priority IRE_CACHE in further 7545 * loops. This ensures that we do not omit any 7546 * (no)resolver. 7547 * This IRE_CACHE, if any, will be processed 7548 * by another thread entering ip_newroute(). 7549 * IRE_CACHE entries, if any, will be processed 7550 * by another thread entering ip_newroute(), 7551 * (upon resolver response, for instance). 7552 * This aims to force parallel multirt 7553 * resolutions as soon as a packet must be sent. 7554 * In the best case, after the tx of only one 7555 * packet, all reachable routes are resolved. 7556 * Otherwise, the resolution of all RTF_MULTIRT 7557 * routes would require several emissions. 7558 */ 7559 multirt_flags &= ~MULTIRT_CACHEGW; 7560 7561 /* 7562 * Search for the next unresolved multirt 7563 * route. 7564 */ 7565 copy_mp = NULL; 7566 save_ire = NULL; 7567 ire = NULL; 7568 multirt_resolve_next = B_TRUE; 7569 continue; 7570 } 7571 7572 /* 7573 * Don't need sire anymore 7574 */ 7575 if (sire != NULL) 7576 ire_refrele(sire); 7577 7578 ipif_refrele(src_ipif); 7579 ill_refrele(dst_ill); 7580 return; 7581 } 7582 case IRE_IF_RESOLVER: 7583 /* 7584 * We can't build an IRE_CACHE yet, but at least we 7585 * found a resolver that can help. 7586 */ 7587 res_mp = dst_ill->ill_resolver_mp; 7588 if (!OK_RESOLVER_MP(res_mp)) 7589 break; 7590 /* 7591 * To be at this point in the code with a non-zero gw 7592 * means that dst is reachable through a gateway that 7593 * we have never resolved. By changing dst to the gw 7594 * addr we resolve the gateway first. 7595 * When ire_add_then_send() tries to put the IP dg 7596 * to dst, it will reenter ip_newroute() at which 7597 * time we will find the IRE_CACHE for the gw and 7598 * create another IRE_CACHE in case IRE_CACHE above. 7599 */ 7600 if (gw != INADDR_ANY) { 7601 /* 7602 * The source ipif that was determined above was 7603 * relative to the destination address, not the 7604 * gateway's. If src_ipif was not taken out of 7605 * the IRE_IF_RESOLVER entry, we'll need to call 7606 * ipif_select_source() again. 7607 */ 7608 if (src_ipif != ire->ire_ipif) { 7609 ipif_refrele(src_ipif); 7610 src_ipif = ipif_select_source(dst_ill, 7611 gw, zoneid); 7612 if (src_ipif == NULL) { 7613 if (ip_debug > 2) { 7614 pr_addr_dbg( 7615 "ip_newroute: no " 7616 "src for gw %s ", 7617 AF_INET, &gw); 7618 printf("through " 7619 "interface %s\n", 7620 dst_ill->ill_name); 7621 } 7622 goto icmp_err_ret; 7623 } 7624 } 7625 save_dst = dst; 7626 dst = gw; 7627 gw = INADDR_ANY; 7628 } 7629 /* 7630 * We obtain a partial IRE_CACHE which we will pass 7631 * along with the resolver query. When the response 7632 * comes back it will be there ready for us to add. 7633 * The ire_max_frag is atomically set under the 7634 * irebucket lock in ire_add_v[46]. 7635 */ 7636 ire = ire_create_mp( 7637 (uchar_t *)&dst, /* dest address */ 7638 (uchar_t *)&ip_g_all_ones, /* mask */ 7639 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 7640 (uchar_t *)&gw, /* gateway address */ 7641 NULL, /* no in_src_addr */ 7642 NULL, /* ire_max_frag */ 7643 NULL, /* Fast Path header */ 7644 dst_ill->ill_rq, /* recv-from queue */ 7645 dst_ill->ill_wq, /* send-to queue */ 7646 IRE_CACHE, 7647 res_mp, 7648 src_ipif, /* Interface ipif */ 7649 in_ill, /* Incoming ILL */ 7650 save_ire->ire_mask, /* Parent mask */ 7651 0, 7652 save_ire->ire_ihandle, /* Interface handle */ 7653 0, /* flags if any */ 7654 &(save_ire->ire_uinfo)); 7655 7656 if (ire == NULL) { 7657 ire_refrele(save_ire); 7658 break; 7659 } 7660 7661 if ((sire != NULL) && 7662 (sire->ire_flags & RTF_MULTIRT)) { 7663 copy_mp = copymsg(first_mp); 7664 if (copy_mp != NULL) 7665 MULTIRT_DEBUG_TAG(copy_mp); 7666 } 7667 7668 ire->ire_marks |= ire_marks; 7669 7670 /* 7671 * Construct message chain for the resolver 7672 * of the form: 7673 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 7674 * Packet could contain a IPSEC_OUT mp. 7675 * 7676 * NOTE : ire will be added later when the response 7677 * comes back from ARP. If the response does not 7678 * come back, ARP frees the packet. For this reason, 7679 * we can't REFHOLD the bucket of save_ire to prevent 7680 * deletions. We may not be able to REFRELE the bucket 7681 * if the response never comes back. Thus, before 7682 * adding the ire, ire_add_v4 will make sure that the 7683 * interface route does not get deleted. This is the 7684 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 7685 * where we can always prevent deletions because of 7686 * the synchronous nature of adding IRES i.e 7687 * ire_add_then_send is called after creating the IRE. 7688 */ 7689 ASSERT(ire->ire_mp != NULL); 7690 ire->ire_mp->b_cont = first_mp; 7691 /* Have saved_mp handy, for cleanup if canput fails */ 7692 saved_mp = mp; 7693 mp = ire->ire_dlureq_mp; 7694 ASSERT(mp != NULL); 7695 ire->ire_dlureq_mp = NULL; 7696 linkb(mp, ire->ire_mp); 7697 7698 7699 /* 7700 * Fill in the source and dest addrs for the resolver. 7701 * NOTE: this depends on memory layouts imposed by 7702 * ill_init(). 7703 */ 7704 areq = (areq_t *)mp->b_rptr; 7705 addrp = (ipaddr_t *)((char *)areq + 7706 areq->areq_sender_addr_offset); 7707 if (do_attach_ill) { 7708 /* 7709 * This is bind to no failover case. 7710 * arp packet also must go out on attach_ill. 7711 */ 7712 ASSERT(ipha->ipha_src != NULL); 7713 *addrp = ipha->ipha_src; 7714 } else { 7715 *addrp = save_ire->ire_src_addr; 7716 } 7717 7718 ire_refrele(save_ire); 7719 addrp = (ipaddr_t *)((char *)areq + 7720 areq->areq_target_addr_offset); 7721 *addrp = dst; 7722 /* Up to the resolver. */ 7723 if (canputnext(dst_ill->ill_rq)) { 7724 putnext(dst_ill->ill_rq, mp); 7725 ire = NULL; 7726 if (copy_mp != NULL) { 7727 /* 7728 * If we found a resolver, we ignore 7729 * any trailing top priority IRE_CACHE 7730 * in the further loops. This ensures 7731 * that we do not omit any resolver. 7732 * IRE_CACHE entries, if any, will be 7733 * processed next time we enter 7734 * ip_newroute(). 7735 */ 7736 multirt_flags &= ~MULTIRT_CACHEGW; 7737 /* 7738 * Search for the next unresolved 7739 * multirt route. 7740 */ 7741 first_mp = copy_mp; 7742 copy_mp = NULL; 7743 /* Prepare the next resolution loop. */ 7744 mp = first_mp; 7745 EXTRACT_PKT_MP(mp, first_mp, 7746 mctl_present); 7747 if (mctl_present) 7748 io = (ipsec_out_t *) 7749 first_mp->b_rptr; 7750 ipha = (ipha_t *)mp->b_rptr; 7751 7752 ASSERT(sire != NULL); 7753 7754 dst = save_dst; 7755 multirt_resolve_next = B_TRUE; 7756 continue; 7757 } 7758 7759 if (sire != NULL) 7760 ire_refrele(sire); 7761 7762 /* 7763 * The response will come back in ip_wput 7764 * with db_type IRE_DB_TYPE. 7765 */ 7766 ipif_refrele(src_ipif); 7767 ill_refrele(dst_ill); 7768 return; 7769 } else { 7770 /* Prepare for cleanup */ 7771 ire->ire_dlureq_mp = mp; 7772 mp->b_cont = NULL; 7773 ire_delete(ire); 7774 mp = saved_mp; 7775 ire = NULL; 7776 if (copy_mp != NULL) { 7777 MULTIRT_DEBUG_UNTAG(copy_mp); 7778 freemsg(copy_mp); 7779 copy_mp = NULL; 7780 } 7781 break; 7782 } 7783 default: 7784 break; 7785 } 7786 } while (multirt_resolve_next); 7787 7788 ip1dbg(("ip_newroute: dropped\n")); 7789 /* Did this packet originate externally? */ 7790 if (mp->b_prev) { 7791 mp->b_next = NULL; 7792 mp->b_prev = NULL; 7793 BUMP_MIB(&ip_mib, ipInDiscards); 7794 } else { 7795 BUMP_MIB(&ip_mib, ipOutDiscards); 7796 } 7797 ASSERT(copy_mp == NULL); 7798 MULTIRT_DEBUG_UNTAG(first_mp); 7799 freemsg(first_mp); 7800 if (ire != NULL) 7801 ire_refrele(ire); 7802 if (sire != NULL) 7803 ire_refrele(sire); 7804 if (src_ipif != NULL) 7805 ipif_refrele(src_ipif); 7806 if (dst_ill != NULL) 7807 ill_refrele(dst_ill); 7808 return; 7809 7810 icmp_err_ret: 7811 ip1dbg(("ip_newroute: no route\n")); 7812 if (src_ipif != NULL) 7813 ipif_refrele(src_ipif); 7814 if (dst_ill != NULL) 7815 ill_refrele(dst_ill); 7816 if (sire != NULL) 7817 ire_refrele(sire); 7818 /* Did this packet originate externally? */ 7819 if (mp->b_prev) { 7820 mp->b_next = NULL; 7821 mp->b_prev = NULL; 7822 /* XXX ipInNoRoutes */ 7823 q = WR(q); 7824 } else { 7825 /* 7826 * Since ip_wput() isn't close to finished, we fill 7827 * in enough of the header for credible error reporting. 7828 */ 7829 if (ip_hdr_complete(ipha, zoneid)) { 7830 /* Failed */ 7831 MULTIRT_DEBUG_UNTAG(first_mp); 7832 freemsg(first_mp); 7833 if (ire != NULL) 7834 ire_refrele(ire); 7835 return; 7836 } 7837 } 7838 BUMP_MIB(&ip_mib, ipOutNoRoutes); 7839 7840 /* 7841 * At this point we will have ire only if RTF_BLACKHOLE 7842 * or RTF_REJECT flags are set on the IRE. It will not 7843 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 7844 */ 7845 if (ire != NULL) { 7846 if (ire->ire_flags & RTF_BLACKHOLE) { 7847 ire_refrele(ire); 7848 MULTIRT_DEBUG_UNTAG(first_mp); 7849 freemsg(first_mp); 7850 return; 7851 } 7852 ire_refrele(ire); 7853 } 7854 if (ip_source_routed(ipha)) { 7855 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED); 7856 return; 7857 } 7858 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE); 7859 } 7860 7861 /* 7862 * IPv4 - 7863 * ip_newroute_ipif is called by ip_wput_multicast and 7864 * ip_rput_forward_multicast whenever we need to send 7865 * out a packet to a destination address for which we do not have specific 7866 * routing information. It is used when the packet will be sent out 7867 * on a specific interface. It is also called by ip_wput() when IP_XMIT_IF 7868 * socket option is set or icmp error message wants to go out on a particular 7869 * interface for a unicast packet. 7870 * 7871 * In most cases, the destination address is resolved thanks to the ipif 7872 * intrinsic resolver. However, there are some cases where the call to 7873 * ip_newroute_ipif must take into account the potential presence of 7874 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire 7875 * that uses the interface. This is specified through flags, 7876 * which can be a combination of: 7877 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC 7878 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address 7879 * and flags. Additionally, the packet source address has to be set to 7880 * the specified address. The caller is thus expected to set this flag 7881 * if the packet has no specific source address yet. 7882 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT 7883 * flag, the resulting ire will inherit the flag. All unresolved routes 7884 * to the destination must be explored in the same call to 7885 * ip_newroute_ipif(). 7886 */ 7887 static void 7888 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, 7889 conn_t *connp, uint32_t flags) 7890 { 7891 areq_t *areq; 7892 ire_t *ire = NULL; 7893 mblk_t *res_mp; 7894 ipaddr_t *addrp; 7895 mblk_t *first_mp; 7896 ire_t *save_ire = NULL; 7897 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ 7898 ipif_t *src_ipif = NULL; 7899 ushort_t ire_marks = 0; 7900 ill_t *dst_ill = NULL; 7901 boolean_t mctl_present; 7902 ipsec_out_t *io; 7903 ipha_t *ipha; 7904 int ihandle = 0; 7905 mblk_t *saved_mp; 7906 ire_t *fire = NULL; 7907 mblk_t *copy_mp = NULL; 7908 boolean_t multirt_resolve_next; 7909 ipaddr_t ipha_dst; 7910 zoneid_t zoneid = (connp != NULL ? connp->conn_zoneid : ALL_ZONES); 7911 7912 /* 7913 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold 7914 * here for uniformity 7915 */ 7916 ipif_refhold(ipif); 7917 7918 /* 7919 * This loop is run only once in most cases. 7920 * We loop to resolve further routes only when the destination 7921 * can be reached through multiple RTF_MULTIRT-flagged ires. 7922 */ 7923 do { 7924 if (dst_ill != NULL) { 7925 ill_refrele(dst_ill); 7926 dst_ill = NULL; 7927 } 7928 if (src_ipif != NULL) { 7929 ipif_refrele(src_ipif); 7930 src_ipif = NULL; 7931 } 7932 multirt_resolve_next = B_FALSE; 7933 7934 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), 7935 ipif->ipif_ill->ill_name)); 7936 7937 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7938 if (mctl_present) 7939 io = (ipsec_out_t *)first_mp->b_rptr; 7940 7941 ipha = (ipha_t *)mp->b_rptr; 7942 7943 /* 7944 * Save the packet destination address, we may need it after 7945 * the packet has been consumed. 7946 */ 7947 ipha_dst = ipha->ipha_dst; 7948 7949 /* 7950 * If the interface is a pt-pt interface we look for an 7951 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 7952 * local_address and the pt-pt destination address. Otherwise 7953 * we just match the local address. 7954 * NOTE: dst could be different than ipha->ipha_dst in case 7955 * of sending igmp multicast packets over a point-to-point 7956 * connection. 7957 * Thus we must be careful enough to check ipha_dst to be a 7958 * multicast address, otherwise it will take xmit_if path for 7959 * multicast packets resulting into kernel stack overflow by 7960 * repeated calls to ip_newroute_ipif from ire_send(). 7961 */ 7962 if (CLASSD(ipha_dst) && 7963 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { 7964 goto err_ret; 7965 } 7966 7967 /* 7968 * We check if an IRE_OFFSUBNET for the addr that goes through 7969 * ipif exists. We need it to determine if the RTF_SETSRC and/or 7970 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may 7971 * propagate its flags to the new ire. 7972 */ 7973 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { 7974 fire = ipif_lookup_multi_ire(ipif, ipha_dst); 7975 ip2dbg(("ip_newroute_ipif: " 7976 "ipif_lookup_multi_ire(" 7977 "ipif %p, dst %08x) = fire %p\n", 7978 (void *)ipif, ntohl(dst), (void *)fire)); 7979 } 7980 7981 if (mctl_present && io->ipsec_out_attach_if) { 7982 attach_ill = ip_grab_attach_ill(NULL, first_mp, 7983 io->ipsec_out_ill_index, B_FALSE); 7984 7985 /* Failure case frees things for us. */ 7986 if (attach_ill == NULL) { 7987 ipif_refrele(ipif); 7988 if (fire != NULL) 7989 ire_refrele(fire); 7990 return; 7991 } 7992 7993 /* 7994 * Check if we need an ire that will not be 7995 * looked up by anybody else i.e. HIDDEN. 7996 */ 7997 if (ill_is_probeonly(attach_ill)) { 7998 ire_marks = IRE_MARK_HIDDEN; 7999 } 8000 /* 8001 * ip_wput passes the right ipif for IPIF_NOFAILOVER 8002 * case. 8003 */ 8004 dst_ill = ipif->ipif_ill; 8005 /* attach_ill has been refheld by ip_grab_attach_ill */ 8006 ASSERT(dst_ill == attach_ill); 8007 } else { 8008 /* 8009 * If this is set by IP_XMIT_IF, then make sure that 8010 * ipif is pointing to the same ill as the IP_XMIT_IF 8011 * specified ill. 8012 */ 8013 ASSERT((connp == NULL) || 8014 (connp->conn_xmit_if_ill == NULL) || 8015 (connp->conn_xmit_if_ill == ipif->ipif_ill)); 8016 /* 8017 * If the interface belongs to an interface group, 8018 * make sure the next possible interface in the group 8019 * is used. This encourages load spreading among 8020 * peers in an interface group. 8021 * Note: load spreading is disabled for RTF_MULTIRT 8022 * routes. 8023 */ 8024 if ((flags & RTF_MULTIRT) && (fire != NULL) && 8025 (fire->ire_flags & RTF_MULTIRT)) { 8026 /* 8027 * Don't perform outbound load spreading 8028 * in the case of an RTF_MULTIRT issued route, 8029 * we actually typically want to replicate 8030 * outgoing packets through particular 8031 * interfaces. 8032 */ 8033 dst_ill = ipif->ipif_ill; 8034 ill_refhold(dst_ill); 8035 } else { 8036 dst_ill = ip_newroute_get_dst_ill( 8037 ipif->ipif_ill); 8038 } 8039 if (dst_ill == NULL) { 8040 if (ip_debug > 2) { 8041 pr_addr_dbg("ip_newroute_ipif: " 8042 "no dst ill for dst %s\n", 8043 AF_INET, &dst); 8044 } 8045 goto err_ret; 8046 } 8047 } 8048 8049 /* 8050 * Pick a source address preferring non-deprecated ones. 8051 * Unlike ip_newroute, we don't do any source address 8052 * selection here since for multicast it really does not help 8053 * in inbound load spreading as in the unicast case. 8054 */ 8055 if ((flags & RTF_SETSRC) && (fire != NULL) && 8056 (fire->ire_flags & RTF_SETSRC)) { 8057 /* 8058 * As requested by flags, an IRE_OFFSUBNET was looked up 8059 * on that interface. This ire has RTF_SETSRC flag, so 8060 * the source address of the packet must be changed. 8061 * Check that the ipif matching the requested source 8062 * address still exists. 8063 */ 8064 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, 8065 zoneid, NULL, NULL, NULL, NULL); 8066 } 8067 if (((ipif->ipif_flags & IPIF_DEPRECATED) || 8068 (connp != NULL && ipif->ipif_zoneid != zoneid)) && 8069 (src_ipif == NULL)) { 8070 src_ipif = ipif_select_source(dst_ill, dst, zoneid); 8071 if (src_ipif == NULL) { 8072 if (ip_debug > 2) { 8073 /* ip1dbg */ 8074 pr_addr_dbg("ip_newroute_ipif: " 8075 "no src for dst %s", 8076 AF_INET, &dst); 8077 } 8078 ip1dbg((" through interface %s\n", 8079 dst_ill->ill_name)); 8080 goto err_ret; 8081 } 8082 ipif_refrele(ipif); 8083 ipif = src_ipif; 8084 ipif_refhold(ipif); 8085 } 8086 if (src_ipif == NULL) { 8087 src_ipif = ipif; 8088 ipif_refhold(src_ipif); 8089 } 8090 8091 /* 8092 * Assign a source address while we have the conn. 8093 * We can't have ip_wput_ire pick a source address when the 8094 * packet returns from arp since conn_unspec_src might be set 8095 * and we loose the conn when going through arp. 8096 */ 8097 if (ipha->ipha_src == INADDR_ANY && 8098 (connp == NULL || !connp->conn_unspec_src)) { 8099 ipha->ipha_src = src_ipif->ipif_src_addr; 8100 } 8101 8102 /* 8103 * In case of IP_XMIT_IF, it is possible that the outgoing 8104 * interface does not have an interface ire. 8105 * Example: Thousands of mobileip PPP interfaces to mobile 8106 * nodes. We don't want to create interface ires because 8107 * packets from other mobile nodes must not take the route 8108 * via interface ires to the visiting mobile node without 8109 * going through the home agent, in absence of mobileip 8110 * route optimization. 8111 */ 8112 if (CLASSD(ipha_dst) && (connp == NULL || 8113 connp->conn_xmit_if_ill == NULL)) { 8114 /* ipif_to_ire returns an held ire */ 8115 ire = ipif_to_ire(ipif); 8116 if (ire == NULL) 8117 goto err_ret; 8118 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 8119 goto err_ret; 8120 /* 8121 * ihandle is needed when the ire is added to 8122 * cache table. 8123 */ 8124 save_ire = ire; 8125 ihandle = save_ire->ire_ihandle; 8126 8127 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " 8128 "flags %04x\n", 8129 (void *)ire, (void *)ipif, flags)); 8130 if ((flags & RTF_MULTIRT) && (fire != NULL) && 8131 (fire->ire_flags & RTF_MULTIRT)) { 8132 /* 8133 * As requested by flags, an IRE_OFFSUBNET was 8134 * looked up on that interface. This ire has 8135 * RTF_MULTIRT flag, so the resolution loop will 8136 * be re-entered to resolve additional routes on 8137 * other interfaces. For that purpose, a copy of 8138 * the packet is performed at this point. 8139 */ 8140 fire->ire_last_used_time = lbolt; 8141 copy_mp = copymsg(first_mp); 8142 if (copy_mp) { 8143 MULTIRT_DEBUG_TAG(copy_mp); 8144 } 8145 } 8146 if ((flags & RTF_SETSRC) && (fire != NULL) && 8147 (fire->ire_flags & RTF_SETSRC)) { 8148 /* 8149 * As requested by flags, an IRE_OFFSUBET was 8150 * looked up on that interface. This ire has 8151 * RTF_SETSRC flag, so the source address of the 8152 * packet must be changed. 8153 */ 8154 ipha->ipha_src = fire->ire_src_addr; 8155 } 8156 } else { 8157 ASSERT((connp == NULL) || 8158 (connp->conn_xmit_if_ill != NULL) || 8159 (connp->conn_dontroute)); 8160 /* 8161 * The only ways we can come here are: 8162 * 1) IP_XMIT_IF socket option is set 8163 * 2) ICMP error message generated from 8164 * ip_mrtun_forward() routine and it needs 8165 * to go through the specified ill. 8166 * 3) SO_DONTROUTE socket option is set 8167 * In all cases, the new ire will not be added 8168 * into cache table. 8169 */ 8170 ire_marks |= IRE_MARK_NOADD; 8171 } 8172 8173 switch (ipif->ipif_net_type) { 8174 case IRE_IF_NORESOLVER: { 8175 /* We have what we need to build an IRE_CACHE. */ 8176 mblk_t *dlureq_mp; 8177 8178 /* 8179 * Create a new dlureq_mp with the 8180 * IP gateway address as destination address in the 8181 * DLPI hdr if the physical length is exactly 4 bytes. 8182 */ 8183 if (dst_ill->ill_phys_addr_length == IP_ADDR_LEN) { 8184 dlureq_mp = ill_dlur_gen((uchar_t *)&dst, 8185 dst_ill->ill_phys_addr_length, 8186 dst_ill->ill_sap, 8187 dst_ill->ill_sap_length); 8188 } else { 8189 /* use the value set in ip_ll_subnet_defaults */ 8190 dlureq_mp = ill_dlur_gen(NULL, 8191 dst_ill->ill_phys_addr_length, 8192 dst_ill->ill_sap, 8193 dst_ill->ill_sap_length); 8194 } 8195 8196 if (dlureq_mp == NULL) 8197 break; 8198 /* 8199 * The new ire inherits the IRE_OFFSUBNET flags 8200 * and source address, if this was requested. 8201 */ 8202 ire = ire_create( 8203 (uchar_t *)&dst, /* dest address */ 8204 (uchar_t *)&ip_g_all_ones, /* mask */ 8205 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8206 NULL, /* gateway address */ 8207 NULL, 8208 &ipif->ipif_mtu, 8209 NULL, /* Fast Path header */ 8210 dst_ill->ill_rq, /* recv-from queue */ 8211 dst_ill->ill_wq, /* send-to queue */ 8212 IRE_CACHE, 8213 dlureq_mp, 8214 src_ipif, 8215 NULL, 8216 (save_ire != NULL ? save_ire->ire_mask : 0), 8217 (fire != NULL) ? /* Parent handle */ 8218 fire->ire_phandle : 0, 8219 ihandle, /* Interface handle */ 8220 (fire != NULL) ? 8221 (fire->ire_flags & 8222 (RTF_SETSRC | RTF_MULTIRT)) : 0, 8223 (save_ire == NULL ? &ire_uinfo_null : 8224 &save_ire->ire_uinfo)); 8225 8226 freeb(dlureq_mp); 8227 8228 if (ire == NULL) { 8229 if (save_ire != NULL) 8230 ire_refrele(save_ire); 8231 break; 8232 } 8233 8234 ire->ire_marks |= ire_marks; 8235 8236 /* 8237 * If IRE_MARK_NOADD is set then we need to convert 8238 * the max_fragp to a useable value now. This is 8239 * normally done in ire_add_v[46]. 8240 */ 8241 if (ire->ire_marks & IRE_MARK_NOADD) { 8242 uint_t max_frag; 8243 8244 max_frag = *ire->ire_max_fragp; 8245 ire->ire_max_fragp = NULL; 8246 ire->ire_max_frag = max_frag; 8247 } 8248 8249 /* Prevent save_ire from getting deleted */ 8250 if (save_ire != NULL) { 8251 IRB_REFHOLD(save_ire->ire_bucket); 8252 /* Has it been removed already ? */ 8253 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 8254 IRB_REFRELE(save_ire->ire_bucket); 8255 ire_refrele(save_ire); 8256 break; 8257 } 8258 } 8259 8260 ire_add_then_send(q, ire, first_mp); 8261 8262 /* Assert that save_ire is not deleted yet. */ 8263 if (save_ire != NULL) { 8264 ASSERT(save_ire->ire_ptpn != NULL); 8265 IRB_REFRELE(save_ire->ire_bucket); 8266 ire_refrele(save_ire); 8267 save_ire = NULL; 8268 } 8269 if (fire != NULL) { 8270 ire_refrele(fire); 8271 fire = NULL; 8272 } 8273 8274 /* 8275 * the resolution loop is re-entered if this 8276 * was requested through flags and if we 8277 * actually are in a multirouting case. 8278 */ 8279 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 8280 boolean_t need_resolve = 8281 ire_multirt_need_resolve(ipha_dst); 8282 if (!need_resolve) { 8283 MULTIRT_DEBUG_UNTAG(copy_mp); 8284 freemsg(copy_mp); 8285 copy_mp = NULL; 8286 } else { 8287 /* 8288 * ipif_lookup_group() calls 8289 * ire_lookup_multi() that uses 8290 * ire_ftable_lookup() to find 8291 * an IRE_INTERFACE for the group. 8292 * In the multirt case, 8293 * ire_lookup_multi() then invokes 8294 * ire_multirt_lookup() to find 8295 * the next resolvable ire. 8296 * As a result, we obtain an new 8297 * interface, derived from the 8298 * next ire. 8299 */ 8300 ipif_refrele(ipif); 8301 ipif = ipif_lookup_group(ipha_dst, 8302 zoneid); 8303 ip2dbg(("ip_newroute_ipif: " 8304 "multirt dst %08x, ipif %p\n", 8305 htonl(dst), (void *)ipif)); 8306 if (ipif != NULL) { 8307 mp = copy_mp; 8308 copy_mp = NULL; 8309 multirt_resolve_next = B_TRUE; 8310 continue; 8311 } else { 8312 freemsg(copy_mp); 8313 } 8314 } 8315 } 8316 if (ipif != NULL) 8317 ipif_refrele(ipif); 8318 ill_refrele(dst_ill); 8319 ipif_refrele(src_ipif); 8320 return; 8321 } 8322 case IRE_IF_RESOLVER: 8323 /* 8324 * We can't build an IRE_CACHE yet, but at least 8325 * we found a resolver that can help. 8326 */ 8327 res_mp = dst_ill->ill_resolver_mp; 8328 if (!OK_RESOLVER_MP(res_mp)) 8329 break; 8330 8331 /* 8332 * We obtain a partial IRE_CACHE which we will pass 8333 * along with the resolver query. When the response 8334 * comes back it will be there ready for us to add. 8335 * The new ire inherits the IRE_OFFSUBNET flags 8336 * and source address, if this was requested. 8337 * The ire_max_frag is atomically set under the 8338 * irebucket lock in ire_add_v[46]. Only in the 8339 * case of IRE_MARK_NOADD, we set it here itself. 8340 */ 8341 ire = ire_create_mp( 8342 (uchar_t *)&dst, /* dest address */ 8343 (uchar_t *)&ip_g_all_ones, /* mask */ 8344 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 8345 NULL, /* gateway address */ 8346 NULL, /* no in_src_addr */ 8347 (ire_marks & IRE_MARK_NOADD) ? 8348 ipif->ipif_mtu : 0, /* max_frag */ 8349 NULL, /* Fast path header */ 8350 dst_ill->ill_rq, /* recv-from queue */ 8351 dst_ill->ill_wq, /* send-to queue */ 8352 IRE_CACHE, 8353 res_mp, 8354 src_ipif, 8355 NULL, 8356 (save_ire != NULL ? save_ire->ire_mask : 0), 8357 (fire != NULL) ? /* Parent handle */ 8358 fire->ire_phandle : 0, 8359 ihandle, /* Interface handle */ 8360 (fire != NULL) ? /* flags if any */ 8361 (fire->ire_flags & 8362 (RTF_SETSRC | RTF_MULTIRT)) : 0, 8363 (save_ire == NULL ? &ire_uinfo_null : 8364 &save_ire->ire_uinfo)); 8365 8366 if (save_ire != NULL) { 8367 ire_refrele(save_ire); 8368 save_ire = NULL; 8369 } 8370 if (ire == NULL) 8371 break; 8372 8373 ire->ire_marks |= ire_marks; 8374 /* 8375 * Construct message chain for the resolver of the 8376 * form: 8377 * ARP_REQ_MBLK-->IRE_MBLK-->Packet 8378 * 8379 * NOTE : ire will be added later when the response 8380 * comes back from ARP. If the response does not 8381 * come back, ARP frees the packet. For this reason, 8382 * we can't REFHOLD the bucket of save_ire to prevent 8383 * deletions. We may not be able to REFRELE the 8384 * bucket if the response never comes back. 8385 * Thus, before adding the ire, ire_add_v4 will make 8386 * sure that the interface route does not get deleted. 8387 * This is the only case unlike ip_newroute_v6, 8388 * ip_newroute_ipif_v6 where we can always prevent 8389 * deletions because ire_add_then_send is called after 8390 * creating the IRE. 8391 * If IRE_MARK_NOADD is set, then ire_add_then_send 8392 * does not add this IRE into the IRE CACHE. 8393 */ 8394 ASSERT(ire->ire_mp != NULL); 8395 ire->ire_mp->b_cont = first_mp; 8396 /* Have saved_mp handy, for cleanup if canput fails */ 8397 saved_mp = mp; 8398 mp = ire->ire_dlureq_mp; 8399 ASSERT(mp != NULL); 8400 ire->ire_dlureq_mp = NULL; 8401 linkb(mp, ire->ire_mp); 8402 8403 /* 8404 * Fill in the source and dest addrs for the resolver. 8405 * NOTE: this depends on memory layouts imposed by 8406 * ill_init(). 8407 */ 8408 areq = (areq_t *)mp->b_rptr; 8409 addrp = (ipaddr_t *)((char *)areq + 8410 areq->areq_sender_addr_offset); 8411 *addrp = ire->ire_src_addr; 8412 addrp = (ipaddr_t *)((char *)areq + 8413 areq->areq_target_addr_offset); 8414 *addrp = dst; 8415 /* Up to the resolver. */ 8416 if (canputnext(dst_ill->ill_rq)) { 8417 putnext(dst_ill->ill_rq, mp); 8418 /* 8419 * The response will come back in ip_wput 8420 * with db_type IRE_DB_TYPE. 8421 */ 8422 } else { 8423 ire->ire_dlureq_mp = mp; 8424 mp->b_cont = NULL; 8425 ire_delete(ire); 8426 saved_mp->b_next = NULL; 8427 saved_mp->b_prev = NULL; 8428 freemsg(first_mp); 8429 ip2dbg(("ip_newroute_ipif: dropped\n")); 8430 } 8431 8432 if (fire != NULL) { 8433 ire_refrele(fire); 8434 fire = NULL; 8435 } 8436 8437 8438 /* 8439 * The resolution loop is re-entered if this was 8440 * requested through flags and we actually are 8441 * in a multirouting case. 8442 */ 8443 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { 8444 boolean_t need_resolve = 8445 ire_multirt_need_resolve(ipha_dst); 8446 if (!need_resolve) { 8447 MULTIRT_DEBUG_UNTAG(copy_mp); 8448 freemsg(copy_mp); 8449 copy_mp = NULL; 8450 } else { 8451 /* 8452 * ipif_lookup_group() calls 8453 * ire_lookup_multi() that uses 8454 * ire_ftable_lookup() to find 8455 * an IRE_INTERFACE for the group. 8456 * In the multirt case, 8457 * ire_lookup_multi() then invokes 8458 * ire_multirt_lookup() to find 8459 * the next resolvable ire. 8460 * As a result, we obtain an new 8461 * interface, derived from the 8462 * next ire. 8463 */ 8464 ipif_refrele(ipif); 8465 ipif = ipif_lookup_group(ipha_dst, 8466 zoneid); 8467 if (ipif != NULL) { 8468 mp = copy_mp; 8469 copy_mp = NULL; 8470 multirt_resolve_next = B_TRUE; 8471 continue; 8472 } else { 8473 freemsg(copy_mp); 8474 } 8475 } 8476 } 8477 if (ipif != NULL) 8478 ipif_refrele(ipif); 8479 ill_refrele(dst_ill); 8480 ipif_refrele(src_ipif); 8481 return; 8482 default: 8483 break; 8484 } 8485 } while (multirt_resolve_next); 8486 8487 err_ret: 8488 ip2dbg(("ip_newroute_ipif: dropped\n")); 8489 if (fire != NULL) 8490 ire_refrele(fire); 8491 ipif_refrele(ipif); 8492 /* Did this packet originate externally? */ 8493 if (dst_ill != NULL) 8494 ill_refrele(dst_ill); 8495 if (src_ipif != NULL) 8496 ipif_refrele(src_ipif); 8497 if (mp->b_prev || mp->b_next) { 8498 mp->b_next = NULL; 8499 mp->b_prev = NULL; 8500 } else { 8501 /* 8502 * Since ip_wput() isn't close to finished, we fill 8503 * in enough of the header for credible error reporting. 8504 */ 8505 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 8506 /* Failed */ 8507 freemsg(first_mp); 8508 if (ire != NULL) 8509 ire_refrele(ire); 8510 return; 8511 } 8512 } 8513 /* 8514 * At this point we will have ire only if RTF_BLACKHOLE 8515 * or RTF_REJECT flags are set on the IRE. It will not 8516 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. 8517 */ 8518 if (ire != NULL) { 8519 if (ire->ire_flags & RTF_BLACKHOLE) { 8520 ire_refrele(ire); 8521 freemsg(first_mp); 8522 return; 8523 } 8524 ire_refrele(ire); 8525 } 8526 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE); 8527 } 8528 8529 /* Name/Value Table Lookup Routine */ 8530 char * 8531 ip_nv_lookup(nv_t *nv, int value) 8532 { 8533 if (!nv) 8534 return (NULL); 8535 for (; nv->nv_name; nv++) { 8536 if (nv->nv_value == value) 8537 return (nv->nv_name); 8538 } 8539 return ("unknown"); 8540 } 8541 8542 /* 8543 * one day it can be patched to 1 from /etc/system for machines that have few 8544 * fast network interfaces feeding multiple cpus. 8545 */ 8546 int ill_stream_putlocks = 0; 8547 8548 /* 8549 * This is a module open, i.e. this is a control stream for access 8550 * to a DLPI device. We allocate an ill_t as the instance data in 8551 * this case. 8552 */ 8553 int 8554 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 8555 { 8556 uint32_t mem_cnt; 8557 uint32_t cpu_cnt; 8558 uint32_t min_cnt; 8559 pgcnt_t mem_avail; 8560 extern uint32_t ip_cache_table_size, ip6_cache_table_size; 8561 ill_t *ill; 8562 int err; 8563 8564 /* 8565 * Prevent unprivileged processes from pushing IP so that 8566 * they can't send raw IP. 8567 */ 8568 if (secpolicy_net_rawaccess(credp) != 0) 8569 return (EPERM); 8570 8571 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 8572 q->q_ptr = WR(q)->q_ptr = ill; 8573 8574 /* 8575 * ill_init initializes the ill fields and then sends down 8576 * down a DL_INFO_REQ after calling qprocson. 8577 */ 8578 err = ill_init(q, ill); 8579 if (err != 0) { 8580 mi_free(ill); 8581 q->q_ptr = NULL; 8582 WR(q)->q_ptr = NULL; 8583 return (err); 8584 } 8585 8586 /* ill_init initializes the ipsq marking this thread as writer */ 8587 ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE, B_TRUE); 8588 /* Wait for the DL_INFO_ACK */ 8589 mutex_enter(&ill->ill_lock); 8590 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 8591 /* 8592 * Return value of 0 indicates a pending signal. 8593 */ 8594 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 8595 if (err == 0) { 8596 mutex_exit(&ill->ill_lock); 8597 (void) ip_close(q, 0); 8598 return (EINTR); 8599 } 8600 } 8601 mutex_exit(&ill->ill_lock); 8602 8603 /* 8604 * ip_rput_other could have set an error in ill_error on 8605 * receipt of M_ERROR. 8606 */ 8607 8608 err = ill->ill_error; 8609 if (err != 0) { 8610 (void) ip_close(q, 0); 8611 return (err); 8612 } 8613 8614 /* 8615 * ip_ire_max_bucket_cnt is sized below based on the memory 8616 * size and the cpu speed of the machine. This is upper 8617 * bounded by the compile time value of ip_ire_max_bucket_cnt 8618 * and is lower bounded by the compile time value of 8619 * ip_ire_min_bucket_cnt. Similar logic applies to 8620 * ip6_ire_max_bucket_cnt. 8621 */ 8622 mem_avail = kmem_avail(); 8623 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 8624 ip_cache_table_size / sizeof (ire_t); 8625 cpu_cnt = CPU->cpu_type_info.pi_clock >> ip_ire_cpu_ratio; 8626 8627 min_cnt = MIN(cpu_cnt, mem_cnt); 8628 if (min_cnt < ip_ire_min_bucket_cnt) 8629 min_cnt = ip_ire_min_bucket_cnt; 8630 if (ip_ire_max_bucket_cnt > min_cnt) { 8631 ip_ire_max_bucket_cnt = min_cnt; 8632 } 8633 8634 mem_cnt = (mem_avail >> ip_ire_mem_ratio) / 8635 ip6_cache_table_size / sizeof (ire_t); 8636 min_cnt = MIN(cpu_cnt, mem_cnt); 8637 if (min_cnt < ip6_ire_min_bucket_cnt) 8638 min_cnt = ip6_ire_min_bucket_cnt; 8639 if (ip6_ire_max_bucket_cnt > min_cnt) { 8640 ip6_ire_max_bucket_cnt = min_cnt; 8641 } 8642 8643 ill->ill_credp = credp; 8644 crhold(credp); 8645 8646 mutex_enter(&ip_mi_lock); 8647 err = mi_open_link(&ip_g_head, (IDP)ill, devp, flag, sflag, credp); 8648 mutex_exit(&ip_mi_lock); 8649 if (err) { 8650 (void) ip_close(q, 0); 8651 return (err); 8652 } 8653 return (0); 8654 } 8655 8656 /* IP open routine. */ 8657 int 8658 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 8659 { 8660 conn_t *connp; 8661 major_t maj; 8662 8663 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); 8664 8665 /* Allow reopen. */ 8666 if (q->q_ptr != NULL) 8667 return (0); 8668 8669 if (sflag & MODOPEN) { 8670 /* This is a module open */ 8671 return (ip_modopen(q, devp, flag, sflag, credp)); 8672 } 8673 8674 /* 8675 * We are opening as a device. This is an IP client stream, and we 8676 * allocate an conn_t as the instance data. 8677 */ 8678 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP); 8679 connp->conn_upq = q; 8680 q->q_ptr = WR(q)->q_ptr = connp; 8681 8682 if (flag & SO_SOCKSTR) 8683 connp->conn_flags |= IPCL_SOCKET; 8684 8685 /* Minor tells us which /dev entry was opened */ 8686 if (geteminor(*devp) == IPV6_MINOR) { 8687 connp->conn_flags |= IPCL_ISV6; 8688 connp->conn_af_isv6 = B_TRUE; 8689 ip_setqinfo(q, geteminor(*devp), B_FALSE); 8690 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 8691 } else { 8692 connp->conn_af_isv6 = B_FALSE; 8693 connp->conn_pkt_isv6 = B_FALSE; 8694 } 8695 8696 if ((connp->conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) { 8697 q->q_ptr = WR(q)->q_ptr = NULL; 8698 CONN_DEC_REF(connp); 8699 return (EBUSY); 8700 } 8701 8702 maj = getemajor(*devp); 8703 *devp = makedevice(maj, (minor_t)connp->conn_dev); 8704 8705 /* 8706 * connp->conn_cred is crfree()ed in ip_close(). 8707 */ 8708 connp->conn_cred = credp; 8709 crhold(connp->conn_cred); 8710 8711 connp->conn_zoneid = getzoneid(); 8712 8713 /* 8714 * This should only happen for ndd, netstat, raw socket or other SCTP 8715 * administrative ops. In these cases, we just need a normal conn_t 8716 * with ulp set to IPPROTO_SCTP. All other ops are trapped and 8717 * an error will be returned. 8718 */ 8719 if (maj != SCTP_MAJ && maj != SCTP6_MAJ) { 8720 connp->conn_rq = q; 8721 connp->conn_wq = WR(q); 8722 } else { 8723 connp->conn_ulp = IPPROTO_SCTP; 8724 connp->conn_rq = connp->conn_wq = NULL; 8725 } 8726 /* Non-zero default values */ 8727 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 8728 8729 /* 8730 * Make the conn globally visible to walkers 8731 */ 8732 mutex_enter(&connp->conn_lock); 8733 connp->conn_state_flags &= ~CONN_INCIPIENT; 8734 mutex_exit(&connp->conn_lock); 8735 ASSERT(connp->conn_ref == 1); 8736 8737 qprocson(q); 8738 8739 return (0); 8740 } 8741 8742 /* 8743 * Change q_qinfo based on the value of isv6. 8744 * This can not called on an ill queue. 8745 * Note that there is no race since either q_qinfo works for conn queues - it 8746 * is just an optimization to enter the best wput routine directly. 8747 */ 8748 void 8749 ip_setqinfo(queue_t *q, minor_t minor, boolean_t bump_mib) 8750 { 8751 ASSERT(q->q_flag & QREADR); 8752 ASSERT(WR(q)->q_next == NULL); 8753 ASSERT(q->q_ptr != NULL); 8754 8755 if (minor == IPV6_MINOR) { 8756 if (bump_mib) 8757 BUMP_MIB(&ip6_mib, ipv6OutSwitchIPv4); 8758 q->q_qinfo = &rinit_ipv6; 8759 WR(q)->q_qinfo = &winit_ipv6; 8760 (Q_TO_CONN(q))->conn_pkt_isv6 = B_TRUE; 8761 } else { 8762 if (bump_mib) 8763 BUMP_MIB(&ip_mib, ipOutSwitchIPv6); 8764 q->q_qinfo = &rinit; 8765 WR(q)->q_qinfo = &winit; 8766 (Q_TO_CONN(q))->conn_pkt_isv6 = B_FALSE; 8767 } 8768 8769 } 8770 8771 /* 8772 * See if IPsec needs loading because of the options in mp. 8773 */ 8774 static boolean_t 8775 ipsec_opt_present(mblk_t *mp) 8776 { 8777 uint8_t *optcp, *next_optcp, *opt_endcp; 8778 struct opthdr *opt; 8779 struct T_opthdr *topt; 8780 int opthdr_len; 8781 t_uscalar_t optname, optlevel; 8782 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; 8783 ipsec_req_t *ipsr; 8784 8785 /* 8786 * Walk through the mess, and find IP_SEC_OPT. If it's there, 8787 * return TRUE. 8788 */ 8789 8790 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); 8791 opt_endcp = optcp + tor->OPT_length; 8792 if (tor->PRIM_type == T_OPTMGMT_REQ) { 8793 opthdr_len = sizeof (struct T_opthdr); 8794 } else { /* O_OPTMGMT_REQ */ 8795 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); 8796 opthdr_len = sizeof (struct opthdr); 8797 } 8798 for (; optcp < opt_endcp; optcp = next_optcp) { 8799 if (optcp + opthdr_len > opt_endcp) 8800 return (B_FALSE); /* Not enough option header. */ 8801 if (tor->PRIM_type == T_OPTMGMT_REQ) { 8802 topt = (struct T_opthdr *)optcp; 8803 optlevel = topt->level; 8804 optname = topt->name; 8805 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); 8806 } else { 8807 opt = (struct opthdr *)optcp; 8808 optlevel = opt->level; 8809 optname = opt->name; 8810 next_optcp = optcp + opthdr_len + 8811 _TPI_ALIGN_OPT(opt->len); 8812 } 8813 if ((next_optcp < optcp) || /* wraparound pointer space */ 8814 ((next_optcp >= opt_endcp) && /* last option bad len */ 8815 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) 8816 return (B_FALSE); /* bad option buffer */ 8817 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || 8818 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { 8819 /* 8820 * Check to see if it's an all-bypass or all-zeroes 8821 * IPsec request. Don't bother loading IPsec if 8822 * the socket doesn't want to use it. (A good example 8823 * is a bypass request.) 8824 * 8825 * Basically, if any of the non-NEVER bits are set, 8826 * load IPsec. 8827 */ 8828 ipsr = (ipsec_req_t *)(optcp + opthdr_len); 8829 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || 8830 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || 8831 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) 8832 != 0) 8833 return (B_TRUE); 8834 } 8835 } 8836 return (B_FALSE); 8837 } 8838 8839 /* 8840 * If conn is is waiting for ipsec to finish loading, kick it. 8841 */ 8842 /* ARGSUSED */ 8843 static void 8844 conn_restart_ipsec_waiter(conn_t *connp, void *arg) 8845 { 8846 t_scalar_t optreq_prim; 8847 mblk_t *mp; 8848 cred_t *cr; 8849 int err = 0; 8850 8851 /* 8852 * This function is called, after ipsec loading is complete. 8853 * Since IP checks exclusively and atomically (i.e it prevents 8854 * ipsec load from completing until ip_optcom_req completes) 8855 * whether ipsec load is complete, there cannot be a race with IP 8856 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. 8857 */ 8858 mutex_enter(&connp->conn_lock); 8859 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { 8860 ASSERT(connp->conn_ipsec_opt_mp != NULL); 8861 mp = connp->conn_ipsec_opt_mp; 8862 connp->conn_ipsec_opt_mp = NULL; 8863 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; 8864 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); 8865 mutex_exit(&connp->conn_lock); 8866 8867 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 8868 8869 optreq_prim = ((union T_primitives *)mp->b_rptr)->type; 8870 if (optreq_prim == T_OPTMGMT_REQ) { 8871 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, 8872 &ip_opt_obj); 8873 } else { 8874 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); 8875 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, 8876 &ip_opt_obj); 8877 } 8878 if (err != EINPROGRESS) 8879 CONN_OPER_PENDING_DONE(connp); 8880 return; 8881 } 8882 mutex_exit(&connp->conn_lock); 8883 } 8884 8885 /* 8886 * Called from the ipsec_loader thread, outside any perimeter, to tell 8887 * ip qenable any of the queues waiting for the ipsec loader to 8888 * complete. 8889 * 8890 * Use ip_mi_lock to be safe here: all modifications of the mi lists 8891 * are done with this lock held, so it's guaranteed that none of the 8892 * links will change along the way. 8893 */ 8894 void 8895 ip_ipsec_load_complete() 8896 { 8897 ipcl_walk(conn_restart_ipsec_waiter, NULL); 8898 } 8899 8900 /* 8901 * Can't be used. Need to call svr4* -> optset directly. the leaf routine 8902 * determines the grp on which it has to become exclusive, queues the mp 8903 * and sq draining restarts the optmgmt 8904 */ 8905 static boolean_t 8906 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) 8907 { 8908 conn_t *connp; 8909 8910 /* 8911 * Take IPsec requests and treat them special. 8912 */ 8913 if (ipsec_opt_present(mp)) { 8914 /* First check if IPsec is loaded. */ 8915 mutex_enter(&ipsec_loader_lock); 8916 if (ipsec_loader_state != IPSEC_LOADER_WAIT) { 8917 mutex_exit(&ipsec_loader_lock); 8918 return (B_FALSE); 8919 } 8920 connp = Q_TO_CONN(q); 8921 mutex_enter(&connp->conn_lock); 8922 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; 8923 8924 ASSERT(connp->conn_ipsec_opt_mp == NULL); 8925 connp->conn_ipsec_opt_mp = mp; 8926 mutex_exit(&connp->conn_lock); 8927 mutex_exit(&ipsec_loader_lock); 8928 8929 ipsec_loader_loadnow(); 8930 return (B_TRUE); 8931 } 8932 return (B_FALSE); 8933 } 8934 8935 /* 8936 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 8937 * all of them are copied to the conn_t. If the req is "zero", the policy is 8938 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 8939 * fields. 8940 * We keep only the latest setting of the policy and thus policy setting 8941 * is not incremental/cumulative. 8942 * 8943 * Requests to set policies with multiple alternative actions will 8944 * go through a different API. 8945 */ 8946 int 8947 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 8948 { 8949 uint_t ah_req = 0; 8950 uint_t esp_req = 0; 8951 uint_t se_req = 0; 8952 ipsec_selkey_t sel; 8953 ipsec_act_t *actp = NULL; 8954 uint_t nact; 8955 ipsec_policy_t *pin4 = NULL, *pout4 = NULL; 8956 ipsec_policy_t *pin6 = NULL, *pout6 = NULL; 8957 ipsec_policy_root_t *pr; 8958 ipsec_policy_head_t *ph; 8959 int fam; 8960 boolean_t is_pol_reset; 8961 int error = 0; 8962 8963 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 8964 8965 /* 8966 * The IP_SEC_OPT option does not allow variable length parameters, 8967 * hence a request cannot be NULL. 8968 */ 8969 if (req == NULL) 8970 return (EINVAL); 8971 8972 ah_req = req->ipsr_ah_req; 8973 esp_req = req->ipsr_esp_req; 8974 se_req = req->ipsr_self_encap_req; 8975 8976 /* 8977 * Are we dealing with a request to reset the policy (i.e. 8978 * zero requests). 8979 */ 8980 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 8981 (esp_req & REQ_MASK) == 0 && 8982 (se_req & REQ_MASK) == 0); 8983 8984 if (!is_pol_reset) { 8985 /* 8986 * If we couldn't load IPsec, fail with "protocol 8987 * not supported". 8988 * IPsec may not have been loaded for a request with zero 8989 * policies, so we don't fail in this case. 8990 */ 8991 mutex_enter(&ipsec_loader_lock); 8992 if (ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 8993 mutex_exit(&ipsec_loader_lock); 8994 return (EPROTONOSUPPORT); 8995 } 8996 mutex_exit(&ipsec_loader_lock); 8997 8998 /* 8999 * Test for valid requests. Invalid algorithms 9000 * need to be tested by IPSEC code because new 9001 * algorithms can be added dynamically. 9002 */ 9003 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 9004 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 9005 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 9006 return (EINVAL); 9007 } 9008 9009 /* 9010 * Only privileged users can issue these 9011 * requests. 9012 */ 9013 if (((ah_req & IPSEC_PREF_NEVER) || 9014 (esp_req & IPSEC_PREF_NEVER) || 9015 (se_req & IPSEC_PREF_NEVER)) && 9016 secpolicy_net_config(cr, B_FALSE) != 0) { 9017 return (EPERM); 9018 } 9019 9020 /* 9021 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 9022 * are mutually exclusive. 9023 */ 9024 if (((ah_req & REQ_MASK) == REQ_MASK) || 9025 ((esp_req & REQ_MASK) == REQ_MASK) || 9026 ((se_req & REQ_MASK) == REQ_MASK)) { 9027 /* Both of them are set */ 9028 return (EINVAL); 9029 } 9030 } 9031 9032 mutex_enter(&connp->conn_lock); 9033 9034 /* 9035 * If we have already cached policies in ip_bind_connected*(), don't 9036 * let them change now. We cache policies for connections 9037 * whose src,dst [addr, port] is known. The exception to this is 9038 * tunnels. Tunnels are allowed to change policies after having 9039 * become fully bound. 9040 */ 9041 if (connp->conn_policy_cached && !IPCL_IS_IPTUN(connp)) { 9042 mutex_exit(&connp->conn_lock); 9043 return (EINVAL); 9044 } 9045 9046 /* 9047 * We have a zero policies, reset the connection policy if already 9048 * set. This will cause the connection to inherit the 9049 * global policy, if any. 9050 */ 9051 if (is_pol_reset) { 9052 if (connp->conn_policy != NULL) { 9053 IPPH_REFRELE(connp->conn_policy); 9054 connp->conn_policy = NULL; 9055 } 9056 connp->conn_flags &= ~IPCL_CHECK_POLICY; 9057 connp->conn_in_enforce_policy = B_FALSE; 9058 connp->conn_out_enforce_policy = B_FALSE; 9059 mutex_exit(&connp->conn_lock); 9060 return (0); 9061 } 9062 9063 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy); 9064 if (ph == NULL) 9065 goto enomem; 9066 9067 ipsec_actvec_from_req(req, &actp, &nact); 9068 if (actp == NULL) 9069 goto enomem; 9070 9071 /* 9072 * Always allocate IPv4 policy entries, since they can also 9073 * apply to ipv6 sockets being used in ipv4-compat mode. 9074 */ 9075 bzero(&sel, sizeof (sel)); 9076 sel.ipsl_valid = IPSL_IPV4; 9077 9078 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET); 9079 if (pin4 == NULL) 9080 goto enomem; 9081 9082 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET); 9083 if (pout4 == NULL) 9084 goto enomem; 9085 9086 if (connp->conn_pkt_isv6) { 9087 /* 9088 * We're looking at a v6 socket, also allocate the 9089 * v6-specific entries... 9090 */ 9091 sel.ipsl_valid = IPSL_IPV6; 9092 pin6 = ipsec_policy_create(&sel, actp, nact, 9093 IPSEC_PRIO_SOCKET); 9094 if (pin6 == NULL) 9095 goto enomem; 9096 9097 pout6 = ipsec_policy_create(&sel, actp, nact, 9098 IPSEC_PRIO_SOCKET); 9099 if (pout6 == NULL) 9100 goto enomem; 9101 9102 /* 9103 * .. and file them away in the right place. 9104 */ 9105 fam = IPSEC_AF_V6; 9106 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 9107 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); 9108 ipsec_insert_always(&ph->iph_rulebyid, pin6); 9109 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 9110 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); 9111 ipsec_insert_always(&ph->iph_rulebyid, pout6); 9112 } 9113 9114 ipsec_actvec_free(actp, nact); 9115 9116 /* 9117 * File the v4 policies. 9118 */ 9119 fam = IPSEC_AF_V4; 9120 pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; 9121 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); 9122 ipsec_insert_always(&ph->iph_rulebyid, pin4); 9123 9124 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; 9125 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); 9126 ipsec_insert_always(&ph->iph_rulebyid, pout4); 9127 9128 /* 9129 * If the requests need security, set enforce_policy. 9130 * If the requests are IPSEC_PREF_NEVER, one should 9131 * still set conn_out_enforce_policy so that an ipsec_out 9132 * gets attached in ip_wput. This is needed so that 9133 * for connections that we don't cache policy in ip_bind, 9134 * if global policy matches in ip_wput_attach_policy, we 9135 * don't wrongly inherit global policy. Similarly, we need 9136 * to set conn_in_enforce_policy also so that we don't verify 9137 * policy wrongly. 9138 */ 9139 if ((ah_req & REQ_MASK) != 0 || 9140 (esp_req & REQ_MASK) != 0 || 9141 (se_req & REQ_MASK) != 0) { 9142 connp->conn_in_enforce_policy = B_TRUE; 9143 connp->conn_out_enforce_policy = B_TRUE; 9144 connp->conn_flags |= IPCL_CHECK_POLICY; 9145 } 9146 9147 /* 9148 * Tunnels are allowed to set policy after having been fully bound. 9149 * If that's the case, cache policy here. 9150 */ 9151 if (IPCL_IS_IPTUN(connp) && connp->conn_fully_bound) 9152 error = ipsec_conn_cache_policy(connp, !connp->conn_af_isv6); 9153 9154 mutex_exit(&connp->conn_lock); 9155 return (error); 9156 #undef REQ_MASK 9157 9158 /* 9159 * Common memory-allocation-failure exit path. 9160 */ 9161 enomem: 9162 mutex_exit(&connp->conn_lock); 9163 if (actp != NULL) 9164 ipsec_actvec_free(actp, nact); 9165 if (pin4 != NULL) 9166 IPPOL_REFRELE(pin4); 9167 if (pout4 != NULL) 9168 IPPOL_REFRELE(pout4); 9169 if (pin6 != NULL) 9170 IPPOL_REFRELE(pin6); 9171 if (pout6 != NULL) 9172 IPPOL_REFRELE(pout6); 9173 return (ENOMEM); 9174 } 9175 9176 /* 9177 * Only for options that pass in an IP addr. Currently only V4 options 9178 * pass in an ipif. V6 options always pass an ifindex specifying the ill. 9179 * So this function assumes level is IPPROTO_IP 9180 */ 9181 int 9182 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, 9183 mblk_t *first_mp) 9184 { 9185 ipif_t *ipif = NULL; 9186 int error; 9187 ill_t *ill; 9188 9189 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); 9190 9191 if (addr != INADDR_ANY || checkonly) { 9192 ASSERT(connp != NULL); 9193 if (option == IP_NEXTHOP) { 9194 ipif = 9195 ipif_lookup_onlink_addr(addr, connp->conn_zoneid); 9196 } else { 9197 ipif = ipif_lookup_addr(addr, NULL, connp->conn_zoneid, 9198 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, 9199 &error); 9200 } 9201 if (ipif == NULL) { 9202 if (error == EINPROGRESS) 9203 return (error); 9204 else if ((option == IP_MULTICAST_IF) || 9205 (option == IP_NEXTHOP)) 9206 return (EHOSTUNREACH); 9207 else 9208 return (EINVAL); 9209 } else if (checkonly) { 9210 if (option == IP_MULTICAST_IF) { 9211 ill = ipif->ipif_ill; 9212 /* not supported by the virtual network iface */ 9213 if (IS_VNI(ill)) { 9214 ipif_refrele(ipif); 9215 return (EINVAL); 9216 } 9217 } 9218 ipif_refrele(ipif); 9219 return (0); 9220 } 9221 ill = ipif->ipif_ill; 9222 mutex_enter(&connp->conn_lock); 9223 mutex_enter(&ill->ill_lock); 9224 if ((ill->ill_state_flags & ILL_CONDEMNED) || 9225 (ipif->ipif_state_flags & IPIF_CONDEMNED)) { 9226 mutex_exit(&ill->ill_lock); 9227 mutex_exit(&connp->conn_lock); 9228 ipif_refrele(ipif); 9229 return (option == IP_MULTICAST_IF ? 9230 EHOSTUNREACH : EINVAL); 9231 } 9232 } else { 9233 mutex_enter(&connp->conn_lock); 9234 } 9235 9236 /* None of the options below are supported on the VNI */ 9237 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { 9238 mutex_exit(&ill->ill_lock); 9239 mutex_exit(&connp->conn_lock); 9240 ipif_refrele(ipif); 9241 return (EINVAL); 9242 } 9243 9244 switch (option) { 9245 case IP_DONTFAILOVER_IF: 9246 /* 9247 * This option is used by in.mpathd to ensure 9248 * that IPMP probe packets only go out on the 9249 * test interfaces. in.mpathd sets this option 9250 * on the non-failover interfaces. 9251 * For backward compatibility, this option 9252 * implicitly sets IP_MULTICAST_IF, as used 9253 * be done in bind(), so that ip_wput gets 9254 * this ipif to send mcast packets. 9255 */ 9256 if (ipif != NULL) { 9257 ASSERT(addr != INADDR_ANY); 9258 connp->conn_nofailover_ill = ipif->ipif_ill; 9259 connp->conn_multicast_ipif = ipif; 9260 } else { 9261 ASSERT(addr == INADDR_ANY); 9262 connp->conn_nofailover_ill = NULL; 9263 connp->conn_multicast_ipif = NULL; 9264 } 9265 break; 9266 9267 case IP_MULTICAST_IF: 9268 connp->conn_multicast_ipif = ipif; 9269 break; 9270 case IP_NEXTHOP: 9271 connp->conn_nexthop_v4 = addr; 9272 connp->conn_nexthop_set = B_TRUE; 9273 break; 9274 } 9275 9276 if (ipif != NULL) { 9277 mutex_exit(&ill->ill_lock); 9278 mutex_exit(&connp->conn_lock); 9279 ipif_refrele(ipif); 9280 return (0); 9281 } 9282 mutex_exit(&connp->conn_lock); 9283 /* We succeded in cleared the option */ 9284 return (0); 9285 } 9286 9287 /* 9288 * For options that pass in an ifindex specifying the ill. V6 options always 9289 * pass in an ill. Some v4 options also pass in ifindex specifying the ill. 9290 */ 9291 int 9292 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, 9293 int level, int option, mblk_t *first_mp) 9294 { 9295 ill_t *ill = NULL; 9296 int error = 0; 9297 9298 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); 9299 if (ifindex != 0) { 9300 ASSERT(connp != NULL); 9301 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), 9302 first_mp, ip_restart_optmgmt, &error); 9303 if (ill != NULL) { 9304 if (checkonly) { 9305 /* not supported by the virtual network iface */ 9306 if (IS_VNI(ill)) { 9307 ill_refrele(ill); 9308 return (EINVAL); 9309 } 9310 ill_refrele(ill); 9311 return (0); 9312 } 9313 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 9314 0, NULL)) { 9315 ill_refrele(ill); 9316 ill = NULL; 9317 mutex_enter(&connp->conn_lock); 9318 goto setit; 9319 } 9320 mutex_enter(&connp->conn_lock); 9321 mutex_enter(&ill->ill_lock); 9322 if (ill->ill_state_flags & ILL_CONDEMNED) { 9323 mutex_exit(&ill->ill_lock); 9324 mutex_exit(&connp->conn_lock); 9325 ill_refrele(ill); 9326 ill = NULL; 9327 mutex_enter(&connp->conn_lock); 9328 } 9329 goto setit; 9330 } else if (error == EINPROGRESS) { 9331 return (error); 9332 } else { 9333 error = 0; 9334 } 9335 } 9336 mutex_enter(&connp->conn_lock); 9337 setit: 9338 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); 9339 9340 /* 9341 * The options below assume that the ILL (if any) transmits and/or 9342 * receives traffic. Neither of which is true for the virtual network 9343 * interface, so fail setting these on a VNI. 9344 */ 9345 if (IS_VNI(ill)) { 9346 ASSERT(ill != NULL); 9347 mutex_exit(&ill->ill_lock); 9348 mutex_exit(&connp->conn_lock); 9349 ill_refrele(ill); 9350 return (EINVAL); 9351 } 9352 9353 if (level == IPPROTO_IP) { 9354 switch (option) { 9355 case IP_BOUND_IF: 9356 connp->conn_incoming_ill = ill; 9357 connp->conn_outgoing_ill = ill; 9358 connp->conn_orig_bound_ifindex = (ill == NULL) ? 9359 0 : ifindex; 9360 break; 9361 9362 case IP_XMIT_IF: 9363 /* 9364 * Similar to IP_BOUND_IF, but this only 9365 * determines the outgoing interface for 9366 * unicast packets. Also no IRE_CACHE entry 9367 * is added for the destination of the 9368 * outgoing packets. This feature is needed 9369 * for mobile IP. 9370 */ 9371 connp->conn_xmit_if_ill = ill; 9372 connp->conn_orig_xmit_ifindex = (ill == NULL) ? 9373 0 : ifindex; 9374 break; 9375 9376 case IP_MULTICAST_IF: 9377 /* 9378 * This option is an internal special. The socket 9379 * level IP_MULTICAST_IF specifies an 'ipaddr' and 9380 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF 9381 * specifies an ifindex and we try first on V6 ill's. 9382 * If we don't find one, we they try using on v4 ill's 9383 * intenally and we come here. 9384 */ 9385 if (!checkonly && ill != NULL) { 9386 ipif_t *ipif; 9387 ipif = ill->ill_ipif; 9388 9389 if (ipif->ipif_state_flags & IPIF_CONDEMNED) { 9390 mutex_exit(&ill->ill_lock); 9391 mutex_exit(&connp->conn_lock); 9392 ill_refrele(ill); 9393 ill = NULL; 9394 mutex_enter(&connp->conn_lock); 9395 } else { 9396 connp->conn_multicast_ipif = ipif; 9397 } 9398 } 9399 break; 9400 } 9401 } else { 9402 switch (option) { 9403 case IPV6_BOUND_IF: 9404 connp->conn_incoming_ill = ill; 9405 connp->conn_outgoing_ill = ill; 9406 connp->conn_orig_bound_ifindex = (ill == NULL) ? 9407 0 : ifindex; 9408 break; 9409 9410 case IPV6_BOUND_PIF: 9411 /* 9412 * Limit all transmit to this ill. 9413 * Unlike IPV6_BOUND_IF, using this option 9414 * prevents load spreading and failover from 9415 * happening when the interface is part of the 9416 * group. That's why we don't need to remember 9417 * the ifindex in orig_bound_ifindex as in 9418 * IPV6_BOUND_IF. 9419 */ 9420 connp->conn_outgoing_pill = ill; 9421 break; 9422 9423 case IPV6_DONTFAILOVER_IF: 9424 /* 9425 * This option is used by in.mpathd to ensure 9426 * that IPMP probe packets only go out on the 9427 * test interfaces. in.mpathd sets this option 9428 * on the non-failover interfaces. 9429 */ 9430 connp->conn_nofailover_ill = ill; 9431 /* 9432 * For backward compatibility, this option 9433 * implicitly sets ip_multicast_ill as used in 9434 * IP_MULTICAST_IF so that ip_wput gets 9435 * this ipif to send mcast packets. 9436 */ 9437 connp->conn_multicast_ill = ill; 9438 connp->conn_orig_multicast_ifindex = (ill == NULL) ? 9439 0 : ifindex; 9440 break; 9441 9442 case IPV6_MULTICAST_IF: 9443 /* 9444 * Set conn_multicast_ill to be the IPv6 ill. 9445 * Set conn_multicast_ipif to be an IPv4 ipif 9446 * for ifindex to make IPv4 mapped addresses 9447 * on PF_INET6 sockets honor IPV6_MULTICAST_IF. 9448 * Even if no IPv6 ill exists for the ifindex 9449 * we need to check for an IPv4 ifindex in order 9450 * for this to work with mapped addresses. In that 9451 * case only set conn_multicast_ipif. 9452 */ 9453 if (!checkonly) { 9454 if (ifindex == 0) { 9455 connp->conn_multicast_ill = NULL; 9456 connp->conn_orig_multicast_ifindex = 0; 9457 connp->conn_multicast_ipif = NULL; 9458 } else if (ill != NULL) { 9459 connp->conn_multicast_ill = ill; 9460 connp->conn_orig_multicast_ifindex = 9461 ifindex; 9462 } 9463 } 9464 break; 9465 } 9466 } 9467 9468 if (ill != NULL) { 9469 mutex_exit(&ill->ill_lock); 9470 mutex_exit(&connp->conn_lock); 9471 ill_refrele(ill); 9472 return (0); 9473 } 9474 mutex_exit(&connp->conn_lock); 9475 /* 9476 * We succeeded in clearing the option (ifindex == 0) or failed to 9477 * locate the ill and could not set the option (ifindex != 0) 9478 */ 9479 return (ifindex == 0 ? 0 : EINVAL); 9480 } 9481 9482 /* This routine sets socket options. */ 9483 /* ARGSUSED */ 9484 int 9485 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, 9486 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9487 void *dummy, cred_t *cr, mblk_t *first_mp) 9488 { 9489 int *i1 = (int *)invalp; 9490 conn_t *connp = Q_TO_CONN(q); 9491 int error = 0; 9492 boolean_t checkonly; 9493 ire_t *ire; 9494 boolean_t found; 9495 9496 switch (optset_context) { 9497 9498 case SETFN_OPTCOM_CHECKONLY: 9499 checkonly = B_TRUE; 9500 /* 9501 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9502 * inlen != 0 implies value supplied and 9503 * we have to "pretend" to set it. 9504 * inlen == 0 implies that there is no 9505 * value part in T_CHECK request and just validation 9506 * done elsewhere should be enough, we just return here. 9507 */ 9508 if (inlen == 0) { 9509 *outlenp = 0; 9510 return (0); 9511 } 9512 break; 9513 case SETFN_OPTCOM_NEGOTIATE: 9514 case SETFN_UD_NEGOTIATE: 9515 case SETFN_CONN_NEGOTIATE: 9516 checkonly = B_FALSE; 9517 break; 9518 default: 9519 /* 9520 * We should never get here 9521 */ 9522 *outlenp = 0; 9523 return (EINVAL); 9524 } 9525 9526 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9527 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9528 9529 /* 9530 * For fixed length options, no sanity check 9531 * of passed in length is done. It is assumed *_optcom_req() 9532 * routines do the right thing. 9533 */ 9534 9535 switch (level) { 9536 case SOL_SOCKET: 9537 /* 9538 * conn_lock protects the bitfields, and is used to 9539 * set the fields atomically. 9540 */ 9541 switch (name) { 9542 case SO_BROADCAST: 9543 if (!checkonly) { 9544 /* TODO: use value someplace? */ 9545 mutex_enter(&connp->conn_lock); 9546 connp->conn_broadcast = *i1 ? 1 : 0; 9547 mutex_exit(&connp->conn_lock); 9548 } 9549 break; /* goto sizeof (int) option return */ 9550 case SO_USELOOPBACK: 9551 if (!checkonly) { 9552 /* TODO: use value someplace? */ 9553 mutex_enter(&connp->conn_lock); 9554 connp->conn_loopback = *i1 ? 1 : 0; 9555 mutex_exit(&connp->conn_lock); 9556 } 9557 break; /* goto sizeof (int) option return */ 9558 case SO_DONTROUTE: 9559 if (!checkonly) { 9560 mutex_enter(&connp->conn_lock); 9561 connp->conn_dontroute = *i1 ? 1 : 0; 9562 mutex_exit(&connp->conn_lock); 9563 } 9564 break; /* goto sizeof (int) option return */ 9565 case SO_REUSEADDR: 9566 if (!checkonly) { 9567 mutex_enter(&connp->conn_lock); 9568 connp->conn_reuseaddr = *i1 ? 1 : 0; 9569 mutex_exit(&connp->conn_lock); 9570 } 9571 break; /* goto sizeof (int) option return */ 9572 case SO_PROTOTYPE: 9573 if (!checkonly) { 9574 mutex_enter(&connp->conn_lock); 9575 connp->conn_proto = *i1; 9576 mutex_exit(&connp->conn_lock); 9577 } 9578 break; /* goto sizeof (int) option return */ 9579 default: 9580 /* 9581 * "soft" error (negative) 9582 * option not handled at this level 9583 * Note: Do not modify *outlenp 9584 */ 9585 return (-EINVAL); 9586 } 9587 break; 9588 case IPPROTO_IP: 9589 switch (name) { 9590 case IP_NEXTHOP: 9591 case IP_MULTICAST_IF: 9592 case IP_DONTFAILOVER_IF: { 9593 ipaddr_t addr = *i1; 9594 9595 error = ip_opt_set_ipif(connp, addr, checkonly, name, 9596 first_mp); 9597 if (error != 0) 9598 return (error); 9599 break; /* goto sizeof (int) option return */ 9600 } 9601 9602 case IP_MULTICAST_TTL: 9603 /* Recorded in transport above IP */ 9604 *outvalp = *invalp; 9605 *outlenp = sizeof (uchar_t); 9606 return (0); 9607 case IP_MULTICAST_LOOP: 9608 if (!checkonly) { 9609 mutex_enter(&connp->conn_lock); 9610 connp->conn_multicast_loop = *invalp ? 1 : 0; 9611 mutex_exit(&connp->conn_lock); 9612 } 9613 *outvalp = *invalp; 9614 *outlenp = sizeof (uchar_t); 9615 return (0); 9616 case IP_ADD_MEMBERSHIP: 9617 case MCAST_JOIN_GROUP: 9618 case IP_DROP_MEMBERSHIP: 9619 case MCAST_LEAVE_GROUP: { 9620 struct ip_mreq *mreqp; 9621 struct group_req *greqp; 9622 ire_t *ire; 9623 boolean_t done = B_FALSE; 9624 ipaddr_t group, ifaddr; 9625 struct sockaddr_in *sin; 9626 uint32_t *ifindexp; 9627 boolean_t mcast_opt = B_TRUE; 9628 mcast_record_t fmode; 9629 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 9630 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 9631 9632 switch (name) { 9633 case IP_ADD_MEMBERSHIP: 9634 mcast_opt = B_FALSE; 9635 /* FALLTHRU */ 9636 case MCAST_JOIN_GROUP: 9637 fmode = MODE_IS_EXCLUDE; 9638 optfn = ip_opt_add_group; 9639 break; 9640 9641 case IP_DROP_MEMBERSHIP: 9642 mcast_opt = B_FALSE; 9643 /* FALLTHRU */ 9644 case MCAST_LEAVE_GROUP: 9645 fmode = MODE_IS_INCLUDE; 9646 optfn = ip_opt_delete_group; 9647 break; 9648 } 9649 9650 if (mcast_opt) { 9651 greqp = (struct group_req *)i1; 9652 sin = (struct sockaddr_in *)&greqp->gr_group; 9653 if (sin->sin_family != AF_INET) { 9654 *outlenp = 0; 9655 return (ENOPROTOOPT); 9656 } 9657 group = (ipaddr_t)sin->sin_addr.s_addr; 9658 ifaddr = INADDR_ANY; 9659 ifindexp = &greqp->gr_interface; 9660 } else { 9661 mreqp = (struct ip_mreq *)i1; 9662 group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; 9663 ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; 9664 ifindexp = NULL; 9665 } 9666 9667 /* 9668 * In the multirouting case, we need to replicate 9669 * the request on all interfaces that will take part 9670 * in replication. We do so because multirouting is 9671 * reflective, thus we will probably receive multi- 9672 * casts on those interfaces. 9673 * The ip_multirt_apply_membership() succeeds if the 9674 * operation succeeds on at least one interface. 9675 */ 9676 ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, 9677 IRE_HOST, NULL, NULL, ALL_ZONES, 0, 9678 MATCH_IRE_MASK | MATCH_IRE_TYPE); 9679 if (ire != NULL) { 9680 if (ire->ire_flags & RTF_MULTIRT) { 9681 error = ip_multirt_apply_membership( 9682 optfn, ire, connp, checkonly, group, 9683 fmode, INADDR_ANY, first_mp); 9684 done = B_TRUE; 9685 } 9686 ire_refrele(ire); 9687 } 9688 if (!done) { 9689 error = optfn(connp, checkonly, group, ifaddr, 9690 ifindexp, fmode, INADDR_ANY, first_mp); 9691 } 9692 if (error) { 9693 /* 9694 * EINPROGRESS is a soft error, needs retry 9695 * so don't make *outlenp zero. 9696 */ 9697 if (error != EINPROGRESS) 9698 *outlenp = 0; 9699 return (error); 9700 } 9701 /* OK return - copy input buffer into output buffer */ 9702 if (invalp != outvalp) { 9703 /* don't trust bcopy for identical src/dst */ 9704 bcopy(invalp, outvalp, inlen); 9705 } 9706 *outlenp = inlen; 9707 return (0); 9708 } 9709 case IP_BLOCK_SOURCE: 9710 case IP_UNBLOCK_SOURCE: 9711 case IP_ADD_SOURCE_MEMBERSHIP: 9712 case IP_DROP_SOURCE_MEMBERSHIP: 9713 case MCAST_BLOCK_SOURCE: 9714 case MCAST_UNBLOCK_SOURCE: 9715 case MCAST_JOIN_SOURCE_GROUP: 9716 case MCAST_LEAVE_SOURCE_GROUP: { 9717 struct ip_mreq_source *imreqp; 9718 struct group_source_req *gsreqp; 9719 in_addr_t grp, src, ifaddr = INADDR_ANY; 9720 uint32_t ifindex = 0; 9721 mcast_record_t fmode; 9722 struct sockaddr_in *sin; 9723 ire_t *ire; 9724 boolean_t mcast_opt = B_TRUE, done = B_FALSE; 9725 int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 9726 uint_t *, mcast_record_t, ipaddr_t, mblk_t *); 9727 9728 switch (name) { 9729 case IP_BLOCK_SOURCE: 9730 mcast_opt = B_FALSE; 9731 /* FALLTHRU */ 9732 case MCAST_BLOCK_SOURCE: 9733 fmode = MODE_IS_EXCLUDE; 9734 optfn = ip_opt_add_group; 9735 break; 9736 9737 case IP_UNBLOCK_SOURCE: 9738 mcast_opt = B_FALSE; 9739 /* FALLTHRU */ 9740 case MCAST_UNBLOCK_SOURCE: 9741 fmode = MODE_IS_EXCLUDE; 9742 optfn = ip_opt_delete_group; 9743 break; 9744 9745 case IP_ADD_SOURCE_MEMBERSHIP: 9746 mcast_opt = B_FALSE; 9747 /* FALLTHRU */ 9748 case MCAST_JOIN_SOURCE_GROUP: 9749 fmode = MODE_IS_INCLUDE; 9750 optfn = ip_opt_add_group; 9751 break; 9752 9753 case IP_DROP_SOURCE_MEMBERSHIP: 9754 mcast_opt = B_FALSE; 9755 /* FALLTHRU */ 9756 case MCAST_LEAVE_SOURCE_GROUP: 9757 fmode = MODE_IS_INCLUDE; 9758 optfn = ip_opt_delete_group; 9759 break; 9760 } 9761 9762 if (mcast_opt) { 9763 gsreqp = (struct group_source_req *)i1; 9764 if (gsreqp->gsr_group.ss_family != AF_INET) { 9765 *outlenp = 0; 9766 return (ENOPROTOOPT); 9767 } 9768 sin = (struct sockaddr_in *)&gsreqp->gsr_group; 9769 grp = (ipaddr_t)sin->sin_addr.s_addr; 9770 sin = (struct sockaddr_in *)&gsreqp->gsr_source; 9771 src = (ipaddr_t)sin->sin_addr.s_addr; 9772 ifindex = gsreqp->gsr_interface; 9773 } else { 9774 imreqp = (struct ip_mreq_source *)i1; 9775 grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; 9776 src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; 9777 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 9778 } 9779 9780 /* 9781 * In the multirouting case, we need to replicate 9782 * the request as noted in the mcast cases above. 9783 */ 9784 ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, 9785 IRE_HOST, NULL, NULL, ALL_ZONES, 0, 9786 MATCH_IRE_MASK | MATCH_IRE_TYPE); 9787 if (ire != NULL) { 9788 if (ire->ire_flags & RTF_MULTIRT) { 9789 error = ip_multirt_apply_membership( 9790 optfn, ire, connp, checkonly, grp, 9791 fmode, src, first_mp); 9792 done = B_TRUE; 9793 } 9794 ire_refrele(ire); 9795 } 9796 if (!done) { 9797 error = optfn(connp, checkonly, grp, ifaddr, 9798 &ifindex, fmode, src, first_mp); 9799 } 9800 if (error != 0) { 9801 /* 9802 * EINPROGRESS is a soft error, needs retry 9803 * so don't make *outlenp zero. 9804 */ 9805 if (error != EINPROGRESS) 9806 *outlenp = 0; 9807 return (error); 9808 } 9809 /* OK return - copy input buffer into output buffer */ 9810 if (invalp != outvalp) { 9811 bcopy(invalp, outvalp, inlen); 9812 } 9813 *outlenp = inlen; 9814 return (0); 9815 } 9816 case IP_SEC_OPT: 9817 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 9818 if (error != 0) { 9819 *outlenp = 0; 9820 return (error); 9821 } 9822 break; 9823 case IP_HDRINCL: 9824 case IP_OPTIONS: 9825 case T_IP_OPTIONS: 9826 case IP_TOS: 9827 case T_IP_TOS: 9828 case IP_TTL: 9829 case IP_RECVDSTADDR: 9830 case IP_RECVOPTS: 9831 /* OK return - copy input buffer into output buffer */ 9832 if (invalp != outvalp) { 9833 /* don't trust bcopy for identical src/dst */ 9834 bcopy(invalp, outvalp, inlen); 9835 } 9836 *outlenp = inlen; 9837 return (0); 9838 case IP_RECVIF: 9839 /* Retrieve the inbound interface index */ 9840 if (!checkonly) { 9841 mutex_enter(&connp->conn_lock); 9842 connp->conn_recvif = *i1 ? 1 : 0; 9843 mutex_exit(&connp->conn_lock); 9844 } 9845 break; /* goto sizeof (int) option return */ 9846 case IP_RECVSLLA: 9847 /* Retrieve the source link layer address */ 9848 if (!checkonly) { 9849 mutex_enter(&connp->conn_lock); 9850 connp->conn_recvslla = *i1 ? 1 : 0; 9851 mutex_exit(&connp->conn_lock); 9852 } 9853 break; /* goto sizeof (int) option return */ 9854 case MRT_INIT: 9855 case MRT_DONE: 9856 case MRT_ADD_VIF: 9857 case MRT_DEL_VIF: 9858 case MRT_ADD_MFC: 9859 case MRT_DEL_MFC: 9860 case MRT_ASSERT: 9861 if ((error = secpolicy_net_config(cr, B_FALSE)) != 0) { 9862 *outlenp = 0; 9863 return (error); 9864 } 9865 error = ip_mrouter_set((int)name, q, checkonly, 9866 (uchar_t *)invalp, inlen, first_mp); 9867 if (error) { 9868 *outlenp = 0; 9869 return (error); 9870 } 9871 /* OK return - copy input buffer into output buffer */ 9872 if (invalp != outvalp) { 9873 /* don't trust bcopy for identical src/dst */ 9874 bcopy(invalp, outvalp, inlen); 9875 } 9876 *outlenp = inlen; 9877 return (0); 9878 case IP_BOUND_IF: 9879 case IP_XMIT_IF: 9880 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 9881 level, name, first_mp); 9882 if (error != 0) 9883 return (error); 9884 break; /* goto sizeof (int) option return */ 9885 9886 case IP_UNSPEC_SRC: 9887 /* Allow sending with a zero source address */ 9888 if (!checkonly) { 9889 mutex_enter(&connp->conn_lock); 9890 connp->conn_unspec_src = *i1 ? 1 : 0; 9891 mutex_exit(&connp->conn_lock); 9892 } 9893 break; /* goto sizeof (int) option return */ 9894 default: 9895 /* 9896 * "soft" error (negative) 9897 * option not handled at this level 9898 * Note: Do not modify *outlenp 9899 */ 9900 return (-EINVAL); 9901 } 9902 break; 9903 case IPPROTO_IPV6: 9904 switch (name) { 9905 case IPV6_BOUND_IF: 9906 case IPV6_BOUND_PIF: 9907 case IPV6_DONTFAILOVER_IF: 9908 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 9909 level, name, first_mp); 9910 if (error != 0) 9911 return (error); 9912 break; /* goto sizeof (int) option return */ 9913 9914 case IPV6_MULTICAST_IF: 9915 /* 9916 * The only possible errors are EINPROGRESS and 9917 * EINVAL. EINPROGRESS will be restarted and is not 9918 * a hard error. We call this option on both V4 and V6 9919 * If both return EINVAL, then this call returns 9920 * EINVAL. If at least one of them succeeds we 9921 * return success. 9922 */ 9923 found = B_FALSE; 9924 error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, 9925 level, name, first_mp); 9926 if (error == EINPROGRESS) 9927 return (error); 9928 if (error == 0) 9929 found = B_TRUE; 9930 error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, 9931 IPPROTO_IP, IP_MULTICAST_IF, first_mp); 9932 if (error == 0) 9933 found = B_TRUE; 9934 if (!found) 9935 return (error); 9936 break; /* goto sizeof (int) option return */ 9937 9938 case IPV6_MULTICAST_HOPS: 9939 /* Recorded in transport above IP */ 9940 break; /* goto sizeof (int) option return */ 9941 case IPV6_MULTICAST_LOOP: 9942 if (!checkonly) { 9943 mutex_enter(&connp->conn_lock); 9944 connp->conn_multicast_loop = *i1; 9945 mutex_exit(&connp->conn_lock); 9946 } 9947 break; /* goto sizeof (int) option return */ 9948 case IPV6_JOIN_GROUP: 9949 case MCAST_JOIN_GROUP: 9950 case IPV6_LEAVE_GROUP: 9951 case MCAST_LEAVE_GROUP: { 9952 struct ipv6_mreq *ip_mreqp; 9953 struct group_req *greqp; 9954 ire_t *ire; 9955 boolean_t done = B_FALSE; 9956 in6_addr_t groupv6; 9957 uint32_t ifindex; 9958 boolean_t mcast_opt = B_TRUE; 9959 mcast_record_t fmode; 9960 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 9961 int, mcast_record_t, const in6_addr_t *, mblk_t *); 9962 9963 switch (name) { 9964 case IPV6_JOIN_GROUP: 9965 mcast_opt = B_FALSE; 9966 /* FALLTHRU */ 9967 case MCAST_JOIN_GROUP: 9968 fmode = MODE_IS_EXCLUDE; 9969 optfn = ip_opt_add_group_v6; 9970 break; 9971 9972 case IPV6_LEAVE_GROUP: 9973 mcast_opt = B_FALSE; 9974 /* FALLTHRU */ 9975 case MCAST_LEAVE_GROUP: 9976 fmode = MODE_IS_INCLUDE; 9977 optfn = ip_opt_delete_group_v6; 9978 break; 9979 } 9980 9981 if (mcast_opt) { 9982 struct sockaddr_in *sin; 9983 struct sockaddr_in6 *sin6; 9984 greqp = (struct group_req *)i1; 9985 if (greqp->gr_group.ss_family == AF_INET) { 9986 sin = (struct sockaddr_in *) 9987 &(greqp->gr_group); 9988 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, 9989 &groupv6); 9990 } else { 9991 sin6 = (struct sockaddr_in6 *) 9992 &(greqp->gr_group); 9993 groupv6 = sin6->sin6_addr; 9994 } 9995 ifindex = greqp->gr_interface; 9996 } else { 9997 ip_mreqp = (struct ipv6_mreq *)i1; 9998 groupv6 = ip_mreqp->ipv6mr_multiaddr; 9999 ifindex = ip_mreqp->ipv6mr_interface; 10000 } 10001 /* 10002 * In the multirouting case, we need to replicate 10003 * the request on all interfaces that will take part 10004 * in replication. We do so because multirouting is 10005 * reflective, thus we will probably receive multi- 10006 * casts on those interfaces. 10007 * The ip_multirt_apply_membership_v6() succeeds if 10008 * the operation succeeds on at least one interface. 10009 */ 10010 ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, 10011 IRE_HOST, NULL, NULL, ALL_ZONES, 0, 10012 MATCH_IRE_MASK | MATCH_IRE_TYPE); 10013 if (ire != NULL) { 10014 if (ire->ire_flags & RTF_MULTIRT) { 10015 error = ip_multirt_apply_membership_v6( 10016 optfn, ire, connp, checkonly, 10017 &groupv6, fmode, &ipv6_all_zeros, 10018 first_mp); 10019 done = B_TRUE; 10020 } 10021 ire_refrele(ire); 10022 } 10023 if (!done) { 10024 error = optfn(connp, checkonly, &groupv6, 10025 ifindex, fmode, &ipv6_all_zeros, first_mp); 10026 } 10027 if (error) { 10028 /* 10029 * EINPROGRESS is a soft error, needs retry 10030 * so don't make *outlenp zero. 10031 */ 10032 if (error != EINPROGRESS) 10033 *outlenp = 0; 10034 return (error); 10035 } 10036 /* OK return - copy input buffer into output buffer */ 10037 if (invalp != outvalp) { 10038 /* don't trust bcopy for identical src/dst */ 10039 bcopy(invalp, outvalp, inlen); 10040 } 10041 *outlenp = inlen; 10042 return (0); 10043 } 10044 case MCAST_BLOCK_SOURCE: 10045 case MCAST_UNBLOCK_SOURCE: 10046 case MCAST_JOIN_SOURCE_GROUP: 10047 case MCAST_LEAVE_SOURCE_GROUP: { 10048 struct group_source_req *gsreqp; 10049 in6_addr_t v6grp, v6src; 10050 uint32_t ifindex; 10051 mcast_record_t fmode; 10052 ire_t *ire; 10053 boolean_t done = B_FALSE; 10054 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 10055 int, mcast_record_t, const in6_addr_t *, mblk_t *); 10056 10057 switch (name) { 10058 case MCAST_BLOCK_SOURCE: 10059 fmode = MODE_IS_EXCLUDE; 10060 optfn = ip_opt_add_group_v6; 10061 break; 10062 case MCAST_UNBLOCK_SOURCE: 10063 fmode = MODE_IS_EXCLUDE; 10064 optfn = ip_opt_delete_group_v6; 10065 break; 10066 case MCAST_JOIN_SOURCE_GROUP: 10067 fmode = MODE_IS_INCLUDE; 10068 optfn = ip_opt_add_group_v6; 10069 break; 10070 case MCAST_LEAVE_SOURCE_GROUP: 10071 fmode = MODE_IS_INCLUDE; 10072 optfn = ip_opt_delete_group_v6; 10073 break; 10074 } 10075 10076 gsreqp = (struct group_source_req *)i1; 10077 ifindex = gsreqp->gsr_interface; 10078 if (gsreqp->gsr_group.ss_family == AF_INET) { 10079 struct sockaddr_in *s; 10080 s = (struct sockaddr_in *)&gsreqp->gsr_group; 10081 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); 10082 s = (struct sockaddr_in *)&gsreqp->gsr_source; 10083 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 10084 } else { 10085 struct sockaddr_in6 *s6; 10086 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 10087 v6grp = s6->sin6_addr; 10088 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 10089 v6src = s6->sin6_addr; 10090 } 10091 10092 /* 10093 * In the multirouting case, we need to replicate 10094 * the request as noted in the mcast cases above. 10095 */ 10096 ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, 10097 IRE_HOST, NULL, NULL, ALL_ZONES, 0, 10098 MATCH_IRE_MASK | MATCH_IRE_TYPE); 10099 if (ire != NULL) { 10100 if (ire->ire_flags & RTF_MULTIRT) { 10101 error = ip_multirt_apply_membership_v6( 10102 optfn, ire, connp, checkonly, 10103 &v6grp, fmode, &v6src, first_mp); 10104 done = B_TRUE; 10105 } 10106 ire_refrele(ire); 10107 } 10108 if (!done) { 10109 error = optfn(connp, checkonly, &v6grp, 10110 ifindex, fmode, &v6src, first_mp); 10111 } 10112 if (error != 0) { 10113 /* 10114 * EINPROGRESS is a soft error, needs retry 10115 * so don't make *outlenp zero. 10116 */ 10117 if (error != EINPROGRESS) 10118 *outlenp = 0; 10119 return (error); 10120 } 10121 /* OK return - copy input buffer into output buffer */ 10122 if (invalp != outvalp) { 10123 bcopy(invalp, outvalp, inlen); 10124 } 10125 *outlenp = inlen; 10126 return (0); 10127 } 10128 case IPV6_UNICAST_HOPS: 10129 /* Recorded in transport above IP */ 10130 break; /* goto sizeof (int) option return */ 10131 case IPV6_UNSPEC_SRC: 10132 /* Allow sending with a zero source address */ 10133 if (!checkonly) { 10134 mutex_enter(&connp->conn_lock); 10135 connp->conn_unspec_src = *i1 ? 1 : 0; 10136 mutex_exit(&connp->conn_lock); 10137 } 10138 break; /* goto sizeof (int) option return */ 10139 case IPV6_RECVPKTINFO: 10140 if (!checkonly) { 10141 mutex_enter(&connp->conn_lock); 10142 connp->conn_ipv6_recvpktinfo = *i1 ? 1 : 0; 10143 mutex_exit(&connp->conn_lock); 10144 } 10145 break; /* goto sizeof (int) option return */ 10146 case IPV6_RECVTCLASS: 10147 if (!checkonly) { 10148 if (*i1 < 0 || *i1 > 1) { 10149 return (EINVAL); 10150 } 10151 mutex_enter(&connp->conn_lock); 10152 connp->conn_ipv6_recvtclass = *i1; 10153 mutex_exit(&connp->conn_lock); 10154 } 10155 break; 10156 case IPV6_RECVPATHMTU: 10157 if (!checkonly) { 10158 if (*i1 < 0 || *i1 > 1) { 10159 return (EINVAL); 10160 } 10161 mutex_enter(&connp->conn_lock); 10162 connp->conn_ipv6_recvpathmtu = *i1; 10163 mutex_exit(&connp->conn_lock); 10164 } 10165 break; 10166 case IPV6_RECVHOPLIMIT: 10167 if (!checkonly) { 10168 mutex_enter(&connp->conn_lock); 10169 connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; 10170 mutex_exit(&connp->conn_lock); 10171 } 10172 break; /* goto sizeof (int) option return */ 10173 case IPV6_RECVHOPOPTS: 10174 if (!checkonly) { 10175 mutex_enter(&connp->conn_lock); 10176 connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; 10177 mutex_exit(&connp->conn_lock); 10178 } 10179 break; /* goto sizeof (int) option return */ 10180 case IPV6_RECVDSTOPTS: 10181 if (!checkonly) { 10182 mutex_enter(&connp->conn_lock); 10183 connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; 10184 mutex_exit(&connp->conn_lock); 10185 } 10186 break; /* goto sizeof (int) option return */ 10187 case IPV6_RECVRTHDR: 10188 if (!checkonly) { 10189 mutex_enter(&connp->conn_lock); 10190 connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; 10191 mutex_exit(&connp->conn_lock); 10192 } 10193 break; /* goto sizeof (int) option return */ 10194 case IPV6_RECVRTHDRDSTOPTS: 10195 if (!checkonly) { 10196 mutex_enter(&connp->conn_lock); 10197 connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; 10198 mutex_exit(&connp->conn_lock); 10199 } 10200 break; /* goto sizeof (int) option return */ 10201 case IPV6_PKTINFO: 10202 if (inlen == 0) 10203 return (-EINVAL); /* clearing option */ 10204 error = ip6_set_pktinfo(cr, connp, 10205 (struct in6_pktinfo *)invalp, first_mp); 10206 if (error != 0) 10207 *outlenp = 0; 10208 else 10209 *outlenp = inlen; 10210 return (error); 10211 case IPV6_NEXTHOP: { 10212 struct sockaddr_in6 *sin6; 10213 10214 /* Verify that the nexthop is reachable */ 10215 if (inlen == 0) 10216 return (-EINVAL); /* clearing option */ 10217 10218 sin6 = (struct sockaddr_in6 *)invalp; 10219 ire = ire_route_lookup_v6(&sin6->sin6_addr, 10220 0, 0, 0, NULL, NULL, connp->conn_zoneid, 10221 MATCH_IRE_DEFAULT); 10222 10223 if (ire == NULL) { 10224 *outlenp = 0; 10225 return (EHOSTUNREACH); 10226 } 10227 ire_refrele(ire); 10228 return (-EINVAL); 10229 } 10230 case IPV6_SEC_OPT: 10231 error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); 10232 if (error != 0) { 10233 *outlenp = 0; 10234 return (error); 10235 } 10236 break; 10237 case IPV6_SRC_PREFERENCES: { 10238 /* 10239 * This is implemented strictly in the ip module 10240 * (here and in tcp_opt_*() to accomodate tcp 10241 * sockets). Modules above ip pass this option 10242 * down here since ip is the only one that needs to 10243 * be aware of source address preferences. 10244 * 10245 * This socket option only affects connected 10246 * sockets that haven't already bound to a specific 10247 * IPv6 address. In other words, sockets that 10248 * don't call bind() with an address other than the 10249 * unspecified address and that call connect(). 10250 * ip_bind_connected_v6() passes these preferences 10251 * to the ipif_select_source_v6() function. 10252 */ 10253 if (inlen != sizeof (uint32_t)) 10254 return (EINVAL); 10255 error = ip6_set_src_preferences(connp, 10256 *(uint32_t *)invalp); 10257 if (error != 0) { 10258 *outlenp = 0; 10259 return (error); 10260 } else { 10261 *outlenp = sizeof (uint32_t); 10262 } 10263 break; 10264 } 10265 case IPV6_V6ONLY: 10266 if (*i1 < 0 || *i1 > 1) { 10267 return (EINVAL); 10268 } 10269 mutex_enter(&connp->conn_lock); 10270 connp->conn_ipv6_v6only = *i1; 10271 mutex_exit(&connp->conn_lock); 10272 break; 10273 default: 10274 return (-EINVAL); 10275 } 10276 break; 10277 default: 10278 /* 10279 * "soft" error (negative) 10280 * option not handled at this level 10281 * Note: Do not modify *outlenp 10282 */ 10283 return (-EINVAL); 10284 } 10285 /* 10286 * Common case of return from an option that is sizeof (int) 10287 */ 10288 *(int *)outvalp = *i1; 10289 *outlenp = sizeof (int); 10290 return (0); 10291 } 10292 10293 /* 10294 * This routine gets default values of certain options whose default 10295 * values are maintained by protocol specific code 10296 */ 10297 /* ARGSUSED */ 10298 int 10299 ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 10300 { 10301 int *i1 = (int *)ptr; 10302 10303 switch (level) { 10304 case IPPROTO_IP: 10305 switch (name) { 10306 case IP_MULTICAST_TTL: 10307 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; 10308 return (sizeof (uchar_t)); 10309 case IP_MULTICAST_LOOP: 10310 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; 10311 return (sizeof (uchar_t)); 10312 default: 10313 return (-1); 10314 } 10315 case IPPROTO_IPV6: 10316 switch (name) { 10317 case IPV6_UNICAST_HOPS: 10318 *i1 = ipv6_def_hops; 10319 return (sizeof (int)); 10320 case IPV6_MULTICAST_HOPS: 10321 *i1 = IP_DEFAULT_MULTICAST_TTL; 10322 return (sizeof (int)); 10323 case IPV6_MULTICAST_LOOP: 10324 *i1 = IP_DEFAULT_MULTICAST_LOOP; 10325 return (sizeof (int)); 10326 case IPV6_V6ONLY: 10327 *i1 = 1; 10328 return (sizeof (int)); 10329 default: 10330 return (-1); 10331 } 10332 default: 10333 return (-1); 10334 } 10335 /* NOTREACHED */ 10336 } 10337 10338 /* 10339 * Given a destination address and a pointer to where to put the information 10340 * this routine fills in the mtuinfo. 10341 */ 10342 int 10343 ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, 10344 struct ip6_mtuinfo *mtuinfo) 10345 { 10346 ire_t *ire; 10347 10348 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 10349 return (-1); 10350 10351 bzero(mtuinfo, sizeof (*mtuinfo)); 10352 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 10353 mtuinfo->ip6m_addr.sin6_port = port; 10354 mtuinfo->ip6m_addr.sin6_addr = *in6; 10355 10356 ire = ire_cache_lookup_v6(in6, ALL_ZONES); 10357 if (ire != NULL) { 10358 mtuinfo->ip6m_mtu = ire->ire_max_frag; 10359 ire_refrele(ire); 10360 } else { 10361 mtuinfo->ip6m_mtu = IPV6_MIN_MTU; 10362 } 10363 return (sizeof (struct ip6_mtuinfo)); 10364 } 10365 10366 /* 10367 * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error 10368 * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and 10369 * isn't. This doesn't matter as the error checking is done properly for the 10370 * other MRT options coming in through ip_opt_set. 10371 */ 10372 int 10373 ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 10374 { 10375 conn_t *connp = Q_TO_CONN(q); 10376 ipsec_req_t *req = (ipsec_req_t *)ptr; 10377 10378 switch (level) { 10379 case IPPROTO_IP: 10380 switch (name) { 10381 case MRT_VERSION: 10382 case MRT_ASSERT: 10383 (void) ip_mrouter_get(name, q, ptr); 10384 return (sizeof (int)); 10385 case IP_SEC_OPT: 10386 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); 10387 case IP_NEXTHOP: 10388 if (connp->conn_nexthop_set) { 10389 *(ipaddr_t *)ptr = connp->conn_nexthop_v4; 10390 return (sizeof (ipaddr_t)); 10391 } else 10392 return (0); 10393 default: 10394 break; 10395 } 10396 break; 10397 case IPPROTO_IPV6: 10398 switch (name) { 10399 case IPV6_SEC_OPT: 10400 return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); 10401 case IPV6_SRC_PREFERENCES: { 10402 return (ip6_get_src_preferences(connp, 10403 (uint32_t *)ptr)); 10404 } 10405 case IPV6_V6ONLY: 10406 *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; 10407 return (sizeof (int)); 10408 case IPV6_PATHMTU: 10409 return (ip_fill_mtuinfo(&connp->conn_remv6, 0, 10410 (struct ip6_mtuinfo *)ptr)); 10411 default: 10412 break; 10413 } 10414 break; 10415 default: 10416 break; 10417 } 10418 return (-1); 10419 } 10420 10421 /* Named Dispatch routine to get a current value out of our parameter table. */ 10422 /* ARGSUSED */ 10423 static int 10424 ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 10425 { 10426 ipparam_t *ippa = (ipparam_t *)cp; 10427 10428 (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); 10429 return (0); 10430 } 10431 10432 /* ARGSUSED */ 10433 static int 10434 ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 10435 { 10436 10437 (void) mi_mpprintf(mp, "%d", *(int *)cp); 10438 return (0); 10439 } 10440 10441 /* 10442 * Set ip{,6}_forwarding values. This means walking through all of the 10443 * ill's and toggling their forwarding values. 10444 */ 10445 /* ARGSUSED */ 10446 static int 10447 ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 10448 { 10449 long new_value; 10450 int *forwarding_value = (int *)cp; 10451 ill_t *walker; 10452 boolean_t isv6 = (forwarding_value == &ipv6_forward); 10453 ill_walk_context_t ctx; 10454 10455 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 10456 new_value < 0 || new_value > 1) { 10457 return (EINVAL); 10458 } 10459 10460 *forwarding_value = new_value; 10461 10462 /* 10463 * Regardless of the current value of ip_forwarding, set all per-ill 10464 * values of ip_forwarding to the value being set. 10465 * 10466 * Bring all the ill's up to date with the new global value. 10467 */ 10468 rw_enter(&ill_g_lock, RW_READER); 10469 10470 if (isv6) 10471 walker = ILL_START_WALK_V6(&ctx); 10472 else 10473 walker = ILL_START_WALK_V4(&ctx); 10474 for (; walker != NULL; walker = ill_next(&ctx, walker)) { 10475 (void) ill_forward_set(q, mp, (new_value != 0), 10476 (caddr_t)walker); 10477 } 10478 rw_exit(&ill_g_lock); 10479 10480 return (0); 10481 } 10482 10483 /* 10484 * Walk through the param array specified registering each element with the 10485 * Named Dispatch handler. This is called only during init. So it is ok 10486 * not to acquire any locks 10487 */ 10488 static boolean_t 10489 ip_param_register(ipparam_t *ippa, size_t ippa_cnt, 10490 ipndp_t *ipnd, size_t ipnd_cnt) 10491 { 10492 for (; ippa_cnt-- > 0; ippa++) { 10493 if (ippa->ip_param_name && ippa->ip_param_name[0]) { 10494 if (!nd_load(&ip_g_nd, ippa->ip_param_name, 10495 ip_param_get, ip_param_set, (caddr_t)ippa)) { 10496 nd_free(&ip_g_nd); 10497 return (B_FALSE); 10498 } 10499 } 10500 } 10501 10502 for (; ipnd_cnt-- > 0; ipnd++) { 10503 if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { 10504 if (!nd_load(&ip_g_nd, ipnd->ip_ndp_name, 10505 ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, 10506 ipnd->ip_ndp_data)) { 10507 nd_free(&ip_g_nd); 10508 return (B_FALSE); 10509 } 10510 } 10511 } 10512 10513 return (B_TRUE); 10514 } 10515 10516 /* Named Dispatch routine to negotiate a new value for one of our parameters. */ 10517 /* ARGSUSED */ 10518 static int 10519 ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) 10520 { 10521 long new_value; 10522 ipparam_t *ippa = (ipparam_t *)cp; 10523 10524 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 10525 new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { 10526 return (EINVAL); 10527 } 10528 ippa->ip_param_value = new_value; 10529 return (0); 10530 } 10531 10532 /* 10533 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 10534 * When an ipf is passed here for the first time, if 10535 * we already have in-order fragments on the queue, we convert from the fast- 10536 * path reassembly scheme to the hard-case scheme. From then on, additional 10537 * fragments are reassembled here. We keep track of the start and end offsets 10538 * of each piece, and the number of holes in the chain. When the hole count 10539 * goes to zero, we are done! 10540 * 10541 * The ipf_count will be updated to account for any mblk(s) added (pointed to 10542 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 10543 * ipfb_count and ill_frag_count by the difference of ipf_count before and 10544 * after the call to ip_reassemble(). 10545 */ 10546 int 10547 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 10548 size_t msg_len) 10549 { 10550 uint_t end; 10551 mblk_t *next_mp; 10552 mblk_t *mp1; 10553 uint_t offset; 10554 boolean_t incr_dups = B_TRUE; 10555 boolean_t offset_zero_seen = B_FALSE; 10556 boolean_t pkt_boundary_checked = B_FALSE; 10557 10558 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 10559 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 10560 10561 /* Add in byte count */ 10562 ipf->ipf_count += msg_len; 10563 if (ipf->ipf_end) { 10564 /* 10565 * We were part way through in-order reassembly, but now there 10566 * is a hole. We walk through messages already queued, and 10567 * mark them for hard case reassembly. We know that up till 10568 * now they were in order starting from offset zero. 10569 */ 10570 offset = 0; 10571 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 10572 IP_REASS_SET_START(mp1, offset); 10573 if (offset == 0) { 10574 ASSERT(ipf->ipf_nf_hdr_len != 0); 10575 offset = -ipf->ipf_nf_hdr_len; 10576 } 10577 offset += mp1->b_wptr - mp1->b_rptr; 10578 IP_REASS_SET_END(mp1, offset); 10579 } 10580 /* One hole at the end. */ 10581 ipf->ipf_hole_cnt = 1; 10582 /* Brand it as a hard case, forever. */ 10583 ipf->ipf_end = 0; 10584 } 10585 /* Walk through all the new pieces. */ 10586 do { 10587 end = start + (mp->b_wptr - mp->b_rptr); 10588 /* 10589 * If start is 0, decrease 'end' only for the first mblk of 10590 * the fragment. Otherwise 'end' can get wrong value in the 10591 * second pass of the loop if first mblk is exactly the 10592 * size of ipf_nf_hdr_len. 10593 */ 10594 if (start == 0 && !offset_zero_seen) { 10595 /* First segment */ 10596 ASSERT(ipf->ipf_nf_hdr_len != 0); 10597 end -= ipf->ipf_nf_hdr_len; 10598 offset_zero_seen = B_TRUE; 10599 } 10600 next_mp = mp->b_cont; 10601 /* 10602 * We are checking to see if there is any interesing data 10603 * to process. If there isn't and the mblk isn't the 10604 * one which carries the unfragmentable header then we 10605 * drop it. It's possible to have just the unfragmentable 10606 * header come through without any data. That needs to be 10607 * saved. 10608 * 10609 * If the assert at the top of this function holds then the 10610 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 10611 * is infrequently traveled enough that the test is left in 10612 * to protect against future code changes which break that 10613 * invariant. 10614 */ 10615 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 10616 /* Empty. Blast it. */ 10617 IP_REASS_SET_START(mp, 0); 10618 IP_REASS_SET_END(mp, 0); 10619 /* 10620 * If the ipf points to the mblk we are about to free, 10621 * update ipf to point to the next mblk (or NULL 10622 * if none). 10623 */ 10624 if (ipf->ipf_mp->b_cont == mp) 10625 ipf->ipf_mp->b_cont = next_mp; 10626 freeb(mp); 10627 continue; 10628 } 10629 mp->b_cont = NULL; 10630 IP_REASS_SET_START(mp, start); 10631 IP_REASS_SET_END(mp, end); 10632 if (!ipf->ipf_tail_mp) { 10633 ipf->ipf_tail_mp = mp; 10634 ipf->ipf_mp->b_cont = mp; 10635 if (start == 0 || !more) { 10636 ipf->ipf_hole_cnt = 1; 10637 /* 10638 * if the first fragment comes in more than one 10639 * mblk, this loop will be executed for each 10640 * mblk. Need to adjust hole count so exiting 10641 * this routine will leave hole count at 1. 10642 */ 10643 if (next_mp) 10644 ipf->ipf_hole_cnt++; 10645 } else 10646 ipf->ipf_hole_cnt = 2; 10647 continue; 10648 } else if (ipf->ipf_last_frag_seen && !more && 10649 !pkt_boundary_checked) { 10650 /* 10651 * We check datagram boundary only if this fragment 10652 * claims to be the last fragment and we have seen a 10653 * last fragment in the past too. We do this only 10654 * once for a given fragment. 10655 * 10656 * start cannot be 0 here as fragments with start=0 10657 * and MF=0 gets handled as a complete packet. These 10658 * fragments should not reach here. 10659 */ 10660 10661 if (start + msgdsize(mp) != 10662 IP_REASS_END(ipf->ipf_tail_mp)) { 10663 /* 10664 * We have two fragments both of which claim 10665 * to be the last fragment but gives conflicting 10666 * information about the whole datagram size. 10667 * Something fishy is going on. Drop the 10668 * fragment and free up the reassembly list. 10669 */ 10670 return (IP_REASS_FAILED); 10671 } 10672 10673 /* 10674 * We shouldn't come to this code block again for this 10675 * particular fragment. 10676 */ 10677 pkt_boundary_checked = B_TRUE; 10678 } 10679 10680 /* New stuff at or beyond tail? */ 10681 offset = IP_REASS_END(ipf->ipf_tail_mp); 10682 if (start >= offset) { 10683 if (ipf->ipf_last_frag_seen) { 10684 /* current fragment is beyond last fragment */ 10685 return (IP_REASS_FAILED); 10686 } 10687 /* Link it on end. */ 10688 ipf->ipf_tail_mp->b_cont = mp; 10689 ipf->ipf_tail_mp = mp; 10690 if (more) { 10691 if (start != offset) 10692 ipf->ipf_hole_cnt++; 10693 } else if (start == offset && next_mp == NULL) 10694 ipf->ipf_hole_cnt--; 10695 continue; 10696 } 10697 mp1 = ipf->ipf_mp->b_cont; 10698 offset = IP_REASS_START(mp1); 10699 /* New stuff at the front? */ 10700 if (start < offset) { 10701 if (start == 0) { 10702 if (end >= offset) { 10703 /* Nailed the hole at the begining. */ 10704 ipf->ipf_hole_cnt--; 10705 } 10706 } else if (end < offset) { 10707 /* 10708 * A hole, stuff, and a hole where there used 10709 * to be just a hole. 10710 */ 10711 ipf->ipf_hole_cnt++; 10712 } 10713 mp->b_cont = mp1; 10714 /* Check for overlap. */ 10715 while (end > offset) { 10716 if (end < IP_REASS_END(mp1)) { 10717 mp->b_wptr -= end - offset; 10718 IP_REASS_SET_END(mp, offset); 10719 if (ill->ill_isv6) { 10720 BUMP_MIB(ill->ill_ip6_mib, 10721 ipv6ReasmPartDups); 10722 } else { 10723 BUMP_MIB(&ip_mib, 10724 ipReasmPartDups); 10725 } 10726 break; 10727 } 10728 /* Did we cover another hole? */ 10729 if ((mp1->b_cont && 10730 IP_REASS_END(mp1) != 10731 IP_REASS_START(mp1->b_cont) && 10732 end >= IP_REASS_START(mp1->b_cont)) || 10733 (!ipf->ipf_last_frag_seen && !more)) { 10734 ipf->ipf_hole_cnt--; 10735 } 10736 /* Clip out mp1. */ 10737 if ((mp->b_cont = mp1->b_cont) == NULL) { 10738 /* 10739 * After clipping out mp1, this guy 10740 * is now hanging off the end. 10741 */ 10742 ipf->ipf_tail_mp = mp; 10743 } 10744 IP_REASS_SET_START(mp1, 0); 10745 IP_REASS_SET_END(mp1, 0); 10746 /* Subtract byte count */ 10747 ipf->ipf_count -= mp1->b_datap->db_lim - 10748 mp1->b_datap->db_base; 10749 freeb(mp1); 10750 if (ill->ill_isv6) { 10751 BUMP_MIB(ill->ill_ip6_mib, 10752 ipv6ReasmPartDups); 10753 } else { 10754 BUMP_MIB(&ip_mib, ipReasmPartDups); 10755 } 10756 mp1 = mp->b_cont; 10757 if (!mp1) 10758 break; 10759 offset = IP_REASS_START(mp1); 10760 } 10761 ipf->ipf_mp->b_cont = mp; 10762 continue; 10763 } 10764 /* 10765 * The new piece starts somewhere between the start of the head 10766 * and before the end of the tail. 10767 */ 10768 for (; mp1; mp1 = mp1->b_cont) { 10769 offset = IP_REASS_END(mp1); 10770 if (start < offset) { 10771 if (end <= offset) { 10772 /* Nothing new. */ 10773 IP_REASS_SET_START(mp, 0); 10774 IP_REASS_SET_END(mp, 0); 10775 /* Subtract byte count */ 10776 ipf->ipf_count -= mp->b_datap->db_lim - 10777 mp->b_datap->db_base; 10778 if (incr_dups) { 10779 ipf->ipf_num_dups++; 10780 incr_dups = B_FALSE; 10781 } 10782 freeb(mp); 10783 if (ill->ill_isv6) { 10784 BUMP_MIB(ill->ill_ip6_mib, 10785 ipv6ReasmDuplicates); 10786 } else { 10787 BUMP_MIB(&ip_mib, 10788 ipReasmDuplicates); 10789 } 10790 break; 10791 } 10792 /* 10793 * Trim redundant stuff off beginning of new 10794 * piece. 10795 */ 10796 IP_REASS_SET_START(mp, offset); 10797 mp->b_rptr += offset - start; 10798 if (ill->ill_isv6) { 10799 BUMP_MIB(ill->ill_ip6_mib, 10800 ipv6ReasmPartDups); 10801 } else { 10802 BUMP_MIB(&ip_mib, ipReasmPartDups); 10803 } 10804 start = offset; 10805 if (!mp1->b_cont) { 10806 /* 10807 * After trimming, this guy is now 10808 * hanging off the end. 10809 */ 10810 mp1->b_cont = mp; 10811 ipf->ipf_tail_mp = mp; 10812 if (!more) { 10813 ipf->ipf_hole_cnt--; 10814 } 10815 break; 10816 } 10817 } 10818 if (start >= IP_REASS_START(mp1->b_cont)) 10819 continue; 10820 /* Fill a hole */ 10821 if (start > offset) 10822 ipf->ipf_hole_cnt++; 10823 mp->b_cont = mp1->b_cont; 10824 mp1->b_cont = mp; 10825 mp1 = mp->b_cont; 10826 offset = IP_REASS_START(mp1); 10827 if (end >= offset) { 10828 ipf->ipf_hole_cnt--; 10829 /* Check for overlap. */ 10830 while (end > offset) { 10831 if (end < IP_REASS_END(mp1)) { 10832 mp->b_wptr -= end - offset; 10833 IP_REASS_SET_END(mp, offset); 10834 /* 10835 * TODO we might bump 10836 * this up twice if there is 10837 * overlap at both ends. 10838 */ 10839 if (ill->ill_isv6) { 10840 BUMP_MIB( 10841 ill->ill_ip6_mib, 10842 ipv6ReasmPartDups); 10843 } else { 10844 BUMP_MIB(&ip_mib, 10845 ipReasmPartDups); 10846 } 10847 break; 10848 } 10849 /* Did we cover another hole? */ 10850 if ((mp1->b_cont && 10851 IP_REASS_END(mp1) 10852 != IP_REASS_START(mp1->b_cont) && 10853 end >= 10854 IP_REASS_START(mp1->b_cont)) || 10855 (!ipf->ipf_last_frag_seen && 10856 !more)) { 10857 ipf->ipf_hole_cnt--; 10858 } 10859 /* Clip out mp1. */ 10860 if ((mp->b_cont = mp1->b_cont) == 10861 NULL) { 10862 /* 10863 * After clipping out mp1, 10864 * this guy is now hanging 10865 * off the end. 10866 */ 10867 ipf->ipf_tail_mp = mp; 10868 } 10869 IP_REASS_SET_START(mp1, 0); 10870 IP_REASS_SET_END(mp1, 0); 10871 /* Subtract byte count */ 10872 ipf->ipf_count -= 10873 mp1->b_datap->db_lim - 10874 mp1->b_datap->db_base; 10875 freeb(mp1); 10876 if (ill->ill_isv6) { 10877 BUMP_MIB(ill->ill_ip6_mib, 10878 ipv6ReasmPartDups); 10879 } else { 10880 BUMP_MIB(&ip_mib, 10881 ipReasmPartDups); 10882 } 10883 mp1 = mp->b_cont; 10884 if (!mp1) 10885 break; 10886 offset = IP_REASS_START(mp1); 10887 } 10888 } 10889 break; 10890 } 10891 } while (start = end, mp = next_mp); 10892 10893 /* Fragment just processed could be the last one. Remember this fact */ 10894 if (!more) 10895 ipf->ipf_last_frag_seen = B_TRUE; 10896 10897 /* Still got holes? */ 10898 if (ipf->ipf_hole_cnt) 10899 return (IP_REASS_PARTIAL); 10900 /* Clean up overloaded fields to avoid upstream disasters. */ 10901 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 10902 IP_REASS_SET_START(mp1, 0); 10903 IP_REASS_SET_END(mp1, 0); 10904 } 10905 return (IP_REASS_COMPLETE); 10906 } 10907 10908 /* 10909 * ipsec processing for the fast path, used for input UDP Packets 10910 */ 10911 static boolean_t 10912 ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, 10913 mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present) 10914 { 10915 uint32_t ill_index; 10916 uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ 10917 10918 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 10919 /* The ill_index of the incoming ILL */ 10920 ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; 10921 10922 /* pass packet up to the transport */ 10923 if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) { 10924 *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, 10925 NULL, mctl_present); 10926 if (*first_mpp == NULL) { 10927 return (B_FALSE); 10928 } 10929 } 10930 10931 /* Initiate IPPF processing for fastpath UDP */ 10932 if (IPP_ENABLED(IPP_LOCAL_IN)) { 10933 ip_process(IPP_LOCAL_IN, mpp, ill_index); 10934 if (*mpp == NULL) { 10935 ip2dbg(("ip_input_ipsec_process: UDP pkt " 10936 "deferred/dropped during IPPF processing\n")); 10937 return (B_FALSE); 10938 } 10939 } 10940 /* 10941 * We make the checks as below since we are in the fast path 10942 * and want to minimize the number of checks if the IP_RECVIF and/or 10943 * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set 10944 */ 10945 if (connp->conn_recvif || connp->conn_recvslla || 10946 connp->conn_ipv6_recvpktinfo) { 10947 if (connp->conn_recvif || 10948 connp->conn_ipv6_recvpktinfo) { 10949 in_flags = IPF_RECVIF; 10950 } 10951 if (connp->conn_recvslla) { 10952 in_flags |= IPF_RECVSLLA; 10953 } 10954 /* 10955 * since in_flags are being set ill will be 10956 * referenced in ip_add_info, so it better not 10957 * be NULL. 10958 */ 10959 /* 10960 * the actual data will be contained in b_cont 10961 * upon successful return of the following call. 10962 * If the call fails then the original mblk is 10963 * returned. 10964 */ 10965 *mpp = ip_add_info(*mpp, ill, in_flags); 10966 } 10967 10968 return (B_TRUE); 10969 } 10970 10971 /* 10972 * Fragmentation reassembly. Each ILL has a hash table for 10973 * queuing packets undergoing reassembly for all IPIFs 10974 * associated with the ILL. The hash is based on the packet 10975 * IP ident field. The ILL frag hash table was allocated 10976 * as a timer block at the time the ILL was created. Whenever 10977 * there is anything on the reassembly queue, the timer will 10978 * be running. Returns B_TRUE if successful else B_FALSE; 10979 * frees mp on failure. 10980 */ 10981 static boolean_t 10982 ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, 10983 uint32_t *cksum_val, uint16_t *cksum_flags) 10984 { 10985 uint32_t frag_offset_flags; 10986 ill_t *ill = (ill_t *)q->q_ptr; 10987 mblk_t *mp = *mpp; 10988 mblk_t *t_mp; 10989 ipaddr_t dst; 10990 uint8_t proto = ipha->ipha_protocol; 10991 uint32_t sum_val; 10992 uint16_t sum_flags; 10993 ipf_t *ipf; 10994 ipf_t **ipfp; 10995 ipfb_t *ipfb; 10996 uint16_t ident; 10997 uint32_t offset; 10998 ipaddr_t src; 10999 uint_t hdr_length; 11000 uint32_t end; 11001 mblk_t *mp1; 11002 mblk_t *tail_mp; 11003 size_t count; 11004 size_t msg_len; 11005 uint8_t ecn_info = 0; 11006 uint32_t packet_size; 11007 boolean_t pruned = B_FALSE; 11008 11009 if (cksum_val != NULL) 11010 *cksum_val = 0; 11011 if (cksum_flags != NULL) 11012 *cksum_flags = 0; 11013 11014 /* 11015 * Drop the fragmented as early as possible, if 11016 * we don't have resource(s) to re-assemble. 11017 */ 11018 if (ip_reass_queue_bytes == 0) { 11019 freemsg(mp); 11020 return (B_FALSE); 11021 } 11022 11023 /* Check for fragmentation offset; return if there's none */ 11024 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 11025 (IPH_MF | IPH_OFFSET)) == 0) 11026 return (B_TRUE); 11027 11028 /* 11029 * We utilize hardware computed checksum info only for UDP since 11030 * IP fragmentation is a normal occurence for the protocol. In 11031 * addition, checksum offload support for IP fragments carrying 11032 * UDP payload is commonly implemented across network adapters. 11033 */ 11034 ASSERT(ill != NULL); 11035 if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 11036 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 11037 mblk_t *mp1 = mp->b_cont; 11038 int32_t len; 11039 11040 /* Record checksum information from the packet */ 11041 sum_val = (uint32_t)DB_CKSUM16(mp); 11042 sum_flags = DB_CKSUMFLAGS(mp); 11043 11044 /* IP payload offset from beginning of mblk */ 11045 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 11046 11047 if ((sum_flags & HCK_PARTIALCKSUM) && 11048 (mp1 == NULL || mp1->b_cont == NULL) && 11049 offset >= DB_CKSUMSTART(mp) && 11050 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 11051 uint32_t adj; 11052 /* 11053 * Partial checksum has been calculated by hardware 11054 * and attached to the packet; in addition, any 11055 * prepended extraneous data is even byte aligned. 11056 * If any such data exists, we adjust the checksum; 11057 * this would also handle any postpended data. 11058 */ 11059 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 11060 mp, mp1, len, adj); 11061 11062 /* One's complement subtract extraneous checksum */ 11063 if (adj >= sum_val) 11064 sum_val = ~(adj - sum_val) & 0xFFFF; 11065 else 11066 sum_val -= adj; 11067 } 11068 } else { 11069 sum_val = 0; 11070 sum_flags = 0; 11071 } 11072 11073 /* Clear hardware checksumming flag */ 11074 DB_CKSUMFLAGS(mp) = 0; 11075 11076 ident = ipha->ipha_ident; 11077 offset = (frag_offset_flags << 3) & 0xFFFF; 11078 src = ipha->ipha_src; 11079 dst = ipha->ipha_dst; 11080 hdr_length = IPH_HDR_LENGTH(ipha); 11081 end = ntohs(ipha->ipha_length) - hdr_length; 11082 11083 /* If end == 0 then we have a packet with no data, so just free it */ 11084 if (end == 0) { 11085 freemsg(mp); 11086 return (B_FALSE); 11087 } 11088 11089 /* Record the ECN field info. */ 11090 ecn_info = (ipha->ipha_type_of_service & 0x3); 11091 if (offset != 0) { 11092 /* 11093 * If this isn't the first piece, strip the header, and 11094 * add the offset to the end value. 11095 */ 11096 mp->b_rptr += hdr_length; 11097 end += offset; 11098 } 11099 11100 msg_len = MBLKSIZE(mp); 11101 tail_mp = mp; 11102 while (tail_mp->b_cont != NULL) { 11103 tail_mp = tail_mp->b_cont; 11104 msg_len += MBLKSIZE(tail_mp); 11105 } 11106 11107 /* If the reassembly list for this ILL will get too big, prune it */ 11108 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 11109 ip_reass_queue_bytes) { 11110 ill_frag_prune(ill, 11111 (ip_reass_queue_bytes < msg_len) ? 0 : 11112 (ip_reass_queue_bytes - msg_len)); 11113 pruned = B_TRUE; 11114 } 11115 11116 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 11117 mutex_enter(&ipfb->ipfb_lock); 11118 11119 ipfp = &ipfb->ipfb_ipf; 11120 /* Try to find an existing fragment queue for this packet. */ 11121 for (;;) { 11122 ipf = ipfp[0]; 11123 if (ipf != NULL) { 11124 /* 11125 * It has to match on ident and src/dst address. 11126 */ 11127 if (ipf->ipf_ident == ident && 11128 ipf->ipf_src == src && 11129 ipf->ipf_dst == dst && 11130 ipf->ipf_protocol == proto) { 11131 /* 11132 * If we have received too many 11133 * duplicate fragments for this packet 11134 * free it. 11135 */ 11136 if (ipf->ipf_num_dups > ip_max_frag_dups) { 11137 ill_frag_free_pkts(ill, ipfb, ipf, 1); 11138 freemsg(mp); 11139 mutex_exit(&ipfb->ipfb_lock); 11140 return (B_FALSE); 11141 } 11142 /* Found it. */ 11143 break; 11144 } 11145 ipfp = &ipf->ipf_hash_next; 11146 continue; 11147 } 11148 11149 /* 11150 * If we pruned the list, do we want to store this new 11151 * fragment?. We apply an optimization here based on the 11152 * fact that most fragments will be received in order. 11153 * So if the offset of this incoming fragment is zero, 11154 * it is the first fragment of a new packet. We will 11155 * keep it. Otherwise drop the fragment, as we have 11156 * probably pruned the packet already (since the 11157 * packet cannot be found). 11158 */ 11159 if (pruned && offset != 0) { 11160 mutex_exit(&ipfb->ipfb_lock); 11161 freemsg(mp); 11162 return (B_FALSE); 11163 } 11164 11165 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS) { 11166 /* 11167 * Too many fragmented packets in this hash 11168 * bucket. Free the oldest. 11169 */ 11170 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 11171 } 11172 11173 /* New guy. Allocate a frag message. */ 11174 mp1 = allocb(sizeof (*ipf), BPRI_MED); 11175 if (mp1 == NULL) { 11176 BUMP_MIB(&ip_mib, ipInDiscards); 11177 freemsg(mp); 11178 reass_done: 11179 mutex_exit(&ipfb->ipfb_lock); 11180 return (B_FALSE); 11181 } 11182 11183 11184 BUMP_MIB(&ip_mib, ipReasmReqds); 11185 mp1->b_cont = mp; 11186 11187 /* Initialize the fragment header. */ 11188 ipf = (ipf_t *)mp1->b_rptr; 11189 ipf->ipf_mp = mp1; 11190 ipf->ipf_ptphn = ipfp; 11191 ipfp[0] = ipf; 11192 ipf->ipf_hash_next = NULL; 11193 ipf->ipf_ident = ident; 11194 ipf->ipf_protocol = proto; 11195 ipf->ipf_src = src; 11196 ipf->ipf_dst = dst; 11197 ipf->ipf_nf_hdr_len = 0; 11198 /* Record reassembly start time. */ 11199 ipf->ipf_timestamp = gethrestime_sec(); 11200 /* Record ipf generation and account for frag header */ 11201 ipf->ipf_gen = ill->ill_ipf_gen++; 11202 ipf->ipf_count = MBLKSIZE(mp1); 11203 ipf->ipf_last_frag_seen = B_FALSE; 11204 ipf->ipf_ecn = ecn_info; 11205 ipf->ipf_num_dups = 0; 11206 ipfb->ipfb_frag_pkts++; 11207 ipf->ipf_checksum = 0; 11208 ipf->ipf_checksum_flags = 0; 11209 11210 /* Store checksum value in fragment header */ 11211 if (sum_flags != 0) { 11212 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 11213 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 11214 ipf->ipf_checksum = sum_val; 11215 ipf->ipf_checksum_flags = sum_flags; 11216 } 11217 11218 /* 11219 * We handle reassembly two ways. In the easy case, 11220 * where all the fragments show up in order, we do 11221 * minimal bookkeeping, and just clip new pieces on 11222 * the end. If we ever see a hole, then we go off 11223 * to ip_reassemble which has to mark the pieces and 11224 * keep track of the number of holes, etc. Obviously, 11225 * the point of having both mechanisms is so we can 11226 * handle the easy case as efficiently as possible. 11227 */ 11228 if (offset == 0) { 11229 /* Easy case, in-order reassembly so far. */ 11230 ipf->ipf_count += msg_len; 11231 ipf->ipf_tail_mp = tail_mp; 11232 /* 11233 * Keep track of next expected offset in 11234 * ipf_end. 11235 */ 11236 ipf->ipf_end = end; 11237 ipf->ipf_nf_hdr_len = hdr_length; 11238 } else { 11239 /* Hard case, hole at the beginning. */ 11240 ipf->ipf_tail_mp = NULL; 11241 /* 11242 * ipf_end == 0 means that we have given up 11243 * on easy reassembly. 11244 */ 11245 ipf->ipf_end = 0; 11246 11247 /* Forget checksum offload from now on */ 11248 ipf->ipf_checksum_flags = 0; 11249 11250 /* 11251 * ipf_hole_cnt is set by ip_reassemble. 11252 * ipf_count is updated by ip_reassemble. 11253 * No need to check for return value here 11254 * as we don't expect reassembly to complete 11255 * or fail for the first fragment itself. 11256 */ 11257 (void) ip_reassemble(mp, ipf, 11258 (frag_offset_flags & IPH_OFFSET) << 3, 11259 (frag_offset_flags & IPH_MF), ill, msg_len); 11260 } 11261 /* Update per ipfb and ill byte counts */ 11262 ipfb->ipfb_count += ipf->ipf_count; 11263 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 11264 ill->ill_frag_count += ipf->ipf_count; 11265 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 11266 /* If the frag timer wasn't already going, start it. */ 11267 mutex_enter(&ill->ill_lock); 11268 ill_frag_timer_start(ill); 11269 mutex_exit(&ill->ill_lock); 11270 goto reass_done; 11271 } 11272 11273 /* 11274 * If the packet's flag has changed (it could be coming up 11275 * from an interface different than the previous, therefore 11276 * possibly different checksum capability), then forget about 11277 * any stored checksum states. Otherwise add the value to 11278 * the existing one stored in the fragment header. 11279 */ 11280 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 11281 sum_val += ipf->ipf_checksum; 11282 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 11283 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 11284 ipf->ipf_checksum = sum_val; 11285 } else if (ipf->ipf_checksum_flags != 0) { 11286 /* Forget checksum offload from now on */ 11287 ipf->ipf_checksum_flags = 0; 11288 } 11289 11290 /* 11291 * We have a new piece of a datagram which is already being 11292 * reassembled. Update the ECN info if all IP fragments 11293 * are ECN capable. If there is one which is not, clear 11294 * all the info. If there is at least one which has CE 11295 * code point, IP needs to report that up to transport. 11296 */ 11297 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 11298 if (ecn_info == IPH_ECN_CE) 11299 ipf->ipf_ecn = IPH_ECN_CE; 11300 } else { 11301 ipf->ipf_ecn = IPH_ECN_NECT; 11302 } 11303 if (offset && ipf->ipf_end == offset) { 11304 /* The new fragment fits at the end */ 11305 ipf->ipf_tail_mp->b_cont = mp; 11306 /* Update the byte count */ 11307 ipf->ipf_count += msg_len; 11308 /* Update per ipfb and ill byte counts */ 11309 ipfb->ipfb_count += msg_len; 11310 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 11311 ill->ill_frag_count += msg_len; 11312 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 11313 if (frag_offset_flags & IPH_MF) { 11314 /* More to come. */ 11315 ipf->ipf_end = end; 11316 ipf->ipf_tail_mp = tail_mp; 11317 goto reass_done; 11318 } 11319 } else { 11320 /* Go do the hard cases. */ 11321 int ret; 11322 11323 if (offset == 0) 11324 ipf->ipf_nf_hdr_len = hdr_length; 11325 11326 /* Save current byte count */ 11327 count = ipf->ipf_count; 11328 ret = ip_reassemble(mp, ipf, 11329 (frag_offset_flags & IPH_OFFSET) << 3, 11330 (frag_offset_flags & IPH_MF), ill, msg_len); 11331 /* Count of bytes added and subtracted (freeb()ed) */ 11332 count = ipf->ipf_count - count; 11333 if (count) { 11334 /* Update per ipfb and ill byte counts */ 11335 ipfb->ipfb_count += count; 11336 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 11337 ill->ill_frag_count += count; 11338 ASSERT(ill->ill_frag_count > 0); 11339 } 11340 if (ret == IP_REASS_PARTIAL) { 11341 goto reass_done; 11342 } else if (ret == IP_REASS_FAILED) { 11343 /* Reassembly failed. Free up all resources */ 11344 ill_frag_free_pkts(ill, ipfb, ipf, 1); 11345 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 11346 IP_REASS_SET_START(t_mp, 0); 11347 IP_REASS_SET_END(t_mp, 0); 11348 } 11349 freemsg(mp); 11350 goto reass_done; 11351 } 11352 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 11353 } 11354 /* 11355 * We have completed reassembly. Unhook the frag header from 11356 * the reassembly list. 11357 * 11358 * Before we free the frag header, record the ECN info 11359 * to report back to the transport. 11360 */ 11361 ecn_info = ipf->ipf_ecn; 11362 BUMP_MIB(&ip_mib, ipReasmOKs); 11363 ipfp = ipf->ipf_ptphn; 11364 11365 /* We need to supply these to caller */ 11366 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 11367 sum_val = ipf->ipf_checksum; 11368 else 11369 sum_val = 0; 11370 11371 mp1 = ipf->ipf_mp; 11372 count = ipf->ipf_count; 11373 ipf = ipf->ipf_hash_next; 11374 if (ipf != NULL) 11375 ipf->ipf_ptphn = ipfp; 11376 ipfp[0] = ipf; 11377 ill->ill_frag_count -= count; 11378 ASSERT(ipfb->ipfb_count >= count); 11379 ipfb->ipfb_count -= count; 11380 ipfb->ipfb_frag_pkts--; 11381 mutex_exit(&ipfb->ipfb_lock); 11382 /* Ditch the frag header. */ 11383 mp = mp1->b_cont; 11384 11385 freeb(mp1); 11386 11387 /* Restore original IP length in header. */ 11388 packet_size = (uint32_t)msgdsize(mp); 11389 if (packet_size > IP_MAXPACKET) { 11390 freemsg(mp); 11391 BUMP_MIB(&ip_mib, ipInHdrErrors); 11392 return (B_FALSE); 11393 } 11394 11395 if (DB_REF(mp) > 1) { 11396 mblk_t *mp2 = copymsg(mp); 11397 11398 freemsg(mp); 11399 if (mp2 == NULL) { 11400 BUMP_MIB(&ip_mib, ipInDiscards); 11401 return (B_FALSE); 11402 } 11403 mp = mp2; 11404 } 11405 ipha = (ipha_t *)mp->b_rptr; 11406 11407 ipha->ipha_length = htons((uint16_t)packet_size); 11408 /* We're now complete, zip the frag state */ 11409 ipha->ipha_fragment_offset_and_flags = 0; 11410 /* Record the ECN info. */ 11411 ipha->ipha_type_of_service &= 0xFC; 11412 ipha->ipha_type_of_service |= ecn_info; 11413 *mpp = mp; 11414 11415 /* Reassembly is successful; return checksum information if needed */ 11416 if (cksum_val != NULL) 11417 *cksum_val = sum_val; 11418 if (cksum_flags != NULL) 11419 *cksum_flags = sum_flags; 11420 11421 return (B_TRUE); 11422 } 11423 11424 /* 11425 * Perform ip header check sum update local options. 11426 * return B_TRUE if all is well, else return B_FALSE and release 11427 * the mp. caller is responsible for decrementing ire ref cnt. 11428 */ 11429 static boolean_t 11430 ip_options_cksum(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire) 11431 { 11432 mblk_t *first_mp; 11433 boolean_t mctl_present; 11434 uint16_t sum; 11435 11436 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 11437 /* 11438 * Don't do the checksum if it has gone through AH/ESP 11439 * processing. 11440 */ 11441 if (!mctl_present) { 11442 sum = ip_csum_hdr(ipha); 11443 if (sum != 0) { 11444 BUMP_MIB(&ip_mib, ipInCksumErrs); 11445 freemsg(first_mp); 11446 return (B_FALSE); 11447 } 11448 } 11449 11450 if (!ip_rput_local_options(q, mp, ipha, ire)) { 11451 if (mctl_present) 11452 freeb(first_mp); 11453 return (B_FALSE); 11454 } 11455 11456 return (B_TRUE); 11457 } 11458 11459 /* 11460 * All udp packet are delivered to the local host via this routine. 11461 */ 11462 void 11463 ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 11464 ill_t *recv_ill) 11465 { 11466 uint32_t sum; 11467 uint32_t u1; 11468 boolean_t mctl_present; 11469 conn_t *connp; 11470 mblk_t *first_mp; 11471 uint16_t *up; 11472 ill_t *ill = (ill_t *)q->q_ptr; 11473 uint16_t reass_hck_flags = 0; 11474 11475 #define rptr ((uchar_t *)ipha) 11476 11477 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 11478 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 11479 ASSERT(ipha->ipha_protocol == IPPROTO_UDP); 11480 11481 /* 11482 * FAST PATH for udp packets 11483 */ 11484 11485 /* u1 is # words of IP options */ 11486 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + 11487 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 11488 11489 /* IP options present */ 11490 if (u1 != 0) 11491 goto ipoptions; 11492 11493 /* Check the IP header checksum. */ 11494 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 11495 /* Clear the IP header h/w cksum flag */ 11496 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 11497 } else { 11498 #define uph ((uint16_t *)ipha) 11499 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 11500 uph[6] + uph[7] + uph[8] + uph[9]; 11501 #undef uph 11502 /* finish doing IP checksum */ 11503 sum = (sum & 0xFFFF) + (sum >> 16); 11504 sum = ~(sum + (sum >> 16)) & 0xFFFF; 11505 /* 11506 * Don't verify header checksum if this packet is coming 11507 * back from AH/ESP as we already did it. 11508 */ 11509 if (!mctl_present && sum != 0 && sum != 0xFFFF) { 11510 BUMP_MIB(&ip_mib, ipInCksumErrs); 11511 freemsg(first_mp); 11512 return; 11513 } 11514 } 11515 11516 /* 11517 * Count for SNMP of inbound packets for ire. 11518 * if mctl is present this might be a secure packet and 11519 * has already been counted for in ip_proto_input(). 11520 */ 11521 if (!mctl_present) { 11522 UPDATE_IB_PKT_COUNT(ire); 11523 ire->ire_last_used_time = lbolt; 11524 } 11525 11526 /* packet part of fragmented IP packet? */ 11527 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 11528 if (u1 & (IPH_MF | IPH_OFFSET)) { 11529 goto fragmented; 11530 } 11531 11532 /* u1 = IP header length (20 bytes) */ 11533 u1 = IP_SIMPLE_HDR_LENGTH; 11534 11535 /* packet does not contain complete IP & UDP headers */ 11536 if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) 11537 goto udppullup; 11538 11539 /* up points to UDP header */ 11540 up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); 11541 #define iphs ((uint16_t *)ipha) 11542 11543 /* if udp hdr cksum != 0, then need to checksum udp packet */ 11544 if (up[3] != 0) { 11545 mblk_t *mp1 = mp->b_cont; 11546 boolean_t cksum_err; 11547 uint16_t hck_flags = 0; 11548 11549 /* Pseudo-header checksum */ 11550 u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 11551 iphs[9] + up[2]; 11552 11553 /* 11554 * Revert to software checksum calculation if the interface 11555 * isn't capable of checksum offload or if IPsec is present. 11556 */ 11557 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 11558 hck_flags = DB_CKSUMFLAGS(mp); 11559 11560 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 11561 IP_STAT(ip_in_sw_cksum); 11562 11563 IP_CKSUM_RECV(hck_flags, u1, 11564 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 11565 (int32_t)((uchar_t *)up - rptr), 11566 mp, mp1, cksum_err); 11567 11568 if (cksum_err) { 11569 BUMP_MIB(&ip_mib, udpInCksumErrs); 11570 11571 if (hck_flags & HCK_FULLCKSUM) 11572 IP_STAT(ip_udp_in_full_hw_cksum_err); 11573 else if (hck_flags & HCK_PARTIALCKSUM) 11574 IP_STAT(ip_udp_in_part_hw_cksum_err); 11575 else 11576 IP_STAT(ip_udp_in_sw_cksum_err); 11577 11578 freemsg(first_mp); 11579 return; 11580 } 11581 } 11582 11583 /* Non-fragmented broadcast or multicast packet? */ 11584 if (ire->ire_type == IRE_BROADCAST) 11585 goto udpslowpath; 11586 11587 if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, 11588 ire->ire_zoneid)) != NULL) { 11589 ASSERT(connp->conn_upq != NULL); 11590 IP_STAT(ip_udp_fast_path); 11591 11592 if (CONN_UDP_FLOWCTLD(connp)) { 11593 freemsg(mp); 11594 BUMP_MIB(&ip_mib, udpInOverflows); 11595 } else { 11596 if (!mctl_present) { 11597 BUMP_MIB(&ip_mib, ipInDelivers); 11598 } 11599 /* 11600 * mp and first_mp can change. 11601 */ 11602 if (ip_udp_check(q, connp, recv_ill, 11603 ipha, &mp, &first_mp, mctl_present)) { 11604 /* Send it upstream */ 11605 CONN_UDP_RECV(connp, mp); 11606 } 11607 } 11608 /* 11609 * freeb() cannot deal with null mblk being passed 11610 * in and first_mp can be set to null in the call 11611 * ipsec_input_fast_proc()->ipsec_check_inbound_policy. 11612 */ 11613 if (mctl_present && first_mp != NULL) { 11614 freeb(first_mp); 11615 } 11616 CONN_DEC_REF(connp); 11617 return; 11618 } 11619 11620 /* 11621 * if we got here we know the packet is not fragmented and 11622 * has no options. The classifier could not find a conn_t and 11623 * most likely its an icmp packet so send it through slow path. 11624 */ 11625 11626 goto udpslowpath; 11627 11628 ipoptions: 11629 if (!ip_options_cksum(q, mp, ipha, ire)) { 11630 goto slow_done; 11631 } 11632 11633 UPDATE_IB_PKT_COUNT(ire); 11634 ire->ire_last_used_time = lbolt; 11635 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 11636 if (u1 & (IPH_MF | IPH_OFFSET)) { 11637 fragmented: 11638 /* 11639 * "sum" and "reass_hck_flags" are non-zero if the 11640 * reassembled packet has a valid hardware computed 11641 * checksum information associated with it. 11642 */ 11643 if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) 11644 goto slow_done; 11645 /* 11646 * Make sure that first_mp points back to mp as 11647 * the mp we came in with could have changed in 11648 * ip_rput_fragment(). 11649 */ 11650 ASSERT(!mctl_present); 11651 ipha = (ipha_t *)mp->b_rptr; 11652 first_mp = mp; 11653 } 11654 11655 /* Now we have a complete datagram, destined for this machine. */ 11656 u1 = IPH_HDR_LENGTH(ipha); 11657 /* Pull up the UDP header, if necessary. */ 11658 if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { 11659 udppullup: 11660 if (!pullupmsg(mp, u1 + UDPH_SIZE)) { 11661 BUMP_MIB(&ip_mib, ipInDiscards); 11662 freemsg(first_mp); 11663 goto slow_done; 11664 } 11665 ipha = (ipha_t *)mp->b_rptr; 11666 } 11667 11668 /* 11669 * Validate the checksum for the reassembled packet; for the 11670 * pullup case we calculate the payload checksum in software. 11671 */ 11672 up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); 11673 if (up[3] != 0) { 11674 boolean_t cksum_err; 11675 11676 if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 11677 IP_STAT(ip_in_sw_cksum); 11678 11679 IP_CKSUM_RECV_REASS(reass_hck_flags, 11680 (int32_t)((uchar_t *)up - (uchar_t *)ipha), 11681 IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 11682 iphs[9] + up[2], sum, cksum_err); 11683 11684 if (cksum_err) { 11685 BUMP_MIB(&ip_mib, udpInCksumErrs); 11686 11687 if (reass_hck_flags & HCK_FULLCKSUM) 11688 IP_STAT(ip_udp_in_full_hw_cksum_err); 11689 else if (reass_hck_flags & HCK_PARTIALCKSUM) 11690 IP_STAT(ip_udp_in_part_hw_cksum_err); 11691 else 11692 IP_STAT(ip_udp_in_sw_cksum_err); 11693 11694 freemsg(first_mp); 11695 goto slow_done; 11696 } 11697 } 11698 udpslowpath: 11699 11700 /* Clear hardware checksum flag to be safe */ 11701 DB_CKSUMFLAGS(mp) = 0; 11702 11703 ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, 11704 (ire->ire_type == IRE_BROADCAST), 11705 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IP6INFO, 11706 mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); 11707 11708 slow_done: 11709 IP_STAT(ip_udp_slow_path); 11710 return; 11711 11712 #undef iphs 11713 #undef rptr 11714 } 11715 11716 /* ARGSUSED */ 11717 static mblk_t * 11718 ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 11719 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, 11720 ill_rx_ring_t *ill_ring) 11721 { 11722 conn_t *connp; 11723 uint32_t sum; 11724 uint32_t u1; 11725 uint16_t *up; 11726 int offset; 11727 ssize_t len; 11728 mblk_t *mp1; 11729 boolean_t syn_present = B_FALSE; 11730 tcph_t *tcph; 11731 uint_t ip_hdr_len; 11732 ill_t *ill = (ill_t *)q->q_ptr; 11733 zoneid_t zoneid = ire->ire_zoneid; 11734 boolean_t cksum_err; 11735 uint16_t hck_flags = 0; 11736 11737 #define rptr ((uchar_t *)ipha) 11738 11739 ASSERT(ipha->ipha_protocol == IPPROTO_TCP); 11740 11741 /* 11742 * FAST PATH for tcp packets 11743 */ 11744 11745 /* u1 is # words of IP options */ 11746 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 11747 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 11748 11749 /* IP options present */ 11750 if (u1) { 11751 goto ipoptions; 11752 } else { 11753 /* Check the IP header checksum. */ 11754 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 11755 /* Clear the IP header h/w cksum flag */ 11756 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 11757 } else { 11758 #define uph ((uint16_t *)ipha) 11759 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 11760 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 11761 #undef uph 11762 /* finish doing IP checksum */ 11763 sum = (sum & 0xFFFF) + (sum >> 16); 11764 sum = ~(sum + (sum >> 16)) & 0xFFFF; 11765 /* 11766 * Don't verify header checksum if this packet 11767 * is coming back from AH/ESP as we already did it. 11768 */ 11769 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 11770 BUMP_MIB(&ip_mib, ipInCksumErrs); 11771 goto error; 11772 } 11773 } 11774 } 11775 11776 if (!mctl_present) { 11777 UPDATE_IB_PKT_COUNT(ire); 11778 ire->ire_last_used_time = lbolt; 11779 } 11780 11781 /* packet part of fragmented IP packet? */ 11782 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 11783 if (u1 & (IPH_MF | IPH_OFFSET)) { 11784 goto fragmented; 11785 } 11786 11787 /* u1 = IP header length (20 bytes) */ 11788 u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; 11789 11790 /* does packet contain IP+TCP headers? */ 11791 len = mp->b_wptr - rptr; 11792 if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { 11793 IP_STAT(ip_tcppullup); 11794 goto tcppullup; 11795 } 11796 11797 /* TCP options present? */ 11798 offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; 11799 11800 /* 11801 * If options need to be pulled up, then goto tcpoptions. 11802 * otherwise we are still in the fast path 11803 */ 11804 if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { 11805 IP_STAT(ip_tcpoptions); 11806 goto tcpoptions; 11807 } 11808 11809 /* multiple mblks of tcp data? */ 11810 if ((mp1 = mp->b_cont) != NULL) { 11811 /* more then two? */ 11812 if (mp1->b_cont != NULL) { 11813 IP_STAT(ip_multipkttcp); 11814 goto multipkttcp; 11815 } 11816 len += mp1->b_wptr - mp1->b_rptr; 11817 } 11818 11819 up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); 11820 11821 /* part of pseudo checksum */ 11822 11823 /* TCP datagram length */ 11824 u1 = len - IP_SIMPLE_HDR_LENGTH; 11825 11826 #define iphs ((uint16_t *)ipha) 11827 11828 #ifdef _BIG_ENDIAN 11829 u1 += IPPROTO_TCP; 11830 #else 11831 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 11832 #endif 11833 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 11834 11835 /* 11836 * Revert to software checksum calculation if the interface 11837 * isn't capable of checksum offload or if IPsec is present. 11838 */ 11839 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 11840 hck_flags = DB_CKSUMFLAGS(mp); 11841 11842 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 11843 IP_STAT(ip_in_sw_cksum); 11844 11845 IP_CKSUM_RECV(hck_flags, u1, 11846 (uchar_t *)(rptr + DB_CKSUMSTART(mp)), 11847 (int32_t)((uchar_t *)up - rptr), 11848 mp, mp1, cksum_err); 11849 11850 if (cksum_err) { 11851 BUMP_MIB(&ip_mib, tcpInErrs); 11852 11853 if (hck_flags & HCK_FULLCKSUM) 11854 IP_STAT(ip_tcp_in_full_hw_cksum_err); 11855 else if (hck_flags & HCK_PARTIALCKSUM) 11856 IP_STAT(ip_tcp_in_part_hw_cksum_err); 11857 else 11858 IP_STAT(ip_tcp_in_sw_cksum_err); 11859 11860 goto error; 11861 } 11862 11863 try_again: 11864 11865 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid)) == 11866 NULL) { 11867 /* Send the TH_RST */ 11868 goto no_conn; 11869 } 11870 11871 /* 11872 * TCP FAST PATH for AF_INET socket. 11873 * 11874 * TCP fast path to avoid extra work. An AF_INET socket type 11875 * does not have facility to receive extra information via 11876 * ip_process or ip_add_info. Also, when the connection was 11877 * established, we made a check if this connection is impacted 11878 * by any global IPSec policy or per connection policy (a 11879 * policy that comes in effect later will not apply to this 11880 * connection). Since all this can be determined at the 11881 * connection establishment time, a quick check of flags 11882 * can avoid extra work. 11883 */ 11884 if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && 11885 !IPP_ENABLED(IPP_LOCAL_IN)) { 11886 ASSERT(first_mp == mp); 11887 SET_SQUEUE(mp, tcp_rput_data, connp); 11888 return (mp); 11889 } 11890 11891 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 11892 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 11893 if (IPCL_IS_TCP(connp)) { 11894 mp->b_datap->db_struioflag |= STRUIO_EAGER; 11895 DB_CKSUMSTART(mp) = 11896 (intptr_t)ip_squeue_get(ill_ring); 11897 if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && 11898 !CONN_INBOUND_POLICY_PRESENT(connp)) { 11899 SET_SQUEUE(mp, connp->conn_recv, connp); 11900 return (mp); 11901 } else if (IPCL_IS_BOUND(connp) && !mctl_present && 11902 !CONN_INBOUND_POLICY_PRESENT(connp)) { 11903 ip_squeue_enter_unbound++; 11904 SET_SQUEUE(mp, tcp_conn_request_unbound, 11905 connp); 11906 return (mp); 11907 } 11908 syn_present = B_TRUE; 11909 } 11910 11911 } 11912 11913 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 11914 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 11915 11916 /* No need to send this packet to TCP */ 11917 if ((flags & TH_RST) || (flags & TH_URG)) { 11918 CONN_DEC_REF(connp); 11919 freemsg(first_mp); 11920 return (NULL); 11921 } 11922 if (flags & TH_ACK) { 11923 tcp_xmit_listeners_reset(first_mp, ip_hdr_len); 11924 CONN_DEC_REF(connp); 11925 return (NULL); 11926 } 11927 11928 CONN_DEC_REF(connp); 11929 freemsg(first_mp); 11930 return (NULL); 11931 } 11932 11933 if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) { 11934 first_mp = ipsec_check_inbound_policy(first_mp, connp, 11935 ipha, NULL, mctl_present); 11936 if (first_mp == NULL) { 11937 CONN_DEC_REF(connp); 11938 return (NULL); 11939 } 11940 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 11941 ASSERT(syn_present); 11942 if (mctl_present) { 11943 ASSERT(first_mp != mp); 11944 first_mp->b_datap->db_struioflag |= 11945 STRUIO_POLICY; 11946 } else { 11947 ASSERT(first_mp == mp); 11948 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 11949 mp->b_datap->db_struioflag |= STRUIO_POLICY; 11950 } 11951 } else { 11952 /* 11953 * Discard first_mp early since we're dealing with a 11954 * fully-connected conn_t and tcp doesn't do policy in 11955 * this case. 11956 */ 11957 if (mctl_present) { 11958 freeb(first_mp); 11959 mctl_present = B_FALSE; 11960 } 11961 first_mp = mp; 11962 } 11963 } 11964 11965 /* Initiate IPPF processing for fastpath */ 11966 if (IPP_ENABLED(IPP_LOCAL_IN)) { 11967 uint32_t ill_index; 11968 11969 ill_index = recv_ill->ill_phyint->phyint_ifindex; 11970 ip_process(IPP_LOCAL_IN, &mp, ill_index); 11971 if (mp == NULL) { 11972 ip2dbg(("ip_input_ipsec_process: TCP pkt " 11973 "deferred/dropped during IPPF processing\n")); 11974 CONN_DEC_REF(connp); 11975 if (mctl_present) 11976 freeb(first_mp); 11977 return (NULL); 11978 } else if (mctl_present) { 11979 /* 11980 * ip_process might return a new mp. 11981 */ 11982 ASSERT(first_mp != mp); 11983 first_mp->b_cont = mp; 11984 } else { 11985 first_mp = mp; 11986 } 11987 11988 } 11989 11990 if (!syn_present && connp->conn_ipv6_recvpktinfo) { 11991 mp = ip_add_info(mp, recv_ill, flags); 11992 if (mp == NULL) { 11993 CONN_DEC_REF(connp); 11994 if (mctl_present) 11995 freeb(first_mp); 11996 return (NULL); 11997 } else if (mctl_present) { 11998 /* 11999 * ip_add_info might return a new mp. 12000 */ 12001 ASSERT(first_mp != mp); 12002 first_mp->b_cont = mp; 12003 } else { 12004 first_mp = mp; 12005 } 12006 } 12007 12008 if (IPCL_IS_TCP(connp)) { 12009 SET_SQUEUE(first_mp, connp->conn_recv, connp); 12010 return (first_mp); 12011 } else { 12012 putnext(connp->conn_rq, first_mp); 12013 CONN_DEC_REF(connp); 12014 return (NULL); 12015 } 12016 12017 no_conn: 12018 /* Initiate IPPf processing, if needed. */ 12019 if (IPP_ENABLED(IPP_LOCAL_IN)) { 12020 uint32_t ill_index; 12021 ill_index = recv_ill->ill_phyint->phyint_ifindex; 12022 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 12023 if (first_mp == NULL) { 12024 return (NULL); 12025 } 12026 } 12027 BUMP_MIB(&ip_mib, ipInDelivers); 12028 tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr)); 12029 return (NULL); 12030 ipoptions: 12031 if (!ip_options_cksum(q, first_mp, ipha, ire)) { 12032 goto slow_done; 12033 } 12034 12035 UPDATE_IB_PKT_COUNT(ire); 12036 ire->ire_last_used_time = lbolt; 12037 12038 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12039 if (u1 & (IPH_MF | IPH_OFFSET)) { 12040 fragmented: 12041 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 12042 if (mctl_present) 12043 freeb(first_mp); 12044 goto slow_done; 12045 } 12046 /* 12047 * Make sure that first_mp points back to mp as 12048 * the mp we came in with could have changed in 12049 * ip_rput_fragment(). 12050 */ 12051 ASSERT(!mctl_present); 12052 ipha = (ipha_t *)mp->b_rptr; 12053 first_mp = mp; 12054 } 12055 12056 tcp_slow: 12057 /* Now we have a complete datagram, destined for this machine. */ 12058 u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); 12059 12060 len = mp->b_wptr - mp->b_rptr; 12061 /* Pull up a minimal TCP header, if necessary. */ 12062 if (len < (u1 + 20)) { 12063 tcppullup: 12064 if (!pullupmsg(mp, u1 + 20)) { 12065 BUMP_MIB(&ip_mib, ipInDiscards); 12066 goto error; 12067 } 12068 ipha = (ipha_t *)mp->b_rptr; 12069 len = mp->b_wptr - mp->b_rptr; 12070 } 12071 12072 /* 12073 * Extract the offset field from the TCP header. As usual, we 12074 * try to help the compiler more than the reader. 12075 */ 12076 offset = ((uchar_t *)ipha)[u1 + 12] >> 4; 12077 if (offset != 5) { 12078 tcpoptions: 12079 if (offset < 5) { 12080 BUMP_MIB(&ip_mib, ipInDiscards); 12081 goto error; 12082 } 12083 /* 12084 * There must be TCP options. 12085 * Make sure we can grab them. 12086 */ 12087 offset <<= 2; 12088 offset += u1; 12089 if (len < offset) { 12090 if (!pullupmsg(mp, offset)) { 12091 BUMP_MIB(&ip_mib, ipInDiscards); 12092 goto error; 12093 } 12094 ipha = (ipha_t *)mp->b_rptr; 12095 len = mp->b_wptr - rptr; 12096 } 12097 } 12098 12099 /* Get the total packet length in len, including headers. */ 12100 if (mp->b_cont) { 12101 multipkttcp: 12102 len = msgdsize(mp); 12103 } 12104 12105 /* 12106 * Check the TCP checksum by pulling together the pseudo- 12107 * header checksum, and passing it to ip_csum to be added in 12108 * with the TCP datagram. 12109 * 12110 * Since we are not using the hwcksum if available we must 12111 * clear the flag. We may come here via tcppullup or tcpoptions. 12112 * If either of these fails along the way the mblk is freed. 12113 * If this logic ever changes and mblk is reused to say send 12114 * ICMP's back, then this flag may need to be cleared in 12115 * other places as well. 12116 */ 12117 DB_CKSUMFLAGS(mp) = 0; 12118 12119 up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); 12120 12121 u1 = (uint32_t)(len - u1); /* TCP datagram length. */ 12122 #ifdef _BIG_ENDIAN 12123 u1 += IPPROTO_TCP; 12124 #else 12125 u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); 12126 #endif 12127 u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; 12128 /* 12129 * Not M_DATA mblk or its a dup, so do the checksum now. 12130 */ 12131 IP_STAT(ip_in_sw_cksum); 12132 if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { 12133 BUMP_MIB(&ip_mib, tcpInErrs); 12134 goto error; 12135 } 12136 12137 IP_STAT(ip_tcp_slow_path); 12138 goto try_again; 12139 #undef iphs 12140 #undef rptr 12141 12142 error: 12143 freemsg(first_mp); 12144 slow_done: 12145 return (NULL); 12146 } 12147 12148 /* ARGSUSED */ 12149 static void 12150 ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, 12151 ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) 12152 { 12153 conn_t *connp; 12154 uint32_t sum; 12155 uint32_t u1; 12156 ssize_t len; 12157 sctp_hdr_t *sctph; 12158 zoneid_t zoneid = ire->ire_zoneid; 12159 uint32_t pktsum; 12160 uint32_t calcsum; 12161 uint32_t ports; 12162 uint_t ipif_seqid; 12163 in6_addr_t map_src, map_dst; 12164 ill_t *ill = (ill_t *)q->q_ptr; 12165 12166 #define rptr ((uchar_t *)ipha) 12167 12168 ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); 12169 12170 /* u1 is # words of IP options */ 12171 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 12172 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 12173 12174 /* IP options present */ 12175 if (u1 > 0) { 12176 goto ipoptions; 12177 } else { 12178 /* Check the IP header checksum. */ 12179 if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { 12180 /* 12181 * Since there is no SCTP h/w cksum support yet, just 12182 * clear the flag. 12183 */ 12184 DB_CKSUMFLAGS(mp) = 0; 12185 } else { 12186 #define uph ((uint16_t *)ipha) 12187 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + 12188 uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; 12189 #undef uph 12190 /* finish doing IP checksum */ 12191 sum = (sum & 0xFFFF) + (sum >> 16); 12192 sum = ~(sum + (sum >> 16)) & 0xFFFF; 12193 /* 12194 * Don't verify header checksum if this packet 12195 * is coming back from AH/ESP as we already did it. 12196 */ 12197 if (!mctl_present && (sum != 0) && sum != 0xFFFF) { 12198 BUMP_MIB(&ip_mib, ipInCksumErrs); 12199 goto error; 12200 } 12201 } 12202 } 12203 12204 /* 12205 * Don't verify header checksum if this packet is coming 12206 * back from AH/ESP as we already did it. 12207 */ 12208 if (!mctl_present) { 12209 UPDATE_IB_PKT_COUNT(ire); 12210 ire->ire_last_used_time = lbolt; 12211 } 12212 12213 /* packet part of fragmented IP packet? */ 12214 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12215 if (u1 & (IPH_MF | IPH_OFFSET)) 12216 goto fragmented; 12217 12218 /* u1 = IP header length (20 bytes) */ 12219 u1 = IP_SIMPLE_HDR_LENGTH; 12220 12221 find_sctp_client: 12222 /* Pullup if we don't have the sctp common header. */ 12223 len = MBLKL(mp); 12224 if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { 12225 if (mp->b_cont == NULL || 12226 !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { 12227 BUMP_MIB(&ip_mib, ipInDiscards); 12228 goto error; 12229 } 12230 ipha = (ipha_t *)mp->b_rptr; 12231 len = MBLKL(mp); 12232 } 12233 12234 sctph = (sctp_hdr_t *)(rptr + u1); 12235 #ifdef DEBUG 12236 if (!skip_sctp_cksum) { 12237 #endif 12238 pktsum = sctph->sh_chksum; 12239 sctph->sh_chksum = 0; 12240 calcsum = sctp_cksum(mp, u1); 12241 if (calcsum != pktsum) { 12242 BUMP_MIB(&sctp_mib, sctpChecksumError); 12243 goto error; 12244 } 12245 sctph->sh_chksum = pktsum; 12246 #ifdef DEBUG /* skip_sctp_cksum */ 12247 } 12248 #endif 12249 /* get the ports */ 12250 ports = *(uint32_t *)&sctph->sh_sport; 12251 12252 ipif_seqid = ire->ire_ipif->ipif_seqid; 12253 IRE_REFRELE(ire); 12254 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); 12255 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); 12256 if ((connp = sctp_find_conn(&map_src, &map_dst, ports, ipif_seqid, 12257 zoneid)) == NULL) { 12258 /* Check for raw socket or OOTB handling */ 12259 goto no_conn; 12260 } 12261 12262 /* Found a client; up it goes */ 12263 BUMP_MIB(&ip_mib, ipInDelivers); 12264 sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); 12265 return; 12266 12267 no_conn: 12268 ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, 12269 ports, mctl_present, flags, B_TRUE, ipif_seqid, zoneid); 12270 return; 12271 12272 ipoptions: 12273 DB_CKSUMFLAGS(mp) = 0; 12274 if (!ip_options_cksum(q, first_mp, ipha, ire)) 12275 goto slow_done; 12276 12277 UPDATE_IB_PKT_COUNT(ire); 12278 ire->ire_last_used_time = lbolt; 12279 12280 u1 = ntohs(ipha->ipha_fragment_offset_and_flags); 12281 if (u1 & (IPH_MF | IPH_OFFSET)) { 12282 fragmented: 12283 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) 12284 goto slow_done; 12285 /* 12286 * Make sure that first_mp points back to mp as 12287 * the mp we came in with could have changed in 12288 * ip_rput_fragment(). 12289 */ 12290 ASSERT(!mctl_present); 12291 ipha = (ipha_t *)mp->b_rptr; 12292 first_mp = mp; 12293 } 12294 12295 /* Now we have a complete datagram, destined for this machine. */ 12296 u1 = IPH_HDR_LENGTH(ipha); 12297 goto find_sctp_client; 12298 #undef iphs 12299 #undef rptr 12300 12301 error: 12302 freemsg(first_mp); 12303 slow_done: 12304 IRE_REFRELE(ire); 12305 } 12306 12307 #define VER_BITS 0xF0 12308 #define VERSION_6 0x60 12309 12310 static boolean_t 12311 ip_rput_multimblk_ipoptions(queue_t *q, mblk_t *mp, ipha_t **iphapp, 12312 ipaddr_t *dstp) 12313 { 12314 uint_t opt_len; 12315 ipha_t *ipha; 12316 ssize_t len; 12317 uint_t pkt_len; 12318 12319 IP_STAT(ip_ipoptions); 12320 ipha = *iphapp; 12321 12322 #define rptr ((uchar_t *)ipha) 12323 /* Assume no IPv6 packets arrive over the IPv4 queue */ 12324 if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { 12325 BUMP_MIB(&ip_mib, ipInIPv6); 12326 freemsg(mp); 12327 return (B_FALSE); 12328 } 12329 12330 /* multiple mblk or too short */ 12331 pkt_len = ntohs(ipha->ipha_length); 12332 12333 /* Get the number of words of IP options in the IP header. */ 12334 opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; 12335 if (opt_len) { 12336 /* IP Options present! Validate and process. */ 12337 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 12338 BUMP_MIB(&ip_mib, ipInHdrErrors); 12339 goto done; 12340 } 12341 /* 12342 * Recompute complete header length and make sure we 12343 * have access to all of it. 12344 */ 12345 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 12346 if (len > (mp->b_wptr - rptr)) { 12347 if (len > pkt_len) { 12348 BUMP_MIB(&ip_mib, ipInHdrErrors); 12349 goto done; 12350 } 12351 if (!pullupmsg(mp, len)) { 12352 BUMP_MIB(&ip_mib, ipInDiscards); 12353 goto done; 12354 } 12355 ipha = (ipha_t *)mp->b_rptr; 12356 } 12357 /* 12358 * Go off to ip_rput_options which returns the next hop 12359 * destination address, which may have been affected 12360 * by source routing. 12361 */ 12362 IP_STAT(ip_opt); 12363 if (ip_rput_options(q, mp, ipha, dstp) == -1) { 12364 return (B_FALSE); 12365 } 12366 } 12367 *iphapp = ipha; 12368 return (B_TRUE); 12369 done: 12370 /* clear b_prev - used by ip_mroute_decap */ 12371 mp->b_prev = NULL; 12372 freemsg(mp); 12373 return (B_FALSE); 12374 #undef rptr 12375 } 12376 12377 /* 12378 * Deal with the fact that there is no ire for the destination. 12379 * The incoming ill (in_ill) is passed in to ip_newroute only 12380 * in the case of packets coming from mobile ip forward tunnel. 12381 * It must be null otherwise. 12382 */ 12383 static void 12384 ip_rput_noire(queue_t *q, ill_t *in_ill, mblk_t *mp, int ll_multicast, 12385 ipaddr_t dst) 12386 { 12387 ipha_t *ipha; 12388 ill_t *ill; 12389 12390 ipha = (ipha_t *)mp->b_rptr; 12391 ill = (ill_t *)q->q_ptr; 12392 12393 ASSERT(ill != NULL); 12394 /* 12395 * No IRE for this destination, so it can't be for us. 12396 * Unless we are forwarding, drop the packet. 12397 * We have to let source routed packets through 12398 * since we don't yet know if they are 'ping -l' 12399 * packets i.e. if they will go out over the 12400 * same interface as they came in on. 12401 */ 12402 if (ll_multicast) { 12403 freemsg(mp); 12404 return; 12405 } 12406 if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha)) { 12407 BUMP_MIB(&ip_mib, ipForwProhibits); 12408 freemsg(mp); 12409 return; 12410 } 12411 12412 /* Check for Martian addresses */ 12413 if ((in_ill == NULL) && (ip_no_forward(ipha, ill))) { 12414 freemsg(mp); 12415 return; 12416 } 12417 12418 /* Mark this packet as having originated externally */ 12419 mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; 12420 12421 /* 12422 * Clear the indication that this may have a hardware checksum 12423 * as we are not using it 12424 */ 12425 DB_CKSUMFLAGS(mp) = 0; 12426 12427 /* 12428 * Now hand the packet to ip_newroute. 12429 */ 12430 ip_newroute(q, mp, dst, in_ill, NULL); 12431 } 12432 12433 /* 12434 * check ip header length and align it. 12435 */ 12436 static boolean_t 12437 ip_check_and_align_header(queue_t *q, mblk_t *mp) 12438 { 12439 ssize_t len; 12440 ill_t *ill; 12441 ipha_t *ipha; 12442 12443 len = MBLKL(mp); 12444 12445 if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { 12446 if (!OK_32PTR(mp->b_rptr)) 12447 IP_STAT(ip_notaligned1); 12448 else 12449 IP_STAT(ip_notaligned2); 12450 /* Guard against bogus device drivers */ 12451 if (len < 0) { 12452 /* clear b_prev - used by ip_mroute_decap */ 12453 mp->b_prev = NULL; 12454 BUMP_MIB(&ip_mib, ipInHdrErrors); 12455 freemsg(mp); 12456 return (B_FALSE); 12457 } 12458 12459 if (ip_rput_pullups++ == 0) { 12460 ill = (ill_t *)q->q_ptr; 12461 ipha = (ipha_t *)mp->b_rptr; 12462 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 12463 "ip_check_and_align_header: %s forced us to " 12464 " pullup pkt, hdr len %ld, hdr addr %p", 12465 ill->ill_name, len, ipha); 12466 } 12467 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 12468 /* clear b_prev - used by ip_mroute_decap */ 12469 mp->b_prev = NULL; 12470 BUMP_MIB(&ip_mib, ipInDiscards); 12471 freemsg(mp); 12472 return (B_FALSE); 12473 } 12474 } 12475 return (B_TRUE); 12476 } 12477 12478 static boolean_t 12479 ip_rput_notforus(queue_t **qp, mblk_t *mp, ire_t *ire, ill_t *ill) 12480 { 12481 ill_group_t *ill_group; 12482 ill_group_t *ire_group; 12483 queue_t *q; 12484 ill_t *ire_ill; 12485 uint_t ill_ifindex; 12486 12487 q = *qp; 12488 /* 12489 * We need to check to make sure the packet came in 12490 * on the queue associated with the destination IRE. 12491 * Note that for multicast packets and broadcast packets sent to 12492 * a broadcast address which is shared between multiple interfaces 12493 * we should not do this since we just got a random broadcast ire. 12494 */ 12495 if (ire->ire_rfq && ire->ire_type != IRE_BROADCAST) { 12496 boolean_t check_multi = B_TRUE; 12497 12498 /* 12499 * This packet came in on an interface other than the 12500 * one associated with the destination address. 12501 * "Gateway" it to the appropriate interface here. 12502 * As long as the ills belong to the same group, 12503 * we don't consider them to arriving on the wrong 12504 * interface. Thus, when the switch is doing inbound 12505 * load spreading, we won't drop packets when we 12506 * are doing strict multihoming checks. Note, the 12507 * same holds true for 'usesrc groups' where the 12508 * destination address may belong to another interface 12509 * to allow multipathing to happen 12510 */ 12511 ill_group = ill->ill_group; 12512 ire_ill = (ill_t *)(ire->ire_rfq)->q_ptr; 12513 ill_ifindex = ill->ill_usesrc_ifindex; 12514 ire_group = ire_ill->ill_group; 12515 12516 /* 12517 * If it's part of the same IPMP group, or if it's a legal 12518 * address on the 'usesrc' interface, then bypass strict 12519 * checks. 12520 */ 12521 if (ill_group != NULL && ill_group == ire_group) { 12522 check_multi = B_FALSE; 12523 } else if (ill_ifindex != 0 && 12524 ill_ifindex == ire_ill->ill_phyint->phyint_ifindex) { 12525 check_multi = B_FALSE; 12526 } 12527 12528 if (check_multi && 12529 ip_strict_dst_multihoming && 12530 ((ill->ill_flags & 12531 ire->ire_ipif->ipif_ill->ill_flags & 12532 ILLF_ROUTER) == 0)) { 12533 /* Drop packet */ 12534 BUMP_MIB(&ip_mib, ipForwProhibits); 12535 freemsg(mp); 12536 ire_refrele(ire); 12537 return (B_TRUE); 12538 } 12539 12540 /* 12541 * Change the queue (for non-virtual destination network 12542 * interfaces) and ip_rput_local will be called with the right 12543 * queue 12544 */ 12545 q = ire->ire_rfq; 12546 } 12547 /* Must be broadcast. We'll take it. */ 12548 *qp = q; 12549 return (B_FALSE); 12550 } 12551 12552 static void 12553 ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, 12554 ill_t *ill, int ll_multicast) 12555 { 12556 ill_group_t *ill_group; 12557 ill_group_t *ire_group; 12558 queue_t *dev_q; 12559 12560 ASSERT(ire->ire_stq != NULL); 12561 if (ll_multicast != 0) 12562 goto drop_pkt; 12563 12564 if (ip_no_forward(ipha, ill)) 12565 goto drop_pkt; 12566 12567 ill_group = ill->ill_group; 12568 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 12569 /* 12570 * Check if we want to forward this one at this time. 12571 * We allow source routed packets on a host provided that 12572 * they go out the same interface or same interface group 12573 * as they came in on. 12574 * 12575 * XXX To be quicker, we may wish to not chase pointers to 12576 * get the ILLF_ROUTER flag and instead store the 12577 * forwarding policy in the ire. An unfortunate 12578 * side-effect of that would be requiring an ire flush 12579 * whenever the ILLF_ROUTER flag changes. 12580 */ 12581 if (((ill->ill_flags & 12582 ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & 12583 ILLF_ROUTER) == 0) && 12584 !(ip_source_routed(ipha) && (ire->ire_rfq == q || 12585 (ill_group != NULL && ill_group == ire_group)))) { 12586 BUMP_MIB(&ip_mib, ipForwProhibits); 12587 if (ip_source_routed(ipha)) { 12588 q = WR(q); 12589 /* 12590 * Clear the indication that this may have 12591 * hardware checksum as we are not using it. 12592 */ 12593 DB_CKSUMFLAGS(mp) = 0; 12594 icmp_unreachable(q, mp, 12595 ICMP_SOURCE_ROUTE_FAILED); 12596 ire_refrele(ire); 12597 return; 12598 } 12599 goto drop_pkt; 12600 } 12601 12602 /* Packet is being forwarded. Turning off hwcksum flag. */ 12603 DB_CKSUMFLAGS(mp) = 0; 12604 if (ip_g_send_redirects) { 12605 /* 12606 * Check whether the incoming interface and outgoing 12607 * interface is part of the same group. If so, 12608 * send redirects. 12609 * 12610 * Check the source address to see if it originated 12611 * on the same logical subnet it is going back out on. 12612 * If so, we should be able to send it a redirect. 12613 * Avoid sending a redirect if the destination 12614 * is directly connected (gw_addr == 0), 12615 * or if the packet was source routed out this 12616 * interface. 12617 */ 12618 ipaddr_t src; 12619 mblk_t *mp1; 12620 ire_t *src_ire = NULL; 12621 12622 /* 12623 * Check whether ire_rfq and q are from the same ill 12624 * or if they are not same, they at least belong 12625 * to the same group. If so, send redirects. 12626 */ 12627 if ((ire->ire_rfq == q || 12628 (ill_group != NULL && ill_group == ire_group)) && 12629 (ire->ire_gateway_addr != 0) && 12630 !ip_source_routed(ipha)) { 12631 12632 src = ipha->ipha_src; 12633 src_ire = ire_ftable_lookup(src, 0, 0, 12634 IRE_INTERFACE, ire->ire_ipif, NULL, ALL_ZONES, 12635 0, MATCH_IRE_IPIF | MATCH_IRE_TYPE); 12636 12637 if (src_ire != NULL) { 12638 /* 12639 * The source is directly connected. 12640 * Just copy the ip header (which is 12641 * in the first mblk) 12642 */ 12643 mp1 = copyb(mp); 12644 if (mp1 != NULL) { 12645 icmp_send_redirect(WR(q), mp1, 12646 ire->ire_gateway_addr); 12647 } 12648 ire_refrele(src_ire); 12649 } 12650 } 12651 } 12652 12653 dev_q = ire->ire_stq->q_next; 12654 if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { 12655 BUMP_MIB(&ip_mib, ipInDiscards); 12656 freemsg(mp); 12657 ire_refrele(ire); 12658 return; 12659 } 12660 12661 ip_rput_forward(ire, ipha, mp, ill); 12662 IRE_REFRELE(ire); 12663 return; 12664 12665 drop_pkt: 12666 ire_refrele(ire); 12667 ip2dbg(("ip_rput_forward: drop pkt\n")); 12668 freemsg(mp); 12669 } 12670 12671 static boolean_t 12672 ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t **irep, ipha_t *ipha, 12673 ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) 12674 { 12675 queue_t *q; 12676 ire_t *ire; 12677 uint16_t hcksumflags; 12678 12679 q = *qp; 12680 ire = *irep; 12681 12682 /* 12683 * Clear the indication that this may have hardware 12684 * checksum as we are not using it for forwarding. 12685 */ 12686 hcksumflags = DB_CKSUMFLAGS(mp); 12687 DB_CKSUMFLAGS(mp) = 0; 12688 12689 /* 12690 * Directed broadcast forwarding: if the packet came in over a 12691 * different interface then it is routed out over we can forward it. 12692 */ 12693 if (ipha->ipha_protocol == IPPROTO_TCP) { 12694 ire_refrele(ire); 12695 freemsg(mp); 12696 BUMP_MIB(&ip_mib, ipInDiscards); 12697 return (B_TRUE); 12698 } 12699 /* 12700 * For multicast we have set dst to be INADDR_BROADCAST 12701 * for delivering to all STREAMS. IRE_MARK_NORECV is really 12702 * only for broadcast packets. 12703 */ 12704 if (!CLASSD(ipha->ipha_dst)) { 12705 ire_t *new_ire; 12706 ipif_t *ipif; 12707 /* 12708 * For ill groups, as the switch duplicates broadcasts 12709 * across all the ports, we need to filter out and 12710 * send up only one copy. There is one copy for every 12711 * broadcast address on each ill. Thus, we look for a 12712 * specific IRE on this ill and look at IRE_MARK_NORECV 12713 * later to see whether this ill is eligible to receive 12714 * them or not. ill_nominate_bcast_rcv() nominates only 12715 * one set of IREs for receiving. 12716 */ 12717 12718 ipif = ipif_get_next_ipif(NULL, ill); 12719 if (ipif == NULL) { 12720 ire_refrele(ire); 12721 freemsg(mp); 12722 BUMP_MIB(&ip_mib, ipInDiscards); 12723 return (B_TRUE); 12724 } 12725 new_ire = ire_ctable_lookup(dst, 0, 0, 12726 ipif, ALL_ZONES, MATCH_IRE_ILL); 12727 ipif_refrele(ipif); 12728 12729 if (new_ire != NULL) { 12730 if (new_ire->ire_marks & IRE_MARK_NORECV) { 12731 ire_refrele(ire); 12732 ire_refrele(new_ire); 12733 freemsg(mp); 12734 BUMP_MIB(&ip_mib, ipInDiscards); 12735 return (B_TRUE); 12736 } 12737 /* 12738 * In the special case of multirouted broadcast 12739 * packets, we unconditionally need to "gateway" 12740 * them to the appropriate interface here. 12741 * In the normal case, this cannot happen, because 12742 * there is no broadcast IRE tagged with the 12743 * RTF_MULTIRT flag. 12744 */ 12745 if (new_ire->ire_flags & RTF_MULTIRT) { 12746 ire_refrele(new_ire); 12747 if (ire->ire_rfq != NULL) { 12748 q = ire->ire_rfq; 12749 *qp = q; 12750 } 12751 } else { 12752 ire_refrele(ire); 12753 ire = new_ire; 12754 } 12755 } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { 12756 if (!ip_g_forward_directed_bcast) { 12757 /* 12758 * Free the message if 12759 * ip_g_forward_directed_bcast is turned 12760 * off for non-local broadcast. 12761 */ 12762 ire_refrele(ire); 12763 freemsg(mp); 12764 BUMP_MIB(&ip_mib, ipInDiscards); 12765 return (B_TRUE); 12766 } 12767 } else { 12768 /* 12769 * This CGTP packet successfully passed the 12770 * CGTP filter, but the related CGTP 12771 * broadcast IRE has not been found, 12772 * meaning that the redundant ipif is 12773 * probably down. However, if we discarded 12774 * this packet, its duplicate would be 12775 * filtered out by the CGTP filter so none 12776 * of them would get through. So we keep 12777 * going with this one. 12778 */ 12779 ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); 12780 if (ire->ire_rfq != NULL) { 12781 q = ire->ire_rfq; 12782 *qp = q; 12783 } 12784 } 12785 } 12786 if (ip_g_forward_directed_bcast && ll_multicast == 0) { 12787 /* 12788 * Verify that there are not more then one 12789 * IRE_BROADCAST with this broadcast address which 12790 * has ire_stq set. 12791 * TODO: simplify, loop over all IRE's 12792 */ 12793 ire_t *ire1; 12794 int num_stq = 0; 12795 mblk_t *mp1; 12796 12797 /* Find the first one with ire_stq set */ 12798 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 12799 for (ire1 = ire; ire1 && 12800 !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; 12801 ire1 = ire1->ire_next) 12802 ; 12803 if (ire1) { 12804 ire_refrele(ire); 12805 ire = ire1; 12806 IRE_REFHOLD(ire); 12807 } 12808 12809 /* Check if there are additional ones with stq set */ 12810 for (ire1 = ire; ire1; ire1 = ire1->ire_next) { 12811 if (ire->ire_addr != ire1->ire_addr) 12812 break; 12813 if (ire1->ire_stq) { 12814 num_stq++; 12815 break; 12816 } 12817 } 12818 rw_exit(&ire->ire_bucket->irb_lock); 12819 if (num_stq == 1 && ire->ire_stq != NULL) { 12820 ip1dbg(("ip_rput_process_broadcast: directed " 12821 "broadcast to 0x%x\n", 12822 ntohl(ire->ire_addr))); 12823 mp1 = copymsg(mp); 12824 if (mp1) { 12825 switch (ipha->ipha_protocol) { 12826 case IPPROTO_UDP: 12827 ip_udp_input(q, mp1, ipha, ire, ill); 12828 break; 12829 default: 12830 ip_proto_input(q, mp1, ipha, ire, ill); 12831 break; 12832 } 12833 } 12834 /* 12835 * Adjust ttl to 2 (1+1 - the forward engine 12836 * will decrement it by one. 12837 */ 12838 if (ip_csum_hdr(ipha)) { 12839 BUMP_MIB(&ip_mib, ipInCksumErrs); 12840 ip2dbg(("ip_rput_broadcast:drop pkt\n")); 12841 freemsg(mp); 12842 ire_refrele(ire); 12843 return (B_TRUE); 12844 } 12845 ipha->ipha_ttl = ip_broadcast_ttl + 1; 12846 ipha->ipha_hdr_checksum = 0; 12847 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 12848 ip_rput_process_forward(q, mp, ire, ipha, 12849 ill, ll_multicast); 12850 return (B_TRUE); 12851 } 12852 ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", 12853 ntohl(ire->ire_addr))); 12854 } 12855 12856 *irep = ire; 12857 12858 /* Restore any hardware checksum flags */ 12859 DB_CKSUMFLAGS(mp) = hcksumflags; 12860 return (B_FALSE); 12861 } 12862 12863 /* ARGSUSED */ 12864 static boolean_t 12865 ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, 12866 int *ll_multicast, ipaddr_t *dstp) 12867 { 12868 /* 12869 * Forward packets only if we have joined the allmulti 12870 * group on this interface. 12871 */ 12872 if (ip_g_mrouter && ill->ill_join_allmulti) { 12873 int retval; 12874 12875 /* 12876 * Clear the indication that this may have hardware 12877 * checksum as we are not using it. 12878 */ 12879 DB_CKSUMFLAGS(mp) = 0; 12880 retval = ip_mforward(ill, ipha, mp); 12881 /* ip_mforward updates mib variables if needed */ 12882 /* clear b_prev - used by ip_mroute_decap */ 12883 mp->b_prev = NULL; 12884 12885 switch (retval) { 12886 case 0: 12887 /* 12888 * pkt is okay and arrived on phyint. 12889 * 12890 * If we are running as a multicast router 12891 * we need to see all IGMP and/or PIM packets. 12892 */ 12893 if ((ipha->ipha_protocol == IPPROTO_IGMP) || 12894 (ipha->ipha_protocol == IPPROTO_PIM)) { 12895 goto done; 12896 } 12897 break; 12898 case -1: 12899 /* pkt is mal-formed, toss it */ 12900 goto drop_pkt; 12901 case 1: 12902 /* pkt is okay and arrived on a tunnel */ 12903 /* 12904 * If we are running a multicast router 12905 * we need to see all igmp packets. 12906 */ 12907 if (ipha->ipha_protocol == IPPROTO_IGMP) { 12908 *dstp = INADDR_BROADCAST; 12909 *ll_multicast = 1; 12910 return (B_FALSE); 12911 } 12912 12913 goto drop_pkt; 12914 } 12915 } 12916 12917 ILM_WALKER_HOLD(ill); 12918 if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { 12919 /* 12920 * This might just be caused by the fact that 12921 * multiple IP Multicast addresses map to the same 12922 * link layer multicast - no need to increment counter! 12923 */ 12924 ILM_WALKER_RELE(ill); 12925 freemsg(mp); 12926 return (B_TRUE); 12927 } 12928 ILM_WALKER_RELE(ill); 12929 done: 12930 ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); 12931 /* 12932 * This assumes the we deliver to all streams for multicast 12933 * and broadcast packets. 12934 */ 12935 *dstp = INADDR_BROADCAST; 12936 *ll_multicast = 1; 12937 return (B_FALSE); 12938 drop_pkt: 12939 ip2dbg(("ip_rput: drop pkt\n")); 12940 freemsg(mp); 12941 return (B_TRUE); 12942 } 12943 12944 static boolean_t 12945 ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, 12946 int *ll_multicast, mblk_t **mpp) 12947 { 12948 mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; 12949 boolean_t must_copy = B_FALSE; 12950 struct iocblk *iocp; 12951 ipha_t *ipha; 12952 12953 #define rptr ((uchar_t *)ipha) 12954 12955 first_mp = *first_mpp; 12956 mp = *mpp; 12957 12958 ASSERT(first_mp == mp); 12959 12960 /* 12961 * if db_ref > 1 then copymsg and free original. Packet may be 12962 * changed and do not want other entity who has a reference to this 12963 * message to trip over the changes. This is a blind change because 12964 * trying to catch all places that might change packet is too 12965 * difficult (since it may be a module above this one) 12966 * 12967 * This corresponds to the non-fast path case. We walk down the full 12968 * chain in this case, and check the db_ref count of all the dblks, 12969 * and do a copymsg if required. It is possible that the db_ref counts 12970 * of the data blocks in the mblk chain can be different. 12971 * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref 12972 * count of 1, followed by a M_DATA block with a ref count of 2, if 12973 * 'snoop' is running. 12974 */ 12975 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 12976 if (mp1->b_datap->db_ref > 1) { 12977 must_copy = B_TRUE; 12978 break; 12979 } 12980 } 12981 12982 if (must_copy) { 12983 mp1 = copymsg(mp); 12984 if (mp1 == NULL) { 12985 for (mp1 = mp; mp1 != NULL; 12986 mp1 = mp1->b_cont) { 12987 mp1->b_next = NULL; 12988 mp1->b_prev = NULL; 12989 } 12990 freemsg(mp); 12991 BUMP_MIB(&ip_mib, ipInDiscards); 12992 return (B_TRUE); 12993 } 12994 for (from_mp = mp, to_mp = mp1; from_mp != NULL; 12995 from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { 12996 /* Copy b_next - used in M_BREAK messages */ 12997 to_mp->b_next = from_mp->b_next; 12998 from_mp->b_next = NULL; 12999 /* Copy b_prev - used by ip_mroute_decap */ 13000 to_mp->b_prev = from_mp->b_prev; 13001 from_mp->b_prev = NULL; 13002 } 13003 *first_mpp = first_mp = mp1; 13004 freemsg(mp); 13005 mp = mp1; 13006 *mpp = mp1; 13007 } 13008 13009 ipha = (ipha_t *)mp->b_rptr; 13010 13011 /* 13012 * previous code has a case for M_DATA. 13013 * We want to check how that happens. 13014 */ 13015 ASSERT(first_mp->b_datap->db_type != M_DATA); 13016 switch (first_mp->b_datap->db_type) { 13017 case M_PROTO: 13018 case M_PCPROTO: 13019 if (((dl_unitdata_ind_t *)rptr)->dl_primitive != 13020 DL_UNITDATA_IND) { 13021 /* Go handle anything other than data elsewhere. */ 13022 ip_rput_dlpi(q, mp); 13023 return (B_TRUE); 13024 } 13025 *ll_multicast = ((dl_unitdata_ind_t *)rptr)->dl_group_address; 13026 /* Ditch the DLPI header. */ 13027 mp1 = mp->b_cont; 13028 ASSERT(first_mp == mp); 13029 *first_mpp = mp1; 13030 freeb(mp); 13031 *mpp = mp1; 13032 return (B_FALSE); 13033 case M_BREAK: 13034 /* 13035 * A packet arrives as M_BREAK following a cycle through 13036 * ip_rput, ip_newroute, ... and finally ire_add_then_send. 13037 * This is an IP datagram sans lower level header. 13038 * M_BREAK are also used to pass back in multicast packets 13039 * that are encapsulated with a source route. 13040 */ 13041 /* Ditch the M_BREAK mblk */ 13042 mp1 = mp->b_cont; 13043 ASSERT(first_mp == mp); 13044 *first_mpp = mp1; 13045 freeb(mp); 13046 mp = mp1; 13047 mp->b_next = NULL; 13048 *mpp = mp; 13049 *ll_multicast = 0; 13050 return (B_FALSE); 13051 case M_IOCACK: 13052 ip1dbg(("got iocack ")); 13053 iocp = (struct iocblk *)mp->b_rptr; 13054 switch (iocp->ioc_cmd) { 13055 case DL_IOC_HDR_INFO: 13056 ill = (ill_t *)q->q_ptr; 13057 ill_fastpath_ack(ill, mp); 13058 return (B_TRUE); 13059 case SIOCSTUNPARAM: 13060 case OSIOCSTUNPARAM: 13061 /* Go through qwriter_ip */ 13062 break; 13063 case SIOCGTUNPARAM: 13064 case OSIOCGTUNPARAM: 13065 ip_rput_other(NULL, q, mp, NULL); 13066 return (B_TRUE); 13067 default: 13068 putnext(q, mp); 13069 return (B_TRUE); 13070 } 13071 /* FALLTHRU */ 13072 case M_ERROR: 13073 case M_HANGUP: 13074 /* 13075 * Since this is on the ill stream we unconditionally 13076 * bump up the refcount 13077 */ 13078 ill_refhold(ill); 13079 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, 13080 B_FALSE); 13081 return (B_TRUE); 13082 case M_CTL: 13083 if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && 13084 (((da_ipsec_t *)first_mp->b_rptr)->da_type == 13085 IPHADA_M_CTL)) { 13086 /* 13087 * It's an IPsec accelerated packet. 13088 * Make sure that the ill from which we received the 13089 * packet has enabled IPsec hardware acceleration. 13090 */ 13091 if (!(ill->ill_capabilities & 13092 (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { 13093 /* IPsec kstats: bean counter */ 13094 freemsg(mp); 13095 return (B_TRUE); 13096 } 13097 13098 /* 13099 * Make mp point to the mblk following the M_CTL, 13100 * then process according to type of mp. 13101 * After this processing, first_mp will point to 13102 * the data-attributes and mp to the pkt following 13103 * the M_CTL. 13104 */ 13105 mp = first_mp->b_cont; 13106 if (mp == NULL) { 13107 freemsg(first_mp); 13108 return (B_TRUE); 13109 } 13110 /* 13111 * A Hardware Accelerated packet can only be M_DATA 13112 * ESP or AH packet. 13113 */ 13114 if (mp->b_datap->db_type != M_DATA) { 13115 /* non-M_DATA IPsec accelerated packet */ 13116 IPSECHW_DEBUG(IPSECHW_PKT, 13117 ("non-M_DATA IPsec accelerated pkt\n")); 13118 freemsg(first_mp); 13119 return (B_TRUE); 13120 } 13121 ipha = (ipha_t *)mp->b_rptr; 13122 if (ipha->ipha_protocol != IPPROTO_AH && 13123 ipha->ipha_protocol != IPPROTO_ESP) { 13124 IPSECHW_DEBUG(IPSECHW_PKT, 13125 ("non-M_DATA IPsec accelerated pkt\n")); 13126 freemsg(first_mp); 13127 return (B_TRUE); 13128 } 13129 *mpp = mp; 13130 return (B_FALSE); 13131 } 13132 putnext(q, mp); 13133 return (B_TRUE); 13134 case M_FLUSH: 13135 if (*mp->b_rptr & FLUSHW) { 13136 *mp->b_rptr &= ~FLUSHR; 13137 qreply(q, mp); 13138 return (B_TRUE); 13139 } 13140 freemsg(mp); 13141 return (B_TRUE); 13142 case M_IOCNAK: 13143 ip1dbg(("got iocnak ")); 13144 iocp = (struct iocblk *)mp->b_rptr; 13145 switch (iocp->ioc_cmd) { 13146 case DL_IOC_HDR_INFO: 13147 case SIOCSTUNPARAM: 13148 case OSIOCSTUNPARAM: 13149 /* 13150 * Since this is on the ill stream we unconditionally 13151 * bump up the refcount 13152 */ 13153 ill_refhold(ill); 13154 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_other, 13155 CUR_OP, B_FALSE); 13156 return (B_TRUE); 13157 case SIOCGTUNPARAM: 13158 case OSIOCGTUNPARAM: 13159 ip_rput_other(NULL, q, mp, NULL); 13160 return (B_TRUE); 13161 default: 13162 break; 13163 } 13164 /* FALLTHRU */ 13165 default: 13166 putnext(q, mp); 13167 return (B_TRUE); 13168 } 13169 } 13170 13171 /* Read side put procedure. Packets coming from the wire arrive here. */ 13172 void 13173 ip_rput(queue_t *q, mblk_t *mp) 13174 { 13175 ill_t *ill; 13176 13177 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); 13178 13179 ill = (ill_t *)q->q_ptr; 13180 13181 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 13182 union DL_primitives *dl; 13183 13184 /* 13185 * Things are opening or closing. Only accept DLPI control 13186 * messages. In the open case, the ill->ill_ipif has not yet 13187 * been created. In the close case, things hanging off the 13188 * ill could have been freed already. In either case it 13189 * may not be safe to proceed further. 13190 */ 13191 13192 dl = (union DL_primitives *)mp->b_rptr; 13193 if ((mp->b_datap->db_type != M_PCPROTO) || 13194 (dl->dl_primitive == DL_UNITDATA_IND)) { 13195 /* 13196 * Also SIOC[GS]TUN* ioctls can come here. 13197 */ 13198 inet_freemsg(mp); 13199 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 13200 "ip_input_end: q %p (%S)", q, "uninit"); 13201 return; 13202 } 13203 } 13204 13205 /* 13206 * if db_ref > 1 then copymsg and free original. Packet may be 13207 * changed and we do not want the other entity who has a reference to 13208 * this message to trip over the changes. This is a blind change because 13209 * trying to catch all places that might change the packet is too 13210 * difficult. 13211 * 13212 * This corresponds to the fast path case, where we have a chain of 13213 * M_DATA mblks. We check the db_ref count of only the 1st data block 13214 * in the mblk chain. There doesn't seem to be a reason why a device 13215 * driver would send up data with varying db_ref counts in the mblk 13216 * chain. In any case the Fast path is a private interface, and our 13217 * drivers don't do such a thing. Given the above assumption, there is 13218 * no need to walk down the entire mblk chain (which could have a 13219 * potential performance problem) 13220 */ 13221 if (mp->b_datap->db_ref > 1) { 13222 mblk_t *mp1; 13223 boolean_t adjusted = B_FALSE; 13224 IP_STAT(ip_db_ref); 13225 13226 /* 13227 * The IP_RECVSLLA option depends on having the link layer 13228 * header. First check that: 13229 * a> the underlying device is of type ether, since this 13230 * option is currently supported only over ethernet. 13231 * b> there is enough room to copy over the link layer header. 13232 * 13233 * Once the checks are done, adjust rptr so that the link layer 13234 * header will be copied via copymsg. Note that, IFT_ETHER may 13235 * be returned by some non-ethernet drivers but in this case the 13236 * second check will fail. 13237 */ 13238 if (ill->ill_type == IFT_ETHER && 13239 (mp->b_rptr - mp->b_datap->db_base) >= 13240 sizeof (struct ether_header)) { 13241 mp->b_rptr -= sizeof (struct ether_header); 13242 adjusted = B_TRUE; 13243 } 13244 mp1 = copymsg(mp); 13245 if (mp1 == NULL) { 13246 /* Clear b_next - used in M_BREAK messages */ 13247 mp->b_next = NULL; 13248 /* clear b_prev - used by ip_mroute_decap */ 13249 mp->b_prev = NULL; 13250 freemsg(mp); 13251 BUMP_MIB(&ip_mib, ipInDiscards); 13252 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 13253 "ip_rput_end: q %p (%S)", q, "copymsg"); 13254 return; 13255 } 13256 if (adjusted) { 13257 /* 13258 * Copy is done. Restore the pointer in the _new_ mblk 13259 */ 13260 mp1->b_rptr += sizeof (struct ether_header); 13261 } 13262 /* Copy b_next - used in M_BREAK messages */ 13263 mp1->b_next = mp->b_next; 13264 mp->b_next = NULL; 13265 /* Copy b_prev - used by ip_mroute_decap */ 13266 mp1->b_prev = mp->b_prev; 13267 mp->b_prev = NULL; 13268 freemsg(mp); 13269 mp = mp1; 13270 } 13271 13272 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 13273 "ip_rput_end: q %p (%S)", q, "end"); 13274 13275 ip_input(ill, NULL, mp, 0); 13276 } 13277 13278 /* 13279 * Direct read side procedure capable of dealing with chains. GLDv3 based 13280 * drivers call this function directly with mblk chains while STREAMS 13281 * read side procedure ip_rput() calls this for single packet with ip_ring 13282 * set to NULL to process one packet at a time. 13283 * 13284 * The ill will always be valid if this function is called directly from 13285 * the driver. 13286 */ 13287 /*ARGSUSED*/ 13288 void 13289 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, size_t hdrlen) 13290 { 13291 ipaddr_t dst; 13292 ire_t *ire; 13293 ipha_t *ipha; 13294 uint_t pkt_len; 13295 ssize_t len; 13296 uint_t opt_len; 13297 int ll_multicast; 13298 int cgtp_flt_pkt; 13299 queue_t *q = ill->ill_rq; 13300 squeue_t *curr_sqp = NULL; 13301 mblk_t *head = NULL; 13302 mblk_t *tail = NULL; 13303 mblk_t *first_mp; 13304 mblk_t *mp; 13305 int cnt = 0; 13306 13307 ASSERT(mp_chain != NULL); 13308 ASSERT(ill != NULL); 13309 13310 TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); 13311 13312 #define rptr ((uchar_t *)ipha) 13313 13314 while (mp_chain != NULL) { 13315 first_mp = mp = mp_chain; 13316 mp_chain = mp_chain->b_next; 13317 mp->b_next = NULL; 13318 ll_multicast = 0; 13319 ire = NULL; 13320 13321 /* 13322 * ip_input fast path 13323 */ 13324 13325 /* mblk type is not M_DATA */ 13326 if (mp->b_datap->db_type != M_DATA) { 13327 if (ip_rput_process_notdata(q, &first_mp, ill, 13328 &ll_multicast, &mp)) 13329 continue; 13330 } 13331 13332 ASSERT(mp->b_datap->db_type == M_DATA); 13333 ASSERT(mp->b_datap->db_ref == 1); 13334 13335 /* 13336 * Invoke the CGTP (multirouting) filtering module to process 13337 * the incoming packet. Packets identified as duplicates 13338 * must be discarded. Filtering is active only if the 13339 * the ip_cgtp_filter ndd variable is non-zero. 13340 */ 13341 cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; 13342 if (ip_cgtp_filter && (ip_cgtp_filter_ops != NULL)) { 13343 cgtp_flt_pkt = 13344 ip_cgtp_filter_ops->cfo_filter_fp(q, mp); 13345 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 13346 freemsg(first_mp); 13347 continue; 13348 } 13349 } 13350 13351 ipha = (ipha_t *)mp->b_rptr; 13352 len = mp->b_wptr - rptr; 13353 13354 BUMP_MIB(&ip_mib, ipInReceives); 13355 13356 /* 13357 * IP header ptr not aligned? 13358 * OR IP header not complete in first mblk 13359 */ 13360 if (!OK_32PTR(rptr) || len < IP_SIMPLE_HDR_LENGTH) { 13361 if (!ip_check_and_align_header(q, mp)) 13362 continue; 13363 ipha = (ipha_t *)mp->b_rptr; 13364 len = mp->b_wptr - rptr; 13365 } 13366 13367 /* multiple mblk or too short */ 13368 pkt_len = ntohs(ipha->ipha_length); 13369 len -= pkt_len; 13370 if (len != 0) { 13371 /* 13372 * Make sure we have data length consistent 13373 * with the IP header. 13374 */ 13375 if (mp->b_cont == NULL) { 13376 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 13377 BUMP_MIB(&ip_mib, ipInHdrErrors); 13378 ip2dbg(("ip_input: drop pkt\n")); 13379 freemsg(mp); 13380 continue; 13381 } 13382 mp->b_wptr = rptr + pkt_len; 13383 } else if (len += msgdsize(mp->b_cont)) { 13384 if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { 13385 BUMP_MIB(&ip_mib, ipInHdrErrors); 13386 ip2dbg(("ip_input: drop pkt\n")); 13387 freemsg(mp); 13388 continue; 13389 } 13390 (void) adjmsg(mp, -len); 13391 IP_STAT(ip_multimblk3); 13392 } 13393 } 13394 13395 if (ip_loopback_src_or_dst(ipha, ill)) { 13396 ip2dbg(("ip_input: drop pkt\n")); 13397 freemsg(mp); 13398 continue; 13399 } 13400 13401 opt_len = ipha->ipha_version_and_hdr_length - 13402 IP_SIMPLE_HDR_VERSION; 13403 /* IP version bad or there are IP options */ 13404 if (opt_len) { 13405 if (len != 0) 13406 IP_STAT(ip_multimblk4); 13407 else 13408 IP_STAT(ip_ipoptions); 13409 if (!ip_rput_multimblk_ipoptions(q, mp, &ipha, &dst)) 13410 continue; 13411 } else { 13412 dst = ipha->ipha_dst; 13413 } 13414 13415 /* 13416 * If rsvpd is running, let RSVP daemon handle its processing 13417 * and forwarding of RSVP multicast/unicast packets. 13418 * If rsvpd is not running but mrouted is running, RSVP 13419 * multicast packets are forwarded as multicast traffic 13420 * and RSVP unicast packets are forwarded by unicast router. 13421 * If neither rsvpd nor mrouted is running, RSVP multicast 13422 * packets are not forwarded, but the unicast packets are 13423 * forwarded like unicast traffic. 13424 */ 13425 if (ipha->ipha_protocol == IPPROTO_RSVP && 13426 ipcl_proto_search(IPPROTO_RSVP) != NULL) { 13427 /* RSVP packet and rsvpd running. Treat as ours */ 13428 ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); 13429 /* 13430 * This assumes that we deliver to all streams for 13431 * multicast and broadcast packets. 13432 * We have to force ll_multicast to 1 to handle the 13433 * M_DATA messages passed in from ip_mroute_decap. 13434 */ 13435 dst = INADDR_BROADCAST; 13436 ll_multicast = 1; 13437 } else if (CLASSD(dst)) { 13438 /* packet is multicast */ 13439 mp->b_next = NULL; 13440 if (ip_rput_process_multicast(q, mp, ill, ipha, 13441 &ll_multicast, &dst)) 13442 continue; 13443 } 13444 13445 13446 /* 13447 * Check if the packet is coming from the Mobile IP 13448 * forward tunnel interface 13449 */ 13450 if (ill->ill_srcif_refcnt > 0) { 13451 ire = ire_srcif_table_lookup(dst, IRE_INTERFACE, 13452 NULL, ill, MATCH_IRE_TYPE); 13453 if (ire != NULL && ire->ire_dlureq_mp == NULL && 13454 ire->ire_ipif->ipif_net_type == 13455 IRE_IF_RESOLVER) { 13456 /* We need to resolve the link layer info */ 13457 ire_refrele(ire); 13458 ip_rput_noire(q, (ill_t *)q->q_ptr, mp, 13459 ll_multicast, dst); 13460 continue; 13461 } 13462 } 13463 13464 if (ire == NULL) 13465 ire = ire_cache_lookup(dst, ALL_ZONES); 13466 13467 /* 13468 * If mipagent is running and reverse tunnel is created as per 13469 * mobile node request, then any packet coming through the 13470 * incoming interface from the mobile-node, should be reverse 13471 * tunneled to it's home agent except those that are destined 13472 * to foreign agent only. 13473 * This needs source address based ire lookup. The routing 13474 * entries for source address based lookup are only created by 13475 * mipagent program only when a reverse tunnel is created. 13476 * Reference : RFC2002, RFC2344 13477 */ 13478 if (ill->ill_mrtun_refcnt > 0) { 13479 ipaddr_t srcaddr; 13480 ire_t *tmp_ire; 13481 13482 tmp_ire = ire; /* Save, we might need it later */ 13483 if (ire == NULL || (ire->ire_type != IRE_LOCAL && 13484 ire->ire_type != IRE_BROADCAST)) { 13485 srcaddr = ipha->ipha_src; 13486 ire = ire_mrtun_lookup(srcaddr, ill); 13487 if (ire != NULL) { 13488 /* 13489 * Should not be getting iphada packet 13490 * here. we should only get those for 13491 * IRE_LOCAL traffic, excluded above. 13492 * Fail-safe (drop packet) in the event 13493 * hardware is misbehaving. 13494 */ 13495 if (first_mp != mp) { 13496 /* IPsec KSTATS: beancount me */ 13497 freemsg(first_mp); 13498 } else { 13499 /* 13500 * This packet must be forwarded 13501 * to Reverse Tunnel 13502 */ 13503 ip_mrtun_forward(ire, ill, mp); 13504 } 13505 ire_refrele(ire); 13506 if (tmp_ire != NULL) 13507 ire_refrele(tmp_ire); 13508 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 13509 "ip_input_end: q %p (%S)", 13510 q, "uninit"); 13511 continue; 13512 } 13513 } 13514 /* 13515 * If this packet is from a non-mobilenode or a 13516 * mobile-node which does not request reverse 13517 * tunnel service 13518 */ 13519 ire = tmp_ire; 13520 } 13521 13522 13523 /* 13524 * If we reach here that means the incoming packet satisfies 13525 * one of the following conditions: 13526 * - packet is from a mobile node which does not request 13527 * reverse tunnel 13528 * - packet is from a non-mobile node, which is the most 13529 * common case 13530 * - packet is from a reverse tunnel enabled mobile node 13531 * and destined to foreign agent only 13532 */ 13533 13534 if (ire == NULL) { 13535 /* 13536 * No IRE for this destination, so it can't be for us. 13537 * Unless we are forwarding, drop the packet. 13538 * We have to let source routed packets through 13539 * since we don't yet know if they are 'ping -l' 13540 * packets i.e. if they will go out over the 13541 * same interface as they came in on. 13542 */ 13543 ip_rput_noire(q, NULL, mp, ll_multicast, dst); 13544 continue; 13545 } 13546 13547 /* 13548 * Broadcast IRE may indicate either broadcast or 13549 * multicast packet 13550 */ 13551 if (ire->ire_type == IRE_BROADCAST) { 13552 /* 13553 * Skip broadcast checks if packet is UDP multicast; 13554 * we'd rather not enter ip_rput_process_broadcast() 13555 * unless the packet is broadcast for real, since 13556 * that routine is a no-op for multicast. 13557 */ 13558 if ((ipha->ipha_protocol != IPPROTO_UDP || 13559 !CLASSD(ipha->ipha_dst)) && 13560 ip_rput_process_broadcast(&q, mp, &ire, ipha, ill, 13561 dst, cgtp_flt_pkt, ll_multicast)) { 13562 continue; 13563 } 13564 } else if (ire->ire_stq != NULL) { 13565 /* fowarding? */ 13566 ip_rput_process_forward(q, mp, ire, ipha, ill, 13567 ll_multicast); 13568 continue; 13569 } 13570 13571 /* packet not for us */ 13572 if (ire->ire_rfq != q) { 13573 if (ip_rput_notforus(&q, mp, ire, ill)) { 13574 continue; 13575 } 13576 } 13577 13578 switch (ipha->ipha_protocol) { 13579 case IPPROTO_TCP: 13580 ASSERT(first_mp == mp); 13581 if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, 13582 mp, 0, q, ip_ring)) != NULL) { 13583 if (curr_sqp == NULL) { 13584 curr_sqp = GET_SQUEUE(mp); 13585 ASSERT(cnt == 0); 13586 cnt++; 13587 head = tail = mp; 13588 } else if (curr_sqp == GET_SQUEUE(mp)) { 13589 ASSERT(tail != NULL); 13590 cnt++; 13591 tail->b_next = mp; 13592 tail = mp; 13593 } else { 13594 /* 13595 * A different squeue. Send the 13596 * chain for the previous squeue on 13597 * its way. This shouldn't happen 13598 * often unless interrupt binding 13599 * changes. 13600 */ 13601 IP_STAT(ip_input_multi_squeue); 13602 squeue_enter_chain(curr_sqp, head, 13603 tail, cnt, SQTAG_IP_INPUT); 13604 curr_sqp = GET_SQUEUE(mp); 13605 head = mp; 13606 tail = mp; 13607 cnt = 1; 13608 } 13609 } 13610 IRE_REFRELE(ire); 13611 continue; 13612 case IPPROTO_UDP: 13613 ASSERT(first_mp == mp); 13614 ip_udp_input(q, mp, ipha, ire, ill); 13615 IRE_REFRELE(ire); 13616 continue; 13617 case IPPROTO_SCTP: 13618 ASSERT(first_mp == mp); 13619 ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, 13620 q, dst); 13621 continue; 13622 default: 13623 ip_proto_input(q, first_mp, ipha, ire, ill); 13624 IRE_REFRELE(ire); 13625 continue; 13626 } 13627 } 13628 13629 if (head != NULL) 13630 squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); 13631 13632 /* 13633 * This code is there just to make netperf/ttcp look good. 13634 * 13635 * Its possible that after being in polling mode (and having cleared 13636 * the backlog), squeues have turned the interrupt frequency higher 13637 * to improve latency at the expense of more CPU utilization (less 13638 * packets per interrupts or more number of interrupts). Workloads 13639 * like ttcp/netperf do manage to tickle polling once in a while 13640 * but for the remaining time, stay in higher interrupt mode since 13641 * their packet arrival rate is pretty uniform and this shows up 13642 * as higher CPU utilization. Since people care about CPU utilization 13643 * while running netperf/ttcp, turn the interrupt frequency back to 13644 * normal/default if polling has not been used in ip_poll_normal_ticks. 13645 */ 13646 if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { 13647 if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { 13648 ip_ring->rr_poll_state &= ~ILL_POLLING; 13649 ip_ring->rr_blank(ip_ring->rr_handle, 13650 ip_ring->rr_normal_blank_time, 13651 ip_ring->rr_normal_pkt_cnt); 13652 } 13653 } 13654 13655 TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, 13656 "ip_input_end: q %p (%S)", q, "end"); 13657 #undef rptr 13658 } 13659 13660 static void 13661 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 13662 t_uscalar_t err) 13663 { 13664 if (dl_err == DL_SYSERR) { 13665 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 13666 "%s: %s failed: DL_SYSERR (errno %u)\n", 13667 ill->ill_name, dlpi_prim_str(prim), err); 13668 return; 13669 } 13670 13671 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 13672 "%s: %s failed: %s\n", ill->ill_name, dlpi_prim_str(prim), 13673 dlpi_err_str(dl_err)); 13674 } 13675 13676 /* 13677 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 13678 * than DL_UNITDATA_IND messages. If we need to process this message 13679 * exclusively, we call qwriter_ip, in which case we also need to call 13680 * ill_refhold before that, since qwriter_ip does an ill_refrele. 13681 */ 13682 void 13683 ip_rput_dlpi(queue_t *q, mblk_t *mp) 13684 { 13685 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 13686 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 13687 ill_t *ill; 13688 13689 ip1dbg(("ip_rput_dlpi")); 13690 ill = (ill_t *)q->q_ptr; 13691 switch (dloa->dl_primitive) { 13692 case DL_ERROR_ACK: 13693 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK %s (0x%x): " 13694 "%s (0x%x), unix %u\n", ill->ill_name, 13695 dlpi_prim_str(dlea->dl_error_primitive), 13696 dlea->dl_error_primitive, 13697 dlpi_err_str(dlea->dl_errno), 13698 dlea->dl_errno, 13699 dlea->dl_unix_errno)); 13700 switch (dlea->dl_error_primitive) { 13701 case DL_NOTIFY_REQ: 13702 case DL_UNBIND_REQ: 13703 case DL_ATTACH_REQ: 13704 case DL_DETACH_REQ: 13705 case DL_INFO_REQ: 13706 case DL_BIND_REQ: 13707 case DL_ENABMULTI_REQ: 13708 case DL_PHYS_ADDR_REQ: 13709 case DL_CAPABILITY_REQ: 13710 case DL_CONTROL_REQ: 13711 /* 13712 * Refhold the ill to match qwriter_ip which does a 13713 * refrele. Since this is on the ill stream we 13714 * unconditionally bump up the refcount without 13715 * checking for ILL_CAN_LOOKUP 13716 */ 13717 ill_refhold(ill); 13718 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 13719 CUR_OP, B_FALSE); 13720 return; 13721 case DL_DISABMULTI_REQ: 13722 freemsg(mp); /* Don't want to pass this up */ 13723 return; 13724 default: 13725 break; 13726 } 13727 ip_dlpi_error(ill, dlea->dl_error_primitive, 13728 dlea->dl_errno, dlea->dl_unix_errno); 13729 freemsg(mp); 13730 return; 13731 case DL_INFO_ACK: 13732 case DL_BIND_ACK: 13733 case DL_PHYS_ADDR_ACK: 13734 case DL_NOTIFY_ACK: 13735 case DL_CAPABILITY_ACK: 13736 case DL_CONTROL_ACK: 13737 /* 13738 * Refhold the ill to match qwriter_ip which does a refrele 13739 * Since this is on the ill stream we unconditionally 13740 * bump up the refcount without doing ILL_CAN_LOOKUP. 13741 */ 13742 ill_refhold(ill); 13743 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 13744 CUR_OP, B_FALSE); 13745 return; 13746 case DL_NOTIFY_IND: 13747 ill_refhold(ill); 13748 /* 13749 * The DL_NOTIFY_IND is an asynchronous message that has no 13750 * relation to the current ioctl in progress (if any). Hence we 13751 * pass in NEW_OP in this case. 13752 */ 13753 (void) qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 13754 NEW_OP, B_FALSE); 13755 return; 13756 case DL_OK_ACK: 13757 ip1dbg(("ip_rput: DL_OK_ACK for %s\n", 13758 dlpi_prim_str((int)dloa->dl_correct_primitive))); 13759 switch (dloa->dl_correct_primitive) { 13760 case DL_UNBIND_REQ: 13761 mutex_enter(&ill->ill_lock); 13762 ill->ill_state_flags |= ILL_DL_UNBIND_DONE; 13763 cv_signal(&ill->ill_cv); 13764 mutex_exit(&ill->ill_lock); 13765 /* FALLTHRU */ 13766 case DL_ATTACH_REQ: 13767 case DL_DETACH_REQ: 13768 /* 13769 * Refhold the ill to match qwriter_ip which does a 13770 * refrele. Since this is on the ill stream we 13771 * unconditionally bump up the refcount 13772 */ 13773 ill_refhold(ill); 13774 qwriter_ip(NULL, ill, q, mp, ip_rput_dlpi_writer, 13775 CUR_OP, B_FALSE); 13776 return; 13777 case DL_ENABMULTI_REQ: 13778 if (ill->ill_dlpi_multicast_state == IDMS_INPROGRESS) 13779 ill->ill_dlpi_multicast_state = IDMS_OK; 13780 break; 13781 13782 } 13783 break; 13784 default: 13785 break; 13786 } 13787 freemsg(mp); 13788 } 13789 13790 /* 13791 * Handling of DLPI messages that require exclusive access to the ipsq. 13792 * 13793 * Need to do ill_pending_mp_release on ioctl completion, which could 13794 * happen here. (along with mi_copy_done) 13795 */ 13796 /* ARGSUSED */ 13797 static void 13798 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 13799 { 13800 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 13801 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 13802 int err = 0; 13803 ill_t *ill; 13804 ipif_t *ipif = NULL; 13805 mblk_t *mp1 = NULL; 13806 conn_t *connp = NULL; 13807 t_uscalar_t physaddr_req; 13808 mblk_t *mp_hw; 13809 union DL_primitives *dlp; 13810 boolean_t success; 13811 boolean_t ioctl_aborted = B_FALSE; 13812 boolean_t log = B_TRUE; 13813 13814 ip1dbg(("ip_rput_dlpi_writer ..")); 13815 ill = (ill_t *)q->q_ptr; 13816 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 13817 13818 ASSERT(IAM_WRITER_ILL(ill)); 13819 13820 /* 13821 * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. 13822 * both are null or non-null. However we can assert that only 13823 * after grabbing the ipsq_lock. So we don't make any assertion 13824 * here and in other places in the code. 13825 */ 13826 ipif = ipsq->ipsq_pending_ipif; 13827 /* 13828 * The current ioctl could have been aborted by the user and a new 13829 * ioctl to bring up another ill could have started. We could still 13830 * get a response from the driver later. 13831 */ 13832 if (ipif != NULL && ipif->ipif_ill != ill) 13833 ioctl_aborted = B_TRUE; 13834 13835 switch (dloa->dl_primitive) { 13836 case DL_ERROR_ACK: 13837 switch (dlea->dl_error_primitive) { 13838 case DL_UNBIND_REQ: 13839 case DL_ATTACH_REQ: 13840 case DL_DETACH_REQ: 13841 case DL_INFO_REQ: 13842 ill_dlpi_done(ill, dlea->dl_error_primitive); 13843 break; 13844 case DL_NOTIFY_REQ: 13845 ill_dlpi_done(ill, DL_NOTIFY_REQ); 13846 log = B_FALSE; 13847 break; 13848 case DL_PHYS_ADDR_REQ: 13849 /* 13850 * For IPv6 only, there are two additional 13851 * phys_addr_req's sent to the driver to get the 13852 * IPv6 token and lla. This allows IP to acquire 13853 * the hardware address format for a given interface 13854 * without having built in knowledge of the hardware 13855 * address. ill_phys_addr_pend keeps track of the last 13856 * DL_PAR sent so we know which response we are 13857 * dealing with. ill_dlpi_done will update 13858 * ill_phys_addr_pend when it sends the next req. 13859 * We don't complete the IOCTL until all three DL_PARs 13860 * have been attempted, so set *_len to 0 and break. 13861 */ 13862 physaddr_req = ill->ill_phys_addr_pend; 13863 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 13864 if (physaddr_req == DL_IPV6_TOKEN) { 13865 ill->ill_token_length = 0; 13866 log = B_FALSE; 13867 break; 13868 } else if (physaddr_req == DL_IPV6_LINK_LAYER_ADDR) { 13869 ill->ill_nd_lla_len = 0; 13870 log = B_FALSE; 13871 break; 13872 } 13873 /* 13874 * Something went wrong with the DL_PHYS_ADDR_REQ. 13875 * We presumably have an IOCTL hanging out waiting 13876 * for completion. Find it and complete the IOCTL 13877 * with the error noted. 13878 * However, ill_dl_phys was called on an ill queue 13879 * (from SIOCSLIFNAME), thus conn_pending_ill is not 13880 * set. But the ioctl is known to be pending on ill_wq. 13881 */ 13882 if (!ill->ill_ifname_pending) 13883 break; 13884 ill->ill_ifname_pending = 0; 13885 if (!ioctl_aborted) 13886 mp1 = ipsq_pending_mp_get(ipsq, &connp); 13887 if (mp1 != NULL) { 13888 /* 13889 * This operation (SIOCSLIFNAME) must have 13890 * happened on the ill. Assert there is no conn 13891 */ 13892 ASSERT(connp == NULL); 13893 q = ill->ill_wq; 13894 } 13895 break; 13896 case DL_BIND_REQ: 13897 ill_dlpi_done(ill, DL_BIND_REQ); 13898 if (ill->ill_ifname_pending) 13899 break; 13900 /* 13901 * Something went wrong with the bind. We presumably 13902 * have an IOCTL hanging out waiting for completion. 13903 * Find it, take down the interface that was coming 13904 * up, and complete the IOCTL with the error noted. 13905 */ 13906 if (!ioctl_aborted) 13907 mp1 = ipsq_pending_mp_get(ipsq, &connp); 13908 if (mp1 != NULL) { 13909 /* 13910 * This operation (SIOCSLIFFLAGS) must have 13911 * happened from a conn. 13912 */ 13913 ASSERT(connp != NULL); 13914 q = CONNP_TO_WQ(connp); 13915 if (ill->ill_move_in_progress) { 13916 ILL_CLEAR_MOVE(ill); 13917 } 13918 (void) ipif_down(ipif, NULL, NULL); 13919 /* error is set below the switch */ 13920 } 13921 break; 13922 case DL_ENABMULTI_REQ: 13923 ip1dbg(("DL_ERROR_ACK to enabmulti\n")); 13924 13925 if (ill->ill_dlpi_multicast_state == IDMS_INPROGRESS) 13926 ill->ill_dlpi_multicast_state = IDMS_FAILED; 13927 if (ill->ill_dlpi_multicast_state == IDMS_FAILED) { 13928 ipif_t *ipif; 13929 13930 log = B_FALSE; 13931 printf("ip: joining multicasts failed (%d)" 13932 " on %s - will use link layer " 13933 "broadcasts for multicast\n", 13934 dlea->dl_errno, ill->ill_name); 13935 13936 /* 13937 * Set up the multicast mapping alone. 13938 * writer, so ok to access ill->ill_ipif 13939 * without any lock. 13940 */ 13941 ipif = ill->ill_ipif; 13942 mutex_enter(&ill->ill_phyint->phyint_lock); 13943 ill->ill_phyint->phyint_flags |= 13944 PHYI_MULTI_BCAST; 13945 mutex_exit(&ill->ill_phyint->phyint_lock); 13946 13947 if (!ill->ill_isv6) { 13948 (void) ipif_arp_setup_multicast(ipif, 13949 NULL); 13950 } else { 13951 (void) ipif_ndp_setup_multicast(ipif, 13952 NULL); 13953 } 13954 } 13955 freemsg(mp); /* Don't want to pass this up */ 13956 return; 13957 case DL_CAPABILITY_REQ: 13958 case DL_CONTROL_REQ: 13959 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 13960 "DL_CAPABILITY/CONTROL REQ\n")); 13961 ill_dlpi_done(ill, dlea->dl_error_primitive); 13962 ill->ill_capab_state = IDMS_FAILED; 13963 freemsg(mp); 13964 return; 13965 } 13966 /* 13967 * Note the error for IOCTL completion (mp1 is set when 13968 * ready to complete ioctl). If ill_ifname_pending_err is 13969 * set, an error occured during plumbing (ill_ifname_pending), 13970 * so we want to report that error. 13971 * 13972 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 13973 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 13974 * expected to get errack'd if the driver doesn't support 13975 * these flags (e.g. ethernet). log will be set to B_FALSE 13976 * if these error conditions are encountered. 13977 */ 13978 if (mp1 != NULL) { 13979 if (ill->ill_ifname_pending_err != 0) { 13980 err = ill->ill_ifname_pending_err; 13981 ill->ill_ifname_pending_err = 0; 13982 } else { 13983 err = dlea->dl_unix_errno ? 13984 dlea->dl_unix_errno : ENXIO; 13985 } 13986 /* 13987 * If we're plumbing an interface and an error hasn't already 13988 * been saved, set ill_ifname_pending_err to the error passed 13989 * up. Ignore the error if log is B_FALSE (see comment above). 13990 */ 13991 } else if (log && ill->ill_ifname_pending && 13992 ill->ill_ifname_pending_err == 0) { 13993 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 13994 dlea->dl_unix_errno : ENXIO; 13995 } 13996 13997 if (log) 13998 ip_dlpi_error(ill, dlea->dl_error_primitive, 13999 dlea->dl_errno, dlea->dl_unix_errno); 14000 break; 14001 case DL_CAPABILITY_ACK: { 14002 boolean_t reneg_flag = B_FALSE; 14003 /* Call a routine to handle this one. */ 14004 ill_dlpi_done(ill, DL_CAPABILITY_REQ); 14005 /* 14006 * Check if the ACK is due to renegotiation case since we 14007 * will need to send a new CAPABILITY_REQ later. 14008 */ 14009 if (ill->ill_capab_state == IDMS_RENEG) { 14010 /* This is the ack for a renogiation case */ 14011 reneg_flag = B_TRUE; 14012 ill->ill_capab_state = IDMS_UNKNOWN; 14013 } 14014 ill_capability_ack(ill, mp); 14015 if (reneg_flag) 14016 ill_capability_probe(ill); 14017 break; 14018 } 14019 case DL_CONTROL_ACK: 14020 /* We treat all of these as "fire and forget" */ 14021 ill_dlpi_done(ill, DL_CONTROL_REQ); 14022 break; 14023 case DL_INFO_ACK: 14024 /* Call a routine to handle this one. */ 14025 ill_dlpi_done(ill, DL_INFO_REQ); 14026 ip_ll_subnet_defaults(ill, mp); 14027 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 14028 return; 14029 case DL_BIND_ACK: 14030 /* 14031 * We should have an IOCTL waiting on this unless 14032 * sent by ill_dl_phys, in which case just return 14033 */ 14034 ill_dlpi_done(ill, DL_BIND_REQ); 14035 if (ill->ill_ifname_pending) 14036 break; 14037 14038 if (!ioctl_aborted) 14039 mp1 = ipsq_pending_mp_get(ipsq, &connp); 14040 if (mp1 == NULL) 14041 break; 14042 ASSERT(connp != NULL); 14043 q = CONNP_TO_WQ(connp); 14044 14045 /* 14046 * We are exclusive. So nothing can change even after 14047 * we get the pending mp. If need be we can put it back 14048 * and restart, as in calling ipif_arp_up() below. 14049 */ 14050 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 14051 14052 mutex_enter(&ill->ill_lock); 14053 ill->ill_dl_up = 1; 14054 mutex_exit(&ill->ill_lock); 14055 14056 /* 14057 * Now bring up the resolver, when that is 14058 * done we'll create IREs and we are done. 14059 */ 14060 if (ill->ill_isv6) { 14061 /* 14062 * v6 interfaces. 14063 * Unlike ARP which has to do another bind 14064 * and attach, once we get here we are 14065 * done withh NDP. Except in the case of 14066 * ILLF_XRESOLV, in which case we send an 14067 * AR_INTERFACE_UP to the external resolver. 14068 * If all goes well, the ioctl will complete 14069 * in ip_rput(). If there's an error, we 14070 * complete it here. 14071 */ 14072 err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr, 14073 B_FALSE); 14074 if (err == 0) { 14075 if (ill->ill_flags & ILLF_XRESOLV) { 14076 mutex_enter(&connp->conn_lock); 14077 mutex_enter(&ill->ill_lock); 14078 success = ipsq_pending_mp_add( 14079 connp, ipif, q, mp1, 0); 14080 mutex_exit(&ill->ill_lock); 14081 mutex_exit(&connp->conn_lock); 14082 if (success) { 14083 err = ipif_resolver_up(ipif, 14084 B_FALSE); 14085 if (err == EINPROGRESS) { 14086 freemsg(mp); 14087 return; 14088 } 14089 ASSERT(err != 0); 14090 mp1 = ipsq_pending_mp_get(ipsq, 14091 &connp); 14092 ASSERT(mp1 != NULL); 14093 } else { 14094 /* conn has started closing */ 14095 err = EINTR; 14096 } 14097 } else { /* Non XRESOLV interface */ 14098 err = ipif_up_done_v6(ipif); 14099 } 14100 } 14101 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 14102 /* 14103 * ARP and other v4 external resolvers. 14104 * Leave the pending mblk intact so that 14105 * the ioctl completes in ip_rput(). 14106 */ 14107 mutex_enter(&connp->conn_lock); 14108 mutex_enter(&ill->ill_lock); 14109 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 14110 mutex_exit(&ill->ill_lock); 14111 mutex_exit(&connp->conn_lock); 14112 if (success) { 14113 err = ipif_resolver_up(ipif, B_FALSE); 14114 if (err == EINPROGRESS) { 14115 freemsg(mp); 14116 return; 14117 } 14118 ASSERT(err != 0); 14119 mp1 = ipsq_pending_mp_get(ipsq, &connp); 14120 } else { 14121 /* The conn has started closing */ 14122 err = EINTR; 14123 } 14124 } else { 14125 /* 14126 * This one is complete. Reply to pending ioctl. 14127 */ 14128 err = ipif_up_done(ipif); 14129 } 14130 14131 if ((err == 0) && (ill->ill_up_ipifs)) { 14132 err = ill_up_ipifs(ill, q, mp1); 14133 if (err == EINPROGRESS) { 14134 freemsg(mp); 14135 return; 14136 } 14137 } 14138 14139 if (ill->ill_up_ipifs) { 14140 ill_group_cleanup(ill); 14141 } 14142 14143 break; 14144 case DL_NOTIFY_IND: { 14145 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 14146 ire_t *ire; 14147 boolean_t need_ire_walk_v4 = B_FALSE; 14148 boolean_t need_ire_walk_v6 = B_FALSE; 14149 14150 /* 14151 * Change the address everywhere we need to. 14152 * What we're getting here is a link-level addr or phys addr. 14153 * The new addr is at notify + notify->dl_addr_offset 14154 * The address length is notify->dl_addr_length; 14155 */ 14156 switch (notify->dl_notification) { 14157 case DL_NOTE_PHYS_ADDR: 14158 mp_hw = copyb(mp); 14159 if (mp_hw == NULL) { 14160 err = ENOMEM; 14161 break; 14162 } 14163 dlp = (union DL_primitives *)mp_hw->b_rptr; 14164 /* 14165 * We currently don't support changing 14166 * the token via DL_NOTIFY_IND. 14167 * When we do support it, we have to consider 14168 * what the implications are with respect to 14169 * the token and the link local address. 14170 */ 14171 mutex_enter(&ill->ill_lock); 14172 if (dlp->notify_ind.dl_data == 14173 DL_IPV6_LINK_LAYER_ADDR) { 14174 if (ill->ill_nd_lla_mp != NULL) 14175 freemsg(ill->ill_nd_lla_mp); 14176 ill->ill_nd_lla_mp = mp_hw; 14177 ill->ill_nd_lla = (uchar_t *)mp_hw->b_rptr + 14178 dlp->notify_ind.dl_addr_offset; 14179 ill->ill_nd_lla_len = 14180 dlp->notify_ind.dl_addr_length - 14181 ABS(ill->ill_sap_length); 14182 mutex_exit(&ill->ill_lock); 14183 break; 14184 } else if (dlp->notify_ind.dl_data == 14185 DL_CURR_PHYS_ADDR) { 14186 if (ill->ill_phys_addr_mp != NULL) 14187 freemsg(ill->ill_phys_addr_mp); 14188 ill->ill_phys_addr_mp = mp_hw; 14189 ill->ill_phys_addr = (uchar_t *)mp_hw->b_rptr + 14190 dlp->notify_ind.dl_addr_offset; 14191 ill->ill_phys_addr_length = 14192 dlp->notify_ind.dl_addr_length - 14193 ABS(ill->ill_sap_length); 14194 if (ill->ill_isv6 && 14195 !(ill->ill_flags & ILLF_XRESOLV)) { 14196 if (ill->ill_nd_lla_mp != NULL) 14197 freemsg(ill->ill_nd_lla_mp); 14198 ill->ill_nd_lla_mp = copyb(mp_hw); 14199 ill->ill_nd_lla = (uchar_t *) 14200 ill->ill_nd_lla_mp->b_rptr + 14201 dlp->notify_ind.dl_addr_offset; 14202 ill->ill_nd_lla_len = 14203 ill->ill_phys_addr_length; 14204 } 14205 } 14206 mutex_exit(&ill->ill_lock); 14207 /* 14208 * Send out gratuitous arp request for our new 14209 * hardware address. 14210 */ 14211 for (ipif = ill->ill_ipif; ipif != NULL; 14212 ipif = ipif->ipif_next) { 14213 if (!(ipif->ipif_flags & IPIF_UP)) 14214 continue; 14215 if (ill->ill_isv6) { 14216 ipif_ndp_down(ipif); 14217 /* 14218 * Set B_TRUE to enable 14219 * ipif_ndp_up() to send out 14220 * unsolicited advertisements. 14221 */ 14222 err = ipif_ndp_up(ipif, 14223 &ipif->ipif_v6lcl_addr, 14224 B_TRUE); 14225 if (err) { 14226 ip1dbg(( 14227 "ip_rput_dlpi_writer: " 14228 "Failed to update ndp " 14229 "err %d\n", err)); 14230 } 14231 } else { 14232 /* 14233 * IPv4 ARP case 14234 * 14235 * Set B_TRUE, as we only want 14236 * ipif_resolver_up to send an 14237 * AR_ENTRY_ADD request up to 14238 * ARP. 14239 */ 14240 err = ipif_resolver_up(ipif, 14241 B_TRUE); 14242 if (err) { 14243 ip1dbg(( 14244 "ip_rput_dlpi_writer: " 14245 "Failed to update arp " 14246 "err %d\n", err)); 14247 } 14248 } 14249 } 14250 /* 14251 * Allow "fall through" to the DL_NOTE_FASTPATH_FLUSH 14252 * case so that all old fastpath information can be 14253 * purged from IRE caches. 14254 */ 14255 /* FALLTHRU */ 14256 case DL_NOTE_FASTPATH_FLUSH: 14257 /* 14258 * Any fastpath probe sent henceforth will get the 14259 * new fp mp. So we first delete any ires that are 14260 * waiting for the fastpath. Then walk all ires and 14261 * delete the ire or delete the fp mp. In the case of 14262 * IRE_MIPRTUN and IRE_BROADCAST it is difficult to 14263 * recreate the ire's without going through a complex 14264 * ipif up/down dance. So we don't delete the ire 14265 * itself, but just the ire_fp_mp for these 2 ire's 14266 * In the case of the other ire's we delete the ire's 14267 * themselves. Access to ire_fp_mp is completely 14268 * protected by ire_lock for IRE_MIPRTUN and 14269 * IRE_BROADCAST. Deleting the ire is preferable in the 14270 * other cases for performance. 14271 */ 14272 if (ill->ill_isv6) { 14273 nce_fastpath_list_dispatch(ill, NULL, NULL); 14274 ndp_walk(ill, (pfi_t)ndp_fastpath_flush, 14275 NULL); 14276 } else { 14277 ire_fastpath_list_dispatch(ill, NULL, NULL); 14278 ire_walk_ill_v4(MATCH_IRE_WQ | MATCH_IRE_TYPE, 14279 IRE_CACHE | IRE_BROADCAST, 14280 ire_fastpath_flush, NULL, ill); 14281 mutex_enter(&ire_mrtun_lock); 14282 if (ire_mrtun_count != 0) { 14283 mutex_exit(&ire_mrtun_lock); 14284 ire_walk_ill_mrtun(MATCH_IRE_WQ, 14285 IRE_MIPRTUN, ire_fastpath_flush, 14286 NULL, ill); 14287 } else { 14288 mutex_exit(&ire_mrtun_lock); 14289 } 14290 } 14291 break; 14292 case DL_NOTE_SDU_SIZE: 14293 /* 14294 * Change the MTU size of the interface, of all 14295 * attached ipif's, and of all relevant ire's. The 14296 * new value's a uint32_t at notify->dl_data. 14297 * Mtu change Vs. new ire creation - protocol below. 14298 * 14299 * a Mark the ipif as IPIF_CHANGING. 14300 * b Set the new mtu in the ipif. 14301 * c Change the ire_max_frag on all affected ires 14302 * d Unmark the IPIF_CHANGING 14303 * 14304 * To see how the protocol works, assume an interface 14305 * route is also being added simultaneously by 14306 * ip_rt_add and let 'ipif' be the ipif referenced by 14307 * the ire. If the ire is created before step a, 14308 * it will be cleaned up by step c. If the ire is 14309 * created after step d, it will see the new value of 14310 * ipif_mtu. Any attempt to create the ire between 14311 * steps a to d will fail because of the IPIF_CHANGING 14312 * flag. Note that ire_create() is passed a pointer to 14313 * the ipif_mtu, and not the value. During ire_add 14314 * under the bucket lock, the ire_max_frag of the 14315 * new ire being created is set from the ipif/ire from 14316 * which it is being derived. 14317 */ 14318 mutex_enter(&ill->ill_lock); 14319 ill->ill_max_frag = (uint_t)notify->dl_data; 14320 14321 /* 14322 * If an SIOCSLIFLNKINFO has changed the ill_max_mtu 14323 * leave it alone 14324 */ 14325 if (ill->ill_mtu_userspecified) { 14326 mutex_exit(&ill->ill_lock); 14327 break; 14328 } 14329 ill->ill_max_mtu = ill->ill_max_frag; 14330 if (ill->ill_isv6) { 14331 if (ill->ill_max_mtu < IPV6_MIN_MTU) 14332 ill->ill_max_mtu = IPV6_MIN_MTU; 14333 } else { 14334 if (ill->ill_max_mtu < IP_MIN_MTU) 14335 ill->ill_max_mtu = IP_MIN_MTU; 14336 } 14337 for (ipif = ill->ill_ipif; ipif != NULL; 14338 ipif = ipif->ipif_next) { 14339 /* 14340 * Don't override the mtu if the user 14341 * has explicitly set it. 14342 */ 14343 if (ipif->ipif_flags & IPIF_FIXEDMTU) 14344 continue; 14345 ipif->ipif_mtu = (uint_t)notify->dl_data; 14346 if (ipif->ipif_isv6) 14347 ire = ipif_to_ire_v6(ipif); 14348 else 14349 ire = ipif_to_ire(ipif); 14350 if (ire != NULL) { 14351 ire->ire_max_frag = ipif->ipif_mtu; 14352 ire_refrele(ire); 14353 } 14354 if (ipif->ipif_flags & IPIF_UP) { 14355 if (ill->ill_isv6) 14356 need_ire_walk_v6 = B_TRUE; 14357 else 14358 need_ire_walk_v4 = B_TRUE; 14359 } 14360 } 14361 mutex_exit(&ill->ill_lock); 14362 if (need_ire_walk_v4) 14363 ire_walk_v4(ill_mtu_change, (char *)ill, 14364 ALL_ZONES); 14365 if (need_ire_walk_v6) 14366 ire_walk_v6(ill_mtu_change, (char *)ill, 14367 ALL_ZONES); 14368 break; 14369 case DL_NOTE_LINK_UP: 14370 case DL_NOTE_LINK_DOWN: { 14371 /* 14372 * We are writer. ill / phyint / ipsq assocs stable. 14373 * The RUNNING flag reflects the state of the link. 14374 */ 14375 phyint_t *phyint = ill->ill_phyint; 14376 uint64_t new_phyint_flags; 14377 boolean_t changed = B_FALSE; 14378 14379 mutex_enter(&phyint->phyint_lock); 14380 new_phyint_flags = 14381 (notify->dl_notification == DL_NOTE_LINK_UP) ? 14382 phyint->phyint_flags | PHYI_RUNNING : 14383 phyint->phyint_flags & ~PHYI_RUNNING; 14384 if (new_phyint_flags != phyint->phyint_flags) { 14385 phyint->phyint_flags = new_phyint_flags; 14386 changed = B_TRUE; 14387 } 14388 mutex_exit(&phyint->phyint_lock); 14389 /* 14390 * If the flags have changed, send a message to 14391 * the routing socket. 14392 */ 14393 if (changed) { 14394 if (phyint->phyint_illv4 != NULL) { 14395 ip_rts_ifmsg( 14396 phyint->phyint_illv4->ill_ipif); 14397 } 14398 if (phyint->phyint_illv6 != NULL) { 14399 ip_rts_ifmsg( 14400 phyint->phyint_illv6->ill_ipif); 14401 } 14402 } 14403 break; 14404 } 14405 case DL_NOTE_PROMISC_ON_PHYS: 14406 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 14407 "got a DL_NOTE_PROMISC_ON_PHYS\n")); 14408 mutex_enter(&ill->ill_lock); 14409 ill->ill_promisc_on_phys = B_TRUE; 14410 mutex_exit(&ill->ill_lock); 14411 break; 14412 case DL_NOTE_PROMISC_OFF_PHYS: 14413 IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " 14414 "got a DL_NOTE_PROMISC_OFF_PHYS\n")); 14415 mutex_enter(&ill->ill_lock); 14416 ill->ill_promisc_on_phys = B_FALSE; 14417 mutex_exit(&ill->ill_lock); 14418 break; 14419 case DL_NOTE_CAPAB_RENEG: 14420 /* 14421 * Something changed on the driver side. 14422 * It wants us to renegotiate the capabilities 14423 * on this ill. The most likely cause is the 14424 * aggregation interface under us where a 14425 * port got added or went away. 14426 * 14427 * We reset the capabilities and set the 14428 * state to IDMS_RENG so that when the ack 14429 * comes back, we can start the 14430 * renegotiation process. 14431 */ 14432 ill_capability_reset(ill); 14433 ill->ill_capab_state = IDMS_RENEG; 14434 break; 14435 default: 14436 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 14437 "type 0x%x for DL_NOTIFY_IND\n", 14438 notify->dl_notification)); 14439 break; 14440 } 14441 14442 /* 14443 * As this is an asynchronous operation, we 14444 * should not call ill_dlpi_done 14445 */ 14446 break; 14447 } 14448 case DL_NOTIFY_ACK: 14449 /* 14450 * Don't really need to check for what notifications 14451 * are supported; we'll process what gets sent upstream, 14452 * and we know it'll be something we support changing 14453 * based on our DL_NOTIFY_REQ. 14454 */ 14455 ill_dlpi_done(ill, DL_NOTIFY_REQ); 14456 break; 14457 case DL_PHYS_ADDR_ACK: { 14458 /* 14459 * We should have an IOCTL waiting on this when request 14460 * sent by ill_dl_phys. 14461 * However, ill_dl_phys was called on an ill queue (from 14462 * SIOCSLIFNAME), thus conn_pending_ill is not set. But the 14463 * ioctl is known to be pending on ill_wq. 14464 * There are two additional phys_addr_req's sent to the 14465 * driver to get the token and lla. ill_phys_addr_pend 14466 * keeps track of the last one sent so we know which 14467 * response we are dealing with. ill_dlpi_done will 14468 * update ill_phys_addr_pend when it sends the next req. 14469 * We don't complete the IOCTL until all three DL_PARs 14470 * have been attempted. 14471 * 14472 * We don't need any lock to update ill_nd_lla* fields, 14473 * since the ill is not yet up, We grab the lock just 14474 * for uniformity with other code that accesses ill_nd_lla. 14475 */ 14476 physaddr_req = ill->ill_phys_addr_pend; 14477 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 14478 if (physaddr_req == DL_IPV6_TOKEN || 14479 physaddr_req == DL_IPV6_LINK_LAYER_ADDR) { 14480 if (physaddr_req == DL_IPV6_TOKEN) { 14481 /* 14482 * bcopy to low-order bits of ill_token 14483 * 14484 * XXX Temporary hack - currently, 14485 * all known tokens are 64 bits, 14486 * so I'll cheat for the moment. 14487 */ 14488 dlp = (union DL_primitives *)mp->b_rptr; 14489 14490 mutex_enter(&ill->ill_lock); 14491 bcopy((uchar_t *)(mp->b_rptr + 14492 dlp->physaddr_ack.dl_addr_offset), 14493 (void *)&ill->ill_token.s6_addr32[2], 14494 dlp->physaddr_ack.dl_addr_length); 14495 ill->ill_token_length = 14496 dlp->physaddr_ack.dl_addr_length; 14497 mutex_exit(&ill->ill_lock); 14498 } else { 14499 ASSERT(ill->ill_nd_lla_mp == NULL); 14500 mp_hw = copyb(mp); 14501 if (mp_hw == NULL) { 14502 err = ENOMEM; 14503 break; 14504 } 14505 dlp = (union DL_primitives *)mp_hw->b_rptr; 14506 mutex_enter(&ill->ill_lock); 14507 ill->ill_nd_lla_mp = mp_hw; 14508 ill->ill_nd_lla = (uchar_t *)mp_hw->b_rptr + 14509 dlp->physaddr_ack.dl_addr_offset; 14510 ill->ill_nd_lla_len = 14511 dlp->physaddr_ack.dl_addr_length; 14512 mutex_exit(&ill->ill_lock); 14513 } 14514 break; 14515 } 14516 ASSERT(physaddr_req == DL_CURR_PHYS_ADDR); 14517 ASSERT(ill->ill_phys_addr_mp == NULL); 14518 if (!ill->ill_ifname_pending) 14519 break; 14520 ill->ill_ifname_pending = 0; 14521 if (!ioctl_aborted) 14522 mp1 = ipsq_pending_mp_get(ipsq, &connp); 14523 if (mp1 != NULL) { 14524 ASSERT(connp == NULL); 14525 q = ill->ill_wq; 14526 } 14527 /* 14528 * If any error acks received during the plumbing sequence, 14529 * ill_ifname_pending_err will be set. Break out and send up 14530 * the error to the pending ioctl. 14531 */ 14532 if (ill->ill_ifname_pending_err != 0) { 14533 err = ill->ill_ifname_pending_err; 14534 ill->ill_ifname_pending_err = 0; 14535 break; 14536 } 14537 /* 14538 * Get the interface token. If the zeroth interface 14539 * address is zero then set the address to the link local 14540 * address 14541 */ 14542 mp_hw = copyb(mp); 14543 if (mp_hw == NULL) { 14544 err = ENOMEM; 14545 break; 14546 } 14547 dlp = (union DL_primitives *)mp_hw->b_rptr; 14548 ill->ill_phys_addr_mp = mp_hw; 14549 ill->ill_phys_addr = (uchar_t *)mp_hw->b_rptr + 14550 dlp->physaddr_ack.dl_addr_offset; 14551 if (dlp->physaddr_ack.dl_addr_length == 0 || 14552 ill->ill_phys_addr_length == 0 || 14553 ill->ill_phys_addr_length == IP_ADDR_LEN) { 14554 /* 14555 * Compatibility: atun driver returns a length of 0. 14556 * ipdptp has an ill_phys_addr_length of zero(from 14557 * DL_BIND_ACK) but a non-zero length here. 14558 * ipd has an ill_phys_addr_length of 4(from 14559 * DL_BIND_ACK) but a non-zero length here. 14560 */ 14561 ill->ill_phys_addr = NULL; 14562 } else if (dlp->physaddr_ack.dl_addr_length != 14563 ill->ill_phys_addr_length) { 14564 ip0dbg(("DL_PHYS_ADDR_ACK: " 14565 "Address length mismatch %d %d\n", 14566 dlp->physaddr_ack.dl_addr_length, 14567 ill->ill_phys_addr_length)); 14568 err = EINVAL; 14569 break; 14570 } 14571 mutex_enter(&ill->ill_lock); 14572 if (ill->ill_nd_lla_mp == NULL) { 14573 ill->ill_nd_lla_mp = copyb(mp_hw); 14574 if (ill->ill_nd_lla_mp == NULL) { 14575 err = ENOMEM; 14576 mutex_exit(&ill->ill_lock); 14577 break; 14578 } 14579 ill->ill_nd_lla = 14580 (uchar_t *)ill->ill_nd_lla_mp->b_rptr + 14581 dlp->physaddr_ack.dl_addr_offset; 14582 ill->ill_nd_lla_len = ill->ill_phys_addr_length; 14583 } 14584 mutex_exit(&ill->ill_lock); 14585 if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) 14586 (void) ill_setdefaulttoken(ill); 14587 14588 /* 14589 * If the ill zero interface has a zero address assign 14590 * it the proper link local address. 14591 */ 14592 ASSERT(ill->ill_ipif->ipif_id == 0); 14593 if (ipif != NULL && 14594 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) 14595 (void) ipif_setlinklocal(ipif); 14596 break; 14597 } 14598 case DL_OK_ACK: 14599 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 14600 dlpi_prim_str((int)dloa->dl_correct_primitive), 14601 dloa->dl_correct_primitive)); 14602 switch (dloa->dl_correct_primitive) { 14603 case DL_UNBIND_REQ: 14604 case DL_ATTACH_REQ: 14605 case DL_DETACH_REQ: 14606 ill_dlpi_done(ill, dloa->dl_correct_primitive); 14607 break; 14608 } 14609 break; 14610 default: 14611 break; 14612 } 14613 14614 freemsg(mp); 14615 if (mp1) { 14616 struct iocblk *iocp; 14617 int mode; 14618 14619 /* 14620 * Complete the waiting IOCTL. For SIOCLIFADDIF or 14621 * SIOCSLIFNAME do a copyout. 14622 */ 14623 iocp = (struct iocblk *)mp1->b_rptr; 14624 14625 if (iocp->ioc_cmd == SIOCLIFADDIF || 14626 iocp->ioc_cmd == SIOCSLIFNAME) 14627 mode = COPYOUT; 14628 else 14629 mode = NO_COPYOUT; 14630 /* 14631 * The ioctl must complete now without EINPROGRESS 14632 * since ipsq_pending_mp_get has removed the ioctl mblk 14633 * from ipsq_pending_mp. Otherwise the ioctl will be 14634 * stuck for ever in the ipsq. 14635 */ 14636 ASSERT(err != EINPROGRESS); 14637 ip_ioctl_finish(q, mp1, err, mode, ipif, ipsq); 14638 14639 } 14640 } 14641 14642 /* 14643 * ip_rput_other is called by ip_rput to handle messages modifying the global 14644 * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) 14645 */ 14646 /* ARGSUSED */ 14647 void 14648 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 14649 { 14650 ill_t *ill; 14651 struct iocblk *iocp; 14652 mblk_t *mp1; 14653 conn_t *connp = NULL; 14654 14655 ip1dbg(("ip_rput_other ")); 14656 ill = (ill_t *)q->q_ptr; 14657 /* 14658 * This routine is not a writer in the case of SIOCGTUNPARAM 14659 * in which case ipsq is NULL. 14660 */ 14661 if (ipsq != NULL) { 14662 ASSERT(IAM_WRITER_IPSQ(ipsq)); 14663 ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); 14664 } 14665 14666 switch (mp->b_datap->db_type) { 14667 case M_ERROR: 14668 case M_HANGUP: 14669 /* 14670 * The device has a problem. We force the ILL down. It can 14671 * be brought up again manually using SIOCSIFFLAGS (via 14672 * ifconfig or equivalent). 14673 */ 14674 ASSERT(ipsq != NULL); 14675 if (mp->b_rptr < mp->b_wptr) 14676 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 14677 if (ill->ill_error == 0) 14678 ill->ill_error = ENXIO; 14679 if (!ill_down_start(q, mp)) 14680 return; 14681 ipif_all_down_tail(ipsq, q, mp, NULL); 14682 break; 14683 case M_IOCACK: 14684 iocp = (struct iocblk *)mp->b_rptr; 14685 ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); 14686 switch (iocp->ioc_cmd) { 14687 case SIOCSTUNPARAM: 14688 case OSIOCSTUNPARAM: 14689 ASSERT(ipsq != NULL); 14690 /* 14691 * Finish socket ioctl passed through to tun. 14692 * We should have an IOCTL waiting on this. 14693 */ 14694 mp1 = ipsq_pending_mp_get(ipsq, &connp); 14695 if (ill->ill_isv6) { 14696 struct iftun_req *ta; 14697 14698 /* 14699 * if a source or destination is 14700 * being set, try and set the link 14701 * local address for the tunnel 14702 */ 14703 ta = (struct iftun_req *)mp->b_cont-> 14704 b_cont->b_rptr; 14705 if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { 14706 ipif_set_tun_llink(ill, ta); 14707 } 14708 14709 } 14710 if (mp1 != NULL) { 14711 /* 14712 * Now copy back the b_next/b_prev used by 14713 * mi code for the mi_copy* functions. 14714 * See ip_sioctl_tunparam() for the reason. 14715 * Also protect against missing b_cont. 14716 */ 14717 if (mp->b_cont != NULL) { 14718 mp->b_cont->b_next = 14719 mp1->b_cont->b_next; 14720 mp->b_cont->b_prev = 14721 mp1->b_cont->b_prev; 14722 } 14723 inet_freemsg(mp1); 14724 ASSERT(ipsq->ipsq_current_ipif != NULL); 14725 ASSERT(connp != NULL); 14726 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 14727 iocp->ioc_error, NO_COPYOUT, 14728 ipsq->ipsq_current_ipif, ipsq); 14729 } else { 14730 ASSERT(connp == NULL); 14731 putnext(q, mp); 14732 } 14733 break; 14734 case SIOCGTUNPARAM: 14735 case OSIOCGTUNPARAM: 14736 /* 14737 * This is really M_IOCDATA from the tunnel driver. 14738 * convert back and complete the ioctl. 14739 * We should have an IOCTL waiting on this. 14740 */ 14741 mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); 14742 if (mp1) { 14743 /* 14744 * Now copy back the b_next/b_prev used by 14745 * mi code for the mi_copy* functions. 14746 * See ip_sioctl_tunparam() for the reason. 14747 * Also protect against missing b_cont. 14748 */ 14749 if (mp->b_cont != NULL) { 14750 mp->b_cont->b_next = 14751 mp1->b_cont->b_next; 14752 mp->b_cont->b_prev = 14753 mp1->b_cont->b_prev; 14754 } 14755 inet_freemsg(mp1); 14756 if (iocp->ioc_error == 0) 14757 mp->b_datap->db_type = M_IOCDATA; 14758 ASSERT(connp != NULL); 14759 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 14760 iocp->ioc_error, COPYOUT, NULL, NULL); 14761 } else { 14762 ASSERT(connp == NULL); 14763 putnext(q, mp); 14764 } 14765 break; 14766 default: 14767 break; 14768 } 14769 break; 14770 case M_IOCNAK: 14771 iocp = (struct iocblk *)mp->b_rptr; 14772 14773 switch (iocp->ioc_cmd) { 14774 int mode; 14775 ipif_t *ipif; 14776 14777 case DL_IOC_HDR_INFO: 14778 /* 14779 * If this was the first attempt turn of the 14780 * fastpath probing. 14781 */ 14782 mutex_enter(&ill->ill_lock); 14783 if (ill->ill_dlpi_fastpath_state == IDMS_INPROGRESS) { 14784 ill->ill_dlpi_fastpath_state = IDMS_FAILED; 14785 mutex_exit(&ill->ill_lock); 14786 ill_fastpath_nack(ill); 14787 ip1dbg(("ip_rput: DLPI fastpath off on " 14788 "interface %s\n", 14789 ill->ill_name)); 14790 } else { 14791 mutex_exit(&ill->ill_lock); 14792 } 14793 freemsg(mp); 14794 break; 14795 case SIOCSTUNPARAM: 14796 case OSIOCSTUNPARAM: 14797 ASSERT(ipsq != NULL); 14798 /* 14799 * Finish socket ioctl passed through to tun 14800 * We should have an IOCTL waiting on this. 14801 */ 14802 /* FALLTHRU */ 14803 case SIOCGTUNPARAM: 14804 case OSIOCGTUNPARAM: 14805 /* 14806 * This is really M_IOCDATA from the tunnel driver. 14807 * convert back and complete the ioctl. 14808 * We should have an IOCTL waiting on this. 14809 */ 14810 if (iocp->ioc_cmd == SIOCGTUNPARAM || 14811 iocp->ioc_cmd == OSIOCGTUNPARAM) { 14812 mp1 = ill_pending_mp_get(ill, &connp, 14813 iocp->ioc_id); 14814 mode = COPYOUT; 14815 ipsq = NULL; 14816 ipif = NULL; 14817 } else { 14818 mp1 = ipsq_pending_mp_get(ipsq, &connp); 14819 mode = NO_COPYOUT; 14820 ASSERT(ipsq->ipsq_current_ipif != NULL); 14821 ipif = ipsq->ipsq_current_ipif; 14822 } 14823 if (mp1 != NULL) { 14824 /* 14825 * Now copy back the b_next/b_prev used by 14826 * mi code for the mi_copy* functions. 14827 * See ip_sioctl_tunparam() for the reason. 14828 * Also protect against missing b_cont. 14829 */ 14830 if (mp->b_cont != NULL) { 14831 mp->b_cont->b_next = 14832 mp1->b_cont->b_next; 14833 mp->b_cont->b_prev = 14834 mp1->b_cont->b_prev; 14835 } 14836 inet_freemsg(mp1); 14837 if (iocp->ioc_error == 0) 14838 iocp->ioc_error = EINVAL; 14839 ASSERT(connp != NULL); 14840 ip_ioctl_finish(CONNP_TO_WQ(connp), mp, 14841 iocp->ioc_error, mode, ipif, ipsq); 14842 } else { 14843 ASSERT(connp == NULL); 14844 putnext(q, mp); 14845 } 14846 break; 14847 default: 14848 break; 14849 } 14850 default: 14851 break; 14852 } 14853 } 14854 14855 /* 14856 * NOTE : This function does not ire_refrele the ire argument passed in. 14857 * 14858 * IPQoS notes 14859 * IP policy is invoked twice for a forwarded packet, once on the read side 14860 * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are 14861 * enabled. An additional parameter, in_ill, has been added for this purpose. 14862 * Note that in_ill could be NULL when called from ip_rput_forward_multicast 14863 * because ip_mroute drops this information. 14864 * 14865 */ 14866 void 14867 ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) 14868 { 14869 uint32_t pkt_len; 14870 queue_t *q; 14871 uint32_t sum; 14872 #define rptr ((uchar_t *)ipha) 14873 uint32_t max_frag; 14874 uint32_t ill_index; 14875 14876 /* Get the ill_index of the incoming ILL */ 14877 ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; 14878 14879 /* Initiate Read side IPPF processing */ 14880 if (IPP_ENABLED(IPP_FWD_IN)) { 14881 ip_process(IPP_FWD_IN, &mp, ill_index); 14882 if (mp == NULL) { 14883 ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ 14884 "during IPPF processing\n")); 14885 return; 14886 } 14887 } 14888 pkt_len = ntohs(ipha->ipha_length); 14889 14890 /* Adjust the checksum to reflect the ttl decrement. */ 14891 sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; 14892 ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); 14893 14894 if (ipha->ipha_ttl-- <= 1) { 14895 if (ip_csum_hdr(ipha)) { 14896 BUMP_MIB(&ip_mib, ipInCksumErrs); 14897 goto drop_pkt; 14898 } 14899 /* 14900 * Note: ire_stq this will be NULL for multicast 14901 * datagrams using the long path through arp (the IRE 14902 * is not an IRE_CACHE). This should not cause 14903 * problems since we don't generate ICMP errors for 14904 * multicast packets. 14905 */ 14906 q = ire->ire_stq; 14907 if (q) 14908 icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED); 14909 else 14910 freemsg(mp); 14911 return; 14912 } 14913 14914 /* 14915 * Don't forward if the interface is down 14916 */ 14917 if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { 14918 BUMP_MIB(&ip_mib, ipInDiscards); 14919 goto drop_pkt; 14920 } 14921 14922 /* Get the ill_index of the outgoing ILL */ 14923 ill_index = ire->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 14924 14925 /* Check if there are options to update */ 14926 if (!IS_SIMPLE_IPH(ipha)) { 14927 if (ip_csum_hdr(ipha)) { 14928 BUMP_MIB(&ip_mib, ipInCksumErrs); 14929 goto drop_pkt; 14930 } 14931 if (ip_rput_forward_options(mp, ipha, ire)) { 14932 return; 14933 } 14934 14935 ipha->ipha_hdr_checksum = 0; 14936 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 14937 } 14938 max_frag = ire->ire_max_frag; 14939 if (pkt_len > max_frag) { 14940 /* 14941 * It needs fragging on its way out. We haven't 14942 * verified the header checksum yet. Since we 14943 * are going to put a surely good checksum in the 14944 * outgoing header, we have to make sure that it 14945 * was good coming in. 14946 */ 14947 if (ip_csum_hdr(ipha)) { 14948 BUMP_MIB(&ip_mib, ipInCksumErrs); 14949 goto drop_pkt; 14950 } 14951 /* Initiate Write side IPPF processing */ 14952 if (IPP_ENABLED(IPP_FWD_OUT)) { 14953 ip_process(IPP_FWD_OUT, &mp, ill_index); 14954 if (mp == NULL) { 14955 ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ 14956 " during IPPF processing\n")); 14957 return; 14958 } 14959 } 14960 ip_wput_frag(ire, mp, IB_PKT, max_frag, 0); 14961 return; 14962 } 14963 14964 mp = ip_wput_attach_llhdr(mp, ire, IPP_FWD_OUT, ill_index); 14965 if (mp == NULL) { 14966 BUMP_MIB(&ip_mib, ipInDiscards); 14967 return; 14968 } 14969 14970 q = ire->ire_stq; 14971 UPDATE_IB_PKT_COUNT(ire); 14972 ire->ire_last_used_time = lbolt; 14973 BUMP_MIB(&ip_mib, ipForwDatagrams); 14974 putnext(q, mp); 14975 return; 14976 14977 drop_pkt:; 14978 ip1dbg(("ip_rput_forward: drop pkt\n")); 14979 freemsg(mp); 14980 #undef rptr 14981 } 14982 14983 void 14984 ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) 14985 { 14986 ire_t *ire; 14987 14988 ASSERT(!ipif->ipif_isv6); 14989 /* 14990 * Find an IRE which matches the destination and the outgoing 14991 * queue in the cache table. All we need is an IRE_CACHE which 14992 * is pointing at ipif->ipif_ill. If it is part of some ill group, 14993 * then it is enough to have some IRE_CACHE in the group. 14994 */ 14995 if (ipif->ipif_flags & IPIF_POINTOPOINT) 14996 dst = ipif->ipif_pp_dst_addr; 14997 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, 14998 MATCH_IRE_ILL_GROUP); 14999 if (!ire) { 15000 /* 15001 * Mark this packet to make it be delivered to 15002 * ip_rput_forward after the new ire has been 15003 * created. 15004 */ 15005 mp->b_prev = NULL; 15006 mp->b_next = mp; 15007 ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, 15008 NULL, 0); 15009 } else { 15010 ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); 15011 IRE_REFRELE(ire); 15012 } 15013 } 15014 15015 /* Update any source route, record route or timestamp options */ 15016 static int 15017 ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire) 15018 { 15019 ipoptp_t opts; 15020 uchar_t *opt; 15021 uint8_t optval; 15022 uint8_t optlen; 15023 ipaddr_t dst; 15024 uint32_t ts; 15025 ire_t *dst_ire = NULL; 15026 ire_t *tmp_ire = NULL; 15027 timestruc_t now; 15028 15029 ip2dbg(("ip_rput_forward_options\n")); 15030 dst = ipha->ipha_dst; 15031 for (optval = ipoptp_first(&opts, ipha); 15032 optval != IPOPT_EOL; 15033 optval = ipoptp_next(&opts)) { 15034 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 15035 opt = opts.ipoptp_cur; 15036 optlen = opts.ipoptp_len; 15037 ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", 15038 optval, opts.ipoptp_len)); 15039 switch (optval) { 15040 uint32_t off; 15041 case IPOPT_SSRR: 15042 case IPOPT_LSRR: 15043 /* Check if adminstratively disabled */ 15044 if (!ip_forward_src_routed) { 15045 BUMP_MIB(&ip_mib, ipForwProhibits); 15046 if (ire->ire_stq) 15047 icmp_unreachable(ire->ire_stq, mp, 15048 ICMP_SOURCE_ROUTE_FAILED); 15049 else { 15050 ip0dbg(("ip_rput_forward_options: " 15051 "unable to send unreach\n")); 15052 freemsg(mp); 15053 } 15054 return (-1); 15055 } 15056 15057 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 15058 NULL, ALL_ZONES, MATCH_IRE_TYPE); 15059 if (dst_ire == NULL) { 15060 /* 15061 * Must be partial since ip_rput_options 15062 * checked for strict. 15063 */ 15064 break; 15065 } 15066 off = opt[IPOPT_OFFSET]; 15067 off--; 15068 redo_srr: 15069 if (optlen < IP_ADDR_LEN || 15070 off > optlen - IP_ADDR_LEN) { 15071 /* End of source route */ 15072 ip1dbg(( 15073 "ip_rput_forward_options: end of SR\n")); 15074 ire_refrele(dst_ire); 15075 break; 15076 } 15077 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 15078 bcopy(&ire->ire_src_addr, (char *)opt + off, 15079 IP_ADDR_LEN); 15080 ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", 15081 ntohl(dst))); 15082 15083 /* 15084 * Check if our address is present more than 15085 * once as consecutive hops in source route. 15086 */ 15087 tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 15088 NULL, ALL_ZONES, MATCH_IRE_TYPE); 15089 if (tmp_ire != NULL) { 15090 ire_refrele(tmp_ire); 15091 off += IP_ADDR_LEN; 15092 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 15093 goto redo_srr; 15094 } 15095 ipha->ipha_dst = dst; 15096 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 15097 ire_refrele(dst_ire); 15098 break; 15099 case IPOPT_RR: 15100 off = opt[IPOPT_OFFSET]; 15101 off--; 15102 if (optlen < IP_ADDR_LEN || 15103 off > optlen - IP_ADDR_LEN) { 15104 /* No more room - ignore */ 15105 ip1dbg(( 15106 "ip_rput_forward_options: end of RR\n")); 15107 break; 15108 } 15109 bcopy(&ire->ire_src_addr, (char *)opt + off, 15110 IP_ADDR_LEN); 15111 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 15112 break; 15113 case IPOPT_TS: 15114 /* Insert timestamp if there is room */ 15115 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 15116 case IPOPT_TS_TSONLY: 15117 off = IPOPT_TS_TIMELEN; 15118 break; 15119 case IPOPT_TS_PRESPEC: 15120 case IPOPT_TS_PRESPEC_RFC791: 15121 /* Verify that the address matched */ 15122 off = opt[IPOPT_OFFSET] - 1; 15123 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 15124 dst_ire = ire_ctable_lookup(dst, 0, 15125 IRE_LOCAL, NULL, ALL_ZONES, MATCH_IRE_TYPE); 15126 if (dst_ire == NULL) { 15127 /* Not for us */ 15128 break; 15129 } 15130 ire_refrele(dst_ire); 15131 /* FALLTHRU */ 15132 case IPOPT_TS_TSANDADDR: 15133 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 15134 break; 15135 default: 15136 /* 15137 * ip_*put_options should have already 15138 * dropped this packet. 15139 */ 15140 cmn_err(CE_PANIC, "ip_rput_forward_options: " 15141 "unknown IT - bug in ip_rput_options?\n"); 15142 return (0); /* Keep "lint" happy */ 15143 } 15144 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 15145 /* Increase overflow counter */ 15146 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 15147 opt[IPOPT_POS_OV_FLG] = 15148 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 15149 (off << 4)); 15150 break; 15151 } 15152 off = opt[IPOPT_OFFSET] - 1; 15153 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 15154 case IPOPT_TS_PRESPEC: 15155 case IPOPT_TS_PRESPEC_RFC791: 15156 case IPOPT_TS_TSANDADDR: 15157 bcopy(&ire->ire_src_addr, 15158 (char *)opt + off, IP_ADDR_LEN); 15159 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 15160 /* FALLTHRU */ 15161 case IPOPT_TS_TSONLY: 15162 off = opt[IPOPT_OFFSET] - 1; 15163 /* Compute # of milliseconds since midnight */ 15164 gethrestime(&now); 15165 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 15166 now.tv_nsec / (NANOSEC / MILLISEC); 15167 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 15168 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 15169 break; 15170 } 15171 break; 15172 } 15173 } 15174 return (0); 15175 } 15176 15177 /* 15178 * This is called after processing at least one of AH/ESP headers. 15179 * 15180 * NOTE: the ill corresponding to ipsec_in_ill_index may not be 15181 * the actual, physical interface on which the packet was received, 15182 * but, when ip_strict_dst_multihoming is set to 1, could be the 15183 * interface which had the ipha_dst configured when the packet went 15184 * through ip_rput. The ill_index corresponding to the recv_ill 15185 * is saved in ipsec_in_rill_index 15186 */ 15187 void 15188 ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) 15189 { 15190 mblk_t *mp; 15191 ipaddr_t dst; 15192 in6_addr_t *v6dstp; 15193 ipha_t *ipha; 15194 ip6_t *ip6h; 15195 ipsec_in_t *ii; 15196 boolean_t ill_need_rele = B_FALSE; 15197 boolean_t rill_need_rele = B_FALSE; 15198 boolean_t ire_need_rele = B_FALSE; 15199 15200 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 15201 ASSERT(ii->ipsec_in_ill_index != 0); 15202 15203 mp = ipsec_mp->b_cont; 15204 ASSERT(mp != NULL); 15205 15206 15207 if (ill == NULL) { 15208 ASSERT(recv_ill == NULL); 15209 /* 15210 * We need to get the original queue on which ip_rput_local 15211 * or ip_rput_data_v6 was called. 15212 */ 15213 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, 15214 !ii->ipsec_in_v4, NULL, NULL, NULL, NULL); 15215 ill_need_rele = B_TRUE; 15216 15217 if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { 15218 recv_ill = ill_lookup_on_ifindex( 15219 ii->ipsec_in_rill_index, !ii->ipsec_in_v4, 15220 NULL, NULL, NULL, NULL); 15221 rill_need_rele = B_TRUE; 15222 } else { 15223 recv_ill = ill; 15224 } 15225 15226 if ((ill == NULL) || (recv_ill == NULL)) { 15227 ip0dbg(("ip_fanout_proto_again: interface " 15228 "disappeared\n")); 15229 if (ill != NULL) 15230 ill_refrele(ill); 15231 if (recv_ill != NULL) 15232 ill_refrele(recv_ill); 15233 freemsg(ipsec_mp); 15234 return; 15235 } 15236 } 15237 15238 ASSERT(ill != NULL && recv_ill != NULL); 15239 15240 if (mp->b_datap->db_type == M_CTL) { 15241 /* 15242 * AH/ESP is returning the ICMP message after 15243 * removing their headers. Fanout again till 15244 * it gets to the right protocol. 15245 */ 15246 if (ii->ipsec_in_v4) { 15247 icmph_t *icmph; 15248 int iph_hdr_length; 15249 int hdr_length; 15250 15251 ipha = (ipha_t *)mp->b_rptr; 15252 iph_hdr_length = IPH_HDR_LENGTH(ipha); 15253 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 15254 ipha = (ipha_t *)&icmph[1]; 15255 hdr_length = IPH_HDR_LENGTH(ipha); 15256 /* 15257 * icmp_inbound_error_fanout may need to do pullupmsg. 15258 * Reset the type to M_DATA. 15259 */ 15260 mp->b_datap->db_type = M_DATA; 15261 icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, 15262 icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, 15263 B_FALSE, ill, ii->ipsec_in_zoneid); 15264 } else { 15265 icmp6_t *icmp6; 15266 int hdr_length; 15267 15268 ip6h = (ip6_t *)mp->b_rptr; 15269 /* Don't call hdr_length_v6() unless you have to. */ 15270 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 15271 hdr_length = ip_hdr_length_v6(mp, ip6h); 15272 else 15273 hdr_length = IPV6_HDR_LEN; 15274 15275 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 15276 /* 15277 * icmp_inbound_error_fanout_v6 may need to do 15278 * pullupmsg. Reset the type to M_DATA. 15279 */ 15280 mp->b_datap->db_type = M_DATA; 15281 icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, 15282 ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); 15283 } 15284 if (ill_need_rele) 15285 ill_refrele(ill); 15286 if (rill_need_rele) 15287 ill_refrele(recv_ill); 15288 return; 15289 } 15290 15291 if (ii->ipsec_in_v4) { 15292 ipha = (ipha_t *)mp->b_rptr; 15293 dst = ipha->ipha_dst; 15294 if (CLASSD(dst)) { 15295 /* 15296 * Multicast has to be delivered to all streams. 15297 */ 15298 dst = INADDR_BROADCAST; 15299 } 15300 15301 if (ire == NULL) { 15302 ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid); 15303 if (ire == NULL) { 15304 if (ill_need_rele) 15305 ill_refrele(ill); 15306 if (rill_need_rele) 15307 ill_refrele(recv_ill); 15308 ip1dbg(("ip_fanout_proto_again: " 15309 "IRE not found")); 15310 freemsg(ipsec_mp); 15311 return; 15312 } 15313 ire_need_rele = B_TRUE; 15314 } 15315 15316 switch (ipha->ipha_protocol) { 15317 case IPPROTO_UDP: 15318 ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, 15319 recv_ill); 15320 if (ire_need_rele) 15321 ire_refrele(ire); 15322 break; 15323 case IPPROTO_TCP: 15324 if (!ire_need_rele) 15325 IRE_REFHOLD(ire); 15326 mp = ip_tcp_input(mp, ipha, ill, B_TRUE, 15327 ire, ipsec_mp, 0, ill->ill_rq, NULL); 15328 IRE_REFRELE(ire); 15329 if (mp != NULL) 15330 squeue_enter_chain(GET_SQUEUE(mp), mp, 15331 mp, 1, SQTAG_IP_PROTO_AGAIN); 15332 break; 15333 case IPPROTO_SCTP: 15334 if (!ire_need_rele) 15335 IRE_REFHOLD(ire); 15336 ip_sctp_input(mp, ipha, ill, B_TRUE, ire, 15337 ipsec_mp, 0, ill->ill_rq, dst); 15338 break; 15339 default: 15340 ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, 15341 recv_ill); 15342 if (ire_need_rele) 15343 ire_refrele(ire); 15344 break; 15345 } 15346 } else { 15347 uint32_t rput_flags = 0; 15348 15349 ip6h = (ip6_t *)mp->b_rptr; 15350 v6dstp = &ip6h->ip6_dst; 15351 /* 15352 * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast 15353 * address. 15354 * 15355 * Currently, we don't store that state in the IPSEC_IN 15356 * message, and we may need to. 15357 */ 15358 rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? 15359 IP6_IN_LLMCAST : 0); 15360 ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, 15361 NULL); 15362 } 15363 if (ill_need_rele) 15364 ill_refrele(ill); 15365 if (rill_need_rele) 15366 ill_refrele(recv_ill); 15367 } 15368 15369 /* 15370 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 15371 * returns 'true' if there are still fragments left on the queue, in 15372 * which case we restart the timer. 15373 */ 15374 void 15375 ill_frag_timer(void *arg) 15376 { 15377 ill_t *ill = (ill_t *)arg; 15378 boolean_t frag_pending; 15379 15380 mutex_enter(&ill->ill_lock); 15381 ASSERT(!ill->ill_fragtimer_executing); 15382 if (ill->ill_state_flags & ILL_CONDEMNED) { 15383 ill->ill_frag_timer_id = 0; 15384 mutex_exit(&ill->ill_lock); 15385 return; 15386 } 15387 ill->ill_fragtimer_executing = 1; 15388 mutex_exit(&ill->ill_lock); 15389 15390 frag_pending = ill_frag_timeout(ill, ip_g_frag_timeout); 15391 15392 /* 15393 * Restart the timer, if we have fragments pending or if someone 15394 * wanted us to be scheduled again. 15395 */ 15396 mutex_enter(&ill->ill_lock); 15397 ill->ill_fragtimer_executing = 0; 15398 ill->ill_frag_timer_id = 0; 15399 if (frag_pending || ill->ill_fragtimer_needrestart) 15400 ill_frag_timer_start(ill); 15401 mutex_exit(&ill->ill_lock); 15402 } 15403 15404 void 15405 ill_frag_timer_start(ill_t *ill) 15406 { 15407 ASSERT(MUTEX_HELD(&ill->ill_lock)); 15408 15409 /* If the ill is closing or opening don't proceed */ 15410 if (ill->ill_state_flags & ILL_CONDEMNED) 15411 return; 15412 15413 if (ill->ill_fragtimer_executing) { 15414 /* 15415 * ill_frag_timer is currently executing. Just record the 15416 * the fact that we want the timer to be restarted. 15417 * ill_frag_timer will post a timeout before it returns, 15418 * ensuring it will be called again. 15419 */ 15420 ill->ill_fragtimer_needrestart = 1; 15421 return; 15422 } 15423 15424 if (ill->ill_frag_timer_id == 0) { 15425 /* 15426 * The timer is neither running nor is the timeout handler 15427 * executing. Post a timeout so that ill_frag_timer will be 15428 * called 15429 */ 15430 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 15431 MSEC_TO_TICK(ip_g_frag_timo_ms >> 1)); 15432 ill->ill_fragtimer_needrestart = 0; 15433 } 15434 } 15435 15436 /* 15437 * This routine is needed for loopback when forwarding multicasts. 15438 * 15439 * IPQoS Notes: 15440 * IPPF processing is done in fanout routines. 15441 * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, 15442 * processing for IPSec packets is done when it comes back in clear. 15443 * NOTE : The callers of this function need to do the ire_refrele for the 15444 * ire that is being passed in. 15445 */ 15446 void 15447 ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, 15448 ill_t *recv_ill) 15449 { 15450 ill_t *ill = (ill_t *)q->q_ptr; 15451 uint32_t sum; 15452 uint32_t u1; 15453 uint32_t u2; 15454 int hdr_length; 15455 boolean_t mctl_present; 15456 mblk_t *first_mp = mp; 15457 mblk_t *hada_mp = NULL; 15458 ipha_t *inner_ipha; 15459 15460 TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, 15461 "ip_rput_locl_start: q %p", q); 15462 15463 ASSERT(ire->ire_ipversion == IPV4_VERSION); 15464 15465 15466 #define rptr ((uchar_t *)ipha) 15467 #define iphs ((uint16_t *)ipha) 15468 15469 /* 15470 * no UDP or TCP packet should come here anymore. 15471 */ 15472 ASSERT((ipha->ipha_protocol != IPPROTO_TCP) && 15473 (ipha->ipha_protocol != IPPROTO_UDP)); 15474 15475 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 15476 if (mctl_present && 15477 ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { 15478 ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); 15479 15480 /* 15481 * It's an IPsec accelerated packet. 15482 * Keep a pointer to the data attributes around until 15483 * we allocate the ipsec_info_t. 15484 */ 15485 IPSECHW_DEBUG(IPSECHW_PKT, 15486 ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); 15487 hada_mp = first_mp; 15488 hada_mp->b_cont = NULL; 15489 /* 15490 * Since it is accelerated, it comes directly from 15491 * the ill and the data attributes is followed by 15492 * the packet data. 15493 */ 15494 ASSERT(mp->b_datap->db_type != M_CTL); 15495 first_mp = mp; 15496 mctl_present = B_FALSE; 15497 } 15498 15499 /* 15500 * IF M_CTL is not present, then ipsec_in_is_secure 15501 * should return B_TRUE. There is a case where loopback 15502 * packets has an M_CTL in the front with all the 15503 * IPSEC options set to IPSEC_PREF_NEVER - which means 15504 * ipsec_in_is_secure will return B_FALSE. As loopback 15505 * packets never comes here, it is safe to ASSERT the 15506 * following. 15507 */ 15508 ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); 15509 15510 15511 /* u1 is # words of IP options */ 15512 u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) 15513 + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 15514 15515 if (u1) { 15516 if (!ip_options_cksum(q, mp, ipha, ire)) { 15517 if (hada_mp != NULL) 15518 freemsg(hada_mp); 15519 return; 15520 } 15521 } else { 15522 /* Check the IP header checksum. */ 15523 #define uph ((uint16_t *)ipha) 15524 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + 15525 uph[6] + uph[7] + uph[8] + uph[9]; 15526 #undef uph 15527 /* finish doing IP checksum */ 15528 sum = (sum & 0xFFFF) + (sum >> 16); 15529 sum = ~(sum + (sum >> 16)) & 0xFFFF; 15530 /* 15531 * Don't verify header checksum if this packet is coming 15532 * back from AH/ESP as we already did it. 15533 */ 15534 if (!mctl_present && (sum && sum != 0xFFFF)) { 15535 BUMP_MIB(&ip_mib, ipInCksumErrs); 15536 goto drop_pkt; 15537 } 15538 } 15539 15540 /* 15541 * Count for SNMP of inbound packets for ire. As ip_proto_input 15542 * might be called more than once for secure packets, count only 15543 * the first time. 15544 */ 15545 if (!mctl_present) { 15546 UPDATE_IB_PKT_COUNT(ire); 15547 ire->ire_last_used_time = lbolt; 15548 } 15549 15550 /* Check for fragmentation offset. */ 15551 u2 = ntohs(ipha->ipha_fragment_offset_and_flags); 15552 u1 = u2 & (IPH_MF | IPH_OFFSET); 15553 if (u1) { 15554 /* 15555 * We re-assemble fragments before we do the AH/ESP 15556 * processing. Thus, M_CTL should not be present 15557 * while we are re-assembling. 15558 */ 15559 ASSERT(!mctl_present); 15560 ASSERT(first_mp == mp); 15561 if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { 15562 return; 15563 } 15564 /* 15565 * Make sure that first_mp points back to mp as 15566 * the mp we came in with could have changed in 15567 * ip_rput_fragment(). 15568 */ 15569 ipha = (ipha_t *)mp->b_rptr; 15570 first_mp = mp; 15571 } 15572 15573 /* 15574 * Clear hardware checksumming flag as it is currently only 15575 * used by TCP and UDP. 15576 */ 15577 DB_CKSUMFLAGS(mp) = 0; 15578 15579 /* Now we have a complete datagram, destined for this machine. */ 15580 u1 = IPH_HDR_LENGTH(ipha); 15581 switch (ipha->ipha_protocol) { 15582 case IPPROTO_ICMP: { 15583 ire_t *ire_zone; 15584 ilm_t *ilm; 15585 mblk_t *mp1; 15586 zoneid_t last_zoneid; 15587 15588 if (CLASSD(ipha->ipha_dst) && 15589 !(recv_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 15590 ASSERT(ire->ire_type == IRE_BROADCAST); 15591 /* 15592 * In the multicast case, applications may have joined 15593 * the group from different zones, so we need to deliver 15594 * the packet to each of them. Loop through the 15595 * multicast memberships structures (ilm) on the receive 15596 * ill and send a copy of the packet up each matching 15597 * one. However, we don't do this for multicasts sent on 15598 * the loopback interface (PHYI_LOOPBACK flag set) as 15599 * they must stay in the sender's zone. 15600 * 15601 * ilm_add_v6() ensures that ilms in the same zone are 15602 * contiguous in the ill_ilm list. We use this property 15603 * to avoid sending duplicates needed when two 15604 * applications in the same zone join the same group on 15605 * different logical interfaces: we ignore the ilm if 15606 * its zoneid is the same as the last matching one. 15607 * In addition, the sending of the packet for 15608 * ire_zoneid is delayed until all of the other ilms 15609 * have been exhausted. 15610 */ 15611 last_zoneid = -1; 15612 ILM_WALKER_HOLD(recv_ill); 15613 for (ilm = recv_ill->ill_ilm; ilm != NULL; 15614 ilm = ilm->ilm_next) { 15615 if ((ilm->ilm_flags & ILM_DELETED) || 15616 ipha->ipha_dst != ilm->ilm_addr || 15617 ilm->ilm_zoneid == last_zoneid || 15618 ilm->ilm_zoneid == ire->ire_zoneid || 15619 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 15620 continue; 15621 mp1 = ip_copymsg(first_mp); 15622 if (mp1 == NULL) 15623 continue; 15624 icmp_inbound(q, mp1, B_TRUE, ill, 15625 0, sum, mctl_present, B_TRUE, 15626 recv_ill, ilm->ilm_zoneid); 15627 last_zoneid = ilm->ilm_zoneid; 15628 } 15629 ILM_WALKER_RELE(recv_ill); 15630 } else if (ire->ire_type == IRE_BROADCAST) { 15631 /* 15632 * In the broadcast case, there may be many zones 15633 * which need a copy of the packet delivered to them. 15634 * There is one IRE_BROADCAST per broadcast address 15635 * and per zone; we walk those using a helper function. 15636 * In addition, the sending of the packet for ire is 15637 * delayed until all of the other ires have been 15638 * processed. 15639 */ 15640 IRB_REFHOLD(ire->ire_bucket); 15641 ire_zone = NULL; 15642 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 15643 ire)) != NULL) { 15644 mp1 = ip_copymsg(first_mp); 15645 if (mp1 == NULL) 15646 continue; 15647 15648 UPDATE_IB_PKT_COUNT(ire_zone); 15649 ire_zone->ire_last_used_time = lbolt; 15650 icmp_inbound(q, mp1, B_TRUE, ill, 15651 0, sum, mctl_present, B_TRUE, 15652 recv_ill, ire_zone->ire_zoneid); 15653 } 15654 IRB_REFRELE(ire->ire_bucket); 15655 } 15656 icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), 15657 ill, 0, sum, mctl_present, B_TRUE, recv_ill, 15658 ire->ire_zoneid); 15659 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 15660 "ip_rput_locl_end: q %p (%S)", q, "icmp"); 15661 return; 15662 } 15663 case IPPROTO_IGMP: 15664 /* 15665 * If we are not willing to accept IGMP packets in clear, 15666 * then check with global policy. 15667 */ 15668 if (igmp_accept_clear_messages == 0) { 15669 first_mp = ipsec_check_global_policy(first_mp, NULL, 15670 ipha, NULL, mctl_present); 15671 if (first_mp == NULL) 15672 return; 15673 } 15674 if (igmp_input(q, mp, ill)) { 15675 /* Bad packet - discarded by igmp_input */ 15676 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 15677 "ip_rput_locl_end: q %p (%S)", q, "igmp"); 15678 if (mctl_present) 15679 freeb(first_mp); 15680 return; 15681 } 15682 /* 15683 * igmp_input() may have pulled up the message so ipha needs to 15684 * be reinitialized. 15685 */ 15686 ipha = (ipha_t *)mp->b_rptr; 15687 if (ipcl_proto_search(ipha->ipha_protocol) == NULL) { 15688 /* No user-level listener for IGMP packets */ 15689 goto drop_pkt; 15690 } 15691 /* deliver to local raw users */ 15692 break; 15693 case IPPROTO_PIM: 15694 /* 15695 * If we are not willing to accept PIM packets in clear, 15696 * then check with global policy. 15697 */ 15698 if (pim_accept_clear_messages == 0) { 15699 first_mp = ipsec_check_global_policy(first_mp, NULL, 15700 ipha, NULL, mctl_present); 15701 if (first_mp == NULL) 15702 return; 15703 } 15704 if (pim_input(q, mp) != 0) { 15705 /* Bad packet - discarded by pim_input */ 15706 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 15707 "ip_rput_locl_end: q %p (%S)", q, "pim"); 15708 if (mctl_present) 15709 freeb(first_mp); 15710 return; 15711 } 15712 15713 /* 15714 * pim_input() may have pulled up the message so ipha needs to 15715 * be reinitialized. 15716 */ 15717 ipha = (ipha_t *)mp->b_rptr; 15718 if (ipcl_proto_search(ipha->ipha_protocol) == NULL) { 15719 /* No user-level listener for PIM packets */ 15720 goto drop_pkt; 15721 } 15722 /* deliver to local raw users */ 15723 break; 15724 case IPPROTO_ENCAP: 15725 /* 15726 * Handle self-encapsulated packets (IP-in-IP where 15727 * the inner addresses == the outer addresses). 15728 */ 15729 hdr_length = IPH_HDR_LENGTH(ipha); 15730 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 15731 mp->b_wptr) { 15732 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 15733 sizeof (ipha_t) - mp->b_rptr)) { 15734 BUMP_MIB(&ip_mib, ipInDiscards); 15735 freemsg(first_mp); 15736 return; 15737 } 15738 ipha = (ipha_t *)mp->b_rptr; 15739 } 15740 inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 15741 /* 15742 * Check the sanity of the inner IP header. 15743 */ 15744 if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { 15745 BUMP_MIB(&ip_mib, ipInDiscards); 15746 freemsg(first_mp); 15747 return; 15748 } 15749 if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { 15750 BUMP_MIB(&ip_mib, ipInDiscards); 15751 freemsg(first_mp); 15752 return; 15753 } 15754 if (inner_ipha->ipha_src == ipha->ipha_src && 15755 inner_ipha->ipha_dst == ipha->ipha_dst) { 15756 ipsec_in_t *ii; 15757 15758 /* 15759 * Self-encapsulated tunnel packet. Remove 15760 * the outer IP header and fanout again. 15761 * We also need to make sure that the inner 15762 * header is pulled up until options. 15763 */ 15764 mp->b_rptr = (uchar_t *)inner_ipha; 15765 ipha = inner_ipha; 15766 hdr_length = IPH_HDR_LENGTH(ipha); 15767 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { 15768 if (!pullupmsg(mp, (uchar_t *)ipha + 15769 + hdr_length - mp->b_rptr)) { 15770 freemsg(first_mp); 15771 return; 15772 } 15773 ipha = (ipha_t *)mp->b_rptr; 15774 } 15775 if (!mctl_present) { 15776 ASSERT(first_mp == mp); 15777 /* 15778 * This means that somebody is sending 15779 * Self-encapsualted packets without AH/ESP. 15780 * If AH/ESP was present, we would have already 15781 * allocated the first_mp. 15782 */ 15783 if ((first_mp = ipsec_in_alloc(B_TRUE)) == 15784 NULL) { 15785 ip1dbg(("ip_proto_input: IPSEC_IN " 15786 "allocation failure.\n")); 15787 BUMP_MIB(&ip_mib, ipInDiscards); 15788 freemsg(mp); 15789 return; 15790 } 15791 first_mp->b_cont = mp; 15792 } 15793 /* 15794 * We generally store the ill_index if we need to 15795 * do IPSEC processing as we lose the ill queue when 15796 * we come back. But in this case, we never should 15797 * have to store the ill_index here as it should have 15798 * been stored previously when we processed the 15799 * AH/ESP header in this routine or for non-ipsec 15800 * cases, we still have the queue. But for some bad 15801 * packets from the wire, we can get to IPSEC after 15802 * this and we better store the index for that case. 15803 */ 15804 ill = (ill_t *)q->q_ptr; 15805 ii = (ipsec_in_t *)first_mp->b_rptr; 15806 ii->ipsec_in_ill_index = 15807 ill->ill_phyint->phyint_ifindex; 15808 ii->ipsec_in_rill_index = 15809 recv_ill->ill_phyint->phyint_ifindex; 15810 if (ii->ipsec_in_decaps) { 15811 /* 15812 * This packet is self-encapsulated multiple 15813 * times. We don't want to recurse infinitely. 15814 * To keep it simple, drop the packet. 15815 */ 15816 BUMP_MIB(&ip_mib, ipInDiscards); 15817 freemsg(first_mp); 15818 return; 15819 } 15820 ii->ipsec_in_decaps = B_TRUE; 15821 ip_proto_input(q, first_mp, ipha, ire, recv_ill); 15822 return; 15823 } 15824 break; 15825 case IPPROTO_AH: 15826 case IPPROTO_ESP: { 15827 /* 15828 * Fast path for AH/ESP. If this is the first time 15829 * we are sending a datagram to AH/ESP, allocate 15830 * a IPSEC_IN message and prepend it. Otherwise, 15831 * just fanout. 15832 */ 15833 15834 int ipsec_rc; 15835 ipsec_in_t *ii; 15836 15837 IP_STAT(ipsec_proto_ahesp); 15838 if (!mctl_present) { 15839 ASSERT(first_mp == mp); 15840 if ((first_mp = ipsec_in_alloc(B_TRUE)) == NULL) { 15841 ip1dbg(("ip_proto_input: IPSEC_IN " 15842 "allocation failure.\n")); 15843 freemsg(hada_mp); /* okay ifnull */ 15844 BUMP_MIB(&ip_mib, ipInDiscards); 15845 freemsg(mp); 15846 return; 15847 } 15848 /* 15849 * Store the ill_index so that when we come back 15850 * from IPSEC we ride on the same queue. 15851 */ 15852 ill = (ill_t *)q->q_ptr; 15853 ii = (ipsec_in_t *)first_mp->b_rptr; 15854 ii->ipsec_in_ill_index = 15855 ill->ill_phyint->phyint_ifindex; 15856 ii->ipsec_in_rill_index = 15857 recv_ill->ill_phyint->phyint_ifindex; 15858 first_mp->b_cont = mp; 15859 /* 15860 * Cache hardware acceleration info. 15861 */ 15862 if (hada_mp != NULL) { 15863 IPSECHW_DEBUG(IPSECHW_PKT, 15864 ("ip_rput_local: caching data attr.\n")); 15865 ii->ipsec_in_accelerated = B_TRUE; 15866 ii->ipsec_in_da = hada_mp; 15867 hada_mp = NULL; 15868 } 15869 } else { 15870 ii = (ipsec_in_t *)first_mp->b_rptr; 15871 } 15872 15873 if (!ipsec_loaded()) { 15874 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 15875 ire->ire_zoneid); 15876 return; 15877 } 15878 15879 /* select inbound SA and have IPsec process the pkt */ 15880 if (ipha->ipha_protocol == IPPROTO_ESP) { 15881 esph_t *esph = ipsec_inbound_esp_sa(first_mp); 15882 if (esph == NULL) 15883 return; 15884 ASSERT(ii->ipsec_in_esp_sa != NULL); 15885 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); 15886 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 15887 first_mp, esph); 15888 } else { 15889 ah_t *ah = ipsec_inbound_ah_sa(first_mp); 15890 if (ah == NULL) 15891 return; 15892 ASSERT(ii->ipsec_in_ah_sa != NULL); 15893 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 15894 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 15895 first_mp, ah); 15896 } 15897 15898 switch (ipsec_rc) { 15899 case IPSEC_STATUS_SUCCESS: 15900 break; 15901 case IPSEC_STATUS_FAILED: 15902 BUMP_MIB(&ip_mib, ipInDiscards); 15903 /* FALLTHRU */ 15904 case IPSEC_STATUS_PENDING: 15905 return; 15906 } 15907 /* we're done with IPsec processing, send it up */ 15908 ip_fanout_proto_again(first_mp, ill, recv_ill, ire); 15909 return; 15910 } 15911 default: 15912 break; 15913 } 15914 /* 15915 * Handle protocols with which IP is less intimate. There 15916 * can be more than one stream bound to a particular 15917 * protocol. When this is the case, each one gets a copy 15918 * of any incoming packets. 15919 */ 15920 ip_fanout_proto(q, first_mp, ill, ipha, 15921 IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, 15922 B_TRUE, recv_ill, ire->ire_zoneid); 15923 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 15924 "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); 15925 return; 15926 15927 drop_pkt: 15928 freemsg(first_mp); 15929 if (hada_mp != NULL) 15930 freeb(hada_mp); 15931 TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, 15932 "ip_rput_locl_end: q %p (%S)", q, "droppkt"); 15933 #undef rptr 15934 #undef iphs 15935 15936 } 15937 15938 /* 15939 * Update any source route, record route or timestamp options. 15940 * Check that we are at end of strict source route. 15941 * The options have already been checked for sanity in ip_rput_options(). 15942 */ 15943 static boolean_t 15944 ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire) 15945 { 15946 ipoptp_t opts; 15947 uchar_t *opt; 15948 uint8_t optval; 15949 uint8_t optlen; 15950 ipaddr_t dst; 15951 uint32_t ts; 15952 ire_t *dst_ire; 15953 timestruc_t now; 15954 15955 ASSERT(ire->ire_ipversion == IPV4_VERSION); 15956 15957 ip2dbg(("ip_rput_local_options\n")); 15958 15959 for (optval = ipoptp_first(&opts, ipha); 15960 optval != IPOPT_EOL; 15961 optval = ipoptp_next(&opts)) { 15962 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 15963 opt = opts.ipoptp_cur; 15964 optlen = opts.ipoptp_len; 15965 ip2dbg(("ip_rput_local_options: opt %d, len %d\n", 15966 optval, optlen)); 15967 switch (optval) { 15968 uint32_t off; 15969 case IPOPT_SSRR: 15970 case IPOPT_LSRR: 15971 off = opt[IPOPT_OFFSET]; 15972 off--; 15973 if (optlen < IP_ADDR_LEN || 15974 off > optlen - IP_ADDR_LEN) { 15975 /* End of source route */ 15976 ip1dbg(("ip_rput_local_options: end of SR\n")); 15977 break; 15978 } 15979 /* 15980 * This will only happen if two consecutive entries 15981 * in the source route contains our address or if 15982 * it is a packet with a loose source route which 15983 * reaches us before consuming the whole source route 15984 */ 15985 ip1dbg(("ip_rput_local_options: not end of SR\n")); 15986 if (optval == IPOPT_SSRR) { 15987 goto bad_src_route; 15988 } 15989 /* 15990 * Hack: instead of dropping the packet truncate the 15991 * source route to what has been used by filling the 15992 * rest with IPOPT_NOP. 15993 */ 15994 opt[IPOPT_OLEN] = (uint8_t)off; 15995 while (off < optlen) { 15996 opt[off++] = IPOPT_NOP; 15997 } 15998 break; 15999 case IPOPT_RR: 16000 off = opt[IPOPT_OFFSET]; 16001 off--; 16002 if (optlen < IP_ADDR_LEN || 16003 off > optlen - IP_ADDR_LEN) { 16004 /* No more room - ignore */ 16005 ip1dbg(( 16006 "ip_rput_local_options: end of RR\n")); 16007 break; 16008 } 16009 bcopy(&ire->ire_src_addr, (char *)opt + off, 16010 IP_ADDR_LEN); 16011 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16012 break; 16013 case IPOPT_TS: 16014 /* Insert timestamp if there is romm */ 16015 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16016 case IPOPT_TS_TSONLY: 16017 off = IPOPT_TS_TIMELEN; 16018 break; 16019 case IPOPT_TS_PRESPEC: 16020 case IPOPT_TS_PRESPEC_RFC791: 16021 /* Verify that the address matched */ 16022 off = opt[IPOPT_OFFSET] - 1; 16023 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16024 dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 16025 NULL, ALL_ZONES, MATCH_IRE_TYPE); 16026 if (dst_ire == NULL) { 16027 /* Not for us */ 16028 break; 16029 } 16030 ire_refrele(dst_ire); 16031 /* FALLTHRU */ 16032 case IPOPT_TS_TSANDADDR: 16033 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16034 break; 16035 default: 16036 /* 16037 * ip_*put_options should have already 16038 * dropped this packet. 16039 */ 16040 cmn_err(CE_PANIC, "ip_rput_local_options: " 16041 "unknown IT - bug in ip_rput_options?\n"); 16042 return (B_TRUE); /* Keep "lint" happy */ 16043 } 16044 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 16045 /* Increase overflow counter */ 16046 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 16047 opt[IPOPT_POS_OV_FLG] = 16048 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 16049 (off << 4)); 16050 break; 16051 } 16052 off = opt[IPOPT_OFFSET] - 1; 16053 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16054 case IPOPT_TS_PRESPEC: 16055 case IPOPT_TS_PRESPEC_RFC791: 16056 case IPOPT_TS_TSANDADDR: 16057 bcopy(&ire->ire_src_addr, (char *)opt + off, 16058 IP_ADDR_LEN); 16059 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 16060 /* FALLTHRU */ 16061 case IPOPT_TS_TSONLY: 16062 off = opt[IPOPT_OFFSET] - 1; 16063 /* Compute # of milliseconds since midnight */ 16064 gethrestime(&now); 16065 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 16066 now.tv_nsec / (NANOSEC / MILLISEC); 16067 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 16068 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 16069 break; 16070 } 16071 break; 16072 } 16073 } 16074 return (B_TRUE); 16075 16076 bad_src_route: 16077 q = WR(q); 16078 /* make sure we clear any indication of a hardware checksum */ 16079 DB_CKSUMFLAGS(mp) = 0; 16080 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED); 16081 return (B_FALSE); 16082 16083 } 16084 16085 /* 16086 * Process IP options in an inbound packet. If an option affects the 16087 * effective destination address, return the next hop address via dstp. 16088 * Returns -1 if something fails in which case an ICMP error has been sent 16089 * and mp freed. 16090 */ 16091 static int 16092 ip_rput_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ipaddr_t *dstp) 16093 { 16094 ipoptp_t opts; 16095 uchar_t *opt; 16096 uint8_t optval; 16097 uint8_t optlen; 16098 ipaddr_t dst; 16099 intptr_t code = 0; 16100 ire_t *ire = NULL; 16101 16102 ip2dbg(("ip_rput_options\n")); 16103 dst = ipha->ipha_dst; 16104 for (optval = ipoptp_first(&opts, ipha); 16105 optval != IPOPT_EOL; 16106 optval = ipoptp_next(&opts)) { 16107 opt = opts.ipoptp_cur; 16108 optlen = opts.ipoptp_len; 16109 ip2dbg(("ip_rput_options: opt %d, len %d\n", 16110 optval, optlen)); 16111 /* 16112 * Note: we need to verify the checksum before we 16113 * modify anything thus this routine only extracts the next 16114 * hop dst from any source route. 16115 */ 16116 switch (optval) { 16117 uint32_t off; 16118 case IPOPT_SSRR: 16119 case IPOPT_LSRR: 16120 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 16121 ALL_ZONES, MATCH_IRE_TYPE); 16122 if (ire == NULL) { 16123 if (optval == IPOPT_SSRR) { 16124 ip1dbg(("ip_rput_options: not next" 16125 " strict source route 0x%x\n", 16126 ntohl(dst))); 16127 code = (char *)&ipha->ipha_dst - 16128 (char *)ipha; 16129 goto param_prob; /* RouterReq's */ 16130 } 16131 ip2dbg(("ip_rput_options: " 16132 "not next source route 0x%x\n", 16133 ntohl(dst))); 16134 break; 16135 } 16136 ire_refrele(ire); 16137 16138 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 16139 ip1dbg(( 16140 "ip_rput_options: bad option offset\n")); 16141 code = (char *)&opt[IPOPT_OLEN] - 16142 (char *)ipha; 16143 goto param_prob; 16144 } 16145 off = opt[IPOPT_OFFSET]; 16146 off--; 16147 redo_srr: 16148 if (optlen < IP_ADDR_LEN || 16149 off > optlen - IP_ADDR_LEN) { 16150 /* End of source route */ 16151 ip1dbg(("ip_rput_options: end of SR\n")); 16152 break; 16153 } 16154 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 16155 ip1dbg(("ip_rput_options: next hop 0x%x\n", 16156 ntohl(dst))); 16157 16158 /* 16159 * Check if our address is present more than 16160 * once as consecutive hops in source route. 16161 * XXX verify per-interface ip_forwarding 16162 * for source route? 16163 */ 16164 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 16165 ALL_ZONES, MATCH_IRE_TYPE); 16166 16167 if (ire != NULL) { 16168 ire_refrele(ire); 16169 off += IP_ADDR_LEN; 16170 goto redo_srr; 16171 } 16172 16173 if (dst == htonl(INADDR_LOOPBACK)) { 16174 ip1dbg(("ip_rput_options: loopback addr in " 16175 "source route!\n")); 16176 goto bad_src_route; 16177 } 16178 /* 16179 * For strict: verify that dst is directly 16180 * reachable. 16181 */ 16182 if (optval == IPOPT_SSRR) { 16183 ire = ire_ftable_lookup(dst, 0, 0, 16184 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 16185 MATCH_IRE_TYPE); 16186 if (ire == NULL) { 16187 ip1dbg(("ip_rput_options: SSRR not " 16188 "directly reachable: 0x%x\n", 16189 ntohl(dst))); 16190 goto bad_src_route; 16191 } 16192 ire_refrele(ire); 16193 } 16194 /* 16195 * Defer update of the offset and the record route 16196 * until the packet is forwarded. 16197 */ 16198 break; 16199 case IPOPT_RR: 16200 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 16201 ip1dbg(( 16202 "ip_rput_options: bad option offset\n")); 16203 code = (char *)&opt[IPOPT_OLEN] - 16204 (char *)ipha; 16205 goto param_prob; 16206 } 16207 break; 16208 case IPOPT_TS: 16209 /* 16210 * Verify that length >= 5 and that there is either 16211 * room for another timestamp or that the overflow 16212 * counter is not maxed out. 16213 */ 16214 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 16215 if (optlen < IPOPT_MINLEN_IT) { 16216 goto param_prob; 16217 } 16218 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 16219 ip1dbg(( 16220 "ip_rput_options: bad option offset\n")); 16221 code = (char *)&opt[IPOPT_OFFSET] - 16222 (char *)ipha; 16223 goto param_prob; 16224 } 16225 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 16226 case IPOPT_TS_TSONLY: 16227 off = IPOPT_TS_TIMELEN; 16228 break; 16229 case IPOPT_TS_TSANDADDR: 16230 case IPOPT_TS_PRESPEC: 16231 case IPOPT_TS_PRESPEC_RFC791: 16232 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 16233 break; 16234 default: 16235 code = (char *)&opt[IPOPT_POS_OV_FLG] - 16236 (char *)ipha; 16237 goto param_prob; 16238 } 16239 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 16240 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 16241 /* 16242 * No room and the overflow counter is 15 16243 * already. 16244 */ 16245 goto param_prob; 16246 } 16247 break; 16248 } 16249 } 16250 16251 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 16252 *dstp = dst; 16253 return (0); 16254 } 16255 16256 ip1dbg(("ip_rput_options: error processing IP options.")); 16257 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 16258 16259 param_prob: 16260 q = WR(q); 16261 /* make sure we clear any indication of a hardware checksum */ 16262 DB_CKSUMFLAGS(mp) = 0; 16263 icmp_param_problem(q, mp, (uint8_t)code); 16264 return (-1); 16265 16266 bad_src_route: 16267 q = WR(q); 16268 /* make sure we clear any indication of a hardware checksum */ 16269 DB_CKSUMFLAGS(mp) = 0; 16270 icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED); 16271 return (-1); 16272 } 16273 16274 /* 16275 * IP & ICMP info in >=14 msg's ... 16276 * - ip fixed part (mib2_ip_t) 16277 * - icmp fixed part (mib2_icmp_t) 16278 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 16279 * - ipRouteEntryTable (ip 21) all IPv4 IREs 16280 * - ipNetToMediaEntryTable (ip 22) IPv4 IREs for on-link destinations 16281 * - ip multicast membership (ip_member_t) 16282 * - ip multicast source filtering (ip_grpsrc_t) 16283 * - igmp fixed part (struct igmpstat) 16284 * - multicast routing stats (struct mrtstat) 16285 * - multicast routing vifs (array of struct vifctl) 16286 * - multicast routing routes (array of struct mfcctl) 16287 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 16288 * One per ill plus one generic 16289 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 16290 * One per ill plus one generic 16291 * - ipv6RouteEntry all IPv6 IREs 16292 * - ipv6NetToMediaEntry all Neighbor Cache entries 16293 * - ipv6AddrEntry all IPv6 ipifs 16294 * - ipv6 multicast membership (ipv6_member_t) 16295 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 16296 * 16297 * IP_ROUTE and IP_MEDIA are augmented in arp to include arp cache entries not 16298 * already present. 16299 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part 16300 * already filled in by caller. 16301 * Return value of 0 indicates that no messages were sent and caller 16302 * should free mpctl. 16303 */ 16304 int 16305 ip_snmp_get(queue_t *q, mblk_t *mpctl) 16306 { 16307 16308 if (mpctl == NULL || mpctl->b_cont == NULL) { 16309 return (0); 16310 } 16311 16312 if ((mpctl = ip_snmp_get_mib2_ip(q, mpctl)) == NULL) { 16313 return (1); 16314 } 16315 16316 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl)) == NULL) { 16317 return (1); 16318 } 16319 16320 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl)) == NULL) { 16321 return (1); 16322 } 16323 16324 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl)) == NULL) { 16325 return (1); 16326 } 16327 16328 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl)) == NULL) { 16329 return (1); 16330 } 16331 16332 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl)) == NULL) { 16333 return (1); 16334 } 16335 16336 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl)) == NULL) { 16337 return (1); 16338 } 16339 16340 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl)) == NULL) { 16341 return (1); 16342 } 16343 16344 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl)) == NULL) { 16345 return (1); 16346 } 16347 16348 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl)) == NULL) { 16349 return (1); 16350 } 16351 16352 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl)) == NULL) { 16353 return (1); 16354 } 16355 16356 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl)) == NULL) { 16357 return (1); 16358 } 16359 16360 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl)) == NULL) { 16361 return (1); 16362 } 16363 16364 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl)) == NULL) { 16365 return (1); 16366 } 16367 16368 if ((mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl)) == NULL) { 16369 return (1); 16370 } 16371 16372 if ((mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl)) == NULL) { 16373 return (1); 16374 } 16375 16376 if ((mpctl = sctp_snmp_get_mib2(q, mpctl)) == NULL) { 16377 return (1); 16378 } 16379 freemsg(mpctl); 16380 return (1); 16381 } 16382 16383 16384 /* Get global IPv4 statistics */ 16385 static mblk_t * 16386 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl) 16387 { 16388 struct opthdr *optp; 16389 mblk_t *mp2ctl; 16390 16391 /* 16392 * make a copy of the original message 16393 */ 16394 mp2ctl = copymsg(mpctl); 16395 16396 /* fixed length IP structure... */ 16397 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16398 optp->level = MIB2_IP; 16399 optp->name = 0; 16400 SET_MIB(ip_mib.ipForwarding, 16401 (WE_ARE_FORWARDING ? 1 : 2)); 16402 SET_MIB(ip_mib.ipDefaultTTL, 16403 (uint32_t)ip_def_ttl); 16404 SET_MIB(ip_mib.ipReasmTimeout, 16405 ip_g_frag_timeout); 16406 SET_MIB(ip_mib.ipAddrEntrySize, 16407 sizeof (mib2_ipAddrEntry_t)); 16408 SET_MIB(ip_mib.ipRouteEntrySize, 16409 sizeof (mib2_ipRouteEntry_t)); 16410 SET_MIB(ip_mib.ipNetToMediaEntrySize, 16411 sizeof (mib2_ipNetToMediaEntry_t)); 16412 SET_MIB(ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 16413 SET_MIB(ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 16414 if (!snmp_append_data(mpctl->b_cont, (char *)&ip_mib, 16415 (int)sizeof (ip_mib))) { 16416 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 16417 (uint_t)sizeof (ip_mib))); 16418 } 16419 16420 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16421 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 16422 (int)optp->level, (int)optp->name, (int)optp->len)); 16423 qreply(q, mpctl); 16424 return (mp2ctl); 16425 } 16426 16427 /* Global IPv4 ICMP statistics */ 16428 static mblk_t * 16429 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl) 16430 { 16431 struct opthdr *optp; 16432 mblk_t *mp2ctl; 16433 16434 /* 16435 * Make a copy of the original message 16436 */ 16437 mp2ctl = copymsg(mpctl); 16438 16439 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16440 optp->level = MIB2_ICMP; 16441 optp->name = 0; 16442 if (!snmp_append_data(mpctl->b_cont, (char *)&icmp_mib, 16443 (int)sizeof (icmp_mib))) { 16444 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 16445 (uint_t)sizeof (icmp_mib))); 16446 } 16447 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16448 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 16449 (int)optp->level, (int)optp->name, (int)optp->len)); 16450 qreply(q, mpctl); 16451 return (mp2ctl); 16452 } 16453 16454 /* Global IPv4 IGMP statistics */ 16455 static mblk_t * 16456 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl) 16457 { 16458 struct opthdr *optp; 16459 mblk_t *mp2ctl; 16460 16461 /* 16462 * make a copy of the original message 16463 */ 16464 mp2ctl = copymsg(mpctl); 16465 16466 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16467 optp->level = EXPER_IGMP; 16468 optp->name = 0; 16469 if (!snmp_append_data(mpctl->b_cont, (char *)&igmpstat, 16470 (int)sizeof (igmpstat))) { 16471 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 16472 (uint_t)sizeof (igmpstat))); 16473 } 16474 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16475 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 16476 (int)optp->level, (int)optp->name, (int)optp->len)); 16477 qreply(q, mpctl); 16478 return (mp2ctl); 16479 } 16480 16481 /* Global IPv4 Multicast Routing statistics */ 16482 static mblk_t * 16483 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl) 16484 { 16485 struct opthdr *optp; 16486 mblk_t *mp2ctl; 16487 16488 /* 16489 * make a copy of the original message 16490 */ 16491 mp2ctl = copymsg(mpctl); 16492 16493 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16494 optp->level = EXPER_DVMRP; 16495 optp->name = 0; 16496 if (!ip_mroute_stats(mpctl->b_cont)) { 16497 ip0dbg(("ip_mroute_stats: failed\n")); 16498 } 16499 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16500 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 16501 (int)optp->level, (int)optp->name, (int)optp->len)); 16502 qreply(q, mpctl); 16503 return (mp2ctl); 16504 } 16505 16506 /* IPv4 address information */ 16507 static mblk_t * 16508 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl) 16509 { 16510 struct opthdr *optp; 16511 mblk_t *mp2ctl; 16512 mblk_t *mp_tail = NULL; 16513 ill_t *ill; 16514 ipif_t *ipif; 16515 uint_t bitval; 16516 mib2_ipAddrEntry_t mae; 16517 zoneid_t zoneid; 16518 ill_walk_context_t ctx; 16519 16520 /* 16521 * make a copy of the original message 16522 */ 16523 mp2ctl = copymsg(mpctl); 16524 16525 /* ipAddrEntryTable */ 16526 16527 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16528 optp->level = MIB2_IP; 16529 optp->name = MIB2_IP_ADDR; 16530 zoneid = Q_TO_CONN(q)->conn_zoneid; 16531 16532 rw_enter(&ill_g_lock, RW_READER); 16533 ill = ILL_START_WALK_V4(&ctx); 16534 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 16535 for (ipif = ill->ill_ipif; ipif != NULL; 16536 ipif = ipif->ipif_next) { 16537 if (ipif->ipif_zoneid != zoneid) 16538 continue; 16539 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 16540 mae.ipAdEntInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 16541 mae.ipAdEntInfo.ae_focnt = ipif->ipif_fo_pkt_count; 16542 16543 (void) ipif_get_name(ipif, 16544 mae.ipAdEntIfIndex.o_bytes, 16545 OCTET_LENGTH); 16546 mae.ipAdEntIfIndex.o_length = 16547 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 16548 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 16549 mae.ipAdEntNetMask = ipif->ipif_net_mask; 16550 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 16551 mae.ipAdEntInfo.ae_subnet_len = 16552 ip_mask_to_plen(ipif->ipif_net_mask); 16553 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_src_addr; 16554 for (bitval = 1; 16555 bitval && 16556 !(bitval & ipif->ipif_brd_addr); 16557 bitval <<= 1) 16558 noop; 16559 mae.ipAdEntBcastAddr = bitval; 16560 mae.ipAdEntReasmMaxSize = 65535; 16561 mae.ipAdEntInfo.ae_mtu = ipif->ipif_mtu; 16562 mae.ipAdEntInfo.ae_metric = ipif->ipif_metric; 16563 mae.ipAdEntInfo.ae_broadcast_addr = 16564 ipif->ipif_brd_addr; 16565 mae.ipAdEntInfo.ae_pp_dst_addr = 16566 ipif->ipif_pp_dst_addr; 16567 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 16568 ill->ill_flags | ill->ill_phyint->phyint_flags; 16569 16570 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 16571 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 16572 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 16573 "allocate %u bytes\n", 16574 (uint_t)sizeof (mib2_ipAddrEntry_t))); 16575 } 16576 } 16577 } 16578 rw_exit(&ill_g_lock); 16579 16580 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16581 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 16582 (int)optp->level, (int)optp->name, (int)optp->len)); 16583 qreply(q, mpctl); 16584 return (mp2ctl); 16585 } 16586 16587 /* IPv6 address information */ 16588 static mblk_t * 16589 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl) 16590 { 16591 struct opthdr *optp; 16592 mblk_t *mp2ctl; 16593 mblk_t *mp_tail = NULL; 16594 ill_t *ill; 16595 ipif_t *ipif; 16596 mib2_ipv6AddrEntry_t mae6; 16597 zoneid_t zoneid; 16598 ill_walk_context_t ctx; 16599 16600 /* 16601 * make a copy of the original message 16602 */ 16603 mp2ctl = copymsg(mpctl); 16604 16605 /* ipv6AddrEntryTable */ 16606 16607 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16608 optp->level = MIB2_IP6; 16609 optp->name = MIB2_IP6_ADDR; 16610 zoneid = Q_TO_CONN(q)->conn_zoneid; 16611 16612 rw_enter(&ill_g_lock, RW_READER); 16613 ill = ILL_START_WALK_V6(&ctx); 16614 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 16615 for (ipif = ill->ill_ipif; ipif; ipif = ipif->ipif_next) { 16616 if (ipif->ipif_zoneid != zoneid) 16617 continue; 16618 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 16619 mae6.ipv6AddrInfo.ae_obcnt = ipif->ipif_ob_pkt_count; 16620 mae6.ipv6AddrInfo.ae_focnt = ipif->ipif_fo_pkt_count; 16621 16622 (void) ipif_get_name(ipif, 16623 mae6.ipv6AddrIfIndex.o_bytes, 16624 OCTET_LENGTH); 16625 mae6.ipv6AddrIfIndex.o_length = 16626 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 16627 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 16628 mae6.ipv6AddrPfxLength = 16629 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 16630 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 16631 mae6.ipv6AddrInfo.ae_subnet_len = 16632 mae6.ipv6AddrPfxLength; 16633 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6src_addr; 16634 16635 /* Type: stateless(1), stateful(2), unknown(3) */ 16636 if (ipif->ipif_flags & IPIF_ADDRCONF) 16637 mae6.ipv6AddrType = 1; 16638 else 16639 mae6.ipv6AddrType = 2; 16640 /* Anycast: true(1), false(2) */ 16641 if (ipif->ipif_flags & IPIF_ANYCAST) 16642 mae6.ipv6AddrAnycastFlag = 1; 16643 else 16644 mae6.ipv6AddrAnycastFlag = 2; 16645 16646 /* 16647 * Address status: preferred(1), deprecated(2), 16648 * invalid(3), inaccessible(4), unknown(5) 16649 */ 16650 if (ipif->ipif_flags & IPIF_NOLOCAL) 16651 mae6.ipv6AddrStatus = 3; 16652 else if (ipif->ipif_flags & IPIF_DEPRECATED) 16653 mae6.ipv6AddrStatus = 2; 16654 else 16655 mae6.ipv6AddrStatus = 1; 16656 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_mtu; 16657 mae6.ipv6AddrInfo.ae_metric = ipif->ipif_metric; 16658 mae6.ipv6AddrInfo.ae_pp_dst_addr = 16659 ipif->ipif_v6pp_dst_addr; 16660 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 16661 ill->ill_flags | ill->ill_phyint->phyint_flags; 16662 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 16663 (char *)&mae6, 16664 (int)sizeof (mib2_ipv6AddrEntry_t))) { 16665 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 16666 "allocate %u bytes\n", 16667 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 16668 } 16669 } 16670 } 16671 rw_exit(&ill_g_lock); 16672 16673 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16674 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 16675 (int)optp->level, (int)optp->name, (int)optp->len)); 16676 qreply(q, mpctl); 16677 return (mp2ctl); 16678 } 16679 16680 /* IPv4 multicast group membership. */ 16681 static mblk_t * 16682 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl) 16683 { 16684 struct opthdr *optp; 16685 mblk_t *mp2ctl; 16686 ill_t *ill; 16687 ipif_t *ipif; 16688 ilm_t *ilm; 16689 ip_member_t ipm; 16690 mblk_t *mp_tail = NULL; 16691 ill_walk_context_t ctx; 16692 zoneid_t zoneid; 16693 16694 /* 16695 * make a copy of the original message 16696 */ 16697 mp2ctl = copymsg(mpctl); 16698 zoneid = Q_TO_CONN(q)->conn_zoneid; 16699 16700 /* ipGroupMember table */ 16701 optp = (struct opthdr *)&mpctl->b_rptr[ 16702 sizeof (struct T_optmgmt_ack)]; 16703 optp->level = MIB2_IP; 16704 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 16705 16706 rw_enter(&ill_g_lock, RW_READER); 16707 ill = ILL_START_WALK_V4(&ctx); 16708 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 16709 ILM_WALKER_HOLD(ill); 16710 for (ipif = ill->ill_ipif; ipif != NULL; 16711 ipif = ipif->ipif_next) { 16712 if (ipif->ipif_zoneid != zoneid) 16713 continue; /* not this zone */ 16714 (void) ipif_get_name(ipif, 16715 ipm.ipGroupMemberIfIndex.o_bytes, 16716 OCTET_LENGTH); 16717 ipm.ipGroupMemberIfIndex.o_length = 16718 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 16719 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 16720 ASSERT(ilm->ilm_ipif != NULL); 16721 ASSERT(ilm->ilm_ill == NULL); 16722 if (ilm->ilm_ipif != ipif) 16723 continue; 16724 ipm.ipGroupMemberAddress = ilm->ilm_addr; 16725 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 16726 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 16727 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 16728 (char *)&ipm, (int)sizeof (ipm))) { 16729 ip1dbg(("ip_snmp_get_mib2_ip_group: " 16730 "failed to allocate %u bytes\n", 16731 (uint_t)sizeof (ipm))); 16732 } 16733 } 16734 } 16735 ILM_WALKER_RELE(ill); 16736 } 16737 rw_exit(&ill_g_lock); 16738 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16739 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 16740 (int)optp->level, (int)optp->name, (int)optp->len)); 16741 qreply(q, mpctl); 16742 return (mp2ctl); 16743 } 16744 16745 /* IPv6 multicast group membership. */ 16746 static mblk_t * 16747 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl) 16748 { 16749 struct opthdr *optp; 16750 mblk_t *mp2ctl; 16751 ill_t *ill; 16752 ilm_t *ilm; 16753 ipv6_member_t ipm6; 16754 mblk_t *mp_tail = NULL; 16755 ill_walk_context_t ctx; 16756 zoneid_t zoneid; 16757 16758 /* 16759 * make a copy of the original message 16760 */ 16761 mp2ctl = copymsg(mpctl); 16762 zoneid = Q_TO_CONN(q)->conn_zoneid; 16763 16764 /* ip6GroupMember table */ 16765 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16766 optp->level = MIB2_IP6; 16767 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 16768 16769 rw_enter(&ill_g_lock, RW_READER); 16770 ill = ILL_START_WALK_V6(&ctx); 16771 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 16772 ILM_WALKER_HOLD(ill); 16773 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 16774 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 16775 ASSERT(ilm->ilm_ipif == NULL); 16776 ASSERT(ilm->ilm_ill != NULL); 16777 if (ilm->ilm_zoneid != zoneid) 16778 continue; /* not this zone */ 16779 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 16780 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 16781 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 16782 if (!snmp_append_data2(mpctl->b_cont, 16783 &mp_tail, 16784 (char *)&ipm6, (int)sizeof (ipm6))) { 16785 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 16786 "failed to allocate %u bytes\n", 16787 (uint_t)sizeof (ipm6))); 16788 } 16789 } 16790 ILM_WALKER_RELE(ill); 16791 } 16792 rw_exit(&ill_g_lock); 16793 16794 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16795 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 16796 (int)optp->level, (int)optp->name, (int)optp->len)); 16797 qreply(q, mpctl); 16798 return (mp2ctl); 16799 } 16800 16801 /* IP multicast filtered sources */ 16802 static mblk_t * 16803 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl) 16804 { 16805 struct opthdr *optp; 16806 mblk_t *mp2ctl; 16807 ill_t *ill; 16808 ipif_t *ipif; 16809 ilm_t *ilm; 16810 ip_grpsrc_t ips; 16811 mblk_t *mp_tail = NULL; 16812 ill_walk_context_t ctx; 16813 zoneid_t zoneid; 16814 int i; 16815 slist_t *sl; 16816 16817 /* 16818 * make a copy of the original message 16819 */ 16820 mp2ctl = copymsg(mpctl); 16821 zoneid = Q_TO_CONN(q)->conn_zoneid; 16822 16823 /* ipGroupSource table */ 16824 optp = (struct opthdr *)&mpctl->b_rptr[ 16825 sizeof (struct T_optmgmt_ack)]; 16826 optp->level = MIB2_IP; 16827 optp->name = EXPER_IP_GROUP_SOURCES; 16828 16829 rw_enter(&ill_g_lock, RW_READER); 16830 ill = ILL_START_WALK_V4(&ctx); 16831 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 16832 ILM_WALKER_HOLD(ill); 16833 for (ipif = ill->ill_ipif; ipif != NULL; 16834 ipif = ipif->ipif_next) { 16835 if (ipif->ipif_zoneid != zoneid) 16836 continue; /* not this zone */ 16837 (void) ipif_get_name(ipif, 16838 ips.ipGroupSourceIfIndex.o_bytes, 16839 OCTET_LENGTH); 16840 ips.ipGroupSourceIfIndex.o_length = 16841 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 16842 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 16843 ASSERT(ilm->ilm_ipif != NULL); 16844 ASSERT(ilm->ilm_ill == NULL); 16845 sl = ilm->ilm_filter; 16846 if (ilm->ilm_ipif != ipif || SLIST_IS_EMPTY(sl)) 16847 continue; 16848 ips.ipGroupSourceGroup = ilm->ilm_addr; 16849 for (i = 0; i < sl->sl_numsrc; i++) { 16850 if (!IN6_IS_ADDR_V4MAPPED( 16851 &sl->sl_addr[i])) 16852 continue; 16853 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 16854 ips.ipGroupSourceAddress); 16855 if (snmp_append_data2(mpctl->b_cont, 16856 &mp_tail, (char *)&ips, 16857 (int)sizeof (ips)) == 0) { 16858 ip1dbg(("ip_snmp_get_mib2_" 16859 "ip_group_src: failed to " 16860 "allocate %u bytes\n", 16861 (uint_t)sizeof (ips))); 16862 } 16863 } 16864 } 16865 } 16866 ILM_WALKER_RELE(ill); 16867 } 16868 rw_exit(&ill_g_lock); 16869 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16870 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 16871 (int)optp->level, (int)optp->name, (int)optp->len)); 16872 qreply(q, mpctl); 16873 return (mp2ctl); 16874 } 16875 16876 /* IPv6 multicast filtered sources. */ 16877 static mblk_t * 16878 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl) 16879 { 16880 struct opthdr *optp; 16881 mblk_t *mp2ctl; 16882 ill_t *ill; 16883 ilm_t *ilm; 16884 ipv6_grpsrc_t ips6; 16885 mblk_t *mp_tail = NULL; 16886 ill_walk_context_t ctx; 16887 zoneid_t zoneid; 16888 int i; 16889 slist_t *sl; 16890 16891 /* 16892 * make a copy of the original message 16893 */ 16894 mp2ctl = copymsg(mpctl); 16895 zoneid = Q_TO_CONN(q)->conn_zoneid; 16896 16897 /* ip6GroupMember table */ 16898 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16899 optp->level = MIB2_IP6; 16900 optp->name = EXPER_IP6_GROUP_SOURCES; 16901 16902 rw_enter(&ill_g_lock, RW_READER); 16903 ill = ILL_START_WALK_V6(&ctx); 16904 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 16905 ILM_WALKER_HOLD(ill); 16906 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 16907 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 16908 ASSERT(ilm->ilm_ipif == NULL); 16909 ASSERT(ilm->ilm_ill != NULL); 16910 sl = ilm->ilm_filter; 16911 if (ilm->ilm_zoneid != zoneid || SLIST_IS_EMPTY(sl)) 16912 continue; 16913 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 16914 for (i = 0; i < sl->sl_numsrc; i++) { 16915 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 16916 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 16917 (char *)&ips6, (int)sizeof (ips6))) { 16918 ip1dbg(("ip_snmp_get_mib2_ip6_" 16919 "group_src: failed to allocate " 16920 "%u bytes\n", 16921 (uint_t)sizeof (ips6))); 16922 } 16923 } 16924 } 16925 ILM_WALKER_RELE(ill); 16926 } 16927 rw_exit(&ill_g_lock); 16928 16929 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16930 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 16931 (int)optp->level, (int)optp->name, (int)optp->len)); 16932 qreply(q, mpctl); 16933 return (mp2ctl); 16934 } 16935 16936 /* Multicast routing virtual interface table. */ 16937 static mblk_t * 16938 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl) 16939 { 16940 struct opthdr *optp; 16941 mblk_t *mp2ctl; 16942 16943 /* 16944 * make a copy of the original message 16945 */ 16946 mp2ctl = copymsg(mpctl); 16947 16948 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16949 optp->level = EXPER_DVMRP; 16950 optp->name = EXPER_DVMRP_VIF; 16951 if (!ip_mroute_vif(mpctl->b_cont)) { 16952 ip0dbg(("ip_mroute_vif: failed\n")); 16953 } 16954 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16955 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 16956 (int)optp->level, (int)optp->name, (int)optp->len)); 16957 qreply(q, mpctl); 16958 return (mp2ctl); 16959 } 16960 16961 /* Multicast routing table. */ 16962 static mblk_t * 16963 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl) 16964 { 16965 struct opthdr *optp; 16966 mblk_t *mp2ctl; 16967 16968 /* 16969 * make a copy of the original message 16970 */ 16971 mp2ctl = copymsg(mpctl); 16972 16973 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16974 optp->level = EXPER_DVMRP; 16975 optp->name = EXPER_DVMRP_MRT; 16976 if (!ip_mroute_mrt(mpctl->b_cont)) { 16977 ip0dbg(("ip_mroute_mrt: failed\n")); 16978 } 16979 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 16980 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 16981 (int)optp->level, (int)optp->name, (int)optp->len)); 16982 qreply(q, mpctl); 16983 return (mp2ctl); 16984 } 16985 16986 /* 16987 * Return both ipRouteEntryTable, and ipNetToMediaEntryTable 16988 * in one IRE walk. 16989 */ 16990 static mblk_t * 16991 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl) 16992 { 16993 struct opthdr *optp; 16994 mblk_t *mp2ctl; /* Returned */ 16995 mblk_t *mp3ctl; /* nettomedia */ 16996 /* 16997 * We need two listptrs, for ipRouteEntryTable and 16998 * ipNetToMediaEntryTable to pass to ip_snmp_get2_v4() 16999 */ 17000 listptr_t re_ntme_v4[2]; 17001 zoneid_t zoneid; 17002 17003 /* 17004 * make a copy of the original message 17005 */ 17006 mp2ctl = copymsg(mpctl); 17007 mp3ctl = copymsg(mpctl); 17008 if (mp3ctl == NULL) { 17009 freemsg(mp2ctl); 17010 freemsg(mpctl); 17011 return (NULL); 17012 } 17013 17014 re_ntme_v4[0].lp_head = mpctl->b_cont; /* ipRouteEntryTable */ 17015 re_ntme_v4[1].lp_head = mp3ctl->b_cont; /* ipNetToMediaEntryTable */ 17016 /* 17017 * We assign NULL to tail ptrs as snmp_append_data2() will assign 17018 * proper values when called. 17019 */ 17020 re_ntme_v4[0].lp_tail = NULL; 17021 re_ntme_v4[1].lp_tail = NULL; 17022 17023 zoneid = Q_TO_CONN(q)->conn_zoneid; 17024 ire_walk_v4(ip_snmp_get2_v4, (char *)re_ntme_v4, zoneid); 17025 if (zoneid == GLOBAL_ZONEID) { 17026 /* 17027 * Those IREs are used by Mobile-IP; since mipagent(1M) requires 17028 * the sys_net_config privilege, it can only run in the global 17029 * zone, so we don't display these IREs in the other zones. 17030 */ 17031 ire_walk_srcif_table_v4(ip_snmp_get2_v4, (char *)re_ntme_v4); 17032 ire_walk_ill_mrtun(0, 0, ip_snmp_get2_v4, (char *)re_ntme_v4, 17033 NULL); 17034 } 17035 17036 /* ipRouteEntryTable in mpctl */ 17037 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17038 optp->level = MIB2_IP; 17039 optp->name = MIB2_IP_ROUTE; 17040 optp->len = (t_uscalar_t)msgdsize(re_ntme_v4[0].lp_head); 17041 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 17042 (int)optp->level, (int)optp->name, (int)optp->len)); 17043 qreply(q, mpctl); 17044 17045 /* ipNetToMediaEntryTable in mp3ctl */ 17046 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17047 optp->level = MIB2_IP; 17048 optp->name = MIB2_IP_MEDIA; 17049 optp->len = (t_uscalar_t)msgdsize(re_ntme_v4[1].lp_head); 17050 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 17051 (int)optp->level, (int)optp->name, (int)optp->len)); 17052 qreply(q, mp3ctl); 17053 return (mp2ctl); 17054 } 17055 17056 /* 17057 * Return both ipv6RouteEntryTable, and ipv6NetToMediaEntryTable 17058 * in one IRE walk. 17059 */ 17060 static mblk_t * 17061 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl) 17062 { 17063 struct opthdr *optp; 17064 mblk_t *mp2ctl; /* Returned */ 17065 mblk_t *mp3ctl; /* nettomedia */ 17066 listptr_t re_ntme_v6; 17067 zoneid_t zoneid; 17068 17069 /* 17070 * make a copy of the original message 17071 */ 17072 mp2ctl = copymsg(mpctl); 17073 mp3ctl = copymsg(mpctl); 17074 if (mp3ctl == NULL) { 17075 freemsg(mp2ctl); 17076 freemsg(mpctl); 17077 return (NULL); 17078 } 17079 17080 /* 17081 * We assign NULL to tail ptrs as snmp_append_data2() will assign 17082 * proper values when called. ipv6RouteEntryTable in is placed 17083 * in mpctl. 17084 */ 17085 re_ntme_v6.lp_head = mpctl->b_cont; /* ip6RouteEntryTable */ 17086 re_ntme_v6.lp_tail = NULL; 17087 zoneid = Q_TO_CONN(q)->conn_zoneid; 17088 ire_walk_v6(ip_snmp_get2_v6_route, (char *)&re_ntme_v6, zoneid); 17089 17090 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17091 optp->level = MIB2_IP6; 17092 optp->name = MIB2_IP6_ROUTE; 17093 optp->len = (t_uscalar_t)msgdsize(re_ntme_v6.lp_head); 17094 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 17095 (int)optp->level, (int)optp->name, (int)optp->len)); 17096 qreply(q, mpctl); 17097 17098 /* ipv6NetToMediaEntryTable in mp3ctl */ 17099 re_ntme_v6.lp_head = mp3ctl->b_cont; /* ip6NetToMediaEntryTable */ 17100 re_ntme_v6.lp_tail = NULL; 17101 ndp_walk(NULL, ip_snmp_get2_v6_media, (uchar_t *)&re_ntme_v6); 17102 17103 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17104 optp->level = MIB2_IP6; 17105 optp->name = MIB2_IP6_MEDIA; 17106 optp->len = (t_uscalar_t)msgdsize(re_ntme_v6.lp_head); 17107 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 17108 (int)optp->level, (int)optp->name, (int)optp->len)); 17109 qreply(q, mp3ctl); 17110 return (mp2ctl); 17111 } 17112 17113 /* 17114 * ICMPv6 mib: One per ill 17115 */ 17116 static mblk_t * 17117 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl) 17118 { 17119 struct opthdr *optp; 17120 mblk_t *mp2ctl; 17121 ill_t *ill; 17122 ill_walk_context_t ctx; 17123 mblk_t *mp_tail = NULL; 17124 17125 /* 17126 * Make a copy of the original message 17127 */ 17128 mp2ctl = copymsg(mpctl); 17129 17130 /* fixed length IPv6 structure ... */ 17131 17132 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17133 optp->level = MIB2_IP6; 17134 optp->name = 0; 17135 /* Include "unknown interface" ip6_mib */ 17136 ip6_mib.ipv6IfIndex = 0; /* Flag to netstat */ 17137 SET_MIB(ip6_mib.ipv6Forwarding, ipv6_forward ? 1 : 2); 17138 SET_MIB(ip6_mib.ipv6DefaultHopLimit, ipv6_def_hops); 17139 SET_MIB(ip6_mib.ipv6IfStatsEntrySize, 17140 sizeof (mib2_ipv6IfStatsEntry_t)); 17141 SET_MIB(ip6_mib.ipv6AddrEntrySize, sizeof (mib2_ipv6AddrEntry_t)); 17142 SET_MIB(ip6_mib.ipv6RouteEntrySize, sizeof (mib2_ipv6RouteEntry_t)); 17143 SET_MIB(ip6_mib.ipv6NetToMediaEntrySize, 17144 sizeof (mib2_ipv6NetToMediaEntry_t)); 17145 SET_MIB(ip6_mib.ipv6MemberEntrySize, sizeof (ipv6_member_t)); 17146 SET_MIB(ip6_mib.ipv6GroupSourceEntrySize, sizeof (ipv6_grpsrc_t)); 17147 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, (char *)&ip6_mib, 17148 (int)sizeof (ip6_mib))) { 17149 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 17150 (uint_t)sizeof (ip6_mib))); 17151 } 17152 17153 rw_enter(&ill_g_lock, RW_READER); 17154 ill = ILL_START_WALK_V6(&ctx); 17155 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 17156 ill->ill_ip6_mib->ipv6IfIndex = 17157 ill->ill_phyint->phyint_ifindex; 17158 SET_MIB(ill->ill_ip6_mib->ipv6Forwarding, 17159 ipv6_forward ? 1 : 2); 17160 SET_MIB(ill->ill_ip6_mib->ipv6DefaultHopLimit, 17161 ill->ill_max_hops); 17162 SET_MIB(ill->ill_ip6_mib->ipv6IfStatsEntrySize, 17163 sizeof (mib2_ipv6IfStatsEntry_t)); 17164 SET_MIB(ill->ill_ip6_mib->ipv6AddrEntrySize, 17165 sizeof (mib2_ipv6AddrEntry_t)); 17166 SET_MIB(ill->ill_ip6_mib->ipv6RouteEntrySize, 17167 sizeof (mib2_ipv6RouteEntry_t)); 17168 SET_MIB(ill->ill_ip6_mib->ipv6NetToMediaEntrySize, 17169 sizeof (mib2_ipv6NetToMediaEntry_t)); 17170 SET_MIB(ill->ill_ip6_mib->ipv6MemberEntrySize, 17171 sizeof (ipv6_member_t)); 17172 17173 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 17174 (char *)ill->ill_ip6_mib, 17175 (int)sizeof (*ill->ill_ip6_mib))) { 17176 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 17177 "%u bytes\n", 17178 (uint_t)sizeof (*ill->ill_ip6_mib))); 17179 } 17180 } 17181 rw_exit(&ill_g_lock); 17182 17183 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 17184 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 17185 (int)optp->level, (int)optp->name, (int)optp->len)); 17186 qreply(q, mpctl); 17187 return (mp2ctl); 17188 } 17189 17190 /* 17191 * ICMPv6 mib: One per ill 17192 */ 17193 static mblk_t * 17194 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl) 17195 { 17196 struct opthdr *optp; 17197 mblk_t *mp2ctl; 17198 ill_t *ill; 17199 ill_walk_context_t ctx; 17200 mblk_t *mp_tail = NULL; 17201 /* 17202 * Make a copy of the original message 17203 */ 17204 mp2ctl = copymsg(mpctl); 17205 17206 /* fixed length ICMPv6 structure ... */ 17207 17208 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 17209 optp->level = MIB2_ICMP6; 17210 optp->name = 0; 17211 /* Include "unknown interface" icmp6_mib */ 17212 icmp6_mib.ipv6IfIcmpIfIndex = 0; /* Flag to netstat */ 17213 icmp6_mib.ipv6IfIcmpEntrySize = sizeof (mib2_ipv6IfIcmpEntry_t); 17214 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, (char *)&icmp6_mib, 17215 (int)sizeof (icmp6_mib))) { 17216 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 17217 (uint_t)sizeof (icmp6_mib))); 17218 } 17219 17220 rw_enter(&ill_g_lock, RW_READER); 17221 ill = ILL_START_WALK_V6(&ctx); 17222 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 17223 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 17224 ill->ill_phyint->phyint_ifindex; 17225 ill->ill_icmp6_mib->ipv6IfIcmpEntrySize = 17226 sizeof (mib2_ipv6IfIcmpEntry_t); 17227 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 17228 (char *)ill->ill_icmp6_mib, 17229 (int)sizeof (*ill->ill_icmp6_mib))) { 17230 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 17231 "%u bytes\n", 17232 (uint_t)sizeof (*ill->ill_icmp6_mib))); 17233 } 17234 } 17235 rw_exit(&ill_g_lock); 17236 17237 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 17238 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 17239 (int)optp->level, (int)optp->name, (int)optp->len)); 17240 qreply(q, mpctl); 17241 return (mp2ctl); 17242 } 17243 17244 /* 17245 * ire_walk routine to create both ipRouteEntryTable and 17246 * ipNetToMediaEntryTable in one IRE walk 17247 */ 17248 static void 17249 ip_snmp_get2_v4(ire_t *ire, listptr_t re_ntme[]) 17250 { 17251 ill_t *ill; 17252 ipif_t *ipif; 17253 mblk_t *llmp; 17254 dl_unitdata_req_t *dlup; 17255 mib2_ipRouteEntry_t re; 17256 mib2_ipNetToMediaEntry_t ntme; 17257 ipaddr_t gw_addr; 17258 17259 ASSERT(ire->ire_ipversion == IPV4_VERSION); 17260 17261 /* 17262 * Return all IRE types for route table... let caller pick and choose 17263 */ 17264 re.ipRouteDest = ire->ire_addr; 17265 ipif = ire->ire_ipif; 17266 re.ipRouteIfIndex.o_length = 0; 17267 if (ire->ire_type == IRE_CACHE) { 17268 ill = (ill_t *)ire->ire_stq->q_ptr; 17269 re.ipRouteIfIndex.o_length = 17270 ill->ill_name_length == 0 ? 0 : 17271 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 17272 bcopy(ill->ill_name, re.ipRouteIfIndex.o_bytes, 17273 re.ipRouteIfIndex.o_length); 17274 } else if (ipif != NULL) { 17275 (void) ipif_get_name(ipif, re.ipRouteIfIndex.o_bytes, 17276 OCTET_LENGTH); 17277 re.ipRouteIfIndex.o_length = 17278 mi_strlen(re.ipRouteIfIndex.o_bytes); 17279 } 17280 re.ipRouteMetric1 = -1; 17281 re.ipRouteMetric2 = -1; 17282 re.ipRouteMetric3 = -1; 17283 re.ipRouteMetric4 = -1; 17284 17285 gw_addr = ire->ire_gateway_addr; 17286 17287 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK|IRE_BROADCAST)) 17288 re.ipRouteNextHop = ire->ire_src_addr; 17289 else 17290 re.ipRouteNextHop = gw_addr; 17291 /* indirect(4), direct(3), or invalid(2) */ 17292 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 17293 re.ipRouteType = 2; 17294 else 17295 re.ipRouteType = (gw_addr != 0) ? 4 : 3; 17296 re.ipRouteProto = -1; 17297 re.ipRouteAge = gethrestime_sec() - ire->ire_create_time; 17298 re.ipRouteMask = ire->ire_mask; 17299 re.ipRouteMetric5 = -1; 17300 re.ipRouteInfo.re_max_frag = ire->ire_max_frag; 17301 re.ipRouteInfo.re_frag_flag = ire->ire_frag_flag; 17302 re.ipRouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 17303 llmp = ire->ire_dlureq_mp; 17304 re.ipRouteInfo.re_ref = ire->ire_refcnt; 17305 re.ipRouteInfo.re_src_addr = ire->ire_src_addr; 17306 re.ipRouteInfo.re_ire_type = ire->ire_type; 17307 re.ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 17308 re.ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 17309 re.ipRouteInfo.re_flags = ire->ire_flags; 17310 re.ipRouteInfo.re_in_ill.o_length = 0; 17311 if (ire->ire_in_ill != NULL) { 17312 re.ipRouteInfo.re_in_ill.o_length = 17313 ire->ire_in_ill->ill_name_length == 0 ? 0 : 17314 MIN(OCTET_LENGTH, ire->ire_in_ill->ill_name_length - 1); 17315 bcopy(ire->ire_in_ill->ill_name, 17316 re.ipRouteInfo.re_in_ill.o_bytes, 17317 re.ipRouteInfo.re_in_ill.o_length); 17318 } 17319 re.ipRouteInfo.re_in_src_addr = ire->ire_in_src_addr; 17320 if (!snmp_append_data2(re_ntme[0].lp_head, &(re_ntme[0].lp_tail), 17321 (char *)&re, (int)sizeof (re))) { 17322 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 17323 (uint_t)sizeof (re))); 17324 } 17325 17326 if (ire->ire_type != IRE_CACHE || gw_addr != 0) 17327 return; 17328 /* 17329 * only IRE_CACHE entries that are for a directly connected subnet 17330 * get appended to net -> phys addr table 17331 * (others in arp) 17332 */ 17333 ntme.ipNetToMediaIfIndex.o_length = 0; 17334 ill = ire_to_ill(ire); 17335 ASSERT(ill != NULL); 17336 ntme.ipNetToMediaIfIndex.o_length = 17337 ill->ill_name_length == 0 ? 0 : 17338 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 17339 bcopy(ill->ill_name, ntme.ipNetToMediaIfIndex.o_bytes, 17340 ntme.ipNetToMediaIfIndex.o_length); 17341 17342 ntme.ipNetToMediaPhysAddress.o_length = 0; 17343 if (llmp) { 17344 uchar_t *addr; 17345 17346 dlup = (dl_unitdata_req_t *)llmp->b_rptr; 17347 /* Remove sap from address */ 17348 if (ill->ill_sap_length < 0) 17349 addr = llmp->b_rptr + dlup->dl_dest_addr_offset; 17350 else 17351 addr = llmp->b_rptr + dlup->dl_dest_addr_offset + 17352 ill->ill_sap_length; 17353 17354 ntme.ipNetToMediaPhysAddress.o_length = 17355 MIN(OCTET_LENGTH, ill->ill_phys_addr_length); 17356 bcopy(addr, ntme.ipNetToMediaPhysAddress.o_bytes, 17357 ntme.ipNetToMediaPhysAddress.o_length); 17358 } 17359 ntme.ipNetToMediaNetAddress = ire->ire_addr; 17360 /* assume dynamic (may be changed in arp) */ 17361 ntme.ipNetToMediaType = 3; 17362 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (uint32_t); 17363 bcopy(&ire->ire_mask, ntme.ipNetToMediaInfo.ntm_mask.o_bytes, 17364 ntme.ipNetToMediaInfo.ntm_mask.o_length); 17365 ntme.ipNetToMediaInfo.ntm_flags = ACE_F_RESOLVED; 17366 if (!snmp_append_data2(re_ntme[1].lp_head, &(re_ntme[1].lp_tail), 17367 (char *)&ntme, (int)sizeof (ntme))) { 17368 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 17369 (uint_t)sizeof (ntme))); 17370 } 17371 } 17372 17373 /* 17374 * ire_walk routine to create ipv6RouteEntryTable. 17375 */ 17376 static void 17377 ip_snmp_get2_v6_route(ire_t *ire, listptr_t *re_ntme) 17378 { 17379 ill_t *ill; 17380 ipif_t *ipif; 17381 mib2_ipv6RouteEntry_t re; 17382 in6_addr_t gw_addr_v6; 17383 17384 ASSERT(ire->ire_ipversion == IPV6_VERSION); 17385 17386 /* 17387 * Return all IRE types for route table... let caller pick and choose 17388 */ 17389 re.ipv6RouteDest = ire->ire_addr_v6; 17390 re.ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 17391 re.ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 17392 re.ipv6RouteIfIndex.o_length = 0; 17393 ipif = ire->ire_ipif; 17394 if (ire->ire_type == IRE_CACHE) { 17395 ill = (ill_t *)ire->ire_stq->q_ptr; 17396 re.ipv6RouteIfIndex.o_length = 17397 ill->ill_name_length == 0 ? 0 : 17398 MIN(OCTET_LENGTH, ill->ill_name_length - 1); 17399 bcopy(ill->ill_name, re.ipv6RouteIfIndex.o_bytes, 17400 re.ipv6RouteIfIndex.o_length); 17401 } else if (ipif != NULL) { 17402 (void) ipif_get_name(ipif, re.ipv6RouteIfIndex.o_bytes, 17403 OCTET_LENGTH); 17404 re.ipv6RouteIfIndex.o_length = 17405 mi_strlen(re.ipv6RouteIfIndex.o_bytes); 17406 } 17407 17408 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 17409 17410 mutex_enter(&ire->ire_lock); 17411 gw_addr_v6 = ire->ire_gateway_addr_v6; 17412 mutex_exit(&ire->ire_lock); 17413 17414 if (ire->ire_type & (IRE_INTERFACE|IRE_LOOPBACK)) 17415 re.ipv6RouteNextHop = ire->ire_src_addr_v6; 17416 else 17417 re.ipv6RouteNextHop = gw_addr_v6; 17418 17419 /* remote(4), local(3), or discard(2) */ 17420 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 17421 re.ipv6RouteType = 2; 17422 else if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) 17423 re.ipv6RouteType = 3; 17424 else 17425 re.ipv6RouteType = 4; 17426 17427 re.ipv6RouteProtocol = -1; 17428 re.ipv6RoutePolicy = 0; 17429 re.ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 17430 re.ipv6RouteNextHopRDI = 0; 17431 re.ipv6RouteWeight = 0; 17432 re.ipv6RouteMetric = 0; 17433 re.ipv6RouteInfo.re_max_frag = ire->ire_max_frag; 17434 re.ipv6RouteInfo.re_frag_flag = ire->ire_frag_flag; 17435 re.ipv6RouteInfo.re_rtt = ire->ire_uinfo.iulp_rtt; 17436 re.ipv6RouteInfo.re_src_addr = ire->ire_src_addr_v6; 17437 re.ipv6RouteInfo.re_ire_type = ire->ire_type; 17438 re.ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 17439 re.ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 17440 re.ipv6RouteInfo.re_ref = ire->ire_refcnt; 17441 re.ipv6RouteInfo.re_flags = ire->ire_flags; 17442 17443 if (!snmp_append_data2(re_ntme->lp_head, &(re_ntme->lp_tail), 17444 (char *)&re, (int)sizeof (re))) { 17445 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 17446 (uint_t)sizeof (re))); 17447 } 17448 } 17449 17450 /* 17451 * ndp_walk routine to create ipv6NetToMediaEntryTable 17452 */ 17453 static int 17454 ip_snmp_get2_v6_media(nce_t *nce, listptr_t *re_ntme) 17455 { 17456 ill_t *ill; 17457 mib2_ipv6NetToMediaEntry_t ntme; 17458 dl_unitdata_req_t *dl; 17459 17460 ill = nce->nce_ill; 17461 ASSERT(ill->ill_isv6); 17462 17463 /* 17464 * Neighbor cache entry attached to IRE with on-link 17465 * destination. 17466 */ 17467 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 17468 ntme.ipv6NetToMediaNetAddress = nce->nce_addr; 17469 if ((ill->ill_flags & ILLF_XRESOLV) && 17470 (nce->nce_res_mp != NULL)) { 17471 dl = (dl_unitdata_req_t *)(nce->nce_res_mp->b_rptr); 17472 ntme.ipv6NetToMediaPhysAddress.o_length = 17473 dl->dl_dest_addr_length; 17474 } else { 17475 ntme.ipv6NetToMediaPhysAddress.o_length = 17476 ill->ill_phys_addr_length; 17477 } 17478 if (nce->nce_res_mp != NULL) { 17479 bcopy((char *)nce->nce_res_mp->b_rptr + 17480 NCE_LL_ADDR_OFFSET(ill), 17481 ntme.ipv6NetToMediaPhysAddress.o_bytes, 17482 ntme.ipv6NetToMediaPhysAddress.o_length); 17483 } else { 17484 bzero(ntme.ipv6NetToMediaPhysAddress.o_bytes, 17485 ill->ill_phys_addr_length); 17486 } 17487 /* 17488 * Note: Returns ND_* states. Should be: 17489 * reachable(1), stale(2), delay(3), probe(4), 17490 * invalid(5), unknown(6) 17491 */ 17492 ntme.ipv6NetToMediaState = nce->nce_state; 17493 ntme.ipv6NetToMediaLastUpdated = 0; 17494 17495 /* other(1), dynamic(2), static(3), local(4) */ 17496 if (IN6_IS_ADDR_LOOPBACK(&nce->nce_addr)) { 17497 ntme.ipv6NetToMediaType = 4; 17498 } else if (IN6_IS_ADDR_MULTICAST(&nce->nce_addr)) { 17499 ntme.ipv6NetToMediaType = 1; 17500 } else { 17501 ntme.ipv6NetToMediaType = 2; 17502 } 17503 17504 if (!snmp_append_data2(re_ntme->lp_head, 17505 &(re_ntme->lp_tail), (char *)&ntme, (int)sizeof (ntme))) { 17506 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 17507 (uint_t)sizeof (ntme))); 17508 } 17509 return (0); 17510 } 17511 17512 /* 17513 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 17514 */ 17515 /* ARGSUSED */ 17516 int 17517 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 17518 { 17519 switch (level) { 17520 case MIB2_IP: 17521 case MIB2_ICMP: 17522 switch (name) { 17523 default: 17524 break; 17525 } 17526 return (1); 17527 default: 17528 return (1); 17529 } 17530 } 17531 17532 /* 17533 * Called before the options are updated to check if this packet will 17534 * be source routed from here. 17535 * This routine assumes that the options are well formed i.e. that they 17536 * have already been checked. 17537 */ 17538 static boolean_t 17539 ip_source_routed(ipha_t *ipha) 17540 { 17541 ipoptp_t opts; 17542 uchar_t *opt; 17543 uint8_t optval; 17544 uint8_t optlen; 17545 ipaddr_t dst; 17546 ire_t *ire; 17547 17548 if (IS_SIMPLE_IPH(ipha)) { 17549 ip2dbg(("not source routed\n")); 17550 return (B_FALSE); 17551 } 17552 dst = ipha->ipha_dst; 17553 for (optval = ipoptp_first(&opts, ipha); 17554 optval != IPOPT_EOL; 17555 optval = ipoptp_next(&opts)) { 17556 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 17557 opt = opts.ipoptp_cur; 17558 optlen = opts.ipoptp_len; 17559 ip2dbg(("ip_source_routed: opt %d, len %d\n", 17560 optval, optlen)); 17561 switch (optval) { 17562 uint32_t off; 17563 case IPOPT_SSRR: 17564 case IPOPT_LSRR: 17565 /* 17566 * If dst is one of our addresses and there are some 17567 * entries left in the source route return (true). 17568 */ 17569 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, 17570 ALL_ZONES, MATCH_IRE_TYPE); 17571 if (ire == NULL) { 17572 ip2dbg(("ip_source_routed: not next" 17573 " source route 0x%x\n", 17574 ntohl(dst))); 17575 return (B_FALSE); 17576 } 17577 ire_refrele(ire); 17578 off = opt[IPOPT_OFFSET]; 17579 off--; 17580 if (optlen < IP_ADDR_LEN || 17581 off > optlen - IP_ADDR_LEN) { 17582 /* End of source route */ 17583 ip1dbg(("ip_source_routed: end of SR\n")); 17584 return (B_FALSE); 17585 } 17586 return (B_TRUE); 17587 } 17588 } 17589 ip2dbg(("not source routed\n")); 17590 return (B_FALSE); 17591 } 17592 17593 /* 17594 * Check if the packet contains any source route. 17595 */ 17596 static boolean_t 17597 ip_source_route_included(ipha_t *ipha) 17598 { 17599 ipoptp_t opts; 17600 uint8_t optval; 17601 17602 if (IS_SIMPLE_IPH(ipha)) 17603 return (B_FALSE); 17604 for (optval = ipoptp_first(&opts, ipha); 17605 optval != IPOPT_EOL; 17606 optval = ipoptp_next(&opts)) { 17607 switch (optval) { 17608 case IPOPT_SSRR: 17609 case IPOPT_LSRR: 17610 return (B_TRUE); 17611 } 17612 } 17613 return (B_FALSE); 17614 } 17615 17616 /* 17617 * Called when the IRE expiration timer fires. 17618 */ 17619 /* ARGSUSED */ 17620 void 17621 ip_trash_timer_expire(void *args) 17622 { 17623 int flush_flag = 0; 17624 17625 /* 17626 * ip_ire_expire_id is protected by ip_trash_timer_lock. 17627 * This lock makes sure that a new invocation of this function 17628 * that occurs due to an almost immediate timer firing will not 17629 * progress beyond this point until the current invocation is done 17630 */ 17631 mutex_enter(&ip_trash_timer_lock); 17632 ip_ire_expire_id = 0; 17633 mutex_exit(&ip_trash_timer_lock); 17634 17635 /* Periodic timer */ 17636 if (ip_ire_arp_time_elapsed >= ip_ire_arp_interval) { 17637 /* 17638 * Remove all IRE_CACHE entries since they might 17639 * contain arp information. 17640 */ 17641 flush_flag |= FLUSH_ARP_TIME; 17642 ip_ire_arp_time_elapsed = 0; 17643 IP_STAT(ip_ire_arp_timer_expired); 17644 } 17645 if (ip_ire_rd_time_elapsed >= ip_ire_redir_interval) { 17646 /* Remove all redirects */ 17647 flush_flag |= FLUSH_REDIRECT_TIME; 17648 ip_ire_rd_time_elapsed = 0; 17649 IP_STAT(ip_ire_redirect_timer_expired); 17650 } 17651 if (ip_ire_pmtu_time_elapsed >= ip_ire_pathmtu_interval) { 17652 /* Increase path mtu */ 17653 flush_flag |= FLUSH_MTU_TIME; 17654 ip_ire_pmtu_time_elapsed = 0; 17655 IP_STAT(ip_ire_pmtu_timer_expired); 17656 } 17657 if (flush_flag != 0) { 17658 /* Walk all IPv4 IRE's and update them */ 17659 ire_walk_v4(ire_expire, (char *)(uintptr_t)flush_flag, 17660 ALL_ZONES); 17661 } 17662 if (flush_flag & FLUSH_MTU_TIME) { 17663 /* 17664 * Walk all IPv6 IRE's and update them 17665 * Note that ARP and redirect timers are not 17666 * needed since NUD handles stale entries. 17667 */ 17668 flush_flag = FLUSH_MTU_TIME; 17669 ire_walk_v6(ire_expire, (char *)(uintptr_t)flush_flag, 17670 ALL_ZONES); 17671 } 17672 17673 ip_ire_arp_time_elapsed += ip_timer_interval; 17674 ip_ire_rd_time_elapsed += ip_timer_interval; 17675 ip_ire_pmtu_time_elapsed += ip_timer_interval; 17676 17677 /* 17678 * Hold the lock to serialize timeout calls and prevent 17679 * stale values in ip_ire_expire_id. Otherwise it is possible 17680 * for the timer to fire and a new invocation of this function 17681 * to start before the return value of timeout has been stored 17682 * in ip_ire_expire_id by the current invocation. 17683 */ 17684 mutex_enter(&ip_trash_timer_lock); 17685 ip_ire_expire_id = timeout(ip_trash_timer_expire, NULL, 17686 MSEC_TO_TICK(ip_timer_interval)); 17687 mutex_exit(&ip_trash_timer_lock); 17688 } 17689 17690 /* 17691 * Called by the memory allocator subsystem directly, when the system 17692 * is running low on memory. 17693 */ 17694 /* ARGSUSED */ 17695 void 17696 ip_trash_ire_reclaim(void *args) 17697 { 17698 ire_cache_count_t icc; 17699 ire_cache_reclaim_t icr; 17700 ncc_cache_count_t ncc; 17701 nce_cache_reclaim_t ncr; 17702 uint_t delete_cnt; 17703 /* 17704 * Memory reclaim call back. 17705 * Count unused, offlink, pmtu, and onlink IRE_CACHE entries. 17706 * Then, with a target of freeing 1/Nth of IRE_CACHE 17707 * entries, determine what fraction to free for 17708 * each category of IRE_CACHE entries giving absolute priority 17709 * in the order of onlink, pmtu, offlink, unused (e.g. no pmtu 17710 * entry will be freed unless all offlink entries are freed). 17711 */ 17712 icc.icc_total = 0; 17713 icc.icc_unused = 0; 17714 icc.icc_offlink = 0; 17715 icc.icc_pmtu = 0; 17716 icc.icc_onlink = 0; 17717 ire_walk(ire_cache_count, (char *)&icc); 17718 17719 /* 17720 * Free NCEs for IPv6 like the onlink ires. 17721 */ 17722 ncc.ncc_total = 0; 17723 ncc.ncc_host = 0; 17724 ndp_walk(NULL, (pfi_t)ndp_cache_count, (uchar_t *)&ncc); 17725 17726 ASSERT(icc.icc_total == icc.icc_unused + icc.icc_offlink + 17727 icc.icc_pmtu + icc.icc_onlink); 17728 delete_cnt = icc.icc_total/ip_ire_reclaim_fraction; 17729 IP_STAT(ip_trash_ire_reclaim_calls); 17730 if (delete_cnt == 0) 17731 return; 17732 IP_STAT(ip_trash_ire_reclaim_success); 17733 /* Always delete all unused offlink entries */ 17734 icr.icr_unused = 1; 17735 if (delete_cnt <= icc.icc_unused) { 17736 /* 17737 * Only need to free unused entries. In other words, 17738 * there are enough unused entries to free to meet our 17739 * target number of freed ire cache entries. 17740 */ 17741 icr.icr_offlink = icr.icr_pmtu = icr.icr_onlink = 0; 17742 ncr.ncr_host = 0; 17743 } else if (delete_cnt <= icc.icc_unused + icc.icc_offlink) { 17744 /* 17745 * Only need to free unused entries, plus a fraction of offlink 17746 * entries. It follows from the first if statement that 17747 * icc_offlink is non-zero, and that delete_cnt != icc_unused. 17748 */ 17749 delete_cnt -= icc.icc_unused; 17750 /* Round up # deleted by truncating fraction */ 17751 icr.icr_offlink = icc.icc_offlink / delete_cnt; 17752 icr.icr_pmtu = icr.icr_onlink = 0; 17753 ncr.ncr_host = 0; 17754 } else if (delete_cnt <= 17755 icc.icc_unused + icc.icc_offlink + icc.icc_pmtu) { 17756 /* 17757 * Free all unused and offlink entries, plus a fraction of 17758 * pmtu entries. It follows from the previous if statement 17759 * that icc_pmtu is non-zero, and that 17760 * delete_cnt != icc_unused + icc_offlink. 17761 */ 17762 icr.icr_offlink = 1; 17763 delete_cnt -= icc.icc_unused + icc.icc_offlink; 17764 /* Round up # deleted by truncating fraction */ 17765 icr.icr_pmtu = icc.icc_pmtu / delete_cnt; 17766 icr.icr_onlink = 0; 17767 ncr.ncr_host = 0; 17768 } else { 17769 /* 17770 * Free all unused, offlink, and pmtu entries, plus a fraction 17771 * of onlink entries. If we're here, then we know that 17772 * icc_onlink is non-zero, and that 17773 * delete_cnt != icc_unused + icc_offlink + icc_pmtu. 17774 */ 17775 icr.icr_offlink = icr.icr_pmtu = 1; 17776 delete_cnt -= icc.icc_unused + icc.icc_offlink + 17777 icc.icc_pmtu; 17778 /* Round up # deleted by truncating fraction */ 17779 icr.icr_onlink = icc.icc_onlink / delete_cnt; 17780 /* Using the same delete fraction as for onlink IREs */ 17781 ncr.ncr_host = ncc.ncc_host / delete_cnt; 17782 } 17783 #ifdef DEBUG 17784 ip1dbg(("IP reclaim: target %d out of %d current %d/%d/%d/%d " 17785 "fractions %d/%d/%d/%d\n", 17786 icc.icc_total/ip_ire_reclaim_fraction, icc.icc_total, 17787 icc.icc_unused, icc.icc_offlink, 17788 icc.icc_pmtu, icc.icc_onlink, 17789 icr.icr_unused, icr.icr_offlink, 17790 icr.icr_pmtu, icr.icr_onlink)); 17791 #endif 17792 ire_walk(ire_cache_reclaim, (char *)&icr); 17793 if (ncr.ncr_host != 0) 17794 ndp_walk(NULL, (pfi_t)ndp_cache_reclaim, 17795 (uchar_t *)&ncr); 17796 #ifdef DEBUG 17797 icc.icc_total = 0; icc.icc_unused = 0; icc.icc_offlink = 0; 17798 icc.icc_pmtu = 0; icc.icc_onlink = 0; 17799 ire_walk(ire_cache_count, (char *)&icc); 17800 ip1dbg(("IP reclaim: result total %d %d/%d/%d/%d\n", 17801 icc.icc_total, icc.icc_unused, icc.icc_offlink, 17802 icc.icc_pmtu, icc.icc_onlink)); 17803 #endif 17804 } 17805 17806 /* 17807 * ip_unbind is called when a copy of an unbind request is received from the 17808 * upper level protocol. We remove this conn from any fanout hash list it is 17809 * on, and zero out the bind information. No reply is expected up above. 17810 */ 17811 mblk_t * 17812 ip_unbind(queue_t *q, mblk_t *mp) 17813 { 17814 conn_t *connp = Q_TO_CONN(q); 17815 17816 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 17817 17818 ipcl_hash_remove(connp); 17819 17820 ASSERT(mp->b_cont == NULL); 17821 /* 17822 * Convert mp into a T_OK_ACK 17823 */ 17824 mp = mi_tpi_ok_ack_alloc(mp); 17825 17826 /* 17827 * should not happen in practice... T_OK_ACK is smaller than the 17828 * original message. 17829 */ 17830 if (mp == NULL) 17831 return (NULL); 17832 17833 /* 17834 * Don't bzero the ports if its TCP since TCP still needs the 17835 * lport to remove it from its own bind hash. TCP will do the 17836 * cleanup. 17837 */ 17838 if (!IPCL_IS_TCP(connp)) 17839 bzero(&connp->u_port, sizeof (connp->u_port)); 17840 17841 return (mp); 17842 } 17843 17844 /* 17845 * Write side put procedure. Outbound data, IOCTLs, responses from 17846 * resolvers, etc, come down through here. 17847 */ 17848 void 17849 ip_output(void *arg, mblk_t *mp, void *arg2, int caller) 17850 { 17851 conn_t *connp = NULL; 17852 queue_t *q = (queue_t *)arg2; 17853 ipha_t *ipha; 17854 #define rptr ((uchar_t *)ipha) 17855 ire_t *ire = NULL; 17856 ire_t *sctp_ire = NULL; 17857 uint32_t v_hlen_tos_len; 17858 ipaddr_t dst; 17859 mblk_t *first_mp = NULL; 17860 boolean_t mctl_present; 17861 ipsec_out_t *io; 17862 int match_flags; 17863 ill_t *attach_ill = NULL; 17864 /* Bind to IPIF_NOFAILOVER ill etc. */ 17865 ill_t *xmit_ill = NULL; /* IP_XMIT_IF etc. */ 17866 ipif_t *dst_ipif; 17867 boolean_t multirt_need_resolve = B_FALSE; 17868 mblk_t *copy_mp = NULL; 17869 int err; 17870 zoneid_t zoneid; 17871 boolean_t need_decref = B_FALSE; 17872 boolean_t ignore_dontroute = B_FALSE; 17873 boolean_t ignore_nexthop = B_FALSE; 17874 boolean_t ip_nexthop = B_FALSE; 17875 ipaddr_t nexthop_addr; 17876 17877 #ifdef _BIG_ENDIAN 17878 #define V_HLEN (v_hlen_tos_len >> 24) 17879 #else 17880 #define V_HLEN (v_hlen_tos_len & 0xFF) 17881 #endif 17882 17883 TRACE_1(TR_FAC_IP, TR_IP_WPUT_START, 17884 "ip_wput_start: q %p", q); 17885 17886 /* 17887 * ip_wput fast path 17888 */ 17889 17890 /* is packet from ARP ? */ 17891 if (q->q_next != NULL) 17892 goto qnext; 17893 17894 connp = (conn_t *)arg; 17895 zoneid = (connp != NULL ? connp->conn_zoneid : ALL_ZONES); 17896 17897 /* is queue flow controlled? */ 17898 if ((q->q_first != NULL || connp->conn_draining) && 17899 (caller == IP_WPUT)) { 17900 ASSERT(!need_decref); 17901 (void) putq(q, mp); 17902 return; 17903 } 17904 17905 /* Multidata transmit? */ 17906 if (DB_TYPE(mp) == M_MULTIDATA) { 17907 /* 17908 * We should never get here, since all Multidata messages 17909 * originating from tcp should have been directed over to 17910 * tcp_multisend() in the first place. 17911 */ 17912 BUMP_MIB(&ip_mib, ipOutDiscards); 17913 freemsg(mp); 17914 return; 17915 } else if (DB_TYPE(mp) != M_DATA) 17916 goto notdata; 17917 if (mp->b_flag & MSGHASREF) { 17918 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 17919 mp->b_flag &= ~MSGHASREF; 17920 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 17921 need_decref = B_TRUE; 17922 } 17923 ipha = (ipha_t *)mp->b_rptr; 17924 17925 /* is IP header non-aligned or mblk smaller than basic IP header */ 17926 #ifndef SAFETY_BEFORE_SPEED 17927 if (!OK_32PTR(rptr) || 17928 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) 17929 goto hdrtoosmall; 17930 #endif 17931 17932 /* 17933 * If there is a policy, try to attach an ipsec_out in 17934 * the front. At the end, first_mp either points to a 17935 * M_DATA message or IPSEC_OUT message linked to a 17936 * M_DATA message. We have to do it now as we might 17937 * lose the "conn" if we go through ip_newroute. 17938 */ 17939 if (connp->conn_out_enforce_policy || (connp->conn_latch != NULL)) { 17940 if (((mp = ipsec_attach_ipsec_out(mp, connp, NULL, 17941 ipha->ipha_protocol)) == NULL)) { 17942 if (need_decref) 17943 CONN_DEC_REF(connp); 17944 return; 17945 } else { 17946 ASSERT(mp->b_datap->db_type == M_CTL); 17947 first_mp = mp; 17948 mp = mp->b_cont; 17949 mctl_present = B_TRUE; 17950 } 17951 } else { 17952 first_mp = mp; 17953 mctl_present = B_FALSE; 17954 } 17955 17956 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 17957 17958 /* is wrong version or IP options present */ 17959 if (V_HLEN != IP_SIMPLE_HDR_VERSION) 17960 goto version_hdrlen_check; 17961 dst = ipha->ipha_dst; 17962 17963 if (connp->conn_nofailover_ill != NULL) { 17964 attach_ill = conn_get_held_ill(connp, 17965 &connp->conn_nofailover_ill, &err); 17966 if (err == ILL_LOOKUP_FAILED) { 17967 if (need_decref) 17968 CONN_DEC_REF(connp); 17969 freemsg(first_mp); 17970 return; 17971 } 17972 } 17973 17974 /* is packet multicast? */ 17975 if (CLASSD(dst)) 17976 goto multicast; 17977 17978 if ((connp->conn_dontroute) || (connp->conn_xmit_if_ill != NULL) || 17979 (connp->conn_nexthop_set)) { 17980 /* 17981 * If the destination is a broadcast or a loopback 17982 * address, SO_DONTROUTE, IP_XMIT_IF and IP_NEXTHOP go 17983 * through the standard path. But in the case of local 17984 * destination only SO_DONTROUTE and IP_NEXTHOP go through 17985 * the standard path not IP_XMIT_IF. 17986 */ 17987 ire = ire_cache_lookup(dst, zoneid); 17988 if ((ire == NULL) || ((ire->ire_type != IRE_BROADCAST) && 17989 (ire->ire_type != IRE_LOOPBACK))) { 17990 if ((connp->conn_dontroute || 17991 connp->conn_nexthop_set) && (ire != NULL) && 17992 (ire->ire_type == IRE_LOCAL)) 17993 goto standard_path; 17994 17995 if (ire != NULL) { 17996 ire_refrele(ire); 17997 /* No more access to ire */ 17998 ire = NULL; 17999 } 18000 /* 18001 * bypass routing checks and go directly to 18002 * interface. 18003 */ 18004 if (connp->conn_dontroute) { 18005 goto dontroute; 18006 } else if (connp->conn_nexthop_set) { 18007 ip_nexthop = B_TRUE; 18008 nexthop_addr = connp->conn_nexthop_v4; 18009 goto send_from_ill; 18010 } 18011 18012 /* 18013 * If IP_XMIT_IF socket option is set, 18014 * then we allow unicast and multicast 18015 * packets to go through the ill. It is 18016 * quite possible that the destination 18017 * is not in the ire cache table and we 18018 * do not want to go to ip_newroute() 18019 * instead we call ip_newroute_ipif. 18020 */ 18021 xmit_ill = conn_get_held_ill(connp, 18022 &connp->conn_xmit_if_ill, &err); 18023 if (err == ILL_LOOKUP_FAILED) { 18024 if (attach_ill != NULL) 18025 ill_refrele(attach_ill); 18026 if (need_decref) 18027 CONN_DEC_REF(connp); 18028 freemsg(first_mp); 18029 return; 18030 } 18031 goto send_from_ill; 18032 } 18033 standard_path: 18034 /* Must be a broadcast, a loopback or a local ire */ 18035 if (ire != NULL) { 18036 ire_refrele(ire); 18037 /* No more access to ire */ 18038 ire = NULL; 18039 } 18040 } 18041 18042 if (attach_ill != NULL) 18043 goto send_from_ill; 18044 18045 /* 18046 * We cache IRE_CACHEs to avoid lookups. We don't do 18047 * this for the tcp global queue and listen end point 18048 * as it does not really have a real destination to 18049 * talk to. This is also true for SCTP. 18050 */ 18051 if (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 18052 !connp->conn_fully_bound) { 18053 ire = ire_cache_lookup(dst, zoneid); 18054 if (ire == NULL) 18055 goto noirefound; 18056 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18057 "ip_wput_end: q %p (%S)", q, "end"); 18058 18059 /* 18060 * Check if the ire has the RTF_MULTIRT flag, inherited 18061 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 18062 */ 18063 if (ire->ire_flags & RTF_MULTIRT) { 18064 18065 /* 18066 * Force the TTL of multirouted packets if required. 18067 * The TTL of such packets is bounded by the 18068 * ip_multirt_ttl ndd variable. 18069 */ 18070 if ((ip_multirt_ttl > 0) && 18071 (ipha->ipha_ttl > ip_multirt_ttl)) { 18072 ip2dbg(("ip_wput: forcing multirt TTL to %d " 18073 "(was %d), dst 0x%08x\n", 18074 ip_multirt_ttl, ipha->ipha_ttl, 18075 ntohl(ire->ire_addr))); 18076 ipha->ipha_ttl = ip_multirt_ttl; 18077 } 18078 /* 18079 * We look at this point if there are pending 18080 * unresolved routes. ire_multirt_resolvable() 18081 * checks in O(n) that all IRE_OFFSUBNET ire 18082 * entries for the packet's destination and 18083 * flagged RTF_MULTIRT are currently resolved. 18084 * If some remain unresolved, we make a copy 18085 * of the current message. It will be used 18086 * to initiate additional route resolutions. 18087 */ 18088 multirt_need_resolve = 18089 ire_multirt_need_resolve(ire->ire_addr); 18090 ip2dbg(("ip_wput[TCP]: ire %p, " 18091 "multirt_need_resolve %d, first_mp %p\n", 18092 (void *)ire, multirt_need_resolve, 18093 (void *)first_mp)); 18094 if (multirt_need_resolve) { 18095 copy_mp = copymsg(first_mp); 18096 if (copy_mp != NULL) { 18097 MULTIRT_DEBUG_TAG(copy_mp); 18098 } 18099 } 18100 } 18101 18102 ip_wput_ire(q, first_mp, ire, connp, caller); 18103 18104 /* 18105 * Try to resolve another multiroute if 18106 * ire_multirt_need_resolve() deemed it necessary. 18107 */ 18108 if (copy_mp != NULL) { 18109 ip_newroute(q, copy_mp, dst, NULL, connp); 18110 } 18111 if (need_decref) 18112 CONN_DEC_REF(connp); 18113 return; 18114 } 18115 18116 /* 18117 * Access to conn_ire_cache. (protected by conn_lock) 18118 * 18119 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't grab 18120 * the ire bucket lock here to check for CONDEMNED as it is okay to 18121 * send a packet or two with the IRE_CACHE that is going away. 18122 * Access to the ire requires an ire refhold on the ire prior to 18123 * its use since an interface unplumb thread may delete the cached 18124 * ire and release the refhold at any time. 18125 * 18126 * Caching an ire in the conn_ire_cache 18127 * 18128 * o Caching an ire pointer in the conn requires a strict check for 18129 * IRE_MARK_CONDEMNED. An interface unplumb thread deletes all relevant 18130 * ires before cleaning up the conns. So the caching of an ire pointer 18131 * in the conn is done after making sure under the bucket lock that the 18132 * ire has not yet been marked CONDEMNED. Otherwise we will end up 18133 * caching an ire after the unplumb thread has cleaned up the conn. 18134 * If the conn does not send a packet subsequently the unplumb thread 18135 * will be hanging waiting for the ire count to drop to zero. 18136 * 18137 * o We also need to atomically test for a null conn_ire_cache and 18138 * set the conn_ire_cache under the the protection of the conn_lock 18139 * to avoid races among concurrent threads trying to simultaneously 18140 * cache an ire in the conn_ire_cache. 18141 */ 18142 mutex_enter(&connp->conn_lock); 18143 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 18144 18145 if (ire != NULL && ire->ire_addr == dst && 18146 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18147 18148 IRE_REFHOLD(ire); 18149 mutex_exit(&connp->conn_lock); 18150 18151 } else { 18152 boolean_t cached = B_FALSE; 18153 connp->conn_ire_cache = NULL; 18154 mutex_exit(&connp->conn_lock); 18155 /* Release the old ire */ 18156 if (ire != NULL && sctp_ire == NULL) 18157 IRE_REFRELE_NOTR(ire); 18158 18159 ire = (ire_t *)ire_cache_lookup(dst, zoneid); 18160 if (ire == NULL) 18161 goto noirefound; 18162 IRE_REFHOLD_NOTR(ire); 18163 18164 mutex_enter(&connp->conn_lock); 18165 if (!(connp->conn_state_flags & CONN_CLOSING) && 18166 connp->conn_ire_cache == NULL) { 18167 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18168 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18169 connp->conn_ire_cache = ire; 18170 cached = B_TRUE; 18171 } 18172 rw_exit(&ire->ire_bucket->irb_lock); 18173 } 18174 mutex_exit(&connp->conn_lock); 18175 18176 /* 18177 * We can continue to use the ire but since it was 18178 * not cached, we should drop the extra reference. 18179 */ 18180 if (!cached) 18181 IRE_REFRELE_NOTR(ire); 18182 } 18183 18184 18185 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18186 "ip_wput_end: q %p (%S)", q, "end"); 18187 18188 /* 18189 * Check if the ire has the RTF_MULTIRT flag, inherited 18190 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 18191 */ 18192 if (ire->ire_flags & RTF_MULTIRT) { 18193 18194 /* 18195 * Force the TTL of multirouted packets if required. 18196 * The TTL of such packets is bounded by the 18197 * ip_multirt_ttl ndd variable. 18198 */ 18199 if ((ip_multirt_ttl > 0) && 18200 (ipha->ipha_ttl > ip_multirt_ttl)) { 18201 ip2dbg(("ip_wput: forcing multirt TTL to %d " 18202 "(was %d), dst 0x%08x\n", 18203 ip_multirt_ttl, ipha->ipha_ttl, 18204 ntohl(ire->ire_addr))); 18205 ipha->ipha_ttl = ip_multirt_ttl; 18206 } 18207 18208 /* 18209 * At this point, we check to see if there are any pending 18210 * unresolved routes. ire_multirt_resolvable() 18211 * checks in O(n) that all IRE_OFFSUBNET ire 18212 * entries for the packet's destination and 18213 * flagged RTF_MULTIRT are currently resolved. 18214 * If some remain unresolved, we make a copy 18215 * of the current message. It will be used 18216 * to initiate additional route resolutions. 18217 */ 18218 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr); 18219 ip2dbg(("ip_wput[not TCP]: ire %p, " 18220 "multirt_need_resolve %d, first_mp %p\n", 18221 (void *)ire, multirt_need_resolve, (void *)first_mp)); 18222 if (multirt_need_resolve) { 18223 copy_mp = copymsg(first_mp); 18224 if (copy_mp != NULL) { 18225 MULTIRT_DEBUG_TAG(copy_mp); 18226 } 18227 } 18228 } 18229 18230 ip_wput_ire(q, first_mp, ire, connp, caller); 18231 18232 /* 18233 * Try to resolve another multiroute if 18234 * ire_multirt_resolvable() deemed it necessary 18235 */ 18236 if (copy_mp != NULL) { 18237 ip_newroute(q, copy_mp, dst, NULL, connp); 18238 } 18239 if (need_decref) 18240 CONN_DEC_REF(connp); 18241 return; 18242 18243 qnext: 18244 /* 18245 * Upper Level Protocols pass down complete IP datagrams 18246 * as M_DATA messages. Everything else is a sideshow. 18247 * 18248 * 1) We could be re-entering ip_wput because of ip_neworute 18249 * in which case we could have a IPSEC_OUT message. We 18250 * need to pass through ip_wput like other datagrams and 18251 * hence cannot branch to ip_wput_nondata. 18252 * 18253 * 2) ARP, AH, ESP, and other clients who are on the module 18254 * instance of IP stream, give us something to deal with. 18255 * We will handle AH and ESP here and rest in ip_wput_nondata. 18256 * 18257 * 3) ICMP replies also could come here. 18258 */ 18259 if (DB_TYPE(mp) != M_DATA) { 18260 notdata: 18261 if (DB_TYPE(mp) == M_CTL) { 18262 /* 18263 * M_CTL messages are used by ARP, AH and ESP to 18264 * communicate with IP. We deal with IPSEC_IN and 18265 * IPSEC_OUT here. ip_wput_nondata handles other 18266 * cases. 18267 */ 18268 ipsec_info_t *ii = (ipsec_info_t *)mp->b_rptr; 18269 if (mp->b_cont && (mp->b_cont->b_flag & MSGHASREF)) { 18270 first_mp = mp->b_cont; 18271 first_mp->b_flag &= ~MSGHASREF; 18272 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 18273 SCTP_EXTRACT_IPINFO(first_mp, sctp_ire); 18274 CONN_DEC_REF(connp); 18275 connp = NULL; 18276 } 18277 if (ii->ipsec_info_type == IPSEC_IN) { 18278 /* 18279 * Either this message goes back to 18280 * IPSEC for further processing or to 18281 * ULP after policy checks. 18282 */ 18283 ip_fanout_proto_again(mp, NULL, NULL, NULL); 18284 return; 18285 } else if (ii->ipsec_info_type == IPSEC_OUT) { 18286 io = (ipsec_out_t *)ii; 18287 if (io->ipsec_out_proc_begin) { 18288 /* 18289 * IPSEC processing has already started. 18290 * Complete it. 18291 * IPQoS notes: We don't care what is 18292 * in ipsec_out_ill_index since this 18293 * won't be processed for IPQoS policies 18294 * in ipsec_out_process. 18295 */ 18296 ipsec_out_process(q, mp, NULL, 18297 io->ipsec_out_ill_index); 18298 return; 18299 } else { 18300 connp = (q->q_next != NULL) ? 18301 NULL : Q_TO_CONN(q); 18302 first_mp = mp; 18303 mp = mp->b_cont; 18304 mctl_present = B_TRUE; 18305 } 18306 zoneid = io->ipsec_out_zoneid; 18307 ASSERT(zoneid != ALL_ZONES); 18308 } else if (ii->ipsec_info_type == IPSEC_CTL) { 18309 /* 18310 * It's an IPsec control message requesting 18311 * an SADB update to be sent to the IPsec 18312 * hardware acceleration capable ills. 18313 */ 18314 ipsec_ctl_t *ipsec_ctl = 18315 (ipsec_ctl_t *)mp->b_rptr; 18316 ipsa_t *sa = (ipsa_t *)ipsec_ctl->ipsec_ctl_sa; 18317 uint_t satype = ipsec_ctl->ipsec_ctl_sa_type; 18318 mblk_t *cmp = mp->b_cont; 18319 18320 ASSERT(MBLKL(mp) >= sizeof (ipsec_ctl_t)); 18321 ASSERT(cmp != NULL); 18322 18323 freeb(mp); 18324 ill_ipsec_capab_send_all(satype, cmp, sa); 18325 return; 18326 } else { 18327 /* 18328 * This must be ARP. 18329 */ 18330 ip_wput_nondata(NULL, q, mp, NULL); 18331 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18332 "ip_wput_end: q %p (%S)", q, "nondata"); 18333 return; 18334 } 18335 } else { 18336 /* 18337 * This must be non-(ARP/AH/ESP) messages. 18338 */ 18339 ASSERT(!need_decref); 18340 ip_wput_nondata(NULL, q, mp, NULL); 18341 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18342 "ip_wput_end: q %p (%S)", q, "nondata"); 18343 return; 18344 } 18345 } else { 18346 first_mp = mp; 18347 mctl_present = B_FALSE; 18348 } 18349 18350 ASSERT(first_mp != NULL); 18351 /* 18352 * ICMP echo replies attach an ipsec_out and set ipsec_out_attach_if 18353 * to make sure that this packet goes out on the same interface it 18354 * came in. We handle that here. 18355 */ 18356 if (mctl_present) { 18357 uint_t ifindex; 18358 18359 io = (ipsec_out_t *)first_mp->b_rptr; 18360 if (io->ipsec_out_attach_if || 18361 io->ipsec_out_xmit_if || 18362 io->ipsec_out_ip_nexthop) { 18363 ill_t *ill; 18364 18365 /* 18366 * We may have lost the conn context if we are 18367 * coming here from ip_newroute(). Copy the 18368 * nexthop information. 18369 */ 18370 if (io->ipsec_out_ip_nexthop) { 18371 ip_nexthop = B_TRUE; 18372 nexthop_addr = io->ipsec_out_nexthop_addr; 18373 18374 ipha = (ipha_t *)mp->b_rptr; 18375 dst = ipha->ipha_dst; 18376 goto send_from_ill; 18377 } else { 18378 ASSERT(io->ipsec_out_ill_index != 0); 18379 ifindex = io->ipsec_out_ill_index; 18380 ill = ill_lookup_on_ifindex(ifindex, B_FALSE, 18381 NULL, NULL, NULL, NULL); 18382 /* 18383 * ipsec_out_xmit_if bit is used to tell 18384 * ip_wput to use the ill to send outgoing data 18385 * as we have no conn when data comes from ICMP 18386 * error msg routines. Currently this feature is 18387 * only used by ip_mrtun_forward routine. 18388 */ 18389 if (io->ipsec_out_xmit_if) { 18390 xmit_ill = ill; 18391 if (xmit_ill == NULL) { 18392 ip1dbg(("ip_output:bad ifindex " 18393 "for xmit_ill %d\n", 18394 ifindex)); 18395 freemsg(first_mp); 18396 BUMP_MIB(&ip_mib, 18397 ipOutDiscards); 18398 ASSERT(!need_decref); 18399 return; 18400 } 18401 /* Free up the ipsec_out_t mblk */ 18402 ASSERT(first_mp->b_cont == mp); 18403 first_mp->b_cont = NULL; 18404 freeb(first_mp); 18405 /* Just send the IP header+ICMP+data */ 18406 first_mp = mp; 18407 ipha = (ipha_t *)mp->b_rptr; 18408 dst = ipha->ipha_dst; 18409 goto send_from_ill; 18410 } else { 18411 attach_ill = ill; 18412 } 18413 18414 if (attach_ill == NULL) { 18415 ASSERT(xmit_ill == NULL); 18416 ip1dbg(("ip_output: bad ifindex for " 18417 "(BIND TO IPIF_NOFAILOVER) %d\n", 18418 ifindex)); 18419 freemsg(first_mp); 18420 BUMP_MIB(&ip_mib, ipOutDiscards); 18421 ASSERT(!need_decref); 18422 return; 18423 } 18424 } 18425 } 18426 } 18427 18428 ASSERT(xmit_ill == NULL); 18429 18430 /* We have a complete IP datagram heading outbound. */ 18431 ipha = (ipha_t *)mp->b_rptr; 18432 18433 #ifndef SPEED_BEFORE_SAFETY 18434 /* 18435 * Make sure we have a full-word aligned message and that at least 18436 * a simple IP header is accessible in the first message. If not, 18437 * try a pullup. 18438 */ 18439 if (!OK_32PTR(rptr) || 18440 (mp->b_wptr - rptr) < IP_SIMPLE_HDR_LENGTH) { 18441 hdrtoosmall: 18442 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { 18443 BUMP_MIB(&ip_mib, ipOutDiscards); 18444 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18445 "ip_wput_end: q %p (%S)", q, "pullupfailed"); 18446 if (first_mp == NULL) 18447 first_mp = mp; 18448 goto drop_pkt; 18449 } 18450 ipha = (ipha_t *)mp->b_rptr; 18451 if (first_mp == NULL) { 18452 ASSERT(attach_ill == NULL && xmit_ill == NULL); 18453 /* 18454 * If we got here because of "goto hdrtoosmall" 18455 * We need to attach a IPSEC_OUT. 18456 */ 18457 if (connp->conn_out_enforce_policy) { 18458 if (((mp = ipsec_attach_ipsec_out(mp, connp, 18459 NULL, ipha->ipha_protocol)) == NULL)) { 18460 if (need_decref) 18461 CONN_DEC_REF(connp); 18462 return; 18463 } else { 18464 ASSERT(mp->b_datap->db_type == M_CTL); 18465 first_mp = mp; 18466 mp = mp->b_cont; 18467 mctl_present = B_TRUE; 18468 } 18469 } else { 18470 first_mp = mp; 18471 mctl_present = B_FALSE; 18472 } 18473 } 18474 } 18475 #endif 18476 18477 /* Most of the code below is written for speed, not readability */ 18478 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 18479 18480 /* 18481 * If ip_newroute() fails, we're going to need a full 18482 * header for the icmp wraparound. 18483 */ 18484 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 18485 uint_t v_hlen; 18486 version_hdrlen_check: 18487 ASSERT(first_mp != NULL); 18488 v_hlen = V_HLEN; 18489 /* 18490 * siphon off IPv6 packets coming down from transport 18491 * layer modules here. 18492 * Note: high-order bit carries NUD reachability confirmation 18493 */ 18494 if (((v_hlen >> 4) & 0x7) == IPV6_VERSION) { 18495 /* 18496 * XXX implement a IPv4 and IPv6 packet counter per 18497 * conn and switch when ratio exceeds e.g. 10:1 18498 */ 18499 #ifdef notyet 18500 if (q->q_next == NULL) /* Avoid ill queue */ 18501 ip_setqinfo(RD(q), B_TRUE, B_TRUE); 18502 #endif 18503 BUMP_MIB(&ip_mib, ipOutIPv6); 18504 ASSERT(xmit_ill == NULL); 18505 if (attach_ill != NULL) 18506 ill_refrele(attach_ill); 18507 if (need_decref) 18508 mp->b_flag |= MSGHASREF; 18509 (void) ip_output_v6(connp, first_mp, q, caller); 18510 return; 18511 } 18512 18513 if ((v_hlen >> 4) != IP_VERSION) { 18514 BUMP_MIB(&ip_mib, ipOutDiscards); 18515 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18516 "ip_wput_end: q %p (%S)", q, "badvers"); 18517 goto drop_pkt; 18518 } 18519 /* 18520 * Is the header length at least 20 bytes? 18521 * 18522 * Are there enough bytes accessible in the header? If 18523 * not, try a pullup. 18524 */ 18525 v_hlen &= 0xF; 18526 v_hlen <<= 2; 18527 if (v_hlen < IP_SIMPLE_HDR_LENGTH) { 18528 BUMP_MIB(&ip_mib, ipOutDiscards); 18529 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18530 "ip_wput_end: q %p (%S)", q, "badlen"); 18531 goto drop_pkt; 18532 } 18533 if (v_hlen > (mp->b_wptr - rptr)) { 18534 if (!pullupmsg(mp, v_hlen)) { 18535 BUMP_MIB(&ip_mib, ipOutDiscards); 18536 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18537 "ip_wput_end: q %p (%S)", q, "badpullup2"); 18538 goto drop_pkt; 18539 } 18540 ipha = (ipha_t *)mp->b_rptr; 18541 } 18542 /* 18543 * Move first entry from any source route into ipha_dst and 18544 * verify the options 18545 */ 18546 if (ip_wput_options(q, first_mp, ipha, mctl_present, zoneid)) { 18547 ASSERT(xmit_ill == NULL); 18548 if (attach_ill != NULL) 18549 ill_refrele(attach_ill); 18550 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18551 "ip_wput_end: q %p (%S)", q, "badopts"); 18552 if (need_decref) 18553 CONN_DEC_REF(connp); 18554 return; 18555 } 18556 } 18557 dst = ipha->ipha_dst; 18558 18559 /* 18560 * Try to get an IRE_CACHE for the destination address. If we can't, 18561 * we have to run the packet through ip_newroute which will take 18562 * the appropriate action to arrange for an IRE_CACHE, such as querying 18563 * a resolver, or assigning a default gateway, etc. 18564 */ 18565 if (CLASSD(dst)) { 18566 ipif_t *ipif; 18567 uint32_t setsrc = 0; 18568 18569 multicast: 18570 ASSERT(first_mp != NULL); 18571 ASSERT(xmit_ill == NULL); 18572 ip2dbg(("ip_wput: CLASSD\n")); 18573 if (connp == NULL) { 18574 /* 18575 * Use the first good ipif on the ill. 18576 * XXX Should this ever happen? (Appears 18577 * to show up with just ppp and no ethernet due 18578 * to in.rdisc.) 18579 * However, ire_send should be able to 18580 * call ip_wput_ire directly. 18581 * 18582 * XXX Also, this can happen for ICMP and other packets 18583 * with multicast source addresses. Perhaps we should 18584 * fix things so that we drop the packet in question, 18585 * but for now, just run with it. 18586 */ 18587 ill_t *ill = (ill_t *)q->q_ptr; 18588 18589 /* 18590 * Don't honor attach_if for this case. If ill 18591 * is part of the group, ipif could belong to 18592 * any ill and we cannot maintain attach_ill 18593 * and ipif_ill same anymore and the assert 18594 * below would fail. 18595 */ 18596 if (mctl_present) { 18597 io->ipsec_out_ill_index = 0; 18598 io->ipsec_out_attach_if = B_FALSE; 18599 ASSERT(attach_ill != NULL); 18600 ill_refrele(attach_ill); 18601 attach_ill = NULL; 18602 } 18603 18604 ASSERT(attach_ill == NULL); 18605 ipif = ipif_select_source(ill, dst, GLOBAL_ZONEID); 18606 if (ipif == NULL) { 18607 if (need_decref) 18608 CONN_DEC_REF(connp); 18609 freemsg(first_mp); 18610 return; 18611 } 18612 ip1dbg(("ip_wput: CLASSD no CONN: dst 0x%x on %s\n", 18613 ntohl(dst), ill->ill_name)); 18614 } else { 18615 /* 18616 * If both IP_MULTICAST_IF and IP_XMIT_IF are set, 18617 * IP_XMIT_IF is honoured. 18618 * Block comment above this function explains the 18619 * locking mechanism used here 18620 */ 18621 xmit_ill = conn_get_held_ill(connp, 18622 &connp->conn_xmit_if_ill, &err); 18623 if (err == ILL_LOOKUP_FAILED) { 18624 ip1dbg(("ip_wput: No ill for IP_XMIT_IF\n")); 18625 goto drop_pkt; 18626 } 18627 if (xmit_ill == NULL) { 18628 ipif = conn_get_held_ipif(connp, 18629 &connp->conn_multicast_ipif, &err); 18630 if (err == IPIF_LOOKUP_FAILED) { 18631 ip1dbg(("ip_wput: No ipif for " 18632 "multicast\n")); 18633 BUMP_MIB(&ip_mib, ipOutNoRoutes); 18634 goto drop_pkt; 18635 } 18636 } 18637 if (xmit_ill != NULL) { 18638 ipif = ipif_get_next_ipif(NULL, xmit_ill); 18639 if (ipif == NULL) { 18640 ip1dbg(("ip_wput: No ipif for " 18641 "IP_XMIT_IF\n")); 18642 BUMP_MIB(&ip_mib, ipOutNoRoutes); 18643 goto drop_pkt; 18644 } 18645 } else if (ipif == NULL || ipif->ipif_isv6) { 18646 /* 18647 * We must do this ipif determination here 18648 * else we could pass through ip_newroute 18649 * and come back here without the conn context. 18650 * 18651 * Note: we do late binding i.e. we bind to 18652 * the interface when the first packet is sent. 18653 * For performance reasons we do not rebind on 18654 * each packet but keep the binding until the 18655 * next IP_MULTICAST_IF option. 18656 * 18657 * conn_multicast_{ipif,ill} are shared between 18658 * IPv4 and IPv6 and AF_INET6 sockets can 18659 * send both IPv4 and IPv6 packets. Hence 18660 * we have to check that "isv6" matches above. 18661 */ 18662 if (ipif != NULL) 18663 ipif_refrele(ipif); 18664 ipif = ipif_lookup_group(dst, zoneid); 18665 if (ipif == NULL) { 18666 ip1dbg(("ip_wput: No ipif for " 18667 "multicast\n")); 18668 BUMP_MIB(&ip_mib, ipOutNoRoutes); 18669 goto drop_pkt; 18670 } 18671 err = conn_set_held_ipif(connp, 18672 &connp->conn_multicast_ipif, ipif); 18673 if (err == IPIF_LOOKUP_FAILED) { 18674 ipif_refrele(ipif); 18675 ip1dbg(("ip_wput: No ipif for " 18676 "multicast\n")); 18677 BUMP_MIB(&ip_mib, ipOutNoRoutes); 18678 goto drop_pkt; 18679 } 18680 } 18681 } 18682 ASSERT(!ipif->ipif_isv6); 18683 /* 18684 * As we may lose the conn by the time we reach ip_wput_ire, 18685 * we copy conn_multicast_loop and conn_dontroute on to an 18686 * ipsec_out. In case if this datagram goes out secure, 18687 * we need the ill_index also. Copy that also into the 18688 * ipsec_out. 18689 */ 18690 if (mctl_present) { 18691 io = (ipsec_out_t *)first_mp->b_rptr; 18692 ASSERT(first_mp->b_datap->db_type == M_CTL); 18693 ASSERT(io->ipsec_out_type == IPSEC_OUT); 18694 } else { 18695 ASSERT(mp == first_mp); 18696 if ((first_mp = allocb(sizeof (ipsec_info_t), 18697 BPRI_HI)) == NULL) { 18698 ipif_refrele(ipif); 18699 first_mp = mp; 18700 goto drop_pkt; 18701 } 18702 first_mp->b_datap->db_type = M_CTL; 18703 first_mp->b_wptr += sizeof (ipsec_info_t); 18704 /* ipsec_out_secure is B_FALSE now */ 18705 bzero(first_mp->b_rptr, sizeof (ipsec_info_t)); 18706 io = (ipsec_out_t *)first_mp->b_rptr; 18707 io->ipsec_out_type = IPSEC_OUT; 18708 io->ipsec_out_len = sizeof (ipsec_out_t); 18709 io->ipsec_out_use_global_policy = B_TRUE; 18710 first_mp->b_cont = mp; 18711 mctl_present = B_TRUE; 18712 } 18713 if (attach_ill != NULL) { 18714 ASSERT(attach_ill == ipif->ipif_ill); 18715 match_flags = MATCH_IRE_ILL; 18716 18717 /* 18718 * Check if we need an ire that will not be 18719 * looked up by anybody else i.e. HIDDEN. 18720 */ 18721 if (ill_is_probeonly(attach_ill)) { 18722 match_flags |= MATCH_IRE_MARK_HIDDEN; 18723 } 18724 io->ipsec_out_ill_index = 18725 attach_ill->ill_phyint->phyint_ifindex; 18726 io->ipsec_out_attach_if = B_TRUE; 18727 } else { 18728 match_flags = MATCH_IRE_ILL_GROUP; 18729 io->ipsec_out_ill_index = 18730 ipif->ipif_ill->ill_phyint->phyint_ifindex; 18731 } 18732 if (connp != NULL) { 18733 io->ipsec_out_multicast_loop = 18734 connp->conn_multicast_loop; 18735 io->ipsec_out_dontroute = connp->conn_dontroute; 18736 io->ipsec_out_zoneid = connp->conn_zoneid; 18737 } 18738 /* 18739 * If the application uses IP_MULTICAST_IF with 18740 * different logical addresses of the same ILL, we 18741 * need to make sure that the soruce address of 18742 * the packet matches the logical IP address used 18743 * in the option. We do it by initializing ipha_src 18744 * here. This should keep IPSEC also happy as 18745 * when we return from IPSEC processing, we don't 18746 * have to worry about getting the right address on 18747 * the packet. Thus it is sufficient to look for 18748 * IRE_CACHE using MATCH_IRE_ILL rathen than 18749 * MATCH_IRE_IPIF. 18750 * 18751 * NOTE : We need to do it for non-secure case also as 18752 * this might go out secure if there is a global policy 18753 * match in ip_wput_ire. For bind to IPIF_NOFAILOVER 18754 * address, the source should be initialized already and 18755 * hence we won't be initializing here. 18756 * 18757 * As we do not have the ire yet, it is possible that 18758 * we set the source address here and then later discover 18759 * that the ire implies the source address to be assigned 18760 * through the RTF_SETSRC flag. 18761 * In that case, the setsrc variable will remind us 18762 * that overwritting the source address by the one 18763 * of the RTF_SETSRC-flagged ire is allowed. 18764 */ 18765 if (ipha->ipha_src == INADDR_ANY && 18766 (connp == NULL || !connp->conn_unspec_src)) { 18767 ipha->ipha_src = ipif->ipif_src_addr; 18768 setsrc = RTF_SETSRC; 18769 } 18770 /* 18771 * Find an IRE which matches the destination and the outgoing 18772 * queue (i.e. the outgoing interface.) 18773 * For loopback use a unicast IP address for 18774 * the ire lookup. 18775 */ 18776 if (ipif->ipif_ill->ill_phyint->phyint_flags & 18777 PHYI_LOOPBACK) { 18778 dst = ipif->ipif_lcl_addr; 18779 } 18780 /* 18781 * If IP_XMIT_IF is set, we branch out to ip_newroute_ipif. 18782 * We don't need to lookup ire in ctable as the packet 18783 * needs to be sent to the destination through the specified 18784 * ill irrespective of ires in the cache table. 18785 */ 18786 ire = NULL; 18787 if (xmit_ill == NULL) { 18788 ire = ire_ctable_lookup(dst, 0, 0, ipif, 18789 zoneid, match_flags); 18790 } 18791 18792 /* 18793 * refrele attach_ill as its not needed anymore. 18794 */ 18795 if (attach_ill != NULL) { 18796 ill_refrele(attach_ill); 18797 attach_ill = NULL; 18798 } 18799 18800 if (ire == NULL) { 18801 /* 18802 * Multicast loopback and multicast forwarding is 18803 * done in ip_wput_ire. 18804 * 18805 * Mark this packet to make it be delivered to 18806 * ip_wput_ire after the new ire has been 18807 * created. 18808 * 18809 * The call to ip_newroute_ipif takes into account 18810 * the setsrc reminder. In any case, we take care 18811 * of the RTF_MULTIRT flag. 18812 */ 18813 mp->b_prev = mp->b_next = NULL; 18814 if (xmit_ill == NULL || 18815 xmit_ill->ill_ipif_up_count > 0) { 18816 ip_newroute_ipif(q, first_mp, ipif, dst, connp, 18817 setsrc | RTF_MULTIRT); 18818 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 18819 "ip_wput_end: q %p (%S)", q, "noire"); 18820 } else { 18821 freemsg(first_mp); 18822 } 18823 ipif_refrele(ipif); 18824 if (xmit_ill != NULL) 18825 ill_refrele(xmit_ill); 18826 if (need_decref) 18827 CONN_DEC_REF(connp); 18828 return; 18829 } 18830 18831 ipif_refrele(ipif); 18832 ipif = NULL; 18833 ASSERT(xmit_ill == NULL); 18834 18835 /* 18836 * Honor the RTF_SETSRC flag for multicast packets, 18837 * if allowed by the setsrc reminder. 18838 */ 18839 if ((ire->ire_flags & RTF_SETSRC) && setsrc) { 18840 ipha->ipha_src = ire->ire_src_addr; 18841 } 18842 18843 /* 18844 * Unconditionally force the TTL to 1 for 18845 * multirouted multicast packets: 18846 * multirouted multicast should not cross 18847 * multicast routers. 18848 */ 18849 if (ire->ire_flags & RTF_MULTIRT) { 18850 if (ipha->ipha_ttl > 1) { 18851 ip2dbg(("ip_wput: forcing multicast " 18852 "multirt TTL to 1 (was %d), dst 0x%08x\n", 18853 ipha->ipha_ttl, ntohl(ire->ire_addr))); 18854 ipha->ipha_ttl = 1; 18855 } 18856 } 18857 } else { 18858 ire = ire_cache_lookup(dst, zoneid); 18859 if ((ire != NULL) && (ire->ire_type & 18860 (IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK))) { 18861 ignore_dontroute = B_TRUE; 18862 ignore_nexthop = B_TRUE; 18863 } 18864 if (ire != NULL) { 18865 ire_refrele(ire); 18866 ire = NULL; 18867 } 18868 /* 18869 * Guard against coming in from arp in which case conn is NULL. 18870 * Also guard against non M_DATA with dontroute set but 18871 * destined to local, loopback or broadcast addresses. 18872 */ 18873 if (connp != NULL && connp->conn_dontroute && 18874 !ignore_dontroute) { 18875 dontroute: 18876 /* 18877 * Set TTL to 1 if SO_DONTROUTE is set to prevent 18878 * routing protocols from seeing false direct 18879 * connectivity. 18880 */ 18881 ipha->ipha_ttl = 1; 18882 /* 18883 * If IP_XMIT_IF is also set (conn_xmit_if_ill != NULL) 18884 * along with SO_DONTROUTE, higher precedence is 18885 * given to IP_XMIT_IF and the IP_XMIT_IF ipif is used. 18886 */ 18887 if (connp->conn_xmit_if_ill == NULL) { 18888 /* If suitable ipif not found, drop packet */ 18889 dst_ipif = ipif_lookup_onlink_addr(dst, zoneid); 18890 if (dst_ipif == NULL) { 18891 ip1dbg(("ip_wput: no route for " 18892 "dst using SO_DONTROUTE\n")); 18893 BUMP_MIB(&ip_mib, ipOutNoRoutes); 18894 mp->b_prev = mp->b_next = NULL; 18895 if (first_mp == NULL) 18896 first_mp = mp; 18897 goto drop_pkt; 18898 } else { 18899 /* 18900 * If suitable ipif has been found, set 18901 * xmit_ill to the corresponding 18902 * ipif_ill because we'll be following 18903 * the IP_XMIT_IF logic. 18904 */ 18905 ASSERT(xmit_ill == NULL); 18906 xmit_ill = dst_ipif->ipif_ill; 18907 mutex_enter(&xmit_ill->ill_lock); 18908 if (!ILL_CAN_LOOKUP(xmit_ill)) { 18909 mutex_exit(&xmit_ill->ill_lock); 18910 xmit_ill = NULL; 18911 ipif_refrele(dst_ipif); 18912 ip1dbg(("ip_wput: no route for" 18913 " dst using" 18914 " SO_DONTROUTE\n")); 18915 BUMP_MIB(&ip_mib, 18916 ipOutNoRoutes); 18917 mp->b_prev = mp->b_next = NULL; 18918 if (first_mp == NULL) 18919 first_mp = mp; 18920 goto drop_pkt; 18921 } 18922 ill_refhold_locked(xmit_ill); 18923 mutex_exit(&xmit_ill->ill_lock); 18924 ipif_refrele(dst_ipif); 18925 } 18926 } 18927 18928 } 18929 /* 18930 * If we are bound to IPIF_NOFAILOVER address, look for 18931 * an IRE_CACHE matching the ill. 18932 */ 18933 send_from_ill: 18934 if (attach_ill != NULL) { 18935 ipif_t *attach_ipif; 18936 18937 match_flags = MATCH_IRE_ILL; 18938 18939 /* 18940 * Check if we need an ire that will not be 18941 * looked up by anybody else i.e. HIDDEN. 18942 */ 18943 if (ill_is_probeonly(attach_ill)) { 18944 match_flags |= MATCH_IRE_MARK_HIDDEN; 18945 } 18946 18947 attach_ipif = ipif_get_next_ipif(NULL, attach_ill); 18948 if (attach_ipif == NULL) { 18949 ip1dbg(("ip_wput: No ipif for attach_ill\n")); 18950 goto drop_pkt; 18951 } 18952 ire = ire_ctable_lookup(dst, 0, 0, attach_ipif, 18953 zoneid, match_flags); 18954 ipif_refrele(attach_ipif); 18955 } else if (xmit_ill != NULL || (connp != NULL && 18956 connp->conn_xmit_if_ill != NULL)) { 18957 /* 18958 * Mark this packet as originated locally 18959 */ 18960 mp->b_prev = mp->b_next = NULL; 18961 /* 18962 * xmit_ill could be NULL if SO_DONTROUTE 18963 * is also set. 18964 */ 18965 if (xmit_ill == NULL) { 18966 xmit_ill = conn_get_held_ill(connp, 18967 &connp->conn_xmit_if_ill, &err); 18968 if (err == ILL_LOOKUP_FAILED) { 18969 if (need_decref) 18970 CONN_DEC_REF(connp); 18971 freemsg(first_mp); 18972 return; 18973 } 18974 if (xmit_ill == NULL) { 18975 if (connp->conn_dontroute) 18976 goto dontroute; 18977 goto send_from_ill; 18978 } 18979 } 18980 /* 18981 * could be SO_DONTROUTE case also. 18982 * check at least one interface is UP as 18983 * spcified by this ILL, and then call 18984 * ip_newroute_ipif() 18985 */ 18986 if (xmit_ill->ill_ipif_up_count > 0) { 18987 ipif_t *ipif; 18988 18989 ipif = ipif_get_next_ipif(NULL, xmit_ill); 18990 if (ipif != NULL) { 18991 ip_newroute_ipif(q, first_mp, ipif, 18992 dst, connp, 0); 18993 ipif_refrele(ipif); 18994 ip1dbg(("ip_wput: ip_unicast_if\n")); 18995 } 18996 } else { 18997 freemsg(first_mp); 18998 } 18999 ill_refrele(xmit_ill); 19000 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 19001 "ip_wput_end: q %p (%S)", q, "unicast_if"); 19002 if (need_decref) 19003 CONN_DEC_REF(connp); 19004 return; 19005 } else if (ip_nexthop || (connp != NULL && 19006 (connp->conn_nexthop_set)) && !ignore_nexthop) { 19007 if (!ip_nexthop) { 19008 ip_nexthop = B_TRUE; 19009 nexthop_addr = connp->conn_nexthop_v4; 19010 } 19011 match_flags = MATCH_IRE_MARK_PRIVATE_ADDR | 19012 MATCH_IRE_GW; 19013 ire = ire_ctable_lookup(dst, nexthop_addr, 0, 19014 NULL, zoneid, match_flags); 19015 } else { 19016 ire = ire_cache_lookup(dst, zoneid); 19017 } 19018 if (!ire) { 19019 /* 19020 * Make sure we don't load spread if this 19021 * is IPIF_NOFAILOVER case. 19022 */ 19023 if ((attach_ill != NULL) || 19024 (ip_nexthop && !ignore_nexthop)) { 19025 if (mctl_present) { 19026 io = (ipsec_out_t *)first_mp->b_rptr; 19027 ASSERT(first_mp->b_datap->db_type == 19028 M_CTL); 19029 ASSERT(io->ipsec_out_type == IPSEC_OUT); 19030 } else { 19031 ASSERT(mp == first_mp); 19032 first_mp = allocb( 19033 sizeof (ipsec_info_t), BPRI_HI); 19034 if (first_mp == NULL) { 19035 first_mp = mp; 19036 goto drop_pkt; 19037 } 19038 first_mp->b_datap->db_type = M_CTL; 19039 first_mp->b_wptr += 19040 sizeof (ipsec_info_t); 19041 /* ipsec_out_secure is B_FALSE now */ 19042 bzero(first_mp->b_rptr, 19043 sizeof (ipsec_info_t)); 19044 io = (ipsec_out_t *)first_mp->b_rptr; 19045 io->ipsec_out_type = IPSEC_OUT; 19046 io->ipsec_out_len = 19047 sizeof (ipsec_out_t); 19048 io->ipsec_out_use_global_policy = 19049 B_TRUE; 19050 first_mp->b_cont = mp; 19051 mctl_present = B_TRUE; 19052 } 19053 if (attach_ill != NULL) { 19054 io->ipsec_out_ill_index = attach_ill-> 19055 ill_phyint->phyint_ifindex; 19056 io->ipsec_out_attach_if = B_TRUE; 19057 } else { 19058 io->ipsec_out_ip_nexthop = ip_nexthop; 19059 io->ipsec_out_nexthop_addr = 19060 nexthop_addr; 19061 } 19062 } 19063 noirefound: 19064 /* 19065 * Mark this packet as having originated on 19066 * this machine. This will be noted in 19067 * ire_add_then_send, which needs to know 19068 * whether to run it back through ip_wput or 19069 * ip_rput following successful resolution. 19070 */ 19071 mp->b_prev = NULL; 19072 mp->b_next = NULL; 19073 ip_newroute(q, first_mp, dst, NULL, connp); 19074 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 19075 "ip_wput_end: q %p (%S)", q, "newroute"); 19076 if (attach_ill != NULL) 19077 ill_refrele(attach_ill); 19078 if (xmit_ill != NULL) 19079 ill_refrele(xmit_ill); 19080 if (need_decref) 19081 CONN_DEC_REF(connp); 19082 return; 19083 } 19084 } 19085 19086 /* We now know where we are going with it. */ 19087 19088 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 19089 "ip_wput_end: q %p (%S)", q, "end"); 19090 19091 /* 19092 * Check if the ire has the RTF_MULTIRT flag, inherited 19093 * from an IRE_OFFSUBNET ire entry in ip_newroute. 19094 */ 19095 if (ire->ire_flags & RTF_MULTIRT) { 19096 /* 19097 * Force the TTL of multirouted packets if required. 19098 * The TTL of such packets is bounded by the 19099 * ip_multirt_ttl ndd variable. 19100 */ 19101 if ((ip_multirt_ttl > 0) && 19102 (ipha->ipha_ttl > ip_multirt_ttl)) { 19103 ip2dbg(("ip_wput: forcing multirt TTL to %d " 19104 "(was %d), dst 0x%08x\n", 19105 ip_multirt_ttl, ipha->ipha_ttl, 19106 ntohl(ire->ire_addr))); 19107 ipha->ipha_ttl = ip_multirt_ttl; 19108 } 19109 /* 19110 * At this point, we check to see if there are any pending 19111 * unresolved routes. ire_multirt_resolvable() 19112 * checks in O(n) that all IRE_OFFSUBNET ire 19113 * entries for the packet's destination and 19114 * flagged RTF_MULTIRT are currently resolved. 19115 * If some remain unresolved, we make a copy 19116 * of the current message. It will be used 19117 * to initiate additional route resolutions. 19118 */ 19119 multirt_need_resolve = ire_multirt_need_resolve(ire->ire_addr); 19120 ip2dbg(("ip_wput[noirefound]: ire %p, " 19121 "multirt_need_resolve %d, first_mp %p\n", 19122 (void *)ire, multirt_need_resolve, (void *)first_mp)); 19123 if (multirt_need_resolve) { 19124 copy_mp = copymsg(first_mp); 19125 if (copy_mp != NULL) { 19126 MULTIRT_DEBUG_TAG(copy_mp); 19127 } 19128 } 19129 } 19130 19131 ip_wput_ire(q, first_mp, ire, connp, caller); 19132 /* 19133 * Try to resolve another multiroute if 19134 * ire_multirt_resolvable() deemed it necessary. 19135 * At this point, we need to distinguish 19136 * multicasts from other packets. For multicasts, 19137 * we call ip_newroute_ipif() and request that both 19138 * multirouting and setsrc flags are checked. 19139 */ 19140 if (copy_mp != NULL) { 19141 if (CLASSD(dst)) { 19142 ipif_t *ipif = ipif_lookup_group(dst, zoneid); 19143 if (ipif) { 19144 ip_newroute_ipif(q, copy_mp, ipif, dst, connp, 19145 RTF_SETSRC | RTF_MULTIRT); 19146 ipif_refrele(ipif); 19147 } else { 19148 MULTIRT_DEBUG_UNTAG(copy_mp); 19149 freemsg(copy_mp); 19150 copy_mp = NULL; 19151 } 19152 } else { 19153 ip_newroute(q, copy_mp, dst, NULL, connp); 19154 } 19155 } 19156 if (attach_ill != NULL) 19157 ill_refrele(attach_ill); 19158 if (xmit_ill != NULL) 19159 ill_refrele(xmit_ill); 19160 if (need_decref) 19161 CONN_DEC_REF(connp); 19162 return; 19163 19164 drop_pkt: 19165 ip1dbg(("ip_wput: dropped packet\n")); 19166 if (ire != NULL) 19167 ire_refrele(ire); 19168 if (need_decref) 19169 CONN_DEC_REF(connp); 19170 freemsg(first_mp); 19171 if (attach_ill != NULL) 19172 ill_refrele(attach_ill); 19173 if (xmit_ill != NULL) 19174 ill_refrele(xmit_ill); 19175 TRACE_2(TR_FAC_IP, TR_IP_WPUT_END, 19176 "ip_wput_end: q %p (%S)", q, "droppkt"); 19177 } 19178 19179 void 19180 ip_wput(queue_t *q, mblk_t *mp) 19181 { 19182 ip_output(Q_TO_CONN(q), mp, q, IP_WPUT); 19183 } 19184 19185 /* 19186 * 19187 * The following rules must be observed when accessing any ipif or ill 19188 * that has been cached in the conn. Typically conn_nofailover_ill, 19189 * conn_xmit_if_ill, conn_multicast_ipif and conn_multicast_ill. 19190 * 19191 * Access: The ipif or ill pointed to from the conn can be accessed under 19192 * the protection of the conn_lock or after it has been refheld under the 19193 * protection of the conn lock. In addition the IPIF_CAN_LOOKUP or 19194 * ILL_CAN_LOOKUP macros must be used before actually doing the refhold. 19195 * The reason for this is that a concurrent unplumb could actually be 19196 * cleaning up these cached pointers by walking the conns and might have 19197 * finished cleaning up the conn in question. The macros check that an 19198 * unplumb has not yet started on the ipif or ill. 19199 * 19200 * Caching: An ipif or ill pointer may be cached in the conn only after 19201 * making sure that an unplumb has not started. So the caching is done 19202 * while holding both the conn_lock and the ill_lock and after using the 19203 * ILL_CAN_LOOKUP/IPIF_CAN_LOOKUP macro. An unplumb will set the ILL_CONDEMNED 19204 * flag before starting the cleanup of conns. 19205 * 19206 * The list of ipifs hanging off the ill is protected by ill_g_lock and ill_lock 19207 * On the other hand to access ipif->ipif_ill, we need one of either ill_g_lock 19208 * or a reference to the ipif or a reference to an ire that references the 19209 * ipif. An ipif does not change its ill except for failover/failback. Since 19210 * failover/failback happens only after bringing down the ipif and making sure 19211 * the ipif refcnt has gone to zero and holding the ill_g_lock and ill_lock 19212 * the above holds. 19213 */ 19214 ipif_t * 19215 conn_get_held_ipif(conn_t *connp, ipif_t **ipifp, int *err) 19216 { 19217 ipif_t *ipif; 19218 ill_t *ill; 19219 19220 *err = 0; 19221 rw_enter(&ill_g_lock, RW_READER); 19222 mutex_enter(&connp->conn_lock); 19223 ipif = *ipifp; 19224 if (ipif != NULL) { 19225 ill = ipif->ipif_ill; 19226 mutex_enter(&ill->ill_lock); 19227 if (IPIF_CAN_LOOKUP(ipif)) { 19228 ipif_refhold_locked(ipif); 19229 mutex_exit(&ill->ill_lock); 19230 mutex_exit(&connp->conn_lock); 19231 rw_exit(&ill_g_lock); 19232 return (ipif); 19233 } else { 19234 *err = IPIF_LOOKUP_FAILED; 19235 } 19236 mutex_exit(&ill->ill_lock); 19237 } 19238 mutex_exit(&connp->conn_lock); 19239 rw_exit(&ill_g_lock); 19240 return (NULL); 19241 } 19242 19243 ill_t * 19244 conn_get_held_ill(conn_t *connp, ill_t **illp, int *err) 19245 { 19246 ill_t *ill; 19247 19248 *err = 0; 19249 mutex_enter(&connp->conn_lock); 19250 ill = *illp; 19251 if (ill != NULL) { 19252 mutex_enter(&ill->ill_lock); 19253 if (ILL_CAN_LOOKUP(ill)) { 19254 ill_refhold_locked(ill); 19255 mutex_exit(&ill->ill_lock); 19256 mutex_exit(&connp->conn_lock); 19257 return (ill); 19258 } else { 19259 *err = ILL_LOOKUP_FAILED; 19260 } 19261 mutex_exit(&ill->ill_lock); 19262 } 19263 mutex_exit(&connp->conn_lock); 19264 return (NULL); 19265 } 19266 19267 static int 19268 conn_set_held_ipif(conn_t *connp, ipif_t **ipifp, ipif_t *ipif) 19269 { 19270 ill_t *ill; 19271 19272 ill = ipif->ipif_ill; 19273 mutex_enter(&connp->conn_lock); 19274 mutex_enter(&ill->ill_lock); 19275 if (IPIF_CAN_LOOKUP(ipif)) { 19276 *ipifp = ipif; 19277 mutex_exit(&ill->ill_lock); 19278 mutex_exit(&connp->conn_lock); 19279 return (0); 19280 } 19281 mutex_exit(&ill->ill_lock); 19282 mutex_exit(&connp->conn_lock); 19283 return (IPIF_LOOKUP_FAILED); 19284 } 19285 19286 /* 19287 * This is called if the outbound datagram needs fragmentation. 19288 * 19289 * NOTE : This function does not ire_refrele the ire argument passed in. 19290 */ 19291 static void 19292 ip_wput_ire_fragmentit(mblk_t *ipsec_mp, ire_t *ire) 19293 { 19294 ipha_t *ipha; 19295 mblk_t *mp; 19296 uint32_t v_hlen_tos_len; 19297 uint32_t max_frag; 19298 uint32_t frag_flag; 19299 boolean_t dont_use; 19300 19301 if (ipsec_mp->b_datap->db_type == M_CTL) { 19302 mp = ipsec_mp->b_cont; 19303 } else { 19304 mp = ipsec_mp; 19305 } 19306 19307 ipha = (ipha_t *)mp->b_rptr; 19308 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 19309 19310 #ifdef _BIG_ENDIAN 19311 #define V_HLEN (v_hlen_tos_len >> 24) 19312 #define LENGTH (v_hlen_tos_len & 0xFFFF) 19313 #else 19314 #define V_HLEN (v_hlen_tos_len & 0xFF) 19315 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 19316 #endif 19317 19318 #ifndef SPEED_BEFORE_SAFETY 19319 /* 19320 * Check that ipha_length is consistent with 19321 * the mblk length 19322 */ 19323 if (LENGTH != (mp->b_cont ? msgdsize(mp) : mp->b_wptr - rptr)) { 19324 ip0dbg(("Packet length mismatch: %d, %ld\n", 19325 LENGTH, msgdsize(mp))); 19326 freemsg(ipsec_mp); 19327 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 19328 "ip_wput_ire_fragmentit: mp %p (%S)", mp, 19329 "packet length mismatch"); 19330 return; 19331 } 19332 #endif 19333 /* 19334 * Don't use frag_flag if pre-built packet or source 19335 * routed or if multicast (since multicast packets do not solicit 19336 * ICMP "packet too big" messages). Get the values of 19337 * max_frag and frag_flag atomically by acquiring the 19338 * ire_lock. 19339 */ 19340 mutex_enter(&ire->ire_lock); 19341 max_frag = ire->ire_max_frag; 19342 frag_flag = ire->ire_frag_flag; 19343 mutex_exit(&ire->ire_lock); 19344 19345 dont_use = ((ipha->ipha_ident == IP_HDR_INCLUDED) || 19346 (V_HLEN != IP_SIMPLE_HDR_VERSION && 19347 ip_source_route_included(ipha)) || CLASSD(ipha->ipha_dst)); 19348 19349 ip_wput_frag(ire, ipsec_mp, OB_PKT, max_frag, 19350 (dont_use ? 0 : frag_flag)); 19351 } 19352 19353 /* 19354 * Used for deciding the MSS size for the upper layer. Thus 19355 * we need to check the outbound policy values in the conn. 19356 */ 19357 int 19358 conn_ipsec_length(conn_t *connp) 19359 { 19360 ipsec_latch_t *ipl; 19361 19362 ipl = connp->conn_latch; 19363 if (ipl == NULL) 19364 return (0); 19365 19366 if (ipl->ipl_out_policy == NULL) 19367 return (0); 19368 19369 return (ipl->ipl_out_policy->ipsp_act->ipa_ovhd); 19370 } 19371 19372 /* 19373 * Returns an estimate of the IPSEC headers size. This is used if 19374 * we don't want to call into IPSEC to get the exact size. 19375 */ 19376 int 19377 ipsec_out_extra_length(mblk_t *ipsec_mp) 19378 { 19379 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 19380 ipsec_action_t *a; 19381 19382 ASSERT(io->ipsec_out_type == IPSEC_OUT); 19383 if (!io->ipsec_out_secure) 19384 return (0); 19385 19386 a = io->ipsec_out_act; 19387 19388 if (a == NULL) { 19389 ASSERT(io->ipsec_out_policy != NULL); 19390 a = io->ipsec_out_policy->ipsp_act; 19391 } 19392 ASSERT(a != NULL); 19393 19394 return (a->ipa_ovhd); 19395 } 19396 19397 /* 19398 * Returns an estimate of the IPSEC headers size. This is used if 19399 * we don't want to call into IPSEC to get the exact size. 19400 */ 19401 int 19402 ipsec_in_extra_length(mblk_t *ipsec_mp) 19403 { 19404 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 19405 ipsec_action_t *a; 19406 19407 ASSERT(ii->ipsec_in_type == IPSEC_IN); 19408 19409 a = ii->ipsec_in_action; 19410 return (a == NULL ? 0 : a->ipa_ovhd); 19411 } 19412 19413 /* 19414 * If there are any source route options, return the true final 19415 * destination. Otherwise, return the destination. 19416 */ 19417 ipaddr_t 19418 ip_get_dst(ipha_t *ipha) 19419 { 19420 ipoptp_t opts; 19421 uchar_t *opt; 19422 uint8_t optval; 19423 uint8_t optlen; 19424 ipaddr_t dst; 19425 uint32_t off; 19426 19427 dst = ipha->ipha_dst; 19428 19429 if (IS_SIMPLE_IPH(ipha)) 19430 return (dst); 19431 19432 for (optval = ipoptp_first(&opts, ipha); 19433 optval != IPOPT_EOL; 19434 optval = ipoptp_next(&opts)) { 19435 opt = opts.ipoptp_cur; 19436 optlen = opts.ipoptp_len; 19437 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 19438 switch (optval) { 19439 case IPOPT_SSRR: 19440 case IPOPT_LSRR: 19441 off = opt[IPOPT_OFFSET]; 19442 /* 19443 * If one of the conditions is true, it means 19444 * end of options and dst already has the right 19445 * value. 19446 */ 19447 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 19448 off = optlen - IP_ADDR_LEN; 19449 bcopy(&opt[off], &dst, IP_ADDR_LEN); 19450 } 19451 return (dst); 19452 default: 19453 break; 19454 } 19455 } 19456 19457 return (dst); 19458 } 19459 19460 mblk_t * 19461 ip_wput_ire_parse_ipsec_out(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, ire_t *ire, 19462 conn_t *connp, boolean_t unspec_src) 19463 { 19464 ipsec_out_t *io; 19465 mblk_t *first_mp; 19466 boolean_t policy_present; 19467 19468 first_mp = mp; 19469 if (mp->b_datap->db_type == M_CTL) { 19470 io = (ipsec_out_t *)first_mp->b_rptr; 19471 /* 19472 * ip_wput[_v6] attaches an IPSEC_OUT in two cases. 19473 * 19474 * 1) There is per-socket policy (including cached global 19475 * policy). 19476 * 2) There is no per-socket policy, but it is 19477 * a multicast packet that needs to go out 19478 * on a specific interface. This is the case 19479 * where (ip_wput and ip_wput_multicast) attaches 19480 * an IPSEC_OUT and sets ipsec_out_secure B_FALSE. 19481 * 19482 * In case (2) we check with global policy to 19483 * see if there is a match and set the ill_index 19484 * appropriately so that we can lookup the ire 19485 * properly in ip_wput_ipsec_out. 19486 */ 19487 19488 /* 19489 * ipsec_out_use_global_policy is set to B_FALSE 19490 * in ipsec_in_to_out(). Refer to that function for 19491 * details. 19492 */ 19493 if ((io->ipsec_out_latch == NULL) && 19494 (io->ipsec_out_use_global_policy)) { 19495 return (ip_wput_attach_policy(first_mp, ipha, ip6h, 19496 ire, connp, unspec_src)); 19497 } 19498 if (!io->ipsec_out_secure) { 19499 /* 19500 * If this is not a secure packet, drop 19501 * the IPSEC_OUT mp and treat it as a clear 19502 * packet. This happens when we are sending 19503 * a ICMP reply back to a clear packet. See 19504 * ipsec_in_to_out() for details. 19505 */ 19506 mp = first_mp->b_cont; 19507 freeb(first_mp); 19508 } 19509 return (mp); 19510 } 19511 /* 19512 * See whether we need to attach a global policy here. We 19513 * don't depend on the conn (as it could be null) for deciding 19514 * what policy this datagram should go through because it 19515 * should have happened in ip_wput if there was some 19516 * policy. This normally happens for connections which are not 19517 * fully bound preventing us from caching policies in 19518 * ip_bind. Packets coming from the TCP listener/global queue 19519 * - which are non-hard_bound - could also be affected by 19520 * applying policy here. 19521 * 19522 * If this packet is coming from tcp global queue or listener, 19523 * we will be applying policy here. This may not be *right* 19524 * if these packets are coming from the detached connection as 19525 * it could have gone in clear before. This happens only if a 19526 * TCP connection started when there is no policy and somebody 19527 * added policy before it became detached. Thus packets of the 19528 * detached connection could go out secure and the other end 19529 * would drop it because it will be expecting in clear. The 19530 * converse is not true i.e if somebody starts a TCP 19531 * connection and deletes the policy, all the packets will 19532 * still go out with the policy that existed before deleting 19533 * because ip_unbind sends up policy information which is used 19534 * by TCP on subsequent ip_wputs. The right solution is to fix 19535 * TCP to attach a dummy IPSEC_OUT and set 19536 * ipsec_out_use_global_policy to B_FALSE. As this might 19537 * affect performance for normal cases, we are not doing it. 19538 * Thus, set policy before starting any TCP connections. 19539 * 19540 * NOTE - We might apply policy even for a hard bound connection 19541 * - for which we cached policy in ip_bind - if somebody added 19542 * global policy after we inherited the policy in ip_bind. 19543 * This means that the packets that were going out in clear 19544 * previously would start going secure and hence get dropped 19545 * on the other side. To fix this, TCP attaches a dummy 19546 * ipsec_out and make sure that we don't apply global policy. 19547 */ 19548 if (ipha != NULL) 19549 policy_present = ipsec_outbound_v4_policy_present; 19550 else 19551 policy_present = ipsec_outbound_v6_policy_present; 19552 if (!policy_present) 19553 return (mp); 19554 19555 return (ip_wput_attach_policy(mp, ipha, ip6h, ire, connp, unspec_src)); 19556 } 19557 19558 ire_t * 19559 conn_set_outgoing_ill(conn_t *connp, ire_t *ire, ill_t **conn_outgoing_ill) 19560 { 19561 ipaddr_t addr; 19562 ire_t *save_ire; 19563 irb_t *irb; 19564 ill_group_t *illgrp; 19565 int err; 19566 19567 save_ire = ire; 19568 addr = ire->ire_addr; 19569 19570 ASSERT(ire->ire_type == IRE_BROADCAST); 19571 19572 illgrp = connp->conn_outgoing_ill->ill_group; 19573 if (illgrp == NULL) { 19574 *conn_outgoing_ill = conn_get_held_ill(connp, 19575 &connp->conn_outgoing_ill, &err); 19576 if (err == ILL_LOOKUP_FAILED) { 19577 ire_refrele(save_ire); 19578 return (NULL); 19579 } 19580 return (save_ire); 19581 } 19582 /* 19583 * If IP_BOUND_IF has been done, conn_outgoing_ill will be set. 19584 * If it is part of the group, we need to send on the ire 19585 * that has been cleared of IRE_MARK_NORECV and that belongs 19586 * to this group. This is okay as IP_BOUND_IF really means 19587 * any ill in the group. We depend on the fact that the 19588 * first ire in the group is always cleared of IRE_MARK_NORECV 19589 * if such an ire exists. This is possible only if you have 19590 * at least one ill in the group that has not failed. 19591 * 19592 * First get to the ire that matches the address and group. 19593 * 19594 * We don't look for an ire with a matching zoneid because a given zone 19595 * won't always have broadcast ires on all ills in the group. 19596 */ 19597 irb = ire->ire_bucket; 19598 rw_enter(&irb->irb_lock, RW_READER); 19599 if (ire->ire_marks & IRE_MARK_NORECV) { 19600 /* 19601 * If the current zone only has an ire broadcast for this 19602 * address marked NORECV, the ire we want is ahead in the 19603 * bucket, so we look it up deliberately ignoring the zoneid. 19604 */ 19605 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 19606 if (ire->ire_addr != addr) 19607 continue; 19608 /* skip over deleted ires */ 19609 if (ire->ire_marks & IRE_MARK_CONDEMNED) 19610 continue; 19611 } 19612 } 19613 while (ire != NULL) { 19614 /* 19615 * If a new interface is coming up, we could end up 19616 * seeing the loopback ire and the non-loopback ire 19617 * may not have been added yet. So check for ire_stq 19618 */ 19619 if (ire->ire_stq != NULL && (ire->ire_addr != addr || 19620 ire->ire_ipif->ipif_ill->ill_group == illgrp)) { 19621 break; 19622 } 19623 ire = ire->ire_next; 19624 } 19625 if (ire != NULL && ire->ire_addr == addr && 19626 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 19627 IRE_REFHOLD(ire); 19628 rw_exit(&irb->irb_lock); 19629 ire_refrele(save_ire); 19630 *conn_outgoing_ill = ire_to_ill(ire); 19631 /* 19632 * Refhold the ill to make the conn_outgoing_ill 19633 * independent of the ire. ip_wput_ire goes in a loop 19634 * and may refrele the ire. Since we have an ire at this 19635 * point we don't need to use ILL_CAN_LOOKUP on the ill. 19636 */ 19637 ill_refhold(*conn_outgoing_ill); 19638 return (ire); 19639 } 19640 rw_exit(&irb->irb_lock); 19641 ip1dbg(("conn_set_outgoing_ill: No matching ire\n")); 19642 /* 19643 * If we can't find a suitable ire, return the original ire. 19644 */ 19645 return (save_ire); 19646 } 19647 19648 /* 19649 * This function does the ire_refrele of the ire passed in as the 19650 * argument. As this function looks up more ires i.e broadcast ires, 19651 * it needs to REFRELE them. Currently, for simplicity we don't 19652 * differentiate the one passed in and looked up here. We always 19653 * REFRELE. 19654 * IPQoS Notes: 19655 * IP policy is invoked if IPP_LOCAL_OUT is enabled. Processing for 19656 * IPSec packets are done in ipsec_out_process. 19657 * 19658 */ 19659 void 19660 ip_wput_ire(queue_t *q, mblk_t *mp, ire_t *ire, conn_t *connp, int caller) 19661 { 19662 ipha_t *ipha; 19663 #define rptr ((uchar_t *)ipha) 19664 mblk_t *mp1; 19665 queue_t *stq; 19666 #define Q_TO_INDEX(stq) (((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex) 19667 uint32_t v_hlen_tos_len; 19668 uint32_t ttl_protocol; 19669 ipaddr_t src; 19670 ipaddr_t dst; 19671 uint32_t cksum; 19672 ipaddr_t orig_src; 19673 ire_t *ire1; 19674 mblk_t *next_mp; 19675 uint_t hlen; 19676 uint16_t *up; 19677 uint32_t max_frag = ire->ire_max_frag; 19678 ill_t *ill = ire_to_ill(ire); 19679 int clusterwide; 19680 uint16_t ip_hdr_included; /* IP header included by ULP? */ 19681 int ipsec_len; 19682 mblk_t *first_mp; 19683 ipsec_out_t *io; 19684 boolean_t conn_dontroute; /* conn value for multicast */ 19685 boolean_t conn_multicast_loop; /* conn value for multicast */ 19686 boolean_t multicast_forward; /* Should we forward ? */ 19687 boolean_t unspec_src; 19688 ill_t *conn_outgoing_ill = NULL; 19689 ill_t *ire_ill; 19690 ill_t *ire1_ill; 19691 uint32_t ill_index = 0; 19692 boolean_t multirt_send = B_FALSE; 19693 int err; 19694 zoneid_t zoneid; 19695 19696 TRACE_1(TR_FAC_IP, TR_IP_WPUT_IRE_START, 19697 "ip_wput_ire_start: q %p", q); 19698 19699 multicast_forward = B_FALSE; 19700 unspec_src = (connp != NULL && connp->conn_unspec_src); 19701 19702 if (ire->ire_flags & RTF_MULTIRT) { 19703 /* 19704 * Multirouting case. The bucket where ire is stored 19705 * probably holds other RTF_MULTIRT flagged ire 19706 * to the destination. In this call to ip_wput_ire, 19707 * we attempt to send the packet through all 19708 * those ires. Thus, we first ensure that ire is the 19709 * first RTF_MULTIRT ire in the bucket, 19710 * before walking the ire list. 19711 */ 19712 ire_t *first_ire; 19713 irb_t *irb = ire->ire_bucket; 19714 ASSERT(irb != NULL); 19715 19716 /* Make sure we do not omit any multiroute ire. */ 19717 IRB_REFHOLD(irb); 19718 for (first_ire = irb->irb_ire; 19719 first_ire != NULL; 19720 first_ire = first_ire->ire_next) { 19721 if ((first_ire->ire_flags & RTF_MULTIRT) && 19722 (first_ire->ire_addr == ire->ire_addr) && 19723 !(first_ire->ire_marks & 19724 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 19725 break; 19726 } 19727 19728 if ((first_ire != NULL) && (first_ire != ire)) { 19729 IRE_REFHOLD(first_ire); 19730 ire_refrele(ire); 19731 ire = first_ire; 19732 ill = ire_to_ill(ire); 19733 } 19734 IRB_REFRELE(irb); 19735 } 19736 19737 /* 19738 * conn_outgoing_ill is used only in the broadcast loop. 19739 * for performance we don't grab the mutexs in the fastpath 19740 */ 19741 if ((connp != NULL) && 19742 (connp->conn_xmit_if_ill == NULL) && 19743 (ire->ire_type == IRE_BROADCAST) && 19744 ((connp->conn_nofailover_ill != NULL) || 19745 (connp->conn_outgoing_ill != NULL))) { 19746 /* 19747 * Bind to IPIF_NOFAILOVER address overrides IP_BOUND_IF 19748 * option. So, see if this endpoint is bound to a 19749 * IPIF_NOFAILOVER address. If so, honor it. This implies 19750 * that if the interface is failed, we will still send 19751 * the packet on the same ill which is what we want. 19752 */ 19753 conn_outgoing_ill = conn_get_held_ill(connp, 19754 &connp->conn_nofailover_ill, &err); 19755 if (err == ILL_LOOKUP_FAILED) { 19756 ire_refrele(ire); 19757 freemsg(mp); 19758 return; 19759 } 19760 if (conn_outgoing_ill == NULL) { 19761 /* 19762 * Choose a good ill in the group to send the 19763 * packets on. 19764 */ 19765 ire = conn_set_outgoing_ill(connp, ire, 19766 &conn_outgoing_ill); 19767 if (ire == NULL) { 19768 freemsg(mp); 19769 return; 19770 } 19771 } 19772 } 19773 19774 if (mp->b_datap->db_type != M_CTL) { 19775 ipha = (ipha_t *)mp->b_rptr; 19776 zoneid = (connp != NULL ? connp->conn_zoneid : ALL_ZONES); 19777 } else { 19778 io = (ipsec_out_t *)mp->b_rptr; 19779 ASSERT(io->ipsec_out_type == IPSEC_OUT); 19780 zoneid = io->ipsec_out_zoneid; 19781 ASSERT(zoneid != ALL_ZONES); 19782 ipha = (ipha_t *)mp->b_cont->b_rptr; 19783 dst = ipha->ipha_dst; 19784 /* 19785 * For the multicast case, ipsec_out carries conn_dontroute and 19786 * conn_multicast_loop as conn may not be available here. We 19787 * need this for multicast loopback and forwarding which is done 19788 * later in the code. 19789 */ 19790 if (CLASSD(dst)) { 19791 conn_dontroute = io->ipsec_out_dontroute; 19792 conn_multicast_loop = io->ipsec_out_multicast_loop; 19793 /* 19794 * If conn_dontroute is not set or conn_multicast_loop 19795 * is set, we need to do forwarding/loopback. For 19796 * datagrams from ip_wput_multicast, conn_dontroute is 19797 * set to B_TRUE and conn_multicast_loop is set to 19798 * B_FALSE so that we neither do forwarding nor 19799 * loopback. 19800 */ 19801 if (!conn_dontroute || conn_multicast_loop) 19802 multicast_forward = B_TRUE; 19803 } 19804 } 19805 19806 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid) { 19807 /* 19808 * When a zone sends a packet to another zone, we try to deliver 19809 * the packet under the same conditions as if the destination 19810 * was a real node on the network. To do so, we look for a 19811 * matching route in the forwarding table. 19812 * RTF_REJECT and RTF_BLACKHOLE are handled just like 19813 * ip_newroute() does. 19814 */ 19815 ire_t *src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, 0, 19816 NULL, NULL, zoneid, 0, (MATCH_IRE_RECURSIVE | 19817 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE)); 19818 if (src_ire != NULL && 19819 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))) { 19820 if (ipha->ipha_src == INADDR_ANY && !unspec_src) 19821 ipha->ipha_src = src_ire->ire_src_addr; 19822 ire_refrele(src_ire); 19823 } else { 19824 ire_refrele(ire); 19825 if (conn_outgoing_ill != NULL) 19826 ill_refrele(conn_outgoing_ill); 19827 BUMP_MIB(&ip_mib, ipOutNoRoutes); 19828 if (src_ire != NULL) { 19829 if (src_ire->ire_flags & RTF_BLACKHOLE) { 19830 ire_refrele(src_ire); 19831 freemsg(mp); 19832 return; 19833 } 19834 ire_refrele(src_ire); 19835 } 19836 if (ip_hdr_complete(ipha, zoneid)) { 19837 /* Failed */ 19838 freemsg(mp); 19839 return; 19840 } 19841 icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE); 19842 return; 19843 } 19844 } 19845 19846 if (mp->b_datap->db_type == M_CTL || 19847 ipsec_outbound_v4_policy_present) { 19848 mp = ip_wput_ire_parse_ipsec_out(mp, ipha, NULL, ire, connp, 19849 unspec_src); 19850 if (mp == NULL) { 19851 ire_refrele(ire); 19852 if (conn_outgoing_ill != NULL) 19853 ill_refrele(conn_outgoing_ill); 19854 return; 19855 } 19856 } 19857 19858 first_mp = mp; 19859 ipsec_len = 0; 19860 19861 if (first_mp->b_datap->db_type == M_CTL) { 19862 io = (ipsec_out_t *)first_mp->b_rptr; 19863 ASSERT(io->ipsec_out_type == IPSEC_OUT); 19864 mp = first_mp->b_cont; 19865 ipsec_len = ipsec_out_extra_length(first_mp); 19866 ASSERT(ipsec_len >= 0); 19867 zoneid = io->ipsec_out_zoneid; 19868 ASSERT(zoneid != ALL_ZONES); 19869 19870 /* 19871 * Drop M_CTL here if IPsec processing is not needed. 19872 * (Non-IPsec use of M_CTL extracted any information it 19873 * needed above). 19874 */ 19875 if (ipsec_len == 0) { 19876 freeb(first_mp); 19877 first_mp = mp; 19878 } 19879 } 19880 19881 /* 19882 * Fast path for ip_wput_ire 19883 */ 19884 19885 ipha = (ipha_t *)mp->b_rptr; 19886 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 19887 dst = ipha->ipha_dst; 19888 19889 /* 19890 * ICMP(RAWIP) module should set the ipha_ident to IP_HDR_INCLUDED 19891 * if the socket is a SOCK_RAW type. The transport checksum should 19892 * be provided in the pre-built packet, so we don't need to compute it. 19893 * Also, other application set flags, like DF, should not be altered. 19894 * Other transport MUST pass down zero. 19895 */ 19896 ip_hdr_included = ipha->ipha_ident; 19897 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19898 19899 if (CLASSD(dst)) { 19900 ip1dbg(("ip_wput_ire: to 0x%x ire %s addr 0x%x\n", 19901 ntohl(dst), 19902 ip_nv_lookup(ire_nv_tbl, ire->ire_type), 19903 ntohl(ire->ire_addr))); 19904 } 19905 19906 /* Macros to extract header fields from data already in registers */ 19907 #ifdef _BIG_ENDIAN 19908 #define V_HLEN (v_hlen_tos_len >> 24) 19909 #define LENGTH (v_hlen_tos_len & 0xFFFF) 19910 #define PROTO (ttl_protocol & 0xFF) 19911 #else 19912 #define V_HLEN (v_hlen_tos_len & 0xFF) 19913 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 19914 #define PROTO (ttl_protocol >> 8) 19915 #endif 19916 19917 19918 orig_src = src = ipha->ipha_src; 19919 /* (The loop back to "another" is explained down below.) */ 19920 another:; 19921 /* 19922 * Assign an ident value for this packet. We assign idents on 19923 * a per destination basis out of the IRE. There could be 19924 * other threads targeting the same destination, so we have to 19925 * arrange for a atomic increment. Note that we use a 32-bit 19926 * atomic add because it has better performance than its 19927 * 16-bit sibling. 19928 * 19929 * If running in cluster mode and if the source address 19930 * belongs to a replicated service then vector through 19931 * cl_inet_ipident vector to allocate ip identifier 19932 * NOTE: This is a contract private interface with the 19933 * clustering group. 19934 */ 19935 clusterwide = 0; 19936 if (cl_inet_ipident) { 19937 ASSERT(cl_inet_isclusterwide); 19938 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 19939 AF_INET, (uint8_t *)(uintptr_t)src)) { 19940 ipha->ipha_ident = (*cl_inet_ipident)(IPPROTO_IP, 19941 AF_INET, (uint8_t *)(uintptr_t)src, 19942 (uint8_t *)(uintptr_t)dst); 19943 clusterwide = 1; 19944 } 19945 } 19946 if (!clusterwide) { 19947 ipha->ipha_ident = 19948 (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19949 } 19950 19951 #ifndef _BIG_ENDIAN 19952 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19953 #endif 19954 19955 /* 19956 * Set source address unless sent on an ill or conn_unspec_src is set. 19957 * This is needed to obey conn_unspec_src when packets go through 19958 * ip_newroute + arp. 19959 * Assumes ip_newroute{,_multi} sets the source address as well. 19960 */ 19961 if (src == INADDR_ANY && !unspec_src) { 19962 /* 19963 * Assign the appropriate source address from the IRE if none 19964 * was specified. 19965 */ 19966 ASSERT(ire->ire_ipversion == IPV4_VERSION); 19967 19968 /* 19969 * With IP multipathing, broadcast packets are sent on the ire 19970 * that has been cleared of IRE_MARK_NORECV and that belongs to 19971 * the group. However, this ire might not be in the same zone so 19972 * we can't always use its source address. We look for a 19973 * broadcast ire in the same group and in the right zone. 19974 */ 19975 if (ire->ire_type == IRE_BROADCAST && 19976 ire->ire_zoneid != zoneid) { 19977 ire_t *src_ire = ire_ctable_lookup(dst, 0, 19978 IRE_BROADCAST, ire->ire_ipif, zoneid, 19979 (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP)); 19980 if (src_ire != NULL) { 19981 src = src_ire->ire_src_addr; 19982 ire_refrele(src_ire); 19983 } else { 19984 ire_refrele(ire); 19985 if (conn_outgoing_ill != NULL) 19986 ill_refrele(conn_outgoing_ill); 19987 freemsg(first_mp); 19988 BUMP_MIB(&ip_mib, ipOutDiscards); 19989 return; 19990 } 19991 } else { 19992 src = ire->ire_src_addr; 19993 } 19994 19995 if (connp == NULL) { 19996 ip1dbg(("ip_wput_ire: no connp and no src " 19997 "address for dst 0x%x, using src 0x%x\n", 19998 ntohl(dst), 19999 ntohl(src))); 20000 } 20001 ipha->ipha_src = src; 20002 } 20003 stq = ire->ire_stq; 20004 20005 /* 20006 * We only allow ire chains for broadcasts since there will 20007 * be multiple IRE_CACHE entries for the same multicast 20008 * address (one per ipif). 20009 */ 20010 next_mp = NULL; 20011 20012 /* broadcast packet */ 20013 if (ire->ire_type == IRE_BROADCAST) 20014 goto broadcast; 20015 20016 /* loopback ? */ 20017 if (stq == NULL) 20018 goto nullstq; 20019 20020 /* The ill_index for outbound ILL */ 20021 ill_index = Q_TO_INDEX(stq); 20022 20023 BUMP_MIB(&ip_mib, ipOutRequests); 20024 ttl_protocol = ((uint16_t *)ipha)[4]; 20025 20026 /* pseudo checksum (do it in parts for IP header checksum) */ 20027 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20028 20029 if (!IP_FLOW_CONTROLLED_ULP(PROTO)) { 20030 queue_t *dev_q = stq->q_next; 20031 20032 /* flow controlled */ 20033 if ((dev_q->q_next || dev_q->q_first) && 20034 !canput(dev_q)) 20035 goto blocked; 20036 if ((PROTO == IPPROTO_UDP) && 20037 (ip_hdr_included != IP_HDR_INCLUDED)) { 20038 hlen = (V_HLEN & 0xF) << 2; 20039 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 20040 if (*up != 0) { 20041 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, 20042 hlen, LENGTH, max_frag, ipsec_len, cksum); 20043 /* Software checksum? */ 20044 if (DB_CKSUMFLAGS(mp) == 0) { 20045 IP_STAT(ip_out_sw_cksum); 20046 IP_STAT_UPDATE( 20047 ip_udp_out_sw_cksum_bytes, 20048 LENGTH - hlen); 20049 } 20050 } 20051 } 20052 } else if (ip_hdr_included != IP_HDR_INCLUDED) { 20053 hlen = (V_HLEN & 0xF) << 2; 20054 if (PROTO == IPPROTO_TCP) { 20055 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 20056 /* 20057 * The packet header is processed once and for all, even 20058 * in the multirouting case. We disable hardware 20059 * checksum if the packet is multirouted, as it will be 20060 * replicated via several interfaces, and not all of 20061 * them may have this capability. 20062 */ 20063 IP_CKSUM_XMIT(ill, ire, mp, ipha, up, PROTO, hlen, 20064 LENGTH, max_frag, ipsec_len, cksum); 20065 /* Software checksum? */ 20066 if (DB_CKSUMFLAGS(mp) == 0) { 20067 IP_STAT(ip_out_sw_cksum); 20068 IP_STAT_UPDATE(ip_tcp_out_sw_cksum_bytes, 20069 LENGTH - hlen); 20070 } 20071 } else { 20072 sctp_hdr_t *sctph; 20073 20074 ASSERT(PROTO == IPPROTO_SCTP); 20075 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 20076 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 20077 /* 20078 * Zero out the checksum field to ensure proper 20079 * checksum calculation. 20080 */ 20081 sctph->sh_chksum = 0; 20082 #ifdef DEBUG 20083 if (!skip_sctp_cksum) 20084 #endif 20085 sctph->sh_chksum = sctp_cksum(mp, hlen); 20086 } 20087 } 20088 20089 /* 20090 * If this is a multicast packet and originated from ip_wput 20091 * we need to do loopback and forwarding checks. If it comes 20092 * from ip_wput_multicast, we SHOULD not do this. 20093 */ 20094 if (CLASSD(ipha->ipha_dst) && multicast_forward) goto multi_loopback; 20095 20096 /* checksum */ 20097 cksum += ttl_protocol; 20098 20099 /* fragment the packet */ 20100 if (max_frag < (uint_t)(LENGTH + ipsec_len)) 20101 goto fragmentit; 20102 /* 20103 * Don't use frag_flag if packet is pre-built or source 20104 * routed or if multicast (since multicast packets do 20105 * not solicit ICMP "packet too big" messages). 20106 */ 20107 if ((ip_hdr_included != IP_HDR_INCLUDED) && 20108 (V_HLEN == IP_SIMPLE_HDR_VERSION || 20109 !ip_source_route_included(ipha)) && 20110 !CLASSD(ipha->ipha_dst)) 20111 ipha->ipha_fragment_offset_and_flags |= 20112 htons(ire->ire_frag_flag); 20113 20114 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 20115 /* calculate IP header checksum */ 20116 cksum += ipha->ipha_ident; 20117 cksum += (v_hlen_tos_len >> 16)+(v_hlen_tos_len & 0xFFFF); 20118 cksum += ipha->ipha_fragment_offset_and_flags; 20119 20120 /* IP options present */ 20121 hlen = (V_HLEN & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; 20122 if (hlen) 20123 goto checksumoptions; 20124 20125 /* calculate hdr checksum */ 20126 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 20127 cksum = ~(cksum + (cksum >> 16)); 20128 ipha->ipha_hdr_checksum = (uint16_t)cksum; 20129 } 20130 if (ipsec_len != 0) { 20131 /* 20132 * We will do the rest of the processing after 20133 * we come back from IPSEC in ip_wput_ipsec_out(). 20134 */ 20135 ASSERT(MBLKL(first_mp) >= sizeof (ipsec_out_t)); 20136 20137 io = (ipsec_out_t *)first_mp->b_rptr; 20138 io->ipsec_out_ill_index = ((ill_t *)stq->q_ptr)-> 20139 ill_phyint->phyint_ifindex; 20140 20141 ipsec_out_process(q, first_mp, ire, ill_index); 20142 ire_refrele(ire); 20143 if (conn_outgoing_ill != NULL) 20144 ill_refrele(conn_outgoing_ill); 20145 return; 20146 } 20147 20148 /* 20149 * In most cases, the emission loop below is entered only 20150 * once. Only in the case where the ire holds the 20151 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 20152 * flagged ires in the bucket, and send the packet 20153 * through all crossed RTF_MULTIRT routes. 20154 */ 20155 if (ire->ire_flags & RTF_MULTIRT) { 20156 multirt_send = B_TRUE; 20157 } 20158 do { 20159 if (multirt_send) { 20160 irb_t *irb; 20161 /* 20162 * We are in a multiple send case, need to get 20163 * the next ire and make a duplicate of the packet. 20164 * ire1 holds here the next ire to process in the 20165 * bucket. If multirouting is expected, 20166 * any non-RTF_MULTIRT ire that has the 20167 * right destination address is ignored. 20168 */ 20169 irb = ire->ire_bucket; 20170 ASSERT(irb != NULL); 20171 20172 IRB_REFHOLD(irb); 20173 for (ire1 = ire->ire_next; 20174 ire1 != NULL; 20175 ire1 = ire1->ire_next) { 20176 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 20177 continue; 20178 if (ire1->ire_addr != ire->ire_addr) 20179 continue; 20180 if (ire1->ire_marks & 20181 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 20182 continue; 20183 20184 /* Got one */ 20185 IRE_REFHOLD(ire1); 20186 break; 20187 } 20188 IRB_REFRELE(irb); 20189 20190 if (ire1 != NULL) { 20191 next_mp = copyb(mp); 20192 if ((next_mp == NULL) || 20193 ((mp->b_cont != NULL) && 20194 ((next_mp->b_cont = 20195 dupmsg(mp->b_cont)) == NULL))) { 20196 freemsg(next_mp); 20197 next_mp = NULL; 20198 ire_refrele(ire1); 20199 ire1 = NULL; 20200 } 20201 } 20202 20203 /* Last multiroute ire; don't loop anymore. */ 20204 if (ire1 == NULL) { 20205 multirt_send = B_FALSE; 20206 } 20207 } 20208 mp = ip_wput_attach_llhdr(mp, ire, IPP_LOCAL_OUT, ill_index); 20209 if (mp == NULL) { 20210 BUMP_MIB(&ip_mib, ipOutDiscards); 20211 ip2dbg(("ip_wput_ire: fastpath wput pkt dropped "\ 20212 "during IPPF processing\n")); 20213 ire_refrele(ire); 20214 if (next_mp != NULL) { 20215 freemsg(next_mp); 20216 ire_refrele(ire1); 20217 } 20218 if (conn_outgoing_ill != NULL) 20219 ill_refrele(conn_outgoing_ill); 20220 return; 20221 } 20222 UPDATE_OB_PKT_COUNT(ire); 20223 ire->ire_last_used_time = lbolt; 20224 20225 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20226 "ip_wput_ire_end: q %p (%S)", 20227 q, "last copy out"); 20228 putnext(stq, mp); 20229 IRE_REFRELE(ire); 20230 20231 if (multirt_send) { 20232 ASSERT(ire1); 20233 /* 20234 * Proceed with the next RTF_MULTIRT ire, 20235 * Also set up the send-to queue accordingly. 20236 */ 20237 ire = ire1; 20238 ire1 = NULL; 20239 stq = ire->ire_stq; 20240 mp = next_mp; 20241 next_mp = NULL; 20242 ipha = (ipha_t *)mp->b_rptr; 20243 ill_index = Q_TO_INDEX(stq); 20244 } 20245 } while (multirt_send); 20246 if (conn_outgoing_ill != NULL) 20247 ill_refrele(conn_outgoing_ill); 20248 return; 20249 20250 /* 20251 * ire->ire_type == IRE_BROADCAST (minimize diffs) 20252 */ 20253 broadcast: 20254 { 20255 /* 20256 * Avoid broadcast storms by setting the ttl to 1 20257 * for broadcasts. This parameter can be set 20258 * via ndd, so make sure that for the SO_DONTROUTE 20259 * case that ipha_ttl is always set to 1. 20260 * In the event that we are replying to incoming 20261 * ICMP packets, conn could be NULL. 20262 */ 20263 if ((connp != NULL) && connp->conn_dontroute) 20264 ipha->ipha_ttl = 1; 20265 else 20266 ipha->ipha_ttl = ip_broadcast_ttl; 20267 20268 /* 20269 * Note that we are not doing a IRB_REFHOLD here. 20270 * Actually we don't care if the list changes i.e 20271 * if somebody deletes an IRE from the list while 20272 * we drop the lock, the next time we come around 20273 * ire_next will be NULL and hence we won't send 20274 * out multiple copies which is fine. 20275 */ 20276 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 20277 ire1 = ire->ire_next; 20278 if (conn_outgoing_ill != NULL) { 20279 while (ire->ire_ipif->ipif_ill != conn_outgoing_ill) { 20280 ASSERT(ire1 == ire->ire_next); 20281 if (ire1 != NULL && ire1->ire_addr == dst) { 20282 ire_refrele(ire); 20283 ire = ire1; 20284 IRE_REFHOLD(ire); 20285 ire1 = ire->ire_next; 20286 continue; 20287 } 20288 rw_exit(&ire->ire_bucket->irb_lock); 20289 /* Did not find a matching ill */ 20290 ip1dbg(("ip_wput_ire: broadcast with no " 20291 "matching IP_BOUND_IF ill %s\n", 20292 conn_outgoing_ill->ill_name)); 20293 freemsg(first_mp); 20294 if (ire != NULL) 20295 ire_refrele(ire); 20296 ill_refrele(conn_outgoing_ill); 20297 return; 20298 } 20299 } else if (ire1 != NULL && ire1->ire_addr == dst) { 20300 /* 20301 * If the next IRE has the same address and is not one 20302 * of the two copies that we need to send, try to see 20303 * whether this copy should be sent at all. This 20304 * assumes that we insert loopbacks first and then 20305 * non-loopbacks. This is acheived by inserting the 20306 * loopback always before non-loopback. 20307 * This is used to send a single copy of a broadcast 20308 * packet out all physical interfaces that have an 20309 * matching IRE_BROADCAST while also looping 20310 * back one copy (to ip_wput_local) for each 20311 * matching physical interface. However, we avoid 20312 * sending packets out different logical that match by 20313 * having ipif_up/ipif_down supress duplicate 20314 * IRE_BROADCASTS. 20315 * 20316 * This feature is currently used to get broadcasts 20317 * sent to multiple interfaces, when the broadcast 20318 * address being used applies to multiple interfaces. 20319 * For example, a whole net broadcast will be 20320 * replicated on every connected subnet of 20321 * the target net. 20322 * 20323 * Each zone has its own set of IRE_BROADCASTs, so that 20324 * we're able to distribute inbound packets to multiple 20325 * zones who share a broadcast address. We avoid looping 20326 * back outbound packets in different zones but on the 20327 * same ill, as the application would see duplicates. 20328 * 20329 * If the interfaces are part of the same group, 20330 * we would want to send only one copy out for 20331 * whole group. 20332 * 20333 * This logic assumes that ire_add_v4() groups the 20334 * IRE_BROADCAST entries so that those with the same 20335 * ire_addr and ill_group are kept together. 20336 */ 20337 ire_ill = ire->ire_ipif->ipif_ill; 20338 if (ire->ire_stq == NULL && ire1->ire_stq != NULL) { 20339 if (ire_ill->ill_group != NULL && 20340 (ire->ire_marks & IRE_MARK_NORECV)) { 20341 /* 20342 * If the current zone only has an ire 20343 * broadcast for this address marked 20344 * NORECV, the ire we want is ahead in 20345 * the bucket, so we look it up 20346 * deliberately ignoring the zoneid. 20347 */ 20348 for (ire1 = ire->ire_bucket->irb_ire; 20349 ire1 != NULL; 20350 ire1 = ire1->ire_next) { 20351 ire1_ill = 20352 ire1->ire_ipif->ipif_ill; 20353 if (ire1->ire_addr != dst) 20354 continue; 20355 /* skip over the current ire */ 20356 if (ire1 == ire) 20357 continue; 20358 /* skip over deleted ires */ 20359 if (ire1->ire_marks & 20360 IRE_MARK_CONDEMNED) 20361 continue; 20362 /* 20363 * non-loopback ire in our 20364 * group: use it for the next 20365 * pass in the loop 20366 */ 20367 if (ire1->ire_stq != NULL && 20368 ire1_ill->ill_group == 20369 ire_ill->ill_group) 20370 break; 20371 } 20372 } 20373 } else { 20374 while (ire1 != NULL && ire1->ire_addr == dst) { 20375 ire1_ill = ire1->ire_ipif->ipif_ill; 20376 /* 20377 * We can have two broadcast ires on the 20378 * same ill in different zones; here 20379 * we'll send a copy of the packet on 20380 * each ill and the fanout code will 20381 * call conn_wantpacket() to check that 20382 * the zone has the broadcast address 20383 * configured on the ill. If the two 20384 * ires are in the same group we only 20385 * send one copy up. 20386 */ 20387 if (ire1_ill != ire_ill && 20388 (ire1_ill->ill_group == NULL || 20389 ire_ill->ill_group == NULL || 20390 ire1_ill->ill_group != 20391 ire_ill->ill_group)) { 20392 break; 20393 } 20394 ire1 = ire1->ire_next; 20395 } 20396 } 20397 } 20398 ASSERT(multirt_send == B_FALSE); 20399 if (ire1 != NULL && ire1->ire_addr == dst) { 20400 if ((ire->ire_flags & RTF_MULTIRT) && 20401 (ire1->ire_flags & RTF_MULTIRT)) { 20402 /* 20403 * We are in the multirouting case. 20404 * The message must be sent at least 20405 * on both ires. These ires have been 20406 * inserted AFTER the standard ones 20407 * in ip_rt_add(). There are thus no 20408 * other ire entries for the destination 20409 * address in the rest of the bucket 20410 * that do not have the RTF_MULTIRT 20411 * flag. We don't process a copy 20412 * of the message here. This will be 20413 * done in the final sending loop. 20414 */ 20415 multirt_send = B_TRUE; 20416 } else { 20417 next_mp = ip_copymsg(first_mp); 20418 if (next_mp != NULL) 20419 IRE_REFHOLD(ire1); 20420 } 20421 } 20422 rw_exit(&ire->ire_bucket->irb_lock); 20423 } 20424 20425 if (stq) { 20426 /* 20427 * A non-NULL send-to queue means this packet is going 20428 * out of this machine. 20429 */ 20430 20431 BUMP_MIB(&ip_mib, ipOutRequests); 20432 ttl_protocol = ((uint16_t *)ipha)[4]; 20433 /* 20434 * We accumulate the pseudo header checksum in cksum. 20435 * This is pretty hairy code, so watch close. One 20436 * thing to keep in mind is that UDP and TCP have 20437 * stored their respective datagram lengths in their 20438 * checksum fields. This lines things up real nice. 20439 */ 20440 cksum = (dst >> 16) + (dst & 0xFFFF) + 20441 (src >> 16) + (src & 0xFFFF); 20442 /* 20443 * We assume the udp checksum field contains the 20444 * length, so to compute the pseudo header checksum, 20445 * all we need is the protocol number and src/dst. 20446 */ 20447 /* Provide the checksums for UDP and TCP. */ 20448 if ((PROTO == IPPROTO_TCP) && 20449 (ip_hdr_included != IP_HDR_INCLUDED)) { 20450 /* hlen gets the number of uchar_ts in the IP header */ 20451 hlen = (V_HLEN & 0xF) << 2; 20452 up = IPH_TCPH_CHECKSUMP(ipha, hlen); 20453 IP_STAT(ip_out_sw_cksum); 20454 IP_STAT_UPDATE(ip_tcp_out_sw_cksum_bytes, 20455 LENGTH - hlen); 20456 *up = IP_CSUM(mp, hlen, cksum + IP_TCP_CSUM_COMP); 20457 if (*up == 0) 20458 *up = 0xFFFF; 20459 } else if (PROTO == IPPROTO_SCTP && 20460 (ip_hdr_included != IP_HDR_INCLUDED)) { 20461 sctp_hdr_t *sctph; 20462 20463 hlen = (V_HLEN & 0xF) << 2; 20464 ASSERT(MBLKL(mp) >= (hlen + sizeof (*sctph))); 20465 sctph = (sctp_hdr_t *)(mp->b_rptr + hlen); 20466 sctph->sh_chksum = 0; 20467 #ifdef DEBUG 20468 if (!skip_sctp_cksum) 20469 #endif 20470 sctph->sh_chksum = sctp_cksum(mp, hlen); 20471 } else { 20472 queue_t *dev_q = stq->q_next; 20473 20474 if ((dev_q->q_next || dev_q->q_first) && 20475 !canput(dev_q)) { 20476 blocked: 20477 ipha->ipha_ident = ip_hdr_included; 20478 /* 20479 * If we don't have a conn to apply 20480 * backpressure, free the message. 20481 * In the ire_send path, we don't know 20482 * the position to requeue the packet. Rather 20483 * than reorder packets, we just drop this 20484 * packet. 20485 */ 20486 if (ip_output_queue && connp != NULL && 20487 caller != IRE_SEND) { 20488 if (caller == IP_WSRV) { 20489 connp->conn_did_putbq = 1; 20490 (void) putbq(connp->conn_wq, 20491 first_mp); 20492 conn_drain_insert(connp); 20493 /* 20494 * This is the service thread, 20495 * and the queue is already 20496 * noenabled. The check for 20497 * canput and the putbq is not 20498 * atomic. So we need to check 20499 * again. 20500 */ 20501 if (canput(stq->q_next)) 20502 connp->conn_did_putbq 20503 = 0; 20504 IP_STAT(ip_conn_flputbq); 20505 } else { 20506 /* 20507 * We are not the service proc. 20508 * ip_wsrv will be scheduled or 20509 * is already running. 20510 */ 20511 (void) putq(connp->conn_wq, 20512 first_mp); 20513 } 20514 } else { 20515 BUMP_MIB(&ip_mib, ipOutDiscards); 20516 freemsg(first_mp); 20517 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20518 "ip_wput_ire_end: q %p (%S)", 20519 q, "discard"); 20520 } 20521 ire_refrele(ire); 20522 if (next_mp) { 20523 ire_refrele(ire1); 20524 freemsg(next_mp); 20525 } 20526 if (conn_outgoing_ill != NULL) 20527 ill_refrele(conn_outgoing_ill); 20528 return; 20529 } 20530 if ((PROTO == IPPROTO_UDP) && 20531 (ip_hdr_included != IP_HDR_INCLUDED)) { 20532 /* 20533 * hlen gets the number of uchar_ts in the 20534 * IP header 20535 */ 20536 hlen = (V_HLEN & 0xF) << 2; 20537 up = IPH_UDPH_CHECKSUMP(ipha, hlen); 20538 max_frag = ire->ire_max_frag; 20539 if (*up != 0) { 20540 IP_CKSUM_XMIT(ire_ill, ire, mp, ipha, 20541 up, PROTO, hlen, LENGTH, max_frag, 20542 ipsec_len, cksum); 20543 /* Software checksum? */ 20544 if (DB_CKSUMFLAGS(mp) == 0) { 20545 IP_STAT(ip_out_sw_cksum); 20546 IP_STAT_UPDATE( 20547 ip_udp_out_sw_cksum_bytes, 20548 LENGTH - hlen); 20549 } 20550 } 20551 } 20552 } 20553 /* 20554 * Need to do this even when fragmenting. The local 20555 * loopback can be done without computing checksums 20556 * but forwarding out other interface must be done 20557 * after the IP checksum (and ULP checksums) have been 20558 * computed. 20559 * 20560 * NOTE : multicast_forward is set only if this packet 20561 * originated from ip_wput. For packets originating from 20562 * ip_wput_multicast, it is not set. 20563 */ 20564 if (CLASSD(ipha->ipha_dst) && multicast_forward) { 20565 multi_loopback: 20566 ip2dbg(("ip_wput: multicast, loop %d\n", 20567 conn_multicast_loop)); 20568 20569 /* Forget header checksum offload */ 20570 DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; 20571 20572 /* 20573 * Local loopback of multicasts? Check the 20574 * ill. 20575 * 20576 * Note that the loopback function will not come 20577 * in through ip_rput - it will only do the 20578 * client fanout thus we need to do an mforward 20579 * as well. The is different from the BSD 20580 * logic. 20581 */ 20582 if (ill != NULL) { 20583 ilm_t *ilm; 20584 20585 ILM_WALKER_HOLD(ill); 20586 ilm = ilm_lookup_ill(ill, ipha->ipha_dst, 20587 ALL_ZONES); 20588 ILM_WALKER_RELE(ill); 20589 if (ilm != NULL) { 20590 /* 20591 * Pass along the virtual output q. 20592 * ip_wput_local() will distribute the 20593 * packet to all the matching zones, 20594 * except the sending zone when 20595 * IP_MULTICAST_LOOP is false. 20596 */ 20597 ip_multicast_loopback(q, ill, first_mp, 20598 conn_multicast_loop ? 0 : 20599 IP_FF_NO_MCAST_LOOP, zoneid); 20600 } 20601 } 20602 if (ipha->ipha_ttl == 0) { 20603 /* 20604 * 0 => only to this host i.e. we are 20605 * done. We are also done if this was the 20606 * loopback interface since it is sufficient 20607 * to loopback one copy of a multicast packet. 20608 */ 20609 freemsg(first_mp); 20610 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20611 "ip_wput_ire_end: q %p (%S)", 20612 q, "loopback"); 20613 ire_refrele(ire); 20614 if (conn_outgoing_ill != NULL) 20615 ill_refrele(conn_outgoing_ill); 20616 return; 20617 } 20618 /* 20619 * ILLF_MULTICAST is checked in ip_newroute 20620 * i.e. we don't need to check it here since 20621 * all IRE_CACHEs come from ip_newroute. 20622 * For multicast traffic, SO_DONTROUTE is interpreted 20623 * to mean only send the packet out the interface 20624 * (optionally specified with IP_MULTICAST_IF) 20625 * and do not forward it out additional interfaces. 20626 * RSVP and the rsvp daemon is an example of a 20627 * protocol and user level process that 20628 * handles it's own routing. Hence, it uses the 20629 * SO_DONTROUTE option to accomplish this. 20630 */ 20631 20632 if (ip_g_mrouter && !conn_dontroute && ill != NULL) { 20633 /* Unconditionally redo the checksum */ 20634 ipha->ipha_hdr_checksum = 0; 20635 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 20636 20637 /* 20638 * If this needs to go out secure, we need 20639 * to wait till we finish the IPSEC 20640 * processing. 20641 */ 20642 if (ipsec_len == 0 && 20643 ip_mforward(ill, ipha, mp)) { 20644 freemsg(first_mp); 20645 ip1dbg(("ip_wput: mforward failed\n")); 20646 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20647 "ip_wput_ire_end: q %p (%S)", 20648 q, "mforward failed"); 20649 ire_refrele(ire); 20650 if (conn_outgoing_ill != NULL) 20651 ill_refrele(conn_outgoing_ill); 20652 return; 20653 } 20654 } 20655 } 20656 max_frag = ire->ire_max_frag; 20657 cksum += ttl_protocol; 20658 if (max_frag >= (uint_t)(LENGTH + ipsec_len)) { 20659 /* No fragmentation required for this one. */ 20660 /* 20661 * Don't use frag_flag if packet is pre-built or source 20662 * routed or if multicast (since multicast packets do 20663 * not solicit ICMP "packet too big" messages). 20664 */ 20665 if ((ip_hdr_included != IP_HDR_INCLUDED) && 20666 (V_HLEN == IP_SIMPLE_HDR_VERSION || 20667 !ip_source_route_included(ipha)) && 20668 !CLASSD(ipha->ipha_dst)) 20669 ipha->ipha_fragment_offset_and_flags |= 20670 htons(ire->ire_frag_flag); 20671 20672 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 20673 /* Complete the IP header checksum. */ 20674 cksum += ipha->ipha_ident; 20675 cksum += (v_hlen_tos_len >> 16)+ 20676 (v_hlen_tos_len & 0xFFFF); 20677 cksum += ipha->ipha_fragment_offset_and_flags; 20678 hlen = (V_HLEN & 0xF) - 20679 IP_SIMPLE_HDR_LENGTH_IN_WORDS; 20680 if (hlen) { 20681 checksumoptions: 20682 /* 20683 * Account for the IP Options in the IP 20684 * header checksum. 20685 */ 20686 up = (uint16_t *)(rptr+ 20687 IP_SIMPLE_HDR_LENGTH); 20688 do { 20689 cksum += up[0]; 20690 cksum += up[1]; 20691 up += 2; 20692 } while (--hlen); 20693 } 20694 cksum = ((cksum & 0xFFFF) + (cksum >> 16)); 20695 cksum = ~(cksum + (cksum >> 16)); 20696 ipha->ipha_hdr_checksum = (uint16_t)cksum; 20697 } 20698 if (ipsec_len != 0) { 20699 ipsec_out_process(q, first_mp, ire, ill_index); 20700 if (!next_mp) { 20701 ire_refrele(ire); 20702 if (conn_outgoing_ill != NULL) 20703 ill_refrele(conn_outgoing_ill); 20704 return; 20705 } 20706 goto next; 20707 } 20708 20709 /* 20710 * multirt_send has already been handled 20711 * for broadcast, but not yet for multicast 20712 * or IP options. 20713 */ 20714 if (next_mp == NULL) { 20715 if (ire->ire_flags & RTF_MULTIRT) { 20716 multirt_send = B_TRUE; 20717 } 20718 } 20719 20720 /* 20721 * In most cases, the emission loop below is 20722 * entered only once. Only in the case where 20723 * the ire holds the RTF_MULTIRT flag, do we loop 20724 * to process all RTF_MULTIRT ires in the bucket, 20725 * and send the packet through all crossed 20726 * RTF_MULTIRT routes. 20727 */ 20728 do { 20729 if (multirt_send) { 20730 irb_t *irb; 20731 20732 irb = ire->ire_bucket; 20733 ASSERT(irb != NULL); 20734 /* 20735 * We are in a multiple send case, 20736 * need to get the next IRE and make 20737 * a duplicate of the packet. 20738 */ 20739 IRB_REFHOLD(irb); 20740 for (ire1 = ire->ire_next; 20741 ire1 != NULL; 20742 ire1 = ire1->ire_next) { 20743 if (!(ire1->ire_flags & 20744 RTF_MULTIRT)) 20745 continue; 20746 if (ire1->ire_addr != 20747 ire->ire_addr) 20748 continue; 20749 if (ire1->ire_marks & 20750 (IRE_MARK_CONDEMNED| 20751 IRE_MARK_HIDDEN)) 20752 continue; 20753 20754 /* Got one */ 20755 IRE_REFHOLD(ire1); 20756 break; 20757 } 20758 IRB_REFRELE(irb); 20759 20760 if (ire1 != NULL) { 20761 next_mp = copyb(mp); 20762 if ((next_mp == NULL) || 20763 ((mp->b_cont != NULL) && 20764 ((next_mp->b_cont = 20765 dupmsg(mp->b_cont)) 20766 == NULL))) { 20767 freemsg(next_mp); 20768 next_mp = NULL; 20769 ire_refrele(ire1); 20770 ire1 = NULL; 20771 } 20772 } 20773 20774 /* 20775 * Last multiroute ire; don't loop 20776 * anymore. The emission is over 20777 * and next_mp is NULL. 20778 */ 20779 if (ire1 == NULL) { 20780 multirt_send = B_FALSE; 20781 } 20782 } 20783 20784 noprepend: 20785 ASSERT(ipsec_len == 0); 20786 mp1 = ip_wput_attach_llhdr(mp, ire, 20787 IPP_LOCAL_OUT, ill_index); 20788 if (mp1 == NULL) { 20789 BUMP_MIB(&ip_mib, ipOutDiscards); 20790 if (next_mp) { 20791 freemsg(next_mp); 20792 ire_refrele(ire1); 20793 } 20794 ire_refrele(ire); 20795 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20796 "ip_wput_ire_end: q %p (%S)", 20797 q, "discard MDATA"); 20798 if (conn_outgoing_ill != NULL) 20799 ill_refrele(conn_outgoing_ill); 20800 return; 20801 } 20802 UPDATE_OB_PKT_COUNT(ire); 20803 ire->ire_last_used_time = lbolt; 20804 20805 if (multirt_send) { 20806 /* 20807 * We are in a multiple send case, 20808 * need to re-enter the sending loop 20809 * using the next ire. 20810 */ 20811 putnext(stq, mp1); 20812 ire_refrele(ire); 20813 ire = ire1; 20814 stq = ire->ire_stq; 20815 mp = next_mp; 20816 next_mp = NULL; 20817 ipha = (ipha_t *)mp->b_rptr; 20818 ill_index = Q_TO_INDEX(stq); 20819 } 20820 } while (multirt_send); 20821 20822 if (!next_mp) { 20823 /* 20824 * Last copy going out (the ultra-common 20825 * case). Note that we intentionally replicate 20826 * the putnext rather than calling it before 20827 * the next_mp check in hopes of a little 20828 * tail-call action out of the compiler. 20829 */ 20830 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20831 "ip_wput_ire_end: q %p (%S)", 20832 q, "last copy out(1)"); 20833 putnext(stq, mp1); 20834 ire_refrele(ire); 20835 if (conn_outgoing_ill != NULL) 20836 ill_refrele(conn_outgoing_ill); 20837 return; 20838 } 20839 /* More copies going out below. */ 20840 putnext(stq, mp1); 20841 } else { 20842 int offset; 20843 fragmentit: 20844 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 20845 /* 20846 * If this would generate a icmp_frag_needed message, 20847 * we need to handle it before we do the IPSEC 20848 * processing. Otherwise, we need to strip the IPSEC 20849 * headers before we send up the message to the ULPs 20850 * which becomes messy and difficult. 20851 */ 20852 if (ipsec_len != 0) { 20853 if ((max_frag < (unsigned int)(LENGTH + 20854 ipsec_len)) && (offset & IPH_DF)) { 20855 20856 BUMP_MIB(&ip_mib, ipFragFails); 20857 ipha->ipha_hdr_checksum = 0; 20858 ipha->ipha_hdr_checksum = 20859 (uint16_t)ip_csum_hdr(ipha); 20860 icmp_frag_needed(ire->ire_stq, first_mp, 20861 max_frag); 20862 if (!next_mp) { 20863 ire_refrele(ire); 20864 if (conn_outgoing_ill != NULL) { 20865 ill_refrele( 20866 conn_outgoing_ill); 20867 } 20868 return; 20869 } 20870 } else { 20871 /* 20872 * This won't cause a icmp_frag_needed 20873 * message. to be gnerated. Send it on 20874 * the wire. Note that this could still 20875 * cause fragmentation and all we 20876 * do is the generation of the message 20877 * to the ULP if needed before IPSEC. 20878 */ 20879 if (!next_mp) { 20880 ipsec_out_process(q, first_mp, 20881 ire, ill_index); 20882 TRACE_2(TR_FAC_IP, 20883 TR_IP_WPUT_IRE_END, 20884 "ip_wput_ire_end: q %p " 20885 "(%S)", q, 20886 "last ipsec_out_process"); 20887 ire_refrele(ire); 20888 if (conn_outgoing_ill != NULL) { 20889 ill_refrele( 20890 conn_outgoing_ill); 20891 } 20892 return; 20893 } 20894 ipsec_out_process(q, first_mp, 20895 ire, ill_index); 20896 } 20897 } else { 20898 /* Initiate IPPF processing */ 20899 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 20900 ip_process(IPP_LOCAL_OUT, &mp, 20901 ill_index); 20902 if (mp == NULL) { 20903 BUMP_MIB(&ip_mib, 20904 ipOutDiscards); 20905 if (next_mp != NULL) { 20906 freemsg(next_mp); 20907 ire_refrele(ire1); 20908 } 20909 ire_refrele(ire); 20910 TRACE_2(TR_FAC_IP, 20911 TR_IP_WPUT_IRE_END, 20912 "ip_wput_ire: q %p (%S)", 20913 q, "discard MDATA"); 20914 if (conn_outgoing_ill != NULL) { 20915 ill_refrele( 20916 conn_outgoing_ill); 20917 } 20918 return; 20919 } 20920 } 20921 if (!next_mp) { 20922 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20923 "ip_wput_ire_end: q %p (%S)", 20924 q, "last fragmentation"); 20925 ip_wput_ire_fragmentit(mp, ire); 20926 ire_refrele(ire); 20927 if (conn_outgoing_ill != NULL) 20928 ill_refrele(conn_outgoing_ill); 20929 return; 20930 } 20931 ip_wput_ire_fragmentit(mp, ire); 20932 } 20933 } 20934 } else { 20935 nullstq: 20936 /* A NULL stq means the destination address is local. */ 20937 UPDATE_OB_PKT_COUNT(ire); 20938 ire->ire_last_used_time = lbolt; 20939 ASSERT(ire->ire_ipif != NULL); 20940 if (!next_mp) { 20941 TRACE_2(TR_FAC_IP, TR_IP_WPUT_IRE_END, 20942 "ip_wput_ire_end: q %p (%S)", 20943 q, "local address"); 20944 ip_wput_local(q, ire->ire_ipif->ipif_ill, ipha, 20945 first_mp, ire, 0, ire->ire_zoneid); 20946 ire_refrele(ire); 20947 if (conn_outgoing_ill != NULL) 20948 ill_refrele(conn_outgoing_ill); 20949 return; 20950 } 20951 ip_wput_local(q, ire->ire_ipif->ipif_ill, ipha, first_mp, 20952 ire, 0, ire->ire_zoneid); 20953 } 20954 next: 20955 /* 20956 * More copies going out to additional interfaces. 20957 * ire1 has already been held. We don't need the 20958 * "ire" anymore. 20959 */ 20960 ire_refrele(ire); 20961 ire = ire1; 20962 ASSERT(ire != NULL && ire->ire_refcnt >= 1 && next_mp != NULL); 20963 mp = next_mp; 20964 ASSERT(ire->ire_ipversion == IPV4_VERSION); 20965 ill = ire_to_ill(ire); 20966 first_mp = mp; 20967 if (ipsec_len != 0) { 20968 ASSERT(first_mp->b_datap->db_type == M_CTL); 20969 mp = mp->b_cont; 20970 } 20971 dst = ire->ire_addr; 20972 ipha = (ipha_t *)mp->b_rptr; 20973 /* 20974 * Restore src so that we will pick up ire->ire_src_addr if src was 0. 20975 * Restore ipha_ident "no checksum" flag. 20976 */ 20977 src = orig_src; 20978 ipha->ipha_ident = ip_hdr_included; 20979 goto another; 20980 20981 #undef rptr 20982 #undef Q_TO_INDEX 20983 } 20984 20985 /* 20986 * Routine to allocate a message that is used to notify the ULP about MDT. 20987 * The caller may provide a pointer to the link-layer MDT capabilities, 20988 * or NULL if MDT is to be disabled on the stream. 20989 */ 20990 mblk_t * 20991 ip_mdinfo_alloc(ill_mdt_capab_t *isrc) 20992 { 20993 mblk_t *mp; 20994 ip_mdt_info_t *mdti; 20995 ill_mdt_capab_t *idst; 20996 20997 if ((mp = allocb(sizeof (*mdti), BPRI_HI)) != NULL) { 20998 DB_TYPE(mp) = M_CTL; 20999 mp->b_wptr = mp->b_rptr + sizeof (*mdti); 21000 mdti = (ip_mdt_info_t *)mp->b_rptr; 21001 mdti->mdt_info_id = MDT_IOC_INFO_UPDATE; 21002 idst = &(mdti->mdt_capab); 21003 21004 /* 21005 * If the caller provides us with the capability, copy 21006 * it over into our notification message; otherwise 21007 * we zero out the capability portion. 21008 */ 21009 if (isrc != NULL) 21010 bcopy((caddr_t)isrc, (caddr_t)idst, sizeof (*idst)); 21011 else 21012 bzero((caddr_t)idst, sizeof (*idst)); 21013 } 21014 return (mp); 21015 } 21016 21017 /* 21018 * Routine which determines whether MDT can be enabled on the destination 21019 * IRE and IPC combination, and if so, allocates and returns the MDT 21020 * notification mblk that may be used by ULP. We also check if we need to 21021 * turn MDT back to 'on' when certain restrictions prohibiting us to allow 21022 * MDT usage in the past have been lifted. This gets called during IP 21023 * and ULP binding. 21024 */ 21025 mblk_t * 21026 ip_mdinfo_return(ire_t *dst_ire, conn_t *connp, char *ill_name, 21027 ill_mdt_capab_t *mdt_cap) 21028 { 21029 mblk_t *mp; 21030 boolean_t rc = B_FALSE; 21031 21032 ASSERT(dst_ire != NULL); 21033 ASSERT(connp != NULL); 21034 ASSERT(mdt_cap != NULL); 21035 21036 /* 21037 * Currently, we only support simple TCP/{IPv4,IPv6} with 21038 * Multidata, which is handled in tcp_multisend(). This 21039 * is the reason why we do all these checks here, to ensure 21040 * that we don't enable Multidata for the cases which we 21041 * can't handle at the moment. 21042 */ 21043 do { 21044 /* Only do TCP at the moment */ 21045 if (connp->conn_ulp != IPPROTO_TCP) 21046 break; 21047 21048 /* 21049 * IPSEC outbound policy present? Note that we get here 21050 * after calling ipsec_conn_cache_policy() where the global 21051 * policy checking is performed. conn_latch will be 21052 * non-NULL as long as there's a policy defined, 21053 * i.e. conn_out_enforce_policy may be NULL in such case 21054 * when the connection is non-secure, and hence we check 21055 * further if the latch refers to an outbound policy. 21056 */ 21057 if (CONN_IPSEC_OUT_ENCAPSULATED(connp)) 21058 break; 21059 21060 /* CGTP (multiroute) is enabled? */ 21061 if (dst_ire->ire_flags & RTF_MULTIRT) 21062 break; 21063 21064 /* Outbound IPQoS enabled? */ 21065 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 21066 /* 21067 * In this case, we disable MDT for this and all 21068 * future connections going over the interface. 21069 */ 21070 mdt_cap->ill_mdt_on = 0; 21071 break; 21072 } 21073 21074 /* socket option(s) present? */ 21075 if (!CONN_IS_MD_FASTPATH(connp)) 21076 break; 21077 21078 rc = B_TRUE; 21079 /* CONSTCOND */ 21080 } while (0); 21081 21082 /* Remember the result */ 21083 connp->conn_mdt_ok = rc; 21084 21085 if (!rc) 21086 return (NULL); 21087 else if (!mdt_cap->ill_mdt_on) { 21088 /* 21089 * If MDT has been previously turned off in the past, and we 21090 * currently can do MDT (due to IPQoS policy removal, etc.) 21091 * then enable it for this interface. 21092 */ 21093 mdt_cap->ill_mdt_on = 1; 21094 ip1dbg(("ip_mdinfo_return: reenabling MDT for " 21095 "interface %s\n", ill_name)); 21096 } 21097 21098 /* Allocate the MDT info mblk */ 21099 if ((mp = ip_mdinfo_alloc(mdt_cap)) == NULL) { 21100 ip0dbg(("ip_mdinfo_return: can't enable Multidata for " 21101 "conn %p on %s (ENOMEM)\n", (void *)connp, ill_name)); 21102 return (NULL); 21103 } 21104 return (mp); 21105 } 21106 21107 /* 21108 * Create destination address attribute, and fill it with the physical 21109 * destination address and SAP taken from the template DL_UNITDATA_REQ 21110 * message block. 21111 */ 21112 boolean_t 21113 ip_md_addr_attr(multidata_t *mmd, pdesc_t *pd, const mblk_t *dlmp) 21114 { 21115 dl_unitdata_req_t *dlurp; 21116 pattr_t *pa; 21117 pattrinfo_t pa_info; 21118 pattr_addr_t **das = (pattr_addr_t **)&pa_info.buf; 21119 uint_t das_len, das_off; 21120 21121 ASSERT(dlmp != NULL); 21122 21123 dlurp = (dl_unitdata_req_t *)dlmp->b_rptr; 21124 das_len = dlurp->dl_dest_addr_length; 21125 das_off = dlurp->dl_dest_addr_offset; 21126 21127 pa_info.type = PATTR_DSTADDRSAP; 21128 pa_info.len = sizeof (**das) + das_len - 1; 21129 21130 /* create and associate the attribute */ 21131 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 21132 if (pa != NULL) { 21133 ASSERT(*das != NULL); 21134 (*das)->addr_is_group = 0; 21135 (*das)->addr_len = (uint8_t)das_len; 21136 bcopy((caddr_t)dlurp + das_off, (*das)->addr, das_len); 21137 } 21138 21139 return (pa != NULL); 21140 } 21141 21142 /* 21143 * Create hardware checksum attribute and fill it with the values passed. 21144 */ 21145 boolean_t 21146 ip_md_hcksum_attr(multidata_t *mmd, pdesc_t *pd, uint32_t start_offset, 21147 uint32_t stuff_offset, uint32_t end_offset, uint32_t flags) 21148 { 21149 pattr_t *pa; 21150 pattrinfo_t pa_info; 21151 21152 ASSERT(mmd != NULL); 21153 21154 pa_info.type = PATTR_HCKSUM; 21155 pa_info.len = sizeof (pattr_hcksum_t); 21156 21157 /* create and associate the attribute */ 21158 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 21159 if (pa != NULL) { 21160 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 21161 21162 hck->hcksum_start_offset = start_offset; 21163 hck->hcksum_stuff_offset = stuff_offset; 21164 hck->hcksum_end_offset = end_offset; 21165 hck->hcksum_flags = flags; 21166 } 21167 return (pa != NULL); 21168 } 21169 21170 /* 21171 * Create zerocopy attribute and fill it with the specified flags 21172 */ 21173 boolean_t 21174 ip_md_zcopy_attr(multidata_t *mmd, pdesc_t *pd, uint_t flags) 21175 { 21176 pattr_t *pa; 21177 pattrinfo_t pa_info; 21178 21179 ASSERT(mmd != NULL); 21180 pa_info.type = PATTR_ZCOPY; 21181 pa_info.len = sizeof (pattr_zcopy_t); 21182 21183 /* create and associate the attribute */ 21184 pa = mmd_addpattr(mmd, pd, &pa_info, B_TRUE, KM_NOSLEEP); 21185 if (pa != NULL) { 21186 pattr_zcopy_t *zcopy = (pattr_zcopy_t *)pa_info.buf; 21187 21188 zcopy->zcopy_flags = flags; 21189 } 21190 return (pa != NULL); 21191 } 21192 21193 /* 21194 * Check if ip_wput_frag_mdt() and ip_wput_frag_mdt_v6() can handle a message 21195 * block chain. We could rewrite to handle arbitrary message block chains but 21196 * that would make the code complicated and slow. Right now there three 21197 * restrictions: 21198 * 21199 * 1. The first message block must contain the complete IP header and 21200 * at least 1 byte of payload data. 21201 * 2. At most MULTIDATA_MAX_PBUFS non-empty message blocks are allowed 21202 * so that we can use a single Multidata message. 21203 * 3. No frag must be distributed over two or more message blocks so 21204 * that we don't need more than two packet descriptors per frag. 21205 * 21206 * The above restrictions allow us to support userland applications (which 21207 * will send down a single message block) and NFS over UDP (which will 21208 * send down a chain of at most three message blocks). 21209 * 21210 * We also don't use MDT for payloads with less than or equal to 21211 * ip_wput_frag_mdt_min bytes because it would cause too much overhead. 21212 */ 21213 boolean_t 21214 ip_can_frag_mdt(mblk_t *mp, ssize_t hdr_len, ssize_t len) 21215 { 21216 int blocks; 21217 ssize_t total, missing, size; 21218 21219 ASSERT(mp != NULL); 21220 ASSERT(hdr_len > 0); 21221 21222 size = MBLKL(mp) - hdr_len; 21223 if (size <= 0) 21224 return (B_FALSE); 21225 21226 /* The first mblk contains the header and some payload. */ 21227 blocks = 1; 21228 total = size; 21229 size %= len; 21230 missing = (size == 0) ? 0 : (len - size); 21231 mp = mp->b_cont; 21232 21233 while (mp != NULL) { 21234 /* 21235 * Give up if we encounter a zero length message block. 21236 * In practice, this should rarely happen and therefore 21237 * not worth the trouble of freeing and re-linking the 21238 * mblk from the chain to handle such case. 21239 */ 21240 if ((size = MBLKL(mp)) == 0) 21241 return (B_FALSE); 21242 21243 /* Too many payload buffers for a single Multidata message? */ 21244 if (++blocks > MULTIDATA_MAX_PBUFS) 21245 return (B_FALSE); 21246 21247 total += size; 21248 /* Is a frag distributed over two or more message blocks? */ 21249 if (missing > size) 21250 return (B_FALSE); 21251 size -= missing; 21252 21253 size %= len; 21254 missing = (size == 0) ? 0 : (len - size); 21255 21256 mp = mp->b_cont; 21257 } 21258 21259 return (total > ip_wput_frag_mdt_min); 21260 } 21261 21262 /* 21263 * Outbound IPv4 fragmentation routine using MDT. 21264 */ 21265 static void 21266 ip_wput_frag_mdt(ire_t *ire, mblk_t *mp, ip_pkt_t pkt_type, int len, 21267 uint32_t frag_flag, int offset) 21268 { 21269 ipha_t *ipha_orig; 21270 int i1, ip_data_end; 21271 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 21272 mblk_t *hdr_mp, *md_mp = NULL; 21273 unsigned char *hdr_ptr, *pld_ptr; 21274 multidata_t *mmd; 21275 ip_pdescinfo_t pdi; 21276 21277 ASSERT(DB_TYPE(mp) == M_DATA); 21278 ASSERT(MBLKL(mp) > sizeof (ipha_t)); 21279 21280 ipha_orig = (ipha_t *)mp->b_rptr; 21281 mp->b_rptr += sizeof (ipha_t); 21282 21283 /* Calculate how many packets we will send out */ 21284 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 21285 pkts = (i1 + len - 1) / len; 21286 ASSERT(pkts > 1); 21287 21288 /* Allocate a message block which will hold all the IP Headers. */ 21289 wroff = ip_wroff_extra; 21290 hdr_chunk_len = wroff + IP_SIMPLE_HDR_LENGTH; 21291 21292 i1 = pkts * hdr_chunk_len; 21293 /* 21294 * Create the header buffer, Multidata and destination address 21295 * and SAP attribute that should be associated with it. 21296 */ 21297 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 21298 ((hdr_mp->b_wptr += i1), 21299 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 21300 !ip_md_addr_attr(mmd, NULL, ire->ire_dlureq_mp)) { 21301 freemsg(mp); 21302 if (md_mp == NULL) { 21303 freemsg(hdr_mp); 21304 } else { 21305 free_mmd: IP_STAT(ip_frag_mdt_discarded); 21306 freemsg(md_mp); 21307 } 21308 IP_STAT(ip_frag_mdt_allocfail); 21309 UPDATE_MIB(&ip_mib, ipOutDiscards, pkts); 21310 return; 21311 } 21312 IP_STAT(ip_frag_mdt_allocd); 21313 21314 /* 21315 * Add a payload buffer to the Multidata; this operation must not 21316 * fail, or otherwise our logic in this routine is broken. There 21317 * is no memory allocation done by the routine, so any returned 21318 * failure simply tells us that we've done something wrong. 21319 * 21320 * A failure tells us that either we're adding the same payload 21321 * buffer more than once, or we're trying to add more buffers than 21322 * allowed. None of the above cases should happen, and we panic 21323 * because either there's horrible heap corruption, and/or 21324 * programming mistake. 21325 */ 21326 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 21327 goto pbuf_panic; 21328 21329 hdr_ptr = hdr_mp->b_rptr; 21330 pld_ptr = mp->b_rptr; 21331 21332 /* Establish the ending byte offset, based on the starting offset. */ 21333 offset <<= 3; 21334 ip_data_end = offset + ntohs(ipha_orig->ipha_length) - 21335 IP_SIMPLE_HDR_LENGTH; 21336 21337 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 21338 21339 while (pld_ptr < mp->b_wptr) { 21340 ipha_t *ipha; 21341 uint16_t offset_and_flags; 21342 uint16_t ip_len; 21343 int error; 21344 21345 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 21346 ipha = (ipha_t *)(hdr_ptr + wroff); 21347 ASSERT(OK_32PTR(ipha)); 21348 *ipha = *ipha_orig; 21349 21350 if (ip_data_end - offset > len) { 21351 offset_and_flags = IPH_MF; 21352 } else { 21353 /* 21354 * Last frag. Set len to the length of this last piece. 21355 */ 21356 len = ip_data_end - offset; 21357 /* A frag of a frag might have IPH_MF non-zero */ 21358 offset_and_flags = 21359 ntohs(ipha->ipha_fragment_offset_and_flags) & 21360 IPH_MF; 21361 } 21362 offset_and_flags |= (uint16_t)(offset >> 3); 21363 offset_and_flags |= (uint16_t)frag_flag; 21364 /* Store the offset and flags in the IP header. */ 21365 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 21366 21367 /* Store the length in the IP header. */ 21368 ip_len = (uint16_t)(len + IP_SIMPLE_HDR_LENGTH); 21369 ipha->ipha_length = htons(ip_len); 21370 21371 /* 21372 * Set the IP header checksum. Note that mp is just 21373 * the header, so this is easy to pass to ip_csum. 21374 */ 21375 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 21376 21377 /* 21378 * Record offset and size of header and data of the next packet 21379 * in the multidata message. 21380 */ 21381 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, IP_SIMPLE_HDR_LENGTH, 0); 21382 PDESC_PLD_INIT(&pdi); 21383 i1 = MIN(mp->b_wptr - pld_ptr, len); 21384 ASSERT(i1 > 0); 21385 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 21386 if (i1 == len) { 21387 pld_ptr += len; 21388 } else { 21389 i1 = len - i1; 21390 mp = mp->b_cont; 21391 ASSERT(mp != NULL); 21392 ASSERT(MBLKL(mp) >= i1); 21393 /* 21394 * Attach the next payload message block to the 21395 * multidata message. 21396 */ 21397 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 21398 goto pbuf_panic; 21399 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 21400 pld_ptr = mp->b_rptr + i1; 21401 } 21402 21403 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 21404 KM_NOSLEEP)) == NULL) { 21405 /* 21406 * Any failure other than ENOMEM indicates that we 21407 * have passed in invalid pdesc info or parameters 21408 * to mmd_addpdesc, which must not happen. 21409 * 21410 * EINVAL is a result of failure on boundary checks 21411 * against the pdesc info contents. It should not 21412 * happen, and we panic because either there's 21413 * horrible heap corruption, and/or programming 21414 * mistake. 21415 */ 21416 if (error != ENOMEM) { 21417 cmn_err(CE_PANIC, "ip_wput_frag_mdt: " 21418 "pdesc logic error detected for " 21419 "mmd %p pinfo %p (%d)\n", 21420 (void *)mmd, (void *)&pdi, error); 21421 /* NOTREACHED */ 21422 } 21423 IP_STAT(ip_frag_mdt_addpdescfail); 21424 /* Free unattached payload message blocks as well */ 21425 md_mp->b_cont = mp->b_cont; 21426 goto free_mmd; 21427 } 21428 21429 /* Advance fragment offset. */ 21430 offset += len; 21431 21432 /* Advance to location for next header in the buffer. */ 21433 hdr_ptr += hdr_chunk_len; 21434 21435 /* Did we reach the next payload message block? */ 21436 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 21437 mp = mp->b_cont; 21438 /* 21439 * Attach the next message block with payload 21440 * data to the multidata message. 21441 */ 21442 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 21443 goto pbuf_panic; 21444 pld_ptr = mp->b_rptr; 21445 } 21446 } 21447 21448 ASSERT(hdr_mp->b_wptr == hdr_ptr); 21449 ASSERT(mp->b_wptr == pld_ptr); 21450 21451 /* Update IP statistics */ 21452 UPDATE_MIB(&ip_mib, ipFragCreates, pkts); 21453 BUMP_MIB(&ip_mib, ipFragOKs); 21454 IP_STAT_UPDATE(ip_frag_mdt_pkt_out, pkts); 21455 21456 if (pkt_type == OB_PKT) { 21457 ire->ire_ob_pkt_count += pkts; 21458 if (ire->ire_ipif != NULL) 21459 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 21460 } else { 21461 /* 21462 * The type is IB_PKT in the forwarding path and in 21463 * the mobile IP case when the packet is being reverse- 21464 * tunneled to the home agent. 21465 */ 21466 ire->ire_ib_pkt_count += pkts; 21467 ASSERT(!IRE_IS_LOCAL(ire)); 21468 if (ire->ire_type & IRE_BROADCAST) 21469 atomic_add_32(&ire->ire_ipif->ipif_ib_pkt_count, pkts); 21470 else 21471 atomic_add_32(&ire->ire_ipif->ipif_fo_pkt_count, pkts); 21472 } 21473 ire->ire_last_used_time = lbolt; 21474 /* Send it down */ 21475 putnext(ire->ire_stq, md_mp); 21476 return; 21477 21478 pbuf_panic: 21479 cmn_err(CE_PANIC, "ip_wput_frag_mdt: payload buffer logic " 21480 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 21481 pbuf_idx); 21482 /* NOTREACHED */ 21483 } 21484 21485 /* 21486 * Outbound IP fragmentation routine. 21487 * 21488 * NOTE : This routine does not ire_refrele the ire that is passed in 21489 * as the argument. 21490 */ 21491 static void 21492 ip_wput_frag(ire_t *ire, mblk_t *mp_orig, ip_pkt_t pkt_type, uint32_t max_frag, 21493 uint32_t frag_flag) 21494 { 21495 int i1; 21496 mblk_t *ll_hdr_mp; 21497 int ll_hdr_len; 21498 int hdr_len; 21499 mblk_t *hdr_mp; 21500 ipha_t *ipha; 21501 int ip_data_end; 21502 int len; 21503 mblk_t *mp = mp_orig; 21504 int offset; 21505 queue_t *q; 21506 uint32_t v_hlen_tos_len; 21507 mblk_t *first_mp; 21508 boolean_t mctl_present; 21509 ill_t *ill; 21510 mblk_t *xmit_mp; 21511 mblk_t *carve_mp; 21512 ire_t *ire1 = NULL; 21513 ire_t *save_ire = NULL; 21514 mblk_t *next_mp = NULL; 21515 boolean_t last_frag = B_FALSE; 21516 boolean_t multirt_send = B_FALSE; 21517 ire_t *first_ire = NULL; 21518 irb_t *irb = NULL; 21519 21520 TRACE_0(TR_FAC_IP, TR_IP_WPUT_FRAG_START, 21521 "ip_wput_frag_start:"); 21522 21523 if (mp->b_datap->db_type == M_CTL) { 21524 first_mp = mp; 21525 mp_orig = mp = mp->b_cont; 21526 mctl_present = B_TRUE; 21527 } else { 21528 first_mp = mp; 21529 mctl_present = B_FALSE; 21530 } 21531 21532 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 21533 ipha = (ipha_t *)mp->b_rptr; 21534 21535 /* 21536 * If the Don't Fragment flag is on, generate an ICMP destination 21537 * unreachable, fragmentation needed. 21538 */ 21539 offset = ntohs(ipha->ipha_fragment_offset_and_flags); 21540 if (offset & IPH_DF) { 21541 BUMP_MIB(&ip_mib, ipFragFails); 21542 /* 21543 * Need to compute hdr checksum if called from ip_wput_ire. 21544 * Note that ip_rput_forward verifies the checksum before 21545 * calling this routine so in that case this is a noop. 21546 */ 21547 ipha->ipha_hdr_checksum = 0; 21548 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 21549 icmp_frag_needed(ire->ire_stq, first_mp, max_frag); 21550 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 21551 "ip_wput_frag_end:(%S)", 21552 "don't fragment"); 21553 return; 21554 } 21555 if (mctl_present) 21556 freeb(first_mp); 21557 /* 21558 * Establish the starting offset. May not be zero if we are fragging 21559 * a fragment that is being forwarded. 21560 */ 21561 offset = offset & IPH_OFFSET; 21562 21563 /* TODO why is this test needed? */ 21564 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 21565 if (((max_frag - LENGTH) & ~7) < 8) { 21566 /* TODO: notify ulp somehow */ 21567 BUMP_MIB(&ip_mib, ipFragFails); 21568 freemsg(mp); 21569 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 21570 "ip_wput_frag_end:(%S)", 21571 "len < 8"); 21572 return; 21573 } 21574 21575 hdr_len = (V_HLEN & 0xF) << 2; 21576 21577 ipha->ipha_hdr_checksum = 0; 21578 21579 /* 21580 * Establish the number of bytes maximum per frag, after putting 21581 * in the header. 21582 */ 21583 len = (max_frag - hdr_len) & ~7; 21584 21585 /* Check if we can use MDT to send out the frags. */ 21586 ASSERT(!IRE_IS_LOCAL(ire)); 21587 if (hdr_len == IP_SIMPLE_HDR_LENGTH && ip_multidata_outbound && 21588 !(ire->ire_flags & RTF_MULTIRT) && !IPP_ENABLED(IPP_LOCAL_OUT) && 21589 (ill = ire_to_ill(ire)) != NULL && ILL_MDT_CAPABLE(ill) && 21590 IP_CAN_FRAG_MDT(mp, IP_SIMPLE_HDR_LENGTH, len)) { 21591 ASSERT(ill->ill_mdt_capab != NULL); 21592 if (!ill->ill_mdt_capab->ill_mdt_on) { 21593 /* 21594 * If MDT has been previously turned off in the past, 21595 * and we currently can do MDT (due to IPQoS policy 21596 * removal, etc.) then enable it for this interface. 21597 */ 21598 ill->ill_mdt_capab->ill_mdt_on = 1; 21599 ip1dbg(("ip_wput_frag: enabled MDT for interface %s\n", 21600 ill->ill_name)); 21601 } 21602 ip_wput_frag_mdt(ire, mp, pkt_type, len, frag_flag, 21603 offset); 21604 return; 21605 } 21606 21607 /* Get a copy of the header for the trailing frags */ 21608 hdr_mp = ip_wput_frag_copyhdr((uchar_t *)ipha, hdr_len, offset); 21609 if (!hdr_mp) { 21610 BUMP_MIB(&ip_mib, ipOutDiscards); 21611 freemsg(mp); 21612 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 21613 "ip_wput_frag_end:(%S)", 21614 "couldn't copy hdr"); 21615 return; 21616 } 21617 21618 /* Store the starting offset, with the MoreFrags flag. */ 21619 i1 = offset | IPH_MF | frag_flag; 21620 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 21621 21622 /* Establish the ending byte offset, based on the starting offset. */ 21623 offset <<= 3; 21624 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 21625 21626 /* Store the length of the first fragment in the IP header. */ 21627 i1 = len + hdr_len; 21628 ASSERT(i1 <= IP_MAXPACKET); 21629 ipha->ipha_length = htons((uint16_t)i1); 21630 21631 /* 21632 * Compute the IP header checksum for the first frag. We have to 21633 * watch out that we stop at the end of the header. 21634 */ 21635 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 21636 21637 /* 21638 * Now carve off the first frag. Note that this will include the 21639 * original IP header. 21640 */ 21641 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 21642 BUMP_MIB(&ip_mib, ipOutDiscards); 21643 freeb(hdr_mp); 21644 freemsg(mp_orig); 21645 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 21646 "ip_wput_frag_end:(%S)", 21647 "couldn't carve first"); 21648 return; 21649 } 21650 21651 /* 21652 * Multirouting case. Each fragment is replicated 21653 * via all non-condemned RTF_MULTIRT routes 21654 * currently resolved. 21655 * We ensure that first_ire is the first RTF_MULTIRT 21656 * ire in the bucket. 21657 */ 21658 if (ire->ire_flags & RTF_MULTIRT) { 21659 irb = ire->ire_bucket; 21660 ASSERT(irb != NULL); 21661 21662 multirt_send = B_TRUE; 21663 21664 /* Make sure we do not omit any multiroute ire. */ 21665 IRB_REFHOLD(irb); 21666 for (first_ire = irb->irb_ire; 21667 first_ire != NULL; 21668 first_ire = first_ire->ire_next) { 21669 if ((first_ire->ire_flags & RTF_MULTIRT) && 21670 (first_ire->ire_addr == ire->ire_addr) && 21671 !(first_ire->ire_marks & 21672 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 21673 break; 21674 } 21675 21676 if (first_ire != NULL) { 21677 if (first_ire != ire) { 21678 IRE_REFHOLD(first_ire); 21679 /* 21680 * Do not release the ire passed in 21681 * as the argument. 21682 */ 21683 ire = first_ire; 21684 } else { 21685 first_ire = NULL; 21686 } 21687 } 21688 IRB_REFRELE(irb); 21689 21690 /* 21691 * Save the first ire; we will need to restore it 21692 * for the trailing frags. 21693 * We REFHOLD save_ire, as each iterated ire will be 21694 * REFRELEd. 21695 */ 21696 save_ire = ire; 21697 IRE_REFHOLD(save_ire); 21698 } 21699 21700 /* 21701 * First fragment emission loop. 21702 * In most cases, the emission loop below is entered only 21703 * once. Only in the case where the ire holds the RTF_MULTIRT 21704 * flag, do we loop to process all RTF_MULTIRT ires in the 21705 * bucket, and send the fragment through all crossed 21706 * RTF_MULTIRT routes. 21707 */ 21708 do { 21709 if (ire->ire_flags & RTF_MULTIRT) { 21710 /* 21711 * We are in a multiple send case, need to get 21712 * the next ire and make a copy of the packet. 21713 * ire1 holds here the next ire to process in the 21714 * bucket. If multirouting is expected, 21715 * any non-RTF_MULTIRT ire that has the 21716 * right destination address is ignored. 21717 * 21718 * We have to take into account the MTU of 21719 * each walked ire. max_frag is set by the 21720 * the caller and generally refers to 21721 * the primary ire entry. Here we ensure that 21722 * no route with a lower MTU will be used, as 21723 * fragments are carved once for all ires, 21724 * then replicated. 21725 */ 21726 ASSERT(irb != NULL); 21727 IRB_REFHOLD(irb); 21728 for (ire1 = ire->ire_next; 21729 ire1 != NULL; 21730 ire1 = ire1->ire_next) { 21731 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 21732 continue; 21733 if (ire1->ire_addr != ire->ire_addr) 21734 continue; 21735 if (ire1->ire_marks & 21736 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 21737 continue; 21738 /* 21739 * Ensure we do not exceed the MTU 21740 * of the next route. 21741 */ 21742 if (ire1->ire_max_frag < max_frag) { 21743 ip_multirt_bad_mtu(ire1, max_frag); 21744 continue; 21745 } 21746 21747 /* Got one. */ 21748 IRE_REFHOLD(ire1); 21749 break; 21750 } 21751 IRB_REFRELE(irb); 21752 21753 if (ire1 != NULL) { 21754 next_mp = copyb(mp); 21755 if ((next_mp == NULL) || 21756 ((mp->b_cont != NULL) && 21757 ((next_mp->b_cont = 21758 dupmsg(mp->b_cont)) == NULL))) { 21759 freemsg(next_mp); 21760 next_mp = NULL; 21761 ire_refrele(ire1); 21762 ire1 = NULL; 21763 } 21764 } 21765 21766 /* Last multiroute ire; don't loop anymore. */ 21767 if (ire1 == NULL) { 21768 multirt_send = B_FALSE; 21769 } 21770 } 21771 21772 ll_hdr_len = 0; 21773 LOCK_IRE_FP_MP(ire); 21774 ll_hdr_mp = ire->ire_fp_mp; 21775 if (ll_hdr_mp != NULL) { 21776 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 21777 ll_hdr_len = ll_hdr_mp->b_wptr - ll_hdr_mp->b_rptr; 21778 } else { 21779 ll_hdr_mp = ire->ire_dlureq_mp; 21780 } 21781 21782 /* If there is a transmit header, get a copy for this frag. */ 21783 /* 21784 * TODO: should check db_ref before calling ip_carve_mp since 21785 * it might give us a dup. 21786 */ 21787 if (!ll_hdr_mp) { 21788 /* No xmit header. */ 21789 xmit_mp = mp; 21790 } else if (mp->b_datap->db_ref == 1 && 21791 ll_hdr_len != 0 && 21792 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 21793 /* M_DATA fastpath */ 21794 mp->b_rptr -= ll_hdr_len; 21795 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, ll_hdr_len); 21796 xmit_mp = mp; 21797 } else if (!(xmit_mp = copyb(ll_hdr_mp))) { 21798 UNLOCK_IRE_FP_MP(ire); 21799 BUMP_MIB(&ip_mib, ipOutDiscards); 21800 freeb(hdr_mp); 21801 freemsg(mp); 21802 freemsg(mp_orig); 21803 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 21804 "ip_wput_frag_end:(%S)", 21805 "discard"); 21806 21807 if (multirt_send) { 21808 ASSERT(ire1); 21809 ASSERT(next_mp); 21810 21811 freemsg(next_mp); 21812 ire_refrele(ire1); 21813 } 21814 if (save_ire != NULL) 21815 IRE_REFRELE(save_ire); 21816 21817 if (first_ire != NULL) 21818 ire_refrele(first_ire); 21819 return; 21820 } else { 21821 xmit_mp->b_cont = mp; 21822 /* Get priority marking, if any. */ 21823 if (DB_TYPE(xmit_mp) == M_DATA) 21824 xmit_mp->b_band = mp->b_band; 21825 } 21826 UNLOCK_IRE_FP_MP(ire); 21827 q = ire->ire_stq; 21828 BUMP_MIB(&ip_mib, ipFragCreates); 21829 putnext(q, xmit_mp); 21830 if (pkt_type != OB_PKT) { 21831 /* 21832 * Update the packet count of trailing 21833 * RTF_MULTIRT ires. 21834 */ 21835 UPDATE_OB_PKT_COUNT(ire); 21836 } 21837 21838 if (multirt_send) { 21839 /* 21840 * We are in a multiple send case; look for 21841 * the next ire and re-enter the loop. 21842 */ 21843 ASSERT(ire1); 21844 ASSERT(next_mp); 21845 /* REFRELE the current ire before looping */ 21846 ire_refrele(ire); 21847 ire = ire1; 21848 ire1 = NULL; 21849 mp = next_mp; 21850 next_mp = NULL; 21851 } 21852 } while (multirt_send); 21853 21854 ASSERT(ire1 == NULL); 21855 21856 /* Restore the original ire; we need it for the trailing frags */ 21857 if (save_ire != NULL) { 21858 /* REFRELE the last iterated ire */ 21859 ire_refrele(ire); 21860 /* save_ire has been REFHOLDed */ 21861 ire = save_ire; 21862 save_ire = NULL; 21863 q = ire->ire_stq; 21864 } 21865 21866 if (pkt_type == OB_PKT) { 21867 UPDATE_OB_PKT_COUNT(ire); 21868 } else { 21869 UPDATE_IB_PKT_COUNT(ire); 21870 } 21871 21872 /* Advance the offset to the second frag starting point. */ 21873 offset += len; 21874 /* 21875 * Update hdr_len from the copied header - there might be less options 21876 * in the later fragments. 21877 */ 21878 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 21879 /* Loop until done. */ 21880 for (;;) { 21881 uint16_t offset_and_flags; 21882 uint16_t ip_len; 21883 21884 if (ip_data_end - offset > len) { 21885 /* 21886 * Carve off the appropriate amount from the original 21887 * datagram. 21888 */ 21889 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 21890 mp = NULL; 21891 break; 21892 } 21893 /* 21894 * More frags after this one. Get another copy 21895 * of the header. 21896 */ 21897 if (carve_mp->b_datap->db_ref == 1 && 21898 hdr_mp->b_wptr - hdr_mp->b_rptr < 21899 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 21900 /* Inline IP header */ 21901 carve_mp->b_rptr -= hdr_mp->b_wptr - 21902 hdr_mp->b_rptr; 21903 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 21904 hdr_mp->b_wptr - hdr_mp->b_rptr); 21905 mp = carve_mp; 21906 } else { 21907 if (!(mp = copyb(hdr_mp))) { 21908 freemsg(carve_mp); 21909 break; 21910 } 21911 /* Get priority marking, if any. */ 21912 mp->b_band = carve_mp->b_band; 21913 mp->b_cont = carve_mp; 21914 } 21915 ipha = (ipha_t *)mp->b_rptr; 21916 offset_and_flags = IPH_MF; 21917 } else { 21918 /* 21919 * Last frag. Consume the header. Set len to 21920 * the length of this last piece. 21921 */ 21922 len = ip_data_end - offset; 21923 21924 /* 21925 * Carve off the appropriate amount from the original 21926 * datagram. 21927 */ 21928 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 21929 mp = NULL; 21930 break; 21931 } 21932 if (carve_mp->b_datap->db_ref == 1 && 21933 hdr_mp->b_wptr - hdr_mp->b_rptr < 21934 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 21935 /* Inline IP header */ 21936 carve_mp->b_rptr -= hdr_mp->b_wptr - 21937 hdr_mp->b_rptr; 21938 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 21939 hdr_mp->b_wptr - hdr_mp->b_rptr); 21940 mp = carve_mp; 21941 freeb(hdr_mp); 21942 hdr_mp = mp; 21943 } else { 21944 mp = hdr_mp; 21945 /* Get priority marking, if any. */ 21946 mp->b_band = carve_mp->b_band; 21947 mp->b_cont = carve_mp; 21948 } 21949 ipha = (ipha_t *)mp->b_rptr; 21950 /* A frag of a frag might have IPH_MF non-zero */ 21951 offset_and_flags = 21952 ntohs(ipha->ipha_fragment_offset_and_flags) & 21953 IPH_MF; 21954 } 21955 offset_and_flags |= (uint16_t)(offset >> 3); 21956 offset_and_flags |= (uint16_t)frag_flag; 21957 /* Store the offset and flags in the IP header. */ 21958 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 21959 21960 /* Store the length in the IP header. */ 21961 ip_len = (uint16_t)(len + hdr_len); 21962 ipha->ipha_length = htons(ip_len); 21963 21964 /* 21965 * Set the IP header checksum. Note that mp is just 21966 * the header, so this is easy to pass to ip_csum. 21967 */ 21968 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 21969 21970 /* Attach a transmit header, if any, and ship it. */ 21971 if (pkt_type == OB_PKT) { 21972 UPDATE_OB_PKT_COUNT(ire); 21973 } else { 21974 UPDATE_IB_PKT_COUNT(ire); 21975 } 21976 21977 if (ire->ire_flags & RTF_MULTIRT) { 21978 irb = ire->ire_bucket; 21979 ASSERT(irb != NULL); 21980 21981 multirt_send = B_TRUE; 21982 21983 /* 21984 * Save the original ire; we will need to restore it 21985 * for the tailing frags. 21986 */ 21987 save_ire = ire; 21988 IRE_REFHOLD(save_ire); 21989 } 21990 /* 21991 * Emission loop for this fragment, similar 21992 * to what is done for the first fragment. 21993 */ 21994 do { 21995 if (multirt_send) { 21996 /* 21997 * We are in a multiple send case, need to get 21998 * the next ire and make a copy of the packet. 21999 */ 22000 ASSERT(irb != NULL); 22001 IRB_REFHOLD(irb); 22002 for (ire1 = ire->ire_next; 22003 ire1 != NULL; 22004 ire1 = ire1->ire_next) { 22005 if (!(ire1->ire_flags & RTF_MULTIRT)) 22006 continue; 22007 if (ire1->ire_addr != ire->ire_addr) 22008 continue; 22009 if (ire1->ire_marks & 22010 (IRE_MARK_CONDEMNED| 22011 IRE_MARK_HIDDEN)) 22012 continue; 22013 /* 22014 * Ensure we do not exceed the MTU 22015 * of the next route. 22016 */ 22017 if (ire1->ire_max_frag < max_frag) { 22018 ip_multirt_bad_mtu(ire1, 22019 max_frag); 22020 continue; 22021 } 22022 22023 /* Got one. */ 22024 IRE_REFHOLD(ire1); 22025 break; 22026 } 22027 IRB_REFRELE(irb); 22028 22029 if (ire1 != NULL) { 22030 next_mp = copyb(mp); 22031 if ((next_mp == NULL) || 22032 ((mp->b_cont != NULL) && 22033 ((next_mp->b_cont = 22034 dupmsg(mp->b_cont)) == NULL))) { 22035 freemsg(next_mp); 22036 next_mp = NULL; 22037 ire_refrele(ire1); 22038 ire1 = NULL; 22039 } 22040 } 22041 22042 /* Last multiroute ire; don't loop anymore. */ 22043 if (ire1 == NULL) { 22044 multirt_send = B_FALSE; 22045 } 22046 } 22047 22048 /* Update transmit header */ 22049 ll_hdr_len = 0; 22050 LOCK_IRE_FP_MP(ire); 22051 ll_hdr_mp = ire->ire_fp_mp; 22052 if (ll_hdr_mp != NULL) { 22053 ASSERT(ll_hdr_mp->b_datap->db_type == M_DATA); 22054 ll_hdr_len = MBLKL(ll_hdr_mp); 22055 } else { 22056 ll_hdr_mp = ire->ire_dlureq_mp; 22057 } 22058 22059 if (!ll_hdr_mp) { 22060 xmit_mp = mp; 22061 } else if (mp->b_datap->db_ref == 1 && 22062 ll_hdr_len != 0 && 22063 ll_hdr_len <= mp->b_rptr - mp->b_datap->db_base) { 22064 /* M_DATA fastpath */ 22065 mp->b_rptr -= ll_hdr_len; 22066 bcopy(ll_hdr_mp->b_rptr, mp->b_rptr, 22067 ll_hdr_len); 22068 xmit_mp = mp; 22069 } else if ((xmit_mp = copyb(ll_hdr_mp)) != NULL) { 22070 xmit_mp->b_cont = mp; 22071 /* Get priority marking, if any. */ 22072 if (DB_TYPE(xmit_mp) == M_DATA) 22073 xmit_mp->b_band = mp->b_band; 22074 } else { 22075 /* 22076 * Exit both the replication and 22077 * fragmentation loops. 22078 */ 22079 UNLOCK_IRE_FP_MP(ire); 22080 goto drop_pkt; 22081 } 22082 UNLOCK_IRE_FP_MP(ire); 22083 BUMP_MIB(&ip_mib, ipFragCreates); 22084 putnext(q, xmit_mp); 22085 22086 if (pkt_type != OB_PKT) { 22087 /* 22088 * Update the packet count of trailing 22089 * RTF_MULTIRT ires. 22090 */ 22091 UPDATE_OB_PKT_COUNT(ire); 22092 } 22093 22094 /* All done if we just consumed the hdr_mp. */ 22095 if (mp == hdr_mp) { 22096 last_frag = B_TRUE; 22097 } 22098 22099 if (multirt_send) { 22100 /* 22101 * We are in a multiple send case; look for 22102 * the next ire and re-enter the loop. 22103 */ 22104 ASSERT(ire1); 22105 ASSERT(next_mp); 22106 /* REFRELE the current ire before looping */ 22107 ire_refrele(ire); 22108 ire = ire1; 22109 ire1 = NULL; 22110 q = ire->ire_stq; 22111 mp = next_mp; 22112 next_mp = NULL; 22113 } 22114 } while (multirt_send); 22115 /* 22116 * Restore the original ire; we need it for the 22117 * trailing frags 22118 */ 22119 if (save_ire != NULL) { 22120 ASSERT(ire1 == NULL); 22121 /* REFRELE the last iterated ire */ 22122 ire_refrele(ire); 22123 /* save_ire has been REFHOLDed */ 22124 ire = save_ire; 22125 q = ire->ire_stq; 22126 save_ire = NULL; 22127 } 22128 22129 if (last_frag) { 22130 BUMP_MIB(&ip_mib, ipFragOKs); 22131 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 22132 "ip_wput_frag_end:(%S)", 22133 "consumed hdr_mp"); 22134 22135 if (first_ire != NULL) 22136 ire_refrele(first_ire); 22137 return; 22138 } 22139 /* Otherwise, advance and loop. */ 22140 offset += len; 22141 } 22142 22143 drop_pkt: 22144 /* Clean up following allocation failure. */ 22145 BUMP_MIB(&ip_mib, ipOutDiscards); 22146 freemsg(mp); 22147 if (mp != hdr_mp) 22148 freeb(hdr_mp); 22149 if (mp != mp_orig) 22150 freemsg(mp_orig); 22151 22152 if (save_ire != NULL) 22153 IRE_REFRELE(save_ire); 22154 if (first_ire != NULL) 22155 ire_refrele(first_ire); 22156 22157 TRACE_1(TR_FAC_IP, TR_IP_WPUT_FRAG_END, 22158 "ip_wput_frag_end:(%S)", 22159 "end--alloc failure"); 22160 } 22161 22162 /* 22163 * Copy the header plus those options which have the copy bit set 22164 */ 22165 static mblk_t * 22166 ip_wput_frag_copyhdr(uchar_t *rptr, int hdr_len, int offset) 22167 { 22168 mblk_t *mp; 22169 uchar_t *up; 22170 22171 /* 22172 * Quick check if we need to look for options without the copy bit 22173 * set 22174 */ 22175 mp = allocb(ip_wroff_extra + hdr_len, BPRI_HI); 22176 if (!mp) 22177 return (mp); 22178 mp->b_rptr += ip_wroff_extra; 22179 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 22180 bcopy(rptr, mp->b_rptr, hdr_len); 22181 mp->b_wptr += hdr_len + ip_wroff_extra; 22182 return (mp); 22183 } 22184 up = mp->b_rptr; 22185 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 22186 up += IP_SIMPLE_HDR_LENGTH; 22187 rptr += IP_SIMPLE_HDR_LENGTH; 22188 hdr_len -= IP_SIMPLE_HDR_LENGTH; 22189 while (hdr_len > 0) { 22190 uint32_t optval; 22191 uint32_t optlen; 22192 22193 optval = *rptr; 22194 if (optval == IPOPT_EOL) 22195 break; 22196 if (optval == IPOPT_NOP) 22197 optlen = 1; 22198 else 22199 optlen = rptr[1]; 22200 if (optval & IPOPT_COPY) { 22201 bcopy(rptr, up, optlen); 22202 up += optlen; 22203 } 22204 rptr += optlen; 22205 hdr_len -= optlen; 22206 } 22207 /* 22208 * Make sure that we drop an even number of words by filling 22209 * with EOL to the next word boundary. 22210 */ 22211 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 22212 hdr_len & 0x3; hdr_len++) 22213 *up++ = IPOPT_EOL; 22214 mp->b_wptr = up; 22215 /* Update header length */ 22216 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 22217 return (mp); 22218 } 22219 22220 /* 22221 * Delivery to local recipients including fanout to multiple recipients. 22222 * Does not do checksumming of UDP/TCP. 22223 * Note: q should be the read side queue for either the ill or conn. 22224 * Note: rq should be the read side q for the lower (ill) stream. 22225 * We don't send packets to IPPF processing, thus the last argument 22226 * to all the fanout calls are B_FALSE. 22227 */ 22228 void 22229 ip_wput_local(queue_t *q, ill_t *ill, ipha_t *ipha, mblk_t *mp, ire_t *ire, 22230 int fanout_flags, zoneid_t zoneid) 22231 { 22232 uint32_t protocol; 22233 mblk_t *first_mp; 22234 boolean_t mctl_present; 22235 int ire_type; 22236 #define rptr ((uchar_t *)ipha) 22237 22238 TRACE_1(TR_FAC_IP, TR_IP_WPUT_LOCAL_START, 22239 "ip_wput_local_start: q %p", q); 22240 22241 if (ire != NULL) { 22242 ire_type = ire->ire_type; 22243 } else { 22244 /* 22245 * Only ip_multicast_loopback() calls us with a NULL ire. If the 22246 * packet is not multicast, we can't tell the ire type. 22247 */ 22248 ASSERT(CLASSD(ipha->ipha_dst)); 22249 ire_type = IRE_BROADCAST; 22250 } 22251 22252 first_mp = mp; 22253 if (first_mp->b_datap->db_type == M_CTL) { 22254 ipsec_out_t *io = (ipsec_out_t *)first_mp->b_rptr; 22255 if (!io->ipsec_out_secure) { 22256 /* 22257 * This ipsec_out_t was allocated in ip_wput 22258 * for multicast packets to store the ill_index. 22259 * As this is being delivered locally, we don't 22260 * need this anymore. 22261 */ 22262 mp = first_mp->b_cont; 22263 freeb(first_mp); 22264 first_mp = mp; 22265 mctl_present = B_FALSE; 22266 } else { 22267 mctl_present = B_TRUE; 22268 mp = first_mp->b_cont; 22269 ASSERT(mp != NULL); 22270 ipsec_out_to_in(first_mp); 22271 } 22272 } else { 22273 mctl_present = B_FALSE; 22274 } 22275 22276 loopback_packets++; 22277 22278 ip2dbg(("ip_wput_local: from 0x%x to 0x%x in zone %d\n", 22279 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), zoneid)); 22280 if (!IS_SIMPLE_IPH(ipha)) { 22281 ip_wput_local_options(ipha); 22282 } 22283 22284 protocol = ipha->ipha_protocol; 22285 switch (protocol) { 22286 case IPPROTO_ICMP: { 22287 ire_t *ire_zone; 22288 ilm_t *ilm; 22289 mblk_t *mp1; 22290 zoneid_t last_zoneid; 22291 22292 if (CLASSD(ipha->ipha_dst) && 22293 !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 22294 ASSERT(ire_type == IRE_BROADCAST); 22295 /* 22296 * In the multicast case, applications may have joined 22297 * the group from different zones, so we need to deliver 22298 * the packet to each of them. Loop through the 22299 * multicast memberships structures (ilm) on the receive 22300 * ill and send a copy of the packet up each matching 22301 * one. However, we don't do this for multicasts sent on 22302 * the loopback interface (PHYI_LOOPBACK flag set) as 22303 * they must stay in the sender's zone. 22304 * 22305 * ilm_add_v6() ensures that ilms in the same zone are 22306 * contiguous in the ill_ilm list. We use this property 22307 * to avoid sending duplicates needed when two 22308 * applications in the same zone join the same group on 22309 * different logical interfaces: we ignore the ilm if 22310 * its zoneid is the same as the last matching one. 22311 * In addition, the sending of the packet for 22312 * ire_zoneid is delayed until all of the other ilms 22313 * have been exhausted. 22314 */ 22315 last_zoneid = -1; 22316 ILM_WALKER_HOLD(ill); 22317 for (ilm = ill->ill_ilm; ilm != NULL; 22318 ilm = ilm->ilm_next) { 22319 if ((ilm->ilm_flags & ILM_DELETED) || 22320 ipha->ipha_dst != ilm->ilm_addr || 22321 ilm->ilm_zoneid == last_zoneid || 22322 ilm->ilm_zoneid == zoneid || 22323 !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) 22324 continue; 22325 mp1 = ip_copymsg(first_mp); 22326 if (mp1 == NULL) 22327 continue; 22328 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 22329 mctl_present, B_FALSE, ill, 22330 ilm->ilm_zoneid); 22331 last_zoneid = ilm->ilm_zoneid; 22332 } 22333 ILM_WALKER_RELE(ill); 22334 /* 22335 * Loopback case: the sending endpoint has 22336 * IP_MULTICAST_LOOP disabled, therefore we don't 22337 * dispatch the multicast packet to the sending zone. 22338 */ 22339 if (fanout_flags & IP_FF_NO_MCAST_LOOP) { 22340 freemsg(first_mp); 22341 return; 22342 } 22343 } else if (ire_type == IRE_BROADCAST) { 22344 /* 22345 * In the broadcast case, there may be many zones 22346 * which need a copy of the packet delivered to them. 22347 * There is one IRE_BROADCAST per broadcast address 22348 * and per zone; we walk those using a helper function. 22349 * In addition, the sending of the packet for zoneid is 22350 * delayed until all of the other ires have been 22351 * processed. 22352 */ 22353 IRB_REFHOLD(ire->ire_bucket); 22354 ire_zone = NULL; 22355 while ((ire_zone = ire_get_next_bcast_ire(ire_zone, 22356 ire)) != NULL) { 22357 mp1 = ip_copymsg(first_mp); 22358 if (mp1 == NULL) 22359 continue; 22360 22361 UPDATE_IB_PKT_COUNT(ire_zone); 22362 ire_zone->ire_last_used_time = lbolt; 22363 icmp_inbound(q, mp1, B_TRUE, ill, 0, 0, 22364 mctl_present, B_FALSE, ill, 22365 ire_zone->ire_zoneid); 22366 } 22367 IRB_REFRELE(ire->ire_bucket); 22368 } 22369 icmp_inbound(q, first_mp, (ire_type == IRE_BROADCAST), ill, 0, 22370 0, mctl_present, B_FALSE, ill, zoneid); 22371 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 22372 "ip_wput_local_end: q %p (%S)", 22373 q, "icmp"); 22374 return; 22375 } 22376 case IPPROTO_IGMP: 22377 if (igmp_input(q, mp, ill)) { 22378 /* Bad packet - discarded by igmp_input */ 22379 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 22380 "ip_wput_local_end: q %p (%S)", 22381 q, "igmp_input--bad packet"); 22382 if (mctl_present) 22383 freeb(first_mp); 22384 return; 22385 } 22386 /* 22387 * igmp_input() may have pulled up the message so ipha needs to 22388 * be reinitialized. 22389 */ 22390 ipha = (ipha_t *)mp->b_rptr; 22391 /* deliver to local raw users */ 22392 break; 22393 case IPPROTO_ENCAP: 22394 /* 22395 * This case is covered by either ip_fanout_proto, or by 22396 * the above security processing for self-tunneled packets. 22397 */ 22398 break; 22399 case IPPROTO_UDP: { 22400 uint16_t *up; 22401 uint32_t ports; 22402 22403 up = (uint16_t *)(rptr + IPH_HDR_LENGTH(ipha) + 22404 UDP_PORTS_OFFSET); 22405 /* Force a 'valid' checksum. */ 22406 up[3] = 0; 22407 22408 ports = *(uint32_t *)up; 22409 ip_fanout_udp(q, first_mp, ill, ipha, ports, 22410 (ire_type == IRE_BROADCAST), 22411 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 22412 IP_FF_SEND_SLLA | IP_FF_IP6INFO, mctl_present, B_FALSE, 22413 ill, zoneid); 22414 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 22415 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_udp"); 22416 return; 22417 } 22418 case IPPROTO_TCP: { 22419 22420 /* 22421 * For TCP, discard broadcast packets. 22422 */ 22423 if ((ushort_t)ire_type == IRE_BROADCAST) { 22424 freemsg(first_mp); 22425 BUMP_MIB(&ip_mib, ipInDiscards); 22426 return; 22427 } 22428 22429 if (mp->b_datap->db_type == M_DATA) { 22430 /* 22431 * M_DATA mblk, so init mblk (chain) for no struio(). 22432 */ 22433 mblk_t *mp1 = mp; 22434 22435 do 22436 mp1->b_datap->db_struioflag = 0; 22437 while ((mp1 = mp1->b_cont) != NULL); 22438 } 22439 ASSERT((rptr + IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET + 4) 22440 <= mp->b_wptr); 22441 ip_fanout_tcp(q, first_mp, ill, ipha, 22442 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 22443 IP_FF_SYN_ADDIRE | IP_FF_IP6INFO, 22444 mctl_present, B_FALSE, zoneid); 22445 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 22446 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_tcp"); 22447 return; 22448 } 22449 case IPPROTO_SCTP: 22450 { 22451 uint32_t ports; 22452 22453 bcopy(rptr + IPH_HDR_LENGTH(ipha), &ports, sizeof (ports)); 22454 ip_fanout_sctp(first_mp, ill, ipha, ports, 22455 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | 22456 IP_FF_IP6INFO, 22457 mctl_present, B_FALSE, 0, zoneid); 22458 return; 22459 } 22460 22461 default: 22462 break; 22463 } 22464 /* 22465 * Find a client for some other protocol. We give 22466 * copies to multiple clients, if more than one is 22467 * bound. 22468 */ 22469 ip_fanout_proto(q, first_mp, ill, ipha, 22470 fanout_flags | IP_FF_SEND_ICMP | IP_FF_HDR_COMPLETE | IP_FF_RAWIP, 22471 mctl_present, B_FALSE, ill, zoneid); 22472 TRACE_2(TR_FAC_IP, TR_IP_WPUT_LOCAL_END, 22473 "ip_wput_local_end: q %p (%S)", q, "ip_fanout_proto"); 22474 #undef rptr 22475 } 22476 22477 /* 22478 * Update any source route, record route, or timestamp options. 22479 * Check that we are at end of strict source route. 22480 * The options have been sanity checked by ip_wput_options(). 22481 */ 22482 static void 22483 ip_wput_local_options(ipha_t *ipha) 22484 { 22485 ipoptp_t opts; 22486 uchar_t *opt; 22487 uint8_t optval; 22488 uint8_t optlen; 22489 ipaddr_t dst; 22490 uint32_t ts; 22491 ire_t *ire; 22492 timestruc_t now; 22493 22494 ip2dbg(("ip_wput_local_options\n")); 22495 for (optval = ipoptp_first(&opts, ipha); 22496 optval != IPOPT_EOL; 22497 optval = ipoptp_next(&opts)) { 22498 opt = opts.ipoptp_cur; 22499 optlen = opts.ipoptp_len; 22500 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 22501 switch (optval) { 22502 uint32_t off; 22503 case IPOPT_SSRR: 22504 case IPOPT_LSRR: 22505 off = opt[IPOPT_OFFSET]; 22506 off--; 22507 if (optlen < IP_ADDR_LEN || 22508 off > optlen - IP_ADDR_LEN) { 22509 /* End of source route */ 22510 break; 22511 } 22512 /* 22513 * This will only happen if two consecutive entries 22514 * in the source route contains our address or if 22515 * it is a packet with a loose source route which 22516 * reaches us before consuming the whole source route 22517 */ 22518 ip1dbg(("ip_wput_local_options: not end of SR\n")); 22519 if (optval == IPOPT_SSRR) { 22520 return; 22521 } 22522 /* 22523 * Hack: instead of dropping the packet truncate the 22524 * source route to what has been used by filling the 22525 * rest with IPOPT_NOP. 22526 */ 22527 opt[IPOPT_OLEN] = (uint8_t)off; 22528 while (off < optlen) { 22529 opt[off++] = IPOPT_NOP; 22530 } 22531 break; 22532 case IPOPT_RR: 22533 off = opt[IPOPT_OFFSET]; 22534 off--; 22535 if (optlen < IP_ADDR_LEN || 22536 off > optlen - IP_ADDR_LEN) { 22537 /* No more room - ignore */ 22538 ip1dbg(( 22539 "ip_wput_forward_options: end of RR\n")); 22540 break; 22541 } 22542 dst = htonl(INADDR_LOOPBACK); 22543 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 22544 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 22545 break; 22546 case IPOPT_TS: 22547 /* Insert timestamp if there is romm */ 22548 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 22549 case IPOPT_TS_TSONLY: 22550 off = IPOPT_TS_TIMELEN; 22551 break; 22552 case IPOPT_TS_PRESPEC: 22553 case IPOPT_TS_PRESPEC_RFC791: 22554 /* Verify that the address matched */ 22555 off = opt[IPOPT_OFFSET] - 1; 22556 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 22557 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, 22558 NULL, ALL_ZONES, MATCH_IRE_TYPE); 22559 if (ire == NULL) { 22560 /* Not for us */ 22561 break; 22562 } 22563 ire_refrele(ire); 22564 /* FALLTHRU */ 22565 case IPOPT_TS_TSANDADDR: 22566 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 22567 break; 22568 default: 22569 /* 22570 * ip_*put_options should have already 22571 * dropped this packet. 22572 */ 22573 cmn_err(CE_PANIC, "ip_wput_local_options: " 22574 "unknown IT - bug in ip_wput_options?\n"); 22575 return; /* Keep "lint" happy */ 22576 } 22577 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 22578 /* Increase overflow counter */ 22579 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 22580 opt[IPOPT_POS_OV_FLG] = (uint8_t) 22581 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 22582 (off << 4); 22583 break; 22584 } 22585 off = opt[IPOPT_OFFSET] - 1; 22586 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 22587 case IPOPT_TS_PRESPEC: 22588 case IPOPT_TS_PRESPEC_RFC791: 22589 case IPOPT_TS_TSANDADDR: 22590 dst = htonl(INADDR_LOOPBACK); 22591 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 22592 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 22593 /* FALLTHRU */ 22594 case IPOPT_TS_TSONLY: 22595 off = opt[IPOPT_OFFSET] - 1; 22596 /* Compute # of milliseconds since midnight */ 22597 gethrestime(&now); 22598 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 22599 now.tv_nsec / (NANOSEC / MILLISEC); 22600 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 22601 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 22602 break; 22603 } 22604 break; 22605 } 22606 } 22607 } 22608 22609 /* 22610 * Send out a multicast packet on interface ipif. 22611 * The sender does not have an conn. 22612 * Caller verifies that this isn't a PHYI_LOOPBACK. 22613 */ 22614 void 22615 ip_wput_multicast(queue_t *q, mblk_t *mp, ipif_t *ipif) 22616 { 22617 ipha_t *ipha; 22618 ire_t *ire; 22619 ipaddr_t dst; 22620 mblk_t *first_mp; 22621 22622 /* igmp_sendpkt always allocates a ipsec_out_t */ 22623 ASSERT(mp->b_datap->db_type == M_CTL); 22624 ASSERT(!ipif->ipif_isv6); 22625 ASSERT(!(ipif->ipif_ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)); 22626 22627 first_mp = mp; 22628 mp = first_mp->b_cont; 22629 ASSERT(mp->b_datap->db_type == M_DATA); 22630 ipha = (ipha_t *)mp->b_rptr; 22631 22632 /* 22633 * Find an IRE which matches the destination and the outgoing 22634 * queue (i.e. the outgoing interface.) 22635 */ 22636 if (ipif->ipif_flags & IPIF_POINTOPOINT) 22637 dst = ipif->ipif_pp_dst_addr; 22638 else 22639 dst = ipha->ipha_dst; 22640 /* 22641 * The source address has already been initialized by the 22642 * caller and hence matching on ILL (MATCH_IRE_ILL) would 22643 * be sufficient rather than MATCH_IRE_IPIF. 22644 * 22645 * This function is used for sending IGMP packets. We need 22646 * to make sure that we send the packet out of the interface 22647 * (ipif->ipif_ill) where we joined the group. This is to 22648 * prevent from switches doing IGMP snooping to send us multicast 22649 * packets for a given group on the interface we have joined. 22650 * If we can't find an ire, igmp_sendpkt has already initialized 22651 * ipsec_out_attach_if so that this will not be load spread in 22652 * ip_newroute_ipif. 22653 */ 22654 ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MATCH_IRE_ILL); 22655 if (!ire) { 22656 /* 22657 * Mark this packet to make it be delivered to 22658 * ip_wput_ire after the new ire has been 22659 * created. 22660 */ 22661 mp->b_prev = NULL; 22662 mp->b_next = NULL; 22663 ip_newroute_ipif(q, first_mp, ipif, dst, NULL, RTF_SETSRC); 22664 return; 22665 } 22666 22667 /* 22668 * Honor the RTF_SETSRC flag; this is the only case 22669 * where we force this addr whatever the current src addr is, 22670 * because this address is set by igmp_sendpkt(), and 22671 * cannot be specified by any user. 22672 */ 22673 if (ire->ire_flags & RTF_SETSRC) { 22674 ipha->ipha_src = ire->ire_src_addr; 22675 } 22676 22677 ip_wput_ire(q, first_mp, ire, NULL, B_FALSE); 22678 } 22679 22680 /* 22681 * NOTE : This function does not ire_refrele the ire argument passed in. 22682 * 22683 * Copy the link layer header and do IPQoS if needed. Frees the mblk on 22684 * failure. The ire_fp_mp can vanish any time in the case of IRE_MIPRTUN 22685 * and IRE_BROADCAST due to DL_NOTE_FASTPATH_FLUSH. Hence we have to hold 22686 * the ire_lock to access the ire_fp_mp in this case. 22687 * IPQoS assumes that the first M_DATA contains the IP header. So, if we are 22688 * prepending a fastpath message IPQoS processing must precede it, we also set 22689 * the b_band of the fastpath message to that of the mblk returned by IPQoS 22690 * (IPQoS might have set the b_band for CoS marking). 22691 * However, if we are prepending DL_UNITDATA_REQ message, IPQoS processing 22692 * must follow it so that IPQoS can mark the dl_priority field for CoS 22693 * marking, if needed. 22694 */ 22695 static mblk_t * 22696 ip_wput_attach_llhdr(mblk_t *mp, ire_t *ire, ip_proc_t proc, uint32_t ill_index) 22697 { 22698 uint_t hlen; 22699 ipha_t *ipha; 22700 mblk_t *mp1; 22701 boolean_t qos_done = B_FALSE; 22702 uchar_t *ll_hdr; 22703 22704 #define rptr ((uchar_t *)ipha) 22705 22706 ipha = (ipha_t *)mp->b_rptr; 22707 hlen = 0; 22708 LOCK_IRE_FP_MP(ire); 22709 if ((mp1 = ire->ire_fp_mp) != NULL) { 22710 ASSERT(DB_TYPE(mp1) == M_DATA); 22711 /* Initiate IPPF processing */ 22712 if ((proc != 0) && IPP_ENABLED(proc)) { 22713 UNLOCK_IRE_FP_MP(ire); 22714 ip_process(proc, &mp, ill_index); 22715 if (mp == NULL) 22716 return (NULL); 22717 22718 ipha = (ipha_t *)mp->b_rptr; 22719 LOCK_IRE_FP_MP(ire); 22720 if ((mp1 = ire->ire_fp_mp) == NULL) { 22721 qos_done = B_TRUE; 22722 goto no_fp_mp; 22723 } 22724 ASSERT(DB_TYPE(mp1) == M_DATA); 22725 } 22726 hlen = MBLKL(mp1); 22727 /* 22728 * Check if we have enough room to prepend fastpath 22729 * header 22730 */ 22731 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 22732 ll_hdr = rptr - hlen; 22733 bcopy(mp1->b_rptr, ll_hdr, hlen); 22734 /* XXX ipha is not aligned here */ 22735 ipha = (ipha_t *)(rptr - hlen); 22736 /* 22737 * Set the b_rptr to the start of the link layer 22738 * header 22739 */ 22740 mp->b_rptr = rptr; 22741 mp1 = mp; 22742 } else { 22743 mp1 = copyb(mp1); 22744 if (mp1 == NULL) 22745 goto unlock_err; 22746 mp1->b_band = mp->b_band; 22747 mp1->b_cont = mp; 22748 /* 22749 * XXX disable ICK_VALID and compute checksum 22750 * here; can happen if ire_fp_mp changes and 22751 * it can't be copied now due to insufficient 22752 * space. (unlikely, fp mp can change, but it 22753 * does not increase in length) 22754 */ 22755 } 22756 UNLOCK_IRE_FP_MP(ire); 22757 } else { 22758 no_fp_mp: 22759 mp1 = copyb(ire->ire_dlureq_mp); 22760 if (mp1 == NULL) { 22761 unlock_err: 22762 UNLOCK_IRE_FP_MP(ire); 22763 freemsg(mp); 22764 return (NULL); 22765 } 22766 UNLOCK_IRE_FP_MP(ire); 22767 mp1->b_cont = mp; 22768 if (!qos_done && (proc != 0) && IPP_ENABLED(proc)) { 22769 ip_process(proc, &mp1, ill_index); 22770 if (mp1 == NULL) 22771 return (NULL); 22772 } 22773 } 22774 return (mp1); 22775 #undef rptr 22776 } 22777 22778 /* 22779 * Finish the outbound IPsec processing for an IPv6 packet. This function 22780 * is called from ipsec_out_process() if the IPsec packet was processed 22781 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 22782 * asynchronously. 22783 */ 22784 void 22785 ip_wput_ipsec_out_v6(queue_t *q, mblk_t *ipsec_mp, ip6_t *ip6h, ill_t *ill, 22786 ire_t *ire_arg) 22787 { 22788 in6_addr_t *v6dstp; 22789 ire_t *ire; 22790 mblk_t *mp; 22791 uint_t ill_index; 22792 ipsec_out_t *io; 22793 boolean_t attach_if, hwaccel; 22794 uint32_t flags = IP6_NO_IPPOLICY; 22795 int match_flags; 22796 zoneid_t zoneid; 22797 boolean_t ill_need_rele = B_FALSE; 22798 boolean_t ire_need_rele = B_FALSE; 22799 22800 mp = ipsec_mp->b_cont; 22801 io = (ipsec_out_t *)ipsec_mp->b_rptr; 22802 ill_index = io->ipsec_out_ill_index; 22803 if (io->ipsec_out_reachable) { 22804 flags |= IPV6_REACHABILITY_CONFIRMATION; 22805 } 22806 attach_if = io->ipsec_out_attach_if; 22807 hwaccel = io->ipsec_out_accelerated; 22808 zoneid = io->ipsec_out_zoneid; 22809 ASSERT(zoneid != ALL_ZONES); 22810 match_flags = MATCH_IRE_ILL_GROUP; 22811 /* Multicast addresses should have non-zero ill_index. */ 22812 v6dstp = &ip6h->ip6_dst; 22813 ASSERT(ip6h->ip6_nxt != IPPROTO_RAW); 22814 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp) || ill_index != 0); 22815 ASSERT(!attach_if || ill_index != 0); 22816 if (ill_index != 0) { 22817 if (ill == NULL) { 22818 ill = ip_grab_attach_ill(NULL, ipsec_mp, ill_index, 22819 B_TRUE); 22820 22821 /* Failure case frees things for us. */ 22822 if (ill == NULL) 22823 return; 22824 22825 ill_need_rele = B_TRUE; 22826 } 22827 /* 22828 * If this packet needs to go out on a particular interface 22829 * honor it. 22830 */ 22831 if (attach_if) { 22832 match_flags = MATCH_IRE_ILL; 22833 22834 /* 22835 * Check if we need an ire that will not be 22836 * looked up by anybody else i.e. HIDDEN. 22837 */ 22838 if (ill_is_probeonly(ill)) { 22839 match_flags |= MATCH_IRE_MARK_HIDDEN; 22840 } 22841 } 22842 } 22843 ASSERT(mp != NULL); 22844 22845 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 22846 boolean_t unspec_src; 22847 ipif_t *ipif; 22848 22849 /* 22850 * Use the ill_index to get the right ill. 22851 */ 22852 unspec_src = io->ipsec_out_unspec_src; 22853 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 22854 if (ipif == NULL) { 22855 if (ill_need_rele) 22856 ill_refrele(ill); 22857 freemsg(ipsec_mp); 22858 return; 22859 } 22860 22861 if (ire_arg != NULL) { 22862 ire = ire_arg; 22863 } else { 22864 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 22865 zoneid, match_flags); 22866 ire_need_rele = B_TRUE; 22867 } 22868 if (ire != NULL) { 22869 ipif_refrele(ipif); 22870 /* 22871 * XXX Do the multicast forwarding now, as the IPSEC 22872 * processing has been done. 22873 */ 22874 goto send; 22875 } 22876 22877 ip0dbg(("ip_wput_ipsec_out_v6: multicast: IRE disappeared\n")); 22878 mp->b_prev = NULL; 22879 mp->b_next = NULL; 22880 22881 /* 22882 * If the IPsec packet was processed asynchronously, 22883 * drop it now. 22884 */ 22885 if (q == NULL) { 22886 if (ill_need_rele) 22887 ill_refrele(ill); 22888 freemsg(ipsec_mp); 22889 return; 22890 } 22891 22892 ip_newroute_ipif_v6(q, ipsec_mp, ipif, *v6dstp, 22893 unspec_src, zoneid); 22894 ipif_refrele(ipif); 22895 } else { 22896 if (attach_if) { 22897 ipif_t *ipif; 22898 22899 ipif = ipif_get_next_ipif(NULL, ill); 22900 if (ipif == NULL) { 22901 if (ill_need_rele) 22902 ill_refrele(ill); 22903 freemsg(ipsec_mp); 22904 return; 22905 } 22906 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ipif, 22907 zoneid, match_flags); 22908 ire_need_rele = B_TRUE; 22909 ipif_refrele(ipif); 22910 } else { 22911 if (ire_arg != NULL) { 22912 ire = ire_arg; 22913 } else { 22914 ire = ire_cache_lookup_v6(v6dstp, zoneid); 22915 ire_need_rele = B_TRUE; 22916 } 22917 } 22918 if (ire != NULL) 22919 goto send; 22920 /* 22921 * ire disappeared underneath. 22922 * 22923 * What we need to do here is the ip_newroute 22924 * logic to get the ire without doing the IPSEC 22925 * processing. Follow the same old path. But this 22926 * time, ip_wput or ire_add_then_send will call us 22927 * directly as all the IPSEC operations are done. 22928 */ 22929 ip1dbg(("ip_wput_ipsec_out_v6: IRE disappeared\n")); 22930 mp->b_prev = NULL; 22931 mp->b_next = NULL; 22932 22933 /* 22934 * If the IPsec packet was processed asynchronously, 22935 * drop it now. 22936 */ 22937 if (q == NULL) { 22938 if (ill_need_rele) 22939 ill_refrele(ill); 22940 freemsg(ipsec_mp); 22941 return; 22942 } 22943 22944 ip_newroute_v6(q, ipsec_mp, v6dstp, &ip6h->ip6_src, ill, 22945 zoneid); 22946 } 22947 if (ill != NULL && ill_need_rele) 22948 ill_refrele(ill); 22949 return; 22950 send: 22951 if (ill != NULL && ill_need_rele) 22952 ill_refrele(ill); 22953 22954 /* Local delivery */ 22955 if (ire->ire_stq == NULL) { 22956 ASSERT(q != NULL); 22957 ip_wput_local_v6(RD(q), ire->ire_ipif->ipif_ill, ip6h, ipsec_mp, 22958 ire, 0); 22959 if (ire_need_rele) 22960 ire_refrele(ire); 22961 return; 22962 } 22963 /* 22964 * Everything is done. Send it out on the wire. 22965 * We force the insertion of a fragment header using the 22966 * IPH_FRAG_HDR flag in two cases: 22967 * - after reception of an ICMPv6 "packet too big" message 22968 * with a MTU < 1280 (cf. RFC 2460 section 5) 22969 * - for multirouted IPv6 packets, so that the receiver can 22970 * discard duplicates according to their fragment identifier 22971 */ 22972 /* XXX fix flow control problems. */ 22973 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > ire->ire_max_frag || 22974 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 22975 if (hwaccel) { 22976 /* 22977 * hardware acceleration does not handle these 22978 * "slow path" cases. 22979 */ 22980 /* IPsec KSTATS: should bump bean counter here. */ 22981 if (ire_need_rele) 22982 ire_refrele(ire); 22983 freemsg(ipsec_mp); 22984 return; 22985 } 22986 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 22987 (mp->b_cont ? msgdsize(mp) : 22988 mp->b_wptr - (uchar_t *)ip6h)) { 22989 /* IPsec KSTATS: should bump bean counter here. */ 22990 ip0dbg(("Packet length mismatch: %d, %ld\n", 22991 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 22992 msgdsize(mp))); 22993 if (ire_need_rele) 22994 ire_refrele(ire); 22995 freemsg(ipsec_mp); 22996 return; 22997 } 22998 ASSERT(mp->b_prev == NULL); 22999 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 23000 ntohs(ip6h->ip6_plen) + 23001 IPV6_HDR_LEN, ire->ire_max_frag)); 23002 ip_wput_frag_v6(mp, ire, flags, NULL, B_FALSE, 23003 ire->ire_max_frag); 23004 } else { 23005 UPDATE_OB_PKT_COUNT(ire); 23006 ire->ire_last_used_time = lbolt; 23007 ip_xmit_v6(mp, ire, flags, NULL, B_FALSE, hwaccel ? io : NULL); 23008 } 23009 if (ire_need_rele) 23010 ire_refrele(ire); 23011 freeb(ipsec_mp); 23012 } 23013 23014 void 23015 ipsec_hw_putnext(queue_t *q, mblk_t *mp) 23016 { 23017 mblk_t *hada_mp; /* attributes M_CTL mblk */ 23018 da_ipsec_t *hada; /* data attributes */ 23019 ill_t *ill = (ill_t *)q->q_ptr; 23020 23021 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_hw_putnext: accelerated packet\n")); 23022 23023 if ((ill->ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP)) == 0) { 23024 /* IPsec KSTATS: Bump lose counter here! */ 23025 freemsg(mp); 23026 return; 23027 } 23028 23029 /* 23030 * It's an IPsec packet that must be 23031 * accelerated by the Provider, and the 23032 * outbound ill is IPsec acceleration capable. 23033 * Prepends the mblk with an IPHADA_M_CTL, and ship it 23034 * to the ill. 23035 * IPsec KSTATS: should bump packet counter here. 23036 */ 23037 23038 hada_mp = allocb(sizeof (da_ipsec_t), BPRI_HI); 23039 if (hada_mp == NULL) { 23040 /* IPsec KSTATS: should bump packet counter here. */ 23041 freemsg(mp); 23042 return; 23043 } 23044 23045 hada_mp->b_datap->db_type = M_CTL; 23046 hada_mp->b_wptr = hada_mp->b_rptr + sizeof (*hada); 23047 hada_mp->b_cont = mp; 23048 23049 hada = (da_ipsec_t *)hada_mp->b_rptr; 23050 bzero(hada, sizeof (da_ipsec_t)); 23051 hada->da_type = IPHADA_M_CTL; 23052 23053 putnext(q, hada_mp); 23054 } 23055 23056 /* 23057 * Finish the outbound IPsec processing. This function is called from 23058 * ipsec_out_process() if the IPsec packet was processed 23059 * synchronously, or from {ah,esp}_kcf_callback() if it was processed 23060 * asynchronously. 23061 */ 23062 void 23063 ip_wput_ipsec_out(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, ill_t *ill, 23064 ire_t *ire_arg) 23065 { 23066 uint32_t v_hlen_tos_len; 23067 ipaddr_t dst; 23068 ipif_t *ipif = NULL; 23069 ire_t *ire; 23070 ire_t *ire1 = NULL; 23071 mblk_t *next_mp = NULL; 23072 uint32_t max_frag; 23073 boolean_t multirt_send = B_FALSE; 23074 mblk_t *mp; 23075 mblk_t *mp1; 23076 uint_t ill_index; 23077 ipsec_out_t *io; 23078 boolean_t attach_if; 23079 int match_flags, offset; 23080 irb_t *irb = NULL; 23081 boolean_t ill_need_rele = B_FALSE, ire_need_rele = B_TRUE; 23082 zoneid_t zoneid; 23083 uint32_t cksum; 23084 uint16_t *up; 23085 #ifdef _BIG_ENDIAN 23086 #define LENGTH (v_hlen_tos_len & 0xFFFF) 23087 #else 23088 #define LENGTH ((v_hlen_tos_len >> 24) | ((v_hlen_tos_len >> 8) & 0xFF00)) 23089 #endif 23090 23091 mp = ipsec_mp->b_cont; 23092 ASSERT(mp != NULL); 23093 v_hlen_tos_len = ((uint32_t *)ipha)[0]; 23094 dst = ipha->ipha_dst; 23095 23096 io = (ipsec_out_t *)ipsec_mp->b_rptr; 23097 ill_index = io->ipsec_out_ill_index; 23098 attach_if = io->ipsec_out_attach_if; 23099 zoneid = io->ipsec_out_zoneid; 23100 ASSERT(zoneid != ALL_ZONES); 23101 match_flags = MATCH_IRE_ILL_GROUP; 23102 if (ill_index != 0) { 23103 if (ill == NULL) { 23104 ill = ip_grab_attach_ill(NULL, ipsec_mp, 23105 ill_index, B_FALSE); 23106 23107 /* Failure case frees things for us. */ 23108 if (ill == NULL) 23109 return; 23110 23111 ill_need_rele = B_TRUE; 23112 } 23113 /* 23114 * If this packet needs to go out on a particular interface 23115 * honor it. 23116 */ 23117 if (attach_if) { 23118 match_flags = MATCH_IRE_ILL; 23119 23120 /* 23121 * Check if we need an ire that will not be 23122 * looked up by anybody else i.e. HIDDEN. 23123 */ 23124 if (ill_is_probeonly(ill)) { 23125 match_flags |= MATCH_IRE_MARK_HIDDEN; 23126 } 23127 } 23128 } 23129 23130 if (CLASSD(dst)) { 23131 boolean_t conn_dontroute; 23132 /* 23133 * Use the ill_index to get the right ipif. 23134 */ 23135 conn_dontroute = io->ipsec_out_dontroute; 23136 if (ill_index == 0) 23137 ipif = ipif_lookup_group(dst, zoneid); 23138 else 23139 (void) ipif_lookup_zoneid(ill, zoneid, 0, &ipif); 23140 if (ipif == NULL) { 23141 ip1dbg(("ip_wput_ipsec_out: No ipif for" 23142 " multicast\n")); 23143 BUMP_MIB(&ip_mib, ipOutNoRoutes); 23144 freemsg(ipsec_mp); 23145 goto done; 23146 } 23147 /* 23148 * ipha_src has already been intialized with the 23149 * value of the ipif in ip_wput. All we need now is 23150 * an ire to send this downstream. 23151 */ 23152 ire = ire_ctable_lookup(dst, 0, 0, ipif, zoneid, match_flags); 23153 if (ire != NULL) { 23154 ill_t *ill1; 23155 /* 23156 * Do the multicast forwarding now, as the IPSEC 23157 * processing has been done. 23158 */ 23159 if (ip_g_mrouter && !conn_dontroute && 23160 (ill1 = ire_to_ill(ire))) { 23161 if (ip_mforward(ill1, ipha, mp)) { 23162 freemsg(ipsec_mp); 23163 ip1dbg(("ip_wput_ipsec_out: mforward " 23164 "failed\n")); 23165 ire_refrele(ire); 23166 goto done; 23167 } 23168 } 23169 goto send; 23170 } 23171 23172 ip0dbg(("ip_wput_ipsec_out: multicast: IRE disappeared\n")); 23173 mp->b_prev = NULL; 23174 mp->b_next = NULL; 23175 23176 /* 23177 * If the IPsec packet was processed asynchronously, 23178 * drop it now. 23179 */ 23180 if (q == NULL) { 23181 freemsg(ipsec_mp); 23182 goto done; 23183 } 23184 23185 /* 23186 * We may be using a wrong ipif to create the ire. 23187 * But it is okay as the source address is assigned 23188 * for the packet already. Next outbound packet would 23189 * create the IRE with the right IPIF in ip_wput. 23190 * 23191 * Also handle RTF_MULTIRT routes. 23192 */ 23193 ip_newroute_ipif(q, ipsec_mp, ipif, dst, NULL, RTF_MULTIRT); 23194 } else { 23195 if (attach_if) { 23196 ire = ire_ctable_lookup(dst, 0, 0, ill->ill_ipif, 23197 zoneid, match_flags); 23198 } else { 23199 if (ire_arg != NULL) { 23200 ire = ire_arg; 23201 ire_need_rele = B_FALSE; 23202 } else { 23203 ire = ire_cache_lookup(dst, zoneid); 23204 } 23205 } 23206 if (ire != NULL) { 23207 goto send; 23208 } 23209 23210 /* 23211 * ire disappeared underneath. 23212 * 23213 * What we need to do here is the ip_newroute 23214 * logic to get the ire without doing the IPSEC 23215 * processing. Follow the same old path. But this 23216 * time, ip_wput or ire_add_then_put will call us 23217 * directly as all the IPSEC operations are done. 23218 */ 23219 ip1dbg(("ip_wput_ipsec_out: IRE disappeared\n")); 23220 mp->b_prev = NULL; 23221 mp->b_next = NULL; 23222 23223 /* 23224 * If the IPsec packet was processed asynchronously, 23225 * drop it now. 23226 */ 23227 if (q == NULL) { 23228 freemsg(ipsec_mp); 23229 goto done; 23230 } 23231 23232 /* 23233 * Since we're going through ip_newroute() again, we 23234 * need to make sure we don't: 23235 * 23236 * 1.) Trigger the ASSERT() with the ipha_ident 23237 * overloading. 23238 * 2.) Redo transport-layer checksumming, since we've 23239 * already done all that to get this far. 23240 * 23241 * The easiest way not do either of the above is to set 23242 * the ipha_ident field to IP_HDR_INCLUDED. 23243 */ 23244 ipha->ipha_ident = IP_HDR_INCLUDED; 23245 ip_newroute(q, ipsec_mp, dst, NULL, 23246 (CONN_Q(q) ? Q_TO_CONN(q) : NULL)); 23247 } 23248 goto done; 23249 send: 23250 if (ipha->ipha_protocol == IPPROTO_UDP && udp_compute_checksum()) { 23251 /* 23252 * ESP NAT-Traversal packet. 23253 * 23254 * Just do software checksum for now. 23255 */ 23256 23257 offset = IP_SIMPLE_HDR_LENGTH + UDP_CHECKSUM_OFFSET; 23258 IP_STAT(ip_out_sw_cksum); 23259 IP_STAT_UPDATE(ip_udp_out_sw_cksum_bytes, 23260 ntohs(htons(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH)); 23261 #define iphs ((uint16_t *)ipha) 23262 cksum = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + 23263 iphs[9] + ntohs(htons(ipha->ipha_length) - 23264 IP_SIMPLE_HDR_LENGTH); 23265 #undef iphs 23266 if ((cksum = IP_CSUM(mp, IP_SIMPLE_HDR_LENGTH, cksum)) == 0) 23267 cksum = 0xFFFF; 23268 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) 23269 if (mp1->b_wptr - mp1->b_rptr >= 23270 offset + sizeof (uint16_t)) { 23271 up = (uint16_t *)(mp1->b_rptr + offset); 23272 *up = cksum; 23273 break; /* out of for loop */ 23274 } else { 23275 offset -= (mp->b_wptr - mp->b_rptr); 23276 } 23277 } /* Otherwise, just keep the all-zero checksum. */ 23278 23279 if (ire->ire_stq == NULL) { 23280 /* 23281 * Loopbacks go through ip_wput_local except for one case. 23282 * We come here if we generate a icmp_frag_needed message 23283 * after IPSEC processing is over. When this function calls 23284 * ip_wput_ire_fragmentit, ip_wput_frag might end up calling 23285 * icmp_frag_needed. The message generated comes back here 23286 * through icmp_frag_needed -> icmp_pkt -> ip_wput -> 23287 * ipsec_out_process -> ip_wput_ipsec_out. We need to set the 23288 * source address as it is usually set in ip_wput_ire. As 23289 * ipsec_out_proc_begin is set, ip_wput calls ipsec_out_process 23290 * and we end up here. We can't enter ip_wput_ire once the 23291 * IPSEC processing is over and hence we need to do it here. 23292 */ 23293 ASSERT(q != NULL); 23294 UPDATE_OB_PKT_COUNT(ire); 23295 ire->ire_last_used_time = lbolt; 23296 if (ipha->ipha_src == 0) 23297 ipha->ipha_src = ire->ire_src_addr; 23298 ip_wput_local(RD(q), ire->ire_ipif->ipif_ill, ipha, ipsec_mp, 23299 ire, 0, zoneid); 23300 if (ire_need_rele) 23301 ire_refrele(ire); 23302 goto done; 23303 } 23304 23305 if (ire->ire_max_frag < (unsigned int)LENGTH) { 23306 /* 23307 * We are through with IPSEC processing. 23308 * Fragment this and send it on the wire. 23309 */ 23310 if (io->ipsec_out_accelerated) { 23311 /* 23312 * The packet has been accelerated but must 23313 * be fragmented. This should not happen 23314 * since AH and ESP must not accelerate 23315 * packets that need fragmentation, however 23316 * the configuration could have changed 23317 * since the AH or ESP processing. 23318 * Drop packet. 23319 * IPsec KSTATS: bump bean counter here. 23320 */ 23321 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_wput_ipsec_out: " 23322 "fragmented accelerated packet!\n")); 23323 freemsg(ipsec_mp); 23324 } else { 23325 ip_wput_ire_fragmentit(ipsec_mp, ire); 23326 } 23327 if (ire_need_rele) 23328 ire_refrele(ire); 23329 goto done; 23330 } 23331 23332 ip2dbg(("ip_wput_ipsec_out: ipsec_mp %p, ire %p, ire_ipif %p, " 23333 "ipif %p\n", (void *)ipsec_mp, (void *)ire, 23334 (void *)ire->ire_ipif, (void *)ipif)); 23335 23336 /* 23337 * Multiroute the secured packet, unless IPsec really 23338 * requires the packet to go out only through a particular 23339 * interface. 23340 */ 23341 if ((ire->ire_flags & RTF_MULTIRT) && !attach_if) { 23342 ire_t *first_ire; 23343 irb = ire->ire_bucket; 23344 ASSERT(irb != NULL); 23345 /* 23346 * This ire has been looked up as the one that 23347 * goes through the given ipif; 23348 * make sure we do not omit any other multiroute ire 23349 * that may be present in the bucket before this one. 23350 */ 23351 IRB_REFHOLD(irb); 23352 for (first_ire = irb->irb_ire; 23353 first_ire != NULL; 23354 first_ire = first_ire->ire_next) { 23355 if ((first_ire->ire_flags & RTF_MULTIRT) && 23356 (first_ire->ire_addr == ire->ire_addr) && 23357 !(first_ire->ire_marks & 23358 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 23359 break; 23360 } 23361 23362 if ((first_ire != NULL) && (first_ire != ire)) { 23363 /* 23364 * Don't change the ire if the packet must 23365 * be fragmented if sent via this new one. 23366 */ 23367 if (first_ire->ire_max_frag >= (unsigned int)LENGTH) { 23368 IRE_REFHOLD(first_ire); 23369 if (ire_need_rele) 23370 ire_refrele(ire); 23371 else 23372 ire_need_rele = B_TRUE; 23373 ire = first_ire; 23374 } 23375 } 23376 IRB_REFRELE(irb); 23377 23378 multirt_send = B_TRUE; 23379 max_frag = ire->ire_max_frag; 23380 } else { 23381 if ((ire->ire_flags & RTF_MULTIRT) && attach_if) { 23382 ip1dbg(("ip_wput_ipsec_out: ignoring multirouting " 23383 "flag, attach_if %d\n", attach_if)); 23384 } 23385 } 23386 23387 /* 23388 * In most cases, the emission loop below is entered only once. 23389 * Only in the case where the ire holds the RTF_MULTIRT 23390 * flag, we loop to process all RTF_MULTIRT ires in the 23391 * bucket, and send the packet through all crossed 23392 * RTF_MULTIRT routes. 23393 */ 23394 do { 23395 if (multirt_send) { 23396 /* 23397 * ire1 holds here the next ire to process in the 23398 * bucket. If multirouting is expected, 23399 * any non-RTF_MULTIRT ire that has the 23400 * right destination address is ignored. 23401 */ 23402 ASSERT(irb != NULL); 23403 IRB_REFHOLD(irb); 23404 for (ire1 = ire->ire_next; 23405 ire1 != NULL; 23406 ire1 = ire1->ire_next) { 23407 if ((ire1->ire_flags & RTF_MULTIRT) == 0) 23408 continue; 23409 if (ire1->ire_addr != ire->ire_addr) 23410 continue; 23411 if (ire1->ire_marks & 23412 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN)) 23413 continue; 23414 /* No loopback here */ 23415 if (ire1->ire_stq == NULL) 23416 continue; 23417 /* 23418 * Ensure we do not exceed the MTU 23419 * of the next route. 23420 */ 23421 if (ire1->ire_max_frag < (unsigned int)LENGTH) { 23422 ip_multirt_bad_mtu(ire1, max_frag); 23423 continue; 23424 } 23425 23426 IRE_REFHOLD(ire1); 23427 break; 23428 } 23429 IRB_REFRELE(irb); 23430 if (ire1 != NULL) { 23431 /* 23432 * We are in a multiple send case, need to 23433 * make a copy of the packet. 23434 */ 23435 next_mp = copymsg(ipsec_mp); 23436 if (next_mp == NULL) { 23437 ire_refrele(ire1); 23438 ire1 = NULL; 23439 } 23440 } 23441 } 23442 23443 /* Everything is done. Send it out on the wire */ 23444 mp1 = ip_wput_attach_llhdr(mp, ire, 0, 0); 23445 if (mp1 == NULL) { 23446 BUMP_MIB(&ip_mib, ipOutDiscards); 23447 freemsg(ipsec_mp); 23448 if (ire_need_rele) 23449 ire_refrele(ire); 23450 if (ire1 != NULL) { 23451 ire_refrele(ire1); 23452 freemsg(next_mp); 23453 } 23454 goto done; 23455 } 23456 UPDATE_OB_PKT_COUNT(ire); 23457 ire->ire_last_used_time = lbolt; 23458 if (!io->ipsec_out_accelerated) { 23459 putnext(ire->ire_stq, mp1); 23460 } else { 23461 /* 23462 * Safety Pup says: make sure this is going to 23463 * the right interface! 23464 */ 23465 ill_t *ill1 = (ill_t *)ire->ire_stq->q_ptr; 23466 int ifindex = ill1->ill_phyint->phyint_ifindex; 23467 23468 if (ifindex != io->ipsec_out_capab_ill_index) { 23469 /* IPsec kstats: bump lose counter */ 23470 freemsg(mp1); 23471 } else { 23472 ipsec_hw_putnext(ire->ire_stq, mp1); 23473 } 23474 } 23475 23476 freeb(ipsec_mp); 23477 if (ire_need_rele) 23478 ire_refrele(ire); 23479 23480 if (ire1 != NULL) { 23481 ire = ire1; 23482 ire_need_rele = B_TRUE; 23483 ASSERT(next_mp); 23484 ipsec_mp = next_mp; 23485 mp = ipsec_mp->b_cont; 23486 ire1 = NULL; 23487 next_mp = NULL; 23488 io = (ipsec_out_t *)ipsec_mp->b_rptr; 23489 } else { 23490 multirt_send = B_FALSE; 23491 } 23492 } while (multirt_send); 23493 done: 23494 if (ill != NULL && ill_need_rele) 23495 ill_refrele(ill); 23496 if (ipif != NULL) 23497 ipif_refrele(ipif); 23498 } 23499 23500 /* 23501 * Get the ill corresponding to the specified ire, and compare its 23502 * capabilities with the protocol and algorithms specified by the 23503 * the SA obtained from ipsec_out. If they match, annotate the 23504 * ipsec_out structure to indicate that the packet needs acceleration. 23505 * 23506 * 23507 * A packet is eligible for outbound hardware acceleration if the 23508 * following conditions are satisfied: 23509 * 23510 * 1. the packet will not be fragmented 23511 * 2. the provider supports the algorithm 23512 * 3. there is no pending control message being exchanged 23513 * 4. snoop is not attached 23514 * 5. the destination address is not a broadcast or multicast address. 23515 * 23516 * Rationale: 23517 * - Hardware drivers do not support fragmentation with 23518 * the current interface. 23519 * - snoop, multicast, and broadcast may result in exposure of 23520 * a cleartext datagram. 23521 * We check all five of these conditions here. 23522 * 23523 * XXX would like to nuke "ire_t *" parameter here; problem is that 23524 * IRE is only way to figure out if a v4 address is a broadcast and 23525 * thus ineligible for acceleration... 23526 */ 23527 static void 23528 ipsec_out_is_accelerated(mblk_t *ipsec_mp, ipsa_t *sa, ill_t *ill, ire_t *ire) 23529 { 23530 ipsec_out_t *io; 23531 mblk_t *data_mp; 23532 uint_t plen, overhead; 23533 23534 if ((sa->ipsa_flags & IPSA_F_HW) == 0) 23535 return; 23536 23537 if (ill == NULL) 23538 return; 23539 23540 /* 23541 * Destination address is a broadcast or multicast. Punt. 23542 */ 23543 if ((ire != NULL) && (ire->ire_type & (IRE_BROADCAST|IRE_LOOPBACK| 23544 IRE_LOCAL))) 23545 return; 23546 23547 data_mp = ipsec_mp->b_cont; 23548 23549 if (ill->ill_isv6) { 23550 ip6_t *ip6h = (ip6_t *)data_mp->b_rptr; 23551 23552 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) 23553 return; 23554 23555 plen = ip6h->ip6_plen; 23556 } else { 23557 ipha_t *ipha = (ipha_t *)data_mp->b_rptr; 23558 23559 if (CLASSD(ipha->ipha_dst)) 23560 return; 23561 23562 plen = ipha->ipha_length; 23563 } 23564 /* 23565 * Is there a pending DLPI control message being exchanged 23566 * between IP/IPsec and the DLS Provider? If there is, it 23567 * could be a SADB update, and the state of the DLS Provider 23568 * SADB might not be in sync with the SADB maintained by 23569 * IPsec. To avoid dropping packets or using the wrong keying 23570 * material, we do not accelerate this packet. 23571 */ 23572 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 23573 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 23574 "ill_dlpi_pending! don't accelerate packet\n")); 23575 return; 23576 } 23577 23578 /* 23579 * Is the Provider in promiscous mode? If it does, we don't 23580 * accelerate the packet since it will bounce back up to the 23581 * listeners in the clear. 23582 */ 23583 if (ill->ill_promisc_on_phys) { 23584 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_out_check_is_accelerated: " 23585 "ill in promiscous mode, don't accelerate packet\n")); 23586 return; 23587 } 23588 23589 /* 23590 * Will the packet require fragmentation? 23591 */ 23592 23593 /* 23594 * IPsec ESP note: this is a pessimistic estimate, but the same 23595 * as is used elsewhere. 23596 * SPI + sequence + MAC + IV(blocksize) + padding(blocksize-1) 23597 * + 2-byte trailer 23598 */ 23599 overhead = (sa->ipsa_type == SADB_SATYPE_AH) ? IPSEC_MAX_AH_HDR_SIZE : 23600 IPSEC_BASE_ESP_HDR_SIZE(sa); 23601 23602 if ((plen + overhead) > ill->ill_max_mtu) 23603 return; 23604 23605 io = (ipsec_out_t *)ipsec_mp->b_rptr; 23606 23607 /* 23608 * Can the ill accelerate this IPsec protocol and algorithm 23609 * specified by the SA? 23610 */ 23611 if (!ipsec_capab_match(ill, io->ipsec_out_capab_ill_index, 23612 ill->ill_isv6, sa)) { 23613 return; 23614 } 23615 23616 /* 23617 * Tell AH or ESP that the outbound ill is capable of 23618 * accelerating this packet. 23619 */ 23620 io->ipsec_out_is_capab_ill = B_TRUE; 23621 } 23622 23623 /* 23624 * Select which AH & ESP SA's to use (if any) for the outbound packet. 23625 * 23626 * If this function returns B_TRUE, the requested SA's have been filled 23627 * into the ipsec_out_*_sa pointers. 23628 * 23629 * If the function returns B_FALSE, the packet has been "consumed", most 23630 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 23631 * 23632 * The SA references created by the protocol-specific "select" 23633 * function will be released when the ipsec_mp is freed, thanks to the 23634 * ipsec_out_free destructor -- see spd.c. 23635 */ 23636 static boolean_t 23637 ipsec_out_select_sa(mblk_t *ipsec_mp) 23638 { 23639 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 23640 ipsec_out_t *io; 23641 ipsec_policy_t *pp; 23642 ipsec_action_t *ap; 23643 io = (ipsec_out_t *)ipsec_mp->b_rptr; 23644 ASSERT(io->ipsec_out_type == IPSEC_OUT); 23645 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 23646 23647 if (!io->ipsec_out_secure) { 23648 /* 23649 * We came here by mistake. 23650 * Don't bother with ipsec processing 23651 * We should "discourage" this path in the future. 23652 */ 23653 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 23654 return (B_FALSE); 23655 } 23656 ASSERT(io->ipsec_out_need_policy == B_FALSE); 23657 ASSERT((io->ipsec_out_policy != NULL) || 23658 (io->ipsec_out_act != NULL)); 23659 23660 ASSERT(io->ipsec_out_failed == B_FALSE); 23661 23662 /* 23663 * IPSEC processing has started. 23664 */ 23665 io->ipsec_out_proc_begin = B_TRUE; 23666 ap = io->ipsec_out_act; 23667 if (ap == NULL) { 23668 pp = io->ipsec_out_policy; 23669 ASSERT(pp != NULL); 23670 ap = pp->ipsp_act; 23671 ASSERT(ap != NULL); 23672 } 23673 23674 /* 23675 * We have an action. now, let's select SA's. 23676 * (In the future, we can cache this in the conn_t..) 23677 */ 23678 if (ap->ipa_want_esp) { 23679 if (io->ipsec_out_esp_sa == NULL) { 23680 need_esp_acquire = !ipsec_outbound_sa(ipsec_mp, 23681 IPPROTO_ESP); 23682 } 23683 ASSERT(need_esp_acquire || io->ipsec_out_esp_sa != NULL); 23684 } 23685 23686 if (ap->ipa_want_ah) { 23687 if (io->ipsec_out_ah_sa == NULL) { 23688 need_ah_acquire = !ipsec_outbound_sa(ipsec_mp, 23689 IPPROTO_AH); 23690 } 23691 ASSERT(need_ah_acquire || io->ipsec_out_ah_sa != NULL); 23692 /* 23693 * The ESP and AH processing order needs to be preserved 23694 * when both protocols are required (ESP should be applied 23695 * before AH for an outbound packet). Force an ESP ACQUIRE 23696 * when both ESP and AH are required, and an AH ACQUIRE 23697 * is needed. 23698 */ 23699 if (ap->ipa_want_esp && need_ah_acquire) 23700 need_esp_acquire = B_TRUE; 23701 } 23702 23703 /* 23704 * Send an ACQUIRE (extended, regular, or both) if we need one. 23705 * Release SAs that got referenced, but will not be used until we 23706 * acquire _all_ of the SAs we need. 23707 */ 23708 if (need_ah_acquire || need_esp_acquire) { 23709 if (io->ipsec_out_ah_sa != NULL) { 23710 IPSA_REFRELE(io->ipsec_out_ah_sa); 23711 io->ipsec_out_ah_sa = NULL; 23712 } 23713 if (io->ipsec_out_esp_sa != NULL) { 23714 IPSA_REFRELE(io->ipsec_out_esp_sa); 23715 io->ipsec_out_esp_sa = NULL; 23716 } 23717 23718 sadb_acquire(ipsec_mp, io, need_ah_acquire, need_esp_acquire); 23719 return (B_FALSE); 23720 } 23721 23722 return (B_TRUE); 23723 } 23724 23725 /* 23726 * Process an IPSEC_OUT message and see what you can 23727 * do with it. 23728 * IPQoS Notes: 23729 * We do IPPF processing if IPP_LOCAL_OUT is enabled before processing for 23730 * IPSec. 23731 * XXX would like to nuke ire_t. 23732 * XXX ill_index better be "real" 23733 */ 23734 void 23735 ipsec_out_process(queue_t *q, mblk_t *ipsec_mp, ire_t *ire, uint_t ill_index) 23736 { 23737 ipsec_out_t *io; 23738 ipsec_policy_t *pp; 23739 ipsec_action_t *ap; 23740 ipha_t *ipha; 23741 ip6_t *ip6h; 23742 mblk_t *mp; 23743 ill_t *ill; 23744 zoneid_t zoneid; 23745 ipsec_status_t ipsec_rc; 23746 boolean_t ill_need_rele = B_FALSE; 23747 23748 io = (ipsec_out_t *)ipsec_mp->b_rptr; 23749 ASSERT(io->ipsec_out_type == IPSEC_OUT); 23750 ASSERT(io->ipsec_out_len == sizeof (ipsec_out_t)); 23751 mp = ipsec_mp->b_cont; 23752 23753 /* 23754 * Initiate IPPF processing. We do it here to account for packets 23755 * coming here that don't have any policy (i.e. !io->ipsec_out_secure). 23756 * We can check for ipsec_out_proc_begin even for such packets, as 23757 * they will always be false (asserted below). 23758 */ 23759 if (IPP_ENABLED(IPP_LOCAL_OUT) && !io->ipsec_out_proc_begin) { 23760 ip_process(IPP_LOCAL_OUT, &mp, io->ipsec_out_ill_index != 0 ? 23761 io->ipsec_out_ill_index : ill_index); 23762 if (mp == NULL) { 23763 ip2dbg(("ipsec_out_process: packet dropped "\ 23764 "during IPPF processing\n")); 23765 freeb(ipsec_mp); 23766 BUMP_MIB(&ip_mib, ipOutDiscards); 23767 return; 23768 } 23769 } 23770 23771 if (!io->ipsec_out_secure) { 23772 /* 23773 * We came here by mistake. 23774 * Don't bother with ipsec processing 23775 * Should "discourage" this path in the future. 23776 */ 23777 ASSERT(io->ipsec_out_proc_begin == B_FALSE); 23778 goto done; 23779 } 23780 ASSERT(io->ipsec_out_need_policy == B_FALSE); 23781 ASSERT((io->ipsec_out_policy != NULL) || 23782 (io->ipsec_out_act != NULL)); 23783 ASSERT(io->ipsec_out_failed == B_FALSE); 23784 23785 if (!ipsec_loaded()) { 23786 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 23787 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 23788 BUMP_MIB(&ip_mib, ipOutDiscards); 23789 } else { 23790 BUMP_MIB(&ip6_mib, ipv6OutDiscards); 23791 } 23792 ip_drop_packet(ipsec_mp, B_FALSE, NULL, ire, 23793 &ipdrops_ip_ipsec_not_loaded, &ip_dropper); 23794 return; 23795 } 23796 23797 /* 23798 * IPSEC processing has started. 23799 */ 23800 io->ipsec_out_proc_begin = B_TRUE; 23801 ap = io->ipsec_out_act; 23802 if (ap == NULL) { 23803 pp = io->ipsec_out_policy; 23804 ASSERT(pp != NULL); 23805 ap = pp->ipsp_act; 23806 ASSERT(ap != NULL); 23807 } 23808 23809 /* 23810 * Save the outbound ill index. When the packet comes back 23811 * from IPsec, we make sure the ill hasn't changed or disappeared 23812 * before sending it the accelerated packet. 23813 */ 23814 if ((ire != NULL) && (io->ipsec_out_capab_ill_index == 0)) { 23815 int ifindex; 23816 ill = ire_to_ill(ire); 23817 ifindex = ill->ill_phyint->phyint_ifindex; 23818 io->ipsec_out_capab_ill_index = ifindex; 23819 } 23820 23821 /* 23822 * The order of processing is first insert a IP header if needed. 23823 * Then insert the ESP header and then the AH header. 23824 */ 23825 if ((io->ipsec_out_se_done == B_FALSE) && 23826 (ap->ipa_want_se)) { 23827 /* 23828 * First get the outer IP header before sending 23829 * it to ESP. 23830 */ 23831 ipha_t *oipha, *iipha; 23832 mblk_t *outer_mp, *inner_mp; 23833 23834 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 23835 (void) mi_strlog(q, 0, SL_ERROR|SL_TRACE|SL_CONSOLE, 23836 "ipsec_out_process: " 23837 "Self-Encapsulation failed: Out of memory\n"); 23838 freemsg(ipsec_mp); 23839 BUMP_MIB(&ip_mib, ipOutDiscards); 23840 return; 23841 } 23842 inner_mp = ipsec_mp->b_cont; 23843 ASSERT(inner_mp->b_datap->db_type == M_DATA); 23844 oipha = (ipha_t *)outer_mp->b_rptr; 23845 iipha = (ipha_t *)inner_mp->b_rptr; 23846 *oipha = *iipha; 23847 outer_mp->b_wptr += sizeof (ipha_t); 23848 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 23849 sizeof (ipha_t)); 23850 oipha->ipha_protocol = IPPROTO_ENCAP; 23851 oipha->ipha_version_and_hdr_length = 23852 IP_SIMPLE_HDR_VERSION; 23853 oipha->ipha_hdr_checksum = 0; 23854 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 23855 outer_mp->b_cont = inner_mp; 23856 ipsec_mp->b_cont = outer_mp; 23857 23858 io->ipsec_out_se_done = B_TRUE; 23859 io->ipsec_out_encaps = B_TRUE; 23860 } 23861 23862 if (((ap->ipa_want_ah && (io->ipsec_out_ah_sa == NULL)) || 23863 (ap->ipa_want_esp && (io->ipsec_out_esp_sa == NULL))) && 23864 !ipsec_out_select_sa(ipsec_mp)) 23865 return; 23866 23867 /* 23868 * By now, we know what SA's to use. Toss over to ESP & AH 23869 * to do the heavy lifting. 23870 */ 23871 zoneid = io->ipsec_out_zoneid; 23872 ASSERT(zoneid != ALL_ZONES); 23873 if ((io->ipsec_out_esp_done == B_FALSE) && (ap->ipa_want_esp)) { 23874 ASSERT(io->ipsec_out_esp_sa != NULL); 23875 io->ipsec_out_esp_done = B_TRUE; 23876 /* 23877 * Note that since hw accel can only apply one transform, 23878 * not two, we skip hw accel for ESP if we also have AH 23879 * This is an design limitation of the interface 23880 * which should be revisited. 23881 */ 23882 ASSERT(ire != NULL); 23883 if (io->ipsec_out_ah_sa == NULL) { 23884 ill = (ill_t *)ire->ire_stq->q_ptr; 23885 ipsec_out_is_accelerated(ipsec_mp, 23886 io->ipsec_out_esp_sa, ill, ire); 23887 } 23888 23889 ipsec_rc = io->ipsec_out_esp_sa->ipsa_output_func(ipsec_mp); 23890 switch (ipsec_rc) { 23891 case IPSEC_STATUS_SUCCESS: 23892 break; 23893 case IPSEC_STATUS_FAILED: 23894 BUMP_MIB(&ip_mib, ipOutDiscards); 23895 /* FALLTHRU */ 23896 case IPSEC_STATUS_PENDING: 23897 return; 23898 } 23899 } 23900 23901 if ((io->ipsec_out_ah_done == B_FALSE) && (ap->ipa_want_ah)) { 23902 ASSERT(io->ipsec_out_ah_sa != NULL); 23903 io->ipsec_out_ah_done = B_TRUE; 23904 if (ire == NULL) { 23905 int idx = io->ipsec_out_capab_ill_index; 23906 ill = ill_lookup_on_ifindex(idx, B_FALSE, 23907 NULL, NULL, NULL, NULL); 23908 ill_need_rele = B_TRUE; 23909 } else { 23910 ill = (ill_t *)ire->ire_stq->q_ptr; 23911 } 23912 ipsec_out_is_accelerated(ipsec_mp, io->ipsec_out_ah_sa, ill, 23913 ire); 23914 23915 ipsec_rc = io->ipsec_out_ah_sa->ipsa_output_func(ipsec_mp); 23916 switch (ipsec_rc) { 23917 case IPSEC_STATUS_SUCCESS: 23918 break; 23919 case IPSEC_STATUS_FAILED: 23920 BUMP_MIB(&ip_mib, ipOutDiscards); 23921 /* FALLTHRU */ 23922 case IPSEC_STATUS_PENDING: 23923 if (ill != NULL && ill_need_rele) 23924 ill_refrele(ill); 23925 return; 23926 } 23927 } 23928 /* 23929 * We are done with IPSEC processing. Send it over 23930 * the wire. 23931 */ 23932 done: 23933 mp = ipsec_mp->b_cont; 23934 ipha = (ipha_t *)mp->b_rptr; 23935 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 23936 ip_wput_ipsec_out(q, ipsec_mp, ipha, ill, ire); 23937 } else { 23938 ip6h = (ip6_t *)ipha; 23939 ip_wput_ipsec_out_v6(q, ipsec_mp, ip6h, ill, ire); 23940 } 23941 if (ill != NULL && ill_need_rele) 23942 ill_refrele(ill); 23943 } 23944 23945 /* ARGSUSED */ 23946 void 23947 ip_restart_optmgmt(ipsq_t *dummy_sq, queue_t *q, mblk_t *first_mp, void *dummy) 23948 { 23949 opt_restart_t *or; 23950 int err; 23951 conn_t *connp; 23952 23953 ASSERT(CONN_Q(q)); 23954 connp = Q_TO_CONN(q); 23955 23956 ASSERT(first_mp->b_datap->db_type == M_CTL); 23957 or = (opt_restart_t *)first_mp->b_rptr; 23958 /* 23959 * We don't need to pass any credentials here since this is just 23960 * a restart. The credentials are passed in when svr4_optcom_req 23961 * is called the first time (from ip_wput_nondata). 23962 */ 23963 if (or->or_type == T_SVR4_OPTMGMT_REQ) { 23964 err = svr4_optcom_req(q, first_mp, NULL, 23965 &ip_opt_obj); 23966 } else { 23967 ASSERT(or->or_type == T_OPTMGMT_REQ); 23968 err = tpi_optcom_req(q, first_mp, NULL, 23969 &ip_opt_obj); 23970 } 23971 if (err != EINPROGRESS) { 23972 /* operation is done */ 23973 CONN_OPER_PENDING_DONE(connp); 23974 } 23975 } 23976 23977 /* 23978 * ioctls that go through a down/up sequence may need to wait for the down 23979 * to complete. This involves waiting for the ire and ipif refcnts to go down 23980 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 23981 */ 23982 /* ARGSUSED */ 23983 void 23984 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 23985 { 23986 struct iocblk *iocp; 23987 mblk_t *mp1; 23988 ipif_t *ipif; 23989 ip_ioctl_cmd_t *ipip; 23990 int err; 23991 sin_t *sin; 23992 struct lifreq *lifr; 23993 struct ifreq *ifr; 23994 23995 iocp = (struct iocblk *)mp->b_rptr; 23996 ASSERT(ipsq != NULL); 23997 /* Existence of mp1 verified in ip_wput_nondata */ 23998 mp1 = mp->b_cont->b_cont; 23999 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 24000 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 24001 ill_t *ill; 24002 /* 24003 * Special case where ipsq_current_ipif may not be set. 24004 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 24005 * ill could also have become part of a ipmp group in the 24006 * process, we are here as were not able to complete the 24007 * operation in ipif_set_values because we could not become 24008 * exclusive on the new ipsq, In such a case ipsq_current_ipif 24009 * will not be set so we need to set it. 24010 */ 24011 ill = (ill_t *)q->q_ptr; 24012 ipsq->ipsq_current_ipif = ill->ill_ipif; 24013 ipsq->ipsq_last_cmd = ipip->ipi_cmd; 24014 } 24015 24016 ipif = ipsq->ipsq_current_ipif; 24017 ASSERT(ipif != NULL); 24018 if (ipip->ipi_cmd_type == IF_CMD) { 24019 /* This a old style SIOC[GS]IF* command */ 24020 ifr = (struct ifreq *)mp1->b_rptr; 24021 sin = (sin_t *)&ifr->ifr_addr; 24022 } else if (ipip->ipi_cmd_type == LIF_CMD) { 24023 /* This a new style SIOC[GS]LIF* command */ 24024 lifr = (struct lifreq *)mp1->b_rptr; 24025 sin = (sin_t *)&lifr->lifr_addr; 24026 } else { 24027 sin = NULL; 24028 } 24029 24030 err = (*ipip->ipi_func_restart)(ipif, sin, q, mp, ipip, 24031 (void *)mp1->b_rptr); 24032 24033 /* SIOCLIFREMOVEIF could have removed the ipif */ 24034 ip_ioctl_finish(q, mp, err, 24035 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 24036 ipip->ipi_cmd == SIOCLIFREMOVEIF ? NULL : ipif, ipsq); 24037 } 24038 24039 /* 24040 * ioctl processing 24041 * 24042 * ioctl processing starts with ip_sioctl_copyin_setup which looks up 24043 * the ioctl command in the ioctl tables and determines the copyin data size 24044 * from the ioctl property ipi_copyin_size, and does an mi_copyin() of that 24045 * size. 24046 * 24047 * ioctl processing then continues when the M_IOCDATA makes its way down. 24048 * Now the ioctl is looked up again in the ioctl table, and its properties are 24049 * extracted. The associated 'conn' is then refheld till the end of the ioctl 24050 * and the general ioctl processing function ip_process_ioctl is called. 24051 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 24052 * so goes thru the serialization primitive ipsq_try_enter. Then the 24053 * appropriate function to handle the ioctl is called based on the entry in 24054 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 24055 * which also refreleases the 'conn' that was refheld at the start of the 24056 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 24057 * ip_extract_lifreq_cmn extracts the interface name from the lifreq/ifreq 24058 * struct and looks up the ipif. ip_extract_tunreq handles the case of tunnel. 24059 * 24060 * Many exclusive ioctls go thru an internal down up sequence as part of 24061 * the operation. For example an attempt to change the IP address of an 24062 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 24063 * does all the cleanup such as deleting all ires that use this address. 24064 * Then we need to wait till all references to the interface go away. 24065 */ 24066 void 24067 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 24068 { 24069 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 24070 ip_ioctl_cmd_t *ipip = (ip_ioctl_cmd_t *)arg; 24071 cmd_info_t ci; 24072 int err; 24073 boolean_t entered_ipsq = B_FALSE; 24074 24075 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 24076 24077 if (ipip == NULL) 24078 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 24079 24080 /* 24081 * SIOCLIFADDIF needs to go thru a special path since the 24082 * ill may not exist yet. This happens in the case of lo0 24083 * which is created using this ioctl. 24084 */ 24085 if (ipip->ipi_cmd == SIOCLIFADDIF) { 24086 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 24087 ip_ioctl_finish(q, mp, err, 24088 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 24089 NULL, NULL); 24090 return; 24091 } 24092 24093 ci.ci_ipif = NULL; 24094 switch (ipip->ipi_cmd_type) { 24095 case IF_CMD: 24096 case LIF_CMD: 24097 /* 24098 * ioctls that pass in a [l]ifreq appear here. 24099 * ip_extract_lifreq_cmn returns a refheld ipif in 24100 * ci.ci_ipif 24101 */ 24102 err = ip_extract_lifreq_cmn(q, mp, ipip->ipi_cmd_type, 24103 ipip->ipi_flags, &ci, ip_process_ioctl); 24104 if (err != 0) { 24105 ip_ioctl_finish(q, mp, err, 24106 ipip->ipi_flags & IPI_GET_CMD ? 24107 COPYOUT : NO_COPYOUT, NULL, NULL); 24108 return; 24109 } 24110 ASSERT(ci.ci_ipif != NULL); 24111 break; 24112 24113 case TUN_CMD: 24114 /* 24115 * SIOC[GS]TUNPARAM appear here. ip_extract_tunreq returns 24116 * a refheld ipif in ci.ci_ipif 24117 */ 24118 err = ip_extract_tunreq(q, mp, &ci.ci_ipif, ip_process_ioctl); 24119 if (err != 0) { 24120 ip_ioctl_finish(q, mp, err, 24121 ipip->ipi_flags & IPI_GET_CMD ? 24122 COPYOUT : NO_COPYOUT, NULL, NULL); 24123 return; 24124 } 24125 ASSERT(ci.ci_ipif != NULL); 24126 break; 24127 24128 case MISC_CMD: 24129 /* 24130 * ioctls that neither pass in [l]ifreq or iftun_req come here 24131 * For eg. SIOCGLIFCONF will appear here. 24132 */ 24133 switch (ipip->ipi_cmd) { 24134 case IF_UNITSEL: 24135 /* ioctl comes down the ill */ 24136 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 24137 ipif_refhold(ci.ci_ipif); 24138 break; 24139 case SIOCGMSFILTER: 24140 case SIOCSMSFILTER: 24141 case SIOCGIPMSFILTER: 24142 case SIOCSIPMSFILTER: 24143 err = ip_extract_msfilter(q, mp, &ci.ci_ipif, 24144 ip_process_ioctl); 24145 if (err != 0) { 24146 ip_ioctl_finish(q, mp, err, 24147 ipip->ipi_flags & IPI_GET_CMD ? 24148 COPYOUT : NO_COPYOUT, NULL, NULL); 24149 return; 24150 } 24151 break; 24152 } 24153 err = 0; 24154 ci.ci_sin = NULL; 24155 ci.ci_sin6 = NULL; 24156 ci.ci_lifr = NULL; 24157 break; 24158 } 24159 24160 /* 24161 * If ipsq is non-null, we are already being called exclusively 24162 */ 24163 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 24164 if (!(ipip->ipi_flags & IPI_WR)) { 24165 /* 24166 * A return value of EINPROGRESS means the ioctl is 24167 * either queued and waiting for some reason or has 24168 * already completed. 24169 */ 24170 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 24171 ci.ci_lifr); 24172 if (ci.ci_ipif != NULL) 24173 ipif_refrele(ci.ci_ipif); 24174 ip_ioctl_finish(q, mp, err, 24175 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 24176 NULL, NULL); 24177 return; 24178 } 24179 24180 ASSERT(ci.ci_ipif != NULL); 24181 24182 if (ipsq == NULL) { 24183 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, 24184 ip_process_ioctl, NEW_OP, B_TRUE); 24185 entered_ipsq = B_TRUE; 24186 } 24187 /* 24188 * Release the ipif so that ipif_down and friends that wait for 24189 * references to go away are not misled about the current ipif_refcnt 24190 * values. We are writer so we can access the ipif even after releasing 24191 * the ipif. 24192 */ 24193 ipif_refrele(ci.ci_ipif); 24194 if (ipsq == NULL) 24195 return; 24196 24197 mutex_enter(&ipsq->ipsq_lock); 24198 ASSERT(ipsq->ipsq_current_ipif == NULL); 24199 ipsq->ipsq_current_ipif = ci.ci_ipif; 24200 ipsq->ipsq_last_cmd = ipip->ipi_cmd; 24201 mutex_exit(&ipsq->ipsq_lock); 24202 mutex_enter(&(ci.ci_ipif)->ipif_ill->ill_lock); 24203 /* 24204 * For most set ioctls that come here, this serves as a single point 24205 * where we set the IPIF_CHANGING flag. This ensures that there won't 24206 * be any new references to the ipif. This helps functions that go 24207 * through this path and end up trying to wait for the refcnts 24208 * associated with the ipif to go down to zero. Some exceptions are 24209 * Failover, Failback, and Groupname commands that operate on more than 24210 * just the ci.ci_ipif. These commands internally determine the 24211 * set of ipif's they operate on and set and clear the IPIF_CHANGING 24212 * flags on that set. Another exception is the Removeif command that 24213 * sets the IPIF_CONDEMNED flag internally after identifying the right 24214 * ipif to operate on. 24215 */ 24216 if (ipip->ipi_cmd != SIOCLIFREMOVEIF && 24217 ipip->ipi_cmd != SIOCLIFFAILOVER && 24218 ipip->ipi_cmd != SIOCLIFFAILBACK && 24219 ipip->ipi_cmd != SIOCSLIFGROUPNAME) 24220 (ci.ci_ipif)->ipif_state_flags |= IPIF_CHANGING; 24221 mutex_exit(&(ci.ci_ipif)->ipif_ill->ill_lock); 24222 24223 /* 24224 * A return value of EINPROGRESS means the ioctl is 24225 * either queued and waiting for some reason or has 24226 * already completed. 24227 */ 24228 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 24229 ci.ci_lifr); 24230 24231 /* SIOCLIFREMOVEIF could have removed the ipif */ 24232 ip_ioctl_finish(q, mp, err, 24233 ipip->ipi_flags & IPI_GET_CMD ? COPYOUT : NO_COPYOUT, 24234 ipip->ipi_cmd == SIOCLIFREMOVEIF ? NULL : ci.ci_ipif, ipsq); 24235 24236 if (entered_ipsq) 24237 ipsq_exit(ipsq, B_TRUE, B_TRUE); 24238 } 24239 24240 /* 24241 * Complete the ioctl. Typically ioctls use the mi package and need to 24242 * do mi_copyout/mi_copy_done. 24243 */ 24244 void 24245 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, 24246 ipif_t *ipif, ipsq_t *ipsq) 24247 { 24248 conn_t *connp = NULL; 24249 24250 if (err == EINPROGRESS) 24251 return; 24252 24253 if (CONN_Q(q)) { 24254 connp = Q_TO_CONN(q); 24255 ASSERT(connp->conn_ref >= 2); 24256 } 24257 24258 switch (mode) { 24259 case COPYOUT: 24260 if (err == 0) 24261 mi_copyout(q, mp); 24262 else 24263 mi_copy_done(q, mp, err); 24264 break; 24265 24266 case NO_COPYOUT: 24267 mi_copy_done(q, mp, err); 24268 break; 24269 24270 default: 24271 /* An ioctl aborted through a conn close would take this path */ 24272 break; 24273 } 24274 24275 /* 24276 * The refhold placed at the start of the ioctl is released here. 24277 */ 24278 if (connp != NULL) 24279 CONN_OPER_PENDING_DONE(connp); 24280 24281 /* 24282 * If the ioctl were an exclusive ioctl it would have set 24283 * IPIF_CHANGING at the start of the ioctl which is undone here. 24284 */ 24285 if (ipif != NULL) { 24286 mutex_enter(&(ipif)->ipif_ill->ill_lock); 24287 ipif->ipif_state_flags &= ~IPIF_CHANGING; 24288 mutex_exit(&(ipif)->ipif_ill->ill_lock); 24289 } 24290 24291 /* 24292 * Clear the current ipif in the ipsq at the completion of the ioctl. 24293 * Note that a non-null ipsq_current_ipif prevents new ioctls from 24294 * entering the ipsq 24295 */ 24296 if (ipsq != NULL) { 24297 mutex_enter(&ipsq->ipsq_lock); 24298 ipsq->ipsq_current_ipif = NULL; 24299 mutex_exit(&ipsq->ipsq_lock); 24300 } 24301 } 24302 24303 /* 24304 * This is called from ip_wput_nondata to resume a deferred TCP bind. 24305 */ 24306 /* ARGSUSED */ 24307 void 24308 ip_resume_tcp_bind(void *arg, mblk_t *mp, void *arg2) 24309 { 24310 conn_t *connp = arg; 24311 tcp_t *tcp; 24312 24313 ASSERT(connp != NULL && IPCL_IS_TCP(connp) && connp->conn_tcp != NULL); 24314 tcp = connp->conn_tcp; 24315 24316 if (connp->conn_tcp->tcp_state == TCPS_CLOSED) 24317 freemsg(mp); 24318 else 24319 tcp_rput_other(tcp, mp); 24320 CONN_OPER_PENDING_DONE(connp); 24321 } 24322 24323 /* Called from ip_wput for all non data messages */ 24324 /* ARGSUSED */ 24325 void 24326 ip_wput_nondata(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 24327 { 24328 mblk_t *mp1; 24329 ire_t *ire; 24330 ill_t *ill; 24331 struct iocblk *iocp; 24332 ip_ioctl_cmd_t *ipip; 24333 cred_t *cr; 24334 conn_t *connp = NULL; 24335 int cmd, err; 24336 24337 if (CONN_Q(q)) 24338 connp = Q_TO_CONN(q); 24339 24340 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 24341 24342 /* Check if it is a queue to /dev/sctp. */ 24343 if (connp != NULL && connp->conn_ulp == IPPROTO_SCTP && 24344 connp->conn_rq == NULL) { 24345 sctp_wput(q, mp); 24346 return; 24347 } 24348 24349 switch (DB_TYPE(mp)) { 24350 case M_IOCTL: 24351 /* 24352 * IOCTL processing begins in ip_sioctl_copyin_setup which 24353 * will arrange to copy in associated control structures. 24354 */ 24355 ip_sioctl_copyin_setup(q, mp); 24356 return; 24357 case M_IOCDATA: 24358 /* 24359 * Ensure that this is associated with one of our trans- 24360 * parent ioctls. If it's not ours, discard it if we're 24361 * running as a driver, or pass it on if we're a module. 24362 */ 24363 iocp = (struct iocblk *)mp->b_rptr; 24364 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 24365 if (ipip == NULL) { 24366 if (q->q_next == NULL) { 24367 goto nak; 24368 } else { 24369 putnext(q, mp); 24370 } 24371 return; 24372 } else if ((q->q_next != NULL) && 24373 !(ipip->ipi_flags & IPI_MODOK)) { 24374 /* 24375 * the ioctl is one we recognise, but is not 24376 * consumed by IP as a module, pass M_IOCDATA 24377 * for processing downstream, but only for 24378 * common Streams ioctls. 24379 */ 24380 if (ipip->ipi_flags & IPI_PASS_DOWN) { 24381 putnext(q, mp); 24382 return; 24383 } else { 24384 goto nak; 24385 } 24386 } 24387 24388 /* IOCTL continuation following copyin or copyout. */ 24389 if (mi_copy_state(q, mp, NULL) == -1) { 24390 /* 24391 * The copy operation failed. mi_copy_state already 24392 * cleaned up, so we're out of here. 24393 */ 24394 return; 24395 } 24396 /* 24397 * If we just completed a copy in, we become writer and 24398 * continue processing in ip_sioctl_copyin_done. If it 24399 * was a copy out, we call mi_copyout again. If there is 24400 * nothing more to copy out, it will complete the IOCTL. 24401 */ 24402 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 24403 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 24404 mi_copy_done(q, mp, EPROTO); 24405 return; 24406 } 24407 /* 24408 * Check for cases that need more copying. A return 24409 * value of 0 means a second copyin has been started, 24410 * so we return; a return value of 1 means no more 24411 * copying is needed, so we continue. 24412 */ 24413 cmd = iocp->ioc_cmd; 24414 if ((cmd == SIOCGMSFILTER || cmd == SIOCSMSFILTER || 24415 cmd == SIOCGIPMSFILTER || cmd == SIOCSIPMSFILTER) && 24416 MI_COPY_COUNT(mp) == 1) { 24417 if (ip_copyin_msfilter(q, mp) == 0) 24418 return; 24419 } 24420 /* 24421 * Refhold the conn, till the ioctl completes. This is 24422 * needed in case the ioctl ends up in the pending mp 24423 * list. Every mp in the ill_pending_mp list and 24424 * the ipsq_pending_mp must have a refhold on the conn 24425 * to resume processing. The refhold is released when 24426 * the ioctl completes. (normally or abnormally) 24427 * In all cases ip_ioctl_finish is called to finish 24428 * the ioctl. 24429 */ 24430 if (connp != NULL) { 24431 /* This is not a reentry */ 24432 ASSERT(ipsq == NULL); 24433 CONN_INC_REF(connp); 24434 } else { 24435 if (!(ipip->ipi_flags & IPI_MODOK)) { 24436 mi_copy_done(q, mp, EINVAL); 24437 return; 24438 } 24439 } 24440 24441 ip_process_ioctl(ipsq, q, mp, ipip); 24442 24443 } else { 24444 mi_copyout(q, mp); 24445 } 24446 return; 24447 nak: 24448 iocp->ioc_error = EINVAL; 24449 mp->b_datap->db_type = M_IOCNAK; 24450 iocp->ioc_count = 0; 24451 qreply(q, mp); 24452 return; 24453 24454 case M_IOCNAK: 24455 /* 24456 * The only way we could get here is if a resolver didn't like 24457 * an IOCTL we sent it. This shouldn't happen. 24458 */ 24459 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 24460 "ip_wput: unexpected M_IOCNAK, ioc_cmd 0x%x", 24461 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 24462 freemsg(mp); 24463 return; 24464 case M_IOCACK: 24465 /* Finish socket ioctls passed through to ARP. */ 24466 ip_sioctl_iocack(q, mp); 24467 return; 24468 case M_FLUSH: 24469 if (*mp->b_rptr & FLUSHW) 24470 flushq(q, FLUSHALL); 24471 if (q->q_next) { 24472 /* 24473 * M_FLUSH is sent up to IP by some drivers during 24474 * unbind. ip_rput has already replied to it. We are 24475 * here for the M_FLUSH that we originated in IP 24476 * before sending the unbind request to the driver. 24477 * Just free it as we don't queue packets in IP 24478 * on the write side of the device instance. 24479 */ 24480 freemsg(mp); 24481 return; 24482 } 24483 if (*mp->b_rptr & FLUSHR) { 24484 *mp->b_rptr &= ~FLUSHW; 24485 qreply(q, mp); 24486 return; 24487 } 24488 freemsg(mp); 24489 return; 24490 case IRE_DB_REQ_TYPE: 24491 /* An Upper Level Protocol wants a copy of an IRE. */ 24492 ip_ire_req(q, mp); 24493 return; 24494 case M_CTL: 24495 /* M_CTL messages are used by ARP to tell us things. */ 24496 if ((mp->b_wptr - mp->b_rptr) < sizeof (arc_t)) 24497 break; 24498 switch (((arc_t *)mp->b_rptr)->arc_cmd) { 24499 case AR_ENTRY_SQUERY: 24500 ip_wput_ctl(q, mp); 24501 return; 24502 case AR_CLIENT_NOTIFY: 24503 ip_arp_news(q, mp); 24504 return; 24505 case AR_DLPIOP_DONE: 24506 ASSERT(q->q_next != NULL); 24507 ill = (ill_t *)q->q_ptr; 24508 /* qwriter_ip releases the refhold */ 24509 /* refhold on ill stream is ok without ILL_CAN_LOOKUP */ 24510 ill_refhold(ill); 24511 (void) qwriter_ip(NULL, ill, q, mp, ip_arp_done, 24512 CUR_OP, B_FALSE); 24513 return; 24514 case AR_ARP_CLOSING: 24515 /* 24516 * ARP (above us) is closing. If no ARP bringup is 24517 * currently pending, ack the message so that ARP 24518 * can complete its close. Also mark ill_arp_closing 24519 * so that new ARP bringups will fail. If any 24520 * ARP bringup is currently in progress, we will 24521 * ack this when the current ARP bringup completes. 24522 */ 24523 ASSERT(q->q_next != NULL); 24524 ill = (ill_t *)q->q_ptr; 24525 mutex_enter(&ill->ill_lock); 24526 ill->ill_arp_closing = 1; 24527 if (!ill->ill_arp_bringup_pending) { 24528 mutex_exit(&ill->ill_lock); 24529 qreply(q, mp); 24530 } else { 24531 mutex_exit(&ill->ill_lock); 24532 freemsg(mp); 24533 } 24534 return; 24535 default: 24536 break; 24537 } 24538 break; 24539 case M_PROTO: 24540 case M_PCPROTO: 24541 /* 24542 * The only PROTO messages we expect are ULP binds and 24543 * copies of option negotiation acknowledgements. 24544 */ 24545 switch (((union T_primitives *)mp->b_rptr)->type) { 24546 case O_T_BIND_REQ: 24547 case T_BIND_REQ: { 24548 /* Request can get queued in bind */ 24549 ASSERT(connp != NULL); 24550 /* 24551 * Both TCP and UDP call ip_bind_{v4,v6}() directly 24552 * instead of going through this path. We only get 24553 * here in the following cases: 24554 * 24555 * a. Bind retries, where ipsq is non-NULL. 24556 * b. T_BIND_REQ is issued from non TCP/UDP 24557 * transport, e.g. icmp for raw socket, 24558 * in which case ipsq will be NULL. 24559 */ 24560 ASSERT(ipsq != NULL || 24561 (!IPCL_IS_TCP(connp) && !IPCL_IS_UDP(connp))); 24562 24563 /* Don't increment refcnt if this is a re-entry */ 24564 if (ipsq == NULL) 24565 CONN_INC_REF(connp); 24566 mp = connp->conn_af_isv6 ? ip_bind_v6(q, mp, 24567 connp, NULL) : ip_bind_v4(q, mp, connp); 24568 if (mp == NULL) 24569 return; 24570 if (IPCL_IS_TCP(connp)) { 24571 /* 24572 * In the case of TCP endpoint we 24573 * come here only for bind retries 24574 */ 24575 ASSERT(ipsq != NULL); 24576 CONN_INC_REF(connp); 24577 squeue_fill(connp->conn_sqp, mp, 24578 ip_resume_tcp_bind, connp, 24579 SQTAG_BIND_RETRY); 24580 return; 24581 } else if (IPCL_IS_UDP(connp)) { 24582 /* 24583 * In the case of UDP endpoint we 24584 * come here only for bind retries 24585 */ 24586 ASSERT(ipsq != NULL); 24587 udp_resume_bind(connp, mp); 24588 return; 24589 } 24590 qreply(q, mp); 24591 CONN_OPER_PENDING_DONE(connp); 24592 return; 24593 } 24594 case T_SVR4_OPTMGMT_REQ: 24595 ip2dbg(("ip_wput: T_SVR4_OPTMGMT_REQ flags %x\n", 24596 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 24597 24598 ASSERT(connp != NULL); 24599 if (!snmpcom_req(q, mp, ip_snmp_set, 24600 ip_snmp_get, cr)) { 24601 /* 24602 * Call svr4_optcom_req so that it can 24603 * generate the ack. We don't come here 24604 * if this operation is being restarted. 24605 * ip_restart_optmgmt will drop the conn ref. 24606 * In the case of ipsec option after the ipsec 24607 * load is complete conn_restart_ipsec_waiter 24608 * drops the conn ref. 24609 */ 24610 ASSERT(ipsq == NULL); 24611 CONN_INC_REF(connp); 24612 if (ip_check_for_ipsec_opt(q, mp)) 24613 return; 24614 err = svr4_optcom_req(q, mp, cr, &ip_opt_obj); 24615 if (err != EINPROGRESS) { 24616 /* Operation is done */ 24617 CONN_OPER_PENDING_DONE(connp); 24618 } 24619 } 24620 return; 24621 case T_OPTMGMT_REQ: 24622 ip2dbg(("ip_wput: T_OPTMGMT_REQ\n")); 24623 /* 24624 * Note: No snmpcom_req support through new 24625 * T_OPTMGMT_REQ. 24626 * Call tpi_optcom_req so that it can 24627 * generate the ack. 24628 */ 24629 ASSERT(connp != NULL); 24630 ASSERT(ipsq == NULL); 24631 /* 24632 * We don't come here for restart. ip_restart_optmgmt 24633 * will drop the conn ref. In the case of ipsec option 24634 * after the ipsec load is complete 24635 * conn_restart_ipsec_waiter drops the conn ref. 24636 */ 24637 CONN_INC_REF(connp); 24638 if (ip_check_for_ipsec_opt(q, mp)) 24639 return; 24640 err = tpi_optcom_req(q, mp, cr, &ip_opt_obj); 24641 if (err != EINPROGRESS) { 24642 /* Operation is done */ 24643 CONN_OPER_PENDING_DONE(connp); 24644 } 24645 return; 24646 case T_UNBIND_REQ: 24647 mp = ip_unbind(q, mp); 24648 qreply(q, mp); 24649 return; 24650 default: 24651 /* 24652 * Have to drop any DLPI messages coming down from 24653 * arp (such as an info_req which would cause ip 24654 * to receive an extra info_ack if it was passed 24655 * through. 24656 */ 24657 ip1dbg(("ip_wput_nondata: dropping M_PROTO %d\n", 24658 (int)*(uint_t *)mp->b_rptr)); 24659 freemsg(mp); 24660 return; 24661 } 24662 /* NOTREACHED */ 24663 case IRE_DB_TYPE: { 24664 nce_t *nce; 24665 ill_t *ill; 24666 in6_addr_t gw_addr_v6; 24667 24668 24669 /* 24670 * This is a response back from a resolver. It 24671 * consists of a message chain containing: 24672 * IRE_MBLK-->LL_HDR_MBLK->pkt 24673 * The IRE_MBLK is the one we allocated in ip_newroute. 24674 * The LL_HDR_MBLK is the DLPI header to use to get 24675 * the attached packet, and subsequent ones for the 24676 * same destination, transmitted. 24677 */ 24678 if ((mp->b_wptr - mp->b_rptr) != sizeof (ire_t)) /* ire */ 24679 break; 24680 /* 24681 * First, check to make sure the resolution succeeded. 24682 * If it failed, the second mblk will be empty. 24683 * If it is, free the chain, dropping the packet. 24684 * (We must ire_delete the ire; that frees the ire mblk) 24685 * We're doing this now to support PVCs for ATM; it's 24686 * a partial xresolv implementation. When we fully implement 24687 * xresolv interfaces, instead of freeing everything here 24688 * we'll initiate neighbor discovery. 24689 * 24690 * For v4 (ARP and other external resolvers) the resolver 24691 * frees the message, so no check is needed. This check 24692 * is required, though, for a full xresolve implementation. 24693 * Including this code here now both shows how external 24694 * resolvers can NACK a resolution request using an 24695 * existing design that has no specific provisions for NACKs, 24696 * and also takes into account that the current non-ARP 24697 * external resolver has been coded to use this method of 24698 * NACKing for all IPv6 (xresolv) cases, 24699 * whether our xresolv implementation is complete or not. 24700 * 24701 */ 24702 ire = (ire_t *)mp->b_rptr; 24703 ill = ire_to_ill(ire); 24704 mp1 = mp->b_cont; /* dl_unitdata_req */ 24705 if (mp1->b_rptr == mp1->b_wptr) { 24706 if (ire->ire_ipversion == IPV6_VERSION) { 24707 /* 24708 * XRESOLV interface. 24709 */ 24710 ASSERT(ill->ill_flags & ILLF_XRESOLV); 24711 mutex_enter(&ire->ire_lock); 24712 gw_addr_v6 = ire->ire_gateway_addr_v6; 24713 mutex_exit(&ire->ire_lock); 24714 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 24715 nce = ndp_lookup(ill, 24716 &ire->ire_addr_v6, B_FALSE); 24717 } else { 24718 nce = ndp_lookup(ill, &gw_addr_v6, 24719 B_FALSE); 24720 } 24721 if (nce != NULL) { 24722 nce_resolv_failed(nce); 24723 ndp_delete(nce); 24724 NCE_REFRELE(nce); 24725 } 24726 } 24727 mp->b_cont = NULL; 24728 freemsg(mp1); /* frees the pkt as well */ 24729 ire_delete((ire_t *)mp->b_rptr); 24730 return; 24731 } 24732 /* 24733 * Split them into IRE_MBLK and pkt and feed it into 24734 * ire_add_then_send. Then in ire_add_then_send 24735 * the IRE will be added, and then the packet will be 24736 * run back through ip_wput. This time it will make 24737 * it to the wire. 24738 */ 24739 mp->b_cont = NULL; 24740 mp = mp1->b_cont; /* now, mp points to pkt */ 24741 mp1->b_cont = NULL; 24742 ip1dbg(("ip_wput_nondata: reply from external resolver \n")); 24743 if (ire->ire_ipversion == IPV6_VERSION) { 24744 /* 24745 * XRESOLV interface. Find the nce and put a copy 24746 * of the dl_unitdata_req in nce_res_mp 24747 */ 24748 ASSERT(ill->ill_flags & ILLF_XRESOLV); 24749 mutex_enter(&ire->ire_lock); 24750 gw_addr_v6 = ire->ire_gateway_addr_v6; 24751 mutex_exit(&ire->ire_lock); 24752 if (IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 24753 nce = ndp_lookup(ill, &ire->ire_addr_v6, 24754 B_FALSE); 24755 } else { 24756 nce = ndp_lookup(ill, &gw_addr_v6, B_FALSE); 24757 } 24758 if (nce != NULL) { 24759 /* 24760 * We have to protect nce_res_mp here 24761 * from being accessed by other threads 24762 * while we change the mblk pointer. 24763 * Other functions will also lock the nce when 24764 * accessing nce_res_mp. 24765 * 24766 * The reason we change the mblk pointer 24767 * here rather than copying the resolved address 24768 * into the template is that, unlike with 24769 * ethernet, we have no guarantee that the 24770 * resolved address length will be 24771 * smaller than or equal to the lla length 24772 * with which the template was allocated, 24773 * (for ethernet, they're equal) 24774 * so we have to use the actual resolved 24775 * address mblk - which holds the real 24776 * dl_unitdata_req with the resolved address. 24777 * 24778 * Doing this is the same behavior as was 24779 * previously used in the v4 ARP case. 24780 */ 24781 mutex_enter(&nce->nce_lock); 24782 if (nce->nce_res_mp != NULL) 24783 freemsg(nce->nce_res_mp); 24784 nce->nce_res_mp = mp1; 24785 mutex_exit(&nce->nce_lock); 24786 /* 24787 * We do a fastpath probe here because 24788 * we have resolved the address without 24789 * using Neighbor Discovery. 24790 * In the non-XRESOLV v6 case, the fastpath 24791 * probe is done right after neighbor 24792 * discovery completes. 24793 */ 24794 if (nce->nce_res_mp != NULL) { 24795 int res; 24796 nce_fastpath_list_add(nce); 24797 res = ill_fastpath_probe(ill, 24798 nce->nce_res_mp); 24799 if (res != 0 && res != EAGAIN) 24800 nce_fastpath_list_delete(nce); 24801 } 24802 24803 ire_add_then_send(q, ire, mp); 24804 /* 24805 * Now we have to clean out any packets 24806 * that may have been queued on the nce 24807 * while it was waiting for address resolution 24808 * to complete. 24809 */ 24810 mutex_enter(&nce->nce_lock); 24811 mp1 = nce->nce_qd_mp; 24812 nce->nce_qd_mp = NULL; 24813 mutex_exit(&nce->nce_lock); 24814 while (mp1 != NULL) { 24815 mblk_t *nxt_mp; 24816 queue_t *fwdq = NULL; 24817 ill_t *inbound_ill; 24818 uint_t ifindex; 24819 24820 nxt_mp = mp1->b_next; 24821 mp1->b_next = NULL; 24822 /* 24823 * Retrieve ifindex stored in 24824 * ip_rput_data_v6() 24825 */ 24826 ifindex = 24827 (uint_t)(uintptr_t)mp1->b_prev; 24828 inbound_ill = 24829 ill_lookup_on_ifindex(ifindex, 24830 B_TRUE, NULL, NULL, NULL, 24831 NULL); 24832 mp1->b_prev = NULL; 24833 if (inbound_ill != NULL) 24834 fwdq = inbound_ill->ill_rq; 24835 24836 if (fwdq != NULL) { 24837 put(fwdq, mp1); 24838 ill_refrele(inbound_ill); 24839 } else 24840 put(WR(ill->ill_rq), mp1); 24841 mp1 = nxt_mp; 24842 } 24843 NCE_REFRELE(nce); 24844 } else { /* nce is NULL; clean up */ 24845 ire_delete(ire); 24846 freemsg(mp); 24847 freemsg(mp1); 24848 return; 24849 } 24850 } else { 24851 ire->ire_dlureq_mp = mp1; 24852 ire_add_then_send(q, ire, mp); 24853 } 24854 return; /* All is well, the packet has been sent. */ 24855 } 24856 default: 24857 break; 24858 } 24859 if (q->q_next) { 24860 putnext(q, mp); 24861 } else 24862 freemsg(mp); 24863 } 24864 24865 /* 24866 * Process IP options in an outbound packet. Modify the destination if there 24867 * is a source route option. 24868 * Returns non-zero if something fails in which case an ICMP error has been 24869 * sent and mp freed. 24870 */ 24871 static int 24872 ip_wput_options(queue_t *q, mblk_t *ipsec_mp, ipha_t *ipha, 24873 boolean_t mctl_present, zoneid_t zoneid) 24874 { 24875 ipoptp_t opts; 24876 uchar_t *opt; 24877 uint8_t optval; 24878 uint8_t optlen; 24879 ipaddr_t dst; 24880 intptr_t code = 0; 24881 mblk_t *mp; 24882 ire_t *ire = NULL; 24883 24884 ip2dbg(("ip_wput_options\n")); 24885 mp = ipsec_mp; 24886 if (mctl_present) { 24887 mp = ipsec_mp->b_cont; 24888 } 24889 24890 dst = ipha->ipha_dst; 24891 for (optval = ipoptp_first(&opts, ipha); 24892 optval != IPOPT_EOL; 24893 optval = ipoptp_next(&opts)) { 24894 opt = opts.ipoptp_cur; 24895 optlen = opts.ipoptp_len; 24896 ip2dbg(("ip_wput_options: opt %d, len %d\n", 24897 optval, optlen)); 24898 switch (optval) { 24899 uint32_t off; 24900 case IPOPT_SSRR: 24901 case IPOPT_LSRR: 24902 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 24903 ip1dbg(( 24904 "ip_wput_options: bad option offset\n")); 24905 code = (char *)&opt[IPOPT_OLEN] - 24906 (char *)ipha; 24907 goto param_prob; 24908 } 24909 off = opt[IPOPT_OFFSET]; 24910 ip1dbg(("ip_wput_options: next hop 0x%x\n", 24911 ntohl(dst))); 24912 /* 24913 * For strict: verify that dst is directly 24914 * reachable. 24915 */ 24916 if (optval == IPOPT_SSRR) { 24917 ire = ire_ftable_lookup(dst, 0, 0, 24918 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 24919 MATCH_IRE_TYPE); 24920 if (ire == NULL) { 24921 ip1dbg(("ip_wput_options: SSRR not" 24922 " directly reachable: 0x%x\n", 24923 ntohl(dst))); 24924 goto bad_src_route; 24925 } 24926 ire_refrele(ire); 24927 } 24928 break; 24929 case IPOPT_RR: 24930 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 24931 ip1dbg(( 24932 "ip_wput_options: bad option offset\n")); 24933 code = (char *)&opt[IPOPT_OLEN] - 24934 (char *)ipha; 24935 goto param_prob; 24936 } 24937 break; 24938 case IPOPT_TS: 24939 /* 24940 * Verify that length >=5 and that there is either 24941 * room for another timestamp or that the overflow 24942 * counter is not maxed out. 24943 */ 24944 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 24945 if (optlen < IPOPT_MINLEN_IT) { 24946 goto param_prob; 24947 } 24948 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 24949 ip1dbg(( 24950 "ip_wput_options: bad option offset\n")); 24951 code = (char *)&opt[IPOPT_OFFSET] - 24952 (char *)ipha; 24953 goto param_prob; 24954 } 24955 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 24956 case IPOPT_TS_TSONLY: 24957 off = IPOPT_TS_TIMELEN; 24958 break; 24959 case IPOPT_TS_TSANDADDR: 24960 case IPOPT_TS_PRESPEC: 24961 case IPOPT_TS_PRESPEC_RFC791: 24962 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 24963 break; 24964 default: 24965 code = (char *)&opt[IPOPT_POS_OV_FLG] - 24966 (char *)ipha; 24967 goto param_prob; 24968 } 24969 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 24970 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 24971 /* 24972 * No room and the overflow counter is 15 24973 * already. 24974 */ 24975 goto param_prob; 24976 } 24977 break; 24978 } 24979 } 24980 24981 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 24982 return (0); 24983 24984 ip1dbg(("ip_wput_options: error processing IP options.")); 24985 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 24986 24987 param_prob: 24988 /* 24989 * Since ip_wput() isn't close to finished, we fill 24990 * in enough of the header for credible error reporting. 24991 */ 24992 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 24993 /* Failed */ 24994 freemsg(ipsec_mp); 24995 return (-1); 24996 } 24997 icmp_param_problem(q, ipsec_mp, (uint8_t)code); 24998 return (-1); 24999 25000 bad_src_route: 25001 /* 25002 * Since ip_wput() isn't close to finished, we fill 25003 * in enough of the header for credible error reporting. 25004 */ 25005 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid)) { 25006 /* Failed */ 25007 freemsg(ipsec_mp); 25008 return (-1); 25009 } 25010 icmp_unreachable(q, ipsec_mp, ICMP_SOURCE_ROUTE_FAILED); 25011 return (-1); 25012 } 25013 25014 /* 25015 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 25016 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 25017 * thru /etc/system. 25018 */ 25019 #define CONN_MAXDRAINCNT 64 25020 25021 static void 25022 conn_drain_init(void) 25023 { 25024 int i; 25025 25026 conn_drain_list_cnt = conn_drain_nthreads; 25027 25028 if ((conn_drain_list_cnt == 0) || 25029 (conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 25030 /* 25031 * Default value of the number of drainers is the 25032 * number of cpus, subject to maximum of 8 drainers. 25033 */ 25034 if (boot_max_ncpus != -1) 25035 conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 25036 else 25037 conn_drain_list_cnt = MIN(max_ncpus, 8); 25038 } 25039 25040 conn_drain_list = kmem_zalloc(conn_drain_list_cnt * sizeof (idl_t), 25041 KM_SLEEP); 25042 25043 for (i = 0; i < conn_drain_list_cnt; i++) { 25044 mutex_init(&conn_drain_list[i].idl_lock, NULL, 25045 MUTEX_DEFAULT, NULL); 25046 } 25047 } 25048 25049 static void 25050 conn_drain_fini(void) 25051 { 25052 int i; 25053 25054 for (i = 0; i < conn_drain_list_cnt; i++) 25055 mutex_destroy(&conn_drain_list[i].idl_lock); 25056 kmem_free(conn_drain_list, conn_drain_list_cnt * sizeof (idl_t)); 25057 conn_drain_list = NULL; 25058 } 25059 25060 /* 25061 * Note: For an overview of how flowcontrol is handled in IP please see the 25062 * IP Flowcontrol notes at the top of this file. 25063 * 25064 * Flow control has blocked us from proceeding. Insert the given conn in one 25065 * of the conn drain lists. These conn wq's will be qenabled later on when 25066 * STREAMS flow control does a backenable. conn_walk_drain will enable 25067 * the first conn in each of these drain lists. Each of these qenabled conns 25068 * in turn enables the next in the list, after it runs, or when it closes, 25069 * thus sustaining the drain process. 25070 * 25071 * The only possible calling sequence is ip_wsrv (on conn) -> ip_wput -> 25072 * conn_drain_insert. Thus there can be only 1 instance of conn_drain_insert 25073 * running at any time, on a given conn, since there can be only 1 service proc 25074 * running on a queue at any time. 25075 */ 25076 void 25077 conn_drain_insert(conn_t *connp) 25078 { 25079 idl_t *idl; 25080 uint_t index; 25081 25082 mutex_enter(&connp->conn_lock); 25083 if (connp->conn_state_flags & CONN_CLOSING) { 25084 /* 25085 * The conn is closing as a result of which CONN_CLOSING 25086 * is set. Return. 25087 */ 25088 mutex_exit(&connp->conn_lock); 25089 return; 25090 } else if (connp->conn_idl == NULL) { 25091 /* 25092 * Assign the next drain list round robin. We dont' use 25093 * a lock, and thus it may not be strictly round robin. 25094 * Atomicity of load/stores is enough to make sure that 25095 * conn_drain_list_index is always within bounds. 25096 */ 25097 index = conn_drain_list_index; 25098 ASSERT(index < conn_drain_list_cnt); 25099 connp->conn_idl = &conn_drain_list[index]; 25100 index++; 25101 if (index == conn_drain_list_cnt) 25102 index = 0; 25103 conn_drain_list_index = index; 25104 } 25105 mutex_exit(&connp->conn_lock); 25106 25107 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 25108 if ((connp->conn_drain_prev != NULL) || 25109 (connp->conn_state_flags & CONN_CLOSING)) { 25110 /* 25111 * The conn is already in the drain list, OR 25112 * the conn is closing. We need to check again for 25113 * the closing case again since close can happen 25114 * after we drop the conn_lock, and before we 25115 * acquire the CONN_DRAIN_LIST_LOCK. 25116 */ 25117 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 25118 return; 25119 } else { 25120 idl = connp->conn_idl; 25121 } 25122 25123 /* 25124 * The conn is not in the drain list. Insert it at the 25125 * tail of the drain list. The drain list is circular 25126 * and doubly linked. idl_conn points to the 1st element 25127 * in the list. 25128 */ 25129 if (idl->idl_conn == NULL) { 25130 idl->idl_conn = connp; 25131 connp->conn_drain_next = connp; 25132 connp->conn_drain_prev = connp; 25133 } else { 25134 conn_t *head = idl->idl_conn; 25135 25136 connp->conn_drain_next = head; 25137 connp->conn_drain_prev = head->conn_drain_prev; 25138 head->conn_drain_prev->conn_drain_next = connp; 25139 head->conn_drain_prev = connp; 25140 } 25141 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 25142 } 25143 25144 /* 25145 * This conn is closing, and we are called from ip_close. OR 25146 * This conn has been serviced by ip_wsrv, and we need to do the tail 25147 * processing. 25148 * If this conn is part of the drain list, we may need to sustain the drain 25149 * process by qenabling the next conn in the drain list. We may also need to 25150 * remove this conn from the list, if it is done. 25151 */ 25152 static void 25153 conn_drain_tail(conn_t *connp, boolean_t closing) 25154 { 25155 idl_t *idl; 25156 25157 /* 25158 * connp->conn_idl is stable at this point, and no lock is needed 25159 * to check it. If we are called from ip_close, close has already 25160 * set CONN_CLOSING, thus freezing the value of conn_idl, and 25161 * called us only because conn_idl is non-null. If we are called thru 25162 * service, conn_idl could be null, but it cannot change because 25163 * service is single-threaded per queue, and there cannot be another 25164 * instance of service trying to call conn_drain_insert on this conn 25165 * now. 25166 */ 25167 ASSERT(!closing || (connp->conn_idl != NULL)); 25168 25169 /* 25170 * If connp->conn_idl is null, the conn has not been inserted into any 25171 * drain list even once since creation of the conn. Just return. 25172 */ 25173 if (connp->conn_idl == NULL) 25174 return; 25175 25176 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 25177 25178 if (connp->conn_drain_prev == NULL) { 25179 /* This conn is currently not in the drain list. */ 25180 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 25181 return; 25182 } 25183 idl = connp->conn_idl; 25184 if (idl->idl_conn_draining == connp) { 25185 /* 25186 * This conn is the current drainer. If this is the last conn 25187 * in the drain list, we need to do more checks, in the 'if' 25188 * below. Otherwwise we need to just qenable the next conn, 25189 * to sustain the draining, and is handled in the 'else' 25190 * below. 25191 */ 25192 if (connp->conn_drain_next == idl->idl_conn) { 25193 /* 25194 * This conn is the last in this list. This round 25195 * of draining is complete. If idl_repeat is set, 25196 * it means another flow enabling has happened from 25197 * the driver/streams and we need to another round 25198 * of draining. 25199 * If there are more than 2 conns in the drain list, 25200 * do a left rotate by 1, so that all conns except the 25201 * conn at the head move towards the head by 1, and the 25202 * the conn at the head goes to the tail. This attempts 25203 * a more even share for all queues that are being 25204 * drained. 25205 */ 25206 if ((connp->conn_drain_next != connp) && 25207 (idl->idl_conn->conn_drain_next != connp)) { 25208 idl->idl_conn = idl->idl_conn->conn_drain_next; 25209 } 25210 if (idl->idl_repeat) { 25211 qenable(idl->idl_conn->conn_wq); 25212 idl->idl_conn_draining = idl->idl_conn; 25213 idl->idl_repeat = 0; 25214 } else { 25215 idl->idl_conn_draining = NULL; 25216 } 25217 } else { 25218 /* 25219 * If the next queue that we are now qenable'ing, 25220 * is closing, it will remove itself from this list 25221 * and qenable the subsequent queue in ip_close(). 25222 * Serialization is acheived thru idl_lock. 25223 */ 25224 qenable(connp->conn_drain_next->conn_wq); 25225 idl->idl_conn_draining = connp->conn_drain_next; 25226 } 25227 } 25228 if (!connp->conn_did_putbq || closing) { 25229 /* 25230 * Remove ourself from the drain list, if we did not do 25231 * a putbq, or if the conn is closing. 25232 * Note: It is possible that q->q_first is non-null. It means 25233 * that these messages landed after we did a enableok() in 25234 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 25235 * service them. 25236 */ 25237 if (connp->conn_drain_next == connp) { 25238 /* Singleton in the list */ 25239 ASSERT(connp->conn_drain_prev == connp); 25240 idl->idl_conn = NULL; 25241 idl->idl_conn_draining = NULL; 25242 } else { 25243 connp->conn_drain_prev->conn_drain_next = 25244 connp->conn_drain_next; 25245 connp->conn_drain_next->conn_drain_prev = 25246 connp->conn_drain_prev; 25247 if (idl->idl_conn == connp) 25248 idl->idl_conn = connp->conn_drain_next; 25249 ASSERT(idl->idl_conn_draining != connp); 25250 25251 } 25252 connp->conn_drain_next = NULL; 25253 connp->conn_drain_prev = NULL; 25254 } 25255 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 25256 } 25257 25258 /* 25259 * Write service routine. Shared perimeter entry point. 25260 * ip_wsrv can be called in any of the following ways. 25261 * 1. The device queue's messages has fallen below the low water mark 25262 * and STREAMS has backenabled the ill_wq. We walk thru all the 25263 * the drain lists and backenable the first conn in each list. 25264 * 2. The above causes STREAMS to run ip_wsrv on the conn_wq of the 25265 * qenabled non-tcp upper layers. We start dequeing messages and call 25266 * ip_wput for each message. 25267 */ 25268 25269 void 25270 ip_wsrv(queue_t *q) 25271 { 25272 conn_t *connp; 25273 ill_t *ill; 25274 mblk_t *mp; 25275 25276 if (q->q_next) { 25277 ill = (ill_t *)q->q_ptr; 25278 if (ill->ill_state_flags == 0) { 25279 /* 25280 * The device flow control has opened up. 25281 * Walk through conn drain lists and qenable the 25282 * first conn in each list. This makes sense only 25283 * if the stream is fully plumbed and setup. 25284 * Hence the if check above. 25285 */ 25286 ip1dbg(("ip_wsrv: walking\n")); 25287 conn_walk_drain(); 25288 } 25289 return; 25290 } 25291 25292 connp = Q_TO_CONN(q); 25293 ip1dbg(("ip_wsrv: %p %p\n", (void *)q, (void *)connp)); 25294 25295 /* 25296 * 1. Set conn_draining flag to signal that service is active. 25297 * 25298 * 2. ip_output determines whether it has been called from service, 25299 * based on the last parameter. If it is IP_WSRV it concludes it 25300 * has been called from service. 25301 * 25302 * 3. Message ordering is preserved by the following logic. 25303 * i. A directly called ip_output (i.e. not thru service) will queue 25304 * the message at the tail, if conn_draining is set (i.e. service 25305 * is running) or if q->q_first is non-null. 25306 * 25307 * ii. If ip_output is called from service, and if ip_output cannot 25308 * putnext due to flow control, it does a putbq. 25309 * 25310 * 4. noenable the queue so that a putbq from ip_wsrv does not reenable 25311 * (causing an infinite loop). 25312 */ 25313 ASSERT(!connp->conn_did_putbq); 25314 while ((q->q_first != NULL) && !connp->conn_did_putbq) { 25315 connp->conn_draining = 1; 25316 noenable(q); 25317 while ((mp = getq(q)) != NULL) { 25318 ip_output(Q_TO_CONN(q), mp, q, IP_WSRV); 25319 if (connp->conn_did_putbq) { 25320 /* ip_wput did a putbq */ 25321 break; 25322 } 25323 } 25324 /* 25325 * At this point, a thread coming down from top, calling 25326 * ip_wput, may end up queueing the message. We have not yet 25327 * enabled the queue, so ip_wsrv won't be called again. 25328 * To avoid this race, check q->q_first again (in the loop) 25329 * If the other thread queued the message before we call 25330 * enableok(), we will catch it in the q->q_first check. 25331 * If the other thread queues the message after we call 25332 * enableok(), ip_wsrv will be called again by STREAMS. 25333 */ 25334 connp->conn_draining = 0; 25335 enableok(q); 25336 } 25337 25338 /* Enable the next conn for draining */ 25339 conn_drain_tail(connp, B_FALSE); 25340 25341 connp->conn_did_putbq = 0; 25342 } 25343 25344 /* 25345 * Walk the list of all conn's calling the function provided with the 25346 * specified argument for each. Note that this only walks conn's that 25347 * have been bound. 25348 * Applies to both IPv4 and IPv6. 25349 */ 25350 static void 25351 conn_walk_fanout(pfv_t func, void *arg, zoneid_t zoneid) 25352 { 25353 conn_walk_fanout_table(ipcl_udp_fanout, ipcl_udp_fanout_size, 25354 func, arg, zoneid); 25355 conn_walk_fanout_table(ipcl_conn_fanout, ipcl_conn_fanout_size, 25356 func, arg, zoneid); 25357 conn_walk_fanout_table(ipcl_bind_fanout, ipcl_bind_fanout_size, 25358 func, arg, zoneid); 25359 conn_walk_fanout_table(ipcl_proto_fanout, 25360 A_CNT(ipcl_proto_fanout), func, arg, zoneid); 25361 conn_walk_fanout_table(ipcl_proto_fanout_v6, 25362 A_CNT(ipcl_proto_fanout_v6), func, arg, zoneid); 25363 } 25364 25365 /* 25366 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 25367 * of conns that need to be drained, check if drain is already in progress. 25368 * If so set the idl_repeat bit, indicating that the last conn in the list 25369 * needs to reinitiate the drain once again, for the list. If drain is not 25370 * in progress for the list, initiate the draining, by qenabling the 1st 25371 * conn in the list. The drain is self-sustaining, each qenabled conn will 25372 * in turn qenable the next conn, when it is done/blocked/closing. 25373 */ 25374 static void 25375 conn_walk_drain(void) 25376 { 25377 int i; 25378 idl_t *idl; 25379 25380 IP_STAT(ip_conn_walk_drain); 25381 25382 for (i = 0; i < conn_drain_list_cnt; i++) { 25383 idl = &conn_drain_list[i]; 25384 mutex_enter(&idl->idl_lock); 25385 if (idl->idl_conn == NULL) { 25386 mutex_exit(&idl->idl_lock); 25387 continue; 25388 } 25389 /* 25390 * If this list is not being drained currently by 25391 * an ip_wsrv thread, start the process. 25392 */ 25393 if (idl->idl_conn_draining == NULL) { 25394 ASSERT(idl->idl_repeat == 0); 25395 qenable(idl->idl_conn->conn_wq); 25396 idl->idl_conn_draining = idl->idl_conn; 25397 } else { 25398 idl->idl_repeat = 1; 25399 } 25400 mutex_exit(&idl->idl_lock); 25401 } 25402 } 25403 25404 /* 25405 * Walk an conn hash table of `count' buckets, calling func for each entry. 25406 */ 25407 static void 25408 conn_walk_fanout_table(connf_t *connfp, uint_t count, pfv_t func, void *arg, 25409 zoneid_t zoneid) 25410 { 25411 conn_t *connp; 25412 25413 while (count-- > 0) { 25414 mutex_enter(&connfp->connf_lock); 25415 for (connp = connfp->connf_head; connp != NULL; 25416 connp = connp->conn_next) { 25417 if (zoneid == GLOBAL_ZONEID || 25418 zoneid == connp->conn_zoneid) { 25419 CONN_INC_REF(connp); 25420 mutex_exit(&connfp->connf_lock); 25421 (*func)(connp, arg); 25422 mutex_enter(&connfp->connf_lock); 25423 CONN_DEC_REF(connp); 25424 } 25425 } 25426 mutex_exit(&connfp->connf_lock); 25427 connfp++; 25428 } 25429 } 25430 25431 /* ipcl_walk routine invoked for ip_conn_report for each conn. */ 25432 static void 25433 conn_report1(conn_t *connp, void *mp) 25434 { 25435 char buf1[INET6_ADDRSTRLEN]; 25436 char buf2[INET6_ADDRSTRLEN]; 25437 uint_t print_len, buf_len; 25438 25439 ASSERT(connp != NULL); 25440 25441 buf_len = ((mblk_t *)mp)->b_datap->db_lim - ((mblk_t *)mp)->b_wptr; 25442 if (buf_len <= 0) 25443 return; 25444 (void) inet_ntop(AF_INET6, &connp->conn_srcv6, buf1, sizeof (buf1)), 25445 (void) inet_ntop(AF_INET6, &connp->conn_remv6, buf2, sizeof (buf2)), 25446 print_len = snprintf((char *)((mblk_t *)mp)->b_wptr, buf_len, 25447 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 25448 "%5d %s/%05d %s/%05d\n", 25449 (void *)connp, (void *)CONNP_TO_RQ(connp), 25450 (void *)CONNP_TO_WQ(connp), connp->conn_zoneid, 25451 buf1, connp->conn_lport, 25452 buf2, connp->conn_fport); 25453 if (print_len < buf_len) { 25454 ((mblk_t *)mp)->b_wptr += print_len; 25455 } else { 25456 ((mblk_t *)mp)->b_wptr += buf_len; 25457 } 25458 } 25459 25460 /* 25461 * Named Dispatch routine to produce a formatted report on all conns 25462 * that are listed in one of the fanout tables. 25463 * This report is accessed by using the ndd utility to "get" ND variable 25464 * "ip_conn_status". 25465 */ 25466 /* ARGSUSED */ 25467 static int 25468 ip_conn_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 25469 { 25470 (void) mi_mpprintf(mp, 25471 "CONN " MI_COL_HDRPAD_STR 25472 "rfq " MI_COL_HDRPAD_STR 25473 "stq " MI_COL_HDRPAD_STR 25474 " zone local remote"); 25475 25476 /* 25477 * Because of the ndd constraint, at most we can have 64K buffer 25478 * to put in all conn info. So to be more efficient, just 25479 * allocate a 64K buffer here, assuming we need that large buffer. 25480 * This should be OK as only privileged processes can do ndd /dev/ip. 25481 */ 25482 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 25483 /* The following may work even if we cannot get a large buf. */ 25484 (void) mi_mpprintf(mp, "<< Out of buffer >>\n"); 25485 return (0); 25486 } 25487 25488 conn_walk_fanout(conn_report1, mp->b_cont, Q_TO_CONN(q)->conn_zoneid); 25489 return (0); 25490 } 25491 25492 /* 25493 * Determine if the ill and multicast aspects of that packets 25494 * "matches" the conn. 25495 */ 25496 boolean_t 25497 conn_wantpacket(conn_t *connp, ill_t *ill, ipha_t *ipha, int fanout_flags, 25498 zoneid_t zoneid) 25499 { 25500 ill_t *in_ill; 25501 boolean_t found; 25502 ipif_t *ipif; 25503 ire_t *ire; 25504 ipaddr_t dst, src; 25505 25506 dst = ipha->ipha_dst; 25507 src = ipha->ipha_src; 25508 25509 /* 25510 * conn_incoming_ill is set by IP_BOUND_IF which limits 25511 * unicast, broadcast and multicast reception to 25512 * conn_incoming_ill. conn_wantpacket itself is called 25513 * only for BROADCAST and multicast. 25514 * 25515 * 1) ip_rput supresses duplicate broadcasts if the ill 25516 * is part of a group. Hence, we should be receiving 25517 * just one copy of broadcast for the whole group. 25518 * Thus, if it is part of the group the packet could 25519 * come on any ill of the group and hence we need a 25520 * match on the group. Otherwise, match on ill should 25521 * be sufficient. 25522 * 25523 * 2) ip_rput does not suppress duplicate multicast packets. 25524 * If there are two interfaces in a ill group and we have 25525 * 2 applications (conns) joined a multicast group G on 25526 * both the interfaces, ilm_lookup_ill filter in ip_rput 25527 * will give us two packets because we join G on both the 25528 * interfaces rather than nominating just one interface 25529 * for receiving multicast like broadcast above. So, 25530 * we have to call ilg_lookup_ill to filter out duplicate 25531 * copies, if ill is part of a group. 25532 */ 25533 in_ill = connp->conn_incoming_ill; 25534 if (in_ill != NULL) { 25535 if (in_ill->ill_group == NULL) { 25536 if (in_ill != ill) 25537 return (B_FALSE); 25538 } else if (in_ill->ill_group != ill->ill_group) { 25539 return (B_FALSE); 25540 } 25541 } 25542 25543 if (!CLASSD(dst)) { 25544 if (connp->conn_zoneid == zoneid) 25545 return (B_TRUE); 25546 /* 25547 * The conn is in a different zone; we need to check that this 25548 * broadcast address is configured in the application's zone and 25549 * on one ill in the group. 25550 */ 25551 ipif = ipif_get_next_ipif(NULL, ill); 25552 if (ipif == NULL) 25553 return (B_FALSE); 25554 ire = ire_ctable_lookup(dst, 0, IRE_BROADCAST, ipif, 25555 connp->conn_zoneid, (MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP)); 25556 ipif_refrele(ipif); 25557 if (ire != NULL) { 25558 ire_refrele(ire); 25559 return (B_TRUE); 25560 } else { 25561 return (B_FALSE); 25562 } 25563 } 25564 25565 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 25566 connp->conn_zoneid == zoneid) { 25567 /* 25568 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 25569 * disabled, therefore we don't dispatch the multicast packet to 25570 * the sending zone. 25571 */ 25572 return (B_FALSE); 25573 } 25574 25575 if ((ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) && 25576 connp->conn_zoneid != zoneid) { 25577 /* 25578 * Multicast packet on the loopback interface: we only match 25579 * conns who joined the group in the specified zone. 25580 */ 25581 return (B_FALSE); 25582 } 25583 25584 if (connp->conn_multi_router) { 25585 /* multicast packet and multicast router socket: send up */ 25586 return (B_TRUE); 25587 } 25588 25589 mutex_enter(&connp->conn_lock); 25590 found = (ilg_lookup_ill_withsrc(connp, dst, src, ill) != NULL); 25591 mutex_exit(&connp->conn_lock); 25592 return (found); 25593 } 25594 25595 /* 25596 * Finish processing of "arp_up" when AR_DLPIOP_DONE is received from arp. 25597 */ 25598 /* ARGSUSED */ 25599 static void 25600 ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg) 25601 { 25602 ill_t *ill = (ill_t *)q->q_ptr; 25603 mblk_t *mp1, *mp2; 25604 ipif_t *ipif; 25605 int err = 0; 25606 conn_t *connp = NULL; 25607 ipsq_t *ipsq; 25608 arc_t *arc; 25609 25610 ip1dbg(("ip_arp_done(%s)\n", ill->ill_name)); 25611 25612 ASSERT((mp->b_wptr - mp->b_rptr) >= sizeof (arc_t)); 25613 ASSERT(((arc_t *)mp->b_rptr)->arc_cmd == AR_DLPIOP_DONE); 25614 25615 ASSERT(IAM_WRITER_ILL(ill)); 25616 mp2 = mp->b_cont; 25617 mp->b_cont = NULL; 25618 25619 /* 25620 * We have now received the arp bringup completion message 25621 * from ARP. Mark the arp bringup as done. Also if the arp 25622 * stream has already started closing, send up the AR_ARP_CLOSING 25623 * ack now since ARP is waiting in close for this ack. 25624 */ 25625 mutex_enter(&ill->ill_lock); 25626 ill->ill_arp_bringup_pending = 0; 25627 if (ill->ill_arp_closing) { 25628 mutex_exit(&ill->ill_lock); 25629 /* Let's reuse the mp for sending the ack */ 25630 arc = (arc_t *)mp->b_rptr; 25631 mp->b_wptr = mp->b_rptr + sizeof (arc_t); 25632 arc->arc_cmd = AR_ARP_CLOSING; 25633 qreply(q, mp); 25634 } else { 25635 mutex_exit(&ill->ill_lock); 25636 freeb(mp); 25637 } 25638 25639 /* We should have an IOCTL waiting on this. */ 25640 ipsq = ill->ill_phyint->phyint_ipsq; 25641 ipif = ipsq->ipsq_pending_ipif; 25642 mp1 = ipsq_pending_mp_get(ipsq, &connp); 25643 ASSERT(!((mp1 != NULL) ^ (ipif != NULL))); 25644 if (mp1 == NULL) { 25645 /* bringup was aborted by the user */ 25646 freemsg(mp2); 25647 return; 25648 } 25649 ASSERT(connp != NULL); 25650 q = CONNP_TO_WQ(connp); 25651 /* 25652 * If the DL_BIND_REQ fails, it is noted 25653 * in arc_name_offset. 25654 */ 25655 err = *((int *)mp2->b_rptr); 25656 if (err == 0) { 25657 if (ipif->ipif_isv6) { 25658 if ((err = ipif_up_done_v6(ipif)) != 0) 25659 ip0dbg(("ip_arp_done: init failed\n")); 25660 } else { 25661 if ((err = ipif_up_done(ipif)) != 0) 25662 ip0dbg(("ip_arp_done: init failed\n")); 25663 } 25664 } else { 25665 ip0dbg(("ip_arp_done: DL_BIND_REQ failed\n")); 25666 } 25667 25668 freemsg(mp2); 25669 25670 if ((err == 0) && (ill->ill_up_ipifs)) { 25671 err = ill_up_ipifs(ill, q, mp1); 25672 if (err == EINPROGRESS) 25673 return; 25674 } 25675 25676 if (ill->ill_up_ipifs) { 25677 ill_group_cleanup(ill); 25678 } 25679 25680 /* 25681 * The ioctl must complete now without EINPROGRESS 25682 * since ipsq_pending_mp_get has removed the ioctl mblk 25683 * from ipsq_pending_mp. Otherwise the ioctl will be 25684 * stuck for ever in the ipsq. 25685 */ 25686 ASSERT(err != EINPROGRESS); 25687 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipif, ipsq); 25688 } 25689 25690 /* Allocate the private structure */ 25691 static int 25692 ip_priv_alloc(void **bufp) 25693 { 25694 void *buf; 25695 25696 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 25697 return (ENOMEM); 25698 25699 *bufp = buf; 25700 return (0); 25701 } 25702 25703 /* Function to delete the private structure */ 25704 void 25705 ip_priv_free(void *buf) 25706 { 25707 ASSERT(buf != NULL); 25708 kmem_free(buf, sizeof (ip_priv_t)); 25709 } 25710 25711 /* 25712 * The entry point for IPPF processing. 25713 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 25714 * routine just returns. 25715 * 25716 * When called, ip_process generates an ipp_packet_t structure 25717 * which holds the state information for this packet and invokes the 25718 * the classifier (via ipp_packet_process). The classification, depending on 25719 * configured filters, results in a list of actions for this packet. Invoking 25720 * an action may cause the packet to be dropped, in which case the resulting 25721 * mblk (*mpp) is NULL. proc indicates the callout position for 25722 * this packet and ill_index is the interface this packet on or will leave 25723 * on (inbound and outbound resp.). 25724 */ 25725 void 25726 ip_process(ip_proc_t proc, mblk_t **mpp, uint32_t ill_index) 25727 { 25728 mblk_t *mp; 25729 ip_priv_t *priv; 25730 ipp_action_id_t aid; 25731 int rc = 0; 25732 ipp_packet_t *pp; 25733 #define IP_CLASS "ip" 25734 25735 /* If the classifier is not loaded, return */ 25736 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 25737 return; 25738 } 25739 25740 mp = *mpp; 25741 ASSERT(mp != NULL); 25742 25743 /* Allocate the packet structure */ 25744 rc = ipp_packet_alloc(&pp, IP_CLASS, aid); 25745 if (rc != 0) { 25746 *mpp = NULL; 25747 freemsg(mp); 25748 return; 25749 } 25750 25751 /* Allocate the private structure */ 25752 rc = ip_priv_alloc((void **)&priv); 25753 if (rc != 0) { 25754 *mpp = NULL; 25755 freemsg(mp); 25756 ipp_packet_free(pp); 25757 return; 25758 } 25759 priv->proc = proc; 25760 priv->ill_index = ill_index; 25761 ipp_packet_set_private(pp, priv, ip_priv_free); 25762 ipp_packet_set_data(pp, mp); 25763 25764 /* Invoke the classifier */ 25765 rc = ipp_packet_process(&pp); 25766 if (pp != NULL) { 25767 mp = ipp_packet_get_data(pp); 25768 ipp_packet_free(pp); 25769 if (rc != 0) { 25770 freemsg(mp); 25771 *mpp = NULL; 25772 } 25773 } else { 25774 *mpp = NULL; 25775 } 25776 #undef IP_CLASS 25777 } 25778 25779 /* 25780 * Propagate a multicast group membership operation (add/drop) on 25781 * all the interfaces crossed by the related multirt routes. 25782 * The call is considered successful if the operation succeeds 25783 * on at least one interface. 25784 */ 25785 static int 25786 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, 25787 uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *ire, conn_t *connp, 25788 boolean_t checkonly, ipaddr_t group, mcast_record_t fmode, ipaddr_t src, 25789 mblk_t *first_mp) 25790 { 25791 ire_t *ire_gw; 25792 irb_t *irb; 25793 int error = 0; 25794 opt_restart_t *or; 25795 25796 irb = ire->ire_bucket; 25797 ASSERT(irb != NULL); 25798 25799 ASSERT(DB_TYPE(first_mp) == M_CTL); 25800 25801 or = (opt_restart_t *)first_mp->b_rptr; 25802 IRB_REFHOLD(irb); 25803 for (; ire != NULL; ire = ire->ire_next) { 25804 if ((ire->ire_flags & RTF_MULTIRT) == 0) 25805 continue; 25806 if (ire->ire_addr != group) 25807 continue; 25808 25809 ire_gw = ire_ftable_lookup(ire->ire_gateway_addr, 0, 0, 25810 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, 25811 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE); 25812 /* No resolver exists for the gateway; skip this ire. */ 25813 if (ire_gw == NULL) 25814 continue; 25815 25816 /* 25817 * This function can return EINPROGRESS. If so the operation 25818 * will be restarted from ip_restart_optmgmt which will 25819 * call ip_opt_set and option processing will restart for 25820 * this option. So we may end up calling 'fn' more than once. 25821 * This requires that 'fn' is idempotent except for the 25822 * return value. The operation is considered a success if 25823 * it succeeds at least once on any one interface. 25824 */ 25825 error = fn(connp, checkonly, group, ire_gw->ire_src_addr, 25826 NULL, fmode, src, first_mp); 25827 if (error == 0) 25828 or->or_private = CGTP_MCAST_SUCCESS; 25829 25830 if (ip_debug > 0) { 25831 ulong_t off; 25832 char *ksym; 25833 ksym = kobj_getsymname((uintptr_t)fn, &off); 25834 ip2dbg(("ip_multirt_apply_membership: " 25835 "called %s, multirt group 0x%08x via itf 0x%08x, " 25836 "error %d [success %u]\n", 25837 ksym ? ksym : "?", 25838 ntohl(group), ntohl(ire_gw->ire_src_addr), 25839 error, or->or_private)); 25840 } 25841 25842 ire_refrele(ire_gw); 25843 if (error == EINPROGRESS) { 25844 IRB_REFRELE(irb); 25845 return (error); 25846 } 25847 } 25848 IRB_REFRELE(irb); 25849 /* 25850 * Consider the call as successful if we succeeded on at least 25851 * one interface. Otherwise, return the last encountered error. 25852 */ 25853 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 25854 } 25855 25856 25857 /* 25858 * Issue a warning regarding a route crossing an interface with an 25859 * incorrect MTU. Only one message every 'ip_multirt_log_interval' 25860 * amount of time is logged. 25861 */ 25862 static void 25863 ip_multirt_bad_mtu(ire_t *ire, uint32_t max_frag) 25864 { 25865 hrtime_t current = gethrtime(); 25866 char buf[16]; 25867 25868 /* Convert interval in ms to hrtime in ns */ 25869 if (multirt_bad_mtu_last_time + 25870 ((hrtime_t)ip_multirt_log_interval * (hrtime_t)1000000) <= 25871 current) { 25872 cmn_err(CE_WARN, "ip: ignoring multiroute " 25873 "to %s, incorrect MTU %u (expected %u)\n", 25874 ip_dot_addr(ire->ire_addr, buf), 25875 ire->ire_max_frag, max_frag); 25876 25877 multirt_bad_mtu_last_time = current; 25878 } 25879 } 25880 25881 25882 /* 25883 * Get the CGTP (multirouting) filtering status. 25884 * If 0, the CGTP hooks are transparent. 25885 */ 25886 /* ARGSUSED */ 25887 static int 25888 ip_cgtp_filter_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 25889 { 25890 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 25891 25892 (void) mi_mpprintf(mp, "%d", (int)*ip_cgtp_filter_value); 25893 return (0); 25894 } 25895 25896 25897 /* 25898 * Set the CGTP (multirouting) filtering status. 25899 * If the status is changed from active to transparent 25900 * or from transparent to active, forward the new status 25901 * to the filtering module (if loaded). 25902 */ 25903 /* ARGSUSED */ 25904 static int 25905 ip_cgtp_filter_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 25906 cred_t *ioc_cr) 25907 { 25908 long new_value; 25909 boolean_t *ip_cgtp_filter_value = (boolean_t *)cp; 25910 25911 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 25912 new_value < 0 || new_value > 1) { 25913 return (EINVAL); 25914 } 25915 25916 /* 25917 * Do not enable CGTP filtering - thus preventing the hooks 25918 * from being invoked - if the version number of the 25919 * filtering module hooks does not match. 25920 */ 25921 if ((ip_cgtp_filter_ops != NULL) && 25922 (ip_cgtp_filter_ops->cfo_filter_rev != CGTP_FILTER_REV)) { 25923 cmn_err(CE_WARN, "IP: CGTP filtering version mismatch " 25924 "(module hooks version %d, expecting %d)\n", 25925 ip_cgtp_filter_ops->cfo_filter_rev, CGTP_FILTER_REV); 25926 return (ENOTSUP); 25927 } 25928 25929 if ((!*ip_cgtp_filter_value) && new_value) { 25930 cmn_err(CE_NOTE, "IP: enabling CGTP filtering%s", 25931 ip_cgtp_filter_ops == NULL ? 25932 " (module not loaded)" : ""); 25933 } 25934 if (*ip_cgtp_filter_value && (!new_value)) { 25935 cmn_err(CE_NOTE, "IP: disabling CGTP filtering%s", 25936 ip_cgtp_filter_ops == NULL ? 25937 " (module not loaded)" : ""); 25938 } 25939 25940 if (ip_cgtp_filter_ops != NULL) { 25941 int res; 25942 if ((res = ip_cgtp_filter_ops->cfo_change_state(new_value))) { 25943 return (res); 25944 } 25945 } 25946 25947 *ip_cgtp_filter_value = (boolean_t)new_value; 25948 25949 return (0); 25950 } 25951 25952 25953 /* 25954 * Return the expected CGTP hooks version number. 25955 */ 25956 int 25957 ip_cgtp_filter_supported(void) 25958 { 25959 return (ip_cgtp_filter_rev); 25960 } 25961 25962 25963 /* 25964 * CGTP hooks can be registered by directly touching ip_cgtp_filter_ops 25965 * or by invoking this function. In the first case, the version number 25966 * of the registered structure is checked at hooks activation time 25967 * in ip_cgtp_filter_set(). 25968 */ 25969 int 25970 ip_cgtp_filter_register(cgtp_filter_ops_t *ops) 25971 { 25972 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 25973 return (ENOTSUP); 25974 25975 ip_cgtp_filter_ops = ops; 25976 return (0); 25977 } 25978 25979 static squeue_func_t 25980 ip_squeue_switch(int val) 25981 { 25982 squeue_func_t rval = squeue_fill; 25983 25984 switch (val) { 25985 case IP_SQUEUE_ENTER_NODRAIN: 25986 rval = squeue_enter_nodrain; 25987 break; 25988 case IP_SQUEUE_ENTER: 25989 rval = squeue_enter; 25990 break; 25991 default: 25992 break; 25993 } 25994 return (rval); 25995 } 25996 25997 /* ARGSUSED */ 25998 static int 25999 ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, 26000 caddr_t addr, cred_t *cr) 26001 { 26002 int *v = (int *)addr; 26003 long new_value; 26004 26005 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 26006 return (EINVAL); 26007 26008 ip_input_proc = ip_squeue_switch(new_value); 26009 *v = new_value; 26010 return (0); 26011 } 26012 26013 /* ARGSUSED */ 26014 static int 26015 ip_int_set(queue_t *q, mblk_t *mp, char *value, 26016 caddr_t addr, cred_t *cr) 26017 { 26018 int *v = (int *)addr; 26019 long new_value; 26020 26021 if (ddi_strtol(value, NULL, 10, &new_value) != 0) 26022 return (EINVAL); 26023 26024 *v = new_value; 26025 return (0); 26026 } 26027 26028 static void 26029 ip_kstat_init(void) 26030 { 26031 ip_named_kstat_t template = { 26032 { "forwarding", KSTAT_DATA_UINT32, 0 }, 26033 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 26034 { "inReceives", KSTAT_DATA_UINT32, 0 }, 26035 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 26036 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 26037 { "forwDatagrams", KSTAT_DATA_UINT32, 0 }, 26038 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 26039 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 26040 { "inDelivers", KSTAT_DATA_UINT32, 0 }, 26041 { "outRequests", KSTAT_DATA_UINT32, 0 }, 26042 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 26043 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 26044 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 26045 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 26046 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 26047 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 26048 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 26049 { "fragFails", KSTAT_DATA_UINT32, 0 }, 26050 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 26051 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 26052 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 26053 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 26054 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 26055 { "inErrs", KSTAT_DATA_UINT32, 0 }, 26056 { "noPorts", KSTAT_DATA_UINT32, 0 }, 26057 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 26058 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 26059 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 26060 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 26061 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 26062 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 26063 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 26064 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 26065 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 26066 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 26067 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 26068 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 26069 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 26070 }; 26071 26072 ip_mibkp = kstat_create("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 26073 NUM_OF_FIELDS(ip_named_kstat_t), 26074 0); 26075 if (!ip_mibkp) 26076 return; 26077 26078 template.forwarding.value.ui32 = WE_ARE_FORWARDING ? 1:2; 26079 template.defaultTTL.value.ui32 = (uint32_t)ip_def_ttl; 26080 template.reasmTimeout.value.ui32 = ip_g_frag_timeout; 26081 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 26082 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 26083 26084 template.netToMediaEntrySize.value.i32 = 26085 sizeof (mib2_ipNetToMediaEntry_t); 26086 26087 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 26088 26089 bcopy(&template, ip_mibkp->ks_data, sizeof (template)); 26090 26091 ip_mibkp->ks_update = ip_kstat_update; 26092 26093 kstat_install(ip_mibkp); 26094 } 26095 26096 static void 26097 ip_kstat_fini(void) 26098 { 26099 26100 if (ip_mibkp != NULL) { 26101 kstat_delete(ip_mibkp); 26102 ip_mibkp = NULL; 26103 } 26104 } 26105 26106 static int 26107 ip_kstat_update(kstat_t *kp, int rw) 26108 { 26109 ip_named_kstat_t *ipkp; 26110 26111 if (!kp || !kp->ks_data) 26112 return (EIO); 26113 26114 if (rw == KSTAT_WRITE) 26115 return (EACCES); 26116 26117 ipkp = (ip_named_kstat_t *)kp->ks_data; 26118 26119 ipkp->forwarding.value.ui32 = ip_mib.ipForwarding; 26120 ipkp->defaultTTL.value.ui32 = ip_mib.ipDefaultTTL; 26121 ipkp->inReceives.value.ui32 = ip_mib.ipInReceives; 26122 ipkp->inHdrErrors.value.ui32 = ip_mib.ipInHdrErrors; 26123 ipkp->inAddrErrors.value.ui32 = ip_mib.ipInAddrErrors; 26124 ipkp->forwDatagrams.value.ui32 = ip_mib.ipForwDatagrams; 26125 ipkp->inUnknownProtos.value.ui32 = ip_mib.ipInUnknownProtos; 26126 ipkp->inDiscards.value.ui32 = ip_mib.ipInDiscards; 26127 ipkp->inDelivers.value.ui32 = ip_mib.ipInDelivers; 26128 ipkp->outRequests.value.ui32 = ip_mib.ipOutRequests; 26129 ipkp->outDiscards.value.ui32 = ip_mib.ipOutDiscards; 26130 ipkp->outNoRoutes.value.ui32 = ip_mib.ipOutNoRoutes; 26131 ipkp->reasmTimeout.value.ui32 = ip_mib.ipReasmTimeout; 26132 ipkp->reasmReqds.value.ui32 = ip_mib.ipReasmReqds; 26133 ipkp->reasmOKs.value.ui32 = ip_mib.ipReasmOKs; 26134 ipkp->reasmFails.value.ui32 = ip_mib.ipReasmFails; 26135 ipkp->fragOKs.value.ui32 = ip_mib.ipFragOKs; 26136 ipkp->fragFails.value.ui32 = ip_mib.ipFragFails; 26137 ipkp->fragCreates.value.ui32 = ip_mib.ipFragCreates; 26138 26139 ipkp->routingDiscards.value.ui32 = ip_mib.ipRoutingDiscards; 26140 ipkp->inErrs.value.ui32 = ip_mib.tcpInErrs; 26141 ipkp->noPorts.value.ui32 = ip_mib.udpNoPorts; 26142 ipkp->inCksumErrs.value.ui32 = ip_mib.ipInCksumErrs; 26143 ipkp->reasmDuplicates.value.ui32 = ip_mib.ipReasmDuplicates; 26144 ipkp->reasmPartDups.value.ui32 = ip_mib.ipReasmPartDups; 26145 ipkp->forwProhibits.value.ui32 = ip_mib.ipForwProhibits; 26146 ipkp->udpInCksumErrs.value.ui32 = ip_mib.udpInCksumErrs; 26147 ipkp->udpInOverflows.value.ui32 = ip_mib.udpInOverflows; 26148 ipkp->rawipInOverflows.value.ui32 = ip_mib.rawipInOverflows; 26149 ipkp->ipsecInSucceeded.value.ui32 = ip_mib.ipsecInSucceeded; 26150 ipkp->ipsecInFailed.value.i32 = ip_mib.ipsecInFailed; 26151 26152 ipkp->inIPv6.value.ui32 = ip_mib.ipInIPv6; 26153 ipkp->outIPv6.value.ui32 = ip_mib.ipOutIPv6; 26154 ipkp->outSwitchIPv6.value.ui32 = ip_mib.ipOutSwitchIPv6; 26155 26156 return (0); 26157 } 26158 26159 static void 26160 icmp_kstat_init(void) 26161 { 26162 icmp_named_kstat_t template = { 26163 { "inMsgs", KSTAT_DATA_UINT32 }, 26164 { "inErrors", KSTAT_DATA_UINT32 }, 26165 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 26166 { "inTimeExcds", KSTAT_DATA_UINT32 }, 26167 { "inParmProbs", KSTAT_DATA_UINT32 }, 26168 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 26169 { "inRedirects", KSTAT_DATA_UINT32 }, 26170 { "inEchos", KSTAT_DATA_UINT32 }, 26171 { "inEchoReps", KSTAT_DATA_UINT32 }, 26172 { "inTimestamps", KSTAT_DATA_UINT32 }, 26173 { "inTimestampReps", KSTAT_DATA_UINT32 }, 26174 { "inAddrMasks", KSTAT_DATA_UINT32 }, 26175 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 26176 { "outMsgs", KSTAT_DATA_UINT32 }, 26177 { "outErrors", KSTAT_DATA_UINT32 }, 26178 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 26179 { "outTimeExcds", KSTAT_DATA_UINT32 }, 26180 { "outParmProbs", KSTAT_DATA_UINT32 }, 26181 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 26182 { "outRedirects", KSTAT_DATA_UINT32 }, 26183 { "outEchos", KSTAT_DATA_UINT32 }, 26184 { "outEchoReps", KSTAT_DATA_UINT32 }, 26185 { "outTimestamps", KSTAT_DATA_UINT32 }, 26186 { "outTimestampReps", KSTAT_DATA_UINT32 }, 26187 { "outAddrMasks", KSTAT_DATA_UINT32 }, 26188 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 26189 { "inChksumErrs", KSTAT_DATA_UINT32 }, 26190 { "inUnknowns", KSTAT_DATA_UINT32 }, 26191 { "inFragNeeded", KSTAT_DATA_UINT32 }, 26192 { "outFragNeeded", KSTAT_DATA_UINT32 }, 26193 { "outDrops", KSTAT_DATA_UINT32 }, 26194 { "inOverFlows", KSTAT_DATA_UINT32 }, 26195 { "inBadRedirects", KSTAT_DATA_UINT32 }, 26196 }; 26197 26198 icmp_mibkp = kstat_create("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 26199 NUM_OF_FIELDS(icmp_named_kstat_t), 26200 0); 26201 if (icmp_mibkp == NULL) 26202 return; 26203 26204 bcopy(&template, icmp_mibkp->ks_data, sizeof (template)); 26205 26206 icmp_mibkp->ks_update = icmp_kstat_update; 26207 26208 kstat_install(icmp_mibkp); 26209 } 26210 26211 static void 26212 icmp_kstat_fini(void) 26213 { 26214 26215 if (icmp_mibkp != NULL) { 26216 kstat_delete(icmp_mibkp); 26217 icmp_mibkp = NULL; 26218 } 26219 } 26220 26221 static int 26222 icmp_kstat_update(kstat_t *kp, int rw) 26223 { 26224 icmp_named_kstat_t *icmpkp; 26225 26226 if ((kp == NULL) || (kp->ks_data == NULL)) 26227 return (EIO); 26228 26229 if (rw == KSTAT_WRITE) 26230 return (EACCES); 26231 26232 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 26233 26234 icmpkp->inMsgs.value.ui32 = icmp_mib.icmpInMsgs; 26235 icmpkp->inErrors.value.ui32 = icmp_mib.icmpInErrors; 26236 icmpkp->inDestUnreachs.value.ui32 = icmp_mib.icmpInDestUnreachs; 26237 icmpkp->inTimeExcds.value.ui32 = icmp_mib.icmpInTimeExcds; 26238 icmpkp->inParmProbs.value.ui32 = icmp_mib.icmpInParmProbs; 26239 icmpkp->inSrcQuenchs.value.ui32 = icmp_mib.icmpInSrcQuenchs; 26240 icmpkp->inRedirects.value.ui32 = icmp_mib.icmpInRedirects; 26241 icmpkp->inEchos.value.ui32 = icmp_mib.icmpInEchos; 26242 icmpkp->inEchoReps.value.ui32 = icmp_mib.icmpInEchoReps; 26243 icmpkp->inTimestamps.value.ui32 = icmp_mib.icmpInTimestamps; 26244 icmpkp->inTimestampReps.value.ui32 = icmp_mib.icmpInTimestampReps; 26245 icmpkp->inAddrMasks.value.ui32 = icmp_mib.icmpInAddrMasks; 26246 icmpkp->inAddrMaskReps.value.ui32 = icmp_mib.icmpInAddrMaskReps; 26247 icmpkp->outMsgs.value.ui32 = icmp_mib.icmpOutMsgs; 26248 icmpkp->outErrors.value.ui32 = icmp_mib.icmpOutErrors; 26249 icmpkp->outDestUnreachs.value.ui32 = icmp_mib.icmpOutDestUnreachs; 26250 icmpkp->outTimeExcds.value.ui32 = icmp_mib.icmpOutTimeExcds; 26251 icmpkp->outParmProbs.value.ui32 = icmp_mib.icmpOutParmProbs; 26252 icmpkp->outSrcQuenchs.value.ui32 = icmp_mib.icmpOutSrcQuenchs; 26253 icmpkp->outRedirects.value.ui32 = icmp_mib.icmpOutRedirects; 26254 icmpkp->outEchos.value.ui32 = icmp_mib.icmpOutEchos; 26255 icmpkp->outEchoReps.value.ui32 = icmp_mib.icmpOutEchoReps; 26256 icmpkp->outTimestamps.value.ui32 = icmp_mib.icmpOutTimestamps; 26257 icmpkp->outTimestampReps.value.ui32 = icmp_mib.icmpOutTimestampReps; 26258 icmpkp->outAddrMasks.value.ui32 = icmp_mib.icmpOutAddrMasks; 26259 icmpkp->outAddrMaskReps.value.ui32 = icmp_mib.icmpOutAddrMaskReps; 26260 icmpkp->inCksumErrs.value.ui32 = icmp_mib.icmpInCksumErrs; 26261 icmpkp->inUnknowns.value.ui32 = icmp_mib.icmpInUnknowns; 26262 icmpkp->inFragNeeded.value.ui32 = icmp_mib.icmpInFragNeeded; 26263 icmpkp->outFragNeeded.value.ui32 = icmp_mib.icmpOutFragNeeded; 26264 icmpkp->outDrops.value.ui32 = icmp_mib.icmpOutDrops; 26265 icmpkp->inOverflows.value.ui32 = icmp_mib.icmpInOverflows; 26266 icmpkp->inBadRedirects.value.ui32 = icmp_mib.icmpInBadRedirects; 26267 26268 return (0); 26269 } 26270 26271 /* 26272 * This is the fanout function for raw socket opened for SCTP. Note 26273 * that it is called after SCTP checks that there is no socket which 26274 * wants a packet. Then before SCTP handles this out of the blue packet, 26275 * this function is called to see if there is any raw socket for SCTP. 26276 * If there is and it is bound to the correct address, the packet will 26277 * be sent to that socket. Note that only one raw socket can be bound to 26278 * a port. This is assured in ipcl_sctp_hash_insert(); 26279 */ 26280 void 26281 ip_fanout_sctp_raw(mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, boolean_t isv4, 26282 uint32_t ports, boolean_t mctl_present, uint_t flags, boolean_t ip_policy, 26283 uint_t ipif_seqid, zoneid_t zoneid) 26284 { 26285 conn_t *connp; 26286 queue_t *rq; 26287 mblk_t *first_mp; 26288 boolean_t secure; 26289 ip6_t *ip6h; 26290 26291 first_mp = mp; 26292 if (mctl_present) { 26293 mp = first_mp->b_cont; 26294 secure = ipsec_in_is_secure(first_mp); 26295 ASSERT(mp != NULL); 26296 } else { 26297 secure = B_FALSE; 26298 } 26299 ip6h = (isv4) ? NULL : (ip6_t *)ipha; 26300 26301 connp = ipcl_classify_raw(IPPROTO_SCTP, zoneid, ports, ipha); 26302 if (connp == NULL) { 26303 sctp_ootb_input(first_mp, recv_ill, ipif_seqid, zoneid, 26304 mctl_present); 26305 return; 26306 } 26307 rq = connp->conn_rq; 26308 if (!canputnext(rq)) { 26309 CONN_DEC_REF(connp); 26310 BUMP_MIB(&ip_mib, rawipInOverflows); 26311 freemsg(first_mp); 26312 return; 26313 } 26314 if ((isv4 ? CONN_INBOUND_POLICY_PRESENT(connp) : 26315 CONN_INBOUND_POLICY_PRESENT_V6(connp)) || secure) { 26316 first_mp = ipsec_check_inbound_policy(first_mp, connp, 26317 (isv4 ? ipha : NULL), ip6h, mctl_present); 26318 if (first_mp == NULL) { 26319 CONN_DEC_REF(connp); 26320 return; 26321 } 26322 } 26323 /* 26324 * We probably should not send M_CTL message up to 26325 * raw socket. 26326 */ 26327 if (mctl_present) 26328 freeb(first_mp); 26329 26330 /* Initiate IPPF processing here if needed. */ 26331 if ((isv4 && IPP_ENABLED(IPP_LOCAL_IN) && ip_policy) || 26332 (!isv4 && IP6_IN_IPP(flags))) { 26333 ip_process(IPP_LOCAL_IN, &mp, 26334 recv_ill->ill_phyint->phyint_ifindex); 26335 if (mp == NULL) { 26336 CONN_DEC_REF(connp); 26337 return; 26338 } 26339 } 26340 26341 if (connp->conn_recvif || connp->conn_recvslla || 26342 ((connp->conn_ipv6_recvpktinfo || 26343 (!isv4 && IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) && 26344 (flags & IP_FF_IP6INFO))) { 26345 int in_flags = 0; 26346 26347 if (connp->conn_recvif || connp->conn_ipv6_recvpktinfo) { 26348 in_flags = IPF_RECVIF; 26349 } 26350 if (connp->conn_recvslla) { 26351 in_flags |= IPF_RECVSLLA; 26352 } 26353 if (isv4) { 26354 mp = ip_add_info(mp, recv_ill, in_flags); 26355 } else { 26356 mp = ip_add_info_v6(mp, recv_ill, &ip6h->ip6_dst); 26357 if (mp == NULL) { 26358 CONN_DEC_REF(connp); 26359 return; 26360 } 26361 } 26362 } 26363 26364 BUMP_MIB(&ip_mib, ipInDelivers); 26365 /* 26366 * We are sending the IPSEC_IN message also up. Refer 26367 * to comments above this function. 26368 */ 26369 putnext(rq, mp); 26370 CONN_DEC_REF(connp); 26371 } 26372 26373 /* 26374 * Martian Address Filtering [RFC 1812, Section 5.3.7] 26375 */ 26376 static boolean_t 26377 ip_no_forward(ipha_t *ipha, ill_t *ill) 26378 { 26379 ipaddr_t ip_src, ip_dst; 26380 ire_t *src_ire = NULL; 26381 26382 ip_src = ntohl(ipha->ipha_src); 26383 ip_dst = ntohl(ipha->ipha_dst); 26384 26385 if (ip_dst == INADDR_ANY) 26386 goto dont_forward; 26387 26388 if (IN_CLASSD(ip_src)) 26389 goto dont_forward; 26390 26391 if ((ip_src >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 26392 goto dont_forward; 26393 26394 if (IN_BADCLASS(ip_dst)) 26395 goto dont_forward; 26396 26397 src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, 26398 ALL_ZONES, MATCH_IRE_TYPE); 26399 if (src_ire != NULL) { 26400 ire_refrele(src_ire); 26401 goto dont_forward; 26402 } 26403 26404 return (B_FALSE); 26405 26406 dont_forward: 26407 if (ip_debug > 2) { 26408 printf("ip_no_forward: dropping packet received on %s\n", 26409 ill->ill_name); 26410 pr_addr_dbg("ip_no_forward: from src %s\n", 26411 AF_INET, &ipha->ipha_src); 26412 pr_addr_dbg("ip_no_forward: to dst %s\n", 26413 AF_INET, &ipha->ipha_dst); 26414 } 26415 BUMP_MIB(&ip_mib, ipForwProhibits); 26416 return (B_TRUE); 26417 } 26418 26419 static boolean_t 26420 ip_loopback_src_or_dst(ipha_t *ipha, ill_t *ill) 26421 { 26422 if (((ntohl(ipha->ipha_src) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) || 26423 ((ntohl(ipha->ipha_dst) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)) { 26424 if (ip_debug > 2) { 26425 if (ill != NULL) { 26426 printf("ip_loopback_src_or_dst: " 26427 "dropping packet received on %s\n", 26428 ill->ill_name); 26429 } else { 26430 printf("ip_loopback_src_or_dst: " 26431 "dropping packet\n"); 26432 } 26433 26434 pr_addr_dbg( 26435 "ip_loopback_src_or_dst: from src %s\n", 26436 AF_INET, &ipha->ipha_src); 26437 pr_addr_dbg( 26438 "ip_loopback_src_or_dst: to dst %s\n", 26439 AF_INET, &ipha->ipha_dst); 26440 } 26441 26442 BUMP_MIB(&ip_mib, ipInAddrErrors); 26443 return (B_TRUE); 26444 } 26445 return (B_FALSE); 26446 } 26447