1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* Copyright (c) 1990 Mentat Inc. */ 27 28 #include <sys/types.h> 29 #include <sys/stream.h> 30 #include <sys/dlpi.h> 31 #include <sys/stropts.h> 32 #include <sys/sysmacros.h> 33 #include <sys/strsubr.h> 34 #include <sys/strlog.h> 35 #include <sys/strsun.h> 36 #include <sys/zone.h> 37 #define _SUN_TPI_VERSION 2 38 #include <sys/tihdr.h> 39 #include <sys/xti_inet.h> 40 #include <sys/ddi.h> 41 #include <sys/suntpi.h> 42 #include <sys/cmn_err.h> 43 #include <sys/debug.h> 44 #include <sys/kobj.h> 45 #include <sys/modctl.h> 46 #include <sys/atomic.h> 47 #include <sys/policy.h> 48 #include <sys/priv.h> 49 #include <sys/taskq.h> 50 51 #include <sys/systm.h> 52 #include <sys/param.h> 53 #include <sys/kmem.h> 54 #include <sys/sdt.h> 55 #include <sys/socket.h> 56 #include <sys/vtrace.h> 57 #include <sys/isa_defs.h> 58 #include <sys/mac.h> 59 #include <net/if.h> 60 #include <net/if_arp.h> 61 #include <net/route.h> 62 #include <sys/sockio.h> 63 #include <netinet/in.h> 64 #include <net/if_dl.h> 65 66 #include <inet/common.h> 67 #include <inet/mi.h> 68 #include <inet/mib2.h> 69 #include <inet/nd.h> 70 #include <inet/arp.h> 71 #include <inet/snmpcom.h> 72 #include <inet/optcom.h> 73 #include <inet/kstatcom.h> 74 75 #include <netinet/igmp_var.h> 76 #include <netinet/ip6.h> 77 #include <netinet/icmp6.h> 78 #include <netinet/sctp.h> 79 80 #include <inet/ip.h> 81 #include <inet/ip_impl.h> 82 #include <inet/ip6.h> 83 #include <inet/ip6_asp.h> 84 #include <inet/tcp.h> 85 #include <inet/tcp_impl.h> 86 #include <inet/ip_multi.h> 87 #include <inet/ip_if.h> 88 #include <inet/ip_ire.h> 89 #include <inet/ip_ftable.h> 90 #include <inet/ip_rts.h> 91 #include <inet/ip_ndp.h> 92 #include <inet/ip_listutils.h> 93 #include <netinet/igmp.h> 94 #include <netinet/ip_mroute.h> 95 #include <inet/ipp_common.h> 96 97 #include <net/pfkeyv2.h> 98 #include <inet/sadb.h> 99 #include <inet/ipsec_impl.h> 100 #include <inet/iptun/iptun_impl.h> 101 #include <inet/ipdrop.h> 102 #include <inet/ip_netinfo.h> 103 #include <inet/ilb_ip.h> 104 105 #include <sys/ethernet.h> 106 #include <net/if_types.h> 107 #include <sys/cpuvar.h> 108 109 #include <ipp/ipp.h> 110 #include <ipp/ipp_impl.h> 111 #include <ipp/ipgpc/ipgpc.h> 112 113 #include <sys/pattr.h> 114 #include <inet/ipclassifier.h> 115 #include <inet/sctp_ip.h> 116 #include <inet/sctp/sctp_impl.h> 117 #include <inet/udp_impl.h> 118 #include <inet/rawip_impl.h> 119 #include <inet/rts_impl.h> 120 121 #include <sys/tsol/label.h> 122 #include <sys/tsol/tnet.h> 123 124 #include <sys/squeue_impl.h> 125 #include <inet/ip_arp.h> 126 127 #include <sys/clock_impl.h> /* For LBOLT_FASTPATH{,64} */ 128 129 /* 130 * Values for squeue switch: 131 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN 132 * IP_SQUEUE_ENTER: SQ_PROCESS 133 * IP_SQUEUE_FILL: SQ_FILL 134 */ 135 int ip_squeue_enter = IP_SQUEUE_ENTER; /* Setable in /etc/system */ 136 137 int ip_squeue_flag; 138 139 /* 140 * Setable in /etc/system 141 */ 142 int ip_poll_normal_ms = 100; 143 int ip_poll_normal_ticks = 0; 144 int ip_modclose_ackwait_ms = 3000; 145 146 /* 147 * It would be nice to have these present only in DEBUG systems, but the 148 * current design of the global symbol checking logic requires them to be 149 * unconditionally present. 150 */ 151 uint_t ip_thread_data; /* TSD key for debug support */ 152 krwlock_t ip_thread_rwlock; 153 list_t ip_thread_list; 154 155 /* 156 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. 157 */ 158 159 struct listptr_s { 160 mblk_t *lp_head; /* pointer to the head of the list */ 161 mblk_t *lp_tail; /* pointer to the tail of the list */ 162 }; 163 164 typedef struct listptr_s listptr_t; 165 166 /* 167 * This is used by ip_snmp_get_mib2_ip_route_media and 168 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. 169 */ 170 typedef struct iproutedata_s { 171 uint_t ird_idx; 172 uint_t ird_flags; /* see below */ 173 listptr_t ird_route; /* ipRouteEntryTable */ 174 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ 175 listptr_t ird_attrs; /* ipRouteAttributeTable */ 176 } iproutedata_t; 177 178 /* Include ire_testhidden and IRE_IF_CLONE routes */ 179 #define IRD_REPORT_ALL 0x01 180 181 /* 182 * Cluster specific hooks. These should be NULL when booted as a non-cluster 183 */ 184 185 /* 186 * Hook functions to enable cluster networking 187 * On non-clustered systems these vectors must always be NULL. 188 * 189 * Hook function to Check ip specified ip address is a shared ip address 190 * in the cluster 191 * 192 */ 193 int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 194 sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL; 195 196 /* 197 * Hook function to generate cluster wide ip fragment identifier 198 */ 199 uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 200 sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp, 201 void *args) = NULL; 202 203 /* 204 * Hook function to generate cluster wide SPI. 205 */ 206 void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t, 207 void *) = NULL; 208 209 /* 210 * Hook function to verify if the SPI is already utlized. 211 */ 212 213 int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL; 214 215 /* 216 * Hook function to delete the SPI from the cluster wide repository. 217 */ 218 219 void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL; 220 221 /* 222 * Hook function to inform the cluster when packet received on an IDLE SA 223 */ 224 225 void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t, 226 in6_addr_t, in6_addr_t, void *) = NULL; 227 228 /* 229 * Synchronization notes: 230 * 231 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any 232 * MT level protection given by STREAMS. IP uses a combination of its own 233 * internal serialization mechanism and standard Solaris locking techniques. 234 * The internal serialization is per phyint. This is used to serialize 235 * plumbing operations, IPMP operations, most set ioctls, etc. 236 * 237 * Plumbing is a long sequence of operations involving message 238 * exchanges between IP, ARP and device drivers. Many set ioctls are typically 239 * involved in plumbing operations. A natural model is to serialize these 240 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in 241 * parallel without any interference. But various set ioctls on hme0 are best 242 * serialized, along with IPMP operations and processing of DLPI control 243 * messages received from drivers on a per phyint basis. This serialization is 244 * provided by the ipsq_t and primitives operating on this. Details can 245 * be found in ip_if.c above the core primitives operating on ipsq_t. 246 * 247 * Lookups of an ipif or ill by a thread return a refheld ipif / ill. 248 * Simiarly lookup of an ire by a thread also returns a refheld ire. 249 * In addition ipif's and ill's referenced by the ire are also indirectly 250 * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld 251 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the 252 * address of an ipif has to go through the ipsq_t. This ensures that only 253 * one such exclusive operation proceeds at any time on the ipif. It then 254 * waits for all refcnts 255 * associated with this ipif to come down to zero. The address is changed 256 * only after the ipif has been quiesced. Then the ipif is brought up again. 257 * More details are described above the comment in ip_sioctl_flags. 258 * 259 * Packet processing is based mostly on IREs and are fully multi-threaded 260 * using standard Solaris MT techniques. 261 * 262 * There are explicit locks in IP to handle: 263 * - The ip_g_head list maintained by mi_open_link() and friends. 264 * 265 * - The reassembly data structures (one lock per hash bucket) 266 * 267 * - conn_lock is meant to protect conn_t fields. The fields actually 268 * protected by conn_lock are documented in the conn_t definition. 269 * 270 * - ire_lock to protect some of the fields of the ire, IRE tables 271 * (one lock per hash bucket). Refer to ip_ire.c for details. 272 * 273 * - ndp_g_lock and ncec_lock for protecting NCEs. 274 * 275 * - ill_lock protects fields of the ill and ipif. Details in ip.h 276 * 277 * - ill_g_lock: This is a global reader/writer lock. Protects the following 278 * * The AVL tree based global multi list of all ills. 279 * * The linked list of all ipifs of an ill 280 * * The <ipsq-xop> mapping 281 * * <ill-phyint> association 282 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif 283 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the 284 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as 285 * writer for the actual duration of the insertion/deletion/change. 286 * 287 * - ill_lock: This is a per ill mutex. 288 * It protects some members of the ill_t struct; see ip.h for details. 289 * It also protects the <ill-phyint> assoc. 290 * It also protects the list of ipifs hanging off the ill. 291 * 292 * - ipsq_lock: This is a per ipsq_t mutex lock. 293 * This protects some members of the ipsq_t struct; see ip.h for details. 294 * It also protects the <ipsq-ipxop> mapping 295 * 296 * - ipx_lock: This is a per ipxop_t mutex lock. 297 * This protects some members of the ipxop_t struct; see ip.h for details. 298 * 299 * - phyint_lock: This is a per phyint mutex lock. Protects just the 300 * phyint_flags 301 * 302 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. 303 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the 304 * uniqueness check also done atomically. 305 * 306 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc 307 * group list linked by ill_usesrc_grp_next. It also protects the 308 * ill_usesrc_ifindex field. It is taken as a writer when a member of the 309 * group is being added or deleted. This lock is taken as a reader when 310 * walking the list/group(eg: to get the number of members in a usesrc group). 311 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next 312 * field is changing state i.e from NULL to non-NULL or vice-versa. For 313 * example, it is not necessary to take this lock in the initial portion 314 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these 315 * operations are executed exclusively and that ensures that the "usesrc 316 * group state" cannot change. The "usesrc group state" change can happen 317 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete. 318 * 319 * Changing <ill-phyint>, <ipsq-xop> assocications: 320 * 321 * To change the <ill-phyint> association, the ill_g_lock must be held 322 * as writer, and the ill_locks of both the v4 and v6 instance of the ill 323 * must be held. 324 * 325 * To change the <ipsq-xop> association, the ill_g_lock must be held as 326 * writer, the ipsq_lock must be held, and one must be writer on the ipsq. 327 * This is only done when ills are added or removed from IPMP groups. 328 * 329 * To add or delete an ipif from the list of ipifs hanging off the ill, 330 * ill_g_lock (writer) and ill_lock must be held and the thread must be 331 * a writer on the associated ipsq. 332 * 333 * To add or delete an ill to the system, the ill_g_lock must be held as 334 * writer and the thread must be a writer on the associated ipsq. 335 * 336 * To add or delete an ilm to an ill, the ill_lock must be held and the thread 337 * must be a writer on the associated ipsq. 338 * 339 * Lock hierarchy 340 * 341 * Some lock hierarchy scenarios are listed below. 342 * 343 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock 344 * ill_g_lock -> ill_lock(s) -> phyint_lock 345 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock 346 * ill_g_lock -> ip_addr_avail_lock 347 * conn_lock -> irb_lock -> ill_lock -> ire_lock 348 * ill_g_lock -> ip_g_nd_lock 349 * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock 350 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock 351 * arl_lock -> ill_lock 352 * ips_ire_dep_lock -> irb_lock 353 * 354 * When more than 1 ill lock is needed to be held, all ill lock addresses 355 * are sorted on address and locked starting from highest addressed lock 356 * downward. 357 * 358 * Multicast scenarios 359 * ips_ill_g_lock -> ill_mcast_lock 360 * conn_ilg_lock -> ips_ill_g_lock -> ill_lock 361 * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock 362 * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock 363 * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock 364 * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock 365 * 366 * IPsec scenarios 367 * 368 * ipsa_lock -> ill_g_lock -> ill_lock 369 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock 370 * 371 * Trusted Solaris scenarios 372 * 373 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock 374 * igsa_lock -> gcdb_lock 375 * gcgrp_rwlock -> ire_lock 376 * gcgrp_rwlock -> gcdb_lock 377 * 378 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking 379 * 380 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock 381 * sq_lock -> conn_lock -> QLOCK(q) 382 * ill_lock -> ft_lock -> fe_lock 383 * 384 * Routing/forwarding table locking notes: 385 * 386 * Lock acquisition order: Radix tree lock, irb_lock. 387 * Requirements: 388 * i. Walker must not hold any locks during the walker callback. 389 * ii Walker must not see a truncated tree during the walk because of any node 390 * deletion. 391 * iii Existing code assumes ire_bucket is valid if it is non-null and is used 392 * in many places in the code to walk the irb list. Thus even if all the 393 * ires in a bucket have been deleted, we still can't free the radix node 394 * until the ires have actually been inactive'd (freed). 395 * 396 * Tree traversal - Need to hold the global tree lock in read mode. 397 * Before dropping the global tree lock, need to either increment the ire_refcnt 398 * to ensure that the radix node can't be deleted. 399 * 400 * Tree add - Need to hold the global tree lock in write mode to add a 401 * radix node. To prevent the node from being deleted, increment the 402 * irb_refcnt, after the node is added to the tree. The ire itself is 403 * added later while holding the irb_lock, but not the tree lock. 404 * 405 * Tree delete - Need to hold the global tree lock and irb_lock in write mode. 406 * All associated ires must be inactive (i.e. freed), and irb_refcnt 407 * must be zero. 408 * 409 * Walker - Increment irb_refcnt before calling the walker callback. Hold the 410 * global tree lock (read mode) for traversal. 411 * 412 * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele 413 * hence we will acquire irb_lock while holding ips_ire_dep_lock. 414 * 415 * IPsec notes : 416 * 417 * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes 418 * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the 419 * ip_xmit_attr_t has the 420 * information used by the IPsec code for applying the right level of 421 * protection. The information initialized by IP in the ip_xmit_attr_t 422 * is determined by the per-socket policy or global policy in the system. 423 * For inbound datagrams, the ip_recv_attr_t 424 * starts out with nothing in it. It gets filled 425 * with the right information if it goes through the AH/ESP code, which 426 * happens if the incoming packet is secure. The information initialized 427 * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether 428 * the policy requirements needed by per-socket policy or global policy 429 * is met or not. 430 * 431 * For fully connected sockets i.e dst, src [addr, port] is known, 432 * conn_policy_cached is set indicating that policy has been cached. 433 * conn_in_enforce_policy may or may not be set depending on whether 434 * there is a global policy match or per-socket policy match. 435 * Policy inheriting happpens in ip_policy_set once the destination is known. 436 * Once the right policy is set on the conn_t, policy cannot change for 437 * this socket. This makes life simpler for TCP (UDP ?) where 438 * re-transmissions go out with the same policy. For symmetry, policy 439 * is cached for fully connected UDP sockets also. Thus if policy is cached, 440 * it also implies that policy is latched i.e policy cannot change 441 * on these sockets. As we have the right policy on the conn, we don't 442 * have to lookup global policy for every outbound and inbound datagram 443 * and thus serving as an optimization. Note that a global policy change 444 * does not affect fully connected sockets if they have policy. If fully 445 * connected sockets did not have any policy associated with it, global 446 * policy change may affect them. 447 * 448 * IP Flow control notes: 449 * --------------------- 450 * Non-TCP streams are flow controlled by IP. The way this is accomplished 451 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When 452 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into 453 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS 454 * functions. 455 * 456 * Per Tx ring udp flow control: 457 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in 458 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true). 459 * 460 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer. 461 * To achieve best performance, outgoing traffic need to be fanned out among 462 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send 463 * traffic out of the NIC and it takes a fanout hint. UDP connections pass 464 * the address of connp as fanout hint to mac_tx(). Under flow controlled 465 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This 466 * cookie points to a specific Tx ring that is blocked. The cookie is used to 467 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t 468 * point to drain_lists (idl_t's). These drain list will store the blocked UDP 469 * connp's. The drain list is not a single list but a configurable number of 470 * lists. 471 * 472 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t 473 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE 474 * which is equal to 128. This array in turn contains a pointer to idl_t[], 475 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain 476 * list will point to the list of connp's that are flow controlled. 477 * 478 * --------------- ------- ------- ------- 479 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|--> 480 * | --------------- ------- ------- ------- 481 * | --------------- ------- ------- ------- 482 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|--> 483 * ---------------- | --------------- ------- ------- ------- 484 * |idl_tx_list[0]|->| --------------- ------- ------- ------- 485 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|--> 486 * | --------------- ------- ------- ------- 487 * . . . . . 488 * | --------------- ------- ------- ------- 489 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|--> 490 * --------------- ------- ------- ------- 491 * --------------- ------- ------- ------- 492 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|--> 493 * | --------------- ------- ------- ------- 494 * | --------------- ------- ------- ------- 495 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|--> 496 * |idl_tx_list[1]|->| --------------- ------- ------- ------- 497 * ---------------- | . . . . 498 * | --------------- ------- ------- ------- 499 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|--> 500 * --------------- ------- ------- ------- 501 * ..... 502 * ---------------- 503 * |idl_tx_list[n]|-> ... 504 * ---------------- 505 * 506 * When mac_tx() returns a cookie, the cookie is used to hash into a 507 * idl_tx_list in ips_idl_tx_list[] array. Then conn_drain_insert() is 508 * called passing idl_tx_list. The connp gets inserted in a drain list 509 * pointed to by idl_tx_list. conn_drain_list() asserts flow control for 510 * the sockets (non stream based) and sets QFULL condition on the conn_wq 511 * of streams sockets, or the su_txqfull for non-streams sockets. 512 * connp->conn_direct_blocked will be set to indicate the blocked 513 * condition. 514 * 515 * GLDv3 mac layer calls ill_flow_enable() when flow control is relieved. 516 * A cookie is passed in the call to ill_flow_enable() that identifies the 517 * blocked Tx ring. This cookie is used to get to the idl_tx_list that 518 * contains the blocked connp's. conn_walk_drain() uses the idl_tx_list_t 519 * and goes through each conn in the drain list and calls conn_idl_remove 520 * for the conn to clear the qfull condition for the conn, as well as to 521 * remove the conn from the idl list. In addition, streams based sockets 522 * will have the conn_wq enabled, causing ip_wsrv to run for the 523 * conn. ip_wsrv drains the queued messages, and removes the conn from the 524 * drain list, if all messages were drained. It also notifies the 525 * conn_upcalls for the conn to signal that flow-control has opened up. 526 * 527 * In reality the drain list is not a single list, but a configurable number 528 * of lists. conn_walk_drain() in the IP module, notifies the conn_upcalls for 529 * each conn in the list. conn_drain_insert and conn_drain_tail are the only 530 * functions that manipulate this drain list. conn_drain_insert is called in 531 * from the protocol layer when conn_ip_output returns EWOULDBLOCK. 532 * (as opposed to from ip_wsrv context for STREAMS 533 * case -- see below). The synchronization between drain insertion and flow 534 * control wakeup is handled by using idl_txl->txl_lock. 535 * 536 * Flow control using STREAMS: 537 * When ILL_DIRECT_CAPABLE() is not TRUE, STREAMS flow control mechanism 538 * is used. On the send side, if the packet cannot be sent down to the 539 * driver by IP, because of a canput failure, ip_xmit drops the packet 540 * and returns EWOULDBLOCK to the caller, who may then invoke 541 * ixa_check_drain_insert to insert the conn on the 0'th drain list. 542 * When ip_wsrv runs on the ill_wq because flow control has been relieved, the 543 * blocked conns in the * 0'th drain list is drained as with the 544 * non-STREAMS case. 545 * 546 * In both the STREAMS and non-STREAMS case, the sockfs upcall to set 547 * qfull is done when the conn is inserted into the drain list 548 * (conn_drain_insert()) and cleared when the conn is removed from the drain 549 * list (conn_idl_remove()). 550 * 551 * IPQOS notes: 552 * 553 * IPQoS Policies are applied to packets using IPPF (IP Policy framework) 554 * and IPQoS modules. IPPF includes hooks in IP at different control points 555 * (callout positions) which direct packets to IPQoS modules for policy 556 * processing. Policies, if present, are global. 557 * 558 * The callout positions are located in the following paths: 559 * o local_in (packets destined for this host) 560 * o local_out (packets orginating from this host ) 561 * o fwd_in (packets forwarded by this m/c - inbound) 562 * o fwd_out (packets forwarded by this m/c - outbound) 563 * Hooks at these callout points can be enabled/disabled using the ndd variable 564 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). 565 * By default all the callout positions are enabled. 566 * 567 * Outbound (local_out) 568 * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6. 569 * 570 * Inbound (local_in) 571 * Hooks are placed in ip_fanout_v4 and ip_fanout_v6. 572 * 573 * Forwarding (in and out) 574 * Hooks are placed in ire_recv_forward_v4/v6. 575 * 576 * IP Policy Framework processing (IPPF processing) 577 * Policy processing for a packet is initiated by ip_process, which ascertains 578 * that the classifier (ipgpc) is loaded and configured, failing which the 579 * packet resumes normal processing in IP. If the clasifier is present, the 580 * packet is acted upon by one or more IPQoS modules (action instances), per 581 * filters configured in ipgpc and resumes normal IP processing thereafter. 582 * An action instance can drop a packet in course of its processing. 583 * 584 * Zones notes: 585 * 586 * The partitioning rules for networking are as follows: 587 * 1) Packets coming from a zone must have a source address belonging to that 588 * zone. 589 * 2) Packets coming from a zone can only be sent on a physical interface on 590 * which the zone has an IP address. 591 * 3) Between two zones on the same machine, packet delivery is only allowed if 592 * there's a matching route for the destination and zone in the forwarding 593 * table. 594 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in 595 * different zones can bind to the same port with the wildcard address 596 * (INADDR_ANY). 597 * 598 * The granularity of interface partitioning is at the logical interface level. 599 * Therefore, every zone has its own IP addresses, and incoming packets can be 600 * attributed to a zone unambiguously. A logical interface is placed into a zone 601 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t 602 * structure. Rule (1) is implemented by modifying the source address selection 603 * algorithm so that the list of eligible addresses is filtered based on the 604 * sending process zone. 605 * 606 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared 607 * across all zones, depending on their type. Here is the break-up: 608 * 609 * IRE type Shared/exclusive 610 * -------- ---------------- 611 * IRE_BROADCAST Exclusive 612 * IRE_DEFAULT (default routes) Shared (*) 613 * IRE_LOCAL Exclusive (x) 614 * IRE_LOOPBACK Exclusive 615 * IRE_PREFIX (net routes) Shared (*) 616 * IRE_IF_NORESOLVER (interface routes) Exclusive 617 * IRE_IF_RESOLVER (interface routes) Exclusive 618 * IRE_IF_CLONE (interface routes) Exclusive 619 * IRE_HOST (host routes) Shared (*) 620 * 621 * (*) A zone can only use a default or off-subnet route if the gateway is 622 * directly reachable from the zone, that is, if the gateway's address matches 623 * one of the zone's logical interfaces. 624 * 625 * (x) IRE_LOCAL are handled a bit differently. 626 * When ip_restrict_interzone_loopback is set (the default), 627 * ire_route_recursive restricts loopback using an IRE_LOCAL 628 * between zone to the case when L2 would have conceptually looped the packet 629 * back, i.e. the loopback which is required since neither Ethernet drivers 630 * nor Ethernet hardware loops them back. This is the case when the normal 631 * routes (ignoring IREs with different zoneids) would send out the packet on 632 * the same ill as the ill with which is IRE_LOCAL is associated. 633 * 634 * Multiple zones can share a common broadcast address; typically all zones 635 * share the 255.255.255.255 address. Incoming as well as locally originated 636 * broadcast packets must be dispatched to all the zones on the broadcast 637 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial 638 * since some zones may not be on the 10.16.72/24 network. To handle this, each 639 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are 640 * sent to every zone that has an IRE_BROADCAST entry for the destination 641 * address on the input ill, see ip_input_broadcast(). 642 * 643 * Applications in different zones can join the same multicast group address. 644 * The same logic applies for multicast as for broadcast. ip_input_multicast 645 * dispatches packets to all zones that have members on the physical interface. 646 */ 647 648 /* 649 * Squeue Fanout flags: 650 * 0: No fanout. 651 * 1: Fanout across all squeues 652 */ 653 boolean_t ip_squeue_fanout = 0; 654 655 /* 656 * Maximum dups allowed per packet. 657 */ 658 uint_t ip_max_frag_dups = 10; 659 660 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag, 661 cred_t *credp, boolean_t isv6); 662 static mblk_t *ip_xmit_attach_llhdr(mblk_t *, nce_t *); 663 664 static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *); 665 static void icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *); 666 static void icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *, 667 ip_recv_attr_t *); 668 static void icmp_options_update(ipha_t *); 669 static void icmp_param_problem(mblk_t *, uint8_t, ip_recv_attr_t *); 670 static void icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *); 671 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *); 672 static void icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *, 673 ip_recv_attr_t *); 674 static void icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *); 675 static void icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *, 676 ip_recv_attr_t *); 677 678 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); 679 char *ip_dot_addr(ipaddr_t, char *); 680 mblk_t *ip_carve_mp(mblk_t **, ssize_t); 681 int ip_close(queue_t *, int); 682 static char *ip_dot_saddr(uchar_t *, char *); 683 static void ip_lrput(queue_t *, mblk_t *); 684 ipaddr_t ip_net_mask(ipaddr_t); 685 char *ip_nv_lookup(nv_t *, int); 686 void ip_rput(queue_t *, mblk_t *); 687 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, 688 void *dummy_arg); 689 int ip_snmp_get(queue_t *, mblk_t *, int); 690 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, 691 mib2_ipIfStatsEntry_t *, ip_stack_t *); 692 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *, 693 ip_stack_t *); 694 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *); 695 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst); 696 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst); 697 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst); 698 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst); 699 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *, 700 ip_stack_t *ipst); 701 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *, 702 ip_stack_t *ipst); 703 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *, 704 ip_stack_t *ipst); 705 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *, 706 ip_stack_t *ipst); 707 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *, 708 ip_stack_t *ipst); 709 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *, 710 ip_stack_t *ipst); 711 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *, 712 ip_stack_t *ipst); 713 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *, 714 ip_stack_t *ipst); 715 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int, 716 ip_stack_t *ipst); 717 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int, 718 ip_stack_t *ipst); 719 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); 720 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); 721 static int ip_snmp_get2_v4_media(ncec_t *, iproutedata_t *); 722 static int ip_snmp_get2_v6_media(ncec_t *, iproutedata_t *); 723 int ip_snmp_set(queue_t *, int, int, uchar_t *, int); 724 725 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *, 726 mblk_t *); 727 728 static void conn_drain_init(ip_stack_t *); 729 static void conn_drain_fini(ip_stack_t *); 730 static void conn_drain_tail(conn_t *connp, boolean_t closing); 731 732 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *); 733 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *); 734 735 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns); 736 static void ip_stack_shutdown(netstackid_t stackid, void *arg); 737 static void ip_stack_fini(netstackid_t stackid, void *arg); 738 739 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 740 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *), 741 ire_t *, conn_t *, boolean_t, const in6_addr_t *, mcast_record_t, 742 const in6_addr_t *); 743 744 static int ip_squeue_switch(int); 745 746 static void *ip_kstat_init(netstackid_t, ip_stack_t *); 747 static void ip_kstat_fini(netstackid_t, kstat_t *); 748 static int ip_kstat_update(kstat_t *kp, int rw); 749 static void *icmp_kstat_init(netstackid_t); 750 static void icmp_kstat_fini(netstackid_t, kstat_t *); 751 static int icmp_kstat_update(kstat_t *kp, int rw); 752 static void *ip_kstat2_init(netstackid_t, ip_stat_t *); 753 static void ip_kstat2_fini(netstackid_t, kstat_t *); 754 755 static void ipobs_init(ip_stack_t *); 756 static void ipobs_fini(ip_stack_t *); 757 758 ipaddr_t ip_g_all_ones = IP_HOST_MASK; 759 760 static long ip_rput_pullups; 761 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ 762 763 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */ 764 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */ 765 766 int ip_debug; 767 768 /* 769 * Multirouting/CGTP stuff 770 */ 771 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ 772 773 /* 774 * IP tunables related declarations. Definitions are in ip_tunables.c 775 */ 776 extern mod_prop_info_t ip_propinfo_tbl[]; 777 extern int ip_propinfo_count; 778 779 /* 780 * Table of IP ioctls encoding the various properties of the ioctl and 781 * indexed based on the last byte of the ioctl command. Occasionally there 782 * is a clash, and there is more than 1 ioctl with the same last byte. 783 * In such a case 1 ioctl is encoded in the ndx table and the remaining 784 * ioctls are encoded in the misc table. An entry in the ndx table is 785 * retrieved by indexing on the last byte of the ioctl command and comparing 786 * the ioctl command with the value in the ndx table. In the event of a 787 * mismatch the misc table is then searched sequentially for the desired 788 * ioctl command. 789 * 790 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func> 791 */ 792 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { 793 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 794 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 795 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 796 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 797 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 798 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 799 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 800 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 801 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 802 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 803 804 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, 805 MISC_CMD, ip_siocaddrt, NULL }, 806 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, 807 MISC_CMD, ip_siocdelrt, NULL }, 808 809 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 810 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 811 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD, 812 IF_CMD, ip_sioctl_get_addr, NULL }, 813 814 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 815 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 816 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), 817 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL }, 818 819 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), 820 IPI_PRIV | IPI_WR, 821 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 822 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), 823 IPI_MODOK | IPI_GET_CMD, 824 IF_CMD, ip_sioctl_get_flags, NULL }, 825 826 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 827 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 828 829 /* copyin size cannot be coded for SIOCGIFCONF */ 830 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD, 831 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 832 833 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 834 IF_CMD, ip_sioctl_mtu, NULL }, 835 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD, 836 IF_CMD, ip_sioctl_get_mtu, NULL }, 837 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), 838 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL }, 839 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 840 IF_CMD, ip_sioctl_brdaddr, NULL }, 841 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), 842 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL }, 843 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, 844 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 845 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), 846 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL }, 847 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, 848 IF_CMD, ip_sioctl_metric, NULL }, 849 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 850 851 /* See 166-168 below for extended SIOC*XARP ioctls */ 852 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR, 853 ARP_CMD, ip_sioctl_arp, NULL }, 854 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD, 855 ARP_CMD, ip_sioctl_arp, NULL }, 856 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR, 857 ARP_CMD, ip_sioctl_arp, NULL }, 858 859 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 860 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 861 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 862 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 863 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 864 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 865 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 866 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 867 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 868 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 869 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 870 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 871 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 872 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 873 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 874 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 875 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 876 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 877 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 878 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 879 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 880 881 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, 882 MISC_CMD, if_unitsel, if_unitsel_restart }, 883 884 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 885 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 886 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 887 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 888 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 889 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 890 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 891 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 892 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 893 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 894 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 895 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 896 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 897 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 898 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 899 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 900 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 901 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 902 903 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), 904 IPI_PRIV | IPI_WR | IPI_MODOK, 905 IF_CMD, ip_sioctl_sifname, NULL }, 906 907 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 908 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 909 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 910 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 911 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 912 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 913 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 914 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 915 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 916 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 917 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 918 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 919 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 920 921 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD, 922 MISC_CMD, ip_sioctl_get_ifnum, NULL }, 923 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD, 924 IF_CMD, ip_sioctl_get_muxid, NULL }, 925 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), 926 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL }, 927 928 /* Both if and lif variants share same func */ 929 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD, 930 IF_CMD, ip_sioctl_get_lifindex, NULL }, 931 /* Both if and lif variants share same func */ 932 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), 933 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL }, 934 935 /* copyin size cannot be coded for SIOCGIFCONF */ 936 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD, 937 MISC_CMD, ip_sioctl_get_ifconf, NULL }, 938 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 939 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 940 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 941 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 942 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 943 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 944 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 945 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 946 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 947 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 948 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 949 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 950 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 951 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 952 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 953 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 954 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 955 956 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), 957 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif, 958 ip_sioctl_removeif_restart }, 959 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), 960 IPI_GET_CMD | IPI_PRIV | IPI_WR, 961 LIF_CMD, ip_sioctl_addif, NULL }, 962 #define SIOCLIFADDR_NDX 112 963 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 964 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, 965 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), 966 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL }, 967 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 968 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, 969 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), 970 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL }, 971 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), 972 IPI_PRIV | IPI_WR, 973 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, 974 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), 975 IPI_GET_CMD | IPI_MODOK, 976 LIF_CMD, ip_sioctl_get_flags, NULL }, 977 978 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 979 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 980 981 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, 982 ip_sioctl_get_lifconf, NULL }, 983 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 984 LIF_CMD, ip_sioctl_mtu, NULL }, 985 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD, 986 LIF_CMD, ip_sioctl_get_mtu, NULL }, 987 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), 988 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL }, 989 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 990 LIF_CMD, ip_sioctl_brdaddr, NULL }, 991 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), 992 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL }, 993 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 994 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, 995 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), 996 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL }, 997 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 998 LIF_CMD, ip_sioctl_metric, NULL }, 999 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), 1000 IPI_PRIV | IPI_WR | IPI_MODOK, 1001 LIF_CMD, ip_sioctl_slifname, 1002 ip_sioctl_slifname_restart }, 1003 1004 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD, 1005 MISC_CMD, ip_sioctl_get_lifnum, NULL }, 1006 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), 1007 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL }, 1008 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), 1009 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL }, 1010 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), 1011 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 }, 1012 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), 1013 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 }, 1014 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1015 LIF_CMD, ip_sioctl_token, NULL }, 1016 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), 1017 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL }, 1018 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1019 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, 1020 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), 1021 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL }, 1022 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1023 LIF_CMD, ip_sioctl_lnkinfo, NULL }, 1024 1025 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), 1026 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, 1027 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, 1028 LIF_CMD, ip_siocdelndp_v6, NULL }, 1029 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, 1030 LIF_CMD, ip_siocqueryndp_v6, NULL }, 1031 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, 1032 LIF_CMD, ip_siocsetndp_v6, NULL }, 1033 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1034 MISC_CMD, ip_sioctl_tmyaddr, NULL }, 1035 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, 1036 MISC_CMD, ip_sioctl_tonlink, NULL }, 1037 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, 1038 MISC_CMD, ip_sioctl_tmysite, NULL }, 1039 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1040 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1041 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ 1042 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1043 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1044 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1045 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, 1046 1047 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1048 1049 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD, 1050 LIF_CMD, ip_sioctl_get_binding, NULL }, 1051 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), 1052 IPI_PRIV | IPI_WR, 1053 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, 1054 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), 1055 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL }, 1056 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t), 1057 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL }, 1058 1059 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ 1060 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1061 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1062 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1063 1064 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1065 1066 /* These are handled in ip_sioctl_copyin_setup itself */ 1067 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, 1068 MISC_CMD, NULL, NULL }, 1069 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, 1070 MISC_CMD, NULL, NULL }, 1071 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, 1072 1073 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, 1074 ip_sioctl_get_lifconf, NULL }, 1075 1076 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR, 1077 XARP_CMD, ip_sioctl_arp, NULL }, 1078 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD, 1079 XARP_CMD, ip_sioctl_arp, NULL }, 1080 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR, 1081 XARP_CMD, ip_sioctl_arp, NULL }, 1082 1083 /* SIOCPOPSOCKFS is not handled by IP */ 1084 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, 1085 1086 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), 1087 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL }, 1088 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), 1089 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone, 1090 ip_sioctl_slifzone_restart }, 1091 /* 172-174 are SCTP ioctls and not handled by IP */ 1092 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1093 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1094 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1095 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), 1096 IPI_GET_CMD, LIF_CMD, 1097 ip_sioctl_get_lifusesrc, 0 }, 1098 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), 1099 IPI_PRIV | IPI_WR, 1100 LIF_CMD, ip_sioctl_slifusesrc, 1101 NULL }, 1102 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, 1103 ip_sioctl_get_lifsrcof, NULL }, 1104 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, 1105 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1106 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0, 1107 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1108 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, 1109 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1110 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0, 1111 MSFILT_CMD, ip_sioctl_msfilter, NULL }, 1112 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, 1113 /* SIOCSENABLESDP is handled by SDP */ 1114 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL }, 1115 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL }, 1116 /* 185 */ { IPI_DONTCARE /* SIOCGIFHWADDR */, 0, 0, 0, NULL, NULL }, 1117 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL }, 1118 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD, 1119 ip_sioctl_ilb_cmd, NULL }, 1120 /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL }, 1121 /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL}, 1122 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq), 1123 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL }, 1124 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, 1125 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart } 1126 }; 1127 1128 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1129 1130 ip_ioctl_cmd_t ip_misc_ioctl_table[] = { 1131 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL }, 1132 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL }, 1133 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL }, 1134 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL }, 1135 { ND_GET, 0, 0, 0, NULL, NULL }, 1136 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL }, 1137 { IP_IOCTL, 0, 0, 0, NULL, NULL }, 1138 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD, 1139 MISC_CMD, mrt_ioctl}, 1140 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD, 1141 MISC_CMD, mrt_ioctl}, 1142 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD, 1143 MISC_CMD, mrt_ioctl} 1144 }; 1145 1146 int ip_misc_ioctl_count = 1147 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); 1148 1149 int conn_drain_nthreads; /* Number of drainers reqd. */ 1150 /* Settable in /etc/system */ 1151 /* Defined in ip_ire.c */ 1152 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; 1153 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; 1154 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; 1155 1156 static nv_t ire_nv_arr[] = { 1157 { IRE_BROADCAST, "BROADCAST" }, 1158 { IRE_LOCAL, "LOCAL" }, 1159 { IRE_LOOPBACK, "LOOPBACK" }, 1160 { IRE_DEFAULT, "DEFAULT" }, 1161 { IRE_PREFIX, "PREFIX" }, 1162 { IRE_IF_NORESOLVER, "IF_NORESOL" }, 1163 { IRE_IF_RESOLVER, "IF_RESOLV" }, 1164 { IRE_IF_CLONE, "IF_CLONE" }, 1165 { IRE_HOST, "HOST" }, 1166 { IRE_MULTICAST, "MULTICAST" }, 1167 { IRE_NOROUTE, "NOROUTE" }, 1168 { 0 } 1169 }; 1170 1171 nv_t *ire_nv_tbl = ire_nv_arr; 1172 1173 /* Simple ICMP IP Header Template */ 1174 static ipha_t icmp_ipha = { 1175 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 1176 }; 1177 1178 struct module_info ip_mod_info = { 1179 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT, 1180 IP_MOD_LOWAT 1181 }; 1182 1183 /* 1184 * Duplicate static symbols within a module confuses mdb; so we avoid the 1185 * problem by making the symbols here distinct from those in udp.c. 1186 */ 1187 1188 /* 1189 * Entry points for IP as a device and as a module. 1190 * We have separate open functions for the /dev/ip and /dev/ip6 devices. 1191 */ 1192 static struct qinit iprinitv4 = { 1193 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL, 1194 &ip_mod_info 1195 }; 1196 1197 struct qinit iprinitv6 = { 1198 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL, 1199 &ip_mod_info 1200 }; 1201 1202 static struct qinit ipwinit = { 1203 (pfi_t)ip_wput_nondata, (pfi_t)ip_wsrv, NULL, NULL, NULL, 1204 &ip_mod_info 1205 }; 1206 1207 static struct qinit iplrinit = { 1208 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL, 1209 &ip_mod_info 1210 }; 1211 1212 static struct qinit iplwinit = { 1213 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL, 1214 &ip_mod_info 1215 }; 1216 1217 /* For AF_INET aka /dev/ip */ 1218 struct streamtab ipinfov4 = { 1219 &iprinitv4, &ipwinit, &iplrinit, &iplwinit 1220 }; 1221 1222 /* For AF_INET6 aka /dev/ip6 */ 1223 struct streamtab ipinfov6 = { 1224 &iprinitv6, &ipwinit, &iplrinit, &iplwinit 1225 }; 1226 1227 #ifdef DEBUG 1228 boolean_t skip_sctp_cksum = B_FALSE; 1229 #endif 1230 1231 /* 1232 * Generate an ICMP fragmentation needed message. 1233 * When called from ip_output side a minimal ip_recv_attr_t needs to be 1234 * constructed by the caller. 1235 */ 1236 void 1237 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira) 1238 { 1239 icmph_t icmph; 1240 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 1241 1242 mp = icmp_pkt_err_ok(mp, ira); 1243 if (mp == NULL) 1244 return; 1245 1246 bzero(&icmph, sizeof (icmph_t)); 1247 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 1248 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; 1249 icmph.icmph_du_mtu = htons((uint16_t)mtu); 1250 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded); 1251 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 1252 1253 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira); 1254 } 1255 1256 /* 1257 * icmp_inbound_v4 deals with ICMP messages that are handled by IP. 1258 * If the ICMP message is consumed by IP, i.e., it should not be delivered 1259 * to any IPPROTO_ICMP raw sockets, then it returns NULL. 1260 * Likewise, if the ICMP error is misformed (too short, etc), then it 1261 * returns NULL. The caller uses this to determine whether or not to send 1262 * to raw sockets. 1263 * 1264 * All error messages are passed to the matching transport stream. 1265 * 1266 * The following cases are handled by icmp_inbound: 1267 * 1) It needs to send a reply back and possibly delivering it 1268 * to the "interested" upper clients. 1269 * 2) Return the mblk so that the caller can pass it to the RAW socket clients. 1270 * 3) It needs to change some values in IP only. 1271 * 4) It needs to change some values in IP and upper layers e.g TCP 1272 * by delivering an error to the upper layers. 1273 * 1274 * We handle the above three cases in the context of IPsec in the 1275 * following way : 1276 * 1277 * 1) Send the reply back in the same way as the request came in. 1278 * If it came in encrypted, it goes out encrypted. If it came in 1279 * clear, it goes out in clear. Thus, this will prevent chosen 1280 * plain text attack. 1281 * 2) The client may or may not expect things to come in secure. 1282 * If it comes in secure, the policy constraints are checked 1283 * before delivering it to the upper layers. If it comes in 1284 * clear, ipsec_inbound_accept_clear will decide whether to 1285 * accept this in clear or not. In both the cases, if the returned 1286 * message (IP header + 8 bytes) that caused the icmp message has 1287 * AH/ESP headers, it is sent up to AH/ESP for validation before 1288 * sending up. If there are only 8 bytes of returned message, then 1289 * upper client will not be notified. 1290 * 3) Check with global policy to see whether it matches the constaints. 1291 * But this will be done only if icmp_accept_messages_in_clear is 1292 * zero. 1293 * 4) If we need to change both in IP and ULP, then the decision taken 1294 * while affecting the values in IP and while delivering up to TCP 1295 * should be the same. 1296 * 1297 * There are two cases. 1298 * 1299 * a) If we reject data at the IP layer (ipsec_check_global_policy() 1300 * failed), we will not deliver it to the ULP, even though they 1301 * are *willing* to accept in *clear*. This is fine as our global 1302 * disposition to icmp messages asks us reject the datagram. 1303 * 1304 * b) If we accept data at the IP layer (ipsec_check_global_policy() 1305 * succeeded or icmp_accept_messages_in_clear is 1), and not able 1306 * to deliver it to ULP (policy failed), it can lead to 1307 * consistency problems. The cases known at this time are 1308 * ICMP_DESTINATION_UNREACHABLE messages with following code 1309 * values : 1310 * 1311 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value 1312 * and Upper layer rejects. Then the communication will 1313 * come to a stop. This is solved by making similar decisions 1314 * at both levels. Currently, when we are unable to deliver 1315 * to the Upper Layer (due to policy failures) while IP has 1316 * adjusted dce_pmtu, the next outbound datagram would 1317 * generate a local ICMP_FRAGMENTATION_NEEDED message - which 1318 * will be with the right level of protection. Thus the right 1319 * value will be communicated even if we are not able to 1320 * communicate when we get from the wire initially. But this 1321 * assumes there would be at least one outbound datagram after 1322 * IP has adjusted its dce_pmtu value. To make things 1323 * simpler, we accept in clear after the validation of 1324 * AH/ESP headers. 1325 * 1326 * - Other ICMP ERRORS : We may not be able to deliver it to the 1327 * upper layer depending on the level of protection the upper 1328 * layer expects and the disposition in ipsec_inbound_accept_clear(). 1329 * ipsec_inbound_accept_clear() decides whether a given ICMP error 1330 * should be accepted in clear when the Upper layer expects secure. 1331 * Thus the communication may get aborted by some bad ICMP 1332 * packets. 1333 */ 1334 mblk_t * 1335 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira) 1336 { 1337 icmph_t *icmph; 1338 ipha_t *ipha; /* Outer header */ 1339 int ip_hdr_length; /* Outer header length */ 1340 boolean_t interested; 1341 ipif_t *ipif; 1342 uint32_t ts; 1343 uint32_t *tsp; 1344 timestruc_t now; 1345 ill_t *ill = ira->ira_ill; 1346 ip_stack_t *ipst = ill->ill_ipst; 1347 zoneid_t zoneid = ira->ira_zoneid; 1348 int len_needed; 1349 mblk_t *mp_ret = NULL; 1350 1351 ipha = (ipha_t *)mp->b_rptr; 1352 1353 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs); 1354 1355 ip_hdr_length = ira->ira_ip_hdr_length; 1356 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) { 1357 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) { 1358 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts); 1359 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill); 1360 freemsg(mp); 1361 return (NULL); 1362 } 1363 /* Last chance to get real. */ 1364 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira); 1365 if (ipha == NULL) { 1366 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 1367 freemsg(mp); 1368 return (NULL); 1369 } 1370 } 1371 1372 /* The IP header will always be a multiple of four bytes */ 1373 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length]; 1374 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type, 1375 icmph->icmph_code)); 1376 1377 /* 1378 * We will set "interested" to "true" if we should pass a copy to 1379 * the transport or if we handle the packet locally. 1380 */ 1381 interested = B_FALSE; 1382 switch (icmph->icmph_type) { 1383 case ICMP_ECHO_REPLY: 1384 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps); 1385 break; 1386 case ICMP_DEST_UNREACHABLE: 1387 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) 1388 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded); 1389 interested = B_TRUE; /* Pass up to transport */ 1390 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs); 1391 break; 1392 case ICMP_SOURCE_QUENCH: 1393 interested = B_TRUE; /* Pass up to transport */ 1394 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs); 1395 break; 1396 case ICMP_REDIRECT: 1397 if (!ipst->ips_ip_ignore_redirect) 1398 interested = B_TRUE; 1399 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects); 1400 break; 1401 case ICMP_ECHO_REQUEST: 1402 /* 1403 * Whether to respond to echo requests that come in as IP 1404 * broadcasts or as IP multicast is subject to debate 1405 * (what isn't?). We aim to please, you pick it. 1406 * Default is do it. 1407 */ 1408 if (ira->ira_flags & IRAF_MULTICAST) { 1409 /* multicast: respond based on tunable */ 1410 interested = ipst->ips_ip_g_resp_to_echo_mcast; 1411 } else if (ira->ira_flags & IRAF_BROADCAST) { 1412 /* broadcast: respond based on tunable */ 1413 interested = ipst->ips_ip_g_resp_to_echo_bcast; 1414 } else { 1415 /* unicast: always respond */ 1416 interested = B_TRUE; 1417 } 1418 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos); 1419 if (!interested) { 1420 /* We never pass these to RAW sockets */ 1421 freemsg(mp); 1422 return (NULL); 1423 } 1424 1425 /* Check db_ref to make sure we can modify the packet. */ 1426 if (mp->b_datap->db_ref > 1) { 1427 mblk_t *mp1; 1428 1429 mp1 = copymsg(mp); 1430 freemsg(mp); 1431 if (!mp1) { 1432 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1433 return (NULL); 1434 } 1435 mp = mp1; 1436 ipha = (ipha_t *)mp->b_rptr; 1437 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length]; 1438 } 1439 icmph->icmph_type = ICMP_ECHO_REPLY; 1440 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps); 1441 icmp_send_reply_v4(mp, ipha, icmph, ira); 1442 return (NULL); 1443 1444 case ICMP_ROUTER_ADVERTISEMENT: 1445 case ICMP_ROUTER_SOLICITATION: 1446 break; 1447 case ICMP_TIME_EXCEEDED: 1448 interested = B_TRUE; /* Pass up to transport */ 1449 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds); 1450 break; 1451 case ICMP_PARAM_PROBLEM: 1452 interested = B_TRUE; /* Pass up to transport */ 1453 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs); 1454 break; 1455 case ICMP_TIME_STAMP_REQUEST: 1456 /* Response to Time Stamp Requests is local policy. */ 1457 if (ipst->ips_ip_g_resp_to_timestamp) { 1458 if (ira->ira_flags & IRAF_MULTIBROADCAST) 1459 interested = 1460 ipst->ips_ip_g_resp_to_timestamp_bcast; 1461 else 1462 interested = B_TRUE; 1463 } 1464 if (!interested) { 1465 /* We never pass these to RAW sockets */ 1466 freemsg(mp); 1467 return (NULL); 1468 } 1469 1470 /* Make sure we have enough of the packet */ 1471 len_needed = ip_hdr_length + ICMPH_SIZE + 1472 3 * sizeof (uint32_t); 1473 1474 if (mp->b_wptr - mp->b_rptr < len_needed) { 1475 ipha = ip_pullup(mp, len_needed, ira); 1476 if (ipha == NULL) { 1477 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1478 ip_drop_input("ipIfStatsInDiscards - ip_pullup", 1479 mp, ill); 1480 freemsg(mp); 1481 return (NULL); 1482 } 1483 /* Refresh following the pullup. */ 1484 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length]; 1485 } 1486 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps); 1487 /* Check db_ref to make sure we can modify the packet. */ 1488 if (mp->b_datap->db_ref > 1) { 1489 mblk_t *mp1; 1490 1491 mp1 = copymsg(mp); 1492 freemsg(mp); 1493 if (!mp1) { 1494 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1495 return (NULL); 1496 } 1497 mp = mp1; 1498 ipha = (ipha_t *)mp->b_rptr; 1499 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length]; 1500 } 1501 icmph->icmph_type = ICMP_TIME_STAMP_REPLY; 1502 tsp = (uint32_t *)&icmph[1]; 1503 tsp++; /* Skip past 'originate time' */ 1504 /* Compute # of milliseconds since midnight */ 1505 gethrestime(&now); 1506 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 1507 now.tv_nsec / (NANOSEC / MILLISEC); 1508 *tsp++ = htonl(ts); /* Lay in 'receive time' */ 1509 *tsp++ = htonl(ts); /* Lay in 'send time' */ 1510 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps); 1511 icmp_send_reply_v4(mp, ipha, icmph, ira); 1512 return (NULL); 1513 1514 case ICMP_TIME_STAMP_REPLY: 1515 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps); 1516 break; 1517 case ICMP_INFO_REQUEST: 1518 /* Per RFC 1122 3.2.2.7, ignore this. */ 1519 case ICMP_INFO_REPLY: 1520 break; 1521 case ICMP_ADDRESS_MASK_REQUEST: 1522 if (ira->ira_flags & IRAF_MULTIBROADCAST) { 1523 interested = 1524 ipst->ips_ip_respond_to_address_mask_broadcast; 1525 } else { 1526 interested = B_TRUE; 1527 } 1528 if (!interested) { 1529 /* We never pass these to RAW sockets */ 1530 freemsg(mp); 1531 return (NULL); 1532 } 1533 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN; 1534 if (mp->b_wptr - mp->b_rptr < len_needed) { 1535 ipha = ip_pullup(mp, len_needed, ira); 1536 if (ipha == NULL) { 1537 BUMP_MIB(ill->ill_ip_mib, 1538 ipIfStatsInTruncatedPkts); 1539 ip_drop_input("ipIfStatsInTruncatedPkts", mp, 1540 ill); 1541 freemsg(mp); 1542 return (NULL); 1543 } 1544 /* Refresh following the pullup. */ 1545 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length]; 1546 } 1547 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks); 1548 /* Check db_ref to make sure we can modify the packet. */ 1549 if (mp->b_datap->db_ref > 1) { 1550 mblk_t *mp1; 1551 1552 mp1 = copymsg(mp); 1553 freemsg(mp); 1554 if (!mp1) { 1555 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 1556 return (NULL); 1557 } 1558 mp = mp1; 1559 ipha = (ipha_t *)mp->b_rptr; 1560 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length]; 1561 } 1562 /* 1563 * Need the ipif with the mask be the same as the source 1564 * address of the mask reply. For unicast we have a specific 1565 * ipif. For multicast/broadcast we only handle onlink 1566 * senders, and use the source address to pick an ipif. 1567 */ 1568 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst); 1569 if (ipif == NULL) { 1570 /* Broadcast or multicast */ 1571 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); 1572 if (ipif == NULL) { 1573 freemsg(mp); 1574 return (NULL); 1575 } 1576 } 1577 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 1578 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN); 1579 ipif_refrele(ipif); 1580 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps); 1581 icmp_send_reply_v4(mp, ipha, icmph, ira); 1582 return (NULL); 1583 1584 case ICMP_ADDRESS_MASK_REPLY: 1585 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps); 1586 break; 1587 default: 1588 interested = B_TRUE; /* Pass up to transport */ 1589 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns); 1590 break; 1591 } 1592 /* 1593 * See if there is an ICMP client to avoid an extra copymsg/freemsg 1594 * if there isn't one. 1595 */ 1596 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) { 1597 /* If there is an ICMP client and we want one too, copy it. */ 1598 1599 if (!interested) { 1600 /* Caller will deliver to RAW sockets */ 1601 return (mp); 1602 } 1603 mp_ret = copymsg(mp); 1604 if (mp_ret == NULL) { 1605 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1606 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill); 1607 } 1608 } else if (!interested) { 1609 /* Neither we nor raw sockets are interested. Drop packet now */ 1610 freemsg(mp); 1611 return (NULL); 1612 } 1613 1614 /* 1615 * ICMP error or redirect packet. Make sure we have enough of 1616 * the header and that db_ref == 1 since we might end up modifying 1617 * the packet. 1618 */ 1619 if (mp->b_cont != NULL) { 1620 if (ip_pullup(mp, -1, ira) == NULL) { 1621 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1622 ip_drop_input("ipIfStatsInDiscards - ip_pullup", 1623 mp, ill); 1624 freemsg(mp); 1625 return (mp_ret); 1626 } 1627 } 1628 1629 if (mp->b_datap->db_ref > 1) { 1630 mblk_t *mp1; 1631 1632 mp1 = copymsg(mp); 1633 if (mp1 == NULL) { 1634 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1635 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill); 1636 freemsg(mp); 1637 return (mp_ret); 1638 } 1639 freemsg(mp); 1640 mp = mp1; 1641 } 1642 1643 /* 1644 * In case mp has changed, verify the message before any further 1645 * processes. 1646 */ 1647 ipha = (ipha_t *)mp->b_rptr; 1648 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length]; 1649 if (!icmp_inbound_verify_v4(mp, icmph, ira)) { 1650 freemsg(mp); 1651 return (mp_ret); 1652 } 1653 1654 switch (icmph->icmph_type) { 1655 case ICMP_REDIRECT: 1656 icmp_redirect_v4(mp, ipha, icmph, ira); 1657 break; 1658 case ICMP_DEST_UNREACHABLE: 1659 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 1660 /* Update DCE and adjust MTU is icmp header if needed */ 1661 icmp_inbound_too_big_v4(icmph, ira); 1662 } 1663 /* FALLTHRU */ 1664 default: 1665 icmp_inbound_error_fanout_v4(mp, icmph, ira); 1666 break; 1667 } 1668 return (mp_ret); 1669 } 1670 1671 /* 1672 * Send an ICMP echo, timestamp or address mask reply. 1673 * The caller has already updated the payload part of the packet. 1674 * We handle the ICMP checksum, IP source address selection and feed 1675 * the packet into ip_output_simple. 1676 */ 1677 static void 1678 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, 1679 ip_recv_attr_t *ira) 1680 { 1681 uint_t ip_hdr_length = ira->ira_ip_hdr_length; 1682 ill_t *ill = ira->ira_ill; 1683 ip_stack_t *ipst = ill->ill_ipst; 1684 ip_xmit_attr_t ixas; 1685 1686 /* Send out an ICMP packet */ 1687 icmph->icmph_checksum = 0; 1688 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0); 1689 /* Reset time to live. */ 1690 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 1691 { 1692 /* Swap source and destination addresses */ 1693 ipaddr_t tmp; 1694 1695 tmp = ipha->ipha_src; 1696 ipha->ipha_src = ipha->ipha_dst; 1697 ipha->ipha_dst = tmp; 1698 } 1699 ipha->ipha_ident = 0; 1700 if (!IS_SIMPLE_IPH(ipha)) 1701 icmp_options_update(ipha); 1702 1703 bzero(&ixas, sizeof (ixas)); 1704 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4; 1705 ixas.ixa_zoneid = ira->ira_zoneid; 1706 ixas.ixa_cred = kcred; 1707 ixas.ixa_cpid = NOPID; 1708 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */ 1709 ixas.ixa_ifindex = 0; 1710 ixas.ixa_ipst = ipst; 1711 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1712 1713 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) { 1714 /* 1715 * This packet should go out the same way as it 1716 * came in i.e in clear, independent of the IPsec policy 1717 * for transmitting packets. 1718 */ 1719 ixas.ixa_flags |= IXAF_NO_IPSEC; 1720 } else { 1721 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) { 1722 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1723 /* Note: mp already consumed and ip_drop_packet done */ 1724 return; 1725 } 1726 } 1727 if (ira->ira_flags & IRAF_MULTIBROADCAST) { 1728 /* 1729 * Not one or our addresses (IRE_LOCALs), thus we let 1730 * ip_output_simple pick the source. 1731 */ 1732 ipha->ipha_src = INADDR_ANY; 1733 ixas.ixa_flags |= IXAF_SET_SOURCE; 1734 } 1735 /* Should we send with DF and use dce_pmtu? */ 1736 if (ipst->ips_ipv4_icmp_return_pmtu) { 1737 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY; 1738 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS; 1739 } 1740 1741 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 1742 1743 (void) ip_output_simple(mp, &ixas); 1744 ixa_cleanup(&ixas); 1745 } 1746 1747 /* 1748 * Verify the ICMP messages for either for ICMP error or redirect packet. 1749 * The caller should have fully pulled up the message. If it's a redirect 1750 * packet, only basic checks on IP header will be done; otherwise, verify 1751 * the packet by looking at the included ULP header. 1752 * 1753 * Called before icmp_inbound_error_fanout_v4 is called. 1754 */ 1755 static boolean_t 1756 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira) 1757 { 1758 ill_t *ill = ira->ira_ill; 1759 int hdr_length; 1760 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 1761 conn_t *connp; 1762 ipha_t *ipha; /* Inner IP header */ 1763 1764 ipha = (ipha_t *)&icmph[1]; 1765 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr) 1766 goto truncated; 1767 1768 hdr_length = IPH_HDR_LENGTH(ipha); 1769 1770 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) 1771 goto discard_pkt; 1772 1773 if (hdr_length < sizeof (ipha_t)) 1774 goto truncated; 1775 1776 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) 1777 goto truncated; 1778 1779 /* 1780 * Stop here for ICMP_REDIRECT. 1781 */ 1782 if (icmph->icmph_type == ICMP_REDIRECT) 1783 return (B_TRUE); 1784 1785 /* 1786 * ICMP errors only. 1787 */ 1788 switch (ipha->ipha_protocol) { 1789 case IPPROTO_UDP: 1790 /* 1791 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 1792 * transport header. 1793 */ 1794 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 1795 mp->b_wptr) 1796 goto truncated; 1797 break; 1798 case IPPROTO_TCP: { 1799 tcpha_t *tcpha; 1800 1801 /* 1802 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 1803 * transport header. 1804 */ 1805 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 1806 mp->b_wptr) 1807 goto truncated; 1808 1809 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length); 1810 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN, 1811 ipst); 1812 if (connp == NULL) 1813 goto discard_pkt; 1814 1815 if ((connp->conn_verifyicmp != NULL) && 1816 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) { 1817 CONN_DEC_REF(connp); 1818 goto discard_pkt; 1819 } 1820 CONN_DEC_REF(connp); 1821 break; 1822 } 1823 case IPPROTO_SCTP: 1824 /* 1825 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 1826 * transport header. 1827 */ 1828 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > 1829 mp->b_wptr) 1830 goto truncated; 1831 break; 1832 case IPPROTO_ESP: 1833 case IPPROTO_AH: 1834 break; 1835 case IPPROTO_ENCAP: 1836 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > 1837 mp->b_wptr) 1838 goto truncated; 1839 break; 1840 default: 1841 break; 1842 } 1843 1844 return (B_TRUE); 1845 1846 discard_pkt: 1847 /* Bogus ICMP error. */ 1848 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 1849 return (B_FALSE); 1850 1851 truncated: 1852 /* We pulled up everthing already. Must be truncated */ 1853 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts); 1854 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill); 1855 return (B_FALSE); 1856 } 1857 1858 /* Table from RFC 1191 */ 1859 static int icmp_frag_size_table[] = 1860 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; 1861 1862 /* 1863 * Process received ICMP Packet too big. 1864 * Just handles the DCE create/update, including using the above table of 1865 * PMTU guesses. The caller is responsible for validating the packet before 1866 * passing it in and also to fanout the ICMP error to any matching transport 1867 * conns. Assumes the message has been fully pulled up and verified. 1868 * 1869 * Before getting here, the caller has called icmp_inbound_verify_v4() 1870 * that should have verified with ULP to prevent undoing the changes we're 1871 * going to make to DCE. For example, TCP might have verified that the packet 1872 * which generated error is in the send window. 1873 * 1874 * In some cases modified this MTU in the ICMP header packet; the caller 1875 * should pass to the matching ULP after this returns. 1876 */ 1877 static void 1878 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira) 1879 { 1880 dce_t *dce; 1881 int old_mtu; 1882 int mtu, orig_mtu; 1883 ipaddr_t dst; 1884 boolean_t disable_pmtud; 1885 ill_t *ill = ira->ira_ill; 1886 ip_stack_t *ipst = ill->ill_ipst; 1887 uint_t hdr_length; 1888 ipha_t *ipha; 1889 1890 /* Caller already pulled up everything. */ 1891 ipha = (ipha_t *)&icmph[1]; 1892 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && 1893 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); 1894 ASSERT(ill != NULL); 1895 1896 hdr_length = IPH_HDR_LENGTH(ipha); 1897 1898 /* 1899 * We handle path MTU for source routed packets since the DCE 1900 * is looked up using the final destination. 1901 */ 1902 dst = ip_get_dst(ipha); 1903 1904 dce = dce_lookup_and_add_v4(dst, ipst); 1905 if (dce == NULL) { 1906 /* Couldn't add a unique one - ENOMEM */ 1907 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n", 1908 ntohl(dst))); 1909 return; 1910 } 1911 1912 /* Check for MTU discovery advice as described in RFC 1191 */ 1913 mtu = ntohs(icmph->icmph_du_mtu); 1914 orig_mtu = mtu; 1915 disable_pmtud = B_FALSE; 1916 1917 mutex_enter(&dce->dce_lock); 1918 if (dce->dce_flags & DCEF_PMTU) 1919 old_mtu = dce->dce_pmtu; 1920 else 1921 old_mtu = ill->ill_mtu; 1922 1923 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) { 1924 uint32_t length; 1925 int i; 1926 1927 /* 1928 * Use the table from RFC 1191 to figure out 1929 * the next "plateau" based on the length in 1930 * the original IP packet. 1931 */ 1932 length = ntohs(ipha->ipha_length); 1933 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce, 1934 uint32_t, length); 1935 if (old_mtu <= length && 1936 old_mtu >= length - hdr_length) { 1937 /* 1938 * Handle broken BSD 4.2 systems that 1939 * return the wrong ipha_length in ICMP 1940 * errors. 1941 */ 1942 ip1dbg(("Wrong mtu: sent %d, dce %d\n", 1943 length, old_mtu)); 1944 length -= hdr_length; 1945 } 1946 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { 1947 if (length > icmp_frag_size_table[i]) 1948 break; 1949 } 1950 if (i == A_CNT(icmp_frag_size_table)) { 1951 /* Smaller than IP_MIN_MTU! */ 1952 ip1dbg(("Too big for packet size %d\n", 1953 length)); 1954 disable_pmtud = B_TRUE; 1955 mtu = ipst->ips_ip_pmtu_min; 1956 } else { 1957 mtu = icmp_frag_size_table[i]; 1958 ip1dbg(("Calculated mtu %d, packet size %d, " 1959 "before %d\n", mtu, length, old_mtu)); 1960 if (mtu < ipst->ips_ip_pmtu_min) { 1961 mtu = ipst->ips_ip_pmtu_min; 1962 disable_pmtud = B_TRUE; 1963 } 1964 } 1965 } 1966 if (disable_pmtud) 1967 dce->dce_flags |= DCEF_TOO_SMALL_PMTU; 1968 else 1969 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU; 1970 1971 dce->dce_pmtu = MIN(old_mtu, mtu); 1972 /* Prepare to send the new max frag size for the ULP. */ 1973 icmph->icmph_du_zero = 0; 1974 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu); 1975 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *, 1976 dce, int, orig_mtu, int, mtu); 1977 1978 /* We now have a PMTU for sure */ 1979 dce->dce_flags |= DCEF_PMTU; 1980 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64()); 1981 mutex_exit(&dce->dce_lock); 1982 /* 1983 * After dropping the lock the new value is visible to everyone. 1984 * Then we bump the generation number so any cached values reinspect 1985 * the dce_t. 1986 */ 1987 dce_increment_generation(dce); 1988 dce_refrele(dce); 1989 } 1990 1991 /* 1992 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4 1993 * calls this function. 1994 */ 1995 static mblk_t * 1996 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha) 1997 { 1998 int length; 1999 2000 ASSERT(mp->b_datap->db_type == M_DATA); 2001 2002 /* icmp_inbound_v4 has already pulled up the whole error packet */ 2003 ASSERT(mp->b_cont == NULL); 2004 2005 /* 2006 * The length that we want to overlay is the inner header 2007 * and what follows it. 2008 */ 2009 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr); 2010 2011 /* 2012 * Overlay the inner header and whatever follows it over the 2013 * outer header. 2014 */ 2015 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); 2016 2017 /* Adjust for what we removed */ 2018 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha; 2019 return (mp); 2020 } 2021 2022 /* 2023 * Try to pass the ICMP message upstream in case the ULP cares. 2024 * 2025 * If the packet that caused the ICMP error is secure, we send 2026 * it to AH/ESP to make sure that the attached packet has a 2027 * valid association. ipha in the code below points to the 2028 * IP header of the packet that caused the error. 2029 * 2030 * For IPsec cases, we let the next-layer-up (which has access to 2031 * cached policy on the conn_t, or can query the SPD directly) 2032 * subtract out any IPsec overhead if they must. We therefore make no 2033 * adjustments here for IPsec overhead. 2034 * 2035 * IFN could have been generated locally or by some router. 2036 * 2037 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call 2038 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN. 2039 * This happens because IP adjusted its value of MTU on an 2040 * earlier IFN message and could not tell the upper layer, 2041 * the new adjusted value of MTU e.g. Packet was encrypted 2042 * or there was not enough information to fanout to upper 2043 * layers. Thus on the next outbound datagram, ire_send_wire 2044 * generates the IFN, where IPsec processing has *not* been 2045 * done. 2046 * 2047 * Note that we retain ixa_fragsize across IPsec thus once 2048 * we have picking ixa_fragsize and entered ipsec_out_process we do 2049 * no change the fragsize even if the path MTU changes before 2050 * we reach ip_output_post_ipsec. 2051 * 2052 * In the local case, IRAF_LOOPBACK will be set indicating 2053 * that IFN was generated locally. 2054 * 2055 * ROUTER : IFN could be secure or non-secure. 2056 * 2057 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the 2058 * packet in error has AH/ESP headers to validate the AH/ESP 2059 * headers. AH/ESP will verify whether there is a valid SA or 2060 * not and send it back. We will fanout again if we have more 2061 * data in the packet. 2062 * 2063 * If the packet in error does not have AH/ESP, we handle it 2064 * like any other case. 2065 * 2066 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it 2067 * up to AH/ESP for validation. AH/ESP will verify whether there is a 2068 * valid SA or not and send it back. We will fanout again if 2069 * we have more data in the packet. 2070 * 2071 * If the packet in error does not have AH/ESP, we handle it 2072 * like any other case. 2073 * 2074 * The caller must have called icmp_inbound_verify_v4. 2075 */ 2076 static void 2077 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira) 2078 { 2079 uint16_t *up; /* Pointer to ports in ULP header */ 2080 uint32_t ports; /* reversed ports for fanout */ 2081 ipha_t ripha; /* With reversed addresses */ 2082 ipha_t *ipha; /* Inner IP header */ 2083 uint_t hdr_length; /* Inner IP header length */ 2084 tcpha_t *tcpha; 2085 conn_t *connp; 2086 ill_t *ill = ira->ira_ill; 2087 ip_stack_t *ipst = ill->ill_ipst; 2088 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 2089 ill_t *rill = ira->ira_rill; 2090 2091 /* Caller already pulled up everything. */ 2092 ipha = (ipha_t *)&icmph[1]; 2093 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr); 2094 ASSERT(mp->b_cont == NULL); 2095 2096 hdr_length = IPH_HDR_LENGTH(ipha); 2097 ira->ira_protocol = ipha->ipha_protocol; 2098 2099 /* 2100 * We need a separate IP header with the source and destination 2101 * addresses reversed to do fanout/classification because the ipha in 2102 * the ICMP error is in the form we sent it out. 2103 */ 2104 ripha.ipha_src = ipha->ipha_dst; 2105 ripha.ipha_dst = ipha->ipha_src; 2106 ripha.ipha_protocol = ipha->ipha_protocol; 2107 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length; 2108 2109 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n", 2110 ripha.ipha_protocol, ntohl(ipha->ipha_src), 2111 ntohl(ipha->ipha_dst), 2112 icmph->icmph_type, icmph->icmph_code)); 2113 2114 switch (ipha->ipha_protocol) { 2115 case IPPROTO_UDP: 2116 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2117 2118 /* Attempt to find a client stream based on port. */ 2119 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n", 2120 ntohs(up[0]), ntohs(up[1]))); 2121 2122 /* Note that we send error to all matches. */ 2123 ira->ira_flags |= IRAF_ICMP_ERROR; 2124 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira); 2125 ira->ira_flags &= ~IRAF_ICMP_ERROR; 2126 return; 2127 2128 case IPPROTO_TCP: 2129 /* 2130 * Find a TCP client stream for this packet. 2131 * Note that we do a reverse lookup since the header is 2132 * in the form we sent it out. 2133 */ 2134 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length); 2135 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN, 2136 ipst); 2137 if (connp == NULL) 2138 goto discard_pkt; 2139 2140 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || 2141 (ira->ira_flags & IRAF_IPSEC_SECURE)) { 2142 mp = ipsec_check_inbound_policy(mp, connp, 2143 ipha, NULL, ira); 2144 if (mp == NULL) { 2145 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2146 /* Note that mp is NULL */ 2147 ip_drop_input("ipIfStatsInDiscards", mp, ill); 2148 CONN_DEC_REF(connp); 2149 return; 2150 } 2151 } 2152 2153 ira->ira_flags |= IRAF_ICMP_ERROR; 2154 ira->ira_ill = ira->ira_rill = NULL; 2155 if (IPCL_IS_TCP(connp)) { 2156 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 2157 connp->conn_recvicmp, connp, ira, SQ_FILL, 2158 SQTAG_TCP_INPUT_ICMP_ERR); 2159 } else { 2160 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */ 2161 (connp->conn_recv)(connp, mp, NULL, ira); 2162 CONN_DEC_REF(connp); 2163 } 2164 ira->ira_ill = ill; 2165 ira->ira_rill = rill; 2166 ira->ira_flags &= ~IRAF_ICMP_ERROR; 2167 return; 2168 2169 case IPPROTO_SCTP: 2170 up = (uint16_t *)((uchar_t *)ipha + hdr_length); 2171 /* Find a SCTP client stream for this packet. */ 2172 ((uint16_t *)&ports)[0] = up[1]; 2173 ((uint16_t *)&ports)[1] = up[0]; 2174 2175 ira->ira_flags |= IRAF_ICMP_ERROR; 2176 ip_fanout_sctp(mp, &ripha, NULL, ports, ira); 2177 ira->ira_flags &= ~IRAF_ICMP_ERROR; 2178 return; 2179 2180 case IPPROTO_ESP: 2181 case IPPROTO_AH: 2182 if (!ipsec_loaded(ipss)) { 2183 ip_proto_not_sup(mp, ira); 2184 return; 2185 } 2186 2187 if (ipha->ipha_protocol == IPPROTO_ESP) 2188 mp = ipsecesp_icmp_error(mp, ira); 2189 else 2190 mp = ipsecah_icmp_error(mp, ira); 2191 if (mp == NULL) 2192 return; 2193 2194 /* Just in case ipsec didn't preserve the NULL b_cont */ 2195 if (mp->b_cont != NULL) { 2196 if (!pullupmsg(mp, -1)) 2197 goto discard_pkt; 2198 } 2199 2200 /* 2201 * Note that ira_pktlen and ira_ip_hdr_length are no longer 2202 * correct, but we don't use them any more here. 2203 * 2204 * If succesful, the mp has been modified to not include 2205 * the ESP/AH header so we can fanout to the ULP's icmp 2206 * error handler. 2207 */ 2208 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) 2209 goto truncated; 2210 2211 /* Verify the modified message before any further processes. */ 2212 ipha = (ipha_t *)mp->b_rptr; 2213 hdr_length = IPH_HDR_LENGTH(ipha); 2214 icmph = (icmph_t *)&mp->b_rptr[hdr_length]; 2215 if (!icmp_inbound_verify_v4(mp, icmph, ira)) { 2216 freemsg(mp); 2217 return; 2218 } 2219 2220 icmp_inbound_error_fanout_v4(mp, icmph, ira); 2221 return; 2222 2223 case IPPROTO_ENCAP: { 2224 /* Look for self-encapsulated packets that caused an error */ 2225 ipha_t *in_ipha; 2226 2227 /* 2228 * Caller has verified that length has to be 2229 * at least the size of IP header. 2230 */ 2231 ASSERT(hdr_length >= sizeof (ipha_t)); 2232 /* 2233 * Check the sanity of the inner IP header like 2234 * we did for the outer header. 2235 */ 2236 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); 2237 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { 2238 goto discard_pkt; 2239 } 2240 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { 2241 goto discard_pkt; 2242 } 2243 /* Check for Self-encapsulated tunnels */ 2244 if (in_ipha->ipha_src == ipha->ipha_src && 2245 in_ipha->ipha_dst == ipha->ipha_dst) { 2246 2247 mp = icmp_inbound_self_encap_error_v4(mp, ipha, 2248 in_ipha); 2249 if (mp == NULL) 2250 goto discard_pkt; 2251 2252 /* 2253 * Just in case self_encap didn't preserve the NULL 2254 * b_cont 2255 */ 2256 if (mp->b_cont != NULL) { 2257 if (!pullupmsg(mp, -1)) 2258 goto discard_pkt; 2259 } 2260 /* 2261 * Note that ira_pktlen and ira_ip_hdr_length are no 2262 * longer correct, but we don't use them any more here. 2263 */ 2264 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) 2265 goto truncated; 2266 2267 /* 2268 * Verify the modified message before any further 2269 * processes. 2270 */ 2271 ipha = (ipha_t *)mp->b_rptr; 2272 hdr_length = IPH_HDR_LENGTH(ipha); 2273 icmph = (icmph_t *)&mp->b_rptr[hdr_length]; 2274 if (!icmp_inbound_verify_v4(mp, icmph, ira)) { 2275 freemsg(mp); 2276 return; 2277 } 2278 2279 /* 2280 * The packet in error is self-encapsualted. 2281 * And we are finding it further encapsulated 2282 * which we could not have possibly generated. 2283 */ 2284 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2285 goto discard_pkt; 2286 } 2287 icmp_inbound_error_fanout_v4(mp, icmph, ira); 2288 return; 2289 } 2290 /* No self-encapsulated */ 2291 /* FALLTHRU */ 2292 } 2293 case IPPROTO_IPV6: 2294 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src, 2295 &ripha.ipha_dst, ipst)) != NULL) { 2296 ira->ira_flags |= IRAF_ICMP_ERROR; 2297 connp->conn_recvicmp(connp, mp, NULL, ira); 2298 CONN_DEC_REF(connp); 2299 ira->ira_flags &= ~IRAF_ICMP_ERROR; 2300 return; 2301 } 2302 /* 2303 * No IP tunnel is interested, fallthrough and see 2304 * if a raw socket will want it. 2305 */ 2306 /* FALLTHRU */ 2307 default: 2308 ira->ira_flags |= IRAF_ICMP_ERROR; 2309 ip_fanout_proto_v4(mp, &ripha, ira); 2310 ira->ira_flags &= ~IRAF_ICMP_ERROR; 2311 return; 2312 } 2313 /* NOTREACHED */ 2314 discard_pkt: 2315 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 2316 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n")); 2317 ip_drop_input("ipIfStatsInDiscards", mp, ill); 2318 freemsg(mp); 2319 return; 2320 2321 truncated: 2322 /* We pulled up everthing already. Must be truncated */ 2323 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts); 2324 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill); 2325 freemsg(mp); 2326 } 2327 2328 /* 2329 * Common IP options parser. 2330 * 2331 * Setup routine: fill in *optp with options-parsing state, then 2332 * tail-call ipoptp_next to return the first option. 2333 */ 2334 uint8_t 2335 ipoptp_first(ipoptp_t *optp, ipha_t *ipha) 2336 { 2337 uint32_t totallen; /* total length of all options */ 2338 2339 totallen = ipha->ipha_version_and_hdr_length - 2340 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 2341 totallen <<= 2; 2342 optp->ipoptp_next = (uint8_t *)(&ipha[1]); 2343 optp->ipoptp_end = optp->ipoptp_next + totallen; 2344 optp->ipoptp_flags = 0; 2345 return (ipoptp_next(optp)); 2346 } 2347 2348 /* Like above but without an ipha_t */ 2349 uint8_t 2350 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt) 2351 { 2352 optp->ipoptp_next = opt; 2353 optp->ipoptp_end = optp->ipoptp_next + totallen; 2354 optp->ipoptp_flags = 0; 2355 return (ipoptp_next(optp)); 2356 } 2357 2358 /* 2359 * Common IP options parser: extract next option. 2360 */ 2361 uint8_t 2362 ipoptp_next(ipoptp_t *optp) 2363 { 2364 uint8_t *end = optp->ipoptp_end; 2365 uint8_t *cur = optp->ipoptp_next; 2366 uint8_t opt, len, pointer; 2367 2368 /* 2369 * If cur > end already, then the ipoptp_end or ipoptp_next pointer 2370 * has been corrupted. 2371 */ 2372 ASSERT(cur <= end); 2373 2374 if (cur == end) 2375 return (IPOPT_EOL); 2376 2377 opt = cur[IPOPT_OPTVAL]; 2378 2379 /* 2380 * Skip any NOP options. 2381 */ 2382 while (opt == IPOPT_NOP) { 2383 cur++; 2384 if (cur == end) 2385 return (IPOPT_EOL); 2386 opt = cur[IPOPT_OPTVAL]; 2387 } 2388 2389 if (opt == IPOPT_EOL) 2390 return (IPOPT_EOL); 2391 2392 /* 2393 * Option requiring a length. 2394 */ 2395 if ((cur + 1) >= end) { 2396 optp->ipoptp_flags |= IPOPTP_ERROR; 2397 return (IPOPT_EOL); 2398 } 2399 len = cur[IPOPT_OLEN]; 2400 if (len < 2) { 2401 optp->ipoptp_flags |= IPOPTP_ERROR; 2402 return (IPOPT_EOL); 2403 } 2404 optp->ipoptp_cur = cur; 2405 optp->ipoptp_len = len; 2406 optp->ipoptp_next = cur + len; 2407 if (cur + len > end) { 2408 optp->ipoptp_flags |= IPOPTP_ERROR; 2409 return (IPOPT_EOL); 2410 } 2411 2412 /* 2413 * For the options which require a pointer field, make sure 2414 * its there, and make sure it points to either something 2415 * inside this option, or the end of the option. 2416 */ 2417 switch (opt) { 2418 case IPOPT_RR: 2419 case IPOPT_TS: 2420 case IPOPT_LSRR: 2421 case IPOPT_SSRR: 2422 if (len <= IPOPT_OFFSET) { 2423 optp->ipoptp_flags |= IPOPTP_ERROR; 2424 return (opt); 2425 } 2426 pointer = cur[IPOPT_OFFSET]; 2427 if (pointer - 1 > len) { 2428 optp->ipoptp_flags |= IPOPTP_ERROR; 2429 return (opt); 2430 } 2431 break; 2432 } 2433 2434 /* 2435 * Sanity check the pointer field based on the type of the 2436 * option. 2437 */ 2438 switch (opt) { 2439 case IPOPT_RR: 2440 case IPOPT_SSRR: 2441 case IPOPT_LSRR: 2442 if (pointer < IPOPT_MINOFF_SR) 2443 optp->ipoptp_flags |= IPOPTP_ERROR; 2444 break; 2445 case IPOPT_TS: 2446 if (pointer < IPOPT_MINOFF_IT) 2447 optp->ipoptp_flags |= IPOPTP_ERROR; 2448 /* 2449 * Note that the Internet Timestamp option also 2450 * contains two four bit fields (the Overflow field, 2451 * and the Flag field), which follow the pointer 2452 * field. We don't need to check that these fields 2453 * fall within the length of the option because this 2454 * was implicitely done above. We've checked that the 2455 * pointer value is at least IPOPT_MINOFF_IT, and that 2456 * it falls within the option. Since IPOPT_MINOFF_IT > 2457 * IPOPT_POS_OV_FLG, we don't need the explicit check. 2458 */ 2459 ASSERT(len > IPOPT_POS_OV_FLG); 2460 break; 2461 } 2462 2463 return (opt); 2464 } 2465 2466 /* 2467 * Use the outgoing IP header to create an IP_OPTIONS option the way 2468 * it was passed down from the application. 2469 * 2470 * This is compatible with BSD in that it returns 2471 * the reverse source route with the final destination 2472 * as the last entry. The first 4 bytes of the option 2473 * will contain the final destination. 2474 */ 2475 int 2476 ip_opt_get_user(conn_t *connp, uchar_t *buf) 2477 { 2478 ipoptp_t opts; 2479 uchar_t *opt; 2480 uint8_t optval; 2481 uint8_t optlen; 2482 uint32_t len = 0; 2483 uchar_t *buf1 = buf; 2484 uint32_t totallen; 2485 ipaddr_t dst; 2486 ip_pkt_t *ipp = &connp->conn_xmit_ipp; 2487 2488 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS)) 2489 return (0); 2490 2491 totallen = ipp->ipp_ipv4_options_len; 2492 if (totallen & 0x3) 2493 return (0); 2494 2495 buf += IP_ADDR_LEN; /* Leave room for final destination */ 2496 len += IP_ADDR_LEN; 2497 bzero(buf1, IP_ADDR_LEN); 2498 2499 dst = connp->conn_faddr_v4; 2500 2501 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options); 2502 optval != IPOPT_EOL; 2503 optval = ipoptp_next(&opts)) { 2504 int off; 2505 2506 opt = opts.ipoptp_cur; 2507 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 2508 break; 2509 } 2510 optlen = opts.ipoptp_len; 2511 2512 switch (optval) { 2513 case IPOPT_SSRR: 2514 case IPOPT_LSRR: 2515 2516 /* 2517 * Insert destination as the first entry in the source 2518 * route and move down the entries on step. 2519 * The last entry gets placed at buf1. 2520 */ 2521 buf[IPOPT_OPTVAL] = optval; 2522 buf[IPOPT_OLEN] = optlen; 2523 buf[IPOPT_OFFSET] = optlen; 2524 2525 off = optlen - IP_ADDR_LEN; 2526 if (off < 0) { 2527 /* No entries in source route */ 2528 break; 2529 } 2530 /* Last entry in source route if not already set */ 2531 if (dst == INADDR_ANY) 2532 bcopy(opt + off, buf1, IP_ADDR_LEN); 2533 off -= IP_ADDR_LEN; 2534 2535 while (off > 0) { 2536 bcopy(opt + off, 2537 buf + off + IP_ADDR_LEN, 2538 IP_ADDR_LEN); 2539 off -= IP_ADDR_LEN; 2540 } 2541 /* ipha_dst into first slot */ 2542 bcopy(&dst, buf + off + IP_ADDR_LEN, 2543 IP_ADDR_LEN); 2544 buf += optlen; 2545 len += optlen; 2546 break; 2547 2548 default: 2549 bcopy(opt, buf, optlen); 2550 buf += optlen; 2551 len += optlen; 2552 break; 2553 } 2554 } 2555 done: 2556 /* Pad the resulting options */ 2557 while (len & 0x3) { 2558 *buf++ = IPOPT_EOL; 2559 len++; 2560 } 2561 return (len); 2562 } 2563 2564 /* 2565 * Update any record route or timestamp options to include this host. 2566 * Reverse any source route option. 2567 * This routine assumes that the options are well formed i.e. that they 2568 * have already been checked. 2569 */ 2570 static void 2571 icmp_options_update(ipha_t *ipha) 2572 { 2573 ipoptp_t opts; 2574 uchar_t *opt; 2575 uint8_t optval; 2576 ipaddr_t src; /* Our local address */ 2577 ipaddr_t dst; 2578 2579 ip2dbg(("icmp_options_update\n")); 2580 src = ipha->ipha_src; 2581 dst = ipha->ipha_dst; 2582 2583 for (optval = ipoptp_first(&opts, ipha); 2584 optval != IPOPT_EOL; 2585 optval = ipoptp_next(&opts)) { 2586 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 2587 opt = opts.ipoptp_cur; 2588 ip2dbg(("icmp_options_update: opt %d, len %d\n", 2589 optval, opts.ipoptp_len)); 2590 switch (optval) { 2591 int off1, off2; 2592 case IPOPT_SSRR: 2593 case IPOPT_LSRR: 2594 /* 2595 * Reverse the source route. The first entry 2596 * should be the next to last one in the current 2597 * source route (the last entry is our address). 2598 * The last entry should be the final destination. 2599 */ 2600 off1 = IPOPT_MINOFF_SR - 1; 2601 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 2602 if (off2 < 0) { 2603 /* No entries in source route */ 2604 ip1dbg(( 2605 "icmp_options_update: bad src route\n")); 2606 break; 2607 } 2608 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); 2609 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); 2610 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); 2611 off2 -= IP_ADDR_LEN; 2612 2613 while (off1 < off2) { 2614 bcopy((char *)opt + off1, &src, IP_ADDR_LEN); 2615 bcopy((char *)opt + off2, (char *)opt + off1, 2616 IP_ADDR_LEN); 2617 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); 2618 off1 += IP_ADDR_LEN; 2619 off2 -= IP_ADDR_LEN; 2620 } 2621 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 2622 break; 2623 } 2624 } 2625 } 2626 2627 /* 2628 * Process received ICMP Redirect messages. 2629 * Assumes the caller has verified that the headers are in the pulled up mblk. 2630 * Consumes mp. 2631 */ 2632 static void 2633 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira) 2634 { 2635 ire_t *ire, *nire; 2636 ire_t *prev_ire; 2637 ipaddr_t src, dst, gateway; 2638 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 2639 ipha_t *inner_ipha; /* Inner IP header */ 2640 2641 /* Caller already pulled up everything. */ 2642 inner_ipha = (ipha_t *)&icmph[1]; 2643 src = ipha->ipha_src; 2644 dst = inner_ipha->ipha_dst; 2645 gateway = icmph->icmph_rd_gateway; 2646 /* Make sure the new gateway is reachable somehow. */ 2647 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL, 2648 ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL); 2649 /* 2650 * Make sure we had a route for the dest in question and that 2651 * that route was pointing to the old gateway (the source of the 2652 * redirect packet.) 2653 * We do longest match and then compare ire_gateway_addr below. 2654 */ 2655 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES, 2656 NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL); 2657 /* 2658 * Check that 2659 * the redirect was not from ourselves 2660 * the new gateway and the old gateway are directly reachable 2661 */ 2662 if (prev_ire == NULL || ire == NULL || 2663 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) || 2664 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 2665 !(ire->ire_type & IRE_IF_ALL) || 2666 prev_ire->ire_gateway_addr != src) { 2667 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 2668 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill); 2669 freemsg(mp); 2670 if (ire != NULL) 2671 ire_refrele(ire); 2672 if (prev_ire != NULL) 2673 ire_refrele(prev_ire); 2674 return; 2675 } 2676 2677 ire_refrele(prev_ire); 2678 ire_refrele(ire); 2679 2680 /* 2681 * TODO: more precise handling for cases 0, 2, 3, the latter two 2682 * require TOS routing 2683 */ 2684 switch (icmph->icmph_code) { 2685 case 0: 2686 case 1: 2687 /* TODO: TOS specificity for cases 2 and 3 */ 2688 case 2: 2689 case 3: 2690 break; 2691 default: 2692 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); 2693 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill); 2694 freemsg(mp); 2695 return; 2696 } 2697 /* 2698 * Create a Route Association. This will allow us to remember that 2699 * someone we believe told us to use the particular gateway. 2700 */ 2701 ire = ire_create( 2702 (uchar_t *)&dst, /* dest addr */ 2703 (uchar_t *)&ip_g_all_ones, /* mask */ 2704 (uchar_t *)&gateway, /* gateway addr */ 2705 IRE_HOST, 2706 NULL, /* ill */ 2707 ALL_ZONES, 2708 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 2709 NULL, /* tsol_gc_t */ 2710 ipst); 2711 2712 if (ire == NULL) { 2713 freemsg(mp); 2714 return; 2715 } 2716 nire = ire_add(ire); 2717 /* Check if it was a duplicate entry */ 2718 if (nire != NULL && nire != ire) { 2719 ASSERT(nire->ire_identical_ref > 1); 2720 ire_delete(nire); 2721 ire_refrele(nire); 2722 nire = NULL; 2723 } 2724 ire = nire; 2725 if (ire != NULL) { 2726 ire_refrele(ire); /* Held in ire_add */ 2727 2728 /* tell routing sockets that we received a redirect */ 2729 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, 2730 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 2731 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst); 2732 } 2733 2734 /* 2735 * Delete any existing IRE_HOST type redirect ires for this destination. 2736 * This together with the added IRE has the effect of 2737 * modifying an existing redirect. 2738 */ 2739 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL, 2740 ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL); 2741 if (prev_ire != NULL) { 2742 if (prev_ire ->ire_flags & RTF_DYNAMIC) 2743 ire_delete(prev_ire); 2744 ire_refrele(prev_ire); 2745 } 2746 2747 freemsg(mp); 2748 } 2749 2750 /* 2751 * Generate an ICMP parameter problem message. 2752 * When called from ip_output side a minimal ip_recv_attr_t needs to be 2753 * constructed by the caller. 2754 */ 2755 static void 2756 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira) 2757 { 2758 icmph_t icmph; 2759 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 2760 2761 mp = icmp_pkt_err_ok(mp, ira); 2762 if (mp == NULL) 2763 return; 2764 2765 bzero(&icmph, sizeof (icmph_t)); 2766 icmph.icmph_type = ICMP_PARAM_PROBLEM; 2767 icmph.icmph_pp_ptr = ptr; 2768 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs); 2769 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira); 2770 } 2771 2772 /* 2773 * Build and ship an IPv4 ICMP message using the packet data in mp, and 2774 * the ICMP header pointed to by "stuff". (May be called as writer.) 2775 * Note: assumes that icmp_pkt_err_ok has been called to verify that 2776 * an icmp error packet can be sent. 2777 * Assigns an appropriate source address to the packet. If ipha_dst is 2778 * one of our addresses use it for source. Otherwise let ip_output_simple 2779 * pick the source address. 2780 */ 2781 static void 2782 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira) 2783 { 2784 ipaddr_t dst; 2785 icmph_t *icmph; 2786 ipha_t *ipha; 2787 uint_t len_needed; 2788 size_t msg_len; 2789 mblk_t *mp1; 2790 ipaddr_t src; 2791 ire_t *ire; 2792 ip_xmit_attr_t ixas; 2793 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 2794 2795 ipha = (ipha_t *)mp->b_rptr; 2796 2797 bzero(&ixas, sizeof (ixas)); 2798 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4; 2799 ixas.ixa_zoneid = ira->ira_zoneid; 2800 ixas.ixa_ifindex = 0; 2801 ixas.ixa_ipst = ipst; 2802 ixas.ixa_cred = kcred; 2803 ixas.ixa_cpid = NOPID; 2804 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */ 2805 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 2806 2807 if (ira->ira_flags & IRAF_IPSEC_SECURE) { 2808 /* 2809 * Apply IPsec based on how IPsec was applied to 2810 * the packet that had the error. 2811 * 2812 * If it was an outbound packet that caused the ICMP 2813 * error, then the caller will have setup the IRA 2814 * appropriately. 2815 */ 2816 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) { 2817 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 2818 /* Note: mp already consumed and ip_drop_packet done */ 2819 return; 2820 } 2821 } else { 2822 /* 2823 * This is in clear. The icmp message we are building 2824 * here should go out in clear, independent of our policy. 2825 */ 2826 ixas.ixa_flags |= IXAF_NO_IPSEC; 2827 } 2828 2829 /* Remember our eventual destination */ 2830 dst = ipha->ipha_src; 2831 2832 /* 2833 * If the packet was for one of our unicast addresses, make 2834 * sure we respond with that as the source. Otherwise 2835 * have ip_output_simple pick the source address. 2836 */ 2837 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0, 2838 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL, 2839 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL); 2840 if (ire != NULL) { 2841 ire_refrele(ire); 2842 src = ipha->ipha_dst; 2843 } else { 2844 src = INADDR_ANY; 2845 ixas.ixa_flags |= IXAF_SET_SOURCE; 2846 } 2847 2848 /* 2849 * Check if we can send back more then 8 bytes in addition to 2850 * the IP header. We try to send 64 bytes of data and the internal 2851 * header in the special cases of ipv4 encapsulated ipv4 or ipv6. 2852 */ 2853 len_needed = IPH_HDR_LENGTH(ipha); 2854 if (ipha->ipha_protocol == IPPROTO_ENCAP || 2855 ipha->ipha_protocol == IPPROTO_IPV6) { 2856 if (!pullupmsg(mp, -1)) { 2857 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); 2858 ip_drop_output("ipIfStatsOutDiscards", mp, NULL); 2859 freemsg(mp); 2860 return; 2861 } 2862 ipha = (ipha_t *)mp->b_rptr; 2863 2864 if (ipha->ipha_protocol == IPPROTO_ENCAP) { 2865 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + 2866 len_needed)); 2867 } else { 2868 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed); 2869 2870 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6); 2871 len_needed += ip_hdr_length_v6(mp, ip6h); 2872 } 2873 } 2874 len_needed += ipst->ips_ip_icmp_return; 2875 msg_len = msgdsize(mp); 2876 if (msg_len > len_needed) { 2877 (void) adjmsg(mp, len_needed - msg_len); 2878 msg_len = len_needed; 2879 } 2880 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED); 2881 if (mp1 == NULL) { 2882 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors); 2883 freemsg(mp); 2884 return; 2885 } 2886 mp1->b_cont = mp; 2887 mp = mp1; 2888 2889 /* 2890 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this 2891 * node generates be accepted in peace by all on-host destinations. 2892 * If we do NOT assume that all on-host destinations trust 2893 * self-generated ICMP messages, then rework here, ip6.c, and spd.c. 2894 * (Look for IXAF_TRUSTED_ICMP). 2895 */ 2896 ixas.ixa_flags |= IXAF_TRUSTED_ICMP; 2897 2898 ipha = (ipha_t *)mp->b_rptr; 2899 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); 2900 *ipha = icmp_ipha; 2901 ipha->ipha_src = src; 2902 ipha->ipha_dst = dst; 2903 ipha->ipha_ttl = ipst->ips_ip_def_ttl; 2904 msg_len += sizeof (icmp_ipha) + len; 2905 if (msg_len > IP_MAXPACKET) { 2906 (void) adjmsg(mp, IP_MAXPACKET - msg_len); 2907 msg_len = IP_MAXPACKET; 2908 } 2909 ipha->ipha_length = htons((uint16_t)msg_len); 2910 icmph = (icmph_t *)&ipha[1]; 2911 bcopy(stuff, icmph, len); 2912 icmph->icmph_checksum = 0; 2913 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); 2914 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); 2915 2916 (void) ip_output_simple(mp, &ixas); 2917 ixa_cleanup(&ixas); 2918 } 2919 2920 /* 2921 * Determine if an ICMP error packet can be sent given the rate limit. 2922 * The limit consists of an average frequency (icmp_pkt_err_interval measured 2923 * in milliseconds) and a burst size. Burst size number of packets can 2924 * be sent arbitrarely closely spaced. 2925 * The state is tracked using two variables to implement an approximate 2926 * token bucket filter: 2927 * icmp_pkt_err_last - lbolt value when the last burst started 2928 * icmp_pkt_err_sent - number of packets sent in current burst 2929 */ 2930 boolean_t 2931 icmp_err_rate_limit(ip_stack_t *ipst) 2932 { 2933 clock_t now = TICK_TO_MSEC(ddi_get_lbolt()); 2934 uint_t refilled; /* Number of packets refilled in tbf since last */ 2935 /* Guard against changes by loading into local variable */ 2936 uint_t err_interval = ipst->ips_ip_icmp_err_interval; 2937 2938 if (err_interval == 0) 2939 return (B_FALSE); 2940 2941 if (ipst->ips_icmp_pkt_err_last > now) { 2942 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ 2943 ipst->ips_icmp_pkt_err_last = 0; 2944 ipst->ips_icmp_pkt_err_sent = 0; 2945 } 2946 /* 2947 * If we are in a burst update the token bucket filter. 2948 * Update the "last" time to be close to "now" but make sure 2949 * we don't loose precision. 2950 */ 2951 if (ipst->ips_icmp_pkt_err_sent != 0) { 2952 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval; 2953 if (refilled > ipst->ips_icmp_pkt_err_sent) { 2954 ipst->ips_icmp_pkt_err_sent = 0; 2955 } else { 2956 ipst->ips_icmp_pkt_err_sent -= refilled; 2957 ipst->ips_icmp_pkt_err_last += refilled * err_interval; 2958 } 2959 } 2960 if (ipst->ips_icmp_pkt_err_sent == 0) { 2961 /* Start of new burst */ 2962 ipst->ips_icmp_pkt_err_last = now; 2963 } 2964 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) { 2965 ipst->ips_icmp_pkt_err_sent++; 2966 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", 2967 ipst->ips_icmp_pkt_err_sent)); 2968 return (B_FALSE); 2969 } 2970 ip1dbg(("icmp_err_rate_limit: dropped\n")); 2971 return (B_TRUE); 2972 } 2973 2974 /* 2975 * Check if it is ok to send an IPv4 ICMP error packet in 2976 * response to the IPv4 packet in mp. 2977 * Free the message and return null if no 2978 * ICMP error packet should be sent. 2979 */ 2980 static mblk_t * 2981 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira) 2982 { 2983 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 2984 icmph_t *icmph; 2985 ipha_t *ipha; 2986 uint_t len_needed; 2987 2988 if (!mp) 2989 return (NULL); 2990 ipha = (ipha_t *)mp->b_rptr; 2991 if (ip_csum_hdr(ipha)) { 2992 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); 2993 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL); 2994 freemsg(mp); 2995 return (NULL); 2996 } 2997 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST || 2998 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST || 2999 CLASSD(ipha->ipha_dst) || 3000 CLASSD(ipha->ipha_src) || 3001 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { 3002 /* Note: only errors to the fragment with offset 0 */ 3003 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3004 freemsg(mp); 3005 return (NULL); 3006 } 3007 if (ipha->ipha_protocol == IPPROTO_ICMP) { 3008 /* 3009 * Check the ICMP type. RFC 1122 sez: don't send ICMP 3010 * errors in response to any ICMP errors. 3011 */ 3012 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; 3013 if (mp->b_wptr - mp->b_rptr < len_needed) { 3014 if (!pullupmsg(mp, len_needed)) { 3015 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); 3016 freemsg(mp); 3017 return (NULL); 3018 } 3019 ipha = (ipha_t *)mp->b_rptr; 3020 } 3021 icmph = (icmph_t *) 3022 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); 3023 switch (icmph->icmph_type) { 3024 case ICMP_DEST_UNREACHABLE: 3025 case ICMP_SOURCE_QUENCH: 3026 case ICMP_TIME_EXCEEDED: 3027 case ICMP_PARAM_PROBLEM: 3028 case ICMP_REDIRECT: 3029 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3030 freemsg(mp); 3031 return (NULL); 3032 default: 3033 break; 3034 } 3035 } 3036 /* 3037 * If this is a labeled system, then check to see if we're allowed to 3038 * send a response to this particular sender. If not, then just drop. 3039 */ 3040 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) { 3041 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); 3042 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); 3043 freemsg(mp); 3044 return (NULL); 3045 } 3046 if (icmp_err_rate_limit(ipst)) { 3047 /* 3048 * Only send ICMP error packets every so often. 3049 * This should be done on a per port/source basis, 3050 * but for now this will suffice. 3051 */ 3052 freemsg(mp); 3053 return (NULL); 3054 } 3055 return (mp); 3056 } 3057 3058 /* 3059 * Called when a packet was sent out the same link that it arrived on. 3060 * Check if it is ok to send a redirect and then send it. 3061 */ 3062 void 3063 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire, 3064 ip_recv_attr_t *ira) 3065 { 3066 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 3067 ipaddr_t src, nhop; 3068 mblk_t *mp1; 3069 ire_t *nhop_ire; 3070 3071 /* 3072 * Check the source address to see if it originated 3073 * on the same logical subnet it is going back out on. 3074 * If so, we should be able to send it a redirect. 3075 * Avoid sending a redirect if the destination 3076 * is directly connected (i.e., we matched an IRE_ONLINK), 3077 * or if the packet was source routed out this interface. 3078 * 3079 * We avoid sending a redirect if the 3080 * destination is directly connected 3081 * because it is possible that multiple 3082 * IP subnets may have been configured on 3083 * the link, and the source may not 3084 * be on the same subnet as ip destination, 3085 * even though they are on the same 3086 * physical link. 3087 */ 3088 if ((ire->ire_type & IRE_ONLINK) || 3089 ip_source_routed(ipha, ipst)) 3090 return; 3091 3092 nhop_ire = ire_nexthop(ire); 3093 if (nhop_ire == NULL) 3094 return; 3095 3096 nhop = nhop_ire->ire_addr; 3097 3098 if (nhop_ire->ire_type & IRE_IF_CLONE) { 3099 ire_t *ire2; 3100 3101 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */ 3102 mutex_enter(&nhop_ire->ire_lock); 3103 ire2 = nhop_ire->ire_dep_parent; 3104 if (ire2 != NULL) 3105 ire_refhold(ire2); 3106 mutex_exit(&nhop_ire->ire_lock); 3107 ire_refrele(nhop_ire); 3108 nhop_ire = ire2; 3109 } 3110 if (nhop_ire == NULL) 3111 return; 3112 3113 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE)); 3114 3115 src = ipha->ipha_src; 3116 3117 /* 3118 * We look at the interface ire for the nexthop, 3119 * to see if ipha_src is in the same subnet 3120 * as the nexthop. 3121 */ 3122 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) { 3123 /* 3124 * The source is directly connected. 3125 */ 3126 mp1 = copymsg(mp); 3127 if (mp1 != NULL) { 3128 icmp_send_redirect(mp1, nhop, ira); 3129 } 3130 } 3131 ire_refrele(nhop_ire); 3132 } 3133 3134 /* 3135 * Generate an ICMP redirect message. 3136 */ 3137 static void 3138 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira) 3139 { 3140 icmph_t icmph; 3141 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 3142 3143 mp = icmp_pkt_err_ok(mp, ira); 3144 if (mp == NULL) 3145 return; 3146 3147 bzero(&icmph, sizeof (icmph_t)); 3148 icmph.icmph_type = ICMP_REDIRECT; 3149 icmph.icmph_code = 1; 3150 icmph.icmph_rd_gateway = gateway; 3151 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects); 3152 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira); 3153 } 3154 3155 /* 3156 * Generate an ICMP time exceeded message. 3157 */ 3158 void 3159 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira) 3160 { 3161 icmph_t icmph; 3162 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 3163 3164 mp = icmp_pkt_err_ok(mp, ira); 3165 if (mp == NULL) 3166 return; 3167 3168 bzero(&icmph, sizeof (icmph_t)); 3169 icmph.icmph_type = ICMP_TIME_EXCEEDED; 3170 icmph.icmph_code = code; 3171 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds); 3172 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira); 3173 } 3174 3175 /* 3176 * Generate an ICMP unreachable message. 3177 * When called from ip_output side a minimal ip_recv_attr_t needs to be 3178 * constructed by the caller. 3179 */ 3180 void 3181 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira) 3182 { 3183 icmph_t icmph; 3184 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 3185 3186 mp = icmp_pkt_err_ok(mp, ira); 3187 if (mp == NULL) 3188 return; 3189 3190 bzero(&icmph, sizeof (icmph_t)); 3191 icmph.icmph_type = ICMP_DEST_UNREACHABLE; 3192 icmph.icmph_code = code; 3193 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); 3194 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira); 3195 } 3196 3197 /* 3198 * Latch in the IPsec state for a stream based the policy in the listener 3199 * and the actions in the ip_recv_attr_t. 3200 * Called directly from TCP and SCTP. 3201 */ 3202 boolean_t 3203 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira) 3204 { 3205 ASSERT(lconnp->conn_policy != NULL); 3206 ASSERT(connp->conn_policy == NULL); 3207 3208 IPPH_REFHOLD(lconnp->conn_policy); 3209 connp->conn_policy = lconnp->conn_policy; 3210 3211 if (ira->ira_ipsec_action != NULL) { 3212 if (connp->conn_latch == NULL) { 3213 connp->conn_latch = iplatch_create(); 3214 if (connp->conn_latch == NULL) 3215 return (B_FALSE); 3216 } 3217 ipsec_latch_inbound(connp, ira); 3218 } 3219 return (B_TRUE); 3220 } 3221 3222 /* 3223 * Verify whether or not the IP address is a valid local address. 3224 * Could be a unicast, including one for a down interface. 3225 * If allow_mcbc then a multicast or broadcast address is also 3226 * acceptable. 3227 * 3228 * In the case of a broadcast/multicast address, however, the 3229 * upper protocol is expected to reset the src address 3230 * to zero when we return IPVL_MCAST/IPVL_BCAST so that 3231 * no packets are emitted with broadcast/multicast address as 3232 * source address (that violates hosts requirements RFC 1122) 3233 * The addresses valid for bind are: 3234 * (1) - INADDR_ANY (0) 3235 * (2) - IP address of an UP interface 3236 * (3) - IP address of a DOWN interface 3237 * (4) - valid local IP broadcast addresses. In this case 3238 * the conn will only receive packets destined to 3239 * the specified broadcast address. 3240 * (5) - a multicast address. In this case 3241 * the conn will only receive packets destined to 3242 * the specified multicast address. Note: the 3243 * application still has to issue an 3244 * IP_ADD_MEMBERSHIP socket option. 3245 * 3246 * In all the above cases, the bound address must be valid in the current zone. 3247 * When the address is loopback, multicast or broadcast, there might be many 3248 * matching IREs so bind has to look up based on the zone. 3249 */ 3250 ip_laddr_t 3251 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid, 3252 ip_stack_t *ipst, boolean_t allow_mcbc) 3253 { 3254 ire_t *src_ire; 3255 3256 ASSERT(src_addr != INADDR_ANY); 3257 3258 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0, 3259 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL); 3260 3261 /* 3262 * If an address other than in6addr_any is requested, 3263 * we verify that it is a valid address for bind 3264 * Note: Following code is in if-else-if form for 3265 * readability compared to a condition check. 3266 */ 3267 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) { 3268 /* 3269 * (2) Bind to address of local UP interface 3270 */ 3271 ire_refrele(src_ire); 3272 return (IPVL_UNICAST_UP); 3273 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) { 3274 /* 3275 * (4) Bind to broadcast address 3276 */ 3277 ire_refrele(src_ire); 3278 if (allow_mcbc) 3279 return (IPVL_BCAST); 3280 else 3281 return (IPVL_BAD); 3282 } else if (CLASSD(src_addr)) { 3283 /* (5) bind to multicast address. */ 3284 if (src_ire != NULL) 3285 ire_refrele(src_ire); 3286 3287 if (allow_mcbc) 3288 return (IPVL_MCAST); 3289 else 3290 return (IPVL_BAD); 3291 } else { 3292 ipif_t *ipif; 3293 3294 /* 3295 * (3) Bind to address of local DOWN interface? 3296 * (ipif_lookup_addr() looks up all interfaces 3297 * but we do not get here for UP interfaces 3298 * - case (2) above) 3299 */ 3300 if (src_ire != NULL) 3301 ire_refrele(src_ire); 3302 3303 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst); 3304 if (ipif == NULL) 3305 return (IPVL_BAD); 3306 3307 /* Not a useful source? */ 3308 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) { 3309 ipif_refrele(ipif); 3310 return (IPVL_BAD); 3311 } 3312 ipif_refrele(ipif); 3313 return (IPVL_UNICAST_DOWN); 3314 } 3315 } 3316 3317 /* 3318 * Insert in the bind fanout for IPv4 and IPv6. 3319 * The caller should already have used ip_laddr_verify_v*() before calling 3320 * this. 3321 */ 3322 int 3323 ip_laddr_fanout_insert(conn_t *connp) 3324 { 3325 int error; 3326 3327 /* 3328 * Allow setting new policies. For example, disconnects result 3329 * in us being called. As we would have set conn_policy_cached 3330 * to B_TRUE before, we should set it to B_FALSE, so that policy 3331 * can change after the disconnect. 3332 */ 3333 connp->conn_policy_cached = B_FALSE; 3334 3335 error = ipcl_bind_insert(connp); 3336 if (error != 0) { 3337 if (connp->conn_anon_port) { 3338 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 3339 connp->conn_mlp_type, connp->conn_proto, 3340 ntohs(connp->conn_lport), B_FALSE); 3341 } 3342 connp->conn_mlp_type = mlptSingle; 3343 } 3344 return (error); 3345 } 3346 3347 /* 3348 * Verify that both the source and destination addresses are valid. If 3349 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable, 3350 * i.e. have no route to it. Protocols like TCP want to verify destination 3351 * reachability, while tunnels do not. 3352 * 3353 * Determine the route, the interface, and (optionally) the source address 3354 * to use to reach a given destination. 3355 * Note that we allow connect to broadcast and multicast addresses when 3356 * IPDF_ALLOW_MCBC is set. 3357 * first_hop and dst_addr are normally the same, but if source routing 3358 * they will differ; in that case the first_hop is what we'll use for the 3359 * routing lookup but the dce and label checks will be done on dst_addr, 3360 * 3361 * If uinfo is set, then we fill in the best available information 3362 * we have for the destination. This is based on (in priority order) any 3363 * metrics and path MTU stored in a dce_t, route metrics, and finally the 3364 * ill_mtu. 3365 * 3366 * Tsol note: If we have a source route then dst_addr != firsthop. But we 3367 * always do the label check on dst_addr. 3368 */ 3369 int 3370 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop, 3371 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode) 3372 { 3373 ire_t *ire = NULL; 3374 int error = 0; 3375 ipaddr_t setsrc; /* RTF_SETSRC */ 3376 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */ 3377 ip_stack_t *ipst = ixa->ixa_ipst; 3378 dce_t *dce; 3379 uint_t pmtu; 3380 uint_t generation; 3381 nce_t *nce; 3382 ill_t *ill = NULL; 3383 boolean_t multirt = B_FALSE; 3384 3385 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4); 3386 3387 /* 3388 * We never send to zero; the ULPs map it to the loopback address. 3389 * We can't allow it since we use zero to mean unitialized in some 3390 * places. 3391 */ 3392 ASSERT(dst_addr != INADDR_ANY); 3393 3394 if (is_system_labeled()) { 3395 ts_label_t *tsl = NULL; 3396 3397 error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION, 3398 mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl); 3399 if (error != 0) 3400 return (error); 3401 if (tsl != NULL) { 3402 /* Update the label */ 3403 ip_xmit_attr_replace_tsl(ixa, tsl); 3404 } 3405 } 3406 3407 setsrc = INADDR_ANY; 3408 /* 3409 * Select a route; For IPMP interfaces, we would only select 3410 * a "hidden" route (i.e., going through a specific under_ill) 3411 * if ixa_ifindex has been specified. 3412 */ 3413 ire = ip_select_route_v4(firsthop, *src_addrp, ixa, 3414 &generation, &setsrc, &error, &multirt); 3415 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */ 3416 if (error != 0) 3417 goto bad_addr; 3418 3419 /* 3420 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set. 3421 * If IPDF_VERIFY_DST is set, the destination must be reachable; 3422 * Otherwise the destination needn't be reachable. 3423 * 3424 * If we match on a reject or black hole, then we've got a 3425 * local failure. May as well fail out the connect() attempt, 3426 * since it's never going to succeed. 3427 */ 3428 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 3429 /* 3430 * If we're verifying destination reachability, we always want 3431 * to complain here. 3432 * 3433 * If we're not verifying destination reachability but the 3434 * destination has a route, we still want to fail on the 3435 * temporary address and broadcast address tests. 3436 * 3437 * In both cases do we let the code continue so some reasonable 3438 * information is returned to the caller. That enables the 3439 * caller to use (and even cache) the IRE. conn_ip_ouput will 3440 * use the generation mismatch path to check for the unreachable 3441 * case thereby avoiding any specific check in the main path. 3442 */ 3443 ASSERT(generation == IRE_GENERATION_VERIFY); 3444 if (flags & IPDF_VERIFY_DST) { 3445 /* 3446 * Set errno but continue to set up ixa_ire to be 3447 * the RTF_REJECT|RTF_BLACKHOLE IRE. 3448 * That allows callers to use ip_output to get an 3449 * ICMP error back. 3450 */ 3451 if (!(ire->ire_type & IRE_HOST)) 3452 error = ENETUNREACH; 3453 else 3454 error = EHOSTUNREACH; 3455 } 3456 } 3457 3458 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) && 3459 !(flags & IPDF_ALLOW_MCBC)) { 3460 ire_refrele(ire); 3461 ire = ire_reject(ipst, B_FALSE); 3462 generation = IRE_GENERATION_VERIFY; 3463 error = ENETUNREACH; 3464 } 3465 3466 /* Cache things */ 3467 if (ixa->ixa_ire != NULL) 3468 ire_refrele_notr(ixa->ixa_ire); 3469 #ifdef DEBUG 3470 ire_refhold_notr(ire); 3471 ire_refrele(ire); 3472 #endif 3473 ixa->ixa_ire = ire; 3474 ixa->ixa_ire_generation = generation; 3475 3476 /* 3477 * For multicast with multirt we have a flag passed back from 3478 * ire_lookup_multi_ill_v4 since we don't have an IRE for each 3479 * possible multicast address. 3480 * We also need a flag for multicast since we can't check 3481 * whether RTF_MULTIRT is set in ixa_ire for multicast. 3482 */ 3483 if (multirt) { 3484 ixa->ixa_postfragfn = ip_postfrag_multirt_v4; 3485 ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST; 3486 } else { 3487 ixa->ixa_postfragfn = ire->ire_postfragfn; 3488 ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST; 3489 } 3490 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) { 3491 /* Get an nce to cache. */ 3492 nce = ire_to_nce(ire, firsthop, NULL); 3493 if (nce == NULL) { 3494 /* Allocation failure? */ 3495 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY; 3496 } else { 3497 if (ixa->ixa_nce != NULL) 3498 nce_refrele(ixa->ixa_nce); 3499 ixa->ixa_nce = nce; 3500 } 3501 } 3502 3503 /* 3504 * If the source address is a loopback address, the 3505 * destination had best be local or multicast. 3506 * If we are sending to an IRE_LOCAL using a loopback source then 3507 * it had better be the same zoneid. 3508 */ 3509 if (*src_addrp == htonl(INADDR_LOOPBACK)) { 3510 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) { 3511 ire = NULL; /* Stored in ixa_ire */ 3512 error = EADDRNOTAVAIL; 3513 goto bad_addr; 3514 } 3515 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) { 3516 ire = NULL; /* Stored in ixa_ire */ 3517 error = EADDRNOTAVAIL; 3518 goto bad_addr; 3519 } 3520 } 3521 if (ire->ire_type & IRE_BROADCAST) { 3522 /* 3523 * If the ULP didn't have a specified source, then we 3524 * make sure we reselect the source when sending 3525 * broadcasts out different interfaces. 3526 */ 3527 if (flags & IPDF_SELECT_SRC) 3528 ixa->ixa_flags |= IXAF_SET_SOURCE; 3529 else 3530 ixa->ixa_flags &= ~IXAF_SET_SOURCE; 3531 } 3532 3533 /* 3534 * Does the caller want us to pick a source address? 3535 */ 3536 if (flags & IPDF_SELECT_SRC) { 3537 ipaddr_t src_addr; 3538 3539 /* 3540 * We use use ire_nexthop_ill to avoid the under ipmp 3541 * interface for source address selection. Note that for ipmp 3542 * probe packets, ixa_ifindex would have been specified, and 3543 * the ip_select_route() invocation would have picked an ire 3544 * will ire_ill pointing at an under interface. 3545 */ 3546 ill = ire_nexthop_ill(ire); 3547 3548 /* If unreachable we have no ill but need some source */ 3549 if (ill == NULL) { 3550 src_addr = htonl(INADDR_LOOPBACK); 3551 /* Make sure we look for a better source address */ 3552 generation = SRC_GENERATION_VERIFY; 3553 } else { 3554 error = ip_select_source_v4(ill, setsrc, dst_addr, 3555 ixa->ixa_multicast_ifaddr, zoneid, 3556 ipst, &src_addr, &generation, NULL); 3557 if (error != 0) { 3558 ire = NULL; /* Stored in ixa_ire */ 3559 goto bad_addr; 3560 } 3561 } 3562 3563 /* 3564 * We allow the source address to to down. 3565 * However, we check that we don't use the loopback address 3566 * as a source when sending out on the wire. 3567 */ 3568 if ((src_addr == htonl(INADDR_LOOPBACK)) && 3569 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) && 3570 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) { 3571 ire = NULL; /* Stored in ixa_ire */ 3572 error = EADDRNOTAVAIL; 3573 goto bad_addr; 3574 } 3575 3576 *src_addrp = src_addr; 3577 ixa->ixa_src_generation = generation; 3578 } 3579 3580 if (flags & IPDF_UNIQUE_DCE) { 3581 /* Fallback to the default dce if allocation fails */ 3582 dce = dce_lookup_and_add_v4(dst_addr, ipst); 3583 if (dce != NULL) 3584 generation = dce->dce_generation; 3585 else 3586 dce = dce_lookup_v4(dst_addr, ipst, &generation); 3587 } else { 3588 dce = dce_lookup_v4(dst_addr, ipst, &generation); 3589 } 3590 ASSERT(dce != NULL); 3591 if (ixa->ixa_dce != NULL) 3592 dce_refrele_notr(ixa->ixa_dce); 3593 #ifdef DEBUG 3594 dce_refhold_notr(dce); 3595 dce_refrele(dce); 3596 #endif 3597 ixa->ixa_dce = dce; 3598 ixa->ixa_dce_generation = generation; 3599 3600 /* 3601 * Make sure we don't leave an unreachable ixa_nce in place 3602 * since ip_select_route is used when we unplumb i.e., remove 3603 * references on ixa_ire, ixa_nce, and ixa_dce. 3604 */ 3605 nce = ixa->ixa_nce; 3606 if (nce != NULL && nce->nce_is_condemned) { 3607 nce_refrele(nce); 3608 ixa->ixa_nce = NULL; 3609 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY; 3610 } 3611 3612 /* 3613 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired. 3614 * However, we can't do it for IPv4 multicast or broadcast. 3615 */ 3616 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) 3617 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY; 3618 3619 /* 3620 * Set initial value for fragmentation limit. Either conn_ip_output 3621 * or ULP might updates it when there are routing changes. 3622 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT. 3623 */ 3624 pmtu = ip_get_pmtu(ixa); 3625 ixa->ixa_fragsize = pmtu; 3626 /* Make sure ixa_fragsize and ixa_pmtu remain identical */ 3627 if (ixa->ixa_flags & IXAF_VERIFY_PMTU) 3628 ixa->ixa_pmtu = pmtu; 3629 3630 /* 3631 * Extract information useful for some transports. 3632 * First we look for DCE metrics. Then we take what we have in 3633 * the metrics in the route, where the offlink is used if we have 3634 * one. 3635 */ 3636 if (uinfo != NULL) { 3637 bzero(uinfo, sizeof (*uinfo)); 3638 3639 if (dce->dce_flags & DCEF_UINFO) 3640 *uinfo = dce->dce_uinfo; 3641 3642 rts_merge_metrics(uinfo, &ire->ire_metrics); 3643 3644 /* Allow ire_metrics to decrease the path MTU from above */ 3645 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu) 3646 uinfo->iulp_mtu = pmtu; 3647 3648 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0; 3649 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0; 3650 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0; 3651 } 3652 3653 if (ill != NULL) 3654 ill_refrele(ill); 3655 3656 return (error); 3657 3658 bad_addr: 3659 if (ire != NULL) 3660 ire_refrele(ire); 3661 3662 if (ill != NULL) 3663 ill_refrele(ill); 3664 3665 /* 3666 * Make sure we don't leave an unreachable ixa_nce in place 3667 * since ip_select_route is used when we unplumb i.e., remove 3668 * references on ixa_ire, ixa_nce, and ixa_dce. 3669 */ 3670 nce = ixa->ixa_nce; 3671 if (nce != NULL && nce->nce_is_condemned) { 3672 nce_refrele(nce); 3673 ixa->ixa_nce = NULL; 3674 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY; 3675 } 3676 3677 return (error); 3678 } 3679 3680 3681 /* 3682 * Get the base MTU for the case when path MTU discovery is not used. 3683 * Takes the MTU of the IRE into account. 3684 */ 3685 uint_t 3686 ip_get_base_mtu(ill_t *ill, ire_t *ire) 3687 { 3688 uint_t mtu = ill->ill_mtu; 3689 uint_t iremtu = ire->ire_metrics.iulp_mtu; 3690 3691 if (iremtu != 0 && iremtu < mtu) 3692 mtu = iremtu; 3693 3694 return (mtu); 3695 } 3696 3697 /* 3698 * Get the PMTU for the attributes. Handles both IPv4 and IPv6. 3699 * Assumes that ixa_ire, dce, and nce have already been set up. 3700 * 3701 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired. 3702 * We avoid path MTU discovery if it is disabled with ndd. 3703 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4. 3704 * 3705 * NOTE: We also used to turn it off for source routed packets. That 3706 * is no longer required since the dce is per final destination. 3707 */ 3708 uint_t 3709 ip_get_pmtu(ip_xmit_attr_t *ixa) 3710 { 3711 ip_stack_t *ipst = ixa->ixa_ipst; 3712 dce_t *dce; 3713 nce_t *nce; 3714 ire_t *ire; 3715 uint_t pmtu; 3716 3717 ire = ixa->ixa_ire; 3718 dce = ixa->ixa_dce; 3719 nce = ixa->ixa_nce; 3720 3721 /* 3722 * If path MTU discovery has been turned off by ndd, then we ignore 3723 * any dce_pmtu and for IPv4 we will not set DF. 3724 */ 3725 if (!ipst->ips_ip_path_mtu_discovery) 3726 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY; 3727 3728 pmtu = IP_MAXPACKET; 3729 /* 3730 * Decide whether whether IPv4 sets DF 3731 * For IPv6 "no DF" means to use the 1280 mtu 3732 */ 3733 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) { 3734 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF; 3735 } else { 3736 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF; 3737 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) 3738 pmtu = IPV6_MIN_MTU; 3739 } 3740 3741 /* Check if the PMTU is to old before we use it */ 3742 if ((dce->dce_flags & DCEF_PMTU) && 3743 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time > 3744 ipst->ips_ip_pathmtu_interval) { 3745 /* 3746 * Older than 20 minutes. Drop the path MTU information. 3747 */ 3748 mutex_enter(&dce->dce_lock); 3749 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU); 3750 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64()); 3751 mutex_exit(&dce->dce_lock); 3752 dce_increment_generation(dce); 3753 } 3754 3755 /* The metrics on the route can lower the path MTU */ 3756 if (ire->ire_metrics.iulp_mtu != 0 && 3757 ire->ire_metrics.iulp_mtu < pmtu) 3758 pmtu = ire->ire_metrics.iulp_mtu; 3759 3760 /* 3761 * If the path MTU is smaller than some minimum, we still use dce_pmtu 3762 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear 3763 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4. 3764 */ 3765 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) { 3766 if (dce->dce_flags & DCEF_PMTU) { 3767 if (dce->dce_pmtu < pmtu) 3768 pmtu = dce->dce_pmtu; 3769 3770 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) { 3771 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL; 3772 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF; 3773 } else { 3774 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL; 3775 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF; 3776 } 3777 } else { 3778 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL; 3779 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF; 3780 } 3781 } 3782 3783 /* 3784 * If we have an IRE_LOCAL we use the loopback mtu instead of 3785 * the ill for going out the wire i.e., IRE_LOCAL gets the same 3786 * mtu as IRE_LOOPBACK. 3787 */ 3788 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) { 3789 uint_t loopback_mtu; 3790 3791 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ? 3792 ip_loopback_mtu_v6plus : ip_loopback_mtuplus; 3793 3794 if (loopback_mtu < pmtu) 3795 pmtu = loopback_mtu; 3796 } else if (nce != NULL) { 3797 /* 3798 * Make sure we don't exceed the interface MTU. 3799 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have 3800 * an ill. We'd use the above IP_MAXPACKET in that case just 3801 * to tell the transport something larger than zero. 3802 */ 3803 if (nce->nce_common->ncec_ill->ill_mtu < pmtu) 3804 pmtu = nce->nce_common->ncec_ill->ill_mtu; 3805 if (nce->nce_common->ncec_ill != nce->nce_ill && 3806 nce->nce_ill->ill_mtu < pmtu) { 3807 /* 3808 * for interfaces in an IPMP group, the mtu of 3809 * the nce_ill (under_ill) could be different 3810 * from the mtu of the ncec_ill, so we take the 3811 * min of the two. 3812 */ 3813 pmtu = nce->nce_ill->ill_mtu; 3814 } 3815 } 3816 3817 /* 3818 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data. 3819 * Only applies to IPv6. 3820 */ 3821 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) { 3822 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) { 3823 switch (ixa->ixa_use_min_mtu) { 3824 case IPV6_USE_MIN_MTU_MULTICAST: 3825 if (ire->ire_type & IRE_MULTICAST) 3826 pmtu = IPV6_MIN_MTU; 3827 break; 3828 case IPV6_USE_MIN_MTU_ALWAYS: 3829 pmtu = IPV6_MIN_MTU; 3830 break; 3831 case IPV6_USE_MIN_MTU_NEVER: 3832 break; 3833 } 3834 } else { 3835 /* Default is IPV6_USE_MIN_MTU_MULTICAST */ 3836 if (ire->ire_type & IRE_MULTICAST) 3837 pmtu = IPV6_MIN_MTU; 3838 } 3839 } 3840 3841 /* 3842 * After receiving an ICMPv6 "packet too big" message with a 3843 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 3844 * will insert a 8-byte fragment header in every packet. We compensate 3845 * for those cases by returning a smaller path MTU to the ULP. 3846 * 3847 * In the case of CGTP then ip_output will add a fragment header. 3848 * Make sure there is room for it by telling a smaller number 3849 * to the transport. 3850 * 3851 * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here 3852 * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu() 3853 * which is the size of the packets it can send. 3854 */ 3855 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) { 3856 if ((dce->dce_flags & DCEF_TOO_SMALL_PMTU) || 3857 (ire->ire_flags & RTF_MULTIRT) || 3858 (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) { 3859 pmtu -= sizeof (ip6_frag_t); 3860 ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR; 3861 } 3862 } 3863 3864 return (pmtu); 3865 } 3866 3867 /* 3868 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping 3869 * the final piece where we don't. Return a pointer to the first mblk in the 3870 * result, and update the pointer to the next mblk to chew on. If anything 3871 * goes wrong (i.e., dupb fails), we waste everything in sight and return a 3872 * NULL pointer. 3873 */ 3874 mblk_t * 3875 ip_carve_mp(mblk_t **mpp, ssize_t len) 3876 { 3877 mblk_t *mp0; 3878 mblk_t *mp1; 3879 mblk_t *mp2; 3880 3881 if (!len || !mpp || !(mp0 = *mpp)) 3882 return (NULL); 3883 /* If we aren't going to consume the first mblk, we need a dup. */ 3884 if (mp0->b_wptr - mp0->b_rptr > len) { 3885 mp1 = dupb(mp0); 3886 if (mp1) { 3887 /* Partition the data between the two mblks. */ 3888 mp1->b_wptr = mp1->b_rptr + len; 3889 mp0->b_rptr = mp1->b_wptr; 3890 /* 3891 * after adjustments if mblk not consumed is now 3892 * unaligned, try to align it. If this fails free 3893 * all messages and let upper layer recover. 3894 */ 3895 if (!OK_32PTR(mp0->b_rptr)) { 3896 if (!pullupmsg(mp0, -1)) { 3897 freemsg(mp0); 3898 freemsg(mp1); 3899 *mpp = NULL; 3900 return (NULL); 3901 } 3902 } 3903 } 3904 return (mp1); 3905 } 3906 /* Eat through as many mblks as we need to get len bytes. */ 3907 len -= mp0->b_wptr - mp0->b_rptr; 3908 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { 3909 if (mp2->b_wptr - mp2->b_rptr > len) { 3910 /* 3911 * We won't consume the entire last mblk. Like 3912 * above, dup and partition it. 3913 */ 3914 mp1->b_cont = dupb(mp2); 3915 mp1 = mp1->b_cont; 3916 if (!mp1) { 3917 /* 3918 * Trouble. Rather than go to a lot of 3919 * trouble to clean up, we free the messages. 3920 * This won't be any worse than losing it on 3921 * the wire. 3922 */ 3923 freemsg(mp0); 3924 freemsg(mp2); 3925 *mpp = NULL; 3926 return (NULL); 3927 } 3928 mp1->b_wptr = mp1->b_rptr + len; 3929 mp2->b_rptr = mp1->b_wptr; 3930 /* 3931 * after adjustments if mblk not consumed is now 3932 * unaligned, try to align it. If this fails free 3933 * all messages and let upper layer recover. 3934 */ 3935 if (!OK_32PTR(mp2->b_rptr)) { 3936 if (!pullupmsg(mp2, -1)) { 3937 freemsg(mp0); 3938 freemsg(mp2); 3939 *mpp = NULL; 3940 return (NULL); 3941 } 3942 } 3943 *mpp = mp2; 3944 return (mp0); 3945 } 3946 /* Decrement len by the amount we just got. */ 3947 len -= mp2->b_wptr - mp2->b_rptr; 3948 } 3949 /* 3950 * len should be reduced to zero now. If not our caller has 3951 * screwed up. 3952 */ 3953 if (len) { 3954 /* Shouldn't happen! */ 3955 freemsg(mp0); 3956 *mpp = NULL; 3957 return (NULL); 3958 } 3959 /* 3960 * We consumed up to exactly the end of an mblk. Detach the part 3961 * we are returning from the rest of the chain. 3962 */ 3963 mp1->b_cont = NULL; 3964 *mpp = mp2; 3965 return (mp0); 3966 } 3967 3968 /* The ill stream is being unplumbed. Called from ip_close */ 3969 int 3970 ip_modclose(ill_t *ill) 3971 { 3972 boolean_t success; 3973 ipsq_t *ipsq; 3974 ipif_t *ipif; 3975 queue_t *q = ill->ill_rq; 3976 ip_stack_t *ipst = ill->ill_ipst; 3977 int i; 3978 arl_ill_common_t *ai = ill->ill_common; 3979 3980 /* 3981 * The punlink prior to this may have initiated a capability 3982 * negotiation. But ipsq_enter will block until that finishes or 3983 * times out. 3984 */ 3985 success = ipsq_enter(ill, B_FALSE, NEW_OP); 3986 3987 /* 3988 * Open/close/push/pop is guaranteed to be single threaded 3989 * per stream by STREAMS. FS guarantees that all references 3990 * from top are gone before close is called. So there can't 3991 * be another close thread that has set CONDEMNED on this ill. 3992 * and cause ipsq_enter to return failure. 3993 */ 3994 ASSERT(success); 3995 ipsq = ill->ill_phyint->phyint_ipsq; 3996 3997 /* 3998 * Mark it condemned. No new reference will be made to this ill. 3999 * Lookup functions will return an error. Threads that try to 4000 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures 4001 * that the refcnt will drop down to zero. 4002 */ 4003 mutex_enter(&ill->ill_lock); 4004 ill->ill_state_flags |= ILL_CONDEMNED; 4005 for (ipif = ill->ill_ipif; ipif != NULL; 4006 ipif = ipif->ipif_next) { 4007 ipif->ipif_state_flags |= IPIF_CONDEMNED; 4008 } 4009 /* 4010 * Wake up anybody waiting to enter the ipsq. ipsq_enter 4011 * returns error if ILL_CONDEMNED is set 4012 */ 4013 cv_broadcast(&ill->ill_cv); 4014 mutex_exit(&ill->ill_lock); 4015 4016 /* 4017 * Send all the deferred DLPI messages downstream which came in 4018 * during the small window right before ipsq_enter(). We do this 4019 * without waiting for the ACKs because all the ACKs for M_PROTO 4020 * messages are ignored in ip_rput() when ILL_CONDEMNED is set. 4021 */ 4022 ill_dlpi_send_deferred(ill); 4023 4024 /* 4025 * Shut down fragmentation reassembly. 4026 * ill_frag_timer won't start a timer again. 4027 * Now cancel any existing timer 4028 */ 4029 (void) untimeout(ill->ill_frag_timer_id); 4030 (void) ill_frag_timeout(ill, 0); 4031 4032 /* 4033 * Call ill_delete to bring down the ipifs, ilms and ill on 4034 * this ill. Then wait for the refcnts to drop to zero. 4035 * ill_is_freeable checks whether the ill is really quiescent. 4036 * Then make sure that threads that are waiting to enter the 4037 * ipsq have seen the error returned by ipsq_enter and have 4038 * gone away. Then we call ill_delete_tail which does the 4039 * DL_UNBIND_REQ with the driver and then qprocsoff. 4040 */ 4041 ill_delete(ill); 4042 mutex_enter(&ill->ill_lock); 4043 while (!ill_is_freeable(ill)) 4044 cv_wait(&ill->ill_cv, &ill->ill_lock); 4045 4046 while (ill->ill_waiters) 4047 cv_wait(&ill->ill_cv, &ill->ill_lock); 4048 4049 mutex_exit(&ill->ill_lock); 4050 4051 /* 4052 * ill_delete_tail drops reference on ill_ipst, but we need to keep 4053 * it held until the end of the function since the cleanup 4054 * below needs to be able to use the ip_stack_t. 4055 */ 4056 netstack_hold(ipst->ips_netstack); 4057 4058 /* qprocsoff is done via ill_delete_tail */ 4059 ill_delete_tail(ill); 4060 /* 4061 * synchronously wait for arp stream to unbind. After this, we 4062 * cannot get any data packets up from the driver. 4063 */ 4064 arp_unbind_complete(ill); 4065 ASSERT(ill->ill_ipst == NULL); 4066 4067 /* 4068 * Walk through all conns and qenable those that have queued data. 4069 * Close synchronization needs this to 4070 * be done to ensure that all upper layers blocked 4071 * due to flow control to the closing device 4072 * get unblocked. 4073 */ 4074 ip1dbg(("ip_wsrv: walking\n")); 4075 for (i = 0; i < TX_FANOUT_SIZE; i++) { 4076 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]); 4077 } 4078 4079 /* 4080 * ai can be null if this is an IPv6 ill, or if the IPv4 4081 * stream is being torn down before ARP was plumbed (e.g., 4082 * /sbin/ifconfig plumbing a stream twice, and encountering 4083 * an error 4084 */ 4085 if (ai != NULL) { 4086 ASSERT(!ill->ill_isv6); 4087 mutex_enter(&ai->ai_lock); 4088 ai->ai_ill = NULL; 4089 if (ai->ai_arl == NULL) { 4090 mutex_destroy(&ai->ai_lock); 4091 kmem_free(ai, sizeof (*ai)); 4092 } else { 4093 cv_signal(&ai->ai_ill_unplumb_done); 4094 mutex_exit(&ai->ai_lock); 4095 } 4096 } 4097 4098 mutex_enter(&ipst->ips_ip_mi_lock); 4099 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill); 4100 mutex_exit(&ipst->ips_ip_mi_lock); 4101 4102 /* 4103 * credp could be null if the open didn't succeed and ip_modopen 4104 * itself calls ip_close. 4105 */ 4106 if (ill->ill_credp != NULL) 4107 crfree(ill->ill_credp); 4108 4109 mutex_destroy(&ill->ill_saved_ire_lock); 4110 mutex_destroy(&ill->ill_lock); 4111 rw_destroy(&ill->ill_mcast_lock); 4112 mutex_destroy(&ill->ill_mcast_serializer); 4113 list_destroy(&ill->ill_nce); 4114 4115 /* 4116 * Now we are done with the module close pieces that 4117 * need the netstack_t. 4118 */ 4119 netstack_rele(ipst->ips_netstack); 4120 4121 mi_close_free((IDP)ill); 4122 q->q_ptr = WR(q)->q_ptr = NULL; 4123 4124 ipsq_exit(ipsq); 4125 4126 return (0); 4127 } 4128 4129 /* 4130 * This is called as part of close() for IP, UDP, ICMP, and RTS 4131 * in order to quiesce the conn. 4132 */ 4133 void 4134 ip_quiesce_conn(conn_t *connp) 4135 { 4136 boolean_t drain_cleanup_reqd = B_FALSE; 4137 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 4138 boolean_t ilg_cleanup_reqd = B_FALSE; 4139 ip_stack_t *ipst; 4140 4141 ASSERT(!IPCL_IS_TCP(connp)); 4142 ipst = connp->conn_netstack->netstack_ip; 4143 4144 /* 4145 * Mark the conn as closing, and this conn must not be 4146 * inserted in future into any list. Eg. conn_drain_insert(), 4147 * won't insert this conn into the conn_drain_list. 4148 * 4149 * conn_idl, and conn_ilg cannot get set henceforth. 4150 */ 4151 mutex_enter(&connp->conn_lock); 4152 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); 4153 connp->conn_state_flags |= CONN_CLOSING; 4154 if (connp->conn_idl != NULL) 4155 drain_cleanup_reqd = B_TRUE; 4156 if (connp->conn_oper_pending_ill != NULL) 4157 conn_ioctl_cleanup_reqd = B_TRUE; 4158 if (connp->conn_dhcpinit_ill != NULL) { 4159 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0); 4160 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit); 4161 ill_set_inputfn(connp->conn_dhcpinit_ill); 4162 connp->conn_dhcpinit_ill = NULL; 4163 } 4164 if (connp->conn_ilg != NULL) 4165 ilg_cleanup_reqd = B_TRUE; 4166 mutex_exit(&connp->conn_lock); 4167 4168 if (conn_ioctl_cleanup_reqd) 4169 conn_ioctl_cleanup(connp); 4170 4171 if (is_system_labeled() && connp->conn_anon_port) { 4172 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 4173 connp->conn_mlp_type, connp->conn_proto, 4174 ntohs(connp->conn_lport), B_FALSE); 4175 connp->conn_anon_port = 0; 4176 } 4177 connp->conn_mlp_type = mlptSingle; 4178 4179 /* 4180 * Remove this conn from any fanout list it is on. 4181 * and then wait for any threads currently operating 4182 * on this endpoint to finish 4183 */ 4184 ipcl_hash_remove(connp); 4185 4186 /* 4187 * Remove this conn from the drain list, and do 4188 * any other cleanup that may be required. 4189 * (Only non-tcp conns may have a non-null conn_idl. 4190 * TCP conns are never flow controlled, and 4191 * conn_idl will be null) 4192 */ 4193 if (drain_cleanup_reqd && connp->conn_idl != NULL) { 4194 mutex_enter(&connp->conn_idl->idl_lock); 4195 conn_drain_tail(connp, B_TRUE); 4196 mutex_exit(&connp->conn_idl->idl_lock); 4197 } 4198 4199 if (connp == ipst->ips_ip_g_mrouter) 4200 (void) ip_mrouter_done(ipst); 4201 4202 if (ilg_cleanup_reqd) 4203 ilg_delete_all(connp); 4204 4205 /* 4206 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. 4207 * callers from write side can't be there now because close 4208 * is in progress. The only other caller is ipcl_walk 4209 * which checks for the condemned flag. 4210 */ 4211 mutex_enter(&connp->conn_lock); 4212 connp->conn_state_flags |= CONN_CONDEMNED; 4213 while (connp->conn_ref != 1) 4214 cv_wait(&connp->conn_cv, &connp->conn_lock); 4215 connp->conn_state_flags |= CONN_QUIESCED; 4216 mutex_exit(&connp->conn_lock); 4217 } 4218 4219 /* ARGSUSED */ 4220 int 4221 ip_close(queue_t *q, int flags) 4222 { 4223 conn_t *connp; 4224 4225 /* 4226 * Call the appropriate delete routine depending on whether this is 4227 * a module or device. 4228 */ 4229 if (WR(q)->q_next != NULL) { 4230 /* This is a module close */ 4231 return (ip_modclose((ill_t *)q->q_ptr)); 4232 } 4233 4234 connp = q->q_ptr; 4235 ip_quiesce_conn(connp); 4236 4237 qprocsoff(q); 4238 4239 /* 4240 * Now we are truly single threaded on this stream, and can 4241 * delete the things hanging off the connp, and finally the connp. 4242 * We removed this connp from the fanout list, it cannot be 4243 * accessed thru the fanouts, and we already waited for the 4244 * conn_ref to drop to 0. We are already in close, so 4245 * there cannot be any other thread from the top. qprocsoff 4246 * has completed, and service has completed or won't run in 4247 * future. 4248 */ 4249 ASSERT(connp->conn_ref == 1); 4250 4251 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 4252 4253 connp->conn_ref--; 4254 ipcl_conn_destroy(connp); 4255 4256 q->q_ptr = WR(q)->q_ptr = NULL; 4257 return (0); 4258 } 4259 4260 /* 4261 * Wapper around putnext() so that ip_rts_request can merely use 4262 * conn_recv. 4263 */ 4264 /*ARGSUSED2*/ 4265 static void 4266 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira) 4267 { 4268 conn_t *connp = (conn_t *)arg1; 4269 4270 putnext(connp->conn_rq, mp); 4271 } 4272 4273 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */ 4274 /* ARGSUSED */ 4275 static void 4276 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira) 4277 { 4278 freemsg(mp); 4279 } 4280 4281 /* 4282 * Called when the module is about to be unloaded 4283 */ 4284 void 4285 ip_ddi_destroy(void) 4286 { 4287 tnet_fini(); 4288 4289 icmp_ddi_g_destroy(); 4290 rts_ddi_g_destroy(); 4291 udp_ddi_g_destroy(); 4292 sctp_ddi_g_destroy(); 4293 tcp_ddi_g_destroy(); 4294 ilb_ddi_g_destroy(); 4295 dce_g_destroy(); 4296 ipsec_policy_g_destroy(); 4297 ipcl_g_destroy(); 4298 ip_net_g_destroy(); 4299 ip_ire_g_fini(); 4300 inet_minor_destroy(ip_minor_arena_sa); 4301 #if defined(_LP64) 4302 inet_minor_destroy(ip_minor_arena_la); 4303 #endif 4304 4305 #ifdef DEBUG 4306 list_destroy(&ip_thread_list); 4307 rw_destroy(&ip_thread_rwlock); 4308 tsd_destroy(&ip_thread_data); 4309 #endif 4310 4311 netstack_unregister(NS_IP); 4312 } 4313 4314 /* 4315 * First step in cleanup. 4316 */ 4317 /* ARGSUSED */ 4318 static void 4319 ip_stack_shutdown(netstackid_t stackid, void *arg) 4320 { 4321 ip_stack_t *ipst = (ip_stack_t *)arg; 4322 4323 #ifdef NS_DEBUG 4324 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid); 4325 #endif 4326 4327 /* 4328 * Perform cleanup for special interfaces (loopback and IPMP). 4329 */ 4330 ip_interface_cleanup(ipst); 4331 4332 /* 4333 * The *_hook_shutdown()s start the process of notifying any 4334 * consumers that things are going away.... nothing is destroyed. 4335 */ 4336 ipv4_hook_shutdown(ipst); 4337 ipv6_hook_shutdown(ipst); 4338 arp_hook_shutdown(ipst); 4339 4340 mutex_enter(&ipst->ips_capab_taskq_lock); 4341 ipst->ips_capab_taskq_quit = B_TRUE; 4342 cv_signal(&ipst->ips_capab_taskq_cv); 4343 mutex_exit(&ipst->ips_capab_taskq_lock); 4344 } 4345 4346 /* 4347 * Free the IP stack instance. 4348 */ 4349 static void 4350 ip_stack_fini(netstackid_t stackid, void *arg) 4351 { 4352 ip_stack_t *ipst = (ip_stack_t *)arg; 4353 int ret; 4354 4355 #ifdef NS_DEBUG 4356 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid); 4357 #endif 4358 /* 4359 * At this point, all of the notifications that the events and 4360 * protocols are going away have been run, meaning that we can 4361 * now set about starting to clean things up. 4362 */ 4363 ipobs_fini(ipst); 4364 ipv4_hook_destroy(ipst); 4365 ipv6_hook_destroy(ipst); 4366 arp_hook_destroy(ipst); 4367 ip_net_destroy(ipst); 4368 4369 mutex_destroy(&ipst->ips_capab_taskq_lock); 4370 cv_destroy(&ipst->ips_capab_taskq_cv); 4371 4372 ipmp_destroy(ipst); 4373 rw_destroy(&ipst->ips_srcid_lock); 4374 4375 ip_kstat_fini(stackid, ipst->ips_ip_mibkp); 4376 ipst->ips_ip_mibkp = NULL; 4377 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp); 4378 ipst->ips_icmp_mibkp = NULL; 4379 ip_kstat2_fini(stackid, ipst->ips_ip_kstat); 4380 ipst->ips_ip_kstat = NULL; 4381 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics)); 4382 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat); 4383 ipst->ips_ip6_kstat = NULL; 4384 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics)); 4385 4386 kmem_free(ipst->ips_propinfo_tbl, 4387 ip_propinfo_count * sizeof (mod_prop_info_t)); 4388 ipst->ips_propinfo_tbl = NULL; 4389 4390 dce_stack_destroy(ipst); 4391 ip_mrouter_stack_destroy(ipst); 4392 4393 mutex_destroy(&ipst->ips_ip_mi_lock); 4394 rw_destroy(&ipst->ips_ill_g_usesrc_lock); 4395 4396 ret = untimeout(ipst->ips_igmp_timeout_id); 4397 if (ret == -1) { 4398 ASSERT(ipst->ips_igmp_timeout_id == 0); 4399 } else { 4400 ASSERT(ipst->ips_igmp_timeout_id != 0); 4401 ipst->ips_igmp_timeout_id = 0; 4402 } 4403 ret = untimeout(ipst->ips_igmp_slowtimeout_id); 4404 if (ret == -1) { 4405 ASSERT(ipst->ips_igmp_slowtimeout_id == 0); 4406 } else { 4407 ASSERT(ipst->ips_igmp_slowtimeout_id != 0); 4408 ipst->ips_igmp_slowtimeout_id = 0; 4409 } 4410 ret = untimeout(ipst->ips_mld_timeout_id); 4411 if (ret == -1) { 4412 ASSERT(ipst->ips_mld_timeout_id == 0); 4413 } else { 4414 ASSERT(ipst->ips_mld_timeout_id != 0); 4415 ipst->ips_mld_timeout_id = 0; 4416 } 4417 ret = untimeout(ipst->ips_mld_slowtimeout_id); 4418 if (ret == -1) { 4419 ASSERT(ipst->ips_mld_slowtimeout_id == 0); 4420 } else { 4421 ASSERT(ipst->ips_mld_slowtimeout_id != 0); 4422 ipst->ips_mld_slowtimeout_id = 0; 4423 } 4424 4425 mutex_destroy(&ipst->ips_igmp_timer_lock); 4426 mutex_destroy(&ipst->ips_mld_timer_lock); 4427 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock); 4428 mutex_destroy(&ipst->ips_mld_slowtimeout_lock); 4429 mutex_destroy(&ipst->ips_ip_addr_avail_lock); 4430 rw_destroy(&ipst->ips_ill_g_lock); 4431 4432 ip_ire_fini(ipst); 4433 ip6_asp_free(ipst); 4434 conn_drain_fini(ipst); 4435 ipcl_destroy(ipst); 4436 4437 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock); 4438 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock); 4439 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t)); 4440 ipst->ips_ndp4 = NULL; 4441 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t)); 4442 ipst->ips_ndp6 = NULL; 4443 4444 if (ipst->ips_loopback_ksp != NULL) { 4445 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid); 4446 ipst->ips_loopback_ksp = NULL; 4447 } 4448 4449 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t)); 4450 ipst->ips_phyint_g_list = NULL; 4451 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS); 4452 ipst->ips_ill_g_heads = NULL; 4453 4454 ldi_ident_release(ipst->ips_ldi_ident); 4455 kmem_free(ipst, sizeof (*ipst)); 4456 } 4457 4458 /* 4459 * This function is called from the TSD destructor, and is used to debug 4460 * reference count issues in IP. See block comment in <inet/ip_if.h> for 4461 * details. 4462 */ 4463 static void 4464 ip_thread_exit(void *phash) 4465 { 4466 th_hash_t *thh = phash; 4467 4468 rw_enter(&ip_thread_rwlock, RW_WRITER); 4469 list_remove(&ip_thread_list, thh); 4470 rw_exit(&ip_thread_rwlock); 4471 mod_hash_destroy_hash(thh->thh_hash); 4472 kmem_free(thh, sizeof (*thh)); 4473 } 4474 4475 /* 4476 * Called when the IP kernel module is loaded into the kernel 4477 */ 4478 void 4479 ip_ddi_init(void) 4480 { 4481 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter); 4482 4483 /* 4484 * For IP and TCP the minor numbers should start from 2 since we have 4 4485 * initial devices: ip, ip6, tcp, tcp6. 4486 */ 4487 /* 4488 * If this is a 64-bit kernel, then create two separate arenas - 4489 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the 4490 * other for socket apps in the range 2^^18 through 2^^32-1. 4491 */ 4492 ip_minor_arena_la = NULL; 4493 ip_minor_arena_sa = NULL; 4494 #if defined(_LP64) 4495 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", 4496 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) { 4497 cmn_err(CE_PANIC, 4498 "ip_ddi_init: ip_minor_arena_sa creation failed\n"); 4499 } 4500 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la", 4501 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) { 4502 cmn_err(CE_PANIC, 4503 "ip_ddi_init: ip_minor_arena_la creation failed\n"); 4504 } 4505 #else 4506 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", 4507 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) { 4508 cmn_err(CE_PANIC, 4509 "ip_ddi_init: ip_minor_arena_sa creation failed\n"); 4510 } 4511 #endif 4512 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); 4513 4514 ipcl_g_init(); 4515 ip_ire_g_init(); 4516 ip_net_g_init(); 4517 4518 #ifdef DEBUG 4519 tsd_create(&ip_thread_data, ip_thread_exit); 4520 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL); 4521 list_create(&ip_thread_list, sizeof (th_hash_t), 4522 offsetof(th_hash_t, thh_link)); 4523 #endif 4524 ipsec_policy_g_init(); 4525 tcp_ddi_g_init(); 4526 sctp_ddi_g_init(); 4527 dce_g_init(); 4528 4529 /* 4530 * We want to be informed each time a stack is created or 4531 * destroyed in the kernel, so we can maintain the 4532 * set of udp_stack_t's. 4533 */ 4534 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown, 4535 ip_stack_fini); 4536 4537 tnet_init(); 4538 4539 udp_ddi_g_init(); 4540 rts_ddi_g_init(); 4541 icmp_ddi_g_init(); 4542 ilb_ddi_g_init(); 4543 } 4544 4545 /* 4546 * Initialize the IP stack instance. 4547 */ 4548 static void * 4549 ip_stack_init(netstackid_t stackid, netstack_t *ns) 4550 { 4551 ip_stack_t *ipst; 4552 size_t arrsz; 4553 major_t major; 4554 4555 #ifdef NS_DEBUG 4556 printf("ip_stack_init(stack %d)\n", stackid); 4557 #endif 4558 4559 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP); 4560 ipst->ips_netstack = ns; 4561 4562 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS, 4563 KM_SLEEP); 4564 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t), 4565 KM_SLEEP); 4566 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 4567 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); 4568 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 4569 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); 4570 4571 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); 4572 ipst->ips_igmp_deferred_next = INFINITY; 4573 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); 4574 ipst->ips_mld_deferred_next = INFINITY; 4575 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 4576 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); 4577 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); 4578 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); 4579 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL); 4580 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); 4581 4582 ipcl_init(ipst); 4583 ip_ire_init(ipst); 4584 ip6_asp_init(ipst); 4585 ipif_init(ipst); 4586 conn_drain_init(ipst); 4587 ip_mrouter_stack_init(ipst); 4588 dce_stack_init(ipst); 4589 4590 ipst->ips_ip_multirt_log_interval = 1000; 4591 4592 ipst->ips_ill_index = 1; 4593 4594 ipst->ips_saved_ip_forwarding = -1; 4595 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */ 4596 4597 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t); 4598 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP); 4599 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz); 4600 4601 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst); 4602 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid); 4603 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics); 4604 ipst->ips_ip6_kstat = 4605 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics); 4606 4607 ipst->ips_ip_src_id = 1; 4608 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL); 4609 4610 ipst->ips_src_generation = SRC_GENERATION_INITIAL; 4611 4612 ip_net_init(ipst, ns); 4613 ipv4_hook_init(ipst); 4614 ipv6_hook_init(ipst); 4615 arp_hook_init(ipst); 4616 ipmp_init(ipst); 4617 ipobs_init(ipst); 4618 4619 /* 4620 * Create the taskq dispatcher thread and initialize related stuff. 4621 */ 4622 ipst->ips_capab_taskq_thread = thread_create(NULL, 0, 4623 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri); 4624 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL); 4625 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL); 4626 4627 major = mod_name_to_major(INET_NAME); 4628 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident); 4629 return (ipst); 4630 } 4631 4632 /* 4633 * Allocate and initialize a DLPI template of the specified length. (May be 4634 * called as writer.) 4635 */ 4636 mblk_t * 4637 ip_dlpi_alloc(size_t len, t_uscalar_t prim) 4638 { 4639 mblk_t *mp; 4640 4641 mp = allocb(len, BPRI_MED); 4642 if (!mp) 4643 return (NULL); 4644 4645 /* 4646 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter 4647 * of which we don't seem to use) are sent with M_PCPROTO, and 4648 * that other DLPI are M_PROTO. 4649 */ 4650 if (prim == DL_INFO_REQ) { 4651 mp->b_datap->db_type = M_PCPROTO; 4652 } else { 4653 mp->b_datap->db_type = M_PROTO; 4654 } 4655 4656 mp->b_wptr = mp->b_rptr + len; 4657 bzero(mp->b_rptr, len); 4658 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; 4659 return (mp); 4660 } 4661 4662 /* 4663 * Allocate and initialize a DLPI notification. (May be called as writer.) 4664 */ 4665 mblk_t * 4666 ip_dlnotify_alloc(uint_t notification, uint_t data) 4667 { 4668 dl_notify_ind_t *notifyp; 4669 mblk_t *mp; 4670 4671 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL) 4672 return (NULL); 4673 4674 notifyp = (dl_notify_ind_t *)mp->b_rptr; 4675 notifyp->dl_notification = notification; 4676 notifyp->dl_data = data; 4677 return (mp); 4678 } 4679 4680 /* 4681 * Debug formatting routine. Returns a character string representation of the 4682 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address 4683 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. 4684 * 4685 * Once the ndd table-printing interfaces are removed, this can be changed to 4686 * standard dotted-decimal form. 4687 */ 4688 char * 4689 ip_dot_addr(ipaddr_t addr, char *buf) 4690 { 4691 uint8_t *ap = (uint8_t *)&addr; 4692 4693 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", 4694 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); 4695 return (buf); 4696 } 4697 4698 /* 4699 * Write the given MAC address as a printable string in the usual colon- 4700 * separated format. 4701 */ 4702 const char * 4703 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) 4704 { 4705 char *bp; 4706 4707 if (alen == 0 || buflen < 4) 4708 return ("?"); 4709 bp = buf; 4710 for (;;) { 4711 /* 4712 * If there are more MAC address bytes available, but we won't 4713 * have any room to print them, then add "..." to the string 4714 * instead. See below for the 'magic number' explanation. 4715 */ 4716 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { 4717 (void) strcpy(bp, "..."); 4718 break; 4719 } 4720 (void) sprintf(bp, "%02x", *addr++); 4721 bp += 2; 4722 if (--alen == 0) 4723 break; 4724 *bp++ = ':'; 4725 buflen -= 3; 4726 /* 4727 * At this point, based on the first 'if' statement above, 4728 * either alen == 1 and buflen >= 3, or alen > 1 and 4729 * buflen >= 4. The first case leaves room for the final "xx" 4730 * number and trailing NUL byte. The second leaves room for at 4731 * least "...". Thus the apparently 'magic' numbers chosen for 4732 * that statement. 4733 */ 4734 } 4735 return (buf); 4736 } 4737 4738 /* 4739 * Called when it is conceptually a ULP that would sent the packet 4740 * e.g., port unreachable and protocol unreachable. Check that the packet 4741 * would have passed the IPsec global policy before sending the error. 4742 * 4743 * Send an ICMP error after patching up the packet appropriately. 4744 * Uses ip_drop_input and bumps the appropriate MIB. 4745 */ 4746 void 4747 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code, 4748 ip_recv_attr_t *ira) 4749 { 4750 ipha_t *ipha; 4751 boolean_t secure; 4752 ill_t *ill = ira->ira_ill; 4753 ip_stack_t *ipst = ill->ill_ipst; 4754 netstack_t *ns = ipst->ips_netstack; 4755 ipsec_stack_t *ipss = ns->netstack_ipsec; 4756 4757 secure = ira->ira_flags & IRAF_IPSEC_SECURE; 4758 4759 /* 4760 * We are generating an icmp error for some inbound packet. 4761 * Called from all ip_fanout_(udp, tcp, proto) functions. 4762 * Before we generate an error, check with global policy 4763 * to see whether this is allowed to enter the system. As 4764 * there is no "conn", we are checking with global policy. 4765 */ 4766 ipha = (ipha_t *)mp->b_rptr; 4767 if (secure || ipss->ipsec_inbound_v4_policy_present) { 4768 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns); 4769 if (mp == NULL) 4770 return; 4771 } 4772 4773 /* We never send errors for protocols that we do implement */ 4774 if (ira->ira_protocol == IPPROTO_ICMP || 4775 ira->ira_protocol == IPPROTO_IGMP) { 4776 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 4777 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill); 4778 freemsg(mp); 4779 return; 4780 } 4781 /* 4782 * Have to correct checksum since 4783 * the packet might have been 4784 * fragmented and the reassembly code in ip_rput 4785 * does not restore the IP checksum. 4786 */ 4787 ipha->ipha_hdr_checksum = 0; 4788 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 4789 4790 switch (icmp_type) { 4791 case ICMP_DEST_UNREACHABLE: 4792 switch (icmp_code) { 4793 case ICMP_PROTOCOL_UNREACHABLE: 4794 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos); 4795 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill); 4796 break; 4797 case ICMP_PORT_UNREACHABLE: 4798 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 4799 ip_drop_input("ipIfStatsNoPorts", mp, ill); 4800 break; 4801 } 4802 4803 icmp_unreachable(mp, icmp_code, ira); 4804 break; 4805 default: 4806 #ifdef DEBUG 4807 panic("ip_fanout_send_icmp_v4: wrong type"); 4808 /*NOTREACHED*/ 4809 #else 4810 freemsg(mp); 4811 break; 4812 #endif 4813 } 4814 } 4815 4816 /* 4817 * Used to send an ICMP error message when a packet is received for 4818 * a protocol that is not supported. The mblk passed as argument 4819 * is consumed by this function. 4820 */ 4821 void 4822 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira) 4823 { 4824 ipha_t *ipha; 4825 4826 ipha = (ipha_t *)mp->b_rptr; 4827 if (ira->ira_flags & IRAF_IS_IPV4) { 4828 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION); 4829 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE, 4830 ICMP_PROTOCOL_UNREACHABLE, ira); 4831 } else { 4832 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION); 4833 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB, 4834 ICMP6_PARAMPROB_NEXTHEADER, ira); 4835 } 4836 } 4837 4838 /* 4839 * Deliver a rawip packet to the given conn, possibly applying ipsec policy. 4840 * Handles IPv4 and IPv6. 4841 * We are responsible for disposing of mp, such as by freemsg() or putnext() 4842 * Caller is responsible for dropping references to the conn. 4843 */ 4844 void 4845 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, 4846 ip_recv_attr_t *ira) 4847 { 4848 ill_t *ill = ira->ira_ill; 4849 ip_stack_t *ipst = ill->ill_ipst; 4850 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 4851 boolean_t secure; 4852 uint_t protocol = ira->ira_protocol; 4853 iaflags_t iraflags = ira->ira_flags; 4854 queue_t *rq; 4855 4856 secure = iraflags & IRAF_IPSEC_SECURE; 4857 4858 rq = connp->conn_rq; 4859 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) { 4860 switch (protocol) { 4861 case IPPROTO_ICMPV6: 4862 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows); 4863 break; 4864 case IPPROTO_ICMP: 4865 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); 4866 break; 4867 default: 4868 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows); 4869 break; 4870 } 4871 freemsg(mp); 4872 return; 4873 } 4874 4875 ASSERT(!(IPCL_IS_IPTUN(connp))); 4876 4877 if (((iraflags & IRAF_IS_IPV4) ? 4878 CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 4879 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || 4880 secure) { 4881 mp = ipsec_check_inbound_policy(mp, connp, ipha, 4882 ip6h, ira); 4883 if (mp == NULL) { 4884 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 4885 /* Note that mp is NULL */ 4886 ip_drop_input("ipIfStatsInDiscards", mp, ill); 4887 return; 4888 } 4889 } 4890 4891 if (iraflags & IRAF_ICMP_ERROR) { 4892 (connp->conn_recvicmp)(connp, mp, NULL, ira); 4893 } else { 4894 ill_t *rill = ira->ira_rill; 4895 4896 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 4897 ira->ira_ill = ira->ira_rill = NULL; 4898 /* Send it upstream */ 4899 (connp->conn_recv)(connp, mp, NULL, ira); 4900 ira->ira_ill = ill; 4901 ira->ira_rill = rill; 4902 } 4903 } 4904 4905 /* 4906 * Handle protocols with which IP is less intimate. There 4907 * can be more than one stream bound to a particular 4908 * protocol. When this is the case, normally each one gets a copy 4909 * of any incoming packets. 4910 * 4911 * IPsec NOTE : 4912 * 4913 * Don't allow a secure packet going up a non-secure connection. 4914 * We don't allow this because 4915 * 4916 * 1) Reply might go out in clear which will be dropped at 4917 * the sending side. 4918 * 2) If the reply goes out in clear it will give the 4919 * adversary enough information for getting the key in 4920 * most of the cases. 4921 * 4922 * Moreover getting a secure packet when we expect clear 4923 * implies that SA's were added without checking for 4924 * policy on both ends. This should not happen once ISAKMP 4925 * is used to negotiate SAs as SAs will be added only after 4926 * verifying the policy. 4927 * 4928 * Zones notes: 4929 * Earlier in ip_input on a system with multiple shared-IP zones we 4930 * duplicate the multicast and broadcast packets and send them up 4931 * with each explicit zoneid that exists on that ill. 4932 * This means that here we can match the zoneid with SO_ALLZONES being special. 4933 */ 4934 void 4935 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira) 4936 { 4937 mblk_t *mp1; 4938 ipaddr_t laddr; 4939 conn_t *connp, *first_connp, *next_connp; 4940 connf_t *connfp; 4941 ill_t *ill = ira->ira_ill; 4942 ip_stack_t *ipst = ill->ill_ipst; 4943 4944 laddr = ipha->ipha_dst; 4945 4946 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol]; 4947 mutex_enter(&connfp->connf_lock); 4948 connp = connfp->connf_head; 4949 for (connp = connfp->connf_head; connp != NULL; 4950 connp = connp->conn_next) { 4951 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */ 4952 if (IPCL_PROTO_MATCH(connp, ira, ipha) && 4953 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) || 4954 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) { 4955 break; 4956 } 4957 } 4958 4959 if (connp == NULL) { 4960 /* 4961 * No one bound to these addresses. Is 4962 * there a client that wants all 4963 * unclaimed datagrams? 4964 */ 4965 mutex_exit(&connfp->connf_lock); 4966 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE, 4967 ICMP_PROTOCOL_UNREACHABLE, ira); 4968 return; 4969 } 4970 4971 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL); 4972 4973 CONN_INC_REF(connp); 4974 first_connp = connp; 4975 connp = connp->conn_next; 4976 4977 for (;;) { 4978 while (connp != NULL) { 4979 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */ 4980 if (IPCL_PROTO_MATCH(connp, ira, ipha) && 4981 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) || 4982 tsol_receive_local(mp, &laddr, IPV4_VERSION, 4983 ira, connp))) 4984 break; 4985 connp = connp->conn_next; 4986 } 4987 4988 if (connp == NULL) { 4989 /* No more interested clients */ 4990 connp = first_connp; 4991 break; 4992 } 4993 if (((mp1 = dupmsg(mp)) == NULL) && 4994 ((mp1 = copymsg(mp)) == NULL)) { 4995 /* Memory allocation failed */ 4996 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 4997 ip_drop_input("ipIfStatsInDiscards", mp, ill); 4998 connp = first_connp; 4999 break; 5000 } 5001 5002 CONN_INC_REF(connp); 5003 mutex_exit(&connfp->connf_lock); 5004 5005 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL, 5006 ira); 5007 5008 mutex_enter(&connfp->connf_lock); 5009 /* Follow the next pointer before releasing the conn. */ 5010 next_connp = connp->conn_next; 5011 CONN_DEC_REF(connp); 5012 connp = next_connp; 5013 } 5014 5015 /* Last one. Send it upstream. */ 5016 mutex_exit(&connfp->connf_lock); 5017 5018 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira); 5019 5020 CONN_DEC_REF(connp); 5021 } 5022 5023 /* 5024 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or 5025 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk 5026 * is not consumed. 5027 * 5028 * One of three things can happen, all of which affect the passed-in mblk: 5029 * 5030 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk.. 5031 * 5032 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent 5033 * ESP packet, and is passed along to ESP for consumption. Return NULL. 5034 * 5035 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL. 5036 */ 5037 mblk_t * 5038 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira) 5039 { 5040 int shift, plen, iph_len; 5041 ipha_t *ipha; 5042 udpha_t *udpha; 5043 uint32_t *spi; 5044 uint32_t esp_ports; 5045 uint8_t *orptr; 5046 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 5047 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 5048 5049 ipha = (ipha_t *)mp->b_rptr; 5050 iph_len = ira->ira_ip_hdr_length; 5051 plen = ira->ira_pktlen; 5052 5053 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) { 5054 /* 5055 * Most likely a keepalive for the benefit of an intervening 5056 * NAT. These aren't for us, per se, so drop it. 5057 * 5058 * RFC 3947/8 doesn't say for sure what to do for 2-3 5059 * byte packets (keepalives are 1-byte), but we'll drop them 5060 * also. 5061 */ 5062 ip_drop_packet(mp, B_TRUE, ira->ira_ill, 5063 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper); 5064 return (NULL); 5065 } 5066 5067 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) { 5068 /* might as well pull it all up - it might be ESP. */ 5069 if (!pullupmsg(mp, -1)) { 5070 ip_drop_packet(mp, B_TRUE, ira->ira_ill, 5071 DROPPER(ipss, ipds_esp_nomem), 5072 &ipss->ipsec_dropper); 5073 return (NULL); 5074 } 5075 5076 ipha = (ipha_t *)mp->b_rptr; 5077 } 5078 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t)); 5079 if (*spi == 0) { 5080 /* UDP packet - remove 0-spi. */ 5081 shift = sizeof (uint32_t); 5082 } else { 5083 /* ESP-in-UDP packet - reduce to ESP. */ 5084 ipha->ipha_protocol = IPPROTO_ESP; 5085 shift = sizeof (udpha_t); 5086 } 5087 5088 /* Fix IP header */ 5089 ira->ira_pktlen = (plen - shift); 5090 ipha->ipha_length = htons(ira->ira_pktlen); 5091 ipha->ipha_hdr_checksum = 0; 5092 5093 orptr = mp->b_rptr; 5094 mp->b_rptr += shift; 5095 5096 udpha = (udpha_t *)(orptr + iph_len); 5097 if (*spi == 0) { 5098 ASSERT((uint8_t *)ipha == orptr); 5099 udpha->uha_length = htons(plen - shift - iph_len); 5100 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */ 5101 esp_ports = 0; 5102 } else { 5103 esp_ports = *((uint32_t *)udpha); 5104 ASSERT(esp_ports != 0); 5105 } 5106 ovbcopy(orptr, orptr + shift, iph_len); 5107 if (esp_ports != 0) /* Punt up for ESP processing. */ { 5108 ipha = (ipha_t *)(orptr + shift); 5109 5110 ira->ira_flags |= IRAF_ESP_UDP_PORTS; 5111 ira->ira_esp_udp_ports = esp_ports; 5112 ip_fanout_v4(mp, ipha, ira); 5113 return (NULL); 5114 } 5115 return (mp); 5116 } 5117 5118 /* 5119 * Deliver a udp packet to the given conn, possibly applying ipsec policy. 5120 * Handles IPv4 and IPv6. 5121 * We are responsible for disposing of mp, such as by freemsg() or putnext() 5122 * Caller is responsible for dropping references to the conn. 5123 */ 5124 void 5125 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, 5126 ip_recv_attr_t *ira) 5127 { 5128 ill_t *ill = ira->ira_ill; 5129 ip_stack_t *ipst = ill->ill_ipst; 5130 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 5131 boolean_t secure; 5132 iaflags_t iraflags = ira->ira_flags; 5133 5134 secure = iraflags & IRAF_IPSEC_SECURE; 5135 5136 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : 5137 !canputnext(connp->conn_rq)) { 5138 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); 5139 freemsg(mp); 5140 return; 5141 } 5142 5143 if (((iraflags & IRAF_IS_IPV4) ? 5144 CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 5145 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || 5146 secure) { 5147 mp = ipsec_check_inbound_policy(mp, connp, ipha, 5148 ip6h, ira); 5149 if (mp == NULL) { 5150 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 5151 /* Note that mp is NULL */ 5152 ip_drop_input("ipIfStatsInDiscards", mp, ill); 5153 return; 5154 } 5155 } 5156 5157 /* 5158 * Since this code is not used for UDP unicast we don't need a NAT_T 5159 * check. Only ip_fanout_v4 has that check. 5160 */ 5161 if (ira->ira_flags & IRAF_ICMP_ERROR) { 5162 (connp->conn_recvicmp)(connp, mp, NULL, ira); 5163 } else { 5164 ill_t *rill = ira->ira_rill; 5165 5166 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 5167 ira->ira_ill = ira->ira_rill = NULL; 5168 /* Send it upstream */ 5169 (connp->conn_recv)(connp, mp, NULL, ira); 5170 ira->ira_ill = ill; 5171 ira->ira_rill = rill; 5172 } 5173 } 5174 5175 /* 5176 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors. 5177 * (Unicast fanout is handled in ip_input_v4.) 5178 * 5179 * If SO_REUSEADDR is set all multicast and broadcast packets 5180 * will be delivered to all conns bound to the same port. 5181 * 5182 * If there is at least one matching AF_INET receiver, then we will 5183 * ignore any AF_INET6 receivers. 5184 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an 5185 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4 5186 * packets. 5187 * 5188 * Zones notes: 5189 * Earlier in ip_input on a system with multiple shared-IP zones we 5190 * duplicate the multicast and broadcast packets and send them up 5191 * with each explicit zoneid that exists on that ill. 5192 * This means that here we can match the zoneid with SO_ALLZONES being special. 5193 */ 5194 void 5195 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport, 5196 ip_recv_attr_t *ira) 5197 { 5198 ipaddr_t laddr; 5199 in6_addr_t v6faddr; 5200 conn_t *connp; 5201 connf_t *connfp; 5202 ipaddr_t faddr; 5203 ill_t *ill = ira->ira_ill; 5204 ip_stack_t *ipst = ill->ill_ipst; 5205 5206 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR)); 5207 5208 laddr = ipha->ipha_dst; 5209 faddr = ipha->ipha_src; 5210 5211 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)]; 5212 mutex_enter(&connfp->connf_lock); 5213 connp = connfp->connf_head; 5214 5215 /* 5216 * If SO_REUSEADDR has been set on the first we send the 5217 * packet to all clients that have joined the group and 5218 * match the port. 5219 */ 5220 while (connp != NULL) { 5221 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) && 5222 conn_wantpacket(connp, ira, ipha) && 5223 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) || 5224 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) 5225 break; 5226 connp = connp->conn_next; 5227 } 5228 5229 if (connp == NULL) 5230 goto notfound; 5231 5232 CONN_INC_REF(connp); 5233 5234 if (connp->conn_reuseaddr) { 5235 conn_t *first_connp = connp; 5236 conn_t *next_connp; 5237 mblk_t *mp1; 5238 5239 connp = connp->conn_next; 5240 for (;;) { 5241 while (connp != NULL) { 5242 if (IPCL_UDP_MATCH(connp, lport, laddr, 5243 fport, faddr) && 5244 conn_wantpacket(connp, ira, ipha) && 5245 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) || 5246 tsol_receive_local(mp, &laddr, IPV4_VERSION, 5247 ira, connp))) 5248 break; 5249 connp = connp->conn_next; 5250 } 5251 if (connp == NULL) { 5252 /* No more interested clients */ 5253 connp = first_connp; 5254 break; 5255 } 5256 if (((mp1 = dupmsg(mp)) == NULL) && 5257 ((mp1 = copymsg(mp)) == NULL)) { 5258 /* Memory allocation failed */ 5259 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 5260 ip_drop_input("ipIfStatsInDiscards", mp, ill); 5261 connp = first_connp; 5262 break; 5263 } 5264 CONN_INC_REF(connp); 5265 mutex_exit(&connfp->connf_lock); 5266 5267 IP_STAT(ipst, ip_udp_fanmb); 5268 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr, 5269 NULL, ira); 5270 mutex_enter(&connfp->connf_lock); 5271 /* Follow the next pointer before releasing the conn */ 5272 next_connp = connp->conn_next; 5273 CONN_DEC_REF(connp); 5274 connp = next_connp; 5275 } 5276 } 5277 5278 /* Last one. Send it upstream. */ 5279 mutex_exit(&connfp->connf_lock); 5280 IP_STAT(ipst, ip_udp_fanmb); 5281 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira); 5282 CONN_DEC_REF(connp); 5283 return; 5284 5285 notfound: 5286 mutex_exit(&connfp->connf_lock); 5287 /* 5288 * IPv6 endpoints bound to multicast IPv4-mapped addresses 5289 * have already been matched above, since they live in the IPv4 5290 * fanout tables. This implies we only need to 5291 * check for IPv6 in6addr_any endpoints here. 5292 * Thus we compare using ipv6_all_zeros instead of the destination 5293 * address, except for the multicast group membership lookup which 5294 * uses the IPv4 destination. 5295 */ 5296 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr); 5297 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)]; 5298 mutex_enter(&connfp->connf_lock); 5299 connp = connfp->connf_head; 5300 /* 5301 * IPv4 multicast packet being delivered to an AF_INET6 5302 * in6addr_any endpoint. 5303 * Need to check conn_wantpacket(). Note that we use conn_wantpacket() 5304 * and not conn_wantpacket_v6() since any multicast membership is 5305 * for an IPv4-mapped multicast address. 5306 */ 5307 while (connp != NULL) { 5308 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros, 5309 fport, v6faddr) && 5310 conn_wantpacket(connp, ira, ipha) && 5311 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) || 5312 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) 5313 break; 5314 connp = connp->conn_next; 5315 } 5316 5317 if (connp == NULL) { 5318 /* 5319 * No one bound to this port. Is 5320 * there a client that wants all 5321 * unclaimed datagrams? 5322 */ 5323 mutex_exit(&connfp->connf_lock); 5324 5325 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head != 5326 NULL) { 5327 ASSERT(ira->ira_protocol == IPPROTO_UDP); 5328 ip_fanout_proto_v4(mp, ipha, ira); 5329 } else { 5330 /* 5331 * We used to attempt to send an icmp error here, but 5332 * since this is known to be a multicast packet 5333 * and we don't send icmp errors in response to 5334 * multicast, just drop the packet and give up sooner. 5335 */ 5336 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); 5337 freemsg(mp); 5338 } 5339 return; 5340 } 5341 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL); 5342 5343 /* 5344 * If SO_REUSEADDR has been set on the first we send the 5345 * packet to all clients that have joined the group and 5346 * match the port. 5347 */ 5348 if (connp->conn_reuseaddr) { 5349 conn_t *first_connp = connp; 5350 conn_t *next_connp; 5351 mblk_t *mp1; 5352 5353 CONN_INC_REF(connp); 5354 connp = connp->conn_next; 5355 for (;;) { 5356 while (connp != NULL) { 5357 if (IPCL_UDP_MATCH_V6(connp, lport, 5358 ipv6_all_zeros, fport, v6faddr) && 5359 conn_wantpacket(connp, ira, ipha) && 5360 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) || 5361 tsol_receive_local(mp, &laddr, IPV4_VERSION, 5362 ira, connp))) 5363 break; 5364 connp = connp->conn_next; 5365 } 5366 if (connp == NULL) { 5367 /* No more interested clients */ 5368 connp = first_connp; 5369 break; 5370 } 5371 if (((mp1 = dupmsg(mp)) == NULL) && 5372 ((mp1 = copymsg(mp)) == NULL)) { 5373 /* Memory allocation failed */ 5374 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 5375 ip_drop_input("ipIfStatsInDiscards", mp, ill); 5376 connp = first_connp; 5377 break; 5378 } 5379 CONN_INC_REF(connp); 5380 mutex_exit(&connfp->connf_lock); 5381 5382 IP_STAT(ipst, ip_udp_fanmb); 5383 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr, 5384 NULL, ira); 5385 mutex_enter(&connfp->connf_lock); 5386 /* Follow the next pointer before releasing the conn */ 5387 next_connp = connp->conn_next; 5388 CONN_DEC_REF(connp); 5389 connp = next_connp; 5390 } 5391 } 5392 5393 /* Last one. Send it upstream. */ 5394 mutex_exit(&connfp->connf_lock); 5395 IP_STAT(ipst, ip_udp_fanmb); 5396 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira); 5397 CONN_DEC_REF(connp); 5398 } 5399 5400 /* 5401 * Split an incoming packet's IPv4 options into the label and the other options. 5402 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including 5403 * clearing out any leftover label or options. 5404 * Otherwise it just makes ipp point into the packet. 5405 * 5406 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated. 5407 */ 5408 int 5409 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate) 5410 { 5411 uchar_t *opt; 5412 uint32_t totallen; 5413 uint32_t optval; 5414 uint32_t optlen; 5415 5416 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR; 5417 ipp->ipp_hoplimit = ipha->ipha_ttl; 5418 ipp->ipp_type_of_service = ipha->ipha_type_of_service; 5419 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr); 5420 5421 /* 5422 * Get length (in 4 byte octets) of IP header options. 5423 */ 5424 totallen = ipha->ipha_version_and_hdr_length - 5425 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 5426 5427 if (totallen == 0) { 5428 if (!allocate) 5429 return (0); 5430 5431 /* Clear out anything from a previous packet */ 5432 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) { 5433 kmem_free(ipp->ipp_ipv4_options, 5434 ipp->ipp_ipv4_options_len); 5435 ipp->ipp_ipv4_options = NULL; 5436 ipp->ipp_ipv4_options_len = 0; 5437 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS; 5438 } 5439 if (ipp->ipp_fields & IPPF_LABEL_V4) { 5440 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4); 5441 ipp->ipp_label_v4 = NULL; 5442 ipp->ipp_label_len_v4 = 0; 5443 ipp->ipp_fields &= ~IPPF_LABEL_V4; 5444 } 5445 return (0); 5446 } 5447 5448 totallen <<= 2; 5449 opt = (uchar_t *)&ipha[1]; 5450 if (!is_system_labeled()) { 5451 5452 copyall: 5453 if (!allocate) { 5454 if (totallen != 0) { 5455 ipp->ipp_ipv4_options = opt; 5456 ipp->ipp_ipv4_options_len = totallen; 5457 ipp->ipp_fields |= IPPF_IPV4_OPTIONS; 5458 } 5459 return (0); 5460 } 5461 /* Just copy all of options */ 5462 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) { 5463 if (totallen == ipp->ipp_ipv4_options_len) { 5464 bcopy(opt, ipp->ipp_ipv4_options, totallen); 5465 return (0); 5466 } 5467 kmem_free(ipp->ipp_ipv4_options, 5468 ipp->ipp_ipv4_options_len); 5469 ipp->ipp_ipv4_options = NULL; 5470 ipp->ipp_ipv4_options_len = 0; 5471 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS; 5472 } 5473 if (totallen == 0) 5474 return (0); 5475 5476 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP); 5477 if (ipp->ipp_ipv4_options == NULL) 5478 return (ENOMEM); 5479 ipp->ipp_ipv4_options_len = totallen; 5480 ipp->ipp_fields |= IPPF_IPV4_OPTIONS; 5481 bcopy(opt, ipp->ipp_ipv4_options, totallen); 5482 return (0); 5483 } 5484 5485 if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) { 5486 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4); 5487 ipp->ipp_label_v4 = NULL; 5488 ipp->ipp_label_len_v4 = 0; 5489 ipp->ipp_fields &= ~IPPF_LABEL_V4; 5490 } 5491 5492 /* 5493 * Search for CIPSO option. 5494 * We assume CIPSO is first in options if it is present. 5495 * If it isn't, then ipp_opt_ipv4_options will not include the options 5496 * prior to the CIPSO option. 5497 */ 5498 while (totallen != 0) { 5499 switch (optval = opt[IPOPT_OPTVAL]) { 5500 case IPOPT_EOL: 5501 return (0); 5502 case IPOPT_NOP: 5503 optlen = 1; 5504 break; 5505 default: 5506 if (totallen <= IPOPT_OLEN) 5507 return (EINVAL); 5508 optlen = opt[IPOPT_OLEN]; 5509 if (optlen < 2) 5510 return (EINVAL); 5511 } 5512 if (optlen > totallen) 5513 return (EINVAL); 5514 5515 switch (optval) { 5516 case IPOPT_COMSEC: 5517 if (!allocate) { 5518 ipp->ipp_label_v4 = opt; 5519 ipp->ipp_label_len_v4 = optlen; 5520 ipp->ipp_fields |= IPPF_LABEL_V4; 5521 } else { 5522 ipp->ipp_label_v4 = kmem_alloc(optlen, 5523 KM_NOSLEEP); 5524 if (ipp->ipp_label_v4 == NULL) 5525 return (ENOMEM); 5526 ipp->ipp_label_len_v4 = optlen; 5527 ipp->ipp_fields |= IPPF_LABEL_V4; 5528 bcopy(opt, ipp->ipp_label_v4, optlen); 5529 } 5530 totallen -= optlen; 5531 opt += optlen; 5532 5533 /* Skip padding bytes until we get to a multiple of 4 */ 5534 while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) { 5535 totallen--; 5536 opt++; 5537 } 5538 /* Remaining as ipp_ipv4_options */ 5539 goto copyall; 5540 } 5541 totallen -= optlen; 5542 opt += optlen; 5543 } 5544 /* No CIPSO found; return everything as ipp_ipv4_options */ 5545 totallen = ipha->ipha_version_and_hdr_length - 5546 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); 5547 totallen <<= 2; 5548 opt = (uchar_t *)&ipha[1]; 5549 goto copyall; 5550 } 5551 5552 /* 5553 * Efficient versions of lookup for an IRE when we only 5554 * match the address. 5555 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE. 5556 * Does not handle multicast addresses. 5557 */ 5558 uint_t 5559 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst) 5560 { 5561 ire_t *ire; 5562 uint_t result; 5563 5564 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL); 5565 ASSERT(ire != NULL); 5566 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) 5567 result = IRE_NOROUTE; 5568 else 5569 result = ire->ire_type; 5570 ire_refrele(ire); 5571 return (result); 5572 } 5573 5574 /* 5575 * Efficient versions of lookup for an IRE when we only 5576 * match the address. 5577 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE. 5578 * Does not handle multicast addresses. 5579 */ 5580 uint_t 5581 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst) 5582 { 5583 ire_t *ire; 5584 uint_t result; 5585 5586 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL); 5587 ASSERT(ire != NULL); 5588 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) 5589 result = IRE_NOROUTE; 5590 else 5591 result = ire->ire_type; 5592 ire_refrele(ire); 5593 return (result); 5594 } 5595 5596 /* 5597 * Nobody should be sending 5598 * packets up this stream 5599 */ 5600 static void 5601 ip_lrput(queue_t *q, mblk_t *mp) 5602 { 5603 switch (mp->b_datap->db_type) { 5604 case M_FLUSH: 5605 /* Turn around */ 5606 if (*mp->b_rptr & FLUSHW) { 5607 *mp->b_rptr &= ~FLUSHR; 5608 qreply(q, mp); 5609 return; 5610 } 5611 break; 5612 } 5613 freemsg(mp); 5614 } 5615 5616 /* Nobody should be sending packets down this stream */ 5617 /* ARGSUSED */ 5618 void 5619 ip_lwput(queue_t *q, mblk_t *mp) 5620 { 5621 freemsg(mp); 5622 } 5623 5624 /* 5625 * Move the first hop in any source route to ipha_dst and remove that part of 5626 * the source route. Called by other protocols. Errors in option formatting 5627 * are ignored - will be handled by ip_output_options. Return the final 5628 * destination (either ipha_dst or the last entry in a source route.) 5629 */ 5630 ipaddr_t 5631 ip_massage_options(ipha_t *ipha, netstack_t *ns) 5632 { 5633 ipoptp_t opts; 5634 uchar_t *opt; 5635 uint8_t optval; 5636 uint8_t optlen; 5637 ipaddr_t dst; 5638 int i; 5639 ip_stack_t *ipst = ns->netstack_ip; 5640 5641 ip2dbg(("ip_massage_options\n")); 5642 dst = ipha->ipha_dst; 5643 for (optval = ipoptp_first(&opts, ipha); 5644 optval != IPOPT_EOL; 5645 optval = ipoptp_next(&opts)) { 5646 opt = opts.ipoptp_cur; 5647 switch (optval) { 5648 uint8_t off; 5649 case IPOPT_SSRR: 5650 case IPOPT_LSRR: 5651 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 5652 ip1dbg(("ip_massage_options: bad src route\n")); 5653 break; 5654 } 5655 optlen = opts.ipoptp_len; 5656 off = opt[IPOPT_OFFSET]; 5657 off--; 5658 redo_srr: 5659 if (optlen < IP_ADDR_LEN || 5660 off > optlen - IP_ADDR_LEN) { 5661 /* End of source route */ 5662 ip1dbg(("ip_massage_options: end of SR\n")); 5663 break; 5664 } 5665 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 5666 ip1dbg(("ip_massage_options: next hop 0x%x\n", 5667 ntohl(dst))); 5668 /* 5669 * Check if our address is present more than 5670 * once as consecutive hops in source route. 5671 * XXX verify per-interface ip_forwarding 5672 * for source route? 5673 */ 5674 if (ip_type_v4(dst, ipst) == IRE_LOCAL) { 5675 off += IP_ADDR_LEN; 5676 goto redo_srr; 5677 } 5678 if (dst == htonl(INADDR_LOOPBACK)) { 5679 ip1dbg(("ip_massage_options: loopback addr in " 5680 "source route!\n")); 5681 break; 5682 } 5683 /* 5684 * Update ipha_dst to be the first hop and remove the 5685 * first hop from the source route (by overwriting 5686 * part of the option with NOP options). 5687 */ 5688 ipha->ipha_dst = dst; 5689 /* Put the last entry in dst */ 5690 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 5691 3; 5692 bcopy(&opt[off], &dst, IP_ADDR_LEN); 5693 5694 ip1dbg(("ip_massage_options: last hop 0x%x\n", 5695 ntohl(dst))); 5696 /* Move down and overwrite */ 5697 opt[IP_ADDR_LEN] = opt[0]; 5698 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; 5699 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; 5700 for (i = 0; i < IP_ADDR_LEN; i++) 5701 opt[i] = IPOPT_NOP; 5702 break; 5703 } 5704 } 5705 return (dst); 5706 } 5707 5708 /* 5709 * Return the network mask 5710 * associated with the specified address. 5711 */ 5712 ipaddr_t 5713 ip_net_mask(ipaddr_t addr) 5714 { 5715 uchar_t *up = (uchar_t *)&addr; 5716 ipaddr_t mask = 0; 5717 uchar_t *maskp = (uchar_t *)&mask; 5718 5719 #if defined(__i386) || defined(__amd64) 5720 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER 5721 #endif 5722 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER 5723 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; 5724 #endif 5725 if (CLASSD(addr)) { 5726 maskp[0] = 0xF0; 5727 return (mask); 5728 } 5729 5730 /* We assume Class E default netmask to be 32 */ 5731 if (CLASSE(addr)) 5732 return (0xffffffffU); 5733 5734 if (addr == 0) 5735 return (0); 5736 maskp[0] = 0xFF; 5737 if ((up[0] & 0x80) == 0) 5738 return (mask); 5739 5740 maskp[1] = 0xFF; 5741 if ((up[0] & 0xC0) == 0x80) 5742 return (mask); 5743 5744 maskp[2] = 0xFF; 5745 if ((up[0] & 0xE0) == 0xC0) 5746 return (mask); 5747 5748 /* Otherwise return no mask */ 5749 return ((ipaddr_t)0); 5750 } 5751 5752 /* Name/Value Table Lookup Routine */ 5753 char * 5754 ip_nv_lookup(nv_t *nv, int value) 5755 { 5756 if (!nv) 5757 return (NULL); 5758 for (; nv->nv_name; nv++) { 5759 if (nv->nv_value == value) 5760 return (nv->nv_name); 5761 } 5762 return ("unknown"); 5763 } 5764 5765 static int 5766 ip_wait_for_info_ack(ill_t *ill) 5767 { 5768 int err; 5769 5770 mutex_enter(&ill->ill_lock); 5771 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { 5772 /* 5773 * Return value of 0 indicates a pending signal. 5774 */ 5775 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); 5776 if (err == 0) { 5777 mutex_exit(&ill->ill_lock); 5778 return (EINTR); 5779 } 5780 } 5781 mutex_exit(&ill->ill_lock); 5782 /* 5783 * ip_rput_other could have set an error in ill_error on 5784 * receipt of M_ERROR. 5785 */ 5786 return (ill->ill_error); 5787 } 5788 5789 /* 5790 * This is a module open, i.e. this is a control stream for access 5791 * to a DLPI device. We allocate an ill_t as the instance data in 5792 * this case. 5793 */ 5794 static int 5795 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 5796 { 5797 ill_t *ill; 5798 int err; 5799 zoneid_t zoneid; 5800 netstack_t *ns; 5801 ip_stack_t *ipst; 5802 5803 /* 5804 * Prevent unprivileged processes from pushing IP so that 5805 * they can't send raw IP. 5806 */ 5807 if (secpolicy_net_rawaccess(credp) != 0) 5808 return (EPERM); 5809 5810 ns = netstack_find_by_cred(credp); 5811 ASSERT(ns != NULL); 5812 ipst = ns->netstack_ip; 5813 ASSERT(ipst != NULL); 5814 5815 /* 5816 * For exclusive stacks we set the zoneid to zero 5817 * to make IP operate as if in the global zone. 5818 */ 5819 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 5820 zoneid = GLOBAL_ZONEID; 5821 else 5822 zoneid = crgetzoneid(credp); 5823 5824 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); 5825 q->q_ptr = WR(q)->q_ptr = ill; 5826 ill->ill_ipst = ipst; 5827 ill->ill_zoneid = zoneid; 5828 5829 /* 5830 * ill_init initializes the ill fields and then sends down 5831 * down a DL_INFO_REQ after calling qprocson. 5832 */ 5833 err = ill_init(q, ill); 5834 5835 if (err != 0) { 5836 mi_free(ill); 5837 netstack_rele(ipst->ips_netstack); 5838 q->q_ptr = NULL; 5839 WR(q)->q_ptr = NULL; 5840 return (err); 5841 } 5842 5843 /* 5844 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent. 5845 * 5846 * ill_init initializes the ipsq marking this thread as 5847 * writer 5848 */ 5849 ipsq_exit(ill->ill_phyint->phyint_ipsq); 5850 err = ip_wait_for_info_ack(ill); 5851 if (err == 0) 5852 ill->ill_credp = credp; 5853 else 5854 goto fail; 5855 5856 crhold(credp); 5857 5858 mutex_enter(&ipst->ips_ip_mi_lock); 5859 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag, 5860 sflag, credp); 5861 mutex_exit(&ipst->ips_ip_mi_lock); 5862 fail: 5863 if (err) { 5864 (void) ip_close(q, 0); 5865 return (err); 5866 } 5867 return (0); 5868 } 5869 5870 /* For /dev/ip aka AF_INET open */ 5871 int 5872 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 5873 { 5874 return (ip_open(q, devp, flag, sflag, credp, B_FALSE)); 5875 } 5876 5877 /* For /dev/ip6 aka AF_INET6 open */ 5878 int 5879 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 5880 { 5881 return (ip_open(q, devp, flag, sflag, credp, B_TRUE)); 5882 } 5883 5884 /* IP open routine. */ 5885 int 5886 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 5887 boolean_t isv6) 5888 { 5889 conn_t *connp; 5890 major_t maj; 5891 zoneid_t zoneid; 5892 netstack_t *ns; 5893 ip_stack_t *ipst; 5894 5895 /* Allow reopen. */ 5896 if (q->q_ptr != NULL) 5897 return (0); 5898 5899 if (sflag & MODOPEN) { 5900 /* This is a module open */ 5901 return (ip_modopen(q, devp, flag, sflag, credp)); 5902 } 5903 5904 if ((flag & ~(FKLYR)) == IP_HELPER_STR) { 5905 /* 5906 * Non streams based socket looking for a stream 5907 * to access IP 5908 */ 5909 return (ip_helper_stream_setup(q, devp, flag, sflag, 5910 credp, isv6)); 5911 } 5912 5913 ns = netstack_find_by_cred(credp); 5914 ASSERT(ns != NULL); 5915 ipst = ns->netstack_ip; 5916 ASSERT(ipst != NULL); 5917 5918 /* 5919 * For exclusive stacks we set the zoneid to zero 5920 * to make IP operate as if in the global zone. 5921 */ 5922 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) 5923 zoneid = GLOBAL_ZONEID; 5924 else 5925 zoneid = crgetzoneid(credp); 5926 5927 /* 5928 * We are opening as a device. This is an IP client stream, and we 5929 * allocate an conn_t as the instance data. 5930 */ 5931 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack); 5932 5933 /* 5934 * ipcl_conn_create did a netstack_hold. Undo the hold that was 5935 * done by netstack_find_by_cred() 5936 */ 5937 netstack_rele(ipst->ips_netstack); 5938 5939 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM; 5940 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */ 5941 connp->conn_ixa->ixa_zoneid = zoneid; 5942 connp->conn_zoneid = zoneid; 5943 5944 connp->conn_rq = q; 5945 q->q_ptr = WR(q)->q_ptr = connp; 5946 5947 /* Minor tells us which /dev entry was opened */ 5948 if (isv6) { 5949 connp->conn_family = AF_INET6; 5950 connp->conn_ipversion = IPV6_VERSION; 5951 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4; 5952 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT; 5953 } else { 5954 connp->conn_family = AF_INET; 5955 connp->conn_ipversion = IPV4_VERSION; 5956 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4; 5957 } 5958 5959 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 5960 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 5961 connp->conn_minor_arena = ip_minor_arena_la; 5962 } else { 5963 /* 5964 * Either minor numbers in the large arena were exhausted 5965 * or a non socket application is doing the open. 5966 * Try to allocate from the small arena. 5967 */ 5968 if ((connp->conn_dev = 5969 inet_minor_alloc(ip_minor_arena_sa)) == 0) { 5970 /* CONN_DEC_REF takes care of netstack_rele() */ 5971 q->q_ptr = WR(q)->q_ptr = NULL; 5972 CONN_DEC_REF(connp); 5973 return (EBUSY); 5974 } 5975 connp->conn_minor_arena = ip_minor_arena_sa; 5976 } 5977 5978 maj = getemajor(*devp); 5979 *devp = makedevice(maj, (minor_t)connp->conn_dev); 5980 5981 /* 5982 * connp->conn_cred is crfree()ed in ipcl_conn_destroy() 5983 */ 5984 connp->conn_cred = credp; 5985 connp->conn_cpid = curproc->p_pid; 5986 /* Cache things in ixa without an extra refhold */ 5987 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED)); 5988 connp->conn_ixa->ixa_cred = connp->conn_cred; 5989 connp->conn_ixa->ixa_cpid = connp->conn_cpid; 5990 if (is_system_labeled()) 5991 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred); 5992 5993 /* 5994 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv 5995 */ 5996 connp->conn_recv = ip_conn_input; 5997 connp->conn_recvicmp = ip_conn_input_icmp; 5998 5999 crhold(connp->conn_cred); 6000 6001 /* 6002 * If the caller has the process-wide flag set, then default to MAC 6003 * exempt mode. This allows read-down to unlabeled hosts. 6004 */ 6005 if (getpflags(NET_MAC_AWARE, credp) != 0) 6006 connp->conn_mac_mode = CONN_MAC_AWARE; 6007 6008 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID); 6009 6010 connp->conn_rq = q; 6011 connp->conn_wq = WR(q); 6012 6013 /* Non-zero default values */ 6014 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP; 6015 6016 /* 6017 * Make the conn globally visible to walkers 6018 */ 6019 ASSERT(connp->conn_ref == 1); 6020 mutex_enter(&connp->conn_lock); 6021 connp->conn_state_flags &= ~CONN_INCIPIENT; 6022 mutex_exit(&connp->conn_lock); 6023 6024 qprocson(q); 6025 6026 return (0); 6027 } 6028 6029 /* 6030 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, 6031 * all of them are copied to the conn_t. If the req is "zero", the policy is 6032 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req 6033 * fields. 6034 * We keep only the latest setting of the policy and thus policy setting 6035 * is not incremental/cumulative. 6036 * 6037 * Requests to set policies with multiple alternative actions will 6038 * go through a different API. 6039 */ 6040 int 6041 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) 6042 { 6043 uint_t ah_req = 0; 6044 uint_t esp_req = 0; 6045 uint_t se_req = 0; 6046 ipsec_act_t *actp = NULL; 6047 uint_t nact; 6048 ipsec_policy_head_t *ph; 6049 boolean_t is_pol_reset, is_pol_inserted = B_FALSE; 6050 int error = 0; 6051 netstack_t *ns = connp->conn_netstack; 6052 ip_stack_t *ipst = ns->netstack_ip; 6053 ipsec_stack_t *ipss = ns->netstack_ipsec; 6054 6055 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) 6056 6057 /* 6058 * The IP_SEC_OPT option does not allow variable length parameters, 6059 * hence a request cannot be NULL. 6060 */ 6061 if (req == NULL) 6062 return (EINVAL); 6063 6064 ah_req = req->ipsr_ah_req; 6065 esp_req = req->ipsr_esp_req; 6066 se_req = req->ipsr_self_encap_req; 6067 6068 /* Don't allow setting self-encap without one or more of AH/ESP. */ 6069 if (se_req != 0 && esp_req == 0 && ah_req == 0) 6070 return (EINVAL); 6071 6072 /* 6073 * Are we dealing with a request to reset the policy (i.e. 6074 * zero requests). 6075 */ 6076 is_pol_reset = ((ah_req & REQ_MASK) == 0 && 6077 (esp_req & REQ_MASK) == 0 && 6078 (se_req & REQ_MASK) == 0); 6079 6080 if (!is_pol_reset) { 6081 /* 6082 * If we couldn't load IPsec, fail with "protocol 6083 * not supported". 6084 * IPsec may not have been loaded for a request with zero 6085 * policies, so we don't fail in this case. 6086 */ 6087 mutex_enter(&ipss->ipsec_loader_lock); 6088 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { 6089 mutex_exit(&ipss->ipsec_loader_lock); 6090 return (EPROTONOSUPPORT); 6091 } 6092 mutex_exit(&ipss->ipsec_loader_lock); 6093 6094 /* 6095 * Test for valid requests. Invalid algorithms 6096 * need to be tested by IPsec code because new 6097 * algorithms can be added dynamically. 6098 */ 6099 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 6100 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || 6101 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { 6102 return (EINVAL); 6103 } 6104 6105 /* 6106 * Only privileged users can issue these 6107 * requests. 6108 */ 6109 if (((ah_req & IPSEC_PREF_NEVER) || 6110 (esp_req & IPSEC_PREF_NEVER) || 6111 (se_req & IPSEC_PREF_NEVER)) && 6112 secpolicy_ip_config(cr, B_FALSE) != 0) { 6113 return (EPERM); 6114 } 6115 6116 /* 6117 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER 6118 * are mutually exclusive. 6119 */ 6120 if (((ah_req & REQ_MASK) == REQ_MASK) || 6121 ((esp_req & REQ_MASK) == REQ_MASK) || 6122 ((se_req & REQ_MASK) == REQ_MASK)) { 6123 /* Both of them are set */ 6124 return (EINVAL); 6125 } 6126 } 6127 6128 ASSERT(MUTEX_HELD(&connp->conn_lock)); 6129 6130 /* 6131 * If we have already cached policies in conn_connect(), don't 6132 * let them change now. We cache policies for connections 6133 * whose src,dst [addr, port] is known. 6134 */ 6135 if (connp->conn_policy_cached) { 6136 return (EINVAL); 6137 } 6138 6139 /* 6140 * We have a zero policies, reset the connection policy if already 6141 * set. This will cause the connection to inherit the 6142 * global policy, if any. 6143 */ 6144 if (is_pol_reset) { 6145 if (connp->conn_policy != NULL) { 6146 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack); 6147 connp->conn_policy = NULL; 6148 } 6149 connp->conn_in_enforce_policy = B_FALSE; 6150 connp->conn_out_enforce_policy = B_FALSE; 6151 return (0); 6152 } 6153 6154 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy, 6155 ipst->ips_netstack); 6156 if (ph == NULL) 6157 goto enomem; 6158 6159 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack); 6160 if (actp == NULL) 6161 goto enomem; 6162 6163 /* 6164 * Always insert IPv4 policy entries, since they can also apply to 6165 * ipv6 sockets being used in ipv4-compat mode. 6166 */ 6167 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4, 6168 IPSEC_TYPE_INBOUND, ns)) 6169 goto enomem; 6170 is_pol_inserted = B_TRUE; 6171 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4, 6172 IPSEC_TYPE_OUTBOUND, ns)) 6173 goto enomem; 6174 6175 /* 6176 * We're looking at a v6 socket, also insert the v6-specific 6177 * entries. 6178 */ 6179 if (connp->conn_family == AF_INET6) { 6180 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6, 6181 IPSEC_TYPE_INBOUND, ns)) 6182 goto enomem; 6183 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6, 6184 IPSEC_TYPE_OUTBOUND, ns)) 6185 goto enomem; 6186 } 6187 6188 ipsec_actvec_free(actp, nact); 6189 6190 /* 6191 * If the requests need security, set enforce_policy. 6192 * If the requests are IPSEC_PREF_NEVER, one should 6193 * still set conn_out_enforce_policy so that ip_set_destination 6194 * marks the ip_xmit_attr_t appropriatly. This is needed so that 6195 * for connections that we don't cache policy in at connect time, 6196 * if global policy matches in ip_output_attach_policy, we 6197 * don't wrongly inherit global policy. Similarly, we need 6198 * to set conn_in_enforce_policy also so that we don't verify 6199 * policy wrongly. 6200 */ 6201 if ((ah_req & REQ_MASK) != 0 || 6202 (esp_req & REQ_MASK) != 0 || 6203 (se_req & REQ_MASK) != 0) { 6204 connp->conn_in_enforce_policy = B_TRUE; 6205 connp->conn_out_enforce_policy = B_TRUE; 6206 } 6207 6208 return (error); 6209 #undef REQ_MASK 6210 6211 /* 6212 * Common memory-allocation-failure exit path. 6213 */ 6214 enomem: 6215 if (actp != NULL) 6216 ipsec_actvec_free(actp, nact); 6217 if (is_pol_inserted) 6218 ipsec_polhead_flush(ph, ns); 6219 return (ENOMEM); 6220 } 6221 6222 /* 6223 * Set socket options for joining and leaving multicast groups. 6224 * Common to IPv4 and IPv6; inet6 indicates the type of socket. 6225 * The caller has already check that the option name is consistent with 6226 * the address family of the socket. 6227 */ 6228 int 6229 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name, 6230 uchar_t *invalp, boolean_t inet6, boolean_t checkonly) 6231 { 6232 int *i1 = (int *)invalp; 6233 int error = 0; 6234 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6235 struct ip_mreq *v4_mreqp; 6236 struct ipv6_mreq *v6_mreqp; 6237 struct group_req *greqp; 6238 ire_t *ire; 6239 boolean_t done = B_FALSE; 6240 ipaddr_t ifaddr; 6241 in6_addr_t v6group; 6242 uint_t ifindex; 6243 boolean_t mcast_opt = B_TRUE; 6244 mcast_record_t fmode; 6245 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 6246 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *); 6247 6248 switch (name) { 6249 case IP_ADD_MEMBERSHIP: 6250 case IPV6_JOIN_GROUP: 6251 mcast_opt = B_FALSE; 6252 /* FALLTHRU */ 6253 case MCAST_JOIN_GROUP: 6254 fmode = MODE_IS_EXCLUDE; 6255 optfn = ip_opt_add_group; 6256 break; 6257 6258 case IP_DROP_MEMBERSHIP: 6259 case IPV6_LEAVE_GROUP: 6260 mcast_opt = B_FALSE; 6261 /* FALLTHRU */ 6262 case MCAST_LEAVE_GROUP: 6263 fmode = MODE_IS_INCLUDE; 6264 optfn = ip_opt_delete_group; 6265 break; 6266 default: 6267 ASSERT(0); 6268 } 6269 6270 if (mcast_opt) { 6271 struct sockaddr_in *sin; 6272 struct sockaddr_in6 *sin6; 6273 6274 greqp = (struct group_req *)i1; 6275 if (greqp->gr_group.ss_family == AF_INET) { 6276 sin = (struct sockaddr_in *)&(greqp->gr_group); 6277 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group); 6278 } else { 6279 if (!inet6) 6280 return (EINVAL); /* Not on INET socket */ 6281 6282 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group); 6283 v6group = sin6->sin6_addr; 6284 } 6285 ifaddr = INADDR_ANY; 6286 ifindex = greqp->gr_interface; 6287 } else if (inet6) { 6288 v6_mreqp = (struct ipv6_mreq *)i1; 6289 v6group = v6_mreqp->ipv6mr_multiaddr; 6290 ifaddr = INADDR_ANY; 6291 ifindex = v6_mreqp->ipv6mr_interface; 6292 } else { 6293 v4_mreqp = (struct ip_mreq *)i1; 6294 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group); 6295 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr; 6296 ifindex = 0; 6297 } 6298 6299 /* 6300 * In the multirouting case, we need to replicate 6301 * the request on all interfaces that will take part 6302 * in replication. We do so because multirouting is 6303 * reflective, thus we will probably receive multi- 6304 * casts on those interfaces. 6305 * The ip_multirt_apply_membership() succeeds if 6306 * the operation succeeds on at least one interface. 6307 */ 6308 if (IN6_IS_ADDR_V4MAPPED(&v6group)) { 6309 ipaddr_t group; 6310 6311 IN6_V4MAPPED_TO_IPADDR(&v6group, group); 6312 6313 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0, 6314 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL, 6315 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL); 6316 } else { 6317 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0, 6318 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL, 6319 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL); 6320 } 6321 if (ire != NULL) { 6322 if (ire->ire_flags & RTF_MULTIRT) { 6323 error = ip_multirt_apply_membership(optfn, ire, connp, 6324 checkonly, &v6group, fmode, &ipv6_all_zeros); 6325 done = B_TRUE; 6326 } 6327 ire_refrele(ire); 6328 } 6329 6330 if (!done) { 6331 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex, 6332 fmode, &ipv6_all_zeros); 6333 } 6334 return (error); 6335 } 6336 6337 /* 6338 * Set socket options for joining and leaving multicast groups 6339 * for specific sources. 6340 * Common to IPv4 and IPv6; inet6 indicates the type of socket. 6341 * The caller has already check that the option name is consistent with 6342 * the address family of the socket. 6343 */ 6344 int 6345 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name, 6346 uchar_t *invalp, boolean_t inet6, boolean_t checkonly) 6347 { 6348 int *i1 = (int *)invalp; 6349 int error = 0; 6350 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6351 struct ip_mreq_source *imreqp; 6352 struct group_source_req *gsreqp; 6353 in6_addr_t v6group, v6src; 6354 uint32_t ifindex; 6355 ipaddr_t ifaddr; 6356 boolean_t mcast_opt = B_TRUE; 6357 mcast_record_t fmode; 6358 ire_t *ire; 6359 boolean_t done = B_FALSE; 6360 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, 6361 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *); 6362 6363 switch (name) { 6364 case IP_BLOCK_SOURCE: 6365 mcast_opt = B_FALSE; 6366 /* FALLTHRU */ 6367 case MCAST_BLOCK_SOURCE: 6368 fmode = MODE_IS_EXCLUDE; 6369 optfn = ip_opt_add_group; 6370 break; 6371 6372 case IP_UNBLOCK_SOURCE: 6373 mcast_opt = B_FALSE; 6374 /* FALLTHRU */ 6375 case MCAST_UNBLOCK_SOURCE: 6376 fmode = MODE_IS_EXCLUDE; 6377 optfn = ip_opt_delete_group; 6378 break; 6379 6380 case IP_ADD_SOURCE_MEMBERSHIP: 6381 mcast_opt = B_FALSE; 6382 /* FALLTHRU */ 6383 case MCAST_JOIN_SOURCE_GROUP: 6384 fmode = MODE_IS_INCLUDE; 6385 optfn = ip_opt_add_group; 6386 break; 6387 6388 case IP_DROP_SOURCE_MEMBERSHIP: 6389 mcast_opt = B_FALSE; 6390 /* FALLTHRU */ 6391 case MCAST_LEAVE_SOURCE_GROUP: 6392 fmode = MODE_IS_INCLUDE; 6393 optfn = ip_opt_delete_group; 6394 break; 6395 default: 6396 ASSERT(0); 6397 } 6398 6399 if (mcast_opt) { 6400 gsreqp = (struct group_source_req *)i1; 6401 ifindex = gsreqp->gsr_interface; 6402 if (gsreqp->gsr_group.ss_family == AF_INET) { 6403 struct sockaddr_in *s; 6404 s = (struct sockaddr_in *)&gsreqp->gsr_group; 6405 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group); 6406 s = (struct sockaddr_in *)&gsreqp->gsr_source; 6407 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); 6408 } else { 6409 struct sockaddr_in6 *s6; 6410 6411 if (!inet6) 6412 return (EINVAL); /* Not on INET socket */ 6413 6414 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; 6415 v6group = s6->sin6_addr; 6416 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; 6417 v6src = s6->sin6_addr; 6418 } 6419 ifaddr = INADDR_ANY; 6420 } else { 6421 imreqp = (struct ip_mreq_source *)i1; 6422 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group); 6423 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src); 6424 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; 6425 ifindex = 0; 6426 } 6427 6428 /* 6429 * Handle src being mapped INADDR_ANY by changing it to unspecified. 6430 */ 6431 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src)) 6432 v6src = ipv6_all_zeros; 6433 6434 /* 6435 * In the multirouting case, we need to replicate 6436 * the request as noted in the mcast cases above. 6437 */ 6438 if (IN6_IS_ADDR_V4MAPPED(&v6group)) { 6439 ipaddr_t group; 6440 6441 IN6_V4MAPPED_TO_IPADDR(&v6group, group); 6442 6443 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0, 6444 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL, 6445 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL); 6446 } else { 6447 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0, 6448 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL, 6449 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL); 6450 } 6451 if (ire != NULL) { 6452 if (ire->ire_flags & RTF_MULTIRT) { 6453 error = ip_multirt_apply_membership(optfn, ire, connp, 6454 checkonly, &v6group, fmode, &v6src); 6455 done = B_TRUE; 6456 } 6457 ire_refrele(ire); 6458 } 6459 if (!done) { 6460 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex, 6461 fmode, &v6src); 6462 } 6463 return (error); 6464 } 6465 6466 /* 6467 * Given a destination address and a pointer to where to put the information 6468 * this routine fills in the mtuinfo. 6469 * The socket must be connected. 6470 * For sctp conn_faddr is the primary address. 6471 */ 6472 int 6473 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo) 6474 { 6475 uint32_t pmtu = IP_MAXPACKET; 6476 uint_t scopeid; 6477 6478 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6)) 6479 return (-1); 6480 6481 /* In case we never sent or called ip_set_destination_v4/v6 */ 6482 if (ixa->ixa_ire != NULL) 6483 pmtu = ip_get_pmtu(ixa); 6484 6485 if (ixa->ixa_flags & IXAF_SCOPEID_SET) 6486 scopeid = ixa->ixa_scopeid; 6487 else 6488 scopeid = 0; 6489 6490 bzero(mtuinfo, sizeof (*mtuinfo)); 6491 mtuinfo->ip6m_addr.sin6_family = AF_INET6; 6492 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport; 6493 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6; 6494 mtuinfo->ip6m_addr.sin6_scope_id = scopeid; 6495 mtuinfo->ip6m_mtu = pmtu; 6496 6497 return (sizeof (struct ip6_mtuinfo)); 6498 } 6499 6500 /* 6501 * When the src multihoming is changed from weak to [strong, preferred] 6502 * ip_ire_rebind_walker is called to walk the list of all ire_t entries 6503 * and identify routes that were created by user-applications in the 6504 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not 6505 * currently defined. These routes are then 'rebound', i.e., their ire_ill 6506 * is selected by finding an interface route for the gateway. 6507 */ 6508 /* ARGSUSED */ 6509 void 6510 ip_ire_rebind_walker(ire_t *ire, void *notused) 6511 { 6512 if (!ire->ire_unbound || ire->ire_ill != NULL) 6513 return; 6514 ire_rebind(ire); 6515 ire_delete(ire); 6516 } 6517 6518 /* 6519 * When the src multihoming is changed from [strong, preferred] to weak, 6520 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and 6521 * set any entries that were created by user-applications in the unbound state 6522 * (i.e., without RTA_IFP) back to having a NULL ire_ill. 6523 */ 6524 /* ARGSUSED */ 6525 void 6526 ip_ire_unbind_walker(ire_t *ire, void *notused) 6527 { 6528 ire_t *new_ire; 6529 6530 if (!ire->ire_unbound || ire->ire_ill == NULL) 6531 return; 6532 if (ire->ire_ipversion == IPV6_VERSION) { 6533 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6, 6534 &ire->ire_gateway_addr_v6, ire->ire_type, NULL, 6535 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst); 6536 } else { 6537 new_ire = ire_create((uchar_t *)&ire->ire_addr, 6538 (uchar_t *)&ire->ire_mask, 6539 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL, 6540 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst); 6541 } 6542 if (new_ire == NULL) 6543 return; 6544 new_ire->ire_unbound = B_TRUE; 6545 /* 6546 * The bound ire must first be deleted so that we don't return 6547 * the existing one on the attempt to add the unbound new_ire. 6548 */ 6549 ire_delete(ire); 6550 new_ire = ire_add(new_ire); 6551 if (new_ire != NULL) 6552 ire_refrele(new_ire); 6553 } 6554 6555 /* 6556 * When the settings of ip*_strict_src_multihoming tunables are changed, 6557 * all cached routes need to be recomputed. This recomputation needs to be 6558 * done when going from weaker to stronger modes so that the cached ire 6559 * for the connection does not violate the current ip*_strict_src_multihoming 6560 * setting. It also needs to be done when going from stronger to weaker modes, 6561 * so that we fall back to matching on the longest-matching-route (as opposed 6562 * to a shorter match that may have been selected in the strong mode 6563 * to satisfy src_multihoming settings). 6564 * 6565 * The cached ixa_ire entires for all conn_t entries are marked as 6566 * "verify" so that they will be recomputed for the next packet. 6567 */ 6568 void 6569 conn_ire_revalidate(conn_t *connp, void *arg) 6570 { 6571 boolean_t isv6 = (boolean_t)arg; 6572 6573 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) || 6574 (!isv6 && connp->conn_ipversion != IPV4_VERSION)) 6575 return; 6576 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY; 6577 } 6578 6579 /* 6580 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, 6581 * When an ipf is passed here for the first time, if 6582 * we already have in-order fragments on the queue, we convert from the fast- 6583 * path reassembly scheme to the hard-case scheme. From then on, additional 6584 * fragments are reassembled here. We keep track of the start and end offsets 6585 * of each piece, and the number of holes in the chain. When the hole count 6586 * goes to zero, we are done! 6587 * 6588 * The ipf_count will be updated to account for any mblk(s) added (pointed to 6589 * by mp) or subtracted (freeb()ed dups), upon return the caller must update 6590 * ipfb_count and ill_frag_count by the difference of ipf_count before and 6591 * after the call to ip_reassemble(). 6592 */ 6593 int 6594 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, 6595 size_t msg_len) 6596 { 6597 uint_t end; 6598 mblk_t *next_mp; 6599 mblk_t *mp1; 6600 uint_t offset; 6601 boolean_t incr_dups = B_TRUE; 6602 boolean_t offset_zero_seen = B_FALSE; 6603 boolean_t pkt_boundary_checked = B_FALSE; 6604 6605 /* If start == 0 then ipf_nf_hdr_len has to be set. */ 6606 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); 6607 6608 /* Add in byte count */ 6609 ipf->ipf_count += msg_len; 6610 if (ipf->ipf_end) { 6611 /* 6612 * We were part way through in-order reassembly, but now there 6613 * is a hole. We walk through messages already queued, and 6614 * mark them for hard case reassembly. We know that up till 6615 * now they were in order starting from offset zero. 6616 */ 6617 offset = 0; 6618 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 6619 IP_REASS_SET_START(mp1, offset); 6620 if (offset == 0) { 6621 ASSERT(ipf->ipf_nf_hdr_len != 0); 6622 offset = -ipf->ipf_nf_hdr_len; 6623 } 6624 offset += mp1->b_wptr - mp1->b_rptr; 6625 IP_REASS_SET_END(mp1, offset); 6626 } 6627 /* One hole at the end. */ 6628 ipf->ipf_hole_cnt = 1; 6629 /* Brand it as a hard case, forever. */ 6630 ipf->ipf_end = 0; 6631 } 6632 /* Walk through all the new pieces. */ 6633 do { 6634 end = start + (mp->b_wptr - mp->b_rptr); 6635 /* 6636 * If start is 0, decrease 'end' only for the first mblk of 6637 * the fragment. Otherwise 'end' can get wrong value in the 6638 * second pass of the loop if first mblk is exactly the 6639 * size of ipf_nf_hdr_len. 6640 */ 6641 if (start == 0 && !offset_zero_seen) { 6642 /* First segment */ 6643 ASSERT(ipf->ipf_nf_hdr_len != 0); 6644 end -= ipf->ipf_nf_hdr_len; 6645 offset_zero_seen = B_TRUE; 6646 } 6647 next_mp = mp->b_cont; 6648 /* 6649 * We are checking to see if there is any interesing data 6650 * to process. If there isn't and the mblk isn't the 6651 * one which carries the unfragmentable header then we 6652 * drop it. It's possible to have just the unfragmentable 6653 * header come through without any data. That needs to be 6654 * saved. 6655 * 6656 * If the assert at the top of this function holds then the 6657 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code 6658 * is infrequently traveled enough that the test is left in 6659 * to protect against future code changes which break that 6660 * invariant. 6661 */ 6662 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { 6663 /* Empty. Blast it. */ 6664 IP_REASS_SET_START(mp, 0); 6665 IP_REASS_SET_END(mp, 0); 6666 /* 6667 * If the ipf points to the mblk we are about to free, 6668 * update ipf to point to the next mblk (or NULL 6669 * if none). 6670 */ 6671 if (ipf->ipf_mp->b_cont == mp) 6672 ipf->ipf_mp->b_cont = next_mp; 6673 freeb(mp); 6674 continue; 6675 } 6676 mp->b_cont = NULL; 6677 IP_REASS_SET_START(mp, start); 6678 IP_REASS_SET_END(mp, end); 6679 if (!ipf->ipf_tail_mp) { 6680 ipf->ipf_tail_mp = mp; 6681 ipf->ipf_mp->b_cont = mp; 6682 if (start == 0 || !more) { 6683 ipf->ipf_hole_cnt = 1; 6684 /* 6685 * if the first fragment comes in more than one 6686 * mblk, this loop will be executed for each 6687 * mblk. Need to adjust hole count so exiting 6688 * this routine will leave hole count at 1. 6689 */ 6690 if (next_mp) 6691 ipf->ipf_hole_cnt++; 6692 } else 6693 ipf->ipf_hole_cnt = 2; 6694 continue; 6695 } else if (ipf->ipf_last_frag_seen && !more && 6696 !pkt_boundary_checked) { 6697 /* 6698 * We check datagram boundary only if this fragment 6699 * claims to be the last fragment and we have seen a 6700 * last fragment in the past too. We do this only 6701 * once for a given fragment. 6702 * 6703 * start cannot be 0 here as fragments with start=0 6704 * and MF=0 gets handled as a complete packet. These 6705 * fragments should not reach here. 6706 */ 6707 6708 if (start + msgdsize(mp) != 6709 IP_REASS_END(ipf->ipf_tail_mp)) { 6710 /* 6711 * We have two fragments both of which claim 6712 * to be the last fragment but gives conflicting 6713 * information about the whole datagram size. 6714 * Something fishy is going on. Drop the 6715 * fragment and free up the reassembly list. 6716 */ 6717 return (IP_REASS_FAILED); 6718 } 6719 6720 /* 6721 * We shouldn't come to this code block again for this 6722 * particular fragment. 6723 */ 6724 pkt_boundary_checked = B_TRUE; 6725 } 6726 6727 /* New stuff at or beyond tail? */ 6728 offset = IP_REASS_END(ipf->ipf_tail_mp); 6729 if (start >= offset) { 6730 if (ipf->ipf_last_frag_seen) { 6731 /* current fragment is beyond last fragment */ 6732 return (IP_REASS_FAILED); 6733 } 6734 /* Link it on end. */ 6735 ipf->ipf_tail_mp->b_cont = mp; 6736 ipf->ipf_tail_mp = mp; 6737 if (more) { 6738 if (start != offset) 6739 ipf->ipf_hole_cnt++; 6740 } else if (start == offset && next_mp == NULL) 6741 ipf->ipf_hole_cnt--; 6742 continue; 6743 } 6744 mp1 = ipf->ipf_mp->b_cont; 6745 offset = IP_REASS_START(mp1); 6746 /* New stuff at the front? */ 6747 if (start < offset) { 6748 if (start == 0) { 6749 if (end >= offset) { 6750 /* Nailed the hole at the begining. */ 6751 ipf->ipf_hole_cnt--; 6752 } 6753 } else if (end < offset) { 6754 /* 6755 * A hole, stuff, and a hole where there used 6756 * to be just a hole. 6757 */ 6758 ipf->ipf_hole_cnt++; 6759 } 6760 mp->b_cont = mp1; 6761 /* Check for overlap. */ 6762 while (end > offset) { 6763 if (end < IP_REASS_END(mp1)) { 6764 mp->b_wptr -= end - offset; 6765 IP_REASS_SET_END(mp, offset); 6766 BUMP_MIB(ill->ill_ip_mib, 6767 ipIfStatsReasmPartDups); 6768 break; 6769 } 6770 /* Did we cover another hole? */ 6771 if ((mp1->b_cont && 6772 IP_REASS_END(mp1) != 6773 IP_REASS_START(mp1->b_cont) && 6774 end >= IP_REASS_START(mp1->b_cont)) || 6775 (!ipf->ipf_last_frag_seen && !more)) { 6776 ipf->ipf_hole_cnt--; 6777 } 6778 /* Clip out mp1. */ 6779 if ((mp->b_cont = mp1->b_cont) == NULL) { 6780 /* 6781 * After clipping out mp1, this guy 6782 * is now hanging off the end. 6783 */ 6784 ipf->ipf_tail_mp = mp; 6785 } 6786 IP_REASS_SET_START(mp1, 0); 6787 IP_REASS_SET_END(mp1, 0); 6788 /* Subtract byte count */ 6789 ipf->ipf_count -= mp1->b_datap->db_lim - 6790 mp1->b_datap->db_base; 6791 freeb(mp1); 6792 BUMP_MIB(ill->ill_ip_mib, 6793 ipIfStatsReasmPartDups); 6794 mp1 = mp->b_cont; 6795 if (!mp1) 6796 break; 6797 offset = IP_REASS_START(mp1); 6798 } 6799 ipf->ipf_mp->b_cont = mp; 6800 continue; 6801 } 6802 /* 6803 * The new piece starts somewhere between the start of the head 6804 * and before the end of the tail. 6805 */ 6806 for (; mp1; mp1 = mp1->b_cont) { 6807 offset = IP_REASS_END(mp1); 6808 if (start < offset) { 6809 if (end <= offset) { 6810 /* Nothing new. */ 6811 IP_REASS_SET_START(mp, 0); 6812 IP_REASS_SET_END(mp, 0); 6813 /* Subtract byte count */ 6814 ipf->ipf_count -= mp->b_datap->db_lim - 6815 mp->b_datap->db_base; 6816 if (incr_dups) { 6817 ipf->ipf_num_dups++; 6818 incr_dups = B_FALSE; 6819 } 6820 freeb(mp); 6821 BUMP_MIB(ill->ill_ip_mib, 6822 ipIfStatsReasmDuplicates); 6823 break; 6824 } 6825 /* 6826 * Trim redundant stuff off beginning of new 6827 * piece. 6828 */ 6829 IP_REASS_SET_START(mp, offset); 6830 mp->b_rptr += offset - start; 6831 BUMP_MIB(ill->ill_ip_mib, 6832 ipIfStatsReasmPartDups); 6833 start = offset; 6834 if (!mp1->b_cont) { 6835 /* 6836 * After trimming, this guy is now 6837 * hanging off the end. 6838 */ 6839 mp1->b_cont = mp; 6840 ipf->ipf_tail_mp = mp; 6841 if (!more) { 6842 ipf->ipf_hole_cnt--; 6843 } 6844 break; 6845 } 6846 } 6847 if (start >= IP_REASS_START(mp1->b_cont)) 6848 continue; 6849 /* Fill a hole */ 6850 if (start > offset) 6851 ipf->ipf_hole_cnt++; 6852 mp->b_cont = mp1->b_cont; 6853 mp1->b_cont = mp; 6854 mp1 = mp->b_cont; 6855 offset = IP_REASS_START(mp1); 6856 if (end >= offset) { 6857 ipf->ipf_hole_cnt--; 6858 /* Check for overlap. */ 6859 while (end > offset) { 6860 if (end < IP_REASS_END(mp1)) { 6861 mp->b_wptr -= end - offset; 6862 IP_REASS_SET_END(mp, offset); 6863 /* 6864 * TODO we might bump 6865 * this up twice if there is 6866 * overlap at both ends. 6867 */ 6868 BUMP_MIB(ill->ill_ip_mib, 6869 ipIfStatsReasmPartDups); 6870 break; 6871 } 6872 /* Did we cover another hole? */ 6873 if ((mp1->b_cont && 6874 IP_REASS_END(mp1) 6875 != IP_REASS_START(mp1->b_cont) && 6876 end >= 6877 IP_REASS_START(mp1->b_cont)) || 6878 (!ipf->ipf_last_frag_seen && 6879 !more)) { 6880 ipf->ipf_hole_cnt--; 6881 } 6882 /* Clip out mp1. */ 6883 if ((mp->b_cont = mp1->b_cont) == 6884 NULL) { 6885 /* 6886 * After clipping out mp1, 6887 * this guy is now hanging 6888 * off the end. 6889 */ 6890 ipf->ipf_tail_mp = mp; 6891 } 6892 IP_REASS_SET_START(mp1, 0); 6893 IP_REASS_SET_END(mp1, 0); 6894 /* Subtract byte count */ 6895 ipf->ipf_count -= 6896 mp1->b_datap->db_lim - 6897 mp1->b_datap->db_base; 6898 freeb(mp1); 6899 BUMP_MIB(ill->ill_ip_mib, 6900 ipIfStatsReasmPartDups); 6901 mp1 = mp->b_cont; 6902 if (!mp1) 6903 break; 6904 offset = IP_REASS_START(mp1); 6905 } 6906 } 6907 break; 6908 } 6909 } while (start = end, mp = next_mp); 6910 6911 /* Fragment just processed could be the last one. Remember this fact */ 6912 if (!more) 6913 ipf->ipf_last_frag_seen = B_TRUE; 6914 6915 /* Still got holes? */ 6916 if (ipf->ipf_hole_cnt) 6917 return (IP_REASS_PARTIAL); 6918 /* Clean up overloaded fields to avoid upstream disasters. */ 6919 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { 6920 IP_REASS_SET_START(mp1, 0); 6921 IP_REASS_SET_END(mp1, 0); 6922 } 6923 return (IP_REASS_COMPLETE); 6924 } 6925 6926 /* 6927 * Fragmentation reassembly. Each ILL has a hash table for 6928 * queuing packets undergoing reassembly for all IPIFs 6929 * associated with the ILL. The hash is based on the packet 6930 * IP ident field. The ILL frag hash table was allocated 6931 * as a timer block at the time the ILL was created. Whenever 6932 * there is anything on the reassembly queue, the timer will 6933 * be running. Returns the reassembled packet if reassembly completes. 6934 */ 6935 mblk_t * 6936 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira) 6937 { 6938 uint32_t frag_offset_flags; 6939 mblk_t *t_mp; 6940 ipaddr_t dst; 6941 uint8_t proto = ipha->ipha_protocol; 6942 uint32_t sum_val; 6943 uint16_t sum_flags; 6944 ipf_t *ipf; 6945 ipf_t **ipfp; 6946 ipfb_t *ipfb; 6947 uint16_t ident; 6948 uint32_t offset; 6949 ipaddr_t src; 6950 uint_t hdr_length; 6951 uint32_t end; 6952 mblk_t *mp1; 6953 mblk_t *tail_mp; 6954 size_t count; 6955 size_t msg_len; 6956 uint8_t ecn_info = 0; 6957 uint32_t packet_size; 6958 boolean_t pruned = B_FALSE; 6959 ill_t *ill = ira->ira_ill; 6960 ip_stack_t *ipst = ill->ill_ipst; 6961 6962 /* 6963 * Drop the fragmented as early as possible, if 6964 * we don't have resource(s) to re-assemble. 6965 */ 6966 if (ipst->ips_ip_reass_queue_bytes == 0) { 6967 freemsg(mp); 6968 return (NULL); 6969 } 6970 6971 /* Check for fragmentation offset; return if there's none */ 6972 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & 6973 (IPH_MF | IPH_OFFSET)) == 0) 6974 return (mp); 6975 6976 /* 6977 * We utilize hardware computed checksum info only for UDP since 6978 * IP fragmentation is a normal occurrence for the protocol. In 6979 * addition, checksum offload support for IP fragments carrying 6980 * UDP payload is commonly implemented across network adapters. 6981 */ 6982 ASSERT(ira->ira_rill != NULL); 6983 if (proto == IPPROTO_UDP && dohwcksum && 6984 ILL_HCKSUM_CAPABLE(ira->ira_rill) && 6985 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 6986 mblk_t *mp1 = mp->b_cont; 6987 int32_t len; 6988 6989 /* Record checksum information from the packet */ 6990 sum_val = (uint32_t)DB_CKSUM16(mp); 6991 sum_flags = DB_CKSUMFLAGS(mp); 6992 6993 /* IP payload offset from beginning of mblk */ 6994 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; 6995 6996 if ((sum_flags & HCK_PARTIALCKSUM) && 6997 (mp1 == NULL || mp1->b_cont == NULL) && 6998 offset >= DB_CKSUMSTART(mp) && 6999 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { 7000 uint32_t adj; 7001 /* 7002 * Partial checksum has been calculated by hardware 7003 * and attached to the packet; in addition, any 7004 * prepended extraneous data is even byte aligned. 7005 * If any such data exists, we adjust the checksum; 7006 * this would also handle any postpended data. 7007 */ 7008 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 7009 mp, mp1, len, adj); 7010 7011 /* One's complement subtract extraneous checksum */ 7012 if (adj >= sum_val) 7013 sum_val = ~(adj - sum_val) & 0xFFFF; 7014 else 7015 sum_val -= adj; 7016 } 7017 } else { 7018 sum_val = 0; 7019 sum_flags = 0; 7020 } 7021 7022 /* Clear hardware checksumming flag */ 7023 DB_CKSUMFLAGS(mp) = 0; 7024 7025 ident = ipha->ipha_ident; 7026 offset = (frag_offset_flags << 3) & 0xFFFF; 7027 src = ipha->ipha_src; 7028 dst = ipha->ipha_dst; 7029 hdr_length = IPH_HDR_LENGTH(ipha); 7030 end = ntohs(ipha->ipha_length) - hdr_length; 7031 7032 /* If end == 0 then we have a packet with no data, so just free it */ 7033 if (end == 0) { 7034 freemsg(mp); 7035 return (NULL); 7036 } 7037 7038 /* Record the ECN field info. */ 7039 ecn_info = (ipha->ipha_type_of_service & 0x3); 7040 if (offset != 0) { 7041 /* 7042 * If this isn't the first piece, strip the header, and 7043 * add the offset to the end value. 7044 */ 7045 mp->b_rptr += hdr_length; 7046 end += offset; 7047 } 7048 7049 /* Handle vnic loopback of fragments */ 7050 if (mp->b_datap->db_ref > 2) 7051 msg_len = 0; 7052 else 7053 msg_len = MBLKSIZE(mp); 7054 7055 tail_mp = mp; 7056 while (tail_mp->b_cont != NULL) { 7057 tail_mp = tail_mp->b_cont; 7058 if (tail_mp->b_datap->db_ref <= 2) 7059 msg_len += MBLKSIZE(tail_mp); 7060 } 7061 7062 /* If the reassembly list for this ILL will get too big, prune it */ 7063 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 7064 ipst->ips_ip_reass_queue_bytes) { 7065 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len, 7066 uint_t, ill->ill_frag_count, 7067 uint_t, ipst->ips_ip_reass_queue_bytes); 7068 ill_frag_prune(ill, 7069 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 : 7070 (ipst->ips_ip_reass_queue_bytes - msg_len)); 7071 pruned = B_TRUE; 7072 } 7073 7074 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; 7075 mutex_enter(&ipfb->ipfb_lock); 7076 7077 ipfp = &ipfb->ipfb_ipf; 7078 /* Try to find an existing fragment queue for this packet. */ 7079 for (;;) { 7080 ipf = ipfp[0]; 7081 if (ipf != NULL) { 7082 /* 7083 * It has to match on ident and src/dst address. 7084 */ 7085 if (ipf->ipf_ident == ident && 7086 ipf->ipf_src == src && 7087 ipf->ipf_dst == dst && 7088 ipf->ipf_protocol == proto) { 7089 /* 7090 * If we have received too many 7091 * duplicate fragments for this packet 7092 * free it. 7093 */ 7094 if (ipf->ipf_num_dups > ip_max_frag_dups) { 7095 ill_frag_free_pkts(ill, ipfb, ipf, 1); 7096 freemsg(mp); 7097 mutex_exit(&ipfb->ipfb_lock); 7098 return (NULL); 7099 } 7100 /* Found it. */ 7101 break; 7102 } 7103 ipfp = &ipf->ipf_hash_next; 7104 continue; 7105 } 7106 7107 /* 7108 * If we pruned the list, do we want to store this new 7109 * fragment?. We apply an optimization here based on the 7110 * fact that most fragments will be received in order. 7111 * So if the offset of this incoming fragment is zero, 7112 * it is the first fragment of a new packet. We will 7113 * keep it. Otherwise drop the fragment, as we have 7114 * probably pruned the packet already (since the 7115 * packet cannot be found). 7116 */ 7117 if (pruned && offset != 0) { 7118 mutex_exit(&ipfb->ipfb_lock); 7119 freemsg(mp); 7120 return (NULL); 7121 } 7122 7123 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) { 7124 /* 7125 * Too many fragmented packets in this hash 7126 * bucket. Free the oldest. 7127 */ 7128 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 7129 } 7130 7131 /* New guy. Allocate a frag message. */ 7132 mp1 = allocb(sizeof (*ipf), BPRI_MED); 7133 if (mp1 == NULL) { 7134 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 7135 ip_drop_input("ipIfStatsInDiscards", mp, ill); 7136 freemsg(mp); 7137 reass_done: 7138 mutex_exit(&ipfb->ipfb_lock); 7139 return (NULL); 7140 } 7141 7142 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); 7143 mp1->b_cont = mp; 7144 7145 /* Initialize the fragment header. */ 7146 ipf = (ipf_t *)mp1->b_rptr; 7147 ipf->ipf_mp = mp1; 7148 ipf->ipf_ptphn = ipfp; 7149 ipfp[0] = ipf; 7150 ipf->ipf_hash_next = NULL; 7151 ipf->ipf_ident = ident; 7152 ipf->ipf_protocol = proto; 7153 ipf->ipf_src = src; 7154 ipf->ipf_dst = dst; 7155 ipf->ipf_nf_hdr_len = 0; 7156 /* Record reassembly start time. */ 7157 ipf->ipf_timestamp = gethrestime_sec(); 7158 /* Record ipf generation and account for frag header */ 7159 ipf->ipf_gen = ill->ill_ipf_gen++; 7160 ipf->ipf_count = MBLKSIZE(mp1); 7161 ipf->ipf_last_frag_seen = B_FALSE; 7162 ipf->ipf_ecn = ecn_info; 7163 ipf->ipf_num_dups = 0; 7164 ipfb->ipfb_frag_pkts++; 7165 ipf->ipf_checksum = 0; 7166 ipf->ipf_checksum_flags = 0; 7167 7168 /* Store checksum value in fragment header */ 7169 if (sum_flags != 0) { 7170 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 7171 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 7172 ipf->ipf_checksum = sum_val; 7173 ipf->ipf_checksum_flags = sum_flags; 7174 } 7175 7176 /* 7177 * We handle reassembly two ways. In the easy case, 7178 * where all the fragments show up in order, we do 7179 * minimal bookkeeping, and just clip new pieces on 7180 * the end. If we ever see a hole, then we go off 7181 * to ip_reassemble which has to mark the pieces and 7182 * keep track of the number of holes, etc. Obviously, 7183 * the point of having both mechanisms is so we can 7184 * handle the easy case as efficiently as possible. 7185 */ 7186 if (offset == 0) { 7187 /* Easy case, in-order reassembly so far. */ 7188 ipf->ipf_count += msg_len; 7189 ipf->ipf_tail_mp = tail_mp; 7190 /* 7191 * Keep track of next expected offset in 7192 * ipf_end. 7193 */ 7194 ipf->ipf_end = end; 7195 ipf->ipf_nf_hdr_len = hdr_length; 7196 } else { 7197 /* Hard case, hole at the beginning. */ 7198 ipf->ipf_tail_mp = NULL; 7199 /* 7200 * ipf_end == 0 means that we have given up 7201 * on easy reassembly. 7202 */ 7203 ipf->ipf_end = 0; 7204 7205 /* Forget checksum offload from now on */ 7206 ipf->ipf_checksum_flags = 0; 7207 7208 /* 7209 * ipf_hole_cnt is set by ip_reassemble. 7210 * ipf_count is updated by ip_reassemble. 7211 * No need to check for return value here 7212 * as we don't expect reassembly to complete 7213 * or fail for the first fragment itself. 7214 */ 7215 (void) ip_reassemble(mp, ipf, 7216 (frag_offset_flags & IPH_OFFSET) << 3, 7217 (frag_offset_flags & IPH_MF), ill, msg_len); 7218 } 7219 /* Update per ipfb and ill byte counts */ 7220 ipfb->ipfb_count += ipf->ipf_count; 7221 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 7222 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count); 7223 /* If the frag timer wasn't already going, start it. */ 7224 mutex_enter(&ill->ill_lock); 7225 ill_frag_timer_start(ill); 7226 mutex_exit(&ill->ill_lock); 7227 goto reass_done; 7228 } 7229 7230 /* 7231 * If the packet's flag has changed (it could be coming up 7232 * from an interface different than the previous, therefore 7233 * possibly different checksum capability), then forget about 7234 * any stored checksum states. Otherwise add the value to 7235 * the existing one stored in the fragment header. 7236 */ 7237 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 7238 sum_val += ipf->ipf_checksum; 7239 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 7240 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 7241 ipf->ipf_checksum = sum_val; 7242 } else if (ipf->ipf_checksum_flags != 0) { 7243 /* Forget checksum offload from now on */ 7244 ipf->ipf_checksum_flags = 0; 7245 } 7246 7247 /* 7248 * We have a new piece of a datagram which is already being 7249 * reassembled. Update the ECN info if all IP fragments 7250 * are ECN capable. If there is one which is not, clear 7251 * all the info. If there is at least one which has CE 7252 * code point, IP needs to report that up to transport. 7253 */ 7254 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 7255 if (ecn_info == IPH_ECN_CE) 7256 ipf->ipf_ecn = IPH_ECN_CE; 7257 } else { 7258 ipf->ipf_ecn = IPH_ECN_NECT; 7259 } 7260 if (offset && ipf->ipf_end == offset) { 7261 /* The new fragment fits at the end */ 7262 ipf->ipf_tail_mp->b_cont = mp; 7263 /* Update the byte count */ 7264 ipf->ipf_count += msg_len; 7265 /* Update per ipfb and ill byte counts */ 7266 ipfb->ipfb_count += msg_len; 7267 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 7268 atomic_add_32(&ill->ill_frag_count, msg_len); 7269 if (frag_offset_flags & IPH_MF) { 7270 /* More to come. */ 7271 ipf->ipf_end = end; 7272 ipf->ipf_tail_mp = tail_mp; 7273 goto reass_done; 7274 } 7275 } else { 7276 /* Go do the hard cases. */ 7277 int ret; 7278 7279 if (offset == 0) 7280 ipf->ipf_nf_hdr_len = hdr_length; 7281 7282 /* Save current byte count */ 7283 count = ipf->ipf_count; 7284 ret = ip_reassemble(mp, ipf, 7285 (frag_offset_flags & IPH_OFFSET) << 3, 7286 (frag_offset_flags & IPH_MF), ill, msg_len); 7287 /* Count of bytes added and subtracted (freeb()ed) */ 7288 count = ipf->ipf_count - count; 7289 if (count) { 7290 /* Update per ipfb and ill byte counts */ 7291 ipfb->ipfb_count += count; 7292 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 7293 atomic_add_32(&ill->ill_frag_count, count); 7294 } 7295 if (ret == IP_REASS_PARTIAL) { 7296 goto reass_done; 7297 } else if (ret == IP_REASS_FAILED) { 7298 /* Reassembly failed. Free up all resources */ 7299 ill_frag_free_pkts(ill, ipfb, ipf, 1); 7300 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 7301 IP_REASS_SET_START(t_mp, 0); 7302 IP_REASS_SET_END(t_mp, 0); 7303 } 7304 freemsg(mp); 7305 goto reass_done; 7306 } 7307 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 7308 } 7309 /* 7310 * We have completed reassembly. Unhook the frag header from 7311 * the reassembly list. 7312 * 7313 * Before we free the frag header, record the ECN info 7314 * to report back to the transport. 7315 */ 7316 ecn_info = ipf->ipf_ecn; 7317 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); 7318 ipfp = ipf->ipf_ptphn; 7319 7320 /* We need to supply these to caller */ 7321 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 7322 sum_val = ipf->ipf_checksum; 7323 else 7324 sum_val = 0; 7325 7326 mp1 = ipf->ipf_mp; 7327 count = ipf->ipf_count; 7328 ipf = ipf->ipf_hash_next; 7329 if (ipf != NULL) 7330 ipf->ipf_ptphn = ipfp; 7331 ipfp[0] = ipf; 7332 atomic_add_32(&ill->ill_frag_count, -count); 7333 ASSERT(ipfb->ipfb_count >= count); 7334 ipfb->ipfb_count -= count; 7335 ipfb->ipfb_frag_pkts--; 7336 mutex_exit(&ipfb->ipfb_lock); 7337 /* Ditch the frag header. */ 7338 mp = mp1->b_cont; 7339 7340 freeb(mp1); 7341 7342 /* Restore original IP length in header. */ 7343 packet_size = (uint32_t)msgdsize(mp); 7344 if (packet_size > IP_MAXPACKET) { 7345 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7346 ip_drop_input("Reassembled packet too large", mp, ill); 7347 freemsg(mp); 7348 return (NULL); 7349 } 7350 7351 if (DB_REF(mp) > 1) { 7352 mblk_t *mp2 = copymsg(mp); 7353 7354 if (mp2 == NULL) { 7355 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 7356 ip_drop_input("ipIfStatsInDiscards", mp, ill); 7357 freemsg(mp); 7358 return (NULL); 7359 } 7360 freemsg(mp); 7361 mp = mp2; 7362 } 7363 ipha = (ipha_t *)mp->b_rptr; 7364 7365 ipha->ipha_length = htons((uint16_t)packet_size); 7366 /* We're now complete, zip the frag state */ 7367 ipha->ipha_fragment_offset_and_flags = 0; 7368 /* Record the ECN info. */ 7369 ipha->ipha_type_of_service &= 0xFC; 7370 ipha->ipha_type_of_service |= ecn_info; 7371 7372 /* Update the receive attributes */ 7373 ira->ira_pktlen = packet_size; 7374 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha); 7375 7376 /* Reassembly is successful; set checksum information in packet */ 7377 DB_CKSUM16(mp) = (uint16_t)sum_val; 7378 DB_CKSUMFLAGS(mp) = sum_flags; 7379 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length; 7380 7381 return (mp); 7382 } 7383 7384 /* 7385 * Pullup function that should be used for IP input in order to 7386 * ensure we do not loose the L2 source address; we need the l2 source 7387 * address for IP_RECVSLLA and for ndp_input. 7388 * 7389 * We return either NULL or b_rptr. 7390 */ 7391 void * 7392 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira) 7393 { 7394 ill_t *ill = ira->ira_ill; 7395 7396 if (ip_rput_pullups++ == 0) { 7397 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE, 7398 "ip_pullup: %s forced us to " 7399 " pullup pkt, hdr len %ld, hdr addr %p", 7400 ill->ill_name, len, (void *)mp->b_rptr); 7401 } 7402 if (!(ira->ira_flags & IRAF_L2SRC_SET)) 7403 ip_setl2src(mp, ira, ira->ira_rill); 7404 ASSERT(ira->ira_flags & IRAF_L2SRC_SET); 7405 if (!pullupmsg(mp, len)) 7406 return (NULL); 7407 else 7408 return (mp->b_rptr); 7409 } 7410 7411 /* 7412 * Make sure ira_l2src has an address. If we don't have one fill with zeros. 7413 * When called from the ULP ira_rill will be NULL hence the caller has to 7414 * pass in the ill. 7415 */ 7416 /* ARGSUSED */ 7417 void 7418 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill) 7419 { 7420 const uchar_t *addr; 7421 int alen; 7422 7423 if (ira->ira_flags & IRAF_L2SRC_SET) 7424 return; 7425 7426 ASSERT(ill != NULL); 7427 alen = ill->ill_phys_addr_length; 7428 ASSERT(alen <= sizeof (ira->ira_l2src)); 7429 if (ira->ira_mhip != NULL && 7430 (addr = ira->ira_mhip->mhi_saddr) != NULL) { 7431 bcopy(addr, ira->ira_l2src, alen); 7432 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) && 7433 (addr = ill->ill_phys_addr) != NULL) { 7434 bcopy(addr, ira->ira_l2src, alen); 7435 } else { 7436 bzero(ira->ira_l2src, alen); 7437 } 7438 ira->ira_flags |= IRAF_L2SRC_SET; 7439 } 7440 7441 /* 7442 * check ip header length and align it. 7443 */ 7444 mblk_t * 7445 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira) 7446 { 7447 ill_t *ill = ira->ira_ill; 7448 ssize_t len; 7449 7450 len = MBLKL(mp); 7451 7452 if (!OK_32PTR(mp->b_rptr)) 7453 IP_STAT(ill->ill_ipst, ip_notaligned); 7454 else 7455 IP_STAT(ill->ill_ipst, ip_recv_pullup); 7456 7457 /* Guard against bogus device drivers */ 7458 if (len < 0) { 7459 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7460 ip_drop_input("ipIfStatsInHdrErrors", mp, ill); 7461 freemsg(mp); 7462 return (NULL); 7463 } 7464 7465 if (len == 0) { 7466 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */ 7467 mblk_t *mp1 = mp->b_cont; 7468 7469 if (!(ira->ira_flags & IRAF_L2SRC_SET)) 7470 ip_setl2src(mp, ira, ira->ira_rill); 7471 ASSERT(ira->ira_flags & IRAF_L2SRC_SET); 7472 7473 freeb(mp); 7474 mp = mp1; 7475 if (mp == NULL) 7476 return (NULL); 7477 7478 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size) 7479 return (mp); 7480 } 7481 if (ip_pullup(mp, min_size, ira) == NULL) { 7482 if (msgdsize(mp) < min_size) { 7483 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7484 ip_drop_input("ipIfStatsInHdrErrors", mp, ill); 7485 } else { 7486 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 7487 ip_drop_input("ipIfStatsInDiscards", mp, ill); 7488 } 7489 freemsg(mp); 7490 return (NULL); 7491 } 7492 return (mp); 7493 } 7494 7495 /* 7496 * Common code for IPv4 and IPv6 to check and pullup multi-mblks 7497 */ 7498 mblk_t * 7499 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len, 7500 uint_t min_size, ip_recv_attr_t *ira) 7501 { 7502 ill_t *ill = ira->ira_ill; 7503 7504 /* 7505 * Make sure we have data length consistent 7506 * with the IP header. 7507 */ 7508 if (mp->b_cont == NULL) { 7509 /* pkt_len is based on ipha_len, not the mblk length */ 7510 if (pkt_len < min_size) { 7511 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7512 ip_drop_input("ipIfStatsInHdrErrors", mp, ill); 7513 freemsg(mp); 7514 return (NULL); 7515 } 7516 if (len < 0) { 7517 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts); 7518 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill); 7519 freemsg(mp); 7520 return (NULL); 7521 } 7522 /* Drop any pad */ 7523 mp->b_wptr = rptr + pkt_len; 7524 } else if ((len += msgdsize(mp->b_cont)) != 0) { 7525 ASSERT(pkt_len >= min_size); 7526 if (pkt_len < min_size) { 7527 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7528 ip_drop_input("ipIfStatsInHdrErrors", mp, ill); 7529 freemsg(mp); 7530 return (NULL); 7531 } 7532 if (len < 0) { 7533 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts); 7534 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill); 7535 freemsg(mp); 7536 return (NULL); 7537 } 7538 /* Drop any pad */ 7539 (void) adjmsg(mp, -len); 7540 /* 7541 * adjmsg may have freed an mblk from the chain, hence 7542 * invalidate any hw checksum here. This will force IP to 7543 * calculate the checksum in sw, but only for this packet. 7544 */ 7545 DB_CKSUMFLAGS(mp) = 0; 7546 IP_STAT(ill->ill_ipst, ip_multimblk); 7547 } 7548 return (mp); 7549 } 7550 7551 /* 7552 * Check that the IPv4 opt_len is consistent with the packet and pullup 7553 * the options. 7554 */ 7555 mblk_t * 7556 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len, 7557 ip_recv_attr_t *ira) 7558 { 7559 ill_t *ill = ira->ira_ill; 7560 ssize_t len; 7561 7562 /* Assume no IPv6 packets arrive over the IPv4 queue */ 7563 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) { 7564 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7565 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); 7566 ip_drop_input("IPvN packet on IPv4 ill", mp, ill); 7567 freemsg(mp); 7568 return (NULL); 7569 } 7570 7571 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { 7572 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7573 ip_drop_input("ipIfStatsInHdrErrors", mp, ill); 7574 freemsg(mp); 7575 return (NULL); 7576 } 7577 /* 7578 * Recompute complete header length and make sure we 7579 * have access to all of it. 7580 */ 7581 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; 7582 if (len > (mp->b_wptr - mp->b_rptr)) { 7583 if (len > pkt_len) { 7584 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); 7585 ip_drop_input("ipIfStatsInHdrErrors", mp, ill); 7586 freemsg(mp); 7587 return (NULL); 7588 } 7589 if (ip_pullup(mp, len, ira) == NULL) { 7590 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 7591 ip_drop_input("ipIfStatsInDiscards", mp, ill); 7592 freemsg(mp); 7593 return (NULL); 7594 } 7595 } 7596 return (mp); 7597 } 7598 7599 /* 7600 * Returns a new ire, or the same ire, or NULL. 7601 * If a different IRE is returned, then it is held; the caller 7602 * needs to release it. 7603 * In no case is there any hold/release on the ire argument. 7604 */ 7605 ire_t * 7606 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill) 7607 { 7608 ire_t *new_ire; 7609 ill_t *ire_ill; 7610 uint_t ifindex; 7611 ip_stack_t *ipst = ill->ill_ipst; 7612 boolean_t strict_check = B_FALSE; 7613 7614 /* 7615 * IPMP common case: if IRE and ILL are in the same group, there's no 7616 * issue (e.g. packet received on an underlying interface matched an 7617 * IRE_LOCAL on its associated group interface). 7618 */ 7619 ASSERT(ire->ire_ill != NULL); 7620 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill)) 7621 return (ire); 7622 7623 /* 7624 * Do another ire lookup here, using the ingress ill, to see if the 7625 * interface is in a usesrc group. 7626 * As long as the ills belong to the same group, we don't consider 7627 * them to be arriving on the wrong interface. Thus, if the switch 7628 * is doing inbound load spreading, we won't drop packets when the 7629 * ip*_strict_dst_multihoming switch is on. 7630 * We also need to check for IPIF_UNNUMBERED point2point interfaces 7631 * where the local address may not be unique. In this case we were 7632 * at the mercy of the initial ire lookup and the IRE_LOCAL it 7633 * actually returned. The new lookup, which is more specific, should 7634 * only find the IRE_LOCAL associated with the ingress ill if one 7635 * exists. 7636 */ 7637 if (ire->ire_ipversion == IPV4_VERSION) { 7638 if (ipst->ips_ip_strict_dst_multihoming) 7639 strict_check = B_TRUE; 7640 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0, 7641 IRE_LOCAL, ill, ALL_ZONES, NULL, 7642 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL); 7643 } else { 7644 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr)); 7645 if (ipst->ips_ipv6_strict_dst_multihoming) 7646 strict_check = B_TRUE; 7647 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL, 7648 IRE_LOCAL, ill, ALL_ZONES, NULL, 7649 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL); 7650 } 7651 /* 7652 * If the same ire that was returned in ip_input() is found then this 7653 * is an indication that usesrc groups are in use. The packet 7654 * arrived on a different ill in the group than the one associated with 7655 * the destination address. If a different ire was found then the same 7656 * IP address must be hosted on multiple ills. This is possible with 7657 * unnumbered point2point interfaces. We switch to use this new ire in 7658 * order to have accurate interface statistics. 7659 */ 7660 if (new_ire != NULL) { 7661 /* Note: held in one case but not the other? Caller handles */ 7662 if (new_ire != ire) 7663 return (new_ire); 7664 /* Unchanged */ 7665 ire_refrele(new_ire); 7666 return (ire); 7667 } 7668 7669 /* 7670 * Chase pointers once and store locally. 7671 */ 7672 ASSERT(ire->ire_ill != NULL); 7673 ire_ill = ire->ire_ill; 7674 ifindex = ill->ill_usesrc_ifindex; 7675 7676 /* 7677 * Check if it's a legal address on the 'usesrc' interface. 7678 * For IPMP data addresses the IRE_LOCAL is the upper, hence we 7679 * can just check phyint_ifindex. 7680 */ 7681 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) { 7682 return (ire); 7683 } 7684 7685 /* 7686 * If the ip*_strict_dst_multihoming switch is on then we can 7687 * only accept this packet if the interface is marked as routing. 7688 */ 7689 if (!(strict_check)) 7690 return (ire); 7691 7692 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) { 7693 return (ire); 7694 } 7695 return (NULL); 7696 } 7697 7698 /* 7699 * This function is used to construct a mac_header_info_s from a 7700 * DL_UNITDATA_IND message. 7701 * The address fields in the mhi structure points into the message, 7702 * thus the caller can't use those fields after freeing the message. 7703 * 7704 * We determine whether the packet received is a non-unicast packet 7705 * and in doing so, determine whether or not it is broadcast vs multicast. 7706 * For it to be a broadcast packet, we must have the appropriate mblk_t 7707 * hanging off the ill_t. If this is either not present or doesn't match 7708 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed 7709 * to be multicast. Thus NICs that have no broadcast address (or no 7710 * capability for one, such as point to point links) cannot return as 7711 * the packet being broadcast. 7712 */ 7713 void 7714 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip) 7715 { 7716 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr; 7717 mblk_t *bmp; 7718 uint_t extra_offset; 7719 7720 bzero(mhip, sizeof (struct mac_header_info_s)); 7721 7722 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST; 7723 7724 if (ill->ill_sap_length < 0) 7725 extra_offset = 0; 7726 else 7727 extra_offset = ill->ill_sap_length; 7728 7729 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset + 7730 extra_offset; 7731 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset + 7732 extra_offset; 7733 7734 if (!ind->dl_group_address) 7735 return; 7736 7737 /* Multicast or broadcast */ 7738 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST; 7739 7740 if (ind->dl_dest_addr_offset > sizeof (*ind) && 7741 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) && 7742 (bmp = ill->ill_bcast_mp) != NULL) { 7743 dl_unitdata_req_t *dlur; 7744 uint8_t *bphys_addr; 7745 7746 dlur = (dl_unitdata_req_t *)bmp->b_rptr; 7747 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset + 7748 extra_offset; 7749 7750 if (bcmp(mhip->mhi_daddr, bphys_addr, 7751 ind->dl_dest_addr_length) == 0) 7752 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST; 7753 } 7754 } 7755 7756 /* 7757 * This function is used to construct a mac_header_info_s from a 7758 * M_DATA fastpath message from a DLPI driver. 7759 * The address fields in the mhi structure points into the message, 7760 * thus the caller can't use those fields after freeing the message. 7761 * 7762 * We determine whether the packet received is a non-unicast packet 7763 * and in doing so, determine whether or not it is broadcast vs multicast. 7764 * For it to be a broadcast packet, we must have the appropriate mblk_t 7765 * hanging off the ill_t. If this is either not present or doesn't match 7766 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed 7767 * to be multicast. Thus NICs that have no broadcast address (or no 7768 * capability for one, such as point to point links) cannot return as 7769 * the packet being broadcast. 7770 */ 7771 void 7772 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip) 7773 { 7774 mblk_t *bmp; 7775 struct ether_header *pether; 7776 7777 bzero(mhip, sizeof (struct mac_header_info_s)); 7778 7779 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST; 7780 7781 pether = (struct ether_header *)((char *)mp->b_rptr 7782 - sizeof (struct ether_header)); 7783 7784 /* 7785 * Make sure the interface is an ethernet type, since we don't 7786 * know the header format for anything but Ethernet. Also make 7787 * sure we are pointing correctly above db_base. 7788 */ 7789 if (ill->ill_type != IFT_ETHER) 7790 return; 7791 7792 retry: 7793 if ((uchar_t *)pether < mp->b_datap->db_base) 7794 return; 7795 7796 /* Is there a VLAN tag? */ 7797 if (ill->ill_isv6) { 7798 if (pether->ether_type != htons(ETHERTYPE_IPV6)) { 7799 pether = (struct ether_header *)((char *)pether - 4); 7800 goto retry; 7801 } 7802 } else { 7803 if (pether->ether_type != htons(ETHERTYPE_IP)) { 7804 pether = (struct ether_header *)((char *)pether - 4); 7805 goto retry; 7806 } 7807 } 7808 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost; 7809 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost; 7810 7811 if (!(mhip->mhi_daddr[0] & 0x01)) 7812 return; 7813 7814 /* Multicast or broadcast */ 7815 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST; 7816 7817 if ((bmp = ill->ill_bcast_mp) != NULL) { 7818 dl_unitdata_req_t *dlur; 7819 uint8_t *bphys_addr; 7820 uint_t addrlen; 7821 7822 dlur = (dl_unitdata_req_t *)bmp->b_rptr; 7823 addrlen = dlur->dl_dest_addr_length; 7824 if (ill->ill_sap_length < 0) { 7825 bphys_addr = (uchar_t *)dlur + 7826 dlur->dl_dest_addr_offset; 7827 addrlen += ill->ill_sap_length; 7828 } else { 7829 bphys_addr = (uchar_t *)dlur + 7830 dlur->dl_dest_addr_offset + 7831 ill->ill_sap_length; 7832 addrlen -= ill->ill_sap_length; 7833 } 7834 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0) 7835 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST; 7836 } 7837 } 7838 7839 /* 7840 * Handle anything but M_DATA messages 7841 * We see the DL_UNITDATA_IND which are part 7842 * of the data path, and also the other messages from the driver. 7843 */ 7844 void 7845 ip_rput_notdata(ill_t *ill, mblk_t *mp) 7846 { 7847 mblk_t *first_mp; 7848 struct iocblk *iocp; 7849 struct mac_header_info_s mhi; 7850 7851 switch (DB_TYPE(mp)) { 7852 case M_PROTO: 7853 case M_PCPROTO: { 7854 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive != 7855 DL_UNITDATA_IND) { 7856 /* Go handle anything other than data elsewhere. */ 7857 ip_rput_dlpi(ill, mp); 7858 return; 7859 } 7860 7861 first_mp = mp; 7862 mp = first_mp->b_cont; 7863 first_mp->b_cont = NULL; 7864 7865 if (mp == NULL) { 7866 freeb(first_mp); 7867 return; 7868 } 7869 ip_dlur_to_mhi(ill, first_mp, &mhi); 7870 if (ill->ill_isv6) 7871 ip_input_v6(ill, NULL, mp, &mhi); 7872 else 7873 ip_input(ill, NULL, mp, &mhi); 7874 7875 /* Ditch the DLPI header. */ 7876 freeb(first_mp); 7877 return; 7878 } 7879 case M_IOCACK: 7880 iocp = (struct iocblk *)mp->b_rptr; 7881 switch (iocp->ioc_cmd) { 7882 case DL_IOC_HDR_INFO: 7883 ill_fastpath_ack(ill, mp); 7884 return; 7885 default: 7886 putnext(ill->ill_rq, mp); 7887 return; 7888 } 7889 /* FALLTHRU */ 7890 case M_ERROR: 7891 case M_HANGUP: 7892 mutex_enter(&ill->ill_lock); 7893 if (ill->ill_state_flags & ILL_CONDEMNED) { 7894 mutex_exit(&ill->ill_lock); 7895 freemsg(mp); 7896 return; 7897 } 7898 ill_refhold_locked(ill); 7899 mutex_exit(&ill->ill_lock); 7900 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP, 7901 B_FALSE); 7902 return; 7903 case M_CTL: 7904 putnext(ill->ill_rq, mp); 7905 return; 7906 case M_IOCNAK: 7907 ip1dbg(("got iocnak ")); 7908 iocp = (struct iocblk *)mp->b_rptr; 7909 switch (iocp->ioc_cmd) { 7910 case DL_IOC_HDR_INFO: 7911 ip_rput_other(NULL, ill->ill_rq, mp, NULL); 7912 return; 7913 default: 7914 break; 7915 } 7916 /* FALLTHRU */ 7917 default: 7918 putnext(ill->ill_rq, mp); 7919 return; 7920 } 7921 } 7922 7923 /* Read side put procedure. Packets coming from the wire arrive here. */ 7924 void 7925 ip_rput(queue_t *q, mblk_t *mp) 7926 { 7927 ill_t *ill; 7928 union DL_primitives *dl; 7929 7930 ill = (ill_t *)q->q_ptr; 7931 7932 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { 7933 /* 7934 * If things are opening or closing, only accept high-priority 7935 * DLPI messages. (On open ill->ill_ipif has not yet been 7936 * created; on close, things hanging off the ill may have been 7937 * freed already.) 7938 */ 7939 dl = (union DL_primitives *)mp->b_rptr; 7940 if (DB_TYPE(mp) != M_PCPROTO || 7941 dl->dl_primitive == DL_UNITDATA_IND) { 7942 inet_freemsg(mp); 7943 return; 7944 } 7945 } 7946 if (DB_TYPE(mp) == M_DATA) { 7947 struct mac_header_info_s mhi; 7948 7949 ip_mdata_to_mhi(ill, mp, &mhi); 7950 ip_input(ill, NULL, mp, &mhi); 7951 } else { 7952 ip_rput_notdata(ill, mp); 7953 } 7954 } 7955 7956 /* 7957 * Move the information to a copy. 7958 */ 7959 mblk_t * 7960 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira) 7961 { 7962 mblk_t *mp1; 7963 ill_t *ill = ira->ira_ill; 7964 ip_stack_t *ipst = ill->ill_ipst; 7965 7966 IP_STAT(ipst, ip_db_ref); 7967 7968 /* Make sure we have ira_l2src before we loose the original mblk */ 7969 if (!(ira->ira_flags & IRAF_L2SRC_SET)) 7970 ip_setl2src(mp, ira, ira->ira_rill); 7971 7972 mp1 = copymsg(mp); 7973 if (mp1 == NULL) { 7974 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 7975 ip_drop_input("ipIfStatsInDiscards", mp, ill); 7976 freemsg(mp); 7977 return (NULL); 7978 } 7979 /* preserve the hardware checksum flags and data, if present */ 7980 if (DB_CKSUMFLAGS(mp) != 0) { 7981 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp); 7982 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp); 7983 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp); 7984 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp); 7985 DB_CKSUM16(mp1) = DB_CKSUM16(mp); 7986 } 7987 freemsg(mp); 7988 return (mp1); 7989 } 7990 7991 static void 7992 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, 7993 t_uscalar_t err) 7994 { 7995 if (dl_err == DL_SYSERR) { 7996 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 7997 "%s: %s failed: DL_SYSERR (errno %u)\n", 7998 ill->ill_name, dl_primstr(prim), err); 7999 return; 8000 } 8001 8002 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, 8003 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim), 8004 dl_errstr(dl_err)); 8005 } 8006 8007 /* 8008 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other 8009 * than DL_UNITDATA_IND messages. If we need to process this message 8010 * exclusively, we call qwriter_ip, in which case we also need to call 8011 * ill_refhold before that, since qwriter_ip does an ill_refrele. 8012 */ 8013 void 8014 ip_rput_dlpi(ill_t *ill, mblk_t *mp) 8015 { 8016 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 8017 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 8018 queue_t *q = ill->ill_rq; 8019 t_uscalar_t prim = dloa->dl_primitive; 8020 t_uscalar_t reqprim = DL_PRIM_INVAL; 8021 8022 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi", 8023 char *, dl_primstr(prim), ill_t *, ill); 8024 ip1dbg(("ip_rput_dlpi")); 8025 8026 /* 8027 * If we received an ACK but didn't send a request for it, then it 8028 * can't be part of any pending operation; discard up-front. 8029 */ 8030 switch (prim) { 8031 case DL_ERROR_ACK: 8032 reqprim = dlea->dl_error_primitive; 8033 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s " 8034 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim), 8035 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno, 8036 dlea->dl_unix_errno)); 8037 break; 8038 case DL_OK_ACK: 8039 reqprim = dloa->dl_correct_primitive; 8040 break; 8041 case DL_INFO_ACK: 8042 reqprim = DL_INFO_REQ; 8043 break; 8044 case DL_BIND_ACK: 8045 reqprim = DL_BIND_REQ; 8046 break; 8047 case DL_PHYS_ADDR_ACK: 8048 reqprim = DL_PHYS_ADDR_REQ; 8049 break; 8050 case DL_NOTIFY_ACK: 8051 reqprim = DL_NOTIFY_REQ; 8052 break; 8053 case DL_CAPABILITY_ACK: 8054 reqprim = DL_CAPABILITY_REQ; 8055 break; 8056 } 8057 8058 if (prim != DL_NOTIFY_IND) { 8059 if (reqprim == DL_PRIM_INVAL || 8060 !ill_dlpi_pending(ill, reqprim)) { 8061 /* Not a DLPI message we support or expected */ 8062 freemsg(mp); 8063 return; 8064 } 8065 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim), 8066 dl_primstr(reqprim))); 8067 } 8068 8069 switch (reqprim) { 8070 case DL_UNBIND_REQ: 8071 /* 8072 * NOTE: we mark the unbind as complete even if we got a 8073 * DL_ERROR_ACK, since there's not much else we can do. 8074 */ 8075 mutex_enter(&ill->ill_lock); 8076 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 8077 cv_signal(&ill->ill_cv); 8078 mutex_exit(&ill->ill_lock); 8079 break; 8080 8081 case DL_ENABMULTI_REQ: 8082 if (prim == DL_OK_ACK) { 8083 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 8084 ill->ill_dlpi_multicast_state = IDS_OK; 8085 } 8086 break; 8087 } 8088 8089 /* 8090 * The message is one we're waiting for (or DL_NOTIFY_IND), but we 8091 * need to become writer to continue to process it. Because an 8092 * exclusive operation doesn't complete until replies to all queued 8093 * DLPI messages have been received, we know we're in the middle of an 8094 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND). 8095 * 8096 * As required by qwriter_ip(), we refhold the ill; it will refrele. 8097 * Since this is on the ill stream we unconditionally bump up the 8098 * refcount without doing ILL_CAN_LOOKUP(). 8099 */ 8100 ill_refhold(ill); 8101 if (prim == DL_NOTIFY_IND) 8102 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE); 8103 else 8104 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE); 8105 } 8106 8107 /* 8108 * Handling of DLPI messages that require exclusive access to the ipsq. 8109 * 8110 * Need to do ipsq_pending_mp_get on ioctl completion, which could 8111 * happen here. (along with mi_copy_done) 8112 */ 8113 /* ARGSUSED */ 8114 static void 8115 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 8116 { 8117 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; 8118 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; 8119 int err = 0; 8120 ill_t *ill = (ill_t *)q->q_ptr; 8121 ipif_t *ipif = NULL; 8122 mblk_t *mp1 = NULL; 8123 conn_t *connp = NULL; 8124 t_uscalar_t paddrreq; 8125 mblk_t *mp_hw; 8126 boolean_t success; 8127 boolean_t ioctl_aborted = B_FALSE; 8128 boolean_t log = B_TRUE; 8129 8130 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer", 8131 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill); 8132 8133 ip1dbg(("ip_rput_dlpi_writer ..")); 8134 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop); 8135 ASSERT(IAM_WRITER_ILL(ill)); 8136 8137 ipif = ipsq->ipsq_xop->ipx_pending_ipif; 8138 /* 8139 * The current ioctl could have been aborted by the user and a new 8140 * ioctl to bring up another ill could have started. We could still 8141 * get a response from the driver later. 8142 */ 8143 if (ipif != NULL && ipif->ipif_ill != ill) 8144 ioctl_aborted = B_TRUE; 8145 8146 switch (dloa->dl_primitive) { 8147 case DL_ERROR_ACK: 8148 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n", 8149 dl_primstr(dlea->dl_error_primitive))); 8150 8151 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error", 8152 char *, dl_primstr(dlea->dl_error_primitive), 8153 ill_t *, ill); 8154 8155 switch (dlea->dl_error_primitive) { 8156 case DL_DISABMULTI_REQ: 8157 ill_dlpi_done(ill, dlea->dl_error_primitive); 8158 break; 8159 case DL_PROMISCON_REQ: 8160 case DL_PROMISCOFF_REQ: 8161 case DL_UNBIND_REQ: 8162 case DL_ATTACH_REQ: 8163 case DL_INFO_REQ: 8164 ill_dlpi_done(ill, dlea->dl_error_primitive); 8165 break; 8166 case DL_NOTIFY_REQ: 8167 ill_dlpi_done(ill, DL_NOTIFY_REQ); 8168 log = B_FALSE; 8169 break; 8170 case DL_PHYS_ADDR_REQ: 8171 /* 8172 * For IPv6 only, there are two additional 8173 * phys_addr_req's sent to the driver to get the 8174 * IPv6 token and lla. This allows IP to acquire 8175 * the hardware address format for a given interface 8176 * without having built in knowledge of the hardware 8177 * address. ill_phys_addr_pend keeps track of the last 8178 * DL_PAR sent so we know which response we are 8179 * dealing with. ill_dlpi_done will update 8180 * ill_phys_addr_pend when it sends the next req. 8181 * We don't complete the IOCTL until all three DL_PARs 8182 * have been attempted, so set *_len to 0 and break. 8183 */ 8184 paddrreq = ill->ill_phys_addr_pend; 8185 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 8186 if (paddrreq == DL_IPV6_TOKEN) { 8187 ill->ill_token_length = 0; 8188 log = B_FALSE; 8189 break; 8190 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 8191 ill->ill_nd_lla_len = 0; 8192 log = B_FALSE; 8193 break; 8194 } 8195 /* 8196 * Something went wrong with the DL_PHYS_ADDR_REQ. 8197 * We presumably have an IOCTL hanging out waiting 8198 * for completion. Find it and complete the IOCTL 8199 * with the error noted. 8200 * However, ill_dl_phys was called on an ill queue 8201 * (from SIOCSLIFNAME), thus conn_pending_ill is not 8202 * set. But the ioctl is known to be pending on ill_wq. 8203 */ 8204 if (!ill->ill_ifname_pending) 8205 break; 8206 ill->ill_ifname_pending = 0; 8207 if (!ioctl_aborted) 8208 mp1 = ipsq_pending_mp_get(ipsq, &connp); 8209 if (mp1 != NULL) { 8210 /* 8211 * This operation (SIOCSLIFNAME) must have 8212 * happened on the ill. Assert there is no conn 8213 */ 8214 ASSERT(connp == NULL); 8215 q = ill->ill_wq; 8216 } 8217 break; 8218 case DL_BIND_REQ: 8219 ill_dlpi_done(ill, DL_BIND_REQ); 8220 if (ill->ill_ifname_pending) 8221 break; 8222 /* 8223 * Something went wrong with the bind. We presumably 8224 * have an IOCTL hanging out waiting for completion. 8225 * Find it, take down the interface that was coming 8226 * up, and complete the IOCTL with the error noted. 8227 */ 8228 if (!ioctl_aborted) 8229 mp1 = ipsq_pending_mp_get(ipsq, &connp); 8230 if (mp1 != NULL) { 8231 /* 8232 * This might be a result of a DL_NOTE_REPLUMB 8233 * notification. In that case, connp is NULL. 8234 */ 8235 if (connp != NULL) 8236 q = CONNP_TO_WQ(connp); 8237 8238 (void) ipif_down(ipif, NULL, NULL); 8239 /* error is set below the switch */ 8240 } 8241 break; 8242 case DL_ENABMULTI_REQ: 8243 ill_dlpi_done(ill, DL_ENABMULTI_REQ); 8244 8245 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) 8246 ill->ill_dlpi_multicast_state = IDS_FAILED; 8247 if (ill->ill_dlpi_multicast_state == IDS_FAILED) { 8248 8249 printf("ip: joining multicasts failed (%d)" 8250 " on %s - will use link layer " 8251 "broadcasts for multicast\n", 8252 dlea->dl_errno, ill->ill_name); 8253 8254 /* 8255 * Set up for multi_bcast; We are the 8256 * writer, so ok to access ill->ill_ipif 8257 * without any lock. 8258 */ 8259 mutex_enter(&ill->ill_phyint->phyint_lock); 8260 ill->ill_phyint->phyint_flags |= 8261 PHYI_MULTI_BCAST; 8262 mutex_exit(&ill->ill_phyint->phyint_lock); 8263 8264 } 8265 freemsg(mp); /* Don't want to pass this up */ 8266 return; 8267 case DL_CAPABILITY_REQ: 8268 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for " 8269 "DL_CAPABILITY REQ\n")); 8270 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT) 8271 ill->ill_dlpi_capab_state = IDCS_FAILED; 8272 ill_capability_done(ill); 8273 freemsg(mp); 8274 return; 8275 } 8276 /* 8277 * Note the error for IOCTL completion (mp1 is set when 8278 * ready to complete ioctl). If ill_ifname_pending_err is 8279 * set, an error occured during plumbing (ill_ifname_pending), 8280 * so we want to report that error. 8281 * 8282 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's 8283 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are 8284 * expected to get errack'd if the driver doesn't support 8285 * these flags (e.g. ethernet). log will be set to B_FALSE 8286 * if these error conditions are encountered. 8287 */ 8288 if (mp1 != NULL) { 8289 if (ill->ill_ifname_pending_err != 0) { 8290 err = ill->ill_ifname_pending_err; 8291 ill->ill_ifname_pending_err = 0; 8292 } else { 8293 err = dlea->dl_unix_errno ? 8294 dlea->dl_unix_errno : ENXIO; 8295 } 8296 /* 8297 * If we're plumbing an interface and an error hasn't already 8298 * been saved, set ill_ifname_pending_err to the error passed 8299 * up. Ignore the error if log is B_FALSE (see comment above). 8300 */ 8301 } else if (log && ill->ill_ifname_pending && 8302 ill->ill_ifname_pending_err == 0) { 8303 ill->ill_ifname_pending_err = dlea->dl_unix_errno ? 8304 dlea->dl_unix_errno : ENXIO; 8305 } 8306 8307 if (log) 8308 ip_dlpi_error(ill, dlea->dl_error_primitive, 8309 dlea->dl_errno, dlea->dl_unix_errno); 8310 break; 8311 case DL_CAPABILITY_ACK: 8312 ill_capability_ack(ill, mp); 8313 /* 8314 * The message has been handed off to ill_capability_ack 8315 * and must not be freed below 8316 */ 8317 mp = NULL; 8318 break; 8319 8320 case DL_INFO_ACK: 8321 /* Call a routine to handle this one. */ 8322 ill_dlpi_done(ill, DL_INFO_REQ); 8323 ip_ll_subnet_defaults(ill, mp); 8324 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); 8325 return; 8326 case DL_BIND_ACK: 8327 /* 8328 * We should have an IOCTL waiting on this unless 8329 * sent by ill_dl_phys, in which case just return 8330 */ 8331 ill_dlpi_done(ill, DL_BIND_REQ); 8332 if (ill->ill_ifname_pending) { 8333 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending, 8334 ill_t *, ill, mblk_t *, mp); 8335 break; 8336 } 8337 if (!ioctl_aborted) 8338 mp1 = ipsq_pending_mp_get(ipsq, &connp); 8339 if (mp1 == NULL) { 8340 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill); 8341 break; 8342 } 8343 /* 8344 * mp1 was added by ill_dl_up(). if that is a result of 8345 * a DL_NOTE_REPLUMB notification, connp could be NULL. 8346 */ 8347 if (connp != NULL) 8348 q = CONNP_TO_WQ(connp); 8349 /* 8350 * We are exclusive. So nothing can change even after 8351 * we get the pending mp. 8352 */ 8353 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); 8354 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill); 8355 8356 mutex_enter(&ill->ill_lock); 8357 ill->ill_dl_up = 1; 8358 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS; 8359 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0); 8360 mutex_exit(&ill->ill_lock); 8361 8362 /* 8363 * Now bring up the resolver; when that is complete, we'll 8364 * create IREs. Note that we intentionally mirror what 8365 * ipif_up() would have done, because we got here by way of 8366 * ill_dl_up(), which stopped ipif_up()'s processing. 8367 */ 8368 if (ill->ill_isv6) { 8369 /* 8370 * v6 interfaces. 8371 * Unlike ARP which has to do another bind 8372 * and attach, once we get here we are 8373 * done with NDP 8374 */ 8375 (void) ipif_resolver_up(ipif, Res_act_initial); 8376 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0) 8377 err = ipif_up_done_v6(ipif); 8378 } else if (ill->ill_net_type == IRE_IF_RESOLVER) { 8379 /* 8380 * ARP and other v4 external resolvers. 8381 * Leave the pending mblk intact so that 8382 * the ioctl completes in ip_rput(). 8383 */ 8384 if (connp != NULL) 8385 mutex_enter(&connp->conn_lock); 8386 mutex_enter(&ill->ill_lock); 8387 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); 8388 mutex_exit(&ill->ill_lock); 8389 if (connp != NULL) 8390 mutex_exit(&connp->conn_lock); 8391 if (success) { 8392 err = ipif_resolver_up(ipif, Res_act_initial); 8393 if (err == EINPROGRESS) { 8394 freemsg(mp); 8395 return; 8396 } 8397 mp1 = ipsq_pending_mp_get(ipsq, &connp); 8398 } else { 8399 /* The conn has started closing */ 8400 err = EINTR; 8401 } 8402 } else { 8403 /* 8404 * This one is complete. Reply to pending ioctl. 8405 */ 8406 (void) ipif_resolver_up(ipif, Res_act_initial); 8407 err = ipif_up_done(ipif); 8408 } 8409 8410 if ((err == 0) && (ill->ill_up_ipifs)) { 8411 err = ill_up_ipifs(ill, q, mp1); 8412 if (err == EINPROGRESS) { 8413 freemsg(mp); 8414 return; 8415 } 8416 } 8417 8418 /* 8419 * If we have a moved ipif to bring up, and everything has 8420 * succeeded to this point, bring it up on the IPMP ill. 8421 * Otherwise, leave it down -- the admin can try to bring it 8422 * up by hand if need be. 8423 */ 8424 if (ill->ill_move_ipif != NULL) { 8425 if (err != 0) { 8426 ill->ill_move_ipif = NULL; 8427 } else { 8428 ipif = ill->ill_move_ipif; 8429 ill->ill_move_ipif = NULL; 8430 err = ipif_up(ipif, q, mp1); 8431 if (err == EINPROGRESS) { 8432 freemsg(mp); 8433 return; 8434 } 8435 } 8436 } 8437 break; 8438 8439 case DL_NOTIFY_IND: { 8440 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; 8441 uint_t orig_mtu; 8442 8443 switch (notify->dl_notification) { 8444 case DL_NOTE_PHYS_ADDR: 8445 err = ill_set_phys_addr(ill, mp); 8446 break; 8447 8448 case DL_NOTE_REPLUMB: 8449 /* 8450 * Directly return after calling ill_replumb(). 8451 * Note that we should not free mp as it is reused 8452 * in the ill_replumb() function. 8453 */ 8454 err = ill_replumb(ill, mp); 8455 return; 8456 8457 case DL_NOTE_FASTPATH_FLUSH: 8458 nce_flush(ill, B_FALSE); 8459 break; 8460 8461 case DL_NOTE_SDU_SIZE: 8462 /* 8463 * The dce and fragmentation code can cope with 8464 * this changing while packets are being sent. 8465 * When packets are sent ip_output will discover 8466 * a change. 8467 * 8468 * Change the MTU size of the interface. 8469 */ 8470 mutex_enter(&ill->ill_lock); 8471 ill->ill_current_frag = (uint_t)notify->dl_data; 8472 if (ill->ill_current_frag > ill->ill_max_frag) 8473 ill->ill_max_frag = ill->ill_current_frag; 8474 8475 orig_mtu = ill->ill_mtu; 8476 if (!(ill->ill_flags & ILLF_FIXEDMTU)) { 8477 ill->ill_mtu = ill->ill_current_frag; 8478 8479 /* 8480 * If ill_user_mtu was set (via 8481 * SIOCSLIFLNKINFO), clamp ill_mtu at it. 8482 */ 8483 if (ill->ill_user_mtu != 0 && 8484 ill->ill_user_mtu < ill->ill_mtu) 8485 ill->ill_mtu = ill->ill_user_mtu; 8486 8487 if (ill->ill_isv6) { 8488 if (ill->ill_mtu < IPV6_MIN_MTU) 8489 ill->ill_mtu = IPV6_MIN_MTU; 8490 } else { 8491 if (ill->ill_mtu < IP_MIN_MTU) 8492 ill->ill_mtu = IP_MIN_MTU; 8493 } 8494 } 8495 mutex_exit(&ill->ill_lock); 8496 /* 8497 * Make sure all dce_generation checks find out 8498 * that ill_mtu has changed. 8499 */ 8500 if (orig_mtu != ill->ill_mtu) { 8501 dce_increment_all_generations(ill->ill_isv6, 8502 ill->ill_ipst); 8503 } 8504 8505 /* 8506 * Refresh IPMP meta-interface MTU if necessary. 8507 */ 8508 if (IS_UNDER_IPMP(ill)) 8509 ipmp_illgrp_refresh_mtu(ill->ill_grp); 8510 break; 8511 8512 case DL_NOTE_LINK_UP: 8513 case DL_NOTE_LINK_DOWN: { 8514 /* 8515 * We are writer. ill / phyint / ipsq assocs stable. 8516 * The RUNNING flag reflects the state of the link. 8517 */ 8518 phyint_t *phyint = ill->ill_phyint; 8519 uint64_t new_phyint_flags; 8520 boolean_t changed = B_FALSE; 8521 boolean_t went_up; 8522 8523 went_up = notify->dl_notification == DL_NOTE_LINK_UP; 8524 mutex_enter(&phyint->phyint_lock); 8525 8526 new_phyint_flags = went_up ? 8527 phyint->phyint_flags | PHYI_RUNNING : 8528 phyint->phyint_flags & ~PHYI_RUNNING; 8529 8530 if (IS_IPMP(ill)) { 8531 new_phyint_flags = went_up ? 8532 new_phyint_flags & ~PHYI_FAILED : 8533 new_phyint_flags | PHYI_FAILED; 8534 } 8535 8536 if (new_phyint_flags != phyint->phyint_flags) { 8537 phyint->phyint_flags = new_phyint_flags; 8538 changed = B_TRUE; 8539 } 8540 mutex_exit(&phyint->phyint_lock); 8541 /* 8542 * ill_restart_dad handles the DAD restart and routing 8543 * socket notification logic. 8544 */ 8545 if (changed) { 8546 ill_restart_dad(phyint->phyint_illv4, went_up); 8547 ill_restart_dad(phyint->phyint_illv6, went_up); 8548 } 8549 break; 8550 } 8551 case DL_NOTE_PROMISC_ON_PHYS: { 8552 phyint_t *phyint = ill->ill_phyint; 8553 8554 mutex_enter(&phyint->phyint_lock); 8555 phyint->phyint_flags |= PHYI_PROMISC; 8556 mutex_exit(&phyint->phyint_lock); 8557 break; 8558 } 8559 case DL_NOTE_PROMISC_OFF_PHYS: { 8560 phyint_t *phyint = ill->ill_phyint; 8561 8562 mutex_enter(&phyint->phyint_lock); 8563 phyint->phyint_flags &= ~PHYI_PROMISC; 8564 mutex_exit(&phyint->phyint_lock); 8565 break; 8566 } 8567 case DL_NOTE_CAPAB_RENEG: 8568 /* 8569 * Something changed on the driver side. 8570 * It wants us to renegotiate the capabilities 8571 * on this ill. One possible cause is the aggregation 8572 * interface under us where a port got added or 8573 * went away. 8574 * 8575 * If the capability negotiation is already done 8576 * or is in progress, reset the capabilities and 8577 * mark the ill's ill_capab_reneg to be B_TRUE, 8578 * so that when the ack comes back, we can start 8579 * the renegotiation process. 8580 * 8581 * Note that if ill_capab_reneg is already B_TRUE 8582 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case), 8583 * the capability resetting request has been sent 8584 * and the renegotiation has not been started yet; 8585 * nothing needs to be done in this case. 8586 */ 8587 ipsq_current_start(ipsq, ill->ill_ipif, 0); 8588 ill_capability_reset(ill, B_TRUE); 8589 ipsq_current_finish(ipsq); 8590 break; 8591 default: 8592 ip0dbg(("ip_rput_dlpi_writer: unknown notification " 8593 "type 0x%x for DL_NOTIFY_IND\n", 8594 notify->dl_notification)); 8595 break; 8596 } 8597 8598 /* 8599 * As this is an asynchronous operation, we 8600 * should not call ill_dlpi_done 8601 */ 8602 break; 8603 } 8604 case DL_NOTIFY_ACK: { 8605 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; 8606 8607 if (noteack->dl_notifications & DL_NOTE_LINK_UP) 8608 ill->ill_note_link = 1; 8609 ill_dlpi_done(ill, DL_NOTIFY_REQ); 8610 break; 8611 } 8612 case DL_PHYS_ADDR_ACK: { 8613 /* 8614 * As part of plumbing the interface via SIOCSLIFNAME, 8615 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, 8616 * whose answers we receive here. As each answer is received, 8617 * we call ill_dlpi_done() to dispatch the next request as 8618 * we're processing the current one. Once all answers have 8619 * been received, we use ipsq_pending_mp_get() to dequeue the 8620 * outstanding IOCTL and reply to it. (Because ill_dl_phys() 8621 * is invoked from an ill queue, conn_oper_pending_ill is not 8622 * available, but we know the ioctl is pending on ill_wq.) 8623 */ 8624 uint_t paddrlen, paddroff; 8625 uint8_t *addr; 8626 8627 paddrreq = ill->ill_phys_addr_pend; 8628 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; 8629 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; 8630 addr = mp->b_rptr + paddroff; 8631 8632 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); 8633 if (paddrreq == DL_IPV6_TOKEN) { 8634 /* 8635 * bcopy to low-order bits of ill_token 8636 * 8637 * XXX Temporary hack - currently, all known tokens 8638 * are 64 bits, so I'll cheat for the moment. 8639 */ 8640 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen); 8641 ill->ill_token_length = paddrlen; 8642 break; 8643 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { 8644 ASSERT(ill->ill_nd_lla_mp == NULL); 8645 ill_set_ndmp(ill, mp, paddroff, paddrlen); 8646 mp = NULL; 8647 break; 8648 } else if (paddrreq == DL_CURR_DEST_ADDR) { 8649 ASSERT(ill->ill_dest_addr_mp == NULL); 8650 ill->ill_dest_addr_mp = mp; 8651 ill->ill_dest_addr = addr; 8652 mp = NULL; 8653 if (ill->ill_isv6) { 8654 ill_setdesttoken(ill); 8655 ipif_setdestlinklocal(ill->ill_ipif); 8656 } 8657 break; 8658 } 8659 8660 ASSERT(paddrreq == DL_CURR_PHYS_ADDR); 8661 ASSERT(ill->ill_phys_addr_mp == NULL); 8662 if (!ill->ill_ifname_pending) 8663 break; 8664 ill->ill_ifname_pending = 0; 8665 if (!ioctl_aborted) 8666 mp1 = ipsq_pending_mp_get(ipsq, &connp); 8667 if (mp1 != NULL) { 8668 ASSERT(connp == NULL); 8669 q = ill->ill_wq; 8670 } 8671 /* 8672 * If any error acks received during the plumbing sequence, 8673 * ill_ifname_pending_err will be set. Break out and send up 8674 * the error to the pending ioctl. 8675 */ 8676 if (ill->ill_ifname_pending_err != 0) { 8677 err = ill->ill_ifname_pending_err; 8678 ill->ill_ifname_pending_err = 0; 8679 break; 8680 } 8681 8682 ill->ill_phys_addr_mp = mp; 8683 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr); 8684 mp = NULL; 8685 8686 /* 8687 * If paddrlen or ill_phys_addr_length is zero, the DLPI 8688 * provider doesn't support physical addresses. We check both 8689 * paddrlen and ill_phys_addr_length because sppp (PPP) does 8690 * not have physical addresses, but historically adversises a 8691 * physical address length of 0 in its DL_INFO_ACK, but 6 in 8692 * its DL_PHYS_ADDR_ACK. 8693 */ 8694 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) { 8695 ill->ill_phys_addr = NULL; 8696 } else if (paddrlen != ill->ill_phys_addr_length) { 8697 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", 8698 paddrlen, ill->ill_phys_addr_length)); 8699 err = EINVAL; 8700 break; 8701 } 8702 8703 if (ill->ill_nd_lla_mp == NULL) { 8704 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { 8705 err = ENOMEM; 8706 break; 8707 } 8708 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); 8709 } 8710 8711 if (ill->ill_isv6) { 8712 ill_setdefaulttoken(ill); 8713 ipif_setlinklocal(ill->ill_ipif); 8714 } 8715 break; 8716 } 8717 case DL_OK_ACK: 8718 ip2dbg(("DL_OK_ACK %s (0x%x)\n", 8719 dl_primstr((int)dloa->dl_correct_primitive), 8720 dloa->dl_correct_primitive)); 8721 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok", 8722 char *, dl_primstr(dloa->dl_correct_primitive), 8723 ill_t *, ill); 8724 8725 switch (dloa->dl_correct_primitive) { 8726 case DL_ENABMULTI_REQ: 8727 case DL_DISABMULTI_REQ: 8728 ill_dlpi_done(ill, dloa->dl_correct_primitive); 8729 break; 8730 case DL_PROMISCON_REQ: 8731 case DL_PROMISCOFF_REQ: 8732 case DL_UNBIND_REQ: 8733 case DL_ATTACH_REQ: 8734 ill_dlpi_done(ill, dloa->dl_correct_primitive); 8735 break; 8736 } 8737 break; 8738 default: 8739 break; 8740 } 8741 8742 freemsg(mp); 8743 if (mp1 == NULL) 8744 return; 8745 8746 /* 8747 * The operation must complete without EINPROGRESS since 8748 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise, 8749 * the operation will be stuck forever inside the IPSQ. 8750 */ 8751 ASSERT(err != EINPROGRESS); 8752 8753 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish", 8754 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill, 8755 ipif_t *, NULL); 8756 8757 switch (ipsq->ipsq_xop->ipx_current_ioctl) { 8758 case 0: 8759 ipsq_current_finish(ipsq); 8760 break; 8761 8762 case SIOCSLIFNAME: 8763 case IF_UNITSEL: { 8764 ill_t *ill_other = ILL_OTHER(ill); 8765 8766 /* 8767 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the 8768 * ill has a peer which is in an IPMP group, then place ill 8769 * into the same group. One catch: although ifconfig plumbs 8770 * the appropriate IPMP meta-interface prior to plumbing this 8771 * ill, it is possible for multiple ifconfig applications to 8772 * race (or for another application to adjust plumbing), in 8773 * which case the IPMP meta-interface we need will be missing. 8774 * If so, kick the phyint out of the group. 8775 */ 8776 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) { 8777 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp; 8778 ipmp_illgrp_t *illg; 8779 8780 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4; 8781 if (illg == NULL) 8782 ipmp_phyint_leave_grp(ill->ill_phyint); 8783 else 8784 ipmp_ill_join_illgrp(ill, illg); 8785 } 8786 8787 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL) 8788 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 8789 else 8790 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 8791 break; 8792 } 8793 case SIOCLIFADDIF: 8794 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); 8795 break; 8796 8797 default: 8798 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); 8799 break; 8800 } 8801 } 8802 8803 /* 8804 * ip_rput_other is called by ip_rput to handle messages modifying the global 8805 * state in IP. If 'ipsq' is non-NULL, caller is writer on it. 8806 */ 8807 /* ARGSUSED */ 8808 void 8809 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 8810 { 8811 ill_t *ill = q->q_ptr; 8812 struct iocblk *iocp; 8813 8814 ip1dbg(("ip_rput_other ")); 8815 if (ipsq != NULL) { 8816 ASSERT(IAM_WRITER_IPSQ(ipsq)); 8817 ASSERT(ipsq->ipsq_xop == 8818 ill->ill_phyint->phyint_ipsq->ipsq_xop); 8819 } 8820 8821 switch (mp->b_datap->db_type) { 8822 case M_ERROR: 8823 case M_HANGUP: 8824 /* 8825 * The device has a problem. We force the ILL down. It can 8826 * be brought up again manually using SIOCSIFFLAGS (via 8827 * ifconfig or equivalent). 8828 */ 8829 ASSERT(ipsq != NULL); 8830 if (mp->b_rptr < mp->b_wptr) 8831 ill->ill_error = (int)(*mp->b_rptr & 0xFF); 8832 if (ill->ill_error == 0) 8833 ill->ill_error = ENXIO; 8834 if (!ill_down_start(q, mp)) 8835 return; 8836 ipif_all_down_tail(ipsq, q, mp, NULL); 8837 break; 8838 case M_IOCNAK: { 8839 iocp = (struct iocblk *)mp->b_rptr; 8840 8841 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO); 8842 /* 8843 * If this was the first attempt, turn off the fastpath 8844 * probing. 8845 */ 8846 mutex_enter(&ill->ill_lock); 8847 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { 8848 ill->ill_dlpi_fastpath_state = IDS_FAILED; 8849 mutex_exit(&ill->ill_lock); 8850 /* 8851 * don't flush the nce_t entries: we use them 8852 * as an index to the ncec itself. 8853 */ 8854 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n", 8855 ill->ill_name)); 8856 } else { 8857 mutex_exit(&ill->ill_lock); 8858 } 8859 freemsg(mp); 8860 break; 8861 } 8862 default: 8863 ASSERT(0); 8864 break; 8865 } 8866 } 8867 8868 /* 8869 * Update any source route, record route or timestamp options 8870 * When it fails it has consumed the message and BUMPed the MIB. 8871 */ 8872 boolean_t 8873 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill, 8874 ip_recv_attr_t *ira) 8875 { 8876 ipoptp_t opts; 8877 uchar_t *opt; 8878 uint8_t optval; 8879 uint8_t optlen; 8880 ipaddr_t dst; 8881 ipaddr_t ifaddr; 8882 uint32_t ts; 8883 timestruc_t now; 8884 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 8885 8886 ip2dbg(("ip_forward_options\n")); 8887 dst = ipha->ipha_dst; 8888 for (optval = ipoptp_first(&opts, ipha); 8889 optval != IPOPT_EOL; 8890 optval = ipoptp_next(&opts)) { 8891 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 8892 opt = opts.ipoptp_cur; 8893 optlen = opts.ipoptp_len; 8894 ip2dbg(("ip_forward_options: opt %d, len %d\n", 8895 optval, opts.ipoptp_len)); 8896 switch (optval) { 8897 uint32_t off; 8898 case IPOPT_SSRR: 8899 case IPOPT_LSRR: 8900 /* Check if adminstratively disabled */ 8901 if (!ipst->ips_ip_forward_src_routed) { 8902 BUMP_MIB(dst_ill->ill_ip_mib, 8903 ipIfStatsForwProhibits); 8904 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", 8905 mp, dst_ill); 8906 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, 8907 ira); 8908 return (B_FALSE); 8909 } 8910 if (ip_type_v4(dst, ipst) != IRE_LOCAL) { 8911 /* 8912 * Must be partial since ip_input_options 8913 * checked for strict. 8914 */ 8915 break; 8916 } 8917 off = opt[IPOPT_OFFSET]; 8918 off--; 8919 redo_srr: 8920 if (optlen < IP_ADDR_LEN || 8921 off > optlen - IP_ADDR_LEN) { 8922 /* End of source route */ 8923 ip1dbg(( 8924 "ip_forward_options: end of SR\n")); 8925 break; 8926 } 8927 /* Pick a reasonable address on the outbound if */ 8928 ASSERT(dst_ill != NULL); 8929 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst, 8930 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL, 8931 NULL) != 0) { 8932 /* No source! Shouldn't happen */ 8933 ifaddr = INADDR_ANY; 8934 } 8935 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 8936 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN); 8937 ip1dbg(("ip_forward_options: next hop 0x%x\n", 8938 ntohl(dst))); 8939 8940 /* 8941 * Check if our address is present more than 8942 * once as consecutive hops in source route. 8943 */ 8944 if (ip_type_v4(dst, ipst) == IRE_LOCAL) { 8945 off += IP_ADDR_LEN; 8946 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 8947 goto redo_srr; 8948 } 8949 ipha->ipha_dst = dst; 8950 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 8951 break; 8952 case IPOPT_RR: 8953 off = opt[IPOPT_OFFSET]; 8954 off--; 8955 if (optlen < IP_ADDR_LEN || 8956 off > optlen - IP_ADDR_LEN) { 8957 /* No more room - ignore */ 8958 ip1dbg(( 8959 "ip_forward_options: end of RR\n")); 8960 break; 8961 } 8962 /* Pick a reasonable address on the outbound if */ 8963 ASSERT(dst_ill != NULL); 8964 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst, 8965 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL, 8966 NULL) != 0) { 8967 /* No source! Shouldn't happen */ 8968 ifaddr = INADDR_ANY; 8969 } 8970 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN); 8971 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 8972 break; 8973 case IPOPT_TS: 8974 /* Insert timestamp if there is room */ 8975 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 8976 case IPOPT_TS_TSONLY: 8977 off = IPOPT_TS_TIMELEN; 8978 break; 8979 case IPOPT_TS_PRESPEC: 8980 case IPOPT_TS_PRESPEC_RFC791: 8981 /* Verify that the address matched */ 8982 off = opt[IPOPT_OFFSET] - 1; 8983 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 8984 if (ip_type_v4(dst, ipst) != IRE_LOCAL) { 8985 /* Not for us */ 8986 break; 8987 } 8988 /* FALLTHRU */ 8989 case IPOPT_TS_TSANDADDR: 8990 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 8991 break; 8992 default: 8993 /* 8994 * ip_*put_options should have already 8995 * dropped this packet. 8996 */ 8997 cmn_err(CE_PANIC, "ip_forward_options: " 8998 "unknown IT - bug in ip_input_options?\n"); 8999 return (B_TRUE); /* Keep "lint" happy */ 9000 } 9001 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 9002 /* Increase overflow counter */ 9003 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 9004 opt[IPOPT_POS_OV_FLG] = 9005 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 9006 (off << 4)); 9007 break; 9008 } 9009 off = opt[IPOPT_OFFSET] - 1; 9010 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 9011 case IPOPT_TS_PRESPEC: 9012 case IPOPT_TS_PRESPEC_RFC791: 9013 case IPOPT_TS_TSANDADDR: 9014 /* Pick a reasonable addr on the outbound if */ 9015 ASSERT(dst_ill != NULL); 9016 if (ip_select_source_v4(dst_ill, INADDR_ANY, 9017 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr, 9018 NULL, NULL) != 0) { 9019 /* No source! Shouldn't happen */ 9020 ifaddr = INADDR_ANY; 9021 } 9022 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN); 9023 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 9024 /* FALLTHRU */ 9025 case IPOPT_TS_TSONLY: 9026 off = opt[IPOPT_OFFSET] - 1; 9027 /* Compute # of milliseconds since midnight */ 9028 gethrestime(&now); 9029 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 9030 now.tv_nsec / (NANOSEC / MILLISEC); 9031 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 9032 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 9033 break; 9034 } 9035 break; 9036 } 9037 } 9038 return (B_TRUE); 9039 } 9040 9041 /* 9042 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout 9043 * returns 'true' if there are still fragments left on the queue, in 9044 * which case we restart the timer. 9045 */ 9046 void 9047 ill_frag_timer(void *arg) 9048 { 9049 ill_t *ill = (ill_t *)arg; 9050 boolean_t frag_pending; 9051 ip_stack_t *ipst = ill->ill_ipst; 9052 time_t timeout; 9053 9054 mutex_enter(&ill->ill_lock); 9055 ASSERT(!ill->ill_fragtimer_executing); 9056 if (ill->ill_state_flags & ILL_CONDEMNED) { 9057 ill->ill_frag_timer_id = 0; 9058 mutex_exit(&ill->ill_lock); 9059 return; 9060 } 9061 ill->ill_fragtimer_executing = 1; 9062 mutex_exit(&ill->ill_lock); 9063 9064 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout : 9065 ipst->ips_ip_reassembly_timeout); 9066 9067 frag_pending = ill_frag_timeout(ill, timeout); 9068 9069 /* 9070 * Restart the timer, if we have fragments pending or if someone 9071 * wanted us to be scheduled again. 9072 */ 9073 mutex_enter(&ill->ill_lock); 9074 ill->ill_fragtimer_executing = 0; 9075 ill->ill_frag_timer_id = 0; 9076 if (frag_pending || ill->ill_fragtimer_needrestart) 9077 ill_frag_timer_start(ill); 9078 mutex_exit(&ill->ill_lock); 9079 } 9080 9081 void 9082 ill_frag_timer_start(ill_t *ill) 9083 { 9084 ip_stack_t *ipst = ill->ill_ipst; 9085 clock_t timeo_ms; 9086 9087 ASSERT(MUTEX_HELD(&ill->ill_lock)); 9088 9089 /* If the ill is closing or opening don't proceed */ 9090 if (ill->ill_state_flags & ILL_CONDEMNED) 9091 return; 9092 9093 if (ill->ill_fragtimer_executing) { 9094 /* 9095 * ill_frag_timer is currently executing. Just record the 9096 * the fact that we want the timer to be restarted. 9097 * ill_frag_timer will post a timeout before it returns, 9098 * ensuring it will be called again. 9099 */ 9100 ill->ill_fragtimer_needrestart = 1; 9101 return; 9102 } 9103 9104 if (ill->ill_frag_timer_id == 0) { 9105 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout : 9106 ipst->ips_ip_reassembly_timeout) * SECONDS; 9107 9108 /* 9109 * The timer is neither running nor is the timeout handler 9110 * executing. Post a timeout so that ill_frag_timer will be 9111 * called 9112 */ 9113 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, 9114 MSEC_TO_TICK(timeo_ms >> 1)); 9115 ill->ill_fragtimer_needrestart = 0; 9116 } 9117 } 9118 9119 /* 9120 * Update any source route, record route or timestamp options. 9121 * Check that we are at end of strict source route. 9122 * The options have already been checked for sanity in ip_input_options(). 9123 */ 9124 boolean_t 9125 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira) 9126 { 9127 ipoptp_t opts; 9128 uchar_t *opt; 9129 uint8_t optval; 9130 uint8_t optlen; 9131 ipaddr_t dst; 9132 ipaddr_t ifaddr; 9133 uint32_t ts; 9134 timestruc_t now; 9135 ill_t *ill = ira->ira_ill; 9136 ip_stack_t *ipst = ill->ill_ipst; 9137 9138 ip2dbg(("ip_input_local_options\n")); 9139 9140 for (optval = ipoptp_first(&opts, ipha); 9141 optval != IPOPT_EOL; 9142 optval = ipoptp_next(&opts)) { 9143 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 9144 opt = opts.ipoptp_cur; 9145 optlen = opts.ipoptp_len; 9146 ip2dbg(("ip_input_local_options: opt %d, len %d\n", 9147 optval, optlen)); 9148 switch (optval) { 9149 uint32_t off; 9150 case IPOPT_SSRR: 9151 case IPOPT_LSRR: 9152 off = opt[IPOPT_OFFSET]; 9153 off--; 9154 if (optlen < IP_ADDR_LEN || 9155 off > optlen - IP_ADDR_LEN) { 9156 /* End of source route */ 9157 ip1dbg(("ip_input_local_options: end of SR\n")); 9158 break; 9159 } 9160 /* 9161 * This will only happen if two consecutive entries 9162 * in the source route contains our address or if 9163 * it is a packet with a loose source route which 9164 * reaches us before consuming the whole source route 9165 */ 9166 ip1dbg(("ip_input_local_options: not end of SR\n")); 9167 if (optval == IPOPT_SSRR) { 9168 goto bad_src_route; 9169 } 9170 /* 9171 * Hack: instead of dropping the packet truncate the 9172 * source route to what has been used by filling the 9173 * rest with IPOPT_NOP. 9174 */ 9175 opt[IPOPT_OLEN] = (uint8_t)off; 9176 while (off < optlen) { 9177 opt[off++] = IPOPT_NOP; 9178 } 9179 break; 9180 case IPOPT_RR: 9181 off = opt[IPOPT_OFFSET]; 9182 off--; 9183 if (optlen < IP_ADDR_LEN || 9184 off > optlen - IP_ADDR_LEN) { 9185 /* No more room - ignore */ 9186 ip1dbg(( 9187 "ip_input_local_options: end of RR\n")); 9188 break; 9189 } 9190 /* Pick a reasonable address on the outbound if */ 9191 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst, 9192 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL, 9193 NULL) != 0) { 9194 /* No source! Shouldn't happen */ 9195 ifaddr = INADDR_ANY; 9196 } 9197 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN); 9198 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 9199 break; 9200 case IPOPT_TS: 9201 /* Insert timestamp if there is romm */ 9202 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 9203 case IPOPT_TS_TSONLY: 9204 off = IPOPT_TS_TIMELEN; 9205 break; 9206 case IPOPT_TS_PRESPEC: 9207 case IPOPT_TS_PRESPEC_RFC791: 9208 /* Verify that the address matched */ 9209 off = opt[IPOPT_OFFSET] - 1; 9210 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 9211 if (ip_type_v4(dst, ipst) != IRE_LOCAL) { 9212 /* Not for us */ 9213 break; 9214 } 9215 /* FALLTHRU */ 9216 case IPOPT_TS_TSANDADDR: 9217 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 9218 break; 9219 default: 9220 /* 9221 * ip_*put_options should have already 9222 * dropped this packet. 9223 */ 9224 cmn_err(CE_PANIC, "ip_input_local_options: " 9225 "unknown IT - bug in ip_input_options?\n"); 9226 return (B_TRUE); /* Keep "lint" happy */ 9227 } 9228 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 9229 /* Increase overflow counter */ 9230 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 9231 opt[IPOPT_POS_OV_FLG] = 9232 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | 9233 (off << 4)); 9234 break; 9235 } 9236 off = opt[IPOPT_OFFSET] - 1; 9237 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 9238 case IPOPT_TS_PRESPEC: 9239 case IPOPT_TS_PRESPEC_RFC791: 9240 case IPOPT_TS_TSANDADDR: 9241 /* Pick a reasonable addr on the outbound if */ 9242 if (ip_select_source_v4(ill, INADDR_ANY, 9243 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst, 9244 &ifaddr, NULL, NULL) != 0) { 9245 /* No source! Shouldn't happen */ 9246 ifaddr = INADDR_ANY; 9247 } 9248 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN); 9249 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 9250 /* FALLTHRU */ 9251 case IPOPT_TS_TSONLY: 9252 off = opt[IPOPT_OFFSET] - 1; 9253 /* Compute # of milliseconds since midnight */ 9254 gethrestime(&now); 9255 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 9256 now.tv_nsec / (NANOSEC / MILLISEC); 9257 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 9258 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 9259 break; 9260 } 9261 break; 9262 } 9263 } 9264 return (B_TRUE); 9265 9266 bad_src_route: 9267 /* make sure we clear any indication of a hardware checksum */ 9268 DB_CKSUMFLAGS(mp) = 0; 9269 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill); 9270 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira); 9271 return (B_FALSE); 9272 9273 } 9274 9275 /* 9276 * Process IP options in an inbound packet. Always returns the nexthop. 9277 * Normally this is the passed in nexthop, but if there is an option 9278 * that effects the nexthop (such as a source route) that will be returned. 9279 * Sets *errorp if there is an error, in which case an ICMP error has been sent 9280 * and mp freed. 9281 */ 9282 ipaddr_t 9283 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp, 9284 ip_recv_attr_t *ira, int *errorp) 9285 { 9286 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 9287 ipoptp_t opts; 9288 uchar_t *opt; 9289 uint8_t optval; 9290 uint8_t optlen; 9291 intptr_t code = 0; 9292 ire_t *ire; 9293 9294 ip2dbg(("ip_input_options\n")); 9295 *errorp = 0; 9296 for (optval = ipoptp_first(&opts, ipha); 9297 optval != IPOPT_EOL; 9298 optval = ipoptp_next(&opts)) { 9299 opt = opts.ipoptp_cur; 9300 optlen = opts.ipoptp_len; 9301 ip2dbg(("ip_input_options: opt %d, len %d\n", 9302 optval, optlen)); 9303 /* 9304 * Note: we need to verify the checksum before we 9305 * modify anything thus this routine only extracts the next 9306 * hop dst from any source route. 9307 */ 9308 switch (optval) { 9309 uint32_t off; 9310 case IPOPT_SSRR: 9311 case IPOPT_LSRR: 9312 if (ip_type_v4(dst, ipst) != IRE_LOCAL) { 9313 if (optval == IPOPT_SSRR) { 9314 ip1dbg(("ip_input_options: not next" 9315 " strict source route 0x%x\n", 9316 ntohl(dst))); 9317 code = (char *)&ipha->ipha_dst - 9318 (char *)ipha; 9319 goto param_prob; /* RouterReq's */ 9320 } 9321 ip2dbg(("ip_input_options: " 9322 "not next source route 0x%x\n", 9323 ntohl(dst))); 9324 break; 9325 } 9326 9327 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 9328 ip1dbg(( 9329 "ip_input_options: bad option offset\n")); 9330 code = (char *)&opt[IPOPT_OLEN] - 9331 (char *)ipha; 9332 goto param_prob; 9333 } 9334 off = opt[IPOPT_OFFSET]; 9335 off--; 9336 redo_srr: 9337 if (optlen < IP_ADDR_LEN || 9338 off > optlen - IP_ADDR_LEN) { 9339 /* End of source route */ 9340 ip1dbg(("ip_input_options: end of SR\n")); 9341 break; 9342 } 9343 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 9344 ip1dbg(("ip_input_options: next hop 0x%x\n", 9345 ntohl(dst))); 9346 9347 /* 9348 * Check if our address is present more than 9349 * once as consecutive hops in source route. 9350 * XXX verify per-interface ip_forwarding 9351 * for source route? 9352 */ 9353 if (ip_type_v4(dst, ipst) == IRE_LOCAL) { 9354 off += IP_ADDR_LEN; 9355 goto redo_srr; 9356 } 9357 9358 if (dst == htonl(INADDR_LOOPBACK)) { 9359 ip1dbg(("ip_input_options: loopback addr in " 9360 "source route!\n")); 9361 goto bad_src_route; 9362 } 9363 /* 9364 * For strict: verify that dst is directly 9365 * reachable. 9366 */ 9367 if (optval == IPOPT_SSRR) { 9368 ire = ire_ftable_lookup_v4(dst, 0, 0, 9369 IRE_IF_ALL, NULL, ALL_ZONES, 9370 ira->ira_tsl, 9371 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst, 9372 NULL); 9373 if (ire == NULL) { 9374 ip1dbg(("ip_input_options: SSRR not " 9375 "directly reachable: 0x%x\n", 9376 ntohl(dst))); 9377 goto bad_src_route; 9378 } 9379 ire_refrele(ire); 9380 } 9381 /* 9382 * Defer update of the offset and the record route 9383 * until the packet is forwarded. 9384 */ 9385 break; 9386 case IPOPT_RR: 9387 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 9388 ip1dbg(( 9389 "ip_input_options: bad option offset\n")); 9390 code = (char *)&opt[IPOPT_OLEN] - 9391 (char *)ipha; 9392 goto param_prob; 9393 } 9394 break; 9395 case IPOPT_TS: 9396 /* 9397 * Verify that length >= 5 and that there is either 9398 * room for another timestamp or that the overflow 9399 * counter is not maxed out. 9400 */ 9401 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 9402 if (optlen < IPOPT_MINLEN_IT) { 9403 goto param_prob; 9404 } 9405 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 9406 ip1dbg(( 9407 "ip_input_options: bad option offset\n")); 9408 code = (char *)&opt[IPOPT_OFFSET] - 9409 (char *)ipha; 9410 goto param_prob; 9411 } 9412 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 9413 case IPOPT_TS_TSONLY: 9414 off = IPOPT_TS_TIMELEN; 9415 break; 9416 case IPOPT_TS_TSANDADDR: 9417 case IPOPT_TS_PRESPEC: 9418 case IPOPT_TS_PRESPEC_RFC791: 9419 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 9420 break; 9421 default: 9422 code = (char *)&opt[IPOPT_POS_OV_FLG] - 9423 (char *)ipha; 9424 goto param_prob; 9425 } 9426 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 9427 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 9428 /* 9429 * No room and the overflow counter is 15 9430 * already. 9431 */ 9432 goto param_prob; 9433 } 9434 break; 9435 } 9436 } 9437 9438 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) { 9439 return (dst); 9440 } 9441 9442 ip1dbg(("ip_input_options: error processing IP options.")); 9443 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 9444 9445 param_prob: 9446 /* make sure we clear any indication of a hardware checksum */ 9447 DB_CKSUMFLAGS(mp) = 0; 9448 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill); 9449 icmp_param_problem(mp, (uint8_t)code, ira); 9450 *errorp = -1; 9451 return (dst); 9452 9453 bad_src_route: 9454 /* make sure we clear any indication of a hardware checksum */ 9455 DB_CKSUMFLAGS(mp) = 0; 9456 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill); 9457 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira); 9458 *errorp = -1; 9459 return (dst); 9460 } 9461 9462 /* 9463 * IP & ICMP info in >=14 msg's ... 9464 * - ip fixed part (mib2_ip_t) 9465 * - icmp fixed part (mib2_icmp_t) 9466 * - ipAddrEntryTable (ip 20) all IPv4 ipifs 9467 * - ipRouteEntryTable (ip 21) all IPv4 IREs 9468 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries 9469 * - ipRouteAttributeTable (ip 102) labeled routes 9470 * - ip multicast membership (ip_member_t) 9471 * - ip multicast source filtering (ip_grpsrc_t) 9472 * - igmp fixed part (struct igmpstat) 9473 * - multicast routing stats (struct mrtstat) 9474 * - multicast routing vifs (array of struct vifctl) 9475 * - multicast routing routes (array of struct mfcctl) 9476 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t) 9477 * One per ill plus one generic 9478 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t) 9479 * One per ill plus one generic 9480 * - ipv6RouteEntry all IPv6 IREs 9481 * - ipv6RouteAttributeTable (ip6 102) labeled routes 9482 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries 9483 * - ipv6AddrEntry all IPv6 ipifs 9484 * - ipv6 multicast membership (ipv6_member_t) 9485 * - ipv6 multicast source filtering (ipv6_grpsrc_t) 9486 * 9487 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is 9488 * already filled in by the caller. 9489 * Return value of 0 indicates that no messages were sent and caller 9490 * should free mpctl. 9491 */ 9492 int 9493 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level) 9494 { 9495 ip_stack_t *ipst; 9496 sctp_stack_t *sctps; 9497 9498 if (q->q_next != NULL) { 9499 ipst = ILLQ_TO_IPST(q); 9500 } else { 9501 ipst = CONNQ_TO_IPST(q); 9502 } 9503 ASSERT(ipst != NULL); 9504 sctps = ipst->ips_netstack->netstack_sctp; 9505 9506 if (mpctl == NULL || mpctl->b_cont == NULL) { 9507 return (0); 9508 } 9509 9510 /* 9511 * For the purposes of the (broken) packet shell use 9512 * of the level we make sure MIB2_TCP/MIB2_UDP can be used 9513 * to make TCP and UDP appear first in the list of mib items. 9514 * TBD: We could expand this and use it in netstat so that 9515 * the kernel doesn't have to produce large tables (connections, 9516 * routes, etc) when netstat only wants the statistics or a particular 9517 * table. 9518 */ 9519 if (!(level == MIB2_TCP || level == MIB2_UDP)) { 9520 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) { 9521 return (1); 9522 } 9523 } 9524 9525 if (level != MIB2_TCP) { 9526 if ((mpctl = udp_snmp_get(q, mpctl)) == NULL) { 9527 return (1); 9528 } 9529 } 9530 9531 if (level != MIB2_UDP) { 9532 if ((mpctl = tcp_snmp_get(q, mpctl)) == NULL) { 9533 return (1); 9534 } 9535 } 9536 9537 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl, 9538 ipst)) == NULL) { 9539 return (1); 9540 } 9541 9542 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst)) == NULL) { 9543 return (1); 9544 } 9545 9546 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) { 9547 return (1); 9548 } 9549 9550 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) { 9551 return (1); 9552 } 9553 9554 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) { 9555 return (1); 9556 } 9557 9558 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) { 9559 return (1); 9560 } 9561 9562 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst)) == NULL) { 9563 return (1); 9564 } 9565 9566 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst)) == NULL) { 9567 return (1); 9568 } 9569 9570 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) { 9571 return (1); 9572 } 9573 9574 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) { 9575 return (1); 9576 } 9577 9578 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) { 9579 return (1); 9580 } 9581 9582 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) { 9583 return (1); 9584 } 9585 9586 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) { 9587 return (1); 9588 } 9589 9590 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) { 9591 return (1); 9592 } 9593 9594 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst); 9595 if (mpctl == NULL) 9596 return (1); 9597 9598 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst); 9599 if (mpctl == NULL) 9600 return (1); 9601 9602 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) { 9603 return (1); 9604 } 9605 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) { 9606 return (1); 9607 } 9608 freemsg(mpctl); 9609 return (1); 9610 } 9611 9612 /* Get global (legacy) IPv4 statistics */ 9613 static mblk_t * 9614 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib, 9615 ip_stack_t *ipst) 9616 { 9617 mib2_ip_t old_ip_mib; 9618 struct opthdr *optp; 9619 mblk_t *mp2ctl; 9620 9621 /* 9622 * make a copy of the original message 9623 */ 9624 mp2ctl = copymsg(mpctl); 9625 9626 /* fixed length IP structure... */ 9627 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 9628 optp->level = MIB2_IP; 9629 optp->name = 0; 9630 SET_MIB(old_ip_mib.ipForwarding, 9631 (WE_ARE_FORWARDING(ipst) ? 1 : 2)); 9632 SET_MIB(old_ip_mib.ipDefaultTTL, 9633 (uint32_t)ipst->ips_ip_def_ttl); 9634 SET_MIB(old_ip_mib.ipReasmTimeout, 9635 ipst->ips_ip_reassembly_timeout); 9636 SET_MIB(old_ip_mib.ipAddrEntrySize, 9637 sizeof (mib2_ipAddrEntry_t)); 9638 SET_MIB(old_ip_mib.ipRouteEntrySize, 9639 sizeof (mib2_ipRouteEntry_t)); 9640 SET_MIB(old_ip_mib.ipNetToMediaEntrySize, 9641 sizeof (mib2_ipNetToMediaEntry_t)); 9642 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t)); 9643 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t)); 9644 SET_MIB(old_ip_mib.ipRouteAttributeSize, 9645 sizeof (mib2_ipAttributeEntry_t)); 9646 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t)); 9647 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t)); 9648 9649 /* 9650 * Grab the statistics from the new IP MIB 9651 */ 9652 SET_MIB(old_ip_mib.ipInReceives, 9653 (uint32_t)ipmib->ipIfStatsHCInReceives); 9654 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors); 9655 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors); 9656 SET_MIB(old_ip_mib.ipForwDatagrams, 9657 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams); 9658 SET_MIB(old_ip_mib.ipInUnknownProtos, 9659 ipmib->ipIfStatsInUnknownProtos); 9660 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards); 9661 SET_MIB(old_ip_mib.ipInDelivers, 9662 (uint32_t)ipmib->ipIfStatsHCInDelivers); 9663 SET_MIB(old_ip_mib.ipOutRequests, 9664 (uint32_t)ipmib->ipIfStatsHCOutRequests); 9665 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards); 9666 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes); 9667 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds); 9668 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs); 9669 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails); 9670 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs); 9671 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails); 9672 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates); 9673 9674 /* ipRoutingDiscards is not being used */ 9675 SET_MIB(old_ip_mib.ipRoutingDiscards, 0); 9676 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs); 9677 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts); 9678 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs); 9679 SET_MIB(old_ip_mib.ipReasmDuplicates, 9680 ipmib->ipIfStatsReasmDuplicates); 9681 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups); 9682 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits); 9683 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs); 9684 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows); 9685 SET_MIB(old_ip_mib.rawipInOverflows, 9686 ipmib->rawipIfStatsInOverflows); 9687 9688 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded); 9689 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed); 9690 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion); 9691 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion); 9692 SET_MIB(old_ip_mib.ipOutSwitchIPv6, 9693 ipmib->ipIfStatsOutSwitchIPVersion); 9694 9695 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib, 9696 (int)sizeof (old_ip_mib))) { 9697 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n", 9698 (uint_t)sizeof (old_ip_mib))); 9699 } 9700 9701 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 9702 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n", 9703 (int)optp->level, (int)optp->name, (int)optp->len)); 9704 qreply(q, mpctl); 9705 return (mp2ctl); 9706 } 9707 9708 /* Per interface IPv4 statistics */ 9709 static mblk_t * 9710 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 9711 { 9712 struct opthdr *optp; 9713 mblk_t *mp2ctl; 9714 ill_t *ill; 9715 ill_walk_context_t ctx; 9716 mblk_t *mp_tail = NULL; 9717 mib2_ipIfStatsEntry_t global_ip_mib; 9718 9719 /* 9720 * Make a copy of the original message 9721 */ 9722 mp2ctl = copymsg(mpctl); 9723 9724 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 9725 optp->level = MIB2_IP; 9726 optp->name = MIB2_IP_TRAFFIC_STATS; 9727 /* Include "unknown interface" ip_mib */ 9728 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 9729 ipst->ips_ip_mib.ipIfStatsIfIndex = 9730 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 9731 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding, 9732 (ipst->ips_ip_forwarding ? 1 : 2)); 9733 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL, 9734 (uint32_t)ipst->ips_ip_def_ttl); 9735 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize, 9736 sizeof (mib2_ipIfStatsEntry_t)); 9737 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize, 9738 sizeof (mib2_ipAddrEntry_t)); 9739 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize, 9740 sizeof (mib2_ipRouteEntry_t)); 9741 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize, 9742 sizeof (mib2_ipNetToMediaEntry_t)); 9743 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize, 9744 sizeof (ip_member_t)); 9745 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize, 9746 sizeof (ip_grpsrc_t)); 9747 9748 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 9749 (char *)&ipst->ips_ip_mib, (int)sizeof (ipst->ips_ip_mib))) { 9750 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 9751 "failed to allocate %u bytes\n", 9752 (uint_t)sizeof (ipst->ips_ip_mib))); 9753 } 9754 9755 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib)); 9756 9757 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 9758 ill = ILL_START_WALK_V4(&ctx, ipst); 9759 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 9760 ill->ill_ip_mib->ipIfStatsIfIndex = 9761 ill->ill_phyint->phyint_ifindex; 9762 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 9763 (ipst->ips_ip_forwarding ? 1 : 2)); 9764 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL, 9765 (uint32_t)ipst->ips_ip_def_ttl); 9766 9767 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib); 9768 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 9769 (char *)ill->ill_ip_mib, 9770 (int)sizeof (*ill->ill_ip_mib))) { 9771 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 9772 "failed to allocate %u bytes\n", 9773 (uint_t)sizeof (*ill->ill_ip_mib))); 9774 } 9775 } 9776 rw_exit(&ipst->ips_ill_g_lock); 9777 9778 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 9779 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: " 9780 "level %d, name %d, len %d\n", 9781 (int)optp->level, (int)optp->name, (int)optp->len)); 9782 qreply(q, mpctl); 9783 9784 if (mp2ctl == NULL) 9785 return (NULL); 9786 9787 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst)); 9788 } 9789 9790 /* Global IPv4 ICMP statistics */ 9791 static mblk_t * 9792 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 9793 { 9794 struct opthdr *optp; 9795 mblk_t *mp2ctl; 9796 9797 /* 9798 * Make a copy of the original message 9799 */ 9800 mp2ctl = copymsg(mpctl); 9801 9802 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 9803 optp->level = MIB2_ICMP; 9804 optp->name = 0; 9805 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib, 9806 (int)sizeof (ipst->ips_icmp_mib))) { 9807 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n", 9808 (uint_t)sizeof (ipst->ips_icmp_mib))); 9809 } 9810 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 9811 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n", 9812 (int)optp->level, (int)optp->name, (int)optp->len)); 9813 qreply(q, mpctl); 9814 return (mp2ctl); 9815 } 9816 9817 /* Global IPv4 IGMP statistics */ 9818 static mblk_t * 9819 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 9820 { 9821 struct opthdr *optp; 9822 mblk_t *mp2ctl; 9823 9824 /* 9825 * make a copy of the original message 9826 */ 9827 mp2ctl = copymsg(mpctl); 9828 9829 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 9830 optp->level = EXPER_IGMP; 9831 optp->name = 0; 9832 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat, 9833 (int)sizeof (ipst->ips_igmpstat))) { 9834 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n", 9835 (uint_t)sizeof (ipst->ips_igmpstat))); 9836 } 9837 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 9838 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n", 9839 (int)optp->level, (int)optp->name, (int)optp->len)); 9840 qreply(q, mpctl); 9841 return (mp2ctl); 9842 } 9843 9844 /* Global IPv4 Multicast Routing statistics */ 9845 static mblk_t * 9846 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 9847 { 9848 struct opthdr *optp; 9849 mblk_t *mp2ctl; 9850 9851 /* 9852 * make a copy of the original message 9853 */ 9854 mp2ctl = copymsg(mpctl); 9855 9856 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 9857 optp->level = EXPER_DVMRP; 9858 optp->name = 0; 9859 if (!ip_mroute_stats(mpctl->b_cont, ipst)) { 9860 ip0dbg(("ip_mroute_stats: failed\n")); 9861 } 9862 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 9863 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n", 9864 (int)optp->level, (int)optp->name, (int)optp->len)); 9865 qreply(q, mpctl); 9866 return (mp2ctl); 9867 } 9868 9869 /* IPv4 address information */ 9870 static mblk_t * 9871 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 9872 { 9873 struct opthdr *optp; 9874 mblk_t *mp2ctl; 9875 mblk_t *mp_tail = NULL; 9876 ill_t *ill; 9877 ipif_t *ipif; 9878 uint_t bitval; 9879 mib2_ipAddrEntry_t mae; 9880 zoneid_t zoneid; 9881 ill_walk_context_t ctx; 9882 9883 /* 9884 * make a copy of the original message 9885 */ 9886 mp2ctl = copymsg(mpctl); 9887 9888 /* ipAddrEntryTable */ 9889 9890 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 9891 optp->level = MIB2_IP; 9892 optp->name = MIB2_IP_ADDR; 9893 zoneid = Q_TO_CONN(q)->conn_zoneid; 9894 9895 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 9896 ill = ILL_START_WALK_V4(&ctx, ipst); 9897 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 9898 for (ipif = ill->ill_ipif; ipif != NULL; 9899 ipif = ipif->ipif_next) { 9900 if (ipif->ipif_zoneid != zoneid && 9901 ipif->ipif_zoneid != ALL_ZONES) 9902 continue; 9903 /* Sum of count from dead IRE_LO* and our current */ 9904 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 9905 if (ipif->ipif_ire_local != NULL) { 9906 mae.ipAdEntInfo.ae_ibcnt += 9907 ipif->ipif_ire_local->ire_ib_pkt_count; 9908 } 9909 mae.ipAdEntInfo.ae_obcnt = 0; 9910 mae.ipAdEntInfo.ae_focnt = 0; 9911 9912 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes, 9913 OCTET_LENGTH); 9914 mae.ipAdEntIfIndex.o_length = 9915 mi_strlen(mae.ipAdEntIfIndex.o_bytes); 9916 mae.ipAdEntAddr = ipif->ipif_lcl_addr; 9917 mae.ipAdEntNetMask = ipif->ipif_net_mask; 9918 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet; 9919 mae.ipAdEntInfo.ae_subnet_len = 9920 ip_mask_to_plen(ipif->ipif_net_mask); 9921 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr; 9922 for (bitval = 1; 9923 bitval && 9924 !(bitval & ipif->ipif_brd_addr); 9925 bitval <<= 1) 9926 noop; 9927 mae.ipAdEntBcastAddr = bitval; 9928 mae.ipAdEntReasmMaxSize = IP_MAXPACKET; 9929 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu; 9930 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric; 9931 mae.ipAdEntInfo.ae_broadcast_addr = 9932 ipif->ipif_brd_addr; 9933 mae.ipAdEntInfo.ae_pp_dst_addr = 9934 ipif->ipif_pp_dst_addr; 9935 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags | 9936 ill->ill_flags | ill->ill_phyint->phyint_flags; 9937 mae.ipAdEntRetransmitTime = 9938 ill->ill_reachable_retrans_time; 9939 9940 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 9941 (char *)&mae, (int)sizeof (mib2_ipAddrEntry_t))) { 9942 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to " 9943 "allocate %u bytes\n", 9944 (uint_t)sizeof (mib2_ipAddrEntry_t))); 9945 } 9946 } 9947 } 9948 rw_exit(&ipst->ips_ill_g_lock); 9949 9950 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 9951 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n", 9952 (int)optp->level, (int)optp->name, (int)optp->len)); 9953 qreply(q, mpctl); 9954 return (mp2ctl); 9955 } 9956 9957 /* IPv6 address information */ 9958 static mblk_t * 9959 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 9960 { 9961 struct opthdr *optp; 9962 mblk_t *mp2ctl; 9963 mblk_t *mp_tail = NULL; 9964 ill_t *ill; 9965 ipif_t *ipif; 9966 mib2_ipv6AddrEntry_t mae6; 9967 zoneid_t zoneid; 9968 ill_walk_context_t ctx; 9969 9970 /* 9971 * make a copy of the original message 9972 */ 9973 mp2ctl = copymsg(mpctl); 9974 9975 /* ipv6AddrEntryTable */ 9976 9977 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 9978 optp->level = MIB2_IP6; 9979 optp->name = MIB2_IP6_ADDR; 9980 zoneid = Q_TO_CONN(q)->conn_zoneid; 9981 9982 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 9983 ill = ILL_START_WALK_V6(&ctx, ipst); 9984 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 9985 for (ipif = ill->ill_ipif; ipif != NULL; 9986 ipif = ipif->ipif_next) { 9987 if (ipif->ipif_zoneid != zoneid && 9988 ipif->ipif_zoneid != ALL_ZONES) 9989 continue; 9990 /* Sum of count from dead IRE_LO* and our current */ 9991 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count; 9992 if (ipif->ipif_ire_local != NULL) { 9993 mae6.ipv6AddrInfo.ae_ibcnt += 9994 ipif->ipif_ire_local->ire_ib_pkt_count; 9995 } 9996 mae6.ipv6AddrInfo.ae_obcnt = 0; 9997 mae6.ipv6AddrInfo.ae_focnt = 0; 9998 9999 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes, 10000 OCTET_LENGTH); 10001 mae6.ipv6AddrIfIndex.o_length = 10002 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes); 10003 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr; 10004 mae6.ipv6AddrPfxLength = 10005 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 10006 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet; 10007 mae6.ipv6AddrInfo.ae_subnet_len = 10008 mae6.ipv6AddrPfxLength; 10009 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr; 10010 10011 /* Type: stateless(1), stateful(2), unknown(3) */ 10012 if (ipif->ipif_flags & IPIF_ADDRCONF) 10013 mae6.ipv6AddrType = 1; 10014 else 10015 mae6.ipv6AddrType = 2; 10016 /* Anycast: true(1), false(2) */ 10017 if (ipif->ipif_flags & IPIF_ANYCAST) 10018 mae6.ipv6AddrAnycastFlag = 1; 10019 else 10020 mae6.ipv6AddrAnycastFlag = 2; 10021 10022 /* 10023 * Address status: preferred(1), deprecated(2), 10024 * invalid(3), inaccessible(4), unknown(5) 10025 */ 10026 if (ipif->ipif_flags & IPIF_NOLOCAL) 10027 mae6.ipv6AddrStatus = 3; 10028 else if (ipif->ipif_flags & IPIF_DEPRECATED) 10029 mae6.ipv6AddrStatus = 2; 10030 else 10031 mae6.ipv6AddrStatus = 1; 10032 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu; 10033 mae6.ipv6AddrInfo.ae_metric = 10034 ipif->ipif_ill->ill_metric; 10035 mae6.ipv6AddrInfo.ae_pp_dst_addr = 10036 ipif->ipif_v6pp_dst_addr; 10037 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags | 10038 ill->ill_flags | ill->ill_phyint->phyint_flags; 10039 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET; 10040 mae6.ipv6AddrIdentifier = ill->ill_token; 10041 mae6.ipv6AddrIdentifierLen = ill->ill_token_length; 10042 mae6.ipv6AddrReachableTime = ill->ill_reachable_time; 10043 mae6.ipv6AddrRetransmitTime = 10044 ill->ill_reachable_retrans_time; 10045 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 10046 (char *)&mae6, 10047 (int)sizeof (mib2_ipv6AddrEntry_t))) { 10048 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to " 10049 "allocate %u bytes\n", 10050 (uint_t)sizeof (mib2_ipv6AddrEntry_t))); 10051 } 10052 } 10053 } 10054 rw_exit(&ipst->ips_ill_g_lock); 10055 10056 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10057 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n", 10058 (int)optp->level, (int)optp->name, (int)optp->len)); 10059 qreply(q, mpctl); 10060 return (mp2ctl); 10061 } 10062 10063 /* IPv4 multicast group membership. */ 10064 static mblk_t * 10065 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10066 { 10067 struct opthdr *optp; 10068 mblk_t *mp2ctl; 10069 ill_t *ill; 10070 ipif_t *ipif; 10071 ilm_t *ilm; 10072 ip_member_t ipm; 10073 mblk_t *mp_tail = NULL; 10074 ill_walk_context_t ctx; 10075 zoneid_t zoneid; 10076 10077 /* 10078 * make a copy of the original message 10079 */ 10080 mp2ctl = copymsg(mpctl); 10081 zoneid = Q_TO_CONN(q)->conn_zoneid; 10082 10083 /* ipGroupMember table */ 10084 optp = (struct opthdr *)&mpctl->b_rptr[ 10085 sizeof (struct T_optmgmt_ack)]; 10086 optp->level = MIB2_IP; 10087 optp->name = EXPER_IP_GROUP_MEMBERSHIP; 10088 10089 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10090 ill = ILL_START_WALK_V4(&ctx, ipst); 10091 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 10092 /* Make sure the ill isn't going away. */ 10093 if (!ill_check_and_refhold(ill)) 10094 continue; 10095 rw_exit(&ipst->ips_ill_g_lock); 10096 rw_enter(&ill->ill_mcast_lock, RW_READER); 10097 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 10098 if (ilm->ilm_zoneid != zoneid && 10099 ilm->ilm_zoneid != ALL_ZONES) 10100 continue; 10101 10102 /* Is there an ipif for ilm_ifaddr? */ 10103 for (ipif = ill->ill_ipif; ipif != NULL; 10104 ipif = ipif->ipif_next) { 10105 if (!IPIF_IS_CONDEMNED(ipif) && 10106 ipif->ipif_lcl_addr == ilm->ilm_ifaddr && 10107 ilm->ilm_ifaddr != INADDR_ANY) 10108 break; 10109 } 10110 if (ipif != NULL) { 10111 ipif_get_name(ipif, 10112 ipm.ipGroupMemberIfIndex.o_bytes, 10113 OCTET_LENGTH); 10114 } else { 10115 ill_get_name(ill, 10116 ipm.ipGroupMemberIfIndex.o_bytes, 10117 OCTET_LENGTH); 10118 } 10119 ipm.ipGroupMemberIfIndex.o_length = 10120 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes); 10121 10122 ipm.ipGroupMemberAddress = ilm->ilm_addr; 10123 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt; 10124 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode; 10125 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 10126 (char *)&ipm, (int)sizeof (ipm))) { 10127 ip1dbg(("ip_snmp_get_mib2_ip_group: " 10128 "failed to allocate %u bytes\n", 10129 (uint_t)sizeof (ipm))); 10130 } 10131 } 10132 rw_exit(&ill->ill_mcast_lock); 10133 ill_refrele(ill); 10134 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10135 } 10136 rw_exit(&ipst->ips_ill_g_lock); 10137 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10138 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 10139 (int)optp->level, (int)optp->name, (int)optp->len)); 10140 qreply(q, mpctl); 10141 return (mp2ctl); 10142 } 10143 10144 /* IPv6 multicast group membership. */ 10145 static mblk_t * 10146 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10147 { 10148 struct opthdr *optp; 10149 mblk_t *mp2ctl; 10150 ill_t *ill; 10151 ilm_t *ilm; 10152 ipv6_member_t ipm6; 10153 mblk_t *mp_tail = NULL; 10154 ill_walk_context_t ctx; 10155 zoneid_t zoneid; 10156 10157 /* 10158 * make a copy of the original message 10159 */ 10160 mp2ctl = copymsg(mpctl); 10161 zoneid = Q_TO_CONN(q)->conn_zoneid; 10162 10163 /* ip6GroupMember table */ 10164 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10165 optp->level = MIB2_IP6; 10166 optp->name = EXPER_IP6_GROUP_MEMBERSHIP; 10167 10168 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10169 ill = ILL_START_WALK_V6(&ctx, ipst); 10170 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 10171 /* Make sure the ill isn't going away. */ 10172 if (!ill_check_and_refhold(ill)) 10173 continue; 10174 rw_exit(&ipst->ips_ill_g_lock); 10175 /* 10176 * Normally we don't have any members on under IPMP interfaces. 10177 * We report them as a debugging aid. 10178 */ 10179 rw_enter(&ill->ill_mcast_lock, RW_READER); 10180 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex; 10181 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 10182 if (ilm->ilm_zoneid != zoneid && 10183 ilm->ilm_zoneid != ALL_ZONES) 10184 continue; /* not this zone */ 10185 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr; 10186 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt; 10187 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode; 10188 if (!snmp_append_data2(mpctl->b_cont, 10189 &mp_tail, 10190 (char *)&ipm6, (int)sizeof (ipm6))) { 10191 ip1dbg(("ip_snmp_get_mib2_ip6_group: " 10192 "failed to allocate %u bytes\n", 10193 (uint_t)sizeof (ipm6))); 10194 } 10195 } 10196 rw_exit(&ill->ill_mcast_lock); 10197 ill_refrele(ill); 10198 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10199 } 10200 rw_exit(&ipst->ips_ill_g_lock); 10201 10202 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10203 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 10204 (int)optp->level, (int)optp->name, (int)optp->len)); 10205 qreply(q, mpctl); 10206 return (mp2ctl); 10207 } 10208 10209 /* IP multicast filtered sources */ 10210 static mblk_t * 10211 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10212 { 10213 struct opthdr *optp; 10214 mblk_t *mp2ctl; 10215 ill_t *ill; 10216 ipif_t *ipif; 10217 ilm_t *ilm; 10218 ip_grpsrc_t ips; 10219 mblk_t *mp_tail = NULL; 10220 ill_walk_context_t ctx; 10221 zoneid_t zoneid; 10222 int i; 10223 slist_t *sl; 10224 10225 /* 10226 * make a copy of the original message 10227 */ 10228 mp2ctl = copymsg(mpctl); 10229 zoneid = Q_TO_CONN(q)->conn_zoneid; 10230 10231 /* ipGroupSource table */ 10232 optp = (struct opthdr *)&mpctl->b_rptr[ 10233 sizeof (struct T_optmgmt_ack)]; 10234 optp->level = MIB2_IP; 10235 optp->name = EXPER_IP_GROUP_SOURCES; 10236 10237 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10238 ill = ILL_START_WALK_V4(&ctx, ipst); 10239 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 10240 /* Make sure the ill isn't going away. */ 10241 if (!ill_check_and_refhold(ill)) 10242 continue; 10243 rw_exit(&ipst->ips_ill_g_lock); 10244 rw_enter(&ill->ill_mcast_lock, RW_READER); 10245 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 10246 sl = ilm->ilm_filter; 10247 if (ilm->ilm_zoneid != zoneid && 10248 ilm->ilm_zoneid != ALL_ZONES) 10249 continue; 10250 if (SLIST_IS_EMPTY(sl)) 10251 continue; 10252 10253 /* Is there an ipif for ilm_ifaddr? */ 10254 for (ipif = ill->ill_ipif; ipif != NULL; 10255 ipif = ipif->ipif_next) { 10256 if (!IPIF_IS_CONDEMNED(ipif) && 10257 ipif->ipif_lcl_addr == ilm->ilm_ifaddr && 10258 ilm->ilm_ifaddr != INADDR_ANY) 10259 break; 10260 } 10261 if (ipif != NULL) { 10262 ipif_get_name(ipif, 10263 ips.ipGroupSourceIfIndex.o_bytes, 10264 OCTET_LENGTH); 10265 } else { 10266 ill_get_name(ill, 10267 ips.ipGroupSourceIfIndex.o_bytes, 10268 OCTET_LENGTH); 10269 } 10270 ips.ipGroupSourceIfIndex.o_length = 10271 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes); 10272 10273 ips.ipGroupSourceGroup = ilm->ilm_addr; 10274 for (i = 0; i < sl->sl_numsrc; i++) { 10275 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i])) 10276 continue; 10277 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i], 10278 ips.ipGroupSourceAddress); 10279 if (snmp_append_data2(mpctl->b_cont, &mp_tail, 10280 (char *)&ips, (int)sizeof (ips)) == 0) { 10281 ip1dbg(("ip_snmp_get_mib2_ip_group_src:" 10282 " failed to allocate %u bytes\n", 10283 (uint_t)sizeof (ips))); 10284 } 10285 } 10286 } 10287 rw_exit(&ill->ill_mcast_lock); 10288 ill_refrele(ill); 10289 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10290 } 10291 rw_exit(&ipst->ips_ill_g_lock); 10292 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10293 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 10294 (int)optp->level, (int)optp->name, (int)optp->len)); 10295 qreply(q, mpctl); 10296 return (mp2ctl); 10297 } 10298 10299 /* IPv6 multicast filtered sources. */ 10300 static mblk_t * 10301 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10302 { 10303 struct opthdr *optp; 10304 mblk_t *mp2ctl; 10305 ill_t *ill; 10306 ilm_t *ilm; 10307 ipv6_grpsrc_t ips6; 10308 mblk_t *mp_tail = NULL; 10309 ill_walk_context_t ctx; 10310 zoneid_t zoneid; 10311 int i; 10312 slist_t *sl; 10313 10314 /* 10315 * make a copy of the original message 10316 */ 10317 mp2ctl = copymsg(mpctl); 10318 zoneid = Q_TO_CONN(q)->conn_zoneid; 10319 10320 /* ip6GroupMember table */ 10321 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10322 optp->level = MIB2_IP6; 10323 optp->name = EXPER_IP6_GROUP_SOURCES; 10324 10325 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10326 ill = ILL_START_WALK_V6(&ctx, ipst); 10327 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 10328 /* Make sure the ill isn't going away. */ 10329 if (!ill_check_and_refhold(ill)) 10330 continue; 10331 rw_exit(&ipst->ips_ill_g_lock); 10332 /* 10333 * Normally we don't have any members on under IPMP interfaces. 10334 * We report them as a debugging aid. 10335 */ 10336 rw_enter(&ill->ill_mcast_lock, RW_READER); 10337 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex; 10338 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) { 10339 sl = ilm->ilm_filter; 10340 if (ilm->ilm_zoneid != zoneid && 10341 ilm->ilm_zoneid != ALL_ZONES) 10342 continue; 10343 if (SLIST_IS_EMPTY(sl)) 10344 continue; 10345 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr; 10346 for (i = 0; i < sl->sl_numsrc; i++) { 10347 ips6.ipv6GroupSourceAddress = sl->sl_addr[i]; 10348 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 10349 (char *)&ips6, (int)sizeof (ips6))) { 10350 ip1dbg(("ip_snmp_get_mib2_ip6_" 10351 "group_src: failed to allocate " 10352 "%u bytes\n", 10353 (uint_t)sizeof (ips6))); 10354 } 10355 } 10356 } 10357 rw_exit(&ill->ill_mcast_lock); 10358 ill_refrele(ill); 10359 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10360 } 10361 rw_exit(&ipst->ips_ill_g_lock); 10362 10363 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10364 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n", 10365 (int)optp->level, (int)optp->name, (int)optp->len)); 10366 qreply(q, mpctl); 10367 return (mp2ctl); 10368 } 10369 10370 /* Multicast routing virtual interface table. */ 10371 static mblk_t * 10372 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10373 { 10374 struct opthdr *optp; 10375 mblk_t *mp2ctl; 10376 10377 /* 10378 * make a copy of the original message 10379 */ 10380 mp2ctl = copymsg(mpctl); 10381 10382 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10383 optp->level = EXPER_DVMRP; 10384 optp->name = EXPER_DVMRP_VIF; 10385 if (!ip_mroute_vif(mpctl->b_cont, ipst)) { 10386 ip0dbg(("ip_mroute_vif: failed\n")); 10387 } 10388 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10389 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n", 10390 (int)optp->level, (int)optp->name, (int)optp->len)); 10391 qreply(q, mpctl); 10392 return (mp2ctl); 10393 } 10394 10395 /* Multicast routing table. */ 10396 static mblk_t * 10397 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10398 { 10399 struct opthdr *optp; 10400 mblk_t *mp2ctl; 10401 10402 /* 10403 * make a copy of the original message 10404 */ 10405 mp2ctl = copymsg(mpctl); 10406 10407 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10408 optp->level = EXPER_DVMRP; 10409 optp->name = EXPER_DVMRP_MRT; 10410 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) { 10411 ip0dbg(("ip_mroute_mrt: failed\n")); 10412 } 10413 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10414 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n", 10415 (int)optp->level, (int)optp->name, (int)optp->len)); 10416 qreply(q, mpctl); 10417 return (mp2ctl); 10418 } 10419 10420 /* 10421 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable 10422 * in one IRE walk. 10423 */ 10424 static mblk_t * 10425 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level, 10426 ip_stack_t *ipst) 10427 { 10428 struct opthdr *optp; 10429 mblk_t *mp2ctl; /* Returned */ 10430 mblk_t *mp3ctl; /* nettomedia */ 10431 mblk_t *mp4ctl; /* routeattrs */ 10432 iproutedata_t ird; 10433 zoneid_t zoneid; 10434 10435 /* 10436 * make copies of the original message 10437 * - mp2ctl is returned unchanged to the caller for his use 10438 * - mpctl is sent upstream as ipRouteEntryTable 10439 * - mp3ctl is sent upstream as ipNetToMediaEntryTable 10440 * - mp4ctl is sent upstream as ipRouteAttributeTable 10441 */ 10442 mp2ctl = copymsg(mpctl); 10443 mp3ctl = copymsg(mpctl); 10444 mp4ctl = copymsg(mpctl); 10445 if (mp3ctl == NULL || mp4ctl == NULL) { 10446 freemsg(mp4ctl); 10447 freemsg(mp3ctl); 10448 freemsg(mp2ctl); 10449 freemsg(mpctl); 10450 return (NULL); 10451 } 10452 10453 bzero(&ird, sizeof (ird)); 10454 10455 ird.ird_route.lp_head = mpctl->b_cont; 10456 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 10457 ird.ird_attrs.lp_head = mp4ctl->b_cont; 10458 /* 10459 * If the level has been set the special EXPER_IP_AND_ALL_IRES value, 10460 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is 10461 * intended a temporary solution until a proper MIB API is provided 10462 * that provides complete filtering/caller-opt-in. 10463 */ 10464 if (level == EXPER_IP_AND_ALL_IRES) 10465 ird.ird_flags |= IRD_REPORT_ALL; 10466 10467 zoneid = Q_TO_CONN(q)->conn_zoneid; 10468 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst); 10469 10470 /* ipRouteEntryTable in mpctl */ 10471 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10472 optp->level = MIB2_IP; 10473 optp->name = MIB2_IP_ROUTE; 10474 optp->len = msgdsize(ird.ird_route.lp_head); 10475 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 10476 (int)optp->level, (int)optp->name, (int)optp->len)); 10477 qreply(q, mpctl); 10478 10479 /* ipNetToMediaEntryTable in mp3ctl */ 10480 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst); 10481 10482 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10483 optp->level = MIB2_IP; 10484 optp->name = MIB2_IP_MEDIA; 10485 optp->len = msgdsize(ird.ird_netmedia.lp_head); 10486 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 10487 (int)optp->level, (int)optp->name, (int)optp->len)); 10488 qreply(q, mp3ctl); 10489 10490 /* ipRouteAttributeTable in mp4ctl */ 10491 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10492 optp->level = MIB2_IP; 10493 optp->name = EXPER_IP_RTATTR; 10494 optp->len = msgdsize(ird.ird_attrs.lp_head); 10495 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n", 10496 (int)optp->level, (int)optp->name, (int)optp->len)); 10497 if (optp->len == 0) 10498 freemsg(mp4ctl); 10499 else 10500 qreply(q, mp4ctl); 10501 10502 return (mp2ctl); 10503 } 10504 10505 /* 10506 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and 10507 * ipv6NetToMediaEntryTable in an NDP walk. 10508 */ 10509 static mblk_t * 10510 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level, 10511 ip_stack_t *ipst) 10512 { 10513 struct opthdr *optp; 10514 mblk_t *mp2ctl; /* Returned */ 10515 mblk_t *mp3ctl; /* nettomedia */ 10516 mblk_t *mp4ctl; /* routeattrs */ 10517 iproutedata_t ird; 10518 zoneid_t zoneid; 10519 10520 /* 10521 * make copies of the original message 10522 * - mp2ctl is returned unchanged to the caller for his use 10523 * - mpctl is sent upstream as ipv6RouteEntryTable 10524 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable 10525 * - mp4ctl is sent upstream as ipv6RouteAttributeTable 10526 */ 10527 mp2ctl = copymsg(mpctl); 10528 mp3ctl = copymsg(mpctl); 10529 mp4ctl = copymsg(mpctl); 10530 if (mp3ctl == NULL || mp4ctl == NULL) { 10531 freemsg(mp4ctl); 10532 freemsg(mp3ctl); 10533 freemsg(mp2ctl); 10534 freemsg(mpctl); 10535 return (NULL); 10536 } 10537 10538 bzero(&ird, sizeof (ird)); 10539 10540 ird.ird_route.lp_head = mpctl->b_cont; 10541 ird.ird_netmedia.lp_head = mp3ctl->b_cont; 10542 ird.ird_attrs.lp_head = mp4ctl->b_cont; 10543 /* 10544 * If the level has been set the special EXPER_IP_AND_ALL_IRES value, 10545 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is 10546 * intended a temporary solution until a proper MIB API is provided 10547 * that provides complete filtering/caller-opt-in. 10548 */ 10549 if (level == EXPER_IP_AND_ALL_IRES) 10550 ird.ird_flags |= IRD_REPORT_ALL; 10551 10552 zoneid = Q_TO_CONN(q)->conn_zoneid; 10553 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst); 10554 10555 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10556 optp->level = MIB2_IP6; 10557 optp->name = MIB2_IP6_ROUTE; 10558 optp->len = msgdsize(ird.ird_route.lp_head); 10559 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 10560 (int)optp->level, (int)optp->name, (int)optp->len)); 10561 qreply(q, mpctl); 10562 10563 /* ipv6NetToMediaEntryTable in mp3ctl */ 10564 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst); 10565 10566 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10567 optp->level = MIB2_IP6; 10568 optp->name = MIB2_IP6_MEDIA; 10569 optp->len = msgdsize(ird.ird_netmedia.lp_head); 10570 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 10571 (int)optp->level, (int)optp->name, (int)optp->len)); 10572 qreply(q, mp3ctl); 10573 10574 /* ipv6RouteAttributeTable in mp4ctl */ 10575 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10576 optp->level = MIB2_IP6; 10577 optp->name = EXPER_IP_RTATTR; 10578 optp->len = msgdsize(ird.ird_attrs.lp_head); 10579 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n", 10580 (int)optp->level, (int)optp->name, (int)optp->len)); 10581 if (optp->len == 0) 10582 freemsg(mp4ctl); 10583 else 10584 qreply(q, mp4ctl); 10585 10586 return (mp2ctl); 10587 } 10588 10589 /* 10590 * IPv6 mib: One per ill 10591 */ 10592 static mblk_t * 10593 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10594 { 10595 struct opthdr *optp; 10596 mblk_t *mp2ctl; 10597 ill_t *ill; 10598 ill_walk_context_t ctx; 10599 mblk_t *mp_tail = NULL; 10600 10601 /* 10602 * Make a copy of the original message 10603 */ 10604 mp2ctl = copymsg(mpctl); 10605 10606 /* fixed length IPv6 structure ... */ 10607 10608 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10609 optp->level = MIB2_IP6; 10610 optp->name = 0; 10611 /* Include "unknown interface" ip6_mib */ 10612 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 10613 ipst->ips_ip6_mib.ipIfStatsIfIndex = 10614 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */ 10615 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding, 10616 ipst->ips_ipv6_forwarding ? 1 : 2); 10617 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit, 10618 ipst->ips_ipv6_def_hops); 10619 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize, 10620 sizeof (mib2_ipIfStatsEntry_t)); 10621 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize, 10622 sizeof (mib2_ipv6AddrEntry_t)); 10623 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize, 10624 sizeof (mib2_ipv6RouteEntry_t)); 10625 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize, 10626 sizeof (mib2_ipv6NetToMediaEntry_t)); 10627 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize, 10628 sizeof (ipv6_member_t)); 10629 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize, 10630 sizeof (ipv6_grpsrc_t)); 10631 10632 /* 10633 * Synchronize 64- and 32-bit counters 10634 */ 10635 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives, 10636 ipIfStatsHCInReceives); 10637 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers, 10638 ipIfStatsHCInDelivers); 10639 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests, 10640 ipIfStatsHCOutRequests); 10641 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams, 10642 ipIfStatsHCOutForwDatagrams); 10643 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts, 10644 ipIfStatsHCOutMcastPkts); 10645 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts, 10646 ipIfStatsHCInMcastPkts); 10647 10648 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 10649 (char *)&ipst->ips_ip6_mib, (int)sizeof (ipst->ips_ip6_mib))) { 10650 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n", 10651 (uint_t)sizeof (ipst->ips_ip6_mib))); 10652 } 10653 10654 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10655 ill = ILL_START_WALK_V6(&ctx, ipst); 10656 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 10657 ill->ill_ip_mib->ipIfStatsIfIndex = 10658 ill->ill_phyint->phyint_ifindex; 10659 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding, 10660 ipst->ips_ipv6_forwarding ? 1 : 2); 10661 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit, 10662 ill->ill_max_hops); 10663 10664 /* 10665 * Synchronize 64- and 32-bit counters 10666 */ 10667 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives, 10668 ipIfStatsHCInReceives); 10669 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers, 10670 ipIfStatsHCInDelivers); 10671 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests, 10672 ipIfStatsHCOutRequests); 10673 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams, 10674 ipIfStatsHCOutForwDatagrams); 10675 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts, 10676 ipIfStatsHCOutMcastPkts); 10677 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts, 10678 ipIfStatsHCInMcastPkts); 10679 10680 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 10681 (char *)ill->ill_ip_mib, 10682 (int)sizeof (*ill->ill_ip_mib))) { 10683 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate " 10684 "%u bytes\n", (uint_t)sizeof (*ill->ill_ip_mib))); 10685 } 10686 } 10687 rw_exit(&ipst->ips_ill_g_lock); 10688 10689 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10690 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n", 10691 (int)optp->level, (int)optp->name, (int)optp->len)); 10692 qreply(q, mpctl); 10693 return (mp2ctl); 10694 } 10695 10696 /* 10697 * ICMPv6 mib: One per ill 10698 */ 10699 static mblk_t * 10700 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst) 10701 { 10702 struct opthdr *optp; 10703 mblk_t *mp2ctl; 10704 ill_t *ill; 10705 ill_walk_context_t ctx; 10706 mblk_t *mp_tail = NULL; 10707 /* 10708 * Make a copy of the original message 10709 */ 10710 mp2ctl = copymsg(mpctl); 10711 10712 /* fixed length ICMPv6 structure ... */ 10713 10714 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 10715 optp->level = MIB2_ICMP6; 10716 optp->name = 0; 10717 /* Include "unknown interface" icmp6_mib */ 10718 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex = 10719 MIB2_UNKNOWN_INTERFACE; /* netstat flag */ 10720 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize = 10721 sizeof (mib2_ipv6IfIcmpEntry_t); 10722 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 10723 (char *)&ipst->ips_icmp6_mib, 10724 (int)sizeof (ipst->ips_icmp6_mib))) { 10725 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n", 10726 (uint_t)sizeof (ipst->ips_icmp6_mib))); 10727 } 10728 10729 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10730 ill = ILL_START_WALK_V6(&ctx, ipst); 10731 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 10732 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 10733 ill->ill_phyint->phyint_ifindex; 10734 if (!snmp_append_data2(mpctl->b_cont, &mp_tail, 10735 (char *)ill->ill_icmp6_mib, 10736 (int)sizeof (*ill->ill_icmp6_mib))) { 10737 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate " 10738 "%u bytes\n", 10739 (uint_t)sizeof (*ill->ill_icmp6_mib))); 10740 } 10741 } 10742 rw_exit(&ipst->ips_ill_g_lock); 10743 10744 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont); 10745 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n", 10746 (int)optp->level, (int)optp->name, (int)optp->len)); 10747 qreply(q, mpctl); 10748 return (mp2ctl); 10749 } 10750 10751 /* 10752 * ire_walk routine to create both ipRouteEntryTable and 10753 * ipRouteAttributeTable in one IRE walk 10754 */ 10755 static void 10756 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird) 10757 { 10758 ill_t *ill; 10759 mib2_ipRouteEntry_t *re; 10760 mib2_ipAttributeEntry_t iaes; 10761 tsol_ire_gw_secattr_t *attrp; 10762 tsol_gc_t *gc = NULL; 10763 tsol_gcgrp_t *gcgrp = NULL; 10764 ip_stack_t *ipst = ire->ire_ipst; 10765 10766 ASSERT(ire->ire_ipversion == IPV4_VERSION); 10767 10768 if (!(ird->ird_flags & IRD_REPORT_ALL)) { 10769 if (ire->ire_testhidden) 10770 return; 10771 if (ire->ire_type & IRE_IF_CLONE) 10772 return; 10773 } 10774 10775 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 10776 return; 10777 10778 if ((attrp = ire->ire_gw_secattr) != NULL) { 10779 mutex_enter(&attrp->igsa_lock); 10780 if ((gc = attrp->igsa_gc) != NULL) { 10781 gcgrp = gc->gc_grp; 10782 ASSERT(gcgrp != NULL); 10783 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 10784 } 10785 mutex_exit(&attrp->igsa_lock); 10786 } 10787 /* 10788 * Return all IRE types for route table... let caller pick and choose 10789 */ 10790 re->ipRouteDest = ire->ire_addr; 10791 ill = ire->ire_ill; 10792 re->ipRouteIfIndex.o_length = 0; 10793 if (ill != NULL) { 10794 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH); 10795 re->ipRouteIfIndex.o_length = 10796 mi_strlen(re->ipRouteIfIndex.o_bytes); 10797 } 10798 re->ipRouteMetric1 = -1; 10799 re->ipRouteMetric2 = -1; 10800 re->ipRouteMetric3 = -1; 10801 re->ipRouteMetric4 = -1; 10802 10803 re->ipRouteNextHop = ire->ire_gateway_addr; 10804 /* indirect(4), direct(3), or invalid(2) */ 10805 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 10806 re->ipRouteType = 2; 10807 else if (ire->ire_type & IRE_ONLINK) 10808 re->ipRouteType = 3; 10809 else 10810 re->ipRouteType = 4; 10811 10812 re->ipRouteProto = -1; 10813 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time; 10814 re->ipRouteMask = ire->ire_mask; 10815 re->ipRouteMetric5 = -1; 10816 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu; 10817 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0) 10818 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu; 10819 10820 re->ipRouteInfo.re_frag_flag = 0; 10821 re->ipRouteInfo.re_rtt = 0; 10822 re->ipRouteInfo.re_src_addr = 0; 10823 re->ipRouteInfo.re_ref = ire->ire_refcnt; 10824 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count; 10825 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 10826 re->ipRouteInfo.re_flags = ire->ire_flags; 10827 10828 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */ 10829 if (ire->ire_type & IRE_INTERFACE) { 10830 ire_t *child; 10831 10832 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 10833 child = ire->ire_dep_children; 10834 while (child != NULL) { 10835 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count; 10836 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count; 10837 child = child->ire_dep_sib_next; 10838 } 10839 rw_exit(&ipst->ips_ire_dep_lock); 10840 } 10841 10842 if (ire->ire_flags & RTF_DYNAMIC) { 10843 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT; 10844 } else { 10845 re->ipRouteInfo.re_ire_type = ire->ire_type; 10846 } 10847 10848 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 10849 (char *)re, (int)sizeof (*re))) { 10850 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n", 10851 (uint_t)sizeof (*re))); 10852 } 10853 10854 if (gc != NULL) { 10855 iaes.iae_routeidx = ird->ird_idx; 10856 iaes.iae_doi = gc->gc_db->gcdb_doi; 10857 iaes.iae_slrange = gc->gc_db->gcdb_slrange; 10858 10859 if (!snmp_append_data2(ird->ird_attrs.lp_head, 10860 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) { 10861 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u " 10862 "bytes\n", (uint_t)sizeof (iaes))); 10863 } 10864 } 10865 10866 /* bump route index for next pass */ 10867 ird->ird_idx++; 10868 10869 kmem_free(re, sizeof (*re)); 10870 if (gcgrp != NULL) 10871 rw_exit(&gcgrp->gcgrp_rwlock); 10872 } 10873 10874 /* 10875 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable. 10876 */ 10877 static void 10878 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird) 10879 { 10880 ill_t *ill; 10881 mib2_ipv6RouteEntry_t *re; 10882 mib2_ipAttributeEntry_t iaes; 10883 tsol_ire_gw_secattr_t *attrp; 10884 tsol_gc_t *gc = NULL; 10885 tsol_gcgrp_t *gcgrp = NULL; 10886 ip_stack_t *ipst = ire->ire_ipst; 10887 10888 ASSERT(ire->ire_ipversion == IPV6_VERSION); 10889 10890 if (!(ird->ird_flags & IRD_REPORT_ALL)) { 10891 if (ire->ire_testhidden) 10892 return; 10893 if (ire->ire_type & IRE_IF_CLONE) 10894 return; 10895 } 10896 10897 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL) 10898 return; 10899 10900 if ((attrp = ire->ire_gw_secattr) != NULL) { 10901 mutex_enter(&attrp->igsa_lock); 10902 if ((gc = attrp->igsa_gc) != NULL) { 10903 gcgrp = gc->gc_grp; 10904 ASSERT(gcgrp != NULL); 10905 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER); 10906 } 10907 mutex_exit(&attrp->igsa_lock); 10908 } 10909 /* 10910 * Return all IRE types for route table... let caller pick and choose 10911 */ 10912 re->ipv6RouteDest = ire->ire_addr_v6; 10913 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6); 10914 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */ 10915 re->ipv6RouteIfIndex.o_length = 0; 10916 ill = ire->ire_ill; 10917 if (ill != NULL) { 10918 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH); 10919 re->ipv6RouteIfIndex.o_length = 10920 mi_strlen(re->ipv6RouteIfIndex.o_bytes); 10921 } 10922 10923 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 10924 10925 mutex_enter(&ire->ire_lock); 10926 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6; 10927 mutex_exit(&ire->ire_lock); 10928 10929 /* remote(4), local(3), or discard(2) */ 10930 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 10931 re->ipv6RouteType = 2; 10932 else if (ire->ire_type & IRE_ONLINK) 10933 re->ipv6RouteType = 3; 10934 else 10935 re->ipv6RouteType = 4; 10936 10937 re->ipv6RouteProtocol = -1; 10938 re->ipv6RoutePolicy = 0; 10939 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time; 10940 re->ipv6RouteNextHopRDI = 0; 10941 re->ipv6RouteWeight = 0; 10942 re->ipv6RouteMetric = 0; 10943 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu; 10944 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0) 10945 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu; 10946 10947 re->ipv6RouteInfo.re_frag_flag = 0; 10948 re->ipv6RouteInfo.re_rtt = 0; 10949 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros; 10950 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count; 10951 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count; 10952 re->ipv6RouteInfo.re_ref = ire->ire_refcnt; 10953 re->ipv6RouteInfo.re_flags = ire->ire_flags; 10954 10955 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */ 10956 if (ire->ire_type & IRE_INTERFACE) { 10957 ire_t *child; 10958 10959 rw_enter(&ipst->ips_ire_dep_lock, RW_READER); 10960 child = ire->ire_dep_children; 10961 while (child != NULL) { 10962 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count; 10963 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count; 10964 child = child->ire_dep_sib_next; 10965 } 10966 rw_exit(&ipst->ips_ire_dep_lock); 10967 } 10968 if (ire->ire_flags & RTF_DYNAMIC) { 10969 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT; 10970 } else { 10971 re->ipv6RouteInfo.re_ire_type = ire->ire_type; 10972 } 10973 10974 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail, 10975 (char *)re, (int)sizeof (*re))) { 10976 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n", 10977 (uint_t)sizeof (*re))); 10978 } 10979 10980 if (gc != NULL) { 10981 iaes.iae_routeidx = ird->ird_idx; 10982 iaes.iae_doi = gc->gc_db->gcdb_doi; 10983 iaes.iae_slrange = gc->gc_db->gcdb_slrange; 10984 10985 if (!snmp_append_data2(ird->ird_attrs.lp_head, 10986 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) { 10987 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u " 10988 "bytes\n", (uint_t)sizeof (iaes))); 10989 } 10990 } 10991 10992 /* bump route index for next pass */ 10993 ird->ird_idx++; 10994 10995 kmem_free(re, sizeof (*re)); 10996 if (gcgrp != NULL) 10997 rw_exit(&gcgrp->gcgrp_rwlock); 10998 } 10999 11000 /* 11001 * ncec_walk routine to create ipv6NetToMediaEntryTable 11002 */ 11003 static int 11004 ip_snmp_get2_v6_media(ncec_t *ncec, iproutedata_t *ird) 11005 { 11006 ill_t *ill; 11007 mib2_ipv6NetToMediaEntry_t ntme; 11008 11009 ill = ncec->ncec_ill; 11010 /* skip arpce entries, and loopback ncec entries */ 11011 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK) 11012 return (0); 11013 /* 11014 * Neighbor cache entry attached to IRE with on-link 11015 * destination. 11016 * We report all IPMP groups on ncec_ill which is normally the upper. 11017 */ 11018 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex; 11019 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr; 11020 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length; 11021 if (ncec->ncec_lladdr != NULL) { 11022 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes, 11023 ntme.ipv6NetToMediaPhysAddress.o_length); 11024 } 11025 /* 11026 * Note: Returns ND_* states. Should be: 11027 * reachable(1), stale(2), delay(3), probe(4), 11028 * invalid(5), unknown(6) 11029 */ 11030 ntme.ipv6NetToMediaState = ncec->ncec_state; 11031 ntme.ipv6NetToMediaLastUpdated = 0; 11032 11033 /* other(1), dynamic(2), static(3), local(4) */ 11034 if (NCE_MYADDR(ncec)) { 11035 ntme.ipv6NetToMediaType = 4; 11036 } else if (ncec->ncec_flags & NCE_F_PUBLISH) { 11037 ntme.ipv6NetToMediaType = 1; /* proxy */ 11038 } else if (ncec->ncec_flags & NCE_F_STATIC) { 11039 ntme.ipv6NetToMediaType = 3; 11040 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) { 11041 ntme.ipv6NetToMediaType = 1; 11042 } else { 11043 ntme.ipv6NetToMediaType = 2; 11044 } 11045 11046 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 11047 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 11048 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n", 11049 (uint_t)sizeof (ntme))); 11050 } 11051 return (0); 11052 } 11053 11054 int 11055 nce2ace(ncec_t *ncec) 11056 { 11057 int flags = 0; 11058 11059 if (NCE_ISREACHABLE(ncec)) 11060 flags |= ACE_F_RESOLVED; 11061 if (ncec->ncec_flags & NCE_F_AUTHORITY) 11062 flags |= ACE_F_AUTHORITY; 11063 if (ncec->ncec_flags & NCE_F_PUBLISH) 11064 flags |= ACE_F_PUBLISH; 11065 if ((ncec->ncec_flags & NCE_F_NONUD) != 0) 11066 flags |= ACE_F_PERMANENT; 11067 if (NCE_MYADDR(ncec)) 11068 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY); 11069 if (ncec->ncec_flags & NCE_F_UNVERIFIED) 11070 flags |= ACE_F_UNVERIFIED; 11071 if (ncec->ncec_flags & NCE_F_AUTHORITY) 11072 flags |= ACE_F_AUTHORITY; 11073 if (ncec->ncec_flags & NCE_F_DELAYED) 11074 flags |= ACE_F_DELAYED; 11075 return (flags); 11076 } 11077 11078 /* 11079 * ncec_walk routine to create ipNetToMediaEntryTable 11080 */ 11081 static int 11082 ip_snmp_get2_v4_media(ncec_t *ncec, iproutedata_t *ird) 11083 { 11084 ill_t *ill; 11085 mib2_ipNetToMediaEntry_t ntme; 11086 const char *name = "unknown"; 11087 ipaddr_t ncec_addr; 11088 11089 ill = ncec->ncec_ill; 11090 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) || 11091 ill->ill_net_type == IRE_LOOPBACK) 11092 return (0); 11093 11094 /* We report all IPMP groups on ncec_ill which is normally the upper. */ 11095 name = ill->ill_name; 11096 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */ 11097 if (NCE_MYADDR(ncec)) { 11098 ntme.ipNetToMediaType = 4; 11099 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) { 11100 ntme.ipNetToMediaType = 1; 11101 } else { 11102 ntme.ipNetToMediaType = 3; 11103 } 11104 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name)); 11105 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes, 11106 ntme.ipNetToMediaIfIndex.o_length); 11107 11108 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr); 11109 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr)); 11110 11111 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t); 11112 ncec_addr = INADDR_BROADCAST; 11113 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes, 11114 sizeof (ncec_addr)); 11115 /* 11116 * map all the flags to the ACE counterpart. 11117 */ 11118 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec); 11119 11120 ntme.ipNetToMediaPhysAddress.o_length = 11121 MIN(OCTET_LENGTH, ill->ill_phys_addr_length); 11122 11123 if (!NCE_ISREACHABLE(ncec)) 11124 ntme.ipNetToMediaPhysAddress.o_length = 0; 11125 else { 11126 if (ncec->ncec_lladdr != NULL) { 11127 bcopy(ncec->ncec_lladdr, 11128 ntme.ipNetToMediaPhysAddress.o_bytes, 11129 ntme.ipNetToMediaPhysAddress.o_length); 11130 } 11131 } 11132 11133 if (!snmp_append_data2(ird->ird_netmedia.lp_head, 11134 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) { 11135 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n", 11136 (uint_t)sizeof (ntme))); 11137 } 11138 return (0); 11139 } 11140 11141 /* 11142 * return (0) if invalid set request, 1 otherwise, including non-tcp requests 11143 */ 11144 /* ARGSUSED */ 11145 int 11146 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 11147 { 11148 switch (level) { 11149 case MIB2_IP: 11150 case MIB2_ICMP: 11151 switch (name) { 11152 default: 11153 break; 11154 } 11155 return (1); 11156 default: 11157 return (1); 11158 } 11159 } 11160 11161 /* 11162 * When there exists both a 64- and 32-bit counter of a particular type 11163 * (i.e., InReceives), only the 64-bit counters are added. 11164 */ 11165 void 11166 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2) 11167 { 11168 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors); 11169 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors); 11170 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes); 11171 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors); 11172 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos); 11173 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts); 11174 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards); 11175 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards); 11176 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs); 11177 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails); 11178 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates); 11179 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds); 11180 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs); 11181 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails); 11182 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes); 11183 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates); 11184 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups); 11185 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits); 11186 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs); 11187 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows); 11188 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows); 11189 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion, 11190 o2->ipIfStatsInWrongIPVersion); 11191 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion, 11192 o2->ipIfStatsInWrongIPVersion); 11193 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion, 11194 o2->ipIfStatsOutSwitchIPVersion); 11195 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives); 11196 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets); 11197 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams, 11198 o2->ipIfStatsHCInForwDatagrams); 11199 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers); 11200 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests); 11201 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams, 11202 o2->ipIfStatsHCOutForwDatagrams); 11203 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds); 11204 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits); 11205 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets); 11206 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts); 11207 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets); 11208 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts); 11209 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets, 11210 o2->ipIfStatsHCOutMcastOctets); 11211 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts); 11212 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts); 11213 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded); 11214 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed); 11215 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs); 11216 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs); 11217 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts); 11218 } 11219 11220 void 11221 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2) 11222 { 11223 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs); 11224 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors); 11225 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs); 11226 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs); 11227 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds); 11228 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems); 11229 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs); 11230 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos); 11231 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies); 11232 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits, 11233 o2->ipv6IfIcmpInRouterSolicits); 11234 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements, 11235 o2->ipv6IfIcmpInRouterAdvertisements); 11236 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits, 11237 o2->ipv6IfIcmpInNeighborSolicits); 11238 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements, 11239 o2->ipv6IfIcmpInNeighborAdvertisements); 11240 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects); 11241 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries, 11242 o2->ipv6IfIcmpInGroupMembQueries); 11243 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses, 11244 o2->ipv6IfIcmpInGroupMembResponses); 11245 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions, 11246 o2->ipv6IfIcmpInGroupMembReductions); 11247 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs); 11248 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors); 11249 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs, 11250 o2->ipv6IfIcmpOutDestUnreachs); 11251 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs, 11252 o2->ipv6IfIcmpOutAdminProhibs); 11253 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds); 11254 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems, 11255 o2->ipv6IfIcmpOutParmProblems); 11256 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs); 11257 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos); 11258 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies); 11259 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits, 11260 o2->ipv6IfIcmpOutRouterSolicits); 11261 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements, 11262 o2->ipv6IfIcmpOutRouterAdvertisements); 11263 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits, 11264 o2->ipv6IfIcmpOutNeighborSolicits); 11265 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements, 11266 o2->ipv6IfIcmpOutNeighborAdvertisements); 11267 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects); 11268 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries, 11269 o2->ipv6IfIcmpOutGroupMembQueries); 11270 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses, 11271 o2->ipv6IfIcmpOutGroupMembResponses); 11272 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions, 11273 o2->ipv6IfIcmpOutGroupMembReductions); 11274 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows); 11275 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit); 11276 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements, 11277 o2->ipv6IfIcmpInBadNeighborAdvertisements); 11278 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations, 11279 o2->ipv6IfIcmpInBadNeighborSolicitations); 11280 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects); 11281 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal, 11282 o2->ipv6IfIcmpInGroupMembTotal); 11283 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries, 11284 o2->ipv6IfIcmpInGroupMembBadQueries); 11285 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports, 11286 o2->ipv6IfIcmpInGroupMembBadReports); 11287 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports, 11288 o2->ipv6IfIcmpInGroupMembOurReports); 11289 } 11290 11291 /* 11292 * Called before the options are updated to check if this packet will 11293 * be source routed from here. 11294 * This routine assumes that the options are well formed i.e. that they 11295 * have already been checked. 11296 */ 11297 boolean_t 11298 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst) 11299 { 11300 ipoptp_t opts; 11301 uchar_t *opt; 11302 uint8_t optval; 11303 uint8_t optlen; 11304 ipaddr_t dst; 11305 11306 if (IS_SIMPLE_IPH(ipha)) { 11307 ip2dbg(("not source routed\n")); 11308 return (B_FALSE); 11309 } 11310 dst = ipha->ipha_dst; 11311 for (optval = ipoptp_first(&opts, ipha); 11312 optval != IPOPT_EOL; 11313 optval = ipoptp_next(&opts)) { 11314 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 11315 opt = opts.ipoptp_cur; 11316 optlen = opts.ipoptp_len; 11317 ip2dbg(("ip_source_routed: opt %d, len %d\n", 11318 optval, optlen)); 11319 switch (optval) { 11320 uint32_t off; 11321 case IPOPT_SSRR: 11322 case IPOPT_LSRR: 11323 /* 11324 * If dst is one of our addresses and there are some 11325 * entries left in the source route return (true). 11326 */ 11327 if (ip_type_v4(dst, ipst) != IRE_LOCAL) { 11328 ip2dbg(("ip_source_routed: not next" 11329 " source route 0x%x\n", 11330 ntohl(dst))); 11331 return (B_FALSE); 11332 } 11333 off = opt[IPOPT_OFFSET]; 11334 off--; 11335 if (optlen < IP_ADDR_LEN || 11336 off > optlen - IP_ADDR_LEN) { 11337 /* End of source route */ 11338 ip1dbg(("ip_source_routed: end of SR\n")); 11339 return (B_FALSE); 11340 } 11341 return (B_TRUE); 11342 } 11343 } 11344 ip2dbg(("not source routed\n")); 11345 return (B_FALSE); 11346 } 11347 11348 /* 11349 * ip_unbind is called by the transports to remove a conn from 11350 * the fanout table. 11351 */ 11352 void 11353 ip_unbind(conn_t *connp) 11354 { 11355 11356 ASSERT(!MUTEX_HELD(&connp->conn_lock)); 11357 11358 if (is_system_labeled() && connp->conn_anon_port) { 11359 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 11360 connp->conn_mlp_type, connp->conn_proto, 11361 ntohs(connp->conn_lport), B_FALSE); 11362 connp->conn_anon_port = 0; 11363 } 11364 connp->conn_mlp_type = mlptSingle; 11365 11366 ipcl_hash_remove(connp); 11367 } 11368 11369 /* 11370 * Used for deciding the MSS size for the upper layer. Thus 11371 * we need to check the outbound policy values in the conn. 11372 */ 11373 int 11374 conn_ipsec_length(conn_t *connp) 11375 { 11376 ipsec_latch_t *ipl; 11377 11378 ipl = connp->conn_latch; 11379 if (ipl == NULL) 11380 return (0); 11381 11382 if (connp->conn_ixa->ixa_ipsec_policy == NULL) 11383 return (0); 11384 11385 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd); 11386 } 11387 11388 /* 11389 * Returns an estimate of the IPsec headers size. This is used if 11390 * we don't want to call into IPsec to get the exact size. 11391 */ 11392 int 11393 ipsec_out_extra_length(ip_xmit_attr_t *ixa) 11394 { 11395 ipsec_action_t *a; 11396 11397 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE)) 11398 return (0); 11399 11400 a = ixa->ixa_ipsec_action; 11401 if (a == NULL) { 11402 ASSERT(ixa->ixa_ipsec_policy != NULL); 11403 a = ixa->ixa_ipsec_policy->ipsp_act; 11404 } 11405 ASSERT(a != NULL); 11406 11407 return (a->ipa_ovhd); 11408 } 11409 11410 /* 11411 * If there are any source route options, return the true final 11412 * destination. Otherwise, return the destination. 11413 */ 11414 ipaddr_t 11415 ip_get_dst(ipha_t *ipha) 11416 { 11417 ipoptp_t opts; 11418 uchar_t *opt; 11419 uint8_t optval; 11420 uint8_t optlen; 11421 ipaddr_t dst; 11422 uint32_t off; 11423 11424 dst = ipha->ipha_dst; 11425 11426 if (IS_SIMPLE_IPH(ipha)) 11427 return (dst); 11428 11429 for (optval = ipoptp_first(&opts, ipha); 11430 optval != IPOPT_EOL; 11431 optval = ipoptp_next(&opts)) { 11432 opt = opts.ipoptp_cur; 11433 optlen = opts.ipoptp_len; 11434 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 11435 switch (optval) { 11436 case IPOPT_SSRR: 11437 case IPOPT_LSRR: 11438 off = opt[IPOPT_OFFSET]; 11439 /* 11440 * If one of the conditions is true, it means 11441 * end of options and dst already has the right 11442 * value. 11443 */ 11444 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 11445 off = optlen - IP_ADDR_LEN; 11446 bcopy(&opt[off], &dst, IP_ADDR_LEN); 11447 } 11448 return (dst); 11449 default: 11450 break; 11451 } 11452 } 11453 11454 return (dst); 11455 } 11456 11457 /* 11458 * Outbound IP fragmentation routine. 11459 * Assumes the caller has checked whether or not fragmentation should 11460 * be allowed. Here we copy the DF bit from the header to all the generated 11461 * fragments. 11462 */ 11463 int 11464 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags, 11465 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone, 11466 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie) 11467 { 11468 int i1; 11469 int hdr_len; 11470 mblk_t *hdr_mp; 11471 ipha_t *ipha; 11472 int ip_data_end; 11473 int len; 11474 mblk_t *mp = mp_orig; 11475 int offset; 11476 ill_t *ill = nce->nce_ill; 11477 ip_stack_t *ipst = ill->ill_ipst; 11478 mblk_t *carve_mp; 11479 uint32_t frag_flag; 11480 uint_t priority = mp->b_band; 11481 int error = 0; 11482 11483 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds); 11484 11485 if (pkt_len != msgdsize(mp)) { 11486 ip0dbg(("Packet length mismatch: %d, %ld\n", 11487 pkt_len, msgdsize(mp))); 11488 freemsg(mp); 11489 return (EINVAL); 11490 } 11491 11492 if (max_frag == 0) { 11493 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n")); 11494 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 11495 ip_drop_output("FragFails: zero max_frag", mp, ill); 11496 freemsg(mp); 11497 return (EINVAL); 11498 } 11499 11500 ASSERT(MBLKL(mp) >= sizeof (ipha_t)); 11501 ipha = (ipha_t *)mp->b_rptr; 11502 ASSERT(ntohs(ipha->ipha_length) == pkt_len); 11503 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF; 11504 11505 /* 11506 * Establish the starting offset. May not be zero if we are fragging 11507 * a fragment that is being forwarded. 11508 */ 11509 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET; 11510 11511 /* TODO why is this test needed? */ 11512 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) { 11513 /* TODO: notify ulp somehow */ 11514 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 11515 ip_drop_output("FragFails: bad starting offset", mp, ill); 11516 freemsg(mp); 11517 return (EINVAL); 11518 } 11519 11520 hdr_len = IPH_HDR_LENGTH(ipha); 11521 ipha->ipha_hdr_checksum = 0; 11522 11523 /* 11524 * Establish the number of bytes maximum per frag, after putting 11525 * in the header. 11526 */ 11527 len = (max_frag - hdr_len) & ~7; 11528 11529 /* Get a copy of the header for the trailing frags */ 11530 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst, 11531 mp); 11532 if (hdr_mp == NULL) { 11533 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 11534 ip_drop_output("FragFails: no hdr_mp", mp, ill); 11535 freemsg(mp); 11536 return (ENOBUFS); 11537 } 11538 11539 /* Store the starting offset, with the MoreFrags flag. */ 11540 i1 = offset | IPH_MF | frag_flag; 11541 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1); 11542 11543 /* Establish the ending byte offset, based on the starting offset. */ 11544 offset <<= 3; 11545 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len; 11546 11547 /* Store the length of the first fragment in the IP header. */ 11548 i1 = len + hdr_len; 11549 ASSERT(i1 <= IP_MAXPACKET); 11550 ipha->ipha_length = htons((uint16_t)i1); 11551 11552 /* 11553 * Compute the IP header checksum for the first frag. We have to 11554 * watch out that we stop at the end of the header. 11555 */ 11556 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 11557 11558 /* 11559 * Now carve off the first frag. Note that this will include the 11560 * original IP header. 11561 */ 11562 if (!(mp = ip_carve_mp(&mp_orig, i1))) { 11563 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 11564 ip_drop_output("FragFails: could not carve mp", mp_orig, ill); 11565 freeb(hdr_mp); 11566 freemsg(mp_orig); 11567 return (ENOBUFS); 11568 } 11569 11570 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates); 11571 11572 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid, 11573 ixa_cookie); 11574 if (error != 0 && error != EWOULDBLOCK) { 11575 /* No point in sending the other fragments */ 11576 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 11577 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill); 11578 freeb(hdr_mp); 11579 freemsg(mp_orig); 11580 return (error); 11581 } 11582 11583 /* No need to redo state machine in loop */ 11584 ixaflags &= ~IXAF_REACH_CONF; 11585 11586 /* Advance the offset to the second frag starting point. */ 11587 offset += len; 11588 /* 11589 * Update hdr_len from the copied header - there might be less options 11590 * in the later fragments. 11591 */ 11592 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr); 11593 /* Loop until done. */ 11594 for (;;) { 11595 uint16_t offset_and_flags; 11596 uint16_t ip_len; 11597 11598 if (ip_data_end - offset > len) { 11599 /* 11600 * Carve off the appropriate amount from the original 11601 * datagram. 11602 */ 11603 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 11604 mp = NULL; 11605 break; 11606 } 11607 /* 11608 * More frags after this one. Get another copy 11609 * of the header. 11610 */ 11611 if (carve_mp->b_datap->db_ref == 1 && 11612 hdr_mp->b_wptr - hdr_mp->b_rptr < 11613 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 11614 /* Inline IP header */ 11615 carve_mp->b_rptr -= hdr_mp->b_wptr - 11616 hdr_mp->b_rptr; 11617 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 11618 hdr_mp->b_wptr - hdr_mp->b_rptr); 11619 mp = carve_mp; 11620 } else { 11621 if (!(mp = copyb(hdr_mp))) { 11622 freemsg(carve_mp); 11623 break; 11624 } 11625 /* Get priority marking, if any. */ 11626 mp->b_band = priority; 11627 mp->b_cont = carve_mp; 11628 } 11629 ipha = (ipha_t *)mp->b_rptr; 11630 offset_and_flags = IPH_MF; 11631 } else { 11632 /* 11633 * Last frag. Consume the header. Set len to 11634 * the length of this last piece. 11635 */ 11636 len = ip_data_end - offset; 11637 11638 /* 11639 * Carve off the appropriate amount from the original 11640 * datagram. 11641 */ 11642 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) { 11643 mp = NULL; 11644 break; 11645 } 11646 if (carve_mp->b_datap->db_ref == 1 && 11647 hdr_mp->b_wptr - hdr_mp->b_rptr < 11648 carve_mp->b_rptr - carve_mp->b_datap->db_base) { 11649 /* Inline IP header */ 11650 carve_mp->b_rptr -= hdr_mp->b_wptr - 11651 hdr_mp->b_rptr; 11652 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr, 11653 hdr_mp->b_wptr - hdr_mp->b_rptr); 11654 mp = carve_mp; 11655 freeb(hdr_mp); 11656 hdr_mp = mp; 11657 } else { 11658 mp = hdr_mp; 11659 /* Get priority marking, if any. */ 11660 mp->b_band = priority; 11661 mp->b_cont = carve_mp; 11662 } 11663 ipha = (ipha_t *)mp->b_rptr; 11664 /* A frag of a frag might have IPH_MF non-zero */ 11665 offset_and_flags = 11666 ntohs(ipha->ipha_fragment_offset_and_flags) & 11667 IPH_MF; 11668 } 11669 offset_and_flags |= (uint16_t)(offset >> 3); 11670 offset_and_flags |= (uint16_t)frag_flag; 11671 /* Store the offset and flags in the IP header. */ 11672 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags); 11673 11674 /* Store the length in the IP header. */ 11675 ip_len = (uint16_t)(len + hdr_len); 11676 ipha->ipha_length = htons(ip_len); 11677 11678 /* 11679 * Set the IP header checksum. Note that mp is just 11680 * the header, so this is easy to pass to ip_csum. 11681 */ 11682 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); 11683 11684 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates); 11685 11686 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone, 11687 nolzid, ixa_cookie); 11688 /* All done if we just consumed the hdr_mp. */ 11689 if (mp == hdr_mp) { 11690 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs); 11691 return (error); 11692 } 11693 if (error != 0 && error != EWOULDBLOCK) { 11694 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill, 11695 mblk_t *, hdr_mp); 11696 /* No point in sending the other fragments */ 11697 break; 11698 } 11699 11700 /* Otherwise, advance and loop. */ 11701 offset += len; 11702 } 11703 /* Clean up following allocation failure. */ 11704 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails); 11705 ip_drop_output("FragFails: loop ended", NULL, ill); 11706 if (mp != hdr_mp) 11707 freeb(hdr_mp); 11708 if (mp != mp_orig) 11709 freemsg(mp_orig); 11710 return (error); 11711 } 11712 11713 /* 11714 * Copy the header plus those options which have the copy bit set 11715 */ 11716 static mblk_t * 11717 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst, 11718 mblk_t *src) 11719 { 11720 mblk_t *mp; 11721 uchar_t *up; 11722 11723 /* 11724 * Quick check if we need to look for options without the copy bit 11725 * set 11726 */ 11727 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src); 11728 if (!mp) 11729 return (mp); 11730 mp->b_rptr += ipst->ips_ip_wroff_extra; 11731 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) { 11732 bcopy(rptr, mp->b_rptr, hdr_len); 11733 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra; 11734 return (mp); 11735 } 11736 up = mp->b_rptr; 11737 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH); 11738 up += IP_SIMPLE_HDR_LENGTH; 11739 rptr += IP_SIMPLE_HDR_LENGTH; 11740 hdr_len -= IP_SIMPLE_HDR_LENGTH; 11741 while (hdr_len > 0) { 11742 uint32_t optval; 11743 uint32_t optlen; 11744 11745 optval = *rptr; 11746 if (optval == IPOPT_EOL) 11747 break; 11748 if (optval == IPOPT_NOP) 11749 optlen = 1; 11750 else 11751 optlen = rptr[1]; 11752 if (optval & IPOPT_COPY) { 11753 bcopy(rptr, up, optlen); 11754 up += optlen; 11755 } 11756 rptr += optlen; 11757 hdr_len -= optlen; 11758 } 11759 /* 11760 * Make sure that we drop an even number of words by filling 11761 * with EOL to the next word boundary. 11762 */ 11763 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH); 11764 hdr_len & 0x3; hdr_len++) 11765 *up++ = IPOPT_EOL; 11766 mp->b_wptr = up; 11767 /* Update header length */ 11768 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2)); 11769 return (mp); 11770 } 11771 11772 /* 11773 * Update any source route, record route, or timestamp options when 11774 * sending a packet back to ourselves. 11775 * Check that we are at end of strict source route. 11776 * The options have been sanity checked by ip_output_options(). 11777 */ 11778 void 11779 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst) 11780 { 11781 ipoptp_t opts; 11782 uchar_t *opt; 11783 uint8_t optval; 11784 uint8_t optlen; 11785 ipaddr_t dst; 11786 uint32_t ts; 11787 timestruc_t now; 11788 11789 for (optval = ipoptp_first(&opts, ipha); 11790 optval != IPOPT_EOL; 11791 optval = ipoptp_next(&opts)) { 11792 opt = opts.ipoptp_cur; 11793 optlen = opts.ipoptp_len; 11794 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); 11795 switch (optval) { 11796 uint32_t off; 11797 case IPOPT_SSRR: 11798 case IPOPT_LSRR: 11799 off = opt[IPOPT_OFFSET]; 11800 off--; 11801 if (optlen < IP_ADDR_LEN || 11802 off > optlen - IP_ADDR_LEN) { 11803 /* End of source route */ 11804 break; 11805 } 11806 /* 11807 * This will only happen if two consecutive entries 11808 * in the source route contains our address or if 11809 * it is a packet with a loose source route which 11810 * reaches us before consuming the whole source route 11811 */ 11812 11813 if (optval == IPOPT_SSRR) { 11814 return; 11815 } 11816 /* 11817 * Hack: instead of dropping the packet truncate the 11818 * source route to what has been used by filling the 11819 * rest with IPOPT_NOP. 11820 */ 11821 opt[IPOPT_OLEN] = (uint8_t)off; 11822 while (off < optlen) { 11823 opt[off++] = IPOPT_NOP; 11824 } 11825 break; 11826 case IPOPT_RR: 11827 off = opt[IPOPT_OFFSET]; 11828 off--; 11829 if (optlen < IP_ADDR_LEN || 11830 off > optlen - IP_ADDR_LEN) { 11831 /* No more room - ignore */ 11832 ip1dbg(( 11833 "ip_output_local_options: end of RR\n")); 11834 break; 11835 } 11836 dst = htonl(INADDR_LOOPBACK); 11837 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 11838 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 11839 break; 11840 case IPOPT_TS: 11841 /* Insert timestamp if there is romm */ 11842 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 11843 case IPOPT_TS_TSONLY: 11844 off = IPOPT_TS_TIMELEN; 11845 break; 11846 case IPOPT_TS_PRESPEC: 11847 case IPOPT_TS_PRESPEC_RFC791: 11848 /* Verify that the address matched */ 11849 off = opt[IPOPT_OFFSET] - 1; 11850 bcopy((char *)opt + off, &dst, IP_ADDR_LEN); 11851 if (ip_type_v4(dst, ipst) != IRE_LOCAL) { 11852 /* Not for us */ 11853 break; 11854 } 11855 /* FALLTHRU */ 11856 case IPOPT_TS_TSANDADDR: 11857 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 11858 break; 11859 default: 11860 /* 11861 * ip_*put_options should have already 11862 * dropped this packet. 11863 */ 11864 cmn_err(CE_PANIC, "ip_output_local_options: " 11865 "unknown IT - bug in ip_output_options?\n"); 11866 return; /* Keep "lint" happy */ 11867 } 11868 if (opt[IPOPT_OFFSET] - 1 + off > optlen) { 11869 /* Increase overflow counter */ 11870 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; 11871 opt[IPOPT_POS_OV_FLG] = (uint8_t) 11872 (opt[IPOPT_POS_OV_FLG] & 0x0F) | 11873 (off << 4); 11874 break; 11875 } 11876 off = opt[IPOPT_OFFSET] - 1; 11877 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 11878 case IPOPT_TS_PRESPEC: 11879 case IPOPT_TS_PRESPEC_RFC791: 11880 case IPOPT_TS_TSANDADDR: 11881 dst = htonl(INADDR_LOOPBACK); 11882 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN); 11883 opt[IPOPT_OFFSET] += IP_ADDR_LEN; 11884 /* FALLTHRU */ 11885 case IPOPT_TS_TSONLY: 11886 off = opt[IPOPT_OFFSET] - 1; 11887 /* Compute # of milliseconds since midnight */ 11888 gethrestime(&now); 11889 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + 11890 now.tv_nsec / (NANOSEC / MILLISEC); 11891 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); 11892 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; 11893 break; 11894 } 11895 break; 11896 } 11897 } 11898 } 11899 11900 /* 11901 * Prepend an M_DATA fastpath header, and if none present prepend a 11902 * DL_UNITDATA_REQ. Frees the mblk on failure. 11903 * 11904 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set. 11905 * If there is a change to them, the nce will be deleted (condemned) and 11906 * a new nce_t will be created when packets are sent. Thus we need no locks 11907 * to access those fields. 11908 * 11909 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended 11910 * we place b_band in dl_priority.dl_max. 11911 */ 11912 static mblk_t * 11913 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce) 11914 { 11915 uint_t hlen; 11916 mblk_t *mp1; 11917 uint_t priority; 11918 uchar_t *rptr; 11919 11920 rptr = mp->b_rptr; 11921 11922 ASSERT(DB_TYPE(mp) == M_DATA); 11923 priority = mp->b_band; 11924 11925 ASSERT(nce != NULL); 11926 if ((mp1 = nce->nce_fp_mp) != NULL) { 11927 hlen = MBLKL(mp1); 11928 /* 11929 * Check if we have enough room to prepend fastpath 11930 * header 11931 */ 11932 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) { 11933 rptr -= hlen; 11934 bcopy(mp1->b_rptr, rptr, hlen); 11935 /* 11936 * Set the b_rptr to the start of the link layer 11937 * header 11938 */ 11939 mp->b_rptr = rptr; 11940 return (mp); 11941 } 11942 mp1 = copyb(mp1); 11943 if (mp1 == NULL) { 11944 ill_t *ill = nce->nce_ill; 11945 11946 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 11947 ip_drop_output("ipIfStatsOutDiscards", mp, ill); 11948 freemsg(mp); 11949 return (NULL); 11950 } 11951 mp1->b_band = priority; 11952 mp1->b_cont = mp; 11953 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp); 11954 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp); 11955 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp); 11956 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp); 11957 DB_LSOMSS(mp1) = DB_LSOMSS(mp); 11958 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1); 11959 /* 11960 * XXX disable ICK_VALID and compute checksum 11961 * here; can happen if nce_fp_mp changes and 11962 * it can't be copied now due to insufficient 11963 * space. (unlikely, fp mp can change, but it 11964 * does not increase in length) 11965 */ 11966 return (mp1); 11967 } 11968 mp1 = copyb(nce->nce_dlur_mp); 11969 11970 if (mp1 == NULL) { 11971 ill_t *ill = nce->nce_ill; 11972 11973 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 11974 ip_drop_output("ipIfStatsOutDiscards", mp, ill); 11975 freemsg(mp); 11976 return (NULL); 11977 } 11978 mp1->b_cont = mp; 11979 if (priority != 0) { 11980 mp1->b_band = priority; 11981 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max = 11982 priority; 11983 } 11984 return (mp1); 11985 #undef rptr 11986 } 11987 11988 /* 11989 * Finish the outbound IPsec processing. This function is called from 11990 * ipsec_out_process() if the IPsec packet was processed 11991 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed 11992 * asynchronously. 11993 * 11994 * This is common to IPv4 and IPv6. 11995 */ 11996 int 11997 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa) 11998 { 11999 iaflags_t ixaflags = ixa->ixa_flags; 12000 uint_t pktlen; 12001 12002 12003 /* AH/ESP don't update ixa_pktlen when they modify the packet */ 12004 if (ixaflags & IXAF_IS_IPV4) { 12005 ipha_t *ipha = (ipha_t *)mp->b_rptr; 12006 12007 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 12008 pktlen = ntohs(ipha->ipha_length); 12009 } else { 12010 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 12011 12012 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION); 12013 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN; 12014 } 12015 12016 /* 12017 * We release any hard reference on the SAs here to make 12018 * sure the SAs can be garbage collected. ipsr_sa has a soft reference 12019 * on the SAs. 12020 * If in the future we want the hard latching of the SAs in the 12021 * ip_xmit_attr_t then we should remove this. 12022 */ 12023 if (ixa->ixa_ipsec_esp_sa != NULL) { 12024 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa); 12025 ixa->ixa_ipsec_esp_sa = NULL; 12026 } 12027 if (ixa->ixa_ipsec_ah_sa != NULL) { 12028 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa); 12029 ixa->ixa_ipsec_ah_sa = NULL; 12030 } 12031 12032 /* Do we need to fragment? */ 12033 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) || 12034 pktlen > ixa->ixa_fragsize) { 12035 if (ixaflags & IXAF_IS_IPV4) { 12036 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR)); 12037 /* 12038 * We check for the DF case in ipsec_out_process 12039 * hence this only handles the non-DF case. 12040 */ 12041 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags, 12042 pktlen, ixa->ixa_fragsize, 12043 ixa->ixa_xmit_hint, ixa->ixa_zoneid, 12044 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn, 12045 &ixa->ixa_cookie)); 12046 } else { 12047 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa); 12048 if (mp == NULL) { 12049 /* MIB and ip_drop_output already done */ 12050 return (ENOMEM); 12051 } 12052 pktlen += sizeof (ip6_frag_t); 12053 if (pktlen > ixa->ixa_fragsize) { 12054 return (ip_fragment_v6(mp, ixa->ixa_nce, 12055 ixa->ixa_flags, pktlen, 12056 ixa->ixa_fragsize, ixa->ixa_xmit_hint, 12057 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid, 12058 ixa->ixa_postfragfn, &ixa->ixa_cookie)); 12059 } 12060 } 12061 } 12062 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags, 12063 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid, 12064 ixa->ixa_no_loop_zoneid, NULL)); 12065 } 12066 12067 /* 12068 * Finish the inbound IPsec processing. This function is called from 12069 * ipsec_out_process() if the IPsec packet was processed 12070 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed 12071 * asynchronously. 12072 * 12073 * This is common to IPv4 and IPv6. 12074 */ 12075 void 12076 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira) 12077 { 12078 iaflags_t iraflags = ira->ira_flags; 12079 12080 /* Length might have changed */ 12081 if (iraflags & IRAF_IS_IPV4) { 12082 ipha_t *ipha = (ipha_t *)mp->b_rptr; 12083 12084 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 12085 ira->ira_pktlen = ntohs(ipha->ipha_length); 12086 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha); 12087 ira->ira_protocol = ipha->ipha_protocol; 12088 12089 ip_fanout_v4(mp, ipha, ira); 12090 } else { 12091 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 12092 uint8_t *nexthdrp; 12093 12094 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION); 12095 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN; 12096 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length, 12097 &nexthdrp)) { 12098 /* Malformed packet */ 12099 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards); 12100 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill); 12101 freemsg(mp); 12102 return; 12103 } 12104 ira->ira_protocol = *nexthdrp; 12105 ip_fanout_v6(mp, ip6h, ira); 12106 } 12107 } 12108 12109 /* 12110 * Select which AH & ESP SA's to use (if any) for the outbound packet. 12111 * 12112 * If this function returns B_TRUE, the requested SA's have been filled 12113 * into the ixa_ipsec_*_sa pointers. 12114 * 12115 * If the function returns B_FALSE, the packet has been "consumed", most 12116 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon. 12117 * 12118 * The SA references created by the protocol-specific "select" 12119 * function will be released in ip_output_post_ipsec. 12120 */ 12121 static boolean_t 12122 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa) 12123 { 12124 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE; 12125 ipsec_policy_t *pp; 12126 ipsec_action_t *ap; 12127 12128 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE); 12129 ASSERT((ixa->ixa_ipsec_policy != NULL) || 12130 (ixa->ixa_ipsec_action != NULL)); 12131 12132 ap = ixa->ixa_ipsec_action; 12133 if (ap == NULL) { 12134 pp = ixa->ixa_ipsec_policy; 12135 ASSERT(pp != NULL); 12136 ap = pp->ipsp_act; 12137 ASSERT(ap != NULL); 12138 } 12139 12140 /* 12141 * We have an action. now, let's select SA's. 12142 * A side effect of setting ixa_ipsec_*_sa is that it will 12143 * be cached in the conn_t. 12144 */ 12145 if (ap->ipa_want_esp) { 12146 if (ixa->ixa_ipsec_esp_sa == NULL) { 12147 need_esp_acquire = !ipsec_outbound_sa(mp, ixa, 12148 IPPROTO_ESP); 12149 } 12150 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL); 12151 } 12152 12153 if (ap->ipa_want_ah) { 12154 if (ixa->ixa_ipsec_ah_sa == NULL) { 12155 need_ah_acquire = !ipsec_outbound_sa(mp, ixa, 12156 IPPROTO_AH); 12157 } 12158 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL); 12159 /* 12160 * The ESP and AH processing order needs to be preserved 12161 * when both protocols are required (ESP should be applied 12162 * before AH for an outbound packet). Force an ESP ACQUIRE 12163 * when both ESP and AH are required, and an AH ACQUIRE 12164 * is needed. 12165 */ 12166 if (ap->ipa_want_esp && need_ah_acquire) 12167 need_esp_acquire = B_TRUE; 12168 } 12169 12170 /* 12171 * Send an ACQUIRE (extended, regular, or both) if we need one. 12172 * Release SAs that got referenced, but will not be used until we 12173 * acquire _all_ of the SAs we need. 12174 */ 12175 if (need_ah_acquire || need_esp_acquire) { 12176 if (ixa->ixa_ipsec_ah_sa != NULL) { 12177 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa); 12178 ixa->ixa_ipsec_ah_sa = NULL; 12179 } 12180 if (ixa->ixa_ipsec_esp_sa != NULL) { 12181 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa); 12182 ixa->ixa_ipsec_esp_sa = NULL; 12183 } 12184 12185 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire); 12186 return (B_FALSE); 12187 } 12188 12189 return (B_TRUE); 12190 } 12191 12192 /* 12193 * Handle IPsec output processing. 12194 * This function is only entered once for a given packet. 12195 * We try to do things synchronously, but if we need to have user-level 12196 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation 12197 * will be completed 12198 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish 12199 * - when asynchronous ESP is done it will do AH 12200 * 12201 * In all cases we come back in ip_output_post_ipsec() to fragment and 12202 * send out the packet. 12203 */ 12204 int 12205 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa) 12206 { 12207 ill_t *ill = ixa->ixa_nce->nce_ill; 12208 ip_stack_t *ipst = ixa->ixa_ipst; 12209 ipsec_stack_t *ipss; 12210 ipsec_policy_t *pp; 12211 ipsec_action_t *ap; 12212 12213 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE); 12214 12215 ASSERT((ixa->ixa_ipsec_policy != NULL) || 12216 (ixa->ixa_ipsec_action != NULL)); 12217 12218 ipss = ipst->ips_netstack->netstack_ipsec; 12219 if (!ipsec_loaded(ipss)) { 12220 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 12221 ip_drop_packet(mp, B_TRUE, ill, 12222 DROPPER(ipss, ipds_ip_ipsec_not_loaded), 12223 &ipss->ipsec_dropper); 12224 return (ENOTSUP); 12225 } 12226 12227 ap = ixa->ixa_ipsec_action; 12228 if (ap == NULL) { 12229 pp = ixa->ixa_ipsec_policy; 12230 ASSERT(pp != NULL); 12231 ap = pp->ipsp_act; 12232 ASSERT(ap != NULL); 12233 } 12234 12235 /* Handle explicit drop action and bypass. */ 12236 switch (ap->ipa_act.ipa_type) { 12237 case IPSEC_ACT_DISCARD: 12238 case IPSEC_ACT_REJECT: 12239 ip_drop_packet(mp, B_FALSE, ill, 12240 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper); 12241 return (EHOSTUNREACH); /* IPsec policy failure */ 12242 case IPSEC_ACT_BYPASS: 12243 return (ip_output_post_ipsec(mp, ixa)); 12244 } 12245 12246 /* 12247 * The order of processing is first insert a IP header if needed. 12248 * Then insert the ESP header and then the AH header. 12249 */ 12250 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) { 12251 /* 12252 * First get the outer IP header before sending 12253 * it to ESP. 12254 */ 12255 ipha_t *oipha, *iipha; 12256 mblk_t *outer_mp, *inner_mp; 12257 12258 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) { 12259 (void) mi_strlog(ill->ill_rq, 0, 12260 SL_ERROR|SL_TRACE|SL_CONSOLE, 12261 "ipsec_out_process: " 12262 "Self-Encapsulation failed: Out of memory\n"); 12263 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 12264 ip_drop_output("ipIfStatsOutDiscards", mp, ill); 12265 freemsg(mp); 12266 return (ENOBUFS); 12267 } 12268 inner_mp = mp; 12269 ASSERT(inner_mp->b_datap->db_type == M_DATA); 12270 oipha = (ipha_t *)outer_mp->b_rptr; 12271 iipha = (ipha_t *)inner_mp->b_rptr; 12272 *oipha = *iipha; 12273 outer_mp->b_wptr += sizeof (ipha_t); 12274 oipha->ipha_length = htons(ntohs(iipha->ipha_length) + 12275 sizeof (ipha_t)); 12276 oipha->ipha_protocol = IPPROTO_ENCAP; 12277 oipha->ipha_version_and_hdr_length = 12278 IP_SIMPLE_HDR_VERSION; 12279 oipha->ipha_hdr_checksum = 0; 12280 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha); 12281 outer_mp->b_cont = inner_mp; 12282 mp = outer_mp; 12283 12284 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL; 12285 } 12286 12287 /* If we need to wait for a SA then we can't return any errno */ 12288 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) || 12289 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) && 12290 !ipsec_out_select_sa(mp, ixa)) 12291 return (0); 12292 12293 /* 12294 * By now, we know what SA's to use. Toss over to ESP & AH 12295 * to do the heavy lifting. 12296 */ 12297 if (ap->ipa_want_esp) { 12298 ASSERT(ixa->ixa_ipsec_esp_sa != NULL); 12299 12300 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa); 12301 if (mp == NULL) { 12302 /* 12303 * Either it failed or is pending. In the former case 12304 * ipIfStatsInDiscards was increased. 12305 */ 12306 return (0); 12307 } 12308 } 12309 12310 if (ap->ipa_want_ah) { 12311 ASSERT(ixa->ixa_ipsec_ah_sa != NULL); 12312 12313 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa); 12314 if (mp == NULL) { 12315 /* 12316 * Either it failed or is pending. In the former case 12317 * ipIfStatsInDiscards was increased. 12318 */ 12319 return (0); 12320 } 12321 } 12322 /* 12323 * We are done with IPsec processing. Send it over 12324 * the wire. 12325 */ 12326 return (ip_output_post_ipsec(mp, ixa)); 12327 } 12328 12329 /* 12330 * ioctls that go through a down/up sequence may need to wait for the down 12331 * to complete. This involves waiting for the ire and ipif refcnts to go down 12332 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail. 12333 */ 12334 /* ARGSUSED */ 12335 void 12336 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 12337 { 12338 struct iocblk *iocp; 12339 mblk_t *mp1; 12340 ip_ioctl_cmd_t *ipip; 12341 int err; 12342 sin_t *sin; 12343 struct lifreq *lifr; 12344 struct ifreq *ifr; 12345 12346 iocp = (struct iocblk *)mp->b_rptr; 12347 ASSERT(ipsq != NULL); 12348 /* Existence of mp1 verified in ip_wput_nondata */ 12349 mp1 = mp->b_cont->b_cont; 12350 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 12351 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) { 12352 /* 12353 * Special case where ipx_current_ipif is not set: 12354 * ill_phyint_reinit merged the v4 and v6 into a single ipsq. 12355 * We are here as were not able to complete the operation in 12356 * ipif_set_values because we could not become exclusive on 12357 * the new ipsq. 12358 */ 12359 ill_t *ill = q->q_ptr; 12360 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd); 12361 } 12362 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL); 12363 12364 if (ipip->ipi_cmd_type == IF_CMD) { 12365 /* This a old style SIOC[GS]IF* command */ 12366 ifr = (struct ifreq *)mp1->b_rptr; 12367 sin = (sin_t *)&ifr->ifr_addr; 12368 } else if (ipip->ipi_cmd_type == LIF_CMD) { 12369 /* This a new style SIOC[GS]LIF* command */ 12370 lifr = (struct lifreq *)mp1->b_rptr; 12371 sin = (sin_t *)&lifr->lifr_addr; 12372 } else { 12373 sin = NULL; 12374 } 12375 12376 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin, 12377 q, mp, ipip, mp1->b_rptr); 12378 12379 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish", 12380 int, ipip->ipi_cmd, 12381 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill, 12382 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif); 12383 12384 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 12385 } 12386 12387 /* 12388 * ioctl processing 12389 * 12390 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up 12391 * the ioctl command in the ioctl tables, determines the copyin data size 12392 * from the ipi_copyin_size field, and does an mi_copyin() of that size. 12393 * 12394 * ioctl processing then continues when the M_IOCDATA makes its way down to 12395 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its 12396 * associated 'conn' is refheld till the end of the ioctl and the general 12397 * ioctl processing function ip_process_ioctl() is called to extract the 12398 * arguments and process the ioctl. To simplify extraction, ioctl commands 12399 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a 12400 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq()) 12401 * is used to extract the ioctl's arguments. 12402 * 12403 * ip_process_ioctl determines if the ioctl needs to be serialized, and if 12404 * so goes thru the serialization primitive ipsq_try_enter. Then the 12405 * appropriate function to handle the ioctl is called based on the entry in 12406 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish 12407 * which also refreleases the 'conn' that was refheld at the start of the 12408 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq. 12409 * 12410 * Many exclusive ioctls go thru an internal down up sequence as part of 12411 * the operation. For example an attempt to change the IP address of an 12412 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface 12413 * does all the cleanup such as deleting all ires that use this address. 12414 * Then we need to wait till all references to the interface go away. 12415 */ 12416 void 12417 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 12418 { 12419 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 12420 ip_ioctl_cmd_t *ipip = arg; 12421 ip_extract_func_t *extract_funcp; 12422 cmd_info_t ci; 12423 int err; 12424 boolean_t entered_ipsq = B_FALSE; 12425 12426 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd)); 12427 12428 if (ipip == NULL) 12429 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 12430 12431 /* 12432 * SIOCLIFADDIF needs to go thru a special path since the 12433 * ill may not exist yet. This happens in the case of lo0 12434 * which is created using this ioctl. 12435 */ 12436 if (ipip->ipi_cmd == SIOCLIFADDIF) { 12437 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL); 12438 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish", 12439 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL); 12440 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 12441 return; 12442 } 12443 12444 ci.ci_ipif = NULL; 12445 switch (ipip->ipi_cmd_type) { 12446 case MISC_CMD: 12447 case MSFILT_CMD: 12448 /* 12449 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF. 12450 */ 12451 if (ipip->ipi_cmd == IF_UNITSEL) { 12452 /* ioctl comes down the ill */ 12453 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif; 12454 ipif_refhold(ci.ci_ipif); 12455 } 12456 err = 0; 12457 ci.ci_sin = NULL; 12458 ci.ci_sin6 = NULL; 12459 ci.ci_lifr = NULL; 12460 extract_funcp = NULL; 12461 break; 12462 12463 case IF_CMD: 12464 case LIF_CMD: 12465 extract_funcp = ip_extract_lifreq; 12466 break; 12467 12468 case ARP_CMD: 12469 case XARP_CMD: 12470 extract_funcp = ip_extract_arpreq; 12471 break; 12472 12473 default: 12474 ASSERT(0); 12475 } 12476 12477 if (extract_funcp != NULL) { 12478 err = (*extract_funcp)(q, mp, ipip, &ci); 12479 if (err != 0) { 12480 DTRACE_PROBE4(ipif__ioctl, 12481 char *, "ip_process_ioctl finish err", 12482 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL); 12483 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 12484 return; 12485 } 12486 12487 /* 12488 * All of the extraction functions return a refheld ipif. 12489 */ 12490 ASSERT(ci.ci_ipif != NULL); 12491 } 12492 12493 if (!(ipip->ipi_flags & IPI_WR)) { 12494 /* 12495 * A return value of EINPROGRESS means the ioctl is 12496 * either queued and waiting for some reason or has 12497 * already completed. 12498 */ 12499 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, 12500 ci.ci_lifr); 12501 if (ci.ci_ipif != NULL) { 12502 DTRACE_PROBE4(ipif__ioctl, 12503 char *, "ip_process_ioctl finish RD", 12504 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill, 12505 ipif_t *, ci.ci_ipif); 12506 ipif_refrele(ci.ci_ipif); 12507 } else { 12508 DTRACE_PROBE4(ipif__ioctl, 12509 char *, "ip_process_ioctl finish RD", 12510 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL); 12511 } 12512 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL); 12513 return; 12514 } 12515 12516 ASSERT(ci.ci_ipif != NULL); 12517 12518 /* 12519 * If ipsq is non-NULL, we are already being called exclusively 12520 */ 12521 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq)); 12522 if (ipsq == NULL) { 12523 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl, 12524 NEW_OP, B_TRUE); 12525 if (ipsq == NULL) { 12526 ipif_refrele(ci.ci_ipif); 12527 return; 12528 } 12529 entered_ipsq = B_TRUE; 12530 } 12531 /* 12532 * Release the ipif so that ipif_down and friends that wait for 12533 * references to go away are not misled about the current ipif_refcnt 12534 * values. We are writer so we can access the ipif even after releasing 12535 * the ipif. 12536 */ 12537 ipif_refrele(ci.ci_ipif); 12538 12539 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd); 12540 12541 /* 12542 * A return value of EINPROGRESS means the ioctl is 12543 * either queued and waiting for some reason or has 12544 * already completed. 12545 */ 12546 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr); 12547 12548 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR", 12549 int, ipip->ipi_cmd, 12550 ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill, 12551 ipif_t *, ci.ci_ipif); 12552 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq); 12553 12554 if (entered_ipsq) 12555 ipsq_exit(ipsq); 12556 } 12557 12558 /* 12559 * Complete the ioctl. Typically ioctls use the mi package and need to 12560 * do mi_copyout/mi_copy_done. 12561 */ 12562 void 12563 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq) 12564 { 12565 conn_t *connp = NULL; 12566 12567 if (err == EINPROGRESS) 12568 return; 12569 12570 if (CONN_Q(q)) { 12571 connp = Q_TO_CONN(q); 12572 ASSERT(connp->conn_ref >= 2); 12573 } 12574 12575 switch (mode) { 12576 case COPYOUT: 12577 if (err == 0) 12578 mi_copyout(q, mp); 12579 else 12580 mi_copy_done(q, mp, err); 12581 break; 12582 12583 case NO_COPYOUT: 12584 mi_copy_done(q, mp, err); 12585 break; 12586 12587 default: 12588 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */ 12589 break; 12590 } 12591 12592 /* 12593 * The conn refhold and ioctlref placed on the conn at the start of the 12594 * ioctl are released here. 12595 */ 12596 if (connp != NULL) { 12597 CONN_DEC_IOCTLREF(connp); 12598 CONN_OPER_PENDING_DONE(connp); 12599 } 12600 12601 if (ipsq != NULL) 12602 ipsq_current_finish(ipsq); 12603 } 12604 12605 /* Handles all non data messages */ 12606 void 12607 ip_wput_nondata(queue_t *q, mblk_t *mp) 12608 { 12609 mblk_t *mp1; 12610 struct iocblk *iocp; 12611 ip_ioctl_cmd_t *ipip; 12612 conn_t *connp; 12613 cred_t *cr; 12614 char *proto_str; 12615 12616 if (CONN_Q(q)) 12617 connp = Q_TO_CONN(q); 12618 else 12619 connp = NULL; 12620 12621 switch (DB_TYPE(mp)) { 12622 case M_IOCTL: 12623 /* 12624 * IOCTL processing begins in ip_sioctl_copyin_setup which 12625 * will arrange to copy in associated control structures. 12626 */ 12627 ip_sioctl_copyin_setup(q, mp); 12628 return; 12629 case M_IOCDATA: 12630 /* 12631 * Ensure that this is associated with one of our trans- 12632 * parent ioctls. If it's not ours, discard it if we're 12633 * running as a driver, or pass it on if we're a module. 12634 */ 12635 iocp = (struct iocblk *)mp->b_rptr; 12636 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 12637 if (ipip == NULL) { 12638 if (q->q_next == NULL) { 12639 goto nak; 12640 } else { 12641 putnext(q, mp); 12642 } 12643 return; 12644 } 12645 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) { 12646 /* 12647 * The ioctl is one we recognise, but is not consumed 12648 * by IP as a module and we are a module, so we drop 12649 */ 12650 goto nak; 12651 } 12652 12653 /* IOCTL continuation following copyin or copyout. */ 12654 if (mi_copy_state(q, mp, NULL) == -1) { 12655 /* 12656 * The copy operation failed. mi_copy_state already 12657 * cleaned up, so we're out of here. 12658 */ 12659 return; 12660 } 12661 /* 12662 * If we just completed a copy in, we become writer and 12663 * continue processing in ip_sioctl_copyin_done. If it 12664 * was a copy out, we call mi_copyout again. If there is 12665 * nothing more to copy out, it will complete the IOCTL. 12666 */ 12667 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) { 12668 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) { 12669 mi_copy_done(q, mp, EPROTO); 12670 return; 12671 } 12672 /* 12673 * Check for cases that need more copying. A return 12674 * value of 0 means a second copyin has been started, 12675 * so we return; a return value of 1 means no more 12676 * copying is needed, so we continue. 12677 */ 12678 if (ipip->ipi_cmd_type == MSFILT_CMD && 12679 MI_COPY_COUNT(mp) == 1) { 12680 if (ip_copyin_msfilter(q, mp) == 0) 12681 return; 12682 } 12683 /* 12684 * Refhold the conn, till the ioctl completes. This is 12685 * needed in case the ioctl ends up in the pending mp 12686 * list. Every mp in the ipx_pending_mp list must have 12687 * a refhold on the conn to resume processing. The 12688 * refhold is released when the ioctl completes 12689 * (whether normally or abnormally). An ioctlref is also 12690 * placed on the conn to prevent TCP from removing the 12691 * queue needed to send the ioctl reply back. 12692 * In all cases ip_ioctl_finish is called to finish 12693 * the ioctl and release the refholds. 12694 */ 12695 if (connp != NULL) { 12696 /* This is not a reentry */ 12697 CONN_INC_REF(connp); 12698 CONN_INC_IOCTLREF(connp); 12699 } else { 12700 if (!(ipip->ipi_flags & IPI_MODOK)) { 12701 mi_copy_done(q, mp, EINVAL); 12702 return; 12703 } 12704 } 12705 12706 ip_process_ioctl(NULL, q, mp, ipip); 12707 12708 } else { 12709 mi_copyout(q, mp); 12710 } 12711 return; 12712 12713 case M_IOCNAK: 12714 /* 12715 * The only way we could get here is if a resolver didn't like 12716 * an IOCTL we sent it. This shouldn't happen. 12717 */ 12718 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 12719 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x", 12720 ((struct iocblk *)mp->b_rptr)->ioc_cmd); 12721 freemsg(mp); 12722 return; 12723 case M_IOCACK: 12724 /* /dev/ip shouldn't see this */ 12725 goto nak; 12726 case M_FLUSH: 12727 if (*mp->b_rptr & FLUSHW) 12728 flushq(q, FLUSHALL); 12729 if (q->q_next) { 12730 putnext(q, mp); 12731 return; 12732 } 12733 if (*mp->b_rptr & FLUSHR) { 12734 *mp->b_rptr &= ~FLUSHW; 12735 qreply(q, mp); 12736 return; 12737 } 12738 freemsg(mp); 12739 return; 12740 case M_CTL: 12741 break; 12742 case M_PROTO: 12743 case M_PCPROTO: 12744 /* 12745 * The only PROTO messages we expect are SNMP-related. 12746 */ 12747 switch (((union T_primitives *)mp->b_rptr)->type) { 12748 case T_SVR4_OPTMGMT_REQ: 12749 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ " 12750 "flags %x\n", 12751 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags)); 12752 12753 if (connp == NULL) { 12754 proto_str = "T_SVR4_OPTMGMT_REQ"; 12755 goto protonak; 12756 } 12757 12758 /* 12759 * All Solaris components should pass a db_credp 12760 * for this TPI message, hence we ASSERT. 12761 * But in case there is some other M_PROTO that looks 12762 * like a TPI message sent by some other kernel 12763 * component, we check and return an error. 12764 */ 12765 cr = msg_getcred(mp, NULL); 12766 ASSERT(cr != NULL); 12767 if (cr == NULL) { 12768 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 12769 if (mp != NULL) 12770 qreply(q, mp); 12771 return; 12772 } 12773 12774 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) { 12775 proto_str = "Bad SNMPCOM request?"; 12776 goto protonak; 12777 } 12778 return; 12779 default: 12780 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n", 12781 (int)*(uint_t *)mp->b_rptr)); 12782 freemsg(mp); 12783 return; 12784 } 12785 default: 12786 break; 12787 } 12788 if (q->q_next) { 12789 putnext(q, mp); 12790 } else 12791 freemsg(mp); 12792 return; 12793 12794 nak: 12795 iocp->ioc_error = EINVAL; 12796 mp->b_datap->db_type = M_IOCNAK; 12797 iocp->ioc_count = 0; 12798 qreply(q, mp); 12799 return; 12800 12801 protonak: 12802 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str); 12803 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL) 12804 qreply(q, mp); 12805 } 12806 12807 /* 12808 * Process IP options in an outbound packet. Verify that the nexthop in a 12809 * strict source route is onlink. 12810 * Returns non-zero if something fails in which case an ICMP error has been 12811 * sent and mp freed. 12812 * 12813 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst. 12814 */ 12815 int 12816 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill) 12817 { 12818 ipoptp_t opts; 12819 uchar_t *opt; 12820 uint8_t optval; 12821 uint8_t optlen; 12822 ipaddr_t dst; 12823 intptr_t code = 0; 12824 ire_t *ire; 12825 ip_stack_t *ipst = ixa->ixa_ipst; 12826 ip_recv_attr_t iras; 12827 12828 ip2dbg(("ip_output_options\n")); 12829 12830 dst = ipha->ipha_dst; 12831 for (optval = ipoptp_first(&opts, ipha); 12832 optval != IPOPT_EOL; 12833 optval = ipoptp_next(&opts)) { 12834 opt = opts.ipoptp_cur; 12835 optlen = opts.ipoptp_len; 12836 ip2dbg(("ip_output_options: opt %d, len %d\n", 12837 optval, optlen)); 12838 switch (optval) { 12839 uint32_t off; 12840 case IPOPT_SSRR: 12841 case IPOPT_LSRR: 12842 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 12843 ip1dbg(( 12844 "ip_output_options: bad option offset\n")); 12845 code = (char *)&opt[IPOPT_OLEN] - 12846 (char *)ipha; 12847 goto param_prob; 12848 } 12849 off = opt[IPOPT_OFFSET]; 12850 ip1dbg(("ip_output_options: next hop 0x%x\n", 12851 ntohl(dst))); 12852 /* 12853 * For strict: verify that dst is directly 12854 * reachable. 12855 */ 12856 if (optval == IPOPT_SSRR) { 12857 ire = ire_ftable_lookup_v4(dst, 0, 0, 12858 IRE_IF_ALL, NULL, ALL_ZONES, ixa->ixa_tsl, 12859 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst, 12860 NULL); 12861 if (ire == NULL) { 12862 ip1dbg(("ip_output_options: SSRR not" 12863 " directly reachable: 0x%x\n", 12864 ntohl(dst))); 12865 goto bad_src_route; 12866 } 12867 ire_refrele(ire); 12868 } 12869 break; 12870 case IPOPT_RR: 12871 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 12872 ip1dbg(( 12873 "ip_output_options: bad option offset\n")); 12874 code = (char *)&opt[IPOPT_OLEN] - 12875 (char *)ipha; 12876 goto param_prob; 12877 } 12878 break; 12879 case IPOPT_TS: 12880 /* 12881 * Verify that length >=5 and that there is either 12882 * room for another timestamp or that the overflow 12883 * counter is not maxed out. 12884 */ 12885 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha; 12886 if (optlen < IPOPT_MINLEN_IT) { 12887 goto param_prob; 12888 } 12889 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 12890 ip1dbg(( 12891 "ip_output_options: bad option offset\n")); 12892 code = (char *)&opt[IPOPT_OFFSET] - 12893 (char *)ipha; 12894 goto param_prob; 12895 } 12896 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { 12897 case IPOPT_TS_TSONLY: 12898 off = IPOPT_TS_TIMELEN; 12899 break; 12900 case IPOPT_TS_TSANDADDR: 12901 case IPOPT_TS_PRESPEC: 12902 case IPOPT_TS_PRESPEC_RFC791: 12903 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; 12904 break; 12905 default: 12906 code = (char *)&opt[IPOPT_POS_OV_FLG] - 12907 (char *)ipha; 12908 goto param_prob; 12909 } 12910 if (opt[IPOPT_OFFSET] - 1 + off > optlen && 12911 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) { 12912 /* 12913 * No room and the overflow counter is 15 12914 * already. 12915 */ 12916 goto param_prob; 12917 } 12918 break; 12919 } 12920 } 12921 12922 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) 12923 return (0); 12924 12925 ip1dbg(("ip_output_options: error processing IP options.")); 12926 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha; 12927 12928 param_prob: 12929 bzero(&iras, sizeof (iras)); 12930 iras.ira_ill = iras.ira_rill = ill; 12931 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex; 12932 iras.ira_rifindex = iras.ira_ruifindex; 12933 iras.ira_flags = IRAF_IS_IPV4; 12934 12935 ip_drop_output("ip_output_options", mp, ill); 12936 icmp_param_problem(mp, (uint8_t)code, &iras); 12937 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE)); 12938 return (-1); 12939 12940 bad_src_route: 12941 bzero(&iras, sizeof (iras)); 12942 iras.ira_ill = iras.ira_rill = ill; 12943 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex; 12944 iras.ira_rifindex = iras.ira_ruifindex; 12945 iras.ira_flags = IRAF_IS_IPV4; 12946 12947 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill); 12948 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras); 12949 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE)); 12950 return (-1); 12951 } 12952 12953 /* 12954 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT. 12955 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads 12956 * thru /etc/system. 12957 */ 12958 #define CONN_MAXDRAINCNT 64 12959 12960 static void 12961 conn_drain_init(ip_stack_t *ipst) 12962 { 12963 int i, j; 12964 idl_tx_list_t *itl_tx; 12965 12966 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads; 12967 12968 if ((ipst->ips_conn_drain_list_cnt == 0) || 12969 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) { 12970 /* 12971 * Default value of the number of drainers is the 12972 * number of cpus, subject to maximum of 8 drainers. 12973 */ 12974 if (boot_max_ncpus != -1) 12975 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8); 12976 else 12977 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8); 12978 } 12979 12980 ipst->ips_idl_tx_list = 12981 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP); 12982 for (i = 0; i < TX_FANOUT_SIZE; i++) { 12983 itl_tx = &ipst->ips_idl_tx_list[i]; 12984 itl_tx->txl_drain_list = 12985 kmem_zalloc(ipst->ips_conn_drain_list_cnt * 12986 sizeof (idl_t), KM_SLEEP); 12987 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL); 12988 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) { 12989 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL, 12990 MUTEX_DEFAULT, NULL); 12991 itl_tx->txl_drain_list[j].idl_itl = itl_tx; 12992 } 12993 } 12994 } 12995 12996 static void 12997 conn_drain_fini(ip_stack_t *ipst) 12998 { 12999 int i; 13000 idl_tx_list_t *itl_tx; 13001 13002 for (i = 0; i < TX_FANOUT_SIZE; i++) { 13003 itl_tx = &ipst->ips_idl_tx_list[i]; 13004 kmem_free(itl_tx->txl_drain_list, 13005 ipst->ips_conn_drain_list_cnt * sizeof (idl_t)); 13006 } 13007 kmem_free(ipst->ips_idl_tx_list, 13008 TX_FANOUT_SIZE * sizeof (idl_tx_list_t)); 13009 ipst->ips_idl_tx_list = NULL; 13010 } 13011 13012 /* 13013 * Note: For an overview of how flowcontrol is handled in IP please see the 13014 * IP Flowcontrol notes at the top of this file. 13015 * 13016 * Flow control has blocked us from proceeding. Insert the given conn in one 13017 * of the conn drain lists. These conn wq's will be qenabled later on when 13018 * STREAMS flow control does a backenable. conn_walk_drain will enable 13019 * the first conn in each of these drain lists. Each of these qenabled conns 13020 * in turn enables the next in the list, after it runs, or when it closes, 13021 * thus sustaining the drain process. 13022 */ 13023 void 13024 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list) 13025 { 13026 idl_t *idl = tx_list->txl_drain_list; 13027 uint_t index; 13028 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 13029 13030 mutex_enter(&connp->conn_lock); 13031 if (connp->conn_state_flags & CONN_CLOSING) { 13032 /* 13033 * The conn is closing as a result of which CONN_CLOSING 13034 * is set. Return. 13035 */ 13036 mutex_exit(&connp->conn_lock); 13037 return; 13038 } else if (connp->conn_idl == NULL) { 13039 /* 13040 * Assign the next drain list round robin. We dont' use 13041 * a lock, and thus it may not be strictly round robin. 13042 * Atomicity of load/stores is enough to make sure that 13043 * conn_drain_list_index is always within bounds. 13044 */ 13045 index = tx_list->txl_drain_index; 13046 ASSERT(index < ipst->ips_conn_drain_list_cnt); 13047 connp->conn_idl = &tx_list->txl_drain_list[index]; 13048 index++; 13049 if (index == ipst->ips_conn_drain_list_cnt) 13050 index = 0; 13051 tx_list->txl_drain_index = index; 13052 } 13053 mutex_exit(&connp->conn_lock); 13054 13055 mutex_enter(CONN_DRAIN_LIST_LOCK(connp)); 13056 if ((connp->conn_drain_prev != NULL) || 13057 (connp->conn_state_flags & CONN_CLOSING)) { 13058 /* 13059 * The conn is already in the drain list, OR 13060 * the conn is closing. We need to check again for 13061 * the closing case again since close can happen 13062 * after we drop the conn_lock, and before we 13063 * acquire the CONN_DRAIN_LIST_LOCK. 13064 */ 13065 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 13066 return; 13067 } else { 13068 idl = connp->conn_idl; 13069 } 13070 13071 /* 13072 * The conn is not in the drain list. Insert it at the 13073 * tail of the drain list. The drain list is circular 13074 * and doubly linked. idl_conn points to the 1st element 13075 * in the list. 13076 */ 13077 if (idl->idl_conn == NULL) { 13078 idl->idl_conn = connp; 13079 connp->conn_drain_next = connp; 13080 connp->conn_drain_prev = connp; 13081 } else { 13082 conn_t *head = idl->idl_conn; 13083 13084 connp->conn_drain_next = head; 13085 connp->conn_drain_prev = head->conn_drain_prev; 13086 head->conn_drain_prev->conn_drain_next = connp; 13087 head->conn_drain_prev = connp; 13088 } 13089 /* 13090 * For non streams based sockets assert flow control. 13091 */ 13092 conn_setqfull(connp, NULL); 13093 mutex_exit(CONN_DRAIN_LIST_LOCK(connp)); 13094 } 13095 13096 static void 13097 conn_idl_remove(conn_t *connp) 13098 { 13099 idl_t *idl = connp->conn_idl; 13100 13101 if (idl != NULL) { 13102 /* 13103 * Remove ourself from the drain list, if we did not do 13104 * a putq, or if the conn is closing. 13105 * Note: It is possible that q->q_first is non-null. It means 13106 * that these messages landed after we did a enableok() in 13107 * ip_wsrv. Thus STREAMS will call ip_wsrv once again to 13108 * service them. 13109 */ 13110 if (connp->conn_drain_next == connp) { 13111 /* Singleton in the list */ 13112 ASSERT(connp->conn_drain_prev == connp); 13113 idl->idl_conn = NULL; 13114 } else { 13115 connp->conn_drain_prev->conn_drain_next = 13116 connp->conn_drain_next; 13117 connp->conn_drain_next->conn_drain_prev = 13118 connp->conn_drain_prev; 13119 if (idl->idl_conn == connp) 13120 idl->idl_conn = connp->conn_drain_next; 13121 } 13122 } 13123 connp->conn_drain_next = NULL; 13124 connp->conn_drain_prev = NULL; 13125 13126 conn_clrqfull(connp, NULL); 13127 /* 13128 * For streams based sockets open up flow control. 13129 */ 13130 if (!IPCL_IS_NONSTR(connp)) 13131 enableok(connp->conn_wq); 13132 } 13133 13134 /* 13135 * This conn is closing, and we are called from ip_close. OR 13136 * this conn is draining because flow-control on the ill has been relieved. 13137 * 13138 * We must also need to remove conn's on this idl from the list, and also 13139 * inform the sockfs upcalls about the change in flow-control. 13140 */ 13141 static void 13142 conn_drain_tail(conn_t *connp, boolean_t closing) 13143 { 13144 idl_t *idl; 13145 conn_t *next_connp; 13146 13147 /* 13148 * connp->conn_idl is stable at this point, and no lock is needed 13149 * to check it. If we are called from ip_close, close has already 13150 * set CONN_CLOSING, thus freezing the value of conn_idl, and 13151 * called us only because conn_idl is non-null. If we are called thru 13152 * service, conn_idl could be null, but it cannot change because 13153 * service is single-threaded per queue, and there cannot be another 13154 * instance of service trying to call conn_drain_insert on this conn 13155 * now. 13156 */ 13157 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL); 13158 13159 /* 13160 * If connp->conn_idl is null, the conn has not been inserted into any 13161 * drain list even once since creation of the conn. Just return. 13162 */ 13163 if (connp == NULL || connp->conn_idl == NULL) 13164 return; 13165 13166 if (connp->conn_drain_prev == NULL) { 13167 /* This conn is currently not in the drain list. */ 13168 return; 13169 } 13170 idl = connp->conn_idl; 13171 if (!closing) { 13172 /* 13173 * This conn is the current drainer. If this is the last conn 13174 * in the drain list, we need to do more checks, in the 'if' 13175 * below. Otherwwise we need to just qenable the next conn, 13176 * to sustain the draining, and is handled in the 'else' 13177 * below. 13178 */ 13179 next_connp = connp->conn_drain_next; 13180 while (next_connp != connp) { 13181 conn_t *delconnp = next_connp; 13182 13183 next_connp = next_connp->conn_drain_next; 13184 conn_idl_remove(delconnp); 13185 } 13186 ASSERT(connp->conn_drain_next == idl->idl_conn); 13187 } 13188 conn_idl_remove(connp); 13189 13190 } 13191 13192 /* 13193 * Write service routine. Shared perimeter entry point. 13194 * The device queue's messages has fallen below the low water mark and STREAMS 13195 * has backenabled the ill_wq. Send sockfs notification about flow-control onx 13196 * each waiting conn. 13197 */ 13198 void 13199 ip_wsrv(queue_t *q) 13200 { 13201 ill_t *ill; 13202 13203 ill = (ill_t *)q->q_ptr; 13204 if (ill->ill_state_flags == 0) { 13205 ip_stack_t *ipst = ill->ill_ipst; 13206 13207 /* 13208 * The device flow control has opened up. 13209 * Walk through conn drain lists and qenable the 13210 * first conn in each list. This makes sense only 13211 * if the stream is fully plumbed and setup. 13212 * Hence the ill_state_flags check above. 13213 */ 13214 ip1dbg(("ip_wsrv: walking\n")); 13215 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]); 13216 enableok(ill->ill_wq); 13217 } 13218 } 13219 13220 /* 13221 * Callback to disable flow control in IP. 13222 * 13223 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability 13224 * is enabled. 13225 * 13226 * When MAC_TX() is not able to send any more packets, dld sets its queue 13227 * to QFULL and enable the STREAMS flow control. Later, when the underlying 13228 * driver is able to continue to send packets, it calls mac_tx_(ring_)update() 13229 * function and wakes up corresponding mac worker threads, which in turn 13230 * calls this callback function, and disables flow control. 13231 */ 13232 void 13233 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie) 13234 { 13235 ill_t *ill = (ill_t *)arg; 13236 ip_stack_t *ipst = ill->ill_ipst; 13237 idl_tx_list_t *idl_txl; 13238 13239 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)]; 13240 mutex_enter(&idl_txl->txl_lock); 13241 /* add code to to set a flag to indicate idl_txl is enabled */ 13242 conn_walk_drain(ipst, idl_txl); 13243 mutex_exit(&idl_txl->txl_lock); 13244 } 13245 13246 /* 13247 * Flowcontrol has relieved, and STREAMS has backenabled us. For each list 13248 * of conns that need to be drained, check if drain is already in progress. 13249 * If so set the idl_repeat bit, indicating that the last conn in the list 13250 * needs to reinitiate the drain once again, for the list. If drain is not 13251 * in progress for the list, initiate the draining, by qenabling the 1st 13252 * conn in the list. The drain is self-sustaining, each qenabled conn will 13253 * in turn qenable the next conn, when it is done/blocked/closing. 13254 */ 13255 static void 13256 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list) 13257 { 13258 int i; 13259 idl_t *idl; 13260 13261 IP_STAT(ipst, ip_conn_walk_drain); 13262 13263 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) { 13264 idl = &tx_list->txl_drain_list[i]; 13265 mutex_enter(&idl->idl_lock); 13266 conn_drain_tail(idl->idl_conn, B_FALSE); 13267 mutex_exit(&idl->idl_lock); 13268 } 13269 } 13270 13271 /* 13272 * Determine if the ill and multicast aspects of that packets 13273 * "matches" the conn. 13274 */ 13275 boolean_t 13276 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha) 13277 { 13278 ill_t *ill = ira->ira_rill; 13279 zoneid_t zoneid = ira->ira_zoneid; 13280 uint_t in_ifindex; 13281 ipaddr_t dst, src; 13282 13283 dst = ipha->ipha_dst; 13284 src = ipha->ipha_src; 13285 13286 /* 13287 * conn_incoming_ifindex is set by IP_BOUND_IF which limits 13288 * unicast, broadcast and multicast reception to 13289 * conn_incoming_ifindex. 13290 * conn_wantpacket is called for unicast, broadcast and 13291 * multicast packets. 13292 */ 13293 in_ifindex = connp->conn_incoming_ifindex; 13294 13295 /* mpathd can bind to the under IPMP interface, which we allow */ 13296 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) { 13297 if (!IS_UNDER_IPMP(ill)) 13298 return (B_FALSE); 13299 13300 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill)) 13301 return (B_FALSE); 13302 } 13303 13304 if (!IPCL_ZONE_MATCH(connp, zoneid)) 13305 return (B_FALSE); 13306 13307 if (!(ira->ira_flags & IRAF_MULTICAST)) 13308 return (B_TRUE); 13309 13310 if (connp->conn_multi_router) { 13311 /* multicast packet and multicast router socket: send up */ 13312 return (B_TRUE); 13313 } 13314 13315 if (ipha->ipha_protocol == IPPROTO_PIM || 13316 ipha->ipha_protocol == IPPROTO_RSVP) 13317 return (B_TRUE); 13318 13319 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill)); 13320 } 13321 13322 void 13323 conn_setqfull(conn_t *connp, boolean_t *flow_stopped) 13324 { 13325 if (IPCL_IS_NONSTR(connp)) { 13326 (*connp->conn_upcalls->su_txq_full) 13327 (connp->conn_upper_handle, B_TRUE); 13328 if (flow_stopped != NULL) 13329 *flow_stopped = B_TRUE; 13330 } else { 13331 queue_t *q = connp->conn_wq; 13332 13333 ASSERT(q != NULL); 13334 if (!(q->q_flag & QFULL)) { 13335 mutex_enter(QLOCK(q)); 13336 if (!(q->q_flag & QFULL)) { 13337 /* still need to set QFULL */ 13338 q->q_flag |= QFULL; 13339 /* set flow_stopped to true under QLOCK */ 13340 if (flow_stopped != NULL) 13341 *flow_stopped = B_TRUE; 13342 mutex_exit(QLOCK(q)); 13343 } else { 13344 /* flow_stopped is left unchanged */ 13345 mutex_exit(QLOCK(q)); 13346 } 13347 } 13348 } 13349 } 13350 13351 void 13352 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped) 13353 { 13354 if (IPCL_IS_NONSTR(connp)) { 13355 (*connp->conn_upcalls->su_txq_full) 13356 (connp->conn_upper_handle, B_FALSE); 13357 if (flow_stopped != NULL) 13358 *flow_stopped = B_FALSE; 13359 } else { 13360 queue_t *q = connp->conn_wq; 13361 13362 ASSERT(q != NULL); 13363 if (q->q_flag & QFULL) { 13364 mutex_enter(QLOCK(q)); 13365 if (q->q_flag & QFULL) { 13366 q->q_flag &= ~QFULL; 13367 /* set flow_stopped to false under QLOCK */ 13368 if (flow_stopped != NULL) 13369 *flow_stopped = B_FALSE; 13370 mutex_exit(QLOCK(q)); 13371 if (q->q_flag & QWANTW) 13372 qbackenable(q, 0); 13373 } else { 13374 /* flow_stopped is left unchanged */ 13375 mutex_exit(QLOCK(q)); 13376 } 13377 } 13378 } 13379 connp->conn_direct_blocked = B_FALSE; 13380 } 13381 13382 /* 13383 * Return the length in bytes of the IPv4 headers (base header, label, and 13384 * other IP options) that will be needed based on the 13385 * ip_pkt_t structure passed by the caller. 13386 * 13387 * The returned length does not include the length of the upper level 13388 * protocol (ULP) header. 13389 * The caller needs to check that the length doesn't exceed the max for IPv4. 13390 */ 13391 int 13392 ip_total_hdrs_len_v4(const ip_pkt_t *ipp) 13393 { 13394 int len; 13395 13396 len = IP_SIMPLE_HDR_LENGTH; 13397 if (ipp->ipp_fields & IPPF_LABEL_V4) { 13398 ASSERT(ipp->ipp_label_len_v4 != 0); 13399 /* We need to round up here */ 13400 len += (ipp->ipp_label_len_v4 + 3) & ~3; 13401 } 13402 13403 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) { 13404 ASSERT(ipp->ipp_ipv4_options_len != 0); 13405 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0); 13406 len += ipp->ipp_ipv4_options_len; 13407 } 13408 return (len); 13409 } 13410 13411 /* 13412 * All-purpose routine to build an IPv4 header with options based 13413 * on the abstract ip_pkt_t. 13414 * 13415 * The caller has to set the source and destination address as well as 13416 * ipha_length. The caller has to massage any source route and compensate 13417 * for the ULP pseudo-header checksum due to the source route. 13418 */ 13419 void 13420 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp, 13421 uint8_t protocol) 13422 { 13423 ipha_t *ipha = (ipha_t *)buf; 13424 uint8_t *cp; 13425 13426 /* Initialize IPv4 header */ 13427 ipha->ipha_type_of_service = ipp->ipp_type_of_service; 13428 ipha->ipha_length = 0; /* Caller will set later */ 13429 ipha->ipha_ident = 0; 13430 ipha->ipha_fragment_offset_and_flags = 0; 13431 ipha->ipha_ttl = ipp->ipp_unicast_hops; 13432 ipha->ipha_protocol = protocol; 13433 ipha->ipha_hdr_checksum = 0; 13434 13435 if ((ipp->ipp_fields & IPPF_ADDR) && 13436 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) 13437 ipha->ipha_src = ipp->ipp_addr_v4; 13438 13439 cp = (uint8_t *)&ipha[1]; 13440 if (ipp->ipp_fields & IPPF_LABEL_V4) { 13441 ASSERT(ipp->ipp_label_len_v4 != 0); 13442 bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4); 13443 cp += ipp->ipp_label_len_v4; 13444 /* We need to round up here */ 13445 while ((uintptr_t)cp & 0x3) { 13446 *cp++ = IPOPT_NOP; 13447 } 13448 } 13449 13450 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) { 13451 ASSERT(ipp->ipp_ipv4_options_len != 0); 13452 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0); 13453 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len); 13454 cp += ipp->ipp_ipv4_options_len; 13455 } 13456 ipha->ipha_version_and_hdr_length = 13457 (uint8_t)((IP_VERSION << 4) + buf_len / 4); 13458 13459 ASSERT((int)(cp - buf) == buf_len); 13460 } 13461 13462 /* Allocate the private structure */ 13463 static int 13464 ip_priv_alloc(void **bufp) 13465 { 13466 void *buf; 13467 13468 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL) 13469 return (ENOMEM); 13470 13471 *bufp = buf; 13472 return (0); 13473 } 13474 13475 /* Function to delete the private structure */ 13476 void 13477 ip_priv_free(void *buf) 13478 { 13479 ASSERT(buf != NULL); 13480 kmem_free(buf, sizeof (ip_priv_t)); 13481 } 13482 13483 /* 13484 * The entry point for IPPF processing. 13485 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the 13486 * routine just returns. 13487 * 13488 * When called, ip_process generates an ipp_packet_t structure 13489 * which holds the state information for this packet and invokes the 13490 * the classifier (via ipp_packet_process). The classification, depending on 13491 * configured filters, results in a list of actions for this packet. Invoking 13492 * an action may cause the packet to be dropped, in which case we return NULL. 13493 * proc indicates the callout position for 13494 * this packet and ill is the interface this packet arrived on or will leave 13495 * on (inbound and outbound resp.). 13496 * 13497 * We do the processing on the rill (mapped to the upper if ipmp), but MIB 13498 * on the ill corrsponding to the destination IP address. 13499 */ 13500 mblk_t * 13501 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill) 13502 { 13503 ip_priv_t *priv; 13504 ipp_action_id_t aid; 13505 int rc = 0; 13506 ipp_packet_t *pp; 13507 13508 /* If the classifier is not loaded, return */ 13509 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) { 13510 return (mp); 13511 } 13512 13513 ASSERT(mp != NULL); 13514 13515 /* Allocate the packet structure */ 13516 rc = ipp_packet_alloc(&pp, "ip", aid); 13517 if (rc != 0) 13518 goto drop; 13519 13520 /* Allocate the private structure */ 13521 rc = ip_priv_alloc((void **)&priv); 13522 if (rc != 0) { 13523 ipp_packet_free(pp); 13524 goto drop; 13525 } 13526 priv->proc = proc; 13527 priv->ill_index = ill_get_upper_ifindex(rill); 13528 13529 ipp_packet_set_private(pp, priv, ip_priv_free); 13530 ipp_packet_set_data(pp, mp); 13531 13532 /* Invoke the classifier */ 13533 rc = ipp_packet_process(&pp); 13534 if (pp != NULL) { 13535 mp = ipp_packet_get_data(pp); 13536 ipp_packet_free(pp); 13537 if (rc != 0) 13538 goto drop; 13539 return (mp); 13540 } else { 13541 /* No mp to trace in ip_drop_input/ip_drop_output */ 13542 mp = NULL; 13543 } 13544 drop: 13545 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) { 13546 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 13547 ip_drop_input("ip_process", mp, ill); 13548 } else { 13549 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 13550 ip_drop_output("ip_process", mp, ill); 13551 } 13552 freemsg(mp); 13553 return (NULL); 13554 } 13555 13556 /* 13557 * Propagate a multicast group membership operation (add/drop) on 13558 * all the interfaces crossed by the related multirt routes. 13559 * The call is considered successful if the operation succeeds 13560 * on at least one interface. 13561 * 13562 * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the 13563 * multicast addresses with the ire argument being the first one. 13564 * We walk the bucket to find all the of those. 13565 * 13566 * Common to IPv4 and IPv6. 13567 */ 13568 static int 13569 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, 13570 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *), 13571 ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group, 13572 mcast_record_t fmode, const in6_addr_t *v6src) 13573 { 13574 ire_t *ire_gw; 13575 irb_t *irb; 13576 int ifindex; 13577 int error = 0; 13578 int result; 13579 ip_stack_t *ipst = ire->ire_ipst; 13580 ipaddr_t group; 13581 boolean_t isv6; 13582 int match_flags; 13583 13584 if (IN6_IS_ADDR_V4MAPPED(v6group)) { 13585 IN6_V4MAPPED_TO_IPADDR(v6group, group); 13586 isv6 = B_FALSE; 13587 } else { 13588 isv6 = B_TRUE; 13589 } 13590 13591 irb = ire->ire_bucket; 13592 ASSERT(irb != NULL); 13593 13594 result = 0; 13595 irb_refhold(irb); 13596 for (; ire != NULL; ire = ire->ire_next) { 13597 if ((ire->ire_flags & RTF_MULTIRT) == 0) 13598 continue; 13599 13600 /* We handle -ifp routes by matching on the ill if set */ 13601 match_flags = MATCH_IRE_TYPE; 13602 if (ire->ire_ill != NULL) 13603 match_flags |= MATCH_IRE_ILL; 13604 13605 if (isv6) { 13606 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group)) 13607 continue; 13608 13609 ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 13610 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL, 13611 match_flags, 0, ipst, NULL); 13612 } else { 13613 if (ire->ire_addr != group) 13614 continue; 13615 13616 ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr, 13617 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL, 13618 match_flags, 0, ipst, NULL); 13619 } 13620 /* No interface route exists for the gateway; skip this ire. */ 13621 if (ire_gw == NULL) 13622 continue; 13623 if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 13624 ire_refrele(ire_gw); 13625 continue; 13626 } 13627 ASSERT(ire_gw->ire_ill != NULL); /* IRE_INTERFACE */ 13628 ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex; 13629 13630 /* 13631 * The operation is considered a success if 13632 * it succeeds at least once on any one interface. 13633 */ 13634 error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex, 13635 fmode, v6src); 13636 if (error == 0) 13637 result = CGTP_MCAST_SUCCESS; 13638 13639 ire_refrele(ire_gw); 13640 } 13641 irb_refrele(irb); 13642 /* 13643 * Consider the call as successful if we succeeded on at least 13644 * one interface. Otherwise, return the last encountered error. 13645 */ 13646 return (result == CGTP_MCAST_SUCCESS ? 0 : error); 13647 } 13648 13649 /* 13650 * Return the expected CGTP hooks version number. 13651 */ 13652 int 13653 ip_cgtp_filter_supported(void) 13654 { 13655 return (ip_cgtp_filter_rev); 13656 } 13657 13658 /* 13659 * CGTP hooks can be registered by invoking this function. 13660 * Checks that the version number matches. 13661 */ 13662 int 13663 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops) 13664 { 13665 netstack_t *ns; 13666 ip_stack_t *ipst; 13667 13668 if (ops->cfo_filter_rev != CGTP_FILTER_REV) 13669 return (ENOTSUP); 13670 13671 ns = netstack_find_by_stackid(stackid); 13672 if (ns == NULL) 13673 return (EINVAL); 13674 ipst = ns->netstack_ip; 13675 ASSERT(ipst != NULL); 13676 13677 if (ipst->ips_ip_cgtp_filter_ops != NULL) { 13678 netstack_rele(ns); 13679 return (EALREADY); 13680 } 13681 13682 ipst->ips_ip_cgtp_filter_ops = ops; 13683 13684 ill_set_inputfn_all(ipst); 13685 13686 netstack_rele(ns); 13687 return (0); 13688 } 13689 13690 /* 13691 * CGTP hooks can be unregistered by invoking this function. 13692 * Returns ENXIO if there was no registration. 13693 * Returns EBUSY if the ndd variable has not been turned off. 13694 */ 13695 int 13696 ip_cgtp_filter_unregister(netstackid_t stackid) 13697 { 13698 netstack_t *ns; 13699 ip_stack_t *ipst; 13700 13701 ns = netstack_find_by_stackid(stackid); 13702 if (ns == NULL) 13703 return (EINVAL); 13704 ipst = ns->netstack_ip; 13705 ASSERT(ipst != NULL); 13706 13707 if (ipst->ips_ip_cgtp_filter) { 13708 netstack_rele(ns); 13709 return (EBUSY); 13710 } 13711 13712 if (ipst->ips_ip_cgtp_filter_ops == NULL) { 13713 netstack_rele(ns); 13714 return (ENXIO); 13715 } 13716 ipst->ips_ip_cgtp_filter_ops = NULL; 13717 13718 ill_set_inputfn_all(ipst); 13719 13720 netstack_rele(ns); 13721 return (0); 13722 } 13723 13724 /* 13725 * Check whether there is a CGTP filter registration. 13726 * Returns non-zero if there is a registration, otherwise returns zero. 13727 * Note: returns zero if bad stackid. 13728 */ 13729 int 13730 ip_cgtp_filter_is_registered(netstackid_t stackid) 13731 { 13732 netstack_t *ns; 13733 ip_stack_t *ipst; 13734 int ret; 13735 13736 ns = netstack_find_by_stackid(stackid); 13737 if (ns == NULL) 13738 return (0); 13739 ipst = ns->netstack_ip; 13740 ASSERT(ipst != NULL); 13741 13742 if (ipst->ips_ip_cgtp_filter_ops != NULL) 13743 ret = 1; 13744 else 13745 ret = 0; 13746 13747 netstack_rele(ns); 13748 return (ret); 13749 } 13750 13751 static int 13752 ip_squeue_switch(int val) 13753 { 13754 int rval; 13755 13756 switch (val) { 13757 case IP_SQUEUE_ENTER_NODRAIN: 13758 rval = SQ_NODRAIN; 13759 break; 13760 case IP_SQUEUE_ENTER: 13761 rval = SQ_PROCESS; 13762 break; 13763 case IP_SQUEUE_FILL: 13764 default: 13765 rval = SQ_FILL; 13766 break; 13767 } 13768 return (rval); 13769 } 13770 13771 static void * 13772 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp) 13773 { 13774 kstat_t *ksp; 13775 13776 ip_stat_t template = { 13777 { "ip_udp_fannorm", KSTAT_DATA_UINT64 }, 13778 { "ip_udp_fanmb", KSTAT_DATA_UINT64 }, 13779 { "ip_recv_pullup", KSTAT_DATA_UINT64 }, 13780 { "ip_db_ref", KSTAT_DATA_UINT64 }, 13781 { "ip_notaligned", KSTAT_DATA_UINT64 }, 13782 { "ip_multimblk", KSTAT_DATA_UINT64 }, 13783 { "ip_opt", KSTAT_DATA_UINT64 }, 13784 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 }, 13785 { "ip_conn_flputbq", KSTAT_DATA_UINT64 }, 13786 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 }, 13787 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 }, 13788 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 13789 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 }, 13790 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 }, 13791 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 }, 13792 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 }, 13793 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 }, 13794 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 }, 13795 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 }, 13796 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 13797 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 13798 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 13799 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 13800 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 13801 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 13802 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 }, 13803 { "conn_in_recvopts", KSTAT_DATA_UINT64 }, 13804 { "conn_in_recvif", KSTAT_DATA_UINT64 }, 13805 { "conn_in_recvslla", KSTAT_DATA_UINT64 }, 13806 { "conn_in_recvucred", KSTAT_DATA_UINT64 }, 13807 { "conn_in_recvttl", KSTAT_DATA_UINT64 }, 13808 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 }, 13809 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 }, 13810 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 }, 13811 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 }, 13812 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 }, 13813 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 }, 13814 { "conn_in_recvtclass", KSTAT_DATA_UINT64 }, 13815 { "conn_in_timestamp", KSTAT_DATA_UINT64 }, 13816 }; 13817 13818 ksp = kstat_create_netstack("ip", 0, "ipstat", "net", 13819 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 13820 KSTAT_FLAG_VIRTUAL, stackid); 13821 13822 if (ksp == NULL) 13823 return (NULL); 13824 13825 bcopy(&template, ip_statisticsp, sizeof (template)); 13826 ksp->ks_data = (void *)ip_statisticsp; 13827 ksp->ks_private = (void *)(uintptr_t)stackid; 13828 13829 kstat_install(ksp); 13830 return (ksp); 13831 } 13832 13833 static void 13834 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 13835 { 13836 if (ksp != NULL) { 13837 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 13838 kstat_delete_netstack(ksp, stackid); 13839 } 13840 } 13841 13842 static void * 13843 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst) 13844 { 13845 kstat_t *ksp; 13846 13847 ip_named_kstat_t template = { 13848 { "forwarding", KSTAT_DATA_UINT32, 0 }, 13849 { "defaultTTL", KSTAT_DATA_UINT32, 0 }, 13850 { "inReceives", KSTAT_DATA_UINT64, 0 }, 13851 { "inHdrErrors", KSTAT_DATA_UINT32, 0 }, 13852 { "inAddrErrors", KSTAT_DATA_UINT32, 0 }, 13853 { "forwDatagrams", KSTAT_DATA_UINT64, 0 }, 13854 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 }, 13855 { "inDiscards", KSTAT_DATA_UINT32, 0 }, 13856 { "inDelivers", KSTAT_DATA_UINT64, 0 }, 13857 { "outRequests", KSTAT_DATA_UINT64, 0 }, 13858 { "outDiscards", KSTAT_DATA_UINT32, 0 }, 13859 { "outNoRoutes", KSTAT_DATA_UINT32, 0 }, 13860 { "reasmTimeout", KSTAT_DATA_UINT32, 0 }, 13861 { "reasmReqds", KSTAT_DATA_UINT32, 0 }, 13862 { "reasmOKs", KSTAT_DATA_UINT32, 0 }, 13863 { "reasmFails", KSTAT_DATA_UINT32, 0 }, 13864 { "fragOKs", KSTAT_DATA_UINT32, 0 }, 13865 { "fragFails", KSTAT_DATA_UINT32, 0 }, 13866 { "fragCreates", KSTAT_DATA_UINT32, 0 }, 13867 { "addrEntrySize", KSTAT_DATA_INT32, 0 }, 13868 { "routeEntrySize", KSTAT_DATA_INT32, 0 }, 13869 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 }, 13870 { "routingDiscards", KSTAT_DATA_UINT32, 0 }, 13871 { "inErrs", KSTAT_DATA_UINT32, 0 }, 13872 { "noPorts", KSTAT_DATA_UINT32, 0 }, 13873 { "inCksumErrs", KSTAT_DATA_UINT32, 0 }, 13874 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 }, 13875 { "reasmPartDups", KSTAT_DATA_UINT32, 0 }, 13876 { "forwProhibits", KSTAT_DATA_UINT32, 0 }, 13877 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 }, 13878 { "udpInOverflows", KSTAT_DATA_UINT32, 0 }, 13879 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 }, 13880 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 }, 13881 { "ipsecInFailed", KSTAT_DATA_INT32, 0 }, 13882 { "memberEntrySize", KSTAT_DATA_INT32, 0 }, 13883 { "inIPv6", KSTAT_DATA_UINT32, 0 }, 13884 { "outIPv6", KSTAT_DATA_UINT32, 0 }, 13885 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 }, 13886 }; 13887 13888 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED, 13889 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid); 13890 if (ksp == NULL || ksp->ks_data == NULL) 13891 return (NULL); 13892 13893 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2; 13894 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl; 13895 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout; 13896 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t); 13897 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t); 13898 13899 template.netToMediaEntrySize.value.i32 = 13900 sizeof (mib2_ipNetToMediaEntry_t); 13901 13902 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t); 13903 13904 bcopy(&template, ksp->ks_data, sizeof (template)); 13905 ksp->ks_update = ip_kstat_update; 13906 ksp->ks_private = (void *)(uintptr_t)stackid; 13907 13908 kstat_install(ksp); 13909 return (ksp); 13910 } 13911 13912 static void 13913 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp) 13914 { 13915 if (ksp != NULL) { 13916 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 13917 kstat_delete_netstack(ksp, stackid); 13918 } 13919 } 13920 13921 static int 13922 ip_kstat_update(kstat_t *kp, int rw) 13923 { 13924 ip_named_kstat_t *ipkp; 13925 mib2_ipIfStatsEntry_t ipmib; 13926 ill_walk_context_t ctx; 13927 ill_t *ill; 13928 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 13929 netstack_t *ns; 13930 ip_stack_t *ipst; 13931 13932 if (kp == NULL || kp->ks_data == NULL) 13933 return (EIO); 13934 13935 if (rw == KSTAT_WRITE) 13936 return (EACCES); 13937 13938 ns = netstack_find_by_stackid(stackid); 13939 if (ns == NULL) 13940 return (-1); 13941 ipst = ns->netstack_ip; 13942 if (ipst == NULL) { 13943 netstack_rele(ns); 13944 return (-1); 13945 } 13946 ipkp = (ip_named_kstat_t *)kp->ks_data; 13947 13948 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib)); 13949 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 13950 ill = ILL_START_WALK_V4(&ctx, ipst); 13951 for (; ill != NULL; ill = ill_next(&ctx, ill)) 13952 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib); 13953 rw_exit(&ipst->ips_ill_g_lock); 13954 13955 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding; 13956 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL; 13957 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives; 13958 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors; 13959 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors; 13960 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams; 13961 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos; 13962 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards; 13963 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers; 13964 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests; 13965 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards; 13966 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes; 13967 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout; 13968 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds; 13969 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs; 13970 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails; 13971 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs; 13972 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails; 13973 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates; 13974 13975 ipkp->routingDiscards.value.ui32 = 0; 13976 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs; 13977 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts; 13978 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs; 13979 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates; 13980 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups; 13981 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits; 13982 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs; 13983 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows; 13984 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows; 13985 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded; 13986 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed; 13987 13988 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion; 13989 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion; 13990 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion; 13991 13992 netstack_rele(ns); 13993 13994 return (0); 13995 } 13996 13997 static void * 13998 icmp_kstat_init(netstackid_t stackid) 13999 { 14000 kstat_t *ksp; 14001 14002 icmp_named_kstat_t template = { 14003 { "inMsgs", KSTAT_DATA_UINT32 }, 14004 { "inErrors", KSTAT_DATA_UINT32 }, 14005 { "inDestUnreachs", KSTAT_DATA_UINT32 }, 14006 { "inTimeExcds", KSTAT_DATA_UINT32 }, 14007 { "inParmProbs", KSTAT_DATA_UINT32 }, 14008 { "inSrcQuenchs", KSTAT_DATA_UINT32 }, 14009 { "inRedirects", KSTAT_DATA_UINT32 }, 14010 { "inEchos", KSTAT_DATA_UINT32 }, 14011 { "inEchoReps", KSTAT_DATA_UINT32 }, 14012 { "inTimestamps", KSTAT_DATA_UINT32 }, 14013 { "inTimestampReps", KSTAT_DATA_UINT32 }, 14014 { "inAddrMasks", KSTAT_DATA_UINT32 }, 14015 { "inAddrMaskReps", KSTAT_DATA_UINT32 }, 14016 { "outMsgs", KSTAT_DATA_UINT32 }, 14017 { "outErrors", KSTAT_DATA_UINT32 }, 14018 { "outDestUnreachs", KSTAT_DATA_UINT32 }, 14019 { "outTimeExcds", KSTAT_DATA_UINT32 }, 14020 { "outParmProbs", KSTAT_DATA_UINT32 }, 14021 { "outSrcQuenchs", KSTAT_DATA_UINT32 }, 14022 { "outRedirects", KSTAT_DATA_UINT32 }, 14023 { "outEchos", KSTAT_DATA_UINT32 }, 14024 { "outEchoReps", KSTAT_DATA_UINT32 }, 14025 { "outTimestamps", KSTAT_DATA_UINT32 }, 14026 { "outTimestampReps", KSTAT_DATA_UINT32 }, 14027 { "outAddrMasks", KSTAT_DATA_UINT32 }, 14028 { "outAddrMaskReps", KSTAT_DATA_UINT32 }, 14029 { "inChksumErrs", KSTAT_DATA_UINT32 }, 14030 { "inUnknowns", KSTAT_DATA_UINT32 }, 14031 { "inFragNeeded", KSTAT_DATA_UINT32 }, 14032 { "outFragNeeded", KSTAT_DATA_UINT32 }, 14033 { "outDrops", KSTAT_DATA_UINT32 }, 14034 { "inOverFlows", KSTAT_DATA_UINT32 }, 14035 { "inBadRedirects", KSTAT_DATA_UINT32 }, 14036 }; 14037 14038 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED, 14039 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid); 14040 if (ksp == NULL || ksp->ks_data == NULL) 14041 return (NULL); 14042 14043 bcopy(&template, ksp->ks_data, sizeof (template)); 14044 14045 ksp->ks_update = icmp_kstat_update; 14046 ksp->ks_private = (void *)(uintptr_t)stackid; 14047 14048 kstat_install(ksp); 14049 return (ksp); 14050 } 14051 14052 static void 14053 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 14054 { 14055 if (ksp != NULL) { 14056 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 14057 kstat_delete_netstack(ksp, stackid); 14058 } 14059 } 14060 14061 static int 14062 icmp_kstat_update(kstat_t *kp, int rw) 14063 { 14064 icmp_named_kstat_t *icmpkp; 14065 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private; 14066 netstack_t *ns; 14067 ip_stack_t *ipst; 14068 14069 if ((kp == NULL) || (kp->ks_data == NULL)) 14070 return (EIO); 14071 14072 if (rw == KSTAT_WRITE) 14073 return (EACCES); 14074 14075 ns = netstack_find_by_stackid(stackid); 14076 if (ns == NULL) 14077 return (-1); 14078 ipst = ns->netstack_ip; 14079 if (ipst == NULL) { 14080 netstack_rele(ns); 14081 return (-1); 14082 } 14083 icmpkp = (icmp_named_kstat_t *)kp->ks_data; 14084 14085 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs; 14086 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors; 14087 icmpkp->inDestUnreachs.value.ui32 = 14088 ipst->ips_icmp_mib.icmpInDestUnreachs; 14089 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds; 14090 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs; 14091 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs; 14092 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects; 14093 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos; 14094 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps; 14095 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps; 14096 icmpkp->inTimestampReps.value.ui32 = 14097 ipst->ips_icmp_mib.icmpInTimestampReps; 14098 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks; 14099 icmpkp->inAddrMaskReps.value.ui32 = 14100 ipst->ips_icmp_mib.icmpInAddrMaskReps; 14101 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs; 14102 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors; 14103 icmpkp->outDestUnreachs.value.ui32 = 14104 ipst->ips_icmp_mib.icmpOutDestUnreachs; 14105 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds; 14106 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs; 14107 icmpkp->outSrcQuenchs.value.ui32 = 14108 ipst->ips_icmp_mib.icmpOutSrcQuenchs; 14109 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects; 14110 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos; 14111 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps; 14112 icmpkp->outTimestamps.value.ui32 = 14113 ipst->ips_icmp_mib.icmpOutTimestamps; 14114 icmpkp->outTimestampReps.value.ui32 = 14115 ipst->ips_icmp_mib.icmpOutTimestampReps; 14116 icmpkp->outAddrMasks.value.ui32 = 14117 ipst->ips_icmp_mib.icmpOutAddrMasks; 14118 icmpkp->outAddrMaskReps.value.ui32 = 14119 ipst->ips_icmp_mib.icmpOutAddrMaskReps; 14120 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs; 14121 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns; 14122 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded; 14123 icmpkp->outFragNeeded.value.ui32 = 14124 ipst->ips_icmp_mib.icmpOutFragNeeded; 14125 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops; 14126 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows; 14127 icmpkp->inBadRedirects.value.ui32 = 14128 ipst->ips_icmp_mib.icmpInBadRedirects; 14129 14130 netstack_rele(ns); 14131 return (0); 14132 } 14133 14134 /* 14135 * This is the fanout function for raw socket opened for SCTP. Note 14136 * that it is called after SCTP checks that there is no socket which 14137 * wants a packet. Then before SCTP handles this out of the blue packet, 14138 * this function is called to see if there is any raw socket for SCTP. 14139 * If there is and it is bound to the correct address, the packet will 14140 * be sent to that socket. Note that only one raw socket can be bound to 14141 * a port. This is assured in ipcl_sctp_hash_insert(); 14142 */ 14143 void 14144 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports, 14145 ip_recv_attr_t *ira) 14146 { 14147 conn_t *connp; 14148 queue_t *rq; 14149 boolean_t secure; 14150 ill_t *ill = ira->ira_ill; 14151 ip_stack_t *ipst = ill->ill_ipst; 14152 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; 14153 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp; 14154 iaflags_t iraflags = ira->ira_flags; 14155 ill_t *rill = ira->ira_rill; 14156 14157 secure = iraflags & IRAF_IPSEC_SECURE; 14158 14159 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h, 14160 ira, ipst); 14161 if (connp == NULL) { 14162 /* 14163 * Although raw sctp is not summed, OOB chunks must be. 14164 * Drop the packet here if the sctp checksum failed. 14165 */ 14166 if (iraflags & IRAF_SCTP_CSUM_ERR) { 14167 BUMP_MIB(&sctps->sctps_mib, sctpChecksumError); 14168 freemsg(mp); 14169 return; 14170 } 14171 ira->ira_ill = ira->ira_rill = NULL; 14172 sctp_ootb_input(mp, ira, ipst); 14173 ira->ira_ill = ill; 14174 ira->ira_rill = rill; 14175 return; 14176 } 14177 rq = connp->conn_rq; 14178 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) { 14179 CONN_DEC_REF(connp); 14180 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows); 14181 freemsg(mp); 14182 return; 14183 } 14184 if (((iraflags & IRAF_IS_IPV4) ? 14185 CONN_INBOUND_POLICY_PRESENT(connp, ipss) : 14186 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) || 14187 secure) { 14188 mp = ipsec_check_inbound_policy(mp, connp, ipha, 14189 ip6h, ira); 14190 if (mp == NULL) { 14191 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); 14192 /* Note that mp is NULL */ 14193 ip_drop_input("ipIfStatsInDiscards", mp, ill); 14194 CONN_DEC_REF(connp); 14195 return; 14196 } 14197 } 14198 14199 if (iraflags & IRAF_ICMP_ERROR) { 14200 (connp->conn_recvicmp)(connp, mp, NULL, ira); 14201 } else { 14202 ill_t *rill = ira->ira_rill; 14203 14204 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); 14205 /* This is the SOCK_RAW, IPPROTO_SCTP case. */ 14206 ira->ira_ill = ira->ira_rill = NULL; 14207 (connp->conn_recv)(connp, mp, NULL, ira); 14208 ira->ira_ill = ill; 14209 ira->ira_rill = rill; 14210 } 14211 CONN_DEC_REF(connp); 14212 } 14213 14214 /* 14215 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path 14216 * header before the ip payload. 14217 */ 14218 static void 14219 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len) 14220 { 14221 int len = (mp->b_wptr - mp->b_rptr); 14222 mblk_t *ip_mp; 14223 14224 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 14225 if (is_fp_mp || len != fp_mp_len) { 14226 if (len > fp_mp_len) { 14227 /* 14228 * fastpath header and ip header in the first mblk 14229 */ 14230 mp->b_rptr += fp_mp_len; 14231 } else { 14232 /* 14233 * ip_xmit_attach_llhdr had to prepend an mblk to 14234 * attach the fastpath header before ip header. 14235 */ 14236 ip_mp = mp->b_cont; 14237 freeb(mp); 14238 mp = ip_mp; 14239 mp->b_rptr += (fp_mp_len - len); 14240 } 14241 } else { 14242 ip_mp = mp->b_cont; 14243 freeb(mp); 14244 mp = ip_mp; 14245 } 14246 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill); 14247 freemsg(mp); 14248 } 14249 14250 /* 14251 * Normal post fragmentation function. 14252 * 14253 * Send a packet using the passed in nce. This handles both IPv4 and IPv6 14254 * using the same state machine. 14255 * 14256 * We return an error on failure. In particular we return EWOULDBLOCK 14257 * when the driver flow controls. In that case this ensures that ip_wsrv runs 14258 * (currently by canputnext failure resulting in backenabling from GLD.) 14259 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an 14260 * indication that they can flow control until ip_wsrv() tells then to restart. 14261 * 14262 * If the nce passed by caller is incomplete, this function 14263 * queues the packet and if necessary, sends ARP request and bails. 14264 * If the Neighbor Cache passed is fully resolved, we simply prepend 14265 * the link-layer header to the packet, do ipsec hw acceleration 14266 * work if necessary, and send the packet out on the wire. 14267 */ 14268 /* ARGSUSED6 */ 14269 int 14270 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len, 14271 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie) 14272 { 14273 queue_t *wq; 14274 ill_t *ill = nce->nce_ill; 14275 ip_stack_t *ipst = ill->ill_ipst; 14276 uint64_t delta; 14277 boolean_t isv6 = ill->ill_isv6; 14278 boolean_t fp_mp; 14279 ncec_t *ncec = nce->nce_common; 14280 int64_t now = LBOLT_FASTPATH64; 14281 boolean_t is_probe; 14282 14283 DTRACE_PROBE1(ip__xmit, nce_t *, nce); 14284 14285 ASSERT(mp != NULL); 14286 ASSERT(mp->b_datap->db_type == M_DATA); 14287 ASSERT(pkt_len == msgdsize(mp)); 14288 14289 /* 14290 * If we have already been here and are coming back after ARP/ND. 14291 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs 14292 * in that case since they have seen the packet when it came here 14293 * the first time. 14294 */ 14295 if (ixaflags & IXAF_NO_TRACE) 14296 goto sendit; 14297 14298 if (ixaflags & IXAF_IS_IPV4) { 14299 ipha_t *ipha = (ipha_t *)mp->b_rptr; 14300 14301 ASSERT(!isv6); 14302 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length)); 14303 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) && 14304 !(ixaflags & IXAF_NO_PFHOOK)) { 14305 int error; 14306 14307 FW_HOOKS(ipst->ips_ip4_physical_out_event, 14308 ipst->ips_ipv4firewall_physical_out, 14309 NULL, ill, ipha, mp, mp, 0, ipst, error); 14310 DTRACE_PROBE1(ip4__physical__out__end, 14311 mblk_t *, mp); 14312 if (mp == NULL) 14313 return (error); 14314 14315 /* The length could have changed */ 14316 pkt_len = msgdsize(mp); 14317 } 14318 if (ipst->ips_ip4_observe.he_interested) { 14319 /* 14320 * Note that for TX the zoneid is the sending 14321 * zone, whether or not MLP is in play. 14322 * Since the szone argument is the IP zoneid (i.e., 14323 * zero for exclusive-IP zones) and ipobs wants 14324 * the system zoneid, we map it here. 14325 */ 14326 szone = IP_REAL_ZONEID(szone, ipst); 14327 14328 /* 14329 * On the outbound path the destination zone will be 14330 * unknown as we're sending this packet out on the 14331 * wire. 14332 */ 14333 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES, 14334 ill, ipst); 14335 } 14336 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, 14337 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill, 14338 ipha_t *, ipha, ip6_t *, NULL, int, 0); 14339 } else { 14340 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 14341 14342 ASSERT(isv6); 14343 ASSERT(pkt_len == 14344 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN); 14345 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) && 14346 !(ixaflags & IXAF_NO_PFHOOK)) { 14347 int error; 14348 14349 FW_HOOKS6(ipst->ips_ip6_physical_out_event, 14350 ipst->ips_ipv6firewall_physical_out, 14351 NULL, ill, ip6h, mp, mp, 0, ipst, error); 14352 DTRACE_PROBE1(ip6__physical__out__end, 14353 mblk_t *, mp); 14354 if (mp == NULL) 14355 return (error); 14356 14357 /* The length could have changed */ 14358 pkt_len = msgdsize(mp); 14359 } 14360 if (ipst->ips_ip6_observe.he_interested) { 14361 /* See above */ 14362 szone = IP_REAL_ZONEID(szone, ipst); 14363 14364 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES, 14365 ill, ipst); 14366 } 14367 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, 14368 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill, 14369 ipha_t *, NULL, ip6_t *, ip6h, int, 0); 14370 } 14371 14372 sendit: 14373 /* 14374 * We check the state without a lock because the state can never 14375 * move "backwards" to initial or incomplete. 14376 */ 14377 switch (ncec->ncec_state) { 14378 case ND_REACHABLE: 14379 case ND_STALE: 14380 case ND_DELAY: 14381 case ND_PROBE: 14382 mp = ip_xmit_attach_llhdr(mp, nce); 14383 if (mp == NULL) { 14384 /* 14385 * ip_xmit_attach_llhdr has increased 14386 * ipIfStatsOutDiscards and called ip_drop_output() 14387 */ 14388 return (ENOBUFS); 14389 } 14390 /* 14391 * check if nce_fastpath completed and we tagged on a 14392 * copy of nce_fp_mp in ip_xmit_attach_llhdr(). 14393 */ 14394 fp_mp = (mp->b_datap->db_type == M_DATA); 14395 14396 if (fp_mp && 14397 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) { 14398 ill_dld_direct_t *idd; 14399 14400 idd = &ill->ill_dld_capab->idc_direct; 14401 /* 14402 * Send the packet directly to DLD, where it 14403 * may be queued depending on the availability 14404 * of transmit resources at the media layer. 14405 * Return value should be taken into 14406 * account and flow control the TCP. 14407 */ 14408 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 14409 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 14410 pkt_len); 14411 14412 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) { 14413 (void) idd->idd_tx_df(idd->idd_tx_dh, mp, 14414 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC); 14415 } else { 14416 uintptr_t cookie; 14417 14418 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh, 14419 mp, (uintptr_t)xmit_hint, 0)) != 0) { 14420 if (ixacookie != NULL) 14421 *ixacookie = cookie; 14422 return (EWOULDBLOCK); 14423 } 14424 } 14425 } else { 14426 wq = ill->ill_wq; 14427 14428 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) && 14429 !canputnext(wq)) { 14430 if (ixacookie != NULL) 14431 *ixacookie = 0; 14432 ip_xmit_flowctl_drop(ill, mp, fp_mp, 14433 nce->nce_fp_mp != NULL ? 14434 MBLKL(nce->nce_fp_mp) : 0); 14435 return (EWOULDBLOCK); 14436 } 14437 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 14438 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 14439 pkt_len); 14440 putnext(wq, mp); 14441 } 14442 14443 /* 14444 * The rest of this function implements Neighbor Unreachability 14445 * detection. Determine if the ncec is eligible for NUD. 14446 */ 14447 if (ncec->ncec_flags & NCE_F_NONUD) 14448 return (0); 14449 14450 ASSERT(ncec->ncec_state != ND_INCOMPLETE); 14451 14452 /* 14453 * Check for upper layer advice 14454 */ 14455 if (ixaflags & IXAF_REACH_CONF) { 14456 timeout_id_t tid; 14457 14458 /* 14459 * It should be o.k. to check the state without 14460 * a lock here, at most we lose an advice. 14461 */ 14462 ncec->ncec_last = TICK_TO_MSEC(now); 14463 if (ncec->ncec_state != ND_REACHABLE) { 14464 mutex_enter(&ncec->ncec_lock); 14465 ncec->ncec_state = ND_REACHABLE; 14466 tid = ncec->ncec_timeout_id; 14467 ncec->ncec_timeout_id = 0; 14468 mutex_exit(&ncec->ncec_lock); 14469 (void) untimeout(tid); 14470 if (ip_debug > 2) { 14471 /* ip1dbg */ 14472 pr_addr_dbg("ip_xmit: state" 14473 " for %s changed to" 14474 " REACHABLE\n", AF_INET6, 14475 &ncec->ncec_addr); 14476 } 14477 } 14478 return (0); 14479 } 14480 14481 delta = TICK_TO_MSEC(now) - ncec->ncec_last; 14482 ip1dbg(("ip_xmit: delta = %" PRId64 14483 " ill_reachable_time = %d \n", delta, 14484 ill->ill_reachable_time)); 14485 if (delta > (uint64_t)ill->ill_reachable_time) { 14486 mutex_enter(&ncec->ncec_lock); 14487 switch (ncec->ncec_state) { 14488 case ND_REACHABLE: 14489 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0); 14490 /* FALLTHROUGH */ 14491 case ND_STALE: 14492 /* 14493 * ND_REACHABLE is identical to 14494 * ND_STALE in this specific case. If 14495 * reachable time has expired for this 14496 * neighbor (delta is greater than 14497 * reachable time), conceptually, the 14498 * neighbor cache is no longer in 14499 * REACHABLE state, but already in 14500 * STALE state. So the correct 14501 * transition here is to ND_DELAY. 14502 */ 14503 ncec->ncec_state = ND_DELAY; 14504 mutex_exit(&ncec->ncec_lock); 14505 nce_restart_timer(ncec, 14506 ipst->ips_delay_first_probe_time); 14507 if (ip_debug > 3) { 14508 /* ip2dbg */ 14509 pr_addr_dbg("ip_xmit: state" 14510 " for %s changed to" 14511 " DELAY\n", AF_INET6, 14512 &ncec->ncec_addr); 14513 } 14514 break; 14515 case ND_DELAY: 14516 case ND_PROBE: 14517 mutex_exit(&ncec->ncec_lock); 14518 /* Timers have already started */ 14519 break; 14520 case ND_UNREACHABLE: 14521 /* 14522 * nce_timer has detected that this ncec 14523 * is unreachable and initiated deleting 14524 * this ncec. 14525 * This is a harmless race where we found the 14526 * ncec before it was deleted and have 14527 * just sent out a packet using this 14528 * unreachable ncec. 14529 */ 14530 mutex_exit(&ncec->ncec_lock); 14531 break; 14532 default: 14533 ASSERT(0); 14534 mutex_exit(&ncec->ncec_lock); 14535 } 14536 } 14537 return (0); 14538 14539 case ND_INCOMPLETE: 14540 /* 14541 * the state could have changed since we didn't hold the lock. 14542 * Re-verify state under lock. 14543 */ 14544 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill); 14545 mutex_enter(&ncec->ncec_lock); 14546 if (NCE_ISREACHABLE(ncec)) { 14547 mutex_exit(&ncec->ncec_lock); 14548 goto sendit; 14549 } 14550 /* queue the packet */ 14551 nce_queue_mp(ncec, mp, is_probe); 14552 mutex_exit(&ncec->ncec_lock); 14553 DTRACE_PROBE2(ip__xmit__incomplete, 14554 (ncec_t *), ncec, (mblk_t *), mp); 14555 return (0); 14556 14557 case ND_INITIAL: 14558 /* 14559 * State could have changed since we didn't hold the lock, so 14560 * re-verify state. 14561 */ 14562 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill); 14563 mutex_enter(&ncec->ncec_lock); 14564 if (NCE_ISREACHABLE(ncec)) { 14565 mutex_exit(&ncec->ncec_lock); 14566 goto sendit; 14567 } 14568 nce_queue_mp(ncec, mp, is_probe); 14569 if (ncec->ncec_state == ND_INITIAL) { 14570 ncec->ncec_state = ND_INCOMPLETE; 14571 mutex_exit(&ncec->ncec_lock); 14572 /* 14573 * figure out the source we want to use 14574 * and resolve it. 14575 */ 14576 ip_ndp_resolve(ncec); 14577 } else { 14578 mutex_exit(&ncec->ncec_lock); 14579 } 14580 return (0); 14581 14582 case ND_UNREACHABLE: 14583 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 14584 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE", 14585 mp, ill); 14586 freemsg(mp); 14587 return (0); 14588 14589 default: 14590 ASSERT(0); 14591 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); 14592 ip_drop_output("ipIfStatsOutDiscards - ND_other", 14593 mp, ill); 14594 freemsg(mp); 14595 return (ENETUNREACH); 14596 } 14597 } 14598 14599 /* 14600 * Return B_TRUE if the buffers differ in length or content. 14601 * This is used for comparing extension header buffers. 14602 * Note that an extension header would be declared different 14603 * even if all that changed was the next header value in that header i.e. 14604 * what really changed is the next extension header. 14605 */ 14606 boolean_t 14607 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf, 14608 uint_t blen) 14609 { 14610 if (!b_valid) 14611 blen = 0; 14612 14613 if (alen != blen) 14614 return (B_TRUE); 14615 if (alen == 0) 14616 return (B_FALSE); /* Both zero length */ 14617 return (bcmp(abuf, bbuf, alen)); 14618 } 14619 14620 /* 14621 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok. 14622 * Return B_FALSE if memory allocation fails - don't change any state! 14623 */ 14624 boolean_t 14625 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 14626 const void *src, uint_t srclen) 14627 { 14628 void *dst; 14629 14630 if (!src_valid) 14631 srclen = 0; 14632 14633 ASSERT(*dstlenp == 0); 14634 if (src != NULL && srclen != 0) { 14635 dst = mi_alloc(srclen, BPRI_MED); 14636 if (dst == NULL) 14637 return (B_FALSE); 14638 } else { 14639 dst = NULL; 14640 } 14641 if (*dstp != NULL) 14642 mi_free(*dstp); 14643 *dstp = dst; 14644 *dstlenp = dst == NULL ? 0 : srclen; 14645 return (B_TRUE); 14646 } 14647 14648 /* 14649 * Replace what is in *dst, *dstlen with the source. 14650 * Assumes ip_allocbuf has already been called. 14651 */ 14652 void 14653 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 14654 const void *src, uint_t srclen) 14655 { 14656 if (!src_valid) 14657 srclen = 0; 14658 14659 ASSERT(*dstlenp == srclen); 14660 if (src != NULL && srclen != 0) 14661 bcopy(src, *dstp, srclen); 14662 } 14663 14664 /* 14665 * Free the storage pointed to by the members of an ip_pkt_t. 14666 */ 14667 void 14668 ip_pkt_free(ip_pkt_t *ipp) 14669 { 14670 uint_t fields = ipp->ipp_fields; 14671 14672 if (fields & IPPF_HOPOPTS) { 14673 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 14674 ipp->ipp_hopopts = NULL; 14675 ipp->ipp_hopoptslen = 0; 14676 } 14677 if (fields & IPPF_RTHDRDSTOPTS) { 14678 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen); 14679 ipp->ipp_rthdrdstopts = NULL; 14680 ipp->ipp_rthdrdstoptslen = 0; 14681 } 14682 if (fields & IPPF_DSTOPTS) { 14683 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 14684 ipp->ipp_dstopts = NULL; 14685 ipp->ipp_dstoptslen = 0; 14686 } 14687 if (fields & IPPF_RTHDR) { 14688 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 14689 ipp->ipp_rthdr = NULL; 14690 ipp->ipp_rthdrlen = 0; 14691 } 14692 if (fields & IPPF_IPV4_OPTIONS) { 14693 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len); 14694 ipp->ipp_ipv4_options = NULL; 14695 ipp->ipp_ipv4_options_len = 0; 14696 } 14697 if (fields & IPPF_LABEL_V4) { 14698 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4); 14699 ipp->ipp_label_v4 = NULL; 14700 ipp->ipp_label_len_v4 = 0; 14701 } 14702 if (fields & IPPF_LABEL_V6) { 14703 kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6); 14704 ipp->ipp_label_v6 = NULL; 14705 ipp->ipp_label_len_v6 = 0; 14706 } 14707 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS | 14708 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6); 14709 } 14710 14711 /* 14712 * Copy from src to dst and allocate as needed. 14713 * Returns zero or ENOMEM. 14714 * 14715 * The caller must initialize dst to zero. 14716 */ 14717 int 14718 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag) 14719 { 14720 uint_t fields = src->ipp_fields; 14721 14722 /* Start with fields that don't require memory allocation */ 14723 dst->ipp_fields = fields & 14724 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS | 14725 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6); 14726 14727 dst->ipp_addr = src->ipp_addr; 14728 dst->ipp_unicast_hops = src->ipp_unicast_hops; 14729 dst->ipp_hoplimit = src->ipp_hoplimit; 14730 dst->ipp_tclass = src->ipp_tclass; 14731 dst->ipp_type_of_service = src->ipp_type_of_service; 14732 14733 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS | 14734 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6))) 14735 return (0); 14736 14737 if (fields & IPPF_HOPOPTS) { 14738 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag); 14739 if (dst->ipp_hopopts == NULL) { 14740 ip_pkt_free(dst); 14741 return (ENOMEM); 14742 } 14743 dst->ipp_fields |= IPPF_HOPOPTS; 14744 bcopy(src->ipp_hopopts, dst->ipp_hopopts, 14745 src->ipp_hopoptslen); 14746 dst->ipp_hopoptslen = src->ipp_hopoptslen; 14747 } 14748 if (fields & IPPF_RTHDRDSTOPTS) { 14749 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen, 14750 kmflag); 14751 if (dst->ipp_rthdrdstopts == NULL) { 14752 ip_pkt_free(dst); 14753 return (ENOMEM); 14754 } 14755 dst->ipp_fields |= IPPF_RTHDRDSTOPTS; 14756 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts, 14757 src->ipp_rthdrdstoptslen); 14758 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen; 14759 } 14760 if (fields & IPPF_DSTOPTS) { 14761 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag); 14762 if (dst->ipp_dstopts == NULL) { 14763 ip_pkt_free(dst); 14764 return (ENOMEM); 14765 } 14766 dst->ipp_fields |= IPPF_DSTOPTS; 14767 bcopy(src->ipp_dstopts, dst->ipp_dstopts, 14768 src->ipp_dstoptslen); 14769 dst->ipp_dstoptslen = src->ipp_dstoptslen; 14770 } 14771 if (fields & IPPF_RTHDR) { 14772 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag); 14773 if (dst->ipp_rthdr == NULL) { 14774 ip_pkt_free(dst); 14775 return (ENOMEM); 14776 } 14777 dst->ipp_fields |= IPPF_RTHDR; 14778 bcopy(src->ipp_rthdr, dst->ipp_rthdr, 14779 src->ipp_rthdrlen); 14780 dst->ipp_rthdrlen = src->ipp_rthdrlen; 14781 } 14782 if (fields & IPPF_IPV4_OPTIONS) { 14783 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len, 14784 kmflag); 14785 if (dst->ipp_ipv4_options == NULL) { 14786 ip_pkt_free(dst); 14787 return (ENOMEM); 14788 } 14789 dst->ipp_fields |= IPPF_IPV4_OPTIONS; 14790 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options, 14791 src->ipp_ipv4_options_len); 14792 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len; 14793 } 14794 if (fields & IPPF_LABEL_V4) { 14795 dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag); 14796 if (dst->ipp_label_v4 == NULL) { 14797 ip_pkt_free(dst); 14798 return (ENOMEM); 14799 } 14800 dst->ipp_fields |= IPPF_LABEL_V4; 14801 bcopy(src->ipp_label_v4, dst->ipp_label_v4, 14802 src->ipp_label_len_v4); 14803 dst->ipp_label_len_v4 = src->ipp_label_len_v4; 14804 } 14805 if (fields & IPPF_LABEL_V6) { 14806 dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag); 14807 if (dst->ipp_label_v6 == NULL) { 14808 ip_pkt_free(dst); 14809 return (ENOMEM); 14810 } 14811 dst->ipp_fields |= IPPF_LABEL_V6; 14812 bcopy(src->ipp_label_v6, dst->ipp_label_v6, 14813 src->ipp_label_len_v6); 14814 dst->ipp_label_len_v6 = src->ipp_label_len_v6; 14815 } 14816 if (fields & IPPF_FRAGHDR) { 14817 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag); 14818 if (dst->ipp_fraghdr == NULL) { 14819 ip_pkt_free(dst); 14820 return (ENOMEM); 14821 } 14822 dst->ipp_fields |= IPPF_FRAGHDR; 14823 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr, 14824 src->ipp_fraghdrlen); 14825 dst->ipp_fraghdrlen = src->ipp_fraghdrlen; 14826 } 14827 return (0); 14828 } 14829 14830 /* 14831 * Returns INADDR_ANY if no source route 14832 */ 14833 ipaddr_t 14834 ip_pkt_source_route_v4(const ip_pkt_t *ipp) 14835 { 14836 ipaddr_t nexthop = INADDR_ANY; 14837 ipoptp_t opts; 14838 uchar_t *opt; 14839 uint8_t optval; 14840 uint8_t optlen; 14841 uint32_t totallen; 14842 14843 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS)) 14844 return (INADDR_ANY); 14845 14846 totallen = ipp->ipp_ipv4_options_len; 14847 if (totallen & 0x3) 14848 return (INADDR_ANY); 14849 14850 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options); 14851 optval != IPOPT_EOL; 14852 optval = ipoptp_next(&opts)) { 14853 opt = opts.ipoptp_cur; 14854 switch (optval) { 14855 uint8_t off; 14856 case IPOPT_SSRR: 14857 case IPOPT_LSRR: 14858 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 14859 break; 14860 } 14861 optlen = opts.ipoptp_len; 14862 off = opt[IPOPT_OFFSET]; 14863 off--; 14864 if (optlen < IP_ADDR_LEN || 14865 off > optlen - IP_ADDR_LEN) { 14866 /* End of source route */ 14867 break; 14868 } 14869 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN); 14870 if (nexthop == htonl(INADDR_LOOPBACK)) { 14871 /* Ignore */ 14872 nexthop = INADDR_ANY; 14873 break; 14874 } 14875 break; 14876 } 14877 } 14878 return (nexthop); 14879 } 14880 14881 /* 14882 * Reverse a source route. 14883 */ 14884 void 14885 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp) 14886 { 14887 ipaddr_t tmp; 14888 ipoptp_t opts; 14889 uchar_t *opt; 14890 uint8_t optval; 14891 uint32_t totallen; 14892 14893 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS)) 14894 return; 14895 14896 totallen = ipp->ipp_ipv4_options_len; 14897 if (totallen & 0x3) 14898 return; 14899 14900 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options); 14901 optval != IPOPT_EOL; 14902 optval = ipoptp_next(&opts)) { 14903 uint8_t off1, off2; 14904 14905 opt = opts.ipoptp_cur; 14906 switch (optval) { 14907 case IPOPT_SSRR: 14908 case IPOPT_LSRR: 14909 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 14910 break; 14911 } 14912 off1 = IPOPT_MINOFF_SR - 1; 14913 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 14914 while (off2 > off1) { 14915 bcopy(opt + off2, &tmp, IP_ADDR_LEN); 14916 bcopy(opt + off1, opt + off2, IP_ADDR_LEN); 14917 bcopy(&tmp, opt + off2, IP_ADDR_LEN); 14918 off2 -= IP_ADDR_LEN; 14919 off1 += IP_ADDR_LEN; 14920 } 14921 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 14922 break; 14923 } 14924 } 14925 } 14926 14927 /* 14928 * Returns NULL if no routing header 14929 */ 14930 in6_addr_t * 14931 ip_pkt_source_route_v6(const ip_pkt_t *ipp) 14932 { 14933 in6_addr_t *nexthop = NULL; 14934 ip6_rthdr0_t *rthdr; 14935 14936 if (!(ipp->ipp_fields & IPPF_RTHDR)) 14937 return (NULL); 14938 14939 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr; 14940 if (rthdr->ip6r0_segleft == 0) 14941 return (NULL); 14942 14943 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr)); 14944 return (nexthop); 14945 } 14946 14947 zoneid_t 14948 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira, 14949 zoneid_t lookup_zoneid) 14950 { 14951 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 14952 ire_t *ire; 14953 int ire_flags = MATCH_IRE_TYPE; 14954 zoneid_t zoneid = ALL_ZONES; 14955 14956 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE)) 14957 return (ALL_ZONES); 14958 14959 if (lookup_zoneid != ALL_ZONES) 14960 ire_flags |= MATCH_IRE_ZONEONLY; 14961 ire = ire_ftable_lookup_v4(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK, 14962 NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL); 14963 if (ire != NULL) { 14964 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst); 14965 ire_refrele(ire); 14966 } 14967 return (zoneid); 14968 } 14969 14970 zoneid_t 14971 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill, 14972 ip_recv_attr_t *ira, zoneid_t lookup_zoneid) 14973 { 14974 ip_stack_t *ipst = ira->ira_ill->ill_ipst; 14975 ire_t *ire; 14976 int ire_flags = MATCH_IRE_TYPE; 14977 zoneid_t zoneid = ALL_ZONES; 14978 14979 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE)) 14980 return (ALL_ZONES); 14981 14982 if (IN6_IS_ADDR_LINKLOCAL(addr)) 14983 ire_flags |= MATCH_IRE_ILL; 14984 14985 if (lookup_zoneid != ALL_ZONES) 14986 ire_flags |= MATCH_IRE_ZONEONLY; 14987 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK, 14988 ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL); 14989 if (ire != NULL) { 14990 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst); 14991 ire_refrele(ire); 14992 } 14993 return (zoneid); 14994 } 14995 14996 /* 14997 * IP obserability hook support functions. 14998 */ 14999 static void 15000 ipobs_init(ip_stack_t *ipst) 15001 { 15002 netid_t id; 15003 15004 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid); 15005 15006 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET); 15007 VERIFY(ipst->ips_ip4_observe_pr != NULL); 15008 15009 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6); 15010 VERIFY(ipst->ips_ip6_observe_pr != NULL); 15011 } 15012 15013 static void 15014 ipobs_fini(ip_stack_t *ipst) 15015 { 15016 15017 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0); 15018 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0); 15019 } 15020 15021 /* 15022 * hook_pkt_observe_t is composed in network byte order so that the 15023 * entire mblk_t chain handed into hook_run can be used as-is. 15024 * The caveat is that use of the fields, such as the zone fields, 15025 * requires conversion into host byte order first. 15026 */ 15027 void 15028 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst, 15029 const ill_t *ill, ip_stack_t *ipst) 15030 { 15031 hook_pkt_observe_t *hdr; 15032 uint64_t grifindex; 15033 mblk_t *imp; 15034 15035 imp = allocb(sizeof (*hdr), BPRI_HI); 15036 if (imp == NULL) 15037 return; 15038 15039 hdr = (hook_pkt_observe_t *)imp->b_rptr; 15040 /* 15041 * b_wptr is set to make the apparent size of the data in the mblk_t 15042 * to exclude the pointers at the end of hook_pkt_observer_t. 15043 */ 15044 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t); 15045 imp->b_cont = mp; 15046 15047 ASSERT(DB_TYPE(mp) == M_DATA); 15048 15049 if (IS_UNDER_IPMP(ill)) 15050 grifindex = ipmp_ill_get_ipmp_ifindex(ill); 15051 else 15052 grifindex = 0; 15053 15054 hdr->hpo_version = 1; 15055 hdr->hpo_htype = htons(htype); 15056 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp)); 15057 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex); 15058 hdr->hpo_grifindex = htonl(grifindex); 15059 hdr->hpo_zsrc = htonl(zsrc); 15060 hdr->hpo_zdst = htonl(zdst); 15061 hdr->hpo_pkt = imp; 15062 hdr->hpo_ctx = ipst->ips_netstack; 15063 15064 if (ill->ill_isv6) { 15065 hdr->hpo_family = AF_INET6; 15066 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks, 15067 ipst->ips_ipv6observing, (hook_data_t)hdr); 15068 } else { 15069 hdr->hpo_family = AF_INET; 15070 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks, 15071 ipst->ips_ipv4observing, (hook_data_t)hdr); 15072 } 15073 15074 imp->b_cont = NULL; 15075 freemsg(imp); 15076 } 15077 15078 /* 15079 * Utility routine that checks if `v4srcp' is a valid address on underlying 15080 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif 15081 * associated with `v4srcp' on success. NOTE: if this is not called from 15082 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the 15083 * group during or after this lookup. 15084 */ 15085 boolean_t 15086 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp) 15087 { 15088 ipif_t *ipif; 15089 15090 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst); 15091 if (ipif != NULL) { 15092 if (ipifp != NULL) 15093 *ipifp = ipif; 15094 else 15095 ipif_refrele(ipif); 15096 return (B_TRUE); 15097 } 15098 15099 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n", 15100 *v4srcp)); 15101 return (B_FALSE); 15102 } 15103