Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / kernel / net / ip / ip.c
blob88189a7b0c4c1f8da12db3886e251266491b9ca4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 1990 Mentat Inc.
25 * Copyright (c) 2017 OmniTI Computer Consulting, Inc. All rights reserved.
26 * Copyright (c) 2016 by Delphix. All rights reserved.
27 * Copyright (c) 2018 Joyent, Inc. All rights reserved.
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/dlpi.h>
33 #include <sys/stropts.h>
34 #include <sys/sysmacros.h>
35 #include <sys/strsubr.h>
36 #include <sys/strlog.h>
37 #include <sys/strsun.h>
38 #include <sys/zone.h>
39 #define _SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/xti_inet.h>
42 #include <sys/ddi.h>
43 #include <sys/suntpi.h>
44 #include <sys/cmn_err.h>
45 #include <sys/debug.h>
46 #include <sys/kobj.h>
47 #include <sys/modctl.h>
48 #include <sys/atomic.h>
49 #include <sys/policy.h>
50 #include <sys/priv.h>
51 #include <sys/taskq.h>
53 #include <sys/systm.h>
54 #include <sys/param.h>
55 #include <sys/kmem.h>
56 #include <sys/sdt.h>
57 #include <sys/socket.h>
58 #include <sys/vtrace.h>
59 #include <sys/isa_defs.h>
60 #include <sys/mac.h>
61 #include <net/if.h>
62 #include <net/if_arp.h>
63 #include <net/route.h>
64 #include <sys/sockio.h>
65 #include <netinet/in.h>
66 #include <net/if_dl.h>
68 #include <inet/common.h>
69 #include <inet/mi.h>
70 #include <inet/mib2.h>
71 #include <inet/nd.h>
72 #include <inet/arp.h>
73 #include <inet/snmpcom.h>
74 #include <inet/optcom.h>
75 #include <inet/kstatcom.h>
77 #include <netinet/igmp_var.h>
78 #include <netinet/ip6.h>
79 #include <netinet/icmp6.h>
80 #include <netinet/sctp.h>
82 #include <inet/ip.h>
83 #include <inet/ip_impl.h>
84 #include <inet/ip6.h>
85 #include <inet/ip6_asp.h>
86 #include <inet/tcp.h>
87 #include <inet/tcp_impl.h>
88 #include <inet/ip_multi.h>
89 #include <inet/ip_if.h>
90 #include <inet/ip_ire.h>
91 #include <inet/ip_ftable.h>
92 #include <inet/ip_rts.h>
93 #include <inet/ip_ndp.h>
94 #include <inet/ip_listutils.h>
95 #include <netinet/igmp.h>
96 #include <netinet/ip_mroute.h>
97 #include <inet/ipp_common.h>
99 #include <net/pfkeyv2.h>
100 #include <inet/sadb.h>
101 #include <inet/ipsec_impl.h>
102 #include <inet/iptun/iptun_impl.h>
103 #include <inet/ipdrop.h>
104 #include <inet/ip_netinfo.h>
105 #include <inet/ilb_ip.h>
107 #include <sys/ethernet.h>
108 #include <net/if_types.h>
109 #include <sys/cpuvar.h>
111 #include <ipp/ipp.h>
112 #include <ipp/ipp_impl.h>
113 #include <ipp/ipgpc/ipgpc.h>
115 #include <sys/pattr.h>
116 #include <inet/ipclassifier.h>
117 #include <inet/sctp_ip.h>
118 #include <inet/sctp/sctp_impl.h>
119 #include <inet/udp_impl.h>
120 #include <inet/rawip_impl.h>
121 #include <inet/rts_impl.h>
123 #include <sys/squeue_impl.h>
124 #include <inet/ip_arp.h>
126 #include <sys/clock_impl.h> /* For LBOLT_FASTPATH{,64} */
129 * Values for squeue switch:
130 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
131 * IP_SQUEUE_ENTER: SQ_PROCESS
132 * IP_SQUEUE_FILL: SQ_FILL
134 int ip_squeue_enter = IP_SQUEUE_ENTER; /* Setable in /etc/system */
136 int ip_squeue_flag;
139 * Setable in /etc/system
141 int ip_poll_normal_ms = 100;
142 int ip_poll_normal_ticks = 0;
143 int ip_modclose_ackwait_ms = 3000;
146 * It would be nice to have these present only in DEBUG systems, but the
147 * current design of the global symbol checking logic requires them to be
148 * unconditionally present.
150 uint_t ip_thread_data; /* TSD key for debug support */
151 krwlock_t ip_thread_rwlock;
152 list_t ip_thread_list;
155 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
158 struct listptr_s {
159 mblk_t *lp_head; /* pointer to the head of the list */
160 mblk_t *lp_tail; /* pointer to the tail of the list */
163 typedef struct listptr_s listptr_t;
166 * This is used by ip_snmp_get_mib2_ip_route_media and
167 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
169 typedef struct iproutedata_s {
170 uint_t ird_idx;
171 uint_t ird_flags; /* see below */
172 listptr_t ird_route; /* ipRouteEntryTable */
173 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
174 } iproutedata_t;
176 /* Include ire_testhidden and IRE_IF_CLONE routes */
177 #define IRD_REPORT_ALL 0x01
180 * Synchronization notes:
182 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
183 * MT level protection given by STREAMS. IP uses a combination of its own
184 * internal serialization mechanism and standard Solaris locking techniques.
185 * The internal serialization is per phyint. This is used to serialize
186 * plumbing operations, IPMP operations, most set ioctls, etc.
188 * Plumbing is a long sequence of operations involving message
189 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
190 * involved in plumbing operations. A natural model is to serialize these
191 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
192 * parallel without any interference. But various set ioctls on hme0 are best
193 * serialized, along with IPMP operations and processing of DLPI control
194 * messages received from drivers on a per phyint basis. This serialization is
195 * provided by the ipsq_t and primitives operating on this. Details can
196 * be found in ip_if.c above the core primitives operating on ipsq_t.
198 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
199 * Simiarly lookup of an ire by a thread also returns a refheld ire.
200 * In addition ipif's and ill's referenced by the ire are also indirectly
201 * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
202 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
203 * address of an ipif has to go through the ipsq_t. This ensures that only
204 * one such exclusive operation proceeds at any time on the ipif. It then
205 * waits for all refcnts
206 * associated with this ipif to come down to zero. The address is changed
207 * only after the ipif has been quiesced. Then the ipif is brought up again.
208 * More details are described above the comment in ip_sioctl_flags.
210 * Packet processing is based mostly on IREs and are fully multi-threaded
211 * using standard Solaris MT techniques.
213 * There are explicit locks in IP to handle:
214 * - The ip_g_head list maintained by mi_open_link() and friends.
216 * - The reassembly data structures (one lock per hash bucket)
218 * - conn_lock is meant to protect conn_t fields. The fields actually
219 * protected by conn_lock are documented in the conn_t definition.
221 * - ire_lock to protect some of the fields of the ire, IRE tables
222 * (one lock per hash bucket). Refer to ip_ire.c for details.
224 * - ndp_g_lock and ncec_lock for protecting NCEs.
226 * - ill_lock protects fields of the ill and ipif. Details in ip.h
228 * - ill_g_lock: This is a global reader/writer lock. Protects the following
229 * * The AVL tree based global multi list of all ills.
230 * * The linked list of all ipifs of an ill
231 * * The <ipsq-xop> mapping
232 * * <ill-phyint> association
233 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
234 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the
235 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
236 * writer for the actual duration of the insertion/deletion/change.
238 * - ill_lock: This is a per ill mutex.
239 * It protects some members of the ill_t struct; see ip.h for details.
240 * It also protects the <ill-phyint> assoc.
241 * It also protects the list of ipifs hanging off the ill.
243 * - ipsq_lock: This is a per ipsq_t mutex lock.
244 * This protects some members of the ipsq_t struct; see ip.h for details.
245 * It also protects the <ipsq-ipxop> mapping
247 * - ipx_lock: This is a per ipxop_t mutex lock.
248 * This protects some members of the ipxop_t struct; see ip.h for details.
250 * - phyint_lock: This is a per phyint mutex lock. Protects just the
251 * phyint_flags
253 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
254 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
255 * uniqueness check also done atomically.
257 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
258 * group list linked by ill_usesrc_grp_next. It also protects the
259 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
260 * group is being added or deleted. This lock is taken as a reader when
261 * walking the list/group(eg: to get the number of members in a usesrc group).
262 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
263 * field is changing state i.e from NULL to non-NULL or vice-versa. For
264 * example, it is not necessary to take this lock in the initial portion
265 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
266 * operations are executed exclusively and that ensures that the "usesrc
267 * group state" cannot change. The "usesrc group state" change can happen
268 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
270 * Changing <ill-phyint>, <ipsq-xop> assocications:
272 * To change the <ill-phyint> association, the ill_g_lock must be held
273 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
274 * must be held.
276 * To change the <ipsq-xop> association, the ill_g_lock must be held as
277 * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
278 * This is only done when ills are added or removed from IPMP groups.
280 * To add or delete an ipif from the list of ipifs hanging off the ill,
281 * ill_g_lock (writer) and ill_lock must be held and the thread must be
282 * a writer on the associated ipsq.
284 * To add or delete an ill to the system, the ill_g_lock must be held as
285 * writer and the thread must be a writer on the associated ipsq.
287 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
288 * must be a writer on the associated ipsq.
290 * Lock hierarchy
292 * Some lock hierarchy scenarios are listed below.
294 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
295 * ill_g_lock -> ill_lock(s) -> phyint_lock
296 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
297 * ill_g_lock -> ip_addr_avail_lock
298 * conn_lock -> irb_lock -> ill_lock -> ire_lock
299 * ill_g_lock -> ip_g_nd_lock
300 * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
301 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
302 * arl_lock -> ill_lock
303 * ips_ire_dep_lock -> irb_lock
305 * When more than 1 ill lock is needed to be held, all ill lock addresses
306 * are sorted on address and locked starting from highest addressed lock
307 * downward.
309 * Multicast scenarios
310 * ips_ill_g_lock -> ill_mcast_lock
311 * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
312 * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
313 * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
314 * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
315 * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
317 * IPsec scenarios
319 * ipsa_lock -> ill_g_lock -> ill_lock
320 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
322 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
324 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
325 * sq_lock -> conn_lock -> QLOCK(q)
326 * ill_lock -> ft_lock -> fe_lock
328 * Routing/forwarding table locking notes:
330 * Lock acquisition order: Radix tree lock, irb_lock.
331 * Requirements:
332 * i. Walker must not hold any locks during the walker callback.
333 * ii Walker must not see a truncated tree during the walk because of any node
334 * deletion.
335 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
336 * in many places in the code to walk the irb list. Thus even if all the
337 * ires in a bucket have been deleted, we still can't free the radix node
338 * until the ires have actually been inactive'd (freed).
340 * Tree traversal - Need to hold the global tree lock in read mode.
341 * Before dropping the global tree lock, need to either increment the ire_refcnt
342 * to ensure that the radix node can't be deleted.
344 * Tree add - Need to hold the global tree lock in write mode to add a
345 * radix node. To prevent the node from being deleted, increment the
346 * irb_refcnt, after the node is added to the tree. The ire itself is
347 * added later while holding the irb_lock, but not the tree lock.
349 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
350 * All associated ires must be inactive (i.e. freed), and irb_refcnt
351 * must be zero.
353 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
354 * global tree lock (read mode) for traversal.
356 * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
357 * hence we will acquire irb_lock while holding ips_ire_dep_lock.
359 * IPsec notes :
361 * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
362 * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
363 * ip_xmit_attr_t has the
364 * information used by the IPsec code for applying the right level of
365 * protection. The information initialized by IP in the ip_xmit_attr_t
366 * is determined by the per-socket policy or global policy in the system.
367 * For inbound datagrams, the ip_recv_attr_t
368 * starts out with nothing in it. It gets filled
369 * with the right information if it goes through the AH/ESP code, which
370 * happens if the incoming packet is secure. The information initialized
371 * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
372 * the policy requirements needed by per-socket policy or global policy
373 * is met or not.
375 * For fully connected sockets i.e dst, src [addr, port] is known,
376 * conn_policy_cached is set indicating that policy has been cached.
377 * conn_in_enforce_policy may or may not be set depending on whether
378 * there is a global policy match or per-socket policy match.
379 * Policy inheriting happpens in ip_policy_set once the destination is known.
380 * Once the right policy is set on the conn_t, policy cannot change for
381 * this socket. This makes life simpler for TCP (UDP ?) where
382 * re-transmissions go out with the same policy. For symmetry, policy
383 * is cached for fully connected UDP sockets also. Thus if policy is cached,
384 * it also implies that policy is latched i.e policy cannot change
385 * on these sockets. As we have the right policy on the conn, we don't
386 * have to lookup global policy for every outbound and inbound datagram
387 * and thus serving as an optimization. Note that a global policy change
388 * does not affect fully connected sockets if they have policy. If fully
389 * connected sockets did not have any policy associated with it, global
390 * policy change may affect them.
392 * IP Flow control notes:
393 * ---------------------
394 * Non-TCP streams are flow controlled by IP. The way this is accomplished
395 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
396 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
397 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
398 * functions.
400 * Per Tx ring udp flow control:
401 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
402 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
404 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
405 * To achieve best performance, outgoing traffic need to be fanned out among
406 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
407 * traffic out of the NIC and it takes a fanout hint. UDP connections pass
408 * the address of connp as fanout hint to mac_tx(). Under flow controlled
409 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
410 * cookie points to a specific Tx ring that is blocked. The cookie is used to
411 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
412 * point to drain_lists (idl_t's). These drain list will store the blocked UDP
413 * connp's. The drain list is not a single list but a configurable number of
414 * lists.
416 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
417 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
418 * which is equal to 128. This array in turn contains a pointer to idl_t[],
419 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
420 * list will point to the list of connp's that are flow controlled.
422 * --------------- ------- ------- -------
423 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
424 * | --------------- ------- ------- -------
425 * | --------------- ------- ------- -------
426 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
427 * ---------------- | --------------- ------- ------- -------
428 * |idl_tx_list[0]|->| --------------- ------- ------- -------
429 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
430 * | --------------- ------- ------- -------
431 * . . . . .
432 * | --------------- ------- ------- -------
433 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
434 * --------------- ------- ------- -------
435 * --------------- ------- ------- -------
436 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
437 * | --------------- ------- ------- -------
438 * | --------------- ------- ------- -------
439 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
440 * |idl_tx_list[1]|->| --------------- ------- ------- -------
441 * ---------------- | . . . .
442 * | --------------- ------- ------- -------
443 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
444 * --------------- ------- ------- -------
445 * .....
446 * ----------------
447 * |idl_tx_list[n]|-> ...
448 * ----------------
450 * When mac_tx() returns a cookie, the cookie is hashed into an index into
451 * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
452 * to insert the conn onto. conn_drain_insert() asserts flow control for the
453 * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
454 * Further, conn_blocked is set to indicate that the conn is blocked.
456 * GLDv3 calls ill_flow_enable() when flow control is relieved. The cookie
457 * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
458 * is again hashed to locate the appropriate idl_tx_list, which is then
459 * drained via conn_walk_drain(). conn_walk_drain() goes through each conn in
460 * the drain list and calls conn_drain_remove() to clear flow control (via
461 * calling su_txq_full() or clearing QFULL), and remove the conn from the
462 * drain list.
464 * Note that the drain list is not a single list but a (configurable) array of
465 * lists (8 elements by default). Synchronization between drain insertion and
466 * flow control wakeup is handled by using idl_txl->txl_lock, and only
467 * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
469 * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
470 * On the send side, if the packet cannot be sent down to the driver by IP
471 * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
472 * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
473 * the 0'th drain list. When ip_wsrv() runs on the ill_wq because flow
474 * control has been relieved, the blocked conns in the 0'th drain list are
475 * drained as in the non-STREAMS case.
477 * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
478 * is done when the conn is inserted into the drain list (conn_drain_insert())
479 * and cleared when the conn is removed from the it (conn_drain_remove()).
481 * IPQOS notes:
483 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
484 * and IPQoS modules. IPPF includes hooks in IP at different control points
485 * (callout positions) which direct packets to IPQoS modules for policy
486 * processing. Policies, if present, are global.
488 * The callout positions are located in the following paths:
489 * o local_in (packets destined for this host)
490 * o local_out (packets orginating from this host )
491 * o fwd_in (packets forwarded by this m/c - inbound)
492 * o fwd_out (packets forwarded by this m/c - outbound)
493 * Hooks at these callout points can be enabled/disabled using the ndd variable
494 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
495 * By default all the callout positions are enabled.
497 * Outbound (local_out)
498 * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
500 * Inbound (local_in)
501 * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
503 * Forwarding (in and out)
504 * Hooks are placed in ire_recv_forward_v4/v6.
506 * IP Policy Framework processing (IPPF processing)
507 * Policy processing for a packet is initiated by ip_process, which ascertains
508 * that the classifier (ipgpc) is loaded and configured, failing which the
509 * packet resumes normal processing in IP. If the clasifier is present, the
510 * packet is acted upon by one or more IPQoS modules (action instances), per
511 * filters configured in ipgpc and resumes normal IP processing thereafter.
512 * An action instance can drop a packet in course of its processing.
514 * Zones notes:
516 * The partitioning rules for networking are as follows:
517 * 1) Packets coming from a zone must have a source address belonging to that
518 * zone.
519 * 2) Packets coming from a zone can only be sent on a physical interface on
520 * which the zone has an IP address.
521 * 3) Between two zones on the same machine, packet delivery is only allowed if
522 * there's a matching route for the destination and zone in the forwarding
523 * table.
524 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
525 * different zones can bind to the same port with the wildcard address
526 * (INADDR_ANY).
528 * The granularity of interface partitioning is at the logical interface level.
529 * Therefore, every zone has its own IP addresses, and incoming packets can be
530 * attributed to a zone unambiguously. A logical interface is placed into a zone
531 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
532 * structure. Rule (1) is implemented by modifying the source address selection
533 * algorithm so that the list of eligible addresses is filtered based on the
534 * sending process zone.
536 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
537 * across all zones, depending on their type. Here is the break-up:
539 * IRE type Shared/exclusive
540 * -------- ----------------
541 * IRE_BROADCAST Exclusive
542 * IRE_DEFAULT (default routes) Shared (*)
543 * IRE_LOCAL Exclusive (x)
544 * IRE_LOOPBACK Exclusive
545 * IRE_PREFIX (net routes) Shared (*)
546 * IRE_IF_NORESOLVER (interface routes) Exclusive
547 * IRE_IF_RESOLVER (interface routes) Exclusive
548 * IRE_IF_CLONE (interface routes) Exclusive
549 * IRE_HOST (host routes) Shared (*)
551 * (*) A zone can only use a default or off-subnet route if the gateway is
552 * directly reachable from the zone, that is, if the gateway's address matches
553 * one of the zone's logical interfaces.
555 * (x) IRE_LOCAL are handled a bit differently.
556 * When ip_restrict_interzone_loopback is set (the default),
557 * ire_route_recursive restricts loopback using an IRE_LOCAL
558 * between zone to the case when L2 would have conceptually looped the packet
559 * back, i.e. the loopback which is required since neither Ethernet drivers
560 * nor Ethernet hardware loops them back. This is the case when the normal
561 * routes (ignoring IREs with different zoneids) would send out the packet on
562 * the same ill as the ill with which is IRE_LOCAL is associated.
564 * Multiple zones can share a common broadcast address; typically all zones
565 * share the 255.255.255.255 address. Incoming as well as locally originated
566 * broadcast packets must be dispatched to all the zones on the broadcast
567 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
568 * since some zones may not be on the 10.16.72/24 network. To handle this, each
569 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
570 * sent to every zone that has an IRE_BROADCAST entry for the destination
571 * address on the input ill, see ip_input_broadcast().
573 * Applications in different zones can join the same multicast group address.
574 * The same logic applies for multicast as for broadcast. ip_input_multicast
575 * dispatches packets to all zones that have members on the physical interface.
579 * Squeue Fanout flags:
580 * 0: No fanout.
581 * 1: Fanout across all squeues
583 boolean_t ip_squeue_fanout = 0;
586 * Maximum dups allowed per packet.
588 uint_t ip_max_frag_dups = 10;
590 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
591 cred_t *credp, boolean_t isv6);
592 static mblk_t *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
594 static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
595 static void icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
596 static void icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
597 ip_recv_attr_t *);
598 static void icmp_options_update(ipha_t *);
599 static void icmp_param_problem(mblk_t *, uint8_t, ip_recv_attr_t *);
600 static void icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
601 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
602 static void icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
603 ip_recv_attr_t *);
604 static void icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
605 static void icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
606 ip_recv_attr_t *);
608 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
609 char *ip_dot_addr(ipaddr_t, char *);
610 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
611 int ip_close(queue_t *, int);
612 static char *ip_dot_saddr(uchar_t *, char *);
613 static void ip_lrput(queue_t *, mblk_t *);
614 ipaddr_t ip_net_mask(ipaddr_t);
615 char *ip_nv_lookup(nv_t *, int);
616 void ip_rput(queue_t *, mblk_t *);
617 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
618 void *dummy_arg);
619 int ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
620 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
621 mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
622 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
623 ip_stack_t *, boolean_t);
624 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
625 boolean_t);
626 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
627 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
628 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
629 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
630 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
631 ip_stack_t *ipst, boolean_t);
632 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
633 ip_stack_t *ipst, boolean_t);
634 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
635 ip_stack_t *ipst);
636 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
637 ip_stack_t *ipst);
638 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
639 ip_stack_t *ipst);
640 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
641 ip_stack_t *ipst);
642 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
643 ip_stack_t *ipst);
644 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
645 ip_stack_t *ipst);
646 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
647 ip_stack_t *ipst);
648 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
649 ip_stack_t *ipst);
650 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
651 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
652 static int ip_snmp_get2_v4_media(ncec_t *, iproutedata_t *);
653 static int ip_snmp_get2_v6_media(ncec_t *, iproutedata_t *);
654 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
656 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
657 mblk_t *);
659 static void conn_drain_init(ip_stack_t *);
660 static void conn_drain_fini(ip_stack_t *);
661 static void conn_drain(conn_t *connp, boolean_t closing);
663 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
664 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
666 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
667 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
668 static void ip_stack_fini(netstackid_t stackid, void *arg);
670 static int ip_squeue_switch(int);
672 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
673 static void ip_kstat_fini(netstackid_t, kstat_t *);
674 static int ip_kstat_update(kstat_t *kp, int rw);
675 static void *icmp_kstat_init(netstackid_t);
676 static void icmp_kstat_fini(netstackid_t, kstat_t *);
677 static int icmp_kstat_update(kstat_t *kp, int rw);
678 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
679 static void ip_kstat2_fini(netstackid_t, kstat_t *);
681 static void ipobs_init(ip_stack_t *);
682 static void ipobs_fini(ip_stack_t *);
684 static int ip_tp_cpu_update(cpu_setup_t, int, void *);
686 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
688 static long ip_rput_pullups;
689 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
691 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
692 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
694 int ip_debug;
697 * IP tunables related declarations. Definitions are in ip_tunables.c
699 extern mod_prop_info_t ip_propinfo_tbl[];
700 extern int ip_propinfo_count;
703 * Table of IP ioctls encoding the various properties of the ioctl and
704 * indexed based on the last byte of the ioctl command. Occasionally there
705 * is a clash, and there is more than 1 ioctl with the same last byte.
706 * In such a case 1 ioctl is encoded in the ndx table and the remaining
707 * ioctls are encoded in the misc table. An entry in the ndx table is
708 * retrieved by indexing on the last byte of the ioctl command and comparing
709 * the ioctl command with the value in the ndx table. In the event of a
710 * mismatch the misc table is then searched sequentially for the desired
711 * ioctl command.
713 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
715 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
716 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
717 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
718 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
719 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
720 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
721 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
722 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
723 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
724 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
725 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
727 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
728 MISC_CMD, ip_siocaddrt, NULL },
729 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
730 MISC_CMD, ip_siocdelrt, NULL },
732 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
733 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
734 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
735 IF_CMD, ip_sioctl_get_addr, NULL },
737 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
738 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
739 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
740 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
742 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
743 IPI_PRIV | IPI_WR,
744 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
745 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
746 IPI_MODOK | IPI_GET_CMD,
747 IF_CMD, ip_sioctl_get_flags, NULL },
749 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
750 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
752 /* copyin size cannot be coded for SIOCGIFCONF */
753 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
754 MISC_CMD, ip_sioctl_get_ifconf, NULL },
756 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
757 IF_CMD, ip_sioctl_mtu, NULL },
758 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
759 IF_CMD, ip_sioctl_get_mtu, NULL },
760 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
761 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
762 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
763 IF_CMD, ip_sioctl_brdaddr, NULL },
764 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
765 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
766 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
767 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
768 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
769 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
770 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
771 IF_CMD, ip_sioctl_metric, NULL },
772 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
774 /* See 166-168 below for extended SIOC*XARP ioctls */
775 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
776 ARP_CMD, ip_sioctl_arp, NULL },
777 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
778 ARP_CMD, ip_sioctl_arp, NULL },
779 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
780 ARP_CMD, ip_sioctl_arp, NULL },
782 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
783 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
784 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
785 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
786 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
787 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
788 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
789 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
790 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
791 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
792 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
793 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
794 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
795 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
796 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
797 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
798 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
799 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
800 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
801 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
802 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
804 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
805 MISC_CMD, if_unitsel, if_unitsel_restart },
807 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
808 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
809 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
810 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
811 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
812 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
813 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
814 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
815 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
816 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
817 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
818 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
819 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
820 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
821 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
822 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
823 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
824 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
826 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
827 IPI_PRIV | IPI_WR | IPI_MODOK,
828 IF_CMD, ip_sioctl_sifname, NULL },
830 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
831 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
832 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
833 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
834 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
835 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
836 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
837 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
838 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
839 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
840 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
841 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
842 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
844 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
845 MISC_CMD, ip_sioctl_get_ifnum, NULL },
846 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
847 IF_CMD, ip_sioctl_get_muxid, NULL },
848 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
849 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
851 /* Both if and lif variants share same func */
852 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
853 IF_CMD, ip_sioctl_get_lifindex, NULL },
854 /* Both if and lif variants share same func */
855 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
856 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
858 /* copyin size cannot be coded for SIOCGIFCONF */
859 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
860 MISC_CMD, ip_sioctl_get_ifconf, NULL },
861 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
862 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
863 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
864 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
865 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
866 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
867 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
868 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
869 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
870 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
871 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
872 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
873 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
874 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
875 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
876 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
877 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
879 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
880 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
881 ip_sioctl_removeif_restart },
882 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
883 IPI_GET_CMD | IPI_PRIV | IPI_WR,
884 LIF_CMD, ip_sioctl_addif, NULL },
885 #define SIOCLIFADDR_NDX 112
886 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
887 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
888 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
889 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
890 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
891 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
892 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
893 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
894 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
895 IPI_PRIV | IPI_WR,
896 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
897 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
898 IPI_GET_CMD | IPI_MODOK,
899 LIF_CMD, ip_sioctl_get_flags, NULL },
901 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
902 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
904 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
905 ip_sioctl_get_lifconf, NULL },
906 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
907 LIF_CMD, ip_sioctl_mtu, NULL },
908 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
909 LIF_CMD, ip_sioctl_get_mtu, NULL },
910 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
911 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
912 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
913 LIF_CMD, ip_sioctl_brdaddr, NULL },
914 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
915 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
916 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
917 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
918 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
919 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
920 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
921 LIF_CMD, ip_sioctl_metric, NULL },
922 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
923 IPI_PRIV | IPI_WR | IPI_MODOK,
924 LIF_CMD, ip_sioctl_slifname,
925 ip_sioctl_slifname_restart },
927 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
928 MISC_CMD, ip_sioctl_get_lifnum, NULL },
929 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
930 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
931 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
932 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
933 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
934 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
935 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
936 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
937 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
938 LIF_CMD, ip_sioctl_token, NULL },
939 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
940 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
941 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
942 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
943 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
944 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
945 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
946 LIF_CMD, ip_sioctl_lnkinfo, NULL },
948 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
949 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
950 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
951 LIF_CMD, ip_siocdelndp_v6, NULL },
952 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
953 LIF_CMD, ip_siocqueryndp_v6, NULL },
954 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
955 LIF_CMD, ip_siocsetndp_v6, NULL },
956 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
957 MISC_CMD, ip_sioctl_tmyaddr, NULL },
958 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
959 MISC_CMD, ip_sioctl_tonlink, NULL },
960 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
961 MISC_CMD, ip_sioctl_tmysite, NULL },
962 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
963 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
965 /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
966 /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
967 /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
968 /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
969 /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
971 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
973 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
974 LIF_CMD, ip_sioctl_get_binding, NULL },
975 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
976 IPI_PRIV | IPI_WR,
977 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
978 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
979 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
980 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
981 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
983 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
984 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
985 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
986 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
988 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
990 /* These are handled in ip_sioctl_copyin_setup itself */
991 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
992 MISC_CMD, NULL, NULL },
993 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
994 MISC_CMD, NULL, NULL },
995 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
997 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
998 ip_sioctl_get_lifconf, NULL },
1000 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1001 XARP_CMD, ip_sioctl_arp, NULL },
1002 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1003 XARP_CMD, ip_sioctl_arp, NULL },
1004 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1005 XARP_CMD, ip_sioctl_arp, NULL },
1007 /* SIOCPOPSOCKFS is not handled by IP */
1008 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1010 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1011 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1012 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1013 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1014 ip_sioctl_slifzone_restart },
1015 /* 172-174 are SCTP ioctls and not handled by IP */
1016 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1017 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1018 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1019 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1020 IPI_GET_CMD, LIF_CMD,
1021 ip_sioctl_get_lifusesrc, 0 },
1022 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1023 IPI_PRIV | IPI_WR,
1024 LIF_CMD, ip_sioctl_slifusesrc,
1025 NULL },
1026 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1027 ip_sioctl_get_lifsrcof, NULL },
1028 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1029 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1030 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1031 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1032 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1033 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1034 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1035 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1036 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 /* SIOCSENABLESDP is handled by SDP */
1038 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1039 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1040 /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1041 IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1042 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1043 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1044 ip_sioctl_ilb_cmd, NULL },
1045 /* 188 */ { SIOCGETPROP, sizeof (mod_ioc_prop_t), IPI_GET_CMD,
1046 MISC_CMD, ip_sioctl_getsetprop, NULL },
1047 /* 189 */ { SIOCSETPROP, sizeof (mod_ioc_prop_t), IPI_PRIV,
1048 MISC_CMD, ip_sioctl_getsetprop, NULL},
1049 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1050 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1051 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1052 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1053 /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1054 LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1057 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1059 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1060 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1061 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1062 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1063 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1064 { ND_GET, 0, 0, 0, NULL, NULL },
1065 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1066 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1067 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1068 MISC_CMD, mrt_ioctl},
1069 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1070 MISC_CMD, mrt_ioctl},
1071 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1072 MISC_CMD, mrt_ioctl}
1075 int ip_misc_ioctl_count =
1076 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1078 int conn_drain_nthreads; /* Number of drainers reqd. */
1079 /* Settable in /etc/system */
1080 /* Defined in ip_ire.c */
1081 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1082 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1083 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1085 static nv_t ire_nv_arr[] = {
1086 { IRE_BROADCAST, "BROADCAST" },
1087 { IRE_LOCAL, "LOCAL" },
1088 { IRE_LOOPBACK, "LOOPBACK" },
1089 { IRE_DEFAULT, "DEFAULT" },
1090 { IRE_PREFIX, "PREFIX" },
1091 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1092 { IRE_IF_RESOLVER, "IF_RESOLV" },
1093 { IRE_IF_CLONE, "IF_CLONE" },
1094 { IRE_HOST, "HOST" },
1095 { IRE_MULTICAST, "MULTICAST" },
1096 { IRE_NOROUTE, "NOROUTE" },
1097 { 0 }
1100 nv_t *ire_nv_tbl = ire_nv_arr;
1102 /* Simple ICMP IP Header Template */
1103 static ipha_t icmp_ipha = {
1104 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1107 struct module_info ip_mod_info = {
1108 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1109 IP_MOD_LOWAT
1113 * Duplicate static symbols within a module confuses mdb; so we avoid the
1114 * problem by making the symbols here distinct from those in udp.c.
1118 * Entry points for IP as a device and as a module.
1119 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1121 static struct qinit iprinitv4 = {
1122 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL,
1123 &ip_mod_info
1126 struct qinit iprinitv6 = {
1127 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL,
1128 &ip_mod_info
1131 static struct qinit ipwinit = {
1132 (pfi_t)ip_wput_nondata, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1133 &ip_mod_info
1136 static struct qinit iplrinit = {
1137 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL,
1138 &ip_mod_info
1141 static struct qinit iplwinit = {
1142 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL,
1143 &ip_mod_info
1146 /* For AF_INET aka /dev/ip */
1147 struct streamtab ipinfov4 = {
1148 &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1151 /* For AF_INET6 aka /dev/ip6 */
1152 struct streamtab ipinfov6 = {
1153 &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1156 #ifdef DEBUG
1157 boolean_t skip_sctp_cksum = B_FALSE;
1158 #endif
1161 * Generate an ICMP fragmentation needed message.
1162 * When called from ip_output side a minimal ip_recv_attr_t needs to be
1163 * constructed by the caller.
1165 void
1166 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1168 icmph_t icmph;
1169 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1171 mp = icmp_pkt_err_ok(mp, ira);
1172 if (mp == NULL)
1173 return;
1175 bzero(&icmph, sizeof (icmph_t));
1176 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1177 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1178 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1179 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1180 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1182 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1186 * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1187 * If the ICMP message is consumed by IP, i.e., it should not be delivered
1188 * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1189 * Likewise, if the ICMP error is misformed (too short, etc), then it
1190 * returns NULL. The caller uses this to determine whether or not to send
1191 * to raw sockets.
1193 * All error messages are passed to the matching transport stream.
1195 * The following cases are handled by icmp_inbound:
1196 * 1) It needs to send a reply back and possibly delivering it
1197 * to the "interested" upper clients.
1198 * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1199 * 3) It needs to change some values in IP only.
1200 * 4) It needs to change some values in IP and upper layers e.g TCP
1201 * by delivering an error to the upper layers.
1203 * We handle the above three cases in the context of IPsec in the
1204 * following way :
1206 * 1) Send the reply back in the same way as the request came in.
1207 * If it came in encrypted, it goes out encrypted. If it came in
1208 * clear, it goes out in clear. Thus, this will prevent chosen
1209 * plain text attack.
1210 * 2) The client may or may not expect things to come in secure.
1211 * If it comes in secure, the policy constraints are checked
1212 * before delivering it to the upper layers. If it comes in
1213 * clear, ipsec_inbound_accept_clear will decide whether to
1214 * accept this in clear or not. In both the cases, if the returned
1215 * message (IP header + 8 bytes) that caused the icmp message has
1216 * AH/ESP headers, it is sent up to AH/ESP for validation before
1217 * sending up. If there are only 8 bytes of returned message, then
1218 * upper client will not be notified.
1219 * 3) Check with global policy to see whether it matches the constaints.
1220 * But this will be done only if icmp_accept_messages_in_clear is
1221 * zero.
1222 * 4) If we need to change both in IP and ULP, then the decision taken
1223 * while affecting the values in IP and while delivering up to TCP
1224 * should be the same.
1226 * There are two cases.
1228 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1229 * failed), we will not deliver it to the ULP, even though they
1230 * are *willing* to accept in *clear*. This is fine as our global
1231 * disposition to icmp messages asks us reject the datagram.
1233 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1234 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1235 * to deliver it to ULP (policy failed), it can lead to
1236 * consistency problems. The cases known at this time are
1237 * ICMP_DESTINATION_UNREACHABLE messages with following code
1238 * values :
1240 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1241 * and Upper layer rejects. Then the communication will
1242 * come to a stop. This is solved by making similar decisions
1243 * at both levels. Currently, when we are unable to deliver
1244 * to the Upper Layer (due to policy failures) while IP has
1245 * adjusted dce_pmtu, the next outbound datagram would
1246 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1247 * will be with the right level of protection. Thus the right
1248 * value will be communicated even if we are not able to
1249 * communicate when we get from the wire initially. But this
1250 * assumes there would be at least one outbound datagram after
1251 * IP has adjusted its dce_pmtu value. To make things
1252 * simpler, we accept in clear after the validation of
1253 * AH/ESP headers.
1255 * - Other ICMP ERRORS : We may not be able to deliver it to the
1256 * upper layer depending on the level of protection the upper
1257 * layer expects and the disposition in ipsec_inbound_accept_clear().
1258 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1259 * should be accepted in clear when the Upper layer expects secure.
1260 * Thus the communication may get aborted by some bad ICMP
1261 * packets.
1263 mblk_t *
1264 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1266 icmph_t *icmph;
1267 ipha_t *ipha; /* Outer header */
1268 int ip_hdr_length; /* Outer header length */
1269 boolean_t interested;
1270 ipif_t *ipif;
1271 uint32_t ts;
1272 uint32_t *tsp;
1273 timestruc_t now;
1274 ill_t *ill = ira->ira_ill;
1275 ip_stack_t *ipst = ill->ill_ipst;
1276 zoneid_t zoneid = ira->ira_zoneid;
1277 int len_needed;
1278 mblk_t *mp_ret = NULL;
1280 ipha = (ipha_t *)mp->b_rptr;
1282 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1284 ip_hdr_length = ira->ira_ip_hdr_length;
1285 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1286 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1287 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1288 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1289 freemsg(mp);
1290 return (NULL);
1292 /* Last chance to get real. */
1293 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1294 if (ipha == NULL) {
1295 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1296 freemsg(mp);
1297 return (NULL);
1301 /* The IP header will always be a multiple of four bytes */
1302 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1303 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1304 icmph->icmph_code));
1307 * We will set "interested" to "true" if we should pass a copy to
1308 * the transport or if we handle the packet locally.
1310 interested = B_FALSE;
1311 switch (icmph->icmph_type) {
1312 case ICMP_ECHO_REPLY:
1313 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1314 break;
1315 case ICMP_DEST_UNREACHABLE:
1316 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1317 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1318 interested = B_TRUE; /* Pass up to transport */
1319 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1320 break;
1321 case ICMP_SOURCE_QUENCH:
1322 interested = B_TRUE; /* Pass up to transport */
1323 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1324 break;
1325 case ICMP_REDIRECT:
1326 if (!ipst->ips_ip_ignore_redirect)
1327 interested = B_TRUE;
1328 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1329 break;
1330 case ICMP_ECHO_REQUEST:
1332 * Whether to respond to echo requests that come in as IP
1333 * broadcasts or as IP multicast is subject to debate
1334 * (what isn't?). We aim to please, you pick it.
1335 * Default is do it.
1337 if (ira->ira_flags & IRAF_MULTICAST) {
1338 /* multicast: respond based on tunable */
1339 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1340 } else if (ira->ira_flags & IRAF_BROADCAST) {
1341 /* broadcast: respond based on tunable */
1342 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1343 } else {
1344 /* unicast: always respond */
1345 interested = B_TRUE;
1347 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1348 if (!interested) {
1349 /* We never pass these to RAW sockets */
1350 freemsg(mp);
1351 return (NULL);
1354 /* Check db_ref to make sure we can modify the packet. */
1355 if (mp->b_datap->db_ref > 1) {
1356 mblk_t *mp1;
1358 mp1 = copymsg(mp);
1359 freemsg(mp);
1360 if (!mp1) {
1361 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1362 return (NULL);
1364 mp = mp1;
1365 ipha = (ipha_t *)mp->b_rptr;
1366 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1368 icmph->icmph_type = ICMP_ECHO_REPLY;
1369 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1370 icmp_send_reply_v4(mp, ipha, icmph, ira);
1371 return (NULL);
1373 case ICMP_ROUTER_ADVERTISEMENT:
1374 case ICMP_ROUTER_SOLICITATION:
1375 break;
1376 case ICMP_TIME_EXCEEDED:
1377 interested = B_TRUE; /* Pass up to transport */
1378 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1379 break;
1380 case ICMP_PARAM_PROBLEM:
1381 interested = B_TRUE; /* Pass up to transport */
1382 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1383 break;
1384 case ICMP_TIME_STAMP_REQUEST:
1385 /* Response to Time Stamp Requests is local policy. */
1386 if (ipst->ips_ip_g_resp_to_timestamp) {
1387 if (ira->ira_flags & IRAF_MULTIBROADCAST)
1388 interested =
1389 ipst->ips_ip_g_resp_to_timestamp_bcast;
1390 else
1391 interested = B_TRUE;
1393 if (!interested) {
1394 /* We never pass these to RAW sockets */
1395 freemsg(mp);
1396 return (NULL);
1399 /* Make sure we have enough of the packet */
1400 len_needed = ip_hdr_length + ICMPH_SIZE +
1401 3 * sizeof (uint32_t);
1403 if (mp->b_wptr - mp->b_rptr < len_needed) {
1404 ipha = ip_pullup(mp, len_needed, ira);
1405 if (ipha == NULL) {
1406 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1407 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1408 mp, ill);
1409 freemsg(mp);
1410 return (NULL);
1412 /* Refresh following the pullup. */
1413 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1415 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1416 /* Check db_ref to make sure we can modify the packet. */
1417 if (mp->b_datap->db_ref > 1) {
1418 mblk_t *mp1;
1420 mp1 = copymsg(mp);
1421 freemsg(mp);
1422 if (!mp1) {
1423 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1424 return (NULL);
1426 mp = mp1;
1427 ipha = (ipha_t *)mp->b_rptr;
1428 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1430 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1431 tsp = (uint32_t *)&icmph[1];
1432 tsp++; /* Skip past 'originate time' */
1433 /* Compute # of milliseconds since midnight */
1434 gethrestime(&now);
1435 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1436 NSEC2MSEC(now.tv_nsec);
1437 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1438 *tsp++ = htonl(ts); /* Lay in 'send time' */
1439 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1440 icmp_send_reply_v4(mp, ipha, icmph, ira);
1441 return (NULL);
1443 case ICMP_TIME_STAMP_REPLY:
1444 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1445 break;
1446 case ICMP_INFO_REQUEST:
1447 /* Per RFC 1122 3.2.2.7, ignore this. */
1448 case ICMP_INFO_REPLY:
1449 break;
1450 case ICMP_ADDRESS_MASK_REQUEST:
1451 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1452 interested =
1453 ipst->ips_ip_respond_to_address_mask_broadcast;
1454 } else {
1455 interested = B_TRUE;
1457 if (!interested) {
1458 /* We never pass these to RAW sockets */
1459 freemsg(mp);
1460 return (NULL);
1462 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1463 if (mp->b_wptr - mp->b_rptr < len_needed) {
1464 ipha = ip_pullup(mp, len_needed, ira);
1465 if (ipha == NULL) {
1466 BUMP_MIB(ill->ill_ip_mib,
1467 ipIfStatsInTruncatedPkts);
1468 ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1469 ill);
1470 freemsg(mp);
1471 return (NULL);
1473 /* Refresh following the pullup. */
1474 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1476 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1477 /* Check db_ref to make sure we can modify the packet. */
1478 if (mp->b_datap->db_ref > 1) {
1479 mblk_t *mp1;
1481 mp1 = copymsg(mp);
1482 freemsg(mp);
1483 if (!mp1) {
1484 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1485 return (NULL);
1487 mp = mp1;
1488 ipha = (ipha_t *)mp->b_rptr;
1489 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1492 * Need the ipif with the mask be the same as the source
1493 * address of the mask reply. For unicast we have a specific
1494 * ipif. For multicast/broadcast we only handle onlink
1495 * senders, and use the source address to pick an ipif.
1497 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1498 if (ipif == NULL) {
1499 /* Broadcast or multicast */
1500 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1501 if (ipif == NULL) {
1502 freemsg(mp);
1503 return (NULL);
1506 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1507 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1508 ipif_refrele(ipif);
1509 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1510 icmp_send_reply_v4(mp, ipha, icmph, ira);
1511 return (NULL);
1513 case ICMP_ADDRESS_MASK_REPLY:
1514 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1515 break;
1516 default:
1517 interested = B_TRUE; /* Pass up to transport */
1518 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1519 break;
1522 * See if there is an ICMP client to avoid an extra copymsg/freemsg
1523 * if there isn't one.
1525 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1526 /* If there is an ICMP client and we want one too, copy it. */
1528 if (!interested) {
1529 /* Caller will deliver to RAW sockets */
1530 return (mp);
1532 mp_ret = copymsg(mp);
1533 if (mp_ret == NULL) {
1534 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1535 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1537 } else if (!interested) {
1538 /* Neither we nor raw sockets are interested. Drop packet now */
1539 freemsg(mp);
1540 return (NULL);
1544 * ICMP error or redirect packet. Make sure we have enough of
1545 * the header and that db_ref == 1 since we might end up modifying
1546 * the packet.
1548 if (mp->b_cont != NULL) {
1549 if (ip_pullup(mp, -1, ira) == NULL) {
1550 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1551 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1552 mp, ill);
1553 freemsg(mp);
1554 return (mp_ret);
1558 if (mp->b_datap->db_ref > 1) {
1559 mblk_t *mp1;
1561 mp1 = copymsg(mp);
1562 if (mp1 == NULL) {
1563 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1564 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1565 freemsg(mp);
1566 return (mp_ret);
1568 freemsg(mp);
1569 mp = mp1;
1573 * In case mp has changed, verify the message before any further
1574 * processes.
1576 ipha = (ipha_t *)mp->b_rptr;
1577 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1578 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1579 freemsg(mp);
1580 return (mp_ret);
1583 switch (icmph->icmph_type) {
1584 case ICMP_REDIRECT:
1585 icmp_redirect_v4(mp, ipha, icmph, ira);
1586 break;
1587 case ICMP_DEST_UNREACHABLE:
1588 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1589 /* Update DCE and adjust MTU is icmp header if needed */
1590 icmp_inbound_too_big_v4(icmph, ira);
1592 /* FALLTHROUGH */
1593 default:
1594 icmp_inbound_error_fanout_v4(mp, icmph, ira);
1595 break;
1597 return (mp_ret);
1601 * Send an ICMP echo, timestamp or address mask reply.
1602 * The caller has already updated the payload part of the packet.
1603 * We handle the ICMP checksum, IP source address selection and feed
1604 * the packet into ip_output_simple.
1606 static void
1607 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1608 ip_recv_attr_t *ira)
1610 uint_t ip_hdr_length = ira->ira_ip_hdr_length;
1611 ill_t *ill = ira->ira_ill;
1612 ip_stack_t *ipst = ill->ill_ipst;
1613 ip_xmit_attr_t ixas;
1615 /* Send out an ICMP packet */
1616 icmph->icmph_checksum = 0;
1617 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1618 /* Reset time to live. */
1619 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1621 /* Swap source and destination addresses */
1622 ipaddr_t tmp;
1624 tmp = ipha->ipha_src;
1625 ipha->ipha_src = ipha->ipha_dst;
1626 ipha->ipha_dst = tmp;
1628 ipha->ipha_ident = 0;
1629 if (!IS_SIMPLE_IPH(ipha))
1630 icmp_options_update(ipha);
1632 bzero(&ixas, sizeof (ixas));
1633 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1634 ixas.ixa_zoneid = ira->ira_zoneid;
1635 ixas.ixa_cred = kcred;
1636 ixas.ixa_cpid = NOPID;
1637 ixas.ixa_ifindex = 0;
1638 ixas.ixa_ipst = ipst;
1639 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1641 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1643 * This packet should go out the same way as it
1644 * came in i.e in clear, independent of the IPsec policy
1645 * for transmitting packets.
1647 ixas.ixa_flags |= IXAF_NO_IPSEC;
1648 } else {
1649 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1650 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1651 /* Note: mp already consumed and ip_drop_packet done */
1652 return;
1655 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1657 * Not one or our addresses (IRE_LOCALs), thus we let
1658 * ip_output_simple pick the source.
1660 ipha->ipha_src = INADDR_ANY;
1661 ixas.ixa_flags |= IXAF_SET_SOURCE;
1663 /* Should we send with DF and use dce_pmtu? */
1664 if (ipst->ips_ipv4_icmp_return_pmtu) {
1665 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1666 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1669 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1671 (void) ip_output_simple(mp, &ixas);
1672 ixa_cleanup(&ixas);
1676 * Verify the ICMP messages for either for ICMP error or redirect packet.
1677 * The caller should have fully pulled up the message. If it's a redirect
1678 * packet, only basic checks on IP header will be done; otherwise, verify
1679 * the packet by looking at the included ULP header.
1681 * Called before icmp_inbound_error_fanout_v4 is called.
1683 static boolean_t
1684 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1686 ill_t *ill = ira->ira_ill;
1687 int hdr_length;
1688 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1689 conn_t *connp;
1690 ipha_t *ipha; /* Inner IP header */
1692 ipha = (ipha_t *)&icmph[1];
1693 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1694 goto truncated;
1696 hdr_length = IPH_HDR_LENGTH(ipha);
1698 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1699 goto discard_pkt;
1701 if (hdr_length < sizeof (ipha_t))
1702 goto truncated;
1704 if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1705 goto truncated;
1708 * Stop here for ICMP_REDIRECT.
1710 if (icmph->icmph_type == ICMP_REDIRECT)
1711 return (B_TRUE);
1714 * ICMP errors only.
1716 switch (ipha->ipha_protocol) {
1717 case IPPROTO_UDP:
1719 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1720 * transport header.
1722 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1723 mp->b_wptr)
1724 goto truncated;
1725 break;
1726 case IPPROTO_TCP: {
1727 tcpha_t *tcpha;
1730 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1731 * transport header.
1733 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1734 mp->b_wptr)
1735 goto truncated;
1737 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1738 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1739 ipst);
1740 if (connp == NULL)
1741 goto discard_pkt;
1743 if ((connp->conn_verifyicmp != NULL) &&
1744 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1745 CONN_DEC_REF(connp);
1746 goto discard_pkt;
1748 CONN_DEC_REF(connp);
1749 break;
1751 case IPPROTO_SCTP:
1753 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1754 * transport header.
1756 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1757 mp->b_wptr)
1758 goto truncated;
1759 break;
1760 case IPPROTO_ESP:
1761 case IPPROTO_AH:
1762 break;
1763 case IPPROTO_ENCAP:
1764 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1765 mp->b_wptr)
1766 goto truncated;
1767 break;
1768 default:
1769 break;
1772 return (B_TRUE);
1774 discard_pkt:
1775 /* Bogus ICMP error. */
1776 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1777 return (B_FALSE);
1779 truncated:
1780 /* We pulled up everthing already. Must be truncated */
1781 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1782 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1783 return (B_FALSE);
1786 /* Table from RFC 1191 */
1787 static int icmp_frag_size_table[] =
1788 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1791 * Process received ICMP Packet too big.
1792 * Just handles the DCE create/update, including using the above table of
1793 * PMTU guesses. The caller is responsible for validating the packet before
1794 * passing it in and also to fanout the ICMP error to any matching transport
1795 * conns. Assumes the message has been fully pulled up and verified.
1797 * Before getting here, the caller has called icmp_inbound_verify_v4()
1798 * that should have verified with ULP to prevent undoing the changes we're
1799 * going to make to DCE. For example, TCP might have verified that the packet
1800 * which generated error is in the send window.
1802 * In some cases modified this MTU in the ICMP header packet; the caller
1803 * should pass to the matching ULP after this returns.
1805 static void
1806 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1808 dce_t *dce;
1809 int old_mtu;
1810 int mtu, orig_mtu;
1811 ipaddr_t dst;
1812 boolean_t disable_pmtud;
1813 ill_t *ill = ira->ira_ill;
1814 ip_stack_t *ipst = ill->ill_ipst;
1815 uint_t hdr_length;
1816 ipha_t *ipha;
1818 /* Caller already pulled up everything. */
1819 ipha = (ipha_t *)&icmph[1];
1820 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1821 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1822 ASSERT(ill != NULL);
1824 hdr_length = IPH_HDR_LENGTH(ipha);
1827 * We handle path MTU for source routed packets since the DCE
1828 * is looked up using the final destination.
1830 dst = ip_get_dst(ipha);
1832 dce = dce_lookup_and_add_v4(dst, ipst);
1833 if (dce == NULL) {
1834 /* Couldn't add a unique one - ENOMEM */
1835 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1836 ntohl(dst)));
1837 return;
1840 /* Check for MTU discovery advice as described in RFC 1191 */
1841 mtu = ntohs(icmph->icmph_du_mtu);
1842 orig_mtu = mtu;
1843 disable_pmtud = B_FALSE;
1845 mutex_enter(&dce->dce_lock);
1846 if (dce->dce_flags & DCEF_PMTU)
1847 old_mtu = dce->dce_pmtu;
1848 else
1849 old_mtu = ill->ill_mtu;
1851 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1852 uint32_t length;
1853 int i;
1856 * Use the table from RFC 1191 to figure out
1857 * the next "plateau" based on the length in
1858 * the original IP packet.
1860 length = ntohs(ipha->ipha_length);
1861 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1862 uint32_t, length);
1863 if (old_mtu <= length &&
1864 old_mtu >= length - hdr_length) {
1866 * Handle broken BSD 4.2 systems that
1867 * return the wrong ipha_length in ICMP
1868 * errors.
1870 ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1871 length, old_mtu));
1872 length -= hdr_length;
1874 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1875 if (length > icmp_frag_size_table[i])
1876 break;
1878 if (i == A_CNT(icmp_frag_size_table)) {
1879 /* Smaller than IP_MIN_MTU! */
1880 ip1dbg(("Too big for packet size %d\n",
1881 length));
1882 disable_pmtud = B_TRUE;
1883 mtu = ipst->ips_ip_pmtu_min;
1884 } else {
1885 mtu = icmp_frag_size_table[i];
1886 ip1dbg(("Calculated mtu %d, packet size %d, "
1887 "before %d\n", mtu, length, old_mtu));
1888 if (mtu < ipst->ips_ip_pmtu_min) {
1889 mtu = ipst->ips_ip_pmtu_min;
1890 disable_pmtud = B_TRUE;
1894 if (disable_pmtud)
1895 dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1896 else
1897 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1899 dce->dce_pmtu = MIN(old_mtu, mtu);
1900 /* Prepare to send the new max frag size for the ULP. */
1901 icmph->icmph_du_zero = 0;
1902 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu);
1903 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1904 dce, int, orig_mtu, int, mtu);
1906 /* We now have a PMTU for sure */
1907 dce->dce_flags |= DCEF_PMTU;
1908 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1909 mutex_exit(&dce->dce_lock);
1911 * After dropping the lock the new value is visible to everyone.
1912 * Then we bump the generation number so any cached values reinspect
1913 * the dce_t.
1915 dce_increment_generation(dce);
1916 dce_refrele(dce);
1920 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1921 * calls this function.
1923 static mblk_t *
1924 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1926 int length;
1928 ASSERT(mp->b_datap->db_type == M_DATA);
1930 /* icmp_inbound_v4 has already pulled up the whole error packet */
1931 ASSERT(mp->b_cont == NULL);
1934 * The length that we want to overlay is the inner header
1935 * and what follows it.
1937 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
1940 * Overlay the inner header and whatever follows it over the
1941 * outer header.
1943 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
1945 /* Adjust for what we removed */
1946 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
1947 return (mp);
1951 * Try to pass the ICMP message upstream in case the ULP cares.
1953 * If the packet that caused the ICMP error is secure, we send
1954 * it to AH/ESP to make sure that the attached packet has a
1955 * valid association. ipha in the code below points to the
1956 * IP header of the packet that caused the error.
1958 * For IPsec cases, we let the next-layer-up (which has access to
1959 * cached policy on the conn_t, or can query the SPD directly)
1960 * subtract out any IPsec overhead if they must. We therefore make no
1961 * adjustments here for IPsec overhead.
1963 * IFN could have been generated locally or by some router.
1965 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
1966 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
1967 * This happens because IP adjusted its value of MTU on an
1968 * earlier IFN message and could not tell the upper layer,
1969 * the new adjusted value of MTU e.g. Packet was encrypted
1970 * or there was not enough information to fanout to upper
1971 * layers. Thus on the next outbound datagram, ire_send_wire
1972 * generates the IFN, where IPsec processing has *not* been
1973 * done.
1975 * Note that we retain ixa_fragsize across IPsec thus once
1976 * we have picking ixa_fragsize and entered ipsec_out_process we do
1977 * no change the fragsize even if the path MTU changes before
1978 * we reach ip_output_post_ipsec.
1980 * In the local case, IRAF_LOOPBACK will be set indicating
1981 * that IFN was generated locally.
1983 * ROUTER : IFN could be secure or non-secure.
1985 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
1986 * packet in error has AH/ESP headers to validate the AH/ESP
1987 * headers. AH/ESP will verify whether there is a valid SA or
1988 * not and send it back. We will fanout again if we have more
1989 * data in the packet.
1991 * If the packet in error does not have AH/ESP, we handle it
1992 * like any other case.
1994 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it
1995 * up to AH/ESP for validation. AH/ESP will verify whether there is a
1996 * valid SA or not and send it back. We will fanout again if
1997 * we have more data in the packet.
1999 * If the packet in error does not have AH/ESP, we handle it
2000 * like any other case.
2002 * The caller must have called icmp_inbound_verify_v4.
2004 static void
2005 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2007 uint16_t *up; /* Pointer to ports in ULP header */
2008 uint32_t ports; /* reversed ports for fanout */
2009 ipha_t ripha; /* With reversed addresses */
2010 ipha_t *ipha; /* Inner IP header */
2011 uint_t hdr_length; /* Inner IP header length */
2012 tcpha_t *tcpha;
2013 conn_t *connp;
2014 ill_t *ill = ira->ira_ill;
2015 ip_stack_t *ipst = ill->ill_ipst;
2016 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2017 ill_t *rill = ira->ira_rill;
2019 /* Caller already pulled up everything. */
2020 ipha = (ipha_t *)&icmph[1];
2021 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2022 ASSERT(mp->b_cont == NULL);
2024 hdr_length = IPH_HDR_LENGTH(ipha);
2025 ira->ira_protocol = ipha->ipha_protocol;
2028 * We need a separate IP header with the source and destination
2029 * addresses reversed to do fanout/classification because the ipha in
2030 * the ICMP error is in the form we sent it out.
2032 ripha.ipha_src = ipha->ipha_dst;
2033 ripha.ipha_dst = ipha->ipha_src;
2034 ripha.ipha_protocol = ipha->ipha_protocol;
2035 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2037 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2038 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2039 ntohl(ipha->ipha_dst),
2040 icmph->icmph_type, icmph->icmph_code));
2042 switch (ipha->ipha_protocol) {
2043 case IPPROTO_UDP:
2044 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2046 /* Attempt to find a client stream based on port. */
2047 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2048 ntohs(up[0]), ntohs(up[1])));
2050 /* Note that we send error to all matches. */
2051 ira->ira_flags |= IRAF_ICMP_ERROR;
2052 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2053 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2054 return;
2056 case IPPROTO_TCP:
2058 * Find a TCP client stream for this packet.
2059 * Note that we do a reverse lookup since the header is
2060 * in the form we sent it out.
2062 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2063 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2064 ipst);
2065 if (connp == NULL)
2066 goto discard_pkt;
2068 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2069 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2070 mp = ipsec_check_inbound_policy(mp, connp,
2071 ipha, NULL, ira);
2072 if (mp == NULL) {
2073 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2074 /* Note that mp is NULL */
2075 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2076 CONN_DEC_REF(connp);
2077 return;
2081 ira->ira_flags |= IRAF_ICMP_ERROR;
2082 ira->ira_ill = ira->ira_rill = NULL;
2083 if (IPCL_IS_TCP(connp)) {
2084 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2085 connp->conn_recvicmp, connp, ira, SQ_FILL,
2086 SQTAG_TCP_INPUT_ICMP_ERR);
2087 } else {
2088 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2089 (connp->conn_recv)(connp, mp, NULL, ira);
2090 CONN_DEC_REF(connp);
2092 ira->ira_ill = ill;
2093 ira->ira_rill = rill;
2094 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2095 return;
2097 case IPPROTO_SCTP:
2098 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2099 /* Find a SCTP client stream for this packet. */
2100 ((uint16_t *)&ports)[0] = up[1];
2101 ((uint16_t *)&ports)[1] = up[0];
2103 ira->ira_flags |= IRAF_ICMP_ERROR;
2104 ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2105 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2106 return;
2108 case IPPROTO_ESP:
2109 case IPPROTO_AH:
2110 if (!ipsec_loaded(ipss)) {
2111 ip_proto_not_sup(mp, ira);
2112 return;
2115 if (ipha->ipha_protocol == IPPROTO_ESP)
2116 mp = ipsecesp_icmp_error(mp, ira);
2117 else
2118 mp = ipsecah_icmp_error(mp, ira);
2119 if (mp == NULL)
2120 return;
2122 /* Just in case ipsec didn't preserve the NULL b_cont */
2123 if (mp->b_cont != NULL) {
2124 if (!pullupmsg(mp, -1))
2125 goto discard_pkt;
2129 * Note that ira_pktlen and ira_ip_hdr_length are no longer
2130 * correct, but we don't use them any more here.
2132 * If succesful, the mp has been modified to not include
2133 * the ESP/AH header so we can fanout to the ULP's icmp
2134 * error handler.
2136 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2137 goto truncated;
2139 /* Verify the modified message before any further processes. */
2140 ipha = (ipha_t *)mp->b_rptr;
2141 hdr_length = IPH_HDR_LENGTH(ipha);
2142 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2143 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2144 freemsg(mp);
2145 return;
2148 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2149 return;
2151 case IPPROTO_ENCAP: {
2152 /* Look for self-encapsulated packets that caused an error */
2153 ipha_t *in_ipha;
2156 * Caller has verified that length has to be
2157 * at least the size of IP header.
2159 ASSERT(hdr_length >= sizeof (ipha_t));
2161 * Check the sanity of the inner IP header like
2162 * we did for the outer header.
2164 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2165 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2166 goto discard_pkt;
2168 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2169 goto discard_pkt;
2171 /* Check for Self-encapsulated tunnels */
2172 if (in_ipha->ipha_src == ipha->ipha_src &&
2173 in_ipha->ipha_dst == ipha->ipha_dst) {
2175 mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2176 in_ipha);
2177 if (mp == NULL)
2178 goto discard_pkt;
2181 * Just in case self_encap didn't preserve the NULL
2182 * b_cont
2184 if (mp->b_cont != NULL) {
2185 if (!pullupmsg(mp, -1))
2186 goto discard_pkt;
2189 * Note that ira_pktlen and ira_ip_hdr_length are no
2190 * longer correct, but we don't use them any more here.
2192 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2193 goto truncated;
2196 * Verify the modified message before any further
2197 * processes.
2199 ipha = (ipha_t *)mp->b_rptr;
2200 hdr_length = IPH_HDR_LENGTH(ipha);
2201 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2202 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2203 freemsg(mp);
2204 return;
2208 * The packet in error is self-encapsualted.
2209 * And we are finding it further encapsulated
2210 * which we could not have possibly generated.
2212 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2213 goto discard_pkt;
2215 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2216 return;
2218 /* No self-encapsulated */
2220 /* FALLTHROUGH */
2221 case IPPROTO_IPV6:
2222 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2223 &ripha.ipha_dst, ipst)) != NULL) {
2224 ira->ira_flags |= IRAF_ICMP_ERROR;
2225 connp->conn_recvicmp(connp, mp, NULL, ira);
2226 CONN_DEC_REF(connp);
2227 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2228 return;
2231 * No IP tunnel is interested, fallthrough and see
2232 * if a raw socket will want it.
2234 /* FALLTHROUGH */
2235 default:
2236 ira->ira_flags |= IRAF_ICMP_ERROR;
2237 ip_fanout_proto_v4(mp, &ripha, ira);
2238 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2239 return;
2241 /* NOTREACHED */
2242 discard_pkt:
2243 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2244 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2245 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2246 freemsg(mp);
2247 return;
2249 truncated:
2250 /* We pulled up everthing already. Must be truncated */
2251 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2252 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2253 freemsg(mp);
2257 * Common IP options parser.
2259 * Setup routine: fill in *optp with options-parsing state, then
2260 * tail-call ipoptp_next to return the first option.
2262 uint8_t
2263 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2265 uint32_t totallen; /* total length of all options */
2267 totallen = ipha->ipha_version_and_hdr_length -
2268 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2269 totallen <<= 2;
2270 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2271 optp->ipoptp_end = optp->ipoptp_next + totallen;
2272 optp->ipoptp_flags = 0;
2273 return (ipoptp_next(optp));
2276 /* Like above but without an ipha_t */
2277 uint8_t
2278 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2280 optp->ipoptp_next = opt;
2281 optp->ipoptp_end = optp->ipoptp_next + totallen;
2282 optp->ipoptp_flags = 0;
2283 return (ipoptp_next(optp));
2287 * Common IP options parser: extract next option.
2289 uint8_t
2290 ipoptp_next(ipoptp_t *optp)
2292 uint8_t *end = optp->ipoptp_end;
2293 uint8_t *cur = optp->ipoptp_next;
2294 uint8_t opt, len, pointer;
2297 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2298 * has been corrupted.
2300 ASSERT(cur <= end);
2302 if (cur == end)
2303 return (IPOPT_EOL);
2305 opt = cur[IPOPT_OPTVAL];
2308 * Skip any NOP options.
2310 while (opt == IPOPT_NOP) {
2311 cur++;
2312 if (cur == end)
2313 return (IPOPT_EOL);
2314 opt = cur[IPOPT_OPTVAL];
2317 if (opt == IPOPT_EOL)
2318 return (IPOPT_EOL);
2321 * Option requiring a length.
2323 if ((cur + 1) >= end) {
2324 optp->ipoptp_flags |= IPOPTP_ERROR;
2325 return (IPOPT_EOL);
2327 len = cur[IPOPT_OLEN];
2328 if (len < 2) {
2329 optp->ipoptp_flags |= IPOPTP_ERROR;
2330 return (IPOPT_EOL);
2332 optp->ipoptp_cur = cur;
2333 optp->ipoptp_len = len;
2334 optp->ipoptp_next = cur + len;
2335 if (cur + len > end) {
2336 optp->ipoptp_flags |= IPOPTP_ERROR;
2337 return (IPOPT_EOL);
2341 * For the options which require a pointer field, make sure
2342 * its there, and make sure it points to either something
2343 * inside this option, or the end of the option.
2345 switch (opt) {
2346 case IPOPT_RR:
2347 case IPOPT_TS:
2348 case IPOPT_LSRR:
2349 case IPOPT_SSRR:
2350 if (len <= IPOPT_OFFSET) {
2351 optp->ipoptp_flags |= IPOPTP_ERROR;
2352 return (opt);
2354 pointer = cur[IPOPT_OFFSET];
2355 if (pointer - 1 > len) {
2356 optp->ipoptp_flags |= IPOPTP_ERROR;
2357 return (opt);
2359 break;
2363 * Sanity check the pointer field based on the type of the
2364 * option.
2366 switch (opt) {
2367 case IPOPT_RR:
2368 case IPOPT_SSRR:
2369 case IPOPT_LSRR:
2370 if (pointer < IPOPT_MINOFF_SR)
2371 optp->ipoptp_flags |= IPOPTP_ERROR;
2372 break;
2373 case IPOPT_TS:
2374 if (pointer < IPOPT_MINOFF_IT)
2375 optp->ipoptp_flags |= IPOPTP_ERROR;
2377 * Note that the Internet Timestamp option also
2378 * contains two four bit fields (the Overflow field,
2379 * and the Flag field), which follow the pointer
2380 * field. We don't need to check that these fields
2381 * fall within the length of the option because this
2382 * was implicitely done above. We've checked that the
2383 * pointer value is at least IPOPT_MINOFF_IT, and that
2384 * it falls within the option. Since IPOPT_MINOFF_IT >
2385 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2387 ASSERT(len > IPOPT_POS_OV_FLG);
2388 break;
2391 return (opt);
2395 * Use the outgoing IP header to create an IP_OPTIONS option the way
2396 * it was passed down from the application.
2398 * This is compatible with BSD in that it returns
2399 * the reverse source route with the final destination
2400 * as the last entry. The first 4 bytes of the option
2401 * will contain the final destination.
2404 ip_opt_get_user(conn_t *connp, uchar_t *buf)
2406 ipoptp_t opts;
2407 uchar_t *opt;
2408 uint8_t optval;
2409 uint8_t optlen;
2410 uint32_t len = 0;
2411 uchar_t *buf1 = buf;
2412 uint32_t totallen;
2413 ipaddr_t dst;
2414 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
2416 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2417 return (0);
2419 totallen = ipp->ipp_ipv4_options_len;
2420 if (totallen & 0x3)
2421 return (0);
2423 buf += IP_ADDR_LEN; /* Leave room for final destination */
2424 len += IP_ADDR_LEN;
2425 bzero(buf1, IP_ADDR_LEN);
2427 dst = connp->conn_faddr_v4;
2429 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2430 optval != IPOPT_EOL;
2431 optval = ipoptp_next(&opts)) {
2432 int off;
2434 opt = opts.ipoptp_cur;
2435 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2436 break;
2438 optlen = opts.ipoptp_len;
2440 switch (optval) {
2441 case IPOPT_SSRR:
2442 case IPOPT_LSRR:
2445 * Insert destination as the first entry in the source
2446 * route and move down the entries on step.
2447 * The last entry gets placed at buf1.
2449 buf[IPOPT_OPTVAL] = optval;
2450 buf[IPOPT_OLEN] = optlen;
2451 buf[IPOPT_OFFSET] = optlen;
2453 off = optlen - IP_ADDR_LEN;
2454 if (off < 0) {
2455 /* No entries in source route */
2456 break;
2458 /* Last entry in source route if not already set */
2459 if (dst == INADDR_ANY)
2460 bcopy(opt + off, buf1, IP_ADDR_LEN);
2461 off -= IP_ADDR_LEN;
2463 while (off > 0) {
2464 bcopy(opt + off,
2465 buf + off + IP_ADDR_LEN,
2466 IP_ADDR_LEN);
2467 off -= IP_ADDR_LEN;
2469 /* ipha_dst into first slot */
2470 bcopy(&dst, buf + off + IP_ADDR_LEN,
2471 IP_ADDR_LEN);
2472 buf += optlen;
2473 len += optlen;
2474 break;
2476 default:
2477 bcopy(opt, buf, optlen);
2478 buf += optlen;
2479 len += optlen;
2480 break;
2483 done:
2484 /* Pad the resulting options */
2485 while (len & 0x3) {
2486 *buf++ = IPOPT_EOL;
2487 len++;
2489 return (len);
2493 * Update any record route or timestamp options to include this host.
2494 * Reverse any source route option.
2495 * This routine assumes that the options are well formed i.e. that they
2496 * have already been checked.
2498 static void
2499 icmp_options_update(ipha_t *ipha)
2501 ipoptp_t opts;
2502 uchar_t *opt;
2503 uint8_t optval;
2504 ipaddr_t src; /* Our local address */
2505 ipaddr_t dst;
2507 ip2dbg(("icmp_options_update\n"));
2508 src = ipha->ipha_src;
2509 dst = ipha->ipha_dst;
2511 for (optval = ipoptp_first(&opts, ipha);
2512 optval != IPOPT_EOL;
2513 optval = ipoptp_next(&opts)) {
2514 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2515 opt = opts.ipoptp_cur;
2516 ip2dbg(("icmp_options_update: opt %d, len %d\n",
2517 optval, opts.ipoptp_len));
2518 switch (optval) {
2519 int off1, off2;
2520 case IPOPT_SSRR:
2521 case IPOPT_LSRR:
2523 * Reverse the source route. The first entry
2524 * should be the next to last one in the current
2525 * source route (the last entry is our address).
2526 * The last entry should be the final destination.
2528 off1 = IPOPT_MINOFF_SR - 1;
2529 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2530 if (off2 < 0) {
2531 /* No entries in source route */
2532 ip1dbg((
2533 "icmp_options_update: bad src route\n"));
2534 break;
2536 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2537 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2538 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2539 off2 -= IP_ADDR_LEN;
2541 while (off1 < off2) {
2542 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2543 bcopy((char *)opt + off2, (char *)opt + off1,
2544 IP_ADDR_LEN);
2545 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2546 off1 += IP_ADDR_LEN;
2547 off2 -= IP_ADDR_LEN;
2549 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2550 break;
2556 * Process received ICMP Redirect messages.
2557 * Assumes the caller has verified that the headers are in the pulled up mblk.
2558 * Consumes mp.
2560 static void
2561 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2563 ire_t *ire, *nire;
2564 ire_t *prev_ire;
2565 ipaddr_t src, dst, gateway;
2566 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2567 ipha_t *inner_ipha; /* Inner IP header */
2569 /* Caller already pulled up everything. */
2570 inner_ipha = (ipha_t *)&icmph[1];
2571 src = ipha->ipha_src;
2572 dst = inner_ipha->ipha_dst;
2573 gateway = icmph->icmph_rd_gateway;
2574 /* Make sure the new gateway is reachable somehow. */
2575 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2576 ALL_ZONES, MATCH_IRE_TYPE, 0, ipst, NULL);
2578 * Make sure we had a route for the dest in question and that
2579 * that route was pointing to the old gateway (the source of the
2580 * redirect packet.)
2581 * We do longest match and then compare ire_gateway_addr below.
2583 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2584 MATCH_IRE_DSTONLY, 0, ipst, NULL);
2586 * Check that
2587 * the redirect was not from ourselves
2588 * the new gateway and the old gateway are directly reachable
2590 if (prev_ire == NULL || ire == NULL ||
2591 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2592 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2593 !(ire->ire_type & IRE_IF_ALL) ||
2594 prev_ire->ire_gateway_addr != src) {
2595 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2596 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2597 freemsg(mp);
2598 if (ire != NULL)
2599 ire_refrele(ire);
2600 if (prev_ire != NULL)
2601 ire_refrele(prev_ire);
2602 return;
2605 ire_refrele(prev_ire);
2606 ire_refrele(ire);
2609 * TODO: more precise handling for cases 0, 2, 3, the latter two
2610 * require TOS routing
2612 switch (icmph->icmph_code) {
2613 case 0:
2614 case 1:
2615 /* TODO: TOS specificity for cases 2 and 3 */
2616 case 2:
2617 case 3:
2618 break;
2619 default:
2620 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2621 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2622 freemsg(mp);
2623 return;
2626 * Create a Route Association. This will allow us to remember that
2627 * someone we believe told us to use the particular gateway.
2629 ire = ire_create(
2630 (uchar_t *)&dst, /* dest addr */
2631 (uchar_t *)&ip_g_all_ones, /* mask */
2632 (uchar_t *)&gateway, /* gateway addr */
2633 IRE_HOST,
2634 NULL, /* ill */
2635 ALL_ZONES,
2636 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2637 ipst);
2639 if (ire == NULL) {
2640 freemsg(mp);
2641 return;
2643 nire = ire_add(ire);
2644 /* Check if it was a duplicate entry */
2645 if (nire != NULL && nire != ire) {
2646 ASSERT(nire->ire_identical_ref > 1);
2647 ire_delete(nire);
2648 ire_refrele(nire);
2649 nire = NULL;
2651 ire = nire;
2652 if (ire != NULL) {
2653 ire_refrele(ire); /* Held in ire_add */
2655 /* tell routing sockets that we received a redirect */
2656 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2657 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2658 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2662 * Delete any existing IRE_HOST type redirect ires for this destination.
2663 * This together with the added IRE has the effect of
2664 * modifying an existing redirect.
2666 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2667 ALL_ZONES, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2668 if (prev_ire != NULL) {
2669 if (prev_ire ->ire_flags & RTF_DYNAMIC)
2670 ire_delete(prev_ire);
2671 ire_refrele(prev_ire);
2674 freemsg(mp);
2678 * Generate an ICMP parameter problem message.
2679 * When called from ip_output side a minimal ip_recv_attr_t needs to be
2680 * constructed by the caller.
2682 static void
2683 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2685 icmph_t icmph;
2686 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2688 mp = icmp_pkt_err_ok(mp, ira);
2689 if (mp == NULL)
2690 return;
2692 bzero(&icmph, sizeof (icmph_t));
2693 icmph.icmph_type = ICMP_PARAM_PROBLEM;
2694 icmph.icmph_pp_ptr = ptr;
2695 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2696 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2700 * Build and ship an IPv4 ICMP message using the packet data in mp, and
2701 * the ICMP header pointed to by "stuff". (May be called as writer.)
2702 * Note: assumes that icmp_pkt_err_ok has been called to verify that
2703 * an icmp error packet can be sent.
2704 * Assigns an appropriate source address to the packet. If ipha_dst is
2705 * one of our addresses use it for source. Otherwise let ip_output_simple
2706 * pick the source address.
2708 static void
2709 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2711 ipaddr_t dst;
2712 icmph_t *icmph;
2713 ipha_t *ipha;
2714 uint_t len_needed;
2715 size_t msg_len;
2716 mblk_t *mp1;
2717 ipaddr_t src;
2718 ire_t *ire;
2719 ip_xmit_attr_t ixas;
2720 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2722 ipha = (ipha_t *)mp->b_rptr;
2724 bzero(&ixas, sizeof (ixas));
2725 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2726 ixas.ixa_zoneid = ira->ira_zoneid;
2727 ixas.ixa_ifindex = 0;
2728 ixas.ixa_ipst = ipst;
2729 ixas.ixa_cred = kcred;
2730 ixas.ixa_cpid = NOPID;
2731 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2733 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2735 * Apply IPsec based on how IPsec was applied to
2736 * the packet that had the error.
2738 * If it was an outbound packet that caused the ICMP
2739 * error, then the caller will have setup the IRA
2740 * appropriately.
2742 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2743 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2744 /* Note: mp already consumed and ip_drop_packet done */
2745 return;
2747 } else {
2749 * This is in clear. The icmp message we are building
2750 * here should go out in clear, independent of our policy.
2752 ixas.ixa_flags |= IXAF_NO_IPSEC;
2755 /* Remember our eventual destination */
2756 dst = ipha->ipha_src;
2759 * If the packet was for one of our unicast addresses, make
2760 * sure we respond with that as the source. Otherwise
2761 * have ip_output_simple pick the source address.
2763 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2764 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid,
2765 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2766 if (ire != NULL) {
2767 ire_refrele(ire);
2768 src = ipha->ipha_dst;
2769 } else {
2770 src = INADDR_ANY;
2771 ixas.ixa_flags |= IXAF_SET_SOURCE;
2775 * Check if we can send back more then 8 bytes in addition to
2776 * the IP header. We try to send 64 bytes of data and the internal
2777 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2779 len_needed = IPH_HDR_LENGTH(ipha);
2780 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2781 ipha->ipha_protocol == IPPROTO_IPV6) {
2782 if (!pullupmsg(mp, -1)) {
2783 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2784 ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2785 freemsg(mp);
2786 return;
2788 ipha = (ipha_t *)mp->b_rptr;
2790 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2791 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2792 len_needed));
2793 } else {
2794 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2796 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2797 len_needed += ip_hdr_length_v6(mp, ip6h);
2800 len_needed += ipst->ips_ip_icmp_return;
2801 msg_len = msgdsize(mp);
2802 if (msg_len > len_needed) {
2803 (void) adjmsg(mp, len_needed - msg_len);
2804 msg_len = len_needed;
2806 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2807 if (mp1 == NULL) {
2808 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2809 freemsg(mp);
2810 return;
2812 mp1->b_cont = mp;
2813 mp = mp1;
2816 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2817 * node generates be accepted in peace by all on-host destinations.
2818 * If we do NOT assume that all on-host destinations trust
2819 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2820 * (Look for IXAF_TRUSTED_ICMP).
2822 ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2824 ipha = (ipha_t *)mp->b_rptr;
2825 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2826 *ipha = icmp_ipha;
2827 ipha->ipha_src = src;
2828 ipha->ipha_dst = dst;
2829 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2830 msg_len += sizeof (icmp_ipha) + len;
2831 if (msg_len > IP_MAXPACKET) {
2832 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2833 msg_len = IP_MAXPACKET;
2835 ipha->ipha_length = htons((uint16_t)msg_len);
2836 icmph = (icmph_t *)&ipha[1];
2837 bcopy(stuff, icmph, len);
2838 icmph->icmph_checksum = 0;
2839 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2840 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2842 (void) ip_output_simple(mp, &ixas);
2843 ixa_cleanup(&ixas);
2847 * Determine if an ICMP error packet can be sent given the rate limit.
2848 * The limit consists of an average frequency (icmp_pkt_err_interval measured
2849 * in milliseconds) and a burst size. Burst size number of packets can
2850 * be sent arbitrarely closely spaced.
2851 * The state is tracked using two variables to implement an approximate
2852 * token bucket filter:
2853 * icmp_pkt_err_last - lbolt value when the last burst started
2854 * icmp_pkt_err_sent - number of packets sent in current burst
2856 boolean_t
2857 icmp_err_rate_limit(ip_stack_t *ipst)
2859 clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2860 uint_t refilled; /* Number of packets refilled in tbf since last */
2861 /* Guard against changes by loading into local variable */
2862 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2864 if (err_interval == 0)
2865 return (B_FALSE);
2867 if (ipst->ips_icmp_pkt_err_last > now) {
2868 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2869 ipst->ips_icmp_pkt_err_last = 0;
2870 ipst->ips_icmp_pkt_err_sent = 0;
2873 * If we are in a burst update the token bucket filter.
2874 * Update the "last" time to be close to "now" but make sure
2875 * we don't loose precision.
2877 if (ipst->ips_icmp_pkt_err_sent != 0) {
2878 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2879 if (refilled > ipst->ips_icmp_pkt_err_sent) {
2880 ipst->ips_icmp_pkt_err_sent = 0;
2881 } else {
2882 ipst->ips_icmp_pkt_err_sent -= refilled;
2883 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2886 if (ipst->ips_icmp_pkt_err_sent == 0) {
2887 /* Start of new burst */
2888 ipst->ips_icmp_pkt_err_last = now;
2890 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2891 ipst->ips_icmp_pkt_err_sent++;
2892 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2893 ipst->ips_icmp_pkt_err_sent));
2894 return (B_FALSE);
2896 ip1dbg(("icmp_err_rate_limit: dropped\n"));
2897 return (B_TRUE);
2901 * Check if it is ok to send an IPv4 ICMP error packet in
2902 * response to the IPv4 packet in mp.
2903 * Free the message and return null if no
2904 * ICMP error packet should be sent.
2906 static mblk_t *
2907 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2909 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2910 icmph_t *icmph;
2911 ipha_t *ipha;
2912 uint_t len_needed;
2914 if (!mp)
2915 return (NULL);
2916 ipha = (ipha_t *)mp->b_rptr;
2917 if (ip_csum_hdr(ipha)) {
2918 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2919 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2920 freemsg(mp);
2921 return (NULL);
2923 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2924 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2925 CLASSD(ipha->ipha_dst) ||
2926 CLASSD(ipha->ipha_src) ||
2927 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2928 /* Note: only errors to the fragment with offset 0 */
2929 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2930 freemsg(mp);
2931 return (NULL);
2933 if (ipha->ipha_protocol == IPPROTO_ICMP) {
2935 * Check the ICMP type. RFC 1122 sez: don't send ICMP
2936 * errors in response to any ICMP errors.
2938 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
2939 if (mp->b_wptr - mp->b_rptr < len_needed) {
2940 if (!pullupmsg(mp, len_needed)) {
2941 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
2942 freemsg(mp);
2943 return (NULL);
2945 ipha = (ipha_t *)mp->b_rptr;
2947 icmph = (icmph_t *)
2948 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
2949 switch (icmph->icmph_type) {
2950 case ICMP_DEST_UNREACHABLE:
2951 case ICMP_SOURCE_QUENCH:
2952 case ICMP_TIME_EXCEEDED:
2953 case ICMP_PARAM_PROBLEM:
2954 case ICMP_REDIRECT:
2955 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2956 freemsg(mp);
2957 return (NULL);
2958 default:
2959 break;
2962 if (icmp_err_rate_limit(ipst)) {
2964 * Only send ICMP error packets every so often.
2965 * This should be done on a per port/source basis,
2966 * but for now this will suffice.
2968 freemsg(mp);
2969 return (NULL);
2971 return (mp);
2975 * Called when a packet was sent out the same link that it arrived on.
2976 * Check if it is ok to send a redirect and then send it.
2978 void
2979 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
2980 ip_recv_attr_t *ira)
2982 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2983 ipaddr_t src, nhop;
2984 mblk_t *mp1;
2985 ire_t *nhop_ire;
2988 * Check the source address to see if it originated
2989 * on the same logical subnet it is going back out on.
2990 * If so, we should be able to send it a redirect.
2991 * Avoid sending a redirect if the destination
2992 * is directly connected (i.e., we matched an IRE_ONLINK),
2993 * or if the packet was source routed out this interface.
2995 * We avoid sending a redirect if the
2996 * destination is directly connected
2997 * because it is possible that multiple
2998 * IP subnets may have been configured on
2999 * the link, and the source may not
3000 * be on the same subnet as ip destination,
3001 * even though they are on the same
3002 * physical link.
3004 if ((ire->ire_type & IRE_ONLINK) ||
3005 ip_source_routed(ipha, ipst))
3006 return;
3008 nhop_ire = ire_nexthop(ire);
3009 if (nhop_ire == NULL)
3010 return;
3012 nhop = nhop_ire->ire_addr;
3014 if (nhop_ire->ire_type & IRE_IF_CLONE) {
3015 ire_t *ire2;
3017 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3018 mutex_enter(&nhop_ire->ire_lock);
3019 ire2 = nhop_ire->ire_dep_parent;
3020 if (ire2 != NULL)
3021 ire_refhold(ire2);
3022 mutex_exit(&nhop_ire->ire_lock);
3023 ire_refrele(nhop_ire);
3024 nhop_ire = ire2;
3026 if (nhop_ire == NULL)
3027 return;
3029 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3031 src = ipha->ipha_src;
3034 * We look at the interface ire for the nexthop,
3035 * to see if ipha_src is in the same subnet
3036 * as the nexthop.
3038 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3040 * The source is directly connected.
3042 mp1 = copymsg(mp);
3043 if (mp1 != NULL) {
3044 icmp_send_redirect(mp1, nhop, ira);
3047 ire_refrele(nhop_ire);
3051 * Generate an ICMP redirect message.
3053 static void
3054 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3056 icmph_t icmph;
3057 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3059 mp = icmp_pkt_err_ok(mp, ira);
3060 if (mp == NULL)
3061 return;
3063 bzero(&icmph, sizeof (icmph_t));
3064 icmph.icmph_type = ICMP_REDIRECT;
3065 icmph.icmph_code = 1;
3066 icmph.icmph_rd_gateway = gateway;
3067 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3068 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3072 * Generate an ICMP time exceeded message.
3074 void
3075 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3077 icmph_t icmph;
3078 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3080 mp = icmp_pkt_err_ok(mp, ira);
3081 if (mp == NULL)
3082 return;
3084 bzero(&icmph, sizeof (icmph_t));
3085 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3086 icmph.icmph_code = code;
3087 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3088 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3092 * Generate an ICMP unreachable message.
3093 * When called from ip_output side a minimal ip_recv_attr_t needs to be
3094 * constructed by the caller.
3096 void
3097 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3099 icmph_t icmph;
3100 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3102 mp = icmp_pkt_err_ok(mp, ira);
3103 if (mp == NULL)
3104 return;
3106 bzero(&icmph, sizeof (icmph_t));
3107 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3108 icmph.icmph_code = code;
3109 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3110 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3114 * Latch in the IPsec state for a stream based the policy in the listener
3115 * and the actions in the ip_recv_attr_t.
3116 * Called directly from TCP and SCTP.
3118 boolean_t
3119 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3121 ASSERT(lconnp->conn_policy != NULL);
3122 ASSERT(connp->conn_policy == NULL);
3124 IPPH_REFHOLD(lconnp->conn_policy);
3125 connp->conn_policy = lconnp->conn_policy;
3127 if (ira->ira_ipsec_action != NULL) {
3128 if (connp->conn_latch == NULL) {
3129 connp->conn_latch = iplatch_create();
3130 if (connp->conn_latch == NULL)
3131 return (B_FALSE);
3133 ipsec_latch_inbound(connp, ira);
3135 return (B_TRUE);
3139 * Verify whether or not the IP address is a valid local address.
3140 * Could be a unicast, including one for a down interface.
3141 * If allow_mcbc then a multicast or broadcast address is also
3142 * acceptable.
3144 * In the case of a broadcast/multicast address, however, the
3145 * upper protocol is expected to reset the src address
3146 * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3147 * no packets are emitted with broadcast/multicast address as
3148 * source address (that violates hosts requirements RFC 1122)
3149 * The addresses valid for bind are:
3150 * (1) - INADDR_ANY (0)
3151 * (2) - IP address of an UP interface
3152 * (3) - IP address of a DOWN interface
3153 * (4) - valid local IP broadcast addresses. In this case
3154 * the conn will only receive packets destined to
3155 * the specified broadcast address.
3156 * (5) - a multicast address. In this case
3157 * the conn will only receive packets destined to
3158 * the specified multicast address. Note: the
3159 * application still has to issue an
3160 * IP_ADD_MEMBERSHIP socket option.
3162 * In all the above cases, the bound address must be valid in the current zone.
3163 * When the address is loopback, multicast or broadcast, there might be many
3164 * matching IREs so bind has to look up based on the zone.
3166 ip_laddr_t
3167 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3168 ip_stack_t *ipst, boolean_t allow_mcbc)
3170 ire_t *src_ire;
3172 ASSERT(src_addr != INADDR_ANY);
3174 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3175 NULL, zoneid, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3178 * If an address other than in6addr_any is requested,
3179 * we verify that it is a valid address for bind
3180 * Note: Following code is in if-else-if form for
3181 * readability compared to a condition check.
3183 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3185 * (2) Bind to address of local UP interface
3187 ire_refrele(src_ire);
3188 return (IPVL_UNICAST_UP);
3189 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3191 * (4) Bind to broadcast address
3193 ire_refrele(src_ire);
3194 if (allow_mcbc)
3195 return (IPVL_BCAST);
3196 else
3197 return (IPVL_BAD);
3198 } else if (CLASSD(src_addr)) {
3199 /* (5) bind to multicast address. */
3200 if (src_ire != NULL)
3201 ire_refrele(src_ire);
3203 if (allow_mcbc)
3204 return (IPVL_MCAST);
3205 else
3206 return (IPVL_BAD);
3207 } else {
3208 ipif_t *ipif;
3211 * (3) Bind to address of local DOWN interface?
3212 * (ipif_lookup_addr() looks up all interfaces
3213 * but we do not get here for UP interfaces
3214 * - case (2) above)
3216 if (src_ire != NULL)
3217 ire_refrele(src_ire);
3219 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3220 if (ipif == NULL)
3221 return (IPVL_BAD);
3223 /* Not a useful source? */
3224 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3225 ipif_refrele(ipif);
3226 return (IPVL_BAD);
3228 ipif_refrele(ipif);
3229 return (IPVL_UNICAST_DOWN);
3234 * Insert in the bind fanout for IPv4 and IPv6.
3235 * The caller should already have used ip_laddr_verify_v*() before calling
3236 * this.
3239 ip_laddr_fanout_insert(conn_t *connp)
3241 int error;
3244 * Allow setting new policies. For example, disconnects result
3245 * in us being called. As we would have set conn_policy_cached
3246 * to B_TRUE before, we should set it to B_FALSE, so that policy
3247 * can change after the disconnect.
3249 connp->conn_policy_cached = B_FALSE;
3251 return (ipcl_bind_insert(connp));
3255 * Verify that both the source and destination addresses are valid. If
3256 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3257 * i.e. have no route to it. Protocols like TCP want to verify destination
3258 * reachability, while tunnels do not.
3260 * Determine the route, the interface, and (optionally) the source address
3261 * to use to reach a given destination.
3262 * Note that we allow connect to broadcast and multicast addresses when
3263 * IPDF_ALLOW_MCBC is set.
3264 * first_hop and dst_addr are normally the same, but if source routing
3265 * they will differ; in that case the first_hop is what we'll use for the
3266 * routing lookup but the dce checks will be done on dst_addr,
3268 * If uinfo is set, then we fill in the best available information
3269 * we have for the destination. This is based on (in priority order) any
3270 * metrics and path MTU stored in a dce_t, route metrics, and finally the
3271 * ill_mtu/ill_mc_mtu.
3274 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3275 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags)
3277 ire_t *ire = NULL;
3278 int error = 0;
3279 ipaddr_t setsrc; /* RTF_SETSRC */
3280 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */
3281 ip_stack_t *ipst = ixa->ixa_ipst;
3282 dce_t *dce;
3283 uint_t pmtu;
3284 uint_t generation;
3285 nce_t *nce;
3286 ill_t *ill = NULL;
3288 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3291 * We never send to zero; the ULPs map it to the loopback address.
3292 * We can't allow it since we use zero to mean unitialized in some
3293 * places.
3295 ASSERT(dst_addr != INADDR_ANY);
3297 setsrc = INADDR_ANY;
3299 * Select a route; For IPMP interfaces, we would only select
3300 * a "hidden" route (i.e., going through a specific under_ill)
3301 * if ixa_ifindex has been specified.
3303 ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3304 &generation, &setsrc, &error);
3305 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */
3306 if (error != 0)
3307 goto bad_addr;
3310 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3311 * If IPDF_VERIFY_DST is set, the destination must be reachable;
3312 * Otherwise the destination needn't be reachable.
3314 * If we match on a reject or black hole, then we've got a
3315 * local failure. May as well fail out the connect() attempt,
3316 * since it's never going to succeed.
3318 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3320 * If we're verifying destination reachability, we always want
3321 * to complain here.
3323 * If we're not verifying destination reachability but the
3324 * destination has a route, we still want to fail on the
3325 * temporary address and broadcast address tests.
3327 * In both cases do we let the code continue so some reasonable
3328 * information is returned to the caller. That enables the
3329 * caller to use (and even cache) the IRE. conn_ip_ouput will
3330 * use the generation mismatch path to check for the unreachable
3331 * case thereby avoiding any specific check in the main path.
3333 ASSERT(generation == IRE_GENERATION_VERIFY);
3334 if (flags & IPDF_VERIFY_DST) {
3336 * Set errno but continue to set up ixa_ire to be
3337 * the RTF_REJECT|RTF_BLACKHOLE IRE.
3338 * That allows callers to use ip_output to get an
3339 * ICMP error back.
3341 if (!(ire->ire_type & IRE_HOST))
3342 error = ENETUNREACH;
3343 else
3344 error = EHOSTUNREACH;
3348 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3349 !(flags & IPDF_ALLOW_MCBC)) {
3350 ire_refrele(ire);
3351 ire = ire_reject(ipst, B_FALSE);
3352 generation = IRE_GENERATION_VERIFY;
3353 error = ENETUNREACH;
3356 /* Cache things */
3357 if (ixa->ixa_ire != NULL)
3358 ire_refrele_notr(ixa->ixa_ire);
3359 #ifdef DEBUG
3360 ire_refhold_notr(ire);
3361 ire_refrele(ire);
3362 #endif
3363 ixa->ixa_ire = ire;
3364 ixa->ixa_ire_generation = generation;
3367 * Ensure that ixa_dce is always set any time that ixa_ire is set,
3368 * since some callers will send a packet to conn_ip_output() even if
3369 * there's an error.
3371 if (flags & IPDF_UNIQUE_DCE) {
3372 /* Fallback to the default dce if allocation fails */
3373 dce = dce_lookup_and_add_v4(dst_addr, ipst);
3374 if (dce != NULL)
3375 generation = dce->dce_generation;
3376 else
3377 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3378 } else {
3379 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3381 ASSERT(dce != NULL);
3382 if (ixa->ixa_dce != NULL)
3383 dce_refrele_notr(ixa->ixa_dce);
3384 #ifdef DEBUG
3385 dce_refhold_notr(dce);
3386 dce_refrele(dce);
3387 #endif
3388 ixa->ixa_dce = dce;
3389 ixa->ixa_dce_generation = generation;
3390 ixa->ixa_postfragfn = ire->ire_postfragfn;
3392 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3393 /* Get an nce to cache. */
3394 nce = ire_to_nce(ire, firsthop, NULL);
3395 if (nce == NULL) {
3396 /* Allocation failure? */
3397 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3398 } else {
3399 if (ixa->ixa_nce != NULL)
3400 nce_refrele(ixa->ixa_nce);
3401 ixa->ixa_nce = nce;
3406 * If the source address is a loopback address, the
3407 * destination had best be local or multicast.
3408 * If we are sending to an IRE_LOCAL using a loopback source then
3409 * it had better be the same zoneid.
3411 if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3412 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3413 ire = NULL; /* Stored in ixa_ire */
3414 error = EADDRNOTAVAIL;
3415 goto bad_addr;
3417 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3418 ire = NULL; /* Stored in ixa_ire */
3419 error = EADDRNOTAVAIL;
3420 goto bad_addr;
3423 if (ire->ire_type & IRE_BROADCAST) {
3425 * If the ULP didn't have a specified source, then we
3426 * make sure we reselect the source when sending
3427 * broadcasts out different interfaces.
3429 if (flags & IPDF_SELECT_SRC)
3430 ixa->ixa_flags |= IXAF_SET_SOURCE;
3431 else
3432 ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3436 * Does the caller want us to pick a source address?
3438 if (flags & IPDF_SELECT_SRC) {
3439 ipaddr_t src_addr;
3442 * We use use ire_nexthop_ill to avoid the under ipmp
3443 * interface for source address selection. Note that for ipmp
3444 * probe packets, ixa_ifindex would have been specified, and
3445 * the ip_select_route() invocation would have picked an ire
3446 * will ire_ill pointing at an under interface.
3448 ill = ire_nexthop_ill(ire);
3450 /* If unreachable we have no ill but need some source */
3451 if (ill == NULL) {
3452 src_addr = htonl(INADDR_LOOPBACK);
3453 /* Make sure we look for a better source address */
3454 generation = SRC_GENERATION_VERIFY;
3455 } else {
3456 error = ip_select_source_v4(ill, setsrc, dst_addr,
3457 ixa->ixa_multicast_ifaddr, zoneid,
3458 ipst, &src_addr, &generation, NULL);
3459 if (error != 0) {
3460 ire = NULL; /* Stored in ixa_ire */
3461 goto bad_addr;
3466 * We allow the source address to to down.
3467 * However, we check that we don't use the loopback address
3468 * as a source when sending out on the wire.
3470 if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3471 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3472 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3473 ire = NULL; /* Stored in ixa_ire */
3474 error = EADDRNOTAVAIL;
3475 goto bad_addr;
3478 *src_addrp = src_addr;
3479 ixa->ixa_src_generation = generation;
3483 * Make sure we don't leave an unreachable ixa_nce in place
3484 * since ip_select_route is used when we unplumb i.e., remove
3485 * references on ixa_ire, ixa_nce, and ixa_dce.
3487 nce = ixa->ixa_nce;
3488 if (nce != NULL && nce->nce_is_condemned) {
3489 nce_refrele(nce);
3490 ixa->ixa_nce = NULL;
3491 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3495 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3496 * However, we can't do it for IPv4 multicast or broadcast.
3498 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3499 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3502 * Set initial value for fragmentation limit. Either conn_ip_output
3503 * or ULP might updates it when there are routing changes.
3504 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3506 pmtu = ip_get_pmtu(ixa);
3507 ixa->ixa_fragsize = pmtu;
3508 /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3509 if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3510 ixa->ixa_pmtu = pmtu;
3513 * Extract information useful for some transports.
3514 * First we look for DCE metrics. Then we take what we have in
3515 * the metrics in the route, where the offlink is used if we have
3516 * one.
3518 if (uinfo != NULL) {
3519 bzero(uinfo, sizeof (*uinfo));
3521 if (dce->dce_flags & DCEF_UINFO)
3522 *uinfo = dce->dce_uinfo;
3524 rts_merge_metrics(uinfo, &ire->ire_metrics);
3526 /* Allow ire_metrics to decrease the path MTU from above */
3527 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3528 uinfo->iulp_mtu = pmtu;
3530 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3531 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3532 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3535 if (ill != NULL)
3536 ill_refrele(ill);
3538 return (error);
3540 bad_addr:
3541 if (ire != NULL)
3542 ire_refrele(ire);
3544 if (ill != NULL)
3545 ill_refrele(ill);
3548 * Make sure we don't leave an unreachable ixa_nce in place
3549 * since ip_select_route is used when we unplumb i.e., remove
3550 * references on ixa_ire, ixa_nce, and ixa_dce.
3552 nce = ixa->ixa_nce;
3553 if (nce != NULL && nce->nce_is_condemned) {
3554 nce_refrele(nce);
3555 ixa->ixa_nce = NULL;
3556 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3559 return (error);
3564 * Get the base MTU for the case when path MTU discovery is not used.
3565 * Takes the MTU of the IRE into account.
3567 uint_t
3568 ip_get_base_mtu(ill_t *ill, ire_t *ire)
3570 uint_t mtu;
3571 uint_t iremtu = ire->ire_metrics.iulp_mtu;
3573 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3574 mtu = ill->ill_mc_mtu;
3575 else
3576 mtu = ill->ill_mtu;
3578 if (iremtu != 0 && iremtu < mtu)
3579 mtu = iremtu;
3581 return (mtu);
3585 * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3586 * Assumes that ixa_ire, dce, and nce have already been set up.
3588 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3589 * We avoid path MTU discovery if it is disabled with ndd.
3590 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3592 * NOTE: We also used to turn it off for source routed packets. That
3593 * is no longer required since the dce is per final destination.
3595 uint_t
3596 ip_get_pmtu(ip_xmit_attr_t *ixa)
3598 ip_stack_t *ipst = ixa->ixa_ipst;
3599 dce_t *dce;
3600 nce_t *nce;
3601 ire_t *ire;
3602 uint_t pmtu;
3604 ire = ixa->ixa_ire;
3605 dce = ixa->ixa_dce;
3606 nce = ixa->ixa_nce;
3609 * If path MTU discovery has been turned off by ndd, then we ignore
3610 * any dce_pmtu and for IPv4 we will not set DF.
3612 if (!ipst->ips_ip_path_mtu_discovery)
3613 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3615 pmtu = IP_MAXPACKET;
3617 * Decide whether whether IPv4 sets DF
3618 * For IPv6 "no DF" means to use the 1280 mtu
3620 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3621 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3622 } else {
3623 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3624 if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3625 pmtu = IPV6_MIN_MTU;
3628 /* Check if the PMTU is to old before we use it */
3629 if ((dce->dce_flags & DCEF_PMTU) &&
3630 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3631 ipst->ips_ip_pathmtu_interval) {
3633 * Older than 20 minutes. Drop the path MTU information.
3635 mutex_enter(&dce->dce_lock);
3636 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3637 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3638 mutex_exit(&dce->dce_lock);
3639 dce_increment_generation(dce);
3642 /* The metrics on the route can lower the path MTU */
3643 if (ire->ire_metrics.iulp_mtu != 0 &&
3644 ire->ire_metrics.iulp_mtu < pmtu)
3645 pmtu = ire->ire_metrics.iulp_mtu;
3648 * If the path MTU is smaller than some minimum, we still use dce_pmtu
3649 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3650 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3652 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3653 if (dce->dce_flags & DCEF_PMTU) {
3654 if (dce->dce_pmtu < pmtu)
3655 pmtu = dce->dce_pmtu;
3657 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3658 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3659 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3660 } else {
3661 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3662 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3664 } else {
3665 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3666 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3671 * If we have an IRE_LOCAL we use the loopback mtu instead of
3672 * the ill for going out the wire i.e., IRE_LOCAL gets the same
3673 * mtu as IRE_LOOPBACK.
3675 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3676 uint_t loopback_mtu;
3678 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3679 ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3681 if (loopback_mtu < pmtu)
3682 pmtu = loopback_mtu;
3683 } else if (nce != NULL) {
3685 * Make sure we don't exceed the interface MTU.
3686 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3687 * an ill. We'd use the above IP_MAXPACKET in that case just
3688 * to tell the transport something larger than zero.
3690 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3691 if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3692 pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3693 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3694 nce->nce_ill->ill_mc_mtu < pmtu) {
3696 * for interfaces in an IPMP group, the mtu of
3697 * the nce_ill (under_ill) could be different
3698 * from the mtu of the ncec_ill, so we take the
3699 * min of the two.
3701 pmtu = nce->nce_ill->ill_mc_mtu;
3703 } else {
3704 if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3705 pmtu = nce->nce_common->ncec_ill->ill_mtu;
3706 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3707 nce->nce_ill->ill_mtu < pmtu) {
3709 * for interfaces in an IPMP group, the mtu of
3710 * the nce_ill (under_ill) could be different
3711 * from the mtu of the ncec_ill, so we take the
3712 * min of the two.
3714 pmtu = nce->nce_ill->ill_mtu;
3720 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3721 * Only applies to IPv6.
3723 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3724 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3725 switch (ixa->ixa_use_min_mtu) {
3726 case IPV6_USE_MIN_MTU_MULTICAST:
3727 if (ire->ire_type & IRE_MULTICAST)
3728 pmtu = IPV6_MIN_MTU;
3729 break;
3730 case IPV6_USE_MIN_MTU_ALWAYS:
3731 pmtu = IPV6_MIN_MTU;
3732 break;
3733 case IPV6_USE_MIN_MTU_NEVER:
3734 break;
3736 } else {
3737 /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3738 if (ire->ire_type & IRE_MULTICAST)
3739 pmtu = IPV6_MIN_MTU;
3743 return (pmtu);
3747 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3748 * the final piece where we don't. Return a pointer to the first mblk in the
3749 * result, and update the pointer to the next mblk to chew on. If anything
3750 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3751 * NULL pointer.
3753 mblk_t *
3754 ip_carve_mp(mblk_t **mpp, ssize_t len)
3756 mblk_t *mp0;
3757 mblk_t *mp1;
3758 mblk_t *mp2;
3760 if (!len || !mpp || !(mp0 = *mpp))
3761 return (NULL);
3762 /* If we aren't going to consume the first mblk, we need a dup. */
3763 if (mp0->b_wptr - mp0->b_rptr > len) {
3764 mp1 = dupb(mp0);
3765 if (mp1) {
3766 /* Partition the data between the two mblks. */
3767 mp1->b_wptr = mp1->b_rptr + len;
3768 mp0->b_rptr = mp1->b_wptr;
3770 * after adjustments if mblk not consumed is now
3771 * unaligned, try to align it. If this fails free
3772 * all messages and let upper layer recover.
3774 if (!OK_32PTR(mp0->b_rptr)) {
3775 if (!pullupmsg(mp0, -1)) {
3776 freemsg(mp0);
3777 freemsg(mp1);
3778 *mpp = NULL;
3779 return (NULL);
3783 return (mp1);
3785 /* Eat through as many mblks as we need to get len bytes. */
3786 len -= mp0->b_wptr - mp0->b_rptr;
3787 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3788 if (mp2->b_wptr - mp2->b_rptr > len) {
3790 * We won't consume the entire last mblk. Like
3791 * above, dup and partition it.
3793 mp1->b_cont = dupb(mp2);
3794 mp1 = mp1->b_cont;
3795 if (!mp1) {
3797 * Trouble. Rather than go to a lot of
3798 * trouble to clean up, we free the messages.
3799 * This won't be any worse than losing it on
3800 * the wire.
3802 freemsg(mp0);
3803 freemsg(mp2);
3804 *mpp = NULL;
3805 return (NULL);
3807 mp1->b_wptr = mp1->b_rptr + len;
3808 mp2->b_rptr = mp1->b_wptr;
3810 * after adjustments if mblk not consumed is now
3811 * unaligned, try to align it. If this fails free
3812 * all messages and let upper layer recover.
3814 if (!OK_32PTR(mp2->b_rptr)) {
3815 if (!pullupmsg(mp2, -1)) {
3816 freemsg(mp0);
3817 freemsg(mp2);
3818 *mpp = NULL;
3819 return (NULL);
3822 *mpp = mp2;
3823 return (mp0);
3825 /* Decrement len by the amount we just got. */
3826 len -= mp2->b_wptr - mp2->b_rptr;
3829 * len should be reduced to zero now. If not our caller has
3830 * screwed up.
3832 if (len) {
3833 /* Shouldn't happen! */
3834 freemsg(mp0);
3835 *mpp = NULL;
3836 return (NULL);
3839 * We consumed up to exactly the end of an mblk. Detach the part
3840 * we are returning from the rest of the chain.
3842 mp1->b_cont = NULL;
3843 *mpp = mp2;
3844 return (mp0);
3847 /* The ill stream is being unplumbed. Called from ip_close */
3849 ip_modclose(ill_t *ill)
3851 boolean_t success;
3852 ipsq_t *ipsq;
3853 ipif_t *ipif;
3854 queue_t *q = ill->ill_rq;
3855 ip_stack_t *ipst = ill->ill_ipst;
3856 int i;
3857 arl_ill_common_t *ai = ill->ill_common;
3860 * The punlink prior to this may have initiated a capability
3861 * negotiation. But ipsq_enter will block until that finishes or
3862 * times out.
3864 success = ipsq_enter(ill, B_FALSE, NEW_OP);
3867 * Open/close/push/pop is guaranteed to be single threaded
3868 * per stream by STREAMS. FS guarantees that all references
3869 * from top are gone before close is called. So there can't
3870 * be another close thread that has set CONDEMNED on this ill.
3871 * and cause ipsq_enter to return failure.
3873 ASSERT(success);
3874 ipsq = ill->ill_phyint->phyint_ipsq;
3877 * Mark it condemned. No new reference will be made to this ill.
3878 * Lookup functions will return an error. Threads that try to
3879 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
3880 * that the refcnt will drop down to zero.
3882 mutex_enter(&ill->ill_lock);
3883 ill->ill_state_flags |= ILL_CONDEMNED;
3884 for (ipif = ill->ill_ipif; ipif != NULL;
3885 ipif = ipif->ipif_next) {
3886 ipif->ipif_state_flags |= IPIF_CONDEMNED;
3889 * Wake up anybody waiting to enter the ipsq. ipsq_enter
3890 * returns error if ILL_CONDEMNED is set
3892 cv_broadcast(&ill->ill_cv);
3893 mutex_exit(&ill->ill_lock);
3896 * Send all the deferred DLPI messages downstream which came in
3897 * during the small window right before ipsq_enter(). We do this
3898 * without waiting for the ACKs because all the ACKs for M_PROTO
3899 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
3901 ill_dlpi_send_deferred(ill);
3904 * Shut down fragmentation reassembly.
3905 * ill_frag_timer won't start a timer again.
3906 * Now cancel any existing timer
3908 (void) untimeout(ill->ill_frag_timer_id);
3909 (void) ill_frag_timeout(ill, 0);
3912 * Call ill_delete to bring down the ipifs, ilms and ill on
3913 * this ill. Then wait for the refcnts to drop to zero.
3914 * ill_is_freeable checks whether the ill is really quiescent.
3915 * Then make sure that threads that are waiting to enter the
3916 * ipsq have seen the error returned by ipsq_enter and have
3917 * gone away. Then we call ill_delete_tail which does the
3918 * DL_UNBIND_REQ with the driver and then qprocsoff.
3920 ill_delete(ill);
3921 mutex_enter(&ill->ill_lock);
3922 while (!ill_is_freeable(ill))
3923 cv_wait(&ill->ill_cv, &ill->ill_lock);
3925 while (ill->ill_waiters)
3926 cv_wait(&ill->ill_cv, &ill->ill_lock);
3928 mutex_exit(&ill->ill_lock);
3931 * ill_delete_tail drops reference on ill_ipst, but we need to keep
3932 * it held until the end of the function since the cleanup
3933 * below needs to be able to use the ip_stack_t.
3935 netstack_hold(ipst->ips_netstack);
3937 /* qprocsoff is done via ill_delete_tail */
3938 ill_delete_tail(ill);
3940 * synchronously wait for arp stream to unbind. After this, we
3941 * cannot get any data packets up from the driver.
3943 arp_unbind_complete(ill);
3944 ASSERT(ill->ill_ipst == NULL);
3947 * Walk through all conns and qenable those that have queued data.
3948 * Close synchronization needs this to
3949 * be done to ensure that all upper layers blocked
3950 * due to flow control to the closing device
3951 * get unblocked.
3953 ip1dbg(("ip_wsrv: walking\n"));
3954 for (i = 0; i < TX_FANOUT_SIZE; i++) {
3955 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
3959 * ai can be null if this is an IPv6 ill, or if the IPv4
3960 * stream is being torn down before ARP was plumbed (e.g.,
3961 * /sbin/ifconfig plumbing a stream twice, and encountering
3962 * an error
3964 if (ai != NULL) {
3965 ASSERT(!ill->ill_isv6);
3966 mutex_enter(&ai->ai_lock);
3967 ai->ai_ill = NULL;
3968 if (ai->ai_arl == NULL) {
3969 mutex_destroy(&ai->ai_lock);
3970 kmem_free(ai, sizeof (*ai));
3971 } else {
3972 cv_signal(&ai->ai_ill_unplumb_done);
3973 mutex_exit(&ai->ai_lock);
3977 mutex_enter(&ipst->ips_ip_mi_lock);
3978 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
3979 mutex_exit(&ipst->ips_ip_mi_lock);
3982 * credp could be null if the open didn't succeed and ip_modopen
3983 * itself calls ip_close.
3985 if (ill->ill_credp != NULL)
3986 crfree(ill->ill_credp);
3988 mutex_destroy(&ill->ill_saved_ire_lock);
3989 mutex_destroy(&ill->ill_lock);
3990 rw_destroy(&ill->ill_mcast_lock);
3991 mutex_destroy(&ill->ill_mcast_serializer);
3992 list_destroy(&ill->ill_nce);
3995 * Now we are done with the module close pieces that
3996 * need the netstack_t.
3998 netstack_rele(ipst->ips_netstack);
4000 mi_close_free((IDP)ill);
4001 q->q_ptr = WR(q)->q_ptr = NULL;
4003 ipsq_exit(ipsq);
4005 return (0);
4009 * This is called as part of close() for IP, UDP, ICMP, and RTS
4010 * in order to quiesce the conn.
4012 void
4013 ip_quiesce_conn(conn_t *connp)
4015 boolean_t drain_cleanup_reqd = B_FALSE;
4016 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
4017 boolean_t ilg_cleanup_reqd = B_FALSE;
4018 ip_stack_t *ipst;
4020 ASSERT(!IPCL_IS_TCP(connp));
4021 ipst = connp->conn_netstack->netstack_ip;
4024 * Mark the conn as closing, and this conn must not be
4025 * inserted in future into any list. Eg. conn_drain_insert(),
4026 * won't insert this conn into the conn_drain_list.
4028 * conn_idl, and conn_ilg cannot get set henceforth.
4030 mutex_enter(&connp->conn_lock);
4031 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4032 connp->conn_state_flags |= CONN_CLOSING;
4033 if (connp->conn_idl != NULL)
4034 drain_cleanup_reqd = B_TRUE;
4035 if (connp->conn_oper_pending_ill != NULL)
4036 conn_ioctl_cleanup_reqd = B_TRUE;
4037 if (connp->conn_dhcpinit_ill != NULL) {
4038 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4039 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4040 ill_set_inputfn(connp->conn_dhcpinit_ill);
4041 connp->conn_dhcpinit_ill = NULL;
4043 if (connp->conn_ilg != NULL)
4044 ilg_cleanup_reqd = B_TRUE;
4045 mutex_exit(&connp->conn_lock);
4047 if (conn_ioctl_cleanup_reqd)
4048 conn_ioctl_cleanup(connp);
4051 * Remove this conn from any fanout list it is on.
4052 * and then wait for any threads currently operating
4053 * on this endpoint to finish
4055 ipcl_hash_remove(connp);
4058 * Remove this conn from the drain list, and do any other cleanup that
4059 * may be required. (TCP conns are never flow controlled, and
4060 * conn_idl will be NULL.)
4062 if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4063 idl_t *idl = connp->conn_idl;
4065 mutex_enter(&idl->idl_lock);
4066 conn_drain(connp, B_TRUE);
4067 mutex_exit(&idl->idl_lock);
4070 if (connp == ipst->ips_ip_g_mrouter)
4071 (void) ip_mrouter_done(ipst);
4073 if (ilg_cleanup_reqd)
4074 ilg_delete_all(connp);
4077 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4078 * callers from write side can't be there now because close
4079 * is in progress. The only other caller is ipcl_walk
4080 * which checks for the condemned flag.
4082 mutex_enter(&connp->conn_lock);
4083 connp->conn_state_flags |= CONN_CONDEMNED;
4084 while (connp->conn_ref != 1)
4085 cv_wait(&connp->conn_cv, &connp->conn_lock);
4086 connp->conn_state_flags |= CONN_QUIESCED;
4087 mutex_exit(&connp->conn_lock);
4090 /* ARGSUSED */
4092 ip_close(queue_t *q, int flags)
4094 conn_t *connp;
4097 * Call the appropriate delete routine depending on whether this is
4098 * a module or device.
4100 if (WR(q)->q_next != NULL) {
4101 /* This is a module close */
4102 return (ip_modclose((ill_t *)q->q_ptr));
4105 connp = q->q_ptr;
4106 ip_quiesce_conn(connp);
4108 qprocsoff(q);
4111 * Now we are truly single threaded on this stream, and can
4112 * delete the things hanging off the connp, and finally the connp.
4113 * We removed this connp from the fanout list, it cannot be
4114 * accessed thru the fanouts, and we already waited for the
4115 * conn_ref to drop to 0. We are already in close, so
4116 * there cannot be any other thread from the top. qprocsoff
4117 * has completed, and service has completed or won't run in
4118 * future.
4120 ASSERT(connp->conn_ref == 1);
4122 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4124 connp->conn_ref--;
4125 ipcl_conn_destroy(connp);
4127 q->q_ptr = WR(q)->q_ptr = NULL;
4128 return (0);
4132 * Wapper around putnext() so that ip_rts_request can merely use
4133 * conn_recv.
4135 /*ARGSUSED2*/
4136 static void
4137 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4139 conn_t *connp = (conn_t *)arg1;
4141 putnext(connp->conn_rq, mp);
4144 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4145 /* ARGSUSED */
4146 static void
4147 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4149 freemsg(mp);
4153 * Called when the module is about to be unloaded
4155 void
4156 ip_ddi_destroy(void)
4158 /* This needs to be called before destroying any transports. */
4159 mutex_enter(&cpu_lock);
4160 unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4161 mutex_exit(&cpu_lock);
4163 icmp_ddi_g_destroy();
4164 rts_ddi_g_destroy();
4165 udp_ddi_g_destroy();
4166 sctp_ddi_g_destroy();
4167 tcp_ddi_g_destroy();
4168 ilb_ddi_g_destroy();
4169 dce_g_destroy();
4170 ipsec_policy_g_destroy();
4171 ipcl_g_destroy();
4172 ip_net_g_destroy();
4173 ip_ire_g_fini();
4174 inet_minor_destroy(ip_minor_arena_sa);
4175 #if defined(_LP64)
4176 inet_minor_destroy(ip_minor_arena_la);
4177 #endif
4179 #ifdef DEBUG
4180 list_destroy(&ip_thread_list);
4181 rw_destroy(&ip_thread_rwlock);
4182 tsd_destroy(&ip_thread_data);
4183 #endif
4185 netstack_unregister(NS_IP);
4189 * First step in cleanup.
4191 /* ARGSUSED */
4192 static void
4193 ip_stack_shutdown(netstackid_t stackid, void *arg)
4195 ip_stack_t *ipst = (ip_stack_t *)arg;
4196 kt_did_t ktid;
4198 #ifdef NS_DEBUG
4199 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4200 #endif
4203 * Perform cleanup for special interfaces (loopback and IPMP).
4205 ip_interface_cleanup(ipst);
4208 * The *_hook_shutdown()s start the process of notifying any
4209 * consumers that things are going away.... nothing is destroyed.
4211 ipv4_hook_shutdown(ipst);
4212 ipv6_hook_shutdown(ipst);
4213 arp_hook_shutdown(ipst);
4215 mutex_enter(&ipst->ips_capab_taskq_lock);
4216 ktid = ipst->ips_capab_taskq_thread->t_did;
4217 ipst->ips_capab_taskq_quit = B_TRUE;
4218 cv_signal(&ipst->ips_capab_taskq_cv);
4219 mutex_exit(&ipst->ips_capab_taskq_lock);
4222 * In rare occurrences, particularly on virtual hardware where CPUs can
4223 * be de-scheduled, the thread that we just signaled will not run until
4224 * after we have gotten through parts of ip_stack_fini. If that happens
4225 * then we'll try to grab the ips_capab_taskq_lock as part of returning
4226 * from cv_wait which no longer exists.
4228 thread_join(ktid);
4232 * Free the IP stack instance.
4234 static void
4235 ip_stack_fini(netstackid_t stackid, void *arg)
4237 ip_stack_t *ipst = (ip_stack_t *)arg;
4238 int ret;
4240 #ifdef NS_DEBUG
4241 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4242 #endif
4244 * At this point, all of the notifications that the events and
4245 * protocols are going away have been run, meaning that we can
4246 * now set about starting to clean things up.
4248 ipobs_fini(ipst);
4249 ipv4_hook_destroy(ipst);
4250 ipv6_hook_destroy(ipst);
4251 arp_hook_destroy(ipst);
4252 ip_net_destroy(ipst);
4254 ipmp_destroy(ipst);
4256 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4257 ipst->ips_ip_mibkp = NULL;
4258 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4259 ipst->ips_icmp_mibkp = NULL;
4260 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4261 ipst->ips_ip_kstat = NULL;
4262 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4263 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4264 ipst->ips_ip6_kstat = NULL;
4265 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4267 kmem_free(ipst->ips_propinfo_tbl,
4268 ip_propinfo_count * sizeof (mod_prop_info_t));
4269 ipst->ips_propinfo_tbl = NULL;
4271 dce_stack_destroy(ipst);
4272 ip_mrouter_stack_destroy(ipst);
4275 * Quiesce all of our timers. Note we set the quiesce flags before we
4276 * call untimeout. The slowtimers may actually kick off another instance
4277 * of the non-slow timers.
4279 mutex_enter(&ipst->ips_igmp_timer_lock);
4280 ipst->ips_igmp_timer_quiesce = B_TRUE;
4281 mutex_exit(&ipst->ips_igmp_timer_lock);
4283 mutex_enter(&ipst->ips_mld_timer_lock);
4284 ipst->ips_mld_timer_quiesce = B_TRUE;
4285 mutex_exit(&ipst->ips_mld_timer_lock);
4287 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
4288 ipst->ips_igmp_slowtimeout_quiesce = B_TRUE;
4289 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
4291 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
4292 ipst->ips_mld_slowtimeout_quiesce = B_TRUE;
4293 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
4295 ret = untimeout(ipst->ips_igmp_timeout_id);
4296 if (ret == -1) {
4297 ASSERT(ipst->ips_igmp_timeout_id == 0);
4298 } else {
4299 ASSERT(ipst->ips_igmp_timeout_id != 0);
4300 ipst->ips_igmp_timeout_id = 0;
4302 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4303 if (ret == -1) {
4304 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4305 } else {
4306 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4307 ipst->ips_igmp_slowtimeout_id = 0;
4309 ret = untimeout(ipst->ips_mld_timeout_id);
4310 if (ret == -1) {
4311 ASSERT(ipst->ips_mld_timeout_id == 0);
4312 } else {
4313 ASSERT(ipst->ips_mld_timeout_id != 0);
4314 ipst->ips_mld_timeout_id = 0;
4316 ret = untimeout(ipst->ips_mld_slowtimeout_id);
4317 if (ret == -1) {
4318 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4319 } else {
4320 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4321 ipst->ips_mld_slowtimeout_id = 0;
4324 ip_ire_fini(ipst);
4325 ip6_asp_free(ipst);
4326 conn_drain_fini(ipst);
4327 ipcl_destroy(ipst);
4329 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4330 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4331 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4332 ipst->ips_ndp4 = NULL;
4333 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4334 ipst->ips_ndp6 = NULL;
4336 if (ipst->ips_loopback_ksp != NULL) {
4337 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4338 ipst->ips_loopback_ksp = NULL;
4341 mutex_destroy(&ipst->ips_capab_taskq_lock);
4342 cv_destroy(&ipst->ips_capab_taskq_cv);
4344 rw_destroy(&ipst->ips_srcid_lock);
4346 mutex_destroy(&ipst->ips_ip_mi_lock);
4347 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4349 mutex_destroy(&ipst->ips_igmp_timer_lock);
4350 mutex_destroy(&ipst->ips_mld_timer_lock);
4351 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4352 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4353 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4354 rw_destroy(&ipst->ips_ill_g_lock);
4356 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4357 ipst->ips_phyint_g_list = NULL;
4358 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4359 ipst->ips_ill_g_heads = NULL;
4361 ldi_ident_release(ipst->ips_ldi_ident);
4362 kmem_free(ipst, sizeof (*ipst));
4366 * This function is called from the TSD destructor, and is used to debug
4367 * reference count issues in IP. See block comment in <inet/ip_if.h> for
4368 * details.
4370 static void
4371 ip_thread_exit(void *phash)
4373 th_hash_t *thh = phash;
4375 rw_enter(&ip_thread_rwlock, RW_WRITER);
4376 list_remove(&ip_thread_list, thh);
4377 rw_exit(&ip_thread_rwlock);
4378 mod_hash_destroy_hash(thh->thh_hash);
4379 kmem_free(thh, sizeof (*thh));
4383 * Called when the IP kernel module is loaded into the kernel
4385 void
4386 ip_ddi_init(void)
4388 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4391 * For IP and TCP the minor numbers should start from 2 since we have 4
4392 * initial devices: ip, ip6, tcp, tcp6.
4395 * If this is a 64-bit kernel, then create two separate arenas -
4396 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4397 * other for socket apps in the range 2^^18 through 2^^32-1.
4399 ip_minor_arena_la = NULL;
4400 ip_minor_arena_sa = NULL;
4401 #if defined(_LP64)
4402 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4403 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4404 cmn_err(CE_PANIC,
4405 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4407 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4408 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4409 cmn_err(CE_PANIC,
4410 "ip_ddi_init: ip_minor_arena_la creation failed\n");
4412 #else
4413 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4414 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4415 cmn_err(CE_PANIC,
4416 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4418 #endif
4419 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4421 ipcl_g_init();
4422 ip_ire_g_init();
4423 ip_net_g_init();
4425 #ifdef DEBUG
4426 tsd_create(&ip_thread_data, ip_thread_exit);
4427 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4428 list_create(&ip_thread_list, sizeof (th_hash_t),
4429 offsetof(th_hash_t, thh_link));
4430 #endif
4431 ipsec_policy_g_init();
4432 tcp_ddi_g_init();
4433 sctp_ddi_g_init();
4434 dce_g_init();
4437 * We want to be informed each time a stack is created or
4438 * destroyed in the kernel, so we can maintain the
4439 * set of udp_stack_t's.
4441 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4442 ip_stack_fini);
4444 udp_ddi_g_init();
4445 rts_ddi_g_init();
4446 icmp_ddi_g_init();
4447 ilb_ddi_g_init();
4449 /* This needs to be called after all transports are initialized. */
4450 mutex_enter(&cpu_lock);
4451 register_cpu_setup_func(ip_tp_cpu_update, NULL);
4452 mutex_exit(&cpu_lock);
4456 * Initialize the IP stack instance.
4458 static void *
4459 ip_stack_init(netstackid_t stackid, netstack_t *ns)
4461 ip_stack_t *ipst;
4462 size_t arrsz;
4463 major_t major;
4465 #ifdef NS_DEBUG
4466 printf("ip_stack_init(stack %d)\n", stackid);
4467 #endif
4469 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4470 ipst->ips_netstack = ns;
4472 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4473 KM_SLEEP);
4474 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4475 KM_SLEEP);
4476 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4477 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4478 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4479 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4481 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4482 ipst->ips_igmp_deferred_next = INFINITY;
4483 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4484 ipst->ips_mld_deferred_next = INFINITY;
4485 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4486 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4487 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4488 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4489 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4490 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4492 ipcl_init(ipst);
4493 ip_ire_init(ipst);
4494 ip6_asp_init(ipst);
4495 ipif_init(ipst);
4496 conn_drain_init(ipst);
4497 ip_mrouter_stack_init(ipst);
4498 dce_stack_init(ipst);
4500 ipst->ips_ill_index = 1;
4502 ipst->ips_saved_ip_forwarding = -1;
4503 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
4505 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4506 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4507 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4509 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4510 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4511 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4512 ipst->ips_ip6_kstat =
4513 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4515 ipst->ips_ip_src_id = 1;
4516 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4518 ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4520 ip_net_init(ipst, ns);
4521 ipv4_hook_init(ipst);
4522 ipv6_hook_init(ipst);
4523 arp_hook_init(ipst);
4524 ipmp_init(ipst);
4525 ipobs_init(ipst);
4528 * Create the taskq dispatcher thread and initialize related stuff.
4530 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4531 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4532 ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4533 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4535 major = mod_name_to_major(INET_NAME);
4536 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4537 return (ipst);
4541 * Allocate and initialize a DLPI template of the specified length. (May be
4542 * called as writer.)
4544 mblk_t *
4545 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4547 mblk_t *mp;
4549 mp = allocb(len, BPRI_MED);
4550 if (!mp)
4551 return (NULL);
4554 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4555 * of which we don't seem to use) are sent with M_PCPROTO, and
4556 * that other DLPI are M_PROTO.
4558 if (prim == DL_INFO_REQ) {
4559 mp->b_datap->db_type = M_PCPROTO;
4560 } else {
4561 mp->b_datap->db_type = M_PROTO;
4564 mp->b_wptr = mp->b_rptr + len;
4565 bzero(mp->b_rptr, len);
4566 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4567 return (mp);
4571 * Allocate and initialize a DLPI notification. (May be called as writer.)
4573 mblk_t *
4574 ip_dlnotify_alloc(uint_t notification, uint_t data)
4576 dl_notify_ind_t *notifyp;
4577 mblk_t *mp;
4579 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4580 return (NULL);
4582 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4583 notifyp->dl_notification = notification;
4584 notifyp->dl_data = data;
4585 return (mp);
4588 mblk_t *
4589 ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4591 dl_notify_ind_t *notifyp;
4592 mblk_t *mp;
4594 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4595 return (NULL);
4597 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4598 notifyp->dl_notification = notification;
4599 notifyp->dl_data1 = data1;
4600 notifyp->dl_data2 = data2;
4601 return (mp);
4605 * Debug formatting routine. Returns a character string representation of the
4606 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
4607 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4609 * Once the ndd table-printing interfaces are removed, this can be changed to
4610 * standard dotted-decimal form.
4612 char *
4613 ip_dot_addr(ipaddr_t addr, char *buf)
4615 uint8_t *ap = (uint8_t *)&addr;
4617 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4618 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4619 return (buf);
4623 * Write the given MAC address as a printable string in the usual colon-
4624 * separated format.
4626 const char *
4627 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4629 char *bp;
4631 if (alen == 0 || buflen < 4)
4632 return ("?");
4633 bp = buf;
4634 for (;;) {
4636 * If there are more MAC address bytes available, but we won't
4637 * have any room to print them, then add "..." to the string
4638 * instead. See below for the 'magic number' explanation.
4640 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4641 (void) strcpy(bp, "...");
4642 break;
4644 (void) sprintf(bp, "%02x", *addr++);
4645 bp += 2;
4646 if (--alen == 0)
4647 break;
4648 *bp++ = ':';
4649 buflen -= 3;
4651 * At this point, based on the first 'if' statement above,
4652 * either alen == 1 and buflen >= 3, or alen > 1 and
4653 * buflen >= 4. The first case leaves room for the final "xx"
4654 * number and trailing NUL byte. The second leaves room for at
4655 * least "...". Thus the apparently 'magic' numbers chosen for
4656 * that statement.
4659 return (buf);
4663 * Called when it is conceptually a ULP that would sent the packet
4664 * e.g., port unreachable and protocol unreachable. Check that the packet
4665 * would have passed the IPsec global policy before sending the error.
4667 * Send an ICMP error after patching up the packet appropriately.
4668 * Uses ip_drop_input and bumps the appropriate MIB.
4670 void
4671 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4672 ip_recv_attr_t *ira)
4674 ipha_t *ipha;
4675 boolean_t secure;
4676 ill_t *ill = ira->ira_ill;
4677 ip_stack_t *ipst = ill->ill_ipst;
4678 netstack_t *ns = ipst->ips_netstack;
4679 ipsec_stack_t *ipss = ns->netstack_ipsec;
4681 secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4684 * We are generating an icmp error for some inbound packet.
4685 * Called from all ip_fanout_(udp, tcp, proto) functions.
4686 * Before we generate an error, check with global policy
4687 * to see whether this is allowed to enter the system. As
4688 * there is no "conn", we are checking with global policy.
4690 ipha = (ipha_t *)mp->b_rptr;
4691 if (secure || ipss->ipsec_inbound_v4_policy_present) {
4692 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4693 if (mp == NULL)
4694 return;
4697 /* We never send errors for protocols that we do implement */
4698 if (ira->ira_protocol == IPPROTO_ICMP ||
4699 ira->ira_protocol == IPPROTO_IGMP) {
4700 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4701 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4702 freemsg(mp);
4703 return;
4706 * Have to correct checksum since
4707 * the packet might have been
4708 * fragmented and the reassembly code in ip_rput
4709 * does not restore the IP checksum.
4711 ipha->ipha_hdr_checksum = 0;
4712 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4714 switch (icmp_type) {
4715 case ICMP_DEST_UNREACHABLE:
4716 switch (icmp_code) {
4717 case ICMP_PROTOCOL_UNREACHABLE:
4718 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4719 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4720 break;
4721 case ICMP_PORT_UNREACHABLE:
4722 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4723 ip_drop_input("ipIfStatsNoPorts", mp, ill);
4724 break;
4727 icmp_unreachable(mp, icmp_code, ira);
4728 break;
4729 default:
4730 #ifdef DEBUG
4731 panic("ip_fanout_send_icmp_v4: wrong type");
4732 /*NOTREACHED*/
4733 #else
4734 freemsg(mp);
4735 break;
4736 #endif
4741 * Used to send an ICMP error message when a packet is received for
4742 * a protocol that is not supported. The mblk passed as argument
4743 * is consumed by this function.
4745 void
4746 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4748 ipha_t *ipha;
4750 ipha = (ipha_t *)mp->b_rptr;
4751 if (ira->ira_flags & IRAF_IS_IPV4) {
4752 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4753 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4754 ICMP_PROTOCOL_UNREACHABLE, ira);
4755 } else {
4756 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4757 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4758 ICMP6_PARAMPROB_NEXTHEADER, ira);
4763 * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4764 * Handles IPv4 and IPv6.
4765 * We are responsible for disposing of mp, such as by freemsg() or putnext()
4766 * Caller is responsible for dropping references to the conn.
4768 void
4769 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4770 ip_recv_attr_t *ira)
4772 ill_t *ill = ira->ira_ill;
4773 ip_stack_t *ipst = ill->ill_ipst;
4774 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4775 boolean_t secure;
4776 uint_t protocol = ira->ira_protocol;
4777 iaflags_t iraflags = ira->ira_flags;
4778 queue_t *rq;
4780 secure = iraflags & IRAF_IPSEC_SECURE;
4782 rq = connp->conn_rq;
4783 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4784 switch (protocol) {
4785 case IPPROTO_ICMPV6:
4786 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4787 break;
4788 case IPPROTO_ICMP:
4789 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4790 break;
4791 default:
4792 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4793 break;
4795 freemsg(mp);
4796 return;
4799 ASSERT(!(IPCL_IS_IPTUN(connp)));
4801 if (((iraflags & IRAF_IS_IPV4) ?
4802 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4803 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4804 secure) {
4805 mp = ipsec_check_inbound_policy(mp, connp, ipha,
4806 ip6h, ira);
4807 if (mp == NULL) {
4808 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4809 /* Note that mp is NULL */
4810 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4811 return;
4815 if (iraflags & IRAF_ICMP_ERROR) {
4816 (connp->conn_recvicmp)(connp, mp, NULL, ira);
4817 } else {
4818 ill_t *rill = ira->ira_rill;
4820 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4821 ira->ira_ill = ira->ira_rill = NULL;
4822 /* Send it upstream */
4823 (connp->conn_recv)(connp, mp, NULL, ira);
4824 ira->ira_ill = ill;
4825 ira->ira_rill = rill;
4830 * Handle protocols with which IP is less intimate. There
4831 * can be more than one stream bound to a particular
4832 * protocol. When this is the case, normally each one gets a copy
4833 * of any incoming packets.
4835 * IPsec NOTE :
4837 * Don't allow a secure packet going up a non-secure connection.
4838 * We don't allow this because
4840 * 1) Reply might go out in clear which will be dropped at
4841 * the sending side.
4842 * 2) If the reply goes out in clear it will give the
4843 * adversary enough information for getting the key in
4844 * most of the cases.
4846 * Moreover getting a secure packet when we expect clear
4847 * implies that SA's were added without checking for
4848 * policy on both ends. This should not happen once ISAKMP
4849 * is used to negotiate SAs as SAs will be added only after
4850 * verifying the policy.
4852 * Zones notes:
4853 * Earlier in ip_input on a system with multiple shared-IP zones we
4854 * duplicate the multicast and broadcast packets and send them up
4855 * with each explicit zoneid that exists on that ill.
4856 * This means that here we can match the zoneid with SO_ALLZONES being special.
4858 void
4859 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
4861 mblk_t *mp1;
4862 ipaddr_t laddr;
4863 conn_t *connp, *first_connp, *next_connp;
4864 connf_t *connfp;
4865 ill_t *ill = ira->ira_ill;
4866 ip_stack_t *ipst = ill->ill_ipst;
4868 laddr = ipha->ipha_dst;
4870 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
4871 mutex_enter(&connfp->connf_lock);
4872 connp = connfp->connf_head;
4874 if (connp == NULL) {
4876 * No one bound to these addresses. Is
4877 * there a client that wants all
4878 * unclaimed datagrams?
4880 mutex_exit(&connfp->connf_lock);
4881 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4882 ICMP_PROTOCOL_UNREACHABLE, ira);
4883 return;
4886 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
4888 CONN_INC_REF(connp);
4889 first_connp = connp;
4890 connp = connp->conn_next;
4892 for (;;) {
4893 while (connp != NULL) {
4894 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
4895 if (IPCL_PROTO_MATCH(connp, ira, ipha))
4896 break;
4897 connp = connp->conn_next;
4900 if (connp == NULL) {
4901 /* No more interested clients */
4902 connp = first_connp;
4903 break;
4905 if (((mp1 = dupmsg(mp)) == NULL) &&
4906 ((mp1 = copymsg(mp)) == NULL)) {
4907 /* Memory allocation failed */
4908 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4909 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4910 connp = first_connp;
4911 break;
4914 CONN_INC_REF(connp);
4915 mutex_exit(&connfp->connf_lock);
4917 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
4918 ira);
4920 mutex_enter(&connfp->connf_lock);
4921 /* Follow the next pointer before releasing the conn. */
4922 next_connp = connp->conn_next;
4923 CONN_DEC_REF(connp);
4924 connp = next_connp;
4927 /* Last one. Send it upstream. */
4928 mutex_exit(&connfp->connf_lock);
4930 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
4932 CONN_DEC_REF(connp);
4936 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
4937 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk
4938 * is not consumed.
4940 * One of three things can happen, all of which affect the passed-in mblk:
4942 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk..
4944 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
4945 * ESP packet, and is passed along to ESP for consumption. Return NULL.
4947 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL.
4949 mblk_t *
4950 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
4952 int shift, plen, iph_len;
4953 ipha_t *ipha;
4954 udpha_t *udpha;
4955 uint32_t *spi;
4956 uint32_t esp_ports;
4957 uint8_t *orptr;
4958 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
4959 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4961 ipha = (ipha_t *)mp->b_rptr;
4962 iph_len = ira->ira_ip_hdr_length;
4963 plen = ira->ira_pktlen;
4965 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
4967 * Most likely a keepalive for the benefit of an intervening
4968 * NAT. These aren't for us, per se, so drop it.
4970 * RFC 3947/8 doesn't say for sure what to do for 2-3
4971 * byte packets (keepalives are 1-byte), but we'll drop them
4972 * also.
4974 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
4975 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
4976 return (NULL);
4979 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
4980 /* might as well pull it all up - it might be ESP. */
4981 if (!pullupmsg(mp, -1)) {
4982 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
4983 DROPPER(ipss, ipds_esp_nomem),
4984 &ipss->ipsec_dropper);
4985 return (NULL);
4988 ipha = (ipha_t *)mp->b_rptr;
4990 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
4991 if (*spi == 0) {
4992 /* UDP packet - remove 0-spi. */
4993 shift = sizeof (uint32_t);
4994 } else {
4995 /* ESP-in-UDP packet - reduce to ESP. */
4996 ipha->ipha_protocol = IPPROTO_ESP;
4997 shift = sizeof (udpha_t);
5000 /* Fix IP header */
5001 ira->ira_pktlen = (plen - shift);
5002 ipha->ipha_length = htons(ira->ira_pktlen);
5003 ipha->ipha_hdr_checksum = 0;
5005 orptr = mp->b_rptr;
5006 mp->b_rptr += shift;
5008 udpha = (udpha_t *)(orptr + iph_len);
5009 if (*spi == 0) {
5010 ASSERT((uint8_t *)ipha == orptr);
5011 udpha->uha_length = htons(plen - shift - iph_len);
5012 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
5013 esp_ports = 0;
5014 } else {
5015 esp_ports = *((uint32_t *)udpha);
5016 ASSERT(esp_ports != 0);
5018 ovbcopy(orptr, orptr + shift, iph_len);
5019 if (esp_ports != 0) /* Punt up for ESP processing. */ {
5020 ipha = (ipha_t *)(orptr + shift);
5022 ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5023 ira->ira_esp_udp_ports = esp_ports;
5024 ip_fanout_v4(mp, ipha, ira);
5025 return (NULL);
5027 return (mp);
5031 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5032 * Handles IPv4 and IPv6.
5033 * We are responsible for disposing of mp, such as by freemsg() or putnext()
5034 * Caller is responsible for dropping references to the conn.
5036 void
5037 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5038 ip_recv_attr_t *ira)
5040 ill_t *ill = ira->ira_ill;
5041 ip_stack_t *ipst = ill->ill_ipst;
5042 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5043 boolean_t secure;
5044 iaflags_t iraflags = ira->ira_flags;
5046 secure = iraflags & IRAF_IPSEC_SECURE;
5048 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5049 !canputnext(connp->conn_rq)) {
5050 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5051 freemsg(mp);
5052 return;
5055 if (((iraflags & IRAF_IS_IPV4) ?
5056 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5057 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5058 secure) {
5059 mp = ipsec_check_inbound_policy(mp, connp, ipha,
5060 ip6h, ira);
5061 if (mp == NULL) {
5062 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5063 /* Note that mp is NULL */
5064 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5065 return;
5070 * Since this code is not used for UDP unicast we don't need a NAT_T
5071 * check. Only ip_fanout_v4 has that check.
5073 if (ira->ira_flags & IRAF_ICMP_ERROR) {
5074 (connp->conn_recvicmp)(connp, mp, NULL, ira);
5075 } else {
5076 ill_t *rill = ira->ira_rill;
5078 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5079 ira->ira_ill = ira->ira_rill = NULL;
5080 /* Send it upstream */
5081 (connp->conn_recv)(connp, mp, NULL, ira);
5082 ira->ira_ill = ill;
5083 ira->ira_rill = rill;
5088 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5089 * (Unicast fanout is handled in ip_input_v4.)
5091 * If SO_REUSEADDR is set all multicast and broadcast packets
5092 * will be delivered to all conns bound to the same port.
5094 * If there is at least one matching AF_INET receiver, then we will
5095 * ignore any AF_INET6 receivers.
5096 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5097 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5098 * packets.
5100 * Zones notes:
5101 * Earlier in ip_input on a system with multiple shared-IP zones we
5102 * duplicate the multicast and broadcast packets and send them up
5103 * with each explicit zoneid that exists on that ill.
5104 * This means that here we can match the zoneid with SO_ALLZONES being special.
5106 void
5107 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5108 ip_recv_attr_t *ira)
5110 ipaddr_t laddr;
5111 in6_addr_t v6faddr;
5112 conn_t *connp;
5113 connf_t *connfp;
5114 ipaddr_t faddr;
5115 ill_t *ill = ira->ira_ill;
5116 ip_stack_t *ipst = ill->ill_ipst;
5118 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5120 laddr = ipha->ipha_dst;
5121 faddr = ipha->ipha_src;
5123 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5124 mutex_enter(&connfp->connf_lock);
5125 connp = connfp->connf_head;
5128 * If SO_REUSEADDR has been set on the first we send the
5129 * packet to all clients that have joined the group and
5130 * match the port.
5132 while (connp != NULL) {
5133 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5134 conn_wantpacket(connp, ira, ipha))
5135 break;
5136 connp = connp->conn_next;
5139 if (connp == NULL)
5140 goto notfound;
5142 CONN_INC_REF(connp);
5144 if (connp->conn_reuseaddr) {
5145 conn_t *first_connp = connp;
5146 conn_t *next_connp;
5147 mblk_t *mp1;
5149 connp = connp->conn_next;
5150 for (;;) {
5151 while (connp != NULL) {
5152 if (IPCL_UDP_MATCH(connp, lport, laddr,
5153 fport, faddr) &&
5154 conn_wantpacket(connp, ira, ipha))
5155 break;
5156 connp = connp->conn_next;
5158 if (connp == NULL) {
5159 /* No more interested clients */
5160 connp = first_connp;
5161 break;
5163 if (((mp1 = dupmsg(mp)) == NULL) &&
5164 ((mp1 = copymsg(mp)) == NULL)) {
5165 /* Memory allocation failed */
5166 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5167 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5168 connp = first_connp;
5169 break;
5171 CONN_INC_REF(connp);
5172 mutex_exit(&connfp->connf_lock);
5174 IP_STAT(ipst, ip_udp_fanmb);
5175 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5176 NULL, ira);
5177 mutex_enter(&connfp->connf_lock);
5178 /* Follow the next pointer before releasing the conn */
5179 next_connp = connp->conn_next;
5180 CONN_DEC_REF(connp);
5181 connp = next_connp;
5185 /* Last one. Send it upstream. */
5186 mutex_exit(&connfp->connf_lock);
5187 IP_STAT(ipst, ip_udp_fanmb);
5188 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5189 CONN_DEC_REF(connp);
5190 return;
5192 notfound:
5193 mutex_exit(&connfp->connf_lock);
5195 * IPv6 endpoints bound to multicast IPv4-mapped addresses
5196 * have already been matched above, since they live in the IPv4
5197 * fanout tables. This implies we only need to
5198 * check for IPv6 in6addr_any endpoints here.
5199 * Thus we compare using ipv6_all_zeros instead of the destination
5200 * address, except for the multicast group membership lookup which
5201 * uses the IPv4 destination.
5203 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5204 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5205 mutex_enter(&connfp->connf_lock);
5206 connp = connfp->connf_head;
5208 * IPv4 multicast packet being delivered to an AF_INET6
5209 * in6addr_any endpoint.
5210 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5211 * and not conn_wantpacket_v6() since any multicast membership is
5212 * for an IPv4-mapped multicast address.
5214 while (connp != NULL) {
5215 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5216 fport, v6faddr) &&
5217 conn_wantpacket(connp, ira, ipha))
5218 break;
5219 connp = connp->conn_next;
5222 if (connp == NULL) {
5224 * No one bound to this port. Is
5225 * there a client that wants all
5226 * unclaimed datagrams?
5228 mutex_exit(&connfp->connf_lock);
5230 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5231 NULL) {
5232 ASSERT(ira->ira_protocol == IPPROTO_UDP);
5233 ip_fanout_proto_v4(mp, ipha, ira);
5234 } else {
5236 * We used to attempt to send an icmp error here, but
5237 * since this is known to be a multicast packet
5238 * and we don't send icmp errors in response to
5239 * multicast, just drop the packet and give up sooner.
5241 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5242 freemsg(mp);
5244 return;
5246 CONN_INC_REF(connp);
5247 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5250 * If SO_REUSEADDR has been set on the first we send the
5251 * packet to all clients that have joined the group and
5252 * match the port.
5254 if (connp->conn_reuseaddr) {
5255 conn_t *first_connp = connp;
5256 conn_t *next_connp;
5257 mblk_t *mp1;
5259 connp = connp->conn_next;
5260 for (;;) {
5261 while (connp != NULL) {
5262 if (IPCL_UDP_MATCH_V6(connp, lport,
5263 ipv6_all_zeros, fport, v6faddr) &&
5264 conn_wantpacket(connp, ira, ipha))
5265 break;
5266 connp = connp->conn_next;
5268 if (connp == NULL) {
5269 /* No more interested clients */
5270 connp = first_connp;
5271 break;
5273 if (((mp1 = dupmsg(mp)) == NULL) &&
5274 ((mp1 = copymsg(mp)) == NULL)) {
5275 /* Memory allocation failed */
5276 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5277 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5278 connp = first_connp;
5279 break;
5281 CONN_INC_REF(connp);
5282 mutex_exit(&connfp->connf_lock);
5284 IP_STAT(ipst, ip_udp_fanmb);
5285 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5286 NULL, ira);
5287 mutex_enter(&connfp->connf_lock);
5288 /* Follow the next pointer before releasing the conn */
5289 next_connp = connp->conn_next;
5290 CONN_DEC_REF(connp);
5291 connp = next_connp;
5295 /* Last one. Send it upstream. */
5296 mutex_exit(&connfp->connf_lock);
5297 IP_STAT(ipst, ip_udp_fanmb);
5298 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5299 CONN_DEC_REF(connp);
5303 * Split an incoming packet's IPv4 options into options.
5304 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5305 * clearing out any leftover options.
5306 * Otherwise it just makes ipp point into the packet.
5308 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5311 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5313 uchar_t *opt;
5314 uint32_t totallen;
5315 uint32_t optval;
5316 uint32_t optlen;
5318 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5319 ipp->ipp_hoplimit = ipha->ipha_ttl;
5320 ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5321 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5324 * Get length (in 4 byte octets) of IP header options.
5326 totallen = ipha->ipha_version_and_hdr_length -
5327 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5329 if (totallen == 0) {
5330 if (!allocate)
5331 return (0);
5333 /* Clear out anything from a previous packet */
5334 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5335 kmem_free(ipp->ipp_ipv4_options,
5336 ipp->ipp_ipv4_options_len);
5337 ipp->ipp_ipv4_options = NULL;
5338 ipp->ipp_ipv4_options_len = 0;
5339 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5341 return (0);
5344 totallen <<= 2;
5345 opt = (uchar_t *)&ipha[1];
5347 copyall:
5348 if (!allocate) {
5349 if (totallen != 0) {
5350 ipp->ipp_ipv4_options = opt;
5351 ipp->ipp_ipv4_options_len = totallen;
5352 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5354 return (0);
5356 /* Just copy all of options */
5357 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5358 if (totallen == ipp->ipp_ipv4_options_len) {
5359 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5360 return (0);
5362 kmem_free(ipp->ipp_ipv4_options,
5363 ipp->ipp_ipv4_options_len);
5364 ipp->ipp_ipv4_options = NULL;
5365 ipp->ipp_ipv4_options_len = 0;
5366 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5368 if (totallen == 0)
5369 return (0);
5371 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5372 if (ipp->ipp_ipv4_options == NULL)
5373 return (ENOMEM);
5374 ipp->ipp_ipv4_options_len = totallen;
5375 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5376 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5377 return (0);
5379 totallen = ipha->ipha_version_and_hdr_length -
5380 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5381 totallen <<= 2;
5382 opt = (uchar_t *)&ipha[1];
5383 goto copyall;
5387 * Efficient versions of lookup for an IRE when we only
5388 * match the address.
5389 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5390 * Does not handle multicast addresses.
5392 uint_t
5393 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5395 ire_t *ire;
5396 uint_t result;
5398 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5399 ASSERT(ire != NULL);
5400 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5401 result = IRE_NOROUTE;
5402 else
5403 result = ire->ire_type;
5404 ire_refrele(ire);
5405 return (result);
5409 * Efficient versions of lookup for an IRE when we only
5410 * match the address.
5411 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5412 * Does not handle multicast addresses.
5414 uint_t
5415 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5417 ire_t *ire;
5418 uint_t result;
5420 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5421 ASSERT(ire != NULL);
5422 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5423 result = IRE_NOROUTE;
5424 else
5425 result = ire->ire_type;
5426 ire_refrele(ire);
5427 return (result);
5431 * Nobody should be sending
5432 * packets up this stream
5434 static void
5435 ip_lrput(queue_t *q, mblk_t *mp)
5437 switch (mp->b_datap->db_type) {
5438 case M_FLUSH:
5439 /* Turn around */
5440 if (*mp->b_rptr & FLUSHW) {
5441 *mp->b_rptr &= ~FLUSHR;
5442 qreply(q, mp);
5443 return;
5445 break;
5447 freemsg(mp);
5450 /* Nobody should be sending packets down this stream */
5451 /* ARGSUSED */
5452 void
5453 ip_lwput(queue_t *q, mblk_t *mp)
5455 freemsg(mp);
5459 * Move the first hop in any source route to ipha_dst and remove that part of
5460 * the source route. Called by other protocols. Errors in option formatting
5461 * are ignored - will be handled by ip_output_options. Return the final
5462 * destination (either ipha_dst or the last entry in a source route.)
5464 ipaddr_t
5465 ip_massage_options(ipha_t *ipha, netstack_t *ns)
5467 ipoptp_t opts;
5468 uchar_t *opt;
5469 uint8_t optval;
5470 uint8_t optlen;
5471 ipaddr_t dst;
5472 int i;
5473 ip_stack_t *ipst = ns->netstack_ip;
5475 ip2dbg(("ip_massage_options\n"));
5476 dst = ipha->ipha_dst;
5477 for (optval = ipoptp_first(&opts, ipha);
5478 optval != IPOPT_EOL;
5479 optval = ipoptp_next(&opts)) {
5480 opt = opts.ipoptp_cur;
5481 switch (optval) {
5482 uint8_t off;
5483 case IPOPT_SSRR:
5484 case IPOPT_LSRR:
5485 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5486 ip1dbg(("ip_massage_options: bad src route\n"));
5487 break;
5489 optlen = opts.ipoptp_len;
5490 off = opt[IPOPT_OFFSET];
5491 off--;
5492 redo_srr:
5493 if (optlen < IP_ADDR_LEN ||
5494 off > optlen - IP_ADDR_LEN) {
5495 /* End of source route */
5496 ip1dbg(("ip_massage_options: end of SR\n"));
5497 break;
5499 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5500 ip1dbg(("ip_massage_options: next hop 0x%x\n",
5501 ntohl(dst)));
5503 * Check if our address is present more than
5504 * once as consecutive hops in source route.
5505 * XXX verify per-interface ip_forwarding
5506 * for source route?
5508 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5509 off += IP_ADDR_LEN;
5510 goto redo_srr;
5512 if (dst == htonl(INADDR_LOOPBACK)) {
5513 ip1dbg(("ip_massage_options: loopback addr in "
5514 "source route!\n"));
5515 break;
5518 * Update ipha_dst to be the first hop and remove the
5519 * first hop from the source route (by overwriting
5520 * part of the option with NOP options).
5522 ipha->ipha_dst = dst;
5523 /* Put the last entry in dst */
5524 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5526 bcopy(&opt[off], &dst, IP_ADDR_LEN);
5528 ip1dbg(("ip_massage_options: last hop 0x%x\n",
5529 ntohl(dst)));
5530 /* Move down and overwrite */
5531 opt[IP_ADDR_LEN] = opt[0];
5532 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5533 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5534 for (i = 0; i < IP_ADDR_LEN; i++)
5535 opt[i] = IPOPT_NOP;
5536 break;
5539 return (dst);
5543 * Return the network mask
5544 * associated with the specified address.
5546 ipaddr_t
5547 ip_net_mask(ipaddr_t addr)
5549 uchar_t *up = (uchar_t *)&addr;
5550 ipaddr_t mask = 0;
5551 uchar_t *maskp = (uchar_t *)&mask;
5553 #if defined(__i386) || defined(__amd64)
5554 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5555 #endif
5556 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
5557 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5558 #endif
5559 if (CLASSD(addr)) {
5560 maskp[0] = 0xF0;
5561 return (mask);
5564 /* We assume Class E default netmask to be 32 */
5565 if (CLASSE(addr))
5566 return (0xffffffffU);
5568 if (addr == 0)
5569 return (0);
5570 maskp[0] = 0xFF;
5571 if ((up[0] & 0x80) == 0)
5572 return (mask);
5574 maskp[1] = 0xFF;
5575 if ((up[0] & 0xC0) == 0x80)
5576 return (mask);
5578 maskp[2] = 0xFF;
5579 if ((up[0] & 0xE0) == 0xC0)
5580 return (mask);
5582 /* Otherwise return no mask */
5583 return ((ipaddr_t)0);
5586 /* Name/Value Table Lookup Routine */
5587 char *
5588 ip_nv_lookup(nv_t *nv, int value)
5590 if (!nv)
5591 return (NULL);
5592 for (; nv->nv_name; nv++) {
5593 if (nv->nv_value == value)
5594 return (nv->nv_name);
5596 return ("unknown");
5599 static int
5600 ip_wait_for_info_ack(ill_t *ill)
5602 int err;
5604 mutex_enter(&ill->ill_lock);
5605 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5607 * Return value of 0 indicates a pending signal.
5609 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5610 if (err == 0) {
5611 mutex_exit(&ill->ill_lock);
5612 return (EINTR);
5615 mutex_exit(&ill->ill_lock);
5617 * ip_rput_other could have set an error in ill_error on
5618 * receipt of M_ERROR.
5620 return (ill->ill_error);
5624 * This is a module open, i.e. this is a control stream for access
5625 * to a DLPI device. We allocate an ill_t as the instance data in
5626 * this case.
5628 static int
5629 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5631 ill_t *ill;
5632 int err;
5633 zoneid_t zoneid;
5634 netstack_t *ns;
5635 ip_stack_t *ipst;
5638 * Prevent unprivileged processes from pushing IP so that
5639 * they can't send raw IP.
5641 if (secpolicy_net_rawaccess(credp) != 0)
5642 return (EPERM);
5644 ns = netstack_find_by_cred(credp);
5645 ASSERT(ns != NULL);
5646 ipst = ns->netstack_ip;
5647 ASSERT(ipst != NULL);
5650 * For exclusive stacks we set the zoneid to zero
5651 * to make IP operate as if in the global zone.
5653 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5654 zoneid = GLOBAL_ZONEID;
5655 else
5656 zoneid = crgetzoneid(credp);
5658 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5659 q->q_ptr = WR(q)->q_ptr = ill;
5660 ill->ill_ipst = ipst;
5661 ill->ill_zoneid = zoneid;
5664 * ill_init initializes the ill fields and then sends down
5665 * down a DL_INFO_REQ after calling qprocson.
5667 err = ill_init(q, ill);
5669 if (err != 0) {
5670 mi_free(ill);
5671 netstack_rele(ipst->ips_netstack);
5672 q->q_ptr = NULL;
5673 WR(q)->q_ptr = NULL;
5674 return (err);
5678 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5680 * ill_init initializes the ipsq marking this thread as
5681 * writer
5683 ipsq_exit(ill->ill_phyint->phyint_ipsq);
5684 err = ip_wait_for_info_ack(ill);
5685 if (err == 0)
5686 ill->ill_credp = credp;
5687 else
5688 goto fail;
5690 crhold(credp);
5692 mutex_enter(&ipst->ips_ip_mi_lock);
5693 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5694 sflag, credp);
5695 mutex_exit(&ipst->ips_ip_mi_lock);
5696 fail:
5697 if (err) {
5698 (void) ip_close(q, 0);
5699 return (err);
5701 return (0);
5704 /* For /dev/ip aka AF_INET open */
5706 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5708 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5711 /* For /dev/ip6 aka AF_INET6 open */
5713 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5715 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5718 /* IP open routine. */
5720 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5721 boolean_t isv6)
5723 conn_t *connp;
5724 major_t maj;
5725 zoneid_t zoneid;
5726 netstack_t *ns;
5727 ip_stack_t *ipst;
5729 /* Allow reopen. */
5730 if (q->q_ptr != NULL)
5731 return (0);
5733 if (sflag & MODOPEN) {
5734 /* This is a module open */
5735 return (ip_modopen(q, devp, flag, sflag, credp));
5738 if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5740 * Non streams based socket looking for a stream
5741 * to access IP
5743 return (ip_helper_stream_setup(q, devp, flag, sflag,
5744 credp, isv6));
5747 ns = netstack_find_by_cred(credp);
5748 ASSERT(ns != NULL);
5749 ipst = ns->netstack_ip;
5750 ASSERT(ipst != NULL);
5753 * For exclusive stacks we set the zoneid to zero
5754 * to make IP operate as if in the global zone.
5756 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5757 zoneid = GLOBAL_ZONEID;
5758 else
5759 zoneid = crgetzoneid(credp);
5762 * We are opening as a device. This is an IP client stream, and we
5763 * allocate an conn_t as the instance data.
5765 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
5768 * ipcl_conn_create did a netstack_hold. Undo the hold that was
5769 * done by netstack_find_by_cred()
5771 netstack_rele(ipst->ips_netstack);
5773 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
5774 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
5775 connp->conn_ixa->ixa_zoneid = zoneid;
5776 connp->conn_zoneid = zoneid;
5778 connp->conn_rq = q;
5779 q->q_ptr = WR(q)->q_ptr = connp;
5781 /* Minor tells us which /dev entry was opened */
5782 if (isv6) {
5783 connp->conn_family = AF_INET6;
5784 connp->conn_ipversion = IPV6_VERSION;
5785 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
5786 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
5787 } else {
5788 connp->conn_family = AF_INET;
5789 connp->conn_ipversion = IPV4_VERSION;
5790 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
5793 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
5794 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
5795 connp->conn_minor_arena = ip_minor_arena_la;
5796 } else {
5798 * Either minor numbers in the large arena were exhausted
5799 * or a non socket application is doing the open.
5800 * Try to allocate from the small arena.
5802 if ((connp->conn_dev =
5803 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
5804 /* CONN_DEC_REF takes care of netstack_rele() */
5805 q->q_ptr = WR(q)->q_ptr = NULL;
5806 CONN_DEC_REF(connp);
5807 return (EBUSY);
5809 connp->conn_minor_arena = ip_minor_arena_sa;
5812 maj = getemajor(*devp);
5813 *devp = makedevice(maj, (minor_t)connp->conn_dev);
5816 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
5818 connp->conn_cred = credp;
5819 connp->conn_cpid = curproc->p_pid;
5820 /* Cache things in ixa without an extra refhold */
5821 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
5822 connp->conn_ixa->ixa_cred = connp->conn_cred;
5823 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
5826 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
5828 connp->conn_recv = ip_conn_input;
5829 connp->conn_recvicmp = ip_conn_input_icmp;
5831 crhold(connp->conn_cred);
5833 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
5835 connp->conn_rq = q;
5836 connp->conn_wq = WR(q);
5838 /* Non-zero default values */
5839 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
5842 * Make the conn globally visible to walkers
5844 ASSERT(connp->conn_ref == 1);
5845 mutex_enter(&connp->conn_lock);
5846 connp->conn_state_flags &= ~CONN_INCIPIENT;
5847 mutex_exit(&connp->conn_lock);
5849 qprocson(q);
5851 return (0);
5855 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
5856 * all of them are copied to the conn_t. If the req is "zero", the policy is
5857 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
5858 * fields.
5859 * We keep only the latest setting of the policy and thus policy setting
5860 * is not incremental/cumulative.
5862 * Requests to set policies with multiple alternative actions will
5863 * go through a different API.
5866 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
5868 uint_t ah_req = 0;
5869 uint_t esp_req = 0;
5870 uint_t se_req = 0;
5871 ipsec_act_t *actp = NULL;
5872 uint_t nact;
5873 ipsec_policy_head_t *ph;
5874 boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
5875 int error = 0;
5876 netstack_t *ns = connp->conn_netstack;
5877 ip_stack_t *ipst = ns->netstack_ip;
5878 ipsec_stack_t *ipss = ns->netstack_ipsec;
5880 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
5883 * The IP_SEC_OPT option does not allow variable length parameters,
5884 * hence a request cannot be NULL.
5886 if (req == NULL)
5887 return (EINVAL);
5889 ah_req = req->ipsr_ah_req;
5890 esp_req = req->ipsr_esp_req;
5891 se_req = req->ipsr_self_encap_req;
5893 /* Don't allow setting self-encap without one or more of AH/ESP. */
5894 if (se_req != 0 && esp_req == 0 && ah_req == 0)
5895 return (EINVAL);
5898 * Are we dealing with a request to reset the policy (i.e.
5899 * zero requests).
5901 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
5902 (esp_req & REQ_MASK) == 0 &&
5903 (se_req & REQ_MASK) == 0);
5905 if (!is_pol_reset) {
5907 * If we couldn't load IPsec, fail with "protocol
5908 * not supported".
5909 * IPsec may not have been loaded for a request with zero
5910 * policies, so we don't fail in this case.
5912 mutex_enter(&ipss->ipsec_loader_lock);
5913 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
5914 mutex_exit(&ipss->ipsec_loader_lock);
5915 return (EPROTONOSUPPORT);
5917 mutex_exit(&ipss->ipsec_loader_lock);
5920 * Test for valid requests. Invalid algorithms
5921 * need to be tested by IPsec code because new
5922 * algorithms can be added dynamically.
5924 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
5925 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
5926 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
5927 return (EINVAL);
5931 * Only privileged users can issue these
5932 * requests.
5934 if (((ah_req & IPSEC_PREF_NEVER) ||
5935 (esp_req & IPSEC_PREF_NEVER) ||
5936 (se_req & IPSEC_PREF_NEVER)) &&
5937 secpolicy_ip_config(cr, B_FALSE) != 0) {
5938 return (EPERM);
5942 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
5943 * are mutually exclusive.
5945 if (((ah_req & REQ_MASK) == REQ_MASK) ||
5946 ((esp_req & REQ_MASK) == REQ_MASK) ||
5947 ((se_req & REQ_MASK) == REQ_MASK)) {
5948 /* Both of them are set */
5949 return (EINVAL);
5953 ASSERT(MUTEX_HELD(&connp->conn_lock));
5956 * If we have already cached policies in conn_connect(), don't
5957 * let them change now. We cache policies for connections
5958 * whose src,dst [addr, port] is known.
5960 if (connp->conn_policy_cached) {
5961 return (EINVAL);
5965 * We have a zero policies, reset the connection policy if already
5966 * set. This will cause the connection to inherit the
5967 * global policy, if any.
5969 if (is_pol_reset) {
5970 if (connp->conn_policy != NULL) {
5971 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
5972 connp->conn_policy = NULL;
5974 connp->conn_in_enforce_policy = B_FALSE;
5975 connp->conn_out_enforce_policy = B_FALSE;
5976 return (0);
5979 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
5980 ipst->ips_netstack);
5981 if (ph == NULL)
5982 goto enomem;
5984 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
5985 if (actp == NULL)
5986 goto enomem;
5989 * Always insert IPv4 policy entries, since they can also apply to
5990 * ipv6 sockets being used in ipv4-compat mode.
5992 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
5993 IPSEC_TYPE_INBOUND, ns))
5994 goto enomem;
5995 is_pol_inserted = B_TRUE;
5996 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
5997 IPSEC_TYPE_OUTBOUND, ns))
5998 goto enomem;
6001 * We're looking at a v6 socket, also insert the v6-specific
6002 * entries.
6004 if (connp->conn_family == AF_INET6) {
6005 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6006 IPSEC_TYPE_INBOUND, ns))
6007 goto enomem;
6008 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6009 IPSEC_TYPE_OUTBOUND, ns))
6010 goto enomem;
6013 ipsec_actvec_free(actp, nact);
6016 * If the requests need security, set enforce_policy.
6017 * If the requests are IPSEC_PREF_NEVER, one should
6018 * still set conn_out_enforce_policy so that ip_set_destination
6019 * marks the ip_xmit_attr_t appropriatly. This is needed so that
6020 * for connections that we don't cache policy in at connect time,
6021 * if global policy matches in ip_output_attach_policy, we
6022 * don't wrongly inherit global policy. Similarly, we need
6023 * to set conn_in_enforce_policy also so that we don't verify
6024 * policy wrongly.
6026 if ((ah_req & REQ_MASK) != 0 ||
6027 (esp_req & REQ_MASK) != 0 ||
6028 (se_req & REQ_MASK) != 0) {
6029 connp->conn_in_enforce_policy = B_TRUE;
6030 connp->conn_out_enforce_policy = B_TRUE;
6033 return (error);
6034 #undef REQ_MASK
6037 * Common memory-allocation-failure exit path.
6039 enomem:
6040 if (actp != NULL)
6041 ipsec_actvec_free(actp, nact);
6042 if (is_pol_inserted)
6043 ipsec_polhead_flush(ph, ns);
6044 return (ENOMEM);
6048 * Set socket options for joining and leaving multicast groups.
6049 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6050 * The caller has already check that the option name is consistent with
6051 * the address family of the socket.
6054 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6055 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6057 int *i1 = (int *)invalp;
6058 int error = 0;
6059 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6060 struct ip_mreq *v4_mreqp;
6061 struct ipv6_mreq *v6_mreqp;
6062 struct group_req *greqp;
6063 ire_t *ire;
6064 boolean_t done = B_FALSE;
6065 ipaddr_t ifaddr;
6066 in6_addr_t v6group;
6067 uint_t ifindex;
6068 boolean_t mcast_opt = B_TRUE;
6069 mcast_record_t fmode;
6070 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6071 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6073 switch (name) {
6074 case IP_ADD_MEMBERSHIP:
6075 case IPV6_JOIN_GROUP:
6076 mcast_opt = B_FALSE;
6077 /* FALLTHROUGH */
6078 case MCAST_JOIN_GROUP:
6079 fmode = MODE_IS_EXCLUDE;
6080 optfn = ip_opt_add_group;
6081 break;
6083 case IP_DROP_MEMBERSHIP:
6084 case IPV6_LEAVE_GROUP:
6085 mcast_opt = B_FALSE;
6086 /* FALLTHROUGH */
6087 case MCAST_LEAVE_GROUP:
6088 fmode = MODE_IS_INCLUDE;
6089 optfn = ip_opt_delete_group;
6090 break;
6091 default:
6092 ASSERT(0);
6095 if (mcast_opt) {
6096 struct sockaddr_in *sin;
6097 struct sockaddr_in6 *sin6;
6099 greqp = (struct group_req *)i1;
6100 if (greqp->gr_group.ss_family == AF_INET) {
6101 sin = (struct sockaddr_in *)&(greqp->gr_group);
6102 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6103 } else {
6104 if (!inet6)
6105 return (EINVAL); /* Not on INET socket */
6107 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6108 v6group = sin6->sin6_addr;
6110 ifaddr = INADDR_ANY;
6111 ifindex = greqp->gr_interface;
6112 } else if (inet6) {
6113 v6_mreqp = (struct ipv6_mreq *)i1;
6114 v6group = v6_mreqp->ipv6mr_multiaddr;
6115 ifaddr = INADDR_ANY;
6116 ifindex = v6_mreqp->ipv6mr_interface;
6117 } else {
6118 v4_mreqp = (struct ip_mreq *)i1;
6119 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6120 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6121 ifindex = 0;
6125 * In the multirouting case, we need to replicate
6126 * the request on all interfaces that will take part
6127 * in replication. We do so because multirouting is
6128 * reflective, thus we will probably receive multi-
6129 * casts on those interfaces.
6131 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6132 ipaddr_t group;
6134 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6136 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6137 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6138 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6139 } else {
6140 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6141 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6142 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6144 if (ire != NULL)
6145 ire_refrele(ire);
6147 if (!done) {
6148 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6149 fmode, &ipv6_all_zeros);
6151 return (error);
6155 * Set socket options for joining and leaving multicast groups
6156 * for specific sources.
6157 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6158 * The caller has already check that the option name is consistent with
6159 * the address family of the socket.
6162 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6163 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6165 int *i1 = (int *)invalp;
6166 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6167 struct ip_mreq_source *imreqp;
6168 struct group_source_req *gsreqp;
6169 in6_addr_t v6group, v6src;
6170 uint32_t ifindex;
6171 ipaddr_t ifaddr;
6172 boolean_t mcast_opt = B_TRUE;
6173 mcast_record_t fmode;
6174 ire_t *ire;
6175 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6176 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6178 switch (name) {
6179 case IP_BLOCK_SOURCE:
6180 mcast_opt = B_FALSE;
6181 /* FALLTHROUGH */
6182 case MCAST_BLOCK_SOURCE:
6183 fmode = MODE_IS_EXCLUDE;
6184 optfn = ip_opt_add_group;
6185 break;
6187 case IP_UNBLOCK_SOURCE:
6188 mcast_opt = B_FALSE;
6189 /* FALLTHROUGH */
6190 case MCAST_UNBLOCK_SOURCE:
6191 fmode = MODE_IS_EXCLUDE;
6192 optfn = ip_opt_delete_group;
6193 break;
6195 case IP_ADD_SOURCE_MEMBERSHIP:
6196 mcast_opt = B_FALSE;
6197 /* FALLTHROUGH */
6198 case MCAST_JOIN_SOURCE_GROUP:
6199 fmode = MODE_IS_INCLUDE;
6200 optfn = ip_opt_add_group;
6201 break;
6203 case IP_DROP_SOURCE_MEMBERSHIP:
6204 mcast_opt = B_FALSE;
6205 /* FALLTHROUGH */
6206 case MCAST_LEAVE_SOURCE_GROUP:
6207 fmode = MODE_IS_INCLUDE;
6208 optfn = ip_opt_delete_group;
6209 break;
6210 default:
6211 ASSERT(0);
6214 if (mcast_opt) {
6215 gsreqp = (struct group_source_req *)i1;
6216 ifindex = gsreqp->gsr_interface;
6217 if (gsreqp->gsr_group.ss_family == AF_INET) {
6218 struct sockaddr_in *s;
6219 s = (struct sockaddr_in *)&gsreqp->gsr_group;
6220 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6221 s = (struct sockaddr_in *)&gsreqp->gsr_source;
6222 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6223 } else {
6224 struct sockaddr_in6 *s6;
6226 if (!inet6)
6227 return (EINVAL); /* Not on INET socket */
6229 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6230 v6group = s6->sin6_addr;
6231 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6232 v6src = s6->sin6_addr;
6234 ifaddr = INADDR_ANY;
6235 } else {
6236 imreqp = (struct ip_mreq_source *)i1;
6237 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6238 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6239 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6240 ifindex = 0;
6244 * Handle src being mapped INADDR_ANY by changing it to unspecified.
6246 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6247 v6src = ipv6_all_zeros;
6250 * In the multirouting case, we need to replicate
6251 * the request as noted in the mcast cases above.
6253 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6254 ipaddr_t group;
6256 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6258 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6259 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6260 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6261 } else {
6262 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6263 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6264 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6266 if (ire != NULL)
6267 ire_refrele(ire);
6268 return (optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6269 fmode, &v6src));
6273 * Given a destination address and a pointer to where to put the information
6274 * this routine fills in the mtuinfo.
6275 * The socket must be connected.
6276 * For sctp conn_faddr is the primary address.
6279 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6281 uint32_t pmtu = IP_MAXPACKET;
6282 uint_t scopeid;
6284 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6285 return (-1);
6287 /* In case we never sent or called ip_set_destination_v4/v6 */
6288 if (ixa->ixa_ire != NULL)
6289 pmtu = ip_get_pmtu(ixa);
6291 if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6292 scopeid = ixa->ixa_scopeid;
6293 else
6294 scopeid = 0;
6296 bzero(mtuinfo, sizeof (*mtuinfo));
6297 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6298 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6299 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6300 mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6301 mtuinfo->ip6m_mtu = pmtu;
6303 return (sizeof (struct ip6_mtuinfo));
6307 * When the src multihoming is changed from weak to [strong, preferred]
6308 * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6309 * and identify routes that were created by user-applications in the
6310 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6311 * currently defined. These routes are then 'rebound', i.e., their ire_ill
6312 * is selected by finding an interface route for the gateway.
6314 /* ARGSUSED */
6315 void
6316 ip_ire_rebind_walker(ire_t *ire, void *notused)
6318 if (!ire->ire_unbound || ire->ire_ill != NULL)
6319 return;
6320 ire_rebind(ire);
6321 ire_delete(ire);
6325 * When the src multihoming is changed from [strong, preferred] to weak,
6326 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6327 * set any entries that were created by user-applications in the unbound state
6328 * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6330 /* ARGSUSED */
6331 void
6332 ip_ire_unbind_walker(ire_t *ire, void *notused)
6334 ire_t *new_ire;
6336 if (!ire->ire_unbound || ire->ire_ill == NULL)
6337 return;
6338 if (ire->ire_ipversion == IPV6_VERSION) {
6339 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6340 &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6341 ire->ire_zoneid, ire->ire_flags, ire->ire_ipst);
6342 } else {
6343 new_ire = ire_create((uchar_t *)&ire->ire_addr,
6344 (uchar_t *)&ire->ire_mask,
6345 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6346 ire->ire_zoneid, ire->ire_flags, ire->ire_ipst);
6348 if (new_ire == NULL)
6349 return;
6350 new_ire->ire_unbound = B_TRUE;
6352 * The bound ire must first be deleted so that we don't return
6353 * the existing one on the attempt to add the unbound new_ire.
6355 ire_delete(ire);
6356 new_ire = ire_add(new_ire);
6357 if (new_ire != NULL)
6358 ire_refrele(new_ire);
6362 * When the settings of ip*_strict_src_multihoming tunables are changed,
6363 * all cached routes need to be recomputed. This recomputation needs to be
6364 * done when going from weaker to stronger modes so that the cached ire
6365 * for the connection does not violate the current ip*_strict_src_multihoming
6366 * setting. It also needs to be done when going from stronger to weaker modes,
6367 * so that we fall back to matching on the longest-matching-route (as opposed
6368 * to a shorter match that may have been selected in the strong mode
6369 * to satisfy src_multihoming settings).
6371 * The cached ixa_ire entires for all conn_t entries are marked as
6372 * "verify" so that they will be recomputed for the next packet.
6374 void
6375 conn_ire_revalidate(conn_t *connp, void *arg)
6377 boolean_t isv6 = (boolean_t)arg;
6379 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6380 (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6381 return;
6382 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6386 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6387 * When an ipf is passed here for the first time, if
6388 * we already have in-order fragments on the queue, we convert from the fast-
6389 * path reassembly scheme to the hard-case scheme. From then on, additional
6390 * fragments are reassembled here. We keep track of the start and end offsets
6391 * of each piece, and the number of holes in the chain. When the hole count
6392 * goes to zero, we are done!
6394 * The ipf_count will be updated to account for any mblk(s) added (pointed to
6395 * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6396 * ipfb_count and ill_frag_count by the difference of ipf_count before and
6397 * after the call to ip_reassemble().
6400 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6401 size_t msg_len)
6403 uint_t end;
6404 mblk_t *next_mp;
6405 mblk_t *mp1;
6406 uint_t offset;
6407 boolean_t incr_dups = B_TRUE;
6408 boolean_t offset_zero_seen = B_FALSE;
6409 boolean_t pkt_boundary_checked = B_FALSE;
6411 /* If start == 0 then ipf_nf_hdr_len has to be set. */
6412 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6414 /* Add in byte count */
6415 ipf->ipf_count += msg_len;
6416 if (ipf->ipf_end) {
6418 * We were part way through in-order reassembly, but now there
6419 * is a hole. We walk through messages already queued, and
6420 * mark them for hard case reassembly. We know that up till
6421 * now they were in order starting from offset zero.
6423 offset = 0;
6424 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6425 IP_REASS_SET_START(mp1, offset);
6426 if (offset == 0) {
6427 ASSERT(ipf->ipf_nf_hdr_len != 0);
6428 offset = -ipf->ipf_nf_hdr_len;
6430 offset += mp1->b_wptr - mp1->b_rptr;
6431 IP_REASS_SET_END(mp1, offset);
6433 /* One hole at the end. */
6434 ipf->ipf_hole_cnt = 1;
6435 /* Brand it as a hard case, forever. */
6436 ipf->ipf_end = 0;
6438 /* Walk through all the new pieces. */
6439 do {
6440 end = start + (mp->b_wptr - mp->b_rptr);
6442 * If start is 0, decrease 'end' only for the first mblk of
6443 * the fragment. Otherwise 'end' can get wrong value in the
6444 * second pass of the loop if first mblk is exactly the
6445 * size of ipf_nf_hdr_len.
6447 if (start == 0 && !offset_zero_seen) {
6448 /* First segment */
6449 ASSERT(ipf->ipf_nf_hdr_len != 0);
6450 end -= ipf->ipf_nf_hdr_len;
6451 offset_zero_seen = B_TRUE;
6453 next_mp = mp->b_cont;
6455 * We are checking to see if there is any interesing data
6456 * to process. If there isn't and the mblk isn't the
6457 * one which carries the unfragmentable header then we
6458 * drop it. It's possible to have just the unfragmentable
6459 * header come through without any data. That needs to be
6460 * saved.
6462 * If the assert at the top of this function holds then the
6463 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code
6464 * is infrequently traveled enough that the test is left in
6465 * to protect against future code changes which break that
6466 * invariant.
6468 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6469 /* Empty. Blast it. */
6470 IP_REASS_SET_START(mp, 0);
6471 IP_REASS_SET_END(mp, 0);
6473 * If the ipf points to the mblk we are about to free,
6474 * update ipf to point to the next mblk (or NULL
6475 * if none).
6477 if (ipf->ipf_mp->b_cont == mp)
6478 ipf->ipf_mp->b_cont = next_mp;
6479 freeb(mp);
6480 continue;
6482 mp->b_cont = NULL;
6483 IP_REASS_SET_START(mp, start);
6484 IP_REASS_SET_END(mp, end);
6485 if (!ipf->ipf_tail_mp) {
6486 ipf->ipf_tail_mp = mp;
6487 ipf->ipf_mp->b_cont = mp;
6488 if (start == 0 || !more) {
6489 ipf->ipf_hole_cnt = 1;
6491 * if the first fragment comes in more than one
6492 * mblk, this loop will be executed for each
6493 * mblk. Need to adjust hole count so exiting
6494 * this routine will leave hole count at 1.
6496 if (next_mp)
6497 ipf->ipf_hole_cnt++;
6498 } else
6499 ipf->ipf_hole_cnt = 2;
6500 continue;
6501 } else if (ipf->ipf_last_frag_seen && !more &&
6502 !pkt_boundary_checked) {
6504 * We check datagram boundary only if this fragment
6505 * claims to be the last fragment and we have seen a
6506 * last fragment in the past too. We do this only
6507 * once for a given fragment.
6509 * start cannot be 0 here as fragments with start=0
6510 * and MF=0 gets handled as a complete packet. These
6511 * fragments should not reach here.
6514 if (start + msgdsize(mp) !=
6515 IP_REASS_END(ipf->ipf_tail_mp)) {
6517 * We have two fragments both of which claim
6518 * to be the last fragment but gives conflicting
6519 * information about the whole datagram size.
6520 * Something fishy is going on. Drop the
6521 * fragment and free up the reassembly list.
6523 return (IP_REASS_FAILED);
6527 * We shouldn't come to this code block again for this
6528 * particular fragment.
6530 pkt_boundary_checked = B_TRUE;
6533 /* New stuff at or beyond tail? */
6534 offset = IP_REASS_END(ipf->ipf_tail_mp);
6535 if (start >= offset) {
6536 if (ipf->ipf_last_frag_seen) {
6537 /* current fragment is beyond last fragment */
6538 return (IP_REASS_FAILED);
6540 /* Link it on end. */
6541 ipf->ipf_tail_mp->b_cont = mp;
6542 ipf->ipf_tail_mp = mp;
6543 if (more) {
6544 if (start != offset)
6545 ipf->ipf_hole_cnt++;
6546 } else if (start == offset && next_mp == NULL)
6547 ipf->ipf_hole_cnt--;
6548 continue;
6550 mp1 = ipf->ipf_mp->b_cont;
6551 offset = IP_REASS_START(mp1);
6552 /* New stuff at the front? */
6553 if (start < offset) {
6554 if (start == 0) {
6555 if (end >= offset) {
6556 /* Nailed the hole at the begining. */
6557 ipf->ipf_hole_cnt--;
6559 } else if (end < offset) {
6561 * A hole, stuff, and a hole where there used
6562 * to be just a hole.
6564 ipf->ipf_hole_cnt++;
6566 mp->b_cont = mp1;
6567 /* Check for overlap. */
6568 while (end > offset) {
6569 if (end < IP_REASS_END(mp1)) {
6570 mp->b_wptr -= end - offset;
6571 IP_REASS_SET_END(mp, offset);
6572 BUMP_MIB(ill->ill_ip_mib,
6573 ipIfStatsReasmPartDups);
6574 break;
6576 /* Did we cover another hole? */
6577 if ((mp1->b_cont &&
6578 IP_REASS_END(mp1) !=
6579 IP_REASS_START(mp1->b_cont) &&
6580 end >= IP_REASS_START(mp1->b_cont)) ||
6581 (!ipf->ipf_last_frag_seen && !more)) {
6582 ipf->ipf_hole_cnt--;
6584 /* Clip out mp1. */
6585 if ((mp->b_cont = mp1->b_cont) == NULL) {
6587 * After clipping out mp1, this guy
6588 * is now hanging off the end.
6590 ipf->ipf_tail_mp = mp;
6592 IP_REASS_SET_START(mp1, 0);
6593 IP_REASS_SET_END(mp1, 0);
6594 /* Subtract byte count */
6595 ipf->ipf_count -= mp1->b_datap->db_lim -
6596 mp1->b_datap->db_base;
6597 freeb(mp1);
6598 BUMP_MIB(ill->ill_ip_mib,
6599 ipIfStatsReasmPartDups);
6600 mp1 = mp->b_cont;
6601 if (!mp1)
6602 break;
6603 offset = IP_REASS_START(mp1);
6605 ipf->ipf_mp->b_cont = mp;
6606 continue;
6609 * The new piece starts somewhere between the start of the head
6610 * and before the end of the tail.
6612 for (; mp1; mp1 = mp1->b_cont) {
6613 offset = IP_REASS_END(mp1);
6614 if (start < offset) {
6615 if (end <= offset) {
6616 /* Nothing new. */
6617 IP_REASS_SET_START(mp, 0);
6618 IP_REASS_SET_END(mp, 0);
6619 /* Subtract byte count */
6620 ipf->ipf_count -= mp->b_datap->db_lim -
6621 mp->b_datap->db_base;
6622 if (incr_dups) {
6623 ipf->ipf_num_dups++;
6624 incr_dups = B_FALSE;
6626 freeb(mp);
6627 BUMP_MIB(ill->ill_ip_mib,
6628 ipIfStatsReasmDuplicates);
6629 break;
6632 * Trim redundant stuff off beginning of new
6633 * piece.
6635 IP_REASS_SET_START(mp, offset);
6636 mp->b_rptr += offset - start;
6637 BUMP_MIB(ill->ill_ip_mib,
6638 ipIfStatsReasmPartDups);
6639 start = offset;
6640 if (!mp1->b_cont) {
6642 * After trimming, this guy is now
6643 * hanging off the end.
6645 mp1->b_cont = mp;
6646 ipf->ipf_tail_mp = mp;
6647 if (!more) {
6648 ipf->ipf_hole_cnt--;
6650 break;
6653 if (start >= IP_REASS_START(mp1->b_cont))
6654 continue;
6655 /* Fill a hole */
6656 if (start > offset)
6657 ipf->ipf_hole_cnt++;
6658 mp->b_cont = mp1->b_cont;
6659 mp1->b_cont = mp;
6660 mp1 = mp->b_cont;
6661 offset = IP_REASS_START(mp1);
6662 if (end >= offset) {
6663 ipf->ipf_hole_cnt--;
6664 /* Check for overlap. */
6665 while (end > offset) {
6666 if (end < IP_REASS_END(mp1)) {
6667 mp->b_wptr -= end - offset;
6668 IP_REASS_SET_END(mp, offset);
6670 * TODO we might bump
6671 * this up twice if there is
6672 * overlap at both ends.
6674 BUMP_MIB(ill->ill_ip_mib,
6675 ipIfStatsReasmPartDups);
6676 break;
6678 /* Did we cover another hole? */
6679 if ((mp1->b_cont &&
6680 IP_REASS_END(mp1)
6681 != IP_REASS_START(mp1->b_cont) &&
6682 end >=
6683 IP_REASS_START(mp1->b_cont)) ||
6684 (!ipf->ipf_last_frag_seen &&
6685 !more)) {
6686 ipf->ipf_hole_cnt--;
6688 /* Clip out mp1. */
6689 if ((mp->b_cont = mp1->b_cont) ==
6690 NULL) {
6692 * After clipping out mp1,
6693 * this guy is now hanging
6694 * off the end.
6696 ipf->ipf_tail_mp = mp;
6698 IP_REASS_SET_START(mp1, 0);
6699 IP_REASS_SET_END(mp1, 0);
6700 /* Subtract byte count */
6701 ipf->ipf_count -=
6702 mp1->b_datap->db_lim -
6703 mp1->b_datap->db_base;
6704 freeb(mp1);
6705 BUMP_MIB(ill->ill_ip_mib,
6706 ipIfStatsReasmPartDups);
6707 mp1 = mp->b_cont;
6708 if (!mp1)
6709 break;
6710 offset = IP_REASS_START(mp1);
6713 break;
6715 } while (start = end, mp = next_mp);
6717 /* Fragment just processed could be the last one. Remember this fact */
6718 if (!more)
6719 ipf->ipf_last_frag_seen = B_TRUE;
6721 /* Still got holes? */
6722 if (ipf->ipf_hole_cnt)
6723 return (IP_REASS_PARTIAL);
6724 /* Clean up overloaded fields to avoid upstream disasters. */
6725 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6726 IP_REASS_SET_START(mp1, 0);
6727 IP_REASS_SET_END(mp1, 0);
6729 return (IP_REASS_COMPLETE);
6733 * Fragmentation reassembly. Each ILL has a hash table for
6734 * queuing packets undergoing reassembly for all IPIFs
6735 * associated with the ILL. The hash is based on the packet
6736 * IP ident field. The ILL frag hash table was allocated
6737 * as a timer block at the time the ILL was created. Whenever
6738 * there is anything on the reassembly queue, the timer will
6739 * be running. Returns the reassembled packet if reassembly completes.
6741 mblk_t *
6742 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
6744 uint32_t frag_offset_flags;
6745 mblk_t *t_mp;
6746 ipaddr_t dst;
6747 uint8_t proto = ipha->ipha_protocol;
6748 uint32_t sum_val;
6749 uint16_t sum_flags;
6750 ipf_t *ipf;
6751 ipf_t **ipfp;
6752 ipfb_t *ipfb;
6753 uint16_t ident;
6754 uint32_t offset;
6755 ipaddr_t src;
6756 uint_t hdr_length;
6757 uint32_t end;
6758 mblk_t *mp1;
6759 mblk_t *tail_mp;
6760 size_t count;
6761 size_t msg_len;
6762 uint8_t ecn_info = 0;
6763 uint32_t packet_size;
6764 boolean_t pruned = B_FALSE;
6765 ill_t *ill = ira->ira_ill;
6766 ip_stack_t *ipst = ill->ill_ipst;
6769 * Drop the fragmented as early as possible, if
6770 * we don't have resource(s) to re-assemble.
6772 if (ipst->ips_ip_reass_queue_bytes == 0) {
6773 freemsg(mp);
6774 return (NULL);
6777 /* Check for fragmentation offset; return if there's none */
6778 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
6779 (IPH_MF | IPH_OFFSET)) == 0)
6780 return (mp);
6783 * We utilize hardware computed checksum info only for UDP since
6784 * IP fragmentation is a normal occurrence for the protocol. In
6785 * addition, checksum offload support for IP fragments carrying
6786 * UDP payload is commonly implemented across network adapters.
6788 ASSERT(ira->ira_rill != NULL);
6789 if (proto == IPPROTO_UDP && dohwcksum &&
6790 ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
6791 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
6792 mblk_t *mp1 = mp->b_cont;
6793 int32_t len;
6795 /* Record checksum information from the packet */
6796 sum_val = (uint32_t)DB_CKSUM16(mp);
6797 sum_flags = DB_CKSUMFLAGS(mp);
6799 /* IP payload offset from beginning of mblk */
6800 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
6802 if ((sum_flags & HCK_PARTIALCKSUM) &&
6803 (mp1 == NULL || mp1->b_cont == NULL) &&
6804 offset >= DB_CKSUMSTART(mp) &&
6805 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
6806 uint32_t adj;
6808 * Partial checksum has been calculated by hardware
6809 * and attached to the packet; in addition, any
6810 * prepended extraneous data is even byte aligned.
6811 * If any such data exists, we adjust the checksum;
6812 * this would also handle any postpended data.
6814 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
6815 mp, mp1, len, adj);
6817 /* One's complement subtract extraneous checksum */
6818 if (adj >= sum_val)
6819 sum_val = ~(adj - sum_val) & 0xFFFF;
6820 else
6821 sum_val -= adj;
6823 } else {
6824 sum_val = 0;
6825 sum_flags = 0;
6828 /* Clear hardware checksumming flag */
6829 DB_CKSUMFLAGS(mp) = 0;
6831 ident = ipha->ipha_ident;
6832 offset = (frag_offset_flags << 3) & 0xFFFF;
6833 src = ipha->ipha_src;
6834 dst = ipha->ipha_dst;
6835 hdr_length = IPH_HDR_LENGTH(ipha);
6836 end = ntohs(ipha->ipha_length) - hdr_length;
6838 /* If end == 0 then we have a packet with no data, so just free it */
6839 if (end == 0) {
6840 freemsg(mp);
6841 return (NULL);
6844 /* Record the ECN field info. */
6845 ecn_info = (ipha->ipha_type_of_service & 0x3);
6846 if (offset != 0) {
6848 * If this isn't the first piece, strip the header, and
6849 * add the offset to the end value.
6851 mp->b_rptr += hdr_length;
6852 end += offset;
6855 /* Handle vnic loopback of fragments */
6856 if (mp->b_datap->db_ref > 2)
6857 msg_len = 0;
6858 else
6859 msg_len = MBLKSIZE(mp);
6861 tail_mp = mp;
6862 while (tail_mp->b_cont != NULL) {
6863 tail_mp = tail_mp->b_cont;
6864 if (tail_mp->b_datap->db_ref <= 2)
6865 msg_len += MBLKSIZE(tail_mp);
6868 /* If the reassembly list for this ILL will get too big, prune it */
6869 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
6870 ipst->ips_ip_reass_queue_bytes) {
6871 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
6872 uint_t, ill->ill_frag_count,
6873 uint_t, ipst->ips_ip_reass_queue_bytes);
6874 ill_frag_prune(ill,
6875 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
6876 (ipst->ips_ip_reass_queue_bytes - msg_len));
6877 pruned = B_TRUE;
6880 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
6881 mutex_enter(&ipfb->ipfb_lock);
6883 ipfp = &ipfb->ipfb_ipf;
6884 /* Try to find an existing fragment queue for this packet. */
6885 for (;;) {
6886 ipf = ipfp[0];
6887 if (ipf != NULL) {
6889 * It has to match on ident and src/dst address.
6891 if (ipf->ipf_ident == ident &&
6892 ipf->ipf_src == src &&
6893 ipf->ipf_dst == dst &&
6894 ipf->ipf_protocol == proto) {
6896 * If we have received too many
6897 * duplicate fragments for this packet
6898 * free it.
6900 if (ipf->ipf_num_dups > ip_max_frag_dups) {
6901 ill_frag_free_pkts(ill, ipfb, ipf, 1);
6902 freemsg(mp);
6903 mutex_exit(&ipfb->ipfb_lock);
6904 return (NULL);
6906 /* Found it. */
6907 break;
6909 ipfp = &ipf->ipf_hash_next;
6910 continue;
6914 * If we pruned the list, do we want to store this new
6915 * fragment?. We apply an optimization here based on the
6916 * fact that most fragments will be received in order.
6917 * So if the offset of this incoming fragment is zero,
6918 * it is the first fragment of a new packet. We will
6919 * keep it. Otherwise drop the fragment, as we have
6920 * probably pruned the packet already (since the
6921 * packet cannot be found).
6923 if (pruned && offset != 0) {
6924 mutex_exit(&ipfb->ipfb_lock);
6925 freemsg(mp);
6926 return (NULL);
6929 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) {
6931 * Too many fragmented packets in this hash
6932 * bucket. Free the oldest.
6934 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
6937 /* New guy. Allocate a frag message. */
6938 mp1 = allocb(sizeof (*ipf), BPRI_MED);
6939 if (mp1 == NULL) {
6940 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
6941 ip_drop_input("ipIfStatsInDiscards", mp, ill);
6942 freemsg(mp);
6943 reass_done:
6944 mutex_exit(&ipfb->ipfb_lock);
6945 return (NULL);
6948 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
6949 mp1->b_cont = mp;
6951 /* Initialize the fragment header. */
6952 ipf = (ipf_t *)mp1->b_rptr;
6953 ipf->ipf_mp = mp1;
6954 ipf->ipf_ptphn = ipfp;
6955 ipfp[0] = ipf;
6956 ipf->ipf_hash_next = NULL;
6957 ipf->ipf_ident = ident;
6958 ipf->ipf_protocol = proto;
6959 ipf->ipf_src = src;
6960 ipf->ipf_dst = dst;
6961 ipf->ipf_nf_hdr_len = 0;
6962 /* Record reassembly start time. */
6963 ipf->ipf_timestamp = gethrestime_sec();
6964 /* Record ipf generation and account for frag header */
6965 ipf->ipf_gen = ill->ill_ipf_gen++;
6966 ipf->ipf_count = MBLKSIZE(mp1);
6967 ipf->ipf_last_frag_seen = B_FALSE;
6968 ipf->ipf_ecn = ecn_info;
6969 ipf->ipf_num_dups = 0;
6970 ipfb->ipfb_frag_pkts++;
6971 ipf->ipf_checksum = 0;
6972 ipf->ipf_checksum_flags = 0;
6974 /* Store checksum value in fragment header */
6975 if (sum_flags != 0) {
6976 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
6977 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
6978 ipf->ipf_checksum = sum_val;
6979 ipf->ipf_checksum_flags = sum_flags;
6983 * We handle reassembly two ways. In the easy case,
6984 * where all the fragments show up in order, we do
6985 * minimal bookkeeping, and just clip new pieces on
6986 * the end. If we ever see a hole, then we go off
6987 * to ip_reassemble which has to mark the pieces and
6988 * keep track of the number of holes, etc. Obviously,
6989 * the point of having both mechanisms is so we can
6990 * handle the easy case as efficiently as possible.
6992 if (offset == 0) {
6993 /* Easy case, in-order reassembly so far. */
6994 ipf->ipf_count += msg_len;
6995 ipf->ipf_tail_mp = tail_mp;
6997 * Keep track of next expected offset in
6998 * ipf_end.
7000 ipf->ipf_end = end;
7001 ipf->ipf_nf_hdr_len = hdr_length;
7002 } else {
7003 /* Hard case, hole at the beginning. */
7004 ipf->ipf_tail_mp = NULL;
7006 * ipf_end == 0 means that we have given up
7007 * on easy reassembly.
7009 ipf->ipf_end = 0;
7011 /* Forget checksum offload from now on */
7012 ipf->ipf_checksum_flags = 0;
7015 * ipf_hole_cnt is set by ip_reassemble.
7016 * ipf_count is updated by ip_reassemble.
7017 * No need to check for return value here
7018 * as we don't expect reassembly to complete
7019 * or fail for the first fragment itself.
7021 (void) ip_reassemble(mp, ipf,
7022 (frag_offset_flags & IPH_OFFSET) << 3,
7023 (frag_offset_flags & IPH_MF), ill, msg_len);
7025 /* Update per ipfb and ill byte counts */
7026 ipfb->ipfb_count += ipf->ipf_count;
7027 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7028 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7029 /* If the frag timer wasn't already going, start it. */
7030 mutex_enter(&ill->ill_lock);
7031 ill_frag_timer_start(ill);
7032 mutex_exit(&ill->ill_lock);
7033 goto reass_done;
7037 * If the packet's flag has changed (it could be coming up
7038 * from an interface different than the previous, therefore
7039 * possibly different checksum capability), then forget about
7040 * any stored checksum states. Otherwise add the value to
7041 * the existing one stored in the fragment header.
7043 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7044 sum_val += ipf->ipf_checksum;
7045 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7046 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7047 ipf->ipf_checksum = sum_val;
7048 } else if (ipf->ipf_checksum_flags != 0) {
7049 /* Forget checksum offload from now on */
7050 ipf->ipf_checksum_flags = 0;
7054 * We have a new piece of a datagram which is already being
7055 * reassembled. Update the ECN info if all IP fragments
7056 * are ECN capable. If there is one which is not, clear
7057 * all the info. If there is at least one which has CE
7058 * code point, IP needs to report that up to transport.
7060 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7061 if (ecn_info == IPH_ECN_CE)
7062 ipf->ipf_ecn = IPH_ECN_CE;
7063 } else {
7064 ipf->ipf_ecn = IPH_ECN_NECT;
7066 if (offset && ipf->ipf_end == offset) {
7067 /* The new fragment fits at the end */
7068 ipf->ipf_tail_mp->b_cont = mp;
7069 /* Update the byte count */
7070 ipf->ipf_count += msg_len;
7071 /* Update per ipfb and ill byte counts */
7072 ipfb->ipfb_count += msg_len;
7073 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7074 atomic_add_32(&ill->ill_frag_count, msg_len);
7075 if (frag_offset_flags & IPH_MF) {
7076 /* More to come. */
7077 ipf->ipf_end = end;
7078 ipf->ipf_tail_mp = tail_mp;
7079 goto reass_done;
7081 } else {
7082 /* Go do the hard cases. */
7083 int ret;
7085 if (offset == 0)
7086 ipf->ipf_nf_hdr_len = hdr_length;
7088 /* Save current byte count */
7089 count = ipf->ipf_count;
7090 ret = ip_reassemble(mp, ipf,
7091 (frag_offset_flags & IPH_OFFSET) << 3,
7092 (frag_offset_flags & IPH_MF), ill, msg_len);
7093 /* Count of bytes added and subtracted (freeb()ed) */
7094 count = ipf->ipf_count - count;
7095 if (count) {
7096 /* Update per ipfb and ill byte counts */
7097 ipfb->ipfb_count += count;
7098 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7099 atomic_add_32(&ill->ill_frag_count, count);
7101 if (ret == IP_REASS_PARTIAL) {
7102 goto reass_done;
7103 } else if (ret == IP_REASS_FAILED) {
7104 /* Reassembly failed. Free up all resources */
7105 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7106 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7107 IP_REASS_SET_START(t_mp, 0);
7108 IP_REASS_SET_END(t_mp, 0);
7110 freemsg(mp);
7111 goto reass_done;
7113 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7116 * We have completed reassembly. Unhook the frag header from
7117 * the reassembly list.
7119 * Before we free the frag header, record the ECN info
7120 * to report back to the transport.
7122 ecn_info = ipf->ipf_ecn;
7123 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7124 ipfp = ipf->ipf_ptphn;
7126 /* We need to supply these to caller */
7127 if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7128 sum_val = ipf->ipf_checksum;
7129 else
7130 sum_val = 0;
7132 mp1 = ipf->ipf_mp;
7133 count = ipf->ipf_count;
7134 ipf = ipf->ipf_hash_next;
7135 if (ipf != NULL)
7136 ipf->ipf_ptphn = ipfp;
7137 ipfp[0] = ipf;
7138 atomic_add_32(&ill->ill_frag_count, -count);
7139 ASSERT(ipfb->ipfb_count >= count);
7140 ipfb->ipfb_count -= count;
7141 ipfb->ipfb_frag_pkts--;
7142 mutex_exit(&ipfb->ipfb_lock);
7143 /* Ditch the frag header. */
7144 mp = mp1->b_cont;
7146 freeb(mp1);
7148 /* Restore original IP length in header. */
7149 packet_size = (uint32_t)msgdsize(mp);
7150 if (packet_size > IP_MAXPACKET) {
7151 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7152 ip_drop_input("Reassembled packet too large", mp, ill);
7153 freemsg(mp);
7154 return (NULL);
7157 if (DB_REF(mp) > 1) {
7158 mblk_t *mp2 = copymsg(mp);
7160 if (mp2 == NULL) {
7161 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7162 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7163 freemsg(mp);
7164 return (NULL);
7166 freemsg(mp);
7167 mp = mp2;
7169 ipha = (ipha_t *)mp->b_rptr;
7171 ipha->ipha_length = htons((uint16_t)packet_size);
7172 /* We're now complete, zip the frag state */
7173 ipha->ipha_fragment_offset_and_flags = 0;
7174 /* Record the ECN info. */
7175 ipha->ipha_type_of_service &= 0xFC;
7176 ipha->ipha_type_of_service |= ecn_info;
7178 /* Update the receive attributes */
7179 ira->ira_pktlen = packet_size;
7180 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7182 /* Reassembly is successful; set checksum information in packet */
7183 DB_CKSUM16(mp) = (uint16_t)sum_val;
7184 DB_CKSUMFLAGS(mp) = sum_flags;
7185 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7187 return (mp);
7191 * Pullup function that should be used for IP input in order to
7192 * ensure we do not loose the L2 source address; we need the l2 source
7193 * address for IP_RECVSLLA and for ndp_input.
7195 * We return either NULL or b_rptr.
7197 void *
7198 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7200 ill_t *ill = ira->ira_ill;
7202 if (ip_rput_pullups++ == 0) {
7203 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7204 "ip_pullup: %s forced us to "
7205 " pullup pkt, hdr len %ld, hdr addr %p",
7206 ill->ill_name, len, (void *)mp->b_rptr);
7208 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7209 ip_setl2src(mp, ira, ira->ira_rill);
7210 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7211 if (!pullupmsg(mp, len))
7212 return (NULL);
7213 else
7214 return (mp->b_rptr);
7218 * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7219 * When called from the ULP ira_rill will be NULL hence the caller has to
7220 * pass in the ill.
7222 /* ARGSUSED */
7223 void
7224 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7226 const uchar_t *addr;
7227 int alen;
7229 if (ira->ira_flags & IRAF_L2SRC_SET)
7230 return;
7232 ASSERT(ill != NULL);
7233 alen = ill->ill_phys_addr_length;
7234 ASSERT(alen <= sizeof (ira->ira_l2src));
7235 if (ira->ira_mhip != NULL &&
7236 (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7237 bcopy(addr, ira->ira_l2src, alen);
7238 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7239 (addr = ill->ill_phys_addr) != NULL) {
7240 bcopy(addr, ira->ira_l2src, alen);
7241 } else {
7242 bzero(ira->ira_l2src, alen);
7244 ira->ira_flags |= IRAF_L2SRC_SET;
7248 * check ip header length and align it.
7250 mblk_t *
7251 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7253 ill_t *ill = ira->ira_ill;
7254 ssize_t len;
7256 len = MBLKL(mp);
7258 if (!OK_32PTR(mp->b_rptr))
7259 IP_STAT(ill->ill_ipst, ip_notaligned);
7260 else
7261 IP_STAT(ill->ill_ipst, ip_recv_pullup);
7263 /* Guard against bogus device drivers */
7264 if (len < 0) {
7265 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7266 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7267 freemsg(mp);
7268 return (NULL);
7271 if (len == 0) {
7272 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7273 mblk_t *mp1 = mp->b_cont;
7275 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7276 ip_setl2src(mp, ira, ira->ira_rill);
7277 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7279 freeb(mp);
7280 mp = mp1;
7281 if (mp == NULL)
7282 return (NULL);
7284 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7285 return (mp);
7287 if (ip_pullup(mp, min_size, ira) == NULL) {
7288 if (msgdsize(mp) < min_size) {
7289 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7290 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7291 } else {
7292 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7293 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7295 freemsg(mp);
7296 return (NULL);
7298 return (mp);
7302 * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7304 mblk_t *
7305 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7306 uint_t min_size, ip_recv_attr_t *ira)
7308 ill_t *ill = ira->ira_ill;
7311 * Make sure we have data length consistent
7312 * with the IP header.
7314 if (mp->b_cont == NULL) {
7315 /* pkt_len is based on ipha_len, not the mblk length */
7316 if (pkt_len < min_size) {
7317 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7318 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7319 freemsg(mp);
7320 return (NULL);
7322 if (len < 0) {
7323 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7324 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7325 freemsg(mp);
7326 return (NULL);
7328 /* Drop any pad */
7329 mp->b_wptr = rptr + pkt_len;
7330 } else if ((len += msgdsize(mp->b_cont)) != 0) {
7331 ASSERT(pkt_len >= min_size);
7332 if (pkt_len < min_size) {
7333 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7334 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7335 freemsg(mp);
7336 return (NULL);
7338 if (len < 0) {
7339 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7340 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7341 freemsg(mp);
7342 return (NULL);
7344 /* Drop any pad */
7345 (void) adjmsg(mp, -len);
7347 * adjmsg may have freed an mblk from the chain, hence
7348 * invalidate any hw checksum here. This will force IP to
7349 * calculate the checksum in sw, but only for this packet.
7351 DB_CKSUMFLAGS(mp) = 0;
7352 IP_STAT(ill->ill_ipst, ip_multimblk);
7354 return (mp);
7358 * Check that the IPv4 opt_len is consistent with the packet and pullup
7359 * the options.
7361 mblk_t *
7362 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7363 ip_recv_attr_t *ira)
7365 ill_t *ill = ira->ira_ill;
7366 ssize_t len;
7368 /* Assume no IPv6 packets arrive over the IPv4 queue */
7369 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7370 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7371 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7372 ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7373 freemsg(mp);
7374 return (NULL);
7377 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7378 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7379 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7380 freemsg(mp);
7381 return (NULL);
7384 * Recompute complete header length and make sure we
7385 * have access to all of it.
7387 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7388 if (len > (mp->b_wptr - mp->b_rptr)) {
7389 if (len > pkt_len) {
7390 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7391 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7392 freemsg(mp);
7393 return (NULL);
7395 if (ip_pullup(mp, len, ira) == NULL) {
7396 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7397 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7398 freemsg(mp);
7399 return (NULL);
7402 return (mp);
7406 * Returns a new ire, or the same ire, or NULL.
7407 * If a different IRE is returned, then it is held; the caller
7408 * needs to release it.
7409 * In no case is there any hold/release on the ire argument.
7411 ire_t *
7412 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7414 ire_t *new_ire;
7415 ill_t *ire_ill;
7416 uint_t ifindex;
7417 ip_stack_t *ipst = ill->ill_ipst;
7418 boolean_t strict_check = B_FALSE;
7421 * IPMP common case: if IRE and ILL are in the same group, there's no
7422 * issue (e.g. packet received on an underlying interface matched an
7423 * IRE_LOCAL on its associated group interface).
7425 ASSERT(ire->ire_ill != NULL);
7426 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7427 return (ire);
7430 * Do another ire lookup here, using the ingress ill, to see if the
7431 * interface is in a usesrc group.
7432 * As long as the ills belong to the same group, we don't consider
7433 * them to be arriving on the wrong interface. Thus, if the switch
7434 * is doing inbound load spreading, we won't drop packets when the
7435 * ip*_strict_dst_multihoming switch is on.
7436 * We also need to check for IPIF_UNNUMBERED point2point interfaces
7437 * where the local address may not be unique. In this case we were
7438 * at the mercy of the initial ire lookup and the IRE_LOCAL it
7439 * actually returned. The new lookup, which is more specific, should
7440 * only find the IRE_LOCAL associated with the ingress ill if one
7441 * exists.
7443 if (ire->ire_ipversion == IPV4_VERSION) {
7444 if (ipst->ips_ip_strict_dst_multihoming)
7445 strict_check = B_TRUE;
7446 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7447 IRE_LOCAL, ill, ALL_ZONES,
7448 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7449 } else {
7450 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7451 if (ipst->ips_ipv6_strict_dst_multihoming)
7452 strict_check = B_TRUE;
7453 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7454 IRE_LOCAL, ill, ALL_ZONES,
7455 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7458 * If the same ire that was returned in ip_input() is found then this
7459 * is an indication that usesrc groups are in use. The packet
7460 * arrived on a different ill in the group than the one associated with
7461 * the destination address. If a different ire was found then the same
7462 * IP address must be hosted on multiple ills. This is possible with
7463 * unnumbered point2point interfaces. We switch to use this new ire in
7464 * order to have accurate interface statistics.
7466 if (new_ire != NULL) {
7467 /* Note: held in one case but not the other? Caller handles */
7468 if (new_ire != ire)
7469 return (new_ire);
7470 /* Unchanged */
7471 ire_refrele(new_ire);
7472 return (ire);
7476 * Chase pointers once and store locally.
7478 ASSERT(ire->ire_ill != NULL);
7479 ire_ill = ire->ire_ill;
7480 ifindex = ill->ill_usesrc_ifindex;
7483 * Check if it's a legal address on the 'usesrc' interface.
7484 * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7485 * can just check phyint_ifindex.
7487 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7488 return (ire);
7492 * If the ip*_strict_dst_multihoming switch is on then we can
7493 * only accept this packet if the interface is marked as routing.
7495 if (!(strict_check))
7496 return (ire);
7498 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7499 return (ire);
7501 return (NULL);
7505 * This function is used to construct a mac_header_info_s from a
7506 * DL_UNITDATA_IND message.
7507 * The address fields in the mhi structure points into the message,
7508 * thus the caller can't use those fields after freeing the message.
7510 * We determine whether the packet received is a non-unicast packet
7511 * and in doing so, determine whether or not it is broadcast vs multicast.
7512 * For it to be a broadcast packet, we must have the appropriate mblk_t
7513 * hanging off the ill_t. If this is either not present or doesn't match
7514 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7515 * to be multicast. Thus NICs that have no broadcast address (or no
7516 * capability for one, such as point to point links) cannot return as
7517 * the packet being broadcast.
7519 void
7520 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7522 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7523 mblk_t *bmp;
7524 uint_t extra_offset;
7526 bzero(mhip, sizeof (struct mac_header_info_s));
7528 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7530 if (ill->ill_sap_length < 0)
7531 extra_offset = 0;
7532 else
7533 extra_offset = ill->ill_sap_length;
7535 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7536 extra_offset;
7537 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7538 extra_offset;
7540 if (!ind->dl_group_address)
7541 return;
7543 /* Multicast or broadcast */
7544 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7546 if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7547 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7548 (bmp = ill->ill_bcast_mp) != NULL) {
7549 dl_unitdata_req_t *dlur;
7550 uint8_t *bphys_addr;
7552 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7553 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7554 extra_offset;
7556 if (bcmp(mhip->mhi_daddr, bphys_addr,
7557 ind->dl_dest_addr_length) == 0)
7558 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7563 * This function is used to construct a mac_header_info_s from a
7564 * M_DATA fastpath message from a DLPI driver.
7565 * The address fields in the mhi structure points into the message,
7566 * thus the caller can't use those fields after freeing the message.
7568 * We determine whether the packet received is a non-unicast packet
7569 * and in doing so, determine whether or not it is broadcast vs multicast.
7570 * For it to be a broadcast packet, we must have the appropriate mblk_t
7571 * hanging off the ill_t. If this is either not present or doesn't match
7572 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7573 * to be multicast. Thus NICs that have no broadcast address (or no
7574 * capability for one, such as point to point links) cannot return as
7575 * the packet being broadcast.
7577 void
7578 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7580 mblk_t *bmp;
7581 struct ether_header *pether;
7583 bzero(mhip, sizeof (struct mac_header_info_s));
7585 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7587 pether = (struct ether_header *)((char *)mp->b_rptr
7588 - sizeof (struct ether_header));
7591 * Make sure the interface is an ethernet type, since we don't
7592 * know the header format for anything but Ethernet. Also make
7593 * sure we are pointing correctly above db_base.
7595 if (ill->ill_type != IFT_ETHER)
7596 return;
7598 retry:
7599 if ((uchar_t *)pether < mp->b_datap->db_base)
7600 return;
7602 /* Is there a VLAN tag? */
7603 if (ill->ill_isv6) {
7604 if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7605 pether = (struct ether_header *)((char *)pether - 4);
7606 goto retry;
7608 } else {
7609 if (pether->ether_type != htons(ETHERTYPE_IP)) {
7610 pether = (struct ether_header *)((char *)pether - 4);
7611 goto retry;
7614 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7615 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7617 if (!(mhip->mhi_daddr[0] & 0x01))
7618 return;
7620 /* Multicast or broadcast */
7621 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7623 if ((bmp = ill->ill_bcast_mp) != NULL) {
7624 dl_unitdata_req_t *dlur;
7625 uint8_t *bphys_addr;
7626 uint_t addrlen;
7628 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7629 addrlen = dlur->dl_dest_addr_length;
7630 if (ill->ill_sap_length < 0) {
7631 bphys_addr = (uchar_t *)dlur +
7632 dlur->dl_dest_addr_offset;
7633 addrlen += ill->ill_sap_length;
7634 } else {
7635 bphys_addr = (uchar_t *)dlur +
7636 dlur->dl_dest_addr_offset +
7637 ill->ill_sap_length;
7638 addrlen -= ill->ill_sap_length;
7640 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7641 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7646 * Handle anything but M_DATA messages
7647 * We see the DL_UNITDATA_IND which are part
7648 * of the data path, and also the other messages from the driver.
7650 void
7651 ip_rput_notdata(ill_t *ill, mblk_t *mp)
7653 mblk_t *first_mp;
7654 struct iocblk *iocp;
7655 struct mac_header_info_s mhi;
7657 switch (DB_TYPE(mp)) {
7658 case M_PROTO:
7659 case M_PCPROTO: {
7660 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7661 DL_UNITDATA_IND) {
7662 /* Go handle anything other than data elsewhere. */
7663 ip_rput_dlpi(ill, mp);
7664 return;
7667 first_mp = mp;
7668 mp = first_mp->b_cont;
7669 first_mp->b_cont = NULL;
7671 if (mp == NULL) {
7672 freeb(first_mp);
7673 return;
7675 ip_dlur_to_mhi(ill, first_mp, &mhi);
7676 if (ill->ill_isv6)
7677 ip_input_v6(ill, NULL, mp, &mhi);
7678 else
7679 ip_input(ill, NULL, mp, &mhi);
7681 /* Ditch the DLPI header. */
7682 freeb(first_mp);
7683 return;
7685 case M_IOCACK:
7686 iocp = (struct iocblk *)mp->b_rptr;
7687 switch (iocp->ioc_cmd) {
7688 case DL_IOC_HDR_INFO:
7689 ill_fastpath_ack(ill, mp);
7690 return;
7691 default:
7692 putnext(ill->ill_rq, mp);
7693 return;
7695 /* FALLTHROUGH */
7696 case M_ERROR:
7697 case M_HANGUP:
7698 mutex_enter(&ill->ill_lock);
7699 if (ill->ill_state_flags & ILL_CONDEMNED) {
7700 mutex_exit(&ill->ill_lock);
7701 freemsg(mp);
7702 return;
7704 ill_refhold_locked(ill);
7705 mutex_exit(&ill->ill_lock);
7706 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7707 B_FALSE);
7708 return;
7709 case M_CTL:
7710 putnext(ill->ill_rq, mp);
7711 return;
7712 case M_IOCNAK:
7713 ip1dbg(("got iocnak "));
7714 iocp = (struct iocblk *)mp->b_rptr;
7715 switch (iocp->ioc_cmd) {
7716 case DL_IOC_HDR_INFO:
7717 ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7718 return;
7719 default:
7720 break;
7722 /* FALLTHROUGH */
7723 default:
7724 putnext(ill->ill_rq, mp);
7725 return;
7729 /* Read side put procedure. Packets coming from the wire arrive here. */
7730 void
7731 ip_rput(queue_t *q, mblk_t *mp)
7733 ill_t *ill;
7734 union DL_primitives *dl;
7736 ill = (ill_t *)q->q_ptr;
7738 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
7740 * If things are opening or closing, only accept high-priority
7741 * DLPI messages. (On open ill->ill_ipif has not yet been
7742 * created; on close, things hanging off the ill may have been
7743 * freed already.)
7745 dl = (union DL_primitives *)mp->b_rptr;
7746 if (DB_TYPE(mp) != M_PCPROTO ||
7747 dl->dl_primitive == DL_UNITDATA_IND) {
7748 inet_freemsg(mp);
7749 return;
7752 if (DB_TYPE(mp) == M_DATA) {
7753 struct mac_header_info_s mhi;
7755 ip_mdata_to_mhi(ill, mp, &mhi);
7756 ip_input(ill, NULL, mp, &mhi);
7757 } else {
7758 ip_rput_notdata(ill, mp);
7763 * Move the information to a copy.
7765 mblk_t *
7766 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
7768 mblk_t *mp1;
7769 ill_t *ill = ira->ira_ill;
7770 ip_stack_t *ipst = ill->ill_ipst;
7772 IP_STAT(ipst, ip_db_ref);
7774 /* Make sure we have ira_l2src before we loose the original mblk */
7775 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7776 ip_setl2src(mp, ira, ira->ira_rill);
7778 mp1 = copymsg(mp);
7779 if (mp1 == NULL) {
7780 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7781 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7782 freemsg(mp);
7783 return (NULL);
7785 /* preserve the hardware checksum flags and data, if present */
7786 if (DB_CKSUMFLAGS(mp) != 0) {
7787 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
7788 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
7789 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
7790 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
7791 DB_CKSUM16(mp1) = DB_CKSUM16(mp);
7793 freemsg(mp);
7794 return (mp1);
7797 static void
7798 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
7799 t_uscalar_t err)
7801 if (dl_err == DL_SYSERR) {
7802 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
7803 "%s: %s failed: DL_SYSERR (errno %u)\n",
7804 ill->ill_name, dl_primstr(prim), err);
7805 return;
7808 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
7809 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
7810 dl_errstr(dl_err));
7814 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
7815 * than DL_UNITDATA_IND messages. If we need to process this message
7816 * exclusively, we call qwriter_ip, in which case we also need to call
7817 * ill_refhold before that, since qwriter_ip does an ill_refrele.
7819 void
7820 ip_rput_dlpi(ill_t *ill, mblk_t *mp)
7822 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
7823 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
7824 queue_t *q = ill->ill_rq;
7825 t_uscalar_t prim = dloa->dl_primitive;
7826 t_uscalar_t reqprim = DL_PRIM_INVAL;
7828 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
7829 char *, dl_primstr(prim), ill_t *, ill);
7830 ip1dbg(("ip_rput_dlpi"));
7833 * If we received an ACK but didn't send a request for it, then it
7834 * can't be part of any pending operation; discard up-front.
7836 switch (prim) {
7837 case DL_ERROR_ACK:
7838 reqprim = dlea->dl_error_primitive;
7839 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
7840 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
7841 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
7842 dlea->dl_unix_errno));
7843 break;
7844 case DL_OK_ACK:
7845 reqprim = dloa->dl_correct_primitive;
7846 break;
7847 case DL_INFO_ACK:
7848 reqprim = DL_INFO_REQ;
7849 break;
7850 case DL_BIND_ACK:
7851 reqprim = DL_BIND_REQ;
7852 break;
7853 case DL_PHYS_ADDR_ACK:
7854 reqprim = DL_PHYS_ADDR_REQ;
7855 break;
7856 case DL_NOTIFY_ACK:
7857 reqprim = DL_NOTIFY_REQ;
7858 break;
7859 case DL_CAPABILITY_ACK:
7860 reqprim = DL_CAPABILITY_REQ;
7861 break;
7864 if (prim != DL_NOTIFY_IND) {
7865 if (reqprim == DL_PRIM_INVAL ||
7866 !ill_dlpi_pending(ill, reqprim)) {
7867 /* Not a DLPI message we support or expected */
7868 freemsg(mp);
7869 return;
7871 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
7872 dl_primstr(reqprim)));
7875 switch (reqprim) {
7876 case DL_UNBIND_REQ:
7878 * NOTE: we mark the unbind as complete even if we got a
7879 * DL_ERROR_ACK, since there's not much else we can do.
7881 mutex_enter(&ill->ill_lock);
7882 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
7883 cv_signal(&ill->ill_cv);
7884 mutex_exit(&ill->ill_lock);
7885 break;
7887 case DL_ENABMULTI_REQ:
7888 if (prim == DL_OK_ACK) {
7889 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
7890 ill->ill_dlpi_multicast_state = IDS_OK;
7892 break;
7896 * The message is one we're waiting for (or DL_NOTIFY_IND), but we
7897 * need to become writer to continue to process it. Because an
7898 * exclusive operation doesn't complete until replies to all queued
7899 * DLPI messages have been received, we know we're in the middle of an
7900 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
7902 * As required by qwriter_ip(), we refhold the ill; it will refrele.
7903 * Since this is on the ill stream we unconditionally bump up the
7904 * refcount without doing ILL_CAN_LOOKUP().
7906 ill_refhold(ill);
7907 if (prim == DL_NOTIFY_IND)
7908 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
7909 else
7910 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
7914 * Handling of DLPI messages that require exclusive access to the ipsq.
7916 * Need to do ipsq_pending_mp_get on ioctl completion, which could
7917 * happen here. (along with mi_copy_done)
7919 /* ARGSUSED */
7920 static void
7921 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
7923 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
7924 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
7925 int err = 0;
7926 ill_t *ill = (ill_t *)q->q_ptr;
7927 ipif_t *ipif = NULL;
7928 mblk_t *mp1 = NULL;
7929 conn_t *connp = NULL;
7930 t_uscalar_t paddrreq;
7931 mblk_t *mp_hw;
7932 boolean_t success;
7933 boolean_t ioctl_aborted = B_FALSE;
7934 boolean_t log = B_TRUE;
7936 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
7937 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
7939 ip1dbg(("ip_rput_dlpi_writer .."));
7940 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
7941 ASSERT(IAM_WRITER_ILL(ill));
7943 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
7945 * The current ioctl could have been aborted by the user and a new
7946 * ioctl to bring up another ill could have started. We could still
7947 * get a response from the driver later.
7949 if (ipif != NULL && ipif->ipif_ill != ill)
7950 ioctl_aborted = B_TRUE;
7952 switch (dloa->dl_primitive) {
7953 case DL_ERROR_ACK:
7954 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
7955 dl_primstr(dlea->dl_error_primitive)));
7957 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
7958 char *, dl_primstr(dlea->dl_error_primitive),
7959 ill_t *, ill);
7961 switch (dlea->dl_error_primitive) {
7962 case DL_DISABMULTI_REQ:
7963 ill_dlpi_done(ill, dlea->dl_error_primitive);
7964 break;
7965 case DL_PROMISCON_REQ:
7966 case DL_PROMISCOFF_REQ:
7967 case DL_UNBIND_REQ:
7968 case DL_ATTACH_REQ:
7969 case DL_INFO_REQ:
7970 ill_dlpi_done(ill, dlea->dl_error_primitive);
7971 break;
7972 case DL_NOTIFY_REQ:
7973 ill_dlpi_done(ill, DL_NOTIFY_REQ);
7974 log = B_FALSE;
7975 break;
7976 case DL_PHYS_ADDR_REQ:
7978 * For IPv6 only, there are two additional
7979 * phys_addr_req's sent to the driver to get the
7980 * IPv6 token and lla. This allows IP to acquire
7981 * the hardware address format for a given interface
7982 * without having built in knowledge of the hardware
7983 * address. ill_phys_addr_pend keeps track of the last
7984 * DL_PAR sent so we know which response we are
7985 * dealing with. ill_dlpi_done will update
7986 * ill_phys_addr_pend when it sends the next req.
7987 * We don't complete the IOCTL until all three DL_PARs
7988 * have been attempted, so set *_len to 0 and break.
7990 paddrreq = ill->ill_phys_addr_pend;
7991 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
7992 if (paddrreq == DL_IPV6_TOKEN) {
7993 ill->ill_token_length = 0;
7994 log = B_FALSE;
7995 break;
7996 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
7997 ill->ill_nd_lla_len = 0;
7998 log = B_FALSE;
7999 break;
8002 * Something went wrong with the DL_PHYS_ADDR_REQ.
8003 * We presumably have an IOCTL hanging out waiting
8004 * for completion. Find it and complete the IOCTL
8005 * with the error noted.
8006 * However, ill_dl_phys was called on an ill queue
8007 * (from SIOCSLIFNAME), thus conn_pending_ill is not
8008 * set. But the ioctl is known to be pending on ill_wq.
8010 if (!ill->ill_ifname_pending)
8011 break;
8012 ill->ill_ifname_pending = 0;
8013 if (!ioctl_aborted)
8014 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8015 if (mp1 != NULL) {
8017 * This operation (SIOCSLIFNAME) must have
8018 * happened on the ill. Assert there is no conn
8020 ASSERT(connp == NULL);
8021 q = ill->ill_wq;
8023 break;
8024 case DL_BIND_REQ:
8025 ill_dlpi_done(ill, DL_BIND_REQ);
8026 if (ill->ill_ifname_pending)
8027 break;
8028 mutex_enter(&ill->ill_lock);
8029 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8030 mutex_exit(&ill->ill_lock);
8032 * Something went wrong with the bind. We presumably
8033 * have an IOCTL hanging out waiting for completion.
8034 * Find it, take down the interface that was coming
8035 * up, and complete the IOCTL with the error noted.
8037 if (!ioctl_aborted)
8038 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8039 if (mp1 != NULL) {
8041 * This might be a result of a DL_NOTE_REPLUMB
8042 * notification. In that case, connp is NULL.
8044 if (connp != NULL)
8045 q = CONNP_TO_WQ(connp);
8047 (void) ipif_down(ipif, NULL, NULL);
8048 /* error is set below the switch */
8050 break;
8051 case DL_ENABMULTI_REQ:
8052 ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8054 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8055 ill->ill_dlpi_multicast_state = IDS_FAILED;
8056 if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8058 printf("ip: joining multicasts failed (%d)"
8059 " on %s - will use link layer "
8060 "broadcasts for multicast\n",
8061 dlea->dl_errno, ill->ill_name);
8064 * Set up for multi_bcast; We are the
8065 * writer, so ok to access ill->ill_ipif
8066 * without any lock.
8068 mutex_enter(&ill->ill_phyint->phyint_lock);
8069 ill->ill_phyint->phyint_flags |=
8070 PHYI_MULTI_BCAST;
8071 mutex_exit(&ill->ill_phyint->phyint_lock);
8074 freemsg(mp); /* Don't want to pass this up */
8075 return;
8076 case DL_CAPABILITY_REQ:
8077 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8078 "DL_CAPABILITY REQ\n"));
8079 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8080 ill->ill_dlpi_capab_state = IDCS_FAILED;
8081 ill_capability_done(ill);
8082 freemsg(mp);
8083 return;
8086 * Note the error for IOCTL completion (mp1 is set when
8087 * ready to complete ioctl). If ill_ifname_pending_err is
8088 * set, an error occured during plumbing (ill_ifname_pending),
8089 * so we want to report that error.
8091 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8092 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8093 * expected to get errack'd if the driver doesn't support
8094 * these flags (e.g. ethernet). log will be set to B_FALSE
8095 * if these error conditions are encountered.
8097 if (mp1 != NULL) {
8098 if (ill->ill_ifname_pending_err != 0) {
8099 err = ill->ill_ifname_pending_err;
8100 ill->ill_ifname_pending_err = 0;
8101 } else {
8102 err = dlea->dl_unix_errno ?
8103 dlea->dl_unix_errno : ENXIO;
8106 * If we're plumbing an interface and an error hasn't already
8107 * been saved, set ill_ifname_pending_err to the error passed
8108 * up. Ignore the error if log is B_FALSE (see comment above).
8110 } else if (log && ill->ill_ifname_pending &&
8111 ill->ill_ifname_pending_err == 0) {
8112 ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8113 dlea->dl_unix_errno : ENXIO;
8116 if (log)
8117 ip_dlpi_error(ill, dlea->dl_error_primitive,
8118 dlea->dl_errno, dlea->dl_unix_errno);
8119 break;
8120 case DL_CAPABILITY_ACK:
8121 ill_capability_ack(ill, mp);
8123 * The message has been handed off to ill_capability_ack
8124 * and must not be freed below
8126 mp = NULL;
8127 break;
8129 case DL_INFO_ACK:
8130 /* Call a routine to handle this one. */
8131 ill_dlpi_done(ill, DL_INFO_REQ);
8132 ip_ll_subnet_defaults(ill, mp);
8133 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8134 return;
8135 case DL_BIND_ACK:
8137 * We should have an IOCTL waiting on this unless
8138 * sent by ill_dl_phys, in which case just return
8140 ill_dlpi_done(ill, DL_BIND_REQ);
8142 if (ill->ill_ifname_pending) {
8143 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8144 ill_t *, ill, mblk_t *, mp);
8145 break;
8147 mutex_enter(&ill->ill_lock);
8148 ill->ill_dl_up = 1;
8149 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8150 mutex_exit(&ill->ill_lock);
8152 if (!ioctl_aborted)
8153 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8154 if (mp1 == NULL) {
8155 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8156 break;
8159 * mp1 was added by ill_dl_up(). if that is a result of
8160 * a DL_NOTE_REPLUMB notification, connp could be NULL.
8162 if (connp != NULL)
8163 q = CONNP_TO_WQ(connp);
8165 * We are exclusive. So nothing can change even after
8166 * we get the pending mp.
8168 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8169 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8170 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8173 * Now bring up the resolver; when that is complete, we'll
8174 * create IREs. Note that we intentionally mirror what
8175 * ipif_up() would have done, because we got here by way of
8176 * ill_dl_up(), which stopped ipif_up()'s processing.
8178 if (ill->ill_isv6) {
8180 * v6 interfaces.
8181 * Unlike ARP which has to do another bind
8182 * and attach, once we get here we are
8183 * done with NDP
8185 (void) ipif_resolver_up(ipif, Res_act_initial);
8186 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8187 err = ipif_up_done_v6(ipif);
8188 } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8190 * ARP and other v4 external resolvers.
8191 * Leave the pending mblk intact so that
8192 * the ioctl completes in ip_rput().
8194 if (connp != NULL)
8195 mutex_enter(&connp->conn_lock);
8196 mutex_enter(&ill->ill_lock);
8197 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8198 mutex_exit(&ill->ill_lock);
8199 if (connp != NULL)
8200 mutex_exit(&connp->conn_lock);
8201 if (success) {
8202 err = ipif_resolver_up(ipif, Res_act_initial);
8203 if (err == EINPROGRESS) {
8204 freemsg(mp);
8205 return;
8207 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8208 } else {
8209 /* The conn has started closing */
8210 err = EINTR;
8212 } else {
8214 * This one is complete. Reply to pending ioctl.
8216 (void) ipif_resolver_up(ipif, Res_act_initial);
8217 err = ipif_up_done(ipif);
8220 if ((err == 0) && (ill->ill_up_ipifs)) {
8221 err = ill_up_ipifs(ill, q, mp1);
8222 if (err == EINPROGRESS) {
8223 freemsg(mp);
8224 return;
8229 * If we have a moved ipif to bring up, and everything has
8230 * succeeded to this point, bring it up on the IPMP ill.
8231 * Otherwise, leave it down -- the admin can try to bring it
8232 * up by hand if need be.
8234 if (ill->ill_move_ipif != NULL) {
8235 if (err != 0) {
8236 ill->ill_move_ipif = NULL;
8237 } else {
8238 ipif = ill->ill_move_ipif;
8239 ill->ill_move_ipif = NULL;
8240 err = ipif_up(ipif, q, mp1);
8241 if (err == EINPROGRESS) {
8242 freemsg(mp);
8243 return;
8247 break;
8249 case DL_NOTIFY_IND: {
8250 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8251 uint_t orig_mtu, orig_mc_mtu;
8253 switch (notify->dl_notification) {
8254 case DL_NOTE_PHYS_ADDR:
8255 err = ill_set_phys_addr(ill, mp);
8256 break;
8258 case DL_NOTE_REPLUMB:
8260 * Directly return after calling ill_replumb().
8261 * Note that we should not free mp as it is reused
8262 * in the ill_replumb() function.
8264 err = ill_replumb(ill, mp);
8265 return;
8267 case DL_NOTE_FASTPATH_FLUSH:
8268 nce_flush(ill, B_FALSE);
8269 break;
8271 case DL_NOTE_SDU_SIZE:
8272 case DL_NOTE_SDU_SIZE2:
8274 * The dce and fragmentation code can cope with
8275 * this changing while packets are being sent.
8276 * When packets are sent ip_output will discover
8277 * a change.
8279 * Change the MTU size of the interface.
8281 mutex_enter(&ill->ill_lock);
8282 orig_mtu = ill->ill_mtu;
8283 orig_mc_mtu = ill->ill_mc_mtu;
8284 switch (notify->dl_notification) {
8285 case DL_NOTE_SDU_SIZE:
8286 ill->ill_current_frag =
8287 (uint_t)notify->dl_data;
8288 ill->ill_mc_mtu = (uint_t)notify->dl_data;
8289 break;
8290 case DL_NOTE_SDU_SIZE2:
8291 ill->ill_current_frag =
8292 (uint_t)notify->dl_data1;
8293 ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8294 break;
8296 if (ill->ill_current_frag > ill->ill_max_frag)
8297 ill->ill_max_frag = ill->ill_current_frag;
8299 if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8300 ill->ill_mtu = ill->ill_current_frag;
8303 * If ill_user_mtu was set (via
8304 * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8306 if (ill->ill_user_mtu != 0 &&
8307 ill->ill_user_mtu < ill->ill_mtu)
8308 ill->ill_mtu = ill->ill_user_mtu;
8310 if (ill->ill_user_mtu != 0 &&
8311 ill->ill_user_mtu < ill->ill_mc_mtu)
8312 ill->ill_mc_mtu = ill->ill_user_mtu;
8314 if (ill->ill_isv6) {
8315 if (ill->ill_mtu < IPV6_MIN_MTU)
8316 ill->ill_mtu = IPV6_MIN_MTU;
8317 if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8318 ill->ill_mc_mtu = IPV6_MIN_MTU;
8319 } else {
8320 if (ill->ill_mtu < IP_MIN_MTU)
8321 ill->ill_mtu = IP_MIN_MTU;
8322 if (ill->ill_mc_mtu < IP_MIN_MTU)
8323 ill->ill_mc_mtu = IP_MIN_MTU;
8325 } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8326 ill->ill_mc_mtu = ill->ill_mtu;
8329 mutex_exit(&ill->ill_lock);
8331 * Make sure all dce_generation checks find out
8332 * that ill_mtu/ill_mc_mtu has changed.
8334 if (orig_mtu != ill->ill_mtu ||
8335 orig_mc_mtu != ill->ill_mc_mtu) {
8336 dce_increment_all_generations(ill->ill_isv6,
8337 ill->ill_ipst);
8341 * Refresh IPMP meta-interface MTU if necessary.
8343 if (IS_UNDER_IPMP(ill))
8344 ipmp_illgrp_refresh_mtu(ill->ill_grp);
8345 break;
8347 case DL_NOTE_LINK_UP:
8348 case DL_NOTE_LINK_DOWN: {
8350 * We are writer. ill / phyint / ipsq assocs stable.
8351 * The RUNNING flag reflects the state of the link.
8353 phyint_t *phyint = ill->ill_phyint;
8354 uint64_t new_phyint_flags;
8355 boolean_t changed = B_FALSE;
8356 boolean_t went_up;
8358 went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8359 mutex_enter(&phyint->phyint_lock);
8361 new_phyint_flags = went_up ?
8362 phyint->phyint_flags | PHYI_RUNNING :
8363 phyint->phyint_flags & ~PHYI_RUNNING;
8365 if (IS_IPMP(ill)) {
8366 new_phyint_flags = went_up ?
8367 new_phyint_flags & ~PHYI_FAILED :
8368 new_phyint_flags | PHYI_FAILED;
8371 if (new_phyint_flags != phyint->phyint_flags) {
8372 phyint->phyint_flags = new_phyint_flags;
8373 changed = B_TRUE;
8375 mutex_exit(&phyint->phyint_lock);
8377 * ill_restart_dad handles the DAD restart and routing
8378 * socket notification logic.
8380 if (changed) {
8381 ill_restart_dad(phyint->phyint_illv4, went_up);
8382 ill_restart_dad(phyint->phyint_illv6, went_up);
8384 break;
8386 case DL_NOTE_PROMISC_ON_PHYS: {
8387 phyint_t *phyint = ill->ill_phyint;
8389 mutex_enter(&phyint->phyint_lock);
8390 phyint->phyint_flags |= PHYI_PROMISC;
8391 mutex_exit(&phyint->phyint_lock);
8392 break;
8394 case DL_NOTE_PROMISC_OFF_PHYS: {
8395 phyint_t *phyint = ill->ill_phyint;
8397 mutex_enter(&phyint->phyint_lock);
8398 phyint->phyint_flags &= ~PHYI_PROMISC;
8399 mutex_exit(&phyint->phyint_lock);
8400 break;
8402 case DL_NOTE_CAPAB_RENEG:
8404 * Something changed on the driver side.
8405 * It wants us to renegotiate the capabilities
8406 * on this ill. One possible cause is the aggregation
8407 * interface under us where a port got added or
8408 * went away.
8410 * If the capability negotiation is already done
8411 * or is in progress, reset the capabilities and
8412 * mark the ill's ill_capab_reneg to be B_TRUE,
8413 * so that when the ack comes back, we can start
8414 * the renegotiation process.
8416 * Note that if ill_capab_reneg is already B_TRUE
8417 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8418 * the capability resetting request has been sent
8419 * and the renegotiation has not been started yet;
8420 * nothing needs to be done in this case.
8422 ipsq_current_start(ipsq, ill->ill_ipif, 0);
8423 ill_capability_reset(ill, B_TRUE);
8424 ipsq_current_finish(ipsq);
8425 break;
8427 case DL_NOTE_ALLOWED_IPS:
8428 ill_set_allowed_ips(ill, mp);
8429 break;
8430 default:
8431 ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8432 "type 0x%x for DL_NOTIFY_IND\n",
8433 notify->dl_notification));
8434 break;
8438 * As this is an asynchronous operation, we
8439 * should not call ill_dlpi_done
8441 break;
8443 case DL_NOTIFY_ACK: {
8444 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8446 if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8447 ill->ill_note_link = 1;
8448 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8449 break;
8451 case DL_PHYS_ADDR_ACK: {
8453 * As part of plumbing the interface via SIOCSLIFNAME,
8454 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8455 * whose answers we receive here. As each answer is received,
8456 * we call ill_dlpi_done() to dispatch the next request as
8457 * we're processing the current one. Once all answers have
8458 * been received, we use ipsq_pending_mp_get() to dequeue the
8459 * outstanding IOCTL and reply to it. (Because ill_dl_phys()
8460 * is invoked from an ill queue, conn_oper_pending_ill is not
8461 * available, but we know the ioctl is pending on ill_wq.)
8463 uint_t paddrlen, paddroff;
8464 uint8_t *addr;
8466 paddrreq = ill->ill_phys_addr_pend;
8467 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8468 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8469 addr = mp->b_rptr + paddroff;
8471 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8472 if (paddrreq == DL_IPV6_TOKEN) {
8474 * bcopy to low-order bits of ill_token
8476 * XXX Temporary hack - currently, all known tokens
8477 * are 64 bits, so I'll cheat for the moment.
8479 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8480 ill->ill_token_length = paddrlen;
8481 break;
8482 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8483 ASSERT(ill->ill_nd_lla_mp == NULL);
8484 ill_set_ndmp(ill, mp, paddroff, paddrlen);
8485 mp = NULL;
8486 break;
8487 } else if (paddrreq == DL_CURR_DEST_ADDR) {
8488 ASSERT(ill->ill_dest_addr_mp == NULL);
8489 ill->ill_dest_addr_mp = mp;
8490 ill->ill_dest_addr = addr;
8491 mp = NULL;
8492 if (ill->ill_isv6) {
8493 ill_setdesttoken(ill);
8494 ipif_setdestlinklocal(ill->ill_ipif);
8496 break;
8499 ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8500 ASSERT(ill->ill_phys_addr_mp == NULL);
8501 if (!ill->ill_ifname_pending)
8502 break;
8503 ill->ill_ifname_pending = 0;
8504 if (!ioctl_aborted)
8505 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8506 if (mp1 != NULL) {
8507 ASSERT(connp == NULL);
8508 q = ill->ill_wq;
8511 * If any error acks received during the plumbing sequence,
8512 * ill_ifname_pending_err will be set. Break out and send up
8513 * the error to the pending ioctl.
8515 if (ill->ill_ifname_pending_err != 0) {
8516 err = ill->ill_ifname_pending_err;
8517 ill->ill_ifname_pending_err = 0;
8518 break;
8521 ill->ill_phys_addr_mp = mp;
8522 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8523 mp = NULL;
8526 * If paddrlen or ill_phys_addr_length is zero, the DLPI
8527 * provider doesn't support physical addresses. We check both
8528 * paddrlen and ill_phys_addr_length because sppp (PPP) does
8529 * not have physical addresses, but historically adversises a
8530 * physical address length of 0 in its DL_INFO_ACK, but 6 in
8531 * its DL_PHYS_ADDR_ACK.
8533 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8534 ill->ill_phys_addr = NULL;
8535 } else if (paddrlen != ill->ill_phys_addr_length) {
8536 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8537 paddrlen, ill->ill_phys_addr_length));
8538 err = EINVAL;
8539 break;
8542 if (ill->ill_nd_lla_mp == NULL) {
8543 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8544 err = ENOMEM;
8545 break;
8547 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8550 if (ill->ill_isv6) {
8551 ill_setdefaulttoken(ill);
8552 ipif_setlinklocal(ill->ill_ipif);
8554 break;
8556 case DL_OK_ACK:
8557 ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8558 dl_primstr((int)dloa->dl_correct_primitive),
8559 dloa->dl_correct_primitive));
8560 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8561 char *, dl_primstr(dloa->dl_correct_primitive),
8562 ill_t *, ill);
8564 switch (dloa->dl_correct_primitive) {
8565 case DL_ENABMULTI_REQ:
8566 case DL_DISABMULTI_REQ:
8567 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8568 break;
8569 case DL_PROMISCON_REQ:
8570 case DL_PROMISCOFF_REQ:
8571 case DL_UNBIND_REQ:
8572 case DL_ATTACH_REQ:
8573 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8574 break;
8576 break;
8577 default:
8578 break;
8581 freemsg(mp);
8582 if (mp1 == NULL)
8583 return;
8586 * The operation must complete without EINPROGRESS since
8587 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise,
8588 * the operation will be stuck forever inside the IPSQ.
8590 ASSERT(err != EINPROGRESS);
8592 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8593 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8594 ipif_t *, NULL);
8596 switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8597 case 0:
8598 ipsq_current_finish(ipsq);
8599 break;
8601 case SIOCSLIFNAME:
8602 case IF_UNITSEL: {
8603 ill_t *ill_other = ILL_OTHER(ill);
8606 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8607 * ill has a peer which is in an IPMP group, then place ill
8608 * into the same group. One catch: although ifconfig plumbs
8609 * the appropriate IPMP meta-interface prior to plumbing this
8610 * ill, it is possible for multiple ifconfig applications to
8611 * race (or for another application to adjust plumbing), in
8612 * which case the IPMP meta-interface we need will be missing.
8613 * If so, kick the phyint out of the group.
8615 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8616 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp;
8617 ipmp_illgrp_t *illg;
8619 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8620 if (illg == NULL)
8621 ipmp_phyint_leave_grp(ill->ill_phyint);
8622 else
8623 ipmp_ill_join_illgrp(ill, illg);
8626 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8627 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8628 else
8629 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8630 break;
8632 case SIOCLIFADDIF:
8633 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8634 break;
8636 default:
8637 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8638 break;
8643 * ip_rput_other is called by ip_rput to handle messages modifying the global
8644 * state in IP. If 'ipsq' is non-NULL, caller is writer on it.
8646 /* ARGSUSED */
8647 void
8648 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8650 ill_t *ill = q->q_ptr;
8651 struct iocblk *iocp;
8653 ip1dbg(("ip_rput_other "));
8654 if (ipsq != NULL) {
8655 ASSERT(IAM_WRITER_IPSQ(ipsq));
8656 ASSERT(ipsq->ipsq_xop ==
8657 ill->ill_phyint->phyint_ipsq->ipsq_xop);
8660 switch (mp->b_datap->db_type) {
8661 case M_ERROR:
8662 case M_HANGUP:
8664 * The device has a problem. We force the ILL down. It can
8665 * be brought up again manually using SIOCSIFFLAGS (via
8666 * ifconfig or equivalent).
8668 ASSERT(ipsq != NULL);
8669 if (mp->b_rptr < mp->b_wptr)
8670 ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8671 if (ill->ill_error == 0)
8672 ill->ill_error = ENXIO;
8673 if (!ill_down_start(q, mp))
8674 return;
8675 ipif_all_down_tail(ipsq, q, mp, NULL);
8676 break;
8677 case M_IOCNAK: {
8678 iocp = (struct iocblk *)mp->b_rptr;
8680 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8682 * If this was the first attempt, turn off the fastpath
8683 * probing.
8685 mutex_enter(&ill->ill_lock);
8686 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8687 ill->ill_dlpi_fastpath_state = IDS_FAILED;
8688 mutex_exit(&ill->ill_lock);
8690 * don't flush the nce_t entries: we use them
8691 * as an index to the ncec itself.
8693 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8694 ill->ill_name));
8695 } else {
8696 mutex_exit(&ill->ill_lock);
8698 freemsg(mp);
8699 break;
8701 default:
8702 ASSERT(0);
8703 break;
8708 * Update any source route, record route or timestamp options
8709 * When it fails it has consumed the message and BUMPed the MIB.
8711 boolean_t
8712 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8713 ip_recv_attr_t *ira)
8715 ipoptp_t opts;
8716 uchar_t *opt;
8717 uint8_t optval;
8718 uint8_t optlen;
8719 ipaddr_t dst;
8720 ipaddr_t ifaddr;
8721 uint32_t ts;
8722 timestruc_t now;
8723 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
8725 ip2dbg(("ip_forward_options\n"));
8726 dst = ipha->ipha_dst;
8727 for (optval = ipoptp_first(&opts, ipha);
8728 optval != IPOPT_EOL;
8729 optval = ipoptp_next(&opts)) {
8730 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8731 opt = opts.ipoptp_cur;
8732 optlen = opts.ipoptp_len;
8733 ip2dbg(("ip_forward_options: opt %d, len %d\n",
8734 optval, opts.ipoptp_len));
8735 switch (optval) {
8736 uint32_t off;
8737 case IPOPT_SSRR:
8738 case IPOPT_LSRR:
8739 /* Check if adminstratively disabled */
8740 if (!ipst->ips_ip_forward_src_routed) {
8741 BUMP_MIB(dst_ill->ill_ip_mib,
8742 ipIfStatsForwProhibits);
8743 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
8744 mp, dst_ill);
8745 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
8746 ira);
8747 return (B_FALSE);
8749 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
8751 * Must be partial since ip_input_options
8752 * checked for strict.
8754 break;
8756 off = opt[IPOPT_OFFSET];
8757 off--;
8758 redo_srr:
8759 if (optlen < IP_ADDR_LEN ||
8760 off > optlen - IP_ADDR_LEN) {
8761 /* End of source route */
8762 ip1dbg((
8763 "ip_forward_options: end of SR\n"));
8764 break;
8766 /* Pick a reasonable address on the outbound if */
8767 ASSERT(dst_ill != NULL);
8768 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
8769 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
8770 NULL) != 0) {
8771 /* No source! Shouldn't happen */
8772 ifaddr = INADDR_ANY;
8774 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
8775 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
8776 ip1dbg(("ip_forward_options: next hop 0x%x\n",
8777 ntohl(dst)));
8780 * Check if our address is present more than
8781 * once as consecutive hops in source route.
8783 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
8784 off += IP_ADDR_LEN;
8785 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8786 goto redo_srr;
8788 ipha->ipha_dst = dst;
8789 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8790 break;
8791 case IPOPT_RR:
8792 off = opt[IPOPT_OFFSET];
8793 off--;
8794 if (optlen < IP_ADDR_LEN ||
8795 off > optlen - IP_ADDR_LEN) {
8796 /* No more room - ignore */
8797 ip1dbg((
8798 "ip_forward_options: end of RR\n"));
8799 break;
8801 /* Pick a reasonable address on the outbound if */
8802 ASSERT(dst_ill != NULL);
8803 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
8804 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
8805 NULL) != 0) {
8806 /* No source! Shouldn't happen */
8807 ifaddr = INADDR_ANY;
8809 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
8810 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8811 break;
8812 case IPOPT_TS:
8813 /* Insert timestamp if there is room */
8814 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
8815 case IPOPT_TS_TSONLY:
8816 off = IPOPT_TS_TIMELEN;
8817 break;
8818 case IPOPT_TS_PRESPEC:
8819 case IPOPT_TS_PRESPEC_RFC791:
8820 /* Verify that the address matched */
8821 off = opt[IPOPT_OFFSET] - 1;
8822 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
8823 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
8824 /* Not for us */
8825 break;
8827 /* FALLTHROUGH */
8828 case IPOPT_TS_TSANDADDR:
8829 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
8830 break;
8831 default:
8833 * ip_*put_options should have already
8834 * dropped this packet.
8836 cmn_err(CE_PANIC, "ip_forward_options: "
8837 "unknown IT - bug in ip_input_options?\n");
8838 return (B_TRUE); /* Keep "lint" happy */
8840 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
8841 /* Increase overflow counter */
8842 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
8843 opt[IPOPT_POS_OV_FLG] =
8844 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
8845 (off << 4));
8846 break;
8848 off = opt[IPOPT_OFFSET] - 1;
8849 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
8850 case IPOPT_TS_PRESPEC:
8851 case IPOPT_TS_PRESPEC_RFC791:
8852 case IPOPT_TS_TSANDADDR:
8853 /* Pick a reasonable addr on the outbound if */
8854 ASSERT(dst_ill != NULL);
8855 if (ip_select_source_v4(dst_ill, INADDR_ANY,
8856 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
8857 NULL, NULL) != 0) {
8858 /* No source! Shouldn't happen */
8859 ifaddr = INADDR_ANY;
8861 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
8862 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8863 /* FALLTHROUGH */
8864 case IPOPT_TS_TSONLY:
8865 off = opt[IPOPT_OFFSET] - 1;
8866 /* Compute # of milliseconds since midnight */
8867 gethrestime(&now);
8868 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
8869 NSEC2MSEC(now.tv_nsec);
8870 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
8871 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
8872 break;
8874 break;
8877 return (B_TRUE);
8881 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
8882 * returns 'true' if there are still fragments left on the queue, in
8883 * which case we restart the timer.
8885 void
8886 ill_frag_timer(void *arg)
8888 ill_t *ill = (ill_t *)arg;
8889 boolean_t frag_pending;
8890 ip_stack_t *ipst = ill->ill_ipst;
8891 time_t timeout;
8893 mutex_enter(&ill->ill_lock);
8894 ASSERT(!ill->ill_fragtimer_executing);
8895 if (ill->ill_state_flags & ILL_CONDEMNED) {
8896 ill->ill_frag_timer_id = 0;
8897 mutex_exit(&ill->ill_lock);
8898 return;
8900 ill->ill_fragtimer_executing = 1;
8901 mutex_exit(&ill->ill_lock);
8903 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
8904 ipst->ips_ip_reassembly_timeout);
8906 frag_pending = ill_frag_timeout(ill, timeout);
8909 * Restart the timer, if we have fragments pending or if someone
8910 * wanted us to be scheduled again.
8912 mutex_enter(&ill->ill_lock);
8913 ill->ill_fragtimer_executing = 0;
8914 ill->ill_frag_timer_id = 0;
8915 if (frag_pending || ill->ill_fragtimer_needrestart)
8916 ill_frag_timer_start(ill);
8917 mutex_exit(&ill->ill_lock);
8920 void
8921 ill_frag_timer_start(ill_t *ill)
8923 ip_stack_t *ipst = ill->ill_ipst;
8924 clock_t timeo_ms;
8926 ASSERT(MUTEX_HELD(&ill->ill_lock));
8928 /* If the ill is closing or opening don't proceed */
8929 if (ill->ill_state_flags & ILL_CONDEMNED)
8930 return;
8932 if (ill->ill_fragtimer_executing) {
8934 * ill_frag_timer is currently executing. Just record the
8935 * the fact that we want the timer to be restarted.
8936 * ill_frag_timer will post a timeout before it returns,
8937 * ensuring it will be called again.
8939 ill->ill_fragtimer_needrestart = 1;
8940 return;
8943 if (ill->ill_frag_timer_id == 0) {
8944 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
8945 ipst->ips_ip_reassembly_timeout) * SECONDS;
8948 * The timer is neither running nor is the timeout handler
8949 * executing. Post a timeout so that ill_frag_timer will be
8950 * called
8952 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
8953 MSEC_TO_TICK(timeo_ms >> 1));
8954 ill->ill_fragtimer_needrestart = 0;
8959 * Update any source route, record route or timestamp options.
8960 * Check that we are at end of strict source route.
8961 * The options have already been checked for sanity in ip_input_options().
8963 boolean_t
8964 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
8966 ipoptp_t opts;
8967 uchar_t *opt;
8968 uint8_t optval;
8969 uint8_t optlen;
8970 ipaddr_t dst;
8971 ipaddr_t ifaddr;
8972 uint32_t ts;
8973 timestruc_t now;
8974 ill_t *ill = ira->ira_ill;
8975 ip_stack_t *ipst = ill->ill_ipst;
8977 ip2dbg(("ip_input_local_options\n"));
8979 for (optval = ipoptp_first(&opts, ipha);
8980 optval != IPOPT_EOL;
8981 optval = ipoptp_next(&opts)) {
8982 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8983 opt = opts.ipoptp_cur;
8984 optlen = opts.ipoptp_len;
8985 ip2dbg(("ip_input_local_options: opt %d, len %d\n",
8986 optval, optlen));
8987 switch (optval) {
8988 uint32_t off;
8989 case IPOPT_SSRR:
8990 case IPOPT_LSRR:
8991 off = opt[IPOPT_OFFSET];
8992 off--;
8993 if (optlen < IP_ADDR_LEN ||
8994 off > optlen - IP_ADDR_LEN) {
8995 /* End of source route */
8996 ip1dbg(("ip_input_local_options: end of SR\n"));
8997 break;
9000 * This will only happen if two consecutive entries
9001 * in the source route contains our address or if
9002 * it is a packet with a loose source route which
9003 * reaches us before consuming the whole source route
9005 ip1dbg(("ip_input_local_options: not end of SR\n"));
9006 if (optval == IPOPT_SSRR) {
9007 goto bad_src_route;
9010 * Hack: instead of dropping the packet truncate the
9011 * source route to what has been used by filling the
9012 * rest with IPOPT_NOP.
9014 opt[IPOPT_OLEN] = (uint8_t)off;
9015 while (off < optlen) {
9016 opt[off++] = IPOPT_NOP;
9018 break;
9019 case IPOPT_RR:
9020 off = opt[IPOPT_OFFSET];
9021 off--;
9022 if (optlen < IP_ADDR_LEN ||
9023 off > optlen - IP_ADDR_LEN) {
9024 /* No more room - ignore */
9025 ip1dbg((
9026 "ip_input_local_options: end of RR\n"));
9027 break;
9029 /* Pick a reasonable address on the outbound if */
9030 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9031 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9032 NULL) != 0) {
9033 /* No source! Shouldn't happen */
9034 ifaddr = INADDR_ANY;
9036 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9037 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9038 break;
9039 case IPOPT_TS:
9040 /* Insert timestamp if there is romm */
9041 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9042 case IPOPT_TS_TSONLY:
9043 off = IPOPT_TS_TIMELEN;
9044 break;
9045 case IPOPT_TS_PRESPEC:
9046 case IPOPT_TS_PRESPEC_RFC791:
9047 /* Verify that the address matched */
9048 off = opt[IPOPT_OFFSET] - 1;
9049 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9050 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9051 /* Not for us */
9052 break;
9054 /* FALLTHROUGH */
9055 case IPOPT_TS_TSANDADDR:
9056 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9057 break;
9058 default:
9060 * ip_*put_options should have already
9061 * dropped this packet.
9063 cmn_err(CE_PANIC, "ip_input_local_options: "
9064 "unknown IT - bug in ip_input_options?\n");
9065 return (B_TRUE); /* Keep "lint" happy */
9067 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9068 /* Increase overflow counter */
9069 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9070 opt[IPOPT_POS_OV_FLG] =
9071 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9072 (off << 4));
9073 break;
9075 off = opt[IPOPT_OFFSET] - 1;
9076 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9077 case IPOPT_TS_PRESPEC:
9078 case IPOPT_TS_PRESPEC_RFC791:
9079 case IPOPT_TS_TSANDADDR:
9080 /* Pick a reasonable addr on the outbound if */
9081 if (ip_select_source_v4(ill, INADDR_ANY,
9082 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9083 &ifaddr, NULL, NULL) != 0) {
9084 /* No source! Shouldn't happen */
9085 ifaddr = INADDR_ANY;
9087 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9088 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9089 /* FALLTHROUGH */
9090 case IPOPT_TS_TSONLY:
9091 off = opt[IPOPT_OFFSET] - 1;
9092 /* Compute # of milliseconds since midnight */
9093 gethrestime(&now);
9094 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9095 NSEC2MSEC(now.tv_nsec);
9096 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9097 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9098 break;
9100 break;
9103 return (B_TRUE);
9105 bad_src_route:
9106 /* make sure we clear any indication of a hardware checksum */
9107 DB_CKSUMFLAGS(mp) = 0;
9108 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9109 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9110 return (B_FALSE);
9115 * Process IP options in an inbound packet. Always returns the nexthop.
9116 * Normally this is the passed in nexthop, but if there is an option
9117 * that effects the nexthop (such as a source route) that will be returned.
9118 * Sets *errorp if there is an error, in which case an ICMP error has been sent
9119 * and mp freed.
9121 ipaddr_t
9122 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9123 ip_recv_attr_t *ira, int *errorp)
9125 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9126 ipoptp_t opts;
9127 uchar_t *opt;
9128 uint8_t optval;
9129 uint8_t optlen;
9130 intptr_t code = 0;
9131 ire_t *ire;
9133 ip2dbg(("ip_input_options\n"));
9134 *errorp = 0;
9135 for (optval = ipoptp_first(&opts, ipha);
9136 optval != IPOPT_EOL;
9137 optval = ipoptp_next(&opts)) {
9138 opt = opts.ipoptp_cur;
9139 optlen = opts.ipoptp_len;
9140 ip2dbg(("ip_input_options: opt %d, len %d\n",
9141 optval, optlen));
9143 * Note: we need to verify the checksum before we
9144 * modify anything thus this routine only extracts the next
9145 * hop dst from any source route.
9147 switch (optval) {
9148 uint32_t off;
9149 case IPOPT_SSRR:
9150 case IPOPT_LSRR:
9151 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9152 if (optval == IPOPT_SSRR) {
9153 ip1dbg(("ip_input_options: not next"
9154 " strict source route 0x%x\n",
9155 ntohl(dst)));
9156 code = (char *)&ipha->ipha_dst -
9157 (char *)ipha;
9158 goto param_prob; /* RouterReq's */
9160 ip2dbg(("ip_input_options: "
9161 "not next source route 0x%x\n",
9162 ntohl(dst)));
9163 break;
9166 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9167 ip1dbg((
9168 "ip_input_options: bad option offset\n"));
9169 code = (char *)&opt[IPOPT_OLEN] -
9170 (char *)ipha;
9171 goto param_prob;
9173 off = opt[IPOPT_OFFSET];
9174 off--;
9175 redo_srr:
9176 if (optlen < IP_ADDR_LEN ||
9177 off > optlen - IP_ADDR_LEN) {
9178 /* End of source route */
9179 ip1dbg(("ip_input_options: end of SR\n"));
9180 break;
9182 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9183 ip1dbg(("ip_input_options: next hop 0x%x\n",
9184 ntohl(dst)));
9187 * Check if our address is present more than
9188 * once as consecutive hops in source route.
9189 * XXX verify per-interface ip_forwarding
9190 * for source route?
9192 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9193 off += IP_ADDR_LEN;
9194 goto redo_srr;
9197 if (dst == htonl(INADDR_LOOPBACK)) {
9198 ip1dbg(("ip_input_options: loopback addr in "
9199 "source route!\n"));
9200 goto bad_src_route;
9203 * For strict: verify that dst is directly
9204 * reachable.
9206 if (optval == IPOPT_SSRR) {
9207 ire = ire_ftable_lookup_v4(dst, 0, 0,
9208 IRE_INTERFACE, NULL, ALL_ZONES,
9209 MATCH_IRE_TYPE, 0, ipst, NULL);
9210 if (ire == NULL) {
9211 ip1dbg(("ip_input_options: SSRR not "
9212 "directly reachable: 0x%x\n",
9213 ntohl(dst)));
9214 goto bad_src_route;
9216 ire_refrele(ire);
9219 * Defer update of the offset and the record route
9220 * until the packet is forwarded.
9222 break;
9223 case IPOPT_RR:
9224 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9225 ip1dbg((
9226 "ip_input_options: bad option offset\n"));
9227 code = (char *)&opt[IPOPT_OLEN] -
9228 (char *)ipha;
9229 goto param_prob;
9231 break;
9232 case IPOPT_TS:
9234 * Verify that length >= 5 and that there is either
9235 * room for another timestamp or that the overflow
9236 * counter is not maxed out.
9238 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9239 if (optlen < IPOPT_MINLEN_IT) {
9240 goto param_prob;
9242 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9243 ip1dbg((
9244 "ip_input_options: bad option offset\n"));
9245 code = (char *)&opt[IPOPT_OFFSET] -
9246 (char *)ipha;
9247 goto param_prob;
9249 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9250 case IPOPT_TS_TSONLY:
9251 off = IPOPT_TS_TIMELEN;
9252 break;
9253 case IPOPT_TS_TSANDADDR:
9254 case IPOPT_TS_PRESPEC:
9255 case IPOPT_TS_PRESPEC_RFC791:
9256 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9257 break;
9258 default:
9259 code = (char *)&opt[IPOPT_POS_OV_FLG] -
9260 (char *)ipha;
9261 goto param_prob;
9263 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9264 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9266 * No room and the overflow counter is 15
9267 * already.
9269 goto param_prob;
9271 break;
9275 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9276 return (dst);
9279 ip1dbg(("ip_input_options: error processing IP options."));
9280 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9282 param_prob:
9283 /* make sure we clear any indication of a hardware checksum */
9284 DB_CKSUMFLAGS(mp) = 0;
9285 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9286 icmp_param_problem(mp, (uint8_t)code, ira);
9287 *errorp = -1;
9288 return (dst);
9290 bad_src_route:
9291 /* make sure we clear any indication of a hardware checksum */
9292 DB_CKSUMFLAGS(mp) = 0;
9293 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9294 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9295 *errorp = -1;
9296 return (dst);
9300 * IP & ICMP info in >=14 msg's ...
9301 * - ip fixed part (mib2_ip_t)
9302 * - icmp fixed part (mib2_icmp_t)
9303 * - ipAddrEntryTable (ip 20) all IPv4 ipifs
9304 * - ipRouteEntryTable (ip 21) all IPv4 IREs
9305 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries
9306 * - ip multicast membership (ip_member_t)
9307 * - ip multicast source filtering (ip_grpsrc_t)
9308 * - igmp fixed part (struct igmpstat)
9309 * - multicast routing stats (struct mrtstat)
9310 * - multicast routing vifs (array of struct vifctl)
9311 * - multicast routing routes (array of struct mfcctl)
9312 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9313 * One per ill plus one generic
9314 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9315 * One per ill plus one generic
9316 * - ipv6RouteEntry all IPv6 IREs
9317 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries
9318 * - ipv6AddrEntry all IPv6 ipifs
9319 * - ipv6 multicast membership (ipv6_member_t)
9320 * - ipv6 multicast source filtering (ipv6_grpsrc_t)
9322 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9323 * already filled in by the caller.
9324 * If legacy_req is true then MIB structures needs to be truncated to their
9325 * legacy sizes before being returned.
9326 * Return value of 0 indicates that no messages were sent and caller
9327 * should free mpctl.
9330 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9332 ip_stack_t *ipst;
9333 sctp_stack_t *sctps;
9335 if (q->q_next != NULL) {
9336 ipst = ILLQ_TO_IPST(q);
9337 } else {
9338 ipst = CONNQ_TO_IPST(q);
9340 ASSERT(ipst != NULL);
9341 sctps = ipst->ips_netstack->netstack_sctp;
9343 if (mpctl == NULL || mpctl->b_cont == NULL) {
9344 return (0);
9348 * For the purposes of the (broken) packet shell use
9349 * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9350 * to make TCP and UDP appear first in the list of mib items.
9351 * TBD: We could expand this and use it in netstat so that
9352 * the kernel doesn't have to produce large tables (connections,
9353 * routes, etc) when netstat only wants the statistics or a particular
9354 * table.
9356 if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9357 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9358 return (1);
9362 if (level != MIB2_TCP) {
9363 if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9364 return (1);
9368 if (level != MIB2_UDP) {
9369 if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9370 return (1);
9374 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9375 ipst, legacy_req)) == NULL) {
9376 return (1);
9379 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9380 legacy_req)) == NULL) {
9381 return (1);
9384 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9385 return (1);
9388 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9389 return (1);
9392 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9393 return (1);
9396 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9397 return (1);
9400 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9401 legacy_req)) == NULL) {
9402 return (1);
9405 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9406 legacy_req)) == NULL) {
9407 return (1);
9410 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9411 return (1);
9414 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9415 return (1);
9418 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9419 return (1);
9422 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9423 return (1);
9426 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9427 return (1);
9430 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9431 return (1);
9434 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9435 if (mpctl == NULL)
9436 return (1);
9438 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9439 if (mpctl == NULL)
9440 return (1);
9442 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9443 return (1);
9445 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9446 return (1);
9448 freemsg(mpctl);
9449 return (1);
9452 /* Get global (legacy) IPv4 statistics */
9453 static mblk_t *
9454 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9455 ip_stack_t *ipst, boolean_t legacy_req)
9457 mib2_ip_t old_ip_mib;
9458 struct opthdr *optp;
9459 mblk_t *mp2ctl;
9460 mib2_ipAddrEntry_t mae;
9463 * make a copy of the original message
9465 mp2ctl = copymsg(mpctl);
9467 /* fixed length IP structure... */
9468 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9469 optp->level = MIB2_IP;
9470 optp->name = 0;
9471 SET_MIB(old_ip_mib.ipForwarding,
9472 (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9473 SET_MIB(old_ip_mib.ipDefaultTTL,
9474 (uint32_t)ipst->ips_ip_def_ttl);
9475 SET_MIB(old_ip_mib.ipReasmTimeout,
9476 ipst->ips_ip_reassembly_timeout);
9477 SET_MIB(old_ip_mib.ipAddrEntrySize,
9478 (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9479 sizeof (mib2_ipAddrEntry_t));
9480 SET_MIB(old_ip_mib.ipRouteEntrySize,
9481 sizeof (mib2_ipRouteEntry_t));
9482 SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9483 sizeof (mib2_ipNetToMediaEntry_t));
9484 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9485 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9486 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9489 * Grab the statistics from the new IP MIB
9491 SET_MIB(old_ip_mib.ipInReceives,
9492 (uint32_t)ipmib->ipIfStatsHCInReceives);
9493 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9494 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9495 SET_MIB(old_ip_mib.ipForwDatagrams,
9496 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9497 SET_MIB(old_ip_mib.ipInUnknownProtos,
9498 ipmib->ipIfStatsInUnknownProtos);
9499 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9500 SET_MIB(old_ip_mib.ipInDelivers,
9501 (uint32_t)ipmib->ipIfStatsHCInDelivers);
9502 SET_MIB(old_ip_mib.ipOutRequests,
9503 (uint32_t)ipmib->ipIfStatsHCOutRequests);
9504 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9505 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9506 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9507 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9508 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9509 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9510 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9511 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9513 /* ipRoutingDiscards is not being used */
9514 SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9515 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9516 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9517 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9518 SET_MIB(old_ip_mib.ipReasmDuplicates,
9519 ipmib->ipIfStatsReasmDuplicates);
9520 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9521 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9522 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9523 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9524 SET_MIB(old_ip_mib.rawipInOverflows,
9525 ipmib->rawipIfStatsInOverflows);
9527 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9528 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9529 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9530 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9531 SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9532 ipmib->ipIfStatsOutSwitchIPVersion);
9534 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9535 (int)sizeof (old_ip_mib))) {
9536 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9537 (uint_t)sizeof (old_ip_mib)));
9540 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9541 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9542 (int)optp->level, (int)optp->name, (int)optp->len));
9543 qreply(q, mpctl);
9544 return (mp2ctl);
9547 /* Per interface IPv4 statistics */
9548 static mblk_t *
9549 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9550 boolean_t legacy_req)
9552 struct opthdr *optp;
9553 mblk_t *mp2ctl;
9554 ill_t *ill;
9555 ill_walk_context_t ctx;
9556 mblk_t *mp_tail = NULL;
9557 mib2_ipIfStatsEntry_t global_ip_mib;
9558 mib2_ipAddrEntry_t mae;
9561 * Make a copy of the original message
9563 mp2ctl = copymsg(mpctl);
9565 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9566 optp->level = MIB2_IP;
9567 optp->name = MIB2_IP_TRAFFIC_STATS;
9568 /* Include "unknown interface" ip_mib */
9569 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9570 ipst->ips_ip_mib.ipIfStatsIfIndex =
9571 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9572 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9573 (ipst->ips_ip_forwarding ? 1 : 2));
9574 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9575 (uint32_t)ipst->ips_ip_def_ttl);
9576 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9577 sizeof (mib2_ipIfStatsEntry_t));
9578 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9579 sizeof (mib2_ipAddrEntry_t));
9580 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9581 sizeof (mib2_ipRouteEntry_t));
9582 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9583 sizeof (mib2_ipNetToMediaEntry_t));
9584 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9585 sizeof (ip_member_t));
9586 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9587 sizeof (ip_grpsrc_t));
9589 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9591 if (legacy_req) {
9592 SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9593 LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9596 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9597 (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9598 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9599 "failed to allocate %u bytes\n",
9600 (uint_t)sizeof (global_ip_mib)));
9603 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9604 ill = ILL_START_WALK_V4(&ctx, ipst);
9605 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9606 ill->ill_ip_mib->ipIfStatsIfIndex =
9607 ill->ill_phyint->phyint_ifindex;
9608 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9609 (ipst->ips_ip_forwarding ? 1 : 2));
9610 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9611 (uint32_t)ipst->ips_ip_def_ttl);
9613 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9614 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9615 (char *)ill->ill_ip_mib,
9616 (int)sizeof (*ill->ill_ip_mib))) {
9617 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9618 "failed to allocate %u bytes\n",
9619 (uint_t)sizeof (*ill->ill_ip_mib)));
9622 rw_exit(&ipst->ips_ill_g_lock);
9624 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9625 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9626 "level %d, name %d, len %d\n",
9627 (int)optp->level, (int)optp->name, (int)optp->len));
9628 qreply(q, mpctl);
9630 if (mp2ctl == NULL)
9631 return (NULL);
9633 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9634 legacy_req));
9637 /* Global IPv4 ICMP statistics */
9638 static mblk_t *
9639 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9641 struct opthdr *optp;
9642 mblk_t *mp2ctl;
9645 * Make a copy of the original message
9647 mp2ctl = copymsg(mpctl);
9649 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9650 optp->level = MIB2_ICMP;
9651 optp->name = 0;
9652 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9653 (int)sizeof (ipst->ips_icmp_mib))) {
9654 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9655 (uint_t)sizeof (ipst->ips_icmp_mib)));
9657 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9658 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9659 (int)optp->level, (int)optp->name, (int)optp->len));
9660 qreply(q, mpctl);
9661 return (mp2ctl);
9664 /* Global IPv4 IGMP statistics */
9665 static mblk_t *
9666 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9668 struct opthdr *optp;
9669 mblk_t *mp2ctl;
9672 * make a copy of the original message
9674 mp2ctl = copymsg(mpctl);
9676 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9677 optp->level = EXPER_IGMP;
9678 optp->name = 0;
9679 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9680 (int)sizeof (ipst->ips_igmpstat))) {
9681 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9682 (uint_t)sizeof (ipst->ips_igmpstat)));
9684 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9685 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9686 (int)optp->level, (int)optp->name, (int)optp->len));
9687 qreply(q, mpctl);
9688 return (mp2ctl);
9691 /* Global IPv4 Multicast Routing statistics */
9692 static mblk_t *
9693 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9695 struct opthdr *optp;
9696 mblk_t *mp2ctl;
9699 * make a copy of the original message
9701 mp2ctl = copymsg(mpctl);
9703 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9704 optp->level = EXPER_DVMRP;
9705 optp->name = 0;
9706 if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9707 ip0dbg(("ip_mroute_stats: failed\n"));
9709 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9710 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9711 (int)optp->level, (int)optp->name, (int)optp->len));
9712 qreply(q, mpctl);
9713 return (mp2ctl);
9716 /* IPv4 address information */
9717 static mblk_t *
9718 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9719 boolean_t legacy_req)
9721 struct opthdr *optp;
9722 mblk_t *mp2ctl;
9723 mblk_t *mp_tail = NULL;
9724 ill_t *ill;
9725 ipif_t *ipif;
9726 uint_t bitval;
9727 mib2_ipAddrEntry_t mae;
9728 size_t mae_size;
9729 zoneid_t zoneid;
9730 ill_walk_context_t ctx;
9733 * make a copy of the original message
9735 mp2ctl = copymsg(mpctl);
9737 mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9738 sizeof (mib2_ipAddrEntry_t);
9740 /* ipAddrEntryTable */
9742 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9743 optp->level = MIB2_IP;
9744 optp->name = MIB2_IP_ADDR;
9745 zoneid = Q_TO_CONN(q)->conn_zoneid;
9747 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9748 ill = ILL_START_WALK_V4(&ctx, ipst);
9749 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9750 for (ipif = ill->ill_ipif; ipif != NULL;
9751 ipif = ipif->ipif_next) {
9752 if (ipif->ipif_zoneid != zoneid &&
9753 ipif->ipif_zoneid != ALL_ZONES)
9754 continue;
9755 /* Sum of count from dead IRE_LO* and our current */
9756 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
9757 if (ipif->ipif_ire_local != NULL) {
9758 mae.ipAdEntInfo.ae_ibcnt +=
9759 ipif->ipif_ire_local->ire_ib_pkt_count;
9761 mae.ipAdEntInfo.ae_obcnt = 0;
9762 mae.ipAdEntInfo.ae_focnt = 0;
9764 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
9765 OCTET_LENGTH);
9766 mae.ipAdEntIfIndex.o_length =
9767 mi_strlen(mae.ipAdEntIfIndex.o_bytes);
9768 mae.ipAdEntAddr = ipif->ipif_lcl_addr;
9769 mae.ipAdEntNetMask = ipif->ipif_net_mask;
9770 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
9771 mae.ipAdEntInfo.ae_subnet_len =
9772 ip_mask_to_plen(ipif->ipif_net_mask);
9773 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
9774 for (bitval = 1;
9775 bitval &&
9776 !(bitval & ipif->ipif_brd_addr);
9777 bitval <<= 1)
9778 noop;
9779 mae.ipAdEntBcastAddr = bitval;
9780 mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
9781 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
9782 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric;
9783 mae.ipAdEntInfo.ae_broadcast_addr =
9784 ipif->ipif_brd_addr;
9785 mae.ipAdEntInfo.ae_pp_dst_addr =
9786 ipif->ipif_pp_dst_addr;
9787 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
9788 ill->ill_flags | ill->ill_phyint->phyint_flags;
9789 mae.ipAdEntRetransmitTime =
9790 ill->ill_reachable_retrans_time;
9792 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9793 (char *)&mae, (int)mae_size)) {
9794 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
9795 "allocate %u bytes\n", (uint_t)mae_size));
9799 rw_exit(&ipst->ips_ill_g_lock);
9801 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9802 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
9803 (int)optp->level, (int)optp->name, (int)optp->len));
9804 qreply(q, mpctl);
9805 return (mp2ctl);
9808 /* IPv6 address information */
9809 static mblk_t *
9810 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9811 boolean_t legacy_req)
9813 struct opthdr *optp;
9814 mblk_t *mp2ctl;
9815 mblk_t *mp_tail = NULL;
9816 ill_t *ill;
9817 ipif_t *ipif;
9818 mib2_ipv6AddrEntry_t mae6;
9819 size_t mae6_size;
9820 zoneid_t zoneid;
9821 ill_walk_context_t ctx;
9824 * make a copy of the original message
9826 mp2ctl = copymsg(mpctl);
9828 mae6_size = (legacy_req) ?
9829 LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
9830 sizeof (mib2_ipv6AddrEntry_t);
9832 /* ipv6AddrEntryTable */
9834 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9835 optp->level = MIB2_IP6;
9836 optp->name = MIB2_IP6_ADDR;
9837 zoneid = Q_TO_CONN(q)->conn_zoneid;
9839 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9840 ill = ILL_START_WALK_V6(&ctx, ipst);
9841 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9842 for (ipif = ill->ill_ipif; ipif != NULL;
9843 ipif = ipif->ipif_next) {
9844 if (ipif->ipif_zoneid != zoneid &&
9845 ipif->ipif_zoneid != ALL_ZONES)
9846 continue;
9847 /* Sum of count from dead IRE_LO* and our current */
9848 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
9849 if (ipif->ipif_ire_local != NULL) {
9850 mae6.ipv6AddrInfo.ae_ibcnt +=
9851 ipif->ipif_ire_local->ire_ib_pkt_count;
9853 mae6.ipv6AddrInfo.ae_obcnt = 0;
9854 mae6.ipv6AddrInfo.ae_focnt = 0;
9856 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
9857 OCTET_LENGTH);
9858 mae6.ipv6AddrIfIndex.o_length =
9859 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
9860 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
9861 mae6.ipv6AddrPfxLength =
9862 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9863 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
9864 mae6.ipv6AddrInfo.ae_subnet_len =
9865 mae6.ipv6AddrPfxLength;
9866 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
9868 /* Type: stateless(1), stateful(2), unknown(3) */
9869 if (ipif->ipif_flags & IPIF_ADDRCONF)
9870 mae6.ipv6AddrType = 1;
9871 else
9872 mae6.ipv6AddrType = 2;
9873 /* Anycast: true(1), false(2) */
9874 if (ipif->ipif_flags & IPIF_ANYCAST)
9875 mae6.ipv6AddrAnycastFlag = 1;
9876 else
9877 mae6.ipv6AddrAnycastFlag = 2;
9880 * Address status: preferred(1), deprecated(2),
9881 * invalid(3), inaccessible(4), unknown(5)
9883 if (ipif->ipif_flags & IPIF_NOLOCAL)
9884 mae6.ipv6AddrStatus = 3;
9885 else if (ipif->ipif_flags & IPIF_DEPRECATED)
9886 mae6.ipv6AddrStatus = 2;
9887 else
9888 mae6.ipv6AddrStatus = 1;
9889 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
9890 mae6.ipv6AddrInfo.ae_metric =
9891 ipif->ipif_ill->ill_metric;
9892 mae6.ipv6AddrInfo.ae_pp_dst_addr =
9893 ipif->ipif_v6pp_dst_addr;
9894 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
9895 ill->ill_flags | ill->ill_phyint->phyint_flags;
9896 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
9897 mae6.ipv6AddrIdentifier = ill->ill_token;
9898 mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
9899 mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
9900 mae6.ipv6AddrRetransmitTime =
9901 ill->ill_reachable_retrans_time;
9902 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9903 (char *)&mae6, (int)mae6_size)) {
9904 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
9905 "allocate %u bytes\n",
9906 (uint_t)mae6_size));
9910 rw_exit(&ipst->ips_ill_g_lock);
9912 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9913 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
9914 (int)optp->level, (int)optp->name, (int)optp->len));
9915 qreply(q, mpctl);
9916 return (mp2ctl);
9919 /* IPv4 multicast group membership. */
9920 static mblk_t *
9921 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9923 struct opthdr *optp;
9924 mblk_t *mp2ctl;
9925 ill_t *ill;
9926 ipif_t *ipif;
9927 ilm_t *ilm;
9928 ip_member_t ipm;
9929 mblk_t *mp_tail = NULL;
9930 ill_walk_context_t ctx;
9931 zoneid_t zoneid;
9934 * make a copy of the original message
9936 mp2ctl = copymsg(mpctl);
9937 zoneid = Q_TO_CONN(q)->conn_zoneid;
9939 /* ipGroupMember table */
9940 optp = (struct opthdr *)&mpctl->b_rptr[
9941 sizeof (struct T_optmgmt_ack)];
9942 optp->level = MIB2_IP;
9943 optp->name = EXPER_IP_GROUP_MEMBERSHIP;
9945 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9946 ill = ILL_START_WALK_V4(&ctx, ipst);
9947 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9948 /* Make sure the ill isn't going away. */
9949 if (!ill_check_and_refhold(ill))
9950 continue;
9951 rw_exit(&ipst->ips_ill_g_lock);
9952 rw_enter(&ill->ill_mcast_lock, RW_READER);
9953 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
9954 if (ilm->ilm_zoneid != zoneid &&
9955 ilm->ilm_zoneid != ALL_ZONES)
9956 continue;
9958 /* Is there an ipif for ilm_ifaddr? */
9959 for (ipif = ill->ill_ipif; ipif != NULL;
9960 ipif = ipif->ipif_next) {
9961 if (!IPIF_IS_CONDEMNED(ipif) &&
9962 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
9963 ilm->ilm_ifaddr != INADDR_ANY)
9964 break;
9966 if (ipif != NULL) {
9967 ipif_get_name(ipif,
9968 ipm.ipGroupMemberIfIndex.o_bytes,
9969 OCTET_LENGTH);
9970 } else {
9971 ill_get_name(ill,
9972 ipm.ipGroupMemberIfIndex.o_bytes,
9973 OCTET_LENGTH);
9975 ipm.ipGroupMemberIfIndex.o_length =
9976 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
9978 ipm.ipGroupMemberAddress = ilm->ilm_addr;
9979 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
9980 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
9981 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9982 (char *)&ipm, (int)sizeof (ipm))) {
9983 ip1dbg(("ip_snmp_get_mib2_ip_group: "
9984 "failed to allocate %u bytes\n",
9985 (uint_t)sizeof (ipm)));
9988 rw_exit(&ill->ill_mcast_lock);
9989 ill_refrele(ill);
9990 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9992 rw_exit(&ipst->ips_ill_g_lock);
9993 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9994 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
9995 (int)optp->level, (int)optp->name, (int)optp->len));
9996 qreply(q, mpctl);
9997 return (mp2ctl);
10000 /* IPv6 multicast group membership. */
10001 static mblk_t *
10002 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10004 struct opthdr *optp;
10005 mblk_t *mp2ctl;
10006 ill_t *ill;
10007 ilm_t *ilm;
10008 ipv6_member_t ipm6;
10009 mblk_t *mp_tail = NULL;
10010 ill_walk_context_t ctx;
10011 zoneid_t zoneid;
10014 * make a copy of the original message
10016 mp2ctl = copymsg(mpctl);
10017 zoneid = Q_TO_CONN(q)->conn_zoneid;
10019 /* ip6GroupMember table */
10020 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10021 optp->level = MIB2_IP6;
10022 optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10024 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10025 ill = ILL_START_WALK_V6(&ctx, ipst);
10026 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10027 /* Make sure the ill isn't going away. */
10028 if (!ill_check_and_refhold(ill))
10029 continue;
10030 rw_exit(&ipst->ips_ill_g_lock);
10032 * Normally we don't have any members on under IPMP interfaces.
10033 * We report them as a debugging aid.
10035 rw_enter(&ill->ill_mcast_lock, RW_READER);
10036 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10037 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10038 if (ilm->ilm_zoneid != zoneid &&
10039 ilm->ilm_zoneid != ALL_ZONES)
10040 continue; /* not this zone */
10041 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10042 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10043 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10044 if (!snmp_append_data2(mpctl->b_cont,
10045 &mp_tail,
10046 (char *)&ipm6, (int)sizeof (ipm6))) {
10047 ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10048 "failed to allocate %u bytes\n",
10049 (uint_t)sizeof (ipm6)));
10052 rw_exit(&ill->ill_mcast_lock);
10053 ill_refrele(ill);
10054 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10056 rw_exit(&ipst->ips_ill_g_lock);
10058 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10059 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10060 (int)optp->level, (int)optp->name, (int)optp->len));
10061 qreply(q, mpctl);
10062 return (mp2ctl);
10065 /* IP multicast filtered sources */
10066 static mblk_t *
10067 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10069 struct opthdr *optp;
10070 mblk_t *mp2ctl;
10071 ill_t *ill;
10072 ipif_t *ipif;
10073 ilm_t *ilm;
10074 ip_grpsrc_t ips;
10075 mblk_t *mp_tail = NULL;
10076 ill_walk_context_t ctx;
10077 zoneid_t zoneid;
10078 int i;
10079 slist_t *sl;
10082 * make a copy of the original message
10084 mp2ctl = copymsg(mpctl);
10085 zoneid = Q_TO_CONN(q)->conn_zoneid;
10087 /* ipGroupSource table */
10088 optp = (struct opthdr *)&mpctl->b_rptr[
10089 sizeof (struct T_optmgmt_ack)];
10090 optp->level = MIB2_IP;
10091 optp->name = EXPER_IP_GROUP_SOURCES;
10093 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10094 ill = ILL_START_WALK_V4(&ctx, ipst);
10095 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10096 /* Make sure the ill isn't going away. */
10097 if (!ill_check_and_refhold(ill))
10098 continue;
10099 rw_exit(&ipst->ips_ill_g_lock);
10100 rw_enter(&ill->ill_mcast_lock, RW_READER);
10101 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10102 sl = ilm->ilm_filter;
10103 if (ilm->ilm_zoneid != zoneid &&
10104 ilm->ilm_zoneid != ALL_ZONES)
10105 continue;
10106 if (SLIST_IS_EMPTY(sl))
10107 continue;
10109 /* Is there an ipif for ilm_ifaddr? */
10110 for (ipif = ill->ill_ipif; ipif != NULL;
10111 ipif = ipif->ipif_next) {
10112 if (!IPIF_IS_CONDEMNED(ipif) &&
10113 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10114 ilm->ilm_ifaddr != INADDR_ANY)
10115 break;
10117 if (ipif != NULL) {
10118 ipif_get_name(ipif,
10119 ips.ipGroupSourceIfIndex.o_bytes,
10120 OCTET_LENGTH);
10121 } else {
10122 ill_get_name(ill,
10123 ips.ipGroupSourceIfIndex.o_bytes,
10124 OCTET_LENGTH);
10126 ips.ipGroupSourceIfIndex.o_length =
10127 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10129 ips.ipGroupSourceGroup = ilm->ilm_addr;
10130 for (i = 0; i < sl->sl_numsrc; i++) {
10131 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10132 continue;
10133 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10134 ips.ipGroupSourceAddress);
10135 if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10136 (char *)&ips, (int)sizeof (ips)) == 0) {
10137 ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10138 " failed to allocate %u bytes\n",
10139 (uint_t)sizeof (ips)));
10143 rw_exit(&ill->ill_mcast_lock);
10144 ill_refrele(ill);
10145 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10147 rw_exit(&ipst->ips_ill_g_lock);
10148 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10149 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10150 (int)optp->level, (int)optp->name, (int)optp->len));
10151 qreply(q, mpctl);
10152 return (mp2ctl);
10155 /* IPv6 multicast filtered sources. */
10156 static mblk_t *
10157 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10159 struct opthdr *optp;
10160 mblk_t *mp2ctl;
10161 ill_t *ill;
10162 ilm_t *ilm;
10163 ipv6_grpsrc_t ips6;
10164 mblk_t *mp_tail = NULL;
10165 ill_walk_context_t ctx;
10166 zoneid_t zoneid;
10167 int i;
10168 slist_t *sl;
10171 * make a copy of the original message
10173 mp2ctl = copymsg(mpctl);
10174 zoneid = Q_TO_CONN(q)->conn_zoneid;
10176 /* ip6GroupMember table */
10177 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10178 optp->level = MIB2_IP6;
10179 optp->name = EXPER_IP6_GROUP_SOURCES;
10181 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10182 ill = ILL_START_WALK_V6(&ctx, ipst);
10183 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10184 /* Make sure the ill isn't going away. */
10185 if (!ill_check_and_refhold(ill))
10186 continue;
10187 rw_exit(&ipst->ips_ill_g_lock);
10189 * Normally we don't have any members on under IPMP interfaces.
10190 * We report them as a debugging aid.
10192 rw_enter(&ill->ill_mcast_lock, RW_READER);
10193 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10194 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10195 sl = ilm->ilm_filter;
10196 if (ilm->ilm_zoneid != zoneid &&
10197 ilm->ilm_zoneid != ALL_ZONES)
10198 continue;
10199 if (SLIST_IS_EMPTY(sl))
10200 continue;
10201 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10202 for (i = 0; i < sl->sl_numsrc; i++) {
10203 ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10204 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10205 (char *)&ips6, (int)sizeof (ips6))) {
10206 ip1dbg(("ip_snmp_get_mib2_ip6_"
10207 "group_src: failed to allocate "
10208 "%u bytes\n",
10209 (uint_t)sizeof (ips6)));
10213 rw_exit(&ill->ill_mcast_lock);
10214 ill_refrele(ill);
10215 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10217 rw_exit(&ipst->ips_ill_g_lock);
10219 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10220 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10221 (int)optp->level, (int)optp->name, (int)optp->len));
10222 qreply(q, mpctl);
10223 return (mp2ctl);
10226 /* Multicast routing virtual interface table. */
10227 static mblk_t *
10228 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10230 struct opthdr *optp;
10231 mblk_t *mp2ctl;
10234 * make a copy of the original message
10236 mp2ctl = copymsg(mpctl);
10238 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10239 optp->level = EXPER_DVMRP;
10240 optp->name = EXPER_DVMRP_VIF;
10241 if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10242 ip0dbg(("ip_mroute_vif: failed\n"));
10244 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10245 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10246 (int)optp->level, (int)optp->name, (int)optp->len));
10247 qreply(q, mpctl);
10248 return (mp2ctl);
10251 /* Multicast routing table. */
10252 static mblk_t *
10253 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10255 struct opthdr *optp;
10256 mblk_t *mp2ctl;
10259 * make a copy of the original message
10261 mp2ctl = copymsg(mpctl);
10263 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10264 optp->level = EXPER_DVMRP;
10265 optp->name = EXPER_DVMRP_MRT;
10266 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10267 ip0dbg(("ip_mroute_mrt: failed\n"));
10269 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10270 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10271 (int)optp->level, (int)optp->name, (int)optp->len));
10272 qreply(q, mpctl);
10273 return (mp2ctl);
10277 * Return ipRouteEntryTable and ipNetToMediaEntryTable in one IRE walk.
10279 static mblk_t *
10280 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10281 ip_stack_t *ipst)
10283 struct opthdr *optp;
10284 mblk_t *mp2ctl; /* Returned */
10285 mblk_t *mp3ctl; /* nettomedia */
10286 iproutedata_t ird;
10287 zoneid_t zoneid;
10290 * make copies of the original message
10291 * - mp2ctl is returned unchanged to the caller for its use
10292 * - mpctl is sent upstream as ipRouteEntryTable
10293 * - mp3ctl is sent upstream as ipNetToMediaEntryTable
10295 mp2ctl = copymsg(mpctl);
10296 mp3ctl = copymsg(mpctl);
10297 if (mp3ctl == NULL) {
10298 freemsg(mp3ctl);
10299 freemsg(mp2ctl);
10300 freemsg(mpctl);
10301 return (NULL);
10304 bzero(&ird, sizeof (ird));
10306 ird.ird_route.lp_head = mpctl->b_cont;
10307 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10309 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10310 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10311 * intended a temporary solution until a proper MIB API is provided
10312 * that provides complete filtering/caller-opt-in.
10314 if (level == EXPER_IP_AND_ALL_IRES)
10315 ird.ird_flags |= IRD_REPORT_ALL;
10317 zoneid = Q_TO_CONN(q)->conn_zoneid;
10318 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10320 /* ipRouteEntryTable in mpctl */
10321 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10322 optp->level = MIB2_IP;
10323 optp->name = MIB2_IP_ROUTE;
10324 optp->len = msgdsize(ird.ird_route.lp_head);
10325 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10326 (int)optp->level, (int)optp->name, (int)optp->len));
10327 qreply(q, mpctl);
10329 /* ipNetToMediaEntryTable in mp3ctl */
10330 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10332 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10333 optp->level = MIB2_IP;
10334 optp->name = MIB2_IP_MEDIA;
10335 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10336 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10337 (int)optp->level, (int)optp->name, (int)optp->len));
10338 qreply(q, mp3ctl);
10340 return (mp2ctl);
10344 * Return ipv6RouteEntryTable in one IRE walk, and ipv6NetToMediaEntryTable in
10345 * an NDP walk.
10347 static mblk_t *
10348 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10349 ip_stack_t *ipst)
10351 struct opthdr *optp;
10352 mblk_t *mp2ctl; /* Returned */
10353 mblk_t *mp3ctl; /* nettomedia */
10354 iproutedata_t ird;
10355 zoneid_t zoneid;
10358 * make copies of the original message
10359 * - mp2ctl is returned unchanged to the caller for its use
10360 * - mpctl is sent upstream as ipv6RouteEntryTable
10361 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10363 mp2ctl = copymsg(mpctl);
10364 mp3ctl = copymsg(mpctl);
10365 if (mp3ctl == NULL) {
10366 freemsg(mp3ctl);
10367 freemsg(mp2ctl);
10368 freemsg(mpctl);
10369 return (NULL);
10372 bzero(&ird, sizeof (ird));
10374 ird.ird_route.lp_head = mpctl->b_cont;
10375 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10377 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10378 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10379 * intended a temporary solution until a proper MIB API is provided
10380 * that provides complete filtering/caller-opt-in.
10382 if (level == EXPER_IP_AND_ALL_IRES)
10383 ird.ird_flags |= IRD_REPORT_ALL;
10385 zoneid = Q_TO_CONN(q)->conn_zoneid;
10386 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10388 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10389 optp->level = MIB2_IP6;
10390 optp->name = MIB2_IP6_ROUTE;
10391 optp->len = msgdsize(ird.ird_route.lp_head);
10392 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10393 (int)optp->level, (int)optp->name, (int)optp->len));
10394 qreply(q, mpctl);
10396 /* ipv6NetToMediaEntryTable in mp3ctl */
10397 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10399 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10400 optp->level = MIB2_IP6;
10401 optp->name = MIB2_IP6_MEDIA;
10402 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10403 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10404 (int)optp->level, (int)optp->name, (int)optp->len));
10405 qreply(q, mp3ctl);
10407 return (mp2ctl);
10411 * IPv6 mib: One per ill
10413 static mblk_t *
10414 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10415 boolean_t legacy_req)
10417 struct opthdr *optp;
10418 mblk_t *mp2ctl;
10419 ill_t *ill;
10420 ill_walk_context_t ctx;
10421 mblk_t *mp_tail = NULL;
10422 mib2_ipv6AddrEntry_t mae6;
10423 mib2_ipIfStatsEntry_t *ise;
10424 size_t ise_size, iae_size;
10427 * Make a copy of the original message
10429 mp2ctl = copymsg(mpctl);
10431 /* fixed length IPv6 structure ... */
10433 if (legacy_req) {
10434 ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10435 mib2_ipIfStatsEntry_t);
10436 iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10437 } else {
10438 ise_size = sizeof (mib2_ipIfStatsEntry_t);
10439 iae_size = sizeof (mib2_ipv6AddrEntry_t);
10442 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10443 optp->level = MIB2_IP6;
10444 optp->name = 0;
10445 /* Include "unknown interface" ip6_mib */
10446 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10447 ipst->ips_ip6_mib.ipIfStatsIfIndex =
10448 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10449 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10450 ipst->ips_ipv6_forwarding ? 1 : 2);
10451 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10452 ipst->ips_ipv6_def_hops);
10453 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10454 sizeof (mib2_ipIfStatsEntry_t));
10455 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10456 sizeof (mib2_ipv6AddrEntry_t));
10457 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10458 sizeof (mib2_ipv6RouteEntry_t));
10459 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10460 sizeof (mib2_ipv6NetToMediaEntry_t));
10461 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10462 sizeof (ipv6_member_t));
10463 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10464 sizeof (ipv6_grpsrc_t));
10467 * Synchronize 64- and 32-bit counters
10469 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10470 ipIfStatsHCInReceives);
10471 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10472 ipIfStatsHCInDelivers);
10473 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10474 ipIfStatsHCOutRequests);
10475 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10476 ipIfStatsHCOutForwDatagrams);
10477 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10478 ipIfStatsHCOutMcastPkts);
10479 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10480 ipIfStatsHCInMcastPkts);
10482 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10483 (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10484 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10485 (uint_t)ise_size));
10486 } else if (legacy_req) {
10487 /* Adjust the EntrySize fields for legacy requests. */
10488 ise =
10489 (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10490 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10491 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10494 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10495 ill = ILL_START_WALK_V6(&ctx, ipst);
10496 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10497 ill->ill_ip_mib->ipIfStatsIfIndex =
10498 ill->ill_phyint->phyint_ifindex;
10499 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10500 ipst->ips_ipv6_forwarding ? 1 : 2);
10501 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10502 ill->ill_max_hops);
10505 * Synchronize 64- and 32-bit counters
10507 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10508 ipIfStatsHCInReceives);
10509 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10510 ipIfStatsHCInDelivers);
10511 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10512 ipIfStatsHCOutRequests);
10513 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10514 ipIfStatsHCOutForwDatagrams);
10515 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10516 ipIfStatsHCOutMcastPkts);
10517 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10518 ipIfStatsHCInMcastPkts);
10520 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10521 (char *)ill->ill_ip_mib, (int)ise_size)) {
10522 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10523 "%u bytes\n", (uint_t)ise_size));
10524 } else if (legacy_req) {
10525 /* Adjust the EntrySize fields for legacy requests. */
10526 ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10527 (int)ise_size);
10528 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10529 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10532 rw_exit(&ipst->ips_ill_g_lock);
10534 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10535 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10536 (int)optp->level, (int)optp->name, (int)optp->len));
10537 qreply(q, mpctl);
10538 return (mp2ctl);
10542 * ICMPv6 mib: One per ill
10544 static mblk_t *
10545 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10547 struct opthdr *optp;
10548 mblk_t *mp2ctl;
10549 ill_t *ill;
10550 ill_walk_context_t ctx;
10551 mblk_t *mp_tail = NULL;
10553 * Make a copy of the original message
10555 mp2ctl = copymsg(mpctl);
10557 /* fixed length ICMPv6 structure ... */
10559 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10560 optp->level = MIB2_ICMP6;
10561 optp->name = 0;
10562 /* Include "unknown interface" icmp6_mib */
10563 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10564 MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10565 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10566 sizeof (mib2_ipv6IfIcmpEntry_t);
10567 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10568 (char *)&ipst->ips_icmp6_mib,
10569 (int)sizeof (ipst->ips_icmp6_mib))) {
10570 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10571 (uint_t)sizeof (ipst->ips_icmp6_mib)));
10574 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10575 ill = ILL_START_WALK_V6(&ctx, ipst);
10576 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10577 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10578 ill->ill_phyint->phyint_ifindex;
10579 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10580 (char *)ill->ill_icmp6_mib,
10581 (int)sizeof (*ill->ill_icmp6_mib))) {
10582 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10583 "%u bytes\n",
10584 (uint_t)sizeof (*ill->ill_icmp6_mib)));
10587 rw_exit(&ipst->ips_ill_g_lock);
10589 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10590 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10591 (int)optp->level, (int)optp->name, (int)optp->len));
10592 qreply(q, mpctl);
10593 return (mp2ctl);
10597 * ire_walk routine to create ipRouteEntryTable in one IRE walk
10599 static void
10600 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10602 ill_t *ill;
10603 mib2_ipRouteEntry_t *re;
10604 ip_stack_t *ipst = ire->ire_ipst;
10606 ASSERT(ire->ire_ipversion == IPV4_VERSION);
10608 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10609 if (ire->ire_testhidden)
10610 return;
10611 if (ire->ire_type & IRE_IF_CLONE)
10612 return;
10615 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10616 return;
10619 * Return all IRE types for route table... let caller pick and choose
10621 re->ipRouteDest = ire->ire_addr;
10622 ill = ire->ire_ill;
10623 re->ipRouteIfIndex.o_length = 0;
10624 if (ill != NULL) {
10625 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10626 re->ipRouteIfIndex.o_length =
10627 mi_strlen(re->ipRouteIfIndex.o_bytes);
10629 re->ipRouteMetric1 = -1;
10630 re->ipRouteMetric2 = -1;
10631 re->ipRouteMetric3 = -1;
10632 re->ipRouteMetric4 = -1;
10634 re->ipRouteNextHop = ire->ire_gateway_addr;
10635 /* indirect(4), direct(3), or invalid(2) */
10636 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10637 re->ipRouteType = 2;
10638 else if (ire->ire_type & IRE_ONLINK)
10639 re->ipRouteType = 3;
10640 else
10641 re->ipRouteType = 4;
10643 re->ipRouteProto = -1;
10644 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10645 re->ipRouteMask = ire->ire_mask;
10646 re->ipRouteMetric5 = -1;
10647 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10648 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10649 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10651 re->ipRouteInfo.re_frag_flag = 0;
10652 re->ipRouteInfo.re_rtt = 0;
10653 re->ipRouteInfo.re_src_addr = 0;
10654 re->ipRouteInfo.re_ref = ire->ire_refcnt;
10655 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10656 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10657 re->ipRouteInfo.re_flags = ire->ire_flags;
10659 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10660 if (ire->ire_type & IRE_INTERFACE) {
10661 ire_t *child;
10663 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10664 child = ire->ire_dep_children;
10665 while (child != NULL) {
10666 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10667 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10668 child = child->ire_dep_sib_next;
10670 rw_exit(&ipst->ips_ire_dep_lock);
10673 if (ire->ire_flags & RTF_DYNAMIC) {
10674 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT;
10675 } else {
10676 re->ipRouteInfo.re_ire_type = ire->ire_type;
10679 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
10680 (char *)re, (int)sizeof (*re))) {
10681 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
10682 (uint_t)sizeof (*re)));
10685 /* bump route index for next pass */
10686 ird->ird_idx++;
10688 kmem_free(re, sizeof (*re));
10692 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
10694 static void
10695 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
10697 ill_t *ill;
10698 mib2_ipv6RouteEntry_t *re;
10699 ip_stack_t *ipst = ire->ire_ipst;
10701 ASSERT(ire->ire_ipversion == IPV6_VERSION);
10703 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10704 if (ire->ire_testhidden)
10705 return;
10706 if (ire->ire_type & IRE_IF_CLONE)
10707 return;
10710 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10711 return;
10714 * Return all IRE types for route table... let caller pick and choose
10716 re->ipv6RouteDest = ire->ire_addr_v6;
10717 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
10718 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
10719 re->ipv6RouteIfIndex.o_length = 0;
10720 ill = ire->ire_ill;
10721 if (ill != NULL) {
10722 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
10723 re->ipv6RouteIfIndex.o_length =
10724 mi_strlen(re->ipv6RouteIfIndex.o_bytes);
10727 ASSERT(!(ire->ire_type & IRE_BROADCAST));
10729 mutex_enter(&ire->ire_lock);
10730 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
10731 mutex_exit(&ire->ire_lock);
10733 /* remote(4), local(3), or discard(2) */
10734 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10735 re->ipv6RouteType = 2;
10736 else if (ire->ire_type & IRE_ONLINK)
10737 re->ipv6RouteType = 3;
10738 else
10739 re->ipv6RouteType = 4;
10741 re->ipv6RouteProtocol = -1;
10742 re->ipv6RoutePolicy = 0;
10743 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time;
10744 re->ipv6RouteNextHopRDI = 0;
10745 re->ipv6RouteWeight = 0;
10746 re->ipv6RouteMetric = 0;
10747 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10748 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
10749 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10751 re->ipv6RouteInfo.re_frag_flag = 0;
10752 re->ipv6RouteInfo.re_rtt = 0;
10753 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros;
10754 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10755 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10756 re->ipv6RouteInfo.re_ref = ire->ire_refcnt;
10757 re->ipv6RouteInfo.re_flags = ire->ire_flags;
10759 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10760 if (ire->ire_type & IRE_INTERFACE) {
10761 ire_t *child;
10763 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10764 child = ire->ire_dep_children;
10765 while (child != NULL) {
10766 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
10767 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10768 child = child->ire_dep_sib_next;
10770 rw_exit(&ipst->ips_ire_dep_lock);
10772 if (ire->ire_flags & RTF_DYNAMIC) {
10773 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT;
10774 } else {
10775 re->ipv6RouteInfo.re_ire_type = ire->ire_type;
10778 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
10779 (char *)re, (int)sizeof (*re))) {
10780 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
10781 (uint_t)sizeof (*re)));
10784 /* bump route index for next pass */
10785 ird->ird_idx++;
10787 kmem_free(re, sizeof (*re));
10791 * ncec_walk routine to create ipv6NetToMediaEntryTable
10793 static int
10794 ip_snmp_get2_v6_media(ncec_t *ncec, iproutedata_t *ird)
10796 ill_t *ill;
10797 mib2_ipv6NetToMediaEntry_t ntme;
10799 ill = ncec->ncec_ill;
10800 /* skip arpce entries, and loopback ncec entries */
10801 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
10802 return (0);
10804 * Neighbor cache entry attached to IRE with on-link
10805 * destination.
10806 * We report all IPMP groups on ncec_ill which is normally the upper.
10808 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
10809 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
10810 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
10811 if (ncec->ncec_lladdr != NULL) {
10812 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
10813 ntme.ipv6NetToMediaPhysAddress.o_length);
10816 * Note: Returns ND_* states. Should be:
10817 * reachable(1), stale(2), delay(3), probe(4),
10818 * invalid(5), unknown(6)
10820 ntme.ipv6NetToMediaState = ncec->ncec_state;
10821 ntme.ipv6NetToMediaLastUpdated = 0;
10823 /* other(1), dynamic(2), static(3), local(4) */
10824 if (NCE_MYADDR(ncec)) {
10825 ntme.ipv6NetToMediaType = 4;
10826 } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
10827 ntme.ipv6NetToMediaType = 1; /* proxy */
10828 } else if (ncec->ncec_flags & NCE_F_STATIC) {
10829 ntme.ipv6NetToMediaType = 3;
10830 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
10831 ntme.ipv6NetToMediaType = 1;
10832 } else {
10833 ntme.ipv6NetToMediaType = 2;
10836 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
10837 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
10838 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
10839 (uint_t)sizeof (ntme)));
10841 return (0);
10845 nce2ace(ncec_t *ncec)
10847 int flags = 0;
10849 if (NCE_ISREACHABLE(ncec))
10850 flags |= ACE_F_RESOLVED;
10851 if (ncec->ncec_flags & NCE_F_AUTHORITY)
10852 flags |= ACE_F_AUTHORITY;
10853 if (ncec->ncec_flags & NCE_F_PUBLISH)
10854 flags |= ACE_F_PUBLISH;
10855 if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
10856 flags |= ACE_F_PERMANENT;
10857 if (NCE_MYADDR(ncec))
10858 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
10859 if (ncec->ncec_flags & NCE_F_UNVERIFIED)
10860 flags |= ACE_F_UNVERIFIED;
10861 if (ncec->ncec_flags & NCE_F_AUTHORITY)
10862 flags |= ACE_F_AUTHORITY;
10863 if (ncec->ncec_flags & NCE_F_DELAYED)
10864 flags |= ACE_F_DELAYED;
10865 return (flags);
10869 * ncec_walk routine to create ipNetToMediaEntryTable
10871 static int
10872 ip_snmp_get2_v4_media(ncec_t *ncec, iproutedata_t *ird)
10874 ill_t *ill;
10875 mib2_ipNetToMediaEntry_t ntme;
10876 const char *name = "unknown";
10877 ipaddr_t ncec_addr;
10879 ill = ncec->ncec_ill;
10880 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
10881 ill->ill_net_type == IRE_LOOPBACK)
10882 return (0);
10884 /* We report all IPMP groups on ncec_ill which is normally the upper. */
10885 name = ill->ill_name;
10886 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
10887 if (NCE_MYADDR(ncec)) {
10888 ntme.ipNetToMediaType = 4;
10889 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
10890 ntme.ipNetToMediaType = 1;
10891 } else {
10892 ntme.ipNetToMediaType = 3;
10894 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
10895 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
10896 ntme.ipNetToMediaIfIndex.o_length);
10898 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
10899 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
10901 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
10902 ncec_addr = INADDR_BROADCAST;
10903 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
10904 sizeof (ncec_addr));
10906 * map all the flags to the ACE counterpart.
10908 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
10910 ntme.ipNetToMediaPhysAddress.o_length =
10911 MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
10913 if (!NCE_ISREACHABLE(ncec))
10914 ntme.ipNetToMediaPhysAddress.o_length = 0;
10915 else {
10916 if (ncec->ncec_lladdr != NULL) {
10917 bcopy(ncec->ncec_lladdr,
10918 ntme.ipNetToMediaPhysAddress.o_bytes,
10919 ntme.ipNetToMediaPhysAddress.o_length);
10923 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
10924 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
10925 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
10926 (uint_t)sizeof (ntme)));
10928 return (0);
10932 * return (0) if invalid set request, 1 otherwise, including non-tcp requests
10934 /* ARGSUSED */
10936 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
10938 switch (level) {
10939 case MIB2_IP:
10940 case MIB2_ICMP:
10941 switch (name) {
10942 default:
10943 break;
10945 return (1);
10946 default:
10947 return (1);
10952 * When there exists both a 64- and 32-bit counter of a particular type
10953 * (i.e., InReceives), only the 64-bit counters are added.
10955 void
10956 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
10958 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
10959 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
10960 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
10961 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
10962 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
10963 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
10964 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
10965 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
10966 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
10967 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
10968 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
10969 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
10970 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
10971 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
10972 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
10973 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
10974 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
10975 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
10976 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
10977 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
10978 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
10979 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
10980 o2->ipIfStatsInWrongIPVersion);
10981 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
10982 o2->ipIfStatsInWrongIPVersion);
10983 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
10984 o2->ipIfStatsOutSwitchIPVersion);
10985 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
10986 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
10987 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
10988 o2->ipIfStatsHCInForwDatagrams);
10989 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
10990 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
10991 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
10992 o2->ipIfStatsHCOutForwDatagrams);
10993 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
10994 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
10995 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
10996 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
10997 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
10998 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
10999 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11000 o2->ipIfStatsHCOutMcastOctets);
11001 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11002 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11003 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11004 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11005 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11006 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11007 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11010 void
11011 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11013 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11014 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11015 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11016 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11017 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11018 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11019 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11020 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11021 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11022 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11023 o2->ipv6IfIcmpInRouterSolicits);
11024 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11025 o2->ipv6IfIcmpInRouterAdvertisements);
11026 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11027 o2->ipv6IfIcmpInNeighborSolicits);
11028 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11029 o2->ipv6IfIcmpInNeighborAdvertisements);
11030 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11031 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11032 o2->ipv6IfIcmpInGroupMembQueries);
11033 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11034 o2->ipv6IfIcmpInGroupMembResponses);
11035 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11036 o2->ipv6IfIcmpInGroupMembReductions);
11037 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11038 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11039 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11040 o2->ipv6IfIcmpOutDestUnreachs);
11041 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11042 o2->ipv6IfIcmpOutAdminProhibs);
11043 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11044 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11045 o2->ipv6IfIcmpOutParmProblems);
11046 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11047 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11048 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11049 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11050 o2->ipv6IfIcmpOutRouterSolicits);
11051 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11052 o2->ipv6IfIcmpOutRouterAdvertisements);
11053 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11054 o2->ipv6IfIcmpOutNeighborSolicits);
11055 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11056 o2->ipv6IfIcmpOutNeighborAdvertisements);
11057 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11058 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11059 o2->ipv6IfIcmpOutGroupMembQueries);
11060 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11061 o2->ipv6IfIcmpOutGroupMembResponses);
11062 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11063 o2->ipv6IfIcmpOutGroupMembReductions);
11064 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11065 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11066 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11067 o2->ipv6IfIcmpInBadNeighborAdvertisements);
11068 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11069 o2->ipv6IfIcmpInBadNeighborSolicitations);
11070 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11071 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11072 o2->ipv6IfIcmpInGroupMembTotal);
11073 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11074 o2->ipv6IfIcmpInGroupMembBadQueries);
11075 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11076 o2->ipv6IfIcmpInGroupMembBadReports);
11077 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11078 o2->ipv6IfIcmpInGroupMembOurReports);
11082 * Called before the options are updated to check if this packet will
11083 * be source routed from here.
11084 * This routine assumes that the options are well formed i.e. that they
11085 * have already been checked.
11087 boolean_t
11088 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11090 ipoptp_t opts;
11091 uchar_t *opt;
11092 uint8_t optval;
11093 uint8_t optlen;
11094 ipaddr_t dst;
11096 if (IS_SIMPLE_IPH(ipha)) {
11097 ip2dbg(("not source routed\n"));
11098 return (B_FALSE);
11100 dst = ipha->ipha_dst;
11101 for (optval = ipoptp_first(&opts, ipha);
11102 optval != IPOPT_EOL;
11103 optval = ipoptp_next(&opts)) {
11104 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11105 opt = opts.ipoptp_cur;
11106 optlen = opts.ipoptp_len;
11107 ip2dbg(("ip_source_routed: opt %d, len %d\n",
11108 optval, optlen));
11109 switch (optval) {
11110 uint32_t off;
11111 case IPOPT_SSRR:
11112 case IPOPT_LSRR:
11114 * If dst is one of our addresses and there are some
11115 * entries left in the source route return (true).
11117 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11118 ip2dbg(("ip_source_routed: not next"
11119 " source route 0x%x\n",
11120 ntohl(dst)));
11121 return (B_FALSE);
11123 off = opt[IPOPT_OFFSET];
11124 off--;
11125 if (optlen < IP_ADDR_LEN ||
11126 off > optlen - IP_ADDR_LEN) {
11127 /* End of source route */
11128 ip1dbg(("ip_source_routed: end of SR\n"));
11129 return (B_FALSE);
11131 return (B_TRUE);
11134 ip2dbg(("not source routed\n"));
11135 return (B_FALSE);
11139 * ip_unbind is called by the transports to remove a conn from
11140 * the fanout table.
11142 void
11143 ip_unbind(conn_t *connp)
11146 ASSERT(!MUTEX_HELD(&connp->conn_lock));
11148 ipcl_hash_remove(connp);
11152 * Used for deciding the MSS size for the upper layer. Thus
11153 * we need to check the outbound policy values in the conn.
11156 conn_ipsec_length(conn_t *connp)
11158 ipsec_latch_t *ipl;
11160 ipl = connp->conn_latch;
11161 if (ipl == NULL)
11162 return (0);
11164 if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11165 return (0);
11167 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11171 * Returns an estimate of the IPsec headers size. This is used if
11172 * we don't want to call into IPsec to get the exact size.
11175 ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11177 ipsec_action_t *a;
11179 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11180 return (0);
11182 a = ixa->ixa_ipsec_action;
11183 if (a == NULL) {
11184 ASSERT(ixa->ixa_ipsec_policy != NULL);
11185 a = ixa->ixa_ipsec_policy->ipsp_act;
11187 ASSERT(a != NULL);
11189 return (a->ipa_ovhd);
11193 * If there are any source route options, return the true final
11194 * destination. Otherwise, return the destination.
11196 ipaddr_t
11197 ip_get_dst(ipha_t *ipha)
11199 ipoptp_t opts;
11200 uchar_t *opt;
11201 uint8_t optval;
11202 uint8_t optlen;
11203 ipaddr_t dst;
11204 uint32_t off;
11206 dst = ipha->ipha_dst;
11208 if (IS_SIMPLE_IPH(ipha))
11209 return (dst);
11211 for (optval = ipoptp_first(&opts, ipha);
11212 optval != IPOPT_EOL;
11213 optval = ipoptp_next(&opts)) {
11214 opt = opts.ipoptp_cur;
11215 optlen = opts.ipoptp_len;
11216 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11217 switch (optval) {
11218 case IPOPT_SSRR:
11219 case IPOPT_LSRR:
11220 off = opt[IPOPT_OFFSET];
11222 * If one of the conditions is true, it means
11223 * end of options and dst already has the right
11224 * value.
11226 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11227 off = optlen - IP_ADDR_LEN;
11228 bcopy(&opt[off], &dst, IP_ADDR_LEN);
11230 return (dst);
11231 default:
11232 break;
11236 return (dst);
11240 * Outbound IP fragmentation routine.
11241 * Assumes the caller has checked whether or not fragmentation should
11242 * be allowed. Here we copy the DF bit from the header to all the generated
11243 * fragments.
11246 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11247 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11248 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11250 int i1;
11251 int hdr_len;
11252 mblk_t *hdr_mp;
11253 ipha_t *ipha;
11254 int ip_data_end;
11255 int len;
11256 mblk_t *mp = mp_orig;
11257 int offset;
11258 ill_t *ill = nce->nce_ill;
11259 ip_stack_t *ipst = ill->ill_ipst;
11260 mblk_t *carve_mp;
11261 uint32_t frag_flag;
11262 uint_t priority = mp->b_band;
11263 int error = 0;
11265 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11267 if (pkt_len != msgdsize(mp)) {
11268 ip0dbg(("Packet length mismatch: %d, %ld\n",
11269 pkt_len, msgdsize(mp)));
11270 freemsg(mp);
11271 return (EINVAL);
11274 if (max_frag == 0) {
11275 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11276 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11277 ip_drop_output("FragFails: zero max_frag", mp, ill);
11278 freemsg(mp);
11279 return (EINVAL);
11282 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11283 ipha = (ipha_t *)mp->b_rptr;
11284 ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11285 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11288 * Establish the starting offset. May not be zero if we are fragging
11289 * a fragment that is being forwarded.
11291 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11293 /* TODO why is this test needed? */
11294 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11295 /* TODO: notify ulp somehow */
11296 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11297 ip_drop_output("FragFails: bad starting offset", mp, ill);
11298 freemsg(mp);
11299 return (EINVAL);
11302 hdr_len = IPH_HDR_LENGTH(ipha);
11303 ipha->ipha_hdr_checksum = 0;
11306 * Establish the number of bytes maximum per frag, after putting
11307 * in the header.
11309 len = (max_frag - hdr_len) & ~7;
11311 /* Get a copy of the header for the trailing frags */
11312 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11313 mp);
11314 if (hdr_mp == NULL) {
11315 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11316 ip_drop_output("FragFails: no hdr_mp", mp, ill);
11317 freemsg(mp);
11318 return (ENOBUFS);
11321 /* Store the starting offset, with the MoreFrags flag. */
11322 i1 = offset | IPH_MF | frag_flag;
11323 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11325 /* Establish the ending byte offset, based on the starting offset. */
11326 offset <<= 3;
11327 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11329 /* Store the length of the first fragment in the IP header. */
11330 i1 = len + hdr_len;
11331 ASSERT(i1 <= IP_MAXPACKET);
11332 ipha->ipha_length = htons((uint16_t)i1);
11335 * Compute the IP header checksum for the first frag. We have to
11336 * watch out that we stop at the end of the header.
11338 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11341 * Now carve off the first frag. Note that this will include the
11342 * original IP header.
11344 if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11345 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11346 ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11347 freeb(hdr_mp);
11348 freemsg(mp_orig);
11349 return (ENOBUFS);
11352 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11354 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11355 ixa_cookie);
11356 if (error != 0 && error != EWOULDBLOCK) {
11357 /* No point in sending the other fragments */
11358 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11359 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11360 freeb(hdr_mp);
11361 freemsg(mp_orig);
11362 return (error);
11365 /* No need to redo state machine in loop */
11366 ixaflags &= ~IXAF_REACH_CONF;
11368 /* Advance the offset to the second frag starting point. */
11369 offset += len;
11371 * Update hdr_len from the copied header - there might be less options
11372 * in the later fragments.
11374 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11375 /* Loop until done. */
11376 for (;;) {
11377 uint16_t offset_and_flags;
11378 uint16_t ip_len;
11380 if (ip_data_end - offset > len) {
11382 * Carve off the appropriate amount from the original
11383 * datagram.
11385 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11386 mp = NULL;
11387 break;
11390 * More frags after this one. Get another copy
11391 * of the header.
11393 if (carve_mp->b_datap->db_ref == 1 &&
11394 hdr_mp->b_wptr - hdr_mp->b_rptr <
11395 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11396 /* Inline IP header */
11397 carve_mp->b_rptr -= hdr_mp->b_wptr -
11398 hdr_mp->b_rptr;
11399 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11400 hdr_mp->b_wptr - hdr_mp->b_rptr);
11401 mp = carve_mp;
11402 } else {
11403 if (!(mp = copyb(hdr_mp))) {
11404 freemsg(carve_mp);
11405 break;
11407 /* Get priority marking, if any. */
11408 mp->b_band = priority;
11409 mp->b_cont = carve_mp;
11411 ipha = (ipha_t *)mp->b_rptr;
11412 offset_and_flags = IPH_MF;
11413 } else {
11415 * Last frag. Consume the header. Set len to
11416 * the length of this last piece.
11418 len = ip_data_end - offset;
11421 * Carve off the appropriate amount from the original
11422 * datagram.
11424 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11425 mp = NULL;
11426 break;
11428 if (carve_mp->b_datap->db_ref == 1 &&
11429 hdr_mp->b_wptr - hdr_mp->b_rptr <
11430 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11431 /* Inline IP header */
11432 carve_mp->b_rptr -= hdr_mp->b_wptr -
11433 hdr_mp->b_rptr;
11434 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11435 hdr_mp->b_wptr - hdr_mp->b_rptr);
11436 mp = carve_mp;
11437 freeb(hdr_mp);
11438 hdr_mp = mp;
11439 } else {
11440 mp = hdr_mp;
11441 /* Get priority marking, if any. */
11442 mp->b_band = priority;
11443 mp->b_cont = carve_mp;
11445 ipha = (ipha_t *)mp->b_rptr;
11446 /* A frag of a frag might have IPH_MF non-zero */
11447 offset_and_flags =
11448 ntohs(ipha->ipha_fragment_offset_and_flags) &
11449 IPH_MF;
11451 offset_and_flags |= (uint16_t)(offset >> 3);
11452 offset_and_flags |= (uint16_t)frag_flag;
11453 /* Store the offset and flags in the IP header. */
11454 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11456 /* Store the length in the IP header. */
11457 ip_len = (uint16_t)(len + hdr_len);
11458 ipha->ipha_length = htons(ip_len);
11461 * Set the IP header checksum. Note that mp is just
11462 * the header, so this is easy to pass to ip_csum.
11464 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11466 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11468 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11469 nolzid, ixa_cookie);
11470 /* All done if we just consumed the hdr_mp. */
11471 if (mp == hdr_mp) {
11472 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11473 return (error);
11475 if (error != 0 && error != EWOULDBLOCK) {
11476 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11477 mblk_t *, hdr_mp);
11478 /* No point in sending the other fragments */
11479 break;
11482 /* Otherwise, advance and loop. */
11483 offset += len;
11485 /* Clean up following allocation failure. */
11486 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11487 ip_drop_output("FragFails: loop ended", NULL, ill);
11488 if (mp != hdr_mp)
11489 freeb(hdr_mp);
11490 if (mp != mp_orig)
11491 freemsg(mp_orig);
11492 return (error);
11496 * Copy the header plus those options which have the copy bit set
11498 static mblk_t *
11499 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11500 mblk_t *src)
11502 mblk_t *mp;
11503 uchar_t *up;
11506 * Quick check if we need to look for options without the copy bit
11507 * set
11509 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11510 if (!mp)
11511 return (mp);
11512 mp->b_rptr += ipst->ips_ip_wroff_extra;
11513 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11514 bcopy(rptr, mp->b_rptr, hdr_len);
11515 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11516 return (mp);
11518 up = mp->b_rptr;
11519 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11520 up += IP_SIMPLE_HDR_LENGTH;
11521 rptr += IP_SIMPLE_HDR_LENGTH;
11522 hdr_len -= IP_SIMPLE_HDR_LENGTH;
11523 while (hdr_len > 0) {
11524 uint32_t optval;
11525 uint32_t optlen;
11527 optval = *rptr;
11528 if (optval == IPOPT_EOL)
11529 break;
11530 if (optval == IPOPT_NOP)
11531 optlen = 1;
11532 else
11533 optlen = rptr[1];
11534 if (optval & IPOPT_COPY) {
11535 bcopy(rptr, up, optlen);
11536 up += optlen;
11538 rptr += optlen;
11539 hdr_len -= optlen;
11542 * Make sure that we drop an even number of words by filling
11543 * with EOL to the next word boundary.
11545 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11546 hdr_len & 0x3; hdr_len++)
11547 *up++ = IPOPT_EOL;
11548 mp->b_wptr = up;
11549 /* Update header length */
11550 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11551 return (mp);
11555 * Update any source route, record route, or timestamp options when
11556 * sending a packet back to ourselves.
11557 * Check that we are at end of strict source route.
11558 * The options have been sanity checked by ip_output_options().
11560 void
11561 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11563 ipoptp_t opts;
11564 uchar_t *opt;
11565 uint8_t optval;
11566 uint8_t optlen;
11567 ipaddr_t dst;
11568 uint32_t ts;
11569 timestruc_t now;
11571 for (optval = ipoptp_first(&opts, ipha);
11572 optval != IPOPT_EOL;
11573 optval = ipoptp_next(&opts)) {
11574 opt = opts.ipoptp_cur;
11575 optlen = opts.ipoptp_len;
11576 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11577 switch (optval) {
11578 uint32_t off;
11579 case IPOPT_SSRR:
11580 case IPOPT_LSRR:
11581 off = opt[IPOPT_OFFSET];
11582 off--;
11583 if (optlen < IP_ADDR_LEN ||
11584 off > optlen - IP_ADDR_LEN) {
11585 /* End of source route */
11586 break;
11589 * This will only happen if two consecutive entries
11590 * in the source route contains our address or if
11591 * it is a packet with a loose source route which
11592 * reaches us before consuming the whole source route
11595 if (optval == IPOPT_SSRR) {
11596 return;
11599 * Hack: instead of dropping the packet truncate the
11600 * source route to what has been used by filling the
11601 * rest with IPOPT_NOP.
11603 opt[IPOPT_OLEN] = (uint8_t)off;
11604 while (off < optlen) {
11605 opt[off++] = IPOPT_NOP;
11607 break;
11608 case IPOPT_RR:
11609 off = opt[IPOPT_OFFSET];
11610 off--;
11611 if (optlen < IP_ADDR_LEN ||
11612 off > optlen - IP_ADDR_LEN) {
11613 /* No more room - ignore */
11614 ip1dbg((
11615 "ip_output_local_options: end of RR\n"));
11616 break;
11618 dst = htonl(INADDR_LOOPBACK);
11619 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11620 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11621 break;
11622 case IPOPT_TS:
11623 /* Insert timestamp if there is romm */
11624 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11625 case IPOPT_TS_TSONLY:
11626 off = IPOPT_TS_TIMELEN;
11627 break;
11628 case IPOPT_TS_PRESPEC:
11629 case IPOPT_TS_PRESPEC_RFC791:
11630 /* Verify that the address matched */
11631 off = opt[IPOPT_OFFSET] - 1;
11632 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
11633 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11634 /* Not for us */
11635 break;
11637 /* FALLTHROUGH */
11638 case IPOPT_TS_TSANDADDR:
11639 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
11640 break;
11641 default:
11643 * ip_*put_options should have already
11644 * dropped this packet.
11646 cmn_err(CE_PANIC, "ip_output_local_options: "
11647 "unknown IT - bug in ip_output_options?\n");
11648 return; /* Keep "lint" happy */
11650 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
11651 /* Increase overflow counter */
11652 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
11653 opt[IPOPT_POS_OV_FLG] = (uint8_t)
11654 (opt[IPOPT_POS_OV_FLG] & 0x0F) |
11655 (off << 4);
11656 break;
11658 off = opt[IPOPT_OFFSET] - 1;
11659 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11660 case IPOPT_TS_PRESPEC:
11661 case IPOPT_TS_PRESPEC_RFC791:
11662 case IPOPT_TS_TSANDADDR:
11663 dst = htonl(INADDR_LOOPBACK);
11664 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11665 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11666 /* FALLTHROUGH */
11667 case IPOPT_TS_TSONLY:
11668 off = opt[IPOPT_OFFSET] - 1;
11669 /* Compute # of milliseconds since midnight */
11670 gethrestime(&now);
11671 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
11672 NSEC2MSEC(now.tv_nsec);
11673 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
11674 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
11675 break;
11677 break;
11683 * Prepend an M_DATA fastpath header, and if none present prepend a
11684 * DL_UNITDATA_REQ. Frees the mblk on failure.
11686 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
11687 * If there is a change to them, the nce will be deleted (condemned) and
11688 * a new nce_t will be created when packets are sent. Thus we need no locks
11689 * to access those fields.
11691 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
11692 * we place b_band in dl_priority.dl_max.
11694 static mblk_t *
11695 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
11697 uint_t hlen;
11698 mblk_t *mp1;
11699 uint_t priority;
11700 uchar_t *rptr;
11702 rptr = mp->b_rptr;
11704 ASSERT(DB_TYPE(mp) == M_DATA);
11705 priority = mp->b_band;
11707 ASSERT(nce != NULL);
11708 if ((mp1 = nce->nce_fp_mp) != NULL) {
11709 hlen = MBLKL(mp1);
11711 * Check if we have enough room to prepend fastpath
11712 * header
11714 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
11715 rptr -= hlen;
11716 bcopy(mp1->b_rptr, rptr, hlen);
11718 * Set the b_rptr to the start of the link layer
11719 * header
11721 mp->b_rptr = rptr;
11722 return (mp);
11724 mp1 = copyb(mp1);
11725 if (mp1 == NULL) {
11726 ill_t *ill = nce->nce_ill;
11728 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
11729 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
11730 freemsg(mp);
11731 return (NULL);
11733 mp1->b_band = priority;
11734 mp1->b_cont = mp;
11735 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
11736 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
11737 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
11738 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
11739 DB_LSOMSS(mp1) = DB_LSOMSS(mp);
11740 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
11742 * XXX disable ICK_VALID and compute checksum
11743 * here; can happen if nce_fp_mp changes and
11744 * it can't be copied now due to insufficient
11745 * space. (unlikely, fp mp can change, but it
11746 * does not increase in length)
11748 return (mp1);
11750 mp1 = copyb(nce->nce_dlur_mp);
11752 if (mp1 == NULL) {
11753 ill_t *ill = nce->nce_ill;
11755 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
11756 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
11757 freemsg(mp);
11758 return (NULL);
11760 mp1->b_cont = mp;
11761 if (priority != 0) {
11762 mp1->b_band = priority;
11763 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
11764 priority;
11766 return (mp1);
11770 * Finish the outbound IPsec processing. This function is called from
11771 * ipsec_out_process() if the IPsec packet was processed
11772 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
11773 * asynchronously.
11775 * This is common to IPv4 and IPv6.
11778 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
11780 iaflags_t ixaflags = ixa->ixa_flags;
11781 uint_t pktlen;
11784 /* AH/ESP don't update ixa_pktlen when they modify the packet */
11785 if (ixaflags & IXAF_IS_IPV4) {
11786 ipha_t *ipha = (ipha_t *)mp->b_rptr;
11788 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
11789 pktlen = ntohs(ipha->ipha_length);
11790 } else {
11791 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
11793 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
11794 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
11798 * We release any hard reference on the SAs here to make
11799 * sure the SAs can be garbage collected. ipsr_sa has a soft reference
11800 * on the SAs.
11801 * If in the future we want the hard latching of the SAs in the
11802 * ip_xmit_attr_t then we should remove this.
11804 if (ixa->ixa_ipsec_esp_sa != NULL) {
11805 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
11806 ixa->ixa_ipsec_esp_sa = NULL;
11808 if (ixa->ixa_ipsec_ah_sa != NULL) {
11809 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
11810 ixa->ixa_ipsec_ah_sa = NULL;
11813 /* Do we need to fragment? */
11814 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
11815 pktlen > ixa->ixa_fragsize) {
11816 if (ixaflags & IXAF_IS_IPV4) {
11817 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
11819 * We check for the DF case in ipsec_out_process
11820 * hence this only handles the non-DF case.
11822 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
11823 pktlen, ixa->ixa_fragsize,
11824 ixa->ixa_xmit_hint, ixa->ixa_zoneid,
11825 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
11826 &ixa->ixa_cookie));
11827 } else {
11828 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
11829 if (mp == NULL) {
11830 /* MIB and ip_drop_output already done */
11831 return (ENOMEM);
11833 pktlen += sizeof (ip6_frag_t);
11834 if (pktlen > ixa->ixa_fragsize) {
11835 return (ip_fragment_v6(mp, ixa->ixa_nce,
11836 ixa->ixa_flags, pktlen,
11837 ixa->ixa_fragsize, ixa->ixa_xmit_hint,
11838 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
11839 ixa->ixa_postfragfn, &ixa->ixa_cookie));
11843 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
11844 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
11845 ixa->ixa_no_loop_zoneid, NULL));
11849 * Finish the inbound IPsec processing. This function is called from
11850 * ipsec_out_process() if the IPsec packet was processed
11851 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
11852 * asynchronously.
11854 * This is common to IPv4 and IPv6.
11856 void
11857 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
11859 iaflags_t iraflags = ira->ira_flags;
11861 /* Length might have changed */
11862 if (iraflags & IRAF_IS_IPV4) {
11863 ipha_t *ipha = (ipha_t *)mp->b_rptr;
11865 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
11866 ira->ira_pktlen = ntohs(ipha->ipha_length);
11867 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
11868 ira->ira_protocol = ipha->ipha_protocol;
11870 ip_fanout_v4(mp, ipha, ira);
11871 } else {
11872 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
11873 uint8_t *nexthdrp;
11875 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
11876 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
11877 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
11878 &nexthdrp)) {
11879 /* Malformed packet */
11880 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
11881 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
11882 freemsg(mp);
11883 return;
11885 ira->ira_protocol = *nexthdrp;
11886 ip_fanout_v6(mp, ip6h, ira);
11891 * Select which AH & ESP SA's to use (if any) for the outbound packet.
11893 * If this function returns B_TRUE, the requested SA's have been filled
11894 * into the ixa_ipsec_*_sa pointers.
11896 * If the function returns B_FALSE, the packet has been "consumed", most
11897 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
11899 * The SA references created by the protocol-specific "select"
11900 * function will be released in ip_output_post_ipsec.
11902 static boolean_t
11903 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
11905 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
11906 ipsec_policy_t *pp;
11907 ipsec_action_t *ap;
11909 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
11910 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
11911 (ixa->ixa_ipsec_action != NULL));
11913 ap = ixa->ixa_ipsec_action;
11914 if (ap == NULL) {
11915 pp = ixa->ixa_ipsec_policy;
11916 ASSERT(pp != NULL);
11917 ap = pp->ipsp_act;
11918 ASSERT(ap != NULL);
11922 * We have an action. now, let's select SA's.
11923 * A side effect of setting ixa_ipsec_*_sa is that it will
11924 * be cached in the conn_t.
11926 if (ap->ipa_want_esp) {
11927 if (ixa->ixa_ipsec_esp_sa == NULL) {
11928 need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
11929 IPPROTO_ESP);
11931 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
11934 if (ap->ipa_want_ah) {
11935 if (ixa->ixa_ipsec_ah_sa == NULL) {
11936 need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
11937 IPPROTO_AH);
11939 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
11941 * The ESP and AH processing order needs to be preserved
11942 * when both protocols are required (ESP should be applied
11943 * before AH for an outbound packet). Force an ESP ACQUIRE
11944 * when both ESP and AH are required, and an AH ACQUIRE
11945 * is needed.
11947 if (ap->ipa_want_esp && need_ah_acquire)
11948 need_esp_acquire = B_TRUE;
11952 * Send an ACQUIRE (extended, regular, or both) if we need one.
11953 * Release SAs that got referenced, but will not be used until we
11954 * acquire _all_ of the SAs we need.
11956 if (need_ah_acquire || need_esp_acquire) {
11957 if (ixa->ixa_ipsec_ah_sa != NULL) {
11958 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
11959 ixa->ixa_ipsec_ah_sa = NULL;
11961 if (ixa->ixa_ipsec_esp_sa != NULL) {
11962 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
11963 ixa->ixa_ipsec_esp_sa = NULL;
11966 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
11967 return (B_FALSE);
11970 return (B_TRUE);
11974 * Handle IPsec output processing.
11975 * This function is only entered once for a given packet.
11976 * We try to do things synchronously, but if we need to have user-level
11977 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
11978 * will be completed
11979 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
11980 * - when asynchronous ESP is done it will do AH
11982 * In all cases we come back in ip_output_post_ipsec() to fragment and
11983 * send out the packet.
11986 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
11988 ill_t *ill = ixa->ixa_nce->nce_ill;
11989 ip_stack_t *ipst = ixa->ixa_ipst;
11990 ipsec_stack_t *ipss;
11991 ipsec_policy_t *pp;
11992 ipsec_action_t *ap;
11994 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
11996 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
11997 (ixa->ixa_ipsec_action != NULL));
11999 ipss = ipst->ips_netstack->netstack_ipsec;
12000 if (!ipsec_loaded(ipss)) {
12001 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12002 ip_drop_packet(mp, B_TRUE, ill,
12003 DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12004 &ipss->ipsec_dropper);
12005 return (ENOTSUP);
12008 ap = ixa->ixa_ipsec_action;
12009 if (ap == NULL) {
12010 pp = ixa->ixa_ipsec_policy;
12011 ASSERT(pp != NULL);
12012 ap = pp->ipsp_act;
12013 ASSERT(ap != NULL);
12016 /* Handle explicit drop action and bypass. */
12017 switch (ap->ipa_act.ipa_type) {
12018 case IPSEC_ACT_DISCARD:
12019 case IPSEC_ACT_REJECT:
12020 ip_drop_packet(mp, B_FALSE, ill,
12021 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12022 return (EHOSTUNREACH); /* IPsec policy failure */
12023 case IPSEC_ACT_BYPASS:
12024 return (ip_output_post_ipsec(mp, ixa));
12028 * The order of processing is first insert a IP header if needed.
12029 * Then insert the ESP header and then the AH header.
12031 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12033 * First get the outer IP header before sending
12034 * it to ESP.
12036 ipha_t *oipha, *iipha;
12037 mblk_t *outer_mp, *inner_mp;
12039 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12040 (void) mi_strlog(ill->ill_rq, 0,
12041 SL_ERROR|SL_TRACE|SL_CONSOLE,
12042 "ipsec_out_process: "
12043 "Self-Encapsulation failed: Out of memory\n");
12044 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12045 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12046 freemsg(mp);
12047 return (ENOBUFS);
12049 inner_mp = mp;
12050 ASSERT(inner_mp->b_datap->db_type == M_DATA);
12051 oipha = (ipha_t *)outer_mp->b_rptr;
12052 iipha = (ipha_t *)inner_mp->b_rptr;
12053 *oipha = *iipha;
12054 outer_mp->b_wptr += sizeof (ipha_t);
12055 oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12056 sizeof (ipha_t));
12057 oipha->ipha_protocol = IPPROTO_ENCAP;
12058 oipha->ipha_version_and_hdr_length =
12059 IP_SIMPLE_HDR_VERSION;
12060 oipha->ipha_hdr_checksum = 0;
12061 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12062 outer_mp->b_cont = inner_mp;
12063 mp = outer_mp;
12065 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12068 /* If we need to wait for a SA then we can't return any errno */
12069 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12070 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12071 !ipsec_out_select_sa(mp, ixa))
12072 return (0);
12075 * By now, we know what SA's to use. Toss over to ESP & AH
12076 * to do the heavy lifting.
12078 if (ap->ipa_want_esp) {
12079 ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12081 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12082 if (mp == NULL) {
12084 * Either it failed or is pending. In the former case
12085 * ipIfStatsInDiscards was increased.
12087 return (0);
12091 if (ap->ipa_want_ah) {
12092 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12094 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12095 if (mp == NULL) {
12097 * Either it failed or is pending. In the former case
12098 * ipIfStatsInDiscards was increased.
12100 return (0);
12104 * We are done with IPsec processing. Send it over
12105 * the wire.
12107 return (ip_output_post_ipsec(mp, ixa));
12111 * ioctls that go through a down/up sequence may need to wait for the down
12112 * to complete. This involves waiting for the ire and ipif refcnts to go down
12113 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12115 /* ARGSUSED */
12116 void
12117 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12119 struct iocblk *iocp;
12120 mblk_t *mp1;
12121 ip_ioctl_cmd_t *ipip;
12122 int err;
12123 sin_t *sin;
12124 struct lifreq *lifr;
12125 struct ifreq *ifr;
12127 iocp = (struct iocblk *)mp->b_rptr;
12128 ASSERT(ipsq != NULL);
12129 /* Existence of mp1 verified in ip_wput_nondata */
12130 mp1 = mp->b_cont->b_cont;
12131 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12132 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12134 * Special case where ipx_current_ipif is not set:
12135 * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12136 * We are here as were not able to complete the operation in
12137 * ipif_set_values because we could not become exclusive on
12138 * the new ipsq.
12140 ill_t *ill = q->q_ptr;
12141 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12143 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12145 if (ipip->ipi_cmd_type == IF_CMD) {
12146 /* This a old style SIOC[GS]IF* command */
12147 ifr = (struct ifreq *)mp1->b_rptr;
12148 sin = (sin_t *)&ifr->ifr_addr;
12149 } else if (ipip->ipi_cmd_type == LIF_CMD) {
12150 /* This a new style SIOC[GS]LIF* command */
12151 lifr = (struct lifreq *)mp1->b_rptr;
12152 sin = (sin_t *)&lifr->lifr_addr;
12153 } else {
12154 sin = NULL;
12157 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12158 q, mp, ipip, mp1->b_rptr);
12160 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12161 int, ipip->ipi_cmd,
12162 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12163 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12165 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12169 * ioctl processing
12171 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12172 * the ioctl command in the ioctl tables, determines the copyin data size
12173 * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12175 * ioctl processing then continues when the M_IOCDATA makes its way down to
12176 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its
12177 * associated 'conn' is refheld till the end of the ioctl and the general
12178 * ioctl processing function ip_process_ioctl() is called to extract the
12179 * arguments and process the ioctl. To simplify extraction, ioctl commands
12180 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12181 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12182 * is used to extract the ioctl's arguments.
12184 * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12185 * so goes thru the serialization primitive ipsq_try_enter. Then the
12186 * appropriate function to handle the ioctl is called based on the entry in
12187 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12188 * which also refreleases the 'conn' that was refheld at the start of the
12189 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12191 * Many exclusive ioctls go thru an internal down up sequence as part of
12192 * the operation. For example an attempt to change the IP address of an
12193 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12194 * does all the cleanup such as deleting all ires that use this address.
12195 * Then we need to wait till all references to the interface go away.
12197 void
12198 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12200 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12201 ip_ioctl_cmd_t *ipip = arg;
12202 ip_extract_func_t *extract_funcp;
12203 cmd_info_t ci;
12204 int err;
12205 boolean_t entered_ipsq = B_FALSE;
12207 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12209 if (ipip == NULL)
12210 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12213 * SIOCLIFADDIF needs to go thru a special path since the
12214 * ill may not exist yet. This happens in the case of lo0
12215 * which is created using this ioctl.
12217 if (ipip->ipi_cmd == SIOCLIFADDIF) {
12218 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12219 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12220 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12221 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12222 return;
12225 ci.ci_ipif = NULL;
12226 switch (ipip->ipi_cmd_type) {
12227 case MISC_CMD:
12228 case MSFILT_CMD:
12230 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12232 if (ipip->ipi_cmd == IF_UNITSEL) {
12233 /* ioctl comes down the ill */
12234 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12235 ipif_refhold(ci.ci_ipif);
12237 err = 0;
12238 ci.ci_sin = NULL;
12239 ci.ci_sin6 = NULL;
12240 ci.ci_lifr = NULL;
12241 extract_funcp = NULL;
12242 break;
12244 case IF_CMD:
12245 case LIF_CMD:
12246 extract_funcp = ip_extract_lifreq;
12247 break;
12249 case ARP_CMD:
12250 case XARP_CMD:
12251 extract_funcp = ip_extract_arpreq;
12252 break;
12254 default:
12255 ASSERT(0);
12258 if (extract_funcp != NULL) {
12259 err = (*extract_funcp)(q, mp, ipip, &ci);
12260 if (err != 0) {
12261 DTRACE_PROBE4(ipif__ioctl,
12262 char *, "ip_process_ioctl finish err",
12263 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12264 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12265 return;
12269 * All of the extraction functions return a refheld ipif.
12271 ASSERT(ci.ci_ipif != NULL);
12274 if (!(ipip->ipi_flags & IPI_WR)) {
12276 * A return value of EINPROGRESS means the ioctl is
12277 * either queued and waiting for some reason or has
12278 * already completed.
12280 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12281 ci.ci_lifr);
12282 if (ci.ci_ipif != NULL) {
12283 DTRACE_PROBE4(ipif__ioctl,
12284 char *, "ip_process_ioctl finish RD",
12285 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12286 ipif_t *, ci.ci_ipif);
12287 ipif_refrele(ci.ci_ipif);
12288 } else {
12289 DTRACE_PROBE4(ipif__ioctl,
12290 char *, "ip_process_ioctl finish RD",
12291 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12293 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12294 return;
12297 ASSERT(ci.ci_ipif != NULL);
12300 * If ipsq is non-NULL, we are already being called exclusively
12302 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12303 if (ipsq == NULL) {
12304 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12305 NEW_OP, B_TRUE);
12306 if (ipsq == NULL) {
12307 ipif_refrele(ci.ci_ipif);
12308 return;
12310 entered_ipsq = B_TRUE;
12313 * Release the ipif so that ipif_down and friends that wait for
12314 * references to go away are not misled about the current ipif_refcnt
12315 * values. We are writer so we can access the ipif even after releasing
12316 * the ipif.
12318 ipif_refrele(ci.ci_ipif);
12320 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12323 * A return value of EINPROGRESS means the ioctl is
12324 * either queued and waiting for some reason or has
12325 * already completed.
12327 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12329 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12330 int, ipip->ipi_cmd,
12331 ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12332 ipif_t *, ci.ci_ipif);
12333 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12335 if (entered_ipsq)
12336 ipsq_exit(ipsq);
12340 * Complete the ioctl. Typically ioctls use the mi package and need to
12341 * do mi_copyout/mi_copy_done.
12343 void
12344 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12346 conn_t *connp = NULL;
12348 if (err == EINPROGRESS)
12349 return;
12351 if (CONN_Q(q)) {
12352 connp = Q_TO_CONN(q);
12353 ASSERT(connp->conn_ref >= 2);
12356 switch (mode) {
12357 case COPYOUT:
12358 if (err == 0)
12359 mi_copyout(q, mp);
12360 else
12361 mi_copy_done(q, mp, err);
12362 break;
12364 case NO_COPYOUT:
12365 mi_copy_done(q, mp, err);
12366 break;
12368 default:
12369 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */
12370 break;
12374 * The conn refhold and ioctlref placed on the conn at the start of the
12375 * ioctl are released here.
12377 if (connp != NULL) {
12378 CONN_DEC_IOCTLREF(connp);
12379 CONN_OPER_PENDING_DONE(connp);
12382 if (ipsq != NULL)
12383 ipsq_current_finish(ipsq);
12386 /* Handles all non data messages */
12387 void
12388 ip_wput_nondata(queue_t *q, mblk_t *mp)
12390 mblk_t *mp1;
12391 struct iocblk *iocp;
12392 ip_ioctl_cmd_t *ipip;
12393 conn_t *connp;
12394 cred_t *cr;
12395 char *proto_str;
12397 if (CONN_Q(q))
12398 connp = Q_TO_CONN(q);
12399 else
12400 connp = NULL;
12402 switch (DB_TYPE(mp)) {
12403 case M_IOCTL:
12405 * IOCTL processing begins in ip_sioctl_copyin_setup which
12406 * will arrange to copy in associated control structures.
12408 ip_sioctl_copyin_setup(q, mp);
12409 return;
12410 case M_IOCDATA:
12412 * Ensure that this is associated with one of our trans-
12413 * parent ioctls. If it's not ours, discard it if we're
12414 * running as a driver, or pass it on if we're a module.
12416 iocp = (struct iocblk *)mp->b_rptr;
12417 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12418 if (ipip == NULL) {
12419 if (q->q_next == NULL) {
12420 goto nak;
12421 } else {
12422 putnext(q, mp);
12424 return;
12426 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12428 * The ioctl is one we recognise, but is not consumed
12429 * by IP as a module and we are a module, so we drop
12431 goto nak;
12434 /* IOCTL continuation following copyin or copyout. */
12435 if (mi_copy_state(q, mp, NULL) == -1) {
12437 * The copy operation failed. mi_copy_state already
12438 * cleaned up, so we're out of here.
12440 return;
12443 * If we just completed a copy in, we become writer and
12444 * continue processing in ip_sioctl_copyin_done. If it
12445 * was a copy out, we call mi_copyout again. If there is
12446 * nothing more to copy out, it will complete the IOCTL.
12448 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12449 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12450 mi_copy_done(q, mp, EPROTO);
12451 return;
12454 * Check for cases that need more copying. A return
12455 * value of 0 means a second copyin has been started,
12456 * so we return; a return value of 1 means no more
12457 * copying is needed, so we continue.
12459 if (ipip->ipi_cmd_type == MSFILT_CMD &&
12460 MI_COPY_COUNT(mp) == 1) {
12461 if (ip_copyin_msfilter(q, mp) == 0)
12462 return;
12465 * Refhold the conn, till the ioctl completes. This is
12466 * needed in case the ioctl ends up in the pending mp
12467 * list. Every mp in the ipx_pending_mp list must have
12468 * a refhold on the conn to resume processing. The
12469 * refhold is released when the ioctl completes
12470 * (whether normally or abnormally). An ioctlref is also
12471 * placed on the conn to prevent TCP from removing the
12472 * queue needed to send the ioctl reply back.
12473 * In all cases ip_ioctl_finish is called to finish
12474 * the ioctl and release the refholds.
12476 if (connp != NULL) {
12477 /* This is not a reentry */
12478 CONN_INC_REF(connp);
12479 CONN_INC_IOCTLREF(connp);
12480 } else {
12481 if (!(ipip->ipi_flags & IPI_MODOK)) {
12482 mi_copy_done(q, mp, EINVAL);
12483 return;
12487 ip_process_ioctl(NULL, q, mp, ipip);
12489 } else {
12490 mi_copyout(q, mp);
12492 return;
12494 case M_IOCNAK:
12496 * The only way we could get here is if a resolver didn't like
12497 * an IOCTL we sent it. This shouldn't happen.
12499 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12500 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12501 ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12502 freemsg(mp);
12503 return;
12504 case M_IOCACK:
12505 /* /dev/ip shouldn't see this */
12506 goto nak;
12507 case M_FLUSH:
12508 if (*mp->b_rptr & FLUSHW)
12509 flushq(q, FLUSHALL);
12510 if (q->q_next) {
12511 putnext(q, mp);
12512 return;
12514 if (*mp->b_rptr & FLUSHR) {
12515 *mp->b_rptr &= ~FLUSHW;
12516 qreply(q, mp);
12517 return;
12519 freemsg(mp);
12520 return;
12521 case M_CTL:
12522 break;
12523 case M_PROTO:
12524 case M_PCPROTO:
12526 * The only PROTO messages we expect are SNMP-related.
12528 switch (((union T_primitives *)mp->b_rptr)->type) {
12529 case T_SVR4_OPTMGMT_REQ:
12530 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12531 "flags %x\n",
12532 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12534 if (connp == NULL) {
12535 proto_str = "T_SVR4_OPTMGMT_REQ";
12536 goto protonak;
12540 * All Solaris components should pass a db_credp
12541 * for this TPI message, hence we ASSERT.
12542 * But in case there is some other M_PROTO that looks
12543 * like a TPI message sent by some other kernel
12544 * component, we check and return an error.
12546 cr = msg_getcred(mp, NULL);
12547 ASSERT(cr != NULL);
12548 if (cr == NULL) {
12549 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12550 if (mp != NULL)
12551 qreply(q, mp);
12552 return;
12555 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12556 proto_str = "Bad SNMPCOM request?";
12557 goto protonak;
12559 return;
12560 default:
12561 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12562 (int)*(uint_t *)mp->b_rptr));
12563 freemsg(mp);
12564 return;
12566 default:
12567 break;
12569 if (q->q_next) {
12570 putnext(q, mp);
12571 } else
12572 freemsg(mp);
12573 return;
12575 nak:
12576 iocp->ioc_error = EINVAL;
12577 mp->b_datap->db_type = M_IOCNAK;
12578 iocp->ioc_count = 0;
12579 qreply(q, mp);
12580 return;
12582 protonak:
12583 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12584 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12585 qreply(q, mp);
12589 * Process IP options in an outbound packet. Verify that the nexthop in a
12590 * strict source route is onlink.
12591 * Returns non-zero if something fails in which case an ICMP error has been
12592 * sent and mp freed.
12594 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12597 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12599 ipoptp_t opts;
12600 uchar_t *opt;
12601 uint8_t optval;
12602 uint8_t optlen;
12603 ipaddr_t dst;
12604 intptr_t code = 0;
12605 ire_t *ire;
12606 ip_stack_t *ipst = ixa->ixa_ipst;
12607 ip_recv_attr_t iras;
12609 ip2dbg(("ip_output_options\n"));
12611 dst = ipha->ipha_dst;
12612 for (optval = ipoptp_first(&opts, ipha);
12613 optval != IPOPT_EOL;
12614 optval = ipoptp_next(&opts)) {
12615 opt = opts.ipoptp_cur;
12616 optlen = opts.ipoptp_len;
12617 ip2dbg(("ip_output_options: opt %d, len %d\n",
12618 optval, optlen));
12619 switch (optval) {
12620 uint32_t off;
12621 case IPOPT_SSRR:
12622 case IPOPT_LSRR:
12623 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12624 ip1dbg((
12625 "ip_output_options: bad option offset\n"));
12626 code = (char *)&opt[IPOPT_OLEN] -
12627 (char *)ipha;
12628 goto param_prob;
12630 off = opt[IPOPT_OFFSET];
12631 ip1dbg(("ip_output_options: next hop 0x%x\n",
12632 ntohl(dst)));
12634 * For strict: verify that dst is directly
12635 * reachable.
12637 if (optval == IPOPT_SSRR) {
12638 ire = ire_ftable_lookup_v4(dst, 0, 0,
12639 IRE_INTERFACE, NULL, ALL_ZONES,
12640 MATCH_IRE_TYPE, 0, ipst, NULL);
12641 if (ire == NULL) {
12642 ip1dbg(("ip_output_options: SSRR not"
12643 " directly reachable: 0x%x\n",
12644 ntohl(dst)));
12645 goto bad_src_route;
12647 ire_refrele(ire);
12649 break;
12650 case IPOPT_RR:
12651 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12652 ip1dbg((
12653 "ip_output_options: bad option offset\n"));
12654 code = (char *)&opt[IPOPT_OLEN] -
12655 (char *)ipha;
12656 goto param_prob;
12658 break;
12659 case IPOPT_TS:
12661 * Verify that length >=5 and that there is either
12662 * room for another timestamp or that the overflow
12663 * counter is not maxed out.
12665 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
12666 if (optlen < IPOPT_MINLEN_IT) {
12667 goto param_prob;
12669 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12670 ip1dbg((
12671 "ip_output_options: bad option offset\n"));
12672 code = (char *)&opt[IPOPT_OFFSET] -
12673 (char *)ipha;
12674 goto param_prob;
12676 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12677 case IPOPT_TS_TSONLY:
12678 off = IPOPT_TS_TIMELEN;
12679 break;
12680 case IPOPT_TS_TSANDADDR:
12681 case IPOPT_TS_PRESPEC:
12682 case IPOPT_TS_PRESPEC_RFC791:
12683 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
12684 break;
12685 default:
12686 code = (char *)&opt[IPOPT_POS_OV_FLG] -
12687 (char *)ipha;
12688 goto param_prob;
12690 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
12691 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
12693 * No room and the overflow counter is 15
12694 * already.
12696 goto param_prob;
12698 break;
12702 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
12703 return (0);
12705 ip1dbg(("ip_output_options: error processing IP options."));
12706 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
12708 param_prob:
12709 bzero(&iras, sizeof (iras));
12710 iras.ira_ill = iras.ira_rill = ill;
12711 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
12712 iras.ira_rifindex = iras.ira_ruifindex;
12713 iras.ira_flags = IRAF_IS_IPV4;
12715 ip_drop_output("ip_output_options", mp, ill);
12716 icmp_param_problem(mp, (uint8_t)code, &iras);
12717 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
12718 return (-1);
12720 bad_src_route:
12721 bzero(&iras, sizeof (iras));
12722 iras.ira_ill = iras.ira_rill = ill;
12723 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
12724 iras.ira_rifindex = iras.ira_ruifindex;
12725 iras.ira_flags = IRAF_IS_IPV4;
12727 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
12728 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
12729 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
12730 return (-1);
12734 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
12735 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
12736 * thru /etc/system.
12738 #define CONN_MAXDRAINCNT 64
12740 static void
12741 conn_drain_init(ip_stack_t *ipst)
12743 int i, j;
12744 idl_tx_list_t *itl_tx;
12746 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
12748 if ((ipst->ips_conn_drain_list_cnt == 0) ||
12749 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
12751 * Default value of the number of drainers is the
12752 * number of cpus, subject to maximum of 8 drainers.
12754 if (boot_max_ncpus != -1)
12755 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
12756 else
12757 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
12760 ipst->ips_idl_tx_list =
12761 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
12762 for (i = 0; i < TX_FANOUT_SIZE; i++) {
12763 itl_tx = &ipst->ips_idl_tx_list[i];
12764 itl_tx->txl_drain_list =
12765 kmem_zalloc(ipst->ips_conn_drain_list_cnt *
12766 sizeof (idl_t), KM_SLEEP);
12767 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
12768 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
12769 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
12770 MUTEX_DEFAULT, NULL);
12771 itl_tx->txl_drain_list[j].idl_itl = itl_tx;
12776 static void
12777 conn_drain_fini(ip_stack_t *ipst)
12779 int i;
12780 idl_tx_list_t *itl_tx;
12782 for (i = 0; i < TX_FANOUT_SIZE; i++) {
12783 itl_tx = &ipst->ips_idl_tx_list[i];
12784 kmem_free(itl_tx->txl_drain_list,
12785 ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
12787 kmem_free(ipst->ips_idl_tx_list,
12788 TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
12789 ipst->ips_idl_tx_list = NULL;
12793 * Flow control has blocked us from proceeding. Insert the given conn in one
12794 * of the conn drain lists. When flow control is unblocked, either ip_wsrv()
12795 * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
12796 * will call conn_walk_drain(). See the flow control notes at the top of this
12797 * file for more details.
12799 void
12800 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
12802 idl_t *idl = tx_list->txl_drain_list;
12803 uint_t index;
12804 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
12806 mutex_enter(&connp->conn_lock);
12807 if (connp->conn_state_flags & CONN_CLOSING) {
12809 * The conn is closing as a result of which CONN_CLOSING
12810 * is set. Return.
12812 mutex_exit(&connp->conn_lock);
12813 return;
12814 } else if (connp->conn_idl == NULL) {
12816 * Assign the next drain list round robin. We dont' use
12817 * a lock, and thus it may not be strictly round robin.
12818 * Atomicity of load/stores is enough to make sure that
12819 * conn_drain_list_index is always within bounds.
12821 index = tx_list->txl_drain_index;
12822 ASSERT(index < ipst->ips_conn_drain_list_cnt);
12823 connp->conn_idl = &tx_list->txl_drain_list[index];
12824 index++;
12825 if (index == ipst->ips_conn_drain_list_cnt)
12826 index = 0;
12827 tx_list->txl_drain_index = index;
12828 } else {
12829 ASSERT(connp->conn_idl->idl_itl == tx_list);
12831 mutex_exit(&connp->conn_lock);
12833 idl = connp->conn_idl;
12834 mutex_enter(&idl->idl_lock);
12835 if ((connp->conn_drain_prev != NULL) ||
12836 (connp->conn_state_flags & CONN_CLOSING)) {
12838 * The conn is either already in the drain list or closing.
12839 * (We needed to check for CONN_CLOSING again since close can
12840 * sneak in between dropping conn_lock and acquiring idl_lock.)
12842 mutex_exit(&idl->idl_lock);
12843 return;
12847 * The conn is not in the drain list. Insert it at the
12848 * tail of the drain list. The drain list is circular
12849 * and doubly linked. idl_conn points to the 1st element
12850 * in the list.
12852 if (idl->idl_conn == NULL) {
12853 idl->idl_conn = connp;
12854 connp->conn_drain_next = connp;
12855 connp->conn_drain_prev = connp;
12856 } else {
12857 conn_t *head = idl->idl_conn;
12859 connp->conn_drain_next = head;
12860 connp->conn_drain_prev = head->conn_drain_prev;
12861 head->conn_drain_prev->conn_drain_next = connp;
12862 head->conn_drain_prev = connp;
12865 * For non streams based sockets assert flow control.
12867 conn_setqfull(connp, NULL);
12868 mutex_exit(&idl->idl_lock);
12871 static void
12872 conn_drain_remove(conn_t *connp)
12874 idl_t *idl = connp->conn_idl;
12876 if (idl != NULL) {
12878 * Remove ourself from the drain list.
12880 if (connp->conn_drain_next == connp) {
12881 /* Singleton in the list */
12882 ASSERT(connp->conn_drain_prev == connp);
12883 idl->idl_conn = NULL;
12884 } else {
12885 connp->conn_drain_prev->conn_drain_next =
12886 connp->conn_drain_next;
12887 connp->conn_drain_next->conn_drain_prev =
12888 connp->conn_drain_prev;
12889 if (idl->idl_conn == connp)
12890 idl->idl_conn = connp->conn_drain_next;
12894 * NOTE: because conn_idl is associated with a specific drain
12895 * list which in turn is tied to the index the TX ring
12896 * (txl_cookie) hashes to, and because the TX ring can change
12897 * over the lifetime of the conn_t, we must clear conn_idl so
12898 * a subsequent conn_drain_insert() will set conn_idl again
12899 * based on the latest txl_cookie.
12901 connp->conn_idl = NULL;
12903 connp->conn_drain_next = NULL;
12904 connp->conn_drain_prev = NULL;
12906 conn_clrqfull(connp, NULL);
12908 * For streams based sockets open up flow control.
12910 if (!IPCL_IS_NONSTR(connp))
12911 enableok(connp->conn_wq);
12915 * This conn is closing, and we are called from ip_close. OR
12916 * this conn is draining because flow-control on the ill has been relieved.
12918 * We must also need to remove conn's on this idl from the list, and also
12919 * inform the sockfs upcalls about the change in flow-control.
12921 static void
12922 conn_drain(conn_t *connp, boolean_t closing)
12924 idl_t *idl;
12925 conn_t *next_connp;
12928 * connp->conn_idl is stable at this point, and no lock is needed
12929 * to check it. If we are called from ip_close, close has already
12930 * set CONN_CLOSING, thus freezing the value of conn_idl, and
12931 * called us only because conn_idl is non-null. If we are called thru
12932 * service, conn_idl could be null, but it cannot change because
12933 * service is single-threaded per queue, and there cannot be another
12934 * instance of service trying to call conn_drain_insert on this conn
12935 * now.
12937 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
12940 * If the conn doesn't exist or is not on a drain list, bail.
12942 if (connp == NULL || connp->conn_idl == NULL ||
12943 connp->conn_drain_prev == NULL) {
12944 return;
12947 idl = connp->conn_idl;
12948 ASSERT(MUTEX_HELD(&idl->idl_lock));
12950 if (!closing) {
12951 next_connp = connp->conn_drain_next;
12952 while (next_connp != connp) {
12953 conn_t *delconnp = next_connp;
12955 next_connp = next_connp->conn_drain_next;
12956 conn_drain_remove(delconnp);
12958 ASSERT(connp->conn_drain_next == idl->idl_conn);
12960 conn_drain_remove(connp);
12964 * Write service routine. Shared perimeter entry point.
12965 * The device queue's messages has fallen below the low water mark and STREAMS
12966 * has backenabled the ill_wq. Send sockfs notification about flow-control on
12967 * each waiting conn.
12969 void
12970 ip_wsrv(queue_t *q)
12972 ill_t *ill;
12974 ill = (ill_t *)q->q_ptr;
12975 if (ill->ill_state_flags == 0) {
12976 ip_stack_t *ipst = ill->ill_ipst;
12979 * The device flow control has opened up.
12980 * Walk through conn drain lists and qenable the
12981 * first conn in each list. This makes sense only
12982 * if the stream is fully plumbed and setup.
12983 * Hence the ill_state_flags check above.
12985 ip1dbg(("ip_wsrv: walking\n"));
12986 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
12987 enableok(ill->ill_wq);
12992 * Callback to disable flow control in IP.
12994 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
12995 * is enabled.
12997 * When MAC_TX() is not able to send any more packets, dld sets its queue
12998 * to QFULL and enable the STREAMS flow control. Later, when the underlying
12999 * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13000 * function and wakes up corresponding mac worker threads, which in turn
13001 * calls this callback function, and disables flow control.
13003 void
13004 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13006 ill_t *ill = (ill_t *)arg;
13007 ip_stack_t *ipst = ill->ill_ipst;
13008 idl_tx_list_t *idl_txl;
13010 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13011 mutex_enter(&idl_txl->txl_lock);
13012 /* add code to to set a flag to indicate idl_txl is enabled */
13013 conn_walk_drain(ipst, idl_txl);
13014 mutex_exit(&idl_txl->txl_lock);
13018 * Flow control has been relieved and STREAMS has backenabled us; drain
13019 * all the conn lists on `tx_list'.
13021 static void
13022 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13024 int i;
13025 idl_t *idl;
13027 IP_STAT(ipst, ip_conn_walk_drain);
13029 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13030 idl = &tx_list->txl_drain_list[i];
13031 mutex_enter(&idl->idl_lock);
13032 conn_drain(idl->idl_conn, B_FALSE);
13033 mutex_exit(&idl->idl_lock);
13038 * Determine if the ill and multicast aspects of that packets
13039 * "matches" the conn.
13041 boolean_t
13042 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13044 ill_t *ill = ira->ira_rill;
13045 zoneid_t zoneid = ira->ira_zoneid;
13046 uint_t in_ifindex;
13047 ipaddr_t dst, src;
13049 dst = ipha->ipha_dst;
13050 src = ipha->ipha_src;
13053 * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13054 * unicast, broadcast and multicast reception to
13055 * conn_incoming_ifindex.
13056 * conn_wantpacket is called for unicast, broadcast and
13057 * multicast packets.
13059 in_ifindex = connp->conn_incoming_ifindex;
13061 /* mpathd can bind to the under IPMP interface, which we allow */
13062 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13063 if (!IS_UNDER_IPMP(ill))
13064 return (B_FALSE);
13066 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13067 return (B_FALSE);
13070 if (!IPCL_ZONE_MATCH(connp, zoneid))
13071 return (B_FALSE);
13073 if (!(ira->ira_flags & IRAF_MULTICAST))
13074 return (B_TRUE);
13076 if (connp->conn_multi_router) {
13077 /* multicast packet and multicast router socket: send up */
13078 return (B_TRUE);
13081 if (ipha->ipha_protocol == IPPROTO_PIM ||
13082 ipha->ipha_protocol == IPPROTO_RSVP)
13083 return (B_TRUE);
13085 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13088 void
13089 conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13091 if (IPCL_IS_NONSTR(connp)) {
13092 (*connp->conn_upcalls->su_txq_full)
13093 (connp->conn_upper_handle, B_TRUE);
13094 if (flow_stopped != NULL)
13095 *flow_stopped = B_TRUE;
13096 } else {
13097 queue_t *q = connp->conn_wq;
13099 ASSERT(q != NULL);
13100 if (!(q->q_flag & QFULL)) {
13101 mutex_enter(QLOCK(q));
13102 if (!(q->q_flag & QFULL)) {
13103 /* still need to set QFULL */
13104 q->q_flag |= QFULL;
13105 /* set flow_stopped to true under QLOCK */
13106 if (flow_stopped != NULL)
13107 *flow_stopped = B_TRUE;
13108 mutex_exit(QLOCK(q));
13109 } else {
13110 /* flow_stopped is left unchanged */
13111 mutex_exit(QLOCK(q));
13117 void
13118 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13120 if (IPCL_IS_NONSTR(connp)) {
13121 (*connp->conn_upcalls->su_txq_full)
13122 (connp->conn_upper_handle, B_FALSE);
13123 if (flow_stopped != NULL)
13124 *flow_stopped = B_FALSE;
13125 } else {
13126 queue_t *q = connp->conn_wq;
13128 ASSERT(q != NULL);
13129 if (q->q_flag & QFULL) {
13130 mutex_enter(QLOCK(q));
13131 if (q->q_flag & QFULL) {
13132 q->q_flag &= ~QFULL;
13133 /* set flow_stopped to false under QLOCK */
13134 if (flow_stopped != NULL)
13135 *flow_stopped = B_FALSE;
13136 mutex_exit(QLOCK(q));
13137 if (q->q_flag & QWANTW)
13138 qbackenable(q, 0);
13139 } else {
13140 /* flow_stopped is left unchanged */
13141 mutex_exit(QLOCK(q));
13146 mutex_enter(&connp->conn_lock);
13147 connp->conn_blocked = B_FALSE;
13148 mutex_exit(&connp->conn_lock);
13152 * Return the length in bytes of the IPv4 headers (base header and IP options)
13153 * that will be needed based on the ip_pkt_t structure passed by the caller.
13155 * The returned length does not include the length of the upper level
13156 * protocol (ULP) header.
13157 * The caller needs to check that the length doesn't exceed the max for IPv4.
13160 ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13162 int len;
13164 len = IP_SIMPLE_HDR_LENGTH;
13166 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13167 ASSERT(ipp->ipp_ipv4_options_len != 0);
13168 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13169 len += ipp->ipp_ipv4_options_len;
13171 return (len);
13175 * All-purpose routine to build an IPv4 header with options based
13176 * on the abstract ip_pkt_t.
13178 * The caller has to set the source and destination address as well as
13179 * ipha_length. The caller has to massage any source route and compensate
13180 * for the ULP pseudo-header checksum due to the source route.
13182 void
13183 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13184 uint8_t protocol)
13186 ipha_t *ipha = (ipha_t *)buf;
13187 uint8_t *cp;
13189 /* Initialize IPv4 header */
13190 ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13191 ipha->ipha_length = 0; /* Caller will set later */
13192 ipha->ipha_ident = 0;
13193 ipha->ipha_fragment_offset_and_flags = 0;
13194 ipha->ipha_ttl = ipp->ipp_unicast_hops;
13195 ipha->ipha_protocol = protocol;
13196 ipha->ipha_hdr_checksum = 0;
13198 if ((ipp->ipp_fields & IPPF_ADDR) &&
13199 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13200 ipha->ipha_src = ipp->ipp_addr_v4;
13202 cp = (uint8_t *)&ipha[1];
13204 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13205 ASSERT(ipp->ipp_ipv4_options_len != 0);
13206 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13207 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13208 cp += ipp->ipp_ipv4_options_len;
13210 ipha->ipha_version_and_hdr_length =
13211 (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13213 ASSERT((int)(cp - buf) == buf_len);
13216 /* Allocate the private structure */
13217 static int
13218 ip_priv_alloc(void **bufp)
13220 void *buf;
13222 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13223 return (ENOMEM);
13225 *bufp = buf;
13226 return (0);
13229 /* Function to delete the private structure */
13230 void
13231 ip_priv_free(void *buf)
13233 ASSERT(buf != NULL);
13234 kmem_free(buf, sizeof (ip_priv_t));
13238 * The entry point for IPPF processing.
13239 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13240 * routine just returns.
13242 * When called, ip_process generates an ipp_packet_t structure
13243 * which holds the state information for this packet and invokes the
13244 * the classifier (via ipp_packet_process). The classification, depending on
13245 * configured filters, results in a list of actions for this packet. Invoking
13246 * an action may cause the packet to be dropped, in which case we return NULL.
13247 * proc indicates the callout position for
13248 * this packet and ill is the interface this packet arrived on or will leave
13249 * on (inbound and outbound resp.).
13251 * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13252 * on the ill corrsponding to the destination IP address.
13254 mblk_t *
13255 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13257 ip_priv_t *priv;
13258 ipp_action_id_t aid;
13259 int rc = 0;
13260 ipp_packet_t *pp;
13262 /* If the classifier is not loaded, return */
13263 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13264 return (mp);
13267 ASSERT(mp != NULL);
13269 /* Allocate the packet structure */
13270 rc = ipp_packet_alloc(&pp, "ip", aid);
13271 if (rc != 0)
13272 goto drop;
13274 /* Allocate the private structure */
13275 rc = ip_priv_alloc((void **)&priv);
13276 if (rc != 0) {
13277 ipp_packet_free(pp);
13278 goto drop;
13280 priv->proc = proc;
13281 priv->ill_index = ill_get_upper_ifindex(rill);
13283 ipp_packet_set_private(pp, priv, ip_priv_free);
13284 ipp_packet_set_data(pp, mp);
13286 /* Invoke the classifier */
13287 rc = ipp_packet_process(&pp);
13288 if (pp != NULL) {
13289 mp = ipp_packet_get_data(pp);
13290 ipp_packet_free(pp);
13291 if (rc != 0)
13292 goto drop;
13293 return (mp);
13294 } else {
13295 /* No mp to trace in ip_drop_input/ip_drop_output */
13296 mp = NULL;
13298 drop:
13299 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13300 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13301 ip_drop_input("ip_process", mp, ill);
13302 } else {
13303 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13304 ip_drop_output("ip_process", mp, ill);
13306 freemsg(mp);
13307 return (NULL);
13310 static int
13311 ip_squeue_switch(int val)
13313 int rval;
13315 switch (val) {
13316 case IP_SQUEUE_ENTER_NODRAIN:
13317 rval = SQ_NODRAIN;
13318 break;
13319 case IP_SQUEUE_ENTER:
13320 rval = SQ_PROCESS;
13321 break;
13322 case IP_SQUEUE_FILL:
13323 default:
13324 rval = SQ_FILL;
13325 break;
13327 return (rval);
13330 static void *
13331 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13333 kstat_t *ksp;
13335 ip_stat_t template = {
13336 { "ip_udp_fannorm", KSTAT_DATA_UINT64 },
13337 { "ip_udp_fanmb", KSTAT_DATA_UINT64 },
13338 { "ip_recv_pullup", KSTAT_DATA_UINT64 },
13339 { "ip_db_ref", KSTAT_DATA_UINT64 },
13340 { "ip_notaligned", KSTAT_DATA_UINT64 },
13341 { "ip_multimblk", KSTAT_DATA_UINT64 },
13342 { "ip_opt", KSTAT_DATA_UINT64 },
13343 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 },
13344 { "ip_conn_flputbq", KSTAT_DATA_UINT64 },
13345 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 },
13346 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 },
13347 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 },
13348 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 },
13349 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 },
13350 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 },
13351 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 },
13352 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 },
13353 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 },
13354 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 },
13355 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13356 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13357 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13358 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13359 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13360 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13361 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 },
13362 { "conn_in_recvopts", KSTAT_DATA_UINT64 },
13363 { "conn_in_recvif", KSTAT_DATA_UINT64 },
13364 { "conn_in_recvslla", KSTAT_DATA_UINT64 },
13365 { "conn_in_recvucred", KSTAT_DATA_UINT64 },
13366 { "conn_in_recvttl", KSTAT_DATA_UINT64 },
13367 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 },
13368 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 },
13369 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 },
13370 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 },
13371 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 },
13372 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 },
13373 { "conn_in_recvtclass", KSTAT_DATA_UINT64 },
13374 { "conn_in_timestamp", KSTAT_DATA_UINT64 },
13377 ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13378 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13379 KSTAT_FLAG_VIRTUAL, stackid);
13381 if (ksp == NULL)
13382 return (NULL);
13384 bcopy(&template, ip_statisticsp, sizeof (template));
13385 ksp->ks_data = (void *)ip_statisticsp;
13386 ksp->ks_private = (void *)(uintptr_t)stackid;
13388 kstat_install(ksp);
13389 return (ksp);
13392 static void
13393 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13395 if (ksp != NULL) {
13396 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13397 kstat_delete_netstack(ksp, stackid);
13401 static void *
13402 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
13404 kstat_t *ksp;
13406 ip_named_kstat_t template = {
13407 { "forwarding", KSTAT_DATA_UINT32, 0 },
13408 { "defaultTTL", KSTAT_DATA_UINT32, 0 },
13409 { "inReceives", KSTAT_DATA_UINT64, 0 },
13410 { "inHdrErrors", KSTAT_DATA_UINT32, 0 },
13411 { "inAddrErrors", KSTAT_DATA_UINT32, 0 },
13412 { "forwDatagrams", KSTAT_DATA_UINT64, 0 },
13413 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 },
13414 { "inDiscards", KSTAT_DATA_UINT32, 0 },
13415 { "inDelivers", KSTAT_DATA_UINT64, 0 },
13416 { "outRequests", KSTAT_DATA_UINT64, 0 },
13417 { "outDiscards", KSTAT_DATA_UINT32, 0 },
13418 { "outNoRoutes", KSTAT_DATA_UINT32, 0 },
13419 { "reasmTimeout", KSTAT_DATA_UINT32, 0 },
13420 { "reasmReqds", KSTAT_DATA_UINT32, 0 },
13421 { "reasmOKs", KSTAT_DATA_UINT32, 0 },
13422 { "reasmFails", KSTAT_DATA_UINT32, 0 },
13423 { "fragOKs", KSTAT_DATA_UINT32, 0 },
13424 { "fragFails", KSTAT_DATA_UINT32, 0 },
13425 { "fragCreates", KSTAT_DATA_UINT32, 0 },
13426 { "addrEntrySize", KSTAT_DATA_INT32, 0 },
13427 { "routeEntrySize", KSTAT_DATA_INT32, 0 },
13428 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 },
13429 { "routingDiscards", KSTAT_DATA_UINT32, 0 },
13430 { "inErrs", KSTAT_DATA_UINT32, 0 },
13431 { "noPorts", KSTAT_DATA_UINT32, 0 },
13432 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
13433 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 },
13434 { "reasmPartDups", KSTAT_DATA_UINT32, 0 },
13435 { "forwProhibits", KSTAT_DATA_UINT32, 0 },
13436 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 },
13437 { "udpInOverflows", KSTAT_DATA_UINT32, 0 },
13438 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 },
13439 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 },
13440 { "ipsecInFailed", KSTAT_DATA_INT32, 0 },
13441 { "memberEntrySize", KSTAT_DATA_INT32, 0 },
13442 { "inIPv6", KSTAT_DATA_UINT32, 0 },
13443 { "outIPv6", KSTAT_DATA_UINT32, 0 },
13444 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 },
13447 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
13448 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
13449 if (ksp == NULL || ksp->ks_data == NULL)
13450 return (NULL);
13452 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
13453 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
13454 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
13455 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
13456 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
13458 template.netToMediaEntrySize.value.i32 =
13459 sizeof (mib2_ipNetToMediaEntry_t);
13461 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
13463 bcopy(&template, ksp->ks_data, sizeof (template));
13464 ksp->ks_update = ip_kstat_update;
13465 ksp->ks_private = (void *)(uintptr_t)stackid;
13467 kstat_install(ksp);
13468 return (ksp);
13471 static void
13472 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
13474 if (ksp != NULL) {
13475 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13476 kstat_delete_netstack(ksp, stackid);
13480 static int
13481 ip_kstat_update(kstat_t *kp, int rw)
13483 ip_named_kstat_t *ipkp;
13484 mib2_ipIfStatsEntry_t ipmib;
13485 ill_walk_context_t ctx;
13486 ill_t *ill;
13487 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
13488 netstack_t *ns;
13489 ip_stack_t *ipst;
13491 if (kp == NULL || kp->ks_data == NULL)
13492 return (EIO);
13494 if (rw == KSTAT_WRITE)
13495 return (EACCES);
13497 ns = netstack_find_by_stackid(stackid);
13498 if (ns == NULL)
13499 return (-1);
13500 ipst = ns->netstack_ip;
13501 if (ipst == NULL) {
13502 netstack_rele(ns);
13503 return (-1);
13505 ipkp = (ip_named_kstat_t *)kp->ks_data;
13507 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
13508 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13509 ill = ILL_START_WALK_V4(&ctx, ipst);
13510 for (; ill != NULL; ill = ill_next(&ctx, ill))
13511 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
13512 rw_exit(&ipst->ips_ill_g_lock);
13514 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding;
13515 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL;
13516 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives;
13517 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors;
13518 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors;
13519 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
13520 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos;
13521 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards;
13522 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers;
13523 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests;
13524 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards;
13525 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes;
13526 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
13527 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds;
13528 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs;
13529 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails;
13530 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs;
13531 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails;
13532 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates;
13534 ipkp->routingDiscards.value.ui32 = 0;
13535 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs;
13536 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts;
13537 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs;
13538 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates;
13539 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups;
13540 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits;
13541 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs;
13542 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows;
13543 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows;
13544 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded;
13545 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed;
13547 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion;
13548 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion;
13549 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
13551 netstack_rele(ns);
13553 return (0);
13556 static void *
13557 icmp_kstat_init(netstackid_t stackid)
13559 kstat_t *ksp;
13561 icmp_named_kstat_t template = {
13562 { "inMsgs", KSTAT_DATA_UINT32 },
13563 { "inErrors", KSTAT_DATA_UINT32 },
13564 { "inDestUnreachs", KSTAT_DATA_UINT32 },
13565 { "inTimeExcds", KSTAT_DATA_UINT32 },
13566 { "inParmProbs", KSTAT_DATA_UINT32 },
13567 { "inSrcQuenchs", KSTAT_DATA_UINT32 },
13568 { "inRedirects", KSTAT_DATA_UINT32 },
13569 { "inEchos", KSTAT_DATA_UINT32 },
13570 { "inEchoReps", KSTAT_DATA_UINT32 },
13571 { "inTimestamps", KSTAT_DATA_UINT32 },
13572 { "inTimestampReps", KSTAT_DATA_UINT32 },
13573 { "inAddrMasks", KSTAT_DATA_UINT32 },
13574 { "inAddrMaskReps", KSTAT_DATA_UINT32 },
13575 { "outMsgs", KSTAT_DATA_UINT32 },
13576 { "outErrors", KSTAT_DATA_UINT32 },
13577 { "outDestUnreachs", KSTAT_DATA_UINT32 },
13578 { "outTimeExcds", KSTAT_DATA_UINT32 },
13579 { "outParmProbs", KSTAT_DATA_UINT32 },
13580 { "outSrcQuenchs", KSTAT_DATA_UINT32 },
13581 { "outRedirects", KSTAT_DATA_UINT32 },
13582 { "outEchos", KSTAT_DATA_UINT32 },
13583 { "outEchoReps", KSTAT_DATA_UINT32 },
13584 { "outTimestamps", KSTAT_DATA_UINT32 },
13585 { "outTimestampReps", KSTAT_DATA_UINT32 },
13586 { "outAddrMasks", KSTAT_DATA_UINT32 },
13587 { "outAddrMaskReps", KSTAT_DATA_UINT32 },
13588 { "inChksumErrs", KSTAT_DATA_UINT32 },
13589 { "inUnknowns", KSTAT_DATA_UINT32 },
13590 { "inFragNeeded", KSTAT_DATA_UINT32 },
13591 { "outFragNeeded", KSTAT_DATA_UINT32 },
13592 { "outDrops", KSTAT_DATA_UINT32 },
13593 { "inOverFlows", KSTAT_DATA_UINT32 },
13594 { "inBadRedirects", KSTAT_DATA_UINT32 },
13597 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
13598 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
13599 if (ksp == NULL || ksp->ks_data == NULL)
13600 return (NULL);
13602 bcopy(&template, ksp->ks_data, sizeof (template));
13604 ksp->ks_update = icmp_kstat_update;
13605 ksp->ks_private = (void *)(uintptr_t)stackid;
13607 kstat_install(ksp);
13608 return (ksp);
13611 static void
13612 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
13614 if (ksp != NULL) {
13615 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13616 kstat_delete_netstack(ksp, stackid);
13620 static int
13621 icmp_kstat_update(kstat_t *kp, int rw)
13623 icmp_named_kstat_t *icmpkp;
13624 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
13625 netstack_t *ns;
13626 ip_stack_t *ipst;
13628 if ((kp == NULL) || (kp->ks_data == NULL))
13629 return (EIO);
13631 if (rw == KSTAT_WRITE)
13632 return (EACCES);
13634 ns = netstack_find_by_stackid(stackid);
13635 if (ns == NULL)
13636 return (-1);
13637 ipst = ns->netstack_ip;
13638 if (ipst == NULL) {
13639 netstack_rele(ns);
13640 return (-1);
13642 icmpkp = (icmp_named_kstat_t *)kp->ks_data;
13644 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs;
13645 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors;
13646 icmpkp->inDestUnreachs.value.ui32 =
13647 ipst->ips_icmp_mib.icmpInDestUnreachs;
13648 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds;
13649 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs;
13650 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs;
13651 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects;
13652 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos;
13653 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps;
13654 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps;
13655 icmpkp->inTimestampReps.value.ui32 =
13656 ipst->ips_icmp_mib.icmpInTimestampReps;
13657 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks;
13658 icmpkp->inAddrMaskReps.value.ui32 =
13659 ipst->ips_icmp_mib.icmpInAddrMaskReps;
13660 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs;
13661 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors;
13662 icmpkp->outDestUnreachs.value.ui32 =
13663 ipst->ips_icmp_mib.icmpOutDestUnreachs;
13664 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds;
13665 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs;
13666 icmpkp->outSrcQuenchs.value.ui32 =
13667 ipst->ips_icmp_mib.icmpOutSrcQuenchs;
13668 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects;
13669 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos;
13670 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps;
13671 icmpkp->outTimestamps.value.ui32 =
13672 ipst->ips_icmp_mib.icmpOutTimestamps;
13673 icmpkp->outTimestampReps.value.ui32 =
13674 ipst->ips_icmp_mib.icmpOutTimestampReps;
13675 icmpkp->outAddrMasks.value.ui32 =
13676 ipst->ips_icmp_mib.icmpOutAddrMasks;
13677 icmpkp->outAddrMaskReps.value.ui32 =
13678 ipst->ips_icmp_mib.icmpOutAddrMaskReps;
13679 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs;
13680 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns;
13681 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded;
13682 icmpkp->outFragNeeded.value.ui32 =
13683 ipst->ips_icmp_mib.icmpOutFragNeeded;
13684 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops;
13685 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows;
13686 icmpkp->inBadRedirects.value.ui32 =
13687 ipst->ips_icmp_mib.icmpInBadRedirects;
13689 netstack_rele(ns);
13690 return (0);
13694 * This is the fanout function for raw socket opened for SCTP. Note
13695 * that it is called after SCTP checks that there is no socket which
13696 * wants a packet. Then before SCTP handles this out of the blue packet,
13697 * this function is called to see if there is any raw socket for SCTP.
13698 * If there is and it is bound to the correct address, the packet will
13699 * be sent to that socket. Note that only one raw socket can be bound to
13700 * a port. This is assured in ipcl_sctp_hash_insert();
13702 void
13703 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
13704 ip_recv_attr_t *ira)
13706 conn_t *connp;
13707 queue_t *rq;
13708 boolean_t secure;
13709 ill_t *ill = ira->ira_ill;
13710 ip_stack_t *ipst = ill->ill_ipst;
13711 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
13712 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp;
13713 iaflags_t iraflags = ira->ira_flags;
13714 ill_t *rill = ira->ira_rill;
13716 secure = iraflags & IRAF_IPSEC_SECURE;
13718 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
13719 ira, ipst);
13720 if (connp == NULL) {
13722 * Although raw sctp is not summed, OOB chunks must be.
13723 * Drop the packet here if the sctp checksum failed.
13725 if (iraflags & IRAF_SCTP_CSUM_ERR) {
13726 SCTPS_BUMP_MIB(sctps, sctpChecksumError);
13727 freemsg(mp);
13728 return;
13730 ira->ira_ill = ira->ira_rill = NULL;
13731 sctp_ootb_input(mp, ira, ipst);
13732 ira->ira_ill = ill;
13733 ira->ira_rill = rill;
13734 return;
13736 rq = connp->conn_rq;
13737 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
13738 CONN_DEC_REF(connp);
13739 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
13740 freemsg(mp);
13741 return;
13743 if (((iraflags & IRAF_IS_IPV4) ?
13744 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
13745 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
13746 secure) {
13747 mp = ipsec_check_inbound_policy(mp, connp, ipha,
13748 ip6h, ira);
13749 if (mp == NULL) {
13750 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13751 /* Note that mp is NULL */
13752 ip_drop_input("ipIfStatsInDiscards", mp, ill);
13753 CONN_DEC_REF(connp);
13754 return;
13758 if (iraflags & IRAF_ICMP_ERROR) {
13759 (connp->conn_recvicmp)(connp, mp, NULL, ira);
13760 } else {
13761 ill_t *rill = ira->ira_rill;
13763 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
13764 /* This is the SOCK_RAW, IPPROTO_SCTP case. */
13765 ira->ira_ill = ira->ira_rill = NULL;
13766 (connp->conn_recv)(connp, mp, NULL, ira);
13767 ira->ira_ill = ill;
13768 ira->ira_rill = rill;
13770 CONN_DEC_REF(connp);
13774 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
13775 * header before the ip payload.
13777 static void
13778 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
13780 int len = (mp->b_wptr - mp->b_rptr);
13781 mblk_t *ip_mp;
13783 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13784 if (is_fp_mp || len != fp_mp_len) {
13785 if (len > fp_mp_len) {
13787 * fastpath header and ip header in the first mblk
13789 mp->b_rptr += fp_mp_len;
13790 } else {
13792 * ip_xmit_attach_llhdr had to prepend an mblk to
13793 * attach the fastpath header before ip header.
13795 ip_mp = mp->b_cont;
13796 freeb(mp);
13797 mp = ip_mp;
13798 mp->b_rptr += (fp_mp_len - len);
13800 } else {
13801 ip_mp = mp->b_cont;
13802 freeb(mp);
13803 mp = ip_mp;
13805 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
13806 freemsg(mp);
13810 * Normal post fragmentation function.
13812 * Send a packet using the passed in nce. This handles both IPv4 and IPv6
13813 * using the same state machine.
13815 * We return an error on failure. In particular we return EWOULDBLOCK
13816 * when the driver flow controls. In that case this ensures that ip_wsrv runs
13817 * (currently by canputnext failure resulting in backenabling from GLD.)
13818 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
13819 * indication that they can flow control until ip_wsrv() tells then to restart.
13821 * If the nce passed by caller is incomplete, this function
13822 * queues the packet and if necessary, sends ARP request and bails.
13823 * If the Neighbor Cache passed is fully resolved, we simply prepend
13824 * the link-layer header to the packet, do ipsec hw acceleration
13825 * work if necessary, and send the packet out on the wire.
13827 /* ARGSUSED6 */
13829 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
13830 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
13832 queue_t *wq;
13833 ill_t *ill = nce->nce_ill;
13834 ip_stack_t *ipst = ill->ill_ipst;
13835 uint64_t delta;
13836 boolean_t isv6 = ill->ill_isv6;
13837 boolean_t fp_mp;
13838 ncec_t *ncec = nce->nce_common;
13839 int64_t now = LBOLT_FASTPATH64;
13840 boolean_t is_probe;
13842 DTRACE_PROBE1(ip__xmit, nce_t *, nce);
13844 ASSERT(mp != NULL);
13845 ASSERT(mp->b_datap->db_type == M_DATA);
13846 ASSERT(pkt_len == msgdsize(mp));
13849 * If we have already been here and are coming back after ARP/ND.
13850 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
13851 * in that case since they have seen the packet when it came here
13852 * the first time.
13854 if (ixaflags & IXAF_NO_TRACE)
13855 goto sendit;
13857 if (ixaflags & IXAF_IS_IPV4) {
13858 ipha_t *ipha = (ipha_t *)mp->b_rptr;
13860 ASSERT(!isv6);
13861 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
13862 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
13863 !(ixaflags & IXAF_NO_PFHOOK)) {
13864 int error;
13866 FW_HOOKS(ipst->ips_ip4_physical_out_event,
13867 ipst->ips_ipv4firewall_physical_out,
13868 NULL, ill, ipha, mp, mp, 0, ipst, error);
13869 DTRACE_PROBE1(ip4__physical__out__end,
13870 mblk_t *, mp);
13871 if (mp == NULL)
13872 return (error);
13874 /* The length could have changed */
13875 pkt_len = msgdsize(mp);
13877 if (ipst->ips_ip4_observe.he_interested) {
13879 * Note that for TX the zoneid is the sending
13880 * zone, whether or not MLP is in play.
13881 * Since the szone argument is the IP zoneid (i.e.,
13882 * zero for exclusive-IP zones) and ipobs wants
13883 * the system zoneid, we map it here.
13885 szone = IP_REAL_ZONEID(szone, ipst);
13888 * On the outbound path the destination zone will be
13889 * unknown as we're sending this packet out on the
13890 * wire.
13892 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
13893 ill, ipst);
13895 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
13896 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill,
13897 ipha_t *, ipha, ip6_t *, NULL, int, 0);
13898 } else {
13899 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
13901 ASSERT(isv6);
13902 ASSERT(pkt_len ==
13903 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
13904 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
13905 !(ixaflags & IXAF_NO_PFHOOK)) {
13906 int error;
13908 FW_HOOKS6(ipst->ips_ip6_physical_out_event,
13909 ipst->ips_ipv6firewall_physical_out,
13910 NULL, ill, ip6h, mp, mp, 0, ipst, error);
13911 DTRACE_PROBE1(ip6__physical__out__end,
13912 mblk_t *, mp);
13913 if (mp == NULL)
13914 return (error);
13916 /* The length could have changed */
13917 pkt_len = msgdsize(mp);
13919 if (ipst->ips_ip6_observe.he_interested) {
13920 /* See above */
13921 szone = IP_REAL_ZONEID(szone, ipst);
13923 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
13924 ill, ipst);
13926 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
13927 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill,
13928 ipha_t *, NULL, ip6_t *, ip6h, int, 0);
13931 sendit:
13933 * We check the state without a lock because the state can never
13934 * move "backwards" to initial or incomplete.
13936 switch (ncec->ncec_state) {
13937 case ND_REACHABLE:
13938 case ND_STALE:
13939 case ND_DELAY:
13940 case ND_PROBE:
13941 mp = ip_xmit_attach_llhdr(mp, nce);
13942 if (mp == NULL) {
13944 * ip_xmit_attach_llhdr has increased
13945 * ipIfStatsOutDiscards and called ip_drop_output()
13947 return (ENOBUFS);
13950 * check if nce_fastpath completed and we tagged on a
13951 * copy of nce_fp_mp in ip_xmit_attach_llhdr().
13953 fp_mp = (mp->b_datap->db_type == M_DATA);
13955 if (fp_mp &&
13956 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
13957 ill_dld_direct_t *idd;
13959 idd = &ill->ill_dld_capab->idc_direct;
13961 * Send the packet directly to DLD, where it
13962 * may be queued depending on the availability
13963 * of transmit resources at the media layer.
13964 * Return value should be taken into
13965 * account and flow control the TCP.
13967 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
13968 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
13969 pkt_len);
13971 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
13972 (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
13973 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
13974 } else {
13975 uintptr_t cookie;
13977 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
13978 mp, (uintptr_t)xmit_hint, 0)) != 0) {
13979 if (ixacookie != NULL)
13980 *ixacookie = cookie;
13981 return (EWOULDBLOCK);
13984 } else {
13985 wq = ill->ill_wq;
13987 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
13988 !canputnext(wq)) {
13989 if (ixacookie != NULL)
13990 *ixacookie = 0;
13991 ip_xmit_flowctl_drop(ill, mp, fp_mp,
13992 nce->nce_fp_mp != NULL ?
13993 MBLKL(nce->nce_fp_mp) : 0);
13994 return (EWOULDBLOCK);
13996 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
13997 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
13998 pkt_len);
13999 putnext(wq, mp);
14003 * The rest of this function implements Neighbor Unreachability
14004 * detection. Determine if the ncec is eligible for NUD.
14006 if (ncec->ncec_flags & NCE_F_NONUD)
14007 return (0);
14009 ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14012 * Check for upper layer advice
14014 if (ixaflags & IXAF_REACH_CONF) {
14015 timeout_id_t tid;
14018 * It should be o.k. to check the state without
14019 * a lock here, at most we lose an advice.
14021 ncec->ncec_last = TICK_TO_MSEC(now);
14022 if (ncec->ncec_state != ND_REACHABLE) {
14023 mutex_enter(&ncec->ncec_lock);
14024 ncec->ncec_state = ND_REACHABLE;
14025 tid = ncec->ncec_timeout_id;
14026 ncec->ncec_timeout_id = 0;
14027 mutex_exit(&ncec->ncec_lock);
14028 (void) untimeout(tid);
14029 if (ip_debug > 2) {
14030 /* ip1dbg */
14031 pr_addr_dbg("ip_xmit: state"
14032 " for %s changed to"
14033 " REACHABLE\n", AF_INET6,
14034 &ncec->ncec_addr);
14037 return (0);
14040 delta = TICK_TO_MSEC(now) - ncec->ncec_last;
14041 ip1dbg(("ip_xmit: delta = %" PRId64
14042 " ill_reachable_time = %d \n", delta,
14043 ill->ill_reachable_time));
14044 if (delta > (uint64_t)ill->ill_reachable_time) {
14045 mutex_enter(&ncec->ncec_lock);
14046 switch (ncec->ncec_state) {
14047 case ND_REACHABLE:
14048 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14049 /* FALLTHROUGH */
14050 case ND_STALE:
14052 * ND_REACHABLE is identical to
14053 * ND_STALE in this specific case. If
14054 * reachable time has expired for this
14055 * neighbor (delta is greater than
14056 * reachable time), conceptually, the
14057 * neighbor cache is no longer in
14058 * REACHABLE state, but already in
14059 * STALE state. So the correct
14060 * transition here is to ND_DELAY.
14062 ncec->ncec_state = ND_DELAY;
14063 mutex_exit(&ncec->ncec_lock);
14064 nce_restart_timer(ncec,
14065 ipst->ips_delay_first_probe_time);
14066 if (ip_debug > 3) {
14067 /* ip2dbg */
14068 pr_addr_dbg("ip_xmit: state"
14069 " for %s changed to"
14070 " DELAY\n", AF_INET6,
14071 &ncec->ncec_addr);
14073 break;
14074 case ND_DELAY:
14075 case ND_PROBE:
14076 mutex_exit(&ncec->ncec_lock);
14077 /* Timers have already started */
14078 break;
14079 case ND_UNREACHABLE:
14081 * nce_timer has detected that this ncec
14082 * is unreachable and initiated deleting
14083 * this ncec.
14084 * This is a harmless race where we found the
14085 * ncec before it was deleted and have
14086 * just sent out a packet using this
14087 * unreachable ncec.
14089 mutex_exit(&ncec->ncec_lock);
14090 break;
14091 default:
14092 ASSERT(0);
14093 mutex_exit(&ncec->ncec_lock);
14096 return (0);
14098 case ND_INCOMPLETE:
14100 * the state could have changed since we didn't hold the lock.
14101 * Re-verify state under lock.
14103 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14104 mutex_enter(&ncec->ncec_lock);
14105 if (NCE_ISREACHABLE(ncec)) {
14106 mutex_exit(&ncec->ncec_lock);
14107 goto sendit;
14109 /* queue the packet */
14110 nce_queue_mp(ncec, mp, is_probe);
14111 mutex_exit(&ncec->ncec_lock);
14112 DTRACE_PROBE2(ip__xmit__incomplete,
14113 (ncec_t *), ncec, (mblk_t *), mp);
14114 return (0);
14116 case ND_INITIAL:
14118 * State could have changed since we didn't hold the lock, so
14119 * re-verify state.
14121 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14122 mutex_enter(&ncec->ncec_lock);
14123 if (NCE_ISREACHABLE(ncec)) {
14124 mutex_exit(&ncec->ncec_lock);
14125 goto sendit;
14127 nce_queue_mp(ncec, mp, is_probe);
14128 if (ncec->ncec_state == ND_INITIAL) {
14129 ncec->ncec_state = ND_INCOMPLETE;
14130 mutex_exit(&ncec->ncec_lock);
14132 * figure out the source we want to use
14133 * and resolve it.
14135 ip_ndp_resolve(ncec);
14136 } else {
14137 mutex_exit(&ncec->ncec_lock);
14139 return (0);
14141 case ND_UNREACHABLE:
14142 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14143 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14144 mp, ill);
14145 freemsg(mp);
14146 return (0);
14148 default:
14149 ASSERT(0);
14150 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14151 ip_drop_output("ipIfStatsOutDiscards - ND_other",
14152 mp, ill);
14153 freemsg(mp);
14154 return (ENETUNREACH);
14159 * Return B_TRUE if the buffers differ in length or content.
14160 * This is used for comparing extension header buffers.
14161 * Note that an extension header would be declared different
14162 * even if all that changed was the next header value in that header i.e.
14163 * what really changed is the next extension header.
14165 boolean_t
14166 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14167 uint_t blen)
14169 if (!b_valid)
14170 blen = 0;
14172 if (alen != blen)
14173 return (B_TRUE);
14174 if (alen == 0)
14175 return (B_FALSE); /* Both zero length */
14176 return (bcmp(abuf, bbuf, alen));
14180 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14181 * Return B_FALSE if memory allocation fails - don't change any state!
14183 boolean_t
14184 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14185 const void *src, uint_t srclen)
14187 void *dst;
14189 if (!src_valid)
14190 srclen = 0;
14192 ASSERT(*dstlenp == 0);
14193 if (src != NULL && srclen != 0) {
14194 dst = mi_alloc(srclen, BPRI_MED);
14195 if (dst == NULL)
14196 return (B_FALSE);
14197 } else {
14198 dst = NULL;
14200 if (*dstp != NULL)
14201 mi_free(*dstp);
14202 *dstp = dst;
14203 *dstlenp = dst == NULL ? 0 : srclen;
14204 return (B_TRUE);
14208 * Replace what is in *dst, *dstlen with the source.
14209 * Assumes ip_allocbuf has already been called.
14211 void
14212 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14213 const void *src, uint_t srclen)
14215 if (!src_valid)
14216 srclen = 0;
14218 ASSERT(*dstlenp == srclen);
14219 if (src != NULL && srclen != 0)
14220 bcopy(src, *dstp, srclen);
14224 * Free the storage pointed to by the members of an ip_pkt_t.
14226 void
14227 ip_pkt_free(ip_pkt_t *ipp)
14229 uint_t fields = ipp->ipp_fields;
14231 if (fields & IPPF_HOPOPTS) {
14232 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14233 ipp->ipp_hopopts = NULL;
14234 ipp->ipp_hopoptslen = 0;
14236 if (fields & IPPF_RTHDRDSTOPTS) {
14237 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14238 ipp->ipp_rthdrdstopts = NULL;
14239 ipp->ipp_rthdrdstoptslen = 0;
14241 if (fields & IPPF_DSTOPTS) {
14242 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14243 ipp->ipp_dstopts = NULL;
14244 ipp->ipp_dstoptslen = 0;
14246 if (fields & IPPF_RTHDR) {
14247 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14248 ipp->ipp_rthdr = NULL;
14249 ipp->ipp_rthdrlen = 0;
14251 if (fields & IPPF_IPV4_OPTIONS) {
14252 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14253 ipp->ipp_ipv4_options = NULL;
14254 ipp->ipp_ipv4_options_len = 0;
14256 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14257 IPPF_RTHDR | IPPF_IPV4_OPTIONS);
14261 * Copy from src to dst and allocate as needed.
14262 * Returns zero or ENOMEM.
14264 * The caller must initialize dst to zero.
14267 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14269 uint_t fields = src->ipp_fields;
14271 /* Start with fields that don't require memory allocation */
14272 dst->ipp_fields = fields &
14273 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14274 IPPF_RTHDR | IPPF_IPV4_OPTIONS);
14276 dst->ipp_addr = src->ipp_addr;
14277 dst->ipp_unicast_hops = src->ipp_unicast_hops;
14278 dst->ipp_hoplimit = src->ipp_hoplimit;
14279 dst->ipp_tclass = src->ipp_tclass;
14280 dst->ipp_type_of_service = src->ipp_type_of_service;
14282 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14283 IPPF_RTHDR | IPPF_IPV4_OPTIONS)))
14284 return (0);
14286 if (fields & IPPF_HOPOPTS) {
14287 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14288 if (dst->ipp_hopopts == NULL) {
14289 ip_pkt_free(dst);
14290 return (ENOMEM);
14292 dst->ipp_fields |= IPPF_HOPOPTS;
14293 bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14294 src->ipp_hopoptslen);
14295 dst->ipp_hopoptslen = src->ipp_hopoptslen;
14297 if (fields & IPPF_RTHDRDSTOPTS) {
14298 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14299 kmflag);
14300 if (dst->ipp_rthdrdstopts == NULL) {
14301 ip_pkt_free(dst);
14302 return (ENOMEM);
14304 dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14305 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14306 src->ipp_rthdrdstoptslen);
14307 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14309 if (fields & IPPF_DSTOPTS) {
14310 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14311 if (dst->ipp_dstopts == NULL) {
14312 ip_pkt_free(dst);
14313 return (ENOMEM);
14315 dst->ipp_fields |= IPPF_DSTOPTS;
14316 bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14317 src->ipp_dstoptslen);
14318 dst->ipp_dstoptslen = src->ipp_dstoptslen;
14320 if (fields & IPPF_RTHDR) {
14321 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14322 if (dst->ipp_rthdr == NULL) {
14323 ip_pkt_free(dst);
14324 return (ENOMEM);
14326 dst->ipp_fields |= IPPF_RTHDR;
14327 bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14328 src->ipp_rthdrlen);
14329 dst->ipp_rthdrlen = src->ipp_rthdrlen;
14331 if (fields & IPPF_IPV4_OPTIONS) {
14332 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14333 kmflag);
14334 if (dst->ipp_ipv4_options == NULL) {
14335 ip_pkt_free(dst);
14336 return (ENOMEM);
14338 dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14339 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14340 src->ipp_ipv4_options_len);
14341 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14343 if (fields & IPPF_FRAGHDR) {
14344 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14345 if (dst->ipp_fraghdr == NULL) {
14346 ip_pkt_free(dst);
14347 return (ENOMEM);
14349 dst->ipp_fields |= IPPF_FRAGHDR;
14350 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14351 src->ipp_fraghdrlen);
14352 dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14354 return (0);
14358 * Returns INADDR_ANY if no source route
14360 ipaddr_t
14361 ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14363 ipaddr_t nexthop = INADDR_ANY;
14364 ipoptp_t opts;
14365 uchar_t *opt;
14366 uint8_t optval;
14367 uint8_t optlen;
14368 uint32_t totallen;
14370 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14371 return (INADDR_ANY);
14373 totallen = ipp->ipp_ipv4_options_len;
14374 if (totallen & 0x3)
14375 return (INADDR_ANY);
14377 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14378 optval != IPOPT_EOL;
14379 optval = ipoptp_next(&opts)) {
14380 opt = opts.ipoptp_cur;
14381 switch (optval) {
14382 uint8_t off;
14383 case IPOPT_SSRR:
14384 case IPOPT_LSRR:
14385 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
14386 break;
14388 optlen = opts.ipoptp_len;
14389 off = opt[IPOPT_OFFSET];
14390 off--;
14391 if (optlen < IP_ADDR_LEN ||
14392 off > optlen - IP_ADDR_LEN) {
14393 /* End of source route */
14394 break;
14396 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
14397 if (nexthop == htonl(INADDR_LOOPBACK)) {
14398 /* Ignore */
14399 nexthop = INADDR_ANY;
14400 break;
14402 break;
14405 return (nexthop);
14409 * Reverse a source route.
14411 void
14412 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
14414 ipaddr_t tmp;
14415 ipoptp_t opts;
14416 uchar_t *opt;
14417 uint8_t optval;
14418 uint32_t totallen;
14420 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14421 return;
14423 totallen = ipp->ipp_ipv4_options_len;
14424 if (totallen & 0x3)
14425 return;
14427 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14428 optval != IPOPT_EOL;
14429 optval = ipoptp_next(&opts)) {
14430 uint8_t off1, off2;
14432 opt = opts.ipoptp_cur;
14433 switch (optval) {
14434 case IPOPT_SSRR:
14435 case IPOPT_LSRR:
14436 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
14437 break;
14439 off1 = IPOPT_MINOFF_SR - 1;
14440 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
14441 while (off2 > off1) {
14442 bcopy(opt + off2, &tmp, IP_ADDR_LEN);
14443 bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
14444 bcopy(&tmp, opt + off2, IP_ADDR_LEN);
14445 off2 -= IP_ADDR_LEN;
14446 off1 += IP_ADDR_LEN;
14448 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
14449 break;
14455 * Returns NULL if no routing header
14457 in6_addr_t *
14458 ip_pkt_source_route_v6(const ip_pkt_t *ipp)
14460 in6_addr_t *nexthop = NULL;
14461 ip6_rthdr0_t *rthdr;
14463 if (!(ipp->ipp_fields & IPPF_RTHDR))
14464 return (NULL);
14466 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
14467 if (rthdr->ip6r0_segleft == 0)
14468 return (NULL);
14470 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
14471 return (nexthop);
14474 zoneid_t
14475 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
14476 zoneid_t lookup_zoneid)
14478 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
14479 ire_t *ire;
14480 int ire_flags = MATCH_IRE_TYPE;
14481 zoneid_t zoneid = ALL_ZONES;
14483 if (lookup_zoneid != ALL_ZONES)
14484 ire_flags |= MATCH_IRE_ZONEONLY;
14485 ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_LOCAL | IRE_LOOPBACK,
14486 NULL, lookup_zoneid, ire_flags, 0, ipst, NULL);
14487 if (ire != NULL) {
14488 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
14489 ire_refrele(ire);
14491 return (zoneid);
14494 zoneid_t
14495 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
14496 ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
14498 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
14499 ire_t *ire;
14500 int ire_flags = MATCH_IRE_TYPE;
14501 zoneid_t zoneid = ALL_ZONES;
14503 if (IN6_IS_ADDR_LINKLOCAL(addr))
14504 ire_flags |= MATCH_IRE_ILL;
14506 if (lookup_zoneid != ALL_ZONES)
14507 ire_flags |= MATCH_IRE_ZONEONLY;
14508 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
14509 ill, lookup_zoneid, ire_flags, 0, ipst, NULL);
14510 if (ire != NULL) {
14511 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
14512 ire_refrele(ire);
14514 return (zoneid);
14518 * IP obserability hook support functions.
14520 static void
14521 ipobs_init(ip_stack_t *ipst)
14523 netid_t id;
14525 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
14527 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
14528 VERIFY(ipst->ips_ip4_observe_pr != NULL);
14530 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
14531 VERIFY(ipst->ips_ip6_observe_pr != NULL);
14534 static void
14535 ipobs_fini(ip_stack_t *ipst)
14538 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
14539 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
14543 * hook_pkt_observe_t is composed in network byte order so that the
14544 * entire mblk_t chain handed into hook_run can be used as-is.
14545 * The caveat is that use of the fields, such as the zone fields,
14546 * requires conversion into host byte order first.
14548 void
14549 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
14550 const ill_t *ill, ip_stack_t *ipst)
14552 hook_pkt_observe_t *hdr;
14553 uint64_t grifindex;
14554 mblk_t *imp;
14556 imp = allocb(sizeof (*hdr), BPRI_HI);
14557 if (imp == NULL)
14558 return;
14560 hdr = (hook_pkt_observe_t *)imp->b_rptr;
14562 * b_wptr is set to make the apparent size of the data in the mblk_t
14563 * to exclude the pointers at the end of hook_pkt_observer_t.
14565 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
14566 imp->b_cont = mp;
14568 ASSERT(DB_TYPE(mp) == M_DATA);
14570 if (IS_UNDER_IPMP(ill))
14571 grifindex = ipmp_ill_get_ipmp_ifindex(ill);
14572 else
14573 grifindex = 0;
14575 hdr->hpo_version = 1;
14576 hdr->hpo_htype = htons(htype);
14577 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
14578 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
14579 hdr->hpo_grifindex = htonl(grifindex);
14580 hdr->hpo_zsrc = htonl(zsrc);
14581 hdr->hpo_zdst = htonl(zdst);
14582 hdr->hpo_pkt = imp;
14583 hdr->hpo_ctx = ipst->ips_netstack;
14585 if (ill->ill_isv6) {
14586 hdr->hpo_family = AF_INET6;
14587 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
14588 ipst->ips_ipv6observing, (hook_data_t)hdr);
14589 } else {
14590 hdr->hpo_family = AF_INET;
14591 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
14592 ipst->ips_ipv4observing, (hook_data_t)hdr);
14595 imp->b_cont = NULL;
14596 freemsg(imp);
14600 * Utility routine that checks if `v4srcp' is a valid address on underlying
14601 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif
14602 * associated with `v4srcp' on success. NOTE: if this is not called from
14603 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
14604 * group during or after this lookup.
14606 boolean_t
14607 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
14609 ipif_t *ipif;
14611 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
14612 if (ipif != NULL) {
14613 if (ipifp != NULL)
14614 *ipifp = ipif;
14615 else
14616 ipif_refrele(ipif);
14617 return (B_TRUE);
14620 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
14621 *v4srcp));
14622 return (B_FALSE);
14626 * Transport protocol call back function for CPU state change.
14628 /* ARGSUSED */
14629 static int
14630 ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
14632 processorid_t cpu_seqid;
14633 netstack_handle_t nh;
14634 netstack_t *ns;
14636 ASSERT(MUTEX_HELD(&cpu_lock));
14638 switch (what) {
14639 case CPU_CONFIG:
14640 case CPU_ON:
14641 case CPU_INIT:
14642 case CPU_CPUPART_IN:
14643 cpu_seqid = cpu[id]->cpu_seqid;
14644 netstack_next_init(&nh);
14645 while ((ns = netstack_next(&nh)) != NULL) {
14646 tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
14647 sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
14648 udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
14649 netstack_rele(ns);
14651 netstack_next_fini(&nh);
14652 break;
14653 case CPU_UNCONFIG:
14654 case CPU_OFF:
14655 case CPU_CPUPART_OUT:
14657 * Nothing to do. We don't remove the per CPU stats from
14658 * the IP stack even when the CPU goes offline.
14660 break;
14661 default:
14662 break;
14664 return (0);