/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ /* Copyright (c) 1990 Mentat Inc. */ #include #include #include #include #include #include #include #include #include #define _SUN_TPI_VERSION 2 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* * Values for squeue switch: * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain * IP_SQUEUE_ENTER: squeue_enter * IP_SQUEUE_FILL: squeue_fill */ int ip_squeue_enter = 2; /* Setable in /etc/system */ squeue_func_t ip_input_proc; #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x)) /* * Setable in /etc/system */ int ip_poll_normal_ms = 100; int ip_poll_normal_ticks = 0; int ip_modclose_ackwait_ms = 3000; /* * It would be nice to have these present only in DEBUG systems, but the * current design of the global symbol checking logic requires them to be * unconditionally present. */ uint_t ip_thread_data; /* TSD key for debug support */ krwlock_t ip_thread_rwlock; list_t ip_thread_list; /* * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions. */ struct listptr_s { mblk_t *lp_head; /* pointer to the head of the list */ mblk_t *lp_tail; /* pointer to the tail of the list */ }; typedef struct listptr_s listptr_t; /* * This is used by ip_snmp_get_mib2_ip_route_media and * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data. */ typedef struct iproutedata_s { uint_t ird_idx; listptr_t ird_route; /* ipRouteEntryTable */ listptr_t ird_netmedia; /* ipNetToMediaEntryTable */ listptr_t ird_attrs; /* ipRouteAttributeTable */ } iproutedata_t; /* * Cluster specific hooks. These should be NULL when booted as a non-cluster */ /* * Hook functions to enable cluster networking * On non-clustered systems these vectors must always be NULL. * * Hook function to Check ip specified ip address is a shared ip address * in the cluster * */ int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, uint8_t *laddrp) = NULL; /* * Hook function to generate cluster wide ip fragment identifier */ uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp) = NULL; /* * Synchronization notes: * * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any * MT level protection given by STREAMS. IP uses a combination of its own * internal serialization mechanism and standard Solaris locking techniques. * The internal serialization is per phyint (no IPMP) or per IPMP group. * This is used to serialize plumbing operations, IPMP operations, certain * multicast operations, most set ioctls, igmp/mld timers etc. * * Plumbing is a long sequence of operations involving message * exchanges between IP, ARP and device drivers. Many set ioctls are typically * involved in plumbing operations. A natural model is to serialize these * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in * parallel without any interference. But various set ioctls on hme0 are best * serialized. However if the system uses IPMP, the operations are easier if * they are serialized on a per IPMP group basis since IPMP operations * happen across ill's of a group. Thus the lowest common denominator is to * serialize most set ioctls, multicast join/leave operations, IPMP operations * igmp/mld timer operations, and processing of DLPI control messages received * from drivers on a per IPMP group basis. If the system does not employ * IPMP the serialization is on a per phyint basis. This serialization is * provided by the ipsq_t and primitives operating on this. Details can * be found in ip_if.c above the core primitives operating on ipsq_t. * * Lookups of an ipif or ill by a thread return a refheld ipif / ill. * Simiarly lookup of an ire by a thread also returns a refheld ire. * In addition ipif's and ill's referenced by the ire are also indirectly * refheld. Thus no ipif or ill can vanish nor can critical parameters like * the ipif's address or netmask change as long as an ipif is refheld * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the * address of an ipif has to go through the ipsq_t. This ensures that only * 1 such exclusive operation proceeds at any time on the ipif. It then * deletes all ires associated with this ipif, and waits for all refcnts * associated with this ipif to come down to zero. The address is changed * only after the ipif has been quiesced. Then the ipif is brought up again. * More details are described above the comment in ip_sioctl_flags. * * Packet processing is based mostly on IREs and are fully multi-threaded * using standard Solaris MT techniques. * * There are explicit locks in IP to handle: * - The ip_g_head list maintained by mi_open_link() and friends. * * - The reassembly data structures (one lock per hash bucket) * * - conn_lock is meant to protect conn_t fields. The fields actually * protected by conn_lock are documented in the conn_t definition. * * - ire_lock to protect some of the fields of the ire, IRE tables * (one lock per hash bucket). Refer to ip_ire.c for details. * * - ndp_g_lock and nce_lock for protecting NCEs. * * - ill_lock protects fields of the ill and ipif. Details in ip.h * * - ill_g_lock: This is a global reader/writer lock. Protects the following * * The AVL tree based global multi list of all ills. * * The linked list of all ipifs of an ill * * The mapping * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next * * The illgroup list threaded by ill_group_next. * * association * Insertion/deletion of an ill in the system, insertion/deletion of an ipif * into an ill, changing the mapping of an ill, insertion/deletion * of an ill into the illgrp list, changing the assoc of an ill * will all have to hold the ill_g_lock as writer for the actual duration * of the insertion/deletion/change. More details about the mapping * may be found in the IPMP section. * * - ill_lock: This is a per ill mutex. * It protects some members of the ill and is documented below. * It also protects the mapping * It also protects the illgroup list threaded by ill_group_next. * It also protects the assoc. * It also protects the list of ipifs hanging off the ill. * * - ipsq_lock: This is a per ipsq_t mutex lock. * This protects all the other members of the ipsq struct except * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock * * - illgrp_lock: This is a per ill_group mutex lock. * The only thing it protects is the illgrp_ill_schednext member of ill_group * which dictates which is the next ill in an ill_group that is to be chosen * for sending outgoing packets, through creation of an IRE_CACHE that * references this ill. * * - phyint_lock: This is a per phyint mutex lock. Protects just the * phyint_flags * * - ip_g_nd_lock: This is a global reader/writer lock. * Any call to nd_load to load a new parameter to the ND table must hold the * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock * as reader. * * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses. * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the * uniqueness check also done atomically. * * - ipsec_capab_ills_lock: This readers/writer lock protects the global * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken * as a writer when adding or deleting elements from these lists, and * as a reader when walking these lists to send a SADB update to the * IPsec capable ills. * * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc * group list linked by ill_usesrc_grp_next. It also protects the * ill_usesrc_ifindex field. It is taken as a writer when a member of the * group is being added or deleted. This lock is taken as a reader when * walking the list/group(eg: to get the number of members in a usesrc group). * Note, it is only necessary to take this lock if the ill_usesrc_grp_next * field is changing state i.e from NULL to non-NULL or vice-versa. For * example, it is not necessary to take this lock in the initial portion * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and * ip_sioctl_flags since the these operations are executed exclusively and * that ensures that the "usesrc group state" cannot change. The "usesrc * group state" change can happen only in the latter part of * ip_sioctl_slifusesrc and in ill_delete. * * Changing , , assocications. * * To change the association, the ill_g_lock must be held * as writer, and the ill_locks of both the v4 and v6 instance of the ill * must be held. * * To change the association the ill_g_lock must be held as writer * and the ill_lock of the ill in question must be held. * * To change the association the ill_g_lock must be held as * writer and the ill_lock of the ill in question must be held. * * To add or delete an ipif from the list of ipifs hanging off the ill, * ill_g_lock (writer) and ill_lock must be held and the thread must be * a writer on the associated ipsq,. * * To add or delete an ill to the system, the ill_g_lock must be held as * writer and the thread must be a writer on the associated ipsq. * * To add or delete an ilm to an ill, the ill_lock must be held and the thread * must be a writer on the associated ipsq. * * Lock hierarchy * * Some lock hierarchy scenarios are listed below. * * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock * ill_g_lock -> illgrp_lock -> ill_lock * ill_g_lock -> ill_lock(s) -> phyint_lock * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock * ill_g_lock -> ip_addr_avail_lock * conn_lock -> irb_lock -> ill_lock -> ire_lock * ill_g_lock -> ip_g_nd_lock * * When more than 1 ill lock is needed to be held, all ill lock addresses * are sorted on address and locked starting from highest addressed lock * downward. * * IPsec scenarios * * ipsa_lock -> ill_g_lock -> ill_lock * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock * ipsec_capab_ills_lock -> ipsa_lock * ill_g_usesrc_lock -> ill_g_lock -> ill_lock * * Trusted Solaris scenarios * * igsa_lock -> gcgrp_rwlock -> gcgrp_lock * igsa_lock -> gcdb_lock * gcgrp_rwlock -> ire_lock * gcgrp_rwlock -> gcdb_lock * * * Routing/forwarding table locking notes: * * Lock acquisition order: Radix tree lock, irb_lock. * Requirements: * i. Walker must not hold any locks during the walker callback. * ii Walker must not see a truncated tree during the walk because of any node * deletion. * iii Existing code assumes ire_bucket is valid if it is non-null and is used * in many places in the code to walk the irb list. Thus even if all the * ires in a bucket have been deleted, we still can't free the radix node * until the ires have actually been inactive'd (freed). * * Tree traversal - Need to hold the global tree lock in read mode. * Before dropping the global tree lock, need to either increment the ire_refcnt * to ensure that the radix node can't be deleted. * * Tree add - Need to hold the global tree lock in write mode to add a * radix node. To prevent the node from being deleted, increment the * irb_refcnt, after the node is added to the tree. The ire itself is * added later while holding the irb_lock, but not the tree lock. * * Tree delete - Need to hold the global tree lock and irb_lock in write mode. * All associated ires must be inactive (i.e. freed), and irb_refcnt * must be zero. * * Walker - Increment irb_refcnt before calling the walker callback. Hold the * global tree lock (read mode) for traversal. * * IPsec notes : * * IP interacts with the IPsec code (AH/ESP) by tagging a M_CTL message * in front of the actual packet. For outbound datagrams, the M_CTL * contains a ipsec_out_t (defined in ipsec_info.h), which has the * information used by the IPsec code for applying the right level of * protection. The information initialized by IP in the ipsec_out_t * is determined by the per-socket policy or global policy in the system. * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in * ipsec_info.h) which starts out with nothing in it. It gets filled * with the right information if it goes through the AH/ESP code, which * happens if the incoming packet is secure. The information initialized * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether * the policy requirements needed by per-socket policy or global policy * is met or not. * * If there is both per-socket policy (set using setsockopt) and there * is also global policy match for the 5 tuples of the socket, * ipsec_override_policy() makes the decision of which one to use. * * For fully connected sockets i.e dst, src [addr, port] is known, * conn_policy_cached is set indicating that policy has been cached. * conn_in_enforce_policy may or may not be set depending on whether * there is a global policy match or per-socket policy match. * Policy inheriting happpens in ip_bind during the ipa_conn_t bind. * Once the right policy is set on the conn_t, policy cannot change for * this socket. This makes life simpler for TCP (UDP ?) where * re-transmissions go out with the same policy. For symmetry, policy * is cached for fully connected UDP sockets also. Thus if policy is cached, * it also implies that policy is latched i.e policy cannot change * on these sockets. As we have the right policy on the conn, we don't * have to lookup global policy for every outbound and inbound datagram * and thus serving as an optimization. Note that a global policy change * does not affect fully connected sockets if they have policy. If fully * connected sockets did not have any policy associated with it, global * policy change may affect them. * * IP Flow control notes: * * Non-TCP streams are flow controlled by IP. On the send side, if the packet * cannot be sent down to the driver by IP, because of a canput failure, IP * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq. * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained * when the flowcontrol condition subsides. Ultimately STREAMS backenables the * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the * first conn in the list of conn's to be drained. ip_wsrv on this conn drains * the queued messages, and removes the conn from the drain list, if all * messages were drained. It also qenables the next conn in the drain list to * continue the drain process. * * In reality the drain list is not a single list, but a configurable number * of lists. The ip_wsrv on the IP module, qenables the first conn in each * list. If the ip_wsrv of the next qenabled conn does not run, because the * stream closes, ip_close takes responsibility to qenable the next conn in * the drain list. The directly called ip_wput path always does a putq, if * it cannot putnext. Thus synchronization problems are handled between * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only * functions that manipulate this drain list. Furthermore conn_drain_insert * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv * running on a queue at any time. conn_drain_tail can be simultaneously called * from both ip_wsrv and ip_close. * * IPQOS notes: * * IPQoS Policies are applied to packets using IPPF (IP Policy framework) * and IPQoS modules. IPPF includes hooks in IP at different control points * (callout positions) which direct packets to IPQoS modules for policy * processing. Policies, if present, are global. * * The callout positions are located in the following paths: * o local_in (packets destined for this host) * o local_out (packets orginating from this host ) * o fwd_in (packets forwarded by this m/c - inbound) * o fwd_out (packets forwarded by this m/c - outbound) * Hooks at these callout points can be enabled/disabled using the ndd variable * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions). * By default all the callout positions are enabled. * * Outbound (local_out) * Hooks are placed in ip_wput_ire and ipsec_out_process. * * Inbound (local_in) * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and * TCP and UDP fanout routines. * * Forwarding (in and out) * Hooks are placed in ip_rput_forward. * * IP Policy Framework processing (IPPF processing) * Policy processing for a packet is initiated by ip_process, which ascertains * that the classifier (ipgpc) is loaded and configured, failing which the * packet resumes normal processing in IP. If the clasifier is present, the * packet is acted upon by one or more IPQoS modules (action instances), per * filters configured in ipgpc and resumes normal IP processing thereafter. * An action instance can drop a packet in course of its processing. * * A boolean variable, ip_policy, is used in all the fanout routines that can * invoke ip_process for a packet. This variable indicates if the packet should * to be sent for policy processing. The variable is set to B_TRUE by default, * i.e. when the routines are invoked in the normal ip procesing path for a * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout; * ip_policy is set to B_FALSE for all the routines called in these two * functions because, in the former case, we don't process loopback traffic * currently while in the latter, the packets have already been processed in * icmp_inbound. * * Zones notes: * * The partitioning rules for networking are as follows: * 1) Packets coming from a zone must have a source address belonging to that * zone. * 2) Packets coming from a zone can only be sent on a physical interface on * which the zone has an IP address. * 3) Between two zones on the same machine, packet delivery is only allowed if * there's a matching route for the destination and zone in the forwarding * table. * 4) The TCP and UDP port spaces are per-zone; that is, two processes in * different zones can bind to the same port with the wildcard address * (INADDR_ANY). * * The granularity of interface partitioning is at the logical interface level. * Therefore, every zone has its own IP addresses, and incoming packets can be * attributed to a zone unambiguously. A logical interface is placed into a zone * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t * structure. Rule (1) is implemented by modifying the source address selection * algorithm so that the list of eligible addresses is filtered based on the * sending process zone. * * The Internet Routing Entries (IREs) are either exclusive to a zone or shared * across all zones, depending on their type. Here is the break-up: * * IRE type Shared/exclusive * -------- ---------------- * IRE_BROADCAST Exclusive * IRE_DEFAULT (default routes) Shared (*) * IRE_LOCAL Exclusive (x) * IRE_LOOPBACK Exclusive * IRE_PREFIX (net routes) Shared (*) * IRE_CACHE Exclusive * IRE_IF_NORESOLVER (interface routes) Exclusive * IRE_IF_RESOLVER (interface routes) Exclusive * IRE_HOST (host routes) Shared (*) * * (*) A zone can only use a default or off-subnet route if the gateway is * directly reachable from the zone, that is, if the gateway's address matches * one of the zone's logical interfaces. * * (x) IRE_LOCAL are handled a bit differently, since for all other entries * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP * address of the zone itself (the destination). Since IRE_LOCAL is used * for communication between zones, ip_wput_ire has special logic to set * the right source address when sending using an IRE_LOCAL. * * Furthermore, when ip_restrict_interzone_loopback is set (the default), * ire_cache_lookup restricts loopback using an IRE_LOCAL * between zone to the case when L2 would have conceptually looped the packet * back, i.e. the loopback which is required since neither Ethernet drivers * nor Ethernet hardware loops them back. This is the case when the normal * routes (ignoring IREs with different zoneids) would send out the packet on * the same ill (or ill group) as the ill with which is IRE_LOCAL is * associated. * * Multiple zones can share a common broadcast address; typically all zones * share the 255.255.255.255 address. Incoming as well as locally originated * broadcast packets must be dispatched to all the zones on the broadcast * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial * since some zones may not be on the 10.16.72/24 network. To handle this, each * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are * sent to every zone that has an IRE_BROADCAST entry for the destination * address on the input ill, see conn_wantpacket(). * * Applications in different zones can join the same multicast group address. * For IPv4, group memberships are per-logical interface, so they're already * inherently part of a zone. For IPv6, group memberships are per-physical * interface, so we distinguish IPv6 group memberships based on group address, * interface and zoneid. In both cases, received multicast packets are sent to * every zone for which a group membership entry exists. On IPv6 we need to * check that the target zone still has an address on the receiving physical * interface; it could have been removed since the application issued the * IPV6_JOIN_GROUP. */ /* * Squeue Fanout flags: * 0: No fanout. * 1: Fanout across all squeues */ boolean_t ip_squeue_fanout = 0; /* * Maximum dups allowed per packet. */ uint_t ip_max_frag_dups = 10; #define IS_SIMPLE_IPH(ipha) \ ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION) /* RFC1122 Conformance */ #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER #define ILL_MAX_NAMELEN LIFNAMSIZ static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *); static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, boolean_t isv6); static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t, ipha_t **); static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t, ip_stack_t *); static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int, uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t); static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp); static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t, mblk_t *, int, ip_stack_t *); static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *, icmph_t *, ipha_t *, int, int, boolean_t, boolean_t, ill_t *, zoneid_t); static void icmp_options_update(ipha_t *); static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t, ip_stack_t *); static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t, zoneid_t zoneid, ip_stack_t *); static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *); static void icmp_redirect(ill_t *, mblk_t *); static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t, ip_stack_t *); static void ip_arp_news(queue_t *, mblk_t *); static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *, ip_stack_t *); mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t); char *ip_dot_addr(ipaddr_t, char *); mblk_t *ip_carve_mp(mblk_t **, ssize_t); int ip_close(queue_t *, int); static char *ip_dot_saddr(uchar_t *, char *); static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t, boolean_t, boolean_t, zoneid_t); static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t, boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t); static void ip_lrput(queue_t *, mblk_t *); ipaddr_t ip_net_mask(ipaddr_t); void ip_newroute(queue_t *, mblk_t *, ipaddr_t, conn_t *, zoneid_t, ip_stack_t *); static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t, conn_t *, uint32_t, zoneid_t, ip_opt_info_t *); char *ip_nv_lookup(nv_t *, int); static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *); static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *); static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *); static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t, ipndp_t *, size_t); static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); void ip_rput(queue_t *, mblk_t *); static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg); void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *); static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *, ip_stack_t *); static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *, ire_t *, ip_stack_t *); static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *, mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *); static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *, ip_stack_t *); static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *, uint16_t *); int ip_snmp_get(queue_t *, mblk_t *, int); static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *, mib2_ipIfStatsEntry_t *, ip_stack_t *); static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *, ip_stack_t *); static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *); static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, ip_stack_t *ipst); static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, ip_stack_t *ipst); static void ip_snmp_get2_v4(ire_t *, iproutedata_t *); static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *); static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *); int ip_snmp_set(queue_t *, int, int, uchar_t *, int); static boolean_t ip_source_routed(ipha_t *, ip_stack_t *); static boolean_t ip_source_route_included(ipha_t *); static void ip_trash_ire_reclaim_stack(ip_stack_t *); static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t, zoneid_t, ip_stack_t *); static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *); static void ip_wput_local_options(ipha_t *, ip_stack_t *); static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t, zoneid_t, ip_stack_t *); static void conn_drain_init(ip_stack_t *); static void conn_drain_fini(ip_stack_t *); static void conn_drain_tail(conn_t *connp, boolean_t closing); static void conn_walk_drain(ip_stack_t *); static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *, zoneid_t); static void *ip_stack_init(netstackid_t stackid, netstack_t *ns); static void ip_stack_shutdown(netstackid_t stackid, void *arg); static void ip_stack_fini(netstackid_t stackid, void *arg); static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int, zoneid_t); static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp, void *dummy_arg); static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *, conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *); static void ip_multirt_bad_mtu(ire_t *, uint32_t); static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *); static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr); extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr); static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); static int ipmp_hook_emulation_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *); static squeue_func_t ip_squeue_switch(int); static void *ip_kstat_init(netstackid_t, ip_stack_t *); static void ip_kstat_fini(netstackid_t, kstat_t *); static int ip_kstat_update(kstat_t *kp, int rw); static void *icmp_kstat_init(netstackid_t); static void icmp_kstat_fini(netstackid_t, kstat_t *); static int icmp_kstat_update(kstat_t *kp, int rw); static void *ip_kstat2_init(netstackid_t, ip_stat_t *); static void ip_kstat2_fini(netstackid_t, kstat_t *); static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *); static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t, ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *); static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *, ipha_t *, ill_t *, boolean_t); ipaddr_t ip_g_all_ones = IP_HOST_MASK; /* How long, in seconds, we allow frags to hang around. */ #define IP_FRAG_TIMEOUT 60 /* * Threshold which determines whether MDT should be used when * generating IP fragments; payload size must be greater than * this threshold for MDT to take place. */ #define IP_WPUT_FRAG_MDT_MIN 32768 /* Setable in /etc/system only */ int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN; static long ip_rput_pullups; int dohwcksum = 1; /* use h/w cksum if supported by the hardware */ vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */ vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */ int ip_debug; #ifdef DEBUG uint32_t ipsechw_debug = 0; #endif /* * Multirouting/CGTP stuff */ int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */ /* * XXX following really should only be in a header. Would need more * header and .c clean up first. */ extern optdb_obj_t ip_opt_obj; ulong_t ip_squeue_enter_unbound = 0; /* * Named Dispatch Parameter Table. * All of these are alterable, within the min/max values given, at run time. */ static ipparam_t lcl_param_arr[] = { /* min max value name */ { 0, 1, 0, "ip_respond_to_address_mask_broadcast"}, { 0, 1, 1, "ip_respond_to_echo_broadcast"}, { 0, 1, 1, "ip_respond_to_echo_multicast"}, { 0, 1, 0, "ip_respond_to_timestamp"}, { 0, 1, 0, "ip_respond_to_timestamp_broadcast"}, { 0, 1, 1, "ip_send_redirects"}, { 0, 1, 0, "ip_forward_directed_broadcasts"}, { 0, 10, 0, "ip_mrtdebug"}, { 5000, 999999999, 60000, "ip_ire_timer_interval" }, { 60000, 999999999, 1200000, "ip_ire_arp_interval" }, { 60000, 999999999, 60000, "ip_ire_redirect_interval" }, { 1, 255, 255, "ip_def_ttl" }, { 0, 1, 0, "ip_forward_src_routed"}, { 0, 256, 32, "ip_wroff_extra" }, { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" }, { 8, 65536, 64, "ip_icmp_return_data_bytes" }, { 0, 1, 1, "ip_path_mtu_discovery" }, { 0, 240, 30, "ip_ignore_delete_time" }, { 0, 1, 0, "ip_ignore_redirect" }, { 0, 1, 1, "ip_output_queue" }, { 1, 254, 1, "ip_broadcast_ttl" }, { 0, 99999, 100, "ip_icmp_err_interval" }, { 1, 99999, 10, "ip_icmp_err_burst" }, { 0, 999999999, 1000000, "ip_reass_queue_bytes" }, { 0, 1, 0, "ip_strict_dst_multihoming" }, { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"}, { 0, 1, 0, "ipsec_override_persocket_policy" }, { 0, 1, 1, "icmp_accept_clear_messages" }, { 0, 1, 1, "igmp_accept_clear_messages" }, { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME, "ip_ndp_delay_first_probe_time"}, { 1, 999999999, ND_MAX_UNICAST_SOLICIT, "ip_ndp_max_unicast_solicit"}, { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" }, { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" }, { 0, 1, 0, "ip6_forward_src_routed"}, { 0, 1, 1, "ip6_respond_to_echo_multicast"}, { 0, 1, 1, "ip6_send_redirects"}, { 0, 1, 0, "ip6_ignore_redirect" }, { 0, 1, 0, "ip6_strict_dst_multihoming" }, { 1, 8, 3, "ip_ire_reclaim_fraction" }, { 0, 999999, 1000, "ipsec_policy_log_interval" }, { 0, 1, 1, "pim_accept_clear_messages" }, { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" }, { 1, 20, 3, "ip_ndp_unsolicit_count" }, { 0, 1, 1, "ip6_ignore_home_address_opt" }, { 0, 15, 0, "ip_policy_mask" }, { 1000, 60000, 1000, "ip_multirt_resolution_interval" }, { 0, 255, 1, "ip_multirt_ttl" }, { 0, 1, 1, "ip_multidata_outbound" }, { 0, 3600000, 300000, "ip_ndp_defense_interval" }, { 0, 999999, 60*60*24, "ip_max_temp_idle" }, { 0, 1000, 1, "ip_max_temp_defend" }, { 0, 1000, 3, "ip_max_defend" }, { 0, 999999, 30, "ip_defend_interval" }, { 0, 3600000, 300000, "ip_dup_recovery" }, { 0, 1, 1, "ip_restrict_interzone_loopback" }, { 0, 1, 1, "ip_lso_outbound" }, { IGMP_V1_ROUTER, IGMP_V3_ROUTER, IGMP_V3_ROUTER, "igmp_max_version" }, { MLD_V1_ROUTER, MLD_V2_ROUTER, MLD_V2_ROUTER, "mld_max_version" }, #ifdef DEBUG { 0, 1, 0, "ip6_drop_inbound_icmpv6" }, #else { 0, 0, 0, "" }, #endif }; /* * Extended NDP table * The addresses for the first two are filled in to be ips_ip_g_forward * and ips_ipv6_forward at init time. */ static ipndp_t lcl_ndp_arr[] = { /* getf setf data name */ #define IPNDP_IP_FORWARDING_OFFSET 0 { ip_param_generic_get, ip_forward_set, NULL, "ip_forwarding" }, #define IPNDP_IP6_FORWARDING_OFFSET 1 { ip_param_generic_get, ip_forward_set, NULL, "ip6_forwarding" }, { ip_ill_report, NULL, NULL, "ip_ill_status" }, { ip_ipif_report, NULL, NULL, "ip_ipif_status" }, { ip_conn_report, NULL, NULL, "ip_conn_status" }, { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups, "ip_rput_pullups" }, { ip_srcid_report, NULL, NULL, "ip_srcid_status" }, { ip_param_generic_get, ip_squeue_profile_set, (caddr_t)&ip_squeue_profile, "ip_squeue_profile" }, { ip_param_generic_get, ip_squeue_bind_set, (caddr_t)&ip_squeue_bind, "ip_squeue_bind" }, { ip_param_generic_get, ip_input_proc_set, (caddr_t)&ip_squeue_enter, "ip_squeue_enter" }, { ip_param_generic_get, ip_int_set, (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" }, #define IPNDP_CGTP_FILTER_OFFSET 11 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL, "ip_cgtp_filter" }, { ip_param_generic_get, ip_int_set, (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" }, #define IPNDP_IPMP_HOOK_OFFSET 13 { ip_param_generic_get, ipmp_hook_emulation_set, NULL, "ipmp_hook_emulation" }, { ip_param_generic_get, ip_int_set, (caddr_t)&ip_debug, "ip_debug" }, }; /* * Table of IP ioctls encoding the various properties of the ioctl and * indexed based on the last byte of the ioctl command. Occasionally there * is a clash, and there is more than 1 ioctl with the same last byte. * In such a case 1 ioctl is encoded in the ndx table and the remaining * ioctls are encoded in the misc table. An entry in the ndx table is * retrieved by indexing on the last byte of the ioctl command and comparing * the ioctl command with the value in the ndx table. In the event of a * mismatch the misc table is then searched sequentially for the desired * ioctl command. * * Entry: */ ip_ioctl_cmd_t ip_ndx_ioctl_table[] = { /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV, MISC_CMD, ip_siocaddrt, NULL }, /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV, MISC_CMD, ip_siocdelrt, NULL }, /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_addr, NULL }, /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_dstaddr, NULL }, /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq), IPI_PRIV | IPI_WR | IPI_REPL, IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq), IPI_MODOK | IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_flags, NULL }, /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* copyin size cannot be coded for SIOCGIFCONF */ /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD, MISC_CMD, ip_sioctl_get_ifconf, NULL }, /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_mtu, NULL }, /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_mtu, NULL }, /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_brdaddr, NULL }, /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_brdaddr, NULL }, /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_netmask, NULL }, /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_metric, NULL }, /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV, IF_CMD, ip_sioctl_metric, NULL }, /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* See 166-168 below for extended SIOC*XARP ioctls */ /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV, ARP_CMD, ip_sioctl_arp, NULL }, /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL, ARP_CMD, ip_sioctl_arp, NULL }, /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV, ARP_CMD, ip_sioctl_arp, NULL }, /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK, MISC_CMD, if_unitsel, if_unitsel_restart }, /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq), IPI_PRIV | IPI_WR | IPI_MODOK, IF_CMD, ip_sioctl_sifname, NULL }, /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL, MISC_CMD, ip_sioctl_get_ifnum, NULL }, /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_muxid, NULL }, /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq), IPI_PRIV | IPI_WR | IPI_REPL, IF_CMD, ip_sioctl_muxid, NULL }, /* Both if and lif variants share same func */ /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL, IF_CMD, ip_sioctl_get_lifindex, NULL }, /* Both if and lif variants share same func */ /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq), IPI_PRIV | IPI_WR | IPI_REPL, IF_CMD, ip_sioctl_slifindex, NULL }, /* copyin size cannot be coded for SIOCGIFCONF */ /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD, MISC_CMD, ip_sioctl_get_ifconf, NULL }, /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_removeif, ip_sioctl_removeif_restart }, /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq), IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_addif, NULL }, #define SIOCLIFADDR_NDX 112 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart }, /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_addr, NULL }, /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart }, /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_dstaddr, NULL }, /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart }, /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq), IPI_GET_CMD | IPI_MODOK | IPI_REPL, LIF_CMD, ip_sioctl_get_flags, NULL }, /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, ip_sioctl_get_lifconf, NULL }, /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_mtu, NULL }, /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_mtu, NULL }, /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_brdaddr, NULL }, /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_brdaddr, NULL }, /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_netmask, NULL }, /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart }, /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_metric, NULL }, /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_metric, NULL }, /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL, LIF_CMD, ip_sioctl_slifname, ip_sioctl_slifname_restart }, /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL, MISC_CMD, ip_sioctl_get_lifnum, NULL }, /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_muxid, NULL }, /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_muxid, NULL }, /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_lifindex, 0 }, /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_slifindex, 0 }, /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_token, NULL }, /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_token, NULL }, /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart }, /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_subnet, NULL }, /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_lnkinfo, NULL }, /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_lnkinfo, NULL }, /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV, LIF_CMD, ip_siocdelndp_v6, NULL }, /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD, LIF_CMD, ip_siocqueryndp_v6, NULL }, /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV, LIF_CMD, ip_siocsetndp_v6, NULL }, /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD, MISC_CMD, ip_sioctl_tmyaddr, NULL }, /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD, MISC_CMD, ip_sioctl_tonlink, NULL }, /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0, MISC_CMD, ip_sioctl_tmysite, NULL }, /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req), IPI_PRIV | IPI_WR, TUN_CMD, ip_sioctl_tunparam, NULL }, /* IPSECioctls handled in ip_sioctl_copyin_setup itself */ /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL }, /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_move, ip_sioctl_move }, /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_move, ip_sioctl_move }, /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname }, /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_groupname, NULL }, /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_oindex, NULL }, /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */ /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifoindex, NULL }, /* These are handled in ip_sioctl_copyin_setup itself */ /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT, MISC_CMD, NULL, NULL }, /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT, MISC_CMD, NULL, NULL }, /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL }, /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD, ip_sioctl_get_lifconf, NULL }, /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV, XARP_CMD, ip_sioctl_arp, NULL }, /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL, XARP_CMD, ip_sioctl_arp, NULL }, /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV, XARP_CMD, ip_sioctl_arp, NULL }, /* SIOCPOPSOCKFS is not handled by IP */ /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL }, /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL, LIF_CMD, ip_sioctl_get_lifzone, NULL }, /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq), IPI_PRIV | IPI_WR | IPI_REPL, LIF_CMD, ip_sioctl_slifzone, ip_sioctl_slifzone_restart }, /* 172-174 are SCTP ioctls and not handled by IP */ /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL }, /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq), IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifusesrc, 0 }, /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq), IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifusesrc, NULL }, /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD, ip_sioctl_get_lifsrcof, NULL }, /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD, MSFILT_CMD, ip_sioctl_msfilter, NULL }, /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR, MSFILT_CMD, ip_sioctl_msfilter, NULL }, /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD, MSFILT_CMD, ip_sioctl_msfilter, NULL }, /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR, MSFILT_CMD, ip_sioctl_msfilter, NULL }, /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD, ip_sioctl_set_ipmpfailback, NULL }, /* SIOCSENABLESDP is handled by SDP */ /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL }, }; int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t); ip_ioctl_cmd_t ip_misc_ioctl_table[] = { { OSIOCGTUNPARAM, sizeof (struct old_iftun_req), IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL }, { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR, TUN_CMD, ip_sioctl_tunparam, NULL }, { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL }, { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL }, { IP_IOCTL, 0, 0, 0, NULL, NULL }, { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD, MISC_CMD, mrt_ioctl}, { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD, MISC_CMD, mrt_ioctl}, { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD, MISC_CMD, mrt_ioctl} }; int ip_misc_ioctl_count = sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t); int conn_drain_nthreads; /* Number of drainers reqd. */ /* Settable in /etc/system */ /* Defined in ip_ire.c */ extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt; extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt; extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio; static nv_t ire_nv_arr[] = { { IRE_BROADCAST, "BROADCAST" }, { IRE_LOCAL, "LOCAL" }, { IRE_LOOPBACK, "LOOPBACK" }, { IRE_CACHE, "CACHE" }, { IRE_DEFAULT, "DEFAULT" }, { IRE_PREFIX, "PREFIX" }, { IRE_IF_NORESOLVER, "IF_NORESOL" }, { IRE_IF_RESOLVER, "IF_RESOLV" }, { IRE_HOST, "HOST" }, { 0 } }; nv_t *ire_nv_tbl = ire_nv_arr; /* Simple ICMP IP Header Template */ static ipha_t icmp_ipha = { IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP }; struct module_info ip_mod_info = { IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024 }; /* * Duplicate static symbols within a module confuses mdb; so we avoid the * problem by making the symbols here distinct from those in udp.c. */ /* * Entry points for IP as a device and as a module. * FIXME: down the road we might want a separate module and driver qinit. * We have separate open functions for the /dev/ip and /dev/ip6 devices. */ static struct qinit iprinitv4 = { (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info }; struct qinit iprinitv6 = { (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL, &ip_mod_info }; static struct qinit ipwinitv4 = { (pfi_t)ip_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL, &ip_mod_info }; struct qinit ipwinitv6 = { (pfi_t)ip_wput_v6, (pfi_t)ip_wsrv, NULL, NULL, NULL, &ip_mod_info }; static struct qinit iplrinit = { (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info }; static struct qinit iplwinit = { (pfi_t)ip_lwput, NULL, NULL, NULL, NULL, &ip_mod_info }; /* For AF_INET aka /dev/ip */ struct streamtab ipinfov4 = { &iprinitv4, &ipwinitv4, &iplrinit, &iplwinit }; /* For AF_INET6 aka /dev/ip6 */ struct streamtab ipinfov6 = { &iprinitv6, &ipwinitv6, &iplrinit, &iplwinit }; #ifdef DEBUG static boolean_t skip_sctp_cksum = B_FALSE; #endif /* * Prepend the zoneid using an ipsec_out_t for later use by functions like * ip_rput_v6(), ip_output(), etc. If the message * block already has a M_CTL at the front of it, then simply set the zoneid * appropriately. */ mblk_t * ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst) { mblk_t *first_mp; ipsec_out_t *io; ASSERT(zoneid != ALL_ZONES); if (mp->b_datap->db_type == M_CTL) { io = (ipsec_out_t *)mp->b_rptr; ASSERT(io->ipsec_out_type == IPSEC_OUT); io->ipsec_out_zoneid = zoneid; return (mp); } first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack); if (first_mp == NULL) return (NULL); io = (ipsec_out_t *)first_mp->b_rptr; /* This is not a secure packet */ io->ipsec_out_secure = B_FALSE; io->ipsec_out_zoneid = zoneid; first_mp->b_cont = mp; return (first_mp); } /* * Copy an M_CTL-tagged message, preserving reference counts appropriately. */ mblk_t * ip_copymsg(mblk_t *mp) { mblk_t *nmp; ipsec_info_t *in; if (mp->b_datap->db_type != M_CTL) return (copymsg(mp)); in = (ipsec_info_t *)mp->b_rptr; /* * Note that M_CTL is also used for delivering ICMP error messages * upstream to transport layers. */ if (in->ipsec_info_type != IPSEC_OUT && in->ipsec_info_type != IPSEC_IN) return (copymsg(mp)); nmp = copymsg(mp->b_cont); if (in->ipsec_info_type == IPSEC_OUT) { return (ipsec_out_tag(mp, nmp, ((ipsec_out_t *)in)->ipsec_out_ns)); } else { return (ipsec_in_tag(mp, nmp, ((ipsec_in_t *)in)->ipsec_in_ns)); } } /* Generate an ICMP fragmentation needed message. */ static void icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid, ip_stack_t *ipst) { icmph_t icmph; mblk_t *first_mp; boolean_t mctl_present; EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (!(mp = icmp_pkt_err_ok(mp, ipst))) { if (mctl_present) freeb(first_mp); return; } bzero(&icmph, sizeof (icmph_t)); icmph.icmph_type = ICMP_DEST_UNREACHABLE; icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED; icmph.icmph_du_mtu = htons((uint16_t)mtu); BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded); BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, ipst); } /* * icmp_inbound deals with ICMP messages in the following ways. * * 1) It needs to send a reply back and possibly delivering it * to the "interested" upper clients. * 2) It needs to send it to the upper clients only. * 3) It needs to change some values in IP only. * 4) It needs to change some values in IP and upper layers e.g TCP. * * We need to accomodate icmp messages coming in clear until we get * everything secure from the wire. If icmp_accept_clear_messages * is zero we check with the global policy and act accordingly. If * it is non-zero, we accept the message without any checks. But * *this does not mean* that this will be delivered to the upper * clients. By accepting we might send replies back, change our MTU * value etc. but delivery to the ULP/clients depends on their policy * dispositions. * * We handle the above 4 cases in the context of IPsec in the * following way : * * 1) Send the reply back in the same way as the request came in. * If it came in encrypted, it goes out encrypted. If it came in * clear, it goes out in clear. Thus, this will prevent chosen * plain text attack. * 2) The client may or may not expect things to come in secure. * If it comes in secure, the policy constraints are checked * before delivering it to the upper layers. If it comes in * clear, ipsec_inbound_accept_clear will decide whether to * accept this in clear or not. In both the cases, if the returned * message (IP header + 8 bytes) that caused the icmp message has * AH/ESP headers, it is sent up to AH/ESP for validation before * sending up. If there are only 8 bytes of returned message, then * upper client will not be notified. * 3) Check with global policy to see whether it matches the constaints. * But this will be done only if icmp_accept_messages_in_clear is * zero. * 4) If we need to change both in IP and ULP, then the decision taken * while affecting the values in IP and while delivering up to TCP * should be the same. * * There are two cases. * * a) If we reject data at the IP layer (ipsec_check_global_policy() * failed), we will not deliver it to the ULP, even though they * are *willing* to accept in *clear*. This is fine as our global * disposition to icmp messages asks us reject the datagram. * * b) If we accept data at the IP layer (ipsec_check_global_policy() * succeeded or icmp_accept_messages_in_clear is 1), and not able * to deliver it to ULP (policy failed), it can lead to * consistency problems. The cases known at this time are * ICMP_DESTINATION_UNREACHABLE messages with following code * values : * * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value * and Upper layer rejects. Then the communication will * come to a stop. This is solved by making similar decisions * at both levels. Currently, when we are unable to deliver * to the Upper Layer (due to policy failures) while IP has * adjusted ire_max_frag, the next outbound datagram would * generate a local ICMP_FRAGMENTATION_NEEDED message - which * will be with the right level of protection. Thus the right * value will be communicated even if we are not able to * communicate when we get from the wire initially. But this * assumes there would be at least one outbound datagram after * IP has adjusted its ire_max_frag value. To make things * simpler, we accept in clear after the validation of * AH/ESP headers. * * - Other ICMP ERRORS : We may not be able to deliver it to the * upper layer depending on the level of protection the upper * layer expects and the disposition in ipsec_inbound_accept_clear(). * ipsec_inbound_accept_clear() decides whether a given ICMP error * should be accepted in clear when the Upper layer expects secure. * Thus the communication may get aborted by some bad ICMP * packets. * * IPQoS Notes: * The only instance when a packet is sent for processing is when there * isn't an ICMP client and if we are interested in it. * If there is a client, IPPF processing will take place in the * ip_fanout_proto routine. * * Zones notes: * The packet is only processed in the context of the specified zone: typically * only this zone will reply to an echo request, and only interested clients in * this zone will receive a copy of the packet. This means that the caller must * call icmp_inbound() for each relevant zone. */ static void icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill, int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) { icmph_t *icmph; ipha_t *ipha; int iph_hdr_length; int hdr_length; boolean_t interested; uint32_t ts; uchar_t *wptr; ipif_t *ipif; mblk_t *first_mp; ipsec_in_t *ii; ire_t *src_ire; boolean_t onlink; timestruc_t now; uint32_t ill_index; ip_stack_t *ipst; ASSERT(ill != NULL); ipst = ill->ill_ipst; first_mp = mp; if (mctl_present) { mp = first_mp->b_cont; ASSERT(mp != NULL); } ipha = (ipha_t *)mp->b_rptr; if (ipst->ips_icmp_accept_clear_messages == 0) { first_mp = ipsec_check_global_policy(first_mp, NULL, ipha, NULL, mctl_present, ipst->ips_netstack); if (first_mp == NULL) return; } /* * On a labeled system, we have to check whether the zone itself is * permitted to receive raw traffic. */ if (is_system_labeled()) { if (zoneid == ALL_ZONES) zoneid = tsol_packet_to_zoneid(mp); if (!tsol_can_accept_raw(mp, B_FALSE)) { ip1dbg(("icmp_inbound: zone %d can't receive raw", zoneid)); BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); freemsg(first_mp); return; } } /* * We have accepted the ICMP message. It means that we will * respond to the packet if needed. It may not be delivered * to the upper client depending on the policy constraints * and the disposition in ipsec_inbound_accept_clear. */ ASSERT(ill != NULL); BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs); iph_hdr_length = IPH_HDR_LENGTH(ipha); if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) { /* Last chance to get real. */ if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) { BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); freemsg(first_mp); return; } /* Refresh iph following the pullup. */ ipha = (ipha_t *)mp->b_rptr; } /* ICMP header checksum, including checksum field, should be zero. */ if (sum_valid ? (sum != 0 && sum != 0xFFFF) : IP_CSUM(mp, iph_hdr_length, 0)) { BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs); freemsg(first_mp); return; } /* The IP header will always be a multiple of four bytes */ icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type, icmph->icmph_code)); wptr = (uchar_t *)icmph + ICMPH_SIZE; /* We will set "interested" to "true" if we want a copy */ interested = B_FALSE; switch (icmph->icmph_type) { case ICMP_ECHO_REPLY: BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps); break; case ICMP_DEST_UNREACHABLE: if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded); interested = B_TRUE; /* Pass up to transport */ BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs); break; case ICMP_SOURCE_QUENCH: interested = B_TRUE; /* Pass up to transport */ BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs); break; case ICMP_REDIRECT: if (!ipst->ips_ip_ignore_redirect) interested = B_TRUE; BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects); break; case ICMP_ECHO_REQUEST: /* * Whether to respond to echo requests that come in as IP * broadcasts or as IP multicast is subject to debate * (what isn't?). We aim to please, you pick it. * Default is do it. */ if (!broadcast && !CLASSD(ipha->ipha_dst)) { /* unicast: always respond */ interested = B_TRUE; } else if (CLASSD(ipha->ipha_dst)) { /* multicast: respond based on tunable */ interested = ipst->ips_ip_g_resp_to_echo_mcast; } else if (broadcast) { /* broadcast: respond based on tunable */ interested = ipst->ips_ip_g_resp_to_echo_bcast; } BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos); break; case ICMP_ROUTER_ADVERTISEMENT: case ICMP_ROUTER_SOLICITATION: break; case ICMP_TIME_EXCEEDED: interested = B_TRUE; /* Pass up to transport */ BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds); break; case ICMP_PARAM_PROBLEM: interested = B_TRUE; /* Pass up to transport */ BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs); break; case ICMP_TIME_STAMP_REQUEST: /* Response to Time Stamp Requests is local policy. */ if (ipst->ips_ip_g_resp_to_timestamp && /* So is whether to respond if it was an IP broadcast. */ (!broadcast || ipst->ips_ip_g_resp_to_timestamp_bcast)) { int tstamp_len = 3 * sizeof (uint32_t); if (wptr + tstamp_len > mp->b_wptr) { if (!pullupmsg(mp, wptr + tstamp_len - mp->b_rptr)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } /* Refresh ipha following the pullup. */ ipha = (ipha_t *)mp->b_rptr; icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; wptr = (uchar_t *)icmph + ICMPH_SIZE; } interested = B_TRUE; } BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps); break; case ICMP_TIME_STAMP_REPLY: BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps); break; case ICMP_INFO_REQUEST: /* Per RFC 1122 3.2.2.7, ignore this. */ case ICMP_INFO_REPLY: break; case ICMP_ADDRESS_MASK_REQUEST: if ((ipst->ips_ip_respond_to_address_mask_broadcast || !broadcast) && /* TODO m_pullup of complete header? */ (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) { interested = B_TRUE; } BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks); break; case ICMP_ADDRESS_MASK_REPLY: BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps); break; default: interested = B_TRUE; /* Pass up to transport */ BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns); break; } /* See if there is an ICMP client. */ if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) { /* If there is an ICMP client and we want one too, copy it. */ mblk_t *first_mp1; if (!interested) { ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present, ip_policy, recv_ill, zoneid); return; } first_mp1 = ip_copymsg(first_mp); if (first_mp1 != NULL) { ip_fanout_proto(q, first_mp1, ill, ipha, 0, mctl_present, ip_policy, recv_ill, zoneid); } } else if (!interested) { freemsg(first_mp); return; } else { /* * Initiate policy processing for this packet if ip_policy * is true. */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { ill_index = ill->ill_phyint->phyint_ifindex; ip_process(IPP_LOCAL_IN, &mp, ill_index); if (mp == NULL) { if (mctl_present) { freeb(first_mp); } BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); return; } } } /* We want to do something with it. */ /* Check db_ref to make sure we can modify the packet. */ if (mp->b_datap->db_ref > 1) { mblk_t *first_mp1; first_mp1 = ip_copymsg(first_mp); freemsg(first_mp); if (!first_mp1) { BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); return; } first_mp = first_mp1; if (mctl_present) { mp = first_mp->b_cont; ASSERT(mp != NULL); } else { mp = first_mp; } ipha = (ipha_t *)mp->b_rptr; icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; wptr = (uchar_t *)icmph + ICMPH_SIZE; } switch (icmph->icmph_type) { case ICMP_ADDRESS_MASK_REQUEST: ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); if (ipif == NULL) { freemsg(first_mp); return; } /* * outging interface must be IPv4 */ ASSERT(ipif != NULL && !ipif->ipif_isv6); icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN); ipif_refrele(ipif); BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps); break; case ICMP_ECHO_REQUEST: icmph->icmph_type = ICMP_ECHO_REPLY; BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps); break; case ICMP_TIME_STAMP_REQUEST: { uint32_t *tsp; icmph->icmph_type = ICMP_TIME_STAMP_REPLY; tsp = (uint32_t *)wptr; tsp++; /* Skip past 'originate time' */ /* Compute # of milliseconds since midnight */ gethrestime(&now); ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + now.tv_nsec / (NANOSEC / MILLISEC); *tsp++ = htonl(ts); /* Lay in 'receive time' */ *tsp++ = htonl(ts); /* Lay in 'send time' */ BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps); break; } default: ipha = (ipha_t *)&icmph[1]; if ((uchar_t *)&ipha[1] > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } hdr_length = IPH_HDR_LENGTH(ipha); if (hdr_length < sizeof (ipha_t)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length - mp->b_rptr)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } switch (icmph->icmph_type) { case ICMP_REDIRECT: /* * As there is no upper client to deliver, we don't * need the first_mp any more. */ if (mctl_present) { freeb(first_mp); } icmp_redirect(ill, mp); return; case ICMP_DEST_UNREACHABLE: if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { if (!icmp_inbound_too_big(icmph, ipha, ill, zoneid, mp, iph_hdr_length, ipst)) { freemsg(first_mp); return; } /* * icmp_inbound_too_big() may alter mp. * Resynch ipha and icmph accordingly. */ icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } /* FALLTHRU */ default : /* * IPQoS notes: Since we have already done IPQoS * processing we don't want to do it again in * the fanout routines called by * icmp_inbound_error_fanout, hence the last * argument, ip_policy, is B_FALSE. */ icmp_inbound_error_fanout(q, ill, first_mp, icmph, ipha, iph_hdr_length, hdr_length, mctl_present, B_FALSE, recv_ill, zoneid); } return; } /* Send out an ICMP packet */ icmph->icmph_checksum = 0; icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0); if (broadcast || CLASSD(ipha->ipha_dst)) { ipif_t *ipif_chosen; /* * Make it look like it was directed to us, so we don't look * like a fool with a broadcast or multicast source address. */ ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid); /* * Make sure that we haven't grabbed an interface that's DOWN. */ if (ipif != NULL) { ipif_chosen = ipif_select_source(ipif->ipif_ill, ipha->ipha_src, zoneid); if (ipif_chosen != NULL) { ipif_refrele(ipif); ipif = ipif_chosen; } } if (ipif == NULL) { ip0dbg(("icmp_inbound: " "No source for broadcast/multicast:\n" "\tsrc 0x%x dst 0x%x ill %p " "ipif_lcl_addr 0x%x\n", ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), (void *)ill, ill->ill_ipif->ipif_lcl_addr)); freemsg(first_mp); return; } ASSERT(ipif != NULL && !ipif->ipif_isv6); ipha->ipha_dst = ipif->ipif_src_addr; ipif_refrele(ipif); } /* Reset time to live. */ ipha->ipha_ttl = ipst->ips_ip_def_ttl; { /* Swap source and destination addresses */ ipaddr_t tmp; tmp = ipha->ipha_src; ipha->ipha_src = ipha->ipha_dst; ipha->ipha_dst = tmp; } ipha->ipha_ident = 0; if (!IS_SIMPLE_IPH(ipha)) icmp_options_update(ipha); /* * ICMP echo replies should go out on the same interface * the request came on as probes used by in.mpathd for detecting * NIC failures are ECHO packets. We turn-off load spreading * by setting ipsec_in_attach_if to B_TRUE, which is copied * to ipsec_out_attach_if by ipsec_in_to_out called later in this * function. This is in turn handled by ip_wput and ip_newroute * to make sure that the packet goes out on the interface it came * in on. If we don't turnoff load spreading, the packets might get * dropped if there are no non-FAILED/INACTIVE interfaces for it * to go out and in.mpathd would wrongly detect a failure or * mis-detect a NIC failure for link failure. As load spreading * can happen only if ill_group is not NULL, we do only for * that case and this does not affect the normal case. * * We turn off load spreading only on echo packets that came from * on-link hosts. If the interface route has been deleted, this will * not be enforced as we can't do much. For off-link hosts, as the * default routes in IPv4 does not typically have an ire_ipif * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute. * Moreover, expecting a default route through this interface may * not be correct. We use ipha_dst because of the swap above. */ onlink = B_FALSE; if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) { /* * First, we need to make sure that it is not one of our * local addresses. If we set onlink when it is one of * our local addresses, we will end up creating IRE_CACHES * for one of our local addresses. Then, we will never * accept packets for them afterwards. */ src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (src_ire == NULL) { ipif = ipif_get_next_ipif(NULL, ill); if (ipif == NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(mp); return; } src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0, IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE, ipst); ipif_refrele(ipif); if (src_ire != NULL) { onlink = B_TRUE; ire_refrele(src_ire); } } else { ire_refrele(src_ire); } } if (!mctl_present) { /* * This packet should go out the same way as it * came in i.e in clear. To make sure that global * policy will not be applied to this in ip_wput_ire, * we attach a IPSEC_IN mp and clear ipsec_in_secure. */ ASSERT(first_mp == mp); first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); if (first_mp == NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(mp); return; } ii = (ipsec_in_t *)first_mp->b_rptr; /* This is not a secure packet */ ii->ipsec_in_secure = B_FALSE; if (onlink) { ii->ipsec_in_attach_if = B_TRUE; ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; } first_mp->b_cont = mp; } else if (onlink) { ii = (ipsec_in_t *)first_mp->b_rptr; ii->ipsec_in_attach_if = B_TRUE; ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ } else { ii = (ipsec_in_t *)first_mp->b_rptr; ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */ } ii->ipsec_in_zoneid = zoneid; ASSERT(zoneid != ALL_ZONES); if (!ipsec_in_to_out(first_mp, ipha, NULL)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return; } BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); put(WR(q), first_mp); } static ipaddr_t icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp) { conn_t *connp; connf_t *connfp; ipaddr_t nexthop_addr = INADDR_ANY; int hdr_length = IPH_HDR_LENGTH(ipha); uint16_t *up; uint32_t ports; ip_stack_t *ipst = ill->ill_ipst; up = (uint16_t *)((uchar_t *)ipha + hdr_length); switch (ipha->ipha_protocol) { case IPPROTO_TCP: { tcph_t *tcph; /* do a reverse lookup */ tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN, ipst); break; } case IPPROTO_UDP: { uint32_t dstport, srcport; ((uint16_t *)&ports)[0] = up[1]; ((uint16_t *)&ports)[1] = up[0]; /* Extract ports in net byte order */ dstport = htons(ntohl(ports) & 0xFFFF); srcport = htons(ntohl(ports) >> 16); connfp = &ipst->ips_ipcl_udp_fanout[ IPCL_UDP_HASH(dstport, ipst)]; mutex_enter(&connfp->connf_lock); connp = connfp->connf_head; /* do a reverse lookup */ while ((connp != NULL) && (!IPCL_UDP_MATCH(connp, dstport, ipha->ipha_src, srcport, ipha->ipha_dst) || !IPCL_ZONE_MATCH(connp, zoneid))) { connp = connp->conn_next; } if (connp != NULL) CONN_INC_REF(connp); mutex_exit(&connfp->connf_lock); break; } case IPPROTO_SCTP: { in6_addr_t map_src, map_dst; IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src); IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst); ((uint16_t *)&ports)[0] = up[1]; ((uint16_t *)&ports)[1] = up[0]; connp = sctp_find_conn(&map_src, &map_dst, ports, zoneid, ipst->ips_netstack->netstack_sctp); if (connp == NULL) { connp = ipcl_classify_raw(mp, IPPROTO_SCTP, zoneid, ports, ipha, ipst); } else { CONN_INC_REF(connp); SCTP_REFRELE(CONN2SCTP(connp)); } break; } default: { ipha_t ripha; ripha.ipha_src = ipha->ipha_dst; ripha.ipha_dst = ipha->ipha_src; ripha.ipha_protocol = ipha->ipha_protocol; connfp = &ipst->ips_ipcl_proto_fanout[ ipha->ipha_protocol]; mutex_enter(&connfp->connf_lock); connp = connfp->connf_head; for (connp = connfp->connf_head; connp != NULL; connp = connp->conn_next) { if (IPCL_PROTO_MATCH(connp, ipha->ipha_protocol, &ripha, ill, 0, zoneid)) { CONN_INC_REF(connp); break; } } mutex_exit(&connfp->connf_lock); } } if (connp != NULL) { if (connp->conn_nexthop_set) nexthop_addr = connp->conn_nexthop_v4; CONN_DEC_REF(connp); } return (nexthop_addr); } /* Table from RFC 1191 */ static int icmp_frag_size_table[] = { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 }; /* * Process received ICMP Packet too big. * After updating any IRE it does the fanout to any matching transport streams. * Assumes the message has been pulled up till the IP header that caused * the error. * * Returns B_FALSE on failure and B_TRUE on success. */ static boolean_t icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp, int iph_hdr_length, ip_stack_t *ipst) { ire_t *ire, *first_ire; int mtu; int hdr_length; ipaddr_t nexthop_addr; ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE && icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED); ASSERT(ill != NULL); hdr_length = IPH_HDR_LENGTH(ipha); /* Drop if the original packet contained a source route */ if (ip_source_route_included(ipha)) { return (B_FALSE); } /* * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport * header. */ if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); ip1dbg(("icmp_inbound_too_big: insufficient hdr\n")); return (B_FALSE); } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp); if (nexthop_addr != INADDR_ANY) { /* nexthop set */ first_ire = ire_ctable_lookup(ipha->ipha_dst, nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp), MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, ipst); } else { /* nexthop not set */ first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); } if (!first_ire) { ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n", ntohl(ipha->ipha_dst))); return (B_FALSE); } /* Check for MTU discovery advice as described in RFC 1191 */ mtu = ntohs(icmph->icmph_du_mtu); rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst; ire = ire->ire_next) { /* * Look for the connection to which this ICMP message is * directed. If it has the IP_NEXTHOP option set, then the * search is limited to IREs with the MATCH_IRE_PRIVATE * option. Else the search is limited to regular IREs. */ if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && (nexthop_addr != ire->ire_gateway_addr)) || (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) && (nexthop_addr != INADDR_ANY))) continue; mutex_enter(&ire->ire_lock); if (icmph->icmph_du_zero == 0 && mtu > 68) { /* Reduce the IRE max frag value as advised. */ ip1dbg(("Received mtu from router: %d (was %d)\n", mtu, ire->ire_max_frag)); ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); } else { uint32_t length; int i; /* * Use the table from RFC 1191 to figure out * the next "plateau" based on the length in * the original IP packet. */ length = ntohs(ipha->ipha_length); if (ire->ire_max_frag <= length && ire->ire_max_frag >= length - hdr_length) { /* * Handle broken BSD 4.2 systems that * return the wrong iph_length in ICMP * errors. */ ip1dbg(("Wrong mtu: sent %d, ire %d\n", length, ire->ire_max_frag)); length -= hdr_length; } for (i = 0; i < A_CNT(icmp_frag_size_table); i++) { if (length > icmp_frag_size_table[i]) break; } if (i == A_CNT(icmp_frag_size_table)) { /* Smaller than 68! */ ip1dbg(("Too big for packet size %d\n", length)); ire->ire_max_frag = MIN(ire->ire_max_frag, 576); ire->ire_frag_flag = 0; } else { mtu = icmp_frag_size_table[i]; ip1dbg(("Calculated mtu %d, packet size %d, " "before %d", mtu, length, ire->ire_max_frag)); ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); ip1dbg((", after %d\n", ire->ire_max_frag)); } /* Record the new max frag size for the ULP. */ icmph->icmph_du_zero = 0; icmph->icmph_du_mtu = htons((uint16_t)ire->ire_max_frag); } mutex_exit(&ire->ire_lock); } rw_exit(&first_ire->ire_bucket->irb_lock); ire_refrele(first_ire); return (B_TRUE); } /* * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout * calls this function. */ static mblk_t * icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length) { ipha_t *ipha; icmph_t *icmph; ipha_t *in_ipha; int length; ASSERT(mp->b_datap->db_type == M_DATA); /* * For Self-encapsulated packets, we added an extra IP header * without the options. Inner IP header is the one from which * the outer IP header was formed. Thus, we need to remove the * outer IP header. To do this, we pullup the whole message * and overlay whatever follows the outer IP header over the * outer IP header. */ if (!pullupmsg(mp, -1)) return (NULL); icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); /* * The length that we want to overlay is following the inner * IP header. Subtracting the IP header + icmp header + outer * IP header's length should give us the length that we want to * overlay. */ length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) - hdr_length; /* * Overlay whatever follows the inner header over the * outer header. */ bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length); /* Set the wptr to account for the outer header */ mp->b_wptr -= hdr_length; return (mp); } /* * Try to pass the ICMP message upstream in case the ULP cares. * * If the packet that caused the ICMP error is secure, we send * it to AH/ESP to make sure that the attached packet has a * valid association. ipha in the code below points to the * IP header of the packet that caused the error. * * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently * in the context of IPsec. Normally we tell the upper layer * whenever we send the ire (including ip_bind), the IPsec header * length in ire_ipsec_overhead. TCP can deduce the MSS as it * has both the MTU (ire_max_frag) and the ire_ipsec_overhead. * Similarly, we pass the new MTU icmph_du_mtu and TCP does the * same thing. As TCP has the IPsec options size that needs to be * adjusted, we just pass the MTU unchanged. * * IFN could have been generated locally or by some router. * * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this. * This happens because IP adjusted its value of MTU on an * earlier IFN message and could not tell the upper layer, * the new adjusted value of MTU e.g. Packet was encrypted * or there was not enough information to fanout to upper * layers. Thus on the next outbound datagram, ip_wput_ire * generates the IFN, where IPsec processing has *not* been * done. * * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed * could have generated this. This happens because ire_max_frag * value in IP was set to a new value, while the IPsec processing * was being done and after we made the fragmentation check in * ip_wput_ire. Thus on return from IPsec processing, * ip_wput_ipsec_out finds that the new length is > ire_max_frag * and generates the IFN. As IPsec processing is over, we fanout * to AH/ESP to remove the header. * * In both these cases, ipsec_in_loopback will be set indicating * that IFN was generated locally. * * ROUTER : IFN could be secure or non-secure. * * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the * packet in error has AH/ESP headers to validate the AH/ESP * headers. AH/ESP will verify whether there is a valid SA or * not and send it back. We will fanout again if we have more * data in the packet. * * If the packet in error does not have AH/ESP, we handle it * like any other case. * * * NON_SECURE : If the packet in error has AH/ESP headers, * we attach a dummy ipsec_in and send it up to AH/ESP * for validation. AH/ESP will verify whether there is a * valid SA or not and send it back. We will fanout again if * we have more data in the packet. * * If the packet in error does not have AH/ESP, we handle it * like any other case. */ static void icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp, icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length, boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) { uint16_t *up; /* Pointer to ports in ULP header */ uint32_t ports; /* reversed ports for fanout */ ipha_t ripha; /* With reversed addresses */ mblk_t *first_mp; ipsec_in_t *ii; tcph_t *tcph; conn_t *connp; ip_stack_t *ipst; ASSERT(ill != NULL); ASSERT(recv_ill != NULL); ipst = recv_ill->ill_ipst; first_mp = mp; if (mctl_present) { mp = first_mp->b_cont; ASSERT(mp != NULL); ii = (ipsec_in_t *)first_mp->b_rptr; ASSERT(ii->ipsec_in_type == IPSEC_IN); } else { ii = NULL; } switch (ipha->ipha_protocol) { case IPPROTO_UDP: /* * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of * transport header. */ if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { goto discard_pkt; } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } up = (uint16_t *)((uchar_t *)ipha + hdr_length); /* * Attempt to find a client stream based on port. * Note that we do a reverse lookup since the header is * in the form we sent it out. * The ripha header is only used for the IP_UDP_MATCH and we * only set the src and dst addresses and protocol. */ ripha.ipha_src = ipha->ipha_dst; ripha.ipha_dst = ipha->ipha_src; ripha.ipha_protocol = ipha->ipha_protocol; ((uint16_t *)&ports)[0] = up[1]; ((uint16_t *)&ports)[1] = up[0]; ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n", ntohl(ipha->ipha_src), ntohs(up[0]), ntohl(ipha->ipha_dst), ntohs(up[1]), icmph->icmph_type, icmph->icmph_code)); /* Have to change db_type after any pullupmsg */ DB_TYPE(mp) = M_CTL; ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0, mctl_present, ip_policy, recv_ill, zoneid); return; case IPPROTO_TCP: /* * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of * transport header. */ if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { goto discard_pkt; } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } /* * Find a TCP client stream for this packet. * Note that we do a reverse lookup since the header is * in the form we sent it out. */ tcph = (tcph_t *)((uchar_t *)ipha + hdr_length); connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN, ipst); if (connp == NULL) goto discard_pkt; /* Have to change db_type after any pullupmsg */ DB_TYPE(mp) = M_CTL; squeue_fill(connp->conn_sqp, first_mp, tcp_input, connp, SQTAG_TCP_INPUT_ICMP_ERR); return; case IPPROTO_SCTP: /* * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of * transport header. */ if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) { goto discard_pkt; } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } up = (uint16_t *)((uchar_t *)ipha + hdr_length); /* * Find a SCTP client stream for this packet. * Note that we do a reverse lookup since the header is * in the form we sent it out. * The ripha header is only used for the matching and we * only set the src and dst addresses, protocol, and version. */ ripha.ipha_src = ipha->ipha_dst; ripha.ipha_dst = ipha->ipha_src; ripha.ipha_protocol = ipha->ipha_protocol; ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length; ((uint16_t *)&ports)[0] = up[1]; ((uint16_t *)&ports)[1] = up[0]; /* Have to change db_type after any pullupmsg */ DB_TYPE(mp) = M_CTL; ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0, mctl_present, ip_policy, zoneid); return; case IPPROTO_ESP: case IPPROTO_AH: { int ipsec_rc; ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; /* * We need a IPSEC_IN in the front to fanout to AH/ESP. * We will re-use the IPSEC_IN if it is already present as * AH/ESP will not affect any fields in the IPSEC_IN for * ICMP errors. If there is no IPSEC_IN, allocate a new * one and attach it in the front. */ if (ii != NULL) { /* * ip_fanout_proto_again converts the ICMP errors * that come back from AH/ESP to M_DATA so that * if it is non-AH/ESP and we do a pullupmsg in * this function, it would work. Convert it back * to M_CTL before we send up as this is a ICMP * error. This could have been generated locally or * by some router. Validate the inner IPsec * headers. * * NOTE : ill_index is used by ip_fanout_proto_again * to locate the ill. */ ASSERT(ill != NULL); ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; DB_TYPE(first_mp->b_cont) = M_CTL; } else { /* * IPSEC_IN is not present. We attach a ipsec_in * message and send up to IPsec for validating * and removing the IPsec headers. Clear * ipsec_in_secure so that when we return * from IPsec, we don't mistakenly think that this * is a secure packet came from the network. * * NOTE : ill_index is used by ip_fanout_proto_again * to locate the ill. */ ASSERT(first_mp == mp); first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); if (first_mp == NULL) { freemsg(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return; } ii = (ipsec_in_t *)first_mp->b_rptr; /* This is not a secure packet */ ii->ipsec_in_secure = B_FALSE; first_mp->b_cont = mp; DB_TYPE(mp) = M_CTL; ASSERT(ill != NULL); ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; } ip2dbg(("icmp_inbound_error: ipsec\n")); if (!ipsec_loaded(ipss)) { ip_proto_not_sup(q, first_mp, 0, zoneid, ipst); return; } if (ipha->ipha_protocol == IPPROTO_ESP) ipsec_rc = ipsecesp_icmp_error(first_mp); else ipsec_rc = ipsecah_icmp_error(first_mp); if (ipsec_rc == IPSEC_STATUS_FAILED) return; ip_fanout_proto_again(first_mp, ill, recv_ill, NULL); return; } default: /* * The ripha header is only used for the lookup and we * only set the src and dst addresses and protocol. */ ripha.ipha_src = ipha->ipha_dst; ripha.ipha_dst = ipha->ipha_src; ripha.ipha_protocol = ipha->ipha_protocol; ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n", ripha.ipha_protocol, ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst), icmph->icmph_type, icmph->icmph_code)); if (ipha->ipha_protocol == IPPROTO_ENCAP) { ipha_t *in_ipha; if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + sizeof (ipha_t) - mp->b_rptr)) { goto discard_pkt; } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; } /* * Caller has verified that length has to be * at least the size of IP header. */ ASSERT(hdr_length >= sizeof (ipha_t)); /* * Check the sanity of the inner IP header like * we did for the outer header. */ in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) { goto discard_pkt; } if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) { goto discard_pkt; } /* Check for Self-encapsulated tunnels */ if (in_ipha->ipha_src == ipha->ipha_src && in_ipha->ipha_dst == ipha->ipha_dst) { mp = icmp_inbound_self_encap_error(mp, iph_hdr_length, hdr_length); if (mp == NULL) goto discard_pkt; icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; hdr_length = IPH_HDR_LENGTH(ipha); /* * The packet in error is self-encapsualted. * And we are finding it further encapsulated * which we could not have possibly generated. */ if (ipha->ipha_protocol == IPPROTO_ENCAP) { goto discard_pkt; } icmp_inbound_error_fanout(q, ill, first_mp, icmph, ipha, iph_hdr_length, hdr_length, mctl_present, ip_policy, recv_ill, zoneid); return; } } if ((ipha->ipha_protocol == IPPROTO_ENCAP || ipha->ipha_protocol == IPPROTO_IPV6) && icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED && ii != NULL && ii->ipsec_in_loopback && ii->ipsec_in_secure) { /* * For IP tunnels that get a looped-back * ICMP_FRAGMENTATION_NEEDED message, adjust the * reported new MTU to take into account the IPsec * headers protecting this configured tunnel. * * This allows the tunnel module (tun.c) to blindly * accept the MTU reported in an ICMP "too big" * message. * * Non-looped back ICMP messages will just be * handled by the security protocols (if needed), * and the first subsequent packet will hit this * path. */ icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) - ipsec_in_extra_length(first_mp)); } /* Have to change db_type after any pullupmsg */ DB_TYPE(mp) = M_CTL; ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present, ip_policy, recv_ill, zoneid); return; } /* NOTREACHED */ discard_pkt: BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); drop_pkt:; ip1dbg(("icmp_inbound_error_fanout: drop pkt\n")); freemsg(first_mp); } /* * Common IP options parser. * * Setup routine: fill in *optp with options-parsing state, then * tail-call ipoptp_next to return the first option. */ uint8_t ipoptp_first(ipoptp_t *optp, ipha_t *ipha) { uint32_t totallen; /* total length of all options */ totallen = ipha->ipha_version_and_hdr_length - (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); totallen <<= 2; optp->ipoptp_next = (uint8_t *)(&ipha[1]); optp->ipoptp_end = optp->ipoptp_next + totallen; optp->ipoptp_flags = 0; return (ipoptp_next(optp)); } /* * Common IP options parser: extract next option. */ uint8_t ipoptp_next(ipoptp_t *optp) { uint8_t *end = optp->ipoptp_end; uint8_t *cur = optp->ipoptp_next; uint8_t opt, len, pointer; /* * If cur > end already, then the ipoptp_end or ipoptp_next pointer * has been corrupted. */ ASSERT(cur <= end); if (cur == end) return (IPOPT_EOL); opt = cur[IPOPT_OPTVAL]; /* * Skip any NOP options. */ while (opt == IPOPT_NOP) { cur++; if (cur == end) return (IPOPT_EOL); opt = cur[IPOPT_OPTVAL]; } if (opt == IPOPT_EOL) return (IPOPT_EOL); /* * Option requiring a length. */ if ((cur + 1) >= end) { optp->ipoptp_flags |= IPOPTP_ERROR; return (IPOPT_EOL); } len = cur[IPOPT_OLEN]; if (len < 2) { optp->ipoptp_flags |= IPOPTP_ERROR; return (IPOPT_EOL); } optp->ipoptp_cur = cur; optp->ipoptp_len = len; optp->ipoptp_next = cur + len; if (cur + len > end) { optp->ipoptp_flags |= IPOPTP_ERROR; return (IPOPT_EOL); } /* * For the options which require a pointer field, make sure * its there, and make sure it points to either something * inside this option, or the end of the option. */ switch (opt) { case IPOPT_RR: case IPOPT_TS: case IPOPT_LSRR: case IPOPT_SSRR: if (len <= IPOPT_OFFSET) { optp->ipoptp_flags |= IPOPTP_ERROR; return (opt); } pointer = cur[IPOPT_OFFSET]; if (pointer - 1 > len) { optp->ipoptp_flags |= IPOPTP_ERROR; return (opt); } break; } /* * Sanity check the pointer field based on the type of the * option. */ switch (opt) { case IPOPT_RR: case IPOPT_SSRR: case IPOPT_LSRR: if (pointer < IPOPT_MINOFF_SR) optp->ipoptp_flags |= IPOPTP_ERROR; break; case IPOPT_TS: if (pointer < IPOPT_MINOFF_IT) optp->ipoptp_flags |= IPOPTP_ERROR; /* * Note that the Internet Timestamp option also * contains two four bit fields (the Overflow field, * and the Flag field), which follow the pointer * field. We don't need to check that these fields * fall within the length of the option because this * was implicitely done above. We've checked that the * pointer value is at least IPOPT_MINOFF_IT, and that * it falls within the option. Since IPOPT_MINOFF_IT > * IPOPT_POS_OV_FLG, we don't need the explicit check. */ ASSERT(len > IPOPT_POS_OV_FLG); break; } return (opt); } /* * Use the outgoing IP header to create an IP_OPTIONS option the way * it was passed down from the application. */ int ip_opt_get_user(const ipha_t *ipha, uchar_t *buf) { ipoptp_t opts; const uchar_t *opt; uint8_t optval; uint8_t optlen; uint32_t len = 0; uchar_t *buf1 = buf; buf += IP_ADDR_LEN; /* Leave room for final destination */ len += IP_ADDR_LEN; bzero(buf1, IP_ADDR_LEN); /* * OK to cast away const here, as we don't store through the returned * opts.ipoptp_cur pointer. */ for (optval = ipoptp_first(&opts, (ipha_t *)ipha); optval != IPOPT_EOL; optval = ipoptp_next(&opts)) { int off; opt = opts.ipoptp_cur; optlen = opts.ipoptp_len; switch (optval) { case IPOPT_SSRR: case IPOPT_LSRR: /* * Insert ipha_dst as the first entry in the source * route and move down the entries on step. * The last entry gets placed at buf1. */ buf[IPOPT_OPTVAL] = optval; buf[IPOPT_OLEN] = optlen; buf[IPOPT_OFFSET] = optlen; off = optlen - IP_ADDR_LEN; if (off < 0) { /* No entries in source route */ break; } /* Last entry in source route */ bcopy(opt + off, buf1, IP_ADDR_LEN); off -= IP_ADDR_LEN; while (off > 0) { bcopy(opt + off, buf + off + IP_ADDR_LEN, IP_ADDR_LEN); off -= IP_ADDR_LEN; } /* ipha_dst into first slot */ bcopy(&ipha->ipha_dst, buf + off + IP_ADDR_LEN, IP_ADDR_LEN); buf += optlen; len += optlen; break; case IPOPT_COMSEC: case IPOPT_SECURITY: /* if passing up a label is not ok, then remove */ if (is_system_labeled()) break; /* FALLTHROUGH */ default: bcopy(opt, buf, optlen); buf += optlen; len += optlen; break; } } done: /* Pad the resulting options */ while (len & 0x3) { *buf++ = IPOPT_EOL; len++; } return (len); } /* * Update any record route or timestamp options to include this host. * Reverse any source route option. * This routine assumes that the options are well formed i.e. that they * have already been checked. */ static void icmp_options_update(ipha_t *ipha) { ipoptp_t opts; uchar_t *opt; uint8_t optval; ipaddr_t src; /* Our local address */ ipaddr_t dst; ip2dbg(("icmp_options_update\n")); src = ipha->ipha_src; dst = ipha->ipha_dst; for (optval = ipoptp_first(&opts, ipha); optval != IPOPT_EOL; optval = ipoptp_next(&opts)) { ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); opt = opts.ipoptp_cur; ip2dbg(("icmp_options_update: opt %d, len %d\n", optval, opts.ipoptp_len)); switch (optval) { int off1, off2; case IPOPT_SSRR: case IPOPT_LSRR: /* * Reverse the source route. The first entry * should be the next to last one in the current * source route (the last entry is our address). * The last entry should be the final destination. */ off1 = IPOPT_MINOFF_SR - 1; off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; if (off2 < 0) { /* No entries in source route */ ip1dbg(( "icmp_options_update: bad src route\n")); break; } bcopy((char *)opt + off2, &dst, IP_ADDR_LEN); bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN); bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN); off2 -= IP_ADDR_LEN; while (off1 < off2) { bcopy((char *)opt + off1, &src, IP_ADDR_LEN); bcopy((char *)opt + off2, (char *)opt + off1, IP_ADDR_LEN); bcopy(&src, (char *)opt + off2, IP_ADDR_LEN); off1 += IP_ADDR_LEN; off2 -= IP_ADDR_LEN; } opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR; break; } } } /* * Process received ICMP Redirect messages. */ static void icmp_redirect(ill_t *ill, mblk_t *mp) { ipha_t *ipha; int iph_hdr_length; icmph_t *icmph; ipha_t *ipha_err; ire_t *ire; ire_t *prev_ire; ire_t *save_ire; ipaddr_t src, dst, gateway; iulp_t ulp_info = { 0 }; int error; ip_stack_t *ipst; ASSERT(ill != NULL); ipst = ill->ill_ipst; ipha = (ipha_t *)mp->b_rptr; iph_hdr_length = IPH_HDR_LENGTH(ipha); if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) < sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) { BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); freemsg(mp); return; } icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha_err = (ipha_t *)&icmph[1]; src = ipha->ipha_src; dst = ipha_err->ipha_dst; gateway = icmph->icmph_rd_gateway; /* Make sure the new gateway is reachable somehow. */ ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); /* * Make sure we had a route for the dest in question and that * that route was pointing to the old gateway (the source of the * redirect packet.) */ prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES, NULL, MATCH_IRE_GW, ipst); /* * Check that * the redirect was not from ourselves * the new gateway and the old gateway are directly reachable */ if (!prev_ire || !ire || ire->ire_type == IRE_LOCAL) { BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); freemsg(mp); if (ire != NULL) ire_refrele(ire); if (prev_ire != NULL) ire_refrele(prev_ire); return; } /* * Should we use the old ULP info to create the new gateway? From * a user's perspective, we should inherit the info so that it * is a "smooth" transition. If we do not do that, then new * connections going thru the new gateway will have no route metrics, * which is counter-intuitive to user. From a network point of * view, this may or may not make sense even though the new gateway * is still directly connected to us so the route metrics should not * change much. * * But if the old ire_uinfo is not initialized, we do another * recursive lookup on the dest using the new gateway. There may * be a route to that. If so, use it to initialize the redirect * route. */ if (prev_ire->ire_uinfo.iulp_set) { bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); } else { ire_t *tmp_ire; ire_t *sire; tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire, ALL_ZONES, 0, NULL, (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT), ipst); if (sire != NULL) { bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); /* * If sire != NULL, ire_ftable_lookup() should not * return a NULL value. */ ASSERT(tmp_ire != NULL); ire_refrele(tmp_ire); ire_refrele(sire); } else if (tmp_ire != NULL) { bcopy(&tmp_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); ire_refrele(tmp_ire); } } if (prev_ire->ire_type == IRE_CACHE) ire_delete(prev_ire); ire_refrele(prev_ire); /* * TODO: more precise handling for cases 0, 2, 3, the latter two * require TOS routing */ switch (icmph->icmph_code) { case 0: case 1: /* TODO: TOS specificity for cases 2 and 3 */ case 2: case 3: break; default: freemsg(mp); BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects); ire_refrele(ire); return; } /* * Create a Route Association. This will allow us to remember that * someone we believe told us to use the particular gateway. */ save_ire = ire; ire = ire_create( (uchar_t *)&dst, /* dest addr */ (uchar_t *)&ip_g_all_ones, /* mask */ (uchar_t *)&save_ire->ire_src_addr, /* source addr */ (uchar_t *)&gateway, /* gateway addr */ &save_ire->ire_max_frag, /* max frag */ NULL, /* no src nce */ NULL, /* no rfq */ NULL, /* no stq */ IRE_HOST, NULL, /* ipif */ 0, /* cmask */ 0, /* phandle */ 0, /* ihandle */ (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), &ulp_info, NULL, /* tsol_gc_t */ NULL, /* gcgrp */ ipst); if (ire == NULL) { freemsg(mp); ire_refrele(save_ire); return; } error = ire_add(&ire, NULL, NULL, NULL, B_FALSE); ire_refrele(save_ire); atomic_inc_32(&ipst->ips_ip_redirect_cnt); if (error == 0) { ire_refrele(ire); /* Held in ire_add_v4 */ /* tell routing sockets that we received a redirect */ ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src, (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst); } /* * Delete any existing IRE_HOST type redirect ires for this destination. * This together with the added IRE has the effect of * modifying an existing redirect. */ prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst); if (prev_ire != NULL) { if (prev_ire ->ire_flags & RTF_DYNAMIC) ire_delete(prev_ire); ire_refrele(prev_ire); } freemsg(mp); } /* * Generate an ICMP parameter problem message. */ static void icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid, ip_stack_t *ipst) { icmph_t icmph; boolean_t mctl_present; mblk_t *first_mp; EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (!(mp = icmp_pkt_err_ok(mp, ipst))) { if (mctl_present) freeb(first_mp); return; } bzero(&icmph, sizeof (icmph_t)); icmph.icmph_type = ICMP_PARAM_PROBLEM; icmph.icmph_pp_ptr = ptr; BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs); icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, ipst); } /* * Build and ship an IPv4 ICMP message using the packet data in mp, and * the ICMP header pointed to by "stuff". (May be called as writer.) * Note: assumes that icmp_pkt_err_ok has been called to verify that * an icmp error packet can be sent. * Assigns an appropriate source address to the packet. If ipha_dst is * one of our addresses use it for source. Otherwise pick a source based * on a route lookup back to ipha_src. * Note that ipha_src must be set here since the * packet is likely to arrive on an ill queue in ip_wput() which will * not set a source address. */ static void icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len, boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) { ipaddr_t dst; icmph_t *icmph; ipha_t *ipha; uint_t len_needed; size_t msg_len; mblk_t *mp1; ipaddr_t src; ire_t *ire; mblk_t *ipsec_mp; ipsec_out_t *io = NULL; if (mctl_present) { /* * If it is : * * 1) a IPSEC_OUT, then this is caused by outbound * datagram originating on this host. IPsec processing * may or may not have been done. Refer to comments above * icmp_inbound_error_fanout for details. * * 2) a IPSEC_IN if we are generating a icmp_message * for an incoming datagram destined for us i.e called * from ip_fanout_send_icmp. */ ipsec_info_t *in; ipsec_mp = mp; mp = ipsec_mp->b_cont; in = (ipsec_info_t *)ipsec_mp->b_rptr; ipha = (ipha_t *)mp->b_rptr; ASSERT(in->ipsec_info_type == IPSEC_OUT || in->ipsec_info_type == IPSEC_IN); if (in->ipsec_info_type == IPSEC_IN) { /* * Convert the IPSEC_IN to IPSEC_OUT. */ if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); return; } io = (ipsec_out_t *)ipsec_mp->b_rptr; } else { ASSERT(in->ipsec_info_type == IPSEC_OUT); io = (ipsec_out_t *)in; /* * Clear out ipsec_out_proc_begin, so we do a fresh * ire lookup. */ io->ipsec_out_proc_begin = B_FALSE; } ASSERT(zoneid == io->ipsec_out_zoneid); ASSERT(zoneid != ALL_ZONES); } else { /* * This is in clear. The icmp message we are building * here should go out in clear. * * Pardon the convolution of it all, but it's easier to * allocate a "use cleartext" IPSEC_IN message and convert * it than it is to allocate a new one. */ ipsec_in_t *ii; ASSERT(DB_TYPE(mp) == M_DATA); ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack); if (ipsec_mp == NULL) { freemsg(mp); BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); return; } ii = (ipsec_in_t *)ipsec_mp->b_rptr; /* This is not a secure packet */ ii->ipsec_in_secure = B_FALSE; /* * For trusted extensions using a shared IP address we can * send using any zoneid. */ if (zoneid == ALL_ZONES) ii->ipsec_in_zoneid = GLOBAL_ZONEID; else ii->ipsec_in_zoneid = zoneid; ipsec_mp->b_cont = mp; ipha = (ipha_t *)mp->b_rptr; /* * Convert the IPSEC_IN to IPSEC_OUT. */ if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); return; } io = (ipsec_out_t *)ipsec_mp->b_rptr; } /* Remember our eventual destination */ dst = ipha->ipha_src; ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst); if (ire != NULL && (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) { src = ipha->ipha_dst; } else { if (ire != NULL) ire_refrele(ire); ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL, (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY), ipst); if (ire == NULL) { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); freemsg(ipsec_mp); return; } src = ire->ire_src_addr; } if (ire != NULL) ire_refrele(ire); /* * Check if we can send back more then 8 bytes in addition to * the IP header. We try to send 64 bytes of data and the internal * header in the special cases of ipv4 encapsulated ipv4 or ipv6. */ len_needed = IPH_HDR_LENGTH(ipha); if (ipha->ipha_protocol == IPPROTO_ENCAP || ipha->ipha_protocol == IPPROTO_IPV6) { if (!pullupmsg(mp, -1)) { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); freemsg(ipsec_mp); return; } ipha = (ipha_t *)mp->b_rptr; if (ipha->ipha_protocol == IPPROTO_ENCAP) { len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha + len_needed)); } else { ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed); ASSERT(ipha->ipha_protocol == IPPROTO_IPV6); len_needed += ip_hdr_length_v6(mp, ip6h); } } len_needed += ipst->ips_ip_icmp_return; msg_len = msgdsize(mp); if (msg_len > len_needed) { (void) adjmsg(mp, len_needed - msg_len); msg_len = len_needed; } mp1 = allocb_tmpl(sizeof (icmp_ipha) + len, mp); if (mp1 == NULL) { BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors); freemsg(ipsec_mp); return; } mp1->b_cont = mp; mp = mp1; ASSERT(ipsec_mp->b_datap->db_type == M_CTL && ipsec_mp->b_rptr == (uint8_t *)io && io->ipsec_out_type == IPSEC_OUT); ipsec_mp->b_cont = mp; /* * Set ipsec_out_icmp_loopback so we can let the ICMP messages this * node generates be accepted in peace by all on-host destinations. * If we do NOT assume that all on-host destinations trust * self-generated ICMP messages, then rework here, ip6.c, and spd.c. * (Look for ipsec_out_icmp_loopback). */ io->ipsec_out_icmp_loopback = B_TRUE; ipha = (ipha_t *)mp->b_rptr; mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len); *ipha = icmp_ipha; ipha->ipha_src = src; ipha->ipha_dst = dst; ipha->ipha_ttl = ipst->ips_ip_def_ttl; msg_len += sizeof (icmp_ipha) + len; if (msg_len > IP_MAXPACKET) { (void) adjmsg(mp, IP_MAXPACKET - msg_len); msg_len = IP_MAXPACKET; } ipha->ipha_length = htons((uint16_t)msg_len); icmph = (icmph_t *)&ipha[1]; bcopy(stuff, icmph, len); icmph->icmph_checksum = 0; icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0); BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs); put(q, ipsec_mp); } /* * Determine if an ICMP error packet can be sent given the rate limit. * The limit consists of an average frequency (icmp_pkt_err_interval measured * in milliseconds) and a burst size. Burst size number of packets can * be sent arbitrarely closely spaced. * The state is tracked using two variables to implement an approximate * token bucket filter: * icmp_pkt_err_last - lbolt value when the last burst started * icmp_pkt_err_sent - number of packets sent in current burst */ boolean_t icmp_err_rate_limit(ip_stack_t *ipst) { clock_t now = TICK_TO_MSEC(lbolt); uint_t refilled; /* Number of packets refilled in tbf since last */ /* Guard against changes by loading into local variable */ uint_t err_interval = ipst->ips_ip_icmp_err_interval; if (err_interval == 0) return (B_FALSE); if (ipst->ips_icmp_pkt_err_last > now) { /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */ ipst->ips_icmp_pkt_err_last = 0; ipst->ips_icmp_pkt_err_sent = 0; } /* * If we are in a burst update the token bucket filter. * Update the "last" time to be close to "now" but make sure * we don't loose precision. */ if (ipst->ips_icmp_pkt_err_sent != 0) { refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval; if (refilled > ipst->ips_icmp_pkt_err_sent) { ipst->ips_icmp_pkt_err_sent = 0; } else { ipst->ips_icmp_pkt_err_sent -= refilled; ipst->ips_icmp_pkt_err_last += refilled * err_interval; } } if (ipst->ips_icmp_pkt_err_sent == 0) { /* Start of new burst */ ipst->ips_icmp_pkt_err_last = now; } if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) { ipst->ips_icmp_pkt_err_sent++; ip1dbg(("icmp_err_rate_limit: %d sent in burst\n", ipst->ips_icmp_pkt_err_sent)); return (B_FALSE); } ip1dbg(("icmp_err_rate_limit: dropped\n")); return (B_TRUE); } /* * Check if it is ok to send an IPv4 ICMP error packet in * response to the IPv4 packet in mp. * Free the message and return null if no * ICMP error packet should be sent. */ static mblk_t * icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst) { icmph_t *icmph; ipha_t *ipha; uint_t len_needed; ire_t *src_ire; ire_t *dst_ire; if (!mp) return (NULL); ipha = (ipha_t *)mp->b_rptr; if (ip_csum_hdr(ipha)) { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); freemsg(mp); return (NULL); } src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (src_ire != NULL || dst_ire != NULL || CLASSD(ipha->ipha_dst) || CLASSD(ipha->ipha_src) || (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) { /* Note: only errors to the fragment with offset 0 */ BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); freemsg(mp); if (src_ire != NULL) ire_refrele(src_ire); if (dst_ire != NULL) ire_refrele(dst_ire); return (NULL); } if (ipha->ipha_protocol == IPPROTO_ICMP) { /* * Check the ICMP type. RFC 1122 sez: don't send ICMP * errors in response to any ICMP errors. */ len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE; if (mp->b_wptr - mp->b_rptr < len_needed) { if (!pullupmsg(mp, len_needed)) { BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors); freemsg(mp); return (NULL); } ipha = (ipha_t *)mp->b_rptr; } icmph = (icmph_t *) (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]); switch (icmph->icmph_type) { case ICMP_DEST_UNREACHABLE: case ICMP_SOURCE_QUENCH: case ICMP_TIME_EXCEEDED: case ICMP_PARAM_PROBLEM: case ICMP_REDIRECT: BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); freemsg(mp); return (NULL); default: break; } } /* * If this is a labeled system, then check to see if we're allowed to * send a response to this particular sender. If not, then just drop. */ if (is_system_labeled() && !tsol_can_reply_error(mp)) { ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n")); BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops); freemsg(mp); return (NULL); } if (icmp_err_rate_limit(ipst)) { /* * Only send ICMP error packets every so often. * This should be done on a per port/source basis, * but for now this will suffice. */ freemsg(mp); return (NULL); } return (mp); } /* * Generate an ICMP redirect message. */ static void icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst) { icmph_t icmph; /* * We are called from ip_rput where we could * not have attached an IPSEC_IN. */ ASSERT(mp->b_datap->db_type == M_DATA); if (!(mp = icmp_pkt_err_ok(mp, ipst))) { return; } bzero(&icmph, sizeof (icmph_t)); icmph.icmph_type = ICMP_REDIRECT; icmph.icmph_code = 1; icmph.icmph_rd_gateway = gateway; BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects); /* Redirects sent by router, and router is global zone */ icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst); } /* * Generate an ICMP time exceeded message. */ void icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, ip_stack_t *ipst) { icmph_t icmph; boolean_t mctl_present; mblk_t *first_mp; EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (!(mp = icmp_pkt_err_ok(mp, ipst))) { if (mctl_present) freeb(first_mp); return; } bzero(&icmph, sizeof (icmph_t)); icmph.icmph_type = ICMP_TIME_EXCEEDED; icmph.icmph_code = code; BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds); icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid, ipst); } /* * Generate an ICMP unreachable message. */ void icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid, ip_stack_t *ipst) { icmph_t icmph; mblk_t *first_mp; boolean_t mctl_present; EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (!(mp = icmp_pkt_err_ok(mp, ipst))) { if (mctl_present) freeb(first_mp); return; } bzero(&icmph, sizeof (icmph_t)); icmph.icmph_type = ICMP_DEST_UNREACHABLE; icmph.icmph_code = code; BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs); ip2dbg(("send icmp destination unreachable code %d\n", code)); icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present, zoneid, ipst); } /* * Attempt to start recovery of an IPv4 interface that's been shut down as a * duplicate. As long as someone else holds the address, the interface will * stay down. When that conflict goes away, the interface is brought back up. * This is done so that accidental shutdowns of addresses aren't made * permanent. Your server will recover from a failure. * * For DHCP, recovery is not done in the kernel. Instead, it's handled by a * user space process (dhcpagent). * * Recovery completes if ARP reports that the address is now ours (via * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation. * * This function is entered on a timer expiry; the ID is in ipif_recovery_id. */ static void ipif_dup_recovery(void *arg) { ipif_t *ipif = arg; ill_t *ill = ipif->ipif_ill; mblk_t *arp_add_mp; mblk_t *arp_del_mp; area_t *area; ip_stack_t *ipst = ill->ill_ipst; ipif->ipif_recovery_id = 0; /* * No lock needed for moving or condemned check, as this is just an * optimization. */ if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) || (ipif->ipif_flags & IPIF_POINTOPOINT) || (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) { /* No reason to try to bring this address back. */ return; } if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL) goto alloc_fail; if (ipif->ipif_arp_del_mp == NULL) { if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL) goto alloc_fail; ipif->ipif_arp_del_mp = arp_del_mp; } /* Setting the 'unverified' flag restarts DAD */ area = (area_t *)arp_add_mp->b_rptr; area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | ACE_F_UNVERIFIED; putnext(ill->ill_rq, arp_add_mp); return; alloc_fail: /* * On allocation failure, just restart the timer. Note that the ipif * is down here, so no other thread could be trying to start a recovery * timer. The ill_lock protects the condemned flag and the recovery * timer ID. */ freemsg(arp_add_mp); mutex_enter(&ill->ill_lock); if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 && !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); } mutex_exit(&ill->ill_lock); } /* * This is for exclusive changes due to ARP. Either tear down an interface due * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery. */ /* ARGSUSED */ static void ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) { ill_t *ill = rq->q_ptr; arh_t *arh; ipaddr_t src; ipif_t *ipif; char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */ char hbuf[MAC_STR_LEN]; char sbuf[INET_ADDRSTRLEN]; const char *failtype; boolean_t bring_up; ip_stack_t *ipst = ill->ill_ipst; switch (((arcn_t *)mp->b_rptr)->arcn_code) { case AR_CN_READY: failtype = NULL; bring_up = B_TRUE; break; case AR_CN_FAILED: failtype = "in use"; bring_up = B_FALSE; break; default: failtype = "claimed"; bring_up = B_FALSE; break; } arh = (arh_t *)mp->b_cont->b_rptr; bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, sizeof (hbuf)); (void) ip_dot_addr(src, sbuf); for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { if ((ipif->ipif_flags & IPIF_POINTOPOINT) || ipif->ipif_lcl_addr != src) { continue; } /* * If we failed on a recovery probe, then restart the timer to * try again later. */ if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) && !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && ill->ill_net_type == IRE_IF_RESOLVER && !(ipif->ipif_state_flags & IPIF_CONDEMNED) && ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0) { ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); continue; } /* * If what we're trying to do has already been done, then do * nothing. */ if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0)) continue; ipif_get_name(ipif, ibuf, sizeof (ibuf)); if (failtype == NULL) { cmn_err(CE_NOTE, "recovered address %s on %s", sbuf, ibuf); } else { cmn_err(CE_WARN, "%s has duplicate address %s (%s " "by %s); disabled", ibuf, sbuf, failtype, hbuf); } if (bring_up) { ASSERT(ill->ill_dl_up); /* * Free up the ARP delete message so we can allocate * a fresh one through the normal path. */ freemsg(ipif->ipif_arp_del_mp); ipif->ipif_arp_del_mp = NULL; if (ipif_resolver_up(ipif, Res_act_initial) != EINPROGRESS) { ipif->ipif_addr_ready = 1; (void) ipif_up_done(ipif); } continue; } mutex_enter(&ill->ill_lock); ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); ipif->ipif_flags |= IPIF_DUPLICATE; ill->ill_ipif_dup_count++; mutex_exit(&ill->ill_lock); /* * Already exclusive on the ill; no need to handle deferred * processing here. */ (void) ipif_down(ipif, NULL, NULL); ipif_down_tail(ipif); mutex_enter(&ill->ill_lock); if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) && ill->ill_net_type == IRE_IF_RESOLVER && !(ipif->ipif_state_flags & IPIF_CONDEMNED) && ipst->ips_ip_dup_recovery > 0) { ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery)); } mutex_exit(&ill->ill_lock); } freemsg(mp); } /* ARGSUSED */ static void ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg) { ill_t *ill = rq->q_ptr; arh_t *arh; ipaddr_t src; ipif_t *ipif; arh = (arh_t *)mp->b_cont->b_rptr; bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN); for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src) (void) ipif_resolver_up(ipif, Res_act_defend); } freemsg(mp); } /* * News from ARP. ARP sends notification of interesting events down * to its clients using M_CTL messages with the interesting ARP packet * attached via b_cont. * The interesting event from a device comes up the corresponding ARP-IP-DEV * queue as opposed to ARP sending the message to all the clients, i.e. all * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache * table if a cache IRE is found to delete all the entries for the address in * the packet. */ static void ip_arp_news(queue_t *q, mblk_t *mp) { arcn_t *arcn; arh_t *arh; ire_t *ire = NULL; char hbuf[MAC_STR_LEN]; char sbuf[INET_ADDRSTRLEN]; ipaddr_t src; in6_addr_t v6src; boolean_t isv6 = B_FALSE; ipif_t *ipif; ill_t *ill; ip_stack_t *ipst; if (CONN_Q(q)) { conn_t *connp = Q_TO_CONN(q); ipst = connp->conn_netstack->netstack_ip; } else { ill_t *ill = (ill_t *)q->q_ptr; ipst = ill->ill_ipst; } if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) { if (q->q_next) { putnext(q, mp); } else freemsg(mp); return; } arh = (arh_t *)mp->b_cont->b_rptr; /* Is it one we are interested in? */ if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) { isv6 = B_TRUE; bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src, IPV6_ADDR_LEN); } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) { bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src, IP_ADDR_LEN); } else { freemsg(mp); return; } ill = q->q_ptr; arcn = (arcn_t *)mp->b_rptr; switch (arcn->arcn_code) { case AR_CN_BOGON: /* * Someone is sending ARP packets with a source protocol * address that we have published and for which we believe our * entry is authoritative and (when ill_arp_extend is set) * verified to be unique on the network. * * The ARP module internally handles the cases where the sender * is just probing (for DAD) and where the hardware address of * a non-authoritative entry has changed. Thus, these are the * real conflicts, and we have to do resolution. * * We back away quickly from the address if it's from DHCP or * otherwise temporary and hasn't been used recently (or at * all). We'd like to include "deprecated" addresses here as * well (as there's no real reason to defend something we're * discarding), but IPMP "reuses" this flag to mean something * other than the standard meaning. * * If the ARP module above is not extended (meaning that it * doesn't know how to defend the address), then we just log * the problem as we always did and continue on. It's not * right, but there's little else we can do, and those old ATM * users are going away anyway. */ (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf, sizeof (hbuf)); (void) ip_dot_addr(src, sbuf); if (isv6) { ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL, ipst); } else { ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst); } if (ire != NULL && IRE_IS_LOCAL(ire)) { uint32_t now; uint32_t maxage; clock_t lused; uint_t maxdefense; uint_t defs; /* * First, figure out if this address hasn't been used * in a while. If it hasn't, then it's a better * candidate for abandoning. */ ipif = ire->ire_ipif; ASSERT(ipif != NULL); now = gethrestime_sec(); maxage = now - ire->ire_create_time; if (maxage > ipst->ips_ip_max_temp_idle) maxage = ipst->ips_ip_max_temp_idle; lused = drv_hztousec(ddi_get_lbolt() - ire->ire_last_used_time) / MICROSEC + 1; if (lused >= maxage && (ipif->ipif_flags & (IPIF_DHCPRUNNING | IPIF_TEMPORARY))) maxdefense = ipst->ips_ip_max_temp_defend; else maxdefense = ipst->ips_ip_max_defend; /* * Now figure out how many times we've defended * ourselves. Ignore defenses that happened long in * the past. */ mutex_enter(&ire->ire_lock); if ((defs = ire->ire_defense_count) > 0 && now - ire->ire_defense_time > ipst->ips_ip_defend_interval) { ire->ire_defense_count = defs = 0; } ire->ire_defense_count++; ire->ire_defense_time = now; mutex_exit(&ire->ire_lock); ill_refhold(ill); ire_refrele(ire); /* * If we've defended ourselves too many times already, * then give up and tear down the interface(s) using * this address. Otherwise, defend by sending out a * gratuitous ARP. */ if (defs >= maxdefense && ill->ill_arp_extend) { qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE); } else { cmn_err(CE_WARN, "node %s is using our IP address %s on %s", hbuf, sbuf, ill->ill_name); /* * If this is an old (ATM) ARP module, then * don't try to defend the address. Remain * compatible with the old behavior. Defend * only with new ARP. */ if (ill->ill_arp_extend) { qwriter_ip(ill, q, mp, ip_arp_defend, NEW_OP, B_FALSE); } else { ill_refrele(ill); } } return; } cmn_err(CE_WARN, "proxy ARP problem? Node '%s' is using %s on %s", hbuf, sbuf, ill->ill_name); if (ire != NULL) ire_refrele(ire); break; case AR_CN_ANNOUNCE: if (isv6) { /* * For XRESOLV interfaces. * Delete the IRE cache entry and NCE for this * v6 address */ ip_ire_clookup_and_delete_v6(&v6src, ipst); /* * If v6src is a non-zero, it's a router address * as below. Do the same sort of thing to clean * out off-net IRE_CACHE entries that go through * the router. */ if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) { ire_walk_v6(ire_delete_cache_gw_v6, (char *)&v6src, ALL_ZONES, ipst); } } else { nce_hw_map_t hwm; /* * ARP gives us a copy of any packet where it thinks * the address has changed, so that we can update our * caches. We're responsible for caching known answers * in the current design. We check whether the * hardware address really has changed in all of our * entries that have cached this mapping, and if so, we * blow them away. This way we will immediately pick * up the rare case of a host changing hardware * address. */ if (src == 0) break; hwm.hwm_addr = src; hwm.hwm_hwlen = arh->arh_hlen; hwm.hwm_hwaddr = (uchar_t *)(arh + 1); NDP_HW_CHANGE_INCR(ipst->ips_ndp4); ndp_walk_common(ipst->ips_ndp4, NULL, (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES); NDP_HW_CHANGE_DECR(ipst->ips_ndp4); } break; case AR_CN_READY: /* No external v6 resolver has a contract to use this */ if (isv6) break; /* If the link is down, we'll retry this later */ if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING)) break; ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst); if (ipif != NULL) { /* * If this is a duplicate recovery, then we now need to * go exclusive to bring this thing back up. */ if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) == IPIF_DUPLICATE) { ipif_refrele(ipif); ill_refhold(ill); qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE); return; } /* * If this is the first notice that this address is * ready, then let the user know now. */ if ((ipif->ipif_flags & IPIF_UP) && !ipif->ipif_addr_ready) { ipif_mask_reply(ipif); ip_rts_ifmsg(ipif); ip_rts_newaddrmsg(RTM_ADD, 0, ipif); sctp_update_ipif(ipif, SCTP_IPIF_UP); } ipif->ipif_addr_ready = 1; ipif_refrele(ipif); } ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp), ipst); if (ire != NULL) { ire->ire_defense_count = 0; ire_refrele(ire); } break; case AR_CN_FAILED: /* No external v6 resolver has a contract to use this */ if (isv6) break; ill_refhold(ill); qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE); return; } freemsg(mp); } /* * Create a mblk suitable for carrying the interface index and/or source link * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user * application. */ mblk_t * ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid, ip_stack_t *ipst) { mblk_t *mp; ip_pktinfo_t *pinfo; ipha_t *ipha; struct ether_header *pether; mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED); if (mp == NULL) { ip1dbg(("ip_add_info: allocation failure.\n")); return (data_mp); } ipha = (ipha_t *)data_mp->b_rptr; pinfo = (ip_pktinfo_t *)mp->b_rptr; bzero(pinfo, sizeof (ip_pktinfo_t)); pinfo->ip_pkt_flags = (uchar_t)flags; pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */ if (flags & (IPF_RECVIF | IPF_RECVADDR)) pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex; if (flags & IPF_RECVADDR) { ipif_t *ipif; ire_t *ire; /* * Only valid for V4 */ ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) == (IPV4_VERSION << 4)); ipif = ipif_get_next_ipif(NULL, ill); if (ipif != NULL) { /* * Since a decision has already been made to deliver the * packet, there is no need to test for SECATTR and * ZONEONLY. * When a multicast packet is transmitted * a cache entry is created for the multicast address. * When delivering a copy of the packet or when new * packets are received we do not want to match on the * cached entry so explicitly match on * IRE_LOCAL and IRE_LOOPBACK */ ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL | IRE_LOOPBACK, ipif, zoneid, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP, ipst); if (ire == NULL) { /* * packet must have come on a different * interface. * Since a decision has already been made to * deliver the packet, there is no need to test * for SECATTR and ZONEONLY. * Only match on local and broadcast ire's. * See detailed comment above. */ ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL | IRE_LOOPBACK, ipif, zoneid, NULL, MATCH_IRE_TYPE, ipst); } if (ire == NULL) { /* * This is either a multicast packet or * the address has been removed since * the packet was received. * Return INADDR_ANY so that normal source * selection occurs for the response. */ pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; } else { pinfo->ip_pkt_match_addr.s_addr = ire->ire_src_addr; ire_refrele(ire); } ipif_refrele(ipif); } else { pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY; } } pether = (struct ether_header *)((char *)ipha - sizeof (struct ether_header)); /* * Make sure the interface is an ethernet type, since this option * is currently supported only on this type of interface. Also make * sure we are pointing correctly above db_base. */ if ((flags & IPF_RECVSLLA) && ((uchar_t *)pether >= data_mp->b_datap->db_base) && (ill->ill_type == IFT_ETHER) && (ill->ill_net_type == IRE_IF_RESOLVER)) { pinfo->ip_pkt_slla.sdl_type = IFT_ETHER; bcopy((uchar_t *)pether->ether_shost.ether_addr_octet, (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL); } else { /* * Clear the bit. Indicate to upper layer that IP is not * sending this ancillary info. */ pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA; } mp->b_datap->db_type = M_CTL; mp->b_wptr += sizeof (ip_pktinfo_t); mp->b_cont = data_mp; return (mp); } /* * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as * part of the bind request. */ boolean_t ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp) { ipsec_in_t *ii; ASSERT(policy_mp != NULL); ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET); ii = (ipsec_in_t *)policy_mp->b_rptr; ASSERT(ii->ipsec_in_type == IPSEC_IN); connp->conn_policy = ii->ipsec_in_policy; ii->ipsec_in_policy = NULL; if (ii->ipsec_in_action != NULL) { if (connp->conn_latch == NULL) { connp->conn_latch = iplatch_create(); if (connp->conn_latch == NULL) return (B_FALSE); } ipsec_latch_inbound(connp->conn_latch, ii); } return (B_TRUE); } /* * Upper level protocols (ULP) pass through bind requests to IP for inspection * and to arrange for power-fanout assist. The ULP is identified by * adding a single byte at the end of the original bind message. * A ULP other than UDP or TCP that wishes to be recognized passes * down a bind with a zero length address. * * The binding works as follows: * - A zero byte address means just bind to the protocol. * - A four byte address is treated as a request to validate * that the address is a valid local address, appropriate for * an application to bind to. This does not affect any fanout * information in IP. * - A sizeof sin_t byte address is used to bind to only the local address * and port. * - A sizeof ipa_conn_t byte address contains complete fanout information * consisting of local and remote addresses and ports. In * this case, the addresses are both validated as appropriate * for this operation, and, if so, the information is retained * for use in the inbound fanout. * * The ULP (except in the zero-length bind) can append an * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants * a copy of the source or destination IRE (source for local bind; * destination for complete bind). IPSEC_POLICY_SET indicates that the * policy information contained should be copied on to the conn. * * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present. */ mblk_t * ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp) { ssize_t len; struct T_bind_req *tbr; sin_t *sin; ipa_conn_t *ac; uchar_t *ucp; mblk_t *mp1; boolean_t ire_requested; boolean_t ipsec_policy_set = B_FALSE; int error = 0; int protocol; ipa_conn_x_t *acx; ASSERT(!connp->conn_af_isv6); connp->conn_pkt_isv6 = B_FALSE; len = MBLKL(mp); if (len < (sizeof (*tbr) + 1)) { (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, "ip_bind: bogus msg, len %ld", len); /* XXX: Need to return something better */ goto bad_addr; } /* Back up and extract the protocol identifier. */ mp->b_wptr--; protocol = *mp->b_wptr & 0xFF; tbr = (struct T_bind_req *)mp->b_rptr; /* Reset the message type in preparation for shipping it back. */ DB_TYPE(mp) = M_PCPROTO; connp->conn_ulp = (uint8_t)protocol; /* * Check for a zero length address. This is from a protocol that * wants to register to receive all packets of its type. */ if (tbr->ADDR_length == 0) { /* * These protocols are now intercepted in ip_bind_v6(). * Reject protocol-level binds here for now. * * For SCTP raw socket, ICMP sends down a bind with sin_t * so that the protocol type cannot be SCTP. */ if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH || protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) { goto bad_addr; } /* * * The udp module never sends down a zero-length address, * and allowing this on a labeled system will break MLP * functionality. */ if (is_system_labeled() && protocol == IPPROTO_UDP) goto bad_addr; if (connp->conn_mac_exempt) goto bad_addr; /* No hash here really. The table is big enough. */ connp->conn_srcv6 = ipv6_all_zeros; ipcl_proto_insert(connp, protocol); tbr->PRIM_type = T_BIND_ACK; return (mp); } /* Extract the address pointer from the message. */ ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, tbr->ADDR_length); if (ucp == NULL) { ip1dbg(("ip_bind: no address\n")); goto bad_addr; } if (!OK_32PTR(ucp)) { ip1dbg(("ip_bind: unaligned address\n")); goto bad_addr; } /* * Check for trailing mps. */ mp1 = mp->b_cont; ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE); ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET); switch (tbr->ADDR_length) { default: ip1dbg(("ip_bind: bad address length %d\n", (int)tbr->ADDR_length)); goto bad_addr; case IP_ADDR_LEN: /* Verification of local address only */ error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0, ire_requested, ipsec_policy_set, B_FALSE); break; case sizeof (sin_t): sin = (sin_t *)ucp; error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr, sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE); break; case sizeof (ipa_conn_t): ac = (ipa_conn_t *)ucp; /* For raw socket, the local port is not set. */ if (ac->ac_lport == 0) ac->ac_lport = connp->conn_lport; /* Always verify destination reachability. */ error = ip_bind_connected(connp, mp, &ac->ac_laddr, ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested, ipsec_policy_set, B_TRUE, B_TRUE); break; case sizeof (ipa_conn_x_t): acx = (ipa_conn_x_t *)ucp; /* * Whether or not to verify destination reachability depends * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags. */ error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr, acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr, acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set, B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0); break; } if (error == EINPROGRESS) return (NULL); else if (error != 0) goto bad_addr; /* * Pass the IPsec headers size in ire_ipsec_overhead. * We can't do this in ip_bind_insert_ire because the policy * may not have been inherited at that point in time and hence * conn_out_enforce_policy may not be set. */ mp1 = mp->b_cont; if (ire_requested && connp->conn_out_enforce_policy && mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { ire_t *ire = (ire_t *)mp1->b_rptr; ASSERT(MBLKL(mp1) >= sizeof (ire_t)); ire->ire_ipsec_overhead = conn_ipsec_length(connp); } /* Send it home. */ mp->b_datap->db_type = M_PCPROTO; tbr->PRIM_type = T_BIND_ACK; return (mp); bad_addr: /* * If error = -1 then we generate a TBADADDR - otherwise error is * a unix errno. */ if (error > 0) mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); else mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); return (mp); } /* * Here address is verified to be a valid local address. * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast * address is also considered a valid local address. * In the case of a broadcast/multicast address, however, the * upper protocol is expected to reset the src address * to 0 if it sees a IRE_BROADCAST type returned so that * no packets are emitted with broadcast/multicast address as * source address (that violates hosts requirements RFC1122) * The addresses valid for bind are: * (1) - INADDR_ANY (0) * (2) - IP address of an UP interface * (3) - IP address of a DOWN interface * (4) - valid local IP broadcast addresses. In this case * the conn will only receive packets destined to * the specified broadcast address. * (5) - a multicast address. In this case * the conn will only receive packets destined to * the specified multicast address. Note: the * application still has to issue an * IP_ADD_MEMBERSHIP socket option. * * On error, return -1 for TBADADDR otherwise pass the * errno with TSYSERR reply. * * In all the above cases, the bound address must be valid in the current zone. * When the address is loopback, multicast or broadcast, there might be many * matching IREs so bind has to look up based on the zone. * * Note: lport is in network byte order. */ int ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport, boolean_t ire_requested, boolean_t ipsec_policy_set, boolean_t fanout_insert) { int error = 0; ire_t *src_ire; mblk_t *policy_mp; ipif_t *ipif; zoneid_t zoneid; ip_stack_t *ipst = connp->conn_netstack->netstack_ip; if (ipsec_policy_set) { policy_mp = mp->b_cont; } /* * If it was previously connected, conn_fully_bound would have * been set. */ connp->conn_fully_bound = B_FALSE; src_ire = NULL; ipif = NULL; zoneid = IPCL_ZONEID(connp); if (src_addr) { src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); /* * If an address other than 0.0.0.0 is requested, * we verify that it is a valid address for bind * Note: Following code is in if-else-if form for * readability compared to a condition check. */ /* LINTED - statement has no consequent */ if (IRE_IS_LOCAL(src_ire)) { /* * (2) Bind to address of local UP interface */ } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) { /* * (4) Bind to broadcast address * Note: permitted only from transports that * request IRE */ if (!ire_requested) error = EADDRNOTAVAIL; } else { /* * (3) Bind to address of local DOWN interface * (ipif_lookup_addr() looks up all interfaces * but we do not get here for UP interfaces * - case (2) above) * We put the protocol byte back into the mblk * since we may come back via ip_wput_nondata() * later with this mblk if ipif_lookup_addr chooses * to defer processing. */ *mp->b_wptr++ = (char)connp->conn_ulp; if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid, CONNP_TO_WQ(connp), mp, ip_wput_nondata, &error, ipst)) != NULL) { ipif_refrele(ipif); } else if (error == EINPROGRESS) { if (src_ire != NULL) ire_refrele(src_ire); return (EINPROGRESS); } else if (CLASSD(src_addr)) { error = 0; if (src_ire != NULL) ire_refrele(src_ire); /* * (5) bind to multicast address. * Fake out the IRE returned to upper * layer to be a broadcast IRE. */ src_ire = ire_ctable_lookup( INADDR_BROADCAST, INADDR_ANY, IRE_BROADCAST, NULL, zoneid, NULL, (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY), ipst); if (src_ire == NULL || !ire_requested) error = EADDRNOTAVAIL; } else { /* * Not a valid address for bind */ error = EADDRNOTAVAIL; } /* * Just to keep it consistent with the processing in * ip_bind_v4() */ mp->b_wptr--; } if (error) { /* Red Alert! Attempting to be a bogon! */ ip1dbg(("ip_bind: bad src address 0x%x\n", ntohl(src_addr))); goto bad_addr; } } /* * Allow setting new policies. For example, disconnects come * down as ipa_t bind. As we would have set conn_policy_cached * to B_TRUE before, we should set it to B_FALSE, so that policy * can change after the disconnect. */ connp->conn_policy_cached = B_FALSE; /* * If not fanout_insert this was just an address verification */ if (fanout_insert) { /* * The addresses have been verified. Time to insert in * the correct fanout list. */ IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6); connp->conn_lport = lport; connp->conn_fport = 0; /* * Do we need to add a check to reject Multicast packets */ error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport); } if (error == 0) { if (ire_requested) { if (!ip_bind_insert_ire(mp, src_ire, NULL, ipst)) { error = -1; /* Falls through to bad_addr */ } } else if (ipsec_policy_set) { if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { error = -1; /* Falls through to bad_addr */ } } } bad_addr: if (error != 0) { if (connp->conn_anon_port) { (void) tsol_mlp_anon(crgetzone(connp->conn_cred), connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), B_FALSE); } connp->conn_mlp_type = mlptSingle; } if (src_ire != NULL) IRE_REFRELE(src_ire); if (ipsec_policy_set) { ASSERT(policy_mp == mp->b_cont); ASSERT(policy_mp != NULL); freeb(policy_mp); /* * As of now assume that nothing else accompanies * IPSEC_POLICY_SET. */ mp->b_cont = NULL; } return (error); } /* * Verify that both the source and destination addresses * are valid. If verify_dst is false, then the destination address may be * unreachable, i.e. have no route to it. Protocols like TCP want to verify * destination reachability, while tunnels do not. * Note that we allow connect to broadcast and multicast * addresses when ire_requested is set. Thus the ULP * has to check for IRE_BROADCAST and multicast. * * Returns zero if ok. * On error: returns -1 to mean TBADADDR otherwise returns an errno * (for use with TSYSERR reply). * * Note: lport and fport are in network byte order. */ int ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp, uint16_t lport, ipaddr_t dst_addr, uint16_t fport, boolean_t ire_requested, boolean_t ipsec_policy_set, boolean_t fanout_insert, boolean_t verify_dst) { ire_t *src_ire; ire_t *dst_ire; int error = 0; int protocol; mblk_t *policy_mp; ire_t *sire = NULL; ire_t *md_dst_ire = NULL; ire_t *lso_dst_ire = NULL; ill_t *ill = NULL; zoneid_t zoneid; ipaddr_t src_addr = *src_addrp; ip_stack_t *ipst = connp->conn_netstack->netstack_ip; src_ire = dst_ire = NULL; protocol = *mp->b_wptr & 0xFF; /* * If we never got a disconnect before, clear it now. */ connp->conn_fully_bound = B_FALSE; if (ipsec_policy_set) { policy_mp = mp->b_cont; } zoneid = IPCL_ZONEID(connp); if (CLASSD(dst_addr)) { /* Pick up an IRE_BROADCAST */ dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | MATCH_IRE_SECATTR), ipst); } else { /* * If conn_dontroute is set or if conn_nexthop_set is set, * and onlink ipif is not found set ENETUNREACH error. */ if (connp->conn_dontroute || connp->conn_nexthop_set) { ipif_t *ipif; ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ? dst_addr : connp->conn_nexthop_v4, zoneid, ipst); if (ipif == NULL) { error = ENETUNREACH; goto bad_addr; } ipif_refrele(ipif); } if (connp->conn_nexthop_set) { dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0, 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp), MATCH_IRE_SECATTR, ipst); } else { dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL, &sire, zoneid, MBLK_GETLABEL(mp), (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | MATCH_IRE_SECATTR), ipst); } } /* * dst_ire can't be a broadcast when not ire_requested. * We also prevent ire's with src address INADDR_ANY to * be used, which are created temporarily for * sending out packets from endpoints that have * conn_unspec_src set. If verify_dst is true, the destination must be * reachable. If verify_dst is false, the destination needn't be * reachable. * * If we match on a reject or black hole, then we've got a * local failure. May as well fail out the connect() attempt, * since it's never going to succeed. */ if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY || (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) { /* * If we're verifying destination reachability, we always want * to complain here. * * If we're not verifying destination reachability but the * destination has a route, we still want to fail on the * temporary address and broadcast address tests. */ if (verify_dst || (dst_ire != NULL)) { if (ip_debug > 2) { pr_addr_dbg("ip_bind_connected: bad connected " "dst %s\n", AF_INET, &dst_addr); } if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST)) error = ENETUNREACH; else error = EHOSTUNREACH; goto bad_addr; } } /* * We now know that routing will allow us to reach the destination. * Check whether Trusted Solaris policy allows communication with this * host, and pretend that the destination is unreachable if not. * * This is never a problem for TCP, since that transport is known to * compute the label properly as part of the tcp_rput_other T_BIND_ACK * handling. If the remote is unreachable, it will be detected at that * point, so there's no reason to check it here. * * Note that for sendto (and other datagram-oriented friends), this * check is done as part of the data path label computation instead. * The check here is just to make non-TCP connect() report the right * error. */ if (dst_ire != NULL && is_system_labeled() && !IPCL_IS_TCP(connp) && tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL, connp->conn_mac_exempt, ipst) != 0) { error = EHOSTUNREACH; if (ip_debug > 2) { pr_addr_dbg("ip_bind_connected: no label for dst %s\n", AF_INET, &dst_addr); } goto bad_addr; } /* * If the app does a connect(), it means that it will most likely * send more than 1 packet to the destination. It makes sense * to clear the temporary flag. */ if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { irb_t *irb = dst_ire->ire_bucket; rw_enter(&irb->irb_lock, RW_WRITER); /* * We need to recheck for IRE_MARK_TEMPORARY after acquiring * the lock to guarantee irb_tmp_ire_cnt. */ if (dst_ire->ire_marks & IRE_MARK_TEMPORARY) { dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; irb->irb_tmp_ire_cnt--; } rw_exit(&irb->irb_lock); } /* * See if we should notify ULP about LSO/MDT; we do this whether or not * ire_requested is TRUE, in order to handle active connects; LSO/MDT * eligibility tests for passive connects are handled separately * through tcp_adapt_ire(). We do this before the source address * selection, because dst_ire may change after a call to * ipif_select_source(). This is a best-effort check, as the * packet for this connection may not actually go through * dst_ire->ire_stq, and the exact IRE can only be known after * calling ip_newroute(). This is why we further check on the * IRE during LSO/Multidata packet transmission in * tcp_lsosend()/tcp_multisend(). */ if (!ipsec_policy_set && dst_ire != NULL && !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && (ill = ire_to_ill(dst_ire), ill != NULL)) { if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { lso_dst_ire = dst_ire; IRE_REFHOLD(lso_dst_ire); } else if (ipst->ips_ip_multidata_outbound && ILL_MDT_CAPABLE(ill)) { md_dst_ire = dst_ire; IRE_REFHOLD(md_dst_ire); } } if (dst_ire != NULL && dst_ire->ire_type == IRE_LOCAL && dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) { /* * If the IRE belongs to a different zone, look for a matching * route in the forwarding table and use the source address from * that route. */ src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL, zoneid, 0, NULL, MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE, ipst); if (src_ire == NULL) { error = EHOSTUNREACH; goto bad_addr; } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { if (!(src_ire->ire_type & IRE_HOST)) error = ENETUNREACH; else error = EHOSTUNREACH; goto bad_addr; } if (src_addr == INADDR_ANY) src_addr = src_ire->ire_src_addr; ire_refrele(src_ire); src_ire = NULL; } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) { if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { src_addr = sire->ire_src_addr; ire_refrele(dst_ire); dst_ire = sire; sire = NULL; } else { /* * Pick a source address so that a proper inbound * load spreading would happen. */ ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill; ipif_t *src_ipif = NULL; ire_t *ipif_ire; /* * Supply a local source address such that inbound * load spreading happens. * * Determine the best source address on this ill for * the destination. * * 1) For broadcast, we should return a broadcast ire * found above so that upper layers know that the * destination address is a broadcast address. * * 2) If this is part of a group, select a better * source address so that better inbound load * balancing happens. Do the same if the ipif * is DEPRECATED. * * 3) If the outgoing interface is part of a usesrc * group, then try selecting a source address from * the usesrc ILL. */ if ((dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) || (!(dst_ire->ire_flags & RTF_SETSRC)) && (!(dst_ire->ire_type & IRE_BROADCAST) && ((dst_ill->ill_group != NULL) || (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || (dst_ill->ill_usesrc_ifindex != 0)))) { /* * If the destination is reachable via a * given gateway, the selected source address * should be in the same subnet as the gateway. * Otherwise, the destination is not reachable. * * If there are no interfaces on the same subnet * as the destination, ipif_select_source gives * first non-deprecated interface which might be * on a different subnet than the gateway. * This is not desirable. Hence pass the dst_ire * source address to ipif_select_source. * It is sure that the destination is reachable * with the dst_ire source address subnet. * So passing dst_ire source address to * ipif_select_source will make sure that the * selected source will be on the same subnet * as dst_ire source address. */ ipaddr_t saddr = dst_ire->ire_ipif->ipif_src_addr; src_ipif = ipif_select_source(dst_ill, saddr, zoneid); if (src_ipif != NULL) { if (IS_VNI(src_ipif->ipif_ill)) { /* * For VNI there is no * interface route */ src_addr = src_ipif->ipif_src_addr; } else { ipif_ire = ipif_to_ire(src_ipif); if (ipif_ire != NULL) { IRE_REFRELE(dst_ire); dst_ire = ipif_ire; } src_addr = dst_ire->ire_src_addr; } ipif_refrele(src_ipif); } else { src_addr = dst_ire->ire_src_addr; } } else { src_addr = dst_ire->ire_src_addr; } } } /* * We do ire_route_lookup() here (and not * interface lookup as we assert that * src_addr should only come from an * UP interface for hard binding. */ ASSERT(src_ire == NULL); src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst); /* src_ire must be a local|loopback */ if (!IRE_IS_LOCAL(src_ire)) { if (ip_debug > 2) { pr_addr_dbg("ip_bind_connected: bad connected " "src %s\n", AF_INET, &src_addr); } error = EADDRNOTAVAIL; goto bad_addr; } /* * If the source address is a loopback address, the * destination had best be local or multicast. * The transports that can't handle multicast will reject * those addresses. */ if (src_ire->ire_type == IRE_LOOPBACK && !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) { ip1dbg(("ip_bind_connected: bad connected loopback\n")); error = -1; goto bad_addr; } /* * Allow setting new policies. For example, disconnects come * down as ipa_t bind. As we would have set conn_policy_cached * to B_TRUE before, we should set it to B_FALSE, so that policy * can change after the disconnect. */ connp->conn_policy_cached = B_FALSE; /* * Set the conn addresses/ports immediately, so the IPsec policy calls * can handle their passed-in conn's. */ IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6); IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6); connp->conn_lport = lport; connp->conn_fport = fport; *src_addrp = src_addr; ASSERT(!(ipsec_policy_set && ire_requested)); if (ire_requested) { iulp_t *ulp_info = NULL; /* * Note that sire will not be NULL if this is an off-link * connection and there is not cache for that dest yet. * * XXX Because of an existing bug, if there are multiple * default routes, the IRE returned now may not be the actual * default route used (default routes are chosen in a * round robin fashion). So if the metrics for different * default routes are different, we may return the wrong * metrics. This will not be a problem if the existing * bug is fixed. */ if (sire != NULL) { ulp_info = &(sire->ire_uinfo); } if (!ip_bind_insert_ire(mp, dst_ire, ulp_info, ipst)) { error = -1; goto bad_addr; } } else if (ipsec_policy_set) { if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { error = -1; goto bad_addr; } } /* * Cache IPsec policy in this conn. If we have per-socket policy, * we'll cache that. If we don't, we'll inherit global policy. * * We can't insert until the conn reflects the policy. Note that * conn_policy_cached is set by ipsec_conn_cache_policy() even for * connections where we don't have a policy. This is to prevent * global policy lookups in the inbound path. * * If we insert before we set conn_policy_cached, * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true * because global policy cound be non-empty. We normally call * ipsec_check_policy() for conn_policy_cached connections only if * ipc_in_enforce_policy is set. But in this case, * conn_policy_cached can get set anytime since we made the * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is * called, which will make the above assumption false. Thus, we * need to insert after we set conn_policy_cached. */ if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0) goto bad_addr; if (fanout_insert) { /* * The addresses have been verified. Time to insert in * the correct fanout list. */ error = ipcl_conn_insert(connp, protocol, src_addr, dst_addr, connp->conn_ports); } if (error == 0) { connp->conn_fully_bound = B_TRUE; /* * Our initial checks for LSO/MDT have passed; the IRE is not * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to * be supporting LSO/MDT. Pass the IRE, IPC and ILL into * ip_xxinfo_return(), which performs further checks * against them and upon success, returns the LSO/MDT info * mblk which we will attach to the bind acknowledgment. */ if (lso_dst_ire != NULL) { mblk_t *lsoinfo_mp; ASSERT(ill->ill_lso_capab != NULL); if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp, ill->ill_name, ill->ill_lso_capab)) != NULL) linkb(mp, lsoinfo_mp); } else if (md_dst_ire != NULL) { mblk_t *mdinfo_mp; ASSERT(ill->ill_mdt_capab != NULL); if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, ill->ill_name, ill->ill_mdt_capab)) != NULL) linkb(mp, mdinfo_mp); } } bad_addr: if (ipsec_policy_set) { ASSERT(policy_mp == mp->b_cont); ASSERT(policy_mp != NULL); freeb(policy_mp); /* * As of now assume that nothing else accompanies * IPSEC_POLICY_SET. */ mp->b_cont = NULL; } if (src_ire != NULL) IRE_REFRELE(src_ire); if (dst_ire != NULL) IRE_REFRELE(dst_ire); if (sire != NULL) IRE_REFRELE(sire); if (md_dst_ire != NULL) IRE_REFRELE(md_dst_ire); if (lso_dst_ire != NULL) IRE_REFRELE(lso_dst_ire); return (error); } /* * Insert the ire in b_cont. Returns false if it fails (due to lack of space). * Prefers dst_ire over src_ire. */ static boolean_t ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst) { mblk_t *mp1; ire_t *ret_ire = NULL; mp1 = mp->b_cont; ASSERT(mp1 != NULL); if (ire != NULL) { /* * mp1 initialized above to IRE_DB_REQ_TYPE * appended mblk. Its 's * job to make sure there is room. */ if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) return (0); mp1->b_datap->db_type = IRE_DB_TYPE; mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); bcopy(ire, mp1->b_rptr, sizeof (ire_t)); ret_ire = (ire_t *)mp1->b_rptr; /* * Pass the latest setting of the ip_path_mtu_discovery and * copy the ulp info if any. */ ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ? IPH_DF : 0; if (ulp_info != NULL) { bcopy(ulp_info, &(ret_ire->ire_uinfo), sizeof (iulp_t)); } ret_ire->ire_mp = mp1; } else { /* * No IRE was found. Remove IRE mblk. */ mp->b_cont = mp1->b_cont; freeb(mp1); } return (1); } /* * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping * the final piece where we don't. Return a pointer to the first mblk in the * result, and update the pointer to the next mblk to chew on. If anything * goes wrong (i.e., dupb fails), we waste everything in sight and return a * NULL pointer. */ mblk_t * ip_carve_mp(mblk_t **mpp, ssize_t len) { mblk_t *mp0; mblk_t *mp1; mblk_t *mp2; if (!len || !mpp || !(mp0 = *mpp)) return (NULL); /* If we aren't going to consume the first mblk, we need a dup. */ if (mp0->b_wptr - mp0->b_rptr > len) { mp1 = dupb(mp0); if (mp1) { /* Partition the data between the two mblks. */ mp1->b_wptr = mp1->b_rptr + len; mp0->b_rptr = mp1->b_wptr; /* * after adjustments if mblk not consumed is now * unaligned, try to align it. If this fails free * all messages and let upper layer recover. */ if (!OK_32PTR(mp0->b_rptr)) { if (!pullupmsg(mp0, -1)) { freemsg(mp0); freemsg(mp1); *mpp = NULL; return (NULL); } } } return (mp1); } /* Eat through as many mblks as we need to get len bytes. */ len -= mp0->b_wptr - mp0->b_rptr; for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) { if (mp2->b_wptr - mp2->b_rptr > len) { /* * We won't consume the entire last mblk. Like * above, dup and partition it. */ mp1->b_cont = dupb(mp2); mp1 = mp1->b_cont; if (!mp1) { /* * Trouble. Rather than go to a lot of * trouble to clean up, we free the messages. * This won't be any worse than losing it on * the wire. */ freemsg(mp0); freemsg(mp2); *mpp = NULL; return (NULL); } mp1->b_wptr = mp1->b_rptr + len; mp2->b_rptr = mp1->b_wptr; /* * after adjustments if mblk not consumed is now * unaligned, try to align it. If this fails free * all messages and let upper layer recover. */ if (!OK_32PTR(mp2->b_rptr)) { if (!pullupmsg(mp2, -1)) { freemsg(mp0); freemsg(mp2); *mpp = NULL; return (NULL); } } *mpp = mp2; return (mp0); } /* Decrement len by the amount we just got. */ len -= mp2->b_wptr - mp2->b_rptr; } /* * len should be reduced to zero now. If not our caller has * screwed up. */ if (len) { /* Shouldn't happen! */ freemsg(mp0); *mpp = NULL; return (NULL); } /* * We consumed up to exactly the end of an mblk. Detach the part * we are returning from the rest of the chain. */ mp1->b_cont = NULL; *mpp = mp2; return (mp0); } /* The ill stream is being unplumbed. Called from ip_close */ int ip_modclose(ill_t *ill) { boolean_t success; ipsq_t *ipsq; ipif_t *ipif; queue_t *q = ill->ill_rq; ip_stack_t *ipst = ill->ill_ipst; clock_t timeout; /* * Wait for the ACKs of all deferred control messages to be processed. * In particular, we wait for a potential capability reset initiated * in ip_sioctl_plink() to complete before proceeding. * * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms) * in case the driver never replies. */ timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms); mutex_enter(&ill->ill_lock); while (ill->ill_dlpi_pending != DL_PRIM_INVAL) { if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) { /* Timeout */ break; } } mutex_exit(&ill->ill_lock); /* * Forcibly enter the ipsq after some delay. This is to take * care of the case when some ioctl does not complete because * we sent a control message to the driver and it did not * send us a reply. We want to be able to at least unplumb * and replumb rather than force the user to reboot the system. */ success = ipsq_enter(ill, B_FALSE); /* * Open/close/push/pop is guaranteed to be single threaded * per stream by STREAMS. FS guarantees that all references * from top are gone before close is called. So there can't * be another close thread that has set CONDEMNED on this ill. * and cause ipsq_enter to return failure. */ ASSERT(success); ipsq = ill->ill_phyint->phyint_ipsq; /* * Mark it condemned. No new reference will be made to this ill. * Lookup functions will return an error. Threads that try to * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures * that the refcnt will drop down to zero. */ mutex_enter(&ill->ill_lock); ill->ill_state_flags |= ILL_CONDEMNED; for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { ipif->ipif_state_flags |= IPIF_CONDEMNED; } /* * Wake up anybody waiting to enter the ipsq. ipsq_enter * returns error if ILL_CONDEMNED is set */ cv_broadcast(&ill->ill_cv); mutex_exit(&ill->ill_lock); /* * Send all the deferred DLPI messages downstream which came in * during the small window right before ipsq_enter(). We do this * without waiting for the ACKs because all the ACKs for M_PROTO * messages are ignored in ip_rput() when ILL_CONDEMNED is set. */ ill_dlpi_send_deferred(ill); /* * Shut down fragmentation reassembly. * ill_frag_timer won't start a timer again. * Now cancel any existing timer */ (void) untimeout(ill->ill_frag_timer_id); (void) ill_frag_timeout(ill, 0); /* * If MOVE was in progress, clear the * move_in_progress fields also. */ if (ill->ill_move_in_progress) { ILL_CLEAR_MOVE(ill); } /* * Call ill_delete to bring down the ipifs, ilms and ill on * this ill. Then wait for the refcnts to drop to zero. * ill_is_freeable checks whether the ill is really quiescent. * Then make sure that threads that are waiting to enter the * ipsq have seen the error returned by ipsq_enter and have * gone away. Then we call ill_delete_tail which does the * DL_UNBIND_REQ with the driver and then qprocsoff. */ ill_delete(ill); mutex_enter(&ill->ill_lock); while (!ill_is_freeable(ill)) cv_wait(&ill->ill_cv, &ill->ill_lock); while (ill->ill_waiters) cv_wait(&ill->ill_cv, &ill->ill_lock); mutex_exit(&ill->ill_lock); /* * ill_delete_tail drops reference on ill_ipst, but we need to keep * it held until the end of the function since the cleanup * below needs to be able to use the ip_stack_t. */ netstack_hold(ipst->ips_netstack); /* qprocsoff is called in ill_delete_tail */ ill_delete_tail(ill); ASSERT(ill->ill_ipst == NULL); /* * Walk through all upper (conn) streams and qenable * those that have queued data. * close synchronization needs this to * be done to ensure that all upper layers blocked * due to flow control to the closing device * get unblocked. */ ip1dbg(("ip_wsrv: walking\n")); conn_walk_drain(ipst); mutex_enter(&ipst->ips_ip_mi_lock); mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill); mutex_exit(&ipst->ips_ip_mi_lock); /* * credp could be null if the open didn't succeed and ip_modopen * itself calls ip_close. */ if (ill->ill_credp != NULL) crfree(ill->ill_credp); mutex_enter(&ill->ill_lock); ill_nic_info_dispatch(ill); mutex_exit(&ill->ill_lock); /* * Now we are done with the module close pieces that * need the netstack_t. */ netstack_rele(ipst->ips_netstack); mi_close_free((IDP)ill); q->q_ptr = WR(q)->q_ptr = NULL; ipsq_exit(ipsq); return (0); } /* * This is called as part of close() for IP, UDP, ICMP, and RTS * in order to quiesce the conn. */ void ip_quiesce_conn(conn_t *connp) { boolean_t drain_cleanup_reqd = B_FALSE; boolean_t conn_ioctl_cleanup_reqd = B_FALSE; boolean_t ilg_cleanup_reqd = B_FALSE; ip_stack_t *ipst; ASSERT(!IPCL_IS_TCP(connp)); ipst = connp->conn_netstack->netstack_ip; /* * Mark the conn as closing, and this conn must not be * inserted in future into any list. Eg. conn_drain_insert(), * won't insert this conn into the conn_drain_list. * Similarly ill_pending_mp_add() will not add any mp to * the pending mp list, after this conn has started closing. * * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg * cannot get set henceforth. */ mutex_enter(&connp->conn_lock); ASSERT(!(connp->conn_state_flags & CONN_QUIESCED)); connp->conn_state_flags |= CONN_CLOSING; if (connp->conn_idl != NULL) drain_cleanup_reqd = B_TRUE; if (connp->conn_oper_pending_ill != NULL) conn_ioctl_cleanup_reqd = B_TRUE; if (connp->conn_dhcpinit_ill != NULL) { ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0); atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit); connp->conn_dhcpinit_ill = NULL; } if (connp->conn_ilg_inuse != 0) ilg_cleanup_reqd = B_TRUE; mutex_exit(&connp->conn_lock); if (conn_ioctl_cleanup_reqd) conn_ioctl_cleanup(connp); if (is_system_labeled() && connp->conn_anon_port) { (void) tsol_mlp_anon(crgetzone(connp->conn_cred), connp->conn_mlp_type, connp->conn_ulp, ntohs(connp->conn_lport), B_FALSE); connp->conn_anon_port = 0; } connp->conn_mlp_type = mlptSingle; /* * Remove this conn from any fanout list it is on. * and then wait for any threads currently operating * on this endpoint to finish */ ipcl_hash_remove(connp); /* * Remove this conn from the drain list, and do * any other cleanup that may be required. * (Only non-tcp streams may have a non-null conn_idl. * TCP streams are never flow controlled, and * conn_idl will be null) */ if (drain_cleanup_reqd) conn_drain_tail(connp, B_TRUE); if (connp == ipst->ips_ip_g_mrouter) (void) ip_mrouter_done(NULL, ipst); if (ilg_cleanup_reqd) ilg_delete_all(connp); conn_delete_ire(connp, NULL); /* * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED. * callers from write side can't be there now because close * is in progress. The only other caller is ipcl_walk * which checks for the condemned flag. */ mutex_enter(&connp->conn_lock); connp->conn_state_flags |= CONN_CONDEMNED; while (connp->conn_ref != 1) cv_wait(&connp->conn_cv, &connp->conn_lock); connp->conn_state_flags |= CONN_QUIESCED; mutex_exit(&connp->conn_lock); } /* ARGSUSED */ int ip_close(queue_t *q, int flags) { conn_t *connp; TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q); /* * Call the appropriate delete routine depending on whether this is * a module or device. */ if (WR(q)->q_next != NULL) { /* This is a module close */ return (ip_modclose((ill_t *)q->q_ptr)); } connp = q->q_ptr; ip_quiesce_conn(connp); qprocsoff(q); /* * Now we are truly single threaded on this stream, and can * delete the things hanging off the connp, and finally the connp. * We removed this connp from the fanout list, it cannot be * accessed thru the fanouts, and we already waited for the * conn_ref to drop to 0. We are already in close, so * there cannot be any other thread from the top. qprocsoff * has completed, and service has completed or won't run in * future. */ ASSERT(connp->conn_ref == 1); inet_minor_free(connp->conn_minor_arena, connp->conn_dev); connp->conn_ref--; ipcl_conn_destroy(connp); q->q_ptr = WR(q)->q_ptr = NULL; return (0); } /* * Wapper around putnext() so that ip_rts_request can merely use * conn_recv. */ /*ARGSUSED2*/ static void ip_conn_input(void *arg1, mblk_t *mp, void *arg2) { conn_t *connp = (conn_t *)arg1; putnext(connp->conn_rq, mp); } /* Return the IP checksum for the IP header at "iph". */ uint16_t ip_csum_hdr(ipha_t *ipha) { uint16_t *uph; uint32_t sum; int opt_len; opt_len = (ipha->ipha_version_and_hdr_length & 0xF) - IP_SIMPLE_HDR_LENGTH_IN_WORDS; uph = (uint16_t *)ipha; sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; if (opt_len > 0) { do { sum += uph[10]; sum += uph[11]; uph += 2; } while (--opt_len); } sum = (sum & 0xFFFF) + (sum >> 16); sum = ~(sum + (sum >> 16)) & 0xFFFF; if (sum == 0xffff) sum = 0; return ((uint16_t)sum); } /* * Called when the module is about to be unloaded */ void ip_ddi_destroy(void) { tnet_fini(); icmp_ddi_destroy(); rts_ddi_destroy(); udp_ddi_destroy(); sctp_ddi_g_destroy(); tcp_ddi_g_destroy(); ipsec_policy_g_destroy(); ipcl_g_destroy(); ip_net_g_destroy(); ip_ire_g_fini(); inet_minor_destroy(ip_minor_arena_sa); #if defined(_LP64) inet_minor_destroy(ip_minor_arena_la); #endif #ifdef DEBUG list_destroy(&ip_thread_list); rw_destroy(&ip_thread_rwlock); tsd_destroy(&ip_thread_data); #endif netstack_unregister(NS_IP); } /* * First step in cleanup. */ /* ARGSUSED */ static void ip_stack_shutdown(netstackid_t stackid, void *arg) { ip_stack_t *ipst = (ip_stack_t *)arg; #ifdef NS_DEBUG printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid); #endif /* Get rid of loopback interfaces and their IREs */ ip_loopback_cleanup(ipst); } /* * Free the IP stack instance. */ static void ip_stack_fini(netstackid_t stackid, void *arg) { ip_stack_t *ipst = (ip_stack_t *)arg; int ret; #ifdef NS_DEBUG printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid); #endif ipv4_hook_destroy(ipst); ipv6_hook_destroy(ipst); ip_net_destroy(ipst); rw_destroy(&ipst->ips_srcid_lock); ip_kstat_fini(stackid, ipst->ips_ip_mibkp); ipst->ips_ip_mibkp = NULL; icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp); ipst->ips_icmp_mibkp = NULL; ip_kstat2_fini(stackid, ipst->ips_ip_kstat); ipst->ips_ip_kstat = NULL; bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics)); ip6_kstat_fini(stackid, ipst->ips_ip6_kstat); ipst->ips_ip6_kstat = NULL; bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics)); nd_free(&ipst->ips_ip_g_nd); kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr)); ipst->ips_param_arr = NULL; kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); ipst->ips_ndp_arr = NULL; ip_mrouter_stack_destroy(ipst); mutex_destroy(&ipst->ips_ip_mi_lock); rw_destroy(&ipst->ips_ipsec_capab_ills_lock); rw_destroy(&ipst->ips_ill_g_usesrc_lock); rw_destroy(&ipst->ips_ip_g_nd_lock); ret = untimeout(ipst->ips_igmp_timeout_id); if (ret == -1) { ASSERT(ipst->ips_igmp_timeout_id == 0); } else { ASSERT(ipst->ips_igmp_timeout_id != 0); ipst->ips_igmp_timeout_id = 0; } ret = untimeout(ipst->ips_igmp_slowtimeout_id); if (ret == -1) { ASSERT(ipst->ips_igmp_slowtimeout_id == 0); } else { ASSERT(ipst->ips_igmp_slowtimeout_id != 0); ipst->ips_igmp_slowtimeout_id = 0; } ret = untimeout(ipst->ips_mld_timeout_id); if (ret == -1) { ASSERT(ipst->ips_mld_timeout_id == 0); } else { ASSERT(ipst->ips_mld_timeout_id != 0); ipst->ips_mld_timeout_id = 0; } ret = untimeout(ipst->ips_mld_slowtimeout_id); if (ret == -1) { ASSERT(ipst->ips_mld_slowtimeout_id == 0); } else { ASSERT(ipst->ips_mld_slowtimeout_id != 0); ipst->ips_mld_slowtimeout_id = 0; } ret = untimeout(ipst->ips_ip_ire_expire_id); if (ret == -1) { ASSERT(ipst->ips_ip_ire_expire_id == 0); } else { ASSERT(ipst->ips_ip_ire_expire_id != 0); ipst->ips_ip_ire_expire_id = 0; } mutex_destroy(&ipst->ips_igmp_timer_lock); mutex_destroy(&ipst->ips_mld_timer_lock); mutex_destroy(&ipst->ips_igmp_slowtimeout_lock); mutex_destroy(&ipst->ips_mld_slowtimeout_lock); mutex_destroy(&ipst->ips_ip_addr_avail_lock); rw_destroy(&ipst->ips_ill_g_lock); ip_ire_fini(ipst); ip6_asp_free(ipst); conn_drain_fini(ipst); ipcl_destroy(ipst); mutex_destroy(&ipst->ips_ndp4->ndp_g_lock); mutex_destroy(&ipst->ips_ndp6->ndp_g_lock); kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t)); ipst->ips_ndp4 = NULL; kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t)); ipst->ips_ndp6 = NULL; if (ipst->ips_loopback_ksp != NULL) { kstat_delete_netstack(ipst->ips_loopback_ksp, stackid); ipst->ips_loopback_ksp = NULL; } kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t)); ipst->ips_phyint_g_list = NULL; kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS); ipst->ips_ill_g_heads = NULL; kmem_free(ipst, sizeof (*ipst)); } /* * This function is called from the TSD destructor, and is used to debug * reference count issues in IP. See block comment in for * details. */ static void ip_thread_exit(void *phash) { th_hash_t *thh = phash; rw_enter(&ip_thread_rwlock, RW_WRITER); list_remove(&ip_thread_list, thh); rw_exit(&ip_thread_rwlock); mod_hash_destroy_hash(thh->thh_hash); kmem_free(thh, sizeof (*thh)); } /* * Called when the IP kernel module is loaded into the kernel */ void ip_ddi_init(void) { ip_input_proc = ip_squeue_switch(ip_squeue_enter); /* * For IP and TCP the minor numbers should start from 2 since we have 4 * initial devices: ip, ip6, tcp, tcp6. */ /* * If this is a 64-bit kernel, then create two separate arenas - * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the * other for socket apps in the range 2^^18 through 2^^32-1. */ ip_minor_arena_la = NULL; ip_minor_arena_sa = NULL; #if defined(_LP64) if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) { cmn_err(CE_PANIC, "ip_ddi_init: ip_minor_arena_sa creation failed\n"); } if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la", MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) { cmn_err(CE_PANIC, "ip_ddi_init: ip_minor_arena_la creation failed\n"); } #else if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa", INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) { cmn_err(CE_PANIC, "ip_ddi_init: ip_minor_arena_sa creation failed\n"); } #endif ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms); ipcl_g_init(); ip_ire_g_init(); ip_net_g_init(); #ifdef DEBUG tsd_create(&ip_thread_data, ip_thread_exit); rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL); list_create(&ip_thread_list, sizeof (th_hash_t), offsetof(th_hash_t, thh_link)); #endif /* * We want to be informed each time a stack is created or * destroyed in the kernel, so we can maintain the * set of udp_stack_t's. */ netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown, ip_stack_fini); ipsec_policy_g_init(); tcp_ddi_g_init(); sctp_ddi_g_init(); tnet_init(); udp_ddi_init(); rts_ddi_init(); icmp_ddi_init(); } /* * Initialize the IP stack instance. */ static void * ip_stack_init(netstackid_t stackid, netstack_t *ns) { ip_stack_t *ipst; ipparam_t *pa; ipndp_t *na; #ifdef NS_DEBUG printf("ip_stack_init(stack %d)\n", stackid); #endif ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP); ipst->ips_netstack = ns; ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS, KM_SLEEP); ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t), KM_SLEEP); ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP); mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL); rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL); mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL); ipst->ips_igmp_deferred_next = INFINITY; mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL); ipst->ips_mld_deferred_next = INFINITY; mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL); mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL); mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL); rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL); rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL); rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL); ipcl_init(ipst); ip_ire_init(ipst); ip6_asp_init(ipst); ipif_init(ipst); conn_drain_init(ipst); ip_mrouter_stack_init(ipst); ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT; ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000; ipst->ips_ip_multirt_log_interval = 1000; ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT; ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT; ipst->ips_ill_index = 1; ipst->ips_saved_ip_g_forward = -1; ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */ pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); ipst->ips_param_arr = pa; bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr)); na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP); ipst->ips_ndp_arr = na; bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr)); ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data = (caddr_t)&ipst->ips_ip_g_forward; ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data = (caddr_t)&ipst->ips_ipv6_forward; ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name, "ip_cgtp_filter") == 0); ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data = (caddr_t)&ipst->ips_ip_cgtp_filter; ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_name, "ipmp_hook_emulation") == 0); ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_data = (caddr_t)&ipst->ips_ipmp_hook_emulation; (void) ip_param_register(&ipst->ips_ip_g_nd, ipst->ips_param_arr, A_CNT(lcl_param_arr), ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr)); ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst); ipst->ips_icmp_mibkp = icmp_kstat_init(stackid); ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics); ipst->ips_ip6_kstat = ip6_kstat_init(stackid, &ipst->ips_ip6_statistics); ipst->ips_ipmp_enable_failback = B_TRUE; ipst->ips_ip_src_id = 1; rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL); ip_net_init(ipst, ns); ipv4_hook_init(ipst); ipv6_hook_init(ipst); return (ipst); } /* * Allocate and initialize a DLPI template of the specified length. (May be * called as writer.) */ mblk_t * ip_dlpi_alloc(size_t len, t_uscalar_t prim) { mblk_t *mp; mp = allocb(len, BPRI_MED); if (!mp) return (NULL); /* * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter * of which we don't seem to use) are sent with M_PCPROTO, and * that other DLPI are M_PROTO. */ if (prim == DL_INFO_REQ) { mp->b_datap->db_type = M_PCPROTO; } else { mp->b_datap->db_type = M_PROTO; } mp->b_wptr = mp->b_rptr + len; bzero(mp->b_rptr, len); ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim; return (mp); } /* * Debug formatting routine. Returns a character string representation of the * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer. * * Once the ndd table-printing interfaces are removed, this can be changed to * standard dotted-decimal form. */ char * ip_dot_addr(ipaddr_t addr, char *buf) { uint8_t *ap = (uint8_t *)&addr; (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d", ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF); return (buf); } /* * Write the given MAC address as a printable string in the usual colon- * separated format. */ const char * mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen) { char *bp; if (alen == 0 || buflen < 4) return ("?"); bp = buf; for (;;) { /* * If there are more MAC address bytes available, but we won't * have any room to print them, then add "..." to the string * instead. See below for the 'magic number' explanation. */ if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) { (void) strcpy(bp, "..."); break; } (void) sprintf(bp, "%02x", *addr++); bp += 2; if (--alen == 0) break; *bp++ = ':'; buflen -= 3; /* * At this point, based on the first 'if' statement above, * either alen == 1 and buflen >= 3, or alen > 1 and * buflen >= 4. The first case leaves room for the final "xx" * number and trailing NUL byte. The second leaves room for at * least "...". Thus the apparently 'magic' numbers chosen for * that statement. */ } return (buf); } /* * Send an ICMP error after patching up the packet appropriately. Returns * non-zero if the appropriate MIB should be bumped; zero otherwise. */ static boolean_t ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags, uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst) { ipha_t *ipha; mblk_t *first_mp; boolean_t secure; unsigned char db_type; ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; first_mp = mp; if (mctl_present) { mp = mp->b_cont; secure = ipsec_in_is_secure(first_mp); ASSERT(mp != NULL); } else { /* * If this is an ICMP error being reported - which goes * up as M_CTLs, we need to convert them to M_DATA till * we finish checking with global policy because * ipsec_check_global_policy() assumes M_DATA as clear * and M_CTL as secure. */ db_type = DB_TYPE(mp); DB_TYPE(mp) = M_DATA; secure = B_FALSE; } /* * We are generating an icmp error for some inbound packet. * Called from all ip_fanout_(udp, tcp, proto) functions. * Before we generate an error, check with global policy * to see whether this is allowed to enter the system. As * there is no "conn", we are checking with global policy. */ ipha = (ipha_t *)mp->b_rptr; if (secure || ipss->ipsec_inbound_v4_policy_present) { first_mp = ipsec_check_global_policy(first_mp, NULL, ipha, NULL, mctl_present, ipst->ips_netstack); if (first_mp == NULL) return (B_FALSE); } if (!mctl_present) DB_TYPE(mp) = db_type; if (flags & IP_FF_SEND_ICMP) { if (flags & IP_FF_HDR_COMPLETE) { if (ip_hdr_complete(ipha, zoneid, ipst)) { freemsg(first_mp); return (B_TRUE); } } if (flags & IP_FF_CKSUM) { /* * Have to correct checksum since * the packet might have been * fragmented and the reassembly code in ip_rput * does not restore the IP checksum. */ ipha->ipha_hdr_checksum = 0; ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); } switch (icmp_type) { case ICMP_DEST_UNREACHABLE: icmp_unreachable(WR(q), first_mp, icmp_code, zoneid, ipst); break; default: freemsg(first_mp); break; } } else { freemsg(first_mp); return (B_FALSE); } return (B_TRUE); } /* * Used to send an ICMP error message when a packet is received for * a protocol that is not supported. The mblk passed as argument * is consumed by this function. */ void ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid, ip_stack_t *ipst) { mblk_t *mp; ipha_t *ipha; ill_t *ill; ipsec_in_t *ii; ii = (ipsec_in_t *)ipsec_mp->b_rptr; ASSERT(ii->ipsec_in_type == IPSEC_IN); mp = ipsec_mp->b_cont; ipsec_mp->b_cont = NULL; ipha = (ipha_t *)mp->b_rptr; /* Get ill from index in ipsec_in_t. */ ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL, ipst); if (ill != NULL) { if (IPH_HDR_VERSION(ipha) == IP_VERSION) { if (ip_fanout_send_icmp(q, mp, flags, ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos); } } else { if (ip_fanout_send_icmp_v6(q, mp, flags, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 0, B_FALSE, zoneid, ipst)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos); } } ill_refrele(ill); } else { /* re-link for the freemsg() below. */ ipsec_mp->b_cont = mp; } /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */ freemsg(ipsec_mp); } /* * See if the inbound datagram has had IPsec processing applied to it. */ boolean_t ipsec_in_is_secure(mblk_t *ipsec_mp) { ipsec_in_t *ii; ii = (ipsec_in_t *)ipsec_mp->b_rptr; ASSERT(ii->ipsec_in_type == IPSEC_IN); if (ii->ipsec_in_loopback) { return (ii->ipsec_in_secure); } else { return (ii->ipsec_in_ah_sa != NULL || ii->ipsec_in_esp_sa != NULL || ii->ipsec_in_decaps); } } /* * Handle protocols with which IP is less intimate. There * can be more than one stream bound to a particular * protocol. When this is the case, normally each one gets a copy * of any incoming packets. * * IPsec NOTE : * * Don't allow a secure packet going up a non-secure connection. * We don't allow this because * * 1) Reply might go out in clear which will be dropped at * the sending side. * 2) If the reply goes out in clear it will give the * adversary enough information for getting the key in * most of the cases. * * Moreover getting a secure packet when we expect clear * implies that SA's were added without checking for * policy on both ends. This should not happen once ISAKMP * is used to negotiate SAs as SAs will be added only after * verifying the policy. * * NOTE : If the packet was tunneled and not multicast we only send * to it the first match. Unlike TCP and UDP fanouts this doesn't fall * back to delivering packets to AF_INET6 raw sockets. * * IPQoS Notes: * Once we have determined the client, invoke IPPF processing. * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local * ip_policy will be false. * * Zones notes: * Currently only applications in the global zone can create raw sockets for * protocols other than ICMP. So unlike the broadcast / multicast case of * ip_fanout_udp(), we only send a copy of the packet to streams in the * specified zone. For ICMP, this is handled by the callers of icmp_inbound(). */ static void ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags, boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) { queue_t *rq; mblk_t *mp1, *first_mp1; uint_t protocol = ipha->ipha_protocol; ipaddr_t dst; boolean_t one_only; mblk_t *first_mp = mp; boolean_t secure; uint32_t ill_index; conn_t *connp, *first_connp, *next_connp; connf_t *connfp; boolean_t shared_addr; mib2_ipIfStatsEntry_t *mibptr; ip_stack_t *ipst = recv_ill->ill_ipst; ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib; if (mctl_present) { mp = first_mp->b_cont; secure = ipsec_in_is_secure(first_mp); ASSERT(mp != NULL); } else { secure = B_FALSE; } dst = ipha->ipha_dst; /* * If the packet was tunneled and not multicast we only send to it * the first match. */ one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) && !CLASSD(dst)); shared_addr = (zoneid == ALL_ZONES); if (shared_addr) { /* * We don't allow multilevel ports for raw IP, so no need to * check for that here. */ zoneid = tsol_packet_to_zoneid(mp); } connfp = &ipst->ips_ipcl_proto_fanout[protocol]; mutex_enter(&connfp->connf_lock); connp = connfp->connf_head; for (connp = connfp->connf_head; connp != NULL; connp = connp->conn_next) { if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, zoneid) && (!is_system_labeled() || tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp))) { break; } } if (connp == NULL || connp->conn_upq == NULL) { /* * No one bound to these addresses. Is * there a client that wants all * unclaimed datagrams? */ mutex_exit(&connfp->connf_lock); /* * Check for IPPROTO_ENCAP... */ if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) { /* * If an IPsec mblk is here on a multicast * tunnel (using ip_mroute stuff), check policy here, * THEN ship off to ip_mroute_decap(). * * BTW, If I match a configured IP-in-IP * tunnel, this path will not be reached, and * ip_mroute_decap will never be called. */ first_mp = ipsec_check_global_policy(first_mp, connp, ipha, NULL, mctl_present, ipst->ips_netstack); if (first_mp != NULL) { if (mctl_present) freeb(first_mp); ip_mroute_decap(q, mp, ill); } /* Else we already freed everything! */ } else { /* * Otherwise send an ICMP protocol unreachable. */ if (ip_fanout_send_icmp(q, first_mp, flags, ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE, mctl_present, zoneid, ipst)) { BUMP_MIB(mibptr, ipIfStatsInUnknownProtos); } } return; } CONN_INC_REF(connp); first_connp = connp; /* * Only send message to one tunnel driver by immediately * terminating the loop. */ connp = one_only ? NULL : connp->conn_next; for (;;) { while (connp != NULL) { if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags, zoneid) && (!is_system_labeled() || tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp))) break; connp = connp->conn_next; } /* * Copy the packet. */ if (connp == NULL || connp->conn_upq == NULL || (((first_mp1 = dupmsg(first_mp)) == NULL) && ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { /* * No more interested clients or memory * allocation failed */ connp = first_connp; break; } mp1 = mctl_present ? first_mp1->b_cont : first_mp1; CONN_INC_REF(connp); mutex_exit(&connfp->connf_lock); rq = connp->conn_rq; if (!canputnext(rq)) { if (flags & IP_FF_RAWIP) { BUMP_MIB(mibptr, rawipIfStatsInOverflows); } else { BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); } freemsg(first_mp1); } else { /* * Don't enforce here if we're an actual tunnel - * let "tun" do it instead. */ if (!IPCL_IS_IPTUN(connp) && (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) { first_mp1 = ipsec_check_inbound_policy (first_mp1, connp, ipha, NULL, mctl_present); } if (first_mp1 != NULL) { int in_flags = 0; /* * ip_fanout_proto also gets called from * icmp_inbound_error_fanout, in which case * the msg type is M_CTL. Don't add info * in this case for the time being. In future * when there is a need for knowing the * inbound iface index for ICMP error msgs, * then this can be changed. */ if (connp->conn_recvif) in_flags = IPF_RECVIF; /* * The ULP may support IP_RECVPKTINFO for both * IP v4 and v6 so pass the appropriate argument * based on conn IP version. */ if (connp->conn_ip_recvpktinfo) { if (connp->conn_af_isv6) { /* * V6 only needs index */ in_flags |= IPF_RECVIF; } else { /* * V4 needs index + * matching address. */ in_flags |= IPF_RECVADDR; } } if ((in_flags != 0) && (mp->b_datap->db_type != M_CTL)) { /* * the actual data will be * contained in b_cont upon * successful return of the * following call else * original mblk is returned */ ASSERT(recv_ill != NULL); mp1 = ip_add_info(mp1, recv_ill, in_flags, IPCL_ZONEID(connp), ipst); } BUMP_MIB(mibptr, ipIfStatsHCInDelivers); if (mctl_present) freeb(first_mp1); (connp->conn_recv)(connp, mp1, NULL); } } mutex_enter(&connfp->connf_lock); /* Follow the next pointer before releasing the conn. */ next_connp = connp->conn_next; CONN_DEC_REF(connp); connp = next_connp; } /* Last one. Send it upstream. */ mutex_exit(&connfp->connf_lock); /* * If this packet is coming from icmp_inbound_error_fanout ip_policy * will be set to false. */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { ill_index = ill->ill_phyint->phyint_ifindex; ip_process(IPP_LOCAL_IN, &mp, ill_index); if (mp == NULL) { CONN_DEC_REF(connp); if (mctl_present) { freeb(first_mp); } return; } } rq = connp->conn_rq; if (!canputnext(rq)) { if (flags & IP_FF_RAWIP) { BUMP_MIB(mibptr, rawipIfStatsInOverflows); } else { BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows); } freemsg(first_mp); } else { if (IPCL_IS_IPTUN(connp)) { /* * Tunneled packet. We enforce policy in the tunnel * module itself. * * Send the WHOLE packet up (incl. IPSEC_IN) without * a policy check. * FIXME to use conn_recv for tun later. */ putnext(rq, first_mp); CONN_DEC_REF(connp); return; } if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) { first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, NULL, mctl_present); } if (first_mp != NULL) { int in_flags = 0; /* * ip_fanout_proto also gets called * from icmp_inbound_error_fanout, in * which case the msg type is M_CTL. * Don't add info in this case for time * being. In future when there is a * need for knowing the inbound iface * index for ICMP error msgs, then this * can be changed */ if (connp->conn_recvif) in_flags = IPF_RECVIF; if (connp->conn_ip_recvpktinfo) { if (connp->conn_af_isv6) { /* * V6 only needs index */ in_flags |= IPF_RECVIF; } else { /* * V4 needs index + * matching address. */ in_flags |= IPF_RECVADDR; } } if ((in_flags != 0) && (mp->b_datap->db_type != M_CTL)) { /* * the actual data will be contained in * b_cont upon successful return * of the following call else original * mblk is returned */ ASSERT(recv_ill != NULL); mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp), ipst); } BUMP_MIB(mibptr, ipIfStatsHCInDelivers); (connp->conn_recv)(connp, mp, NULL); if (mctl_present) freeb(first_mp); } } CONN_DEC_REF(connp); } /* * Fanout for TCP packets * The caller puts in the ports parameter. * * IPQoS Notes * Before sending it to the client, invoke IPPF processing. * Policy processing takes place only if the callout_position, IPP_LOCAL_IN, * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local * ip_policy is false. */ static void ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha, uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid) { mblk_t *first_mp; boolean_t secure; uint32_t ill_index; int ip_hdr_len; tcph_t *tcph; boolean_t syn_present = B_FALSE; conn_t *connp; ip_stack_t *ipst = recv_ill->ill_ipst; ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; ASSERT(recv_ill != NULL); first_mp = mp; if (mctl_present) { ASSERT(first_mp->b_datap->db_type == M_CTL); mp = first_mp->b_cont; secure = ipsec_in_is_secure(first_mp); ASSERT(mp != NULL); } else { secure = B_FALSE; } ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr); if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid, ipst)) == NULL) { /* * No connected connection or listener. Send a * TH_RST via tcp_xmit_listeners_reset. */ /* Initiate IPPf processing, if needed. */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { uint32_t ill_index; ill_index = recv_ill->ill_phyint->phyint_ifindex; ip_process(IPP_LOCAL_IN, &first_mp, ill_index); if (first_mp == NULL) return; } BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n", zoneid)); tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, ipst->ips_netstack->netstack_tcp, NULL); return; } /* * Allocate the SYN for the TCP connection here itself */ tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { if (IPCL_IS_TCP(connp)) { squeue_t *sqp; /* * For fused tcp loopback, assign the eager's * squeue to be that of the active connect's. * Note that we don't check for IP_FF_LOOPBACK * here since this routine gets called only * for loopback (unlike the IPv6 counterpart). */ ASSERT(Q_TO_CONN(q) != NULL); if (do_tcp_fusion && !CONN_INBOUND_POLICY_PRESENT(connp, ipss) && !secure && !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy && IPCL_IS_TCP(Q_TO_CONN(q))) { ASSERT(Q_TO_CONN(q)->conn_sqp != NULL); sqp = Q_TO_CONN(q)->conn_sqp; } else { sqp = IP_SQUEUE_GET(lbolt); } mp->b_datap->db_struioflag |= STRUIO_EAGER; DB_CKSUMSTART(mp) = (intptr_t)sqp; syn_present = B_TRUE; } } if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); if ((flags & TH_RST) || (flags & TH_URG)) { DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL, void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph); CONN_DEC_REF(connp); freemsg(first_mp); return; } if (flags & TH_ACK) { tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, ipst->ips_netstack->netstack_tcp, connp); CONN_DEC_REF(connp); return; } DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL, void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph); CONN_DEC_REF(connp); freemsg(first_mp); return; } if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, NULL, mctl_present); if (first_mp == NULL) { BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); CONN_DEC_REF(connp); return; } if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { ASSERT(syn_present); if (mctl_present) { ASSERT(first_mp != mp); first_mp->b_datap->db_struioflag |= STRUIO_POLICY; } else { ASSERT(first_mp == mp); mp->b_datap->db_struioflag &= ~STRUIO_EAGER; mp->b_datap->db_struioflag |= STRUIO_POLICY; } } else { /* * Discard first_mp early since we're dealing with a * fully-connected conn_t and tcp doesn't do policy in * this case. */ if (mctl_present) { freeb(first_mp); mctl_present = B_FALSE; } first_mp = mp; } } /* * Initiate policy processing here if needed. If we get here from * icmp_inbound_error_fanout, ip_policy is false. */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { ill_index = recv_ill->ill_phyint->phyint_ifindex; ip_process(IPP_LOCAL_IN, &mp, ill_index); if (mp == NULL) { CONN_DEC_REF(connp); if (mctl_present) freeb(first_mp); return; } else if (mctl_present) { ASSERT(first_mp != mp); first_mp->b_cont = mp; } else { first_mp = mp; } } /* Handle socket options. */ if (!syn_present && connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { /* Add header */ ASSERT(recv_ill != NULL); /* * Since tcp does not support IP_RECVPKTINFO for V4, only pass * IPF_RECVIF. */ mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp), ipst); if (mp == NULL) { BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards); CONN_DEC_REF(connp); if (mctl_present) freeb(first_mp); return; } else if (mctl_present) { /* * ip_add_info might return a new mp. */ ASSERT(first_mp != mp); first_mp->b_cont = mp; } else { first_mp = mp; } } BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers); if (IPCL_IS_TCP(connp)) { /* do not drain, certain use cases can blow the stack */ squeue_enter_nodrain(connp->conn_sqp, first_mp, connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP); } else { /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */ (connp->conn_recv)(connp, first_mp, NULL); CONN_DEC_REF(connp); } } /* * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or * pass it along to ESP if the SPI is non-zero. Returns TRUE if the mblk * is not consumed. * * One of four things can happen, all of which affect the passed-in mblk: * * 1.) ICMP messages that go through here just get returned TRUE. * * 2.) The packet is stock UDP and gets its zero-SPI stripped. Return TRUE. * * 3.) The packet is ESP-in-UDP, gets transformed into an equivalent * ESP packet, and is passed along to ESP for consumption. Return FALSE. * * 4.) The packet is an ESP-in-UDP Keepalive. Drop it and return FALSE. */ static boolean_t zero_spi_check(queue_t *q, mblk_t *mp, ire_t *ire, ill_t *recv_ill, ipsec_stack_t *ipss) { int shift, plen, iph_len; ipha_t *ipha; udpha_t *udpha; uint32_t *spi; uint32_t esp_ports; uint8_t *orptr; boolean_t free_ire; if (DB_TYPE(mp) == M_CTL) { /* * ICMP message with UDP inside. Don't bother stripping, just * send it up. * * NOTE: Any app with UDP_NAT_T_ENDPOINT set is probably going * to ignore errors set by ICMP anyway ('cause they might be * forged), but that's the app's decision, not ours. */ /* Bunch of reality checks for DEBUG kernels... */ ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION); ASSERT(((ipha_t *)mp->b_rptr)->ipha_protocol == IPPROTO_ICMP); return (B_TRUE); } ipha = (ipha_t *)mp->b_rptr; iph_len = IPH_HDR_LENGTH(ipha); plen = ntohs(ipha->ipha_length); if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) { /* * Most likely a keepalive for the benefit of an intervening * NAT. These aren't for us, per se, so drop it. * * RFC 3947/8 doesn't say for sure what to do for 2-3 * byte packets (keepalives are 1-byte), but we'll drop them * also. */ ip_drop_packet(mp, B_TRUE, recv_ill, NULL, DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper); return (B_FALSE); } if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) { /* might as well pull it all up - it might be ESP. */ if (!pullupmsg(mp, -1)) { ip_drop_packet(mp, B_TRUE, recv_ill, NULL, DROPPER(ipss, ipds_esp_nomem), &ipss->ipsec_dropper); return (B_FALSE); } ipha = (ipha_t *)mp->b_rptr; } spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t)); if (*spi == 0) { /* UDP packet - remove 0-spi. */ shift = sizeof (uint32_t); } else { /* ESP-in-UDP packet - reduce to ESP. */ ipha->ipha_protocol = IPPROTO_ESP; shift = sizeof (udpha_t); } /* Fix IP header */ ipha->ipha_length = htons(plen - shift); ipha->ipha_hdr_checksum = 0; orptr = mp->b_rptr; mp->b_rptr += shift; udpha = (udpha_t *)(orptr + iph_len); if (*spi == 0) { ASSERT((uint8_t *)ipha == orptr); udpha->uha_length = htons(plen - shift - iph_len); iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */ esp_ports = 0; } else { esp_ports = *((uint32_t *)udpha); ASSERT(esp_ports != 0); } ovbcopy(orptr, orptr + shift, iph_len); if (esp_ports != 0) /* Punt up for ESP processing. */ { ipha = (ipha_t *)(orptr + shift); free_ire = (ire == NULL); if (free_ire) { /* Re-acquire ire. */ ire = ire_cache_lookup(ipha->ipha_dst, ALL_ZONES, NULL, ipss->ipsec_netstack->netstack_ip); if (ire == NULL || !(ire->ire_type & IRE_LOCAL)) { if (ire != NULL) ire_refrele(ire); /* * Do a regular freemsg(), as this is an IP * error (no local route) not an IPsec one. */ freemsg(mp); } } ip_proto_input(q, mp, ipha, ire, recv_ill, esp_ports); if (free_ire) ire_refrele(ire); } return (esp_ports == 0); } /* * Deliver a udp packet to the given conn, possibly applying ipsec policy. * We are responsible for disposing of mp, such as by freemsg() or putnext() * Caller is responsible for dropping references to the conn, and freeing * first_mp. * * IPQoS Notes * Before sending it to the client, invoke IPPF processing. Policy processing * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and * ip_policy is true. If we get here from icmp_inbound_error_fanout or * ip_wput_local, ip_policy is false. */ static void ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp, boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill, boolean_t ip_policy) { boolean_t mctl_present = (first_mp != NULL); uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */ uint32_t ill_index; ip_stack_t *ipst = recv_ill->ill_ipst; ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; ASSERT(ill != NULL); if (mctl_present) first_mp->b_cont = mp; else first_mp = mp; if (CONN_UDP_FLOWCTLD(connp)) { BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); freemsg(first_mp); return; } if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) { first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, NULL, mctl_present); if (first_mp == NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return; /* Freed by ipsec_check_inbound_policy(). */ } } if (mctl_present) freeb(first_mp); /* Let's hope the compilers utter "branch, predict-not-taken..." ;) */ if (connp->conn_udp->udp_nat_t_endpoint) { if (mctl_present) { /* mctl_present *shouldn't* happen. */ ip_drop_packet(mp, B_TRUE, NULL, NULL, DROPPER(ipss, ipds_esp_nat_t_ipsec), &ipss->ipsec_dropper); return; } if (!zero_spi_check(ill->ill_rq, mp, NULL, recv_ill, ipss)) return; } /* Handle options. */ if (connp->conn_recvif) in_flags = IPF_RECVIF; /* * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag * passed to ip_add_info is based on IP version of connp. */ if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) { if (connp->conn_af_isv6) { /* * V6 only needs index */ in_flags |= IPF_RECVIF; } else { /* * V4 needs index + matching address. */ in_flags |= IPF_RECVADDR; } } if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA)) in_flags |= IPF_RECVSLLA; /* * Initiate IPPF processing here, if needed. Note first_mp won't be * freed if the packet is dropped. The caller will do so. */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) { ill_index = recv_ill->ill_phyint->phyint_ifindex; ip_process(IPP_LOCAL_IN, &mp, ill_index); if (mp == NULL) { return; } } if ((in_flags != 0) && (mp->b_datap->db_type != M_CTL)) { /* * The actual data will be contained in b_cont * upon successful return of the following call * else original mblk is returned */ ASSERT(recv_ill != NULL); mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp), ipst); } BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); /* Send it upstream */ (connp->conn_recv)(connp, mp, NULL); } /* * Fanout for UDP packets. * The caller puts in the ports parameter. * * If SO_REUSEADDR is set all multicast and broadcast packets * will be delivered to all streams bound to the same port. * * Zones notes: * Multicast and broadcast packets will be distributed to streams in all zones. * In the special case where an AF_INET socket binds to 0.0.0.0/ and an * AF_INET6 socket binds to ::/, only the AF_INET socket receives the IPv4 * packets. To maintain this behavior with multiple zones, the conns are grouped * by zone and the SO_REUSEADDR flag is checked for the first matching conn in * each zone. If unset, all the following conns in the same zone are skipped. */ static void ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid) { uint32_t dstport, srcport; ipaddr_t dst; mblk_t *first_mp; boolean_t secure; in6_addr_t v6src; conn_t *connp; connf_t *connfp; conn_t *first_connp; conn_t *next_connp; mblk_t *mp1, *first_mp1; ipaddr_t src; zoneid_t last_zoneid; boolean_t reuseaddr; boolean_t shared_addr; boolean_t unlabeled; ip_stack_t *ipst; ASSERT(recv_ill != NULL); ipst = recv_ill->ill_ipst; first_mp = mp; if (mctl_present) { mp = first_mp->b_cont; first_mp->b_cont = NULL; secure = ipsec_in_is_secure(first_mp); ASSERT(mp != NULL); } else { first_mp = NULL; secure = B_FALSE; } /* Extract ports in net byte order */ dstport = htons(ntohl(ports) & 0xFFFF); srcport = htons(ntohl(ports) >> 16); dst = ipha->ipha_dst; src = ipha->ipha_src; unlabeled = B_FALSE; if (is_system_labeled()) /* Cred cannot be null on IPv4 */ unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags & TSLF_UNLABELED) != 0; shared_addr = (zoneid == ALL_ZONES); if (shared_addr) { /* * No need to handle exclusive-stack zones since ALL_ZONES * only applies to the shared stack. */ zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); /* * If no shared MLP is found, tsol_mlp_findzone returns * ALL_ZONES. In that case, we assume it's SLP, and * search for the zone based on the packet label. * * If there is such a zone, we prefer to find a * connection in it. Otherwise, we look for a * MAC-exempt connection in any zone whose label * dominates the default label on the packet. */ if (zoneid == ALL_ZONES) zoneid = tsol_packet_to_zoneid(mp); else unlabeled = B_FALSE; } connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; mutex_enter(&connfp->connf_lock); connp = connfp->connf_head; if (!broadcast && !CLASSD(dst)) { /* * Not broadcast or multicast. Send to the one (first) * client we find. No need to check conn_wantpacket() * since IP_BOUND_IF/conn_incoming_ill does not apply to * IPv4 unicast packets. */ while ((connp != NULL) && (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) || (!IPCL_ZONE_MATCH(connp, zoneid) && !(unlabeled && connp->conn_mac_exempt)))) { /* * We keep searching since the conn did not match, * or its zone did not match and it is not either * an allzones conn or a mac exempt conn (if the * sender is unlabeled.) */ connp = connp->conn_next; } if (connp == NULL || connp->conn_upq == NULL) goto notfound; if (is_system_labeled() && !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp)) goto notfound; CONN_INC_REF(connp); mutex_exit(&connfp->connf_lock); ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, recv_ill, ip_policy); IP_STAT(ipst, ip_udp_fannorm); CONN_DEC_REF(connp); return; } /* * Broadcast and multicast case * * Need to check conn_wantpacket(). * If SO_REUSEADDR has been set on the first we send the * packet to all clients that have joined the group and * match the port. */ while (connp != NULL) { if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) && conn_wantpacket(connp, ill, ipha, flags, zoneid) && (!is_system_labeled() || tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp))) break; connp = connp->conn_next; } if (connp == NULL || connp->conn_upq == NULL) goto notfound; first_connp = connp; /* * When SO_REUSEADDR is not set, send the packet only to the first * matching connection in its zone by keeping track of the zoneid. */ reuseaddr = first_connp->conn_reuseaddr; last_zoneid = first_connp->conn_zoneid; CONN_INC_REF(connp); connp = connp->conn_next; for (;;) { while (connp != NULL) { if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) && (reuseaddr || connp->conn_zoneid != last_zoneid) && conn_wantpacket(connp, ill, ipha, flags, zoneid) && (!is_system_labeled() || tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp))) break; connp = connp->conn_next; } /* * Just copy the data part alone. The mctl part is * needed just for verifying policy and it is never * sent up. */ if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && ((mp1 = copymsg(mp)) == NULL))) { /* * No more interested clients or memory * allocation failed */ connp = first_connp; break; } if (connp->conn_zoneid != last_zoneid) { /* * Update the zoneid so that the packet isn't sent to * any more conns in the same zone unless SO_REUSEADDR * is set. */ reuseaddr = connp->conn_reuseaddr; last_zoneid = connp->conn_zoneid; } if (first_mp != NULL) { ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> ipsec_info_type == IPSEC_IN); first_mp1 = ipsec_in_tag(first_mp, NULL, ipst->ips_netstack); if (first_mp1 == NULL) { freemsg(mp1); connp = first_connp; break; } } else { first_mp1 = NULL; } CONN_INC_REF(connp); mutex_exit(&connfp->connf_lock); /* * IPQoS notes: We don't send the packet for policy * processing here, will do it for the last one (below). * i.e. we do it per-packet now, but if we do policy * processing per-conn, then we would need to do it * here too. */ ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, ipha, flags, recv_ill, B_FALSE); mutex_enter(&connfp->connf_lock); /* Follow the next pointer before releasing the conn. */ next_connp = connp->conn_next; IP_STAT(ipst, ip_udp_fanmb); CONN_DEC_REF(connp); connp = next_connp; } /* Last one. Send it upstream. */ mutex_exit(&connfp->connf_lock); ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, recv_ill, ip_policy); IP_STAT(ipst, ip_udp_fanmb); CONN_DEC_REF(connp); return; notfound: mutex_exit(&connfp->connf_lock); IP_STAT(ipst, ip_udp_fanothers); /* * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses * have already been matched above, since they live in the IPv4 * fanout tables. This implies we only need to * check for IPv6 in6addr_any endpoints here. * Thus we compare using ipv6_all_zeros instead of the destination * address, except for the multicast group membership lookup which * uses the IPv4 destination. */ IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src); connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)]; mutex_enter(&connfp->connf_lock); connp = connfp->connf_head; if (!broadcast && !CLASSD(dst)) { while (connp != NULL) { if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) && conn_wantpacket(connp, ill, ipha, flags, zoneid) && !connp->conn_ipv6_v6only) break; connp = connp->conn_next; } if (connp != NULL && is_system_labeled() && !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp)) connp = NULL; if (connp == NULL || connp->conn_upq == NULL) { /* * No one bound to this port. Is * there a client that wants all * unclaimed datagrams? */ mutex_exit(&connfp->connf_lock); if (mctl_present) first_mp->b_cont = mp; else first_mp = mp; if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP]. connf_head != NULL) { ip_fanout_proto(q, first_mp, ill, ipha, flags | IP_FF_RAWIP, mctl_present, ip_policy, recv_ill, zoneid); } else { if (ip_fanout_send_icmp(q, first_mp, flags, ICMP_DEST_UNREACHABLE, ICMP_PORT_UNREACHABLE, mctl_present, zoneid, ipst)) { BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); } } return; } CONN_INC_REF(connp); mutex_exit(&connfp->connf_lock); ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, recv_ill, ip_policy); CONN_DEC_REF(connp); return; } /* * IPv4 multicast packet being delivered to an AF_INET6 * in6addr_any endpoint. * Need to check conn_wantpacket(). Note that we use conn_wantpacket() * and not conn_wantpacket_v6() since any multicast membership is * for an IPv4-mapped multicast address. * The packet is sent to all clients in all zones that have joined the * group and match the port. */ while (connp != NULL) { if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, srcport, v6src) && conn_wantpacket(connp, ill, ipha, flags, zoneid) && (!is_system_labeled() || tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp))) break; connp = connp->conn_next; } if (connp == NULL || connp->conn_upq == NULL) { /* * No one bound to this port. Is * there a client that wants all * unclaimed datagrams? */ mutex_exit(&connfp->connf_lock); if (mctl_present) first_mp->b_cont = mp; else first_mp = mp; if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head != NULL) { ip_fanout_proto(q, first_mp, ill, ipha, flags | IP_FF_RAWIP, mctl_present, ip_policy, recv_ill, zoneid); } else { /* * We used to attempt to send an icmp error here, but * since this is known to be a multicast packet * and we don't send icmp errors in response to * multicast, just drop the packet and give up sooner. */ BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts); freemsg(first_mp); } return; } first_connp = connp; CONN_INC_REF(connp); connp = connp->conn_next; for (;;) { while (connp != NULL) { if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros, srcport, v6src) && conn_wantpacket(connp, ill, ipha, flags, zoneid) && (!is_system_labeled() || tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr, connp))) break; connp = connp->conn_next; } /* * Just copy the data part alone. The mctl part is * needed just for verifying policy and it is never * sent up. */ if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) && ((mp1 = copymsg(mp)) == NULL))) { /* * No more intested clients or memory * allocation failed */ connp = first_connp; break; } if (first_mp != NULL) { ASSERT(((ipsec_info_t *)first_mp->b_rptr)-> ipsec_info_type == IPSEC_IN); first_mp1 = ipsec_in_tag(first_mp, NULL, ipst->ips_netstack); if (first_mp1 == NULL) { freemsg(mp1); connp = first_connp; break; } } else { first_mp1 = NULL; } CONN_INC_REF(connp); mutex_exit(&connfp->connf_lock); /* * IPQoS notes: We don't send the packet for policy * processing here, will do it for the last one (below). * i.e. we do it per-packet now, but if we do policy * processing per-conn, then we would need to do it * here too. */ ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill, ipha, flags, recv_ill, B_FALSE); mutex_enter(&connfp->connf_lock); /* Follow the next pointer before releasing the conn. */ next_connp = connp->conn_next; CONN_DEC_REF(connp); connp = next_connp; } /* Last one. Send it upstream. */ mutex_exit(&connfp->connf_lock); ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags, recv_ill, ip_policy); CONN_DEC_REF(connp); } /* * Complete the ip_wput header so that it * is possible to generate ICMP * errors. */ int ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst) { ire_t *ire; if (ipha->ipha_src == INADDR_ANY) { ire = ire_lookup_local(zoneid, ipst); if (ire == NULL) { ip1dbg(("ip_hdr_complete: no source IRE\n")); return (1); } ipha->ipha_src = ire->ire_addr; ire_refrele(ire); } ipha->ipha_ttl = ipst->ips_ip_def_ttl; ipha->ipha_hdr_checksum = 0; ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); return (0); } /* * Nobody should be sending * packets up this stream */ static void ip_lrput(queue_t *q, mblk_t *mp) { mblk_t *mp1; switch (mp->b_datap->db_type) { case M_FLUSH: /* Turn around */ if (*mp->b_rptr & FLUSHW) { *mp->b_rptr &= ~FLUSHR; qreply(q, mp); return; } break; } /* Could receive messages that passed through ar_rput */ for (mp1 = mp; mp1; mp1 = mp1->b_cont) mp1->b_prev = mp1->b_next = NULL; freemsg(mp); } /* Nobody should be sending packets down this stream */ /* ARGSUSED */ void ip_lwput(queue_t *q, mblk_t *mp) { freemsg(mp); } /* * Move the first hop in any source route to ipha_dst and remove that part of * the source route. Called by other protocols. Errors in option formatting * are ignored - will be handled by ip_wput_options Return the final * destination (either ipha_dst or the last entry in a source route.) */ ipaddr_t ip_massage_options(ipha_t *ipha, netstack_t *ns) { ipoptp_t opts; uchar_t *opt; uint8_t optval; uint8_t optlen; ipaddr_t dst; int i; ire_t *ire; ip_stack_t *ipst = ns->netstack_ip; ip2dbg(("ip_massage_options\n")); dst = ipha->ipha_dst; for (optval = ipoptp_first(&opts, ipha); optval != IPOPT_EOL; optval = ipoptp_next(&opts)) { opt = opts.ipoptp_cur; switch (optval) { uint8_t off; case IPOPT_SSRR: case IPOPT_LSRR: if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { ip1dbg(("ip_massage_options: bad src route\n")); break; } optlen = opts.ipoptp_len; off = opt[IPOPT_OFFSET]; off--; redo_srr: if (optlen < IP_ADDR_LEN || off > optlen - IP_ADDR_LEN) { /* End of source route */ ip1dbg(("ip_massage_options: end of SR\n")); break; } bcopy((char *)opt + off, &dst, IP_ADDR_LEN); ip1dbg(("ip_massage_options: next hop 0x%x\n", ntohl(dst))); /* * Check if our address is present more than * once as consecutive hops in source route. * XXX verify per-interface ip_forwarding * for source route? */ ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (ire != NULL) { ire_refrele(ire); off += IP_ADDR_LEN; goto redo_srr; } if (dst == htonl(INADDR_LOOPBACK)) { ip1dbg(("ip_massage_options: loopback addr in " "source route!\n")); break; } /* * Update ipha_dst to be the first hop and remove the * first hop from the source route (by overwriting * part of the option with NOP options). */ ipha->ipha_dst = dst; /* Put the last entry in dst */ off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) + 3; bcopy(&opt[off], &dst, IP_ADDR_LEN); ip1dbg(("ip_massage_options: last hop 0x%x\n", ntohl(dst))); /* Move down and overwrite */ opt[IP_ADDR_LEN] = opt[0]; opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN; opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET]; for (i = 0; i < IP_ADDR_LEN; i++) opt[i] = IPOPT_NOP; break; } } return (dst); } /* * Return the network mask * associated with the specified address. */ ipaddr_t ip_net_mask(ipaddr_t addr) { uchar_t *up = (uchar_t *)&addr; ipaddr_t mask = 0; uchar_t *maskp = (uchar_t *)&mask; #if defined(__i386) || defined(__amd64) #define TOTALLY_BRAIN_DAMAGED_C_COMPILER #endif #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0; #endif if (CLASSD(addr)) { maskp[0] = 0xF0; return (mask); } /* We assume Class E default netmask to be 32 */ if (CLASSE(addr)) return (0xffffffffU); if (addr == 0) return (0); maskp[0] = 0xFF; if ((up[0] & 0x80) == 0) return (mask); maskp[1] = 0xFF; if ((up[0] & 0xC0) == 0x80) return (mask); maskp[2] = 0xFF; if ((up[0] & 0xE0) == 0xC0) return (mask); /* Otherwise return no mask */ return ((ipaddr_t)0); } /* * Select an ill for the packet by considering load spreading across * a different ill in the group if dst_ill is part of some group. */ ill_t * ip_newroute_get_dst_ill(ill_t *dst_ill) { ill_t *ill; /* * We schedule irrespective of whether the source address is * INADDR_ANY or not. illgrp_scheduler returns a held ill. */ ill = illgrp_scheduler(dst_ill); if (ill == NULL) return (NULL); /* * For groups with names ip_sioctl_groupname ensures that all * ills are of same type. For groups without names, ifgrp_insert * ensures this. */ ASSERT(dst_ill->ill_type == ill->ill_type); return (ill); } /* * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case. */ ill_t * ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6, ip_stack_t *ipst) { ill_t *ret_ill; ASSERT(ifindex != 0); ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL, ipst); if (ret_ill == NULL || (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) { if (isv6) { if (ill != NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); } else { BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutDiscards); } ip1dbg(("ip_grab_attach_ill (IPv6): " "bad ifindex %d.\n", ifindex)); } else { if (ill != NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards); } else { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); } ip1dbg(("ip_grab_attach_ill (IPv4): " "bad ifindex %d.\n", ifindex)); } if (ret_ill != NULL) ill_refrele(ret_ill); freemsg(first_mp); return (NULL); } return (ret_ill); } /* * IPv4 - * ip_newroute is called by ip_rput or ip_wput whenever we need to send * out a packet to a destination address for which we do not have specific * (or sufficient) routing information. * * NOTE : These are the scopes of some of the variables that point at IRE, * which needs to be followed while making any future modifications * to avoid memory leaks. * * - ire and sire are the entries looked up initially by * ire_ftable_lookup. * - ipif_ire is used to hold the interface ire associated with * the new cache ire. But it's scope is limited, so we always REFRELE * it before branching out to error paths. * - save_ire is initialized before ire_create, so that ire returned * by ire_create will not over-write the ire. We REFRELE save_ire * before breaking out of the switch. * * Thus on failures, we have to REFRELE only ire and sire, if they * are not NULL. */ void ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, conn_t *connp, zoneid_t zoneid, ip_stack_t *ipst) { areq_t *areq; ipaddr_t gw = 0; ire_t *ire = NULL; mblk_t *res_mp; ipaddr_t *addrp; ipaddr_t nexthop_addr; ipif_t *src_ipif = NULL; ill_t *dst_ill = NULL; ipha_t *ipha; ire_t *sire = NULL; mblk_t *first_mp; ire_t *save_ire; ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */ ushort_t ire_marks = 0; boolean_t mctl_present; ipsec_out_t *io; mblk_t *saved_mp; ire_t *first_sire = NULL; mblk_t *copy_mp = NULL; mblk_t *xmit_mp = NULL; ipaddr_t save_dst; uint32_t multirt_flags = MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; boolean_t multirt_is_resolvable; boolean_t multirt_resolve_next; boolean_t unspec_src; boolean_t do_attach_ill = B_FALSE; boolean_t ip_nexthop = B_FALSE; tsol_ire_gw_secattr_t *attrp = NULL; tsol_gcgrp_t *gcgrp = NULL; tsol_gcgrp_addr_t ga; if (ip_debug > 2) { /* ip1dbg */ pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst); } EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (mctl_present) { io = (ipsec_out_t *)first_mp->b_rptr; ASSERT(io->ipsec_out_type == IPSEC_OUT); ASSERT(zoneid == io->ipsec_out_zoneid); ASSERT(zoneid != ALL_ZONES); } ipha = (ipha_t *)mp->b_rptr; /* All multicast lookups come through ip_newroute_ipif() */ if (CLASSD(dst)) { ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n", ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next)); freemsg(first_mp); return; } if (mctl_present && io->ipsec_out_attach_if) { /* ip_grab_attach_ill returns a held ill */ attach_ill = ip_grab_attach_ill(NULL, first_mp, io->ipsec_out_ill_index, B_FALSE, ipst); /* Failure case frees things for us. */ if (attach_ill == NULL) return; /* * Check if we need an ire that will not be * looked up by anybody else i.e. HIDDEN. */ if (ill_is_probeonly(attach_ill)) ire_marks = IRE_MARK_HIDDEN; } if (mctl_present && io->ipsec_out_ip_nexthop) { ip_nexthop = B_TRUE; nexthop_addr = io->ipsec_out_nexthop_addr; } /* * If this IRE is created for forwarding or it is not for * traffic for congestion controlled protocols, mark it as temporary. */ if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol)) ire_marks |= IRE_MARK_TEMPORARY; /* * Get what we can from ire_ftable_lookup which will follow an IRE * chain until it gets the most specific information available. * For example, we know that there is no IRE_CACHE for this dest, * but there may be an IRE_OFFSUBNET which specifies a gateway. * ire_ftable_lookup will look up the gateway, etc. * Otherwise, given ire_ftable_lookup algorithm, only one among routes * to the destination, of equal netmask length in the forward table, * will be recursively explored. If no information is available * for the final gateway of that route, we force the returned ire * to be equal to sire using MATCH_IRE_PARENT. * At least, in this case we have a starting point (in the buckets) * to look for other routes to the destination in the forward table. * This is actually used only for multirouting, where a list * of routes has to be processed in sequence. * * In the process of coming up with the most specific information, * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry * for the gateway (i.e., one for which the ire_nce->nce_state is * not yet ND_REACHABLE, and is in the middle of arp resolution). * Two caveats when handling incomplete ire's in ip_newroute: * - we should be careful when accessing its ire_nce (specifically * the nce_res_mp) ast it might change underneath our feet, and, * - not all legacy code path callers are prepared to handle * incomplete ire's, so we should not create/add incomplete * ire_cache entries here. (See discussion about temporary solution * further below). * * In order to minimize packet dropping, and to preserve existing * behavior, we treat this case as if there were no IRE_CACHE for the * gateway, and instead use the IF_RESOLVER ire to send out * another request to ARP (this is achieved by passing the * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the * arp response comes back in ip_wput_nondata, we will create * a per-dst ire_cache that has an ND_COMPLETE ire. * * Note that this is a temporary solution; the correct solution is * to create an incomplete per-dst ire_cache entry, and send the * packet out when the gw's nce is resolved. In order to achieve this, * all packet processing must have been completed prior to calling * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need * to be modified to accomodate this solution. */ if (ip_nexthop) { /* * The first time we come here, we look for an IRE_INTERFACE * entry for the specified nexthop, set the dst to be the * nexthop address and create an IRE_CACHE entry for the * nexthop. The next time around, we are able to find an * IRE_CACHE entry for the nexthop, set the gateway to be the * nexthop address and create an IRE_CACHE entry for the * destination address via the specified nexthop. */ ire = ire_cache_lookup(nexthop_addr, zoneid, MBLK_GETLABEL(mp), ipst); if (ire != NULL) { gw = nexthop_addr; ire_marks |= IRE_MARK_PRIVATE_ADDR; } else { ire = ire_ftable_lookup(nexthop_addr, 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, MBLK_GETLABEL(mp), MATCH_IRE_TYPE | MATCH_IRE_SECATTR, ipst); if (ire != NULL) { dst = nexthop_addr; } } } else if (attach_ill == NULL) { ire = ire_ftable_lookup(dst, 0, 0, 0, NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT | MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE, ipst); } else { /* * attach_ill is set only for communicating with * on-link hosts. So, don't look for DEFAULT. */ ipif_t *attach_ipif; attach_ipif = ipif_get_next_ipif(NULL, attach_ill); if (attach_ipif == NULL) { ill_refrele(attach_ill); goto icmp_err_ret; } ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif, &sire, zoneid, 0, MBLK_GETLABEL(mp), MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL | MATCH_IRE_SECATTR, ipst); ipif_refrele(attach_ipif); } ip3dbg(("ip_newroute: ire_ftable_lookup() " "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); /* * This loop is run only once in most cases. * We loop to resolve further routes only when the destination * can be reached through multiple RTF_MULTIRT-flagged ires. */ do { /* Clear the previous iteration's values */ if (src_ipif != NULL) { ipif_refrele(src_ipif); src_ipif = NULL; } if (dst_ill != NULL) { ill_refrele(dst_ill); dst_ill = NULL; } multirt_resolve_next = B_FALSE; /* * We check if packets have to be multirouted. * In this case, given the current couple, * we look for the next suitable . * This check is done in ire_multirt_lookup(), * which applies various criteria to find the next route * to resolve. ire_multirt_lookup() leaves * unchanged if it detects it has not been tried yet. */ if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { ip3dbg(("ip_newroute: starting next_resolution " "with first_mp %p, tag %d\n", (void *)first_mp, MULTIRT_DEBUG_TAGGED(first_mp))); ASSERT(sire != NULL); multirt_is_resolvable = ire_multirt_lookup(&ire, &sire, multirt_flags, MBLK_GETLABEL(mp), ipst); ip3dbg(("ip_newroute: multirt_is_resolvable %d, " "ire %p, sire %p\n", multirt_is_resolvable, (void *)ire, (void *)sire)); if (!multirt_is_resolvable) { /* * No more multirt route to resolve; give up * (all routes resolved or no more * resolvable routes). */ if (ire != NULL) { ire_refrele(ire); ire = NULL; } } else { ASSERT(sire != NULL); ASSERT(ire != NULL); /* * We simply use first_sire as a flag that * indicates if a resolvable multirt route * has already been found. * If it is not the case, we may have to send * an ICMP error to report that the * destination is unreachable. * We do not IRE_REFHOLD first_sire. */ if (first_sire == NULL) { first_sire = sire; } } } if (ire == NULL) { if (ip_debug > 3) { /* ip2dbg */ pr_addr_dbg("ip_newroute: " "can't resolve %s\n", AF_INET, &dst); } ip3dbg(("ip_newroute: " "ire %p, sire %p, first_sire %p\n", (void *)ire, (void *)sire, (void *)first_sire)); if (sire != NULL) { ire_refrele(sire); sire = NULL; } if (first_sire != NULL) { /* * At least one multirt route has been found * in the same call to ip_newroute(); * there is no need to report an ICMP error. * first_sire was not IRE_REFHOLDed. */ MULTIRT_DEBUG_UNTAG(first_mp); freemsg(first_mp); return; } ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0, RTA_DST, ipst); if (attach_ill != NULL) ill_refrele(attach_ill); goto icmp_err_ret; } /* * Verify that the returned IRE does not have either * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. */ if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) { if (attach_ill != NULL) ill_refrele(attach_ill); goto icmp_err_ret; } /* * Increment the ire_ob_pkt_count field for ire if it is an * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and * increment the same for the parent IRE, sire, if it is some * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST) */ if ((ire->ire_type & IRE_INTERFACE) != 0) { UPDATE_OB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; } if (sire != NULL) { gw = sire->ire_gateway_addr; ASSERT((sire->ire_type & (IRE_CACHETABLE | IRE_INTERFACE)) == 0); UPDATE_OB_PKT_COUNT(sire); sire->ire_last_used_time = lbolt; } /* * We have a route to reach the destination. * * 1) If the interface is part of ill group, try to get a new * ill taking load spreading into account. * * 2) After selecting the ill, get a source address that * might create good inbound load spreading. * ipif_select_source does this for us. * * If the application specified the ill (ifindex), we still * load spread. Only if the packets needs to go out * specifically on a given ill e.g. binding to * IPIF_NOFAILOVER address, then we don't try to use a * different ill for load spreading. */ if (attach_ill == NULL) { /* * Don't perform outbound load spreading in the * case of an RTF_MULTIRT route, as we actually * typically want to replicate outgoing packets * through particular interfaces. */ if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { dst_ill = ire->ire_ipif->ipif_ill; /* for uniformity */ ill_refhold(dst_ill); } else { /* * If we are here trying to create an IRE_CACHE * for an offlink destination and have the * IRE_CACHE for the next hop and the latter is * using virtual IP source address selection i.e * it's ire->ire_ipif is pointing to a virtual * network interface (vni) then * ip_newroute_get_dst_ll() will return the vni * interface as the dst_ill. Since the vni is * virtual i.e not associated with any physical * interface, it cannot be the dst_ill, hence * in such a case call ip_newroute_get_dst_ll() * with the stq_ill instead of the ire_ipif ILL. * The function returns a refheld ill. */ if ((ire->ire_type == IRE_CACHE) && IS_VNI(ire->ire_ipif->ipif_ill)) dst_ill = ip_newroute_get_dst_ill( ire->ire_stq->q_ptr); else dst_ill = ip_newroute_get_dst_ill( ire->ire_ipif->ipif_ill); } if (dst_ill == NULL) { if (ip_debug > 2) { pr_addr_dbg("ip_newroute: " "no dst ill for dst" " %s\n", AF_INET, &dst); } goto icmp_err_ret; } } else { dst_ill = ire->ire_ipif->ipif_ill; /* for uniformity */ ill_refhold(dst_ill); /* * We should have found a route matching ill as we * called ire_ftable_lookup with MATCH_IRE_ILL. * Rather than asserting, when there is a mismatch, * we just drop the packet. */ if (dst_ill != attach_ill) { ip0dbg(("ip_newroute: Packet dropped as " "IPIF_NOFAILOVER ill is %s, " "ire->ire_ipif->ipif_ill is %s\n", attach_ill->ill_name, dst_ill->ill_name)); ill_refrele(attach_ill); goto icmp_err_ret; } } /* attach_ill can't go in loop. IPMP and CGTP are disjoint */ if (attach_ill != NULL) { ill_refrele(attach_ill); attach_ill = NULL; do_attach_ill = B_TRUE; } ASSERT(dst_ill != NULL); ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name)); /* * Pick the best source address from dst_ill. * * 1) If it is part of a multipathing group, we would * like to spread the inbound packets across different * interfaces. ipif_select_source picks a random source * across the different ills in the group. * * 2) If it is not part of a multipathing group, we try * to pick the source address from the destination * route. Clustering assumes that when we have multiple * prefixes hosted on an interface, the prefix of the * source address matches the prefix of the destination * route. We do this only if the address is not * DEPRECATED. * * 3) If the conn is in a different zone than the ire, we * need to pick a source address from the right zone. * * NOTE : If we hit case (1) above, the prefix of the source * address picked may not match the prefix of the * destination routes prefix as ipif_select_source * does not look at "dst" while picking a source * address. * If we want the same behavior as (2), we will need * to change the behavior of ipif_select_source. */ ASSERT(src_ipif == NULL); if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { /* * The RTF_SETSRC flag is set in the parent ire (sire). * Check that the ipif matching the requested source * address still exists. */ src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL, zoneid, NULL, NULL, NULL, NULL, ipst); } unspec_src = (connp != NULL && connp->conn_unspec_src); if (src_ipif == NULL && (!unspec_src || ipha->ipha_src != INADDR_ANY)) { ire_marks |= IRE_MARK_USESRC_CHECK; if ((dst_ill->ill_group != NULL) || (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) || (connp != NULL && ire->ire_zoneid != zoneid && ire->ire_zoneid != ALL_ZONES) || (dst_ill->ill_usesrc_ifindex != 0)) { /* * If the destination is reachable via a * given gateway, the selected source address * should be in the same subnet as the gateway. * Otherwise, the destination is not reachable. * * If there are no interfaces on the same subnet * as the destination, ipif_select_source gives * first non-deprecated interface which might be * on a different subnet than the gateway. * This is not desirable. Hence pass the dst_ire * source address to ipif_select_source. * It is sure that the destination is reachable * with the dst_ire source address subnet. * So passing dst_ire source address to * ipif_select_source will make sure that the * selected source will be on the same subnet * as dst_ire source address. */ ipaddr_t saddr = ire->ire_ipif->ipif_src_addr; src_ipif = ipif_select_source(dst_ill, saddr, zoneid); if (src_ipif == NULL) { if (ip_debug > 2) { pr_addr_dbg("ip_newroute: " "no src for dst %s ", AF_INET, &dst); printf("through interface %s\n", dst_ill->ill_name); } goto icmp_err_ret; } } else { src_ipif = ire->ire_ipif; ASSERT(src_ipif != NULL); /* hold src_ipif for uniformity */ ipif_refhold(src_ipif); } } /* * Assign a source address while we have the conn. * We can't have ip_wput_ire pick a source address when the * packet returns from arp since we need to look at * conn_unspec_src and conn_zoneid, and we lose the conn when * going through arp. * * NOTE : ip_newroute_v6 does not have this piece of code as * it uses ip6i to store this information. */ if (ipha->ipha_src == INADDR_ANY && !unspec_src) ipha->ipha_src = src_ipif->ipif_src_addr; if (ip_debug > 3) { /* ip2dbg */ pr_addr_dbg("ip_newroute: first hop %s\n", AF_INET, &gw); } ip2dbg(("\tire type %s (%d)\n", ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); /* * The TTL of multirouted packets is bounded by the * ip_multirt_ttl ndd variable. */ if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { /* Force TTL of multirouted packets */ if ((ipst->ips_ip_multirt_ttl > 0) && (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) { ip2dbg(("ip_newroute: forcing multirt TTL " "to %d (was %d), dst 0x%08x\n", ipst->ips_ip_multirt_ttl, ipha->ipha_ttl, ntohl(sire->ire_addr))); ipha->ipha_ttl = ipst->ips_ip_multirt_ttl; } } /* * At this point in ip_newroute(), ire is either the * IRE_CACHE of the next-hop gateway for an off-subnet * destination or an IRE_INTERFACE type that should be used * to resolve an on-subnet destination or an on-subnet * next-hop gateway. * * In the IRE_CACHE case, we have the following : * * 1) src_ipif - used for getting a source address. * * 2) dst_ill - from which we derive ire_stq/ire_rfq. This * means packets using this IRE_CACHE will go out on * dst_ill. * * 3) The IRE sire will point to the prefix that is the * longest matching route for the destination. These * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST. * * The newly created IRE_CACHE entry for the off-subnet * destination is tied to both the prefix route and the * interface route used to resolve the next-hop gateway * via the ire_phandle and ire_ihandle fields, * respectively. * * In the IRE_INTERFACE case, we have the following : * * 1) src_ipif - used for getting a source address. * * 2) dst_ill - from which we derive ire_stq/ire_rfq. This * means packets using the IRE_CACHE that we will build * here will go out on dst_ill. * * 3) sire may or may not be NULL. But, the IRE_CACHE that is * to be created will only be tied to the IRE_INTERFACE * that was derived from the ire_ihandle field. * * If sire is non-NULL, it means the destination is * off-link and we will first create the IRE_CACHE for the * gateway. Next time through ip_newroute, we will create * the IRE_CACHE for the final destination as described * above. * * In both cases, after the current resolution has been * completed (or possibly initialised, in the IRE_INTERFACE * case), the loop may be re-entered to attempt the resolution * of another RTF_MULTIRT route. * * When an IRE_CACHE entry for the off-subnet destination is * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire, * for further processing in emission loops. */ save_ire = ire; switch (ire->ire_type) { case IRE_CACHE: { ire_t *ipif_ire; ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE); if (gw == 0) gw = ire->ire_gateway_addr; /* * We need 3 ire's to create a new cache ire for an * off-link destination from the cache ire of the * gateway. * * 1. The prefix ire 'sire' (Note that this does * not apply to the conn_nexthop_set case) * 2. The cache ire of the gateway 'ire' * 3. The interface ire 'ipif_ire' * * We have (1) and (2). We lookup (3) below. * * If there is no interface route to the gateway, * it is a race condition, where we found the cache * but the interface route has been deleted. */ if (ip_nexthop) { ipif_ire = ire_ihandle_lookup_onlink(ire); } else { ipif_ire = ire_ihandle_lookup_offlink(ire, sire); } if (ipif_ire == NULL) { ip1dbg(("ip_newroute: " "ire_ihandle_lookup_offlink failed\n")); goto icmp_err_ret; } /* * Check cached gateway IRE for any security * attributes; if found, associate the gateway * credentials group to the destination IRE. */ if ((attrp = save_ire->ire_gw_secattr) != NULL) { mutex_enter(&attrp->igsa_lock); if ((gcgrp = attrp->igsa_gcgrp) != NULL) GCGRP_REFHOLD(gcgrp); mutex_exit(&attrp->igsa_lock); } /* * XXX For the source of the resolver mp, * we are using the same DL_UNITDATA_REQ * (from save_ire->ire_nce->nce_res_mp) * though the save_ire is not pointing at the same ill. * This is incorrect. We need to send it up to the * resolver to get the right res_mp. For ethernets * this may be okay (ill_type == DL_ETHER). */ ire = ire_create( (uchar_t *)&dst, /* dest address */ (uchar_t *)&ip_g_all_ones, /* mask */ (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ (uchar_t *)&gw, /* gateway address */ &save_ire->ire_max_frag, save_ire->ire_nce, /* src nce */ dst_ill->ill_rq, /* recv-from queue */ dst_ill->ill_wq, /* send-to queue */ IRE_CACHE, /* IRE type */ src_ipif, (sire != NULL) ? sire->ire_mask : 0, /* Parent mask */ (sire != NULL) ? sire->ire_phandle : 0, /* Parent handle */ ipif_ire->ire_ihandle, /* Interface handle */ (sire != NULL) ? (sire->ire_flags & (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */ (sire != NULL) ? &(sire->ire_uinfo) : &(save_ire->ire_uinfo), NULL, gcgrp, ipst); if (ire == NULL) { if (gcgrp != NULL) { GCGRP_REFRELE(gcgrp); gcgrp = NULL; } ire_refrele(ipif_ire); ire_refrele(save_ire); break; } /* reference now held by IRE */ gcgrp = NULL; ire->ire_marks |= ire_marks; /* * Prevent sire and ipif_ire from getting deleted. * The newly created ire is tied to both of them via * the phandle and ihandle respectively. */ if (sire != NULL) { IRB_REFHOLD(sire->ire_bucket); /* Has it been removed already ? */ if (sire->ire_marks & IRE_MARK_CONDEMNED) { IRB_REFRELE(sire->ire_bucket); ire_refrele(ipif_ire); ire_refrele(save_ire); break; } } IRB_REFHOLD(ipif_ire->ire_bucket); /* Has it been removed already ? */ if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { IRB_REFRELE(ipif_ire->ire_bucket); if (sire != NULL) IRB_REFRELE(sire->ire_bucket); ire_refrele(ipif_ire); ire_refrele(save_ire); break; } xmit_mp = first_mp; /* * In the case of multirouting, a copy * of the packet is done before its sending. * The copy is used to attempt another * route resolution, in a next loop. */ if (ire->ire_flags & RTF_MULTIRT) { copy_mp = copymsg(first_mp); if (copy_mp != NULL) { xmit_mp = copy_mp; MULTIRT_DEBUG_TAG(first_mp); } } ire_add_then_send(q, ire, xmit_mp); ire_refrele(save_ire); /* Assert that sire is not deleted yet. */ if (sire != NULL) { ASSERT(sire->ire_ptpn != NULL); IRB_REFRELE(sire->ire_bucket); } /* Assert that ipif_ire is not deleted yet. */ ASSERT(ipif_ire->ire_ptpn != NULL); IRB_REFRELE(ipif_ire->ire_bucket); ire_refrele(ipif_ire); /* * If copy_mp is not NULL, multirouting was * requested. We loop to initiate a next * route resolution attempt, starting from sire. */ if (copy_mp != NULL) { /* * Search for the next unresolved * multirt route. */ copy_mp = NULL; ipif_ire = NULL; ire = NULL; multirt_resolve_next = B_TRUE; continue; } if (sire != NULL) ire_refrele(sire); ipif_refrele(src_ipif); ill_refrele(dst_ill); return; } case IRE_IF_NORESOLVER: { if (dst_ill->ill_phys_addr_length != IP_ADDR_LEN && dst_ill->ill_resolver_mp == NULL) { ip1dbg(("ip_newroute: dst_ill %p " "for IRE_IF_NORESOLVER ire %p has " "no ill_resolver_mp\n", (void *)dst_ill, (void *)ire)); break; } /* * TSol note: We are creating the ire cache for the * destination 'dst'. If 'dst' is offlink, going * through the first hop 'gw', the security attributes * of 'dst' must be set to point to the gateway * credentials of gateway 'gw'. If 'dst' is onlink, it * is possible that 'dst' is a potential gateway that is * referenced by some route that has some security * attributes. Thus in the former case, we need to do a * gcgrp_lookup of 'gw' while in the latter case we * need to do gcgrp_lookup of 'dst' itself. */ ga.ga_af = AF_INET; IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst, &ga.ga_addr); gcgrp = gcgrp_lookup(&ga, B_FALSE); ire = ire_create( (uchar_t *)&dst, /* dest address */ (uchar_t *)&ip_g_all_ones, /* mask */ (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ (uchar_t *)&gw, /* gateway address */ &save_ire->ire_max_frag, NULL, /* no src nce */ dst_ill->ill_rq, /* recv-from queue */ dst_ill->ill_wq, /* send-to queue */ IRE_CACHE, src_ipif, save_ire->ire_mask, /* Parent mask */ (sire != NULL) ? /* Parent handle */ sire->ire_phandle : 0, save_ire->ire_ihandle, /* Interface handle */ (sire != NULL) ? sire->ire_flags & (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */ &(save_ire->ire_uinfo), NULL, gcgrp, ipst); if (ire == NULL) { if (gcgrp != NULL) { GCGRP_REFRELE(gcgrp); gcgrp = NULL; } ire_refrele(save_ire); break; } /* reference now held by IRE */ gcgrp = NULL; ire->ire_marks |= ire_marks; /* Prevent save_ire from getting deleted */ IRB_REFHOLD(save_ire->ire_bucket); /* Has it been removed already ? */ if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { IRB_REFRELE(save_ire->ire_bucket); ire_refrele(save_ire); break; } /* * In the case of multirouting, a copy * of the packet is made before it is sent. * The copy is used in the next * loop to attempt another resolution. */ xmit_mp = first_mp; if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { copy_mp = copymsg(first_mp); if (copy_mp != NULL) { xmit_mp = copy_mp; MULTIRT_DEBUG_TAG(first_mp); } } ire_add_then_send(q, ire, xmit_mp); /* Assert that it is not deleted yet. */ ASSERT(save_ire->ire_ptpn != NULL); IRB_REFRELE(save_ire->ire_bucket); ire_refrele(save_ire); if (copy_mp != NULL) { /* * If we found a (no)resolver, we ignore any * trailing top priority IRE_CACHE in further * loops. This ensures that we do not omit any * (no)resolver. * This IRE_CACHE, if any, will be processed * by another thread entering ip_newroute(). * IRE_CACHE entries, if any, will be processed * by another thread entering ip_newroute(), * (upon resolver response, for instance). * This aims to force parallel multirt * resolutions as soon as a packet must be sent. * In the best case, after the tx of only one * packet, all reachable routes are resolved. * Otherwise, the resolution of all RTF_MULTIRT * routes would require several emissions. */ multirt_flags &= ~MULTIRT_CACHEGW; /* * Search for the next unresolved multirt * route. */ copy_mp = NULL; save_ire = NULL; ire = NULL; multirt_resolve_next = B_TRUE; continue; } /* * Don't need sire anymore */ if (sire != NULL) ire_refrele(sire); ipif_refrele(src_ipif); ill_refrele(dst_ill); return; } case IRE_IF_RESOLVER: /* * We can't build an IRE_CACHE yet, but at least we * found a resolver that can help. */ res_mp = dst_ill->ill_resolver_mp; if (!OK_RESOLVER_MP(res_mp)) break; /* * To be at this point in the code with a non-zero gw * means that dst is reachable through a gateway that * we have never resolved. By changing dst to the gw * addr we resolve the gateway first. * When ire_add_then_send() tries to put the IP dg * to dst, it will reenter ip_newroute() at which * time we will find the IRE_CACHE for the gw and * create another IRE_CACHE in case IRE_CACHE above. */ if (gw != INADDR_ANY) { /* * The source ipif that was determined above was * relative to the destination address, not the * gateway's. If src_ipif was not taken out of * the IRE_IF_RESOLVER entry, we'll need to call * ipif_select_source() again. */ if (src_ipif != ire->ire_ipif) { ipif_refrele(src_ipif); src_ipif = ipif_select_source(dst_ill, gw, zoneid); if (src_ipif == NULL) { if (ip_debug > 2) { pr_addr_dbg( "ip_newroute: no " "src for gw %s ", AF_INET, &gw); printf("through " "interface %s\n", dst_ill->ill_name); } goto icmp_err_ret; } } save_dst = dst; dst = gw; gw = INADDR_ANY; } /* * We obtain a partial IRE_CACHE which we will pass * along with the resolver query. When the response * comes back it will be there ready for us to add. * The ire_max_frag is atomically set under the * irebucket lock in ire_add_v[46]. */ ire = ire_create_mp( (uchar_t *)&dst, /* dest address */ (uchar_t *)&ip_g_all_ones, /* mask */ (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ (uchar_t *)&gw, /* gateway address */ NULL, /* ire_max_frag */ NULL, /* no src nce */ dst_ill->ill_rq, /* recv-from queue */ dst_ill->ill_wq, /* send-to queue */ IRE_CACHE, src_ipif, /* Interface ipif */ save_ire->ire_mask, /* Parent mask */ 0, save_ire->ire_ihandle, /* Interface handle */ 0, /* flags if any */ &(save_ire->ire_uinfo), NULL, NULL, ipst); if (ire == NULL) { ire_refrele(save_ire); break; } if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { copy_mp = copymsg(first_mp); if (copy_mp != NULL) MULTIRT_DEBUG_TAG(copy_mp); } ire->ire_marks |= ire_marks; /* * Construct message chain for the resolver * of the form: * ARP_REQ_MBLK-->IRE_MBLK-->Packet * Packet could contain a IPSEC_OUT mp. * * NOTE : ire will be added later when the response * comes back from ARP. If the response does not * come back, ARP frees the packet. For this reason, * we can't REFHOLD the bucket of save_ire to prevent * deletions. We may not be able to REFRELE the bucket * if the response never comes back. Thus, before * adding the ire, ire_add_v4 will make sure that the * interface route does not get deleted. This is the * only case unlike ip_newroute_v6, ip_newroute_ipif_v6 * where we can always prevent deletions because of * the synchronous nature of adding IRES i.e * ire_add_then_send is called after creating the IRE. */ ASSERT(ire->ire_mp != NULL); ire->ire_mp->b_cont = first_mp; /* Have saved_mp handy, for cleanup if canput fails */ saved_mp = mp; mp = copyb(res_mp); if (mp == NULL) { /* Prepare for cleanup */ mp = saved_mp; /* pkt */ ire_delete(ire); /* ire_mp */ ire = NULL; ire_refrele(save_ire); if (copy_mp != NULL) { MULTIRT_DEBUG_UNTAG(copy_mp); freemsg(copy_mp); copy_mp = NULL; } break; } linkb(mp, ire->ire_mp); /* * Fill in the source and dest addrs for the resolver. * NOTE: this depends on memory layouts imposed by * ill_init(). */ areq = (areq_t *)mp->b_rptr; addrp = (ipaddr_t *)((char *)areq + areq->areq_sender_addr_offset); if (do_attach_ill) { /* * This is bind to no failover case. * arp packet also must go out on attach_ill. */ ASSERT(ipha->ipha_src != NULL); *addrp = ipha->ipha_src; } else { *addrp = save_ire->ire_src_addr; } ire_refrele(save_ire); addrp = (ipaddr_t *)((char *)areq + areq->areq_target_addr_offset); *addrp = dst; /* Up to the resolver. */ if (canputnext(dst_ill->ill_rq) && !(dst_ill->ill_arp_closing)) { putnext(dst_ill->ill_rq, mp); ire = NULL; if (copy_mp != NULL) { /* * If we found a resolver, we ignore * any trailing top priority IRE_CACHE * in the further loops. This ensures * that we do not omit any resolver. * IRE_CACHE entries, if any, will be * processed next time we enter * ip_newroute(). */ multirt_flags &= ~MULTIRT_CACHEGW; /* * Search for the next unresolved * multirt route. */ first_mp = copy_mp; copy_mp = NULL; /* Prepare the next resolution loop. */ mp = first_mp; EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (mctl_present) io = (ipsec_out_t *) first_mp->b_rptr; ipha = (ipha_t *)mp->b_rptr; ASSERT(sire != NULL); dst = save_dst; multirt_resolve_next = B_TRUE; continue; } if (sire != NULL) ire_refrele(sire); /* * The response will come back in ip_wput * with db_type IRE_DB_TYPE. */ ipif_refrele(src_ipif); ill_refrele(dst_ill); return; } else { /* Prepare for cleanup */ DTRACE_PROBE1(ip__newroute__drop, mblk_t *, mp); mp->b_cont = NULL; freeb(mp); /* areq */ /* * this is an ire that is not added to the * cache. ire_freemblk will handle the release * of any resources associated with the ire. */ ire_delete(ire); /* ire_mp */ mp = saved_mp; /* pkt */ ire = NULL; if (copy_mp != NULL) { MULTIRT_DEBUG_UNTAG(copy_mp); freemsg(copy_mp); copy_mp = NULL; } break; } default: break; } } while (multirt_resolve_next); ip1dbg(("ip_newroute: dropped\n")); /* Did this packet originate externally? */ if (mp->b_prev) { mp->b_next = NULL; mp->b_prev = NULL; BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); } else { if (dst_ill != NULL) { BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards); } else { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards); } } ASSERT(copy_mp == NULL); MULTIRT_DEBUG_UNTAG(first_mp); freemsg(first_mp); if (ire != NULL) ire_refrele(ire); if (sire != NULL) ire_refrele(sire); if (src_ipif != NULL) ipif_refrele(src_ipif); if (dst_ill != NULL) ill_refrele(dst_ill); return; icmp_err_ret: ip1dbg(("ip_newroute: no route\n")); if (src_ipif != NULL) ipif_refrele(src_ipif); if (dst_ill != NULL) ill_refrele(dst_ill); if (sire != NULL) ire_refrele(sire); /* Did this packet originate externally? */ if (mp->b_prev) { mp->b_next = NULL; mp->b_prev = NULL; BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes); q = WR(q); } else { /* * There is no outgoing ill, so just increment the * system MIB. */ BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes); /* * Since ip_wput() isn't close to finished, we fill * in enough of the header for credible error reporting. */ if (ip_hdr_complete(ipha, zoneid, ipst)) { /* Failed */ MULTIRT_DEBUG_UNTAG(first_mp); freemsg(first_mp); if (ire != NULL) ire_refrele(ire); return; } } /* * At this point we will have ire only if RTF_BLACKHOLE * or RTF_REJECT flags are set on the IRE. It will not * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. */ if (ire != NULL) { if (ire->ire_flags & RTF_BLACKHOLE) { ire_refrele(ire); MULTIRT_DEBUG_UNTAG(first_mp); freemsg(first_mp); return; } ire_refrele(ire); } if (ip_source_routed(ipha, ipst)) { icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, zoneid, ipst); return; } icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); } ip_opt_info_t zero_info; /* * IPv4 - * ip_newroute_ipif is called by ip_wput_multicast and * ip_rput_forward_multicast whenever we need to send * out a packet to a destination address for which we do not have specific * routing information. It is used when the packet will be sent out * on a specific interface. It is also called by ip_wput() when IP_BOUND_IF * socket option is set or icmp error message wants to go out on a particular * interface for a unicast packet. * * In most cases, the destination address is resolved thanks to the ipif * intrinsic resolver. However, there are some cases where the call to * ip_newroute_ipif must take into account the potential presence of * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire * that uses the interface. This is specified through flags, * which can be a combination of: * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC * flag, the resulting ire will inherit the IRE_OFFSUBNET source address * and flags. Additionally, the packet source address has to be set to * the specified address. The caller is thus expected to set this flag * if the packet has no specific source address yet. * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT * flag, the resulting ire will inherit the flag. All unresolved routes * to the destination must be explored in the same call to * ip_newroute_ipif(). */ static void ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst, conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop) { areq_t *areq; ire_t *ire = NULL; mblk_t *res_mp; ipaddr_t *addrp; mblk_t *first_mp; ire_t *save_ire = NULL; ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */ ipif_t *src_ipif = NULL; ushort_t ire_marks = 0; ill_t *dst_ill = NULL; boolean_t mctl_present; ipsec_out_t *io; ipha_t *ipha; int ihandle = 0; mblk_t *saved_mp; ire_t *fire = NULL; mblk_t *copy_mp = NULL; boolean_t multirt_resolve_next; boolean_t unspec_src; ipaddr_t ipha_dst; ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; /* * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold * here for uniformity */ ipif_refhold(ipif); /* * This loop is run only once in most cases. * We loop to resolve further routes only when the destination * can be reached through multiple RTF_MULTIRT-flagged ires. */ do { if (dst_ill != NULL) { ill_refrele(dst_ill); dst_ill = NULL; } if (src_ipif != NULL) { ipif_refrele(src_ipif); src_ipif = NULL; } multirt_resolve_next = B_FALSE; ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst), ipif->ipif_ill->ill_name)); EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (mctl_present) io = (ipsec_out_t *)first_mp->b_rptr; ipha = (ipha_t *)mp->b_rptr; /* * Save the packet destination address, we may need it after * the packet has been consumed. */ ipha_dst = ipha->ipha_dst; /* * If the interface is a pt-pt interface we look for an * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the * local_address and the pt-pt destination address. Otherwise * we just match the local address. * NOTE: dst could be different than ipha->ipha_dst in case * of sending igmp multicast packets over a point-to-point * connection. * Thus we must be careful enough to check ipha_dst to be a * multicast address, otherwise it will take xmit_if path for * multicast packets resulting into kernel stack overflow by * repeated calls to ip_newroute_ipif from ire_send(). */ if (CLASSD(ipha_dst) && !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) { goto err_ret; } /* * We check if an IRE_OFFSUBNET for the addr that goes through * ipif exists. We need it to determine if the RTF_SETSRC and/or * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may * propagate its flags to the new ire. */ if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) { fire = ipif_lookup_multi_ire(ipif, ipha_dst); ip2dbg(("ip_newroute_ipif: " "ipif_lookup_multi_ire(" "ipif %p, dst %08x) = fire %p\n", (void *)ipif, ntohl(dst), (void *)fire)); } if (mctl_present && io->ipsec_out_attach_if) { attach_ill = ip_grab_attach_ill(NULL, first_mp, io->ipsec_out_ill_index, B_FALSE, ipst); /* Failure case frees things for us. */ if (attach_ill == NULL) { ipif_refrele(ipif); if (fire != NULL) ire_refrele(fire); return; } /* * Check if we need an ire that will not be * looked up by anybody else i.e. HIDDEN. */ if (ill_is_probeonly(attach_ill)) { ire_marks = IRE_MARK_HIDDEN; } /* * ip_wput passes the right ipif for IPIF_NOFAILOVER * case. */ dst_ill = ipif->ipif_ill; /* attach_ill has been refheld by ip_grab_attach_ill */ ASSERT(dst_ill == attach_ill); } else { /* * If the interface belongs to an interface group, * make sure the next possible interface in the group * is used. This encourages load spreading among * peers in an interface group. * Note: load spreading is disabled for RTF_MULTIRT * routes. */ if ((flags & RTF_MULTIRT) && (fire != NULL) && (fire->ire_flags & RTF_MULTIRT)) { /* * Don't perform outbound load spreading * in the case of an RTF_MULTIRT issued route, * we actually typically want to replicate * outgoing packets through particular * interfaces. */ dst_ill = ipif->ipif_ill; ill_refhold(dst_ill); } else { dst_ill = ip_newroute_get_dst_ill( ipif->ipif_ill); } if (dst_ill == NULL) { if (ip_debug > 2) { pr_addr_dbg("ip_newroute_ipif: " "no dst ill for dst %s\n", AF_INET, &dst); } goto err_ret; } } /* * Pick a source address preferring non-deprecated ones. * Unlike ip_newroute, we don't do any source address * selection here since for multicast it really does not help * in inbound load spreading as in the unicast case. */ if ((flags & RTF_SETSRC) && (fire != NULL) && (fire->ire_flags & RTF_SETSRC)) { /* * As requested by flags, an IRE_OFFSUBNET was looked up * on that interface. This ire has RTF_SETSRC flag, so * the source address of the packet must be changed. * Check that the ipif matching the requested source * address still exists. */ src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL, zoneid, NULL, NULL, NULL, NULL, ipst); } unspec_src = (connp != NULL && connp->conn_unspec_src); if (((!ipif->ipif_isv6 && ipif->ipif_lcl_addr == INADDR_ANY) || (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_UP)) != IPIF_UP || (connp != NULL && ipif->ipif_zoneid != zoneid && ipif->ipif_zoneid != ALL_ZONES)) && (src_ipif == NULL) && (!unspec_src || ipha->ipha_src != INADDR_ANY)) { src_ipif = ipif_select_source(dst_ill, dst, zoneid); if (src_ipif == NULL) { if (ip_debug > 2) { /* ip1dbg */ pr_addr_dbg("ip_newroute_ipif: " "no src for dst %s", AF_INET, &dst); } ip1dbg((" through interface %s\n", dst_ill->ill_name)); goto err_ret; } ipif_refrele(ipif); ipif = src_ipif; ipif_refhold(ipif); } if (src_ipif == NULL) { src_ipif = ipif; ipif_refhold(src_ipif); } /* * Assign a source address while we have the conn. * We can't have ip_wput_ire pick a source address when the * packet returns from arp since conn_unspec_src might be set * and we lose the conn when going through arp. */ if (ipha->ipha_src == INADDR_ANY && !unspec_src) ipha->ipha_src = src_ipif->ipif_src_addr; /* * In the case of IP_BOUND_IF and IP_PKTINFO, it is possible * that the outgoing interface does not have an interface ire. */ if (CLASSD(ipha_dst) && (connp == NULL || connp->conn_outgoing_ill == NULL) && infop->ip_opt_ill_index == 0) { /* ipif_to_ire returns an held ire */ ire = ipif_to_ire(ipif); if (ire == NULL) goto err_ret; if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) goto err_ret; /* * ihandle is needed when the ire is added to * cache table. */ save_ire = ire; ihandle = save_ire->ire_ihandle; ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, " "flags %04x\n", (void *)ire, (void *)ipif, flags)); if ((flags & RTF_MULTIRT) && (fire != NULL) && (fire->ire_flags & RTF_MULTIRT)) { /* * As requested by flags, an IRE_OFFSUBNET was * looked up on that interface. This ire has * RTF_MULTIRT flag, so the resolution loop will * be re-entered to resolve additional routes on * other interfaces. For that purpose, a copy of * the packet is performed at this point. */ fire->ire_last_used_time = lbolt; copy_mp = copymsg(first_mp); if (copy_mp) { MULTIRT_DEBUG_TAG(copy_mp); } } if ((flags & RTF_SETSRC) && (fire != NULL) && (fire->ire_flags & RTF_SETSRC)) { /* * As requested by flags, an IRE_OFFSUBET was * looked up on that interface. This ire has * RTF_SETSRC flag, so the source address of the * packet must be changed. */ ipha->ipha_src = fire->ire_src_addr; } } else { ASSERT((connp == NULL) || (connp->conn_outgoing_ill != NULL) || (connp->conn_dontroute) || infop->ip_opt_ill_index != 0); /* * The only ways we can come here are: * 1) IP_BOUND_IF socket option is set * 2) SO_DONTROUTE socket option is set * 3) IP_PKTINFO option is passed in as ancillary data. * In all cases, the new ire will not be added * into cache table. */ ire_marks |= IRE_MARK_NOADD; } switch (ipif->ipif_net_type) { case IRE_IF_NORESOLVER: { /* We have what we need to build an IRE_CACHE. */ if ((dst_ill->ill_phys_addr_length != IP_ADDR_LEN) && (dst_ill->ill_resolver_mp == NULL)) { ip1dbg(("ip_newroute_ipif: dst_ill %p " "for IRE_IF_NORESOLVER ire %p has " "no ill_resolver_mp\n", (void *)dst_ill, (void *)ire)); break; } /* * The new ire inherits the IRE_OFFSUBNET flags * and source address, if this was requested. */ ire = ire_create( (uchar_t *)&dst, /* dest address */ (uchar_t *)&ip_g_all_ones, /* mask */ (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ NULL, /* gateway address */ &ipif->ipif_mtu, NULL, /* no src nce */ dst_ill->ill_rq, /* recv-from queue */ dst_ill->ill_wq, /* send-to queue */ IRE_CACHE, src_ipif, (save_ire != NULL ? save_ire->ire_mask : 0), (fire != NULL) ? /* Parent handle */ fire->ire_phandle : 0, ihandle, /* Interface handle */ (fire != NULL) ? (fire->ire_flags & (RTF_SETSRC | RTF_MULTIRT)) : 0, (save_ire == NULL ? &ire_uinfo_null : &save_ire->ire_uinfo), NULL, NULL, ipst); if (ire == NULL) { if (save_ire != NULL) ire_refrele(save_ire); break; } ire->ire_marks |= ire_marks; /* * If IRE_MARK_NOADD is set then we need to convert * the max_fragp to a useable value now. This is * normally done in ire_add_v[46]. We also need to * associate the ire with an nce (normally would be * done in ip_wput_nondata()). * * Note that IRE_MARK_NOADD packets created here * do not have a non-null ire_mp pointer. The null * value of ire_bucket indicates that they were * never added. */ if (ire->ire_marks & IRE_MARK_NOADD) { uint_t max_frag; max_frag = *ire->ire_max_fragp; ire->ire_max_fragp = NULL; ire->ire_max_frag = max_frag; if ((ire->ire_nce = ndp_lookup_v4( ire_to_ill(ire), (ire->ire_gateway_addr != INADDR_ANY ? &ire->ire_gateway_addr : &ire->ire_addr), B_FALSE)) == NULL) { if (save_ire != NULL) ire_refrele(save_ire); break; } ASSERT(ire->ire_nce->nce_state == ND_REACHABLE); NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce); } /* Prevent save_ire from getting deleted */ if (save_ire != NULL) { IRB_REFHOLD(save_ire->ire_bucket); /* Has it been removed already ? */ if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { IRB_REFRELE(save_ire->ire_bucket); ire_refrele(save_ire); break; } } ire_add_then_send(q, ire, first_mp); /* Assert that save_ire is not deleted yet. */ if (save_ire != NULL) { ASSERT(save_ire->ire_ptpn != NULL); IRB_REFRELE(save_ire->ire_bucket); ire_refrele(save_ire); save_ire = NULL; } if (fire != NULL) { ire_refrele(fire); fire = NULL; } /* * the resolution loop is re-entered if this * was requested through flags and if we * actually are in a multirouting case. */ if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { boolean_t need_resolve = ire_multirt_need_resolve(ipha_dst, MBLK_GETLABEL(copy_mp), ipst); if (!need_resolve) { MULTIRT_DEBUG_UNTAG(copy_mp); freemsg(copy_mp); copy_mp = NULL; } else { /* * ipif_lookup_group() calls * ire_lookup_multi() that uses * ire_ftable_lookup() to find * an IRE_INTERFACE for the group. * In the multirt case, * ire_lookup_multi() then invokes * ire_multirt_lookup() to find * the next resolvable ire. * As a result, we obtain an new * interface, derived from the * next ire. */ ipif_refrele(ipif); ipif = ipif_lookup_group(ipha_dst, zoneid, ipst); ip2dbg(("ip_newroute_ipif: " "multirt dst %08x, ipif %p\n", htonl(dst), (void *)ipif)); if (ipif != NULL) { mp = copy_mp; copy_mp = NULL; multirt_resolve_next = B_TRUE; continue; } else { freemsg(copy_mp); } } } if (ipif != NULL) ipif_refrele(ipif); ill_refrele(dst_ill); ipif_refrele(src_ipif); return; } case IRE_IF_RESOLVER: /* * We can't build an IRE_CACHE yet, but at least * we found a resolver that can help. */ res_mp = dst_ill->ill_resolver_mp; if (!OK_RESOLVER_MP(res_mp)) break; /* * We obtain a partial IRE_CACHE which we will pass * along with the resolver query. When the response * comes back it will be there ready for us to add. * The new ire inherits the IRE_OFFSUBNET flags * and source address, if this was requested. * The ire_max_frag is atomically set under the * irebucket lock in ire_add_v[46]. Only in the * case of IRE_MARK_NOADD, we set it here itself. */ ire = ire_create_mp( (uchar_t *)&dst, /* dest address */ (uchar_t *)&ip_g_all_ones, /* mask */ (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ NULL, /* gateway address */ (ire_marks & IRE_MARK_NOADD) ? ipif->ipif_mtu : 0, /* max_frag */ NULL, /* no src nce */ dst_ill->ill_rq, /* recv-from queue */ dst_ill->ill_wq, /* send-to queue */ IRE_CACHE, src_ipif, (save_ire != NULL ? save_ire->ire_mask : 0), (fire != NULL) ? /* Parent handle */ fire->ire_phandle : 0, ihandle, /* Interface handle */ (fire != NULL) ? /* flags if any */ (fire->ire_flags & (RTF_SETSRC | RTF_MULTIRT)) : 0, (save_ire == NULL ? &ire_uinfo_null : &save_ire->ire_uinfo), NULL, NULL, ipst); if (save_ire != NULL) { ire_refrele(save_ire); save_ire = NULL; } if (ire == NULL) break; ire->ire_marks |= ire_marks; /* * Construct message chain for the resolver of the * form: * ARP_REQ_MBLK-->IRE_MBLK-->Packet * * NOTE : ire will be added later when the response * comes back from ARP. If the response does not * come back, ARP frees the packet. For this reason, * we can't REFHOLD the bucket of save_ire to prevent * deletions. We may not be able to REFRELE the * bucket if the response never comes back. * Thus, before adding the ire, ire_add_v4 will make * sure that the interface route does not get deleted. * This is the only case unlike ip_newroute_v6, * ip_newroute_ipif_v6 where we can always prevent * deletions because ire_add_then_send is called after * creating the IRE. * If IRE_MARK_NOADD is set, then ire_add_then_send * does not add this IRE into the IRE CACHE. */ ASSERT(ire->ire_mp != NULL); ire->ire_mp->b_cont = first_mp; /* Have saved_mp handy, for cleanup if canput fails */ saved_mp = mp; mp = copyb(res_mp); if (mp == NULL) { /* Prepare for cleanup */ mp = saved_mp; /* pkt */ ire_delete(ire); /* ire_mp */ ire = NULL; if (copy_mp != NULL) { MULTIRT_DEBUG_UNTAG(copy_mp); freemsg(copy_mp); copy_mp = NULL; } break; } linkb(mp, ire->ire_mp); /* * Fill in the source and dest addrs for the resolver. * NOTE: this depends on memory layouts imposed by * ill_init(). */ areq = (areq_t *)mp->b_rptr; addrp = (ipaddr_t *)((char *)areq + areq->areq_sender_addr_offset); *addrp = ire->ire_src_addr; addrp = (ipaddr_t *)((char *)areq + areq->areq_target_addr_offset); *addrp = dst; /* Up to the resolver. */ if (canputnext(dst_ill->ill_rq) && !(dst_ill->ill_arp_closing)) { putnext(dst_ill->ill_rq, mp); /* * The response will come back in ip_wput * with db_type IRE_DB_TYPE. */ } else { mp->b_cont = NULL; freeb(mp); /* areq */ ire_delete(ire); /* ire_mp */ saved_mp->b_next = NULL; saved_mp->b_prev = NULL; freemsg(first_mp); /* pkt */ ip2dbg(("ip_newroute_ipif: dropped\n")); } if (fire != NULL) { ire_refrele(fire); fire = NULL; } /* * The resolution loop is re-entered if this was * requested through flags and we actually are * in a multirouting case. */ if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) { boolean_t need_resolve = ire_multirt_need_resolve(ipha_dst, MBLK_GETLABEL(copy_mp), ipst); if (!need_resolve) { MULTIRT_DEBUG_UNTAG(copy_mp); freemsg(copy_mp); copy_mp = NULL; } else { /* * ipif_lookup_group() calls * ire_lookup_multi() that uses * ire_ftable_lookup() to find * an IRE_INTERFACE for the group. * In the multirt case, * ire_lookup_multi() then invokes * ire_multirt_lookup() to find * the next resolvable ire. * As a result, we obtain an new * interface, derived from the * next ire. */ ipif_refrele(ipif); ipif = ipif_lookup_group(ipha_dst, zoneid, ipst); if (ipif != NULL) { mp = copy_mp; copy_mp = NULL; multirt_resolve_next = B_TRUE; continue; } else { freemsg(copy_mp); } } } if (ipif != NULL) ipif_refrele(ipif); ill_refrele(dst_ill); ipif_refrele(src_ipif); return; default: break; } } while (multirt_resolve_next); err_ret: ip2dbg(("ip_newroute_ipif: dropped\n")); if (fire != NULL) ire_refrele(fire); ipif_refrele(ipif); /* Did this packet originate externally? */ if (dst_ill != NULL) ill_refrele(dst_ill); if (src_ipif != NULL) ipif_refrele(src_ipif); if (mp->b_prev || mp->b_next) { mp->b_next = NULL; mp->b_prev = NULL; } else { /* * Since ip_wput() isn't close to finished, we fill * in enough of the header for credible error reporting. */ if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) { /* Failed */ freemsg(first_mp); if (ire != NULL) ire_refrele(ire); return; } } /* * At this point we will have ire only if RTF_BLACKHOLE * or RTF_REJECT flags are set on the IRE. It will not * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set. */ if (ire != NULL) { if (ire->ire_flags & RTF_BLACKHOLE) { ire_refrele(ire); freemsg(first_mp); return; } ire_refrele(ire); } icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst); } /* Name/Value Table Lookup Routine */ char * ip_nv_lookup(nv_t *nv, int value) { if (!nv) return (NULL); for (; nv->nv_name; nv++) { if (nv->nv_value == value) return (nv->nv_name); } return ("unknown"); } /* * This is a module open, i.e. this is a control stream for access * to a DLPI device. We allocate an ill_t as the instance data in * this case. */ int ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) { ill_t *ill; int err; zoneid_t zoneid; netstack_t *ns; ip_stack_t *ipst; /* * Prevent unprivileged processes from pushing IP so that * they can't send raw IP. */ if (secpolicy_net_rawaccess(credp) != 0) return (EPERM); ns = netstack_find_by_cred(credp); ASSERT(ns != NULL); ipst = ns->netstack_ip; ASSERT(ipst != NULL); /* * For exclusive stacks we set the zoneid to zero * to make IP operate as if in the global zone. */ if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) zoneid = GLOBAL_ZONEID; else zoneid = crgetzoneid(credp); ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t)); q->q_ptr = WR(q)->q_ptr = ill; ill->ill_ipst = ipst; ill->ill_zoneid = zoneid; /* * ill_init initializes the ill fields and then sends down * down a DL_INFO_REQ after calling qprocson. */ err = ill_init(q, ill); if (err != 0) { mi_free(ill); netstack_rele(ipst->ips_netstack); q->q_ptr = NULL; WR(q)->q_ptr = NULL; return (err); } /* ill_init initializes the ipsq marking this thread as writer */ ipsq_exit(ill->ill_phyint->phyint_ipsq); /* Wait for the DL_INFO_ACK */ mutex_enter(&ill->ill_lock); while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) { /* * Return value of 0 indicates a pending signal. */ err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock); if (err == 0) { mutex_exit(&ill->ill_lock); (void) ip_close(q, 0); return (EINTR); } } mutex_exit(&ill->ill_lock); /* * ip_rput_other could have set an error in ill_error on * receipt of M_ERROR. */ err = ill->ill_error; if (err != 0) { (void) ip_close(q, 0); return (err); } ill->ill_credp = credp; crhold(credp); mutex_enter(&ipst->ips_ip_mi_lock); err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag, credp); mutex_exit(&ipst->ips_ip_mi_lock); if (err) { (void) ip_close(q, 0); return (err); } return (0); } /* For /dev/ip aka AF_INET open */ int ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) { return (ip_open(q, devp, flag, sflag, credp, B_FALSE)); } /* For /dev/ip6 aka AF_INET6 open */ int ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) { return (ip_open(q, devp, flag, sflag, credp, B_TRUE)); } /* IP open routine. */ int ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, boolean_t isv6) { conn_t *connp; major_t maj; zoneid_t zoneid; netstack_t *ns; ip_stack_t *ipst; TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q); /* Allow reopen. */ if (q->q_ptr != NULL) return (0); if (sflag & MODOPEN) { /* This is a module open */ return (ip_modopen(q, devp, flag, sflag, credp)); } ns = netstack_find_by_cred(credp); ASSERT(ns != NULL); ipst = ns->netstack_ip; ASSERT(ipst != NULL); /* * For exclusive stacks we set the zoneid to zero * to make IP operate as if in the global zone. */ if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID) zoneid = GLOBAL_ZONEID; else zoneid = crgetzoneid(credp); /* * We are opening as a device. This is an IP client stream, and we * allocate an conn_t as the instance data. */ connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack); /* * ipcl_conn_create did a netstack_hold. Undo the hold that was * done by netstack_find_by_cred() */ netstack_rele(ipst->ips_netstack); connp->conn_zoneid = zoneid; connp->conn_upq = q; q->q_ptr = WR(q)->q_ptr = connp; if (flag & SO_SOCKSTR) connp->conn_flags |= IPCL_SOCKET; /* Minor tells us which /dev entry was opened */ if (isv6) { connp->conn_flags |= IPCL_ISV6; connp->conn_af_isv6 = B_TRUE; ip_setpktversion(connp, isv6, B_FALSE, ipst); connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; } else { connp->conn_af_isv6 = B_FALSE; connp->conn_pkt_isv6 = B_FALSE; } if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { connp->conn_minor_arena = ip_minor_arena_la; } else { /* * Either minor numbers in the large arena were exhausted * or a non socket application is doing the open. * Try to allocate from the small arena. */ if ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { /* CONN_DEC_REF takes care of netstack_rele() */ q->q_ptr = WR(q)->q_ptr = NULL; CONN_DEC_REF(connp); return (EBUSY); } connp->conn_minor_arena = ip_minor_arena_sa; } maj = getemajor(*devp); *devp = makedevice(maj, (minor_t)connp->conn_dev); /* * connp->conn_cred is crfree()ed in ipcl_conn_destroy() */ connp->conn_cred = credp; /* * Handle IP_RTS_REQUEST and other ioctls which use conn_recv */ connp->conn_recv = ip_conn_input; crhold(connp->conn_cred); /* * If the caller has the process-wide flag set, then default to MAC * exempt mode. This allows read-down to unlabeled hosts. */ if (getpflags(NET_MAC_AWARE, credp) != 0) connp->conn_mac_exempt = B_TRUE; connp->conn_rq = q; connp->conn_wq = WR(q); /* Non-zero default values */ connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; /* * Make the conn globally visible to walkers */ ASSERT(connp->conn_ref == 1); mutex_enter(&connp->conn_lock); connp->conn_state_flags &= ~CONN_INCIPIENT; mutex_exit(&connp->conn_lock); qprocson(q); return (0); } /* * Change the output format (IPv4 vs. IPv6) for a conn_t. * Note that there is no race since either ip_output function works - it * is just an optimization to enter the best ip_output routine directly. */ void ip_setpktversion(conn_t *connp, boolean_t isv6, boolean_t bump_mib, ip_stack_t *ipst) { if (isv6) { if (bump_mib) { BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsOutSwitchIPVersion); } connp->conn_send = ip_output_v6; connp->conn_pkt_isv6 = B_TRUE; } else { if (bump_mib) { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutSwitchIPVersion); } connp->conn_send = ip_output; connp->conn_pkt_isv6 = B_FALSE; } } /* * See if IPsec needs loading because of the options in mp. */ static boolean_t ipsec_opt_present(mblk_t *mp) { uint8_t *optcp, *next_optcp, *opt_endcp; struct opthdr *opt; struct T_opthdr *topt; int opthdr_len; t_uscalar_t optname, optlevel; struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr; ipsec_req_t *ipsr; /* * Walk through the mess, and find IP_SEC_OPT. If it's there, * return TRUE. */ optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length); opt_endcp = optcp + tor->OPT_length; if (tor->PRIM_type == T_OPTMGMT_REQ) { opthdr_len = sizeof (struct T_opthdr); } else { /* O_OPTMGMT_REQ */ ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ); opthdr_len = sizeof (struct opthdr); } for (; optcp < opt_endcp; optcp = next_optcp) { if (optcp + opthdr_len > opt_endcp) return (B_FALSE); /* Not enough option header. */ if (tor->PRIM_type == T_OPTMGMT_REQ) { topt = (struct T_opthdr *)optcp; optlevel = topt->level; optname = topt->name; next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len); } else { opt = (struct opthdr *)optcp; optlevel = opt->level; optname = opt->name; next_optcp = optcp + opthdr_len + _TPI_ALIGN_OPT(opt->len); } if ((next_optcp < optcp) || /* wraparound pointer space */ ((next_optcp >= opt_endcp) && /* last option bad len */ ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE))) return (B_FALSE); /* bad option buffer */ if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) || (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) { /* * Check to see if it's an all-bypass or all-zeroes * IPsec request. Don't bother loading IPsec if * the socket doesn't want to use it. (A good example * is a bypass request.) * * Basically, if any of the non-NEVER bits are set, * load IPsec. */ ipsr = (ipsec_req_t *)(optcp + opthdr_len); if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 || (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 || (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER) != 0) return (B_TRUE); } } return (B_FALSE); } /* * If conn is is waiting for ipsec to finish loading, kick it. */ /* ARGSUSED */ static void conn_restart_ipsec_waiter(conn_t *connp, void *arg) { t_scalar_t optreq_prim; mblk_t *mp; cred_t *cr; int err = 0; /* * This function is called, after ipsec loading is complete. * Since IP checks exclusively and atomically (i.e it prevents * ipsec load from completing until ip_optcom_req completes) * whether ipsec load is complete, there cannot be a race with IP * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now. */ mutex_enter(&connp->conn_lock); if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) { ASSERT(connp->conn_ipsec_opt_mp != NULL); mp = connp->conn_ipsec_opt_mp; connp->conn_ipsec_opt_mp = NULL; connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT; cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp))); mutex_exit(&connp->conn_lock); ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); optreq_prim = ((union T_primitives *)mp->b_rptr)->type; if (optreq_prim == T_OPTMGMT_REQ) { err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr, &ip_opt_obj, B_FALSE); } else { ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ); err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr, &ip_opt_obj, B_FALSE); } if (err != EINPROGRESS) CONN_OPER_PENDING_DONE(connp); return; } mutex_exit(&connp->conn_lock); } /* * Called from the ipsec_loader thread, outside any perimeter, to tell * ip qenable any of the queues waiting for the ipsec loader to * complete. */ void ip_ipsec_load_complete(ipsec_stack_t *ipss) { netstack_t *ns = ipss->ipsec_netstack; ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip); } /* * Can't be used. Need to call svr4* -> optset directly. the leaf routine * determines the grp on which it has to become exclusive, queues the mp * and sq draining restarts the optmgmt */ static boolean_t ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp) { conn_t *connp = Q_TO_CONN(q); ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec; /* * Take IPsec requests and treat them special. */ if (ipsec_opt_present(mp)) { /* First check if IPsec is loaded. */ mutex_enter(&ipss->ipsec_loader_lock); if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) { mutex_exit(&ipss->ipsec_loader_lock); return (B_FALSE); } mutex_enter(&connp->conn_lock); connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT; ASSERT(connp->conn_ipsec_opt_mp == NULL); connp->conn_ipsec_opt_mp = mp; mutex_exit(&connp->conn_lock); mutex_exit(&ipss->ipsec_loader_lock); ipsec_loader_loadnow(ipss); return (B_TRUE); } return (B_FALSE); } /* * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid, * all of them are copied to the conn_t. If the req is "zero", the policy is * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req * fields. * We keep only the latest setting of the policy and thus policy setting * is not incremental/cumulative. * * Requests to set policies with multiple alternative actions will * go through a different API. */ int ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req) { uint_t ah_req = 0; uint_t esp_req = 0; uint_t se_req = 0; ipsec_selkey_t sel; ipsec_act_t *actp = NULL; uint_t nact; ipsec_policy_t *pin4 = NULL, *pout4 = NULL; ipsec_policy_t *pin6 = NULL, *pout6 = NULL; ipsec_policy_root_t *pr; ipsec_policy_head_t *ph; int fam; boolean_t is_pol_reset; int error = 0; netstack_t *ns = connp->conn_netstack; ip_stack_t *ipst = ns->netstack_ip; ipsec_stack_t *ipss = ns->netstack_ipsec; #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER) /* * The IP_SEC_OPT option does not allow variable length parameters, * hence a request cannot be NULL. */ if (req == NULL) return (EINVAL); ah_req = req->ipsr_ah_req; esp_req = req->ipsr_esp_req; se_req = req->ipsr_self_encap_req; /* Don't allow setting self-encap without one or more of AH/ESP. */ if (se_req != 0 && esp_req == 0 && ah_req == 0) return (EINVAL); /* * Are we dealing with a request to reset the policy (i.e. * zero requests). */ is_pol_reset = ((ah_req & REQ_MASK) == 0 && (esp_req & REQ_MASK) == 0 && (se_req & REQ_MASK) == 0); if (!is_pol_reset) { /* * If we couldn't load IPsec, fail with "protocol * not supported". * IPsec may not have been loaded for a request with zero * policies, so we don't fail in this case. */ mutex_enter(&ipss->ipsec_loader_lock); if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) { mutex_exit(&ipss->ipsec_loader_lock); return (EPROTONOSUPPORT); } mutex_exit(&ipss->ipsec_loader_lock); /* * Test for valid requests. Invalid algorithms * need to be tested by IPsec code because new * algorithms can be added dynamically. */ if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 || (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) { return (EINVAL); } /* * Only privileged users can issue these * requests. */ if (((ah_req & IPSEC_PREF_NEVER) || (esp_req & IPSEC_PREF_NEVER) || (se_req & IPSEC_PREF_NEVER)) && secpolicy_ip_config(cr, B_FALSE) != 0) { return (EPERM); } /* * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER * are mutually exclusive. */ if (((ah_req & REQ_MASK) == REQ_MASK) || ((esp_req & REQ_MASK) == REQ_MASK) || ((se_req & REQ_MASK) == REQ_MASK)) { /* Both of them are set */ return (EINVAL); } } mutex_enter(&connp->conn_lock); /* * If we have already cached policies in ip_bind_connected*(), don't * let them change now. We cache policies for connections * whose src,dst [addr, port] is known. */ if (connp->conn_policy_cached) { mutex_exit(&connp->conn_lock); return (EINVAL); } /* * We have a zero policies, reset the connection policy if already * set. This will cause the connection to inherit the * global policy, if any. */ if (is_pol_reset) { if (connp->conn_policy != NULL) { IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack); connp->conn_policy = NULL; } connp->conn_flags &= ~IPCL_CHECK_POLICY; connp->conn_in_enforce_policy = B_FALSE; connp->conn_out_enforce_policy = B_FALSE; mutex_exit(&connp->conn_lock); return (0); } ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy, ipst->ips_netstack); if (ph == NULL) goto enomem; ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack); if (actp == NULL) goto enomem; /* * Always allocate IPv4 policy entries, since they can also * apply to ipv6 sockets being used in ipv4-compat mode. */ bzero(&sel, sizeof (sel)); sel.ipsl_valid = IPSL_IPV4; pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); if (pin4 == NULL) goto enomem; pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); if (pout4 == NULL) goto enomem; if (connp->conn_af_isv6) { /* * We're looking at a v6 socket, also allocate the * v6-specific entries... */ sel.ipsl_valid = IPSL_IPV6; pin6 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); if (pin6 == NULL) goto enomem; pout6 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack); if (pout6 == NULL) goto enomem; /* * .. and file them away in the right place. */ fam = IPSEC_AF_V6; pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]); ipsec_insert_always(&ph->iph_rulebyid, pin6); pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]); ipsec_insert_always(&ph->iph_rulebyid, pout6); } ipsec_actvec_free(actp, nact); /* * File the v4 policies. */ fam = IPSEC_AF_V4; pr = &ph->iph_root[IPSEC_TYPE_INBOUND]; HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]); ipsec_insert_always(&ph->iph_rulebyid, pin4); pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND]; HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]); ipsec_insert_always(&ph->iph_rulebyid, pout4); /* * If the requests need security, set enforce_policy. * If the requests are IPSEC_PREF_NEVER, one should * still set conn_out_enforce_policy so that an ipsec_out * gets attached in ip_wput. This is needed so that * for connections that we don't cache policy in ip_bind, * if global policy matches in ip_wput_attach_policy, we * don't wrongly inherit global policy. Similarly, we need * to set conn_in_enforce_policy also so that we don't verify * policy wrongly. */ if ((ah_req & REQ_MASK) != 0 || (esp_req & REQ_MASK) != 0 || (se_req & REQ_MASK) != 0) { connp->conn_in_enforce_policy = B_TRUE; connp->conn_out_enforce_policy = B_TRUE; connp->conn_flags |= IPCL_CHECK_POLICY; } mutex_exit(&connp->conn_lock); return (error); #undef REQ_MASK /* * Common memory-allocation-failure exit path. */ enomem: mutex_exit(&connp->conn_lock); if (actp != NULL) ipsec_actvec_free(actp, nact); if (pin4 != NULL) IPPOL_REFRELE(pin4, ipst->ips_netstack); if (pout4 != NULL) IPPOL_REFRELE(pout4, ipst->ips_netstack); if (pin6 != NULL) IPPOL_REFRELE(pin6, ipst->ips_netstack); if (pout6 != NULL) IPPOL_REFRELE(pout6, ipst->ips_netstack); return (ENOMEM); } /* * Only for options that pass in an IP addr. Currently only V4 options * pass in an ipif. V6 options always pass an ifindex specifying the ill. * So this function assumes level is IPPROTO_IP */ int ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option, mblk_t *first_mp) { ipif_t *ipif = NULL; int error; ill_t *ill; int zoneid; ip_stack_t *ipst = connp->conn_netstack->netstack_ip; ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr)); if (addr != INADDR_ANY || checkonly) { ASSERT(connp != NULL); zoneid = IPCL_ZONEID(connp); if (option == IP_NEXTHOP) { ipif = ipif_lookup_onlink_addr(addr, connp->conn_zoneid, ipst); } else { ipif = ipif_lookup_addr(addr, NULL, zoneid, CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, &error, ipst); } if (ipif == NULL) { if (error == EINPROGRESS) return (error); else if ((option == IP_MULTICAST_IF) || (option == IP_NEXTHOP)) return (EHOSTUNREACH); else return (EINVAL); } else if (checkonly) { if (option == IP_MULTICAST_IF) { ill = ipif->ipif_ill; /* not supported by the virtual network iface */ if (IS_VNI(ill)) { ipif_refrele(ipif); return (EINVAL); } } ipif_refrele(ipif); return (0); } ill = ipif->ipif_ill; mutex_enter(&connp->conn_lock); mutex_enter(&ill->ill_lock); if ((ill->ill_state_flags & ILL_CONDEMNED) || (ipif->ipif_state_flags & IPIF_CONDEMNED)) { mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); ipif_refrele(ipif); return (option == IP_MULTICAST_IF ? EHOSTUNREACH : EINVAL); } } else { mutex_enter(&connp->conn_lock); } /* None of the options below are supported on the VNI */ if (ipif != NULL && IS_VNI(ipif->ipif_ill)) { mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); ipif_refrele(ipif); return (EINVAL); } switch (option) { case IP_DONTFAILOVER_IF: /* * This option is used by in.mpathd to ensure * that IPMP probe packets only go out on the * test interfaces. in.mpathd sets this option * on the non-failover interfaces. * For backward compatibility, this option * implicitly sets IP_MULTICAST_IF, as used * be done in bind(), so that ip_wput gets * this ipif to send mcast packets. */ if (ipif != NULL) { ASSERT(addr != INADDR_ANY); connp->conn_nofailover_ill = ipif->ipif_ill; connp->conn_multicast_ipif = ipif; } else { ASSERT(addr == INADDR_ANY); connp->conn_nofailover_ill = NULL; connp->conn_multicast_ipif = NULL; } break; case IP_MULTICAST_IF: connp->conn_multicast_ipif = ipif; break; case IP_NEXTHOP: connp->conn_nexthop_v4 = addr; connp->conn_nexthop_set = B_TRUE; break; } if (ipif != NULL) { mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); ipif_refrele(ipif); return (0); } mutex_exit(&connp->conn_lock); /* We succeded in cleared the option */ return (0); } /* * For options that pass in an ifindex specifying the ill. V6 options always * pass in an ill. Some v4 options also pass in ifindex specifying the ill. */ int ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly, int level, int option, mblk_t *first_mp) { ill_t *ill = NULL; int error = 0; ip_stack_t *ipst = connp->conn_netstack->netstack_ip; ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex)); if (ifindex != 0) { ASSERT(connp != NULL); ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt, &error, ipst); if (ill != NULL) { if (checkonly) { /* not supported by the virtual network iface */ if (IS_VNI(ill)) { ill_refrele(ill); return (EINVAL); } ill_refrele(ill); return (0); } if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid, 0, NULL)) { ill_refrele(ill); ill = NULL; mutex_enter(&connp->conn_lock); goto setit; } mutex_enter(&connp->conn_lock); mutex_enter(&ill->ill_lock); if (ill->ill_state_flags & ILL_CONDEMNED) { mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); ill_refrele(ill); ill = NULL; mutex_enter(&connp->conn_lock); } goto setit; } else if (error == EINPROGRESS) { return (error); } else { error = 0; } } mutex_enter(&connp->conn_lock); setit: ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6)); /* * The options below assume that the ILL (if any) transmits and/or * receives traffic. Neither of which is true for the virtual network * interface, so fail setting these on a VNI. */ if (IS_VNI(ill)) { ASSERT(ill != NULL); mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); ill_refrele(ill); return (EINVAL); } if (level == IPPROTO_IP) { switch (option) { case IP_BOUND_IF: connp->conn_incoming_ill = ill; connp->conn_outgoing_ill = ill; connp->conn_orig_bound_ifindex = (ill == NULL) ? 0 : ifindex; break; case IP_MULTICAST_IF: /* * This option is an internal special. The socket * level IP_MULTICAST_IF specifies an 'ipaddr' and * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF * specifies an ifindex and we try first on V6 ill's. * If we don't find one, we they try using on v4 ill's * intenally and we come here. */ if (!checkonly && ill != NULL) { ipif_t *ipif; ipif = ill->ill_ipif; if (ipif->ipif_state_flags & IPIF_CONDEMNED) { mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); ill_refrele(ill); ill = NULL; mutex_enter(&connp->conn_lock); } else { connp->conn_multicast_ipif = ipif; } } break; case IP_DHCPINIT_IF: if (connp->conn_dhcpinit_ill != NULL) { /* * We've locked the conn so conn_cleanup_ill() * cannot clear conn_dhcpinit_ill -- so it's * safe to access the ill. */ ill_t *oill = connp->conn_dhcpinit_ill; ASSERT(oill->ill_dhcpinit != 0); atomic_dec_32(&oill->ill_dhcpinit); connp->conn_dhcpinit_ill = NULL; } if (ill != NULL) { connp->conn_dhcpinit_ill = ill; atomic_inc_32(&ill->ill_dhcpinit); } break; } } else { switch (option) { case IPV6_BOUND_IF: connp->conn_incoming_ill = ill; connp->conn_outgoing_ill = ill; connp->conn_orig_bound_ifindex = (ill == NULL) ? 0 : ifindex; break; case IPV6_BOUND_PIF: /* * Limit all transmit to this ill. * Unlike IPV6_BOUND_IF, using this option * prevents load spreading and failover from * happening when the interface is part of the * group. That's why we don't need to remember * the ifindex in orig_bound_ifindex as in * IPV6_BOUND_IF. */ connp->conn_outgoing_pill = ill; break; case IPV6_DONTFAILOVER_IF: /* * This option is used by in.mpathd to ensure * that IPMP probe packets only go out on the * test interfaces. in.mpathd sets this option * on the non-failover interfaces. */ connp->conn_nofailover_ill = ill; /* * For backward compatibility, this option * implicitly sets ip_multicast_ill as used in * IPV6_MULTICAST_IF so that ip_wput gets * this ill to send mcast packets. */ connp->conn_multicast_ill = ill; connp->conn_orig_multicast_ifindex = (ill == NULL) ? 0 : ifindex; break; case IPV6_MULTICAST_IF: /* * Set conn_multicast_ill to be the IPv6 ill. * Set conn_multicast_ipif to be an IPv4 ipif * for ifindex to make IPv4 mapped addresses * on PF_INET6 sockets honor IPV6_MULTICAST_IF. * Even if no IPv6 ill exists for the ifindex * we need to check for an IPv4 ifindex in order * for this to work with mapped addresses. In that * case only set conn_multicast_ipif. */ if (!checkonly) { if (ifindex == 0) { connp->conn_multicast_ill = NULL; connp->conn_orig_multicast_ifindex = 0; connp->conn_multicast_ipif = NULL; } else if (ill != NULL) { connp->conn_multicast_ill = ill; connp->conn_orig_multicast_ifindex = ifindex; } } break; } } if (ill != NULL) { mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); ill_refrele(ill); return (0); } mutex_exit(&connp->conn_lock); /* * We succeeded in clearing the option (ifindex == 0) or failed to * locate the ill and could not set the option (ifindex != 0) */ return (ifindex == 0 ? 0 : EINVAL); } /* This routine sets socket options. */ /* ARGSUSED */ int ip_opt_set(queue_t *q, uint_t optset_context, int level, int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, void *dummy, cred_t *cr, mblk_t *first_mp) { int *i1 = (int *)invalp; conn_t *connp = Q_TO_CONN(q); int error = 0; boolean_t checkonly; ire_t *ire; boolean_t found; ip_stack_t *ipst = connp->conn_netstack->netstack_ip; switch (optset_context) { case SETFN_OPTCOM_CHECKONLY: checkonly = B_TRUE; /* * Note: Implies T_CHECK semantics for T_OPTCOM_REQ * inlen != 0 implies value supplied and * we have to "pretend" to set it. * inlen == 0 implies that there is no * value part in T_CHECK request and just validation * done elsewhere should be enough, we just return here. */ if (inlen == 0) { *outlenp = 0; return (0); } break; case SETFN_OPTCOM_NEGOTIATE: case SETFN_UD_NEGOTIATE: case SETFN_CONN_NEGOTIATE: checkonly = B_FALSE; break; default: /* * We should never get here */ *outlenp = 0; return (EINVAL); } ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); /* * For fixed length options, no sanity check * of passed in length is done. It is assumed *_optcom_req() * routines do the right thing. */ switch (level) { case SOL_SOCKET: /* * conn_lock protects the bitfields, and is used to * set the fields atomically. */ switch (name) { case SO_BROADCAST: if (!checkonly) { /* TODO: use value someplace? */ mutex_enter(&connp->conn_lock); connp->conn_broadcast = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case SO_USELOOPBACK: if (!checkonly) { /* TODO: use value someplace? */ mutex_enter(&connp->conn_lock); connp->conn_loopback = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case SO_DONTROUTE: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_dontroute = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case SO_REUSEADDR: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_reuseaddr = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case SO_PROTOTYPE: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_proto = *i1; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case SO_ALLZONES: if (!checkonly) { mutex_enter(&connp->conn_lock); if (IPCL_IS_BOUND(connp)) { mutex_exit(&connp->conn_lock); return (EINVAL); } connp->conn_allzones = *i1 != 0 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case SO_ANON_MLP: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_anon_mlp = *i1 != 0 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case SO_MAC_EXEMPT: if (secpolicy_net_mac_aware(cr) != 0 || IPCL_IS_BOUND(connp)) return (EACCES); if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_mac_exempt = *i1 != 0 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ default: /* * "soft" error (negative) * option not handled at this level * Note: Do not modify *outlenp */ return (-EINVAL); } break; case IPPROTO_IP: switch (name) { case IP_NEXTHOP: if (secpolicy_ip_config(cr, B_FALSE) != 0) return (EPERM); /* FALLTHRU */ case IP_MULTICAST_IF: case IP_DONTFAILOVER_IF: { ipaddr_t addr = *i1; error = ip_opt_set_ipif(connp, addr, checkonly, name, first_mp); if (error != 0) return (error); break; /* goto sizeof (int) option return */ } case IP_MULTICAST_TTL: /* Recorded in transport above IP */ *outvalp = *invalp; *outlenp = sizeof (uchar_t); return (0); case IP_MULTICAST_LOOP: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_multicast_loop = *invalp ? 1 : 0; mutex_exit(&connp->conn_lock); } *outvalp = *invalp; *outlenp = sizeof (uchar_t); return (0); case IP_ADD_MEMBERSHIP: case MCAST_JOIN_GROUP: case IP_DROP_MEMBERSHIP: case MCAST_LEAVE_GROUP: { struct ip_mreq *mreqp; struct group_req *greqp; ire_t *ire; boolean_t done = B_FALSE; ipaddr_t group, ifaddr; struct sockaddr_in *sin; uint32_t *ifindexp; boolean_t mcast_opt = B_TRUE; mcast_record_t fmode; int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *); switch (name) { case IP_ADD_MEMBERSHIP: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_JOIN_GROUP: fmode = MODE_IS_EXCLUDE; optfn = ip_opt_add_group; break; case IP_DROP_MEMBERSHIP: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_LEAVE_GROUP: fmode = MODE_IS_INCLUDE; optfn = ip_opt_delete_group; break; } if (mcast_opt) { greqp = (struct group_req *)i1; sin = (struct sockaddr_in *)&greqp->gr_group; if (sin->sin_family != AF_INET) { *outlenp = 0; return (ENOPROTOOPT); } group = (ipaddr_t)sin->sin_addr.s_addr; ifaddr = INADDR_ANY; ifindexp = &greqp->gr_interface; } else { mreqp = (struct ip_mreq *)i1; group = (ipaddr_t)mreqp->imr_multiaddr.s_addr; ifaddr = (ipaddr_t)mreqp->imr_interface.s_addr; ifindexp = NULL; } /* * In the multirouting case, we need to replicate * the request on all interfaces that will take part * in replication. We do so because multirouting is * reflective, thus we will probably receive multi- * casts on those interfaces. * The ip_multirt_apply_membership() succeeds if the * operation succeeds on at least one interface. */ ire = ire_ftable_lookup(group, IP_HOST_MASK, 0, IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); if (ire != NULL) { if (ire->ire_flags & RTF_MULTIRT) { error = ip_multirt_apply_membership( optfn, ire, connp, checkonly, group, fmode, INADDR_ANY, first_mp); done = B_TRUE; } ire_refrele(ire); } if (!done) { error = optfn(connp, checkonly, group, ifaddr, ifindexp, fmode, INADDR_ANY, first_mp); } if (error) { /* * EINPROGRESS is a soft error, needs retry * so don't make *outlenp zero. */ if (error != EINPROGRESS) *outlenp = 0; return (error); } /* OK return - copy input buffer into output buffer */ if (invalp != outvalp) { /* don't trust bcopy for identical src/dst */ bcopy(invalp, outvalp, inlen); } *outlenp = inlen; return (0); } case IP_BLOCK_SOURCE: case IP_UNBLOCK_SOURCE: case IP_ADD_SOURCE_MEMBERSHIP: case IP_DROP_SOURCE_MEMBERSHIP: case MCAST_BLOCK_SOURCE: case MCAST_UNBLOCK_SOURCE: case MCAST_JOIN_SOURCE_GROUP: case MCAST_LEAVE_SOURCE_GROUP: { struct ip_mreq_source *imreqp; struct group_source_req *gsreqp; in_addr_t grp, src, ifaddr = INADDR_ANY; uint32_t ifindex = 0; mcast_record_t fmode; struct sockaddr_in *sin; ire_t *ire; boolean_t mcast_opt = B_TRUE, done = B_FALSE; int (*optfn)(conn_t *, boolean_t, ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *); switch (name) { case IP_BLOCK_SOURCE: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_BLOCK_SOURCE: fmode = MODE_IS_EXCLUDE; optfn = ip_opt_add_group; break; case IP_UNBLOCK_SOURCE: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_UNBLOCK_SOURCE: fmode = MODE_IS_EXCLUDE; optfn = ip_opt_delete_group; break; case IP_ADD_SOURCE_MEMBERSHIP: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_JOIN_SOURCE_GROUP: fmode = MODE_IS_INCLUDE; optfn = ip_opt_add_group; break; case IP_DROP_SOURCE_MEMBERSHIP: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_LEAVE_SOURCE_GROUP: fmode = MODE_IS_INCLUDE; optfn = ip_opt_delete_group; break; } if (mcast_opt) { gsreqp = (struct group_source_req *)i1; if (gsreqp->gsr_group.ss_family != AF_INET) { *outlenp = 0; return (ENOPROTOOPT); } sin = (struct sockaddr_in *)&gsreqp->gsr_group; grp = (ipaddr_t)sin->sin_addr.s_addr; sin = (struct sockaddr_in *)&gsreqp->gsr_source; src = (ipaddr_t)sin->sin_addr.s_addr; ifindex = gsreqp->gsr_interface; } else { imreqp = (struct ip_mreq_source *)i1; grp = (ipaddr_t)imreqp->imr_multiaddr.s_addr; src = (ipaddr_t)imreqp->imr_sourceaddr.s_addr; ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr; } /* * In the multirouting case, we need to replicate * the request as noted in the mcast cases above. */ ire = ire_ftable_lookup(grp, IP_HOST_MASK, 0, IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); if (ire != NULL) { if (ire->ire_flags & RTF_MULTIRT) { error = ip_multirt_apply_membership( optfn, ire, connp, checkonly, grp, fmode, src, first_mp); done = B_TRUE; } ire_refrele(ire); } if (!done) { error = optfn(connp, checkonly, grp, ifaddr, &ifindex, fmode, src, first_mp); } if (error != 0) { /* * EINPROGRESS is a soft error, needs retry * so don't make *outlenp zero. */ if (error != EINPROGRESS) *outlenp = 0; return (error); } /* OK return - copy input buffer into output buffer */ if (invalp != outvalp) { bcopy(invalp, outvalp, inlen); } *outlenp = inlen; return (0); } case IP_SEC_OPT: error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); if (error != 0) { *outlenp = 0; return (error); } break; case IP_HDRINCL: case IP_OPTIONS: case T_IP_OPTIONS: case IP_TOS: case T_IP_TOS: case IP_TTL: case IP_RECVDSTADDR: case IP_RECVOPTS: /* OK return - copy input buffer into output buffer */ if (invalp != outvalp) { /* don't trust bcopy for identical src/dst */ bcopy(invalp, outvalp, inlen); } *outlenp = inlen; return (0); case IP_RECVIF: /* Retrieve the inbound interface index */ if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_recvif = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IP_RECVPKTINFO: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IP_RECVSLLA: /* Retrieve the source link layer address */ if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_recvslla = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case MRT_INIT: case MRT_DONE: case MRT_ADD_VIF: case MRT_DEL_VIF: case MRT_ADD_MFC: case MRT_DEL_MFC: case MRT_ASSERT: if ((error = secpolicy_ip_config(cr, B_FALSE)) != 0) { *outlenp = 0; return (error); } error = ip_mrouter_set((int)name, q, checkonly, (uchar_t *)invalp, inlen, first_mp); if (error) { *outlenp = 0; return (error); } /* OK return - copy input buffer into output buffer */ if (invalp != outvalp) { /* don't trust bcopy for identical src/dst */ bcopy(invalp, outvalp, inlen); } *outlenp = inlen; return (0); case IP_BOUND_IF: case IP_DHCPINIT_IF: error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, level, name, first_mp); if (error != 0) return (error); break; /* goto sizeof (int) option return */ case IP_UNSPEC_SRC: /* Allow sending with a zero source address */ if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_unspec_src = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ default: /* * "soft" error (negative) * option not handled at this level * Note: Do not modify *outlenp */ return (-EINVAL); } break; case IPPROTO_IPV6: switch (name) { case IPV6_BOUND_IF: case IPV6_BOUND_PIF: case IPV6_DONTFAILOVER_IF: error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, level, name, first_mp); if (error != 0) return (error); break; /* goto sizeof (int) option return */ case IPV6_MULTICAST_IF: /* * The only possible errors are EINPROGRESS and * EINVAL. EINPROGRESS will be restarted and is not * a hard error. We call this option on both V4 and V6 * If both return EINVAL, then this call returns * EINVAL. If at least one of them succeeds we * return success. */ found = B_FALSE; error = ip_opt_set_ill(connp, *i1, B_TRUE, checkonly, level, name, first_mp); if (error == EINPROGRESS) return (error); if (error == 0) found = B_TRUE; error = ip_opt_set_ill(connp, *i1, B_FALSE, checkonly, IPPROTO_IP, IP_MULTICAST_IF, first_mp); if (error == 0) found = B_TRUE; if (!found) return (error); break; /* goto sizeof (int) option return */ case IPV6_MULTICAST_HOPS: /* Recorded in transport above IP */ break; /* goto sizeof (int) option return */ case IPV6_MULTICAST_LOOP: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_multicast_loop = *i1; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_JOIN_GROUP: case MCAST_JOIN_GROUP: case IPV6_LEAVE_GROUP: case MCAST_LEAVE_GROUP: { struct ipv6_mreq *ip_mreqp; struct group_req *greqp; ire_t *ire; boolean_t done = B_FALSE; in6_addr_t groupv6; uint32_t ifindex; boolean_t mcast_opt = B_TRUE; mcast_record_t fmode; int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, int, mcast_record_t, const in6_addr_t *, mblk_t *); switch (name) { case IPV6_JOIN_GROUP: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_JOIN_GROUP: fmode = MODE_IS_EXCLUDE; optfn = ip_opt_add_group_v6; break; case IPV6_LEAVE_GROUP: mcast_opt = B_FALSE; /* FALLTHRU */ case MCAST_LEAVE_GROUP: fmode = MODE_IS_INCLUDE; optfn = ip_opt_delete_group_v6; break; } if (mcast_opt) { struct sockaddr_in *sin; struct sockaddr_in6 *sin6; greqp = (struct group_req *)i1; if (greqp->gr_group.ss_family == AF_INET) { sin = (struct sockaddr_in *) &(greqp->gr_group); IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &groupv6); } else { sin6 = (struct sockaddr_in6 *) &(greqp->gr_group); groupv6 = sin6->sin6_addr; } ifindex = greqp->gr_interface; } else { ip_mreqp = (struct ipv6_mreq *)i1; groupv6 = ip_mreqp->ipv6mr_multiaddr; ifindex = ip_mreqp->ipv6mr_interface; } /* * In the multirouting case, we need to replicate * the request on all interfaces that will take part * in replication. We do so because multirouting is * reflective, thus we will probably receive multi- * casts on those interfaces. * The ip_multirt_apply_membership_v6() succeeds if * the operation succeeds on at least one interface. */ ire = ire_ftable_lookup_v6(&groupv6, &ipv6_all_ones, 0, IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); if (ire != NULL) { if (ire->ire_flags & RTF_MULTIRT) { error = ip_multirt_apply_membership_v6( optfn, ire, connp, checkonly, &groupv6, fmode, &ipv6_all_zeros, first_mp); done = B_TRUE; } ire_refrele(ire); } if (!done) { error = optfn(connp, checkonly, &groupv6, ifindex, fmode, &ipv6_all_zeros, first_mp); } if (error) { /* * EINPROGRESS is a soft error, needs retry * so don't make *outlenp zero. */ if (error != EINPROGRESS) *outlenp = 0; return (error); } /* OK return - copy input buffer into output buffer */ if (invalp != outvalp) { /* don't trust bcopy for identical src/dst */ bcopy(invalp, outvalp, inlen); } *outlenp = inlen; return (0); } case MCAST_BLOCK_SOURCE: case MCAST_UNBLOCK_SOURCE: case MCAST_JOIN_SOURCE_GROUP: case MCAST_LEAVE_SOURCE_GROUP: { struct group_source_req *gsreqp; in6_addr_t v6grp, v6src; uint32_t ifindex; mcast_record_t fmode; ire_t *ire; boolean_t done = B_FALSE; int (*optfn)(conn_t *, boolean_t, const in6_addr_t *, int, mcast_record_t, const in6_addr_t *, mblk_t *); switch (name) { case MCAST_BLOCK_SOURCE: fmode = MODE_IS_EXCLUDE; optfn = ip_opt_add_group_v6; break; case MCAST_UNBLOCK_SOURCE: fmode = MODE_IS_EXCLUDE; optfn = ip_opt_delete_group_v6; break; case MCAST_JOIN_SOURCE_GROUP: fmode = MODE_IS_INCLUDE; optfn = ip_opt_add_group_v6; break; case MCAST_LEAVE_SOURCE_GROUP: fmode = MODE_IS_INCLUDE; optfn = ip_opt_delete_group_v6; break; } gsreqp = (struct group_source_req *)i1; ifindex = gsreqp->gsr_interface; if (gsreqp->gsr_group.ss_family == AF_INET) { struct sockaddr_in *s; s = (struct sockaddr_in *)&gsreqp->gsr_group; IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6grp); s = (struct sockaddr_in *)&gsreqp->gsr_source; IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src); } else { struct sockaddr_in6 *s6; s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group; v6grp = s6->sin6_addr; s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source; v6src = s6->sin6_addr; } /* * In the multirouting case, we need to replicate * the request as noted in the mcast cases above. */ ire = ire_ftable_lookup_v6(&v6grp, &ipv6_all_ones, 0, IRE_HOST, NULL, NULL, ALL_ZONES, 0, NULL, MATCH_IRE_MASK | MATCH_IRE_TYPE, ipst); if (ire != NULL) { if (ire->ire_flags & RTF_MULTIRT) { error = ip_multirt_apply_membership_v6( optfn, ire, connp, checkonly, &v6grp, fmode, &v6src, first_mp); done = B_TRUE; } ire_refrele(ire); } if (!done) { error = optfn(connp, checkonly, &v6grp, ifindex, fmode, &v6src, first_mp); } if (error != 0) { /* * EINPROGRESS is a soft error, needs retry * so don't make *outlenp zero. */ if (error != EINPROGRESS) *outlenp = 0; return (error); } /* OK return - copy input buffer into output buffer */ if (invalp != outvalp) { bcopy(invalp, outvalp, inlen); } *outlenp = inlen; return (0); } case IPV6_UNICAST_HOPS: /* Recorded in transport above IP */ break; /* goto sizeof (int) option return */ case IPV6_UNSPEC_SRC: /* Allow sending with a zero source address */ if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_unspec_src = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_RECVPKTINFO: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_ip_recvpktinfo = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_RECVTCLASS: if (!checkonly) { if (*i1 < 0 || *i1 > 1) { return (EINVAL); } mutex_enter(&connp->conn_lock); connp->conn_ipv6_recvtclass = *i1; mutex_exit(&connp->conn_lock); } break; case IPV6_RECVPATHMTU: if (!checkonly) { if (*i1 < 0 || *i1 > 1) { return (EINVAL); } mutex_enter(&connp->conn_lock); connp->conn_ipv6_recvpathmtu = *i1; mutex_exit(&connp->conn_lock); } break; case IPV6_RECVHOPLIMIT: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_ipv6_recvhoplimit = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_RECVHOPOPTS: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_ipv6_recvhopopts = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_RECVDSTOPTS: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_ipv6_recvdstopts = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_RECVRTHDR: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_ipv6_recvrthdr = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_RECVRTHDRDSTOPTS: if (!checkonly) { mutex_enter(&connp->conn_lock); connp->conn_ipv6_recvrtdstopts = *i1 ? 1 : 0; mutex_exit(&connp->conn_lock); } break; /* goto sizeof (int) option return */ case IPV6_PKTINFO: if (inlen == 0) return (-EINVAL); /* clearing option */ error = ip6_set_pktinfo(cr, connp, (struct in6_pktinfo *)invalp, first_mp); if (error != 0) *outlenp = 0; else *outlenp = inlen; return (error); case IPV6_NEXTHOP: { struct sockaddr_in6 *sin6; /* Verify that the nexthop is reachable */ if (inlen == 0) return (-EINVAL); /* clearing option */ sin6 = (struct sockaddr_in6 *)invalp; ire = ire_route_lookup_v6(&sin6->sin6_addr, 0, 0, 0, NULL, NULL, connp->conn_zoneid, NULL, MATCH_IRE_DEFAULT, ipst); if (ire == NULL) { *outlenp = 0; return (EHOSTUNREACH); } ire_refrele(ire); return (-EINVAL); } case IPV6_SEC_OPT: error = ipsec_set_req(cr, connp, (ipsec_req_t *)invalp); if (error != 0) { *outlenp = 0; return (error); } break; case IPV6_SRC_PREFERENCES: { /* * This is implemented strictly in the ip module * (here and in tcp_opt_*() to accomodate tcp * sockets). Modules above ip pass this option * down here since ip is the only one that needs to * be aware of source address preferences. * * This socket option only affects connected * sockets that haven't already bound to a specific * IPv6 address. In other words, sockets that * don't call bind() with an address other than the * unspecified address and that call connect(). * ip_bind_connected_v6() passes these preferences * to the ipif_select_source_v6() function. */ if (inlen != sizeof (uint32_t)) return (EINVAL); error = ip6_set_src_preferences(connp, *(uint32_t *)invalp); if (error != 0) { *outlenp = 0; return (error); } else { *outlenp = sizeof (uint32_t); } break; } case IPV6_V6ONLY: if (*i1 < 0 || *i1 > 1) { return (EINVAL); } mutex_enter(&connp->conn_lock); connp->conn_ipv6_v6only = *i1; mutex_exit(&connp->conn_lock); break; default: return (-EINVAL); } break; default: /* * "soft" error (negative) * option not handled at this level * Note: Do not modify *outlenp */ return (-EINVAL); } /* * Common case of return from an option that is sizeof (int) */ *(int *)outvalp = *i1; *outlenp = sizeof (int); return (0); } /* * This routine gets default values of certain options whose default * values are maintained by protocol specific code */ /* ARGSUSED */ int ip_opt_default(queue_t *q, int level, int name, uchar_t *ptr) { int *i1 = (int *)ptr; ip_stack_t *ipst = CONNQ_TO_IPST(q); switch (level) { case IPPROTO_IP: switch (name) { case IP_MULTICAST_TTL: *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL; return (sizeof (uchar_t)); case IP_MULTICAST_LOOP: *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP; return (sizeof (uchar_t)); default: return (-1); } case IPPROTO_IPV6: switch (name) { case IPV6_UNICAST_HOPS: *i1 = ipst->ips_ipv6_def_hops; return (sizeof (int)); case IPV6_MULTICAST_HOPS: *i1 = IP_DEFAULT_MULTICAST_TTL; return (sizeof (int)); case IPV6_MULTICAST_LOOP: *i1 = IP_DEFAULT_MULTICAST_LOOP; return (sizeof (int)); case IPV6_V6ONLY: *i1 = 1; return (sizeof (int)); default: return (-1); } default: return (-1); } /* NOTREACHED */ } /* * Given a destination address and a pointer to where to put the information * this routine fills in the mtuinfo. */ int ip_fill_mtuinfo(struct in6_addr *in6, in_port_t port, struct ip6_mtuinfo *mtuinfo, netstack_t *ns) { ire_t *ire; ip_stack_t *ipst = ns->netstack_ip; if (IN6_IS_ADDR_UNSPECIFIED(in6)) return (-1); bzero(mtuinfo, sizeof (*mtuinfo)); mtuinfo->ip6m_addr.sin6_family = AF_INET6; mtuinfo->ip6m_addr.sin6_port = port; mtuinfo->ip6m_addr.sin6_addr = *in6; ire = ire_cache_lookup_v6(in6, ALL_ZONES, NULL, ipst); if (ire != NULL) { mtuinfo->ip6m_mtu = ire->ire_max_frag; ire_refrele(ire); } else { mtuinfo->ip6m_mtu = IPV6_MIN_MTU; } return (sizeof (struct ip6_mtuinfo)); } /* * This routine gets socket options. For MRT_VERSION and MRT_ASSERT, error * checking of GET_QUEUE_CRED(q) and that ip_g_mrouter is set should be done and * isn't. This doesn't matter as the error checking is done properly for the * other MRT options coming in through ip_opt_set. */ int ip_opt_get(queue_t *q, int level, int name, uchar_t *ptr) { conn_t *connp = Q_TO_CONN(q); ipsec_req_t *req = (ipsec_req_t *)ptr; switch (level) { case IPPROTO_IP: switch (name) { case MRT_VERSION: case MRT_ASSERT: (void) ip_mrouter_get(name, q, ptr); return (sizeof (int)); case IP_SEC_OPT: return (ipsec_req_from_conn(connp, req, IPSEC_AF_V4)); case IP_NEXTHOP: if (connp->conn_nexthop_set) { *(ipaddr_t *)ptr = connp->conn_nexthop_v4; return (sizeof (ipaddr_t)); } else return (0); case IP_RECVPKTINFO: *(int *)ptr = connp->conn_ip_recvpktinfo ? 1: 0; return (sizeof (int)); default: break; } break; case IPPROTO_IPV6: switch (name) { case IPV6_SEC_OPT: return (ipsec_req_from_conn(connp, req, IPSEC_AF_V6)); case IPV6_SRC_PREFERENCES: { return (ip6_get_src_preferences(connp, (uint32_t *)ptr)); } case IPV6_V6ONLY: *(int *)ptr = connp->conn_ipv6_v6only ? 1 : 0; return (sizeof (int)); case IPV6_PATHMTU: return (ip_fill_mtuinfo(&connp->conn_remv6, 0, (struct ip6_mtuinfo *)ptr, connp->conn_netstack)); default: break; } break; default: break; } return (-1); } /* Named Dispatch routine to get a current value out of our parameter table. */ /* ARGSUSED */ static int ip_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) { ipparam_t *ippa = (ipparam_t *)cp; (void) mi_mpprintf(mp, "%d", ippa->ip_param_value); return (0); } /* ARGSUSED */ static int ip_param_generic_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) { (void) mi_mpprintf(mp, "%d", *(int *)cp); return (0); } /* * Set ip{,6}_forwarding values. This means walking through all of the * ill's and toggling their forwarding values. */ /* ARGSUSED */ static int ip_forward_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) { long new_value; int *forwarding_value = (int *)cp; ill_t *ill; boolean_t isv6; ill_walk_context_t ctx; ip_stack_t *ipst = CONNQ_TO_IPST(q); isv6 = (forwarding_value == &ipst->ips_ipv6_forward); if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value < 0 || new_value > 1) { return (EINVAL); } *forwarding_value = new_value; /* * Regardless of the current value of ip_forwarding, set all per-ill * values of ip_forwarding to the value being set. * * Bring all the ill's up to date with the new global value. */ rw_enter(&ipst->ips_ill_g_lock, RW_READER); if (isv6) ill = ILL_START_WALK_V6(&ctx, ipst); else ill = ILL_START_WALK_V4(&ctx, ipst); for (; ill != NULL; ill = ill_next(&ctx, ill)) (void) ill_forward_set(ill, new_value != 0); rw_exit(&ipst->ips_ill_g_lock); return (0); } /* * Walk through the param array specified registering each element with the * Named Dispatch handler. This is called only during init. So it is ok * not to acquire any locks */ static boolean_t ip_param_register(IDP *ndp, ipparam_t *ippa, size_t ippa_cnt, ipndp_t *ipnd, size_t ipnd_cnt) { for (; ippa_cnt-- > 0; ippa++) { if (ippa->ip_param_name && ippa->ip_param_name[0]) { if (!nd_load(ndp, ippa->ip_param_name, ip_param_get, ip_param_set, (caddr_t)ippa)) { nd_free(ndp); return (B_FALSE); } } } for (; ipnd_cnt-- > 0; ipnd++) { if (ipnd->ip_ndp_name && ipnd->ip_ndp_name[0]) { if (!nd_load(ndp, ipnd->ip_ndp_name, ipnd->ip_ndp_getf, ipnd->ip_ndp_setf, ipnd->ip_ndp_data)) { nd_free(ndp); return (B_FALSE); } } } return (B_TRUE); } /* Named Dispatch routine to negotiate a new value for one of our parameters. */ /* ARGSUSED */ static int ip_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *ioc_cr) { long new_value; ipparam_t *ippa = (ipparam_t *)cp; if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value < ippa->ip_param_min || new_value > ippa->ip_param_max) { return (EINVAL); } ippa->ip_param_value = new_value; return (0); } /* * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases, * When an ipf is passed here for the first time, if * we already have in-order fragments on the queue, we convert from the fast- * path reassembly scheme to the hard-case scheme. From then on, additional * fragments are reassembled here. We keep track of the start and end offsets * of each piece, and the number of holes in the chain. When the hole count * goes to zero, we are done! * * The ipf_count will be updated to account for any mblk(s) added (pointed to * by mp) or subtracted (freeb()ed dups), upon return the caller must update * ipfb_count and ill_frag_count by the difference of ipf_count before and * after the call to ip_reassemble(). */ int ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill, size_t msg_len) { uint_t end; mblk_t *next_mp; mblk_t *mp1; uint_t offset; boolean_t incr_dups = B_TRUE; boolean_t offset_zero_seen = B_FALSE; boolean_t pkt_boundary_checked = B_FALSE; /* If start == 0 then ipf_nf_hdr_len has to be set. */ ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0); /* Add in byte count */ ipf->ipf_count += msg_len; if (ipf->ipf_end) { /* * We were part way through in-order reassembly, but now there * is a hole. We walk through messages already queued, and * mark them for hard case reassembly. We know that up till * now they were in order starting from offset zero. */ offset = 0; for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { IP_REASS_SET_START(mp1, offset); if (offset == 0) { ASSERT(ipf->ipf_nf_hdr_len != 0); offset = -ipf->ipf_nf_hdr_len; } offset += mp1->b_wptr - mp1->b_rptr; IP_REASS_SET_END(mp1, offset); } /* One hole at the end. */ ipf->ipf_hole_cnt = 1; /* Brand it as a hard case, forever. */ ipf->ipf_end = 0; } /* Walk through all the new pieces. */ do { end = start + (mp->b_wptr - mp->b_rptr); /* * If start is 0, decrease 'end' only for the first mblk of * the fragment. Otherwise 'end' can get wrong value in the * second pass of the loop if first mblk is exactly the * size of ipf_nf_hdr_len. */ if (start == 0 && !offset_zero_seen) { /* First segment */ ASSERT(ipf->ipf_nf_hdr_len != 0); end -= ipf->ipf_nf_hdr_len; offset_zero_seen = B_TRUE; } next_mp = mp->b_cont; /* * We are checking to see if there is any interesing data * to process. If there isn't and the mblk isn't the * one which carries the unfragmentable header then we * drop it. It's possible to have just the unfragmentable * header come through without any data. That needs to be * saved. * * If the assert at the top of this function holds then the * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code * is infrequently traveled enough that the test is left in * to protect against future code changes which break that * invariant. */ if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) { /* Empty. Blast it. */ IP_REASS_SET_START(mp, 0); IP_REASS_SET_END(mp, 0); /* * If the ipf points to the mblk we are about to free, * update ipf to point to the next mblk (or NULL * if none). */ if (ipf->ipf_mp->b_cont == mp) ipf->ipf_mp->b_cont = next_mp; freeb(mp); continue; } mp->b_cont = NULL; IP_REASS_SET_START(mp, start); IP_REASS_SET_END(mp, end); if (!ipf->ipf_tail_mp) { ipf->ipf_tail_mp = mp; ipf->ipf_mp->b_cont = mp; if (start == 0 || !more) { ipf->ipf_hole_cnt = 1; /* * if the first fragment comes in more than one * mblk, this loop will be executed for each * mblk. Need to adjust hole count so exiting * this routine will leave hole count at 1. */ if (next_mp) ipf->ipf_hole_cnt++; } else ipf->ipf_hole_cnt = 2; continue; } else if (ipf->ipf_last_frag_seen && !more && !pkt_boundary_checked) { /* * We check datagram boundary only if this fragment * claims to be the last fragment and we have seen a * last fragment in the past too. We do this only * once for a given fragment. * * start cannot be 0 here as fragments with start=0 * and MF=0 gets handled as a complete packet. These * fragments should not reach here. */ if (start + msgdsize(mp) != IP_REASS_END(ipf->ipf_tail_mp)) { /* * We have two fragments both of which claim * to be the last fragment but gives conflicting * information about the whole datagram size. * Something fishy is going on. Drop the * fragment and free up the reassembly list. */ return (IP_REASS_FAILED); } /* * We shouldn't come to this code block again for this * particular fragment. */ pkt_boundary_checked = B_TRUE; } /* New stuff at or beyond tail? */ offset = IP_REASS_END(ipf->ipf_tail_mp); if (start >= offset) { if (ipf->ipf_last_frag_seen) { /* current fragment is beyond last fragment */ return (IP_REASS_FAILED); } /* Link it on end. */ ipf->ipf_tail_mp->b_cont = mp; ipf->ipf_tail_mp = mp; if (more) { if (start != offset) ipf->ipf_hole_cnt++; } else if (start == offset && next_mp == NULL) ipf->ipf_hole_cnt--; continue; } mp1 = ipf->ipf_mp->b_cont; offset = IP_REASS_START(mp1); /* New stuff at the front? */ if (start < offset) { if (start == 0) { if (end >= offset) { /* Nailed the hole at the begining. */ ipf->ipf_hole_cnt--; } } else if (end < offset) { /* * A hole, stuff, and a hole where there used * to be just a hole. */ ipf->ipf_hole_cnt++; } mp->b_cont = mp1; /* Check for overlap. */ while (end > offset) { if (end < IP_REASS_END(mp1)) { mp->b_wptr -= end - offset; IP_REASS_SET_END(mp, offset); BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmPartDups); break; } /* Did we cover another hole? */ if ((mp1->b_cont && IP_REASS_END(mp1) != IP_REASS_START(mp1->b_cont) && end >= IP_REASS_START(mp1->b_cont)) || (!ipf->ipf_last_frag_seen && !more)) { ipf->ipf_hole_cnt--; } /* Clip out mp1. */ if ((mp->b_cont = mp1->b_cont) == NULL) { /* * After clipping out mp1, this guy * is now hanging off the end. */ ipf->ipf_tail_mp = mp; } IP_REASS_SET_START(mp1, 0); IP_REASS_SET_END(mp1, 0); /* Subtract byte count */ ipf->ipf_count -= mp1->b_datap->db_lim - mp1->b_datap->db_base; freeb(mp1); BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmPartDups); mp1 = mp->b_cont; if (!mp1) break; offset = IP_REASS_START(mp1); } ipf->ipf_mp->b_cont = mp; continue; } /* * The new piece starts somewhere between the start of the head * and before the end of the tail. */ for (; mp1; mp1 = mp1->b_cont) { offset = IP_REASS_END(mp1); if (start < offset) { if (end <= offset) { /* Nothing new. */ IP_REASS_SET_START(mp, 0); IP_REASS_SET_END(mp, 0); /* Subtract byte count */ ipf->ipf_count -= mp->b_datap->db_lim - mp->b_datap->db_base; if (incr_dups) { ipf->ipf_num_dups++; incr_dups = B_FALSE; } freeb(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmDuplicates); break; } /* * Trim redundant stuff off beginning of new * piece. */ IP_REASS_SET_START(mp, offset); mp->b_rptr += offset - start; BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmPartDups); start = offset; if (!mp1->b_cont) { /* * After trimming, this guy is now * hanging off the end. */ mp1->b_cont = mp; ipf->ipf_tail_mp = mp; if (!more) { ipf->ipf_hole_cnt--; } break; } } if (start >= IP_REASS_START(mp1->b_cont)) continue; /* Fill a hole */ if (start > offset) ipf->ipf_hole_cnt++; mp->b_cont = mp1->b_cont; mp1->b_cont = mp; mp1 = mp->b_cont; offset = IP_REASS_START(mp1); if (end >= offset) { ipf->ipf_hole_cnt--; /* Check for overlap. */ while (end > offset) { if (end < IP_REASS_END(mp1)) { mp->b_wptr -= end - offset; IP_REASS_SET_END(mp, offset); /* * TODO we might bump * this up twice if there is * overlap at both ends. */ BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmPartDups); break; } /* Did we cover another hole? */ if ((mp1->b_cont && IP_REASS_END(mp1) != IP_REASS_START(mp1->b_cont) && end >= IP_REASS_START(mp1->b_cont)) || (!ipf->ipf_last_frag_seen && !more)) { ipf->ipf_hole_cnt--; } /* Clip out mp1. */ if ((mp->b_cont = mp1->b_cont) == NULL) { /* * After clipping out mp1, * this guy is now hanging * off the end. */ ipf->ipf_tail_mp = mp; } IP_REASS_SET_START(mp1, 0); IP_REASS_SET_END(mp1, 0); /* Subtract byte count */ ipf->ipf_count -= mp1->b_datap->db_lim - mp1->b_datap->db_base; freeb(mp1); BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmPartDups); mp1 = mp->b_cont; if (!mp1) break; offset = IP_REASS_START(mp1); } } break; } } while (start = end, mp = next_mp); /* Fragment just processed could be the last one. Remember this fact */ if (!more) ipf->ipf_last_frag_seen = B_TRUE; /* Still got holes? */ if (ipf->ipf_hole_cnt) return (IP_REASS_PARTIAL); /* Clean up overloaded fields to avoid upstream disasters. */ for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) { IP_REASS_SET_START(mp1, 0); IP_REASS_SET_END(mp1, 0); } return (IP_REASS_COMPLETE); } /* * ipsec processing for the fast path, used for input UDP Packets * Returns true if ready for passup to UDP. * Return false if packet is not passable to UDP (e.g. it failed IPsec policy, * was an ESP-in-UDP packet, etc.). */ static boolean_t ip_udp_check(queue_t *q, conn_t *connp, ill_t *ill, ipha_t *ipha, mblk_t **mpp, mblk_t **first_mpp, boolean_t mctl_present, ire_t *ire) { uint32_t ill_index; uint_t in_flags; /* IPF_RECVSLLA and/or IPF_RECVIF */ ip_stack_t *ipst = connp->conn_netstack->netstack_ip; ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; udp_t *udp = connp->conn_udp; ASSERT(ipha->ipha_protocol == IPPROTO_UDP); /* The ill_index of the incoming ILL */ ill_index = ((ill_t *)q->q_ptr)->ill_phyint->phyint_ifindex; /* pass packet up to the transport */ if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { *first_mpp = ipsec_check_inbound_policy(*first_mpp, connp, ipha, NULL, mctl_present); if (*first_mpp == NULL) { return (B_FALSE); } } /* Initiate IPPF processing for fastpath UDP */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { ip_process(IPP_LOCAL_IN, mpp, ill_index); if (*mpp == NULL) { ip2dbg(("ip_input_ipsec_process: UDP pkt " "deferred/dropped during IPPF processing\n")); return (B_FALSE); } } /* * Remove 0-spi if it's 0, or move everything behind * the UDP header over it and forward to ESP via * ip_proto_input(). */ if (udp->udp_nat_t_endpoint) { if (mctl_present) { /* mctl_present *shouldn't* happen. */ ip_drop_packet(*first_mpp, B_TRUE, NULL, NULL, DROPPER(ipss, ipds_esp_nat_t_ipsec), &ipss->ipsec_dropper); *first_mpp = NULL; return (B_FALSE); } /* "ill" is "recv_ill" in actuality. */ if (!zero_spi_check(q, *mpp, ire, ill, ipss)) return (B_FALSE); /* Else continue like a normal UDP packet. */ } /* * We make the checks as below since we are in the fast path * and want to minimize the number of checks if the IP_RECVIF and/or * IP_RECVSLLA and/or IPV6_RECVPKTINFO options are not set */ if (connp->conn_recvif || connp->conn_recvslla || connp->conn_ip_recvpktinfo) { if (connp->conn_recvif) { in_flags = IPF_RECVIF; } /* * UDP supports IP_RECVPKTINFO option for both v4 and v6 * so the flag passed to ip_add_info is based on IP version * of connp. */ if (connp->conn_ip_recvpktinfo) { if (connp->conn_af_isv6) { /* * V6 only needs index */ in_flags |= IPF_RECVIF; } else { /* * V4 needs index + matching address. */ in_flags |= IPF_RECVADDR; } } if (connp->conn_recvslla) { in_flags |= IPF_RECVSLLA; } /* * since in_flags are being set ill will be * referenced in ip_add_info, so it better not * be NULL. */ /* * the actual data will be contained in b_cont * upon successful return of the following call. * If the call fails then the original mblk is * returned. */ *mpp = ip_add_info(*mpp, ill, in_flags, IPCL_ZONEID(connp), ipst); } return (B_TRUE); } /* * Fragmentation reassembly. Each ILL has a hash table for * queuing packets undergoing reassembly for all IPIFs * associated with the ILL. The hash is based on the packet * IP ident field. The ILL frag hash table was allocated * as a timer block at the time the ILL was created. Whenever * there is anything on the reassembly queue, the timer will * be running. Returns B_TRUE if successful else B_FALSE; * frees mp on failure. */ static boolean_t ip_rput_fragment(queue_t *q, mblk_t **mpp, ipha_t *ipha, uint32_t *cksum_val, uint16_t *cksum_flags) { uint32_t frag_offset_flags; ill_t *ill = (ill_t *)q->q_ptr; mblk_t *mp = *mpp; mblk_t *t_mp; ipaddr_t dst; uint8_t proto = ipha->ipha_protocol; uint32_t sum_val; uint16_t sum_flags; ipf_t *ipf; ipf_t **ipfp; ipfb_t *ipfb; uint16_t ident; uint32_t offset; ipaddr_t src; uint_t hdr_length; uint32_t end; mblk_t *mp1; mblk_t *tail_mp; size_t count; size_t msg_len; uint8_t ecn_info = 0; uint32_t packet_size; boolean_t pruned = B_FALSE; ip_stack_t *ipst = ill->ill_ipst; if (cksum_val != NULL) *cksum_val = 0; if (cksum_flags != NULL) *cksum_flags = 0; /* * Drop the fragmented as early as possible, if * we don't have resource(s) to re-assemble. */ if (ipst->ips_ip_reass_queue_bytes == 0) { freemsg(mp); return (B_FALSE); } /* Check for fragmentation offset; return if there's none */ if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) & (IPH_MF | IPH_OFFSET)) == 0) return (B_TRUE); /* * We utilize hardware computed checksum info only for UDP since * IP fragmentation is a normal occurence for the protocol. In * addition, checksum offload support for IP fragments carrying * UDP payload is commonly implemented across network adapters. */ ASSERT(ill != NULL); if (proto == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { mblk_t *mp1 = mp->b_cont; int32_t len; /* Record checksum information from the packet */ sum_val = (uint32_t)DB_CKSUM16(mp); sum_flags = DB_CKSUMFLAGS(mp); /* IP payload offset from beginning of mblk */ offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr; if ((sum_flags & HCK_PARTIALCKSUM) && (mp1 == NULL || mp1->b_cont == NULL) && offset >= DB_CKSUMSTART(mp) && ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) { uint32_t adj; /* * Partial checksum has been calculated by hardware * and attached to the packet; in addition, any * prepended extraneous data is even byte aligned. * If any such data exists, we adjust the checksum; * this would also handle any postpended data. */ IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), mp, mp1, len, adj); /* One's complement subtract extraneous checksum */ if (adj >= sum_val) sum_val = ~(adj - sum_val) & 0xFFFF; else sum_val -= adj; } } else { sum_val = 0; sum_flags = 0; } /* Clear hardware checksumming flag */ DB_CKSUMFLAGS(mp) = 0; ident = ipha->ipha_ident; offset = (frag_offset_flags << 3) & 0xFFFF; src = ipha->ipha_src; dst = ipha->ipha_dst; hdr_length = IPH_HDR_LENGTH(ipha); end = ntohs(ipha->ipha_length) - hdr_length; /* If end == 0 then we have a packet with no data, so just free it */ if (end == 0) { freemsg(mp); return (B_FALSE); } /* Record the ECN field info. */ ecn_info = (ipha->ipha_type_of_service & 0x3); if (offset != 0) { /* * If this isn't the first piece, strip the header, and * add the offset to the end value. */ mp->b_rptr += hdr_length; end += offset; } msg_len = MBLKSIZE(mp); tail_mp = mp; while (tail_mp->b_cont != NULL) { tail_mp = tail_mp->b_cont; msg_len += MBLKSIZE(tail_mp); } /* If the reassembly list for this ILL will get too big, prune it */ if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= ipst->ips_ip_reass_queue_bytes) { ill_frag_prune(ill, (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 : (ipst->ips_ip_reass_queue_bytes - msg_len)); pruned = B_TRUE; } ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)]; mutex_enter(&ipfb->ipfb_lock); ipfp = &ipfb->ipfb_ipf; /* Try to find an existing fragment queue for this packet. */ for (;;) { ipf = ipfp[0]; if (ipf != NULL) { /* * It has to match on ident and src/dst address. */ if (ipf->ipf_ident == ident && ipf->ipf_src == src && ipf->ipf_dst == dst && ipf->ipf_protocol == proto) { /* * If we have received too many * duplicate fragments for this packet * free it. */ if (ipf->ipf_num_dups > ip_max_frag_dups) { ill_frag_free_pkts(ill, ipfb, ipf, 1); freemsg(mp); mutex_exit(&ipfb->ipfb_lock); return (B_FALSE); } /* Found it. */ break; } ipfp = &ipf->ipf_hash_next; continue; } /* * If we pruned the list, do we want to store this new * fragment?. We apply an optimization here based on the * fact that most fragments will be received in order. * So if the offset of this incoming fragment is zero, * it is the first fragment of a new packet. We will * keep it. Otherwise drop the fragment, as we have * probably pruned the packet already (since the * packet cannot be found). */ if (pruned && offset != 0) { mutex_exit(&ipfb->ipfb_lock); freemsg(mp); return (B_FALSE); } if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) { /* * Too many fragmented packets in this hash * bucket. Free the oldest. */ ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); } /* New guy. Allocate a frag message. */ mp1 = allocb(sizeof (*ipf), BPRI_MED); if (mp1 == NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(mp); reass_done: mutex_exit(&ipfb->ipfb_lock); return (B_FALSE); } BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds); mp1->b_cont = mp; /* Initialize the fragment header. */ ipf = (ipf_t *)mp1->b_rptr; ipf->ipf_mp = mp1; ipf->ipf_ptphn = ipfp; ipfp[0] = ipf; ipf->ipf_hash_next = NULL; ipf->ipf_ident = ident; ipf->ipf_protocol = proto; ipf->ipf_src = src; ipf->ipf_dst = dst; ipf->ipf_nf_hdr_len = 0; /* Record reassembly start time. */ ipf->ipf_timestamp = gethrestime_sec(); /* Record ipf generation and account for frag header */ ipf->ipf_gen = ill->ill_ipf_gen++; ipf->ipf_count = MBLKSIZE(mp1); ipf->ipf_last_frag_seen = B_FALSE; ipf->ipf_ecn = ecn_info; ipf->ipf_num_dups = 0; ipfb->ipfb_frag_pkts++; ipf->ipf_checksum = 0; ipf->ipf_checksum_flags = 0; /* Store checksum value in fragment header */ if (sum_flags != 0) { sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); ipf->ipf_checksum = sum_val; ipf->ipf_checksum_flags = sum_flags; } /* * We handle reassembly two ways. In the easy case, * where all the fragments show up in order, we do * minimal bookkeeping, and just clip new pieces on * the end. If we ever see a hole, then we go off * to ip_reassemble which has to mark the pieces and * keep track of the number of holes, etc. Obviously, * the point of having both mechanisms is so we can * handle the easy case as efficiently as possible. */ if (offset == 0) { /* Easy case, in-order reassembly so far. */ ipf->ipf_count += msg_len; ipf->ipf_tail_mp = tail_mp; /* * Keep track of next expected offset in * ipf_end. */ ipf->ipf_end = end; ipf->ipf_nf_hdr_len = hdr_length; } else { /* Hard case, hole at the beginning. */ ipf->ipf_tail_mp = NULL; /* * ipf_end == 0 means that we have given up * on easy reassembly. */ ipf->ipf_end = 0; /* Forget checksum offload from now on */ ipf->ipf_checksum_flags = 0; /* * ipf_hole_cnt is set by ip_reassemble. * ipf_count is updated by ip_reassemble. * No need to check for return value here * as we don't expect reassembly to complete * or fail for the first fragment itself. */ (void) ip_reassemble(mp, ipf, (frag_offset_flags & IPH_OFFSET) << 3, (frag_offset_flags & IPH_MF), ill, msg_len); } /* Update per ipfb and ill byte counts */ ipfb->ipfb_count += ipf->ipf_count; ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ atomic_add_32(&ill->ill_frag_count, ipf->ipf_count); /* If the frag timer wasn't already going, start it. */ mutex_enter(&ill->ill_lock); ill_frag_timer_start(ill); mutex_exit(&ill->ill_lock); goto reass_done; } /* * If the packet's flag has changed (it could be coming up * from an interface different than the previous, therefore * possibly different checksum capability), then forget about * any stored checksum states. Otherwise add the value to * the existing one stored in the fragment header. */ if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { sum_val += ipf->ipf_checksum; sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); ipf->ipf_checksum = sum_val; } else if (ipf->ipf_checksum_flags != 0) { /* Forget checksum offload from now on */ ipf->ipf_checksum_flags = 0; } /* * We have a new piece of a datagram which is already being * reassembled. Update the ECN info if all IP fragments * are ECN capable. If there is one which is not, clear * all the info. If there is at least one which has CE * code point, IP needs to report that up to transport. */ if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { if (ecn_info == IPH_ECN_CE) ipf->ipf_ecn = IPH_ECN_CE; } else { ipf->ipf_ecn = IPH_ECN_NECT; } if (offset && ipf->ipf_end == offset) { /* The new fragment fits at the end */ ipf->ipf_tail_mp->b_cont = mp; /* Update the byte count */ ipf->ipf_count += msg_len; /* Update per ipfb and ill byte counts */ ipfb->ipfb_count += msg_len; ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ atomic_add_32(&ill->ill_frag_count, msg_len); if (frag_offset_flags & IPH_MF) { /* More to come. */ ipf->ipf_end = end; ipf->ipf_tail_mp = tail_mp; goto reass_done; } } else { /* Go do the hard cases. */ int ret; if (offset == 0) ipf->ipf_nf_hdr_len = hdr_length; /* Save current byte count */ count = ipf->ipf_count; ret = ip_reassemble(mp, ipf, (frag_offset_flags & IPH_OFFSET) << 3, (frag_offset_flags & IPH_MF), ill, msg_len); /* Count of bytes added and subtracted (freeb()ed) */ count = ipf->ipf_count - count; if (count) { /* Update per ipfb and ill byte counts */ ipfb->ipfb_count += count; ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ atomic_add_32(&ill->ill_frag_count, count); } if (ret == IP_REASS_PARTIAL) { goto reass_done; } else if (ret == IP_REASS_FAILED) { /* Reassembly failed. Free up all resources */ ill_frag_free_pkts(ill, ipfb, ipf, 1); for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { IP_REASS_SET_START(t_mp, 0); IP_REASS_SET_END(t_mp, 0); } freemsg(mp); goto reass_done; } /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ } /* * We have completed reassembly. Unhook the frag header from * the reassembly list. * * Before we free the frag header, record the ECN info * to report back to the transport. */ ecn_info = ipf->ipf_ecn; BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs); ipfp = ipf->ipf_ptphn; /* We need to supply these to caller */ if ((sum_flags = ipf->ipf_checksum_flags) != 0) sum_val = ipf->ipf_checksum; else sum_val = 0; mp1 = ipf->ipf_mp; count = ipf->ipf_count; ipf = ipf->ipf_hash_next; if (ipf != NULL) ipf->ipf_ptphn = ipfp; ipfp[0] = ipf; atomic_add_32(&ill->ill_frag_count, -count); ASSERT(ipfb->ipfb_count >= count); ipfb->ipfb_count -= count; ipfb->ipfb_frag_pkts--; mutex_exit(&ipfb->ipfb_lock); /* Ditch the frag header. */ mp = mp1->b_cont; freeb(mp1); /* Restore original IP length in header. */ packet_size = (uint32_t)msgdsize(mp); if (packet_size > IP_MAXPACKET) { freemsg(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); return (B_FALSE); } if (DB_REF(mp) > 1) { mblk_t *mp2 = copymsg(mp); freemsg(mp); if (mp2 == NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return (B_FALSE); } mp = mp2; } ipha = (ipha_t *)mp->b_rptr; ipha->ipha_length = htons((uint16_t)packet_size); /* We're now complete, zip the frag state */ ipha->ipha_fragment_offset_and_flags = 0; /* Record the ECN info. */ ipha->ipha_type_of_service &= 0xFC; ipha->ipha_type_of_service |= ecn_info; *mpp = mp; /* Reassembly is successful; return checksum information if needed */ if (cksum_val != NULL) *cksum_val = sum_val; if (cksum_flags != NULL) *cksum_flags = sum_flags; return (B_TRUE); } /* * Perform ip header check sum update local options. * return B_TRUE if all is well, else return B_FALSE and release * the mp. caller is responsible for decrementing ire ref cnt. */ static boolean_t ip_options_cksum(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) { mblk_t *first_mp; boolean_t mctl_present; uint16_t sum; EXTRACT_PKT_MP(mp, first_mp, mctl_present); /* * Don't do the checksum if it has gone through AH/ESP * processing. */ if (!mctl_present) { sum = ip_csum_hdr(ipha); if (sum != 0) { if (ill != NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); } else { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs); } freemsg(first_mp); return (B_FALSE); } } if (!ip_rput_local_options(q, mp, ipha, ire, ipst)) { if (mctl_present) freeb(first_mp); return (B_FALSE); } return (B_TRUE); } /* * All udp packet are delivered to the local host via this routine. */ void ip_udp_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, ill_t *recv_ill) { uint32_t sum; uint32_t u1; boolean_t mctl_present; conn_t *connp; mblk_t *first_mp; uint16_t *up; ill_t *ill = (ill_t *)q->q_ptr; uint16_t reass_hck_flags = 0; ip_stack_t *ipst; ASSERT(recv_ill != NULL); ipst = recv_ill->ill_ipst; #define rptr ((uchar_t *)ipha) EXTRACT_PKT_MP(mp, first_mp, mctl_present); ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); ASSERT(ipha->ipha_protocol == IPPROTO_UDP); ASSERT(ill != NULL); /* * FAST PATH for udp packets */ /* u1 is # words of IP options */ u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); /* IP options present */ if (u1 != 0) goto ipoptions; /* Check the IP header checksum. */ if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { /* Clear the IP header h/w cksum flag */ DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; } else if (!mctl_present) { /* * Don't verify header checksum if this packet is coming * back from AH/ESP as we already did it. */ #define uph ((uint16_t *)ipha) sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; #undef uph /* finish doing IP checksum */ sum = (sum & 0xFFFF) + (sum >> 16); sum = ~(sum + (sum >> 16)) & 0xFFFF; if (sum != 0 && sum != 0xFFFF) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); freemsg(first_mp); return; } } /* * Count for SNMP of inbound packets for ire. * if mctl is present this might be a secure packet and * has already been counted for in ip_proto_input(). */ if (!mctl_present) { UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; } /* packet part of fragmented IP packet? */ u1 = ntohs(ipha->ipha_fragment_offset_and_flags); if (u1 & (IPH_MF | IPH_OFFSET)) { goto fragmented; } /* u1 = IP header length (20 bytes) */ u1 = IP_SIMPLE_HDR_LENGTH; /* packet does not contain complete IP & UDP headers */ if ((mp->b_wptr - rptr) < (IP_SIMPLE_HDR_LENGTH + UDPH_SIZE)) goto udppullup; /* up points to UDP header */ up = (uint16_t *)((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH); #define iphs ((uint16_t *)ipha) /* if udp hdr cksum != 0, then need to checksum udp packet */ if (up[3] != 0) { mblk_t *mp1 = mp->b_cont; boolean_t cksum_err; uint16_t hck_flags = 0; /* Pseudo-header checksum */ u1 = IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + iphs[9] + up[2]; /* * Revert to software checksum calculation if the interface * isn't capable of checksum offload or if IPsec is present. */ if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) hck_flags = DB_CKSUMFLAGS(mp); if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) IP_STAT(ipst, ip_in_sw_cksum); IP_CKSUM_RECV(hck_flags, u1, (uchar_t *)(rptr + DB_CKSUMSTART(mp)), (int32_t)((uchar_t *)up - rptr), mp, mp1, cksum_err); if (cksum_err) { BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); if (hck_flags & HCK_FULLCKSUM) IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); else if (hck_flags & HCK_PARTIALCKSUM) IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); else IP_STAT(ipst, ip_udp_in_sw_cksum_err); freemsg(first_mp); return; } } /* Non-fragmented broadcast or multicast packet? */ if (ire->ire_type == IRE_BROADCAST) goto udpslowpath; if ((connp = ipcl_classify_v4(mp, IPPROTO_UDP, IP_SIMPLE_HDR_LENGTH, ire->ire_zoneid, ipst)) != NULL) { ASSERT(connp->conn_upq != NULL); IP_STAT(ipst, ip_udp_fast_path); if (CONN_UDP_FLOWCTLD(connp)) { freemsg(mp); BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows); } else { if (!mctl_present) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); } /* * mp and first_mp can change. */ if (ip_udp_check(q, connp, recv_ill, ipha, &mp, &first_mp, mctl_present, ire)) { /* Send it upstream */ (connp->conn_recv)(connp, mp, NULL); } } /* * freeb() cannot deal with null mblk being passed * in and first_mp can be set to null in the call * ipsec_input_fast_proc()->ipsec_check_inbound_policy. */ if (mctl_present && first_mp != NULL) { freeb(first_mp); } CONN_DEC_REF(connp); return; } /* * if we got here we know the packet is not fragmented and * has no options. The classifier could not find a conn_t and * most likely its an icmp packet so send it through slow path. */ goto udpslowpath; ipoptions: if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { goto slow_done; } UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; u1 = ntohs(ipha->ipha_fragment_offset_and_flags); if (u1 & (IPH_MF | IPH_OFFSET)) { fragmented: /* * "sum" and "reass_hck_flags" are non-zero if the * reassembled packet has a valid hardware computed * checksum information associated with it. */ if (!ip_rput_fragment(q, &mp, ipha, &sum, &reass_hck_flags)) goto slow_done; /* * Make sure that first_mp points back to mp as * the mp we came in with could have changed in * ip_rput_fragment(). */ ASSERT(!mctl_present); ipha = (ipha_t *)mp->b_rptr; first_mp = mp; } /* Now we have a complete datagram, destined for this machine. */ u1 = IPH_HDR_LENGTH(ipha); /* Pull up the UDP header, if necessary. */ if ((MBLKL(mp)) < (u1 + UDPH_SIZE)) { udppullup: if (!pullupmsg(mp, u1 + UDPH_SIZE)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); goto slow_done; } ipha = (ipha_t *)mp->b_rptr; } /* * Validate the checksum for the reassembled packet; for the * pullup case we calculate the payload checksum in software. */ up = (uint16_t *)((uchar_t *)ipha + u1 + UDP_PORTS_OFFSET); if (up[3] != 0) { boolean_t cksum_err; if ((reass_hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) IP_STAT(ipst, ip_in_sw_cksum); IP_CKSUM_RECV_REASS(reass_hck_flags, (int32_t)((uchar_t *)up - (uchar_t *)ipha), IP_UDP_CSUM_COMP + iphs[6] + iphs[7] + iphs[8] + iphs[9] + up[2], sum, cksum_err); if (cksum_err) { BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs); if (reass_hck_flags & HCK_FULLCKSUM) IP_STAT(ipst, ip_udp_in_full_hw_cksum_err); else if (reass_hck_flags & HCK_PARTIALCKSUM) IP_STAT(ipst, ip_udp_in_part_hw_cksum_err); else IP_STAT(ipst, ip_udp_in_sw_cksum_err); freemsg(first_mp); goto slow_done; } } udpslowpath: /* Clear hardware checksum flag to be safe */ DB_CKSUMFLAGS(mp) = 0; ip_fanout_udp(q, first_mp, ill, ipha, *(uint32_t *)up, (ire->ire_type == IRE_BROADCAST), IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_IPINFO, mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); slow_done: IP_STAT(ipst, ip_udp_slow_path); return; #undef iphs #undef rptr } /* ARGSUSED */ static mblk_t * ip_tcp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ill_rx_ring_t *ill_ring) { conn_t *connp; uint32_t sum; uint32_t u1; uint16_t *up; int offset; ssize_t len; mblk_t *mp1; boolean_t syn_present = B_FALSE; tcph_t *tcph; uint_t ip_hdr_len; ill_t *ill = (ill_t *)q->q_ptr; zoneid_t zoneid = ire->ire_zoneid; boolean_t cksum_err; uint16_t hck_flags = 0; ip_stack_t *ipst = recv_ill->ill_ipst; ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; #define rptr ((uchar_t *)ipha) ASSERT(ipha->ipha_protocol == IPPROTO_TCP); ASSERT(ill != NULL); /* * FAST PATH for tcp packets */ /* u1 is # words of IP options */ u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); /* IP options present */ if (u1) { goto ipoptions; } else if (!mctl_present) { /* Check the IP header checksum. */ if (IS_IP_HDR_HWCKSUM(mctl_present, mp, ill)) { /* Clear the IP header h/w cksum flag */ DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM; } else if (!mctl_present) { /* * Don't verify header checksum if this packet * is coming back from AH/ESP as we already did it. */ #define uph ((uint16_t *)ipha) sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; #undef uph /* finish doing IP checksum */ sum = (sum & 0xFFFF) + (sum >> 16); sum = ~(sum + (sum >> 16)) & 0xFFFF; if (sum != 0 && sum != 0xFFFF) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); goto error; } } } if (!mctl_present) { UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; } /* packet part of fragmented IP packet? */ u1 = ntohs(ipha->ipha_fragment_offset_and_flags); if (u1 & (IPH_MF | IPH_OFFSET)) { goto fragmented; } /* u1 = IP header length (20 bytes) */ u1 = ip_hdr_len = IP_SIMPLE_HDR_LENGTH; /* does packet contain IP+TCP headers? */ len = mp->b_wptr - rptr; if (len < (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH)) { IP_STAT(ipst, ip_tcppullup); goto tcppullup; } /* TCP options present? */ offset = ((uchar_t *)ipha)[IP_SIMPLE_HDR_LENGTH + 12] >> 4; /* * If options need to be pulled up, then goto tcpoptions. * otherwise we are still in the fast path */ if (len < (offset << 2) + IP_SIMPLE_HDR_LENGTH) { IP_STAT(ipst, ip_tcpoptions); goto tcpoptions; } /* multiple mblks of tcp data? */ if ((mp1 = mp->b_cont) != NULL) { /* more then two? */ if (mp1->b_cont != NULL) { IP_STAT(ipst, ip_multipkttcp); goto multipkttcp; } len += mp1->b_wptr - mp1->b_rptr; } up = (uint16_t *)(rptr + IP_SIMPLE_HDR_LENGTH + TCP_PORTS_OFFSET); /* part of pseudo checksum */ /* TCP datagram length */ u1 = len - IP_SIMPLE_HDR_LENGTH; #define iphs ((uint16_t *)ipha) #ifdef _BIG_ENDIAN u1 += IPPROTO_TCP; #else u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); #endif u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; /* * Revert to software checksum calculation if the interface * isn't capable of checksum offload or if IPsec is present. */ if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) hck_flags = DB_CKSUMFLAGS(mp); if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) IP_STAT(ipst, ip_in_sw_cksum); IP_CKSUM_RECV(hck_flags, u1, (uchar_t *)(rptr + DB_CKSUMSTART(mp)), (int32_t)((uchar_t *)up - rptr), mp, mp1, cksum_err); if (cksum_err) { BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); if (hck_flags & HCK_FULLCKSUM) IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err); else if (hck_flags & HCK_PARTIALCKSUM) IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err); else IP_STAT(ipst, ip_tcp_in_sw_cksum_err); goto error; } try_again: if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len, zoneid, ipst)) == NULL) { /* Send the TH_RST */ goto no_conn; } /* * TCP FAST PATH for AF_INET socket. * * TCP fast path to avoid extra work. An AF_INET socket type * does not have facility to receive extra information via * ip_process or ip_add_info. Also, when the connection was * established, we made a check if this connection is impacted * by any global IPsec policy or per connection policy (a * policy that comes in effect later will not apply to this * connection). Since all this can be determined at the * connection establishment time, a quick check of flags * can avoid extra work. */ if (IPCL_IS_TCP4_CONNECTED_NO_POLICY(connp) && !mctl_present && !IPP_ENABLED(IPP_LOCAL_IN, ipst)) { ASSERT(first_mp == mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); SET_SQUEUE(mp, tcp_rput_data, connp); return (mp); } tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { if (IPCL_IS_TCP(connp)) { mp->b_datap->db_struioflag |= STRUIO_EAGER; DB_CKSUMSTART(mp) = (intptr_t)ip_squeue_get(ill_ring); if (IPCL_IS_FULLY_BOUND(connp) && !mctl_present && !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); SET_SQUEUE(mp, connp->conn_recv, connp); return (mp); } else if (IPCL_IS_BOUND(connp) && !mctl_present && !CONN_INBOUND_POLICY_PRESENT(connp, ipss)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); ip_squeue_enter_unbound++; SET_SQUEUE(mp, tcp_conn_request_unbound, connp); return (mp); } syn_present = B_TRUE; } } if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); /* No need to send this packet to TCP */ if ((flags & TH_RST) || (flags & TH_URG)) { DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL, void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph); CONN_DEC_REF(connp); freemsg(first_mp); return (NULL); } if (flags & TH_ACK) { tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid, ipst->ips_netstack->netstack_tcp, connp); CONN_DEC_REF(connp); return (NULL); } DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL, void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph); CONN_DEC_REF(connp); freemsg(first_mp); return (NULL); } if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) { first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha, NULL, mctl_present); if (first_mp == NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); CONN_DEC_REF(connp); return (NULL); } if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { ASSERT(syn_present); if (mctl_present) { ASSERT(first_mp != mp); first_mp->b_datap->db_struioflag |= STRUIO_POLICY; } else { ASSERT(first_mp == mp); mp->b_datap->db_struioflag &= ~STRUIO_EAGER; mp->b_datap->db_struioflag |= STRUIO_POLICY; } } else { /* * Discard first_mp early since we're dealing with a * fully-connected conn_t and tcp doesn't do policy in * this case. */ if (mctl_present) { freeb(first_mp); mctl_present = B_FALSE; } first_mp = mp; } } /* Initiate IPPF processing for fastpath */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { uint32_t ill_index; ill_index = recv_ill->ill_phyint->phyint_ifindex; ip_process(IPP_LOCAL_IN, &mp, ill_index); if (mp == NULL) { ip2dbg(("ip_input_ipsec_process: TCP pkt " "deferred/dropped during IPPF processing\n")); CONN_DEC_REF(connp); if (mctl_present) freeb(first_mp); return (NULL); } else if (mctl_present) { /* * ip_process might return a new mp. */ ASSERT(first_mp != mp); first_mp->b_cont = mp; } else { first_mp = mp; } } if (!syn_present && connp->conn_ip_recvpktinfo) { /* * TCP does not support IP_RECVPKTINFO for v4 so lets * make sure IPF_RECVIF is passed to ip_add_info. */ mp = ip_add_info(mp, recv_ill, flags|IPF_RECVIF, IPCL_ZONEID(connp), ipst); if (mp == NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); CONN_DEC_REF(connp); if (mctl_present) freeb(first_mp); return (NULL); } else if (mctl_present) { /* * ip_add_info might return a new mp. */ ASSERT(first_mp != mp); first_mp->b_cont = mp; } else { first_mp = mp; } } BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); if (IPCL_IS_TCP(connp)) { SET_SQUEUE(first_mp, connp->conn_recv, connp); return (first_mp); } else { /* SOCK_RAW, IPPROTO_TCP case */ (connp->conn_recv)(connp, first_mp, NULL); CONN_DEC_REF(connp); return (NULL); } no_conn: /* Initiate IPPf processing, if needed. */ if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) { uint32_t ill_index; ill_index = recv_ill->ill_phyint->phyint_ifindex; ip_process(IPP_LOCAL_IN, &first_mp, ill_index); if (first_mp == NULL) { return (NULL); } } BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); tcp_xmit_listeners_reset(first_mp, IPH_HDR_LENGTH(mp->b_rptr), zoneid, ipst->ips_netstack->netstack_tcp, NULL); return (NULL); ipoptions: if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) { goto slow_done; } UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; u1 = ntohs(ipha->ipha_fragment_offset_and_flags); if (u1 & (IPH_MF | IPH_OFFSET)) { fragmented: if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { if (mctl_present) freeb(first_mp); goto slow_done; } /* * Make sure that first_mp points back to mp as * the mp we came in with could have changed in * ip_rput_fragment(). */ ASSERT(!mctl_present); ipha = (ipha_t *)mp->b_rptr; first_mp = mp; } /* Now we have a complete datagram, destined for this machine. */ u1 = ip_hdr_len = IPH_HDR_LENGTH(ipha); len = mp->b_wptr - mp->b_rptr; /* Pull up a minimal TCP header, if necessary. */ if (len < (u1 + 20)) { tcppullup: if (!pullupmsg(mp, u1 + 20)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); goto error; } ipha = (ipha_t *)mp->b_rptr; len = mp->b_wptr - mp->b_rptr; } /* * Extract the offset field from the TCP header. As usual, we * try to help the compiler more than the reader. */ offset = ((uchar_t *)ipha)[u1 + 12] >> 4; if (offset != 5) { tcpoptions: if (offset < 5) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); goto error; } /* * There must be TCP options. * Make sure we can grab them. */ offset <<= 2; offset += u1; if (len < offset) { if (!pullupmsg(mp, offset)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); goto error; } ipha = (ipha_t *)mp->b_rptr; len = mp->b_wptr - rptr; } } /* Get the total packet length in len, including headers. */ if (mp->b_cont) { multipkttcp: len = msgdsize(mp); } /* * Check the TCP checksum by pulling together the pseudo- * header checksum, and passing it to ip_csum to be added in * with the TCP datagram. * * Since we are not using the hwcksum if available we must * clear the flag. We may come here via tcppullup or tcpoptions. * If either of these fails along the way the mblk is freed. * If this logic ever changes and mblk is reused to say send * ICMP's back, then this flag may need to be cleared in * other places as well. */ DB_CKSUMFLAGS(mp) = 0; up = (uint16_t *)(rptr + u1 + TCP_PORTS_OFFSET); u1 = (uint32_t)(len - u1); /* TCP datagram length. */ #ifdef _BIG_ENDIAN u1 += IPPROTO_TCP; #else u1 = ((u1 >> 8) & 0xFF) + (((u1 & 0xFF) + IPPROTO_TCP) << 8); #endif u1 += iphs[6] + iphs[7] + iphs[8] + iphs[9]; /* * Not M_DATA mblk or its a dup, so do the checksum now. */ IP_STAT(ipst, ip_in_sw_cksum); if (IP_CSUM(mp, (int32_t)((uchar_t *)up - rptr), u1) != 0) { BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs); goto error; } IP_STAT(ipst, ip_tcp_slow_path); goto try_again; #undef iphs #undef rptr error: freemsg(first_mp); slow_done: return (NULL); } /* ARGSUSED */ static void ip_sctp_input(mblk_t *mp, ipha_t *ipha, ill_t *recv_ill, boolean_t mctl_present, ire_t *ire, mblk_t *first_mp, uint_t flags, queue_t *q, ipaddr_t dst) { conn_t *connp; uint32_t sum; uint32_t u1; ssize_t len; sctp_hdr_t *sctph; zoneid_t zoneid = ire->ire_zoneid; uint32_t pktsum; uint32_t calcsum; uint32_t ports; in6_addr_t map_src, map_dst; ill_t *ill = (ill_t *)q->q_ptr; ip_stack_t *ipst; sctp_stack_t *sctps; boolean_t sctp_csum_err = B_FALSE; ASSERT(recv_ill != NULL); ipst = recv_ill->ill_ipst; sctps = ipst->ips_netstack->netstack_sctp; #define rptr ((uchar_t *)ipha) ASSERT(ipha->ipha_protocol == IPPROTO_SCTP); ASSERT(ill != NULL); /* u1 is # words of IP options */ u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); /* IP options present */ if (u1 > 0) { goto ipoptions; } else { /* Check the IP header checksum. */ if (!IS_IP_HDR_HWCKSUM(mctl_present, mp, ill) && !mctl_present) { #define uph ((uint16_t *)ipha) sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; #undef uph /* finish doing IP checksum */ sum = (sum & 0xFFFF) + (sum >> 16); sum = ~(sum + (sum >> 16)) & 0xFFFF; /* * Don't verify header checksum if this packet * is coming back from AH/ESP as we already did it. */ if (sum != 0 && sum != 0xFFFF) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); goto error; } } /* * Since there is no SCTP h/w cksum support yet, just * clear the flag. */ DB_CKSUMFLAGS(mp) = 0; } /* * Don't verify header checksum if this packet is coming * back from AH/ESP as we already did it. */ if (!mctl_present) { UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; } /* packet part of fragmented IP packet? */ u1 = ntohs(ipha->ipha_fragment_offset_and_flags); if (u1 & (IPH_MF | IPH_OFFSET)) goto fragmented; /* u1 = IP header length (20 bytes) */ u1 = IP_SIMPLE_HDR_LENGTH; find_sctp_client: /* Pullup if we don't have the sctp common header. */ len = MBLKL(mp); if (len < (u1 + SCTP_COMMON_HDR_LENGTH)) { if (mp->b_cont == NULL || !pullupmsg(mp, u1 + SCTP_COMMON_HDR_LENGTH)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); goto error; } ipha = (ipha_t *)mp->b_rptr; len = MBLKL(mp); } sctph = (sctp_hdr_t *)(rptr + u1); #ifdef DEBUG if (!skip_sctp_cksum) { #endif pktsum = sctph->sh_chksum; sctph->sh_chksum = 0; calcsum = sctp_cksum(mp, u1); sctph->sh_chksum = pktsum; if (calcsum != pktsum) sctp_csum_err = B_TRUE; #ifdef DEBUG /* skip_sctp_cksum */ } #endif /* get the ports */ ports = *(uint32_t *)&sctph->sh_sport; IRE_REFRELE(ire); IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst); IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src); if (sctp_csum_err) { /* * No potential sctp checksum errors go to the Sun * sctp stack however they might be Adler-32 summed * packets a userland stack bound to a raw IP socket * could reasonably use. Note though that Adler-32 is * a long deprecated algorithm and customer sctp * networks should eventually migrate to CRC-32 at * which time this facility should be removed. */ flags |= IP_FF_SCTP_CSUM_ERR; goto no_conn; } if ((connp = sctp_fanout(&map_src, &map_dst, ports, zoneid, mp, sctps)) == NULL) { /* Check for raw socket or OOTB handling */ goto no_conn; } /* Found a client; up it goes */ BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers); sctp_input(connp, ipha, mp, first_mp, recv_ill, B_TRUE, mctl_present); return; no_conn: ip_fanout_sctp_raw(first_mp, recv_ill, ipha, B_TRUE, ports, mctl_present, flags, B_TRUE, zoneid); return; ipoptions: DB_CKSUMFLAGS(mp) = 0; if (!ip_options_cksum(q, ill, first_mp, ipha, ire, ipst)) goto slow_done; UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; u1 = ntohs(ipha->ipha_fragment_offset_and_flags); if (u1 & (IPH_MF | IPH_OFFSET)) { fragmented: if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) goto slow_done; /* * Make sure that first_mp points back to mp as * the mp we came in with could have changed in * ip_rput_fragment(). */ ASSERT(!mctl_present); ipha = (ipha_t *)mp->b_rptr; first_mp = mp; } /* Now we have a complete datagram, destined for this machine. */ u1 = IPH_HDR_LENGTH(ipha); goto find_sctp_client; #undef iphs #undef rptr error: freemsg(first_mp); slow_done: IRE_REFRELE(ire); } #define VER_BITS 0xF0 #define VERSION_6 0x60 static boolean_t ip_rput_multimblk_ipoptions(queue_t *q, ill_t *ill, mblk_t *mp, ipha_t **iphapp, ipaddr_t *dstp, ip_stack_t *ipst) { uint_t opt_len; ipha_t *ipha; ssize_t len; uint_t pkt_len; ASSERT(ill != NULL); IP_STAT(ipst, ip_ipoptions); ipha = *iphapp; #define rptr ((uchar_t *)ipha) /* Assume no IPv6 packets arrive over the IPv4 queue */ if (IPH_HDR_VERSION(ipha) == IPV6_VERSION) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion); freemsg(mp); return (B_FALSE); } /* multiple mblk or too short */ pkt_len = ntohs(ipha->ipha_length); /* Get the number of words of IP options in the IP header. */ opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; if (opt_len) { /* IP Options present! Validate and process. */ if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); goto done; } /* * Recompute complete header length and make sure we * have access to all of it. */ len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2; if (len > (mp->b_wptr - rptr)) { if (len > pkt_len) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); goto done; } if (!pullupmsg(mp, len)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); goto done; } ipha = (ipha_t *)mp->b_rptr; } /* * Go off to ip_rput_options which returns the next hop * destination address, which may have been affected * by source routing. */ IP_STAT(ipst, ip_opt); if (ip_rput_options(q, mp, ipha, dstp, ipst) == -1) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return (B_FALSE); } } *iphapp = ipha; return (B_TRUE); done: /* clear b_prev - used by ip_mroute_decap */ mp->b_prev = NULL; freemsg(mp); return (B_FALSE); #undef rptr } /* * Deal with the fact that there is no ire for the destination. */ static ire_t * ip_rput_noire(queue_t *q, mblk_t *mp, int ll_multicast, ipaddr_t dst) { ipha_t *ipha; ill_t *ill; ire_t *ire; ip_stack_t *ipst; enum ire_forward_action ret_action; ipha = (ipha_t *)mp->b_rptr; ill = (ill_t *)q->q_ptr; ASSERT(ill != NULL); ipst = ill->ill_ipst; /* * No IRE for this destination, so it can't be for us. * Unless we are forwarding, drop the packet. * We have to let source routed packets through * since we don't yet know if they are 'ping -l' * packets i.e. if they will go out over the * same interface as they came in on. */ if (ll_multicast) { freemsg(mp); return (NULL); } if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); freemsg(mp); return (NULL); } /* * Mark this packet as having originated externally. * * For non-forwarding code path, ire_send later double * checks this interface to see if it is still exists * post-ARP resolution. * * Also, IPQOS uses this to differentiate between * IPP_FWD_OUT and IPP_LOCAL_OUT for post-ARP * QOS packet processing in ip_wput_attach_llhdr(). * The QoS module can mark the b_band for a fastpath message * or the dl_priority field in a unitdata_req header for * CoS marking. This info can only be found in * ip_wput_attach_llhdr(). */ mp->b_prev = (mblk_t *)(uintptr_t)ill->ill_phyint->phyint_ifindex; /* * Clear the indication that this may have a hardware checksum * as we are not using it */ DB_CKSUMFLAGS(mp) = 0; ire = ire_forward(dst, &ret_action, NULL, NULL, MBLK_GETLABEL(mp), ipst); if (ire == NULL && ret_action == Forward_check_multirt) { /* Let ip_newroute handle CGTP */ ip_newroute(q, mp, dst, NULL, GLOBAL_ZONEID, ipst); return (NULL); } if (ire != NULL) return (ire); mp->b_prev = mp->b_next = 0; if (ret_action == Forward_blackhole) { freemsg(mp); return (NULL); } /* send icmp unreachable */ q = WR(q); /* Sent by forwarding path, and router is global zone */ if (ip_source_routed(ipha, ipst)) { icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); } else { icmp_unreachable(q, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, ipst); } return (NULL); } /* * check ip header length and align it. */ static boolean_t ip_check_and_align_header(queue_t *q, mblk_t *mp, ip_stack_t *ipst) { ssize_t len; ill_t *ill; ipha_t *ipha; len = MBLKL(mp); if (!OK_32PTR(mp->b_rptr) || len < IP_SIMPLE_HDR_LENGTH) { ill = (ill_t *)q->q_ptr; if (!OK_32PTR(mp->b_rptr)) IP_STAT(ipst, ip_notaligned1); else IP_STAT(ipst, ip_notaligned2); /* Guard against bogus device drivers */ if (len < 0) { /* clear b_prev - used by ip_mroute_decap */ mp->b_prev = NULL; BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); freemsg(mp); return (B_FALSE); } if (ip_rput_pullups++ == 0) { ipha = (ipha_t *)mp->b_rptr; (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, "ip_check_and_align_header: %s forced us to " " pullup pkt, hdr len %ld, hdr addr %p", ill->ill_name, len, ipha); } if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) { /* clear b_prev - used by ip_mroute_decap */ mp->b_prev = NULL; BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(mp); return (B_FALSE); } } return (B_TRUE); } ire_t * ip_check_multihome(void *addr, ire_t *ire, ill_t *ill) { ire_t *new_ire; ill_t *ire_ill; uint_t ifindex; ip_stack_t *ipst = ill->ill_ipst; boolean_t strict_check = B_FALSE; /* * This packet came in on an interface other than the one associated * with the first ire we found for the destination address. We do * another ire lookup here, using the ingress ill, to see if the * interface is in an interface group. * As long as the ills belong to the same group, we don't consider * them to be arriving on the wrong interface. Thus, if the switch * is doing inbound load spreading, we won't drop packets when the * ip*_strict_dst_multihoming switch is on. Note, the same holds true * for 'usesrc groups' where the destination address may belong to * another interface to allow multipathing to happen. * We also need to check for IPIF_UNNUMBERED point2point interfaces * where the local address may not be unique. In this case we were * at the mercy of the initial ire cache lookup and the IRE_LOCAL it * actually returned. The new lookup, which is more specific, should * only find the IRE_LOCAL associated with the ingress ill if one * exists. */ if (ire->ire_ipversion == IPV4_VERSION) { if (ipst->ips_ip_strict_dst_multihoming) strict_check = B_TRUE; new_ire = ire_ctable_lookup(*((ipaddr_t *)addr), 0, IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE|MATCH_IRE_ILL_GROUP), ipst); } else { ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr)); if (ipst->ips_ipv6_strict_dst_multihoming) strict_check = B_TRUE; new_ire = ire_ctable_lookup_v6((in6_addr_t *)addr, NULL, IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE|MATCH_IRE_ILL_GROUP), ipst); } /* * If the same ire that was returned in ip_input() is found then this * is an indication that interface groups are in use. The packet * arrived on a different ill in the group than the one associated with * the destination address. If a different ire was found then the same * IP address must be hosted on multiple ills. This is possible with * unnumbered point2point interfaces. We switch to use this new ire in * order to have accurate interface statistics. */ if (new_ire != NULL) { if ((new_ire != ire) && (new_ire->ire_rfq != NULL)) { ire_refrele(ire); ire = new_ire; } else { ire_refrele(new_ire); } return (ire); } else if ((ire->ire_rfq == NULL) && (ire->ire_ipversion == IPV4_VERSION)) { /* * The best match could have been the original ire which * was created against an IRE_LOCAL on lo0. In the IPv4 case * the strict multihoming checks are irrelevant as we consider * local addresses hosted on lo0 to be interface agnostic. We * only expect a null ire_rfq on IREs which are associated with * lo0 hence we can return now. */ return (ire); } /* * Chase pointers once and store locally. */ ire_ill = (ire->ire_rfq == NULL) ? NULL : (ill_t *)(ire->ire_rfq->q_ptr); ifindex = ill->ill_usesrc_ifindex; /* * Check if it's a legal address on the 'usesrc' interface. */ if ((ifindex != 0) && (ire_ill != NULL) && (ifindex == ire_ill->ill_phyint->phyint_ifindex)) { return (ire); } /* * If the ip*_strict_dst_multihoming switch is on then we can * only accept this packet if the interface is marked as routing. */ if (!(strict_check)) return (ire); if ((ill->ill_flags & ire->ire_ipif->ipif_ill->ill_flags & ILLF_ROUTER) != 0) { return (ire); } ire_refrele(ire); return (NULL); } ire_t * ip_fast_forward(ire_t *ire, ipaddr_t dst, ill_t *ill, mblk_t *mp) { ipha_t *ipha; ire_t *src_ire; ill_t *stq_ill; uint_t hlen; uint_t pkt_len; uint32_t sum; queue_t *dev_q; ip_stack_t *ipst = ill->ill_ipst; mblk_t *fpmp; enum ire_forward_action ret_action; ipha = (ipha_t *)mp->b_rptr; if (ire != NULL && ire->ire_zoneid != GLOBAL_ZONEID && ire->ire_zoneid != ALL_ZONES) { /* * Should only use IREs that are visible to the global * zone for forwarding. */ ire_refrele(ire); ire = ire_cache_lookup(dst, GLOBAL_ZONEID, NULL, ipst); } /* * Martian Address Filtering [RFC 1812, Section 5.3.7] * The loopback address check for both src and dst has already * been checked in ip_input */ if (dst == INADDR_ANY || CLASSD(ipha->ipha_src)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); goto drop; } src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (src_ire != NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); ire_refrele(src_ire); goto drop; } /* No ire cache of nexthop. So first create one */ if (ire == NULL) { ire = ire_forward(dst, &ret_action, NULL, NULL, NULL, ipst); /* * We only come to ip_fast_forward if ip_cgtp_filter * is not set. So ire_forward() should not return with * Forward_check_multirt as the next action. */ ASSERT(ret_action != Forward_check_multirt); if (ire == NULL) { /* An attempt was made to forward the packet */ BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); mp->b_prev = mp->b_next = 0; /* send icmp unreachable */ /* Sent by forwarding path, and router is global zone */ if (ret_action == Forward_ret_icmp_err) { if (ip_source_routed(ipha, ipst)) { icmp_unreachable(ill->ill_wq, mp, ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); } else { icmp_unreachable(ill->ill_wq, mp, ICMP_HOST_UNREACHABLE, GLOBAL_ZONEID, ipst); } } else { freemsg(mp); } return (NULL); } } /* * Forwarding fastpath exception case: * If either of the follwoing case is true, we take * the slowpath * o forwarding is not enabled * o incoming and outgoing interface are the same, or the same * IPMP group * o corresponding ire is in incomplete state * o packet needs fragmentation * o ARP cache is not resolved * * The codeflow from here on is thus: * ip_rput_process_forward->ip_rput_forward->ip_xmit_v4 */ pkt_len = ntohs(ipha->ipha_length); stq_ill = (ill_t *)ire->ire_stq->q_ptr; if (!(stq_ill->ill_flags & ILLF_ROUTER) || !(ill->ill_flags & ILLF_ROUTER) || (ill == stq_ill) || (ill->ill_group != NULL && ill->ill_group == stq_ill->ill_group) || (ire->ire_nce == NULL) || (pkt_len > ire->ire_max_frag) || ((fpmp = ire->ire_nce->nce_fp_mp) == NULL) || ((hlen = MBLKL(fpmp)) > MBLKHEAD(mp)) || ipha->ipha_ttl <= 1) { ip_rput_process_forward(ill->ill_rq, mp, ire, ipha, ill, B_FALSE); return (ire); } BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); DTRACE_PROBE4(ip4__forwarding__start, ill_t *, ill, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); FW_HOOKS(ipst->ips_ip4_forwarding_event, ipst->ips_ipv4firewall_forwarding, ill, stq_ill, ipha, mp, mp, 0, ipst); DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); if (mp == NULL) goto drop; mp->b_datap->db_struioun.cksum.flags = 0; /* Adjust the checksum to reflect the ttl decrement. */ sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); ipha->ipha_ttl--; /* * Write the link layer header. We can do this safely here, * because we have already tested to make sure that the IP * policy is not set, and that we have a fast path destination * header. */ mp->b_rptr -= hlen; bcopy(fpmp->b_rptr, mp->b_rptr, hlen); UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams); BUMP_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutTransmits); UPDATE_MIB(stq_ill->ill_ip_mib, ipIfStatsHCOutOctets, pkt_len); dev_q = ire->ire_stq->q_next; if ((dev_q->q_next != NULL || dev_q->q_first != NULL) && !canputnext(ire->ire_stq)) { goto indiscard; } if (ILL_DLS_CAPABLE(stq_ill)) { /* * Send the packet directly to DLD, where it * may be queued depending on the availability * of transmit resources at the media layer. */ IP_DLS_ILL_TX(stq_ill, ipha, mp, ipst); } else { DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, ill_t *, stq_ill, ipha_t *, ipha, mblk_t *, mp); FW_HOOKS(ipst->ips_ip4_physical_out_event, ipst->ips_ipv4firewall_physical_out, NULL, stq_ill, ipha, mp, mp, 0, ipst); DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); if (mp == NULL) goto drop; DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, ipha, __dtrace_ipsr_ill_t *, stq_ill, ipha_t *, ipha, ip6_t *, NULL, int, 0); putnext(ire->ire_stq, mp); } return (ire); indiscard: BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); drop: if (mp != NULL) freemsg(mp); return (ire); } /* * This function is called in the forwarding slowpath, when * either the ire lacks the link-layer address, or the packet needs * further processing(eg. fragmentation), before transmission. */ static void ip_rput_process_forward(queue_t *q, mblk_t *mp, ire_t *ire, ipha_t *ipha, ill_t *ill, boolean_t ll_multicast) { ill_group_t *ill_group; ill_group_t *ire_group; queue_t *dev_q; ire_t *src_ire; ip_stack_t *ipst = ill->ill_ipst; ASSERT(ire->ire_stq != NULL); mp->b_prev = NULL; /* ip_rput_noire sets incoming interface here */ mp->b_next = NULL; /* ip_rput_noire sets dst here */ if (ll_multicast != 0) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); goto drop_pkt; } /* * check if ipha_src is a broadcast address. Note that this * check is redundant when we get here from ip_fast_forward() * which has already done this check. However, since we can * also get here from ip_rput_process_broadcast() or, for * for the slow path through ip_fast_forward(), we perform * the check again for code-reusability */ src_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (src_ire != NULL || ipha->ipha_dst == INADDR_ANY) { if (src_ire != NULL) ire_refrele(src_ire); BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); ip2dbg(("ip_rput_process_forward: Received packet with" " bad src/dst address on %s\n", ill->ill_name)); goto drop_pkt; } ill_group = ill->ill_group; ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; /* * Check if we want to forward this one at this time. * We allow source routed packets on a host provided that * they go out the same interface or same interface group * as they came in on. * * XXX To be quicker, we may wish to not chase pointers to * get the ILLF_ROUTER flag and instead store the * forwarding policy in the ire. An unfortunate * side-effect of that would be requiring an ire flush * whenever the ILLF_ROUTER flag changes. */ if (((ill->ill_flags & ((ill_t *)ire->ire_stq->q_ptr)->ill_flags & ILLF_ROUTER) == 0) && !(ip_source_routed(ipha, ipst) && (ire->ire_rfq == q || (ill_group != NULL && ill_group == ire_group)))) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); if (ip_source_routed(ipha, ipst)) { q = WR(q); /* * Clear the indication that this may have * hardware checksum as we are not using it. */ DB_CKSUMFLAGS(mp) = 0; /* Sent by forwarding path, and router is global zone */ icmp_unreachable(q, mp, ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); return; } goto drop_pkt; } BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams); /* Packet is being forwarded. Turning off hwcksum flag. */ DB_CKSUMFLAGS(mp) = 0; if (ipst->ips_ip_g_send_redirects) { /* * Check whether the incoming interface and outgoing * interface is part of the same group. If so, * send redirects. * * Check the source address to see if it originated * on the same logical subnet it is going back out on. * If so, we should be able to send it a redirect. * Avoid sending a redirect if the destination * is directly connected (i.e., ipha_dst is the same * as ire_gateway_addr or the ire_addr of the * nexthop IRE_CACHE ), or if the packet was source * routed out this interface. */ ipaddr_t src, nhop; mblk_t *mp1; ire_t *nhop_ire = NULL; /* * Check whether ire_rfq and q are from the same ill * or if they are not same, they at least belong * to the same group. If so, send redirects. */ if ((ire->ire_rfq == q || (ill_group != NULL && ill_group == ire_group)) && !ip_source_routed(ipha, ipst)) { nhop = (ire->ire_gateway_addr != 0 ? ire->ire_gateway_addr : ire->ire_addr); if (ipha->ipha_dst == nhop) { /* * We avoid sending a redirect if the * destination is directly connected * because it is possible that multiple * IP subnets may have been configured on * the link, and the source may not * be on the same subnet as ip destination, * even though they are on the same * physical link. */ goto sendit; } src = ipha->ipha_src; /* * We look up the interface ire for the nexthop, * to see if ipha_src is in the same subnet * as the nexthop. * * Note that, if, in the future, IRE_CACHE entries * are obsoleted, this lookup will not be needed, * as the ire passed to this function will be the * same as the nhop_ire computed below. */ nhop_ire = ire_ftable_lookup(nhop, 0, 0, IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, MATCH_IRE_TYPE, ipst); if (nhop_ire != NULL) { if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) { /* * The source is directly connected. * Just copy the ip header (which is * in the first mblk) */ mp1 = copyb(mp); if (mp1 != NULL) { icmp_send_redirect(WR(q), mp1, nhop, ipst); } } ire_refrele(nhop_ire); } } } sendit: dev_q = ire->ire_stq->q_next; if ((dev_q->q_next || dev_q->q_first) && !canput(dev_q)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(mp); return; } ip_rput_forward(ire, ipha, mp, ill); return; drop_pkt: ip2dbg(("ip_rput_process_forward: drop pkt\n")); freemsg(mp); } ire_t * ip_rput_process_broadcast(queue_t **qp, mblk_t *mp, ire_t *ire, ipha_t *ipha, ill_t *ill, ipaddr_t dst, int cgtp_flt_pkt, int ll_multicast) { queue_t *q; uint16_t hcksumflags; ip_stack_t *ipst = ill->ill_ipst; q = *qp; BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts); /* * Clear the indication that this may have hardware * checksum as we are not using it for forwarding. */ hcksumflags = DB_CKSUMFLAGS(mp); DB_CKSUMFLAGS(mp) = 0; /* * Directed broadcast forwarding: if the packet came in over a * different interface then it is routed out over we can forward it. */ if (ipha->ipha_protocol == IPPROTO_TCP) { ire_refrele(ire); freemsg(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return (NULL); } /* * For multicast we have set dst to be INADDR_BROADCAST * for delivering to all STREAMS. IRE_MARK_NORECV is really * only for broadcast packets. */ if (!CLASSD(ipha->ipha_dst)) { ire_t *new_ire; ipif_t *ipif; /* * For ill groups, as the switch duplicates broadcasts * across all the ports, we need to filter out and * send up only one copy. There is one copy for every * broadcast address on each ill. Thus, we look for a * specific IRE on this ill and look at IRE_MARK_NORECV * later to see whether this ill is eligible to receive * them or not. ill_nominate_bcast_rcv() nominates only * one set of IREs for receiving. */ ipif = ipif_get_next_ipif(NULL, ill); if (ipif == NULL) { ire_refrele(ire); freemsg(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return (NULL); } new_ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, NULL, MATCH_IRE_ILL, ipst); ipif_refrele(ipif); if (new_ire != NULL) { if (new_ire->ire_marks & IRE_MARK_NORECV) { ire_refrele(ire); ire_refrele(new_ire); freemsg(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return (NULL); } /* * In the special case of multirouted broadcast * packets, we unconditionally need to "gateway" * them to the appropriate interface here. * In the normal case, this cannot happen, because * there is no broadcast IRE tagged with the * RTF_MULTIRT flag. */ if (new_ire->ire_flags & RTF_MULTIRT) { ire_refrele(new_ire); if (ire->ire_rfq != NULL) { q = ire->ire_rfq; *qp = q; } } else { ire_refrele(ire); ire = new_ire; } } else if (cgtp_flt_pkt == CGTP_IP_PKT_NOT_CGTP) { if (!ipst->ips_ip_g_forward_directed_bcast) { /* * Free the message if * ip_g_forward_directed_bcast is turned * off for non-local broadcast. */ ire_refrele(ire); freemsg(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return (NULL); } } else { /* * This CGTP packet successfully passed the * CGTP filter, but the related CGTP * broadcast IRE has not been found, * meaning that the redundant ipif is * probably down. However, if we discarded * this packet, its duplicate would be * filtered out by the CGTP filter so none * of them would get through. So we keep * going with this one. */ ASSERT(cgtp_flt_pkt == CGTP_IP_PKT_PREMIUM); if (ire->ire_rfq != NULL) { q = ire->ire_rfq; *qp = q; } } } if (ipst->ips_ip_g_forward_directed_bcast && ll_multicast == 0) { /* * Verify that there are not more then one * IRE_BROADCAST with this broadcast address which * has ire_stq set. * TODO: simplify, loop over all IRE's */ ire_t *ire1; int num_stq = 0; mblk_t *mp1; /* Find the first one with ire_stq set */ rw_enter(&ire->ire_bucket->irb_lock, RW_READER); for (ire1 = ire; ire1 && !ire1->ire_stq && ire1->ire_addr == ire->ire_addr; ire1 = ire1->ire_next) ; if (ire1) { ire_refrele(ire); ire = ire1; IRE_REFHOLD(ire); } /* Check if there are additional ones with stq set */ for (ire1 = ire; ire1; ire1 = ire1->ire_next) { if (ire->ire_addr != ire1->ire_addr) break; if (ire1->ire_stq) { num_stq++; break; } } rw_exit(&ire->ire_bucket->irb_lock); if (num_stq == 1 && ire->ire_stq != NULL) { ip1dbg(("ip_rput_process_broadcast: directed " "broadcast to 0x%x\n", ntohl(ire->ire_addr))); mp1 = copymsg(mp); if (mp1) { switch (ipha->ipha_protocol) { case IPPROTO_UDP: ip_udp_input(q, mp1, ipha, ire, ill); break; default: ip_proto_input(q, mp1, ipha, ire, ill, 0); break; } } /* * Adjust ttl to 2 (1+1 - the forward engine * will decrement it by one. */ if (ip_csum_hdr(ipha)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); ip2dbg(("ip_rput_broadcast:drop pkt\n")); freemsg(mp); ire_refrele(ire); return (NULL); } ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1; ipha->ipha_hdr_checksum = 0; ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); ip_rput_process_forward(q, mp, ire, ipha, ill, ll_multicast); ire_refrele(ire); return (NULL); } ip1dbg(("ip_rput: NO directed broadcast to 0x%x\n", ntohl(ire->ire_addr))); } /* Restore any hardware checksum flags */ DB_CKSUMFLAGS(mp) = hcksumflags; return (ire); } /* ARGSUSED */ static boolean_t ip_rput_process_multicast(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, int *ll_multicast, ipaddr_t *dstp) { ip_stack_t *ipst = ill->ill_ipst; BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts); UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, ntohs(ipha->ipha_length)); /* * Forward packets only if we have joined the allmulti * group on this interface. */ if (ipst->ips_ip_g_mrouter && ill->ill_join_allmulti) { int retval; /* * Clear the indication that this may have hardware * checksum as we are not using it. */ DB_CKSUMFLAGS(mp) = 0; retval = ip_mforward(ill, ipha, mp); /* ip_mforward updates mib variables if needed */ /* clear b_prev - used by ip_mroute_decap */ mp->b_prev = NULL; switch (retval) { case 0: /* * pkt is okay and arrived on phyint. * * If we are running as a multicast router * we need to see all IGMP and/or PIM packets. */ if ((ipha->ipha_protocol == IPPROTO_IGMP) || (ipha->ipha_protocol == IPPROTO_PIM)) { goto done; } break; case -1: /* pkt is mal-formed, toss it */ goto drop_pkt; case 1: /* pkt is okay and arrived on a tunnel */ /* * If we are running a multicast router * we need to see all igmp packets. */ if (ipha->ipha_protocol == IPPROTO_IGMP) { *dstp = INADDR_BROADCAST; *ll_multicast = 1; return (B_FALSE); } goto drop_pkt; } } ILM_WALKER_HOLD(ill); if (ilm_lookup_ill(ill, *dstp, ALL_ZONES) == NULL) { /* * This might just be caused by the fact that * multiple IP Multicast addresses map to the same * link layer multicast - no need to increment counter! */ ILM_WALKER_RELE(ill); freemsg(mp); return (B_TRUE); } ILM_WALKER_RELE(ill); done: ip2dbg(("ip_rput: multicast for us: 0x%x\n", ntohl(*dstp))); /* * This assumes the we deliver to all streams for multicast * and broadcast packets. */ *dstp = INADDR_BROADCAST; *ll_multicast = 1; return (B_FALSE); drop_pkt: ip2dbg(("ip_rput: drop pkt\n")); freemsg(mp); return (B_TRUE); } /* * This function is used to both return an indication of whether or not * the packet received is a non-unicast packet (by way of the DL_UNITDATA_IND) * and in doing so, determine whether or not it is broadcast vs multicast. * For it to be a broadcast packet, we must have the appropriate mblk_t * hanging off the ill_t. If this is either not present or doesn't match * the destination mac address in the DL_UNITDATA_IND, the packet is deemed * to be multicast. Thus NICs that have no broadcast address (or no * capability for one, such as point to point links) cannot return as * the packet being broadcast. The use of HPE_BROADCAST/HPE_MULTICAST as * the return values simplifies the current use of the return value of this * function, which is to pass through the multicast/broadcast characteristic * to consumers of the netinfo/pfhooks API. While this is not cast in stone, * changing the return value to some other symbol demands the appropriate * "translation" when hpe_flags is set prior to calling hook_run() for * packet events. */ int ip_get_dlpi_mbcast(ill_t *ill, mblk_t *mb) { dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr; mblk_t *bmp; if (ind->dl_group_address) { if (ind->dl_dest_addr_offset > sizeof (*ind) && ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) && (bmp = ill->ill_bcast_mp) != NULL) { dl_unitdata_req_t *dlur; uint8_t *bphys_addr; dlur = (dl_unitdata_req_t *)bmp->b_rptr; if (ill->ill_sap_length < 0) bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset; else bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset + ill->ill_sap_length; if (bcmp(mb->b_rptr + ind->dl_dest_addr_offset, bphys_addr, ind->dl_dest_addr_length) == 0) { return (HPE_BROADCAST); } return (HPE_MULTICAST); } return (HPE_MULTICAST); } return (0); } static boolean_t ip_rput_process_notdata(queue_t *q, mblk_t **first_mpp, ill_t *ill, int *ll_multicast, mblk_t **mpp) { mblk_t *mp1, *from_mp, *to_mp, *mp, *first_mp; boolean_t must_copy = B_FALSE; struct iocblk *iocp; ipha_t *ipha; ip_stack_t *ipst = ill->ill_ipst; #define rptr ((uchar_t *)ipha) first_mp = *first_mpp; mp = *mpp; ASSERT(first_mp == mp); /* * if db_ref > 1 then copymsg and free original. Packet may be * changed and do not want other entity who has a reference to this * message to trip over the changes. This is a blind change because * trying to catch all places that might change packet is too * difficult (since it may be a module above this one) * * This corresponds to the non-fast path case. We walk down the full * chain in this case, and check the db_ref count of all the dblks, * and do a copymsg if required. It is possible that the db_ref counts * of the data blocks in the mblk chain can be different. * For Example, we can get a DL_UNITDATA_IND(M_PROTO) with a db_ref * count of 1, followed by a M_DATA block with a ref count of 2, if * 'snoop' is running. */ for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { if (mp1->b_datap->db_ref > 1) { must_copy = B_TRUE; break; } } if (must_copy) { mp1 = copymsg(mp); if (mp1 == NULL) { for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { mp1->b_next = NULL; mp1->b_prev = NULL; } freemsg(mp); if (ill != NULL) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); } else { BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); } return (B_TRUE); } for (from_mp = mp, to_mp = mp1; from_mp != NULL; from_mp = from_mp->b_cont, to_mp = to_mp->b_cont) { /* Copy b_prev - used by ip_mroute_decap */ to_mp->b_prev = from_mp->b_prev; from_mp->b_prev = NULL; } *first_mpp = first_mp = mp1; freemsg(mp); mp = mp1; *mpp = mp1; } ipha = (ipha_t *)mp->b_rptr; /* * previous code has a case for M_DATA. * We want to check how that happens. */ ASSERT(first_mp->b_datap->db_type != M_DATA); switch (first_mp->b_datap->db_type) { case M_PROTO: case M_PCPROTO: if (((dl_unitdata_ind_t *)rptr)->dl_primitive != DL_UNITDATA_IND) { /* Go handle anything other than data elsewhere. */ ip_rput_dlpi(q, mp); return (B_TRUE); } *ll_multicast = ip_get_dlpi_mbcast(ill, mp); /* Ditch the DLPI header. */ mp1 = mp->b_cont; ASSERT(first_mp == mp); *first_mpp = mp1; freeb(mp); *mpp = mp1; return (B_FALSE); case M_IOCACK: ip1dbg(("got iocack ")); iocp = (struct iocblk *)mp->b_rptr; switch (iocp->ioc_cmd) { case DL_IOC_HDR_INFO: ill = (ill_t *)q->q_ptr; ill_fastpath_ack(ill, mp); return (B_TRUE); case SIOCSTUNPARAM: case OSIOCSTUNPARAM: /* Go through qwriter_ip */ break; case SIOCGTUNPARAM: case OSIOCGTUNPARAM: ip_rput_other(NULL, q, mp, NULL); return (B_TRUE); default: putnext(q, mp); return (B_TRUE); } /* FALLTHRU */ case M_ERROR: case M_HANGUP: /* * Since this is on the ill stream we unconditionally * bump up the refcount */ ill_refhold(ill); qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); return (B_TRUE); case M_CTL: if ((MBLKL(first_mp) >= sizeof (da_ipsec_t)) && (((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL)) { /* * It's an IPsec accelerated packet. * Make sure that the ill from which we received the * packet has enabled IPsec hardware acceleration. */ if (!(ill->ill_capabilities & (ILL_CAPAB_AH|ILL_CAPAB_ESP))) { /* IPsec kstats: bean counter */ freemsg(mp); return (B_TRUE); } /* * Make mp point to the mblk following the M_CTL, * then process according to type of mp. * After this processing, first_mp will point to * the data-attributes and mp to the pkt following * the M_CTL. */ mp = first_mp->b_cont; if (mp == NULL) { freemsg(first_mp); return (B_TRUE); } /* * A Hardware Accelerated packet can only be M_DATA * ESP or AH packet. */ if (mp->b_datap->db_type != M_DATA) { /* non-M_DATA IPsec accelerated packet */ IPSECHW_DEBUG(IPSECHW_PKT, ("non-M_DATA IPsec accelerated pkt\n")); freemsg(first_mp); return (B_TRUE); } ipha = (ipha_t *)mp->b_rptr; if (ipha->ipha_protocol != IPPROTO_AH && ipha->ipha_protocol != IPPROTO_ESP) { IPSECHW_DEBUG(IPSECHW_PKT, ("non-M_DATA IPsec accelerated pkt\n")); freemsg(first_mp); return (B_TRUE); } *mpp = mp; return (B_FALSE); } putnext(q, mp); return (B_TRUE); case M_IOCNAK: ip1dbg(("got iocnak ")); iocp = (struct iocblk *)mp->b_rptr; switch (iocp->ioc_cmd) { case SIOCSTUNPARAM: case OSIOCSTUNPARAM: /* * Since this is on the ill stream we unconditionally * bump up the refcount */ ill_refhold(ill); qwriter_ip(ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); return (B_TRUE); case DL_IOC_HDR_INFO: case SIOCGTUNPARAM: case OSIOCGTUNPARAM: ip_rput_other(NULL, q, mp, NULL); return (B_TRUE); default: break; } /* FALLTHRU */ default: putnext(q, mp); return (B_TRUE); } } /* Read side put procedure. Packets coming from the wire arrive here. */ void ip_rput(queue_t *q, mblk_t *mp) { ill_t *ill; union DL_primitives *dl; TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_rput_start: q %p", q); ill = (ill_t *)q->q_ptr; if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) { /* * If things are opening or closing, only accept high-priority * DLPI messages. (On open ill->ill_ipif has not yet been * created; on close, things hanging off the ill may have been * freed already.) */ dl = (union DL_primitives *)mp->b_rptr; if (DB_TYPE(mp) != M_PCPROTO || dl->dl_primitive == DL_UNITDATA_IND) { /* * SIOC[GS]TUNPARAM ioctls can come here. */ inet_freemsg(mp); TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, "ip_rput_end: q %p (%S)", q, "uninit"); return; } } TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, "ip_rput_end: q %p (%S)", q, "end"); ip_input(ill, NULL, mp, NULL); } static mblk_t * ip_fix_dbref(ill_t *ill, mblk_t *mp) { mblk_t *mp1; boolean_t adjusted = B_FALSE; ip_stack_t *ipst = ill->ill_ipst; IP_STAT(ipst, ip_db_ref); /* * The IP_RECVSLLA option depends on having the * link layer header. First check that: * a> the underlying device is of type ether, * since this option is currently supported only * over ethernet. * b> there is enough room to copy over the link * layer header. * * Once the checks are done, adjust rptr so that * the link layer header will be copied via * copymsg. Note that, IFT_ETHER may be returned * by some non-ethernet drivers but in this case * the second check will fail. */ if (ill->ill_type == IFT_ETHER && (mp->b_rptr - mp->b_datap->db_base) >= sizeof (struct ether_header)) { mp->b_rptr -= sizeof (struct ether_header); adjusted = B_TRUE; } mp1 = copymsg(mp); if (mp1 == NULL) { mp->b_next = NULL; /* clear b_prev - used by ip_mroute_decap */ mp->b_prev = NULL; freemsg(mp); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return (NULL); } if (adjusted) { /* * Copy is done. Restore the pointer in * the _new_ mblk */ mp1->b_rptr += sizeof (struct ether_header); } /* Copy b_prev - used by ip_mroute_decap */ mp1->b_prev = mp->b_prev; mp->b_prev = NULL; /* preserve the hardware checksum flags and data, if present */ if (DB_CKSUMFLAGS(mp) != 0) { DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp); DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp); DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp); DB_CKSUMEND(mp1) = DB_CKSUMEND(mp); DB_CKSUM16(mp1) = DB_CKSUM16(mp); } freemsg(mp); return (mp1); } /* * Direct read side procedure capable of dealing with chains. GLDv3 based * drivers call this function directly with mblk chains while STREAMS * read side procedure ip_rput() calls this for single packet with ip_ring * set to NULL to process one packet at a time. * * The ill will always be valid if this function is called directly from * the driver. * * If ip_input() is called from GLDv3: * * - This must be a non-VLAN IP stream. * - 'mp' is either an untagged or a special priority-tagged packet. * - Any VLAN tag that was in the MAC header has been stripped. * * If the IP header in packet is not 32-bit aligned, every message in the * chain will be aligned before further operations. This is required on SPARC * platform. */ /* ARGSUSED */ void ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain, struct mac_header_info_s *mhip) { ipaddr_t dst = NULL; ipaddr_t prev_dst; ire_t *ire = NULL; ipha_t *ipha; uint_t pkt_len; ssize_t len; uint_t opt_len; int ll_multicast; int cgtp_flt_pkt; queue_t *q = ill->ill_rq; squeue_t *curr_sqp = NULL; mblk_t *head = NULL; mblk_t *tail = NULL; mblk_t *first_mp; mblk_t *mp; mblk_t *dmp; int cnt = 0; ip_stack_t *ipst = ill->ill_ipst; ASSERT(mp_chain != NULL); ASSERT(ill != NULL); TRACE_1(TR_FAC_IP, TR_IP_RPUT_START, "ip_input_start: q %p", q); #define rptr ((uchar_t *)ipha) while (mp_chain != NULL) { first_mp = mp = mp_chain; mp_chain = mp_chain->b_next; mp->b_next = NULL; ll_multicast = 0; /* * We do ire caching from one iteration to * another. In the event the packet chain contains * all packets from the same dst, this caching saves * an ire_cache_lookup for each of the succeeding * packets in a packet chain. */ prev_dst = dst; /* * if db_ref > 1 then copymsg and free original. Packet * may be changed and we do not want the other entity * who has a reference to this message to trip over the * changes. This is a blind change because trying to * catch all places that might change the packet is too * difficult. * * This corresponds to the fast path case, where we have * a chain of M_DATA mblks. We check the db_ref count * of only the 1st data block in the mblk chain. There * doesn't seem to be a reason why a device driver would * send up data with varying db_ref counts in the mblk * chain. In any case the Fast path is a private * interface, and our drivers don't do such a thing. * Given the above assumption, there is no need to walk * down the entire mblk chain (which could have a * potential performance problem) */ if (DB_REF(mp) > 1) { if ((mp = ip_fix_dbref(ill, mp)) == NULL) continue; } /* * Check and align the IP header. */ first_mp = mp; if (DB_TYPE(mp) == M_DATA) { dmp = mp; } else if (DB_TYPE(mp) == M_PROTO && *(t_uscalar_t *)mp->b_rptr == DL_UNITDATA_IND) { dmp = mp->b_cont; } else { dmp = NULL; } if (dmp != NULL) { /* * IP header ptr not aligned? * OR IP header not complete in first mblk */ if (!OK_32PTR(dmp->b_rptr) || MBLKL(dmp) < IP_SIMPLE_HDR_LENGTH) { if (!ip_check_and_align_header(q, dmp, ipst)) continue; } } /* * ip_input fast path */ /* mblk type is not M_DATA */ if (DB_TYPE(mp) != M_DATA) { if (ip_rput_process_notdata(q, &first_mp, ill, &ll_multicast, &mp)) continue; /* * The only way we can get here is if we had a * packet that was either a DL_UNITDATA_IND or * an M_CTL for an IPsec accelerated packet. * * In either case, the first_mp will point to * the leading M_PROTO or M_CTL. */ ASSERT(first_mp != NULL); } else if (mhip != NULL) { /* * ll_multicast is set here so that it is ready * for easy use with FW_HOOKS(). ip_get_dlpi_mbcast * manipulates ll_multicast in the same fashion when * called from ip_rput_process_notdata. */ switch (mhip->mhi_dsttype) { case MAC_ADDRTYPE_MULTICAST : ll_multicast = HPE_MULTICAST; break; case MAC_ADDRTYPE_BROADCAST : ll_multicast = HPE_BROADCAST; break; default : break; } } /* Make sure its an M_DATA and that its aligned */ ASSERT(DB_TYPE(mp) == M_DATA); ASSERT(DB_REF(mp) == 1 && OK_32PTR(mp->b_rptr)); ipha = (ipha_t *)mp->b_rptr; len = mp->b_wptr - rptr; pkt_len = ntohs(ipha->ipha_length); /* * We must count all incoming packets, even if they end * up being dropped later on. */ BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives); UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets, pkt_len); /* multiple mblk or too short */ len -= pkt_len; if (len != 0) { /* * Make sure we have data length consistent * with the IP header. */ if (mp->b_cont == NULL) { if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); ip2dbg(("ip_input: drop pkt\n")); freemsg(mp); continue; } mp->b_wptr = rptr + pkt_len; } else if ((len += msgdsize(mp->b_cont)) != 0) { if (len < 0 || pkt_len < IP_SIMPLE_HDR_LENGTH) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors); ip2dbg(("ip_input: drop pkt\n")); freemsg(mp); continue; } (void) adjmsg(mp, -len); IP_STAT(ipst, ip_multimblk3); } } /* Obtain the dst of the current packet */ dst = ipha->ipha_dst; DTRACE_IP7(receive, mblk_t *, first_mp, conn_t *, NULL, void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, ip6_t *, NULL, int, 0); /* * The following test for loopback is faster than * IP_LOOPBACK_ADDR(), because it avoids any bitwise * operations. * Note that these addresses are always in network byte order */ if (((*(uchar_t *)&ipha->ipha_dst) == 127) || ((*(uchar_t *)&ipha->ipha_src) == 127)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors); freemsg(mp); continue; } /* * The event for packets being received from a 'physical' * interface is placed after validation of the source and/or * destination address as being local so that packets can be * redirected to loopback addresses using ipnat. */ DTRACE_PROBE4(ip4__physical__in__start, ill_t *, ill, ill_t *, NULL, ipha_t *, ipha, mblk_t *, first_mp); FW_HOOKS(ipst->ips_ip4_physical_in_event, ipst->ips_ipv4firewall_physical_in, ill, NULL, ipha, first_mp, mp, ll_multicast, ipst); DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, first_mp); if (first_mp == NULL) { continue; } dst = ipha->ipha_dst; /* * Attach any necessary label information to * this packet */ if (is_system_labeled() && !tsol_get_pkt_label(mp, IPV4_VERSION)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(mp); continue; } /* * Reuse the cached ire only if the ipha_dst of the previous * packet is the same as the current packet AND it is not * INADDR_ANY. */ if (!(dst == prev_dst && dst != INADDR_ANY) && (ire != NULL)) { ire_refrele(ire); ire = NULL; } opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION; /* * Check to see if we can take the fastpath. * That is possible if the following conditions are met * o Tsol disabled * o CGTP disabled * o ipp_action_count is 0 * o no options in the packet * o not a RSVP packet * o not a multicast packet * o ill not in IP_DHCPINIT_IF mode */ if (!is_system_labeled() && !ipst->ips_ip_cgtp_filter && ipp_action_count == 0 && opt_len == 0 && ipha->ipha_protocol != IPPROTO_RSVP && !ll_multicast && !CLASSD(dst) && ill->ill_dhcpinit == 0) { if (ire == NULL) ire = ire_cache_lookup(dst, ALL_ZONES, NULL, ipst); /* incoming packet is for forwarding */ if (ire == NULL || (ire->ire_type & IRE_CACHE)) { ire = ip_fast_forward(ire, dst, ill, mp); continue; } /* incoming packet is for local consumption */ if (ire->ire_type & IRE_LOCAL) goto local; } /* * Disable ire caching for anything more complex * than the simple fast path case we checked for above. */ if (ire != NULL) { ire_refrele(ire); ire = NULL; } /* * Brutal hack for DHCPv4 unicast: RFC2131 allows a DHCP * server to unicast DHCP packets to a DHCP client using the * IP address it is offering to the client. This can be * disabled through the "broadcast bit", but not all DHCP * servers honor that bit. Therefore, to interoperate with as * many DHCP servers as possible, the DHCP client allows the * server to unicast, but we treat those packets as broadcast * here. Note that we don't rewrite the packet itself since * (a) that would mess up the checksums and (b) the DHCP * client conn is bound to INADDR_ANY so ip_fanout_udp() will * hand it the packet regardless. */ if (ill->ill_dhcpinit != 0 && IS_SIMPLE_IPH(ipha) && ipha->ipha_protocol == IPPROTO_UDP && pullupmsg(mp, sizeof (ipha_t) + sizeof (udpha_t)) == 1) { udpha_t *udpha; /* * Reload ipha since pullupmsg() can change b_rptr. */ ipha = (ipha_t *)mp->b_rptr; udpha = (udpha_t *)&ipha[1]; if (ntohs(udpha->uha_dst_port) == IPPORT_BOOTPC) { DTRACE_PROBE2(ip4__dhcpinit__pkt, ill_t *, ill, mblk_t *, mp); dst = INADDR_BROADCAST; } } /* Full-blown slow path */ if (opt_len != 0) { if (len != 0) IP_STAT(ipst, ip_multimblk4); else IP_STAT(ipst, ip_ipoptions); if (!ip_rput_multimblk_ipoptions(q, ill, mp, &ipha, &dst, ipst)) continue; } /* * Invoke the CGTP (multirouting) filtering module to process * the incoming packet. Packets identified as duplicates * must be discarded. Filtering is active only if the * the ip_cgtp_filter ndd variable is non-zero. */ cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP; if (ipst->ips_ip_cgtp_filter && ipst->ips_ip_cgtp_filter_ops != NULL) { netstackid_t stackid; stackid = ipst->ips_netstack->netstack_stackid; cgtp_flt_pkt = ipst->ips_ip_cgtp_filter_ops->cfo_filter(stackid, ill->ill_phyint->phyint_ifindex, mp); if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { freemsg(first_mp); continue; } } /* * If rsvpd is running, let RSVP daemon handle its processing * and forwarding of RSVP multicast/unicast packets. * If rsvpd is not running but mrouted is running, RSVP * multicast packets are forwarded as multicast traffic * and RSVP unicast packets are forwarded by unicast router. * If neither rsvpd nor mrouted is running, RSVP multicast * packets are not forwarded, but the unicast packets are * forwarded like unicast traffic. */ if (ipha->ipha_protocol == IPPROTO_RSVP && ipst->ips_ipcl_proto_fanout[IPPROTO_RSVP].connf_head != NULL) { /* RSVP packet and rsvpd running. Treat as ours */ ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(dst))); /* * This assumes that we deliver to all streams for * multicast and broadcast packets. * We have to force ll_multicast to 1 to handle the * M_DATA messages passed in from ip_mroute_decap. */ dst = INADDR_BROADCAST; ll_multicast = 1; } else if (CLASSD(dst)) { /* packet is multicast */ mp->b_next = NULL; if (ip_rput_process_multicast(q, mp, ill, ipha, &ll_multicast, &dst)) continue; } if (ire == NULL) { ire = ire_cache_lookup(dst, ALL_ZONES, MBLK_GETLABEL(mp), ipst); } if (ire != NULL && ire->ire_stq != NULL && ire->ire_zoneid != GLOBAL_ZONEID && ire->ire_zoneid != ALL_ZONES) { /* * Should only use IREs that are visible from the * global zone for forwarding. */ ire_refrele(ire); ire = ire_cache_lookup(dst, GLOBAL_ZONEID, MBLK_GETLABEL(mp), ipst); } if (ire == NULL) { /* * No IRE for this destination, so it can't be for us. * Unless we are forwarding, drop the packet. * We have to let source routed packets through * since we don't yet know if they are 'ping -l' * packets i.e. if they will go out over the * same interface as they came in on. */ ire = ip_rput_noire(q, mp, ll_multicast, dst); if (ire == NULL) continue; } /* * Broadcast IRE may indicate either broadcast or * multicast packet */ if (ire->ire_type == IRE_BROADCAST) { /* * Skip broadcast checks if packet is UDP multicast; * we'd rather not enter ip_rput_process_broadcast() * unless the packet is broadcast for real, since * that routine is a no-op for multicast. */ if (ipha->ipha_protocol != IPPROTO_UDP || !CLASSD(ipha->ipha_dst)) { ire = ip_rput_process_broadcast(&q, mp, ire, ipha, ill, dst, cgtp_flt_pkt, ll_multicast); if (ire == NULL) continue; } } else if (ire->ire_stq != NULL) { /* fowarding? */ ip_rput_process_forward(q, mp, ire, ipha, ill, ll_multicast); /* ip_rput_process_forward consumed the packet */ continue; } local: /* * If the queue in the ire is different to the ingress queue * then we need to check to see if we can accept the packet. * Note that for multicast packets and broadcast packets sent * to a broadcast address which is shared between multiple * interfaces we should not do this since we just got a random * broadcast ire. */ if ((ire->ire_rfq != q) && (ire->ire_type != IRE_BROADCAST)) { if ((ire = ip_check_multihome(&ipha->ipha_dst, ire, ill)) == NULL) { /* Drop packet */ BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits); freemsg(mp); continue; } if (ire->ire_rfq != NULL) q = ire->ire_rfq; } switch (ipha->ipha_protocol) { case IPPROTO_TCP: ASSERT(first_mp == mp); if ((mp = ip_tcp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, q, ip_ring)) != NULL) { if (curr_sqp == NULL) { curr_sqp = GET_SQUEUE(mp); ASSERT(cnt == 0); cnt++; head = tail = mp; } else if (curr_sqp == GET_SQUEUE(mp)) { ASSERT(tail != NULL); cnt++; tail->b_next = mp; tail = mp; } else { /* * A different squeue. Send the * chain for the previous squeue on * its way. This shouldn't happen * often unless interrupt binding * changes. */ IP_STAT(ipst, ip_input_multi_squeue); squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); curr_sqp = GET_SQUEUE(mp); head = mp; tail = mp; cnt = 1; } } continue; case IPPROTO_UDP: ASSERT(first_mp == mp); ip_udp_input(q, mp, ipha, ire, ill); continue; case IPPROTO_SCTP: ASSERT(first_mp == mp); ip_sctp_input(mp, ipha, ill, B_FALSE, ire, mp, 0, q, dst); /* ire has been released by ip_sctp_input */ ire = NULL; continue; default: ip_proto_input(q, first_mp, ipha, ire, ill, 0); continue; } } if (ire != NULL) ire_refrele(ire); if (head != NULL) squeue_enter_chain(curr_sqp, head, tail, cnt, SQTAG_IP_INPUT); /* * This code is there just to make netperf/ttcp look good. * * Its possible that after being in polling mode (and having cleared * the backlog), squeues have turned the interrupt frequency higher * to improve latency at the expense of more CPU utilization (less * packets per interrupts or more number of interrupts). Workloads * like ttcp/netperf do manage to tickle polling once in a while * but for the remaining time, stay in higher interrupt mode since * their packet arrival rate is pretty uniform and this shows up * as higher CPU utilization. Since people care about CPU utilization * while running netperf/ttcp, turn the interrupt frequency back to * normal/default if polling has not been used in ip_poll_normal_ticks. */ if (ip_ring != NULL && (ip_ring->rr_poll_state & ILL_POLLING)) { if (lbolt >= (ip_ring->rr_poll_time + ip_poll_normal_ticks)) { ip_ring->rr_poll_state &= ~ILL_POLLING; ip_ring->rr_blank(ip_ring->rr_handle, ip_ring->rr_normal_blank_time, ip_ring->rr_normal_pkt_cnt); } } TRACE_2(TR_FAC_IP, TR_IP_RPUT_END, "ip_input_end: q %p (%S)", q, "end"); #undef rptr } static void ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err, t_uscalar_t err) { if (dl_err == DL_SYSERR) { (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, "%s: %s failed: DL_SYSERR (errno %u)\n", ill->ill_name, dl_primstr(prim), err); return; } (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE, "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim), dl_errstr(dl_err)); } /* * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other * than DL_UNITDATA_IND messages. If we need to process this message * exclusively, we call qwriter_ip, in which case we also need to call * ill_refhold before that, since qwriter_ip does an ill_refrele. */ void ip_rput_dlpi(queue_t *q, mblk_t *mp) { dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; ill_t *ill = q->q_ptr; t_uscalar_t prim = dloa->dl_primitive; t_uscalar_t reqprim = DL_PRIM_INVAL; ip1dbg(("ip_rput_dlpi")); /* * If we received an ACK but didn't send a request for it, then it * can't be part of any pending operation; discard up-front. */ switch (prim) { case DL_ERROR_ACK: reqprim = dlea->dl_error_primitive; ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s " "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim), reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno, dlea->dl_unix_errno)); break; case DL_OK_ACK: reqprim = dloa->dl_correct_primitive; break; case DL_INFO_ACK: reqprim = DL_INFO_REQ; break; case DL_BIND_ACK: reqprim = DL_BIND_REQ; break; case DL_PHYS_ADDR_ACK: reqprim = DL_PHYS_ADDR_REQ; break; case DL_NOTIFY_ACK: reqprim = DL_NOTIFY_REQ; break; case DL_CONTROL_ACK: reqprim = DL_CONTROL_REQ; break; case DL_CAPABILITY_ACK: reqprim = DL_CAPABILITY_REQ; break; } if (prim != DL_NOTIFY_IND) { if (reqprim == DL_PRIM_INVAL || !ill_dlpi_pending(ill, reqprim)) { /* Not a DLPI message we support or expected */ freemsg(mp); return; } ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim), dl_primstr(reqprim))); } switch (reqprim) { case DL_UNBIND_REQ: /* * NOTE: we mark the unbind as complete even if we got a * DL_ERROR_ACK, since there's not much else we can do. */ mutex_enter(&ill->ill_lock); ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; cv_signal(&ill->ill_cv); mutex_exit(&ill->ill_lock); break; case DL_ENABMULTI_REQ: if (prim == DL_OK_ACK) { if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) ill->ill_dlpi_multicast_state = IDS_OK; } break; } /* * The message is one we're waiting for (or DL_NOTIFY_IND), but we * need to become writer to continue to process it. Because an * exclusive operation doesn't complete until replies to all queued * DLPI messages have been received, we know we're in the middle of an * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND). * * As required by qwriter_ip(), we refhold the ill; it will refrele. * Since this is on the ill stream we unconditionally bump up the * refcount without doing ILL_CAN_LOOKUP(). */ ill_refhold(ill); if (prim == DL_NOTIFY_IND) qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE); else qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE); } /* * Handling of DLPI messages that require exclusive access to the ipsq. * * Need to do ill_pending_mp_release on ioctl completion, which could * happen here. (along with mi_copy_done) */ /* ARGSUSED */ static void ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) { dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr; dl_error_ack_t *dlea = (dl_error_ack_t *)dloa; int err = 0; ill_t *ill; ipif_t *ipif = NULL; mblk_t *mp1 = NULL; conn_t *connp = NULL; t_uscalar_t paddrreq; mblk_t *mp_hw; boolean_t success; boolean_t ioctl_aborted = B_FALSE; boolean_t log = B_TRUE; ip_stack_t *ipst; ip1dbg(("ip_rput_dlpi_writer ..")); ill = (ill_t *)q->q_ptr; ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); ASSERT(IAM_WRITER_ILL(ill)); ipst = ill->ill_ipst; /* * ipsq_pending_mp and ipsq_pending_ipif track each other. i.e. * both are null or non-null. However we can assert that only * after grabbing the ipsq_lock. So we don't make any assertion * here and in other places in the code. */ ipif = ipsq->ipsq_pending_ipif; /* * The current ioctl could have been aborted by the user and a new * ioctl to bring up another ill could have started. We could still * get a response from the driver later. */ if (ipif != NULL && ipif->ipif_ill != ill) ioctl_aborted = B_TRUE; switch (dloa->dl_primitive) { case DL_ERROR_ACK: ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n", dl_primstr(dlea->dl_error_primitive))); switch (dlea->dl_error_primitive) { case DL_DISABMULTI_REQ: if (!ill->ill_isv6) ipsq_current_finish(ipsq); ill_dlpi_done(ill, dlea->dl_error_primitive); break; case DL_PROMISCON_REQ: case DL_PROMISCOFF_REQ: case DL_UNBIND_REQ: case DL_ATTACH_REQ: case DL_INFO_REQ: ill_dlpi_done(ill, dlea->dl_error_primitive); break; case DL_NOTIFY_REQ: ill_dlpi_done(ill, DL_NOTIFY_REQ); log = B_FALSE; break; case DL_PHYS_ADDR_REQ: /* * For IPv6 only, there are two additional * phys_addr_req's sent to the driver to get the * IPv6 token and lla. This allows IP to acquire * the hardware address format for a given interface * without having built in knowledge of the hardware * address. ill_phys_addr_pend keeps track of the last * DL_PAR sent so we know which response we are * dealing with. ill_dlpi_done will update * ill_phys_addr_pend when it sends the next req. * We don't complete the IOCTL until all three DL_PARs * have been attempted, so set *_len to 0 and break. */ paddrreq = ill->ill_phys_addr_pend; ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); if (paddrreq == DL_IPV6_TOKEN) { ill->ill_token_length = 0; log = B_FALSE; break; } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { ill->ill_nd_lla_len = 0; log = B_FALSE; break; } /* * Something went wrong with the DL_PHYS_ADDR_REQ. * We presumably have an IOCTL hanging out waiting * for completion. Find it and complete the IOCTL * with the error noted. * However, ill_dl_phys was called on an ill queue * (from SIOCSLIFNAME), thus conn_pending_ill is not * set. But the ioctl is known to be pending on ill_wq. */ if (!ill->ill_ifname_pending) break; ill->ill_ifname_pending = 0; if (!ioctl_aborted) mp1 = ipsq_pending_mp_get(ipsq, &connp); if (mp1 != NULL) { /* * This operation (SIOCSLIFNAME) must have * happened on the ill. Assert there is no conn */ ASSERT(connp == NULL); q = ill->ill_wq; } break; case DL_BIND_REQ: ill_dlpi_done(ill, DL_BIND_REQ); if (ill->ill_ifname_pending) break; /* * Something went wrong with the bind. We presumably * have an IOCTL hanging out waiting for completion. * Find it, take down the interface that was coming * up, and complete the IOCTL with the error noted. */ if (!ioctl_aborted) mp1 = ipsq_pending_mp_get(ipsq, &connp); if (mp1 != NULL) { /* * This operation (SIOCSLIFFLAGS) must have * happened from a conn. */ ASSERT(connp != NULL); q = CONNP_TO_WQ(connp); if (ill->ill_move_in_progress) { ILL_CLEAR_MOVE(ill); } (void) ipif_down(ipif, NULL, NULL); /* error is set below the switch */ } break; case DL_ENABMULTI_REQ: if (!ill->ill_isv6) ipsq_current_finish(ipsq); ill_dlpi_done(ill, DL_ENABMULTI_REQ); if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS) ill->ill_dlpi_multicast_state = IDS_FAILED; if (ill->ill_dlpi_multicast_state == IDS_FAILED) { ipif_t *ipif; printf("ip: joining multicasts failed (%d)" " on %s - will use link layer " "broadcasts for multicast\n", dlea->dl_errno, ill->ill_name); /* * Set up the multicast mapping alone. * writer, so ok to access ill->ill_ipif * without any lock. */ ipif = ill->ill_ipif; mutex_enter(&ill->ill_phyint->phyint_lock); ill->ill_phyint->phyint_flags |= PHYI_MULTI_BCAST; mutex_exit(&ill->ill_phyint->phyint_lock); if (!ill->ill_isv6) { (void) ipif_arp_setup_multicast(ipif, NULL); } else { (void) ipif_ndp_setup_multicast(ipif, NULL); } } freemsg(mp); /* Don't want to pass this up */ return; case DL_CAPABILITY_REQ: case DL_CONTROL_REQ: ill_dlpi_done(ill, dlea->dl_error_primitive); ill->ill_dlpi_capab_state = IDS_FAILED; freemsg(mp); return; } /* * Note the error for IOCTL completion (mp1 is set when * ready to complete ioctl). If ill_ifname_pending_err is * set, an error occured during plumbing (ill_ifname_pending), * so we want to report that error. * * NOTE: there are two addtional DL_PHYS_ADDR_REQ's * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are * expected to get errack'd if the driver doesn't support * these flags (e.g. ethernet). log will be set to B_FALSE * if these error conditions are encountered. */ if (mp1 != NULL) { if (ill->ill_ifname_pending_err != 0) { err = ill->ill_ifname_pending_err; ill->ill_ifname_pending_err = 0; } else { err = dlea->dl_unix_errno ? dlea->dl_unix_errno : ENXIO; } /* * If we're plumbing an interface and an error hasn't already * been saved, set ill_ifname_pending_err to the error passed * up. Ignore the error if log is B_FALSE (see comment above). */ } else if (log && ill->ill_ifname_pending && ill->ill_ifname_pending_err == 0) { ill->ill_ifname_pending_err = dlea->dl_unix_errno ? dlea->dl_unix_errno : ENXIO; } if (log) ip_dlpi_error(ill, dlea->dl_error_primitive, dlea->dl_errno, dlea->dl_unix_errno); break; case DL_CAPABILITY_ACK: /* Call a routine to handle this one. */ ill_dlpi_done(ill, DL_CAPABILITY_REQ); ill_capability_ack(ill, mp); /* * If the ack is due to renegotiation, we will need to send * a new CAPABILITY_REQ to start the renegotiation. */ if (ill->ill_capab_reneg) { ill->ill_capab_reneg = B_FALSE; ill_capability_probe(ill); } break; case DL_CONTROL_ACK: /* We treat all of these as "fire and forget" */ ill_dlpi_done(ill, DL_CONTROL_REQ); break; case DL_INFO_ACK: /* Call a routine to handle this one. */ ill_dlpi_done(ill, DL_INFO_REQ); ip_ll_subnet_defaults(ill, mp); ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock)); return; case DL_BIND_ACK: /* * We should have an IOCTL waiting on this unless * sent by ill_dl_phys, in which case just return */ ill_dlpi_done(ill, DL_BIND_REQ); if (ill->ill_ifname_pending) break; if (!ioctl_aborted) mp1 = ipsq_pending_mp_get(ipsq, &connp); if (mp1 == NULL) break; /* * Because mp1 was added by ill_dl_up(), and it always * passes a valid connp, connp must be valid here. */ ASSERT(connp != NULL); q = CONNP_TO_WQ(connp); /* * We are exclusive. So nothing can change even after * we get the pending mp. If need be we can put it back * and restart, as in calling ipif_arp_up() below. */ ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name)); mutex_enter(&ill->ill_lock); ill->ill_dl_up = 1; (void) ill_hook_event_create(ill, 0, NE_UP, NULL, 0); mutex_exit(&ill->ill_lock); /* * Now bring up the resolver; when that is complete, we'll * create IREs. Note that we intentionally mirror what * ipif_up() would have done, because we got here by way of * ill_dl_up(), which stopped ipif_up()'s processing. */ if (ill->ill_isv6) { /* * v6 interfaces. * Unlike ARP which has to do another bind * and attach, once we get here we are * done with NDP. Except in the case of * ILLF_XRESOLV, in which case we send an * AR_INTERFACE_UP to the external resolver. * If all goes well, the ioctl will complete * in ip_rput(). If there's an error, we * complete it here. */ if ((err = ipif_ndp_up(ipif)) == 0) { if (ill->ill_flags & ILLF_XRESOLV) { mutex_enter(&connp->conn_lock); mutex_enter(&ill->ill_lock); success = ipsq_pending_mp_add( connp, ipif, q, mp1, 0); mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); if (success) { err = ipif_resolver_up(ipif, Res_act_initial); if (err == EINPROGRESS) { freemsg(mp); return; } ASSERT(err != 0); mp1 = ipsq_pending_mp_get(ipsq, &connp); ASSERT(mp1 != NULL); } else { /* conn has started closing */ err = EINTR; } } else { /* Non XRESOLV interface */ (void) ipif_resolver_up(ipif, Res_act_initial); err = ipif_up_done_v6(ipif); } } } else if (ill->ill_net_type == IRE_IF_RESOLVER) { /* * ARP and other v4 external resolvers. * Leave the pending mblk intact so that * the ioctl completes in ip_rput(). */ mutex_enter(&connp->conn_lock); mutex_enter(&ill->ill_lock); success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0); mutex_exit(&ill->ill_lock); mutex_exit(&connp->conn_lock); if (success) { err = ipif_resolver_up(ipif, Res_act_initial); if (err == EINPROGRESS) { freemsg(mp); return; } ASSERT(err != 0); mp1 = ipsq_pending_mp_get(ipsq, &connp); } else { /* The conn has started closing */ err = EINTR; } } else { /* * This one is complete. Reply to pending ioctl. */ (void) ipif_resolver_up(ipif, Res_act_initial); err = ipif_up_done(ipif); } if ((err == 0) && (ill->ill_up_ipifs)) { err = ill_up_ipifs(ill, q, mp1); if (err == EINPROGRESS) { freemsg(mp); return; } } if (ill->ill_up_ipifs) { ill_group_cleanup(ill); } break; case DL_NOTIFY_IND: { dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr; ire_t *ire; boolean_t need_ire_walk_v4 = B_FALSE; boolean_t need_ire_walk_v6 = B_FALSE; switch (notify->dl_notification) { case DL_NOTE_PHYS_ADDR: err = ill_set_phys_addr(ill, mp); break; case DL_NOTE_FASTPATH_FLUSH: ill_fastpath_flush(ill); break; case DL_NOTE_SDU_SIZE: /* * Change the MTU size of the interface, of all * attached ipif's, and of all relevant ire's. The * new value's a uint32_t at notify->dl_data. * Mtu change Vs. new ire creation - protocol below. * * a Mark the ipif as IPIF_CHANGING. * b Set the new mtu in the ipif. * c Change the ire_max_frag on all affected ires * d Unmark the IPIF_CHANGING * * To see how the protocol works, assume an interface * route is also being added simultaneously by * ip_rt_add and let 'ipif' be the ipif referenced by * the ire. If the ire is created before step a, * it will be cleaned up by step c. If the ire is * created after step d, it will see the new value of * ipif_mtu. Any attempt to create the ire between * steps a to d will fail because of the IPIF_CHANGING * flag. Note that ire_create() is passed a pointer to * the ipif_mtu, and not the value. During ire_add * under the bucket lock, the ire_max_frag of the * new ire being created is set from the ipif/ire from * which it is being derived. */ mutex_enter(&ill->ill_lock); ill->ill_max_frag = (uint_t)notify->dl_data; /* * If an SIOCSLIFLNKINFO has changed the ill_max_mtu * leave it alone */ if (ill->ill_mtu_userspecified) { mutex_exit(&ill->ill_lock); break; } ill->ill_max_mtu = ill->ill_max_frag; if (ill->ill_isv6) { if (ill->ill_max_mtu < IPV6_MIN_MTU) ill->ill_max_mtu = IPV6_MIN_MTU; } else { if (ill->ill_max_mtu < IP_MIN_MTU) ill->ill_max_mtu = IP_MIN_MTU; } for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { /* * Don't override the mtu if the user * has explicitly set it. */ if (ipif->ipif_flags & IPIF_FIXEDMTU) continue; ipif->ipif_mtu = (uint_t)notify->dl_data; if (ipif->ipif_isv6) ire = ipif_to_ire_v6(ipif); else ire = ipif_to_ire(ipif); if (ire != NULL) { ire->ire_max_frag = ipif->ipif_mtu; ire_refrele(ire); } if (ipif->ipif_flags & IPIF_UP) { if (ill->ill_isv6) need_ire_walk_v6 = B_TRUE; else need_ire_walk_v4 = B_TRUE; } } mutex_exit(&ill->ill_lock); if (need_ire_walk_v4) ire_walk_v4(ill_mtu_change, (char *)ill, ALL_ZONES, ipst); if (need_ire_walk_v6) ire_walk_v6(ill_mtu_change, (char *)ill, ALL_ZONES, ipst); break; case DL_NOTE_LINK_UP: case DL_NOTE_LINK_DOWN: { /* * We are writer. ill / phyint / ipsq assocs stable. * The RUNNING flag reflects the state of the link. */ phyint_t *phyint = ill->ill_phyint; uint64_t new_phyint_flags; boolean_t changed = B_FALSE; boolean_t went_up; went_up = notify->dl_notification == DL_NOTE_LINK_UP; mutex_enter(&phyint->phyint_lock); new_phyint_flags = went_up ? phyint->phyint_flags | PHYI_RUNNING : phyint->phyint_flags & ~PHYI_RUNNING; if (new_phyint_flags != phyint->phyint_flags) { phyint->phyint_flags = new_phyint_flags; changed = B_TRUE; } mutex_exit(&phyint->phyint_lock); /* * ill_restart_dad handles the DAD restart and routing * socket notification logic. */ if (changed) { ill_restart_dad(phyint->phyint_illv4, went_up); ill_restart_dad(phyint->phyint_illv6, went_up); } break; } case DL_NOTE_PROMISC_ON_PHYS: IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " "got a DL_NOTE_PROMISC_ON_PHYS\n")); mutex_enter(&ill->ill_lock); ill->ill_promisc_on_phys = B_TRUE; mutex_exit(&ill->ill_lock); break; case DL_NOTE_PROMISC_OFF_PHYS: IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_dlpi_writer: " "got a DL_NOTE_PROMISC_OFF_PHYS\n")); mutex_enter(&ill->ill_lock); ill->ill_promisc_on_phys = B_FALSE; mutex_exit(&ill->ill_lock); break; case DL_NOTE_CAPAB_RENEG: /* * Something changed on the driver side. * It wants us to renegotiate the capabilities * on this ill. One possible cause is the aggregation * interface under us where a port got added or * went away. * * If the capability negotiation is already done * or is in progress, reset the capabilities and * mark the ill's ill_capab_reneg to be B_TRUE, * so that when the ack comes back, we can start * the renegotiation process. * * Note that if ill_capab_reneg is already B_TRUE * (ill_dlpi_capab_state is IDS_UNKNOWN in this case), * the capability resetting request has been sent * and the renegotiation has not been started yet; * nothing needs to be done in this case. */ if (ill->ill_dlpi_capab_state != IDS_UNKNOWN) { ill_capability_reset(ill); ill->ill_capab_reneg = B_TRUE; } break; default: ip0dbg(("ip_rput_dlpi_writer: unknown notification " "type 0x%x for DL_NOTIFY_IND\n", notify->dl_notification)); break; } /* * As this is an asynchronous operation, we * should not call ill_dlpi_done */ break; } case DL_NOTIFY_ACK: { dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr; if (noteack->dl_notifications & DL_NOTE_LINK_UP) ill->ill_note_link = 1; ill_dlpi_done(ill, DL_NOTIFY_REQ); break; } case DL_PHYS_ADDR_ACK: { /* * As part of plumbing the interface via SIOCSLIFNAME, * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs, * whose answers we receive here. As each answer is received, * we call ill_dlpi_done() to dispatch the next request as * we're processing the current one. Once all answers have * been received, we use ipsq_pending_mp_get() to dequeue the * outstanding IOCTL and reply to it. (Because ill_dl_phys() * is invoked from an ill queue, conn_oper_pending_ill is not * available, but we know the ioctl is pending on ill_wq.) */ uint_t paddrlen, paddroff; paddrreq = ill->ill_phys_addr_pend; paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length; paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset; ill_dlpi_done(ill, DL_PHYS_ADDR_REQ); if (paddrreq == DL_IPV6_TOKEN) { /* * bcopy to low-order bits of ill_token * * XXX Temporary hack - currently, all known tokens * are 64 bits, so I'll cheat for the moment. */ bcopy(mp->b_rptr + paddroff, &ill->ill_token.s6_addr32[2], paddrlen); ill->ill_token_length = paddrlen; break; } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) { ASSERT(ill->ill_nd_lla_mp == NULL); ill_set_ndmp(ill, mp, paddroff, paddrlen); mp = NULL; break; } ASSERT(paddrreq == DL_CURR_PHYS_ADDR); ASSERT(ill->ill_phys_addr_mp == NULL); if (!ill->ill_ifname_pending) break; ill->ill_ifname_pending = 0; if (!ioctl_aborted) mp1 = ipsq_pending_mp_get(ipsq, &connp); if (mp1 != NULL) { ASSERT(connp == NULL); q = ill->ill_wq; } /* * If any error acks received during the plumbing sequence, * ill_ifname_pending_err will be set. Break out and send up * the error to the pending ioctl. */ if (ill->ill_ifname_pending_err != 0) { err = ill->ill_ifname_pending_err; ill->ill_ifname_pending_err = 0; break; } ill->ill_phys_addr_mp = mp; ill->ill_phys_addr = mp->b_rptr + paddroff; mp = NULL; /* * If paddrlen is zero, the DLPI provider doesn't support * physical addresses. The other two tests were historical * workarounds for bugs in our former PPP implementation, but * now other things have grown dependencies on them -- e.g., * the tun module specifies a dl_addr_length of zero in its * DL_BIND_ACK, but then specifies an incorrect value in its * DL_PHYS_ADDR_ACK. These bogus checks need to be removed, * but only after careful testing ensures that all dependent * broken DLPI providers have been fixed. */ if (paddrlen == 0 || ill->ill_phys_addr_length == 0 || ill->ill_phys_addr_length == IP_ADDR_LEN) { ill->ill_phys_addr = NULL; } else if (paddrlen != ill->ill_phys_addr_length) { ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d", paddrlen, ill->ill_phys_addr_length)); err = EINVAL; break; } if (ill->ill_nd_lla_mp == NULL) { if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) { err = ENOMEM; break; } ill_set_ndmp(ill, mp_hw, paddroff, paddrlen); } /* * Set the interface token. If the zeroth interface address * is unspecified, then set it to the link local address. */ if (IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token)) (void) ill_setdefaulttoken(ill); ASSERT(ill->ill_ipif->ipif_id == 0); if (ipif != NULL && IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { (void) ipif_setlinklocal(ipif); } break; } case DL_OK_ACK: ip2dbg(("DL_OK_ACK %s (0x%x)\n", dl_primstr((int)dloa->dl_correct_primitive), dloa->dl_correct_primitive)); switch (dloa->dl_correct_primitive) { case DL_ENABMULTI_REQ: case DL_DISABMULTI_REQ: if (!ill->ill_isv6) ipsq_current_finish(ipsq); ill_dlpi_done(ill, dloa->dl_correct_primitive); break; case DL_PROMISCON_REQ: case DL_PROMISCOFF_REQ: case DL_UNBIND_REQ: case DL_ATTACH_REQ: ill_dlpi_done(ill, dloa->dl_correct_primitive); break; } break; default: break; } freemsg(mp); if (mp1 != NULL) { /* * The operation must complete without EINPROGRESS * since ipsq_pending_mp_get() has removed the mblk * from ipsq_pending_mp. Otherwise, the operation * will be stuck forever in the ipsq. */ ASSERT(err != EINPROGRESS); switch (ipsq->ipsq_current_ioctl) { case 0: ipsq_current_finish(ipsq); break; case SIOCLIFADDIF: case SIOCSLIFNAME: ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq); break; default: ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq); break; } } } /* * ip_rput_other is called by ip_rput to handle messages modifying the global * state in IP. Normally called as writer. Exception SIOCGTUNPARAM (shared) */ /* ARGSUSED */ void ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) { ill_t *ill; struct iocblk *iocp; mblk_t *mp1; conn_t *connp = NULL; ip1dbg(("ip_rput_other ")); ill = (ill_t *)q->q_ptr; /* * This routine is not a writer in the case of SIOCGTUNPARAM * in which case ipsq is NULL. */ if (ipsq != NULL) { ASSERT(IAM_WRITER_IPSQ(ipsq)); ASSERT(ipsq == ill->ill_phyint->phyint_ipsq); } switch (mp->b_datap->db_type) { case M_ERROR: case M_HANGUP: /* * The device has a problem. We force the ILL down. It can * be brought up again manually using SIOCSIFFLAGS (via * ifconfig or equivalent). */ ASSERT(ipsq != NULL); if (mp->b_rptr < mp->b_wptr) ill->ill_error = (int)(*mp->b_rptr & 0xFF); if (ill->ill_error == 0) ill->ill_error = ENXIO; if (!ill_down_start(q, mp)) return; ipif_all_down_tail(ipsq, q, mp, NULL); break; case M_IOCACK: iocp = (struct iocblk *)mp->b_rptr; ASSERT(iocp->ioc_cmd != DL_IOC_HDR_INFO); switch (iocp->ioc_cmd) { case SIOCSTUNPARAM: case OSIOCSTUNPARAM: ASSERT(ipsq != NULL); /* * Finish socket ioctl passed through to tun. * We should have an IOCTL waiting on this. */ mp1 = ipsq_pending_mp_get(ipsq, &connp); if (ill->ill_isv6) { struct iftun_req *ta; /* * if a source or destination is * being set, try and set the link * local address for the tunnel */ ta = (struct iftun_req *)mp->b_cont-> b_cont->b_rptr; if (ta->ifta_flags & (IFTUN_SRC | IFTUN_DST)) { ipif_set_tun_llink(ill, ta); } } if (mp1 != NULL) { /* * Now copy back the b_next/b_prev used by * mi code for the mi_copy* functions. * See ip_sioctl_tunparam() for the reason. * Also protect against missing b_cont. */ if (mp->b_cont != NULL) { mp->b_cont->b_next = mp1->b_cont->b_next; mp->b_cont->b_prev = mp1->b_cont->b_prev; } inet_freemsg(mp1); ASSERT(connp != NULL); ip_ioctl_finish(CONNP_TO_WQ(connp), mp, iocp->ioc_error, NO_COPYOUT, ipsq); } else { ASSERT(connp == NULL); putnext(q, mp); } break; case SIOCGTUNPARAM: case OSIOCGTUNPARAM: /* * This is really M_IOCDATA from the tunnel driver. * convert back and complete the ioctl. * We should have an IOCTL waiting on this. */ mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); if (mp1) { /* * Now copy back the b_next/b_prev used by * mi code for the mi_copy* functions. * See ip_sioctl_tunparam() for the reason. * Also protect against missing b_cont. */ if (mp->b_cont != NULL) { mp->b_cont->b_next = mp1->b_cont->b_next; mp->b_cont->b_prev = mp1->b_cont->b_prev; } inet_freemsg(mp1); if (iocp->ioc_error == 0) mp->b_datap->db_type = M_IOCDATA; ASSERT(connp != NULL); ip_ioctl_finish(CONNP_TO_WQ(connp), mp, iocp->ioc_error, COPYOUT, NULL); } else { ASSERT(connp == NULL); putnext(q, mp); } break; default: break; } break; case M_IOCNAK: iocp = (struct iocblk *)mp->b_rptr; switch (iocp->ioc_cmd) { int mode; case DL_IOC_HDR_INFO: /* * If this was the first attempt turn of the * fastpath probing. */ mutex_enter(&ill->ill_lock); if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) { ill->ill_dlpi_fastpath_state = IDS_FAILED; mutex_exit(&ill->ill_lock); ill_fastpath_nack(ill); ip1dbg(("ip_rput: DLPI fastpath off on " "interface %s\n", ill->ill_name)); } else { mutex_exit(&ill->ill_lock); } freemsg(mp); break; case SIOCSTUNPARAM: case OSIOCSTUNPARAM: ASSERT(ipsq != NULL); /* * Finish socket ioctl passed through to tun * We should have an IOCTL waiting on this. */ /* FALLTHRU */ case SIOCGTUNPARAM: case OSIOCGTUNPARAM: /* * This is really M_IOCDATA from the tunnel driver. * convert back and complete the ioctl. * We should have an IOCTL waiting on this. */ if (iocp->ioc_cmd == SIOCGTUNPARAM || iocp->ioc_cmd == OSIOCGTUNPARAM) { mp1 = ill_pending_mp_get(ill, &connp, iocp->ioc_id); mode = COPYOUT; ipsq = NULL; } else { mp1 = ipsq_pending_mp_get(ipsq, &connp); mode = NO_COPYOUT; } if (mp1 != NULL) { /* * Now copy back the b_next/b_prev used by * mi code for the mi_copy* functions. * See ip_sioctl_tunparam() for the reason. * Also protect against missing b_cont. */ if (mp->b_cont != NULL) { mp->b_cont->b_next = mp1->b_cont->b_next; mp->b_cont->b_prev = mp1->b_cont->b_prev; } inet_freemsg(mp1); if (iocp->ioc_error == 0) iocp->ioc_error = EINVAL; ASSERT(connp != NULL); ip_ioctl_finish(CONNP_TO_WQ(connp), mp, iocp->ioc_error, mode, ipsq); } else { ASSERT(connp == NULL); putnext(q, mp); } break; default: break; } default: break; } } /* * NOTE : This function does not ire_refrele the ire argument passed in. * * IPQoS notes * IP policy is invoked twice for a forwarded packet, once on the read side * and again on the write side if both, IPP_FWD_IN and IPP_FWD_OUT are * enabled. An additional parameter, in_ill, has been added for this purpose. * Note that in_ill could be NULL when called from ip_rput_forward_multicast * because ip_mroute drops this information. * */ void ip_rput_forward(ire_t *ire, ipha_t *ipha, mblk_t *mp, ill_t *in_ill) { uint32_t old_pkt_len; uint32_t pkt_len; queue_t *q; uint32_t sum; #define rptr ((uchar_t *)ipha) uint32_t max_frag; uint32_t ill_index; ill_t *out_ill; mib2_ipIfStatsEntry_t *mibptr; ip_stack_t *ipst = ((ill_t *)(ire->ire_stq->q_ptr))->ill_ipst; /* Get the ill_index of the incoming ILL */ ill_index = (in_ill != NULL) ? in_ill->ill_phyint->phyint_ifindex : 0; mibptr = (in_ill != NULL) ? in_ill->ill_ip_mib : &ipst->ips_ip_mib; /* Initiate Read side IPPF processing */ if (IPP_ENABLED(IPP_FWD_IN, ipst)) { ip_process(IPP_FWD_IN, &mp, ill_index); if (mp == NULL) { ip2dbg(("ip_rput_forward: pkt dropped/deferred "\ "during IPPF processing\n")); return; } } /* Adjust the checksum to reflect the ttl decrement. */ sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST; ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16)); if (ipha->ipha_ttl-- <= 1) { if (ip_csum_hdr(ipha)) { BUMP_MIB(mibptr, ipIfStatsInCksumErrs); goto drop_pkt; } /* * Note: ire_stq this will be NULL for multicast * datagrams using the long path through arp (the IRE * is not an IRE_CACHE). This should not cause * problems since we don't generate ICMP errors for * multicast packets. */ BUMP_MIB(mibptr, ipIfStatsForwProhibits); q = ire->ire_stq; if (q != NULL) { /* Sent by forwarding path, and router is global zone */ icmp_time_exceeded(q, mp, ICMP_TTL_EXCEEDED, GLOBAL_ZONEID, ipst); } else freemsg(mp); return; } /* * Don't forward if the interface is down */ if (ire->ire_ipif->ipif_ill->ill_ipif_up_count == 0) { BUMP_MIB(mibptr, ipIfStatsInDiscards); ip2dbg(("ip_rput_forward:interface is down\n")); goto drop_pkt; } /* Get the ill_index of the outgoing ILL */ out_ill = ire_to_ill(ire); ill_index = out_ill->ill_phyint->phyint_ifindex; DTRACE_PROBE4(ip4__forwarding__start, ill_t *, in_ill, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); FW_HOOKS(ipst->ips_ip4_forwarding_event, ipst->ips_ipv4firewall_forwarding, in_ill, out_ill, ipha, mp, mp, 0, ipst); DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp); if (mp == NULL) return; old_pkt_len = pkt_len = ntohs(ipha->ipha_length); if (is_system_labeled()) { mblk_t *mp1; if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { BUMP_MIB(mibptr, ipIfStatsForwProhibits); goto drop_pkt; } /* Size may have changed */ mp = mp1; ipha = (ipha_t *)mp->b_rptr; pkt_len = ntohs(ipha->ipha_length); } /* Check if there are options to update */ if (!IS_SIMPLE_IPH(ipha)) { if (ip_csum_hdr(ipha)) { BUMP_MIB(mibptr, ipIfStatsInCksumErrs); goto drop_pkt; } if (ip_rput_forward_options(mp, ipha, ire, ipst)) { BUMP_MIB(mibptr, ipIfStatsForwProhibits); return; } ipha->ipha_hdr_checksum = 0; ipha->ipha_hdr_checksum = ip_csum_hdr(ipha); } max_frag = ire->ire_max_frag; if (pkt_len > max_frag) { /* * It needs fragging on its way out. We haven't * verified the header checksum yet. Since we * are going to put a surely good checksum in the * outgoing header, we have to make sure that it * was good coming in. */ if (ip_csum_hdr(ipha)) { BUMP_MIB(mibptr, ipIfStatsInCksumErrs); goto drop_pkt; } /* Initiate Write side IPPF processing */ if (IPP_ENABLED(IPP_FWD_OUT, ipst)) { ip_process(IPP_FWD_OUT, &mp, ill_index); if (mp == NULL) { ip2dbg(("ip_rput_forward: pkt dropped/deferred"\ " during IPPF processing\n")); return; } } /* * Handle labeled packet resizing. * * If we have added a label, inform ip_wput_frag() of its * effect on the MTU for ICMP messages. */ if (pkt_len > old_pkt_len) { uint32_t secopt_size; secopt_size = pkt_len - old_pkt_len; if (secopt_size < max_frag) max_frag -= secopt_size; } ip_wput_frag(ire, mp, IB_PKT, max_frag, 0, GLOBAL_ZONEID, ipst); ip2dbg(("ip_rput_forward:sent to ip_wput_frag\n")); return; } DTRACE_PROBE4(ip4__physical__out__start, ill_t *, NULL, ill_t *, out_ill, ipha_t *, ipha, mblk_t *, mp); FW_HOOKS(ipst->ips_ip4_physical_out_event, ipst->ips_ipv4firewall_physical_out, NULL, out_ill, ipha, mp, mp, 0, ipst); DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); if (mp == NULL) return; mp->b_prev = (mblk_t *)IPP_FWD_OUT; ip1dbg(("ip_rput_forward: Calling ip_xmit_v4\n")); (void) ip_xmit_v4(mp, ire, NULL, B_FALSE); /* ip_xmit_v4 always consumes the packet */ return; drop_pkt:; ip1dbg(("ip_rput_forward: drop pkt\n")); freemsg(mp); #undef rptr } void ip_rput_forward_multicast(ipaddr_t dst, mblk_t *mp, ipif_t *ipif) { ire_t *ire; ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; ASSERT(!ipif->ipif_isv6); /* * Find an IRE which matches the destination and the outgoing * queue in the cache table. All we need is an IRE_CACHE which * is pointing at ipif->ipif_ill. If it is part of some ill group, * then it is enough to have some IRE_CACHE in the group. */ if (ipif->ipif_flags & IPIF_POINTOPOINT) dst = ipif->ipif_pp_dst_addr; ire = ire_ctable_lookup(dst, 0, 0, ipif, ALL_ZONES, MBLK_GETLABEL(mp), MATCH_IRE_ILL_GROUP | MATCH_IRE_SECATTR, ipst); if (ire == NULL) { /* * Mark this packet to make it be delivered to * ip_rput_forward after the new ire has been * created. */ mp->b_prev = NULL; mp->b_next = mp; ip_newroute_ipif(ipif->ipif_ill->ill_wq, mp, ipif, dst, NULL, 0, GLOBAL_ZONEID, &zero_info); } else { ip_rput_forward(ire, (ipha_t *)mp->b_rptr, mp, NULL); IRE_REFRELE(ire); } } /* Update any source route, record route or timestamp options */ static int ip_rput_forward_options(mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) { ipoptp_t opts; uchar_t *opt; uint8_t optval; uint8_t optlen; ipaddr_t dst; uint32_t ts; ire_t *dst_ire = NULL; ire_t *tmp_ire = NULL; timestruc_t now; ip2dbg(("ip_rput_forward_options\n")); dst = ipha->ipha_dst; for (optval = ipoptp_first(&opts, ipha); optval != IPOPT_EOL; optval = ipoptp_next(&opts)) { ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); opt = opts.ipoptp_cur; optlen = opts.ipoptp_len; ip2dbg(("ip_rput_forward_options: opt %d, len %d\n", optval, opts.ipoptp_len)); switch (optval) { uint32_t off; case IPOPT_SSRR: case IPOPT_LSRR: /* Check if adminstratively disabled */ if (!ipst->ips_ip_forward_src_routed) { if (ire->ire_stq != NULL) { /* * Sent by forwarding path, and router * is global zone */ icmp_unreachable(ire->ire_stq, mp, ICMP_SOURCE_ROUTE_FAILED, GLOBAL_ZONEID, ipst); } else { ip0dbg(("ip_rput_forward_options: " "unable to send unreach\n")); freemsg(mp); } return (-1); } dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (dst_ire == NULL) { /* * Must be partial since ip_rput_options * checked for strict. */ break; } off = opt[IPOPT_OFFSET]; off--; redo_srr: if (optlen < IP_ADDR_LEN || off > optlen - IP_ADDR_LEN) { /* End of source route */ ip1dbg(( "ip_rput_forward_options: end of SR\n")); ire_refrele(dst_ire); break; } bcopy((char *)opt + off, &dst, IP_ADDR_LEN); bcopy(&ire->ire_src_addr, (char *)opt + off, IP_ADDR_LEN); ip1dbg(("ip_rput_forward_options: next hop 0x%x\n", ntohl(dst))); /* * Check if our address is present more than * once as consecutive hops in source route. */ tmp_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (tmp_ire != NULL) { ire_refrele(tmp_ire); off += IP_ADDR_LEN; opt[IPOPT_OFFSET] += IP_ADDR_LEN; goto redo_srr; } ipha->ipha_dst = dst; opt[IPOPT_OFFSET] += IP_ADDR_LEN; ire_refrele(dst_ire); break; case IPOPT_RR: off = opt[IPOPT_OFFSET]; off--; if (optlen < IP_ADDR_LEN || off > optlen - IP_ADDR_LEN) { /* No more room - ignore */ ip1dbg(( "ip_rput_forward_options: end of RR\n")); break; } bcopy(&ire->ire_src_addr, (char *)opt + off, IP_ADDR_LEN); opt[IPOPT_OFFSET] += IP_ADDR_LEN; break; case IPOPT_TS: /* Insert timestamp if there is room */ switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { case IPOPT_TS_TSONLY: off = IPOPT_TS_TIMELEN; break; case IPOPT_TS_PRESPEC: case IPOPT_TS_PRESPEC_RFC791: /* Verify that the address matched */ off = opt[IPOPT_OFFSET] - 1; bcopy((char *)opt + off, &dst, IP_ADDR_LEN); dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (dst_ire == NULL) { /* Not for us */ break; } ire_refrele(dst_ire); /* FALLTHRU */ case IPOPT_TS_TSANDADDR: off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; break; default: /* * ip_*put_options should have already * dropped this packet. */ cmn_err(CE_PANIC, "ip_rput_forward_options: " "unknown IT - bug in ip_rput_options?\n"); return (0); /* Keep "lint" happy */ } if (opt[IPOPT_OFFSET] - 1 + off > optlen) { /* Increase overflow counter */ off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; opt[IPOPT_POS_OV_FLG] = (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | (off << 4)); break; } off = opt[IPOPT_OFFSET] - 1; switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { case IPOPT_TS_PRESPEC: case IPOPT_TS_PRESPEC_RFC791: case IPOPT_TS_TSANDADDR: bcopy(&ire->ire_src_addr, (char *)opt + off, IP_ADDR_LEN); opt[IPOPT_OFFSET] += IP_ADDR_LEN; /* FALLTHRU */ case IPOPT_TS_TSONLY: off = opt[IPOPT_OFFSET] - 1; /* Compute # of milliseconds since midnight */ gethrestime(&now); ts = (now.tv_sec % (24 * 60 * 60)) * 1000 + now.tv_nsec / (NANOSEC / MILLISEC); bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN); opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN; break; } break; } } return (0); } /* * This is called after processing at least one of AH/ESP headers. * * NOTE: the ill corresponding to ipsec_in_ill_index may not be * the actual, physical interface on which the packet was received, * but, when ip_strict_dst_multihoming is set to 1, could be the * interface which had the ipha_dst configured when the packet went * through ip_rput. The ill_index corresponding to the recv_ill * is saved in ipsec_in_rill_index * * NOTE2: The "ire" argument is only used in IPv4 cases. This function * cannot assume "ire" points to valid data for any IPv6 cases. */ void ip_fanout_proto_again(mblk_t *ipsec_mp, ill_t *ill, ill_t *recv_ill, ire_t *ire) { mblk_t *mp; ipaddr_t dst; in6_addr_t *v6dstp; ipha_t *ipha; ip6_t *ip6h; ipsec_in_t *ii; boolean_t ill_need_rele = B_FALSE; boolean_t rill_need_rele = B_FALSE; boolean_t ire_need_rele = B_FALSE; netstack_t *ns; ip_stack_t *ipst; ii = (ipsec_in_t *)ipsec_mp->b_rptr; ASSERT(ii->ipsec_in_ill_index != 0); ns = ii->ipsec_in_ns; ASSERT(ii->ipsec_in_ns != NULL); ipst = ns->netstack_ip; mp = ipsec_mp->b_cont; ASSERT(mp != NULL); if (ill == NULL) { ASSERT(recv_ill == NULL); /* * We need to get the original queue on which ip_rput_local * or ip_rput_data_v6 was called. */ ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index, !ii->ipsec_in_v4, NULL, NULL, NULL, NULL, ipst); ill_need_rele = B_TRUE; if (ii->ipsec_in_ill_index != ii->ipsec_in_rill_index) { recv_ill = ill_lookup_on_ifindex( ii->ipsec_in_rill_index, !ii->ipsec_in_v4, NULL, NULL, NULL, NULL, ipst); rill_need_rele = B_TRUE; } else { recv_ill = ill; } if ((ill == NULL) || (recv_ill == NULL)) { ip0dbg(("ip_fanout_proto_again: interface " "disappeared\n")); if (ill != NULL) ill_refrele(ill); if (recv_ill != NULL) ill_refrele(recv_ill); freemsg(ipsec_mp); return; } } ASSERT(ill != NULL && recv_ill != NULL); if (mp->b_datap->db_type == M_CTL) { /* * AH/ESP is returning the ICMP message after * removing their headers. Fanout again till * it gets to the right protocol. */ if (ii->ipsec_in_v4) { icmph_t *icmph; int iph_hdr_length; int hdr_length; ipha = (ipha_t *)mp->b_rptr; iph_hdr_length = IPH_HDR_LENGTH(ipha); icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; ipha = (ipha_t *)&icmph[1]; hdr_length = IPH_HDR_LENGTH(ipha); /* * icmp_inbound_error_fanout may need to do pullupmsg. * Reset the type to M_DATA. */ mp->b_datap->db_type = M_DATA; icmp_inbound_error_fanout(ill->ill_rq, ill, ipsec_mp, icmph, ipha, iph_hdr_length, hdr_length, B_TRUE, B_FALSE, ill, ii->ipsec_in_zoneid); } else { icmp6_t *icmp6; int hdr_length; ip6h = (ip6_t *)mp->b_rptr; /* Don't call hdr_length_v6() unless you have to. */ if (ip6h->ip6_nxt != IPPROTO_ICMPV6) hdr_length = ip_hdr_length_v6(mp, ip6h); else hdr_length = IPV6_HDR_LEN; icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); /* * icmp_inbound_error_fanout_v6 may need to do * pullupmsg. Reset the type to M_DATA. */ mp->b_datap->db_type = M_DATA; icmp_inbound_error_fanout_v6(ill->ill_rq, ipsec_mp, ip6h, icmp6, ill, B_TRUE, ii->ipsec_in_zoneid); } if (ill_need_rele) ill_refrele(ill); if (rill_need_rele) ill_refrele(recv_ill); return; } if (ii->ipsec_in_v4) { ipha = (ipha_t *)mp->b_rptr; dst = ipha->ipha_dst; if (CLASSD(dst)) { /* * Multicast has to be delivered to all streams. */ dst = INADDR_BROADCAST; } if (ire == NULL) { ire = ire_cache_lookup(dst, ii->ipsec_in_zoneid, MBLK_GETLABEL(mp), ipst); if (ire == NULL) { if (ill_need_rele) ill_refrele(ill); if (rill_need_rele) ill_refrele(recv_ill); ip1dbg(("ip_fanout_proto_again: " "IRE not found")); freemsg(ipsec_mp); return; } ire_need_rele = B_TRUE; } switch (ipha->ipha_protocol) { case IPPROTO_UDP: ip_udp_input(ill->ill_rq, ipsec_mp, ipha, ire, recv_ill); if (ire_need_rele) ire_refrele(ire); break; case IPPROTO_TCP: if (!ire_need_rele) IRE_REFHOLD(ire); mp = ip_tcp_input(mp, ipha, ill, B_TRUE, ire, ipsec_mp, 0, ill->ill_rq, NULL); IRE_REFRELE(ire); if (mp != NULL) squeue_enter_chain(GET_SQUEUE(mp), mp, mp, 1, SQTAG_IP_PROTO_AGAIN); break; case IPPROTO_SCTP: if (!ire_need_rele) IRE_REFHOLD(ire); ip_sctp_input(mp, ipha, ill, B_TRUE, ire, ipsec_mp, 0, ill->ill_rq, dst); break; default: ip_proto_input(ill->ill_rq, ipsec_mp, ipha, ire, recv_ill, 0); if (ire_need_rele) ire_refrele(ire); break; } } else { uint32_t rput_flags = 0; ip6h = (ip6_t *)mp->b_rptr; v6dstp = &ip6h->ip6_dst; /* * XXX Assumes ip_rput_v6 sets ll_multicast only for multicast * address. * * Currently, we don't store that state in the IPSEC_IN * message, and we may need to. */ rput_flags |= (IN6_IS_ADDR_MULTICAST(v6dstp) ? IP6_IN_LLMCAST : 0); ip_rput_data_v6(ill->ill_rq, ill, ipsec_mp, ip6h, rput_flags, NULL, NULL); } if (ill_need_rele) ill_refrele(ill); if (rill_need_rele) ill_refrele(recv_ill); } /* * Call ill_frag_timeout to do garbage collection. ill_frag_timeout * returns 'true' if there are still fragments left on the queue, in * which case we restart the timer. */ void ill_frag_timer(void *arg) { ill_t *ill = (ill_t *)arg; boolean_t frag_pending; ip_stack_t *ipst = ill->ill_ipst; mutex_enter(&ill->ill_lock); ASSERT(!ill->ill_fragtimer_executing); if (ill->ill_state_flags & ILL_CONDEMNED) { ill->ill_frag_timer_id = 0; mutex_exit(&ill->ill_lock); return; } ill->ill_fragtimer_executing = 1; mutex_exit(&ill->ill_lock); frag_pending = ill_frag_timeout(ill, ipst->ips_ip_g_frag_timeout); /* * Restart the timer, if we have fragments pending or if someone * wanted us to be scheduled again. */ mutex_enter(&ill->ill_lock); ill->ill_fragtimer_executing = 0; ill->ill_frag_timer_id = 0; if (frag_pending || ill->ill_fragtimer_needrestart) ill_frag_timer_start(ill); mutex_exit(&ill->ill_lock); } void ill_frag_timer_start(ill_t *ill) { ip_stack_t *ipst = ill->ill_ipst; ASSERT(MUTEX_HELD(&ill->ill_lock)); /* If the ill is closing or opening don't proceed */ if (ill->ill_state_flags & ILL_CONDEMNED) return; if (ill->ill_fragtimer_executing) { /* * ill_frag_timer is currently executing. Just record the * the fact that we want the timer to be restarted. * ill_frag_timer will post a timeout before it returns, * ensuring it will be called again. */ ill->ill_fragtimer_needrestart = 1; return; } if (ill->ill_frag_timer_id == 0) { /* * The timer is neither running nor is the timeout handler * executing. Post a timeout so that ill_frag_timer will be * called */ ill->ill_frag_timer_id = timeout(ill_frag_timer, ill, MSEC_TO_TICK(ipst->ips_ip_g_frag_timo_ms >> 1)); ill->ill_fragtimer_needrestart = 0; } } /* * This routine is needed for loopback when forwarding multicasts. * * IPQoS Notes: * IPPF processing is done in fanout routines. * Policy processing is done only if IPP_lOCAL_IN is enabled. Further, * processing for IPsec packets is done when it comes back in clear. * NOTE : The callers of this function need to do the ire_refrele for the * ire that is being passed in. */ void ip_proto_input(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, ill_t *recv_ill, uint32_t esp_udp_ports) { boolean_t esp_in_udp_packet = (esp_udp_ports != 0); ill_t *ill = (ill_t *)q->q_ptr; uint32_t sum; uint32_t u1; uint32_t u2; int hdr_length; boolean_t mctl_present; mblk_t *first_mp = mp; mblk_t *hada_mp = NULL; ipha_t *inner_ipha; ip_stack_t *ipst; ASSERT(recv_ill != NULL); ipst = recv_ill->ill_ipst; TRACE_1(TR_FAC_IP, TR_IP_RPUT_LOCL_START, "ip_rput_locl_start: q %p", q); ASSERT(ire->ire_ipversion == IPV4_VERSION); ASSERT(ill != NULL); #define rptr ((uchar_t *)ipha) #define iphs ((uint16_t *)ipha) /* * no UDP or TCP packet should come here anymore. */ ASSERT(ipha->ipha_protocol != IPPROTO_TCP && ipha->ipha_protocol != IPPROTO_UDP); EXTRACT_PKT_MP(mp, first_mp, mctl_present); if (mctl_present && ((da_ipsec_t *)first_mp->b_rptr)->da_type == IPHADA_M_CTL) { ASSERT(MBLKL(first_mp) >= sizeof (da_ipsec_t)); /* * It's an IPsec accelerated packet. * Keep a pointer to the data attributes around until * we allocate the ipsec_info_t. */ IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_local: inbound HW accelerated IPsec pkt\n")); hada_mp = first_mp; hada_mp->b_cont = NULL; /* * Since it is accelerated, it comes directly from * the ill and the data attributes is followed by * the packet data. */ ASSERT(mp->b_datap->db_type != M_CTL); first_mp = mp; mctl_present = B_FALSE; } /* * IF M_CTL is not present, then ipsec_in_is_secure * should return B_TRUE. There is a case where loopback * packets has an M_CTL in the front with all the * IPsec options set to IPSEC_PREF_NEVER - which means * ipsec_in_is_secure will return B_FALSE. As loopback * packets never comes here, it is safe to ASSERT the * following. */ ASSERT(!mctl_present || ipsec_in_is_secure(first_mp)); /* * Also, we should never have an mctl_present if this is an * ESP-in-UDP packet. */ ASSERT(!mctl_present || !esp_in_udp_packet); /* u1 is # words of IP options */ u1 = ipha->ipha_version_and_hdr_length - (uchar_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS); /* * Don't verify header checksum if we just removed UDP header or * packet is coming back from AH/ESP. */ if (!esp_in_udp_packet && !mctl_present) { if (u1) { if (!ip_options_cksum(q, ill, mp, ipha, ire, ipst)) { if (hada_mp != NULL) freemsg(hada_mp); return; } } else { /* Check the IP header checksum. */ #define uph ((uint16_t *)ipha) sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] + uph[5] + uph[6] + uph[7] + uph[8] + uph[9]; #undef uph /* finish doing IP checksum */ sum = (sum & 0xFFFF) + (sum >> 16); sum = ~(sum + (sum >> 16)) & 0xFFFF; if (sum && sum != 0xFFFF) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs); goto drop_pkt; } } } /* * Count for SNMP of inbound packets for ire. As ip_proto_input * might be called more than once for secure packets, count only * the first time. */ if (!mctl_present) { UPDATE_IB_PKT_COUNT(ire); ire->ire_last_used_time = lbolt; } /* Check for fragmentation offset. */ u2 = ntohs(ipha->ipha_fragment_offset_and_flags); u1 = u2 & (IPH_MF | IPH_OFFSET); if (u1) { /* * We re-assemble fragments before we do the AH/ESP * processing. Thus, M_CTL should not be present * while we are re-assembling. */ ASSERT(!mctl_present); ASSERT(first_mp == mp); if (!ip_rput_fragment(q, &mp, ipha, NULL, NULL)) { return; } /* * Make sure that first_mp points back to mp as * the mp we came in with could have changed in * ip_rput_fragment(). */ ipha = (ipha_t *)mp->b_rptr; first_mp = mp; } /* * Clear hardware checksumming flag as it is currently only * used by TCP and UDP. */ DB_CKSUMFLAGS(mp) = 0; /* Now we have a complete datagram, destined for this machine. */ u1 = IPH_HDR_LENGTH(ipha); switch (ipha->ipha_protocol) { case IPPROTO_ICMP: { ire_t *ire_zone; ilm_t *ilm; mblk_t *mp1; zoneid_t last_zoneid; if (CLASSD(ipha->ipha_dst) && !IS_LOOPBACK(recv_ill)) { ASSERT(ire->ire_type == IRE_BROADCAST); /* * In the multicast case, applications may have joined * the group from different zones, so we need to deliver * the packet to each of them. Loop through the * multicast memberships structures (ilm) on the receive * ill and send a copy of the packet up each matching * one. However, we don't do this for multicasts sent on * the loopback interface (PHYI_LOOPBACK flag set) as * they must stay in the sender's zone. * * ilm_add_v6() ensures that ilms in the same zone are * contiguous in the ill_ilm list. We use this property * to avoid sending duplicates needed when two * applications in the same zone join the same group on * different logical interfaces: we ignore the ilm if * its zoneid is the same as the last matching one. * In addition, the sending of the packet for * ire_zoneid is delayed until all of the other ilms * have been exhausted. */ last_zoneid = -1; ILM_WALKER_HOLD(recv_ill); for (ilm = recv_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) { if ((ilm->ilm_flags & ILM_DELETED) || ipha->ipha_dst != ilm->ilm_addr || ilm->ilm_zoneid == last_zoneid || ilm->ilm_zoneid == ire->ire_zoneid || ilm->ilm_zoneid == ALL_ZONES || !(ilm->ilm_ipif->ipif_flags & IPIF_UP)) continue; mp1 = ip_copymsg(first_mp); if (mp1 == NULL) continue; icmp_inbound(q, mp1, B_TRUE, ill, 0, sum, mctl_present, B_TRUE, recv_ill, ilm->ilm_zoneid); last_zoneid = ilm->ilm_zoneid; } ILM_WALKER_RELE(recv_ill); } else if (ire->ire_type == IRE_BROADCAST) { /* * In the broadcast case, there may be many zones * which need a copy of the packet delivered to them. * There is one IRE_BROADCAST per broadcast address * and per zone; we walk those using a helper function. * In addition, the sending of the packet for ire is * delayed until all of the other ires have been * processed. */ IRB_REFHOLD(ire->ire_bucket); ire_zone = NULL; while ((ire_zone = ire_get_next_bcast_ire(ire_zone, ire)) != NULL) { mp1 = ip_copymsg(first_mp); if (mp1 == NULL) continue; UPDATE_IB_PKT_COUNT(ire_zone); ire_zone->ire_last_used_time = lbolt; icmp_inbound(q, mp1, B_TRUE, ill, 0, sum, mctl_present, B_TRUE, recv_ill, ire_zone->ire_zoneid); } IRB_REFRELE(ire->ire_bucket); } icmp_inbound(q, first_mp, (ire->ire_type == IRE_BROADCAST), ill, 0, sum, mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, "ip_rput_locl_end: q %p (%S)", q, "icmp"); return; } case IPPROTO_IGMP: /* * If we are not willing to accept IGMP packets in clear, * then check with global policy. */ if (ipst->ips_igmp_accept_clear_messages == 0) { first_mp = ipsec_check_global_policy(first_mp, NULL, ipha, NULL, mctl_present, ipst->ips_netstack); if (first_mp == NULL) return; } if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { freemsg(first_mp); ip1dbg(("ip_proto_input: zone all cannot accept raw")); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return; } if ((mp = igmp_input(q, mp, ill)) == NULL) { /* Bad packet - discarded by igmp_input */ TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, "ip_rput_locl_end: q %p (%S)", q, "igmp"); if (mctl_present) freeb(first_mp); return; } /* * igmp_input() may have returned the pulled up message. * So first_mp and ipha need to be reinitialized. */ ipha = (ipha_t *)mp->b_rptr; if (mctl_present) first_mp->b_cont = mp; else first_mp = mp; if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. connf_head != NULL) { /* No user-level listener for IGMP packets */ goto drop_pkt; } /* deliver to local raw users */ break; case IPPROTO_PIM: /* * If we are not willing to accept PIM packets in clear, * then check with global policy. */ if (ipst->ips_pim_accept_clear_messages == 0) { first_mp = ipsec_check_global_policy(first_mp, NULL, ipha, NULL, mctl_present, ipst->ips_netstack); if (first_mp == NULL) return; } if (is_system_labeled() && !tsol_can_accept_raw(mp, B_TRUE)) { freemsg(first_mp); ip1dbg(("ip_proto_input: zone all cannot accept PIM")); BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); return; } if (pim_input(q, mp, ill) != 0) { /* Bad packet - discarded by pim_input */ TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, "ip_rput_locl_end: q %p (%S)", q, "pim"); if (mctl_present) freeb(first_mp); return; } /* * pim_input() may have pulled up the message so ipha needs to * be reinitialized. */ ipha = (ipha_t *)mp->b_rptr; if (ipst->ips_ipcl_proto_fanout[ipha->ipha_protocol]. connf_head != NULL) { /* No user-level listener for PIM packets */ goto drop_pkt; } /* deliver to local raw users */ break; case IPPROTO_ENCAP: /* * Handle self-encapsulated packets (IP-in-IP where * the inner addresses == the outer addresses). */ hdr_length = IPH_HDR_LENGTH(ipha); if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + sizeof (ipha_t) - mp->b_rptr)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } ipha = (ipha_t *)mp->b_rptr; } inner_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length); /* * Check the sanity of the inner IP header. */ if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } if (inner_ipha->ipha_src == ipha->ipha_src && inner_ipha->ipha_dst == ipha->ipha_dst) { ipsec_in_t *ii; /* * Self-encapsulated tunnel packet. Remove * the outer IP header and fanout again. * We also need to make sure that the inner * header is pulled up until options. */ mp->b_rptr = (uchar_t *)inner_ipha; ipha = inner_ipha; hdr_length = IPH_HDR_LENGTH(ipha); if ((uchar_t *)ipha + hdr_length > mp->b_wptr) { if (!pullupmsg(mp, (uchar_t *)ipha + + hdr_length - mp->b_rptr)) { freemsg(first_mp); return; } ipha = (ipha_t *)mp->b_rptr; } if (hdr_length > sizeof (ipha_t)) { /* We got options on the inner packet. */ ipaddr_t dst = ipha->ipha_dst; if (ip_rput_options(q, mp, ipha, &dst, ipst) == -1) { /* Bad options! */ return; } if (dst != ipha->ipha_dst) { /* * Someone put a source-route in * the inside header of a self- * encapsulated packet. Drop it * with extreme prejudice and let * the sender know. */ icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED, recv_ill->ill_zoneid, ipst); return; } } if (!mctl_present) { ASSERT(first_mp == mp); /* * This means that somebody is sending * Self-encapsualted packets without AH/ESP. * If AH/ESP was present, we would have already * allocated the first_mp. * * Send this packet to find a tunnel endpoint. * if I can't find one, an ICMP * PROTOCOL_UNREACHABLE will get sent. */ goto fanout; } /* * We generally store the ill_index if we need to * do IPsec processing as we lose the ill queue when * we come back. But in this case, we never should * have to store the ill_index here as it should have * been stored previously when we processed the * AH/ESP header in this routine or for non-ipsec * cases, we still have the queue. But for some bad * packets from the wire, we can get to IPsec after * this and we better store the index for that case. */ ill = (ill_t *)q->q_ptr; ii = (ipsec_in_t *)first_mp->b_rptr; ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; if (ii->ipsec_in_decaps) { /* * This packet is self-encapsulated multiple * times. We don't want to recurse infinitely. * To keep it simple, drop the packet. */ BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(first_mp); return; } ii->ipsec_in_decaps = B_TRUE; ip_fanout_proto_again(first_mp, recv_ill, recv_ill, ire); return; } break; case IPPROTO_AH: case IPPROTO_ESP: { ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec; /* * Fast path for AH/ESP. If this is the first time * we are sending a datagram to AH/ESP, allocate * a IPSEC_IN message and prepend it. Otherwise, * just fanout. */ int ipsec_rc; ipsec_in_t *ii; netstack_t *ns = ipst->ips_netstack; IP_STAT(ipst, ipsec_proto_ahesp); if (!mctl_present) { ASSERT(first_mp == mp); first_mp = ipsec_in_alloc(B_TRUE, ns); if (first_mp == NULL) { ip1dbg(("ip_proto_input: IPSEC_IN " "allocation failure.\n")); freemsg(hada_mp); /* okay ifnull */ BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); freemsg(mp); return; } /* * Store the ill_index so that when we come back * from IPsec we ride on the same queue. */ ill = (ill_t *)q->q_ptr; ii = (ipsec_in_t *)first_mp->b_rptr; ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex; first_mp->b_cont = mp; /* * Cache hardware acceleration info. */ if (hada_mp != NULL) { IPSECHW_DEBUG(IPSECHW_PKT, ("ip_rput_local: caching data attr.\n")); ii->ipsec_in_accelerated = B_TRUE; ii->ipsec_in_da = hada_mp; hada_mp = NULL; } } else { ii = (ipsec_in_t *)first_mp->b_rptr; } ii->ipsec_in_esp_udp_ports = esp_udp_ports; if (!ipsec_loaded(ipss)) { ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, ire->ire_zoneid, ipst); return; } ns = ipst->ips_netstack; /* select inbound SA and have IPsec process the pkt */ if (ipha->ipha_protocol == IPPROTO_ESP) { esph_t *esph = ipsec_inbound_esp_sa(first_mp, ns); boolean_t esp_in_udp_sa; if (esph == NULL) return; ASSERT(ii->ipsec_in_esp_sa != NULL); ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != NULL); esp_in_udp_sa = ((ii->ipsec_in_esp_sa->ipsa_flags & IPSA_F_NATT) != 0); /* * The following is a fancy, but quick, way of saying: * ESP-in-UDP SA and Raw ESP packet --> drop * OR * ESP SA and ESP-in-UDP packet --> drop */ if (esp_in_udp_sa != esp_in_udp_packet) { BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); ip_drop_packet(first_mp, B_TRUE, ill, NULL, DROPPER(ns->netstack_ipsec, ipds_esp_no_sa), &ns->netstack_ipsec->ipsec_dropper); return; } ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( first_mp, esph); } else { ah_t *ah = ipsec_inbound_ah_sa(first_mp, ns); if (ah == NULL) return; ASSERT(ii->ipsec_in_ah_sa != NULL); ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( first_mp, ah); } switch (ipsec_rc) { case IPSEC_STATUS_SUCCESS: break; case IPSEC_STATUS_FAILED: BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards); /* FALLTHRU */ case IPSEC_STATUS_PENDING: return; } /* we're done with IPsec processing, send it up */ ip_fanout_proto_again(first_mp, ill, recv_ill, ire); return; } default: break; } if (is_system_labeled() && !tsol_can_accept_raw(mp, B_FALSE)) { ip1dbg(("ip_proto_input: zone %d cannot accept raw IP", ire->ire_zoneid)); goto drop_pkt; } /* * Handle protocols with which IP is less intimate. There * can be more than one stream bound to a particular * protocol. When this is the case, each one gets a copy * of any incoming packets. */ fanout: ip_fanout_proto(q, first_mp, ill, ipha, IP_FF_SEND_ICMP | IP_FF_CKSUM | IP_FF_RAWIP, mctl_present, B_TRUE, recv_ill, ire->ire_zoneid); TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, "ip_rput_locl_end: q %p (%S)", q, "ip_fanout_proto"); return; drop_pkt: freemsg(first_mp); if (hada_mp != NULL) freeb(hada_mp); TRACE_2(TR_FAC_IP, TR_IP_RPUT_LOCL_END, "ip_rput_locl_end: q %p (%S)", q, "droppkt"); #undef rptr #undef iphs } /* * Update any source route, record route or timestamp options. * Check that we are at end of strict source route. * The options have already been checked for sanity in ip_rput_options(). */ static boolean_t ip_rput_local_options(queue_t *q, mblk_t *mp, ipha_t *ipha, ire_t *ire, ip_stack_t *ipst) { ipoptp_t opts; uchar_t *opt; uint8_t optval; uint8_t optlen; ipaddr_t dst; uint32_t ts; ire_t *dst_ire; timestruc_t now; zoneid_t zoneid; ill_t *ill; ASSERT(ire->ire_ipversion == IPV4_VERSION); ip2dbg(("ip_rput_local_options\n")); for (optval = ipoptp_first(&opts, ipha); optval != IPOPT_EOL; optval = ipoptp_next(&opts)) { ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0); opt = opts.ipoptp_cur; optlen = opts.ipoptp_len; ip2dbg(("ip_rput_local_options: opt %d, len %d\n", optval, optlen)); switch (optval) { uint32_t off; case IPOPT_SSRR: case IPOPT_LSRR: off = opt[IPOPT_OFFSET]; off--; if (optlen < IP_ADDR_LEN || off > optlen - IP_ADDR_LEN) { /* End of source route */ ip1dbg(("ip_rput_local_options: end of SR\n")); break; } /* * This will only happen if two consecutive entries * in the source route contains our address or if * it is a packet with a loose source route which * reaches us before consuming the whole source route */ ip1dbg(("ip_rput_local_options: not end of SR\n")); if (optval == IPOPT_SSRR) { goto bad_src_route; } /* * Hack: instead of dropping the packet truncate the * source route to what has been used by filling the * rest with IPOPT_NOP. */ opt[IPOPT_OLEN] = (uint8_t)off; while (off < optlen) { opt[off++] = IPOPT_NOP; } break; case IPOPT_RR: off = opt[IPOPT_OFFSET]; off--; if (optlen < IP_ADDR_LEN || off > optlen - IP_ADDR_LEN) { /* No more room - ignore */ ip1dbg(( "ip_rput_local_options: end of RR\n")); break; } bcopy(&ire->ire_src_addr, (char *)opt + off, IP_ADDR_LEN); opt[IPOPT_OFFSET] += IP_ADDR_LEN; break; case IPOPT_TS: /* Insert timestamp if there is romm */ switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { case IPOPT_TS_TSONLY: off = IPOPT_TS_TIMELEN; break; case IPOPT_TS_PRESPEC: case IPOPT_TS_PRESPEC_RFC791: /* Verify that the address matched */ off = opt[IPOPT_OFFSET] - 1; bcopy((char *)opt + off, &dst, IP_ADDR_LEN); dst_ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); if (dst_ire == NULL) { /* Not for us */ break; } ire_refrele(dst_ire); /* FALLTHRU */ case IPOPT_TS_TSANDADDR: off = IP_ADDR_LEN + IPOPT_TS_TIMELEN; break; default: /* * ip_*put_options should have already * dropped this packet. */ cmn_err(CE_PANIC, "ip_rput_local_options: " "unknown IT - bug in ip_rput_options?\n"); return (B_TRUE); /* Keep "lint" happy */ } if (opt[IPOPT_OFFSET] - 1 + off > optlen) { /* Increase overflow counter */ off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1; opt[IPOPT_POS_OV_FLG] = (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) | (off << 4)); break; } off = opt[IPOPT_OFFSET] - 1; switch (opt[IPOPT_POS_OV_FLG] & 0x0F) { case IPOPT_TS_PRESPEC: case IPOPT_TS_PRESPEC_RFC791: case IPOPT_TS_TSANDADDR: bcopy(&ire->ire_src_addr, (char *)opt + off, IP_A