Unleashed v1.4
[unleashed.git] / kernel / net / ip / ip.c
blob31105a55ad1b8ee5fadd6ec0de8335c0f2a44db4
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 static char *ip_dot_saddr(uchar_t *, char *);
612 static int ip_lrput(queue_t *, mblk_t *);
613 ipaddr_t ip_net_mask(ipaddr_t);
614 char *ip_nv_lookup(nv_t *, int);
615 int ip_rput(queue_t *, mblk_t *);
616 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
617 void *dummy_arg);
618 int ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
619 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
620 mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
621 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
622 ip_stack_t *, boolean_t);
623 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
624 boolean_t);
625 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
626 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
627 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
628 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
629 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
630 ip_stack_t *ipst, boolean_t);
631 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
632 ip_stack_t *ipst, boolean_t);
633 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
634 ip_stack_t *ipst);
635 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
636 ip_stack_t *ipst);
637 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
638 ip_stack_t *ipst);
639 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
640 ip_stack_t *ipst);
641 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
642 ip_stack_t *ipst);
643 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
644 ip_stack_t *ipst);
645 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
646 ip_stack_t *ipst);
647 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
648 ip_stack_t *ipst);
649 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
650 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
651 static void ip_snmp_get2_v4_media(ncec_t *, void *);
652 static void ip_snmp_get2_v6_media(ncec_t *, void *);
653 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
655 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
656 mblk_t *);
658 static void conn_drain_init(ip_stack_t *);
659 static void conn_drain_fini(ip_stack_t *);
660 static void conn_drain(conn_t *connp, boolean_t closing);
662 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
663 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
665 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
666 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
667 static void ip_stack_fini(netstackid_t stackid, void *arg);
669 static int ip_squeue_switch(int);
671 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
672 static void ip_kstat_fini(netstackid_t, kstat_t *);
673 static int ip_kstat_update(kstat_t *kp, int rw);
674 static void *icmp_kstat_init(netstackid_t);
675 static void icmp_kstat_fini(netstackid_t, kstat_t *);
676 static int icmp_kstat_update(kstat_t *kp, int rw);
677 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
678 static void ip_kstat2_fini(netstackid_t, kstat_t *);
680 static void ipobs_init(ip_stack_t *);
681 static void ipobs_fini(ip_stack_t *);
683 static int ip_tp_cpu_update(cpu_setup_t, int, void *);
685 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
687 static long ip_rput_pullups;
688 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
690 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
691 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
693 int ip_debug;
696 * IP tunables related declarations. Definitions are in ip_tunables.c
698 extern mod_prop_info_t ip_propinfo_tbl[];
699 extern int ip_propinfo_count;
702 * Table of IP ioctls encoding the various properties of the ioctl and
703 * indexed based on the last byte of the ioctl command. Occasionally there
704 * is a clash, and there is more than 1 ioctl with the same last byte.
705 * In such a case 1 ioctl is encoded in the ndx table and the remaining
706 * ioctls are encoded in the misc table. An entry in the ndx table is
707 * retrieved by indexing on the last byte of the ioctl command and comparing
708 * the ioctl command with the value in the ndx table. In the event of a
709 * mismatch the misc table is then searched sequentially for the desired
710 * ioctl command.
712 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
714 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
715 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
716 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
717 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
718 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
719 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
720 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
721 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
722 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
723 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
724 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
726 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
727 MISC_CMD, ip_siocaddrt, NULL },
728 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
729 MISC_CMD, ip_siocdelrt, NULL },
731 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
732 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
733 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
734 IF_CMD, ip_sioctl_get_addr, NULL },
736 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
737 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
738 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
739 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
741 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
742 IPI_PRIV | IPI_WR,
743 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
744 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
745 IPI_MODOK | IPI_GET_CMD,
746 IF_CMD, ip_sioctl_get_flags, NULL },
748 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
749 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
751 /* copyin size cannot be coded for SIOCGIFCONF */
752 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
753 MISC_CMD, ip_sioctl_get_ifconf, NULL },
755 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
756 IF_CMD, ip_sioctl_mtu, NULL },
757 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
758 IF_CMD, ip_sioctl_get_mtu, NULL },
759 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
760 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
761 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
762 IF_CMD, ip_sioctl_brdaddr, NULL },
763 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
764 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
765 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
766 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
767 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
768 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
769 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
770 IF_CMD, ip_sioctl_metric, NULL },
771 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
773 /* See 166-168 below for extended SIOC*XARP ioctls */
774 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
775 ARP_CMD, ip_sioctl_arp, NULL },
776 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
777 ARP_CMD, ip_sioctl_arp, NULL },
778 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
779 ARP_CMD, ip_sioctl_arp, NULL },
781 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
782 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
783 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
784 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
785 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
786 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
787 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
788 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
789 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
790 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
791 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
792 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
793 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
794 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
795 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
796 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
797 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
798 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
799 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
800 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
801 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
803 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
804 MISC_CMD, if_unitsel, if_unitsel_restart },
806 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
807 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
808 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
809 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
810 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
811 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
812 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
813 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
814 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
815 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
816 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
817 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
818 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
819 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
820 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
821 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
822 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
823 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
825 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
826 IPI_PRIV | IPI_WR | IPI_MODOK,
827 IF_CMD, ip_sioctl_sifname, NULL },
829 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
830 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
831 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
832 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
833 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
834 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
835 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
836 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
837 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
838 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
839 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
840 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
841 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
843 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
844 MISC_CMD, ip_sioctl_get_ifnum, NULL },
845 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
846 IF_CMD, ip_sioctl_get_muxid, NULL },
847 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
848 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
850 /* Both if and lif variants share same func */
851 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
852 IF_CMD, ip_sioctl_get_lifindex, NULL },
853 /* Both if and lif variants share same func */
854 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
855 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
857 /* copyin size cannot be coded for SIOCGIFCONF */
858 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
859 MISC_CMD, ip_sioctl_get_ifconf, NULL },
860 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
861 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
862 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
863 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
864 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
865 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
866 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
867 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
868 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
869 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
870 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
871 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
872 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
873 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
874 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
875 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
876 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
878 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
879 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
880 ip_sioctl_removeif_restart },
881 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
882 IPI_GET_CMD | IPI_PRIV | IPI_WR,
883 LIF_CMD, ip_sioctl_addif, NULL },
884 #define SIOCLIFADDR_NDX 112
885 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
886 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
887 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
888 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
889 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
890 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
891 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
892 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
893 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
894 IPI_PRIV | IPI_WR,
895 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
896 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
897 IPI_GET_CMD | IPI_MODOK,
898 LIF_CMD, ip_sioctl_get_flags, NULL },
900 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
901 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
903 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
904 ip_sioctl_get_lifconf, NULL },
905 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
906 LIF_CMD, ip_sioctl_mtu, NULL },
907 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
908 LIF_CMD, ip_sioctl_get_mtu, NULL },
909 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
910 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
911 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
912 LIF_CMD, ip_sioctl_brdaddr, NULL },
913 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
914 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
915 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
916 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
917 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
918 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
919 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
920 LIF_CMD, ip_sioctl_metric, NULL },
921 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
922 IPI_PRIV | IPI_WR | IPI_MODOK,
923 LIF_CMD, ip_sioctl_slifname,
924 ip_sioctl_slifname_restart },
926 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
927 MISC_CMD, ip_sioctl_get_lifnum, NULL },
928 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
929 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
930 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
931 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
932 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
933 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
934 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
935 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
936 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
937 LIF_CMD, ip_sioctl_token, NULL },
938 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
939 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
940 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
941 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
942 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
943 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
944 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
945 LIF_CMD, ip_sioctl_lnkinfo, NULL },
947 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
948 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
949 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
950 LIF_CMD, ip_siocdelndp_v6, NULL },
951 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
952 LIF_CMD, ip_siocqueryndp_v6, NULL },
953 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
954 LIF_CMD, ip_siocsetndp_v6, NULL },
955 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
956 MISC_CMD, ip_sioctl_tmyaddr, NULL },
957 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
958 MISC_CMD, ip_sioctl_tonlink, NULL },
959 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
960 MISC_CMD, ip_sioctl_tmysite, NULL },
961 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
962 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
964 /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
965 /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
966 /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
967 /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
968 /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
970 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
972 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
973 LIF_CMD, ip_sioctl_get_binding, NULL },
974 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
975 IPI_PRIV | IPI_WR,
976 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
977 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
978 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
979 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
980 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
982 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
983 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
984 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
985 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
987 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
989 /* These are handled in ip_sioctl_copyin_setup itself */
990 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
991 MISC_CMD, NULL, NULL },
992 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
993 MISC_CMD, NULL, NULL },
994 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
996 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
997 ip_sioctl_get_lifconf, NULL },
999 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1000 XARP_CMD, ip_sioctl_arp, NULL },
1001 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1002 XARP_CMD, ip_sioctl_arp, NULL },
1003 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1004 XARP_CMD, ip_sioctl_arp, NULL },
1006 /* SIOCPOPSOCKFS is not handled by IP */
1007 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1009 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1010 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1011 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1012 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1013 ip_sioctl_slifzone_restart },
1014 /* 172-174 are SCTP ioctls and not handled by IP */
1015 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1016 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1017 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1018 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1019 IPI_GET_CMD, LIF_CMD,
1020 ip_sioctl_get_lifusesrc, 0 },
1021 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1022 IPI_PRIV | IPI_WR,
1023 LIF_CMD, ip_sioctl_slifusesrc,
1024 NULL },
1025 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1026 ip_sioctl_get_lifsrcof, NULL },
1027 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1028 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1029 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1030 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1031 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1032 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1033 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1034 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1035 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1036 /* SIOCSENABLESDP is handled by SDP */
1037 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1038 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1039 /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1040 IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1041 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1042 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1043 ip_sioctl_ilb_cmd, NULL },
1044 /* 188 */ { SIOCGETPROP, sizeof (mod_ioc_prop_t), IPI_GET_CMD,
1045 MISC_CMD, ip_sioctl_getsetprop, NULL },
1046 /* 189 */ { SIOCSETPROP, sizeof (mod_ioc_prop_t), IPI_PRIV,
1047 MISC_CMD, ip_sioctl_getsetprop, NULL},
1048 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1049 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1050 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1051 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1052 /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1053 LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1056 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1058 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1059 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1060 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1061 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1062 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1063 { ND_GET, 0, 0, 0, NULL, NULL },
1064 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1065 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1066 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1067 MISC_CMD, mrt_ioctl},
1068 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1069 MISC_CMD, mrt_ioctl},
1070 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1071 MISC_CMD, mrt_ioctl}
1074 int ip_misc_ioctl_count =
1075 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1077 int conn_drain_nthreads; /* Number of drainers reqd. */
1078 /* Settable in /etc/system */
1079 /* Defined in ip_ire.c */
1080 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1081 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1082 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1084 static nv_t ire_nv_arr[] = {
1085 { IRE_BROADCAST, "BROADCAST" },
1086 { IRE_LOCAL, "LOCAL" },
1087 { IRE_LOOPBACK, "LOOPBACK" },
1088 { IRE_DEFAULT, "DEFAULT" },
1089 { IRE_PREFIX, "PREFIX" },
1090 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1091 { IRE_IF_RESOLVER, "IF_RESOLV" },
1092 { IRE_IF_CLONE, "IF_CLONE" },
1093 { IRE_HOST, "HOST" },
1094 { IRE_MULTICAST, "MULTICAST" },
1095 { IRE_NOROUTE, "NOROUTE" },
1096 { 0 }
1099 nv_t *ire_nv_tbl = ire_nv_arr;
1101 /* Simple ICMP IP Header Template */
1102 static ipha_t icmp_ipha = {
1103 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1106 struct module_info ip_mod_info = {
1107 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1108 IP_MOD_LOWAT
1112 * Duplicate static symbols within a module confuses mdb; so we avoid the
1113 * problem by making the symbols here distinct from those in udp.c.
1117 * Entry points for IP as a device and as a module.
1118 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1120 static struct qinit iprinitv4 = {
1121 ip_rput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1124 struct qinit iprinitv6 = {
1125 ip_rput_v6, NULL, ip_openv6, ip_close, NULL, &ip_mod_info
1128 static struct qinit ipwinit = {
1129 ip_wput_nondata, ip_wsrv, NULL, NULL, NULL, &ip_mod_info
1132 static struct qinit iplrinit = {
1133 ip_lrput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1136 static struct qinit iplwinit = {
1137 ip_lwput, NULL, NULL, NULL, NULL, &ip_mod_info
1140 /* For AF_INET aka /dev/ip */
1141 struct streamtab ipinfov4 = {
1142 &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1145 /* For AF_INET6 aka /dev/ip6 */
1146 struct streamtab ipinfov6 = {
1147 &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1150 #ifdef DEBUG
1151 boolean_t skip_sctp_cksum = B_FALSE;
1152 #endif
1155 * Generate an ICMP fragmentation needed message.
1156 * When called from ip_output side a minimal ip_recv_attr_t needs to be
1157 * constructed by the caller.
1159 void
1160 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1162 icmph_t icmph;
1163 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1165 mp = icmp_pkt_err_ok(mp, ira);
1166 if (mp == NULL)
1167 return;
1169 bzero(&icmph, sizeof (icmph_t));
1170 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1171 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1172 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1173 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1174 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1176 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1180 * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1181 * If the ICMP message is consumed by IP, i.e., it should not be delivered
1182 * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1183 * Likewise, if the ICMP error is misformed (too short, etc), then it
1184 * returns NULL. The caller uses this to determine whether or not to send
1185 * to raw sockets.
1187 * All error messages are passed to the matching transport stream.
1189 * The following cases are handled by icmp_inbound:
1190 * 1) It needs to send a reply back and possibly delivering it
1191 * to the "interested" upper clients.
1192 * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1193 * 3) It needs to change some values in IP only.
1194 * 4) It needs to change some values in IP and upper layers e.g TCP
1195 * by delivering an error to the upper layers.
1197 * We handle the above three cases in the context of IPsec in the
1198 * following way :
1200 * 1) Send the reply back in the same way as the request came in.
1201 * If it came in encrypted, it goes out encrypted. If it came in
1202 * clear, it goes out in clear. Thus, this will prevent chosen
1203 * plain text attack.
1204 * 2) The client may or may not expect things to come in secure.
1205 * If it comes in secure, the policy constraints are checked
1206 * before delivering it to the upper layers. If it comes in
1207 * clear, ipsec_inbound_accept_clear will decide whether to
1208 * accept this in clear or not. In both the cases, if the returned
1209 * message (IP header + 8 bytes) that caused the icmp message has
1210 * AH/ESP headers, it is sent up to AH/ESP for validation before
1211 * sending up. If there are only 8 bytes of returned message, then
1212 * upper client will not be notified.
1213 * 3) Check with global policy to see whether it matches the constaints.
1214 * But this will be done only if icmp_accept_messages_in_clear is
1215 * zero.
1216 * 4) If we need to change both in IP and ULP, then the decision taken
1217 * while affecting the values in IP and while delivering up to TCP
1218 * should be the same.
1220 * There are two cases.
1222 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1223 * failed), we will not deliver it to the ULP, even though they
1224 * are *willing* to accept in *clear*. This is fine as our global
1225 * disposition to icmp messages asks us reject the datagram.
1227 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1228 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1229 * to deliver it to ULP (policy failed), it can lead to
1230 * consistency problems. The cases known at this time are
1231 * ICMP_DESTINATION_UNREACHABLE messages with following code
1232 * values :
1234 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1235 * and Upper layer rejects. Then the communication will
1236 * come to a stop. This is solved by making similar decisions
1237 * at both levels. Currently, when we are unable to deliver
1238 * to the Upper Layer (due to policy failures) while IP has
1239 * adjusted dce_pmtu, the next outbound datagram would
1240 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1241 * will be with the right level of protection. Thus the right
1242 * value will be communicated even if we are not able to
1243 * communicate when we get from the wire initially. But this
1244 * assumes there would be at least one outbound datagram after
1245 * IP has adjusted its dce_pmtu value. To make things
1246 * simpler, we accept in clear after the validation of
1247 * AH/ESP headers.
1249 * - Other ICMP ERRORS : We may not be able to deliver it to the
1250 * upper layer depending on the level of protection the upper
1251 * layer expects and the disposition in ipsec_inbound_accept_clear().
1252 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1253 * should be accepted in clear when the Upper layer expects secure.
1254 * Thus the communication may get aborted by some bad ICMP
1255 * packets.
1257 mblk_t *
1258 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1260 icmph_t *icmph;
1261 ipha_t *ipha; /* Outer header */
1262 int ip_hdr_length; /* Outer header length */
1263 boolean_t interested;
1264 ipif_t *ipif;
1265 uint32_t ts;
1266 uint32_t *tsp;
1267 timestruc_t now;
1268 ill_t *ill = ira->ira_ill;
1269 ip_stack_t *ipst = ill->ill_ipst;
1270 zoneid_t zoneid = ira->ira_zoneid;
1271 int len_needed;
1272 mblk_t *mp_ret = NULL;
1274 ipha = (ipha_t *)mp->b_rptr;
1276 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1278 ip_hdr_length = ira->ira_ip_hdr_length;
1279 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1280 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1281 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1282 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1283 freemsg(mp);
1284 return (NULL);
1286 /* Last chance to get real. */
1287 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1288 if (ipha == NULL) {
1289 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1290 freemsg(mp);
1291 return (NULL);
1295 /* The IP header will always be a multiple of four bytes */
1296 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1297 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1298 icmph->icmph_code));
1301 * We will set "interested" to "true" if we should pass a copy to
1302 * the transport or if we handle the packet locally.
1304 interested = B_FALSE;
1305 switch (icmph->icmph_type) {
1306 case ICMP_ECHO_REPLY:
1307 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1308 break;
1309 case ICMP_DEST_UNREACHABLE:
1310 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1311 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1312 interested = B_TRUE; /* Pass up to transport */
1313 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1314 break;
1315 case ICMP_SOURCE_QUENCH:
1316 interested = B_TRUE; /* Pass up to transport */
1317 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1318 break;
1319 case ICMP_REDIRECT:
1320 if (!ipst->ips_ip_ignore_redirect)
1321 interested = B_TRUE;
1322 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1323 break;
1324 case ICMP_ECHO_REQUEST:
1326 * Whether to respond to echo requests that come in as IP
1327 * broadcasts or as IP multicast is subject to debate
1328 * (what isn't?). We aim to please, you pick it.
1329 * Default is do it.
1331 if (ira->ira_flags & IRAF_MULTICAST) {
1332 /* multicast: respond based on tunable */
1333 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1334 } else if (ira->ira_flags & IRAF_BROADCAST) {
1335 /* broadcast: respond based on tunable */
1336 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1337 } else {
1338 /* unicast: always respond */
1339 interested = B_TRUE;
1341 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1342 if (!interested) {
1343 /* We never pass these to RAW sockets */
1344 freemsg(mp);
1345 return (NULL);
1348 /* Check db_ref to make sure we can modify the packet. */
1349 if (mp->b_datap->db_ref > 1) {
1350 mblk_t *mp1;
1352 mp1 = copymsg(mp);
1353 freemsg(mp);
1354 if (!mp1) {
1355 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1356 return (NULL);
1358 mp = mp1;
1359 ipha = (ipha_t *)mp->b_rptr;
1360 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1362 icmph->icmph_type = ICMP_ECHO_REPLY;
1363 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1364 icmp_send_reply_v4(mp, ipha, icmph, ira);
1365 return (NULL);
1367 case ICMP_ROUTER_ADVERTISEMENT:
1368 case ICMP_ROUTER_SOLICITATION:
1369 break;
1370 case ICMP_TIME_EXCEEDED:
1371 interested = B_TRUE; /* Pass up to transport */
1372 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1373 break;
1374 case ICMP_PARAM_PROBLEM:
1375 interested = B_TRUE; /* Pass up to transport */
1376 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1377 break;
1378 case ICMP_TIME_STAMP_REQUEST:
1379 /* Response to Time Stamp Requests is local policy. */
1380 if (ipst->ips_ip_g_resp_to_timestamp) {
1381 if (ira->ira_flags & IRAF_MULTIBROADCAST)
1382 interested =
1383 ipst->ips_ip_g_resp_to_timestamp_bcast;
1384 else
1385 interested = B_TRUE;
1387 if (!interested) {
1388 /* We never pass these to RAW sockets */
1389 freemsg(mp);
1390 return (NULL);
1393 /* Make sure we have enough of the packet */
1394 len_needed = ip_hdr_length + ICMPH_SIZE +
1395 3 * sizeof (uint32_t);
1397 if (mp->b_wptr - mp->b_rptr < len_needed) {
1398 ipha = ip_pullup(mp, len_needed, ira);
1399 if (ipha == NULL) {
1400 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1401 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1402 mp, ill);
1403 freemsg(mp);
1404 return (NULL);
1406 /* Refresh following the pullup. */
1407 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1409 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1410 /* Check db_ref to make sure we can modify the packet. */
1411 if (mp->b_datap->db_ref > 1) {
1412 mblk_t *mp1;
1414 mp1 = copymsg(mp);
1415 freemsg(mp);
1416 if (!mp1) {
1417 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1418 return (NULL);
1420 mp = mp1;
1421 ipha = (ipha_t *)mp->b_rptr;
1422 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1424 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1425 tsp = (uint32_t *)&icmph[1];
1426 tsp++; /* Skip past 'originate time' */
1427 /* Compute # of milliseconds since midnight */
1428 gethrestime(&now);
1429 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1430 NSEC2MSEC(now.tv_nsec);
1431 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1432 *tsp++ = htonl(ts); /* Lay in 'send time' */
1433 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1434 icmp_send_reply_v4(mp, ipha, icmph, ira);
1435 return (NULL);
1437 case ICMP_TIME_STAMP_REPLY:
1438 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1439 break;
1440 case ICMP_INFO_REQUEST:
1441 /* Per RFC 1122 3.2.2.7, ignore this. */
1442 case ICMP_INFO_REPLY:
1443 break;
1444 case ICMP_ADDRESS_MASK_REQUEST:
1445 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1446 interested =
1447 ipst->ips_ip_respond_to_address_mask_broadcast;
1448 } else {
1449 interested = B_TRUE;
1451 if (!interested) {
1452 /* We never pass these to RAW sockets */
1453 freemsg(mp);
1454 return (NULL);
1456 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1457 if (mp->b_wptr - mp->b_rptr < len_needed) {
1458 ipha = ip_pullup(mp, len_needed, ira);
1459 if (ipha == NULL) {
1460 BUMP_MIB(ill->ill_ip_mib,
1461 ipIfStatsInTruncatedPkts);
1462 ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1463 ill);
1464 freemsg(mp);
1465 return (NULL);
1467 /* Refresh following the pullup. */
1468 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1470 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1471 /* Check db_ref to make sure we can modify the packet. */
1472 if (mp->b_datap->db_ref > 1) {
1473 mblk_t *mp1;
1475 mp1 = copymsg(mp);
1476 freemsg(mp);
1477 if (!mp1) {
1478 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1479 return (NULL);
1481 mp = mp1;
1482 ipha = (ipha_t *)mp->b_rptr;
1483 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1486 * Need the ipif with the mask be the same as the source
1487 * address of the mask reply. For unicast we have a specific
1488 * ipif. For multicast/broadcast we only handle onlink
1489 * senders, and use the source address to pick an ipif.
1491 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1492 if (ipif == NULL) {
1493 /* Broadcast or multicast */
1494 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1495 if (ipif == NULL) {
1496 freemsg(mp);
1497 return (NULL);
1500 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1501 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1502 ipif_refrele(ipif);
1503 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1504 icmp_send_reply_v4(mp, ipha, icmph, ira);
1505 return (NULL);
1507 case ICMP_ADDRESS_MASK_REPLY:
1508 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1509 break;
1510 default:
1511 interested = B_TRUE; /* Pass up to transport */
1512 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1513 break;
1516 * See if there is an ICMP client to avoid an extra copymsg/freemsg
1517 * if there isn't one.
1519 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1520 /* If there is an ICMP client and we want one too, copy it. */
1522 if (!interested) {
1523 /* Caller will deliver to RAW sockets */
1524 return (mp);
1526 mp_ret = copymsg(mp);
1527 if (mp_ret == NULL) {
1528 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1529 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1531 } else if (!interested) {
1532 /* Neither we nor raw sockets are interested. Drop packet now */
1533 freemsg(mp);
1534 return (NULL);
1538 * ICMP error or redirect packet. Make sure we have enough of
1539 * the header and that db_ref == 1 since we might end up modifying
1540 * the packet.
1542 if (mp->b_cont != NULL) {
1543 if (ip_pullup(mp, -1, ira) == NULL) {
1544 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1545 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1546 mp, ill);
1547 freemsg(mp);
1548 return (mp_ret);
1552 if (mp->b_datap->db_ref > 1) {
1553 mblk_t *mp1;
1555 mp1 = copymsg(mp);
1556 if (mp1 == NULL) {
1557 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1558 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1559 freemsg(mp);
1560 return (mp_ret);
1562 freemsg(mp);
1563 mp = mp1;
1567 * In case mp has changed, verify the message before any further
1568 * processes.
1570 ipha = (ipha_t *)mp->b_rptr;
1571 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1572 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1573 freemsg(mp);
1574 return (mp_ret);
1577 switch (icmph->icmph_type) {
1578 case ICMP_REDIRECT:
1579 icmp_redirect_v4(mp, ipha, icmph, ira);
1580 break;
1581 case ICMP_DEST_UNREACHABLE:
1582 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1583 /* Update DCE and adjust MTU is icmp header if needed */
1584 icmp_inbound_too_big_v4(icmph, ira);
1586 /* FALLTHROUGH */
1587 default:
1588 icmp_inbound_error_fanout_v4(mp, icmph, ira);
1589 break;
1591 return (mp_ret);
1595 * Send an ICMP echo, timestamp or address mask reply.
1596 * The caller has already updated the payload part of the packet.
1597 * We handle the ICMP checksum, IP source address selection and feed
1598 * the packet into ip_output_simple.
1600 static void
1601 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1602 ip_recv_attr_t *ira)
1604 uint_t ip_hdr_length = ira->ira_ip_hdr_length;
1605 ill_t *ill = ira->ira_ill;
1606 ip_stack_t *ipst = ill->ill_ipst;
1607 ip_xmit_attr_t ixas;
1609 /* Send out an ICMP packet */
1610 icmph->icmph_checksum = 0;
1611 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1612 /* Reset time to live. */
1613 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1615 /* Swap source and destination addresses */
1616 ipaddr_t tmp;
1618 tmp = ipha->ipha_src;
1619 ipha->ipha_src = ipha->ipha_dst;
1620 ipha->ipha_dst = tmp;
1622 ipha->ipha_ident = 0;
1623 if (!IS_SIMPLE_IPH(ipha))
1624 icmp_options_update(ipha);
1626 bzero(&ixas, sizeof (ixas));
1627 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1628 ixas.ixa_zoneid = ira->ira_zoneid;
1629 ixas.ixa_cred = kcred;
1630 ixas.ixa_cpid = NOPID;
1631 ixas.ixa_ifindex = 0;
1632 ixas.ixa_ipst = ipst;
1633 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1635 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1637 * This packet should go out the same way as it
1638 * came in i.e in clear, independent of the IPsec policy
1639 * for transmitting packets.
1641 ixas.ixa_flags |= IXAF_NO_IPSEC;
1642 } else {
1643 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1644 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1645 /* Note: mp already consumed and ip_drop_packet done */
1646 return;
1649 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1651 * Not one or our addresses (IRE_LOCALs), thus we let
1652 * ip_output_simple pick the source.
1654 ipha->ipha_src = INADDR_ANY;
1655 ixas.ixa_flags |= IXAF_SET_SOURCE;
1657 /* Should we send with DF and use dce_pmtu? */
1658 if (ipst->ips_ipv4_icmp_return_pmtu) {
1659 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1660 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1663 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1665 (void) ip_output_simple(mp, &ixas);
1666 ixa_cleanup(&ixas);
1670 * Verify the ICMP messages for either for ICMP error or redirect packet.
1671 * The caller should have fully pulled up the message. If it's a redirect
1672 * packet, only basic checks on IP header will be done; otherwise, verify
1673 * the packet by looking at the included ULP header.
1675 * Called before icmp_inbound_error_fanout_v4 is called.
1677 static boolean_t
1678 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1680 ill_t *ill = ira->ira_ill;
1681 int hdr_length;
1682 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1683 conn_t *connp;
1684 ipha_t *ipha; /* Inner IP header */
1686 ipha = (ipha_t *)&icmph[1];
1687 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1688 goto truncated;
1690 hdr_length = IPH_HDR_LENGTH(ipha);
1692 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1693 goto discard_pkt;
1695 if (hdr_length < sizeof (ipha_t))
1696 goto truncated;
1698 if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1699 goto truncated;
1702 * Stop here for ICMP_REDIRECT.
1704 if (icmph->icmph_type == ICMP_REDIRECT)
1705 return (B_TRUE);
1708 * ICMP errors only.
1710 switch (ipha->ipha_protocol) {
1711 case IPPROTO_UDP:
1713 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1714 * transport header.
1716 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1717 mp->b_wptr)
1718 goto truncated;
1719 break;
1720 case IPPROTO_TCP: {
1721 tcpha_t *tcpha;
1724 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1725 * transport header.
1727 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1728 mp->b_wptr)
1729 goto truncated;
1731 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1732 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1733 ipst);
1734 if (connp == NULL)
1735 goto discard_pkt;
1737 if ((connp->conn_verifyicmp != NULL) &&
1738 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1739 CONN_DEC_REF(connp);
1740 goto discard_pkt;
1742 CONN_DEC_REF(connp);
1743 break;
1745 case IPPROTO_SCTP:
1747 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1748 * transport header.
1750 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1751 mp->b_wptr)
1752 goto truncated;
1753 break;
1754 case IPPROTO_ESP:
1755 case IPPROTO_AH:
1756 break;
1757 case IPPROTO_ENCAP:
1758 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1759 mp->b_wptr)
1760 goto truncated;
1761 break;
1762 default:
1763 break;
1766 return (B_TRUE);
1768 discard_pkt:
1769 /* Bogus ICMP error. */
1770 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1771 return (B_FALSE);
1773 truncated:
1774 /* We pulled up everthing already. Must be truncated */
1775 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1776 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1777 return (B_FALSE);
1780 /* Table from RFC 1191 */
1781 static int icmp_frag_size_table[] =
1782 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1785 * Process received ICMP Packet too big.
1786 * Just handles the DCE create/update, including using the above table of
1787 * PMTU guesses. The caller is responsible for validating the packet before
1788 * passing it in and also to fanout the ICMP error to any matching transport
1789 * conns. Assumes the message has been fully pulled up and verified.
1791 * Before getting here, the caller has called icmp_inbound_verify_v4()
1792 * that should have verified with ULP to prevent undoing the changes we're
1793 * going to make to DCE. For example, TCP might have verified that the packet
1794 * which generated error is in the send window.
1796 * In some cases modified this MTU in the ICMP header packet; the caller
1797 * should pass to the matching ULP after this returns.
1799 static void
1800 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1802 dce_t *dce;
1803 int old_mtu;
1804 int mtu, orig_mtu;
1805 ipaddr_t dst;
1806 boolean_t disable_pmtud;
1807 ill_t *ill = ira->ira_ill;
1808 ip_stack_t *ipst = ill->ill_ipst;
1809 uint_t hdr_length;
1810 ipha_t *ipha;
1812 /* Caller already pulled up everything. */
1813 ipha = (ipha_t *)&icmph[1];
1814 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1815 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1816 ASSERT(ill != NULL);
1818 hdr_length = IPH_HDR_LENGTH(ipha);
1821 * We handle path MTU for source routed packets since the DCE
1822 * is looked up using the final destination.
1824 dst = ip_get_dst(ipha);
1826 dce = dce_lookup_and_add_v4(dst, ipst);
1827 if (dce == NULL) {
1828 /* Couldn't add a unique one - ENOMEM */
1829 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1830 ntohl(dst)));
1831 return;
1834 /* Check for MTU discovery advice as described in RFC 1191 */
1835 mtu = ntohs(icmph->icmph_du_mtu);
1836 orig_mtu = mtu;
1837 disable_pmtud = B_FALSE;
1839 mutex_enter(&dce->dce_lock);
1840 if (dce->dce_flags & DCEF_PMTU)
1841 old_mtu = dce->dce_pmtu;
1842 else
1843 old_mtu = ill->ill_mtu;
1845 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1846 uint32_t length;
1847 int i;
1850 * Use the table from RFC 1191 to figure out
1851 * the next "plateau" based on the length in
1852 * the original IP packet.
1854 length = ntohs(ipha->ipha_length);
1855 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1856 uint32_t, length);
1857 if (old_mtu <= length &&
1858 old_mtu >= length - hdr_length) {
1860 * Handle broken BSD 4.2 systems that
1861 * return the wrong ipha_length in ICMP
1862 * errors.
1864 ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1865 length, old_mtu));
1866 length -= hdr_length;
1868 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1869 if (length > icmp_frag_size_table[i])
1870 break;
1872 if (i == A_CNT(icmp_frag_size_table)) {
1873 /* Smaller than IP_MIN_MTU! */
1874 ip1dbg(("Too big for packet size %d\n",
1875 length));
1876 disable_pmtud = B_TRUE;
1877 mtu = ipst->ips_ip_pmtu_min;
1878 } else {
1879 mtu = icmp_frag_size_table[i];
1880 ip1dbg(("Calculated mtu %d, packet size %d, "
1881 "before %d\n", mtu, length, old_mtu));
1882 if (mtu < ipst->ips_ip_pmtu_min) {
1883 mtu = ipst->ips_ip_pmtu_min;
1884 disable_pmtud = B_TRUE;
1888 if (disable_pmtud)
1889 dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1890 else
1891 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1893 dce->dce_pmtu = MIN(old_mtu, mtu);
1894 /* Prepare to send the new max frag size for the ULP. */
1895 icmph->icmph_du_zero = 0;
1896 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu);
1897 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1898 dce, int, orig_mtu, int, mtu);
1900 /* We now have a PMTU for sure */
1901 dce->dce_flags |= DCEF_PMTU;
1902 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1903 mutex_exit(&dce->dce_lock);
1905 * After dropping the lock the new value is visible to everyone.
1906 * Then we bump the generation number so any cached values reinspect
1907 * the dce_t.
1909 dce_increment_generation(dce);
1910 dce_refrele(dce);
1914 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1915 * calls this function.
1917 static mblk_t *
1918 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1920 int length;
1922 ASSERT(mp->b_datap->db_type == M_DATA);
1924 /* icmp_inbound_v4 has already pulled up the whole error packet */
1925 ASSERT(mp->b_cont == NULL);
1928 * The length that we want to overlay is the inner header
1929 * and what follows it.
1931 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
1934 * Overlay the inner header and whatever follows it over the
1935 * outer header.
1937 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
1939 /* Adjust for what we removed */
1940 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
1941 return (mp);
1945 * Try to pass the ICMP message upstream in case the ULP cares.
1947 * If the packet that caused the ICMP error is secure, we send
1948 * it to AH/ESP to make sure that the attached packet has a
1949 * valid association. ipha in the code below points to the
1950 * IP header of the packet that caused the error.
1952 * For IPsec cases, we let the next-layer-up (which has access to
1953 * cached policy on the conn_t, or can query the SPD directly)
1954 * subtract out any IPsec overhead if they must. We therefore make no
1955 * adjustments here for IPsec overhead.
1957 * IFN could have been generated locally or by some router.
1959 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
1960 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
1961 * This happens because IP adjusted its value of MTU on an
1962 * earlier IFN message and could not tell the upper layer,
1963 * the new adjusted value of MTU e.g. Packet was encrypted
1964 * or there was not enough information to fanout to upper
1965 * layers. Thus on the next outbound datagram, ire_send_wire
1966 * generates the IFN, where IPsec processing has *not* been
1967 * done.
1969 * Note that we retain ixa_fragsize across IPsec thus once
1970 * we have picking ixa_fragsize and entered ipsec_out_process we do
1971 * no change the fragsize even if the path MTU changes before
1972 * we reach ip_output_post_ipsec.
1974 * In the local case, IRAF_LOOPBACK will be set indicating
1975 * that IFN was generated locally.
1977 * ROUTER : IFN could be secure or non-secure.
1979 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
1980 * packet in error has AH/ESP headers to validate the AH/ESP
1981 * headers. AH/ESP will verify whether there is a valid SA or
1982 * not and send it back. We will fanout again if we have more
1983 * data in the packet.
1985 * If the packet in error does not have AH/ESP, we handle it
1986 * like any other case.
1988 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it
1989 * up to AH/ESP for validation. AH/ESP will verify whether there is a
1990 * valid SA or not and send it back. We will fanout again if
1991 * we have more data in the packet.
1993 * If the packet in error does not have AH/ESP, we handle it
1994 * like any other case.
1996 * The caller must have called icmp_inbound_verify_v4.
1998 static void
1999 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2001 uint16_t *up; /* Pointer to ports in ULP header */
2002 uint32_t ports; /* reversed ports for fanout */
2003 ipha_t ripha; /* With reversed addresses */
2004 ipha_t *ipha; /* Inner IP header */
2005 uint_t hdr_length; /* Inner IP header length */
2006 tcpha_t *tcpha;
2007 conn_t *connp;
2008 ill_t *ill = ira->ira_ill;
2009 ip_stack_t *ipst = ill->ill_ipst;
2010 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2011 ill_t *rill = ira->ira_rill;
2013 /* Caller already pulled up everything. */
2014 ipha = (ipha_t *)&icmph[1];
2015 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2016 ASSERT(mp->b_cont == NULL);
2018 hdr_length = IPH_HDR_LENGTH(ipha);
2019 ira->ira_protocol = ipha->ipha_protocol;
2022 * We need a separate IP header with the source and destination
2023 * addresses reversed to do fanout/classification because the ipha in
2024 * the ICMP error is in the form we sent it out.
2026 ripha.ipha_src = ipha->ipha_dst;
2027 ripha.ipha_dst = ipha->ipha_src;
2028 ripha.ipha_protocol = ipha->ipha_protocol;
2029 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2031 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2032 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2033 ntohl(ipha->ipha_dst),
2034 icmph->icmph_type, icmph->icmph_code));
2036 switch (ipha->ipha_protocol) {
2037 case IPPROTO_UDP:
2038 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2040 /* Attempt to find a client stream based on port. */
2041 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2042 ntohs(up[0]), ntohs(up[1])));
2044 /* Note that we send error to all matches. */
2045 ira->ira_flags |= IRAF_ICMP_ERROR;
2046 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2047 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2048 return;
2050 case IPPROTO_TCP:
2052 * Find a TCP client stream for this packet.
2053 * Note that we do a reverse lookup since the header is
2054 * in the form we sent it out.
2056 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2057 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2058 ipst);
2059 if (connp == NULL)
2060 goto discard_pkt;
2062 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2063 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2064 mp = ipsec_check_inbound_policy(mp, connp,
2065 ipha, NULL, ira);
2066 if (mp == NULL) {
2067 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2068 /* Note that mp is NULL */
2069 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2070 CONN_DEC_REF(connp);
2071 return;
2075 ira->ira_flags |= IRAF_ICMP_ERROR;
2076 ira->ira_ill = ira->ira_rill = NULL;
2077 if (IPCL_IS_TCP(connp)) {
2078 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2079 connp->conn_recvicmp, connp, ira, SQ_FILL,
2080 SQTAG_TCP_INPUT_ICMP_ERR);
2081 } else {
2082 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2083 (connp->conn_recv)(connp, mp, NULL, ira);
2084 CONN_DEC_REF(connp);
2086 ira->ira_ill = ill;
2087 ira->ira_rill = rill;
2088 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2089 return;
2091 case IPPROTO_SCTP:
2092 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2093 /* Find a SCTP client stream for this packet. */
2094 ((uint16_t *)&ports)[0] = up[1];
2095 ((uint16_t *)&ports)[1] = up[0];
2097 ira->ira_flags |= IRAF_ICMP_ERROR;
2098 ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2099 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2100 return;
2102 case IPPROTO_ESP:
2103 case IPPROTO_AH:
2104 if (!ipsec_loaded(ipss)) {
2105 ip_proto_not_sup(mp, ira);
2106 return;
2109 if (ipha->ipha_protocol == IPPROTO_ESP)
2110 mp = ipsecesp_icmp_error(mp, ira);
2111 else
2112 mp = ipsecah_icmp_error(mp, ira);
2113 if (mp == NULL)
2114 return;
2116 /* Just in case ipsec didn't preserve the NULL b_cont */
2117 if (mp->b_cont != NULL) {
2118 if (!pullupmsg(mp, -1))
2119 goto discard_pkt;
2123 * Note that ira_pktlen and ira_ip_hdr_length are no longer
2124 * correct, but we don't use them any more here.
2126 * If succesful, the mp has been modified to not include
2127 * the ESP/AH header so we can fanout to the ULP's icmp
2128 * error handler.
2130 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2131 goto truncated;
2133 /* Verify the modified message before any further processes. */
2134 ipha = (ipha_t *)mp->b_rptr;
2135 hdr_length = IPH_HDR_LENGTH(ipha);
2136 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2137 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2138 freemsg(mp);
2139 return;
2142 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2143 return;
2145 case IPPROTO_ENCAP: {
2146 /* Look for self-encapsulated packets that caused an error */
2147 ipha_t *in_ipha;
2150 * Caller has verified that length has to be
2151 * at least the size of IP header.
2153 ASSERT(hdr_length >= sizeof (ipha_t));
2155 * Check the sanity of the inner IP header like
2156 * we did for the outer header.
2158 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2159 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2160 goto discard_pkt;
2162 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2163 goto discard_pkt;
2165 /* Check for Self-encapsulated tunnels */
2166 if (in_ipha->ipha_src == ipha->ipha_src &&
2167 in_ipha->ipha_dst == ipha->ipha_dst) {
2169 mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2170 in_ipha);
2171 if (mp == NULL)
2172 goto discard_pkt;
2175 * Just in case self_encap didn't preserve the NULL
2176 * b_cont
2178 if (mp->b_cont != NULL) {
2179 if (!pullupmsg(mp, -1))
2180 goto discard_pkt;
2183 * Note that ira_pktlen and ira_ip_hdr_length are no
2184 * longer correct, but we don't use them any more here.
2186 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2187 goto truncated;
2190 * Verify the modified message before any further
2191 * processes.
2193 ipha = (ipha_t *)mp->b_rptr;
2194 hdr_length = IPH_HDR_LENGTH(ipha);
2195 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2196 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2197 freemsg(mp);
2198 return;
2202 * The packet in error is self-encapsualted.
2203 * And we are finding it further encapsulated
2204 * which we could not have possibly generated.
2206 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2207 goto discard_pkt;
2209 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2210 return;
2212 /* No self-encapsulated */
2214 /* FALLTHROUGH */
2215 case IPPROTO_IPV6:
2216 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2217 &ripha.ipha_dst, ipst)) != NULL) {
2218 ira->ira_flags |= IRAF_ICMP_ERROR;
2219 connp->conn_recvicmp(connp, mp, NULL, ira);
2220 CONN_DEC_REF(connp);
2221 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2222 return;
2225 * No IP tunnel is interested, fallthrough and see
2226 * if a raw socket will want it.
2228 /* FALLTHROUGH */
2229 default:
2230 ira->ira_flags |= IRAF_ICMP_ERROR;
2231 ip_fanout_proto_v4(mp, &ripha, ira);
2232 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2233 return;
2235 /* NOTREACHED */
2236 discard_pkt:
2237 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2238 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2239 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2240 freemsg(mp);
2241 return;
2243 truncated:
2244 /* We pulled up everthing already. Must be truncated */
2245 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2246 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2247 freemsg(mp);
2251 * Common IP options parser.
2253 * Setup routine: fill in *optp with options-parsing state, then
2254 * tail-call ipoptp_next to return the first option.
2256 uint8_t
2257 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2259 uint32_t totallen; /* total length of all options */
2261 totallen = ipha->ipha_version_and_hdr_length -
2262 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2263 totallen <<= 2;
2264 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2265 optp->ipoptp_end = optp->ipoptp_next + totallen;
2266 optp->ipoptp_flags = 0;
2267 return (ipoptp_next(optp));
2270 /* Like above but without an ipha_t */
2271 uint8_t
2272 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2274 optp->ipoptp_next = opt;
2275 optp->ipoptp_end = optp->ipoptp_next + totallen;
2276 optp->ipoptp_flags = 0;
2277 return (ipoptp_next(optp));
2281 * Common IP options parser: extract next option.
2283 uint8_t
2284 ipoptp_next(ipoptp_t *optp)
2286 uint8_t *end = optp->ipoptp_end;
2287 uint8_t *cur = optp->ipoptp_next;
2288 uint8_t opt, len, pointer;
2291 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2292 * has been corrupted.
2294 ASSERT(cur <= end);
2296 if (cur == end)
2297 return (IPOPT_EOL);
2299 opt = cur[IPOPT_OPTVAL];
2302 * Skip any NOP options.
2304 while (opt == IPOPT_NOP) {
2305 cur++;
2306 if (cur == end)
2307 return (IPOPT_EOL);
2308 opt = cur[IPOPT_OPTVAL];
2311 if (opt == IPOPT_EOL)
2312 return (IPOPT_EOL);
2315 * Option requiring a length.
2317 if ((cur + 1) >= end) {
2318 optp->ipoptp_flags |= IPOPTP_ERROR;
2319 return (IPOPT_EOL);
2321 len = cur[IPOPT_OLEN];
2322 if (len < 2) {
2323 optp->ipoptp_flags |= IPOPTP_ERROR;
2324 return (IPOPT_EOL);
2326 optp->ipoptp_cur = cur;
2327 optp->ipoptp_len = len;
2328 optp->ipoptp_next = cur + len;
2329 if (cur + len > end) {
2330 optp->ipoptp_flags |= IPOPTP_ERROR;
2331 return (IPOPT_EOL);
2335 * For the options which require a pointer field, make sure
2336 * its there, and make sure it points to either something
2337 * inside this option, or the end of the option.
2339 switch (opt) {
2340 case IPOPT_RR:
2341 case IPOPT_TS:
2342 case IPOPT_LSRR:
2343 case IPOPT_SSRR:
2344 if (len <= IPOPT_OFFSET) {
2345 optp->ipoptp_flags |= IPOPTP_ERROR;
2346 return (opt);
2348 pointer = cur[IPOPT_OFFSET];
2349 if (pointer - 1 > len) {
2350 optp->ipoptp_flags |= IPOPTP_ERROR;
2351 return (opt);
2353 break;
2357 * Sanity check the pointer field based on the type of the
2358 * option.
2360 switch (opt) {
2361 case IPOPT_RR:
2362 case IPOPT_SSRR:
2363 case IPOPT_LSRR:
2364 if (pointer < IPOPT_MINOFF_SR)
2365 optp->ipoptp_flags |= IPOPTP_ERROR;
2366 break;
2367 case IPOPT_TS:
2368 if (pointer < IPOPT_MINOFF_IT)
2369 optp->ipoptp_flags |= IPOPTP_ERROR;
2371 * Note that the Internet Timestamp option also
2372 * contains two four bit fields (the Overflow field,
2373 * and the Flag field), which follow the pointer
2374 * field. We don't need to check that these fields
2375 * fall within the length of the option because this
2376 * was implicitely done above. We've checked that the
2377 * pointer value is at least IPOPT_MINOFF_IT, and that
2378 * it falls within the option. Since IPOPT_MINOFF_IT >
2379 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2381 ASSERT(len > IPOPT_POS_OV_FLG);
2382 break;
2385 return (opt);
2389 * Use the outgoing IP header to create an IP_OPTIONS option the way
2390 * it was passed down from the application.
2392 * This is compatible with BSD in that it returns
2393 * the reverse source route with the final destination
2394 * as the last entry. The first 4 bytes of the option
2395 * will contain the final destination.
2398 ip_opt_get_user(conn_t *connp, uchar_t *buf)
2400 ipoptp_t opts;
2401 uchar_t *opt;
2402 uint8_t optval;
2403 uint8_t optlen;
2404 uint32_t len = 0;
2405 uchar_t *buf1 = buf;
2406 uint32_t totallen;
2407 ipaddr_t dst;
2408 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
2410 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2411 return (0);
2413 totallen = ipp->ipp_ipv4_options_len;
2414 if (totallen & 0x3)
2415 return (0);
2417 buf += IP_ADDR_LEN; /* Leave room for final destination */
2418 len += IP_ADDR_LEN;
2419 bzero(buf1, IP_ADDR_LEN);
2421 dst = connp->conn_faddr_v4;
2423 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2424 optval != IPOPT_EOL;
2425 optval = ipoptp_next(&opts)) {
2426 int off;
2428 opt = opts.ipoptp_cur;
2429 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2430 break;
2432 optlen = opts.ipoptp_len;
2434 switch (optval) {
2435 case IPOPT_SSRR:
2436 case IPOPT_LSRR:
2439 * Insert destination as the first entry in the source
2440 * route and move down the entries on step.
2441 * The last entry gets placed at buf1.
2443 buf[IPOPT_OPTVAL] = optval;
2444 buf[IPOPT_OLEN] = optlen;
2445 buf[IPOPT_OFFSET] = optlen;
2447 off = optlen - IP_ADDR_LEN;
2448 if (off < 0) {
2449 /* No entries in source route */
2450 break;
2452 /* Last entry in source route if not already set */
2453 if (dst == INADDR_ANY)
2454 bcopy(opt + off, buf1, IP_ADDR_LEN);
2455 off -= IP_ADDR_LEN;
2457 while (off > 0) {
2458 bcopy(opt + off,
2459 buf + off + IP_ADDR_LEN,
2460 IP_ADDR_LEN);
2461 off -= IP_ADDR_LEN;
2463 /* ipha_dst into first slot */
2464 bcopy(&dst, buf + off + IP_ADDR_LEN,
2465 IP_ADDR_LEN);
2466 buf += optlen;
2467 len += optlen;
2468 break;
2470 default:
2471 bcopy(opt, buf, optlen);
2472 buf += optlen;
2473 len += optlen;
2474 break;
2477 done:
2478 /* Pad the resulting options */
2479 while (len & 0x3) {
2480 *buf++ = IPOPT_EOL;
2481 len++;
2483 return (len);
2487 * Update any record route or timestamp options to include this host.
2488 * Reverse any source route option.
2489 * This routine assumes that the options are well formed i.e. that they
2490 * have already been checked.
2492 static void
2493 icmp_options_update(ipha_t *ipha)
2495 ipoptp_t opts;
2496 uchar_t *opt;
2497 uint8_t optval;
2498 ipaddr_t src; /* Our local address */
2499 ipaddr_t dst;
2501 ip2dbg(("icmp_options_update\n"));
2502 src = ipha->ipha_src;
2503 dst = ipha->ipha_dst;
2505 for (optval = ipoptp_first(&opts, ipha);
2506 optval != IPOPT_EOL;
2507 optval = ipoptp_next(&opts)) {
2508 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2509 opt = opts.ipoptp_cur;
2510 ip2dbg(("icmp_options_update: opt %d, len %d\n",
2511 optval, opts.ipoptp_len));
2512 switch (optval) {
2513 int off1, off2;
2514 case IPOPT_SSRR:
2515 case IPOPT_LSRR:
2517 * Reverse the source route. The first entry
2518 * should be the next to last one in the current
2519 * source route (the last entry is our address).
2520 * The last entry should be the final destination.
2522 off1 = IPOPT_MINOFF_SR - 1;
2523 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2524 if (off2 < 0) {
2525 /* No entries in source route */
2526 ip1dbg((
2527 "icmp_options_update: bad src route\n"));
2528 break;
2530 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2531 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2532 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2533 off2 -= IP_ADDR_LEN;
2535 while (off1 < off2) {
2536 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2537 bcopy((char *)opt + off2, (char *)opt + off1,
2538 IP_ADDR_LEN);
2539 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2540 off1 += IP_ADDR_LEN;
2541 off2 -= IP_ADDR_LEN;
2543 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2544 break;
2550 * Process received ICMP Redirect messages.
2551 * Assumes the caller has verified that the headers are in the pulled up mblk.
2552 * Consumes mp.
2554 static void
2555 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2557 ire_t *ire, *nire;
2558 ire_t *prev_ire;
2559 ipaddr_t src, dst, gateway;
2560 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2561 ipha_t *inner_ipha; /* Inner IP header */
2563 /* Caller already pulled up everything. */
2564 inner_ipha = (ipha_t *)&icmph[1];
2565 src = ipha->ipha_src;
2566 dst = inner_ipha->ipha_dst;
2567 gateway = icmph->icmph_rd_gateway;
2568 /* Make sure the new gateway is reachable somehow. */
2569 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2570 ALL_ZONES, MATCH_IRE_TYPE, 0, ipst, NULL);
2572 * Make sure we had a route for the dest in question and that
2573 * that route was pointing to the old gateway (the source of the
2574 * redirect packet.)
2575 * We do longest match and then compare ire_gateway_addr below.
2577 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2578 MATCH_IRE_DSTONLY, 0, ipst, NULL);
2580 * Check that
2581 * the redirect was not from ourselves
2582 * the new gateway and the old gateway are directly reachable
2584 if (prev_ire == NULL || ire == NULL ||
2585 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2586 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2587 !(ire->ire_type & IRE_IF_ALL) ||
2588 prev_ire->ire_gateway_addr != src) {
2589 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2590 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2591 freemsg(mp);
2592 if (ire != NULL)
2593 ire_refrele(ire);
2594 if (prev_ire != NULL)
2595 ire_refrele(prev_ire);
2596 return;
2599 ire_refrele(prev_ire);
2600 ire_refrele(ire);
2603 * TODO: more precise handling for cases 0, 2, 3, the latter two
2604 * require TOS routing
2606 switch (icmph->icmph_code) {
2607 case 0:
2608 case 1:
2609 /* TODO: TOS specificity for cases 2 and 3 */
2610 case 2:
2611 case 3:
2612 break;
2613 default:
2614 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2615 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2616 freemsg(mp);
2617 return;
2620 * Create a Route Association. This will allow us to remember that
2621 * someone we believe told us to use the particular gateway.
2623 ire = ire_create(
2624 (uchar_t *)&dst, /* dest addr */
2625 (uchar_t *)&ip_g_all_ones, /* mask */
2626 (uchar_t *)&gateway, /* gateway addr */
2627 IRE_HOST,
2628 NULL, /* ill */
2629 ALL_ZONES,
2630 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2631 ipst);
2633 if (ire == NULL) {
2634 freemsg(mp);
2635 return;
2637 nire = ire_add(ire);
2638 /* Check if it was a duplicate entry */
2639 if (nire != NULL && nire != ire) {
2640 ASSERT(nire->ire_identical_ref > 1);
2641 ire_delete(nire);
2642 ire_refrele(nire);
2643 nire = NULL;
2645 ire = nire;
2646 if (ire != NULL) {
2647 ire_refrele(ire); /* Held in ire_add */
2649 /* tell routing sockets that we received a redirect */
2650 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2651 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2652 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2656 * Delete any existing IRE_HOST type redirect ires for this destination.
2657 * This together with the added IRE has the effect of
2658 * modifying an existing redirect.
2660 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2661 ALL_ZONES, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2662 if (prev_ire != NULL) {
2663 if (prev_ire ->ire_flags & RTF_DYNAMIC)
2664 ire_delete(prev_ire);
2665 ire_refrele(prev_ire);
2668 freemsg(mp);
2672 * Generate an ICMP parameter problem message.
2673 * When called from ip_output side a minimal ip_recv_attr_t needs to be
2674 * constructed by the caller.
2676 static void
2677 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2679 icmph_t icmph;
2680 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2682 mp = icmp_pkt_err_ok(mp, ira);
2683 if (mp == NULL)
2684 return;
2686 bzero(&icmph, sizeof (icmph_t));
2687 icmph.icmph_type = ICMP_PARAM_PROBLEM;
2688 icmph.icmph_pp_ptr = ptr;
2689 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2690 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2694 * Build and ship an IPv4 ICMP message using the packet data in mp, and
2695 * the ICMP header pointed to by "stuff". (May be called as writer.)
2696 * Note: assumes that icmp_pkt_err_ok has been called to verify that
2697 * an icmp error packet can be sent.
2698 * Assigns an appropriate source address to the packet. If ipha_dst is
2699 * one of our addresses use it for source. Otherwise let ip_output_simple
2700 * pick the source address.
2702 static void
2703 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2705 ipaddr_t dst;
2706 icmph_t *icmph;
2707 ipha_t *ipha;
2708 uint_t len_needed;
2709 size_t msg_len;
2710 mblk_t *mp1;
2711 ipaddr_t src;
2712 ire_t *ire;
2713 ip_xmit_attr_t ixas;
2714 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2716 ipha = (ipha_t *)mp->b_rptr;
2718 bzero(&ixas, sizeof (ixas));
2719 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2720 ixas.ixa_zoneid = ira->ira_zoneid;
2721 ixas.ixa_ifindex = 0;
2722 ixas.ixa_ipst = ipst;
2723 ixas.ixa_cred = kcred;
2724 ixas.ixa_cpid = NOPID;
2725 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2727 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2729 * Apply IPsec based on how IPsec was applied to
2730 * the packet that had the error.
2732 * If it was an outbound packet that caused the ICMP
2733 * error, then the caller will have setup the IRA
2734 * appropriately.
2736 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2737 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2738 /* Note: mp already consumed and ip_drop_packet done */
2739 return;
2741 } else {
2743 * This is in clear. The icmp message we are building
2744 * here should go out in clear, independent of our policy.
2746 ixas.ixa_flags |= IXAF_NO_IPSEC;
2749 /* Remember our eventual destination */
2750 dst = ipha->ipha_src;
2753 * If the packet was for one of our unicast addresses, make
2754 * sure we respond with that as the source. Otherwise
2755 * have ip_output_simple pick the source address.
2757 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2758 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid,
2759 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2760 if (ire != NULL) {
2761 ire_refrele(ire);
2762 src = ipha->ipha_dst;
2763 } else {
2764 src = INADDR_ANY;
2765 ixas.ixa_flags |= IXAF_SET_SOURCE;
2769 * Check if we can send back more then 8 bytes in addition to
2770 * the IP header. We try to send 64 bytes of data and the internal
2771 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2773 len_needed = IPH_HDR_LENGTH(ipha);
2774 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2775 ipha->ipha_protocol == IPPROTO_IPV6) {
2776 if (!pullupmsg(mp, -1)) {
2777 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2778 ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2779 freemsg(mp);
2780 return;
2782 ipha = (ipha_t *)mp->b_rptr;
2784 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2785 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2786 len_needed));
2787 } else {
2788 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2790 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2791 len_needed += ip_hdr_length_v6(mp, ip6h);
2794 len_needed += ipst->ips_ip_icmp_return;
2795 msg_len = msgdsize(mp);
2796 if (msg_len > len_needed) {
2797 (void) adjmsg(mp, len_needed - msg_len);
2798 msg_len = len_needed;
2800 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2801 if (mp1 == NULL) {
2802 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2803 freemsg(mp);
2804 return;
2806 mp1->b_cont = mp;
2807 mp = mp1;
2810 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2811 * node generates be accepted in peace by all on-host destinations.
2812 * If we do NOT assume that all on-host destinations trust
2813 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2814 * (Look for IXAF_TRUSTED_ICMP).
2816 ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2818 ipha = (ipha_t *)mp->b_rptr;
2819 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2820 *ipha = icmp_ipha;
2821 ipha->ipha_src = src;
2822 ipha->ipha_dst = dst;
2823 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2824 msg_len += sizeof (icmp_ipha) + len;
2825 if (msg_len > IP_MAXPACKET) {
2826 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2827 msg_len = IP_MAXPACKET;
2829 ipha->ipha_length = htons((uint16_t)msg_len);
2830 icmph = (icmph_t *)&ipha[1];
2831 bcopy(stuff, icmph, len);
2832 icmph->icmph_checksum = 0;
2833 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2834 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2836 (void) ip_output_simple(mp, &ixas);
2837 ixa_cleanup(&ixas);
2841 * Determine if an ICMP error packet can be sent given the rate limit.
2842 * The limit consists of an average frequency (icmp_pkt_err_interval measured
2843 * in milliseconds) and a burst size. Burst size number of packets can
2844 * be sent arbitrarely closely spaced.
2845 * The state is tracked using two variables to implement an approximate
2846 * token bucket filter:
2847 * icmp_pkt_err_last - lbolt value when the last burst started
2848 * icmp_pkt_err_sent - number of packets sent in current burst
2850 boolean_t
2851 icmp_err_rate_limit(ip_stack_t *ipst)
2853 clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2854 uint_t refilled; /* Number of packets refilled in tbf since last */
2855 /* Guard against changes by loading into local variable */
2856 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2858 if (err_interval == 0)
2859 return (B_FALSE);
2861 if (ipst->ips_icmp_pkt_err_last > now) {
2862 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2863 ipst->ips_icmp_pkt_err_last = 0;
2864 ipst->ips_icmp_pkt_err_sent = 0;
2867 * If we are in a burst update the token bucket filter.
2868 * Update the "last" time to be close to "now" but make sure
2869 * we don't loose precision.
2871 if (ipst->ips_icmp_pkt_err_sent != 0) {
2872 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2873 if (refilled > ipst->ips_icmp_pkt_err_sent) {
2874 ipst->ips_icmp_pkt_err_sent = 0;
2875 } else {
2876 ipst->ips_icmp_pkt_err_sent -= refilled;
2877 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2880 if (ipst->ips_icmp_pkt_err_sent == 0) {
2881 /* Start of new burst */
2882 ipst->ips_icmp_pkt_err_last = now;
2884 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2885 ipst->ips_icmp_pkt_err_sent++;
2886 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2887 ipst->ips_icmp_pkt_err_sent));
2888 return (B_FALSE);
2890 ip1dbg(("icmp_err_rate_limit: dropped\n"));
2891 return (B_TRUE);
2895 * Check if it is ok to send an IPv4 ICMP error packet in
2896 * response to the IPv4 packet in mp.
2897 * Free the message and return null if no
2898 * ICMP error packet should be sent.
2900 static mblk_t *
2901 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2903 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2904 icmph_t *icmph;
2905 ipha_t *ipha;
2906 uint_t len_needed;
2908 if (!mp)
2909 return (NULL);
2910 ipha = (ipha_t *)mp->b_rptr;
2911 if (ip_csum_hdr(ipha)) {
2912 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2913 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2914 freemsg(mp);
2915 return (NULL);
2917 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2918 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2919 CLASSD(ipha->ipha_dst) ||
2920 CLASSD(ipha->ipha_src) ||
2921 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2922 /* Note: only errors to the fragment with offset 0 */
2923 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2924 freemsg(mp);
2925 return (NULL);
2927 if (ipha->ipha_protocol == IPPROTO_ICMP) {
2929 * Check the ICMP type. RFC 1122 sez: don't send ICMP
2930 * errors in response to any ICMP errors.
2932 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
2933 if (mp->b_wptr - mp->b_rptr < len_needed) {
2934 if (!pullupmsg(mp, len_needed)) {
2935 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
2936 freemsg(mp);
2937 return (NULL);
2939 ipha = (ipha_t *)mp->b_rptr;
2941 icmph = (icmph_t *)
2942 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
2943 switch (icmph->icmph_type) {
2944 case ICMP_DEST_UNREACHABLE:
2945 case ICMP_SOURCE_QUENCH:
2946 case ICMP_TIME_EXCEEDED:
2947 case ICMP_PARAM_PROBLEM:
2948 case ICMP_REDIRECT:
2949 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2950 freemsg(mp);
2951 return (NULL);
2952 default:
2953 break;
2956 if (icmp_err_rate_limit(ipst)) {
2958 * Only send ICMP error packets every so often.
2959 * This should be done on a per port/source basis,
2960 * but for now this will suffice.
2962 freemsg(mp);
2963 return (NULL);
2965 return (mp);
2969 * Called when a packet was sent out the same link that it arrived on.
2970 * Check if it is ok to send a redirect and then send it.
2972 void
2973 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
2974 ip_recv_attr_t *ira)
2976 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2977 ipaddr_t src, nhop;
2978 mblk_t *mp1;
2979 ire_t *nhop_ire;
2982 * Check the source address to see if it originated
2983 * on the same logical subnet it is going back out on.
2984 * If so, we should be able to send it a redirect.
2985 * Avoid sending a redirect if the destination
2986 * is directly connected (i.e., we matched an IRE_ONLINK),
2987 * or if the packet was source routed out this interface.
2989 * We avoid sending a redirect if the
2990 * destination is directly connected
2991 * because it is possible that multiple
2992 * IP subnets may have been configured on
2993 * the link, and the source may not
2994 * be on the same subnet as ip destination,
2995 * even though they are on the same
2996 * physical link.
2998 if ((ire->ire_type & IRE_ONLINK) ||
2999 ip_source_routed(ipha, ipst))
3000 return;
3002 nhop_ire = ire_nexthop(ire);
3003 if (nhop_ire == NULL)
3004 return;
3006 nhop = nhop_ire->ire_addr;
3008 if (nhop_ire->ire_type & IRE_IF_CLONE) {
3009 ire_t *ire2;
3011 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3012 mutex_enter(&nhop_ire->ire_lock);
3013 ire2 = nhop_ire->ire_dep_parent;
3014 if (ire2 != NULL)
3015 ire_refhold(ire2);
3016 mutex_exit(&nhop_ire->ire_lock);
3017 ire_refrele(nhop_ire);
3018 nhop_ire = ire2;
3020 if (nhop_ire == NULL)
3021 return;
3023 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3025 src = ipha->ipha_src;
3028 * We look at the interface ire for the nexthop,
3029 * to see if ipha_src is in the same subnet
3030 * as the nexthop.
3032 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3034 * The source is directly connected.
3036 mp1 = copymsg(mp);
3037 if (mp1 != NULL) {
3038 icmp_send_redirect(mp1, nhop, ira);
3041 ire_refrele(nhop_ire);
3045 * Generate an ICMP redirect message.
3047 static void
3048 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3050 icmph_t icmph;
3051 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3053 mp = icmp_pkt_err_ok(mp, ira);
3054 if (mp == NULL)
3055 return;
3057 bzero(&icmph, sizeof (icmph_t));
3058 icmph.icmph_type = ICMP_REDIRECT;
3059 icmph.icmph_code = 1;
3060 icmph.icmph_rd_gateway = gateway;
3061 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3062 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3066 * Generate an ICMP time exceeded message.
3068 void
3069 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3071 icmph_t icmph;
3072 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3074 mp = icmp_pkt_err_ok(mp, ira);
3075 if (mp == NULL)
3076 return;
3078 bzero(&icmph, sizeof (icmph_t));
3079 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3080 icmph.icmph_code = code;
3081 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3082 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3086 * Generate an ICMP unreachable message.
3087 * When called from ip_output side a minimal ip_recv_attr_t needs to be
3088 * constructed by the caller.
3090 void
3091 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3093 icmph_t icmph;
3094 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3096 mp = icmp_pkt_err_ok(mp, ira);
3097 if (mp == NULL)
3098 return;
3100 bzero(&icmph, sizeof (icmph_t));
3101 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3102 icmph.icmph_code = code;
3103 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3104 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3108 * Latch in the IPsec state for a stream based the policy in the listener
3109 * and the actions in the ip_recv_attr_t.
3110 * Called directly from TCP and SCTP.
3112 boolean_t
3113 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3115 ASSERT(lconnp->conn_policy != NULL);
3116 ASSERT(connp->conn_policy == NULL);
3118 IPPH_REFHOLD(lconnp->conn_policy);
3119 connp->conn_policy = lconnp->conn_policy;
3121 if (ira->ira_ipsec_action != NULL) {
3122 if (connp->conn_latch == NULL) {
3123 connp->conn_latch = iplatch_create();
3124 if (connp->conn_latch == NULL)
3125 return (B_FALSE);
3127 ipsec_latch_inbound(connp, ira);
3129 return (B_TRUE);
3133 * Verify whether or not the IP address is a valid local address.
3134 * Could be a unicast, including one for a down interface.
3135 * If allow_mcbc then a multicast or broadcast address is also
3136 * acceptable.
3138 * In the case of a broadcast/multicast address, however, the
3139 * upper protocol is expected to reset the src address
3140 * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3141 * no packets are emitted with broadcast/multicast address as
3142 * source address (that violates hosts requirements RFC 1122)
3143 * The addresses valid for bind are:
3144 * (1) - INADDR_ANY (0)
3145 * (2) - IP address of an UP interface
3146 * (3) - IP address of a DOWN interface
3147 * (4) - valid local IP broadcast addresses. In this case
3148 * the conn will only receive packets destined to
3149 * the specified broadcast address.
3150 * (5) - a multicast address. In this case
3151 * the conn will only receive packets destined to
3152 * the specified multicast address. Note: the
3153 * application still has to issue an
3154 * IP_ADD_MEMBERSHIP socket option.
3156 * In all the above cases, the bound address must be valid in the current zone.
3157 * When the address is loopback, multicast or broadcast, there might be many
3158 * matching IREs so bind has to look up based on the zone.
3160 ip_laddr_t
3161 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3162 ip_stack_t *ipst, boolean_t allow_mcbc)
3164 ire_t *src_ire;
3166 ASSERT(src_addr != INADDR_ANY);
3168 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3169 NULL, zoneid, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3172 * If an address other than in6addr_any is requested,
3173 * we verify that it is a valid address for bind
3174 * Note: Following code is in if-else-if form for
3175 * readability compared to a condition check.
3177 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3179 * (2) Bind to address of local UP interface
3181 ire_refrele(src_ire);
3182 return (IPVL_UNICAST_UP);
3183 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3185 * (4) Bind to broadcast address
3187 ire_refrele(src_ire);
3188 if (allow_mcbc)
3189 return (IPVL_BCAST);
3190 else
3191 return (IPVL_BAD);
3192 } else if (CLASSD(src_addr)) {
3193 /* (5) bind to multicast address. */
3194 if (src_ire != NULL)
3195 ire_refrele(src_ire);
3197 if (allow_mcbc)
3198 return (IPVL_MCAST);
3199 else
3200 return (IPVL_BAD);
3201 } else {
3202 ipif_t *ipif;
3205 * (3) Bind to address of local DOWN interface?
3206 * (ipif_lookup_addr() looks up all interfaces
3207 * but we do not get here for UP interfaces
3208 * - case (2) above)
3210 if (src_ire != NULL)
3211 ire_refrele(src_ire);
3213 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3214 if (ipif == NULL)
3215 return (IPVL_BAD);
3217 /* Not a useful source? */
3218 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3219 ipif_refrele(ipif);
3220 return (IPVL_BAD);
3222 ipif_refrele(ipif);
3223 return (IPVL_UNICAST_DOWN);
3228 * Insert in the bind fanout for IPv4 and IPv6.
3229 * The caller should already have used ip_laddr_verify_v*() before calling
3230 * this.
3233 ip_laddr_fanout_insert(conn_t *connp)
3235 int error;
3238 * Allow setting new policies. For example, disconnects result
3239 * in us being called. As we would have set conn_policy_cached
3240 * to B_TRUE before, we should set it to B_FALSE, so that policy
3241 * can change after the disconnect.
3243 connp->conn_policy_cached = B_FALSE;
3245 return (ipcl_bind_insert(connp));
3249 * Verify that both the source and destination addresses are valid. If
3250 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3251 * i.e. have no route to it. Protocols like TCP want to verify destination
3252 * reachability, while tunnels do not.
3254 * Determine the route, the interface, and (optionally) the source address
3255 * to use to reach a given destination.
3256 * Note that we allow connect to broadcast and multicast addresses when
3257 * IPDF_ALLOW_MCBC is set.
3258 * first_hop and dst_addr are normally the same, but if source routing
3259 * they will differ; in that case the first_hop is what we'll use for the
3260 * routing lookup but the dce checks will be done on dst_addr,
3262 * If uinfo is set, then we fill in the best available information
3263 * we have for the destination. This is based on (in priority order) any
3264 * metrics and path MTU stored in a dce_t, route metrics, and finally the
3265 * ill_mtu/ill_mc_mtu.
3268 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3269 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags)
3271 ire_t *ire = NULL;
3272 int error = 0;
3273 ipaddr_t setsrc; /* RTF_SETSRC */
3274 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */
3275 ip_stack_t *ipst = ixa->ixa_ipst;
3276 dce_t *dce;
3277 uint_t pmtu;
3278 uint_t generation;
3279 nce_t *nce;
3280 ill_t *ill = NULL;
3282 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3285 * We never send to zero; the ULPs map it to the loopback address.
3286 * We can't allow it since we use zero to mean unitialized in some
3287 * places.
3289 ASSERT(dst_addr != INADDR_ANY);
3291 setsrc = INADDR_ANY;
3293 * Select a route; For IPMP interfaces, we would only select
3294 * a "hidden" route (i.e., going through a specific under_ill)
3295 * if ixa_ifindex has been specified.
3297 ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3298 &generation, &setsrc, &error);
3299 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */
3300 if (error != 0)
3301 goto bad_addr;
3304 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3305 * If IPDF_VERIFY_DST is set, the destination must be reachable;
3306 * Otherwise the destination needn't be reachable.
3308 * If we match on a reject or black hole, then we've got a
3309 * local failure. May as well fail out the connect() attempt,
3310 * since it's never going to succeed.
3312 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3314 * If we're verifying destination reachability, we always want
3315 * to complain here.
3317 * If we're not verifying destination reachability but the
3318 * destination has a route, we still want to fail on the
3319 * temporary address and broadcast address tests.
3321 * In both cases do we let the code continue so some reasonable
3322 * information is returned to the caller. That enables the
3323 * caller to use (and even cache) the IRE. conn_ip_ouput will
3324 * use the generation mismatch path to check for the unreachable
3325 * case thereby avoiding any specific check in the main path.
3327 ASSERT(generation == IRE_GENERATION_VERIFY);
3328 if (flags & IPDF_VERIFY_DST) {
3330 * Set errno but continue to set up ixa_ire to be
3331 * the RTF_REJECT|RTF_BLACKHOLE IRE.
3332 * That allows callers to use ip_output to get an
3333 * ICMP error back.
3335 if (!(ire->ire_type & IRE_HOST))
3336 error = ENETUNREACH;
3337 else
3338 error = EHOSTUNREACH;
3342 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3343 !(flags & IPDF_ALLOW_MCBC)) {
3344 ire_refrele(ire);
3345 ire = ire_reject(ipst, B_FALSE);
3346 generation = IRE_GENERATION_VERIFY;
3347 error = ENETUNREACH;
3350 /* Cache things */
3351 if (ixa->ixa_ire != NULL)
3352 ire_refrele_notr(ixa->ixa_ire);
3353 #ifdef DEBUG
3354 ire_refhold_notr(ire);
3355 ire_refrele(ire);
3356 #endif
3357 ixa->ixa_ire = ire;
3358 ixa->ixa_ire_generation = generation;
3361 * Ensure that ixa_dce is always set any time that ixa_ire is set,
3362 * since some callers will send a packet to conn_ip_output() even if
3363 * there's an error.
3365 if (flags & IPDF_UNIQUE_DCE) {
3366 /* Fallback to the default dce if allocation fails */
3367 dce = dce_lookup_and_add_v4(dst_addr, ipst);
3368 if (dce != NULL)
3369 generation = dce->dce_generation;
3370 else
3371 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3372 } else {
3373 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3375 ASSERT(dce != NULL);
3376 if (ixa->ixa_dce != NULL)
3377 dce_refrele_notr(ixa->ixa_dce);
3378 #ifdef DEBUG
3379 dce_refhold_notr(dce);
3380 dce_refrele(dce);
3381 #endif
3382 ixa->ixa_dce = dce;
3383 ixa->ixa_dce_generation = generation;
3384 ixa->ixa_postfragfn = ire->ire_postfragfn;
3386 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3387 /* Get an nce to cache. */
3388 nce = ire_to_nce(ire, firsthop, NULL);
3389 if (nce == NULL) {
3390 /* Allocation failure? */
3391 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3392 } else {
3393 if (ixa->ixa_nce != NULL)
3394 nce_refrele(ixa->ixa_nce);
3395 ixa->ixa_nce = nce;
3400 * If the source address is a loopback address, the
3401 * destination had best be local or multicast.
3402 * If we are sending to an IRE_LOCAL using a loopback source then
3403 * it had better be the same zoneid.
3405 if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3406 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3407 ire = NULL; /* Stored in ixa_ire */
3408 error = EADDRNOTAVAIL;
3409 goto bad_addr;
3411 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3412 ire = NULL; /* Stored in ixa_ire */
3413 error = EADDRNOTAVAIL;
3414 goto bad_addr;
3417 if (ire->ire_type & IRE_BROADCAST) {
3419 * If the ULP didn't have a specified source, then we
3420 * make sure we reselect the source when sending
3421 * broadcasts out different interfaces.
3423 if (flags & IPDF_SELECT_SRC)
3424 ixa->ixa_flags |= IXAF_SET_SOURCE;
3425 else
3426 ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3430 * Does the caller want us to pick a source address?
3432 if (flags & IPDF_SELECT_SRC) {
3433 ipaddr_t src_addr;
3436 * We use use ire_nexthop_ill to avoid the under ipmp
3437 * interface for source address selection. Note that for ipmp
3438 * probe packets, ixa_ifindex would have been specified, and
3439 * the ip_select_route() invocation would have picked an ire
3440 * will ire_ill pointing at an under interface.
3442 ill = ire_nexthop_ill(ire);
3444 /* If unreachable we have no ill but need some source */
3445 if (ill == NULL) {
3446 src_addr = htonl(INADDR_LOOPBACK);
3447 /* Make sure we look for a better source address */
3448 generation = SRC_GENERATION_VERIFY;
3449 } else {
3450 error = ip_select_source_v4(ill, setsrc, dst_addr,
3451 ixa->ixa_multicast_ifaddr, zoneid,
3452 ipst, &src_addr, &generation, NULL);
3453 if (error != 0) {
3454 ire = NULL; /* Stored in ixa_ire */
3455 goto bad_addr;
3460 * We allow the source address to to down.
3461 * However, we check that we don't use the loopback address
3462 * as a source when sending out on the wire.
3464 if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3465 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3466 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3467 ire = NULL; /* Stored in ixa_ire */
3468 error = EADDRNOTAVAIL;
3469 goto bad_addr;
3472 *src_addrp = src_addr;
3473 ixa->ixa_src_generation = generation;
3477 * Make sure we don't leave an unreachable ixa_nce in place
3478 * since ip_select_route is used when we unplumb i.e., remove
3479 * references on ixa_ire, ixa_nce, and ixa_dce.
3481 nce = ixa->ixa_nce;
3482 if (nce != NULL && nce->nce_is_condemned) {
3483 nce_refrele(nce);
3484 ixa->ixa_nce = NULL;
3485 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3489 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3490 * However, we can't do it for IPv4 multicast or broadcast.
3492 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3493 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3496 * Set initial value for fragmentation limit. Either conn_ip_output
3497 * or ULP might updates it when there are routing changes.
3498 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3500 pmtu = ip_get_pmtu(ixa);
3501 ixa->ixa_fragsize = pmtu;
3502 /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3503 if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3504 ixa->ixa_pmtu = pmtu;
3507 * Extract information useful for some transports.
3508 * First we look for DCE metrics. Then we take what we have in
3509 * the metrics in the route, where the offlink is used if we have
3510 * one.
3512 if (uinfo != NULL) {
3513 bzero(uinfo, sizeof (*uinfo));
3515 if (dce->dce_flags & DCEF_UINFO)
3516 *uinfo = dce->dce_uinfo;
3518 rts_merge_metrics(uinfo, &ire->ire_metrics);
3520 /* Allow ire_metrics to decrease the path MTU from above */
3521 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3522 uinfo->iulp_mtu = pmtu;
3524 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3525 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3526 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3529 if (ill != NULL)
3530 ill_refrele(ill);
3532 return (error);
3534 bad_addr:
3535 if (ire != NULL)
3536 ire_refrele(ire);
3538 if (ill != NULL)
3539 ill_refrele(ill);
3542 * Make sure we don't leave an unreachable ixa_nce in place
3543 * since ip_select_route is used when we unplumb i.e., remove
3544 * references on ixa_ire, ixa_nce, and ixa_dce.
3546 nce = ixa->ixa_nce;
3547 if (nce != NULL && nce->nce_is_condemned) {
3548 nce_refrele(nce);
3549 ixa->ixa_nce = NULL;
3550 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3553 return (error);
3558 * Get the base MTU for the case when path MTU discovery is not used.
3559 * Takes the MTU of the IRE into account.
3561 uint_t
3562 ip_get_base_mtu(ill_t *ill, ire_t *ire)
3564 uint_t mtu;
3565 uint_t iremtu = ire->ire_metrics.iulp_mtu;
3567 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3568 mtu = ill->ill_mc_mtu;
3569 else
3570 mtu = ill->ill_mtu;
3572 if (iremtu != 0 && iremtu < mtu)
3573 mtu = iremtu;
3575 return (mtu);
3579 * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3580 * Assumes that ixa_ire, dce, and nce have already been set up.
3582 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3583 * We avoid path MTU discovery if it is disabled with ndd.
3584 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3586 * NOTE: We also used to turn it off for source routed packets. That
3587 * is no longer required since the dce is per final destination.
3589 uint_t
3590 ip_get_pmtu(ip_xmit_attr_t *ixa)
3592 ip_stack_t *ipst = ixa->ixa_ipst;
3593 dce_t *dce;
3594 nce_t *nce;
3595 ire_t *ire;
3596 uint_t pmtu;
3598 ire = ixa->ixa_ire;
3599 dce = ixa->ixa_dce;
3600 nce = ixa->ixa_nce;
3603 * If path MTU discovery has been turned off by ndd, then we ignore
3604 * any dce_pmtu and for IPv4 we will not set DF.
3606 if (!ipst->ips_ip_path_mtu_discovery)
3607 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3609 pmtu = IP_MAXPACKET;
3611 * Decide whether whether IPv4 sets DF
3612 * For IPv6 "no DF" means to use the 1280 mtu
3614 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3615 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3616 } else {
3617 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3618 if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3619 pmtu = IPV6_MIN_MTU;
3622 /* Check if the PMTU is to old before we use it */
3623 if ((dce->dce_flags & DCEF_PMTU) &&
3624 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3625 ipst->ips_ip_pathmtu_interval) {
3627 * Older than 20 minutes. Drop the path MTU information.
3629 mutex_enter(&dce->dce_lock);
3630 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3631 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3632 mutex_exit(&dce->dce_lock);
3633 dce_increment_generation(dce);
3636 /* The metrics on the route can lower the path MTU */
3637 if (ire->ire_metrics.iulp_mtu != 0 &&
3638 ire->ire_metrics.iulp_mtu < pmtu)
3639 pmtu = ire->ire_metrics.iulp_mtu;
3642 * If the path MTU is smaller than some minimum, we still use dce_pmtu
3643 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3644 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3646 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3647 if (dce->dce_flags & DCEF_PMTU) {
3648 if (dce->dce_pmtu < pmtu)
3649 pmtu = dce->dce_pmtu;
3651 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3652 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3653 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3654 } else {
3655 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3656 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3658 } else {
3659 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3660 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3665 * If we have an IRE_LOCAL we use the loopback mtu instead of
3666 * the ill for going out the wire i.e., IRE_LOCAL gets the same
3667 * mtu as IRE_LOOPBACK.
3669 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3670 uint_t loopback_mtu;
3672 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3673 ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3675 if (loopback_mtu < pmtu)
3676 pmtu = loopback_mtu;
3677 } else if (nce != NULL) {
3679 * Make sure we don't exceed the interface MTU.
3680 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3681 * an ill. We'd use the above IP_MAXPACKET in that case just
3682 * to tell the transport something larger than zero.
3684 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3685 if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3686 pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3687 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3688 nce->nce_ill->ill_mc_mtu < pmtu) {
3690 * for interfaces in an IPMP group, the mtu of
3691 * the nce_ill (under_ill) could be different
3692 * from the mtu of the ncec_ill, so we take the
3693 * min of the two.
3695 pmtu = nce->nce_ill->ill_mc_mtu;
3697 } else {
3698 if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3699 pmtu = nce->nce_common->ncec_ill->ill_mtu;
3700 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3701 nce->nce_ill->ill_mtu < pmtu) {
3703 * for interfaces in an IPMP group, the mtu of
3704 * the nce_ill (under_ill) could be different
3705 * from the mtu of the ncec_ill, so we take the
3706 * min of the two.
3708 pmtu = nce->nce_ill->ill_mtu;
3714 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3715 * Only applies to IPv6.
3717 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3718 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3719 switch (ixa->ixa_use_min_mtu) {
3720 case IPV6_USE_MIN_MTU_MULTICAST:
3721 if (ire->ire_type & IRE_MULTICAST)
3722 pmtu = IPV6_MIN_MTU;
3723 break;
3724 case IPV6_USE_MIN_MTU_ALWAYS:
3725 pmtu = IPV6_MIN_MTU;
3726 break;
3727 case IPV6_USE_MIN_MTU_NEVER:
3728 break;
3730 } else {
3731 /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3732 if (ire->ire_type & IRE_MULTICAST)
3733 pmtu = IPV6_MIN_MTU;
3737 return (pmtu);
3741 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3742 * the final piece where we don't. Return a pointer to the first mblk in the
3743 * result, and update the pointer to the next mblk to chew on. If anything
3744 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3745 * NULL pointer.
3747 mblk_t *
3748 ip_carve_mp(mblk_t **mpp, ssize_t len)
3750 mblk_t *mp0;
3751 mblk_t *mp1;
3752 mblk_t *mp2;
3754 if (!len || !mpp || !(mp0 = *mpp))
3755 return (NULL);
3756 /* If we aren't going to consume the first mblk, we need a dup. */
3757 if (mp0->b_wptr - mp0->b_rptr > len) {
3758 mp1 = dupb(mp0);
3759 if (mp1) {
3760 /* Partition the data between the two mblks. */
3761 mp1->b_wptr = mp1->b_rptr + len;
3762 mp0->b_rptr = mp1->b_wptr;
3764 * after adjustments if mblk not consumed is now
3765 * unaligned, try to align it. If this fails free
3766 * all messages and let upper layer recover.
3768 if (!OK_32PTR(mp0->b_rptr)) {
3769 if (!pullupmsg(mp0, -1)) {
3770 freemsg(mp0);
3771 freemsg(mp1);
3772 *mpp = NULL;
3773 return (NULL);
3777 return (mp1);
3779 /* Eat through as many mblks as we need to get len bytes. */
3780 len -= mp0->b_wptr - mp0->b_rptr;
3781 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3782 if (mp2->b_wptr - mp2->b_rptr > len) {
3784 * We won't consume the entire last mblk. Like
3785 * above, dup and partition it.
3787 mp1->b_cont = dupb(mp2);
3788 mp1 = mp1->b_cont;
3789 if (!mp1) {
3791 * Trouble. Rather than go to a lot of
3792 * trouble to clean up, we free the messages.
3793 * This won't be any worse than losing it on
3794 * the wire.
3796 freemsg(mp0);
3797 freemsg(mp2);
3798 *mpp = NULL;
3799 return (NULL);
3801 mp1->b_wptr = mp1->b_rptr + len;
3802 mp2->b_rptr = mp1->b_wptr;
3804 * after adjustments if mblk not consumed is now
3805 * unaligned, try to align it. If this fails free
3806 * all messages and let upper layer recover.
3808 if (!OK_32PTR(mp2->b_rptr)) {
3809 if (!pullupmsg(mp2, -1)) {
3810 freemsg(mp0);
3811 freemsg(mp2);
3812 *mpp = NULL;
3813 return (NULL);
3816 *mpp = mp2;
3817 return (mp0);
3819 /* Decrement len by the amount we just got. */
3820 len -= mp2->b_wptr - mp2->b_rptr;
3823 * len should be reduced to zero now. If not our caller has
3824 * screwed up.
3826 if (len) {
3827 /* Shouldn't happen! */
3828 freemsg(mp0);
3829 *mpp = NULL;
3830 return (NULL);
3833 * We consumed up to exactly the end of an mblk. Detach the part
3834 * we are returning from the rest of the chain.
3836 mp1->b_cont = NULL;
3837 *mpp = mp2;
3838 return (mp0);
3841 /* The ill stream is being unplumbed. Called from ip_close */
3843 ip_modclose(ill_t *ill)
3845 boolean_t success;
3846 ipsq_t *ipsq;
3847 ipif_t *ipif;
3848 queue_t *q = ill->ill_rq;
3849 ip_stack_t *ipst = ill->ill_ipst;
3850 int i;
3851 arl_ill_common_t *ai = ill->ill_common;
3854 * The punlink prior to this may have initiated a capability
3855 * negotiation. But ipsq_enter will block until that finishes or
3856 * times out.
3858 success = ipsq_enter(ill, B_FALSE, NEW_OP);
3861 * Open/close/push/pop is guaranteed to be single threaded
3862 * per stream by STREAMS. FS guarantees that all references
3863 * from top are gone before close is called. So there can't
3864 * be another close thread that has set CONDEMNED on this ill.
3865 * and cause ipsq_enter to return failure.
3867 ASSERT(success);
3868 ipsq = ill->ill_phyint->phyint_ipsq;
3871 * Mark it condemned. No new reference will be made to this ill.
3872 * Lookup functions will return an error. Threads that try to
3873 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
3874 * that the refcnt will drop down to zero.
3876 mutex_enter(&ill->ill_lock);
3877 ill->ill_state_flags |= ILL_CONDEMNED;
3878 for (ipif = ill->ill_ipif; ipif != NULL;
3879 ipif = ipif->ipif_next) {
3880 ipif->ipif_state_flags |= IPIF_CONDEMNED;
3883 * Wake up anybody waiting to enter the ipsq. ipsq_enter
3884 * returns error if ILL_CONDEMNED is set
3886 cv_broadcast(&ill->ill_cv);
3887 mutex_exit(&ill->ill_lock);
3890 * Send all the deferred DLPI messages downstream which came in
3891 * during the small window right before ipsq_enter(). We do this
3892 * without waiting for the ACKs because all the ACKs for M_PROTO
3893 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
3895 ill_dlpi_send_deferred(ill);
3898 * Shut down fragmentation reassembly.
3899 * ill_frag_timer won't start a timer again.
3900 * Now cancel any existing timer
3902 (void) untimeout(ill->ill_frag_timer_id);
3903 (void) ill_frag_timeout(ill, 0);
3906 * Call ill_delete to bring down the ipifs, ilms and ill on
3907 * this ill. Then wait for the refcnts to drop to zero.
3908 * ill_is_freeable checks whether the ill is really quiescent.
3909 * Then make sure that threads that are waiting to enter the
3910 * ipsq have seen the error returned by ipsq_enter and have
3911 * gone away. Then we call ill_delete_tail which does the
3912 * DL_UNBIND_REQ with the driver and then qprocsoff.
3914 ill_delete(ill);
3915 mutex_enter(&ill->ill_lock);
3916 while (!ill_is_freeable(ill))
3917 cv_wait(&ill->ill_cv, &ill->ill_lock);
3919 while (ill->ill_waiters)
3920 cv_wait(&ill->ill_cv, &ill->ill_lock);
3922 mutex_exit(&ill->ill_lock);
3925 * ill_delete_tail drops reference on ill_ipst, but we need to keep
3926 * it held until the end of the function since the cleanup
3927 * below needs to be able to use the ip_stack_t.
3929 netstack_hold(ipst->ips_netstack);
3931 /* qprocsoff is done via ill_delete_tail */
3932 ill_delete_tail(ill);
3934 * synchronously wait for arp stream to unbind. After this, we
3935 * cannot get any data packets up from the driver.
3937 arp_unbind_complete(ill);
3938 ASSERT(ill->ill_ipst == NULL);
3941 * Walk through all conns and qenable those that have queued data.
3942 * Close synchronization needs this to
3943 * be done to ensure that all upper layers blocked
3944 * due to flow control to the closing device
3945 * get unblocked.
3947 ip1dbg(("ip_wsrv: walking\n"));
3948 for (i = 0; i < TX_FANOUT_SIZE; i++) {
3949 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
3953 * ai can be null if this is an IPv6 ill, or if the IPv4
3954 * stream is being torn down before ARP was plumbed (e.g.,
3955 * /sbin/ifconfig plumbing a stream twice, and encountering
3956 * an error
3958 if (ai != NULL) {
3959 ASSERT(!ill->ill_isv6);
3960 mutex_enter(&ai->ai_lock);
3961 ai->ai_ill = NULL;
3962 if (ai->ai_arl == NULL) {
3963 mutex_destroy(&ai->ai_lock);
3964 kmem_free(ai, sizeof (*ai));
3965 } else {
3966 cv_signal(&ai->ai_ill_unplumb_done);
3967 mutex_exit(&ai->ai_lock);
3971 mutex_enter(&ipst->ips_ip_mi_lock);
3972 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
3973 mutex_exit(&ipst->ips_ip_mi_lock);
3976 * credp could be null if the open didn't succeed and ip_modopen
3977 * itself calls ip_close.
3979 if (ill->ill_credp != NULL)
3980 crfree(ill->ill_credp);
3982 mutex_destroy(&ill->ill_saved_ire_lock);
3983 mutex_destroy(&ill->ill_lock);
3984 rw_destroy(&ill->ill_mcast_lock);
3985 mutex_destroy(&ill->ill_mcast_serializer);
3986 list_destroy(&ill->ill_nce);
3989 * Now we are done with the module close pieces that
3990 * need the netstack_t.
3992 netstack_rele(ipst->ips_netstack);
3994 mi_close_free((IDP)ill);
3995 q->q_ptr = WR(q)->q_ptr = NULL;
3997 ipsq_exit(ipsq);
3999 return (0);
4003 * This is called as part of close() for IP, UDP, ICMP, and RTS
4004 * in order to quiesce the conn.
4006 void
4007 ip_quiesce_conn(conn_t *connp)
4009 boolean_t drain_cleanup_reqd = B_FALSE;
4010 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
4011 boolean_t ilg_cleanup_reqd = B_FALSE;
4012 ip_stack_t *ipst;
4014 ASSERT(!IPCL_IS_TCP(connp));
4015 ipst = connp->conn_netstack->netstack_ip;
4018 * Mark the conn as closing, and this conn must not be
4019 * inserted in future into any list. Eg. conn_drain_insert(),
4020 * won't insert this conn into the conn_drain_list.
4022 * conn_idl, and conn_ilg cannot get set henceforth.
4024 mutex_enter(&connp->conn_lock);
4025 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4026 connp->conn_state_flags |= CONN_CLOSING;
4027 if (connp->conn_idl != NULL)
4028 drain_cleanup_reqd = B_TRUE;
4029 if (connp->conn_oper_pending_ill != NULL)
4030 conn_ioctl_cleanup_reqd = B_TRUE;
4031 if (connp->conn_dhcpinit_ill != NULL) {
4032 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4033 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4034 ill_set_inputfn(connp->conn_dhcpinit_ill);
4035 connp->conn_dhcpinit_ill = NULL;
4037 if (connp->conn_ilg != NULL)
4038 ilg_cleanup_reqd = B_TRUE;
4039 mutex_exit(&connp->conn_lock);
4041 if (conn_ioctl_cleanup_reqd)
4042 conn_ioctl_cleanup(connp);
4045 * Remove this conn from any fanout list it is on.
4046 * and then wait for any threads currently operating
4047 * on this endpoint to finish
4049 ipcl_hash_remove(connp);
4052 * Remove this conn from the drain list, and do any other cleanup that
4053 * may be required. (TCP conns are never flow controlled, and
4054 * conn_idl will be NULL.)
4056 if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4057 idl_t *idl = connp->conn_idl;
4059 mutex_enter(&idl->idl_lock);
4060 conn_drain(connp, B_TRUE);
4061 mutex_exit(&idl->idl_lock);
4064 if (connp == ipst->ips_ip_g_mrouter)
4065 (void) ip_mrouter_done(ipst);
4067 if (ilg_cleanup_reqd)
4068 ilg_delete_all(connp);
4071 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4072 * callers from write side can't be there now because close
4073 * is in progress. The only other caller is ipcl_walk
4074 * which checks for the condemned flag.
4076 mutex_enter(&connp->conn_lock);
4077 connp->conn_state_flags |= CONN_CONDEMNED;
4078 while (connp->conn_ref != 1)
4079 cv_wait(&connp->conn_cv, &connp->conn_lock);
4080 connp->conn_state_flags |= CONN_QUIESCED;
4081 mutex_exit(&connp->conn_lock);
4084 /* ARGSUSED */
4086 ip_close(queue_t *q, int flags, cred_t *credp __unused)
4088 conn_t *connp;
4091 * Call the appropriate delete routine depending on whether this is
4092 * a module or device.
4094 if (WR(q)->q_next != NULL) {
4095 /* This is a module close */
4096 return (ip_modclose((ill_t *)q->q_ptr));
4099 connp = q->q_ptr;
4100 ip_quiesce_conn(connp);
4102 qprocsoff(q);
4105 * Now we are truly single threaded on this stream, and can
4106 * delete the things hanging off the connp, and finally the connp.
4107 * We removed this connp from the fanout list, it cannot be
4108 * accessed thru the fanouts, and we already waited for the
4109 * conn_ref to drop to 0. We are already in close, so
4110 * there cannot be any other thread from the top. qprocsoff
4111 * has completed, and service has completed or won't run in
4112 * future.
4114 ASSERT(connp->conn_ref == 1);
4116 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4118 connp->conn_ref--;
4119 ipcl_conn_destroy(connp);
4121 q->q_ptr = WR(q)->q_ptr = NULL;
4122 return (0);
4126 * Wapper around putnext() so that ip_rts_request can merely use
4127 * conn_recv.
4129 /*ARGSUSED2*/
4130 static void
4131 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4133 conn_t *connp = (conn_t *)arg1;
4135 putnext(connp->conn_rq, mp);
4138 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4139 /* ARGSUSED */
4140 static void
4141 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4143 freemsg(mp);
4147 * Called when the module is about to be unloaded
4149 void
4150 ip_ddi_destroy(void)
4152 /* This needs to be called before destroying any transports. */
4153 mutex_enter(&cpu_lock);
4154 unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4155 mutex_exit(&cpu_lock);
4157 icmp_ddi_g_destroy();
4158 rts_ddi_g_destroy();
4159 udp_ddi_g_destroy();
4160 sctp_ddi_g_destroy();
4161 tcp_ddi_g_destroy();
4162 ilb_ddi_g_destroy();
4163 dce_g_destroy();
4164 ipsec_policy_g_destroy();
4165 ipcl_g_destroy();
4166 ip_net_g_destroy();
4167 ip_ire_g_fini();
4168 inet_minor_destroy(ip_minor_arena_sa);
4169 #if defined(_LP64)
4170 inet_minor_destroy(ip_minor_arena_la);
4171 #endif
4173 #ifdef DEBUG
4174 list_destroy(&ip_thread_list);
4175 rw_destroy(&ip_thread_rwlock);
4176 tsd_destroy(&ip_thread_data);
4177 #endif
4179 netstack_unregister(NS_IP);
4183 * First step in cleanup.
4185 /* ARGSUSED */
4186 static void
4187 ip_stack_shutdown(netstackid_t stackid, void *arg)
4189 ip_stack_t *ipst = (ip_stack_t *)arg;
4190 kt_did_t ktid;
4192 #ifdef NS_DEBUG
4193 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4194 #endif
4197 * Perform cleanup for special interfaces (loopback and IPMP).
4199 ip_interface_cleanup(ipst);
4202 * The *_hook_shutdown()s start the process of notifying any
4203 * consumers that things are going away.... nothing is destroyed.
4205 ipv4_hook_shutdown(ipst);
4206 ipv6_hook_shutdown(ipst);
4207 arp_hook_shutdown(ipst);
4209 mutex_enter(&ipst->ips_capab_taskq_lock);
4210 ktid = ipst->ips_capab_taskq_thread->t_did;
4211 ipst->ips_capab_taskq_quit = B_TRUE;
4212 cv_signal(&ipst->ips_capab_taskq_cv);
4213 mutex_exit(&ipst->ips_capab_taskq_lock);
4216 * In rare occurrences, particularly on virtual hardware where CPUs can
4217 * be de-scheduled, the thread that we just signaled will not run until
4218 * after we have gotten through parts of ip_stack_fini. If that happens
4219 * then we'll try to grab the ips_capab_taskq_lock as part of returning
4220 * from cv_wait which no longer exists.
4222 thread_join(ktid);
4226 * Free the IP stack instance.
4228 static void
4229 ip_stack_fini(netstackid_t stackid, void *arg)
4231 ip_stack_t *ipst = (ip_stack_t *)arg;
4232 int ret;
4234 #ifdef NS_DEBUG
4235 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4236 #endif
4238 * At this point, all of the notifications that the events and
4239 * protocols are going away have been run, meaning that we can
4240 * now set about starting to clean things up.
4242 ipobs_fini(ipst);
4243 ipv4_hook_destroy(ipst);
4244 ipv6_hook_destroy(ipst);
4245 arp_hook_destroy(ipst);
4246 ip_net_destroy(ipst);
4248 ipmp_destroy(ipst);
4250 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4251 ipst->ips_ip_mibkp = NULL;
4252 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4253 ipst->ips_icmp_mibkp = NULL;
4254 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4255 ipst->ips_ip_kstat = NULL;
4256 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4257 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4258 ipst->ips_ip6_kstat = NULL;
4259 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4261 kmem_free(ipst->ips_propinfo_tbl,
4262 ip_propinfo_count * sizeof (mod_prop_info_t));
4263 ipst->ips_propinfo_tbl = NULL;
4265 dce_stack_destroy(ipst);
4266 ip_mrouter_stack_destroy(ipst);
4269 * Quiesce all of our timers. Note we set the quiesce flags before we
4270 * call untimeout. The slowtimers may actually kick off another instance
4271 * of the non-slow timers.
4273 mutex_enter(&ipst->ips_igmp_timer_lock);
4274 ipst->ips_igmp_timer_quiesce = B_TRUE;
4275 mutex_exit(&ipst->ips_igmp_timer_lock);
4277 mutex_enter(&ipst->ips_mld_timer_lock);
4278 ipst->ips_mld_timer_quiesce = B_TRUE;
4279 mutex_exit(&ipst->ips_mld_timer_lock);
4281 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
4282 ipst->ips_igmp_slowtimeout_quiesce = B_TRUE;
4283 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
4285 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
4286 ipst->ips_mld_slowtimeout_quiesce = B_TRUE;
4287 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
4289 ret = untimeout(ipst->ips_igmp_timeout_id);
4290 if (ret == -1) {
4291 ASSERT(ipst->ips_igmp_timeout_id == 0);
4292 } else {
4293 ASSERT(ipst->ips_igmp_timeout_id != 0);
4294 ipst->ips_igmp_timeout_id = 0;
4296 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4297 if (ret == -1) {
4298 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4299 } else {
4300 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4301 ipst->ips_igmp_slowtimeout_id = 0;
4303 ret = untimeout(ipst->ips_mld_timeout_id);
4304 if (ret == -1) {
4305 ASSERT(ipst->ips_mld_timeout_id == 0);
4306 } else {
4307 ASSERT(ipst->ips_mld_timeout_id != 0);
4308 ipst->ips_mld_timeout_id = 0;
4310 ret = untimeout(ipst->ips_mld_slowtimeout_id);
4311 if (ret == -1) {
4312 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4313 } else {
4314 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4315 ipst->ips_mld_slowtimeout_id = 0;
4318 ip_ire_fini(ipst);
4319 ip6_asp_free(ipst);
4320 conn_drain_fini(ipst);
4321 ipcl_destroy(ipst);
4323 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4324 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4325 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4326 ipst->ips_ndp4 = NULL;
4327 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4328 ipst->ips_ndp6 = NULL;
4330 if (ipst->ips_loopback_ksp != NULL) {
4331 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4332 ipst->ips_loopback_ksp = NULL;
4335 mutex_destroy(&ipst->ips_capab_taskq_lock);
4336 cv_destroy(&ipst->ips_capab_taskq_cv);
4338 rw_destroy(&ipst->ips_srcid_lock);
4340 mutex_destroy(&ipst->ips_ip_mi_lock);
4341 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4343 mutex_destroy(&ipst->ips_igmp_timer_lock);
4344 mutex_destroy(&ipst->ips_mld_timer_lock);
4345 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4346 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4347 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4348 rw_destroy(&ipst->ips_ill_g_lock);
4350 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4351 ipst->ips_phyint_g_list = NULL;
4352 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4353 ipst->ips_ill_g_heads = NULL;
4355 ldi_ident_release(ipst->ips_ldi_ident);
4356 kmem_free(ipst, sizeof (*ipst));
4360 * This function is called from the TSD destructor, and is used to debug
4361 * reference count issues in IP. See block comment in <inet/ip_if.h> for
4362 * details.
4364 static void
4365 ip_thread_exit(void *phash)
4367 th_hash_t *thh = phash;
4369 rw_enter(&ip_thread_rwlock, RW_WRITER);
4370 list_remove(&ip_thread_list, thh);
4371 rw_exit(&ip_thread_rwlock);
4372 mod_hash_destroy_hash(thh->thh_hash);
4373 kmem_free(thh, sizeof (*thh));
4377 * Called when the IP kernel module is loaded into the kernel
4379 void
4380 ip_ddi_init(void)
4382 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4385 * For IP and TCP the minor numbers should start from 2 since we have 4
4386 * initial devices: ip, ip6, tcp, tcp6.
4389 * If this is a 64-bit kernel, then create two separate arenas -
4390 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4391 * other for socket apps in the range 2^^18 through 2^^32-1.
4393 ip_minor_arena_la = NULL;
4394 ip_minor_arena_sa = NULL;
4395 #if defined(_LP64)
4396 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4397 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4398 cmn_err(CE_PANIC,
4399 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4401 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4402 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4403 cmn_err(CE_PANIC,
4404 "ip_ddi_init: ip_minor_arena_la creation failed\n");
4406 #else
4407 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4408 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4409 cmn_err(CE_PANIC,
4410 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4412 #endif
4413 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4415 ipcl_g_init();
4416 ip_ire_g_init();
4417 ip_net_g_init();
4419 #ifdef DEBUG
4420 tsd_create(&ip_thread_data, ip_thread_exit);
4421 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4422 list_create(&ip_thread_list, sizeof (th_hash_t),
4423 offsetof(th_hash_t, thh_link));
4424 #endif
4425 ipsec_policy_g_init();
4426 tcp_ddi_g_init();
4427 sctp_ddi_g_init();
4428 dce_g_init();
4431 * We want to be informed each time a stack is created or
4432 * destroyed in the kernel, so we can maintain the
4433 * set of udp_stack_t's.
4435 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4436 ip_stack_fini);
4438 udp_ddi_g_init();
4439 rts_ddi_g_init();
4440 icmp_ddi_g_init();
4441 ilb_ddi_g_init();
4443 /* This needs to be called after all transports are initialized. */
4444 mutex_enter(&cpu_lock);
4445 register_cpu_setup_func(ip_tp_cpu_update, NULL);
4446 mutex_exit(&cpu_lock);
4450 * Initialize the IP stack instance.
4452 static void *
4453 ip_stack_init(netstackid_t stackid, netstack_t *ns)
4455 ip_stack_t *ipst;
4456 size_t arrsz;
4457 major_t major;
4459 #ifdef NS_DEBUG
4460 printf("ip_stack_init(stack %d)\n", stackid);
4461 #endif
4463 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4464 ipst->ips_netstack = ns;
4466 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4467 KM_SLEEP);
4468 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4469 KM_SLEEP);
4470 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4471 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4472 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4473 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4475 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4476 ipst->ips_igmp_deferred_next = INFINITY;
4477 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4478 ipst->ips_mld_deferred_next = INFINITY;
4479 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4480 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4481 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4482 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4483 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4484 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4486 ipcl_init(ipst);
4487 ip_ire_init(ipst);
4488 ip6_asp_init(ipst);
4489 ipif_init(ipst);
4490 conn_drain_init(ipst);
4491 ip_mrouter_stack_init(ipst);
4492 dce_stack_init(ipst);
4494 ipst->ips_ill_index = 1;
4496 ipst->ips_saved_ip_forwarding = -1;
4497 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
4499 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4500 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4501 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4503 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4504 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4505 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4506 ipst->ips_ip6_kstat =
4507 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4509 ipst->ips_ip_src_id = 1;
4510 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4512 ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4514 ip_net_init(ipst, ns);
4515 ipv4_hook_init(ipst);
4516 ipv6_hook_init(ipst);
4517 arp_hook_init(ipst);
4518 ipmp_init(ipst);
4519 ipobs_init(ipst);
4522 * Create the taskq dispatcher thread and initialize related stuff.
4524 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4525 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4526 ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4527 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4529 major = mod_name_to_major(INET_NAME);
4530 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4531 return (ipst);
4535 * Allocate and initialize a DLPI template of the specified length. (May be
4536 * called as writer.)
4538 mblk_t *
4539 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4541 mblk_t *mp;
4543 mp = allocb(len, BPRI_MED);
4544 if (!mp)
4545 return (NULL);
4548 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4549 * of which we don't seem to use) are sent with M_PCPROTO, and
4550 * that other DLPI are M_PROTO.
4552 if (prim == DL_INFO_REQ) {
4553 mp->b_datap->db_type = M_PCPROTO;
4554 } else {
4555 mp->b_datap->db_type = M_PROTO;
4558 mp->b_wptr = mp->b_rptr + len;
4559 bzero(mp->b_rptr, len);
4560 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4561 return (mp);
4565 * Allocate and initialize a DLPI notification. (May be called as writer.)
4567 mblk_t *
4568 ip_dlnotify_alloc(uint_t notification, uint_t data)
4570 dl_notify_ind_t *notifyp;
4571 mblk_t *mp;
4573 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4574 return (NULL);
4576 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4577 notifyp->dl_notification = notification;
4578 notifyp->dl_data = data;
4579 return (mp);
4582 mblk_t *
4583 ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4585 dl_notify_ind_t *notifyp;
4586 mblk_t *mp;
4588 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4589 return (NULL);
4591 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4592 notifyp->dl_notification = notification;
4593 notifyp->dl_data1 = data1;
4594 notifyp->dl_data2 = data2;
4595 return (mp);
4599 * Debug formatting routine. Returns a character string representation of the
4600 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
4601 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4603 * Once the ndd table-printing interfaces are removed, this can be changed to
4604 * standard dotted-decimal form.
4606 char *
4607 ip_dot_addr(ipaddr_t addr, char *buf)
4609 uint8_t *ap = (uint8_t *)&addr;
4611 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4612 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4613 return (buf);
4617 * Write the given MAC address as a printable string in the usual colon-
4618 * separated format.
4620 const char *
4621 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4623 char *bp;
4625 if (alen == 0 || buflen < 4)
4626 return ("?");
4627 bp = buf;
4628 for (;;) {
4630 * If there are more MAC address bytes available, but we won't
4631 * have any room to print them, then add "..." to the string
4632 * instead. See below for the 'magic number' explanation.
4634 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4635 (void) strcpy(bp, "...");
4636 break;
4638 (void) sprintf(bp, "%02x", *addr++);
4639 bp += 2;
4640 if (--alen == 0)
4641 break;
4642 *bp++ = ':';
4643 buflen -= 3;
4645 * At this point, based on the first 'if' statement above,
4646 * either alen == 1 and buflen >= 3, or alen > 1 and
4647 * buflen >= 4. The first case leaves room for the final "xx"
4648 * number and trailing NUL byte. The second leaves room for at
4649 * least "...". Thus the apparently 'magic' numbers chosen for
4650 * that statement.
4653 return (buf);
4657 * Called when it is conceptually a ULP that would sent the packet
4658 * e.g., port unreachable and protocol unreachable. Check that the packet
4659 * would have passed the IPsec global policy before sending the error.
4661 * Send an ICMP error after patching up the packet appropriately.
4662 * Uses ip_drop_input and bumps the appropriate MIB.
4664 void
4665 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4666 ip_recv_attr_t *ira)
4668 ipha_t *ipha;
4669 boolean_t secure;
4670 ill_t *ill = ira->ira_ill;
4671 ip_stack_t *ipst = ill->ill_ipst;
4672 netstack_t *ns = ipst->ips_netstack;
4673 ipsec_stack_t *ipss = ns->netstack_ipsec;
4675 secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4678 * We are generating an icmp error for some inbound packet.
4679 * Called from all ip_fanout_(udp, tcp, proto) functions.
4680 * Before we generate an error, check with global policy
4681 * to see whether this is allowed to enter the system. As
4682 * there is no "conn", we are checking with global policy.
4684 ipha = (ipha_t *)mp->b_rptr;
4685 if (secure || ipss->ipsec_inbound_v4_policy_present) {
4686 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4687 if (mp == NULL)
4688 return;
4691 /* We never send errors for protocols that we do implement */
4692 if (ira->ira_protocol == IPPROTO_ICMP ||
4693 ira->ira_protocol == IPPROTO_IGMP) {
4694 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4695 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4696 freemsg(mp);
4697 return;
4700 * Have to correct checksum since
4701 * the packet might have been
4702 * fragmented and the reassembly code in ip_rput
4703 * does not restore the IP checksum.
4705 ipha->ipha_hdr_checksum = 0;
4706 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4708 switch (icmp_type) {
4709 case ICMP_DEST_UNREACHABLE:
4710 switch (icmp_code) {
4711 case ICMP_PROTOCOL_UNREACHABLE:
4712 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4713 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4714 break;
4715 case ICMP_PORT_UNREACHABLE:
4716 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4717 ip_drop_input("ipIfStatsNoPorts", mp, ill);
4718 break;
4721 icmp_unreachable(mp, icmp_code, ira);
4722 break;
4723 default:
4724 #ifdef DEBUG
4725 panic("ip_fanout_send_icmp_v4: wrong type");
4726 /*NOTREACHED*/
4727 #else
4728 freemsg(mp);
4729 break;
4730 #endif
4735 * Used to send an ICMP error message when a packet is received for
4736 * a protocol that is not supported. The mblk passed as argument
4737 * is consumed by this function.
4739 void
4740 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4742 ipha_t *ipha;
4744 ipha = (ipha_t *)mp->b_rptr;
4745 if (ira->ira_flags & IRAF_IS_IPV4) {
4746 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4747 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4748 ICMP_PROTOCOL_UNREACHABLE, ira);
4749 } else {
4750 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4751 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4752 ICMP6_PARAMPROB_NEXTHEADER, ira);
4757 * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4758 * Handles IPv4 and IPv6.
4759 * We are responsible for disposing of mp, such as by freemsg() or putnext()
4760 * Caller is responsible for dropping references to the conn.
4762 void
4763 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4764 ip_recv_attr_t *ira)
4766 ill_t *ill = ira->ira_ill;
4767 ip_stack_t *ipst = ill->ill_ipst;
4768 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4769 boolean_t secure;
4770 uint_t protocol = ira->ira_protocol;
4771 iaflags_t iraflags = ira->ira_flags;
4772 queue_t *rq;
4774 secure = iraflags & IRAF_IPSEC_SECURE;
4776 rq = connp->conn_rq;
4777 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4778 switch (protocol) {
4779 case IPPROTO_ICMPV6:
4780 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4781 break;
4782 case IPPROTO_ICMP:
4783 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4784 break;
4785 default:
4786 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4787 break;
4789 freemsg(mp);
4790 return;
4793 ASSERT(!(IPCL_IS_IPTUN(connp)));
4795 if (((iraflags & IRAF_IS_IPV4) ?
4796 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4797 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4798 secure) {
4799 mp = ipsec_check_inbound_policy(mp, connp, ipha,
4800 ip6h, ira);
4801 if (mp == NULL) {
4802 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4803 /* Note that mp is NULL */
4804 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4805 return;
4809 if (iraflags & IRAF_ICMP_ERROR) {
4810 (connp->conn_recvicmp)(connp, mp, NULL, ira);
4811 } else {
4812 ill_t *rill = ira->ira_rill;
4814 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4815 ira->ira_ill = ira->ira_rill = NULL;
4816 /* Send it upstream */
4817 (connp->conn_recv)(connp, mp, NULL, ira);
4818 ira->ira_ill = ill;
4819 ira->ira_rill = rill;
4824 * Handle protocols with which IP is less intimate. There
4825 * can be more than one stream bound to a particular
4826 * protocol. When this is the case, normally each one gets a copy
4827 * of any incoming packets.
4829 * IPsec NOTE :
4831 * Don't allow a secure packet going up a non-secure connection.
4832 * We don't allow this because
4834 * 1) Reply might go out in clear which will be dropped at
4835 * the sending side.
4836 * 2) If the reply goes out in clear it will give the
4837 * adversary enough information for getting the key in
4838 * most of the cases.
4840 * Moreover getting a secure packet when we expect clear
4841 * implies that SA's were added without checking for
4842 * policy on both ends. This should not happen once ISAKMP
4843 * is used to negotiate SAs as SAs will be added only after
4844 * verifying the policy.
4846 * Zones notes:
4847 * Earlier in ip_input on a system with multiple shared-IP zones we
4848 * duplicate the multicast and broadcast packets and send them up
4849 * with each explicit zoneid that exists on that ill.
4850 * This means that here we can match the zoneid with SO_ALLZONES being special.
4852 void
4853 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
4855 mblk_t *mp1;
4856 ipaddr_t laddr;
4857 conn_t *connp, *first_connp, *next_connp;
4858 connf_t *connfp;
4859 ill_t *ill = ira->ira_ill;
4860 ip_stack_t *ipst = ill->ill_ipst;
4862 laddr = ipha->ipha_dst;
4864 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
4865 mutex_enter(&connfp->connf_lock);
4866 connp = connfp->connf_head;
4868 if (connp == NULL) {
4870 * No one bound to these addresses. Is
4871 * there a client that wants all
4872 * unclaimed datagrams?
4874 mutex_exit(&connfp->connf_lock);
4875 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4876 ICMP_PROTOCOL_UNREACHABLE, ira);
4877 return;
4880 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
4882 CONN_INC_REF(connp);
4883 first_connp = connp;
4884 connp = connp->conn_next;
4886 for (;;) {
4887 while (connp != NULL) {
4888 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
4889 if (IPCL_PROTO_MATCH(connp, ira, ipha))
4890 break;
4891 connp = connp->conn_next;
4894 if (connp == NULL) {
4895 /* No more interested clients */
4896 connp = first_connp;
4897 break;
4899 if (((mp1 = dupmsg(mp)) == NULL) &&
4900 ((mp1 = copymsg(mp)) == NULL)) {
4901 /* Memory allocation failed */
4902 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4903 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4904 connp = first_connp;
4905 break;
4908 CONN_INC_REF(connp);
4909 mutex_exit(&connfp->connf_lock);
4911 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
4912 ira);
4914 mutex_enter(&connfp->connf_lock);
4915 /* Follow the next pointer before releasing the conn. */
4916 next_connp = connp->conn_next;
4917 CONN_DEC_REF(connp);
4918 connp = next_connp;
4921 /* Last one. Send it upstream. */
4922 mutex_exit(&connfp->connf_lock);
4924 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
4926 CONN_DEC_REF(connp);
4930 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
4931 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk
4932 * is not consumed.
4934 * One of three things can happen, all of which affect the passed-in mblk:
4936 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk..
4938 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
4939 * ESP packet, and is passed along to ESP for consumption. Return NULL.
4941 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL.
4943 mblk_t *
4944 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
4946 int shift, plen, iph_len;
4947 ipha_t *ipha;
4948 udpha_t *udpha;
4949 uint32_t *spi;
4950 uint32_t esp_ports;
4951 uint8_t *orptr;
4952 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
4953 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4955 ipha = (ipha_t *)mp->b_rptr;
4956 iph_len = ira->ira_ip_hdr_length;
4957 plen = ira->ira_pktlen;
4959 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
4961 * Most likely a keepalive for the benefit of an intervening
4962 * NAT. These aren't for us, per se, so drop it.
4964 * RFC 3947/8 doesn't say for sure what to do for 2-3
4965 * byte packets (keepalives are 1-byte), but we'll drop them
4966 * also.
4968 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
4969 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
4970 return (NULL);
4973 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
4974 /* might as well pull it all up - it might be ESP. */
4975 if (!pullupmsg(mp, -1)) {
4976 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
4977 DROPPER(ipss, ipds_esp_nomem),
4978 &ipss->ipsec_dropper);
4979 return (NULL);
4982 ipha = (ipha_t *)mp->b_rptr;
4984 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
4985 if (*spi == 0) {
4986 /* UDP packet - remove 0-spi. */
4987 shift = sizeof (uint32_t);
4988 } else {
4989 /* ESP-in-UDP packet - reduce to ESP. */
4990 ipha->ipha_protocol = IPPROTO_ESP;
4991 shift = sizeof (udpha_t);
4994 /* Fix IP header */
4995 ira->ira_pktlen = (plen - shift);
4996 ipha->ipha_length = htons(ira->ira_pktlen);
4997 ipha->ipha_hdr_checksum = 0;
4999 orptr = mp->b_rptr;
5000 mp->b_rptr += shift;
5002 udpha = (udpha_t *)(orptr + iph_len);
5003 if (*spi == 0) {
5004 ASSERT((uint8_t *)ipha == orptr);
5005 udpha->uha_length = htons(plen - shift - iph_len);
5006 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
5007 esp_ports = 0;
5008 } else {
5009 esp_ports = *((uint32_t *)udpha);
5010 ASSERT(esp_ports != 0);
5012 ovbcopy(orptr, orptr + shift, iph_len);
5013 if (esp_ports != 0) /* Punt up for ESP processing. */ {
5014 ipha = (ipha_t *)(orptr + shift);
5016 ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5017 ira->ira_esp_udp_ports = esp_ports;
5018 ip_fanout_v4(mp, ipha, ira);
5019 return (NULL);
5021 return (mp);
5025 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5026 * Handles IPv4 and IPv6.
5027 * We are responsible for disposing of mp, such as by freemsg() or putnext()
5028 * Caller is responsible for dropping references to the conn.
5030 void
5031 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5032 ip_recv_attr_t *ira)
5034 ill_t *ill = ira->ira_ill;
5035 ip_stack_t *ipst = ill->ill_ipst;
5036 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5037 boolean_t secure;
5038 iaflags_t iraflags = ira->ira_flags;
5040 secure = iraflags & IRAF_IPSEC_SECURE;
5042 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5043 !canputnext(connp->conn_rq)) {
5044 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5045 freemsg(mp);
5046 return;
5049 if (((iraflags & IRAF_IS_IPV4) ?
5050 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5051 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5052 secure) {
5053 mp = ipsec_check_inbound_policy(mp, connp, ipha,
5054 ip6h, ira);
5055 if (mp == NULL) {
5056 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5057 /* Note that mp is NULL */
5058 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5059 return;
5064 * Since this code is not used for UDP unicast we don't need a NAT_T
5065 * check. Only ip_fanout_v4 has that check.
5067 if (ira->ira_flags & IRAF_ICMP_ERROR) {
5068 (connp->conn_recvicmp)(connp, mp, NULL, ira);
5069 } else {
5070 ill_t *rill = ira->ira_rill;
5072 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5073 ira->ira_ill = ira->ira_rill = NULL;
5074 /* Send it upstream */
5075 (connp->conn_recv)(connp, mp, NULL, ira);
5076 ira->ira_ill = ill;
5077 ira->ira_rill = rill;
5082 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5083 * (Unicast fanout is handled in ip_input_v4.)
5085 * If SO_REUSEADDR is set all multicast and broadcast packets
5086 * will be delivered to all conns bound to the same port.
5088 * If there is at least one matching AF_INET receiver, then we will
5089 * ignore any AF_INET6 receivers.
5090 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5091 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5092 * packets.
5094 * Zones notes:
5095 * Earlier in ip_input on a system with multiple shared-IP zones we
5096 * duplicate the multicast and broadcast packets and send them up
5097 * with each explicit zoneid that exists on that ill.
5098 * This means that here we can match the zoneid with SO_ALLZONES being special.
5100 void
5101 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5102 ip_recv_attr_t *ira)
5104 ipaddr_t laddr;
5105 in6_addr_t v6faddr;
5106 conn_t *connp;
5107 connf_t *connfp;
5108 ipaddr_t faddr;
5109 ill_t *ill = ira->ira_ill;
5110 ip_stack_t *ipst = ill->ill_ipst;
5112 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5114 laddr = ipha->ipha_dst;
5115 faddr = ipha->ipha_src;
5117 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5118 mutex_enter(&connfp->connf_lock);
5119 connp = connfp->connf_head;
5122 * If SO_REUSEADDR has been set on the first we send the
5123 * packet to all clients that have joined the group and
5124 * match the port.
5126 while (connp != NULL) {
5127 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5128 conn_wantpacket(connp, ira, ipha))
5129 break;
5130 connp = connp->conn_next;
5133 if (connp == NULL)
5134 goto notfound;
5136 CONN_INC_REF(connp);
5138 if (connp->conn_reuseaddr) {
5139 conn_t *first_connp = connp;
5140 conn_t *next_connp;
5141 mblk_t *mp1;
5143 connp = connp->conn_next;
5144 for (;;) {
5145 while (connp != NULL) {
5146 if (IPCL_UDP_MATCH(connp, lport, laddr,
5147 fport, faddr) &&
5148 conn_wantpacket(connp, ira, ipha))
5149 break;
5150 connp = connp->conn_next;
5152 if (connp == NULL) {
5153 /* No more interested clients */
5154 connp = first_connp;
5155 break;
5157 if (((mp1 = dupmsg(mp)) == NULL) &&
5158 ((mp1 = copymsg(mp)) == NULL)) {
5159 /* Memory allocation failed */
5160 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5161 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5162 connp = first_connp;
5163 break;
5165 CONN_INC_REF(connp);
5166 mutex_exit(&connfp->connf_lock);
5168 IP_STAT(ipst, ip_udp_fanmb);
5169 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5170 NULL, ira);
5171 mutex_enter(&connfp->connf_lock);
5172 /* Follow the next pointer before releasing the conn */
5173 next_connp = connp->conn_next;
5174 CONN_DEC_REF(connp);
5175 connp = next_connp;
5179 /* Last one. Send it upstream. */
5180 mutex_exit(&connfp->connf_lock);
5181 IP_STAT(ipst, ip_udp_fanmb);
5182 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5183 CONN_DEC_REF(connp);
5184 return;
5186 notfound:
5187 mutex_exit(&connfp->connf_lock);
5189 * IPv6 endpoints bound to multicast IPv4-mapped addresses
5190 * have already been matched above, since they live in the IPv4
5191 * fanout tables. This implies we only need to
5192 * check for IPv6 in6addr_any endpoints here.
5193 * Thus we compare using ipv6_all_zeros instead of the destination
5194 * address, except for the multicast group membership lookup which
5195 * uses the IPv4 destination.
5197 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5198 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5199 mutex_enter(&connfp->connf_lock);
5200 connp = connfp->connf_head;
5202 * IPv4 multicast packet being delivered to an AF_INET6
5203 * in6addr_any endpoint.
5204 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5205 * and not conn_wantpacket_v6() since any multicast membership is
5206 * for an IPv4-mapped multicast address.
5208 while (connp != NULL) {
5209 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5210 fport, v6faddr) &&
5211 conn_wantpacket(connp, ira, ipha))
5212 break;
5213 connp = connp->conn_next;
5216 if (connp == NULL) {
5218 * No one bound to this port. Is
5219 * there a client that wants all
5220 * unclaimed datagrams?
5222 mutex_exit(&connfp->connf_lock);
5224 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5225 NULL) {
5226 ASSERT(ira->ira_protocol == IPPROTO_UDP);
5227 ip_fanout_proto_v4(mp, ipha, ira);
5228 } else {
5230 * We used to attempt to send an icmp error here, but
5231 * since this is known to be a multicast packet
5232 * and we don't send icmp errors in response to
5233 * multicast, just drop the packet and give up sooner.
5235 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5236 freemsg(mp);
5238 return;
5240 CONN_INC_REF(connp);
5241 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5244 * If SO_REUSEADDR has been set on the first we send the
5245 * packet to all clients that have joined the group and
5246 * match the port.
5248 if (connp->conn_reuseaddr) {
5249 conn_t *first_connp = connp;
5250 conn_t *next_connp;
5251 mblk_t *mp1;
5253 connp = connp->conn_next;
5254 for (;;) {
5255 while (connp != NULL) {
5256 if (IPCL_UDP_MATCH_V6(connp, lport,
5257 ipv6_all_zeros, fport, v6faddr) &&
5258 conn_wantpacket(connp, ira, ipha))
5259 break;
5260 connp = connp->conn_next;
5262 if (connp == NULL) {
5263 /* No more interested clients */
5264 connp = first_connp;
5265 break;
5267 if (((mp1 = dupmsg(mp)) == NULL) &&
5268 ((mp1 = copymsg(mp)) == NULL)) {
5269 /* Memory allocation failed */
5270 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5271 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5272 connp = first_connp;
5273 break;
5275 CONN_INC_REF(connp);
5276 mutex_exit(&connfp->connf_lock);
5278 IP_STAT(ipst, ip_udp_fanmb);
5279 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5280 NULL, ira);
5281 mutex_enter(&connfp->connf_lock);
5282 /* Follow the next pointer before releasing the conn */
5283 next_connp = connp->conn_next;
5284 CONN_DEC_REF(connp);
5285 connp = next_connp;
5289 /* Last one. Send it upstream. */
5290 mutex_exit(&connfp->connf_lock);
5291 IP_STAT(ipst, ip_udp_fanmb);
5292 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5293 CONN_DEC_REF(connp);
5297 * Split an incoming packet's IPv4 options into options.
5298 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5299 * clearing out any leftover options.
5300 * Otherwise it just makes ipp point into the packet.
5302 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5305 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5307 uchar_t *opt;
5308 uint32_t totallen;
5309 uint32_t optval;
5310 uint32_t optlen;
5312 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5313 ipp->ipp_hoplimit = ipha->ipha_ttl;
5314 ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5315 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5318 * Get length (in 4 byte octets) of IP header options.
5320 totallen = ipha->ipha_version_and_hdr_length -
5321 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5323 if (totallen == 0) {
5324 if (!allocate)
5325 return (0);
5327 /* Clear out anything from a previous packet */
5328 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5329 kmem_free(ipp->ipp_ipv4_options,
5330 ipp->ipp_ipv4_options_len);
5331 ipp->ipp_ipv4_options = NULL;
5332 ipp->ipp_ipv4_options_len = 0;
5333 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5335 return (0);
5338 totallen <<= 2;
5339 opt = (uchar_t *)&ipha[1];
5341 copyall:
5342 if (!allocate) {
5343 if (totallen != 0) {
5344 ipp->ipp_ipv4_options = opt;
5345 ipp->ipp_ipv4_options_len = totallen;
5346 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5348 return (0);
5350 /* Just copy all of options */
5351 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5352 if (totallen == ipp->ipp_ipv4_options_len) {
5353 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5354 return (0);
5356 kmem_free(ipp->ipp_ipv4_options,
5357 ipp->ipp_ipv4_options_len);
5358 ipp->ipp_ipv4_options = NULL;
5359 ipp->ipp_ipv4_options_len = 0;
5360 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5362 if (totallen == 0)
5363 return (0);
5365 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5366 if (ipp->ipp_ipv4_options == NULL)
5367 return (ENOMEM);
5368 ipp->ipp_ipv4_options_len = totallen;
5369 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5370 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5371 return (0);
5373 totallen = ipha->ipha_version_and_hdr_length -
5374 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5375 totallen <<= 2;
5376 opt = (uchar_t *)&ipha[1];
5377 goto copyall;
5381 * Efficient versions of lookup for an IRE when we only
5382 * match the address.
5383 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5384 * Does not handle multicast addresses.
5386 uint_t
5387 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5389 ire_t *ire;
5390 uint_t result;
5392 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5393 ASSERT(ire != NULL);
5394 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5395 result = IRE_NOROUTE;
5396 else
5397 result = ire->ire_type;
5398 ire_refrele(ire);
5399 return (result);
5403 * Efficient versions of lookup for an IRE when we only
5404 * match the address.
5405 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5406 * Does not handle multicast addresses.
5408 uint_t
5409 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5411 ire_t *ire;
5412 uint_t result;
5414 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5415 ASSERT(ire != NULL);
5416 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5417 result = IRE_NOROUTE;
5418 else
5419 result = ire->ire_type;
5420 ire_refrele(ire);
5421 return (result);
5425 * Nobody should be sending
5426 * packets up this stream
5428 static int
5429 ip_lrput(queue_t *q, mblk_t *mp)
5431 switch (mp->b_datap->db_type) {
5432 case M_FLUSH:
5433 /* Turn around */
5434 if (*mp->b_rptr & FLUSHW) {
5435 *mp->b_rptr &= ~FLUSHR;
5436 qreply(q, mp);
5437 return (0);
5439 break;
5441 freemsg(mp);
5442 return (0);
5445 /* Nobody should be sending packets down this stream */
5446 /* ARGSUSED */
5448 ip_lwput(queue_t *q, mblk_t *mp)
5450 freemsg(mp);
5451 return (0);
5455 * Move the first hop in any source route to ipha_dst and remove that part of
5456 * the source route. Called by other protocols. Errors in option formatting
5457 * are ignored - will be handled by ip_output_options. Return the final
5458 * destination (either ipha_dst or the last entry in a source route.)
5460 ipaddr_t
5461 ip_massage_options(ipha_t *ipha, netstack_t *ns)
5463 ipoptp_t opts;
5464 uchar_t *opt;
5465 uint8_t optval;
5466 uint8_t optlen;
5467 ipaddr_t dst;
5468 int i;
5469 ip_stack_t *ipst = ns->netstack_ip;
5471 ip2dbg(("ip_massage_options\n"));
5472 dst = ipha->ipha_dst;
5473 for (optval = ipoptp_first(&opts, ipha);
5474 optval != IPOPT_EOL;
5475 optval = ipoptp_next(&opts)) {
5476 opt = opts.ipoptp_cur;
5477 switch (optval) {
5478 uint8_t off;
5479 case IPOPT_SSRR:
5480 case IPOPT_LSRR:
5481 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5482 ip1dbg(("ip_massage_options: bad src route\n"));
5483 break;
5485 optlen = opts.ipoptp_len;
5486 off = opt[IPOPT_OFFSET];
5487 off--;
5488 redo_srr:
5489 if (optlen < IP_ADDR_LEN ||
5490 off > optlen - IP_ADDR_LEN) {
5491 /* End of source route */
5492 ip1dbg(("ip_massage_options: end of SR\n"));
5493 break;
5495 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5496 ip1dbg(("ip_massage_options: next hop 0x%x\n",
5497 ntohl(dst)));
5499 * Check if our address is present more than
5500 * once as consecutive hops in source route.
5501 * XXX verify per-interface ip_forwarding
5502 * for source route?
5504 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5505 off += IP_ADDR_LEN;
5506 goto redo_srr;
5508 if (dst == htonl(INADDR_LOOPBACK)) {
5509 ip1dbg(("ip_massage_options: loopback addr in "
5510 "source route!\n"));
5511 break;
5514 * Update ipha_dst to be the first hop and remove the
5515 * first hop from the source route (by overwriting
5516 * part of the option with NOP options).
5518 ipha->ipha_dst = dst;
5519 /* Put the last entry in dst */
5520 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5522 bcopy(&opt[off], &dst, IP_ADDR_LEN);
5524 ip1dbg(("ip_massage_options: last hop 0x%x\n",
5525 ntohl(dst)));
5526 /* Move down and overwrite */
5527 opt[IP_ADDR_LEN] = opt[0];
5528 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5529 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5530 for (i = 0; i < IP_ADDR_LEN; i++)
5531 opt[i] = IPOPT_NOP;
5532 break;
5535 return (dst);
5539 * Return the network mask
5540 * associated with the specified address.
5542 ipaddr_t
5543 ip_net_mask(ipaddr_t addr)
5545 uchar_t *up = (uchar_t *)&addr;
5546 ipaddr_t mask = 0;
5547 uchar_t *maskp = (uchar_t *)&mask;
5549 #if defined(__i386) || defined(__amd64)
5550 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5551 #endif
5552 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
5553 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5554 #endif
5555 if (CLASSD(addr)) {
5556 maskp[0] = 0xF0;
5557 return (mask);
5560 /* We assume Class E default netmask to be 32 */
5561 if (CLASSE(addr))
5562 return (0xffffffffU);
5564 if (addr == 0)
5565 return (0);
5566 maskp[0] = 0xFF;
5567 if ((up[0] & 0x80) == 0)
5568 return (mask);
5570 maskp[1] = 0xFF;
5571 if ((up[0] & 0xC0) == 0x80)
5572 return (mask);
5574 maskp[2] = 0xFF;
5575 if ((up[0] & 0xE0) == 0xC0)
5576 return (mask);
5578 /* Otherwise return no mask */
5579 return ((ipaddr_t)0);
5582 /* Name/Value Table Lookup Routine */
5583 char *
5584 ip_nv_lookup(nv_t *nv, int value)
5586 if (!nv)
5587 return (NULL);
5588 for (; nv->nv_name; nv++) {
5589 if (nv->nv_value == value)
5590 return (nv->nv_name);
5592 return ("unknown");
5595 static int
5596 ip_wait_for_info_ack(ill_t *ill)
5598 int err;
5600 mutex_enter(&ill->ill_lock);
5601 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5603 * Return value of 0 indicates a pending signal.
5605 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5606 if (err == 0) {
5607 mutex_exit(&ill->ill_lock);
5608 return (EINTR);
5611 mutex_exit(&ill->ill_lock);
5613 * ip_rput_other could have set an error in ill_error on
5614 * receipt of M_ERROR.
5616 return (ill->ill_error);
5620 * This is a module open, i.e. this is a control stream for access
5621 * to a DLPI device. We allocate an ill_t as the instance data in
5622 * this case.
5624 static int
5625 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5627 ill_t *ill;
5628 int err;
5629 zoneid_t zoneid;
5630 netstack_t *ns;
5631 ip_stack_t *ipst;
5634 * Prevent unprivileged processes from pushing IP so that
5635 * they can't send raw IP.
5637 if (secpolicy_net_rawaccess(credp) != 0)
5638 return (EPERM);
5640 ns = netstack_find_by_cred(credp);
5641 ASSERT(ns != NULL);
5642 ipst = ns->netstack_ip;
5643 ASSERT(ipst != NULL);
5646 * For exclusive stacks we set the zoneid to zero
5647 * to make IP operate as if in the global zone.
5649 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5650 zoneid = GLOBAL_ZONEID;
5651 else
5652 zoneid = crgetzoneid(credp);
5654 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5655 q->q_ptr = WR(q)->q_ptr = ill;
5656 ill->ill_ipst = ipst;
5657 ill->ill_zoneid = zoneid;
5660 * ill_init initializes the ill fields and then sends down
5661 * down a DL_INFO_REQ after calling qprocson.
5663 err = ill_init(q, ill);
5665 if (err != 0) {
5666 mi_free(ill);
5667 netstack_rele(ipst->ips_netstack);
5668 q->q_ptr = NULL;
5669 WR(q)->q_ptr = NULL;
5670 return (err);
5674 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5676 * ill_init initializes the ipsq marking this thread as
5677 * writer
5679 ipsq_exit(ill->ill_phyint->phyint_ipsq);
5680 err = ip_wait_for_info_ack(ill);
5681 if (err == 0)
5682 ill->ill_credp = credp;
5683 else
5684 goto fail;
5686 crhold(credp);
5688 mutex_enter(&ipst->ips_ip_mi_lock);
5689 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5690 sflag, credp);
5691 mutex_exit(&ipst->ips_ip_mi_lock);
5692 fail:
5693 if (err) {
5694 (void) ip_close(q, 0, credp);
5695 return (err);
5697 return (0);
5700 /* For /dev/ip aka AF_INET open */
5702 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5704 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5707 /* For /dev/ip6 aka AF_INET6 open */
5709 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5711 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5714 /* IP open routine. */
5716 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5717 boolean_t isv6)
5719 conn_t *connp;
5720 major_t maj;
5721 zoneid_t zoneid;
5722 netstack_t *ns;
5723 ip_stack_t *ipst;
5725 /* Allow reopen. */
5726 if (q->q_ptr != NULL)
5727 return (0);
5729 if (sflag & MODOPEN) {
5730 /* This is a module open */
5731 return (ip_modopen(q, devp, flag, sflag, credp));
5734 if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5736 * Non streams based socket looking for a stream
5737 * to access IP
5739 return (ip_helper_stream_setup(q, devp, flag, sflag,
5740 credp, isv6));
5743 ns = netstack_find_by_cred(credp);
5744 ASSERT(ns != NULL);
5745 ipst = ns->netstack_ip;
5746 ASSERT(ipst != NULL);
5749 * For exclusive stacks we set the zoneid to zero
5750 * to make IP operate as if in the global zone.
5752 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5753 zoneid = GLOBAL_ZONEID;
5754 else
5755 zoneid = crgetzoneid(credp);
5758 * We are opening as a device. This is an IP client stream, and we
5759 * allocate an conn_t as the instance data.
5761 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
5764 * ipcl_conn_create did a netstack_hold. Undo the hold that was
5765 * done by netstack_find_by_cred()
5767 netstack_rele(ipst->ips_netstack);
5769 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
5770 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
5771 connp->conn_ixa->ixa_zoneid = zoneid;
5772 connp->conn_zoneid = zoneid;
5774 connp->conn_rq = q;
5775 q->q_ptr = WR(q)->q_ptr = connp;
5777 /* Minor tells us which /dev entry was opened */
5778 if (isv6) {
5779 connp->conn_family = AF_INET6;
5780 connp->conn_ipversion = IPV6_VERSION;
5781 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
5782 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
5783 } else {
5784 connp->conn_family = AF_INET;
5785 connp->conn_ipversion = IPV4_VERSION;
5786 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
5789 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
5790 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
5791 connp->conn_minor_arena = ip_minor_arena_la;
5792 } else {
5794 * Either minor numbers in the large arena were exhausted
5795 * or a non socket application is doing the open.
5796 * Try to allocate from the small arena.
5798 if ((connp->conn_dev =
5799 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
5800 /* CONN_DEC_REF takes care of netstack_rele() */
5801 q->q_ptr = WR(q)->q_ptr = NULL;
5802 CONN_DEC_REF(connp);
5803 return (EBUSY);
5805 connp->conn_minor_arena = ip_minor_arena_sa;
5808 maj = getemajor(*devp);
5809 *devp = makedevice(maj, (minor_t)connp->conn_dev);
5812 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
5814 connp->conn_cred = credp;
5815 connp->conn_cpid = curproc->p_pid;
5816 /* Cache things in ixa without an extra refhold */
5817 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
5818 connp->conn_ixa->ixa_cred = connp->conn_cred;
5819 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
5822 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
5824 connp->conn_recv = ip_conn_input;
5825 connp->conn_recvicmp = ip_conn_input_icmp;
5827 crhold(connp->conn_cred);
5829 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
5831 connp->conn_rq = q;
5832 connp->conn_wq = WR(q);
5834 /* Non-zero default values */
5835 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
5838 * Make the conn globally visible to walkers
5840 ASSERT(connp->conn_ref == 1);
5841 mutex_enter(&connp->conn_lock);
5842 connp->conn_state_flags &= ~CONN_INCIPIENT;
5843 mutex_exit(&connp->conn_lock);
5845 qprocson(q);
5847 return (0);
5851 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
5852 * all of them are copied to the conn_t. If the req is "zero", the policy is
5853 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
5854 * fields.
5855 * We keep only the latest setting of the policy and thus policy setting
5856 * is not incremental/cumulative.
5858 * Requests to set policies with multiple alternative actions will
5859 * go through a different API.
5862 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
5864 uint_t ah_req = 0;
5865 uint_t esp_req = 0;
5866 uint_t se_req = 0;
5867 ipsec_act_t *actp = NULL;
5868 uint_t nact;
5869 ipsec_policy_head_t *ph;
5870 boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
5871 int error = 0;
5872 netstack_t *ns = connp->conn_netstack;
5873 ip_stack_t *ipst = ns->netstack_ip;
5874 ipsec_stack_t *ipss = ns->netstack_ipsec;
5876 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
5879 * The IP_SEC_OPT option does not allow variable length parameters,
5880 * hence a request cannot be NULL.
5882 if (req == NULL)
5883 return (EINVAL);
5885 ah_req = req->ipsr_ah_req;
5886 esp_req = req->ipsr_esp_req;
5887 se_req = req->ipsr_self_encap_req;
5889 /* Don't allow setting self-encap without one or more of AH/ESP. */
5890 if (se_req != 0 && esp_req == 0 && ah_req == 0)
5891 return (EINVAL);
5894 * Are we dealing with a request to reset the policy (i.e.
5895 * zero requests).
5897 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
5898 (esp_req & REQ_MASK) == 0 &&
5899 (se_req & REQ_MASK) == 0);
5901 if (!is_pol_reset) {
5903 * If we couldn't load IPsec, fail with "protocol
5904 * not supported".
5905 * IPsec may not have been loaded for a request with zero
5906 * policies, so we don't fail in this case.
5908 mutex_enter(&ipss->ipsec_loader_lock);
5909 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
5910 mutex_exit(&ipss->ipsec_loader_lock);
5911 return (EPROTONOSUPPORT);
5913 mutex_exit(&ipss->ipsec_loader_lock);
5916 * Test for valid requests. Invalid algorithms
5917 * need to be tested by IPsec code because new
5918 * algorithms can be added dynamically.
5920 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
5921 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
5922 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
5923 return (EINVAL);
5927 * Only privileged users can issue these
5928 * requests.
5930 if (((ah_req & IPSEC_PREF_NEVER) ||
5931 (esp_req & IPSEC_PREF_NEVER) ||
5932 (se_req & IPSEC_PREF_NEVER)) &&
5933 secpolicy_ip_config(cr, B_FALSE) != 0) {
5934 return (EPERM);
5938 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
5939 * are mutually exclusive.
5941 if (((ah_req & REQ_MASK) == REQ_MASK) ||
5942 ((esp_req & REQ_MASK) == REQ_MASK) ||
5943 ((se_req & REQ_MASK) == REQ_MASK)) {
5944 /* Both of them are set */
5945 return (EINVAL);
5949 ASSERT(MUTEX_HELD(&connp->conn_lock));
5952 * If we have already cached policies in conn_connect(), don't
5953 * let them change now. We cache policies for connections
5954 * whose src,dst [addr, port] is known.
5956 if (connp->conn_policy_cached) {
5957 return (EINVAL);
5961 * We have a zero policies, reset the connection policy if already
5962 * set. This will cause the connection to inherit the
5963 * global policy, if any.
5965 if (is_pol_reset) {
5966 if (connp->conn_policy != NULL) {
5967 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
5968 connp->conn_policy = NULL;
5970 connp->conn_in_enforce_policy = B_FALSE;
5971 connp->conn_out_enforce_policy = B_FALSE;
5972 return (0);
5975 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
5976 ipst->ips_netstack);
5977 if (ph == NULL)
5978 goto enomem;
5980 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
5981 if (actp == NULL)
5982 goto enomem;
5985 * Always insert IPv4 policy entries, since they can also apply to
5986 * ipv6 sockets being used in ipv4-compat mode.
5988 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
5989 IPSEC_TYPE_INBOUND, ns))
5990 goto enomem;
5991 is_pol_inserted = B_TRUE;
5992 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
5993 IPSEC_TYPE_OUTBOUND, ns))
5994 goto enomem;
5997 * We're looking at a v6 socket, also insert the v6-specific
5998 * entries.
6000 if (connp->conn_family == AF_INET6) {
6001 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6002 IPSEC_TYPE_INBOUND, ns))
6003 goto enomem;
6004 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6005 IPSEC_TYPE_OUTBOUND, ns))
6006 goto enomem;
6009 ipsec_actvec_free(actp, nact);
6012 * If the requests need security, set enforce_policy.
6013 * If the requests are IPSEC_PREF_NEVER, one should
6014 * still set conn_out_enforce_policy so that ip_set_destination
6015 * marks the ip_xmit_attr_t appropriatly. This is needed so that
6016 * for connections that we don't cache policy in at connect time,
6017 * if global policy matches in ip_output_attach_policy, we
6018 * don't wrongly inherit global policy. Similarly, we need
6019 * to set conn_in_enforce_policy also so that we don't verify
6020 * policy wrongly.
6022 if ((ah_req & REQ_MASK) != 0 ||
6023 (esp_req & REQ_MASK) != 0 ||
6024 (se_req & REQ_MASK) != 0) {
6025 connp->conn_in_enforce_policy = B_TRUE;
6026 connp->conn_out_enforce_policy = B_TRUE;
6029 return (error);
6030 #undef REQ_MASK
6033 * Common memory-allocation-failure exit path.
6035 enomem:
6036 if (actp != NULL)
6037 ipsec_actvec_free(actp, nact);
6038 if (is_pol_inserted)
6039 ipsec_polhead_flush(ph, ns);
6040 return (ENOMEM);
6044 * Set socket options for joining and leaving multicast groups.
6045 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6046 * The caller has already check that the option name is consistent with
6047 * the address family of the socket.
6050 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6051 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6053 int *i1 = (int *)invalp;
6054 int error = 0;
6055 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6056 struct ip_mreq *v4_mreqp;
6057 struct ipv6_mreq *v6_mreqp;
6058 struct group_req *greqp;
6059 ire_t *ire;
6060 boolean_t done = B_FALSE;
6061 ipaddr_t ifaddr;
6062 in6_addr_t v6group;
6063 uint_t ifindex;
6064 boolean_t mcast_opt = B_TRUE;
6065 mcast_record_t fmode;
6066 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6067 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6069 switch (name) {
6070 case IP_ADD_MEMBERSHIP:
6071 case IPV6_JOIN_GROUP:
6072 mcast_opt = B_FALSE;
6073 /* FALLTHROUGH */
6074 case MCAST_JOIN_GROUP:
6075 fmode = MODE_IS_EXCLUDE;
6076 optfn = ip_opt_add_group;
6077 break;
6079 case IP_DROP_MEMBERSHIP:
6080 case IPV6_LEAVE_GROUP:
6081 mcast_opt = B_FALSE;
6082 /* FALLTHROUGH */
6083 case MCAST_LEAVE_GROUP:
6084 fmode = MODE_IS_INCLUDE;
6085 optfn = ip_opt_delete_group;
6086 break;
6087 default:
6088 ASSERT(0);
6091 if (mcast_opt) {
6092 struct sockaddr_in *sin;
6093 struct sockaddr_in6 *sin6;
6095 greqp = (struct group_req *)i1;
6096 if (greqp->gr_group.ss_family == AF_INET) {
6097 sin = (struct sockaddr_in *)&(greqp->gr_group);
6098 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6099 } else {
6100 if (!inet6)
6101 return (EINVAL); /* Not on INET socket */
6103 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6104 v6group = sin6->sin6_addr;
6106 ifaddr = INADDR_ANY;
6107 ifindex = greqp->gr_interface;
6108 } else if (inet6) {
6109 v6_mreqp = (struct ipv6_mreq *)i1;
6110 v6group = v6_mreqp->ipv6mr_multiaddr;
6111 ifaddr = INADDR_ANY;
6112 ifindex = v6_mreqp->ipv6mr_interface;
6113 } else {
6114 v4_mreqp = (struct ip_mreq *)i1;
6115 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6116 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6117 ifindex = 0;
6121 * In the multirouting case, we need to replicate
6122 * the request on all interfaces that will take part
6123 * in replication. We do so because multirouting is
6124 * reflective, thus we will probably receive multi-
6125 * casts on those interfaces.
6127 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6128 ipaddr_t group;
6130 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6132 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6133 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6134 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6135 } else {
6136 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6137 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6138 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6140 if (ire != NULL)
6141 ire_refrele(ire);
6143 if (!done) {
6144 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6145 fmode, &ipv6_all_zeros);
6147 return (error);
6151 * Set socket options for joining and leaving multicast groups
6152 * for specific sources.
6153 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6154 * The caller has already check that the option name is consistent with
6155 * the address family of the socket.
6158 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6159 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6161 int *i1 = (int *)invalp;
6162 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6163 struct ip_mreq_source *imreqp;
6164 struct group_source_req *gsreqp;
6165 in6_addr_t v6group, v6src;
6166 uint32_t ifindex;
6167 ipaddr_t ifaddr;
6168 boolean_t mcast_opt = B_TRUE;
6169 mcast_record_t fmode;
6170 ire_t *ire;
6171 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6172 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6174 switch (name) {
6175 case IP_BLOCK_SOURCE:
6176 mcast_opt = B_FALSE;
6177 /* FALLTHROUGH */
6178 case MCAST_BLOCK_SOURCE:
6179 fmode = MODE_IS_EXCLUDE;
6180 optfn = ip_opt_add_group;
6181 break;
6183 case IP_UNBLOCK_SOURCE:
6184 mcast_opt = B_FALSE;
6185 /* FALLTHROUGH */
6186 case MCAST_UNBLOCK_SOURCE:
6187 fmode = MODE_IS_EXCLUDE;
6188 optfn = ip_opt_delete_group;
6189 break;
6191 case IP_ADD_SOURCE_MEMBERSHIP:
6192 mcast_opt = B_FALSE;
6193 /* FALLTHROUGH */
6194 case MCAST_JOIN_SOURCE_GROUP:
6195 fmode = MODE_IS_INCLUDE;
6196 optfn = ip_opt_add_group;
6197 break;
6199 case IP_DROP_SOURCE_MEMBERSHIP:
6200 mcast_opt = B_FALSE;
6201 /* FALLTHROUGH */
6202 case MCAST_LEAVE_SOURCE_GROUP:
6203 fmode = MODE_IS_INCLUDE;
6204 optfn = ip_opt_delete_group;
6205 break;
6206 default:
6207 ASSERT(0);
6210 if (mcast_opt) {
6211 gsreqp = (struct group_source_req *)i1;
6212 ifindex = gsreqp->gsr_interface;
6213 if (gsreqp->gsr_group.ss_family == AF_INET) {
6214 struct sockaddr_in *s;
6215 s = (struct sockaddr_in *)&gsreqp->gsr_group;
6216 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6217 s = (struct sockaddr_in *)&gsreqp->gsr_source;
6218 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6219 } else {
6220 struct sockaddr_in6 *s6;
6222 if (!inet6)
6223 return (EINVAL); /* Not on INET socket */
6225 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6226 v6group = s6->sin6_addr;
6227 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6228 v6src = s6->sin6_addr;
6230 ifaddr = INADDR_ANY;
6231 } else {
6232 imreqp = (struct ip_mreq_source *)i1;
6233 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6234 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6235 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6236 ifindex = 0;
6240 * Handle src being mapped INADDR_ANY by changing it to unspecified.
6242 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6243 v6src = ipv6_all_zeros;
6246 * In the multirouting case, we need to replicate
6247 * the request as noted in the mcast cases above.
6249 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6250 ipaddr_t group;
6252 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6254 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6255 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6256 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6257 } else {
6258 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6259 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES,
6260 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6262 if (ire != NULL)
6263 ire_refrele(ire);
6264 return (optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6265 fmode, &v6src));
6269 * Given a destination address and a pointer to where to put the information
6270 * this routine fills in the mtuinfo.
6271 * The socket must be connected.
6272 * For sctp conn_faddr is the primary address.
6275 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6277 uint32_t pmtu = IP_MAXPACKET;
6278 uint_t scopeid;
6280 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6281 return (-1);
6283 /* In case we never sent or called ip_set_destination_v4/v6 */
6284 if (ixa->ixa_ire != NULL)
6285 pmtu = ip_get_pmtu(ixa);
6287 if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6288 scopeid = ixa->ixa_scopeid;
6289 else
6290 scopeid = 0;
6292 bzero(mtuinfo, sizeof (*mtuinfo));
6293 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6294 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6295 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6296 mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6297 mtuinfo->ip6m_mtu = pmtu;
6299 return (sizeof (struct ip6_mtuinfo));
6303 * When the src multihoming is changed from weak to [strong, preferred]
6304 * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6305 * and identify routes that were created by user-applications in the
6306 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6307 * currently defined. These routes are then 'rebound', i.e., their ire_ill
6308 * is selected by finding an interface route for the gateway.
6310 /* ARGSUSED */
6311 void
6312 ip_ire_rebind_walker(ire_t *ire, void *notused)
6314 if (!ire->ire_unbound || ire->ire_ill != NULL)
6315 return;
6316 ire_rebind(ire);
6317 ire_delete(ire);
6321 * When the src multihoming is changed from [strong, preferred] to weak,
6322 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6323 * set any entries that were created by user-applications in the unbound state
6324 * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6326 /* ARGSUSED */
6327 void
6328 ip_ire_unbind_walker(ire_t *ire, void *notused)
6330 ire_t *new_ire;
6332 if (!ire->ire_unbound || ire->ire_ill == NULL)
6333 return;
6334 if (ire->ire_ipversion == IPV6_VERSION) {
6335 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6336 &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6337 ire->ire_zoneid, ire->ire_flags, ire->ire_ipst);
6338 } else {
6339 new_ire = ire_create((uchar_t *)&ire->ire_addr,
6340 (uchar_t *)&ire->ire_mask,
6341 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6342 ire->ire_zoneid, ire->ire_flags, ire->ire_ipst);
6344 if (new_ire == NULL)
6345 return;
6346 new_ire->ire_unbound = B_TRUE;
6348 * The bound ire must first be deleted so that we don't return
6349 * the existing one on the attempt to add the unbound new_ire.
6351 ire_delete(ire);
6352 new_ire = ire_add(new_ire);
6353 if (new_ire != NULL)
6354 ire_refrele(new_ire);
6358 * When the settings of ip*_strict_src_multihoming tunables are changed,
6359 * all cached routes need to be recomputed. This recomputation needs to be
6360 * done when going from weaker to stronger modes so that the cached ire
6361 * for the connection does not violate the current ip*_strict_src_multihoming
6362 * setting. It also needs to be done when going from stronger to weaker modes,
6363 * so that we fall back to matching on the longest-matching-route (as opposed
6364 * to a shorter match that may have been selected in the strong mode
6365 * to satisfy src_multihoming settings).
6367 * The cached ixa_ire entires for all conn_t entries are marked as
6368 * "verify" so that they will be recomputed for the next packet.
6370 void
6371 conn_ire_revalidate(conn_t *connp, void *arg)
6373 boolean_t isv6 = (boolean_t)arg;
6375 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6376 (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6377 return;
6378 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6382 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6383 * When an ipf is passed here for the first time, if
6384 * we already have in-order fragments on the queue, we convert from the fast-
6385 * path reassembly scheme to the hard-case scheme. From then on, additional
6386 * fragments are reassembled here. We keep track of the start and end offsets
6387 * of each piece, and the number of holes in the chain. When the hole count
6388 * goes to zero, we are done!
6390 * The ipf_count will be updated to account for any mblk(s) added (pointed to
6391 * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6392 * ipfb_count and ill_frag_count by the difference of ipf_count before and
6393 * after the call to ip_reassemble().
6396 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6397 size_t msg_len)
6399 uint_t end;
6400 mblk_t *next_mp;
6401 mblk_t *mp1;
6402 uint_t offset;
6403 boolean_t incr_dups = B_TRUE;
6404 boolean_t offset_zero_seen = B_FALSE;
6405 boolean_t pkt_boundary_checked = B_FALSE;
6407 /* If start == 0 then ipf_nf_hdr_len has to be set. */
6408 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6410 /* Add in byte count */
6411 ipf->ipf_count += msg_len;
6412 if (ipf->ipf_end) {
6414 * We were part way through in-order reassembly, but now there
6415 * is a hole. We walk through messages already queued, and
6416 * mark them for hard case reassembly. We know that up till
6417 * now they were in order starting from offset zero.
6419 offset = 0;
6420 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6421 IP_REASS_SET_START(mp1, offset);
6422 if (offset == 0) {
6423 ASSERT(ipf->ipf_nf_hdr_len != 0);
6424 offset = -ipf->ipf_nf_hdr_len;
6426 offset += mp1->b_wptr - mp1->b_rptr;
6427 IP_REASS_SET_END(mp1, offset);
6429 /* One hole at the end. */
6430 ipf->ipf_hole_cnt = 1;
6431 /* Brand it as a hard case, forever. */
6432 ipf->ipf_end = 0;
6434 /* Walk through all the new pieces. */
6435 do {
6436 end = start + (mp->b_wptr - mp->b_rptr);
6438 * If start is 0, decrease 'end' only for the first mblk of
6439 * the fragment. Otherwise 'end' can get wrong value in the
6440 * second pass of the loop if first mblk is exactly the
6441 * size of ipf_nf_hdr_len.
6443 if (start == 0 && !offset_zero_seen) {
6444 /* First segment */
6445 ASSERT(ipf->ipf_nf_hdr_len != 0);
6446 end -= ipf->ipf_nf_hdr_len;
6447 offset_zero_seen = B_TRUE;
6449 next_mp = mp->b_cont;
6451 * We are checking to see if there is any interesing data
6452 * to process. If there isn't and the mblk isn't the
6453 * one which carries the unfragmentable header then we
6454 * drop it. It's possible to have just the unfragmentable
6455 * header come through without any data. That needs to be
6456 * saved.
6458 * If the assert at the top of this function holds then the
6459 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code
6460 * is infrequently traveled enough that the test is left in
6461 * to protect against future code changes which break that
6462 * invariant.
6464 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6465 /* Empty. Blast it. */
6466 IP_REASS_SET_START(mp, 0);
6467 IP_REASS_SET_END(mp, 0);
6469 * If the ipf points to the mblk we are about to free,
6470 * update ipf to point to the next mblk (or NULL
6471 * if none).
6473 if (ipf->ipf_mp->b_cont == mp)
6474 ipf->ipf_mp->b_cont = next_mp;
6475 freeb(mp);
6476 continue;
6478 mp->b_cont = NULL;
6479 IP_REASS_SET_START(mp, start);
6480 IP_REASS_SET_END(mp, end);
6481 if (!ipf->ipf_tail_mp) {
6482 ipf->ipf_tail_mp = mp;
6483 ipf->ipf_mp->b_cont = mp;
6484 if (start == 0 || !more) {
6485 ipf->ipf_hole_cnt = 1;
6487 * if the first fragment comes in more than one
6488 * mblk, this loop will be executed for each
6489 * mblk. Need to adjust hole count so exiting
6490 * this routine will leave hole count at 1.
6492 if (next_mp)
6493 ipf->ipf_hole_cnt++;
6494 } else
6495 ipf->ipf_hole_cnt = 2;
6496 continue;
6497 } else if (ipf->ipf_last_frag_seen && !more &&
6498 !pkt_boundary_checked) {
6500 * We check datagram boundary only if this fragment
6501 * claims to be the last fragment and we have seen a
6502 * last fragment in the past too. We do this only
6503 * once for a given fragment.
6505 * start cannot be 0 here as fragments with start=0
6506 * and MF=0 gets handled as a complete packet. These
6507 * fragments should not reach here.
6510 if (start + msgdsize(mp) !=
6511 IP_REASS_END(ipf->ipf_tail_mp)) {
6513 * We have two fragments both of which claim
6514 * to be the last fragment but gives conflicting
6515 * information about the whole datagram size.
6516 * Something fishy is going on. Drop the
6517 * fragment and free up the reassembly list.
6519 return (IP_REASS_FAILED);
6523 * We shouldn't come to this code block again for this
6524 * particular fragment.
6526 pkt_boundary_checked = B_TRUE;
6529 /* New stuff at or beyond tail? */
6530 offset = IP_REASS_END(ipf->ipf_tail_mp);
6531 if (start >= offset) {
6532 if (ipf->ipf_last_frag_seen) {
6533 /* current fragment is beyond last fragment */
6534 return (IP_REASS_FAILED);
6536 /* Link it on end. */
6537 ipf->ipf_tail_mp->b_cont = mp;
6538 ipf->ipf_tail_mp = mp;
6539 if (more) {
6540 if (start != offset)
6541 ipf->ipf_hole_cnt++;
6542 } else if (start == offset && next_mp == NULL)
6543 ipf->ipf_hole_cnt--;
6544 continue;
6546 mp1 = ipf->ipf_mp->b_cont;
6547 offset = IP_REASS_START(mp1);
6548 /* New stuff at the front? */
6549 if (start < offset) {
6550 if (start == 0) {
6551 if (end >= offset) {
6552 /* Nailed the hole at the begining. */
6553 ipf->ipf_hole_cnt--;
6555 } else if (end < offset) {
6557 * A hole, stuff, and a hole where there used
6558 * to be just a hole.
6560 ipf->ipf_hole_cnt++;
6562 mp->b_cont = mp1;
6563 /* Check for overlap. */
6564 while (end > offset) {
6565 if (end < IP_REASS_END(mp1)) {
6566 mp->b_wptr -= end - offset;
6567 IP_REASS_SET_END(mp, offset);
6568 BUMP_MIB(ill->ill_ip_mib,
6569 ipIfStatsReasmPartDups);
6570 break;
6572 /* Did we cover another hole? */
6573 if ((mp1->b_cont &&
6574 IP_REASS_END(mp1) !=
6575 IP_REASS_START(mp1->b_cont) &&
6576 end >= IP_REASS_START(mp1->b_cont)) ||
6577 (!ipf->ipf_last_frag_seen && !more)) {
6578 ipf->ipf_hole_cnt--;
6580 /* Clip out mp1. */
6581 if ((mp->b_cont = mp1->b_cont) == NULL) {
6583 * After clipping out mp1, this guy
6584 * is now hanging off the end.
6586 ipf->ipf_tail_mp = mp;
6588 IP_REASS_SET_START(mp1, 0);
6589 IP_REASS_SET_END(mp1, 0);
6590 /* Subtract byte count */
6591 ipf->ipf_count -= mp1->b_datap->db_lim -
6592 mp1->b_datap->db_base;
6593 freeb(mp1);
6594 BUMP_MIB(ill->ill_ip_mib,
6595 ipIfStatsReasmPartDups);
6596 mp1 = mp->b_cont;
6597 if (!mp1)
6598 break;
6599 offset = IP_REASS_START(mp1);
6601 ipf->ipf_mp->b_cont = mp;
6602 continue;
6605 * The new piece starts somewhere between the start of the head
6606 * and before the end of the tail.
6608 for (; mp1; mp1 = mp1->b_cont) {
6609 offset = IP_REASS_END(mp1);
6610 if (start < offset) {
6611 if (end <= offset) {
6612 /* Nothing new. */
6613 IP_REASS_SET_START(mp, 0);
6614 IP_REASS_SET_END(mp, 0);
6615 /* Subtract byte count */
6616 ipf->ipf_count -= mp->b_datap->db_lim -
6617 mp->b_datap->db_base;
6618 if (incr_dups) {
6619 ipf->ipf_num_dups++;
6620 incr_dups = B_FALSE;
6622 freeb(mp);
6623 BUMP_MIB(ill->ill_ip_mib,
6624 ipIfStatsReasmDuplicates);
6625 break;
6628 * Trim redundant stuff off beginning of new
6629 * piece.
6631 IP_REASS_SET_START(mp, offset);
6632 mp->b_rptr += offset - start;
6633 BUMP_MIB(ill->ill_ip_mib,
6634 ipIfStatsReasmPartDups);
6635 start = offset;
6636 if (!mp1->b_cont) {
6638 * After trimming, this guy is now
6639 * hanging off the end.
6641 mp1->b_cont = mp;
6642 ipf->ipf_tail_mp = mp;
6643 if (!more) {
6644 ipf->ipf_hole_cnt--;
6646 break;
6649 if (start >= IP_REASS_START(mp1->b_cont))
6650 continue;
6651 /* Fill a hole */
6652 if (start > offset)
6653 ipf->ipf_hole_cnt++;
6654 mp->b_cont = mp1->b_cont;
6655 mp1->b_cont = mp;
6656 mp1 = mp->b_cont;
6657 offset = IP_REASS_START(mp1);
6658 if (end >= offset) {
6659 ipf->ipf_hole_cnt--;
6660 /* Check for overlap. */
6661 while (end > offset) {
6662 if (end < IP_REASS_END(mp1)) {
6663 mp->b_wptr -= end - offset;
6664 IP_REASS_SET_END(mp, offset);
6666 * TODO we might bump
6667 * this up twice if there is
6668 * overlap at both ends.
6670 BUMP_MIB(ill->ill_ip_mib,
6671 ipIfStatsReasmPartDups);
6672 break;
6674 /* Did we cover another hole? */
6675 if ((mp1->b_cont &&
6676 IP_REASS_END(mp1)
6677 != IP_REASS_START(mp1->b_cont) &&
6678 end >=
6679 IP_REASS_START(mp1->b_cont)) ||
6680 (!ipf->ipf_last_frag_seen &&
6681 !more)) {
6682 ipf->ipf_hole_cnt--;
6684 /* Clip out mp1. */
6685 if ((mp->b_cont = mp1->b_cont) ==
6686 NULL) {
6688 * After clipping out mp1,
6689 * this guy is now hanging
6690 * off the end.
6692 ipf->ipf_tail_mp = mp;
6694 IP_REASS_SET_START(mp1, 0);
6695 IP_REASS_SET_END(mp1, 0);
6696 /* Subtract byte count */
6697 ipf->ipf_count -=
6698 mp1->b_datap->db_lim -
6699 mp1->b_datap->db_base;
6700 freeb(mp1);
6701 BUMP_MIB(ill->ill_ip_mib,
6702 ipIfStatsReasmPartDups);
6703 mp1 = mp->b_cont;
6704 if (!mp1)
6705 break;
6706 offset = IP_REASS_START(mp1);
6709 break;
6711 } while (start = end, mp = next_mp);
6713 /* Fragment just processed could be the last one. Remember this fact */
6714 if (!more)
6715 ipf->ipf_last_frag_seen = B_TRUE;
6717 /* Still got holes? */
6718 if (ipf->ipf_hole_cnt)
6719 return (IP_REASS_PARTIAL);
6720 /* Clean up overloaded fields to avoid upstream disasters. */
6721 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6722 IP_REASS_SET_START(mp1, 0);
6723 IP_REASS_SET_END(mp1, 0);
6725 return (IP_REASS_COMPLETE);
6729 * Fragmentation reassembly. Each ILL has a hash table for
6730 * queuing packets undergoing reassembly for all IPIFs
6731 * associated with the ILL. The hash is based on the packet
6732 * IP ident field. The ILL frag hash table was allocated
6733 * as a timer block at the time the ILL was created. Whenever
6734 * there is anything on the reassembly queue, the timer will
6735 * be running. Returns the reassembled packet if reassembly completes.
6737 mblk_t *
6738 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
6740 uint32_t frag_offset_flags;
6741 mblk_t *t_mp;
6742 ipaddr_t dst;
6743 uint8_t proto = ipha->ipha_protocol;
6744 uint32_t sum_val;
6745 uint16_t sum_flags;
6746 ipf_t *ipf;
6747 ipf_t **ipfp;
6748 ipfb_t *ipfb;
6749 uint16_t ident;
6750 uint32_t offset;
6751 ipaddr_t src;
6752 uint_t hdr_length;
6753 uint32_t end;
6754 mblk_t *mp1;
6755 mblk_t *tail_mp;
6756 size_t count;
6757 size_t msg_len;
6758 uint8_t ecn_info = 0;
6759 uint32_t packet_size;
6760 boolean_t pruned = B_FALSE;
6761 ill_t *ill = ira->ira_ill;
6762 ip_stack_t *ipst = ill->ill_ipst;
6765 * Drop the fragmented as early as possible, if
6766 * we don't have resource(s) to re-assemble.
6768 if (ipst->ips_ip_reass_queue_bytes == 0) {
6769 freemsg(mp);
6770 return (NULL);
6773 /* Check for fragmentation offset; return if there's none */
6774 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
6775 (IPH_MF | IPH_OFFSET)) == 0)
6776 return (mp);
6779 * We utilize hardware computed checksum info only for UDP since
6780 * IP fragmentation is a normal occurrence for the protocol. In
6781 * addition, checksum offload support for IP fragments carrying
6782 * UDP payload is commonly implemented across network adapters.
6784 ASSERT(ira->ira_rill != NULL);
6785 if (proto == IPPROTO_UDP && dohwcksum &&
6786 ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
6787 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
6788 mblk_t *mp1 = mp->b_cont;
6789 int32_t len;
6791 /* Record checksum information from the packet */
6792 sum_val = (uint32_t)DB_CKSUM16(mp);
6793 sum_flags = DB_CKSUMFLAGS(mp);
6795 /* IP payload offset from beginning of mblk */
6796 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
6798 if ((sum_flags & HCK_PARTIALCKSUM) &&
6799 (mp1 == NULL || mp1->b_cont == NULL) &&
6800 offset >= DB_CKSUMSTART(mp) &&
6801 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
6802 uint32_t adj;
6804 * Partial checksum has been calculated by hardware
6805 * and attached to the packet; in addition, any
6806 * prepended extraneous data is even byte aligned.
6807 * If any such data exists, we adjust the checksum;
6808 * this would also handle any postpended data.
6810 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
6811 mp, mp1, len, adj);
6813 /* One's complement subtract extraneous checksum */
6814 if (adj >= sum_val)
6815 sum_val = ~(adj - sum_val) & 0xFFFF;
6816 else
6817 sum_val -= adj;
6819 } else {
6820 sum_val = 0;
6821 sum_flags = 0;
6824 /* Clear hardware checksumming flag */
6825 DB_CKSUMFLAGS(mp) = 0;
6827 ident = ipha->ipha_ident;
6828 offset = (frag_offset_flags << 3) & 0xFFFF;
6829 src = ipha->ipha_src;
6830 dst = ipha->ipha_dst;
6831 hdr_length = IPH_HDR_LENGTH(ipha);
6832 end = ntohs(ipha->ipha_length) - hdr_length;
6834 /* If end == 0 then we have a packet with no data, so just free it */
6835 if (end == 0) {
6836 freemsg(mp);
6837 return (NULL);
6840 /* Record the ECN field info. */
6841 ecn_info = (ipha->ipha_type_of_service & 0x3);
6842 if (offset != 0) {
6844 * If this isn't the first piece, strip the header, and
6845 * add the offset to the end value.
6847 mp->b_rptr += hdr_length;
6848 end += offset;
6851 /* Handle vnic loopback of fragments */
6852 if (mp->b_datap->db_ref > 2)
6853 msg_len = 0;
6854 else
6855 msg_len = MBLKSIZE(mp);
6857 tail_mp = mp;
6858 while (tail_mp->b_cont != NULL) {
6859 tail_mp = tail_mp->b_cont;
6860 if (tail_mp->b_datap->db_ref <= 2)
6861 msg_len += MBLKSIZE(tail_mp);
6864 /* If the reassembly list for this ILL will get too big, prune it */
6865 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
6866 ipst->ips_ip_reass_queue_bytes) {
6867 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
6868 uint_t, ill->ill_frag_count,
6869 uint_t, ipst->ips_ip_reass_queue_bytes);
6870 ill_frag_prune(ill,
6871 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
6872 (ipst->ips_ip_reass_queue_bytes - msg_len));
6873 pruned = B_TRUE;
6876 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
6877 mutex_enter(&ipfb->ipfb_lock);
6879 ipfp = &ipfb->ipfb_ipf;
6880 /* Try to find an existing fragment queue for this packet. */
6881 for (;;) {
6882 ipf = ipfp[0];
6883 if (ipf != NULL) {
6885 * It has to match on ident and src/dst address.
6887 if (ipf->ipf_ident == ident &&
6888 ipf->ipf_src == src &&
6889 ipf->ipf_dst == dst &&
6890 ipf->ipf_protocol == proto) {
6892 * If we have received too many
6893 * duplicate fragments for this packet
6894 * free it.
6896 if (ipf->ipf_num_dups > ip_max_frag_dups) {
6897 ill_frag_free_pkts(ill, ipfb, ipf, 1);
6898 freemsg(mp);
6899 mutex_exit(&ipfb->ipfb_lock);
6900 return (NULL);
6902 /* Found it. */
6903 break;
6905 ipfp = &ipf->ipf_hash_next;
6906 continue;
6910 * If we pruned the list, do we want to store this new
6911 * fragment?. We apply an optimization here based on the
6912 * fact that most fragments will be received in order.
6913 * So if the offset of this incoming fragment is zero,
6914 * it is the first fragment of a new packet. We will
6915 * keep it. Otherwise drop the fragment, as we have
6916 * probably pruned the packet already (since the
6917 * packet cannot be found).
6919 if (pruned && offset != 0) {
6920 mutex_exit(&ipfb->ipfb_lock);
6921 freemsg(mp);
6922 return (NULL);
6925 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) {
6927 * Too many fragmented packets in this hash
6928 * bucket. Free the oldest.
6930 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
6933 /* New guy. Allocate a frag message. */
6934 mp1 = allocb(sizeof (*ipf), BPRI_MED);
6935 if (mp1 == NULL) {
6936 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
6937 ip_drop_input("ipIfStatsInDiscards", mp, ill);
6938 freemsg(mp);
6939 reass_done:
6940 mutex_exit(&ipfb->ipfb_lock);
6941 return (NULL);
6944 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
6945 mp1->b_cont = mp;
6947 /* Initialize the fragment header. */
6948 ipf = (ipf_t *)mp1->b_rptr;
6949 ipf->ipf_mp = mp1;
6950 ipf->ipf_ptphn = ipfp;
6951 ipfp[0] = ipf;
6952 ipf->ipf_hash_next = NULL;
6953 ipf->ipf_ident = ident;
6954 ipf->ipf_protocol = proto;
6955 ipf->ipf_src = src;
6956 ipf->ipf_dst = dst;
6957 ipf->ipf_nf_hdr_len = 0;
6958 /* Record reassembly start time. */
6959 ipf->ipf_timestamp = gethrestime_sec();
6960 /* Record ipf generation and account for frag header */
6961 ipf->ipf_gen = ill->ill_ipf_gen++;
6962 ipf->ipf_count = MBLKSIZE(mp1);
6963 ipf->ipf_last_frag_seen = B_FALSE;
6964 ipf->ipf_ecn = ecn_info;
6965 ipf->ipf_num_dups = 0;
6966 ipfb->ipfb_frag_pkts++;
6967 ipf->ipf_checksum = 0;
6968 ipf->ipf_checksum_flags = 0;
6970 /* Store checksum value in fragment header */
6971 if (sum_flags != 0) {
6972 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
6973 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
6974 ipf->ipf_checksum = sum_val;
6975 ipf->ipf_checksum_flags = sum_flags;
6979 * We handle reassembly two ways. In the easy case,
6980 * where all the fragments show up in order, we do
6981 * minimal bookkeeping, and just clip new pieces on
6982 * the end. If we ever see a hole, then we go off
6983 * to ip_reassemble which has to mark the pieces and
6984 * keep track of the number of holes, etc. Obviously,
6985 * the point of having both mechanisms is so we can
6986 * handle the easy case as efficiently as possible.
6988 if (offset == 0) {
6989 /* Easy case, in-order reassembly so far. */
6990 ipf->ipf_count += msg_len;
6991 ipf->ipf_tail_mp = tail_mp;
6993 * Keep track of next expected offset in
6994 * ipf_end.
6996 ipf->ipf_end = end;
6997 ipf->ipf_nf_hdr_len = hdr_length;
6998 } else {
6999 /* Hard case, hole at the beginning. */
7000 ipf->ipf_tail_mp = NULL;
7002 * ipf_end == 0 means that we have given up
7003 * on easy reassembly.
7005 ipf->ipf_end = 0;
7007 /* Forget checksum offload from now on */
7008 ipf->ipf_checksum_flags = 0;
7011 * ipf_hole_cnt is set by ip_reassemble.
7012 * ipf_count is updated by ip_reassemble.
7013 * No need to check for return value here
7014 * as we don't expect reassembly to complete
7015 * or fail for the first fragment itself.
7017 (void) ip_reassemble(mp, ipf,
7018 (frag_offset_flags & IPH_OFFSET) << 3,
7019 (frag_offset_flags & IPH_MF), ill, msg_len);
7021 /* Update per ipfb and ill byte counts */
7022 ipfb->ipfb_count += ipf->ipf_count;
7023 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7024 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7025 /* If the frag timer wasn't already going, start it. */
7026 mutex_enter(&ill->ill_lock);
7027 ill_frag_timer_start(ill);
7028 mutex_exit(&ill->ill_lock);
7029 goto reass_done;
7033 * If the packet's flag has changed (it could be coming up
7034 * from an interface different than the previous, therefore
7035 * possibly different checksum capability), then forget about
7036 * any stored checksum states. Otherwise add the value to
7037 * the existing one stored in the fragment header.
7039 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7040 sum_val += ipf->ipf_checksum;
7041 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7042 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7043 ipf->ipf_checksum = sum_val;
7044 } else if (ipf->ipf_checksum_flags != 0) {
7045 /* Forget checksum offload from now on */
7046 ipf->ipf_checksum_flags = 0;
7050 * We have a new piece of a datagram which is already being
7051 * reassembled. Update the ECN info if all IP fragments
7052 * are ECN capable. If there is one which is not, clear
7053 * all the info. If there is at least one which has CE
7054 * code point, IP needs to report that up to transport.
7056 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7057 if (ecn_info == IPH_ECN_CE)
7058 ipf->ipf_ecn = IPH_ECN_CE;
7059 } else {
7060 ipf->ipf_ecn = IPH_ECN_NECT;
7062 if (offset && ipf->ipf_end == offset) {
7063 /* The new fragment fits at the end */
7064 ipf->ipf_tail_mp->b_cont = mp;
7065 /* Update the byte count */
7066 ipf->ipf_count += msg_len;
7067 /* Update per ipfb and ill byte counts */
7068 ipfb->ipfb_count += msg_len;
7069 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7070 atomic_add_32(&ill->ill_frag_count, msg_len);
7071 if (frag_offset_flags & IPH_MF) {
7072 /* More to come. */
7073 ipf->ipf_end = end;
7074 ipf->ipf_tail_mp = tail_mp;
7075 goto reass_done;
7077 } else {
7078 /* Go do the hard cases. */
7079 int ret;
7081 if (offset == 0)
7082 ipf->ipf_nf_hdr_len = hdr_length;
7084 /* Save current byte count */
7085 count = ipf->ipf_count;
7086 ret = ip_reassemble(mp, ipf,
7087 (frag_offset_flags & IPH_OFFSET) << 3,
7088 (frag_offset_flags & IPH_MF), ill, msg_len);
7089 /* Count of bytes added and subtracted (freeb()ed) */
7090 count = ipf->ipf_count - count;
7091 if (count) {
7092 /* Update per ipfb and ill byte counts */
7093 ipfb->ipfb_count += count;
7094 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7095 atomic_add_32(&ill->ill_frag_count, count);
7097 if (ret == IP_REASS_PARTIAL) {
7098 goto reass_done;
7099 } else if (ret == IP_REASS_FAILED) {
7100 /* Reassembly failed. Free up all resources */
7101 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7102 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7103 IP_REASS_SET_START(t_mp, 0);
7104 IP_REASS_SET_END(t_mp, 0);
7106 freemsg(mp);
7107 goto reass_done;
7109 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7112 * We have completed reassembly. Unhook the frag header from
7113 * the reassembly list.
7115 * Before we free the frag header, record the ECN info
7116 * to report back to the transport.
7118 ecn_info = ipf->ipf_ecn;
7119 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7120 ipfp = ipf->ipf_ptphn;
7122 /* We need to supply these to caller */
7123 if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7124 sum_val = ipf->ipf_checksum;
7125 else
7126 sum_val = 0;
7128 mp1 = ipf->ipf_mp;
7129 count = ipf->ipf_count;
7130 ipf = ipf->ipf_hash_next;
7131 if (ipf != NULL)
7132 ipf->ipf_ptphn = ipfp;
7133 ipfp[0] = ipf;
7134 atomic_add_32(&ill->ill_frag_count, -count);
7135 ASSERT(ipfb->ipfb_count >= count);
7136 ipfb->ipfb_count -= count;
7137 ipfb->ipfb_frag_pkts--;
7138 mutex_exit(&ipfb->ipfb_lock);
7139 /* Ditch the frag header. */
7140 mp = mp1->b_cont;
7142 freeb(mp1);
7144 /* Restore original IP length in header. */
7145 packet_size = (uint32_t)msgdsize(mp);
7146 if (packet_size > IP_MAXPACKET) {
7147 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7148 ip_drop_input("Reassembled packet too large", mp, ill);
7149 freemsg(mp);
7150 return (NULL);
7153 if (DB_REF(mp) > 1) {
7154 mblk_t *mp2 = copymsg(mp);
7156 if (mp2 == NULL) {
7157 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7158 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7159 freemsg(mp);
7160 return (NULL);
7162 freemsg(mp);
7163 mp = mp2;
7165 ipha = (ipha_t *)mp->b_rptr;
7167 ipha->ipha_length = htons((uint16_t)packet_size);
7168 /* We're now complete, zip the frag state */
7169 ipha->ipha_fragment_offset_and_flags = 0;
7170 /* Record the ECN info. */
7171 ipha->ipha_type_of_service &= 0xFC;
7172 ipha->ipha_type_of_service |= ecn_info;
7174 /* Update the receive attributes */
7175 ira->ira_pktlen = packet_size;
7176 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7178 /* Reassembly is successful; set checksum information in packet */
7179 DB_CKSUM16(mp) = (uint16_t)sum_val;
7180 DB_CKSUMFLAGS(mp) = sum_flags;
7181 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7183 return (mp);
7187 * Pullup function that should be used for IP input in order to
7188 * ensure we do not loose the L2 source address; we need the l2 source
7189 * address for IP_RECVSLLA and for ndp_input.
7191 * We return either NULL or b_rptr.
7193 void *
7194 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7196 ill_t *ill = ira->ira_ill;
7198 if (ip_rput_pullups++ == 0) {
7199 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7200 "ip_pullup: %s forced us to "
7201 " pullup pkt, hdr len %ld, hdr addr %p",
7202 ill->ill_name, len, (void *)mp->b_rptr);
7204 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7205 ip_setl2src(mp, ira, ira->ira_rill);
7206 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7207 if (!pullupmsg(mp, len))
7208 return (NULL);
7209 else
7210 return (mp->b_rptr);
7214 * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7215 * When called from the ULP ira_rill will be NULL hence the caller has to
7216 * pass in the ill.
7218 /* ARGSUSED */
7219 void
7220 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7222 const uchar_t *addr;
7223 int alen;
7225 if (ira->ira_flags & IRAF_L2SRC_SET)
7226 return;
7228 ASSERT(ill != NULL);
7229 alen = ill->ill_phys_addr_length;
7230 ASSERT(alen <= sizeof (ira->ira_l2src));
7231 if (ira->ira_mhip != NULL &&
7232 (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7233 bcopy(addr, ira->ira_l2src, alen);
7234 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7235 (addr = ill->ill_phys_addr) != NULL) {
7236 bcopy(addr, ira->ira_l2src, alen);
7237 } else {
7238 bzero(ira->ira_l2src, alen);
7240 ira->ira_flags |= IRAF_L2SRC_SET;
7244 * check ip header length and align it.
7246 mblk_t *
7247 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7249 ill_t *ill = ira->ira_ill;
7250 ssize_t len;
7252 len = MBLKL(mp);
7254 if (!OK_32PTR(mp->b_rptr))
7255 IP_STAT(ill->ill_ipst, ip_notaligned);
7256 else
7257 IP_STAT(ill->ill_ipst, ip_recv_pullup);
7259 /* Guard against bogus device drivers */
7260 if (len < 0) {
7261 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7262 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7263 freemsg(mp);
7264 return (NULL);
7267 if (len == 0) {
7268 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7269 mblk_t *mp1 = mp->b_cont;
7271 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7272 ip_setl2src(mp, ira, ira->ira_rill);
7273 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7275 freeb(mp);
7276 mp = mp1;
7277 if (mp == NULL)
7278 return (NULL);
7280 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7281 return (mp);
7283 if (ip_pullup(mp, min_size, ira) == NULL) {
7284 if (msgdsize(mp) < min_size) {
7285 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7286 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7287 } else {
7288 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7289 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7291 freemsg(mp);
7292 return (NULL);
7294 return (mp);
7298 * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7300 mblk_t *
7301 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7302 uint_t min_size, ip_recv_attr_t *ira)
7304 ill_t *ill = ira->ira_ill;
7307 * Make sure we have data length consistent
7308 * with the IP header.
7310 if (mp->b_cont == NULL) {
7311 /* pkt_len is based on ipha_len, not the mblk length */
7312 if (pkt_len < min_size) {
7313 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7314 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7315 freemsg(mp);
7316 return (NULL);
7318 if (len < 0) {
7319 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7320 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7321 freemsg(mp);
7322 return (NULL);
7324 /* Drop any pad */
7325 mp->b_wptr = rptr + pkt_len;
7326 } else if ((len += msgdsize(mp->b_cont)) != 0) {
7327 ASSERT(pkt_len >= min_size);
7328 if (pkt_len < min_size) {
7329 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7330 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7331 freemsg(mp);
7332 return (NULL);
7334 if (len < 0) {
7335 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7336 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7337 freemsg(mp);
7338 return (NULL);
7340 /* Drop any pad */
7341 (void) adjmsg(mp, -len);
7343 * adjmsg may have freed an mblk from the chain, hence
7344 * invalidate any hw checksum here. This will force IP to
7345 * calculate the checksum in sw, but only for this packet.
7347 DB_CKSUMFLAGS(mp) = 0;
7348 IP_STAT(ill->ill_ipst, ip_multimblk);
7350 return (mp);
7354 * Check that the IPv4 opt_len is consistent with the packet and pullup
7355 * the options.
7357 mblk_t *
7358 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7359 ip_recv_attr_t *ira)
7361 ill_t *ill = ira->ira_ill;
7362 ssize_t len;
7364 /* Assume no IPv6 packets arrive over the IPv4 queue */
7365 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7366 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7367 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7368 ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7369 freemsg(mp);
7370 return (NULL);
7373 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7374 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7375 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7376 freemsg(mp);
7377 return (NULL);
7380 * Recompute complete header length and make sure we
7381 * have access to all of it.
7383 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7384 if (len > (mp->b_wptr - mp->b_rptr)) {
7385 if (len > pkt_len) {
7386 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7387 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7388 freemsg(mp);
7389 return (NULL);
7391 if (ip_pullup(mp, len, ira) == NULL) {
7392 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7393 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7394 freemsg(mp);
7395 return (NULL);
7398 return (mp);
7402 * Returns a new ire, or the same ire, or NULL.
7403 * If a different IRE is returned, then it is held; the caller
7404 * needs to release it.
7405 * In no case is there any hold/release on the ire argument.
7407 ire_t *
7408 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7410 ire_t *new_ire;
7411 ill_t *ire_ill;
7412 uint_t ifindex;
7413 ip_stack_t *ipst = ill->ill_ipst;
7414 boolean_t strict_check = B_FALSE;
7417 * IPMP common case: if IRE and ILL are in the same group, there's no
7418 * issue (e.g. packet received on an underlying interface matched an
7419 * IRE_LOCAL on its associated group interface).
7421 ASSERT(ire->ire_ill != NULL);
7422 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7423 return (ire);
7426 * Do another ire lookup here, using the ingress ill, to see if the
7427 * interface is in a usesrc group.
7428 * As long as the ills belong to the same group, we don't consider
7429 * them to be arriving on the wrong interface. Thus, if the switch
7430 * is doing inbound load spreading, we won't drop packets when the
7431 * ip*_strict_dst_multihoming switch is on.
7432 * We also need to check for IPIF_UNNUMBERED point2point interfaces
7433 * where the local address may not be unique. In this case we were
7434 * at the mercy of the initial ire lookup and the IRE_LOCAL it
7435 * actually returned. The new lookup, which is more specific, should
7436 * only find the IRE_LOCAL associated with the ingress ill if one
7437 * exists.
7439 if (ire->ire_ipversion == IPV4_VERSION) {
7440 if (ipst->ips_ip_strict_dst_multihoming)
7441 strict_check = B_TRUE;
7442 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7443 IRE_LOCAL, ill, ALL_ZONES,
7444 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7445 } else {
7446 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7447 if (ipst->ips_ipv6_strict_dst_multihoming)
7448 strict_check = B_TRUE;
7449 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7450 IRE_LOCAL, ill, ALL_ZONES,
7451 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7454 * If the same ire that was returned in ip_input() is found then this
7455 * is an indication that usesrc groups are in use. The packet
7456 * arrived on a different ill in the group than the one associated with
7457 * the destination address. If a different ire was found then the same
7458 * IP address must be hosted on multiple ills. This is possible with
7459 * unnumbered point2point interfaces. We switch to use this new ire in
7460 * order to have accurate interface statistics.
7462 if (new_ire != NULL) {
7463 /* Note: held in one case but not the other? Caller handles */
7464 if (new_ire != ire)
7465 return (new_ire);
7466 /* Unchanged */
7467 ire_refrele(new_ire);
7468 return (ire);
7472 * Chase pointers once and store locally.
7474 ASSERT(ire->ire_ill != NULL);
7475 ire_ill = ire->ire_ill;
7476 ifindex = ill->ill_usesrc_ifindex;
7479 * Check if it's a legal address on the 'usesrc' interface.
7480 * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7481 * can just check phyint_ifindex.
7483 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7484 return (ire);
7488 * If the ip*_strict_dst_multihoming switch is on then we can
7489 * only accept this packet if the interface is marked as routing.
7491 if (!(strict_check))
7492 return (ire);
7494 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7495 return (ire);
7497 return (NULL);
7501 * This function is used to construct a mac_header_info_s from a
7502 * DL_UNITDATA_IND message.
7503 * The address fields in the mhi structure points into the message,
7504 * thus the caller can't use those fields after freeing the message.
7506 * We determine whether the packet received is a non-unicast packet
7507 * and in doing so, determine whether or not it is broadcast vs multicast.
7508 * For it to be a broadcast packet, we must have the appropriate mblk_t
7509 * hanging off the ill_t. If this is either not present or doesn't match
7510 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7511 * to be multicast. Thus NICs that have no broadcast address (or no
7512 * capability for one, such as point to point links) cannot return as
7513 * the packet being broadcast.
7515 void
7516 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7518 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7519 mblk_t *bmp;
7520 uint_t extra_offset;
7522 bzero(mhip, sizeof (struct mac_header_info_s));
7524 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7526 if (ill->ill_sap_length < 0)
7527 extra_offset = 0;
7528 else
7529 extra_offset = ill->ill_sap_length;
7531 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7532 extra_offset;
7533 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7534 extra_offset;
7536 if (!ind->dl_group_address)
7537 return;
7539 /* Multicast or broadcast */
7540 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7542 if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7543 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7544 (bmp = ill->ill_bcast_mp) != NULL) {
7545 dl_unitdata_req_t *dlur;
7546 uint8_t *bphys_addr;
7548 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7549 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7550 extra_offset;
7552 if (bcmp(mhip->mhi_daddr, bphys_addr,
7553 ind->dl_dest_addr_length) == 0)
7554 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7559 * This function is used to construct a mac_header_info_s from a
7560 * M_DATA fastpath message from a DLPI driver.
7561 * The address fields in the mhi structure points into the message,
7562 * thus the caller can't use those fields after freeing the message.
7564 * We determine whether the packet received is a non-unicast packet
7565 * and in doing so, determine whether or not it is broadcast vs multicast.
7566 * For it to be a broadcast packet, we must have the appropriate mblk_t
7567 * hanging off the ill_t. If this is either not present or doesn't match
7568 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7569 * to be multicast. Thus NICs that have no broadcast address (or no
7570 * capability for one, such as point to point links) cannot return as
7571 * the packet being broadcast.
7573 void
7574 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7576 mblk_t *bmp;
7577 struct ether_header *pether;
7579 bzero(mhip, sizeof (struct mac_header_info_s));
7581 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7583 pether = (struct ether_header *)((char *)mp->b_rptr
7584 - sizeof (struct ether_header));
7587 * Make sure the interface is an ethernet type, since we don't
7588 * know the header format for anything but Ethernet. Also make
7589 * sure we are pointing correctly above db_base.
7591 if (ill->ill_type != IFT_ETHER)
7592 return;
7594 retry:
7595 if ((uchar_t *)pether < mp->b_datap->db_base)
7596 return;
7598 /* Is there a VLAN tag? */
7599 if (ill->ill_isv6) {
7600 if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7601 pether = (struct ether_header *)((char *)pether - 4);
7602 goto retry;
7604 } else {
7605 if (pether->ether_type != htons(ETHERTYPE_IP)) {
7606 pether = (struct ether_header *)((char *)pether - 4);
7607 goto retry;
7610 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7611 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7613 if (!(mhip->mhi_daddr[0] & 0x01))
7614 return;
7616 /* Multicast or broadcast */
7617 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7619 if ((bmp = ill->ill_bcast_mp) != NULL) {
7620 dl_unitdata_req_t *dlur;
7621 uint8_t *bphys_addr;
7622 uint_t addrlen;
7624 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7625 addrlen = dlur->dl_dest_addr_length;
7626 if (ill->ill_sap_length < 0) {
7627 bphys_addr = (uchar_t *)dlur +
7628 dlur->dl_dest_addr_offset;
7629 addrlen += ill->ill_sap_length;
7630 } else {
7631 bphys_addr = (uchar_t *)dlur +
7632 dlur->dl_dest_addr_offset +
7633 ill->ill_sap_length;
7634 addrlen -= ill->ill_sap_length;
7636 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7637 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7642 * Handle anything but M_DATA messages
7643 * We see the DL_UNITDATA_IND which are part
7644 * of the data path, and also the other messages from the driver.
7646 void
7647 ip_rput_notdata(ill_t *ill, mblk_t *mp)
7649 mblk_t *first_mp;
7650 struct iocblk *iocp;
7651 struct mac_header_info_s mhi;
7653 switch (DB_TYPE(mp)) {
7654 case M_PROTO:
7655 case M_PCPROTO: {
7656 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7657 DL_UNITDATA_IND) {
7658 /* Go handle anything other than data elsewhere. */
7659 ip_rput_dlpi(ill, mp);
7660 return;
7663 first_mp = mp;
7664 mp = first_mp->b_cont;
7665 first_mp->b_cont = NULL;
7667 if (mp == NULL) {
7668 freeb(first_mp);
7669 return;
7671 ip_dlur_to_mhi(ill, first_mp, &mhi);
7672 if (ill->ill_isv6)
7673 ip_input_v6(ill, NULL, mp, &mhi);
7674 else
7675 ip_input(ill, NULL, mp, &mhi);
7677 /* Ditch the DLPI header. */
7678 freeb(first_mp);
7679 return;
7681 case M_IOCACK:
7682 iocp = (struct iocblk *)mp->b_rptr;
7683 switch (iocp->ioc_cmd) {
7684 case DL_IOC_HDR_INFO:
7685 ill_fastpath_ack(ill, mp);
7686 return;
7687 default:
7688 putnext(ill->ill_rq, mp);
7689 return;
7691 /* FALLTHROUGH */
7692 case M_ERROR:
7693 case M_HANGUP:
7694 mutex_enter(&ill->ill_lock);
7695 if (ill->ill_state_flags & ILL_CONDEMNED) {
7696 mutex_exit(&ill->ill_lock);
7697 freemsg(mp);
7698 return;
7700 ill_refhold_locked(ill);
7701 mutex_exit(&ill->ill_lock);
7702 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7703 B_FALSE);
7704 return;
7705 case M_CTL:
7706 putnext(ill->ill_rq, mp);
7707 return;
7708 case M_IOCNAK:
7709 ip1dbg(("got iocnak "));
7710 iocp = (struct iocblk *)mp->b_rptr;
7711 switch (iocp->ioc_cmd) {
7712 case DL_IOC_HDR_INFO:
7713 ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7714 return;
7715 default:
7716 break;
7718 /* FALLTHROUGH */
7719 default:
7720 putnext(ill->ill_rq, mp);
7721 return;
7725 /* Read side put procedure. Packets coming from the wire arrive here. */
7727 ip_rput(queue_t *q, mblk_t *mp)
7729 ill_t *ill;
7730 union DL_primitives *dl;
7732 ill = (ill_t *)q->q_ptr;
7734 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
7736 * If things are opening or closing, only accept high-priority
7737 * DLPI messages. (On open ill->ill_ipif has not yet been
7738 * created; on close, things hanging off the ill may have been
7739 * freed already.)
7741 dl = (union DL_primitives *)mp->b_rptr;
7742 if (DB_TYPE(mp) != M_PCPROTO ||
7743 dl->dl_primitive == DL_UNITDATA_IND) {
7744 inet_freemsg(mp);
7745 return (0);
7748 if (DB_TYPE(mp) == M_DATA) {
7749 struct mac_header_info_s mhi;
7751 ip_mdata_to_mhi(ill, mp, &mhi);
7752 ip_input(ill, NULL, mp, &mhi);
7753 } else {
7754 ip_rput_notdata(ill, mp);
7756 return (0);
7760 * Move the information to a copy.
7762 mblk_t *
7763 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
7765 mblk_t *mp1;
7766 ill_t *ill = ira->ira_ill;
7767 ip_stack_t *ipst = ill->ill_ipst;
7769 IP_STAT(ipst, ip_db_ref);
7771 /* Make sure we have ira_l2src before we loose the original mblk */
7772 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7773 ip_setl2src(mp, ira, ira->ira_rill);
7775 mp1 = copymsg(mp);
7776 if (mp1 == NULL) {
7777 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7778 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7779 freemsg(mp);
7780 return (NULL);
7782 /* preserve the hardware checksum flags and data, if present */
7783 if (DB_CKSUMFLAGS(mp) != 0) {
7784 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
7785 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
7786 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
7787 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
7788 DB_CKSUM16(mp1) = DB_CKSUM16(mp);
7790 freemsg(mp);
7791 return (mp1);
7794 static void
7795 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
7796 t_uscalar_t err)
7798 if (dl_err == DL_SYSERR) {
7799 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
7800 "%s: %s failed: DL_SYSERR (errno %u)\n",
7801 ill->ill_name, dl_primstr(prim), err);
7802 return;
7805 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
7806 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
7807 dl_errstr(dl_err));
7811 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
7812 * than DL_UNITDATA_IND messages. If we need to process this message
7813 * exclusively, we call qwriter_ip, in which case we also need to call
7814 * ill_refhold before that, since qwriter_ip does an ill_refrele.
7816 void
7817 ip_rput_dlpi(ill_t *ill, mblk_t *mp)
7819 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
7820 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
7821 queue_t *q = ill->ill_rq;
7822 t_uscalar_t prim = dloa->dl_primitive;
7823 t_uscalar_t reqprim = DL_PRIM_INVAL;
7825 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
7826 char *, dl_primstr(prim), ill_t *, ill);
7827 ip1dbg(("ip_rput_dlpi"));
7830 * If we received an ACK but didn't send a request for it, then it
7831 * can't be part of any pending operation; discard up-front.
7833 switch (prim) {
7834 case DL_ERROR_ACK:
7835 reqprim = dlea->dl_error_primitive;
7836 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
7837 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
7838 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
7839 dlea->dl_unix_errno));
7840 break;
7841 case DL_OK_ACK:
7842 reqprim = dloa->dl_correct_primitive;
7843 break;
7844 case DL_INFO_ACK:
7845 reqprim = DL_INFO_REQ;
7846 break;
7847 case DL_BIND_ACK:
7848 reqprim = DL_BIND_REQ;
7849 break;
7850 case DL_PHYS_ADDR_ACK:
7851 reqprim = DL_PHYS_ADDR_REQ;
7852 break;
7853 case DL_NOTIFY_ACK:
7854 reqprim = DL_NOTIFY_REQ;
7855 break;
7856 case DL_CAPABILITY_ACK:
7857 reqprim = DL_CAPABILITY_REQ;
7858 break;
7861 if (prim != DL_NOTIFY_IND) {
7862 if (reqprim == DL_PRIM_INVAL ||
7863 !ill_dlpi_pending(ill, reqprim)) {
7864 /* Not a DLPI message we support or expected */
7865 freemsg(mp);
7866 return;
7868 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
7869 dl_primstr(reqprim)));
7872 switch (reqprim) {
7873 case DL_UNBIND_REQ:
7875 * NOTE: we mark the unbind as complete even if we got a
7876 * DL_ERROR_ACK, since there's not much else we can do.
7878 mutex_enter(&ill->ill_lock);
7879 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
7880 cv_signal(&ill->ill_cv);
7881 mutex_exit(&ill->ill_lock);
7882 break;
7884 case DL_ENABMULTI_REQ:
7885 if (prim == DL_OK_ACK) {
7886 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
7887 ill->ill_dlpi_multicast_state = IDS_OK;
7889 break;
7893 * The message is one we're waiting for (or DL_NOTIFY_IND), but we
7894 * need to become writer to continue to process it. Because an
7895 * exclusive operation doesn't complete until replies to all queued
7896 * DLPI messages have been received, we know we're in the middle of an
7897 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
7899 * As required by qwriter_ip(), we refhold the ill; it will refrele.
7900 * Since this is on the ill stream we unconditionally bump up the
7901 * refcount without doing ILL_CAN_LOOKUP().
7903 ill_refhold(ill);
7904 if (prim == DL_NOTIFY_IND)
7905 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
7906 else
7907 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
7911 * Handling of DLPI messages that require exclusive access to the ipsq.
7913 * Need to do ipsq_pending_mp_get on ioctl completion, which could
7914 * happen here. (along with mi_copy_done)
7916 /* ARGSUSED */
7917 static void
7918 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
7920 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
7921 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
7922 int err = 0;
7923 ill_t *ill = (ill_t *)q->q_ptr;
7924 ipif_t *ipif = NULL;
7925 mblk_t *mp1 = NULL;
7926 conn_t *connp = NULL;
7927 t_uscalar_t paddrreq;
7928 mblk_t *mp_hw;
7929 boolean_t success;
7930 boolean_t ioctl_aborted = B_FALSE;
7931 boolean_t log = B_TRUE;
7933 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
7934 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
7936 ip1dbg(("ip_rput_dlpi_writer .."));
7937 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
7938 ASSERT(IAM_WRITER_ILL(ill));
7940 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
7942 * The current ioctl could have been aborted by the user and a new
7943 * ioctl to bring up another ill could have started. We could still
7944 * get a response from the driver later.
7946 if (ipif != NULL && ipif->ipif_ill != ill)
7947 ioctl_aborted = B_TRUE;
7949 switch (dloa->dl_primitive) {
7950 case DL_ERROR_ACK:
7951 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
7952 dl_primstr(dlea->dl_error_primitive)));
7954 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
7955 char *, dl_primstr(dlea->dl_error_primitive),
7956 ill_t *, ill);
7958 switch (dlea->dl_error_primitive) {
7959 case DL_DISABMULTI_REQ:
7960 ill_dlpi_done(ill, dlea->dl_error_primitive);
7961 break;
7962 case DL_PROMISCON_REQ:
7963 case DL_PROMISCOFF_REQ:
7964 case DL_UNBIND_REQ:
7965 case DL_ATTACH_REQ:
7966 case DL_INFO_REQ:
7967 ill_dlpi_done(ill, dlea->dl_error_primitive);
7968 break;
7969 case DL_NOTIFY_REQ:
7970 ill_dlpi_done(ill, DL_NOTIFY_REQ);
7971 log = B_FALSE;
7972 break;
7973 case DL_PHYS_ADDR_REQ:
7975 * For IPv6 only, there are two additional
7976 * phys_addr_req's sent to the driver to get the
7977 * IPv6 token and lla. This allows IP to acquire
7978 * the hardware address format for a given interface
7979 * without having built in knowledge of the hardware
7980 * address. ill_phys_addr_pend keeps track of the last
7981 * DL_PAR sent so we know which response we are
7982 * dealing with. ill_dlpi_done will update
7983 * ill_phys_addr_pend when it sends the next req.
7984 * We don't complete the IOCTL until all three DL_PARs
7985 * have been attempted, so set *_len to 0 and break.
7987 paddrreq = ill->ill_phys_addr_pend;
7988 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
7989 if (paddrreq == DL_IPV6_TOKEN) {
7990 ill->ill_token_length = 0;
7991 log = B_FALSE;
7992 break;
7993 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
7994 ill->ill_nd_lla_len = 0;
7995 log = B_FALSE;
7996 break;
7999 * Something went wrong with the DL_PHYS_ADDR_REQ.
8000 * We presumably have an IOCTL hanging out waiting
8001 * for completion. Find it and complete the IOCTL
8002 * with the error noted.
8003 * However, ill_dl_phys was called on an ill queue
8004 * (from SIOCSLIFNAME), thus conn_pending_ill is not
8005 * set. But the ioctl is known to be pending on ill_wq.
8007 if (!ill->ill_ifname_pending)
8008 break;
8009 ill->ill_ifname_pending = 0;
8010 if (!ioctl_aborted)
8011 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8012 if (mp1 != NULL) {
8014 * This operation (SIOCSLIFNAME) must have
8015 * happened on the ill. Assert there is no conn
8017 ASSERT(connp == NULL);
8018 q = ill->ill_wq;
8020 break;
8021 case DL_BIND_REQ:
8022 ill_dlpi_done(ill, DL_BIND_REQ);
8023 if (ill->ill_ifname_pending)
8024 break;
8025 mutex_enter(&ill->ill_lock);
8026 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8027 mutex_exit(&ill->ill_lock);
8029 * Something went wrong with the bind. We presumably
8030 * have an IOCTL hanging out waiting for completion.
8031 * Find it, take down the interface that was coming
8032 * up, and complete the IOCTL with the error noted.
8034 if (!ioctl_aborted)
8035 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8036 if (mp1 != NULL) {
8038 * This might be a result of a DL_NOTE_REPLUMB
8039 * notification. In that case, connp is NULL.
8041 if (connp != NULL)
8042 q = CONNP_TO_WQ(connp);
8044 (void) ipif_down(ipif, NULL, NULL);
8045 /* error is set below the switch */
8047 break;
8048 case DL_ENABMULTI_REQ:
8049 ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8051 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8052 ill->ill_dlpi_multicast_state = IDS_FAILED;
8053 if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8055 printf("ip: joining multicasts failed (%d)"
8056 " on %s - will use link layer "
8057 "broadcasts for multicast\n",
8058 dlea->dl_errno, ill->ill_name);
8061 * Set up for multi_bcast; We are the
8062 * writer, so ok to access ill->ill_ipif
8063 * without any lock.
8065 mutex_enter(&ill->ill_phyint->phyint_lock);
8066 ill->ill_phyint->phyint_flags |=
8067 PHYI_MULTI_BCAST;
8068 mutex_exit(&ill->ill_phyint->phyint_lock);
8071 freemsg(mp); /* Don't want to pass this up */
8072 return;
8073 case DL_CAPABILITY_REQ:
8074 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8075 "DL_CAPABILITY REQ\n"));
8076 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8077 ill->ill_dlpi_capab_state = IDCS_FAILED;
8078 ill_capability_done(ill);
8079 freemsg(mp);
8080 return;
8083 * Note the error for IOCTL completion (mp1 is set when
8084 * ready to complete ioctl). If ill_ifname_pending_err is
8085 * set, an error occured during plumbing (ill_ifname_pending),
8086 * so we want to report that error.
8088 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8089 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8090 * expected to get errack'd if the driver doesn't support
8091 * these flags (e.g. ethernet). log will be set to B_FALSE
8092 * if these error conditions are encountered.
8094 if (mp1 != NULL) {
8095 if (ill->ill_ifname_pending_err != 0) {
8096 err = ill->ill_ifname_pending_err;
8097 ill->ill_ifname_pending_err = 0;
8098 } else {
8099 err = dlea->dl_unix_errno ?
8100 dlea->dl_unix_errno : ENXIO;
8103 * If we're plumbing an interface and an error hasn't already
8104 * been saved, set ill_ifname_pending_err to the error passed
8105 * up. Ignore the error if log is B_FALSE (see comment above).
8107 } else if (log && ill->ill_ifname_pending &&
8108 ill->ill_ifname_pending_err == 0) {
8109 ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8110 dlea->dl_unix_errno : ENXIO;
8113 if (log)
8114 ip_dlpi_error(ill, dlea->dl_error_primitive,
8115 dlea->dl_errno, dlea->dl_unix_errno);
8116 break;
8117 case DL_CAPABILITY_ACK:
8118 ill_capability_ack(ill, mp);
8120 * The message has been handed off to ill_capability_ack
8121 * and must not be freed below
8123 mp = NULL;
8124 break;
8126 case DL_INFO_ACK:
8127 /* Call a routine to handle this one. */
8128 ill_dlpi_done(ill, DL_INFO_REQ);
8129 ip_ll_subnet_defaults(ill, mp);
8130 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8131 return;
8132 case DL_BIND_ACK:
8134 * We should have an IOCTL waiting on this unless
8135 * sent by ill_dl_phys, in which case just return
8137 ill_dlpi_done(ill, DL_BIND_REQ);
8139 if (ill->ill_ifname_pending) {
8140 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8141 ill_t *, ill, mblk_t *, mp);
8142 break;
8144 mutex_enter(&ill->ill_lock);
8145 ill->ill_dl_up = 1;
8146 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8147 mutex_exit(&ill->ill_lock);
8149 if (!ioctl_aborted)
8150 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8151 if (mp1 == NULL) {
8152 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8153 break;
8156 * mp1 was added by ill_dl_up(). if that is a result of
8157 * a DL_NOTE_REPLUMB notification, connp could be NULL.
8159 if (connp != NULL)
8160 q = CONNP_TO_WQ(connp);
8162 * We are exclusive. So nothing can change even after
8163 * we get the pending mp.
8165 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8166 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8167 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8170 * Now bring up the resolver; when that is complete, we'll
8171 * create IREs. Note that we intentionally mirror what
8172 * ipif_up() would have done, because we got here by way of
8173 * ill_dl_up(), which stopped ipif_up()'s processing.
8175 if (ill->ill_isv6) {
8177 * v6 interfaces.
8178 * Unlike ARP which has to do another bind
8179 * and attach, once we get here we are
8180 * done with NDP
8182 (void) ipif_resolver_up(ipif, Res_act_initial);
8183 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8184 err = ipif_up_done_v6(ipif);
8185 } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8187 * ARP and other v4 external resolvers.
8188 * Leave the pending mblk intact so that
8189 * the ioctl completes in ip_rput().
8191 if (connp != NULL)
8192 mutex_enter(&connp->conn_lock);
8193 mutex_enter(&ill->ill_lock);
8194 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8195 mutex_exit(&ill->ill_lock);
8196 if (connp != NULL)
8197 mutex_exit(&connp->conn_lock);
8198 if (success) {
8199 err = ipif_resolver_up(ipif, Res_act_initial);
8200 if (err == EINPROGRESS) {
8201 freemsg(mp);
8202 return;
8204 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8205 } else {
8206 /* The conn has started closing */
8207 err = EINTR;
8209 } else {
8211 * This one is complete. Reply to pending ioctl.
8213 (void) ipif_resolver_up(ipif, Res_act_initial);
8214 err = ipif_up_done(ipif);
8217 if ((err == 0) && (ill->ill_up_ipifs)) {
8218 err = ill_up_ipifs(ill, q, mp1);
8219 if (err == EINPROGRESS) {
8220 freemsg(mp);
8221 return;
8226 * If we have a moved ipif to bring up, and everything has
8227 * succeeded to this point, bring it up on the IPMP ill.
8228 * Otherwise, leave it down -- the admin can try to bring it
8229 * up by hand if need be.
8231 if (ill->ill_move_ipif != NULL) {
8232 if (err != 0) {
8233 ill->ill_move_ipif = NULL;
8234 } else {
8235 ipif = ill->ill_move_ipif;
8236 ill->ill_move_ipif = NULL;
8237 err = ipif_up(ipif, q, mp1);
8238 if (err == EINPROGRESS) {
8239 freemsg(mp);
8240 return;
8244 break;
8246 case DL_NOTIFY_IND: {
8247 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8248 uint_t orig_mtu, orig_mc_mtu;
8250 switch (notify->dl_notification) {
8251 case DL_NOTE_PHYS_ADDR:
8252 err = ill_set_phys_addr(ill, mp);
8253 break;
8255 case DL_NOTE_REPLUMB:
8257 * Directly return after calling ill_replumb().
8258 * Note that we should not free mp as it is reused
8259 * in the ill_replumb() function.
8261 err = ill_replumb(ill, mp);
8262 return;
8264 case DL_NOTE_FASTPATH_FLUSH:
8265 nce_flush(ill, B_FALSE);
8266 break;
8268 case DL_NOTE_SDU_SIZE:
8269 case DL_NOTE_SDU_SIZE2:
8271 * The dce and fragmentation code can cope with
8272 * this changing while packets are being sent.
8273 * When packets are sent ip_output will discover
8274 * a change.
8276 * Change the MTU size of the interface.
8278 mutex_enter(&ill->ill_lock);
8279 orig_mtu = ill->ill_mtu;
8280 orig_mc_mtu = ill->ill_mc_mtu;
8281 switch (notify->dl_notification) {
8282 case DL_NOTE_SDU_SIZE:
8283 ill->ill_current_frag =
8284 (uint_t)notify->dl_data;
8285 ill->ill_mc_mtu = (uint_t)notify->dl_data;
8286 break;
8287 case DL_NOTE_SDU_SIZE2:
8288 ill->ill_current_frag =
8289 (uint_t)notify->dl_data1;
8290 ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8291 break;
8293 if (ill->ill_current_frag > ill->ill_max_frag)
8294 ill->ill_max_frag = ill->ill_current_frag;
8296 if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8297 ill->ill_mtu = ill->ill_current_frag;
8300 * If ill_user_mtu was set (via
8301 * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8303 if (ill->ill_user_mtu != 0 &&
8304 ill->ill_user_mtu < ill->ill_mtu)
8305 ill->ill_mtu = ill->ill_user_mtu;
8307 if (ill->ill_user_mtu != 0 &&
8308 ill->ill_user_mtu < ill->ill_mc_mtu)
8309 ill->ill_mc_mtu = ill->ill_user_mtu;
8311 if (ill->ill_isv6) {
8312 if (ill->ill_mtu < IPV6_MIN_MTU)
8313 ill->ill_mtu = IPV6_MIN_MTU;
8314 if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8315 ill->ill_mc_mtu = IPV6_MIN_MTU;
8316 } else {
8317 if (ill->ill_mtu < IP_MIN_MTU)
8318 ill->ill_mtu = IP_MIN_MTU;
8319 if (ill->ill_mc_mtu < IP_MIN_MTU)
8320 ill->ill_mc_mtu = IP_MIN_MTU;
8322 } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8323 ill->ill_mc_mtu = ill->ill_mtu;
8326 mutex_exit(&ill->ill_lock);
8328 * Make sure all dce_generation checks find out
8329 * that ill_mtu/ill_mc_mtu has changed.
8331 if (orig_mtu != ill->ill_mtu ||
8332 orig_mc_mtu != ill->ill_mc_mtu) {
8333 dce_increment_all_generations(ill->ill_isv6,
8334 ill->ill_ipst);
8338 * Refresh IPMP meta-interface MTU if necessary.
8340 if (IS_UNDER_IPMP(ill))
8341 ipmp_illgrp_refresh_mtu(ill->ill_grp);
8342 break;
8344 case DL_NOTE_LINK_UP:
8345 case DL_NOTE_LINK_DOWN: {
8347 * We are writer. ill / phyint / ipsq assocs stable.
8348 * The RUNNING flag reflects the state of the link.
8350 phyint_t *phyint = ill->ill_phyint;
8351 uint64_t new_phyint_flags;
8352 boolean_t changed = B_FALSE;
8353 boolean_t went_up;
8355 went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8356 mutex_enter(&phyint->phyint_lock);
8358 new_phyint_flags = went_up ?
8359 phyint->phyint_flags | PHYI_RUNNING :
8360 phyint->phyint_flags & ~PHYI_RUNNING;
8362 if (IS_IPMP(ill)) {
8363 new_phyint_flags = went_up ?
8364 new_phyint_flags & ~PHYI_FAILED :
8365 new_phyint_flags | PHYI_FAILED;
8368 if (new_phyint_flags != phyint->phyint_flags) {
8369 phyint->phyint_flags = new_phyint_flags;
8370 changed = B_TRUE;
8372 mutex_exit(&phyint->phyint_lock);
8374 * ill_restart_dad handles the DAD restart and routing
8375 * socket notification logic.
8377 if (changed) {
8378 ill_restart_dad(phyint->phyint_illv4, went_up);
8379 ill_restart_dad(phyint->phyint_illv6, went_up);
8381 break;
8383 case DL_NOTE_PROMISC_ON_PHYS: {
8384 phyint_t *phyint = ill->ill_phyint;
8386 mutex_enter(&phyint->phyint_lock);
8387 phyint->phyint_flags |= PHYI_PROMISC;
8388 mutex_exit(&phyint->phyint_lock);
8389 break;
8391 case DL_NOTE_PROMISC_OFF_PHYS: {
8392 phyint_t *phyint = ill->ill_phyint;
8394 mutex_enter(&phyint->phyint_lock);
8395 phyint->phyint_flags &= ~PHYI_PROMISC;
8396 mutex_exit(&phyint->phyint_lock);
8397 break;
8399 case DL_NOTE_CAPAB_RENEG:
8401 * Something changed on the driver side.
8402 * It wants us to renegotiate the capabilities
8403 * on this ill. One possible cause is the aggregation
8404 * interface under us where a port got added or
8405 * went away.
8407 * If the capability negotiation is already done
8408 * or is in progress, reset the capabilities and
8409 * mark the ill's ill_capab_reneg to be B_TRUE,
8410 * so that when the ack comes back, we can start
8411 * the renegotiation process.
8413 * Note that if ill_capab_reneg is already B_TRUE
8414 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8415 * the capability resetting request has been sent
8416 * and the renegotiation has not been started yet;
8417 * nothing needs to be done in this case.
8419 ipsq_current_start(ipsq, ill->ill_ipif, 0);
8420 ill_capability_reset(ill, B_TRUE);
8421 ipsq_current_finish(ipsq);
8422 break;
8424 case DL_NOTE_ALLOWED_IPS:
8425 ill_set_allowed_ips(ill, mp);
8426 break;
8427 default:
8428 ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8429 "type 0x%x for DL_NOTIFY_IND\n",
8430 notify->dl_notification));
8431 break;
8435 * As this is an asynchronous operation, we
8436 * should not call ill_dlpi_done
8438 break;
8440 case DL_NOTIFY_ACK: {
8441 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8443 if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8444 ill->ill_note_link = 1;
8445 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8446 break;
8448 case DL_PHYS_ADDR_ACK: {
8450 * As part of plumbing the interface via SIOCSLIFNAME,
8451 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8452 * whose answers we receive here. As each answer is received,
8453 * we call ill_dlpi_done() to dispatch the next request as
8454 * we're processing the current one. Once all answers have
8455 * been received, we use ipsq_pending_mp_get() to dequeue the
8456 * outstanding IOCTL and reply to it. (Because ill_dl_phys()
8457 * is invoked from an ill queue, conn_oper_pending_ill is not
8458 * available, but we know the ioctl is pending on ill_wq.)
8460 uint_t paddrlen, paddroff;
8461 uint8_t *addr;
8463 paddrreq = ill->ill_phys_addr_pend;
8464 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8465 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8466 addr = mp->b_rptr + paddroff;
8468 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8469 if (paddrreq == DL_IPV6_TOKEN) {
8471 * bcopy to low-order bits of ill_token
8473 * XXX Temporary hack - currently, all known tokens
8474 * are 64 bits, so I'll cheat for the moment.
8476 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8477 ill->ill_token_length = paddrlen;
8478 break;
8479 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8480 ASSERT(ill->ill_nd_lla_mp == NULL);
8481 ill_set_ndmp(ill, mp, paddroff, paddrlen);
8482 mp = NULL;
8483 break;
8484 } else if (paddrreq == DL_CURR_DEST_ADDR) {
8485 ASSERT(ill->ill_dest_addr_mp == NULL);
8486 ill->ill_dest_addr_mp = mp;
8487 ill->ill_dest_addr = addr;
8488 mp = NULL;
8489 if (ill->ill_isv6) {
8490 ill_setdesttoken(ill);
8491 ipif_setdestlinklocal(ill->ill_ipif);
8493 break;
8496 ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8497 ASSERT(ill->ill_phys_addr_mp == NULL);
8498 if (!ill->ill_ifname_pending)
8499 break;
8500 ill->ill_ifname_pending = 0;
8501 if (!ioctl_aborted)
8502 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8503 if (mp1 != NULL) {
8504 ASSERT(connp == NULL);
8505 q = ill->ill_wq;
8508 * If any error acks received during the plumbing sequence,
8509 * ill_ifname_pending_err will be set. Break out and send up
8510 * the error to the pending ioctl.
8512 if (ill->ill_ifname_pending_err != 0) {
8513 err = ill->ill_ifname_pending_err;
8514 ill->ill_ifname_pending_err = 0;
8515 break;
8518 ill->ill_phys_addr_mp = mp;
8519 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8520 mp = NULL;
8523 * If paddrlen or ill_phys_addr_length is zero, the DLPI
8524 * provider doesn't support physical addresses. We check both
8525 * paddrlen and ill_phys_addr_length because sppp (PPP) does
8526 * not have physical addresses, but historically adversises a
8527 * physical address length of 0 in its DL_INFO_ACK, but 6 in
8528 * its DL_PHYS_ADDR_ACK.
8530 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8531 ill->ill_phys_addr = NULL;
8532 } else if (paddrlen != ill->ill_phys_addr_length) {
8533 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8534 paddrlen, ill->ill_phys_addr_length));
8535 err = EINVAL;
8536 break;
8539 if (ill->ill_nd_lla_mp == NULL) {
8540 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8541 err = ENOMEM;
8542 break;
8544 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8547 if (ill->ill_isv6) {
8548 ill_setdefaulttoken(ill);
8549 ipif_setlinklocal(ill->ill_ipif);
8551 break;
8553 case DL_OK_ACK:
8554 ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8555 dl_primstr((int)dloa->dl_correct_primitive),
8556 dloa->dl_correct_primitive));
8557 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8558 char *, dl_primstr(dloa->dl_correct_primitive),
8559 ill_t *, ill);
8561 switch (dloa->dl_correct_primitive) {
8562 case DL_ENABMULTI_REQ:
8563 case DL_DISABMULTI_REQ:
8564 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8565 break;
8566 case DL_PROMISCON_REQ:
8567 case DL_PROMISCOFF_REQ:
8568 case DL_UNBIND_REQ:
8569 case DL_ATTACH_REQ:
8570 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8571 break;
8573 break;
8574 default:
8575 break;
8578 freemsg(mp);
8579 if (mp1 == NULL)
8580 return;
8583 * The operation must complete without EINPROGRESS since
8584 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise,
8585 * the operation will be stuck forever inside the IPSQ.
8587 ASSERT(err != EINPROGRESS);
8589 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8590 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8591 ipif_t *, NULL);
8593 switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8594 case 0:
8595 ipsq_current_finish(ipsq);
8596 break;
8598 case SIOCSLIFNAME:
8599 case IF_UNITSEL: {
8600 ill_t *ill_other = ILL_OTHER(ill);
8603 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8604 * ill has a peer which is in an IPMP group, then place ill
8605 * into the same group. One catch: although ifconfig plumbs
8606 * the appropriate IPMP meta-interface prior to plumbing this
8607 * ill, it is possible for multiple ifconfig applications to
8608 * race (or for another application to adjust plumbing), in
8609 * which case the IPMP meta-interface we need will be missing.
8610 * If so, kick the phyint out of the group.
8612 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8613 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp;
8614 ipmp_illgrp_t *illg;
8616 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8617 if (illg == NULL)
8618 ipmp_phyint_leave_grp(ill->ill_phyint);
8619 else
8620 ipmp_ill_join_illgrp(ill, illg);
8623 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8624 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8625 else
8626 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8627 break;
8629 case SIOCLIFADDIF:
8630 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8631 break;
8633 default:
8634 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8635 break;
8640 * ip_rput_other is called by ip_rput to handle messages modifying the global
8641 * state in IP. If 'ipsq' is non-NULL, caller is writer on it.
8643 /* ARGSUSED */
8644 void
8645 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8647 ill_t *ill = q->q_ptr;
8648 struct iocblk *iocp;
8650 ip1dbg(("ip_rput_other "));
8651 if (ipsq != NULL) {
8652 ASSERT(IAM_WRITER_IPSQ(ipsq));
8653 ASSERT(ipsq->ipsq_xop ==
8654 ill->ill_phyint->phyint_ipsq->ipsq_xop);
8657 switch (mp->b_datap->db_type) {
8658 case M_ERROR:
8659 case M_HANGUP:
8661 * The device has a problem. We force the ILL down. It can
8662 * be brought up again manually using SIOCSIFFLAGS (via
8663 * ifconfig or equivalent).
8665 ASSERT(ipsq != NULL);
8666 if (mp->b_rptr < mp->b_wptr)
8667 ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8668 if (ill->ill_error == 0)
8669 ill->ill_error = ENXIO;
8670 if (!ill_down_start(q, mp))
8671 return;
8672 ipif_all_down_tail(ipsq, q, mp, NULL);
8673 break;
8674 case M_IOCNAK: {
8675 iocp = (struct iocblk *)mp->b_rptr;
8677 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8679 * If this was the first attempt, turn off the fastpath
8680 * probing.
8682 mutex_enter(&ill->ill_lock);
8683 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8684 ill->ill_dlpi_fastpath_state = IDS_FAILED;
8685 mutex_exit(&ill->ill_lock);
8687 * don't flush the nce_t entries: we use them
8688 * as an index to the ncec itself.
8690 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8691 ill->ill_name));
8692 } else {
8693 mutex_exit(&ill->ill_lock);
8695 freemsg(mp);
8696 break;
8698 default:
8699 ASSERT(0);
8700 break;
8705 * Update any source route, record route or timestamp options
8706 * When it fails it has consumed the message and BUMPed the MIB.
8708 boolean_t
8709 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8710 ip_recv_attr_t *ira)
8712 ipoptp_t opts;
8713 uchar_t *opt;
8714 uint8_t optval;
8715 uint8_t optlen;
8716 ipaddr_t dst;
8717 ipaddr_t ifaddr;
8718 uint32_t ts;
8719 timestruc_t now;
8720 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
8722 ip2dbg(("ip_forward_options\n"));
8723 dst = ipha->ipha_dst;
8724 for (optval = ipoptp_first(&opts, ipha);
8725 optval != IPOPT_EOL;
8726 optval = ipoptp_next(&opts)) {
8727 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8728 opt = opts.ipoptp_cur;
8729 optlen = opts.ipoptp_len;
8730 ip2dbg(("ip_forward_options: opt %d, len %d\n",
8731 optval, opts.ipoptp_len));
8732 switch (optval) {
8733 uint32_t off;
8734 case IPOPT_SSRR:
8735 case IPOPT_LSRR:
8736 /* Check if adminstratively disabled */
8737 if (!ipst->ips_ip_forward_src_routed) {
8738 BUMP_MIB(dst_ill->ill_ip_mib,
8739 ipIfStatsForwProhibits);
8740 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
8741 mp, dst_ill);
8742 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
8743 ira);
8744 return (B_FALSE);
8746 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
8748 * Must be partial since ip_input_options
8749 * checked for strict.
8751 break;
8753 off = opt[IPOPT_OFFSET];
8754 off--;
8755 redo_srr:
8756 if (optlen < IP_ADDR_LEN ||
8757 off > optlen - IP_ADDR_LEN) {
8758 /* End of source route */
8759 ip1dbg((
8760 "ip_forward_options: end of SR\n"));
8761 break;
8763 /* Pick a reasonable address on the outbound if */
8764 ASSERT(dst_ill != NULL);
8765 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
8766 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
8767 NULL) != 0) {
8768 /* No source! Shouldn't happen */
8769 ifaddr = INADDR_ANY;
8771 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
8772 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
8773 ip1dbg(("ip_forward_options: next hop 0x%x\n",
8774 ntohl(dst)));
8777 * Check if our address is present more than
8778 * once as consecutive hops in source route.
8780 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
8781 off += IP_ADDR_LEN;
8782 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8783 goto redo_srr;
8785 ipha->ipha_dst = dst;
8786 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8787 break;
8788 case IPOPT_RR:
8789 off = opt[IPOPT_OFFSET];
8790 off--;
8791 if (optlen < IP_ADDR_LEN ||
8792 off > optlen - IP_ADDR_LEN) {
8793 /* No more room - ignore */
8794 ip1dbg((
8795 "ip_forward_options: end of RR\n"));
8796 break;
8798 /* Pick a reasonable address on the outbound if */
8799 ASSERT(dst_ill != NULL);
8800 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
8801 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
8802 NULL) != 0) {
8803 /* No source! Shouldn't happen */
8804 ifaddr = INADDR_ANY;
8806 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
8807 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8808 break;
8809 case IPOPT_TS:
8810 /* Insert timestamp if there is room */
8811 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
8812 case IPOPT_TS_TSONLY:
8813 off = IPOPT_TS_TIMELEN;
8814 break;
8815 case IPOPT_TS_PRESPEC:
8816 case IPOPT_TS_PRESPEC_RFC791:
8817 /* Verify that the address matched */
8818 off = opt[IPOPT_OFFSET] - 1;
8819 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
8820 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
8821 /* Not for us */
8822 break;
8824 /* FALLTHROUGH */
8825 case IPOPT_TS_TSANDADDR:
8826 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
8827 break;
8828 default:
8830 * ip_*put_options should have already
8831 * dropped this packet.
8833 cmn_err(CE_PANIC, "ip_forward_options: "
8834 "unknown IT - bug in ip_input_options?\n");
8835 return (B_TRUE); /* Keep "lint" happy */
8837 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
8838 /* Increase overflow counter */
8839 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
8840 opt[IPOPT_POS_OV_FLG] =
8841 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
8842 (off << 4));
8843 break;
8845 off = opt[IPOPT_OFFSET] - 1;
8846 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
8847 case IPOPT_TS_PRESPEC:
8848 case IPOPT_TS_PRESPEC_RFC791:
8849 case IPOPT_TS_TSANDADDR:
8850 /* Pick a reasonable addr on the outbound if */
8851 ASSERT(dst_ill != NULL);
8852 if (ip_select_source_v4(dst_ill, INADDR_ANY,
8853 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
8854 NULL, NULL) != 0) {
8855 /* No source! Shouldn't happen */
8856 ifaddr = INADDR_ANY;
8858 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
8859 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8860 /* FALLTHROUGH */
8861 case IPOPT_TS_TSONLY:
8862 off = opt[IPOPT_OFFSET] - 1;
8863 /* Compute # of milliseconds since midnight */
8864 gethrestime(&now);
8865 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
8866 NSEC2MSEC(now.tv_nsec);
8867 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
8868 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
8869 break;
8871 break;
8874 return (B_TRUE);
8878 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
8879 * returns 'true' if there are still fragments left on the queue, in
8880 * which case we restart the timer.
8882 void
8883 ill_frag_timer(void *arg)
8885 ill_t *ill = (ill_t *)arg;
8886 boolean_t frag_pending;
8887 ip_stack_t *ipst = ill->ill_ipst;
8888 time_t timeout;
8890 mutex_enter(&ill->ill_lock);
8891 ASSERT(!ill->ill_fragtimer_executing);
8892 if (ill->ill_state_flags & ILL_CONDEMNED) {
8893 ill->ill_frag_timer_id = 0;
8894 mutex_exit(&ill->ill_lock);
8895 return;
8897 ill->ill_fragtimer_executing = 1;
8898 mutex_exit(&ill->ill_lock);
8900 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
8901 ipst->ips_ip_reassembly_timeout);
8903 frag_pending = ill_frag_timeout(ill, timeout);
8906 * Restart the timer, if we have fragments pending or if someone
8907 * wanted us to be scheduled again.
8909 mutex_enter(&ill->ill_lock);
8910 ill->ill_fragtimer_executing = 0;
8911 ill->ill_frag_timer_id = 0;
8912 if (frag_pending || ill->ill_fragtimer_needrestart)
8913 ill_frag_timer_start(ill);
8914 mutex_exit(&ill->ill_lock);
8917 void
8918 ill_frag_timer_start(ill_t *ill)
8920 ip_stack_t *ipst = ill->ill_ipst;
8921 clock_t timeo_ms;
8923 ASSERT(MUTEX_HELD(&ill->ill_lock));
8925 /* If the ill is closing or opening don't proceed */
8926 if (ill->ill_state_flags & ILL_CONDEMNED)
8927 return;
8929 if (ill->ill_fragtimer_executing) {
8931 * ill_frag_timer is currently executing. Just record the
8932 * the fact that we want the timer to be restarted.
8933 * ill_frag_timer will post a timeout before it returns,
8934 * ensuring it will be called again.
8936 ill->ill_fragtimer_needrestart = 1;
8937 return;
8940 if (ill->ill_frag_timer_id == 0) {
8941 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
8942 ipst->ips_ip_reassembly_timeout) * SECONDS;
8945 * The timer is neither running nor is the timeout handler
8946 * executing. Post a timeout so that ill_frag_timer will be
8947 * called
8949 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
8950 MSEC_TO_TICK(timeo_ms >> 1));
8951 ill->ill_fragtimer_needrestart = 0;
8956 * Update any source route, record route or timestamp options.
8957 * Check that we are at end of strict source route.
8958 * The options have already been checked for sanity in ip_input_options().
8960 boolean_t
8961 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
8963 ipoptp_t opts;
8964 uchar_t *opt;
8965 uint8_t optval;
8966 uint8_t optlen;
8967 ipaddr_t dst;
8968 ipaddr_t ifaddr;
8969 uint32_t ts;
8970 timestruc_t now;
8971 ill_t *ill = ira->ira_ill;
8972 ip_stack_t *ipst = ill->ill_ipst;
8974 ip2dbg(("ip_input_local_options\n"));
8976 for (optval = ipoptp_first(&opts, ipha);
8977 optval != IPOPT_EOL;
8978 optval = ipoptp_next(&opts)) {
8979 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8980 opt = opts.ipoptp_cur;
8981 optlen = opts.ipoptp_len;
8982 ip2dbg(("ip_input_local_options: opt %d, len %d\n",
8983 optval, optlen));
8984 switch (optval) {
8985 uint32_t off;
8986 case IPOPT_SSRR:
8987 case IPOPT_LSRR:
8988 off = opt[IPOPT_OFFSET];
8989 off--;
8990 if (optlen < IP_ADDR_LEN ||
8991 off > optlen - IP_ADDR_LEN) {
8992 /* End of source route */
8993 ip1dbg(("ip_input_local_options: end of SR\n"));
8994 break;
8997 * This will only happen if two consecutive entries
8998 * in the source route contains our address or if
8999 * it is a packet with a loose source route which
9000 * reaches us before consuming the whole source route
9002 ip1dbg(("ip_input_local_options: not end of SR\n"));
9003 if (optval == IPOPT_SSRR) {
9004 goto bad_src_route;
9007 * Hack: instead of dropping the packet truncate the
9008 * source route to what has been used by filling the
9009 * rest with IPOPT_NOP.
9011 opt[IPOPT_OLEN] = (uint8_t)off;
9012 while (off < optlen) {
9013 opt[off++] = IPOPT_NOP;
9015 break;
9016 case IPOPT_RR:
9017 off = opt[IPOPT_OFFSET];
9018 off--;
9019 if (optlen < IP_ADDR_LEN ||
9020 off > optlen - IP_ADDR_LEN) {
9021 /* No more room - ignore */
9022 ip1dbg((
9023 "ip_input_local_options: end of RR\n"));
9024 break;
9026 /* Pick a reasonable address on the outbound if */
9027 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9028 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9029 NULL) != 0) {
9030 /* No source! Shouldn't happen */
9031 ifaddr = INADDR_ANY;
9033 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9034 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9035 break;
9036 case IPOPT_TS:
9037 /* Insert timestamp if there is romm */
9038 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9039 case IPOPT_TS_TSONLY:
9040 off = IPOPT_TS_TIMELEN;
9041 break;
9042 case IPOPT_TS_PRESPEC:
9043 case IPOPT_TS_PRESPEC_RFC791:
9044 /* Verify that the address matched */
9045 off = opt[IPOPT_OFFSET] - 1;
9046 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9047 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9048 /* Not for us */
9049 break;
9051 /* FALLTHROUGH */
9052 case IPOPT_TS_TSANDADDR:
9053 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9054 break;
9055 default:
9057 * ip_*put_options should have already
9058 * dropped this packet.
9060 cmn_err(CE_PANIC, "ip_input_local_options: "
9061 "unknown IT - bug in ip_input_options?\n");
9062 return (B_TRUE); /* Keep "lint" happy */
9064 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9065 /* Increase overflow counter */
9066 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9067 opt[IPOPT_POS_OV_FLG] =
9068 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9069 (off << 4));
9070 break;
9072 off = opt[IPOPT_OFFSET] - 1;
9073 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9074 case IPOPT_TS_PRESPEC:
9075 case IPOPT_TS_PRESPEC_RFC791:
9076 case IPOPT_TS_TSANDADDR:
9077 /* Pick a reasonable addr on the outbound if */
9078 if (ip_select_source_v4(ill, INADDR_ANY,
9079 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9080 &ifaddr, NULL, NULL) != 0) {
9081 /* No source! Shouldn't happen */
9082 ifaddr = INADDR_ANY;
9084 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9085 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9086 /* FALLTHROUGH */
9087 case IPOPT_TS_TSONLY:
9088 off = opt[IPOPT_OFFSET] - 1;
9089 /* Compute # of milliseconds since midnight */
9090 gethrestime(&now);
9091 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9092 NSEC2MSEC(now.tv_nsec);
9093 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9094 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9095 break;
9097 break;
9100 return (B_TRUE);
9102 bad_src_route:
9103 /* make sure we clear any indication of a hardware checksum */
9104 DB_CKSUMFLAGS(mp) = 0;
9105 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9106 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9107 return (B_FALSE);
9112 * Process IP options in an inbound packet. Always returns the nexthop.
9113 * Normally this is the passed in nexthop, but if there is an option
9114 * that effects the nexthop (such as a source route) that will be returned.
9115 * Sets *errorp if there is an error, in which case an ICMP error has been sent
9116 * and mp freed.
9118 ipaddr_t
9119 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9120 ip_recv_attr_t *ira, int *errorp)
9122 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9123 ipoptp_t opts;
9124 uchar_t *opt;
9125 uint8_t optval;
9126 uint8_t optlen;
9127 intptr_t code = 0;
9128 ire_t *ire;
9130 ip2dbg(("ip_input_options\n"));
9131 *errorp = 0;
9132 for (optval = ipoptp_first(&opts, ipha);
9133 optval != IPOPT_EOL;
9134 optval = ipoptp_next(&opts)) {
9135 opt = opts.ipoptp_cur;
9136 optlen = opts.ipoptp_len;
9137 ip2dbg(("ip_input_options: opt %d, len %d\n",
9138 optval, optlen));
9140 * Note: we need to verify the checksum before we
9141 * modify anything thus this routine only extracts the next
9142 * hop dst from any source route.
9144 switch (optval) {
9145 uint32_t off;
9146 case IPOPT_SSRR:
9147 case IPOPT_LSRR:
9148 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9149 if (optval == IPOPT_SSRR) {
9150 ip1dbg(("ip_input_options: not next"
9151 " strict source route 0x%x\n",
9152 ntohl(dst)));
9153 code = (char *)&ipha->ipha_dst -
9154 (char *)ipha;
9155 goto param_prob; /* RouterReq's */
9157 ip2dbg(("ip_input_options: "
9158 "not next source route 0x%x\n",
9159 ntohl(dst)));
9160 break;
9163 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9164 ip1dbg((
9165 "ip_input_options: bad option offset\n"));
9166 code = (char *)&opt[IPOPT_OLEN] -
9167 (char *)ipha;
9168 goto param_prob;
9170 off = opt[IPOPT_OFFSET];
9171 off--;
9172 redo_srr:
9173 if (optlen < IP_ADDR_LEN ||
9174 off > optlen - IP_ADDR_LEN) {
9175 /* End of source route */
9176 ip1dbg(("ip_input_options: end of SR\n"));
9177 break;
9179 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9180 ip1dbg(("ip_input_options: next hop 0x%x\n",
9181 ntohl(dst)));
9184 * Check if our address is present more than
9185 * once as consecutive hops in source route.
9186 * XXX verify per-interface ip_forwarding
9187 * for source route?
9189 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9190 off += IP_ADDR_LEN;
9191 goto redo_srr;
9194 if (dst == htonl(INADDR_LOOPBACK)) {
9195 ip1dbg(("ip_input_options: loopback addr in "
9196 "source route!\n"));
9197 goto bad_src_route;
9200 * For strict: verify that dst is directly
9201 * reachable.
9203 if (optval == IPOPT_SSRR) {
9204 ire = ire_ftable_lookup_v4(dst, 0, 0,
9205 IRE_INTERFACE, NULL, ALL_ZONES,
9206 MATCH_IRE_TYPE, 0, ipst, NULL);
9207 if (ire == NULL) {
9208 ip1dbg(("ip_input_options: SSRR not "
9209 "directly reachable: 0x%x\n",
9210 ntohl(dst)));
9211 goto bad_src_route;
9213 ire_refrele(ire);
9216 * Defer update of the offset and the record route
9217 * until the packet is forwarded.
9219 break;
9220 case IPOPT_RR:
9221 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9222 ip1dbg((
9223 "ip_input_options: bad option offset\n"));
9224 code = (char *)&opt[IPOPT_OLEN] -
9225 (char *)ipha;
9226 goto param_prob;
9228 break;
9229 case IPOPT_TS:
9231 * Verify that length >= 5 and that there is either
9232 * room for another timestamp or that the overflow
9233 * counter is not maxed out.
9235 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9236 if (optlen < IPOPT_MINLEN_IT) {
9237 goto param_prob;
9239 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9240 ip1dbg((
9241 "ip_input_options: bad option offset\n"));
9242 code = (char *)&opt[IPOPT_OFFSET] -
9243 (char *)ipha;
9244 goto param_prob;
9246 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9247 case IPOPT_TS_TSONLY:
9248 off = IPOPT_TS_TIMELEN;
9249 break;
9250 case IPOPT_TS_TSANDADDR:
9251 case IPOPT_TS_PRESPEC:
9252 case IPOPT_TS_PRESPEC_RFC791:
9253 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9254 break;
9255 default:
9256 code = (char *)&opt[IPOPT_POS_OV_FLG] -
9257 (char *)ipha;
9258 goto param_prob;
9260 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9261 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9263 * No room and the overflow counter is 15
9264 * already.
9266 goto param_prob;
9268 break;
9272 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9273 return (dst);
9276 ip1dbg(("ip_input_options: error processing IP options."));
9277 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9279 param_prob:
9280 /* make sure we clear any indication of a hardware checksum */
9281 DB_CKSUMFLAGS(mp) = 0;
9282 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9283 icmp_param_problem(mp, (uint8_t)code, ira);
9284 *errorp = -1;
9285 return (dst);
9287 bad_src_route:
9288 /* make sure we clear any indication of a hardware checksum */
9289 DB_CKSUMFLAGS(mp) = 0;
9290 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9291 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9292 *errorp = -1;
9293 return (dst);
9297 * IP & ICMP info in >=14 msg's ...
9298 * - ip fixed part (mib2_ip_t)
9299 * - icmp fixed part (mib2_icmp_t)
9300 * - ipAddrEntryTable (ip 20) all IPv4 ipifs
9301 * - ipRouteEntryTable (ip 21) all IPv4 IREs
9302 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries
9303 * - ip multicast membership (ip_member_t)
9304 * - ip multicast source filtering (ip_grpsrc_t)
9305 * - igmp fixed part (struct igmpstat)
9306 * - multicast routing stats (struct mrtstat)
9307 * - multicast routing vifs (array of struct vifctl)
9308 * - multicast routing routes (array of struct mfcctl)
9309 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9310 * One per ill plus one generic
9311 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9312 * One per ill plus one generic
9313 * - ipv6RouteEntry all IPv6 IREs
9314 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries
9315 * - ipv6AddrEntry all IPv6 ipifs
9316 * - ipv6 multicast membership (ipv6_member_t)
9317 * - ipv6 multicast source filtering (ipv6_grpsrc_t)
9319 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9320 * already filled in by the caller.
9321 * If legacy_req is true then MIB structures needs to be truncated to their
9322 * legacy sizes before being returned.
9323 * Return value of 0 indicates that no messages were sent and caller
9324 * should free mpctl.
9327 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9329 ip_stack_t *ipst;
9330 sctp_stack_t *sctps;
9332 if (q->q_next != NULL) {
9333 ipst = ILLQ_TO_IPST(q);
9334 } else {
9335 ipst = CONNQ_TO_IPST(q);
9337 ASSERT(ipst != NULL);
9338 sctps = ipst->ips_netstack->netstack_sctp;
9340 if (mpctl == NULL || mpctl->b_cont == NULL) {
9341 return (0);
9345 * For the purposes of the (broken) packet shell use
9346 * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9347 * to make TCP and UDP appear first in the list of mib items.
9348 * TBD: We could expand this and use it in netstat so that
9349 * the kernel doesn't have to produce large tables (connections,
9350 * routes, etc) when netstat only wants the statistics or a particular
9351 * table.
9353 if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9354 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9355 return (1);
9359 if (level != MIB2_TCP) {
9360 if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9361 return (1);
9365 if (level != MIB2_UDP) {
9366 if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9367 return (1);
9371 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9372 ipst, legacy_req)) == NULL) {
9373 return (1);
9376 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9377 legacy_req)) == NULL) {
9378 return (1);
9381 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9382 return (1);
9385 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9386 return (1);
9389 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9390 return (1);
9393 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9394 return (1);
9397 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9398 legacy_req)) == NULL) {
9399 return (1);
9402 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9403 legacy_req)) == NULL) {
9404 return (1);
9407 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9408 return (1);
9411 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9412 return (1);
9415 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9416 return (1);
9419 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9420 return (1);
9423 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9424 return (1);
9427 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9428 return (1);
9431 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9432 if (mpctl == NULL)
9433 return (1);
9435 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9436 if (mpctl == NULL)
9437 return (1);
9439 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9440 return (1);
9442 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9443 return (1);
9445 freemsg(mpctl);
9446 return (1);
9449 /* Get global (legacy) IPv4 statistics */
9450 static mblk_t *
9451 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9452 ip_stack_t *ipst, boolean_t legacy_req)
9454 mib2_ip_t old_ip_mib;
9455 struct opthdr *optp;
9456 mblk_t *mp2ctl;
9457 mib2_ipAddrEntry_t mae;
9460 * make a copy of the original message
9462 mp2ctl = copymsg(mpctl);
9464 /* fixed length IP structure... */
9465 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9466 optp->level = MIB2_IP;
9467 optp->name = 0;
9468 SET_MIB(old_ip_mib.ipForwarding,
9469 (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9470 SET_MIB(old_ip_mib.ipDefaultTTL,
9471 (uint32_t)ipst->ips_ip_def_ttl);
9472 SET_MIB(old_ip_mib.ipReasmTimeout,
9473 ipst->ips_ip_reassembly_timeout);
9474 SET_MIB(old_ip_mib.ipAddrEntrySize,
9475 (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9476 sizeof (mib2_ipAddrEntry_t));
9477 SET_MIB(old_ip_mib.ipRouteEntrySize,
9478 sizeof (mib2_ipRouteEntry_t));
9479 SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9480 sizeof (mib2_ipNetToMediaEntry_t));
9481 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9482 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9483 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9486 * Grab the statistics from the new IP MIB
9488 SET_MIB(old_ip_mib.ipInReceives,
9489 (uint32_t)ipmib->ipIfStatsHCInReceives);
9490 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9491 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9492 SET_MIB(old_ip_mib.ipForwDatagrams,
9493 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9494 SET_MIB(old_ip_mib.ipInUnknownProtos,
9495 ipmib->ipIfStatsInUnknownProtos);
9496 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9497 SET_MIB(old_ip_mib.ipInDelivers,
9498 (uint32_t)ipmib->ipIfStatsHCInDelivers);
9499 SET_MIB(old_ip_mib.ipOutRequests,
9500 (uint32_t)ipmib->ipIfStatsHCOutRequests);
9501 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9502 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9503 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9504 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9505 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9506 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9507 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9508 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9510 /* ipRoutingDiscards is not being used */
9511 SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9512 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9513 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9514 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9515 SET_MIB(old_ip_mib.ipReasmDuplicates,
9516 ipmib->ipIfStatsReasmDuplicates);
9517 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9518 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9519 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9520 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9521 SET_MIB(old_ip_mib.rawipInOverflows,
9522 ipmib->rawipIfStatsInOverflows);
9524 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9525 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9526 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9527 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9528 SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9529 ipmib->ipIfStatsOutSwitchIPVersion);
9531 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9532 (int)sizeof (old_ip_mib))) {
9533 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9534 (uint_t)sizeof (old_ip_mib)));
9537 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9538 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9539 (int)optp->level, (int)optp->name, (int)optp->len));
9540 qreply(q, mpctl);
9541 return (mp2ctl);
9544 /* Per interface IPv4 statistics */
9545 static mblk_t *
9546 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9547 boolean_t legacy_req)
9549 struct opthdr *optp;
9550 mblk_t *mp2ctl;
9551 ill_t *ill;
9552 ill_walk_context_t ctx;
9553 mblk_t *mp_tail = NULL;
9554 mib2_ipIfStatsEntry_t global_ip_mib;
9555 mib2_ipAddrEntry_t mae;
9558 * Make a copy of the original message
9560 mp2ctl = copymsg(mpctl);
9562 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9563 optp->level = MIB2_IP;
9564 optp->name = MIB2_IP_TRAFFIC_STATS;
9565 /* Include "unknown interface" ip_mib */
9566 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9567 ipst->ips_ip_mib.ipIfStatsIfIndex =
9568 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9569 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9570 (ipst->ips_ip_forwarding ? 1 : 2));
9571 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9572 (uint32_t)ipst->ips_ip_def_ttl);
9573 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9574 sizeof (mib2_ipIfStatsEntry_t));
9575 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9576 sizeof (mib2_ipAddrEntry_t));
9577 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9578 sizeof (mib2_ipRouteEntry_t));
9579 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9580 sizeof (mib2_ipNetToMediaEntry_t));
9581 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9582 sizeof (ip_member_t));
9583 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9584 sizeof (ip_grpsrc_t));
9586 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9588 if (legacy_req) {
9589 SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9590 LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9593 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9594 (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9595 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9596 "failed to allocate %u bytes\n",
9597 (uint_t)sizeof (global_ip_mib)));
9600 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9601 ill = ILL_START_WALK_V4(&ctx, ipst);
9602 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9603 ill->ill_ip_mib->ipIfStatsIfIndex =
9604 ill->ill_phyint->phyint_ifindex;
9605 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9606 (ipst->ips_ip_forwarding ? 1 : 2));
9607 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9608 (uint32_t)ipst->ips_ip_def_ttl);
9610 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9611 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9612 (char *)ill->ill_ip_mib,
9613 (int)sizeof (*ill->ill_ip_mib))) {
9614 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9615 "failed to allocate %u bytes\n",
9616 (uint_t)sizeof (*ill->ill_ip_mib)));
9619 rw_exit(&ipst->ips_ill_g_lock);
9621 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9622 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9623 "level %d, name %d, len %d\n",
9624 (int)optp->level, (int)optp->name, (int)optp->len));
9625 qreply(q, mpctl);
9627 if (mp2ctl == NULL)
9628 return (NULL);
9630 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9631 legacy_req));
9634 /* Global IPv4 ICMP statistics */
9635 static mblk_t *
9636 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9638 struct opthdr *optp;
9639 mblk_t *mp2ctl;
9642 * Make a copy of the original message
9644 mp2ctl = copymsg(mpctl);
9646 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9647 optp->level = MIB2_ICMP;
9648 optp->name = 0;
9649 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9650 (int)sizeof (ipst->ips_icmp_mib))) {
9651 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9652 (uint_t)sizeof (ipst->ips_icmp_mib)));
9654 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9655 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9656 (int)optp->level, (int)optp->name, (int)optp->len));
9657 qreply(q, mpctl);
9658 return (mp2ctl);
9661 /* Global IPv4 IGMP statistics */
9662 static mblk_t *
9663 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9665 struct opthdr *optp;
9666 mblk_t *mp2ctl;
9669 * make a copy of the original message
9671 mp2ctl = copymsg(mpctl);
9673 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9674 optp->level = EXPER_IGMP;
9675 optp->name = 0;
9676 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9677 (int)sizeof (ipst->ips_igmpstat))) {
9678 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9679 (uint_t)sizeof (ipst->ips_igmpstat)));
9681 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9682 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9683 (int)optp->level, (int)optp->name, (int)optp->len));
9684 qreply(q, mpctl);
9685 return (mp2ctl);
9688 /* Global IPv4 Multicast Routing statistics */
9689 static mblk_t *
9690 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9692 struct opthdr *optp;
9693 mblk_t *mp2ctl;
9696 * make a copy of the original message
9698 mp2ctl = copymsg(mpctl);
9700 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9701 optp->level = EXPER_DVMRP;
9702 optp->name = 0;
9703 if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9704 ip0dbg(("ip_mroute_stats: failed\n"));
9706 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9707 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9708 (int)optp->level, (int)optp->name, (int)optp->len));
9709 qreply(q, mpctl);
9710 return (mp2ctl);
9713 /* IPv4 address information */
9714 static mblk_t *
9715 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9716 boolean_t legacy_req)
9718 struct opthdr *optp;
9719 mblk_t *mp2ctl;
9720 mblk_t *mp_tail = NULL;
9721 ill_t *ill;
9722 ipif_t *ipif;
9723 uint_t bitval;
9724 mib2_ipAddrEntry_t mae;
9725 size_t mae_size;
9726 zoneid_t zoneid;
9727 ill_walk_context_t ctx;
9730 * make a copy of the original message
9732 mp2ctl = copymsg(mpctl);
9734 mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9735 sizeof (mib2_ipAddrEntry_t);
9737 /* ipAddrEntryTable */
9739 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9740 optp->level = MIB2_IP;
9741 optp->name = MIB2_IP_ADDR;
9742 zoneid = Q_TO_CONN(q)->conn_zoneid;
9744 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9745 ill = ILL_START_WALK_V4(&ctx, ipst);
9746 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9747 for (ipif = ill->ill_ipif; ipif != NULL;
9748 ipif = ipif->ipif_next) {
9749 if (ipif->ipif_zoneid != zoneid &&
9750 ipif->ipif_zoneid != ALL_ZONES)
9751 continue;
9752 /* Sum of count from dead IRE_LO* and our current */
9753 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
9754 if (ipif->ipif_ire_local != NULL) {
9755 mae.ipAdEntInfo.ae_ibcnt +=
9756 ipif->ipif_ire_local->ire_ib_pkt_count;
9758 mae.ipAdEntInfo.ae_obcnt = 0;
9759 mae.ipAdEntInfo.ae_focnt = 0;
9761 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
9762 OCTET_LENGTH);
9763 mae.ipAdEntIfIndex.o_length =
9764 mi_strlen(mae.ipAdEntIfIndex.o_bytes);
9765 mae.ipAdEntAddr = ipif->ipif_lcl_addr;
9766 mae.ipAdEntNetMask = ipif->ipif_net_mask;
9767 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
9768 mae.ipAdEntInfo.ae_subnet_len =
9769 ip_mask_to_plen(ipif->ipif_net_mask);
9770 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
9771 for (bitval = 1;
9772 bitval &&
9773 !(bitval & ipif->ipif_brd_addr);
9774 bitval <<= 1)
9775 noop;
9776 mae.ipAdEntBcastAddr = bitval;
9777 mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
9778 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
9779 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric;
9780 mae.ipAdEntInfo.ae_broadcast_addr =
9781 ipif->ipif_brd_addr;
9782 mae.ipAdEntInfo.ae_pp_dst_addr =
9783 ipif->ipif_pp_dst_addr;
9784 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
9785 ill->ill_flags | ill->ill_phyint->phyint_flags;
9786 mae.ipAdEntRetransmitTime =
9787 ill->ill_reachable_retrans_time;
9789 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9790 (char *)&mae, (int)mae_size)) {
9791 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
9792 "allocate %u bytes\n", (uint_t)mae_size));
9796 rw_exit(&ipst->ips_ill_g_lock);
9798 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9799 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
9800 (int)optp->level, (int)optp->name, (int)optp->len));
9801 qreply(q, mpctl);
9802 return (mp2ctl);
9805 /* IPv6 address information */
9806 static mblk_t *
9807 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9808 boolean_t legacy_req)
9810 struct opthdr *optp;
9811 mblk_t *mp2ctl;
9812 mblk_t *mp_tail = NULL;
9813 ill_t *ill;
9814 ipif_t *ipif;
9815 mib2_ipv6AddrEntry_t mae6;
9816 size_t mae6_size;
9817 zoneid_t zoneid;
9818 ill_walk_context_t ctx;
9821 * make a copy of the original message
9823 mp2ctl = copymsg(mpctl);
9825 mae6_size = (legacy_req) ?
9826 LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
9827 sizeof (mib2_ipv6AddrEntry_t);
9829 /* ipv6AddrEntryTable */
9831 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9832 optp->level = MIB2_IP6;
9833 optp->name = MIB2_IP6_ADDR;
9834 zoneid = Q_TO_CONN(q)->conn_zoneid;
9836 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9837 ill = ILL_START_WALK_V6(&ctx, ipst);
9838 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9839 for (ipif = ill->ill_ipif; ipif != NULL;
9840 ipif = ipif->ipif_next) {
9841 if (ipif->ipif_zoneid != zoneid &&
9842 ipif->ipif_zoneid != ALL_ZONES)
9843 continue;
9844 /* Sum of count from dead IRE_LO* and our current */
9845 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
9846 if (ipif->ipif_ire_local != NULL) {
9847 mae6.ipv6AddrInfo.ae_ibcnt +=
9848 ipif->ipif_ire_local->ire_ib_pkt_count;
9850 mae6.ipv6AddrInfo.ae_obcnt = 0;
9851 mae6.ipv6AddrInfo.ae_focnt = 0;
9853 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
9854 OCTET_LENGTH);
9855 mae6.ipv6AddrIfIndex.o_length =
9856 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
9857 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
9858 mae6.ipv6AddrPfxLength =
9859 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9860 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
9861 mae6.ipv6AddrInfo.ae_subnet_len =
9862 mae6.ipv6AddrPfxLength;
9863 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
9865 /* Type: stateless(1), stateful(2), unknown(3) */
9866 if (ipif->ipif_flags & IPIF_ADDRCONF)
9867 mae6.ipv6AddrType = 1;
9868 else
9869 mae6.ipv6AddrType = 2;
9870 /* Anycast: true(1), false(2) */
9871 if (ipif->ipif_flags & IPIF_ANYCAST)
9872 mae6.ipv6AddrAnycastFlag = 1;
9873 else
9874 mae6.ipv6AddrAnycastFlag = 2;
9877 * Address status: preferred(1), deprecated(2),
9878 * invalid(3), inaccessible(4), unknown(5)
9880 if (ipif->ipif_flags & IPIF_NOLOCAL)
9881 mae6.ipv6AddrStatus = 3;
9882 else if (ipif->ipif_flags & IPIF_DEPRECATED)
9883 mae6.ipv6AddrStatus = 2;
9884 else
9885 mae6.ipv6AddrStatus = 1;
9886 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
9887 mae6.ipv6AddrInfo.ae_metric =
9888 ipif->ipif_ill->ill_metric;
9889 mae6.ipv6AddrInfo.ae_pp_dst_addr =
9890 ipif->ipif_v6pp_dst_addr;
9891 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
9892 ill->ill_flags | ill->ill_phyint->phyint_flags;
9893 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
9894 mae6.ipv6AddrIdentifier = ill->ill_token;
9895 mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
9896 mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
9897 mae6.ipv6AddrRetransmitTime =
9898 ill->ill_reachable_retrans_time;
9899 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9900 (char *)&mae6, (int)mae6_size)) {
9901 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
9902 "allocate %u bytes\n",
9903 (uint_t)mae6_size));
9907 rw_exit(&ipst->ips_ill_g_lock);
9909 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9910 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
9911 (int)optp->level, (int)optp->name, (int)optp->len));
9912 qreply(q, mpctl);
9913 return (mp2ctl);
9916 /* IPv4 multicast group membership. */
9917 static mblk_t *
9918 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9920 struct opthdr *optp;
9921 mblk_t *mp2ctl;
9922 ill_t *ill;
9923 ipif_t *ipif;
9924 ilm_t *ilm;
9925 ip_member_t ipm;
9926 mblk_t *mp_tail = NULL;
9927 ill_walk_context_t ctx;
9928 zoneid_t zoneid;
9931 * make a copy of the original message
9933 mp2ctl = copymsg(mpctl);
9934 zoneid = Q_TO_CONN(q)->conn_zoneid;
9936 /* ipGroupMember table */
9937 optp = (struct opthdr *)&mpctl->b_rptr[
9938 sizeof (struct T_optmgmt_ack)];
9939 optp->level = MIB2_IP;
9940 optp->name = EXPER_IP_GROUP_MEMBERSHIP;
9942 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9943 ill = ILL_START_WALK_V4(&ctx, ipst);
9944 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9945 /* Make sure the ill isn't going away. */
9946 if (!ill_check_and_refhold(ill))
9947 continue;
9948 rw_exit(&ipst->ips_ill_g_lock);
9949 rw_enter(&ill->ill_mcast_lock, RW_READER);
9950 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
9951 if (ilm->ilm_zoneid != zoneid &&
9952 ilm->ilm_zoneid != ALL_ZONES)
9953 continue;
9955 /* Is there an ipif for ilm_ifaddr? */
9956 for (ipif = ill->ill_ipif; ipif != NULL;
9957 ipif = ipif->ipif_next) {
9958 if (!IPIF_IS_CONDEMNED(ipif) &&
9959 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
9960 ilm->ilm_ifaddr != INADDR_ANY)
9961 break;
9963 if (ipif != NULL) {
9964 ipif_get_name(ipif,
9965 ipm.ipGroupMemberIfIndex.o_bytes,
9966 OCTET_LENGTH);
9967 } else {
9968 ill_get_name(ill,
9969 ipm.ipGroupMemberIfIndex.o_bytes,
9970 OCTET_LENGTH);
9972 ipm.ipGroupMemberIfIndex.o_length =
9973 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
9975 ipm.ipGroupMemberAddress = ilm->ilm_addr;
9976 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
9977 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
9978 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9979 (char *)&ipm, (int)sizeof (ipm))) {
9980 ip1dbg(("ip_snmp_get_mib2_ip_group: "
9981 "failed to allocate %u bytes\n",
9982 (uint_t)sizeof (ipm)));
9985 rw_exit(&ill->ill_mcast_lock);
9986 ill_refrele(ill);
9987 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9989 rw_exit(&ipst->ips_ill_g_lock);
9990 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9991 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
9992 (int)optp->level, (int)optp->name, (int)optp->len));
9993 qreply(q, mpctl);
9994 return (mp2ctl);
9997 /* IPv6 multicast group membership. */
9998 static mblk_t *
9999 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10001 struct opthdr *optp;
10002 mblk_t *mp2ctl;
10003 ill_t *ill;
10004 ilm_t *ilm;
10005 ipv6_member_t ipm6;
10006 mblk_t *mp_tail = NULL;
10007 ill_walk_context_t ctx;
10008 zoneid_t zoneid;
10011 * make a copy of the original message
10013 mp2ctl = copymsg(mpctl);
10014 zoneid = Q_TO_CONN(q)->conn_zoneid;
10016 /* ip6GroupMember table */
10017 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10018 optp->level = MIB2_IP6;
10019 optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10021 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10022 ill = ILL_START_WALK_V6(&ctx, ipst);
10023 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10024 /* Make sure the ill isn't going away. */
10025 if (!ill_check_and_refhold(ill))
10026 continue;
10027 rw_exit(&ipst->ips_ill_g_lock);
10029 * Normally we don't have any members on under IPMP interfaces.
10030 * We report them as a debugging aid.
10032 rw_enter(&ill->ill_mcast_lock, RW_READER);
10033 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10034 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10035 if (ilm->ilm_zoneid != zoneid &&
10036 ilm->ilm_zoneid != ALL_ZONES)
10037 continue; /* not this zone */
10038 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10039 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10040 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10041 if (!snmp_append_data2(mpctl->b_cont,
10042 &mp_tail,
10043 (char *)&ipm6, (int)sizeof (ipm6))) {
10044 ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10045 "failed to allocate %u bytes\n",
10046 (uint_t)sizeof (ipm6)));
10049 rw_exit(&ill->ill_mcast_lock);
10050 ill_refrele(ill);
10051 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10053 rw_exit(&ipst->ips_ill_g_lock);
10055 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10056 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10057 (int)optp->level, (int)optp->name, (int)optp->len));
10058 qreply(q, mpctl);
10059 return (mp2ctl);
10062 /* IP multicast filtered sources */
10063 static mblk_t *
10064 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10066 struct opthdr *optp;
10067 mblk_t *mp2ctl;
10068 ill_t *ill;
10069 ipif_t *ipif;
10070 ilm_t *ilm;
10071 ip_grpsrc_t ips;
10072 mblk_t *mp_tail = NULL;
10073 ill_walk_context_t ctx;
10074 zoneid_t zoneid;
10075 int i;
10076 slist_t *sl;
10079 * make a copy of the original message
10081 mp2ctl = copymsg(mpctl);
10082 zoneid = Q_TO_CONN(q)->conn_zoneid;
10084 /* ipGroupSource table */
10085 optp = (struct opthdr *)&mpctl->b_rptr[
10086 sizeof (struct T_optmgmt_ack)];
10087 optp->level = MIB2_IP;
10088 optp->name = EXPER_IP_GROUP_SOURCES;
10090 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10091 ill = ILL_START_WALK_V4(&ctx, ipst);
10092 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10093 /* Make sure the ill isn't going away. */
10094 if (!ill_check_and_refhold(ill))
10095 continue;
10096 rw_exit(&ipst->ips_ill_g_lock);
10097 rw_enter(&ill->ill_mcast_lock, RW_READER);
10098 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10099 sl = ilm->ilm_filter;
10100 if (ilm->ilm_zoneid != zoneid &&
10101 ilm->ilm_zoneid != ALL_ZONES)
10102 continue;
10103 if (SLIST_IS_EMPTY(sl))
10104 continue;
10106 /* Is there an ipif for ilm_ifaddr? */
10107 for (ipif = ill->ill_ipif; ipif != NULL;
10108 ipif = ipif->ipif_next) {
10109 if (!IPIF_IS_CONDEMNED(ipif) &&
10110 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10111 ilm->ilm_ifaddr != INADDR_ANY)
10112 break;
10114 if (ipif != NULL) {
10115 ipif_get_name(ipif,
10116 ips.ipGroupSourceIfIndex.o_bytes,
10117 OCTET_LENGTH);
10118 } else {
10119 ill_get_name(ill,
10120 ips.ipGroupSourceIfIndex.o_bytes,
10121 OCTET_LENGTH);
10123 ips.ipGroupSourceIfIndex.o_length =
10124 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10126 ips.ipGroupSourceGroup = ilm->ilm_addr;
10127 for (i = 0; i < sl->sl_numsrc; i++) {
10128 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10129 continue;
10130 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10131 ips.ipGroupSourceAddress);
10132 if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10133 (char *)&ips, (int)sizeof (ips)) == 0) {
10134 ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10135 " failed to allocate %u bytes\n",
10136 (uint_t)sizeof (ips)));
10140 rw_exit(&ill->ill_mcast_lock);
10141 ill_refrele(ill);
10142 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10144 rw_exit(&ipst->ips_ill_g_lock);
10145 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10146 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10147 (int)optp->level, (int)optp->name, (int)optp->len));
10148 qreply(q, mpctl);
10149 return (mp2ctl);
10152 /* IPv6 multicast filtered sources. */
10153 static mblk_t *
10154 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10156 struct opthdr *optp;
10157 mblk_t *mp2ctl;
10158 ill_t *ill;
10159 ilm_t *ilm;
10160 ipv6_grpsrc_t ips6;
10161 mblk_t *mp_tail = NULL;
10162 ill_walk_context_t ctx;
10163 zoneid_t zoneid;
10164 int i;
10165 slist_t *sl;
10168 * make a copy of the original message
10170 mp2ctl = copymsg(mpctl);
10171 zoneid = Q_TO_CONN(q)->conn_zoneid;
10173 /* ip6GroupMember table */
10174 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10175 optp->level = MIB2_IP6;
10176 optp->name = EXPER_IP6_GROUP_SOURCES;
10178 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10179 ill = ILL_START_WALK_V6(&ctx, ipst);
10180 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10181 /* Make sure the ill isn't going away. */
10182 if (!ill_check_and_refhold(ill))
10183 continue;
10184 rw_exit(&ipst->ips_ill_g_lock);
10186 * Normally we don't have any members on under IPMP interfaces.
10187 * We report them as a debugging aid.
10189 rw_enter(&ill->ill_mcast_lock, RW_READER);
10190 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10191 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10192 sl = ilm->ilm_filter;
10193 if (ilm->ilm_zoneid != zoneid &&
10194 ilm->ilm_zoneid != ALL_ZONES)
10195 continue;
10196 if (SLIST_IS_EMPTY(sl))
10197 continue;
10198 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10199 for (i = 0; i < sl->sl_numsrc; i++) {
10200 ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10201 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10202 (char *)&ips6, (int)sizeof (ips6))) {
10203 ip1dbg(("ip_snmp_get_mib2_ip6_"
10204 "group_src: failed to allocate "
10205 "%u bytes\n",
10206 (uint_t)sizeof (ips6)));
10210 rw_exit(&ill->ill_mcast_lock);
10211 ill_refrele(ill);
10212 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10214 rw_exit(&ipst->ips_ill_g_lock);
10216 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10217 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10218 (int)optp->level, (int)optp->name, (int)optp->len));
10219 qreply(q, mpctl);
10220 return (mp2ctl);
10223 /* Multicast routing virtual interface table. */
10224 static mblk_t *
10225 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10227 struct opthdr *optp;
10228 mblk_t *mp2ctl;
10231 * make a copy of the original message
10233 mp2ctl = copymsg(mpctl);
10235 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10236 optp->level = EXPER_DVMRP;
10237 optp->name = EXPER_DVMRP_VIF;
10238 if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10239 ip0dbg(("ip_mroute_vif: failed\n"));
10241 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10242 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10243 (int)optp->level, (int)optp->name, (int)optp->len));
10244 qreply(q, mpctl);
10245 return (mp2ctl);
10248 /* Multicast routing table. */
10249 static mblk_t *
10250 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10252 struct opthdr *optp;
10253 mblk_t *mp2ctl;
10256 * make a copy of the original message
10258 mp2ctl = copymsg(mpctl);
10260 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10261 optp->level = EXPER_DVMRP;
10262 optp->name = EXPER_DVMRP_MRT;
10263 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10264 ip0dbg(("ip_mroute_mrt: failed\n"));
10266 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10267 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10268 (int)optp->level, (int)optp->name, (int)optp->len));
10269 qreply(q, mpctl);
10270 return (mp2ctl);
10274 * Return ipRouteEntryTable and ipNetToMediaEntryTable in one IRE walk.
10276 static mblk_t *
10277 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10278 ip_stack_t *ipst)
10280 struct opthdr *optp;
10281 mblk_t *mp2ctl; /* Returned */
10282 mblk_t *mp3ctl; /* nettomedia */
10283 iproutedata_t ird;
10284 zoneid_t zoneid;
10287 * make copies of the original message
10288 * - mp2ctl is returned unchanged to the caller for its use
10289 * - mpctl is sent upstream as ipRouteEntryTable
10290 * - mp3ctl is sent upstream as ipNetToMediaEntryTable
10292 mp2ctl = copymsg(mpctl);
10293 mp3ctl = copymsg(mpctl);
10294 if (mp3ctl == NULL) {
10295 freemsg(mp3ctl);
10296 freemsg(mp2ctl);
10297 freemsg(mpctl);
10298 return (NULL);
10301 bzero(&ird, sizeof (ird));
10303 ird.ird_route.lp_head = mpctl->b_cont;
10304 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10306 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10307 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10308 * intended a temporary solution until a proper MIB API is provided
10309 * that provides complete filtering/caller-opt-in.
10311 if (level == EXPER_IP_AND_ALL_IRES)
10312 ird.ird_flags |= IRD_REPORT_ALL;
10314 zoneid = Q_TO_CONN(q)->conn_zoneid;
10315 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10317 /* ipRouteEntryTable in mpctl */
10318 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10319 optp->level = MIB2_IP;
10320 optp->name = MIB2_IP_ROUTE;
10321 optp->len = msgdsize(ird.ird_route.lp_head);
10322 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10323 (int)optp->level, (int)optp->name, (int)optp->len));
10324 qreply(q, mpctl);
10326 /* ipNetToMediaEntryTable in mp3ctl */
10327 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10329 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10330 optp->level = MIB2_IP;
10331 optp->name = MIB2_IP_MEDIA;
10332 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10333 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10334 (int)optp->level, (int)optp->name, (int)optp->len));
10335 qreply(q, mp3ctl);
10337 return (mp2ctl);
10341 * Return ipv6RouteEntryTable in one IRE walk, and ipv6NetToMediaEntryTable in
10342 * an NDP walk.
10344 static mblk_t *
10345 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10346 ip_stack_t *ipst)
10348 struct opthdr *optp;
10349 mblk_t *mp2ctl; /* Returned */
10350 mblk_t *mp3ctl; /* nettomedia */
10351 iproutedata_t ird;
10352 zoneid_t zoneid;
10355 * make copies of the original message
10356 * - mp2ctl is returned unchanged to the caller for its use
10357 * - mpctl is sent upstream as ipv6RouteEntryTable
10358 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10360 mp2ctl = copymsg(mpctl);
10361 mp3ctl = copymsg(mpctl);
10362 if (mp3ctl == NULL) {
10363 freemsg(mp3ctl);
10364 freemsg(mp2ctl);
10365 freemsg(mpctl);
10366 return (NULL);
10369 bzero(&ird, sizeof (ird));
10371 ird.ird_route.lp_head = mpctl->b_cont;
10372 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10374 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10375 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10376 * intended a temporary solution until a proper MIB API is provided
10377 * that provides complete filtering/caller-opt-in.
10379 if (level == EXPER_IP_AND_ALL_IRES)
10380 ird.ird_flags |= IRD_REPORT_ALL;
10382 zoneid = Q_TO_CONN(q)->conn_zoneid;
10383 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10385 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10386 optp->level = MIB2_IP6;
10387 optp->name = MIB2_IP6_ROUTE;
10388 optp->len = msgdsize(ird.ird_route.lp_head);
10389 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10390 (int)optp->level, (int)optp->name, (int)optp->len));
10391 qreply(q, mpctl);
10393 /* ipv6NetToMediaEntryTable in mp3ctl */
10394 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10396 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10397 optp->level = MIB2_IP6;
10398 optp->name = MIB2_IP6_MEDIA;
10399 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10400 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10401 (int)optp->level, (int)optp->name, (int)optp->len));
10402 qreply(q, mp3ctl);
10404 return (mp2ctl);
10408 * IPv6 mib: One per ill
10410 static mblk_t *
10411 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10412 boolean_t legacy_req)
10414 struct opthdr *optp;
10415 mblk_t *mp2ctl;
10416 ill_t *ill;
10417 ill_walk_context_t ctx;
10418 mblk_t *mp_tail = NULL;
10419 mib2_ipv6AddrEntry_t mae6;
10420 mib2_ipIfStatsEntry_t *ise;
10421 size_t ise_size, iae_size;
10424 * Make a copy of the original message
10426 mp2ctl = copymsg(mpctl);
10428 /* fixed length IPv6 structure ... */
10430 if (legacy_req) {
10431 ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10432 mib2_ipIfStatsEntry_t);
10433 iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10434 } else {
10435 ise_size = sizeof (mib2_ipIfStatsEntry_t);
10436 iae_size = sizeof (mib2_ipv6AddrEntry_t);
10439 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10440 optp->level = MIB2_IP6;
10441 optp->name = 0;
10442 /* Include "unknown interface" ip6_mib */
10443 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10444 ipst->ips_ip6_mib.ipIfStatsIfIndex =
10445 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10446 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10447 ipst->ips_ipv6_forwarding ? 1 : 2);
10448 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10449 ipst->ips_ipv6_def_hops);
10450 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10451 sizeof (mib2_ipIfStatsEntry_t));
10452 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10453 sizeof (mib2_ipv6AddrEntry_t));
10454 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10455 sizeof (mib2_ipv6RouteEntry_t));
10456 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10457 sizeof (mib2_ipv6NetToMediaEntry_t));
10458 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10459 sizeof (ipv6_member_t));
10460 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10461 sizeof (ipv6_grpsrc_t));
10464 * Synchronize 64- and 32-bit counters
10466 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10467 ipIfStatsHCInReceives);
10468 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10469 ipIfStatsHCInDelivers);
10470 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10471 ipIfStatsHCOutRequests);
10472 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10473 ipIfStatsHCOutForwDatagrams);
10474 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10475 ipIfStatsHCOutMcastPkts);
10476 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10477 ipIfStatsHCInMcastPkts);
10479 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10480 (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10481 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10482 (uint_t)ise_size));
10483 } else if (legacy_req) {
10484 /* Adjust the EntrySize fields for legacy requests. */
10485 ise =
10486 (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10487 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10488 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10491 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10492 ill = ILL_START_WALK_V6(&ctx, ipst);
10493 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10494 ill->ill_ip_mib->ipIfStatsIfIndex =
10495 ill->ill_phyint->phyint_ifindex;
10496 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10497 ipst->ips_ipv6_forwarding ? 1 : 2);
10498 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10499 ill->ill_max_hops);
10502 * Synchronize 64- and 32-bit counters
10504 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10505 ipIfStatsHCInReceives);
10506 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10507 ipIfStatsHCInDelivers);
10508 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10509 ipIfStatsHCOutRequests);
10510 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10511 ipIfStatsHCOutForwDatagrams);
10512 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10513 ipIfStatsHCOutMcastPkts);
10514 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10515 ipIfStatsHCInMcastPkts);
10517 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10518 (char *)ill->ill_ip_mib, (int)ise_size)) {
10519 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10520 "%u bytes\n", (uint_t)ise_size));
10521 } else if (legacy_req) {
10522 /* Adjust the EntrySize fields for legacy requests. */
10523 ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10524 (int)ise_size);
10525 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10526 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10529 rw_exit(&ipst->ips_ill_g_lock);
10531 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10532 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10533 (int)optp->level, (int)optp->name, (int)optp->len));
10534 qreply(q, mpctl);
10535 return (mp2ctl);
10539 * ICMPv6 mib: One per ill
10541 static mblk_t *
10542 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10544 struct opthdr *optp;
10545 mblk_t *mp2ctl;
10546 ill_t *ill;
10547 ill_walk_context_t ctx;
10548 mblk_t *mp_tail = NULL;
10550 * Make a copy of the original message
10552 mp2ctl = copymsg(mpctl);
10554 /* fixed length ICMPv6 structure ... */
10556 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10557 optp->level = MIB2_ICMP6;
10558 optp->name = 0;
10559 /* Include "unknown interface" icmp6_mib */
10560 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10561 MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10562 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10563 sizeof (mib2_ipv6IfIcmpEntry_t);
10564 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10565 (char *)&ipst->ips_icmp6_mib,
10566 (int)sizeof (ipst->ips_icmp6_mib))) {
10567 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10568 (uint_t)sizeof (ipst->ips_icmp6_mib)));
10571 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10572 ill = ILL_START_WALK_V6(&ctx, ipst);
10573 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10574 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10575 ill->ill_phyint->phyint_ifindex;
10576 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10577 (char *)ill->ill_icmp6_mib,
10578 (int)sizeof (*ill->ill_icmp6_mib))) {
10579 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10580 "%u bytes\n",
10581 (uint_t)sizeof (*ill->ill_icmp6_mib)));
10584 rw_exit(&ipst->ips_ill_g_lock);
10586 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10587 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10588 (int)optp->level, (int)optp->name, (int)optp->len));
10589 qreply(q, mpctl);
10590 return (mp2ctl);
10594 * ire_walk routine to create ipRouteEntryTable in one IRE walk
10596 static void
10597 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10599 ill_t *ill;
10600 mib2_ipRouteEntry_t *re;
10601 ip_stack_t *ipst = ire->ire_ipst;
10603 ASSERT(ire->ire_ipversion == IPV4_VERSION);
10605 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10606 if (ire->ire_testhidden)
10607 return;
10608 if (ire->ire_type & IRE_IF_CLONE)
10609 return;
10612 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10613 return;
10616 * Return all IRE types for route table... let caller pick and choose
10618 re->ipRouteDest = ire->ire_addr;
10619 ill = ire->ire_ill;
10620 re->ipRouteIfIndex.o_length = 0;
10621 if (ill != NULL) {
10622 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10623 re->ipRouteIfIndex.o_length =
10624 mi_strlen(re->ipRouteIfIndex.o_bytes);
10626 re->ipRouteMetric1 = -1;
10627 re->ipRouteMetric2 = -1;
10628 re->ipRouteMetric3 = -1;
10629 re->ipRouteMetric4 = -1;
10631 re->ipRouteNextHop = ire->ire_gateway_addr;
10632 /* indirect(4), direct(3), or invalid(2) */
10633 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10634 re->ipRouteType = 2;
10635 else if (ire->ire_type & IRE_ONLINK)
10636 re->ipRouteType = 3;
10637 else
10638 re->ipRouteType = 4;
10640 re->ipRouteProto = -1;
10641 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10642 re->ipRouteMask = ire->ire_mask;
10643 re->ipRouteMetric5 = -1;
10644 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10645 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10646 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10648 re->ipRouteInfo.re_frag_flag = 0;
10649 re->ipRouteInfo.re_rtt = 0;
10650 re->ipRouteInfo.re_src_addr = 0;
10651 re->ipRouteInfo.re_ref = ire->ire_refcnt;
10652 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10653 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10654 re->ipRouteInfo.re_flags = ire->ire_flags;
10656 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10657 if (ire->ire_type & IRE_INTERFACE) {
10658 ire_t *child;
10660 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10661 child = ire->ire_dep_children;
10662 while (child != NULL) {
10663 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10664 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10665 child = child->ire_dep_sib_next;
10667 rw_exit(&ipst->ips_ire_dep_lock);
10670 if (ire->ire_flags & RTF_DYNAMIC) {
10671 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT;
10672 } else {
10673 re->ipRouteInfo.re_ire_type = ire->ire_type;
10676 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
10677 (char *)re, (int)sizeof (*re))) {
10678 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
10679 (uint_t)sizeof (*re)));
10682 /* bump route index for next pass */
10683 ird->ird_idx++;
10685 kmem_free(re, sizeof (*re));
10689 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
10691 static void
10692 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
10694 ill_t *ill;
10695 mib2_ipv6RouteEntry_t *re;
10696 ip_stack_t *ipst = ire->ire_ipst;
10698 ASSERT(ire->ire_ipversion == IPV6_VERSION);
10700 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10701 if (ire->ire_testhidden)
10702 return;
10703 if (ire->ire_type & IRE_IF_CLONE)
10704 return;
10707 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10708 return;
10711 * Return all IRE types for route table... let caller pick and choose
10713 re->ipv6RouteDest = ire->ire_addr_v6;
10714 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
10715 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
10716 re->ipv6RouteIfIndex.o_length = 0;
10717 ill = ire->ire_ill;
10718 if (ill != NULL) {
10719 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
10720 re->ipv6RouteIfIndex.o_length =
10721 mi_strlen(re->ipv6RouteIfIndex.o_bytes);
10724 ASSERT(!(ire->ire_type & IRE_BROADCAST));
10726 mutex_enter(&ire->ire_lock);
10727 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
10728 mutex_exit(&ire->ire_lock);
10730 /* remote(4), local(3), or discard(2) */
10731 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10732 re->ipv6RouteType = 2;
10733 else if (ire->ire_type & IRE_ONLINK)
10734 re->ipv6RouteType = 3;
10735 else
10736 re->ipv6RouteType = 4;
10738 re->ipv6RouteProtocol = -1;
10739 re->ipv6RoutePolicy = 0;
10740 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time;
10741 re->ipv6RouteNextHopRDI = 0;
10742 re->ipv6RouteWeight = 0;
10743 re->ipv6RouteMetric = 0;
10744 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10745 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
10746 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10748 re->ipv6RouteInfo.re_frag_flag = 0;
10749 re->ipv6RouteInfo.re_rtt = 0;
10750 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros;
10751 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10752 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10753 re->ipv6RouteInfo.re_ref = ire->ire_refcnt;
10754 re->ipv6RouteInfo.re_flags = ire->ire_flags;
10756 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10757 if (ire->ire_type & IRE_INTERFACE) {
10758 ire_t *child;
10760 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10761 child = ire->ire_dep_children;
10762 while (child != NULL) {
10763 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
10764 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10765 child = child->ire_dep_sib_next;
10767 rw_exit(&ipst->ips_ire_dep_lock);
10769 if (ire->ire_flags & RTF_DYNAMIC) {
10770 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT;
10771 } else {
10772 re->ipv6RouteInfo.re_ire_type = ire->ire_type;
10775 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
10776 (char *)re, (int)sizeof (*re))) {
10777 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
10778 (uint_t)sizeof (*re)));
10781 /* bump route index for next pass */
10782 ird->ird_idx++;
10784 kmem_free(re, sizeof (*re));
10788 * ncec_walk routine to create ipv6NetToMediaEntryTable
10790 static void
10791 ip_snmp_get2_v6_media(ncec_t *ncec, void *ptr)
10793 iproutedata_t *ird = ptr;
10794 ill_t *ill;
10795 mib2_ipv6NetToMediaEntry_t ntme;
10797 ill = ncec->ncec_ill;
10798 /* skip arpce entries, and loopback ncec entries */
10799 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
10800 return;
10802 * Neighbor cache entry attached to IRE with on-link
10803 * destination.
10804 * We report all IPMP groups on ncec_ill which is normally the upper.
10806 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
10807 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
10808 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
10809 if (ncec->ncec_lladdr != NULL) {
10810 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
10811 ntme.ipv6NetToMediaPhysAddress.o_length);
10814 * Note: Returns ND_* states. Should be:
10815 * reachable(1), stale(2), delay(3), probe(4),
10816 * invalid(5), unknown(6)
10818 ntme.ipv6NetToMediaState = ncec->ncec_state;
10819 ntme.ipv6NetToMediaLastUpdated = 0;
10821 /* other(1), dynamic(2), static(3), local(4) */
10822 if (NCE_MYADDR(ncec)) {
10823 ntme.ipv6NetToMediaType = 4;
10824 } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
10825 ntme.ipv6NetToMediaType = 1; /* proxy */
10826 } else if (ncec->ncec_flags & NCE_F_STATIC) {
10827 ntme.ipv6NetToMediaType = 3;
10828 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
10829 ntme.ipv6NetToMediaType = 1;
10830 } else {
10831 ntme.ipv6NetToMediaType = 2;
10834 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
10835 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
10836 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
10837 (uint_t)sizeof (ntme)));
10842 nce2ace(ncec_t *ncec)
10844 int flags = 0;
10846 if (NCE_ISREACHABLE(ncec))
10847 flags |= ACE_F_RESOLVED;
10848 if (ncec->ncec_flags & NCE_F_AUTHORITY)
10849 flags |= ACE_F_AUTHORITY;
10850 if (ncec->ncec_flags & NCE_F_PUBLISH)
10851 flags |= ACE_F_PUBLISH;
10852 if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
10853 flags |= ACE_F_PERMANENT;
10854 if (NCE_MYADDR(ncec))
10855 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
10856 if (ncec->ncec_flags & NCE_F_UNVERIFIED)
10857 flags |= ACE_F_UNVERIFIED;
10858 if (ncec->ncec_flags & NCE_F_AUTHORITY)
10859 flags |= ACE_F_AUTHORITY;
10860 if (ncec->ncec_flags & NCE_F_DELAYED)
10861 flags |= ACE_F_DELAYED;
10862 return (flags);
10866 * ncec_walk routine to create ipNetToMediaEntryTable
10868 static void
10869 ip_snmp_get2_v4_media(ncec_t *ncec, void *ptr)
10871 iproutedata_t *ird = ptr;
10872 ill_t *ill;
10873 mib2_ipNetToMediaEntry_t ntme;
10874 const char *name = "unknown";
10875 ipaddr_t ncec_addr;
10877 ill = ncec->ncec_ill;
10878 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
10879 ill->ill_net_type == IRE_LOOPBACK)
10880 return;
10882 /* We report all IPMP groups on ncec_ill which is normally the upper. */
10883 name = ill->ill_name;
10884 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
10885 if (NCE_MYADDR(ncec)) {
10886 ntme.ipNetToMediaType = 4;
10887 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
10888 ntme.ipNetToMediaType = 1;
10889 } else {
10890 ntme.ipNetToMediaType = 3;
10892 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
10893 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
10894 ntme.ipNetToMediaIfIndex.o_length);
10896 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
10897 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
10899 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
10900 ncec_addr = INADDR_BROADCAST;
10901 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
10902 sizeof (ncec_addr));
10904 * map all the flags to the ACE counterpart.
10906 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
10908 ntme.ipNetToMediaPhysAddress.o_length =
10909 MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
10911 if (!NCE_ISREACHABLE(ncec))
10912 ntme.ipNetToMediaPhysAddress.o_length = 0;
10913 else {
10914 if (ncec->ncec_lladdr != NULL) {
10915 bcopy(ncec->ncec_lladdr,
10916 ntme.ipNetToMediaPhysAddress.o_bytes,
10917 ntme.ipNetToMediaPhysAddress.o_length);
10921 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
10922 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
10923 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
10924 (uint_t)sizeof (ntme)));
10929 * return (0) if invalid set request, 1 otherwise, including non-tcp requests
10931 /* ARGSUSED */
10933 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
10935 switch (level) {
10936 case MIB2_IP:
10937 case MIB2_ICMP:
10938 switch (name) {
10939 default:
10940 break;
10942 return (1);
10943 default:
10944 return (1);
10949 * When there exists both a 64- and 32-bit counter of a particular type
10950 * (i.e., InReceives), only the 64-bit counters are added.
10952 void
10953 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
10955 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
10956 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
10957 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
10958 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
10959 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
10960 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
10961 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
10962 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
10963 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
10964 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
10965 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
10966 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
10967 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
10968 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
10969 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
10970 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
10971 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
10972 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
10973 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
10974 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
10975 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
10976 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
10977 o2->ipIfStatsInWrongIPVersion);
10978 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
10979 o2->ipIfStatsInWrongIPVersion);
10980 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
10981 o2->ipIfStatsOutSwitchIPVersion);
10982 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
10983 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
10984 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
10985 o2->ipIfStatsHCInForwDatagrams);
10986 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
10987 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
10988 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
10989 o2->ipIfStatsHCOutForwDatagrams);
10990 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
10991 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
10992 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
10993 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
10994 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
10995 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
10996 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
10997 o2->ipIfStatsHCOutMcastOctets);
10998 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
10999 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11000 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11001 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11002 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11003 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11004 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11007 void
11008 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11010 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11011 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11012 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11013 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11014 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11015 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11016 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11017 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11018 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11019 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11020 o2->ipv6IfIcmpInRouterSolicits);
11021 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11022 o2->ipv6IfIcmpInRouterAdvertisements);
11023 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11024 o2->ipv6IfIcmpInNeighborSolicits);
11025 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11026 o2->ipv6IfIcmpInNeighborAdvertisements);
11027 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11028 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11029 o2->ipv6IfIcmpInGroupMembQueries);
11030 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11031 o2->ipv6IfIcmpInGroupMembResponses);
11032 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11033 o2->ipv6IfIcmpInGroupMembReductions);
11034 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11035 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11036 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11037 o2->ipv6IfIcmpOutDestUnreachs);
11038 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11039 o2->ipv6IfIcmpOutAdminProhibs);
11040 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11041 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11042 o2->ipv6IfIcmpOutParmProblems);
11043 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11044 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11045 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11046 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11047 o2->ipv6IfIcmpOutRouterSolicits);
11048 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11049 o2->ipv6IfIcmpOutRouterAdvertisements);
11050 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11051 o2->ipv6IfIcmpOutNeighborSolicits);
11052 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11053 o2->ipv6IfIcmpOutNeighborAdvertisements);
11054 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11055 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11056 o2->ipv6IfIcmpOutGroupMembQueries);
11057 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11058 o2->ipv6IfIcmpOutGroupMembResponses);
11059 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11060 o2->ipv6IfIcmpOutGroupMembReductions);
11061 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11062 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11063 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11064 o2->ipv6IfIcmpInBadNeighborAdvertisements);
11065 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11066 o2->ipv6IfIcmpInBadNeighborSolicitations);
11067 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11068 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11069 o2->ipv6IfIcmpInGroupMembTotal);
11070 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11071 o2->ipv6IfIcmpInGroupMembBadQueries);
11072 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11073 o2->ipv6IfIcmpInGroupMembBadReports);
11074 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11075 o2->ipv6IfIcmpInGroupMembOurReports);
11079 * Called before the options are updated to check if this packet will
11080 * be source routed from here.
11081 * This routine assumes that the options are well formed i.e. that they
11082 * have already been checked.
11084 boolean_t
11085 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11087 ipoptp_t opts;
11088 uchar_t *opt;
11089 uint8_t optval;
11090 uint8_t optlen;
11091 ipaddr_t dst;
11093 if (IS_SIMPLE_IPH(ipha)) {
11094 ip2dbg(("not source routed\n"));
11095 return (B_FALSE);
11097 dst = ipha->ipha_dst;
11098 for (optval = ipoptp_first(&opts, ipha);
11099 optval != IPOPT_EOL;
11100 optval = ipoptp_next(&opts)) {
11101 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11102 opt = opts.ipoptp_cur;
11103 optlen = opts.ipoptp_len;
11104 ip2dbg(("ip_source_routed: opt %d, len %d\n",
11105 optval, optlen));
11106 switch (optval) {
11107 uint32_t off;
11108 case IPOPT_SSRR:
11109 case IPOPT_LSRR:
11111 * If dst is one of our addresses and there are some
11112 * entries left in the source route return (true).
11114 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11115 ip2dbg(("ip_source_routed: not next"
11116 " source route 0x%x\n",
11117 ntohl(dst)));
11118 return (B_FALSE);
11120 off = opt[IPOPT_OFFSET];
11121 off--;
11122 if (optlen < IP_ADDR_LEN ||
11123 off > optlen - IP_ADDR_LEN) {
11124 /* End of source route */
11125 ip1dbg(("ip_source_routed: end of SR\n"));
11126 return (B_FALSE);
11128 return (B_TRUE);
11131 ip2dbg(("not source routed\n"));
11132 return (B_FALSE);
11136 * ip_unbind is called by the transports to remove a conn from
11137 * the fanout table.
11139 void
11140 ip_unbind(conn_t *connp)
11143 ASSERT(!MUTEX_HELD(&connp->conn_lock));
11145 ipcl_hash_remove(connp);
11149 * Used for deciding the MSS size for the upper layer. Thus
11150 * we need to check the outbound policy values in the conn.
11153 conn_ipsec_length(conn_t *connp)
11155 ipsec_latch_t *ipl;
11157 ipl = connp->conn_latch;
11158 if (ipl == NULL)
11159 return (0);
11161 if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11162 return (0);
11164 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11168 * Returns an estimate of the IPsec headers size. This is used if
11169 * we don't want to call into IPsec to get the exact size.
11172 ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11174 ipsec_action_t *a;
11176 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11177 return (0);
11179 a = ixa->ixa_ipsec_action;
11180 if (a == NULL) {
11181 ASSERT(ixa->ixa_ipsec_policy != NULL);
11182 a = ixa->ixa_ipsec_policy->ipsp_act;
11184 ASSERT(a != NULL);
11186 return (a->ipa_ovhd);
11190 * If there are any source route options, return the true final
11191 * destination. Otherwise, return the destination.
11193 ipaddr_t
11194 ip_get_dst(ipha_t *ipha)
11196 ipoptp_t opts;
11197 uchar_t *opt;
11198 uint8_t optval;
11199 uint8_t optlen;
11200 ipaddr_t dst;
11201 uint32_t off;
11203 dst = ipha->ipha_dst;
11205 if (IS_SIMPLE_IPH(ipha))
11206 return (dst);
11208 for (optval = ipoptp_first(&opts, ipha);
11209 optval != IPOPT_EOL;
11210 optval = ipoptp_next(&opts)) {
11211 opt = opts.ipoptp_cur;
11212 optlen = opts.ipoptp_len;
11213 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11214 switch (optval) {
11215 case IPOPT_SSRR:
11216 case IPOPT_LSRR:
11217 off = opt[IPOPT_OFFSET];
11219 * If one of the conditions is true, it means
11220 * end of options and dst already has the right
11221 * value.
11223 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11224 off = optlen - IP_ADDR_LEN;
11225 bcopy(&opt[off], &dst, IP_ADDR_LEN);
11227 return (dst);
11228 default:
11229 break;
11233 return (dst);
11237 * Outbound IP fragmentation routine.
11238 * Assumes the caller has checked whether or not fragmentation should
11239 * be allowed. Here we copy the DF bit from the header to all the generated
11240 * fragments.
11243 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11244 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11245 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11247 int i1;
11248 int hdr_len;
11249 mblk_t *hdr_mp;
11250 ipha_t *ipha;
11251 int ip_data_end;
11252 int len;
11253 mblk_t *mp = mp_orig;
11254 int offset;
11255 ill_t *ill = nce->nce_ill;
11256 ip_stack_t *ipst = ill->ill_ipst;
11257 mblk_t *carve_mp;
11258 uint32_t frag_flag;
11259 uint_t priority = mp->b_band;
11260 int error = 0;
11262 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11264 if (pkt_len != msgdsize(mp)) {
11265 ip0dbg(("Packet length mismatch: %d, %ld\n",
11266 pkt_len, msgdsize(mp)));
11267 freemsg(mp);
11268 return (EINVAL);
11271 if (max_frag == 0) {
11272 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11273 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11274 ip_drop_output("FragFails: zero max_frag", mp, ill);
11275 freemsg(mp);
11276 return (EINVAL);
11279 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11280 ipha = (ipha_t *)mp->b_rptr;
11281 ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11282 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11285 * Establish the starting offset. May not be zero if we are fragging
11286 * a fragment that is being forwarded.
11288 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11290 /* TODO why is this test needed? */
11291 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11292 /* TODO: notify ulp somehow */
11293 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11294 ip_drop_output("FragFails: bad starting offset", mp, ill);
11295 freemsg(mp);
11296 return (EINVAL);
11299 hdr_len = IPH_HDR_LENGTH(ipha);
11300 ipha->ipha_hdr_checksum = 0;
11303 * Establish the number of bytes maximum per frag, after putting
11304 * in the header.
11306 len = (max_frag - hdr_len) & ~7;
11308 /* Get a copy of the header for the trailing frags */
11309 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11310 mp);
11311 if (hdr_mp == NULL) {
11312 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11313 ip_drop_output("FragFails: no hdr_mp", mp, ill);
11314 freemsg(mp);
11315 return (ENOBUFS);
11318 /* Store the starting offset, with the MoreFrags flag. */
11319 i1 = offset | IPH_MF | frag_flag;
11320 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11322 /* Establish the ending byte offset, based on the starting offset. */
11323 offset <<= 3;
11324 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11326 /* Store the length of the first fragment in the IP header. */
11327 i1 = len + hdr_len;
11328 ASSERT(i1 <= IP_MAXPACKET);
11329 ipha->ipha_length = htons((uint16_t)i1);
11332 * Compute the IP header checksum for the first frag. We have to
11333 * watch out that we stop at the end of the header.
11335 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11338 * Now carve off the first frag. Note that this will include the
11339 * original IP header.
11341 if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11342 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11343 ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11344 freeb(hdr_mp);
11345 freemsg(mp_orig);
11346 return (ENOBUFS);
11349 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11351 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11352 ixa_cookie);
11353 if (error != 0 && error != EWOULDBLOCK) {
11354 /* No point in sending the other fragments */
11355 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11356 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11357 freeb(hdr_mp);
11358 freemsg(mp_orig);
11359 return (error);
11362 /* No need to redo state machine in loop */
11363 ixaflags &= ~IXAF_REACH_CONF;
11365 /* Advance the offset to the second frag starting point. */
11366 offset += len;
11368 * Update hdr_len from the copied header - there might be less options
11369 * in the later fragments.
11371 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11372 /* Loop until done. */
11373 for (;;) {
11374 uint16_t offset_and_flags;
11375 uint16_t ip_len;
11377 if (ip_data_end - offset > len) {
11379 * Carve off the appropriate amount from the original
11380 * datagram.
11382 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11383 mp = NULL;
11384 break;
11387 * More frags after this one. Get another copy
11388 * of the header.
11390 if (carve_mp->b_datap->db_ref == 1 &&
11391 hdr_mp->b_wptr - hdr_mp->b_rptr <
11392 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11393 /* Inline IP header */
11394 carve_mp->b_rptr -= hdr_mp->b_wptr -
11395 hdr_mp->b_rptr;
11396 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11397 hdr_mp->b_wptr - hdr_mp->b_rptr);
11398 mp = carve_mp;
11399 } else {
11400 if (!(mp = copyb(hdr_mp))) {
11401 freemsg(carve_mp);
11402 break;
11404 /* Get priority marking, if any. */
11405 mp->b_band = priority;
11406 mp->b_cont = carve_mp;
11408 ipha = (ipha_t *)mp->b_rptr;
11409 offset_and_flags = IPH_MF;
11410 } else {
11412 * Last frag. Consume the header. Set len to
11413 * the length of this last piece.
11415 len = ip_data_end - offset;
11418 * Carve off the appropriate amount from the original
11419 * datagram.
11421 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11422 mp = NULL;
11423 break;
11425 if (carve_mp->b_datap->db_ref == 1 &&
11426 hdr_mp->b_wptr - hdr_mp->b_rptr <
11427 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11428 /* Inline IP header */
11429 carve_mp->b_rptr -= hdr_mp->b_wptr -
11430 hdr_mp->b_rptr;
11431 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11432 hdr_mp->b_wptr - hdr_mp->b_rptr);
11433 mp = carve_mp;
11434 freeb(hdr_mp);
11435 hdr_mp = mp;
11436 } else {
11437 mp = hdr_mp;
11438 /* Get priority marking, if any. */
11439 mp->b_band = priority;
11440 mp->b_cont = carve_mp;
11442 ipha = (ipha_t *)mp->b_rptr;
11443 /* A frag of a frag might have IPH_MF non-zero */
11444 offset_and_flags =
11445 ntohs(ipha->ipha_fragment_offset_and_flags) &
11446 IPH_MF;
11448 offset_and_flags |= (uint16_t)(offset >> 3);
11449 offset_and_flags |= (uint16_t)frag_flag;
11450 /* Store the offset and flags in the IP header. */
11451 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11453 /* Store the length in the IP header. */
11454 ip_len = (uint16_t)(len + hdr_len);
11455 ipha->ipha_length = htons(ip_len);
11458 * Set the IP header checksum. Note that mp is just
11459 * the header, so this is easy to pass to ip_csum.
11461 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11463 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11465 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11466 nolzid, ixa_cookie);
11467 /* All done if we just consumed the hdr_mp. */
11468 if (mp == hdr_mp) {
11469 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11470 return (error);
11472 if (error != 0 && error != EWOULDBLOCK) {
11473 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11474 mblk_t *, hdr_mp);
11475 /* No point in sending the other fragments */
11476 break;
11479 /* Otherwise, advance and loop. */
11480 offset += len;
11482 /* Clean up following allocation failure. */
11483 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11484 ip_drop_output("FragFails: loop ended", NULL, ill);
11485 if (mp != hdr_mp)
11486 freeb(hdr_mp);
11487 if (mp != mp_orig)
11488 freemsg(mp_orig);
11489 return (error);
11493 * Copy the header plus those options which have the copy bit set
11495 static mblk_t *
11496 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11497 mblk_t *src)
11499 mblk_t *mp;
11500 uchar_t *up;
11503 * Quick check if we need to look for options without the copy bit
11504 * set
11506 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11507 if (!mp)
11508 return (mp);
11509 mp->b_rptr += ipst->ips_ip_wroff_extra;
11510 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11511 bcopy(rptr, mp->b_rptr, hdr_len);
11512 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11513 return (mp);
11515 up = mp->b_rptr;
11516 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11517 up += IP_SIMPLE_HDR_LENGTH;
11518 rptr += IP_SIMPLE_HDR_LENGTH;
11519 hdr_len -= IP_SIMPLE_HDR_LENGTH;
11520 while (hdr_len > 0) {
11521 uint32_t optval;
11522 uint32_t optlen;
11524 optval = *rptr;
11525 if (optval == IPOPT_EOL)
11526 break;
11527 if (optval == IPOPT_NOP)
11528 optlen = 1;
11529 else
11530 optlen = rptr[1];
11531 if (optval & IPOPT_COPY) {
11532 bcopy(rptr, up, optlen);
11533 up += optlen;
11535 rptr += optlen;
11536 hdr_len -= optlen;
11539 * Make sure that we drop an even number of words by filling
11540 * with EOL to the next word boundary.
11542 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11543 hdr_len & 0x3; hdr_len++)
11544 *up++ = IPOPT_EOL;
11545 mp->b_wptr = up;
11546 /* Update header length */
11547 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11548 return (mp);
11552 * Update any source route, record route, or timestamp options when
11553 * sending a packet back to ourselves.
11554 * Check that we are at end of strict source route.
11555 * The options have been sanity checked by ip_output_options().
11557 void
11558 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11560 ipoptp_t opts;
11561 uchar_t *opt;
11562 uint8_t optval;
11563 uint8_t optlen;
11564 ipaddr_t dst;
11565 uint32_t ts;
11566 timestruc_t now;
11568 for (optval = ipoptp_first(&opts, ipha);
11569 optval != IPOPT_EOL;
11570 optval = ipoptp_next(&opts)) {
11571 opt = opts.ipoptp_cur;
11572 optlen = opts.ipoptp_len;
11573 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11574 switch (optval) {
11575 uint32_t off;
11576 case IPOPT_SSRR:
11577 case IPOPT_LSRR:
11578 off = opt[IPOPT_OFFSET];
11579 off--;
11580 if (optlen < IP_ADDR_LEN ||
11581 off > optlen - IP_ADDR_LEN) {
11582 /* End of source route */
11583 break;
11586 * This will only happen if two consecutive entries
11587 * in the source route contains our address or if
11588 * it is a packet with a loose source route which
11589 * reaches us before consuming the whole source route
11592 if (optval == IPOPT_SSRR) {
11593 return;
11596 * Hack: instead of dropping the packet truncate the
11597 * source route to what has been used by filling the
11598 * rest with IPOPT_NOP.
11600 opt[IPOPT_OLEN] = (uint8_t)off;
11601 while (off < optlen) {
11602 opt[off++] = IPOPT_NOP;
11604 break;
11605 case IPOPT_RR:
11606 off = opt[IPOPT_OFFSET];
11607 off--;
11608 if (optlen < IP_ADDR_LEN ||
11609 off > optlen - IP_ADDR_LEN) {
11610 /* No more room - ignore */
11611 ip1dbg((
11612 "ip_output_local_options: end of RR\n"));
11613 break;
11615 dst = htonl(INADDR_LOOPBACK);
11616 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11617 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11618 break;
11619 case IPOPT_TS:
11620 /* Insert timestamp if there is romm */
11621 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11622 case IPOPT_TS_TSONLY:
11623 off = IPOPT_TS_TIMELEN;
11624 break;
11625 case IPOPT_TS_PRESPEC:
11626 case IPOPT_TS_PRESPEC_RFC791:
11627 /* Verify that the address matched */
11628 off = opt[IPOPT_OFFSET] - 1;
11629 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
11630 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11631 /* Not for us */
11632 break;
11634 /* FALLTHROUGH */
11635 case IPOPT_TS_TSANDADDR:
11636 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
11637 break;
11638 default:
11640 * ip_*put_options should have already
11641 * dropped this packet.
11643 cmn_err(CE_PANIC, "ip_output_local_options: "
11644 "unknown IT - bug in ip_output_options?\n");
11645 return; /* Keep "lint" happy */
11647 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
11648 /* Increase overflow counter */
11649 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
11650 opt[IPOPT_POS_OV_FLG] = (uint8_t)
11651 (opt[IPOPT_POS_OV_FLG] & 0x0F) |
11652 (off << 4);
11653 break;
11655 off = opt[IPOPT_OFFSET] - 1;
11656 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11657 case IPOPT_TS_PRESPEC:
11658 case IPOPT_TS_PRESPEC_RFC791:
11659 case IPOPT_TS_TSANDADDR:
11660 dst = htonl(INADDR_LOOPBACK);
11661 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11662 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11663 /* FALLTHROUGH */
11664 case IPOPT_TS_TSONLY:
11665 off = opt[IPOPT_OFFSET] - 1;
11666 /* Compute # of milliseconds since midnight */
11667 gethrestime(&now);
11668 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
11669 NSEC2MSEC(now.tv_nsec);
11670 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
11671 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
11672 break;
11674 break;
11680 * Prepend an M_DATA fastpath header, and if none present prepend a
11681 * DL_UNITDATA_REQ. Frees the mblk on failure.
11683 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
11684 * If there is a change to them, the nce will be deleted (condemned) and
11685 * a new nce_t will be created when packets are sent. Thus we need no locks
11686 * to access those fields.
11688 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
11689 * we place b_band in dl_priority.dl_max.
11691 static mblk_t *
11692 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
11694 uint_t hlen;
11695 mblk_t *mp1;
11696 uint_t priority;
11697 uchar_t *rptr;
11699 rptr = mp->b_rptr;
11701 ASSERT(DB_TYPE(mp) == M_DATA);
11702 priority = mp->b_band;
11704 ASSERT(nce != NULL);
11705 if ((mp1 = nce->nce_fp_mp) != NULL) {
11706 hlen = MBLKL(mp1);
11708 * Check if we have enough room to prepend fastpath
11709 * header
11711 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
11712 rptr -= hlen;
11713 bcopy(mp1->b_rptr, rptr, hlen);
11715 * Set the b_rptr to the start of the link layer
11716 * header
11718 mp->b_rptr = rptr;
11719 return (mp);
11721 mp1 = copyb(mp1);
11722 if (mp1 == NULL) {
11723 ill_t *ill = nce->nce_ill;
11725 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
11726 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
11727 freemsg(mp);
11728 return (NULL);
11730 mp1->b_band = priority;
11731 mp1->b_cont = mp;
11732 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
11733 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
11734 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
11735 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
11736 DB_LSOMSS(mp1) = DB_LSOMSS(mp);
11737 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
11739 * XXX disable ICK_VALID and compute checksum
11740 * here; can happen if nce_fp_mp changes and
11741 * it can't be copied now due to insufficient
11742 * space. (unlikely, fp mp can change, but it
11743 * does not increase in length)
11745 return (mp1);
11747 mp1 = copyb(nce->nce_dlur_mp);
11749 if (mp1 == NULL) {
11750 ill_t *ill = nce->nce_ill;
11752 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
11753 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
11754 freemsg(mp);
11755 return (NULL);
11757 mp1->b_cont = mp;
11758 if (priority != 0) {
11759 mp1->b_band = priority;
11760 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
11761 priority;
11763 return (mp1);
11767 * Finish the outbound IPsec processing. This function is called from
11768 * ipsec_out_process() if the IPsec packet was processed
11769 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
11770 * asynchronously.
11772 * This is common to IPv4 and IPv6.
11775 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
11777 iaflags_t ixaflags = ixa->ixa_flags;
11778 uint_t pktlen;
11781 /* AH/ESP don't update ixa_pktlen when they modify the packet */
11782 if (ixaflags & IXAF_IS_IPV4) {
11783 ipha_t *ipha = (ipha_t *)mp->b_rptr;
11785 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
11786 pktlen = ntohs(ipha->ipha_length);
11787 } else {
11788 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
11790 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
11791 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
11795 * We release any hard reference on the SAs here to make
11796 * sure the SAs can be garbage collected. ipsr_sa has a soft reference
11797 * on the SAs.
11798 * If in the future we want the hard latching of the SAs in the
11799 * ip_xmit_attr_t then we should remove this.
11801 if (ixa->ixa_ipsec_esp_sa != NULL) {
11802 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
11803 ixa->ixa_ipsec_esp_sa = NULL;
11805 if (ixa->ixa_ipsec_ah_sa != NULL) {
11806 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
11807 ixa->ixa_ipsec_ah_sa = NULL;
11810 /* Do we need to fragment? */
11811 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
11812 pktlen > ixa->ixa_fragsize) {
11813 if (ixaflags & IXAF_IS_IPV4) {
11814 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
11816 * We check for the DF case in ipsec_out_process
11817 * hence this only handles the non-DF case.
11819 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
11820 pktlen, ixa->ixa_fragsize,
11821 ixa->ixa_xmit_hint, ixa->ixa_zoneid,
11822 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
11823 &ixa->ixa_cookie));
11824 } else {
11825 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
11826 if (mp == NULL) {
11827 /* MIB and ip_drop_output already done */
11828 return (ENOMEM);
11830 pktlen += sizeof (ip6_frag_t);
11831 if (pktlen > ixa->ixa_fragsize) {
11832 return (ip_fragment_v6(mp, ixa->ixa_nce,
11833 ixa->ixa_flags, pktlen,
11834 ixa->ixa_fragsize, ixa->ixa_xmit_hint,
11835 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
11836 ixa->ixa_postfragfn, &ixa->ixa_cookie));
11840 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
11841 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
11842 ixa->ixa_no_loop_zoneid, NULL));
11846 * Finish the inbound IPsec processing. This function is called from
11847 * ipsec_out_process() if the IPsec packet was processed
11848 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
11849 * asynchronously.
11851 * This is common to IPv4 and IPv6.
11853 void
11854 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
11856 iaflags_t iraflags = ira->ira_flags;
11858 /* Length might have changed */
11859 if (iraflags & IRAF_IS_IPV4) {
11860 ipha_t *ipha = (ipha_t *)mp->b_rptr;
11862 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
11863 ira->ira_pktlen = ntohs(ipha->ipha_length);
11864 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
11865 ira->ira_protocol = ipha->ipha_protocol;
11867 ip_fanout_v4(mp, ipha, ira);
11868 } else {
11869 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
11870 uint8_t *nexthdrp;
11872 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
11873 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
11874 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
11875 &nexthdrp)) {
11876 /* Malformed packet */
11877 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
11878 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
11879 freemsg(mp);
11880 return;
11882 ira->ira_protocol = *nexthdrp;
11883 ip_fanout_v6(mp, ip6h, ira);
11888 * Select which AH & ESP SA's to use (if any) for the outbound packet.
11890 * If this function returns B_TRUE, the requested SA's have been filled
11891 * into the ixa_ipsec_*_sa pointers.
11893 * If the function returns B_FALSE, the packet has been "consumed", most
11894 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
11896 * The SA references created by the protocol-specific "select"
11897 * function will be released in ip_output_post_ipsec.
11899 static boolean_t
11900 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
11902 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
11903 ipsec_policy_t *pp;
11904 ipsec_action_t *ap;
11906 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
11907 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
11908 (ixa->ixa_ipsec_action != NULL));
11910 ap = ixa->ixa_ipsec_action;
11911 if (ap == NULL) {
11912 pp = ixa->ixa_ipsec_policy;
11913 ASSERT(pp != NULL);
11914 ap = pp->ipsp_act;
11915 ASSERT(ap != NULL);
11919 * We have an action. now, let's select SA's.
11920 * A side effect of setting ixa_ipsec_*_sa is that it will
11921 * be cached in the conn_t.
11923 if (ap->ipa_want_esp) {
11924 if (ixa->ixa_ipsec_esp_sa == NULL) {
11925 need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
11926 IPPROTO_ESP);
11928 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
11931 if (ap->ipa_want_ah) {
11932 if (ixa->ixa_ipsec_ah_sa == NULL) {
11933 need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
11934 IPPROTO_AH);
11936 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
11938 * The ESP and AH processing order needs to be preserved
11939 * when both protocols are required (ESP should be applied
11940 * before AH for an outbound packet). Force an ESP ACQUIRE
11941 * when both ESP and AH are required, and an AH ACQUIRE
11942 * is needed.
11944 if (ap->ipa_want_esp && need_ah_acquire)
11945 need_esp_acquire = B_TRUE;
11949 * Send an ACQUIRE (extended, regular, or both) if we need one.
11950 * Release SAs that got referenced, but will not be used until we
11951 * acquire _all_ of the SAs we need.
11953 if (need_ah_acquire || need_esp_acquire) {
11954 if (ixa->ixa_ipsec_ah_sa != NULL) {
11955 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
11956 ixa->ixa_ipsec_ah_sa = NULL;
11958 if (ixa->ixa_ipsec_esp_sa != NULL) {
11959 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
11960 ixa->ixa_ipsec_esp_sa = NULL;
11963 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
11964 return (B_FALSE);
11967 return (B_TRUE);
11971 * Handle IPsec output processing.
11972 * This function is only entered once for a given packet.
11973 * We try to do things synchronously, but if we need to have user-level
11974 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
11975 * will be completed
11976 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
11977 * - when asynchronous ESP is done it will do AH
11979 * In all cases we come back in ip_output_post_ipsec() to fragment and
11980 * send out the packet.
11983 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
11985 ill_t *ill = ixa->ixa_nce->nce_ill;
11986 ip_stack_t *ipst = ixa->ixa_ipst;
11987 ipsec_stack_t *ipss;
11988 ipsec_policy_t *pp;
11989 ipsec_action_t *ap;
11991 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
11993 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
11994 (ixa->ixa_ipsec_action != NULL));
11996 ipss = ipst->ips_netstack->netstack_ipsec;
11997 if (!ipsec_loaded(ipss)) {
11998 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
11999 ip_drop_packet(mp, B_TRUE, ill,
12000 DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12001 &ipss->ipsec_dropper);
12002 return (ENOTSUP);
12005 ap = ixa->ixa_ipsec_action;
12006 if (ap == NULL) {
12007 pp = ixa->ixa_ipsec_policy;
12008 ASSERT(pp != NULL);
12009 ap = pp->ipsp_act;
12010 ASSERT(ap != NULL);
12013 /* Handle explicit drop action and bypass. */
12014 switch (ap->ipa_act.ipa_type) {
12015 case IPSEC_ACT_DISCARD:
12016 case IPSEC_ACT_REJECT:
12017 ip_drop_packet(mp, B_FALSE, ill,
12018 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12019 return (EHOSTUNREACH); /* IPsec policy failure */
12020 case IPSEC_ACT_BYPASS:
12021 return (ip_output_post_ipsec(mp, ixa));
12025 * The order of processing is first insert a IP header if needed.
12026 * Then insert the ESP header and then the AH header.
12028 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12030 * First get the outer IP header before sending
12031 * it to ESP.
12033 ipha_t *oipha, *iipha;
12034 mblk_t *outer_mp, *inner_mp;
12036 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12037 (void) mi_strlog(ill->ill_rq, 0,
12038 SL_ERROR|SL_TRACE|SL_CONSOLE,
12039 "ipsec_out_process: "
12040 "Self-Encapsulation failed: Out of memory\n");
12041 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12042 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12043 freemsg(mp);
12044 return (ENOBUFS);
12046 inner_mp = mp;
12047 ASSERT(inner_mp->b_datap->db_type == M_DATA);
12048 oipha = (ipha_t *)outer_mp->b_rptr;
12049 iipha = (ipha_t *)inner_mp->b_rptr;
12050 *oipha = *iipha;
12051 outer_mp->b_wptr += sizeof (ipha_t);
12052 oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12053 sizeof (ipha_t));
12054 oipha->ipha_protocol = IPPROTO_ENCAP;
12055 oipha->ipha_version_and_hdr_length =
12056 IP_SIMPLE_HDR_VERSION;
12057 oipha->ipha_hdr_checksum = 0;
12058 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12059 outer_mp->b_cont = inner_mp;
12060 mp = outer_mp;
12062 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12065 /* If we need to wait for a SA then we can't return any errno */
12066 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12067 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12068 !ipsec_out_select_sa(mp, ixa))
12069 return (0);
12072 * By now, we know what SA's to use. Toss over to ESP & AH
12073 * to do the heavy lifting.
12075 if (ap->ipa_want_esp) {
12076 ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12078 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12079 if (mp == NULL) {
12081 * Either it failed or is pending. In the former case
12082 * ipIfStatsInDiscards was increased.
12084 return (0);
12088 if (ap->ipa_want_ah) {
12089 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12091 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12092 if (mp == NULL) {
12094 * Either it failed or is pending. In the former case
12095 * ipIfStatsInDiscards was increased.
12097 return (0);
12101 * We are done with IPsec processing. Send it over
12102 * the wire.
12104 return (ip_output_post_ipsec(mp, ixa));
12108 * ioctls that go through a down/up sequence may need to wait for the down
12109 * to complete. This involves waiting for the ire and ipif refcnts to go down
12110 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12112 /* ARGSUSED */
12113 void
12114 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12116 struct iocblk *iocp;
12117 mblk_t *mp1;
12118 ip_ioctl_cmd_t *ipip;
12119 int err;
12120 sin_t *sin;
12121 struct lifreq *lifr;
12122 struct ifreq *ifr;
12124 iocp = (struct iocblk *)mp->b_rptr;
12125 ASSERT(ipsq != NULL);
12126 /* Existence of mp1 verified in ip_wput_nondata */
12127 mp1 = mp->b_cont->b_cont;
12128 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12129 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12131 * Special case where ipx_current_ipif is not set:
12132 * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12133 * We are here as were not able to complete the operation in
12134 * ipif_set_values because we could not become exclusive on
12135 * the new ipsq.
12137 ill_t *ill = q->q_ptr;
12138 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12140 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12142 if (ipip->ipi_cmd_type == IF_CMD) {
12143 /* This a old style SIOC[GS]IF* command */
12144 ifr = (struct ifreq *)mp1->b_rptr;
12145 sin = (sin_t *)&ifr->ifr_addr;
12146 } else if (ipip->ipi_cmd_type == LIF_CMD) {
12147 /* This a new style SIOC[GS]LIF* command */
12148 lifr = (struct lifreq *)mp1->b_rptr;
12149 sin = (sin_t *)&lifr->lifr_addr;
12150 } else {
12151 sin = NULL;
12154 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12155 q, mp, ipip, mp1->b_rptr);
12157 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12158 int, ipip->ipi_cmd,
12159 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12160 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12162 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12166 * ioctl processing
12168 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12169 * the ioctl command in the ioctl tables, determines the copyin data size
12170 * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12172 * ioctl processing then continues when the M_IOCDATA makes its way down to
12173 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its
12174 * associated 'conn' is refheld till the end of the ioctl and the general
12175 * ioctl processing function ip_process_ioctl() is called to extract the
12176 * arguments and process the ioctl. To simplify extraction, ioctl commands
12177 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12178 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12179 * is used to extract the ioctl's arguments.
12181 * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12182 * so goes thru the serialization primitive ipsq_try_enter. Then the
12183 * appropriate function to handle the ioctl is called based on the entry in
12184 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12185 * which also refreleases the 'conn' that was refheld at the start of the
12186 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12188 * Many exclusive ioctls go thru an internal down up sequence as part of
12189 * the operation. For example an attempt to change the IP address of an
12190 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12191 * does all the cleanup such as deleting all ires that use this address.
12192 * Then we need to wait till all references to the interface go away.
12194 void
12195 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12197 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12198 ip_ioctl_cmd_t *ipip = arg;
12199 ip_extract_func_t *extract_funcp;
12200 cmd_info_t ci;
12201 int err;
12202 boolean_t entered_ipsq = B_FALSE;
12204 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12206 if (ipip == NULL)
12207 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12210 * SIOCLIFADDIF needs to go thru a special path since the
12211 * ill may not exist yet. This happens in the case of lo0
12212 * which is created using this ioctl.
12214 if (ipip->ipi_cmd == SIOCLIFADDIF) {
12215 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12216 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12217 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12218 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12219 return;
12222 ci.ci_ipif = NULL;
12223 switch (ipip->ipi_cmd_type) {
12224 case MISC_CMD:
12225 case MSFILT_CMD:
12227 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12229 if (ipip->ipi_cmd == IF_UNITSEL) {
12230 /* ioctl comes down the ill */
12231 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12232 ipif_refhold(ci.ci_ipif);
12234 err = 0;
12235 ci.ci_sin = NULL;
12236 ci.ci_sin6 = NULL;
12237 ci.ci_lifr = NULL;
12238 extract_funcp = NULL;
12239 break;
12241 case IF_CMD:
12242 case LIF_CMD:
12243 extract_funcp = ip_extract_lifreq;
12244 break;
12246 case ARP_CMD:
12247 case XARP_CMD:
12248 extract_funcp = ip_extract_arpreq;
12249 break;
12251 default:
12252 ASSERT(0);
12255 if (extract_funcp != NULL) {
12256 err = (*extract_funcp)(q, mp, ipip, &ci);
12257 if (err != 0) {
12258 DTRACE_PROBE4(ipif__ioctl,
12259 char *, "ip_process_ioctl finish err",
12260 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12261 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12262 return;
12266 * All of the extraction functions return a refheld ipif.
12268 ASSERT(ci.ci_ipif != NULL);
12271 if (!(ipip->ipi_flags & IPI_WR)) {
12273 * A return value of EINPROGRESS means the ioctl is
12274 * either queued and waiting for some reason or has
12275 * already completed.
12277 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12278 ci.ci_lifr);
12279 if (ci.ci_ipif != NULL) {
12280 DTRACE_PROBE4(ipif__ioctl,
12281 char *, "ip_process_ioctl finish RD",
12282 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12283 ipif_t *, ci.ci_ipif);
12284 ipif_refrele(ci.ci_ipif);
12285 } else {
12286 DTRACE_PROBE4(ipif__ioctl,
12287 char *, "ip_process_ioctl finish RD",
12288 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12290 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12291 return;
12294 ASSERT(ci.ci_ipif != NULL);
12297 * If ipsq is non-NULL, we are already being called exclusively
12299 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12300 if (ipsq == NULL) {
12301 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12302 NEW_OP, B_TRUE);
12303 if (ipsq == NULL) {
12304 ipif_refrele(ci.ci_ipif);
12305 return;
12307 entered_ipsq = B_TRUE;
12310 * Release the ipif so that ipif_down and friends that wait for
12311 * references to go away are not misled about the current ipif_refcnt
12312 * values. We are writer so we can access the ipif even after releasing
12313 * the ipif.
12315 ipif_refrele(ci.ci_ipif);
12317 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12320 * A return value of EINPROGRESS means the ioctl is
12321 * either queued and waiting for some reason or has
12322 * already completed.
12324 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12326 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12327 int, ipip->ipi_cmd,
12328 ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12329 ipif_t *, ci.ci_ipif);
12330 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12332 if (entered_ipsq)
12333 ipsq_exit(ipsq);
12337 * Complete the ioctl. Typically ioctls use the mi package and need to
12338 * do mi_copyout/mi_copy_done.
12340 void
12341 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12343 conn_t *connp = NULL;
12345 if (err == EINPROGRESS)
12346 return;
12348 if (CONN_Q(q)) {
12349 connp = Q_TO_CONN(q);
12350 ASSERT(connp->conn_ref >= 2);
12353 switch (mode) {
12354 case COPYOUT:
12355 if (err == 0)
12356 mi_copyout(q, mp);
12357 else
12358 mi_copy_done(q, mp, err);
12359 break;
12361 case NO_COPYOUT:
12362 mi_copy_done(q, mp, err);
12363 break;
12365 default:
12366 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */
12367 break;
12371 * The conn refhold and ioctlref placed on the conn at the start of the
12372 * ioctl are released here.
12374 if (connp != NULL) {
12375 CONN_DEC_IOCTLREF(connp);
12376 CONN_OPER_PENDING_DONE(connp);
12379 if (ipsq != NULL)
12380 ipsq_current_finish(ipsq);
12383 /* Handles all non data messages */
12385 ip_wput_nondata(queue_t *q, mblk_t *mp)
12387 mblk_t *mp1;
12388 struct iocblk *iocp;
12389 ip_ioctl_cmd_t *ipip;
12390 conn_t *connp;
12391 cred_t *cr;
12392 char *proto_str;
12394 if (CONN_Q(q))
12395 connp = Q_TO_CONN(q);
12396 else
12397 connp = NULL;
12399 switch (DB_TYPE(mp)) {
12400 case M_IOCTL:
12402 * IOCTL processing begins in ip_sioctl_copyin_setup which
12403 * will arrange to copy in associated control structures.
12405 ip_sioctl_copyin_setup(q, mp);
12406 return (0);
12407 case M_IOCDATA:
12409 * Ensure that this is associated with one of our trans-
12410 * parent ioctls. If it's not ours, discard it if we're
12411 * running as a driver, or pass it on if we're a module.
12413 iocp = (struct iocblk *)mp->b_rptr;
12414 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12415 if (ipip == NULL) {
12416 if (q->q_next == NULL) {
12417 goto nak;
12418 } else {
12419 putnext(q, mp);
12421 return (0);
12423 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12425 * The ioctl is one we recognise, but is not consumed
12426 * by IP as a module and we are a module, so we drop
12428 goto nak;
12431 /* IOCTL continuation following copyin or copyout. */
12432 if (mi_copy_state(q, mp, NULL) == -1) {
12434 * The copy operation failed. mi_copy_state already
12435 * cleaned up, so we're out of here.
12437 return (0);
12440 * If we just completed a copy in, we become writer and
12441 * continue processing in ip_sioctl_copyin_done. If it
12442 * was a copy out, we call mi_copyout again. If there is
12443 * nothing more to copy out, it will complete the IOCTL.
12445 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12446 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12447 mi_copy_done(q, mp, EPROTO);
12448 return (0);
12451 * Check for cases that need more copying. A return
12452 * value of 0 means a second copyin has been started,
12453 * so we return; a return value of 1 means no more
12454 * copying is needed, so we continue.
12456 if (ipip->ipi_cmd_type == MSFILT_CMD &&
12457 MI_COPY_COUNT(mp) == 1) {
12458 if (ip_copyin_msfilter(q, mp) == 0)
12459 return (0);
12462 * Refhold the conn, till the ioctl completes. This is
12463 * needed in case the ioctl ends up in the pending mp
12464 * list. Every mp in the ipx_pending_mp list must have
12465 * a refhold on the conn to resume processing. The
12466 * refhold is released when the ioctl completes
12467 * (whether normally or abnormally). An ioctlref is also
12468 * placed on the conn to prevent TCP from removing the
12469 * queue needed to send the ioctl reply back.
12470 * In all cases ip_ioctl_finish is called to finish
12471 * the ioctl and release the refholds.
12473 if (connp != NULL) {
12474 /* This is not a reentry */
12475 CONN_INC_REF(connp);
12476 CONN_INC_IOCTLREF(connp);
12477 } else {
12478 if (!(ipip->ipi_flags & IPI_MODOK)) {
12479 mi_copy_done(q, mp, EINVAL);
12480 return (0);
12484 ip_process_ioctl(NULL, q, mp, ipip);
12486 } else {
12487 mi_copyout(q, mp);
12489 return (0);
12491 case M_IOCNAK:
12493 * The only way we could get here is if a resolver didn't like
12494 * an IOCTL we sent it. This shouldn't happen.
12496 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12497 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12498 ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12499 freemsg(mp);
12500 return (0);
12501 case M_IOCACK:
12502 /* /dev/ip shouldn't see this */
12503 goto nak;
12504 case M_FLUSH:
12505 if (*mp->b_rptr & FLUSHW)
12506 flushq(q, FLUSHALL);
12507 if (q->q_next) {
12508 putnext(q, mp);
12509 return (0);
12511 if (*mp->b_rptr & FLUSHR) {
12512 *mp->b_rptr &= ~FLUSHW;
12513 qreply(q, mp);
12514 return (0);
12516 freemsg(mp);
12517 return (0);
12518 case M_CTL:
12519 break;
12520 case M_PROTO:
12521 case M_PCPROTO:
12523 * The only PROTO messages we expect are SNMP-related.
12525 switch (((union T_primitives *)mp->b_rptr)->type) {
12526 case T_SVR4_OPTMGMT_REQ:
12527 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12528 "flags %x\n",
12529 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12531 if (connp == NULL) {
12532 proto_str = "T_SVR4_OPTMGMT_REQ";
12533 goto protonak;
12537 * All Solaris components should pass a db_credp
12538 * for this TPI message, hence we ASSERT.
12539 * But in case there is some other M_PROTO that looks
12540 * like a TPI message sent by some other kernel
12541 * component, we check and return an error.
12543 cr = msg_getcred(mp, NULL);
12544 ASSERT(cr != NULL);
12545 if (cr == NULL) {
12546 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12547 if (mp != NULL)
12548 qreply(q, mp);
12549 return (0);
12552 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12553 proto_str = "Bad SNMPCOM request?";
12554 goto protonak;
12556 return (0);
12557 default:
12558 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12559 (int)*(uint_t *)mp->b_rptr));
12560 freemsg(mp);
12561 return (0);
12563 default:
12564 break;
12566 if (q->q_next) {
12567 putnext(q, mp);
12568 } else
12569 freemsg(mp);
12570 return (0);
12572 nak:
12573 iocp->ioc_error = EINVAL;
12574 mp->b_datap->db_type = M_IOCNAK;
12575 iocp->ioc_count = 0;
12576 qreply(q, mp);
12577 return (0);
12579 protonak:
12580 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12581 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12582 qreply(q, mp);
12583 return (0);
12587 * Process IP options in an outbound packet. Verify that the nexthop in a
12588 * strict source route is onlink.
12589 * Returns non-zero if something fails in which case an ICMP error has been
12590 * sent and mp freed.
12592 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12595 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12597 ipoptp_t opts;
12598 uchar_t *opt;
12599 uint8_t optval;
12600 uint8_t optlen;
12601 ipaddr_t dst;
12602 intptr_t code = 0;
12603 ire_t *ire;
12604 ip_stack_t *ipst = ixa->ixa_ipst;
12605 ip_recv_attr_t iras;
12607 ip2dbg(("ip_output_options\n"));
12609 dst = ipha->ipha_dst;
12610 for (optval = ipoptp_first(&opts, ipha);
12611 optval != IPOPT_EOL;
12612 optval = ipoptp_next(&opts)) {
12613 opt = opts.ipoptp_cur;
12614 optlen = opts.ipoptp_len;
12615 ip2dbg(("ip_output_options: opt %d, len %d\n",
12616 optval, optlen));
12617 switch (optval) {
12618 uint32_t off;
12619 case IPOPT_SSRR:
12620 case IPOPT_LSRR:
12621 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12622 ip1dbg((
12623 "ip_output_options: bad option offset\n"));
12624 code = (char *)&opt[IPOPT_OLEN] -
12625 (char *)ipha;
12626 goto param_prob;
12628 off = opt[IPOPT_OFFSET];
12629 ip1dbg(("ip_output_options: next hop 0x%x\n",
12630 ntohl(dst)));
12632 * For strict: verify that dst is directly
12633 * reachable.
12635 if (optval == IPOPT_SSRR) {
12636 ire = ire_ftable_lookup_v4(dst, 0, 0,
12637 IRE_INTERFACE, NULL, ALL_ZONES,
12638 MATCH_IRE_TYPE, 0, ipst, NULL);
12639 if (ire == NULL) {
12640 ip1dbg(("ip_output_options: SSRR not"
12641 " directly reachable: 0x%x\n",
12642 ntohl(dst)));
12643 goto bad_src_route;
12645 ire_refrele(ire);
12647 break;
12648 case IPOPT_RR:
12649 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12650 ip1dbg((
12651 "ip_output_options: bad option offset\n"));
12652 code = (char *)&opt[IPOPT_OLEN] -
12653 (char *)ipha;
12654 goto param_prob;
12656 break;
12657 case IPOPT_TS:
12659 * Verify that length >=5 and that there is either
12660 * room for another timestamp or that the overflow
12661 * counter is not maxed out.
12663 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
12664 if (optlen < IPOPT_MINLEN_IT) {
12665 goto param_prob;
12667 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12668 ip1dbg((
12669 "ip_output_options: bad option offset\n"));
12670 code = (char *)&opt[IPOPT_OFFSET] -
12671 (char *)ipha;
12672 goto param_prob;
12674 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12675 case IPOPT_TS_TSONLY:
12676 off = IPOPT_TS_TIMELEN;
12677 break;
12678 case IPOPT_TS_TSANDADDR:
12679 case IPOPT_TS_PRESPEC:
12680 case IPOPT_TS_PRESPEC_RFC791:
12681 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
12682 break;
12683 default:
12684 code = (char *)&opt[IPOPT_POS_OV_FLG] -
12685 (char *)ipha;
12686 goto param_prob;
12688 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
12689 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
12691 * No room and the overflow counter is 15
12692 * already.
12694 goto param_prob;
12696 break;
12700 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
12701 return (0);
12703 ip1dbg(("ip_output_options: error processing IP options."));
12704 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
12706 param_prob:
12707 bzero(&iras, sizeof (iras));
12708 iras.ira_ill = iras.ira_rill = ill;
12709 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
12710 iras.ira_rifindex = iras.ira_ruifindex;
12711 iras.ira_flags = IRAF_IS_IPV4;
12713 ip_drop_output("ip_output_options", mp, ill);
12714 icmp_param_problem(mp, (uint8_t)code, &iras);
12715 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
12716 return (-1);
12718 bad_src_route:
12719 bzero(&iras, sizeof (iras));
12720 iras.ira_ill = iras.ira_rill = ill;
12721 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
12722 iras.ira_rifindex = iras.ira_ruifindex;
12723 iras.ira_flags = IRAF_IS_IPV4;
12725 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
12726 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
12727 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
12728 return (-1);
12732 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
12733 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
12734 * thru /etc/system.
12736 #define CONN_MAXDRAINCNT 64
12738 static void
12739 conn_drain_init(ip_stack_t *ipst)
12741 int i, j;
12742 idl_tx_list_t *itl_tx;
12744 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
12746 if ((ipst->ips_conn_drain_list_cnt == 0) ||
12747 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
12749 * Default value of the number of drainers is the
12750 * number of cpus, subject to maximum of 8 drainers.
12752 if (boot_max_ncpus != -1)
12753 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
12754 else
12755 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
12758 ipst->ips_idl_tx_list =
12759 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
12760 for (i = 0; i < TX_FANOUT_SIZE; i++) {
12761 itl_tx = &ipst->ips_idl_tx_list[i];
12762 itl_tx->txl_drain_list =
12763 kmem_zalloc(ipst->ips_conn_drain_list_cnt *
12764 sizeof (idl_t), KM_SLEEP);
12765 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
12766 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
12767 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
12768 MUTEX_DEFAULT, NULL);
12769 itl_tx->txl_drain_list[j].idl_itl = itl_tx;
12774 static void
12775 conn_drain_fini(ip_stack_t *ipst)
12777 int i;
12778 idl_tx_list_t *itl_tx;
12780 for (i = 0; i < TX_FANOUT_SIZE; i++) {
12781 itl_tx = &ipst->ips_idl_tx_list[i];
12782 kmem_free(itl_tx->txl_drain_list,
12783 ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
12785 kmem_free(ipst->ips_idl_tx_list,
12786 TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
12787 ipst->ips_idl_tx_list = NULL;
12791 * Flow control has blocked us from proceeding. Insert the given conn in one
12792 * of the conn drain lists. When flow control is unblocked, either ip_wsrv()
12793 * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
12794 * will call conn_walk_drain(). See the flow control notes at the top of this
12795 * file for more details.
12797 void
12798 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
12800 idl_t *idl = tx_list->txl_drain_list;
12801 uint_t index;
12802 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
12804 mutex_enter(&connp->conn_lock);
12805 if (connp->conn_state_flags & CONN_CLOSING) {
12807 * The conn is closing as a result of which CONN_CLOSING
12808 * is set. Return.
12810 mutex_exit(&connp->conn_lock);
12811 return;
12812 } else if (connp->conn_idl == NULL) {
12814 * Assign the next drain list round robin. We dont' use
12815 * a lock, and thus it may not be strictly round robin.
12816 * Atomicity of load/stores is enough to make sure that
12817 * conn_drain_list_index is always within bounds.
12819 index = tx_list->txl_drain_index;
12820 ASSERT(index < ipst->ips_conn_drain_list_cnt);
12821 connp->conn_idl = &tx_list->txl_drain_list[index];
12822 index++;
12823 if (index == ipst->ips_conn_drain_list_cnt)
12824 index = 0;
12825 tx_list->txl_drain_index = index;
12826 } else {
12827 ASSERT(connp->conn_idl->idl_itl == tx_list);
12829 mutex_exit(&connp->conn_lock);
12831 idl = connp->conn_idl;
12832 mutex_enter(&idl->idl_lock);
12833 if ((connp->conn_drain_prev != NULL) ||
12834 (connp->conn_state_flags & CONN_CLOSING)) {
12836 * The conn is either already in the drain list or closing.
12837 * (We needed to check for CONN_CLOSING again since close can
12838 * sneak in between dropping conn_lock and acquiring idl_lock.)
12840 mutex_exit(&idl->idl_lock);
12841 return;
12845 * The conn is not in the drain list. Insert it at the
12846 * tail of the drain list. The drain list is circular
12847 * and doubly linked. idl_conn points to the 1st element
12848 * in the list.
12850 if (idl->idl_conn == NULL) {
12851 idl->idl_conn = connp;
12852 connp->conn_drain_next = connp;
12853 connp->conn_drain_prev = connp;
12854 } else {
12855 conn_t *head = idl->idl_conn;
12857 connp->conn_drain_next = head;
12858 connp->conn_drain_prev = head->conn_drain_prev;
12859 head->conn_drain_prev->conn_drain_next = connp;
12860 head->conn_drain_prev = connp;
12863 * For non streams based sockets assert flow control.
12865 conn_setqfull(connp, NULL);
12866 mutex_exit(&idl->idl_lock);
12869 static void
12870 conn_drain_remove(conn_t *connp)
12872 idl_t *idl = connp->conn_idl;
12874 if (idl != NULL) {
12876 * Remove ourself from the drain list.
12878 if (connp->conn_drain_next == connp) {
12879 /* Singleton in the list */
12880 ASSERT(connp->conn_drain_prev == connp);
12881 idl->idl_conn = NULL;
12882 } else {
12883 connp->conn_drain_prev->conn_drain_next =
12884 connp->conn_drain_next;
12885 connp->conn_drain_next->conn_drain_prev =
12886 connp->conn_drain_prev;
12887 if (idl->idl_conn == connp)
12888 idl->idl_conn = connp->conn_drain_next;
12892 * NOTE: because conn_idl is associated with a specific drain
12893 * list which in turn is tied to the index the TX ring
12894 * (txl_cookie) hashes to, and because the TX ring can change
12895 * over the lifetime of the conn_t, we must clear conn_idl so
12896 * a subsequent conn_drain_insert() will set conn_idl again
12897 * based on the latest txl_cookie.
12899 connp->conn_idl = NULL;
12901 connp->conn_drain_next = NULL;
12902 connp->conn_drain_prev = NULL;
12904 conn_clrqfull(connp, NULL);
12906 * For streams based sockets open up flow control.
12908 if (!IPCL_IS_NONSTR(connp))
12909 enableok(connp->conn_wq);
12913 * This conn is closing, and we are called from ip_close. OR
12914 * this conn is draining because flow-control on the ill has been relieved.
12916 * We must also need to remove conn's on this idl from the list, and also
12917 * inform the sockfs upcalls about the change in flow-control.
12919 static void
12920 conn_drain(conn_t *connp, boolean_t closing)
12922 idl_t *idl;
12923 conn_t *next_connp;
12926 * connp->conn_idl is stable at this point, and no lock is needed
12927 * to check it. If we are called from ip_close, close has already
12928 * set CONN_CLOSING, thus freezing the value of conn_idl, and
12929 * called us only because conn_idl is non-null. If we are called thru
12930 * service, conn_idl could be null, but it cannot change because
12931 * service is single-threaded per queue, and there cannot be another
12932 * instance of service trying to call conn_drain_insert on this conn
12933 * now.
12935 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
12938 * If the conn doesn't exist or is not on a drain list, bail.
12940 if (connp == NULL || connp->conn_idl == NULL ||
12941 connp->conn_drain_prev == NULL) {
12942 return;
12945 idl = connp->conn_idl;
12946 ASSERT(MUTEX_HELD(&idl->idl_lock));
12948 if (!closing) {
12949 next_connp = connp->conn_drain_next;
12950 while (next_connp != connp) {
12951 conn_t *delconnp = next_connp;
12953 next_connp = next_connp->conn_drain_next;
12954 conn_drain_remove(delconnp);
12956 ASSERT(connp->conn_drain_next == idl->idl_conn);
12958 conn_drain_remove(connp);
12962 * Write service routine. Shared perimeter entry point.
12963 * The device queue's messages has fallen below the low water mark and STREAMS
12964 * has backenabled the ill_wq. Send sockfs notification about flow-control on
12965 * each waiting conn.
12968 ip_wsrv(queue_t *q)
12970 ill_t *ill;
12972 ill = (ill_t *)q->q_ptr;
12973 if (ill->ill_state_flags == 0) {
12974 ip_stack_t *ipst = ill->ill_ipst;
12977 * The device flow control has opened up.
12978 * Walk through conn drain lists and qenable the
12979 * first conn in each list. This makes sense only
12980 * if the stream is fully plumbed and setup.
12981 * Hence the ill_state_flags check above.
12983 ip1dbg(("ip_wsrv: walking\n"));
12984 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
12985 enableok(ill->ill_wq);
12987 return (0);
12991 * Callback to disable flow control in IP.
12993 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
12994 * is enabled.
12996 * When MAC_TX() is not able to send any more packets, dld sets its queue
12997 * to QFULL and enable the STREAMS flow control. Later, when the underlying
12998 * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
12999 * function and wakes up corresponding mac worker threads, which in turn
13000 * calls this callback function, and disables flow control.
13002 void
13003 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13005 ill_t *ill = (ill_t *)arg;
13006 ip_stack_t *ipst = ill->ill_ipst;
13007 idl_tx_list_t *idl_txl;
13009 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13010 mutex_enter(&idl_txl->txl_lock);
13011 /* add code to to set a flag to indicate idl_txl is enabled */
13012 conn_walk_drain(ipst, idl_txl);
13013 mutex_exit(&idl_txl->txl_lock);
13017 * Flow control has been relieved and STREAMS has backenabled us; drain
13018 * all the conn lists on `tx_list'.
13020 static void
13021 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13023 int i;
13024 idl_t *idl;
13026 IP_STAT(ipst, ip_conn_walk_drain);
13028 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13029 idl = &tx_list->txl_drain_list[i];
13030 mutex_enter(&idl->idl_lock);
13031 conn_drain(idl->idl_conn, B_FALSE);
13032 mutex_exit(&idl->idl_lock);
13037 * Determine if the ill and multicast aspects of that packets
13038 * "matches" the conn.
13040 boolean_t
13041 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13043 ill_t *ill = ira->ira_rill;
13044 zoneid_t zoneid = ira->ira_zoneid;
13045 uint_t in_ifindex;
13046 ipaddr_t dst, src;
13048 dst = ipha->ipha_dst;
13049 src = ipha->ipha_src;
13052 * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13053 * unicast, broadcast and multicast reception to
13054 * conn_incoming_ifindex.
13055 * conn_wantpacket is called for unicast, broadcast and
13056 * multicast packets.
13058 in_ifindex = connp->conn_incoming_ifindex;
13060 /* mpathd can bind to the under IPMP interface, which we allow */
13061 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13062 if (!IS_UNDER_IPMP(ill))
13063 return (B_FALSE);
13065 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13066 return (B_FALSE);
13069 if (!IPCL_ZONE_MATCH(connp, zoneid))
13070 return (B_FALSE);
13072 if (!(ira->ira_flags & IRAF_MULTICAST))
13073 return (B_TRUE);
13075 if (connp->conn_multi_router) {
13076 /* multicast packet and multicast router socket: send up */
13077 return (B_TRUE);
13080 if (ipha->ipha_protocol == IPPROTO_PIM ||
13081 ipha->ipha_protocol == IPPROTO_RSVP)
13082 return (B_TRUE);
13084 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13087 void
13088 conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13090 if (IPCL_IS_NONSTR(connp)) {
13091 (*connp->conn_upcalls->su_txq_full)
13092 (connp->conn_upper_handle, B_TRUE);
13093 if (flow_stopped != NULL)
13094 *flow_stopped = B_TRUE;
13095 } else {
13096 queue_t *q = connp->conn_wq;
13098 ASSERT(q != NULL);
13099 if (!(q->q_flag & QFULL)) {
13100 mutex_enter(QLOCK(q));
13101 if (!(q->q_flag & QFULL)) {
13102 /* still need to set QFULL */
13103 q->q_flag |= QFULL;
13104 /* set flow_stopped to true under QLOCK */
13105 if (flow_stopped != NULL)
13106 *flow_stopped = B_TRUE;
13107 mutex_exit(QLOCK(q));
13108 } else {
13109 /* flow_stopped is left unchanged */
13110 mutex_exit(QLOCK(q));
13116 void
13117 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13119 if (IPCL_IS_NONSTR(connp)) {
13120 (*connp->conn_upcalls->su_txq_full)
13121 (connp->conn_upper_handle, B_FALSE);
13122 if (flow_stopped != NULL)
13123 *flow_stopped = B_FALSE;
13124 } else {
13125 queue_t *q = connp->conn_wq;
13127 ASSERT(q != NULL);
13128 if (q->q_flag & QFULL) {
13129 mutex_enter(QLOCK(q));
13130 if (q->q_flag & QFULL) {
13131 q->q_flag &= ~QFULL;
13132 /* set flow_stopped to false under QLOCK */
13133 if (flow_stopped != NULL)
13134 *flow_stopped = B_FALSE;
13135 mutex_exit(QLOCK(q));
13136 if (q->q_flag & QWANTW)
13137 qbackenable(q, 0);
13138 } else {
13139 /* flow_stopped is left unchanged */
13140 mutex_exit(QLOCK(q));
13145 mutex_enter(&connp->conn_lock);
13146 connp->conn_blocked = B_FALSE;
13147 mutex_exit(&connp->conn_lock);
13151 * Return the length in bytes of the IPv4 headers (base header and IP options)
13152 * that will be needed based on the ip_pkt_t structure passed by the caller.
13154 * The returned length does not include the length of the upper level
13155 * protocol (ULP) header.
13156 * The caller needs to check that the length doesn't exceed the max for IPv4.
13159 ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13161 int len;
13163 len = IP_SIMPLE_HDR_LENGTH;
13165 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13166 ASSERT(ipp->ipp_ipv4_options_len != 0);
13167 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13168 len += ipp->ipp_ipv4_options_len;
13170 return (len);
13174 * All-purpose routine to build an IPv4 header with options based
13175 * on the abstract ip_pkt_t.
13177 * The caller has to set the source and destination address as well as
13178 * ipha_length. The caller has to massage any source route and compensate
13179 * for the ULP pseudo-header checksum due to the source route.
13181 void
13182 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13183 uint8_t protocol)
13185 ipha_t *ipha = (ipha_t *)buf;
13186 uint8_t *cp;
13188 /* Initialize IPv4 header */
13189 ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13190 ipha->ipha_length = 0; /* Caller will set later */
13191 ipha->ipha_ident = 0;
13192 ipha->ipha_fragment_offset_and_flags = 0;
13193 ipha->ipha_ttl = ipp->ipp_unicast_hops;
13194 ipha->ipha_protocol = protocol;
13195 ipha->ipha_hdr_checksum = 0;
13197 if ((ipp->ipp_fields & IPPF_ADDR) &&
13198 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13199 ipha->ipha_src = ipp->ipp_addr_v4;
13201 cp = (uint8_t *)&ipha[1];
13203 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13204 ASSERT(ipp->ipp_ipv4_options_len != 0);
13205 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13206 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13207 cp += ipp->ipp_ipv4_options_len;
13209 ipha->ipha_version_and_hdr_length =
13210 (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13212 ASSERT((int)(cp - buf) == buf_len);
13215 /* Allocate the private structure */
13216 static int
13217 ip_priv_alloc(void **bufp)
13219 void *buf;
13221 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13222 return (ENOMEM);
13224 *bufp = buf;
13225 return (0);
13228 /* Function to delete the private structure */
13229 void
13230 ip_priv_free(void *buf)
13232 ASSERT(buf != NULL);
13233 kmem_free(buf, sizeof (ip_priv_t));
13237 * The entry point for IPPF processing.
13238 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13239 * routine just returns.
13241 * When called, ip_process generates an ipp_packet_t structure
13242 * which holds the state information for this packet and invokes the
13243 * the classifier (via ipp_packet_process). The classification, depending on
13244 * configured filters, results in a list of actions for this packet. Invoking
13245 * an action may cause the packet to be dropped, in which case we return NULL.
13246 * proc indicates the callout position for
13247 * this packet and ill is the interface this packet arrived on or will leave
13248 * on (inbound and outbound resp.).
13250 * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13251 * on the ill corrsponding to the destination IP address.
13253 mblk_t *
13254 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13256 ip_priv_t *priv;
13257 ipp_action_id_t aid;
13258 int rc = 0;
13259 ipp_packet_t *pp;
13261 /* If the classifier is not loaded, return */
13262 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13263 return (mp);
13266 ASSERT(mp != NULL);
13268 /* Allocate the packet structure */
13269 rc = ipp_packet_alloc(&pp, "ip", aid);
13270 if (rc != 0)
13271 goto drop;
13273 /* Allocate the private structure */
13274 rc = ip_priv_alloc((void **)&priv);
13275 if (rc != 0) {
13276 ipp_packet_free(pp);
13277 goto drop;
13279 priv->proc = proc;
13280 priv->ill_index = ill_get_upper_ifindex(rill);
13282 ipp_packet_set_private(pp, priv, ip_priv_free);
13283 ipp_packet_set_data(pp, mp);
13285 /* Invoke the classifier */
13286 rc = ipp_packet_process(&pp);
13287 if (pp != NULL) {
13288 mp = ipp_packet_get_data(pp);
13289 ipp_packet_free(pp);
13290 if (rc != 0)
13291 goto drop;
13292 return (mp);
13293 } else {
13294 /* No mp to trace in ip_drop_input/ip_drop_output */
13295 mp = NULL;
13297 drop:
13298 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13299 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13300 ip_drop_input("ip_process", mp, ill);
13301 } else {
13302 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13303 ip_drop_output("ip_process", mp, ill);
13305 freemsg(mp);
13306 return (NULL);
13309 static int
13310 ip_squeue_switch(int val)
13312 int rval;
13314 switch (val) {
13315 case IP_SQUEUE_ENTER_NODRAIN:
13316 rval = SQ_NODRAIN;
13317 break;
13318 case IP_SQUEUE_ENTER:
13319 rval = SQ_PROCESS;
13320 break;
13321 case IP_SQUEUE_FILL:
13322 default:
13323 rval = SQ_FILL;
13324 break;
13326 return (rval);
13329 static void *
13330 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13332 kstat_t *ksp;
13334 ip_stat_t template = {
13335 { "ip_udp_fannorm", KSTAT_DATA_UINT64 },
13336 { "ip_udp_fanmb", KSTAT_DATA_UINT64 },
13337 { "ip_recv_pullup", KSTAT_DATA_UINT64 },
13338 { "ip_db_ref", KSTAT_DATA_UINT64 },
13339 { "ip_notaligned", KSTAT_DATA_UINT64 },
13340 { "ip_multimblk", KSTAT_DATA_UINT64 },
13341 { "ip_opt", KSTAT_DATA_UINT64 },
13342 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 },
13343 { "ip_conn_flputbq", KSTAT_DATA_UINT64 },
13344 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 },
13345 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 },
13346 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 },
13347 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 },
13348 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 },
13349 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 },
13350 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 },
13351 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 },
13352 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 },
13353 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 },
13354 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13355 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13356 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13357 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13358 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13359 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13360 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 },
13361 { "conn_in_recvopts", KSTAT_DATA_UINT64 },
13362 { "conn_in_recvif", KSTAT_DATA_UINT64 },
13363 { "conn_in_recvslla", KSTAT_DATA_UINT64 },
13364 { "conn_in_recvucred", KSTAT_DATA_UINT64 },
13365 { "conn_in_recvttl", KSTAT_DATA_UINT64 },
13366 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 },
13367 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 },
13368 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 },
13369 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 },
13370 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 },
13371 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 },
13372 { "conn_in_recvtclass", KSTAT_DATA_UINT64 },
13373 { "conn_in_timestamp", KSTAT_DATA_UINT64 },
13376 ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13377 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13378 KSTAT_FLAG_VIRTUAL, stackid);
13380 if (ksp == NULL)
13381 return (NULL);
13383 bcopy(&template, ip_statisticsp, sizeof (template));
13384 ksp->ks_data = (void *)ip_statisticsp;
13385 ksp->ks_private = (void *)(uintptr_t)stackid;
13387 kstat_install(ksp);
13388 return (ksp);
13391 static void
13392 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13394 if (ksp != NULL) {
13395 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13396 kstat_delete_netstack(ksp, stackid);
13400 static void *
13401 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
13403 kstat_t *ksp;
13405 ip_named_kstat_t template = {
13406 { "forwarding", KSTAT_DATA_UINT32, 0 },
13407 { "defaultTTL", KSTAT_DATA_UINT32, 0 },
13408 { "inReceives", KSTAT_DATA_UINT64, 0 },
13409 { "inHdrErrors", KSTAT_DATA_UINT32, 0 },
13410 { "inAddrErrors", KSTAT_DATA_UINT32, 0 },
13411 { "forwDatagrams", KSTAT_DATA_UINT64, 0 },
13412 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 },
13413 { "inDiscards", KSTAT_DATA_UINT32, 0 },
13414 { "inDelivers", KSTAT_DATA_UINT64, 0 },
13415 { "outRequests", KSTAT_DATA_UINT64, 0 },
13416 { "outDiscards", KSTAT_DATA_UINT32, 0 },
13417 { "outNoRoutes", KSTAT_DATA_UINT32, 0 },
13418 { "reasmTimeout", KSTAT_DATA_UINT32, 0 },
13419 { "reasmReqds", KSTAT_DATA_UINT32, 0 },
13420 { "reasmOKs", KSTAT_DATA_UINT32, 0 },
13421 { "reasmFails", KSTAT_DATA_UINT32, 0 },
13422 { "fragOKs", KSTAT_DATA_UINT32, 0 },
13423 { "fragFails", KSTAT_DATA_UINT32, 0 },
13424 { "fragCreates", KSTAT_DATA_UINT32, 0 },
13425 { "addrEntrySize", KSTAT_DATA_INT32, 0 },
13426 { "routeEntrySize", KSTAT_DATA_INT32, 0 },
13427 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 },
13428 { "routingDiscards", KSTAT_DATA_UINT32, 0 },
13429 { "inErrs", KSTAT_DATA_UINT32, 0 },
13430 { "noPorts", KSTAT_DATA_UINT32, 0 },
13431 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
13432 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 },
13433 { "reasmPartDups", KSTAT_DATA_UINT32, 0 },
13434 { "forwProhibits", KSTAT_DATA_UINT32, 0 },
13435 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 },
13436 { "udpInOverflows", KSTAT_DATA_UINT32, 0 },
13437 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 },
13438 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 },
13439 { "ipsecInFailed", KSTAT_DATA_INT32, 0 },
13440 { "memberEntrySize", KSTAT_DATA_INT32, 0 },
13441 { "inIPv6", KSTAT_DATA_UINT32, 0 },
13442 { "outIPv6", KSTAT_DATA_UINT32, 0 },
13443 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 },
13446 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
13447 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
13448 if (ksp == NULL || ksp->ks_data == NULL)
13449 return (NULL);
13451 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
13452 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
13453 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
13454 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
13455 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
13457 template.netToMediaEntrySize.value.i32 =
13458 sizeof (mib2_ipNetToMediaEntry_t);
13460 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
13462 bcopy(&template, ksp->ks_data, sizeof (template));
13463 ksp->ks_update = ip_kstat_update;
13464 ksp->ks_private = (void *)(uintptr_t)stackid;
13466 kstat_install(ksp);
13467 return (ksp);
13470 static void
13471 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
13473 if (ksp != NULL) {
13474 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13475 kstat_delete_netstack(ksp, stackid);
13479 static int
13480 ip_kstat_update(kstat_t *kp, int rw)
13482 ip_named_kstat_t *ipkp;
13483 mib2_ipIfStatsEntry_t ipmib;
13484 ill_walk_context_t ctx;
13485 ill_t *ill;
13486 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
13487 netstack_t *ns;
13488 ip_stack_t *ipst;
13490 if (kp == NULL || kp->ks_data == NULL)
13491 return (EIO);
13493 if (rw == KSTAT_WRITE)
13494 return (EACCES);
13496 ns = netstack_find_by_stackid(stackid);
13497 if (ns == NULL)
13498 return (-1);
13499 ipst = ns->netstack_ip;
13500 if (ipst == NULL) {
13501 netstack_rele(ns);
13502 return (-1);
13504 ipkp = (ip_named_kstat_t *)kp->ks_data;
13506 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
13507 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
13508 ill = ILL_START_WALK_V4(&ctx, ipst);
13509 for (; ill != NULL; ill = ill_next(&ctx, ill))
13510 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
13511 rw_exit(&ipst->ips_ill_g_lock);
13513 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding;
13514 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL;
13515 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives;
13516 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors;
13517 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors;
13518 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
13519 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos;
13520 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards;
13521 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers;
13522 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests;
13523 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards;
13524 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes;
13525 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
13526 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds;
13527 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs;
13528 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails;
13529 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs;
13530 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails;
13531 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates;
13533 ipkp->routingDiscards.value.ui32 = 0;
13534 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs;
13535 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts;
13536 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs;
13537 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates;
13538 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups;
13539 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits;
13540 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs;
13541 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows;
13542 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows;
13543 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded;
13544 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed;
13546 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion;
13547 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion;
13548 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
13550 netstack_rele(ns);
13552 return (0);
13555 static void *
13556 icmp_kstat_init(netstackid_t stackid)
13558 kstat_t *ksp;
13560 icmp_named_kstat_t template = {
13561 { "inMsgs", KSTAT_DATA_UINT32 },
13562 { "inErrors", KSTAT_DATA_UINT32 },
13563 { "inDestUnreachs", KSTAT_DATA_UINT32 },
13564 { "inTimeExcds", KSTAT_DATA_UINT32 },
13565 { "inParmProbs", KSTAT_DATA_UINT32 },
13566 { "inSrcQuenchs", KSTAT_DATA_UINT32 },
13567 { "inRedirects", KSTAT_DATA_UINT32 },
13568 { "inEchos", KSTAT_DATA_UINT32 },
13569 { "inEchoReps", KSTAT_DATA_UINT32 },
13570 { "inTimestamps", KSTAT_DATA_UINT32 },
13571 { "inTimestampReps", KSTAT_DATA_UINT32 },
13572 { "inAddrMasks", KSTAT_DATA_UINT32 },
13573 { "inAddrMaskReps", KSTAT_DATA_UINT32 },
13574 { "outMsgs", KSTAT_DATA_UINT32 },
13575 { "outErrors", KSTAT_DATA_UINT32 },
13576 { "outDestUnreachs", KSTAT_DATA_UINT32 },
13577 { "outTimeExcds", KSTAT_DATA_UINT32 },
13578 { "outParmProbs", KSTAT_DATA_UINT32 },
13579 { "outSrcQuenchs", KSTAT_DATA_UINT32 },
13580 { "outRedirects", KSTAT_DATA_UINT32 },
13581 { "outEchos", KSTAT_DATA_UINT32 },
13582 { "outEchoReps", KSTAT_DATA_UINT32 },
13583 { "outTimestamps", KSTAT_DATA_UINT32 },
13584 { "outTimestampReps", KSTAT_DATA_UINT32 },
13585 { "outAddrMasks", KSTAT_DATA_UINT32 },
13586 { "outAddrMaskReps", KSTAT_DATA_UINT32 },
13587 { "inChksumErrs", KSTAT_DATA_UINT32 },
13588 { "inUnknowns", KSTAT_DATA_UINT32 },
13589 { "inFragNeeded", KSTAT_DATA_UINT32 },
13590 { "outFragNeeded", KSTAT_DATA_UINT32 },
13591 { "outDrops", KSTAT_DATA_UINT32 },
13592 { "inOverFlows", KSTAT_DATA_UINT32 },
13593 { "inBadRedirects", KSTAT_DATA_UINT32 },
13596 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
13597 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
13598 if (ksp == NULL || ksp->ks_data == NULL)
13599 return (NULL);
13601 bcopy(&template, ksp->ks_data, sizeof (template));
13603 ksp->ks_update = icmp_kstat_update;
13604 ksp->ks_private = (void *)(uintptr_t)stackid;
13606 kstat_install(ksp);
13607 return (ksp);
13610 static void
13611 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
13613 if (ksp != NULL) {
13614 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13615 kstat_delete_netstack(ksp, stackid);
13619 static int
13620 icmp_kstat_update(kstat_t *kp, int rw)
13622 icmp_named_kstat_t *icmpkp;
13623 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
13624 netstack_t *ns;
13625 ip_stack_t *ipst;
13627 if ((kp == NULL) || (kp->ks_data == NULL))
13628 return (EIO);
13630 if (rw == KSTAT_WRITE)
13631 return (EACCES);
13633 ns = netstack_find_by_stackid(stackid);
13634 if (ns == NULL)
13635 return (-1);
13636 ipst = ns->netstack_ip;
13637 if (ipst == NULL) {
13638 netstack_rele(ns);
13639 return (-1);
13641 icmpkp = (icmp_named_kstat_t *)kp->ks_data;
13643 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs;
13644 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors;
13645 icmpkp->inDestUnreachs.value.ui32 =
13646 ipst->ips_icmp_mib.icmpInDestUnreachs;
13647 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds;
13648 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs;
13649 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs;
13650 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects;
13651 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos;
13652 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps;
13653 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps;
13654 icmpkp->inTimestampReps.value.ui32 =
13655 ipst->ips_icmp_mib.icmpInTimestampReps;
13656 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks;
13657 icmpkp->inAddrMaskReps.value.ui32 =
13658 ipst->ips_icmp_mib.icmpInAddrMaskReps;
13659 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs;
13660 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors;
13661 icmpkp->outDestUnreachs.value.ui32 =
13662 ipst->ips_icmp_mib.icmpOutDestUnreachs;
13663 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds;
13664 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs;
13665 icmpkp->outSrcQuenchs.value.ui32 =
13666 ipst->ips_icmp_mib.icmpOutSrcQuenchs;
13667 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects;
13668 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos;
13669 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps;
13670 icmpkp->outTimestamps.value.ui32 =
13671 ipst->ips_icmp_mib.icmpOutTimestamps;
13672 icmpkp->outTimestampReps.value.ui32 =
13673 ipst->ips_icmp_mib.icmpOutTimestampReps;
13674 icmpkp->outAddrMasks.value.ui32 =
13675 ipst->ips_icmp_mib.icmpOutAddrMasks;
13676 icmpkp->outAddrMaskReps.value.ui32 =
13677 ipst->ips_icmp_mib.icmpOutAddrMaskReps;
13678 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs;
13679 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns;
13680 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded;
13681 icmpkp->outFragNeeded.value.ui32 =
13682 ipst->ips_icmp_mib.icmpOutFragNeeded;
13683 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops;
13684 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows;
13685 icmpkp->inBadRedirects.value.ui32 =
13686 ipst->ips_icmp_mib.icmpInBadRedirects;
13688 netstack_rele(ns);
13689 return (0);
13693 * This is the fanout function for raw socket opened for SCTP. Note
13694 * that it is called after SCTP checks that there is no socket which
13695 * wants a packet. Then before SCTP handles this out of the blue packet,
13696 * this function is called to see if there is any raw socket for SCTP.
13697 * If there is and it is bound to the correct address, the packet will
13698 * be sent to that socket. Note that only one raw socket can be bound to
13699 * a port. This is assured in ipcl_sctp_hash_insert();
13701 void
13702 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
13703 ip_recv_attr_t *ira)
13705 conn_t *connp;
13706 queue_t *rq;
13707 boolean_t secure;
13708 ill_t *ill = ira->ira_ill;
13709 ip_stack_t *ipst = ill->ill_ipst;
13710 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
13711 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp;
13712 iaflags_t iraflags = ira->ira_flags;
13713 ill_t *rill = ira->ira_rill;
13715 secure = iraflags & IRAF_IPSEC_SECURE;
13717 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
13718 ira, ipst);
13719 if (connp == NULL) {
13721 * Although raw sctp is not summed, OOB chunks must be.
13722 * Drop the packet here if the sctp checksum failed.
13724 if (iraflags & IRAF_SCTP_CSUM_ERR) {
13725 SCTPS_BUMP_MIB(sctps, sctpChecksumError);
13726 freemsg(mp);
13727 return;
13729 ira->ira_ill = ira->ira_rill = NULL;
13730 sctp_ootb_input(mp, ira, ipst);
13731 ira->ira_ill = ill;
13732 ira->ira_rill = rill;
13733 return;
13735 rq = connp->conn_rq;
13736 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
13737 CONN_DEC_REF(connp);
13738 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
13739 freemsg(mp);
13740 return;
13742 if (((iraflags & IRAF_IS_IPV4) ?
13743 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
13744 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
13745 secure) {
13746 mp = ipsec_check_inbound_policy(mp, connp, ipha,
13747 ip6h, ira);
13748 if (mp == NULL) {
13749 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13750 /* Note that mp is NULL */
13751 ip_drop_input("ipIfStatsInDiscards", mp, ill);
13752 CONN_DEC_REF(connp);
13753 return;
13757 if (iraflags & IRAF_ICMP_ERROR) {
13758 (connp->conn_recvicmp)(connp, mp, NULL, ira);
13759 } else {
13760 ill_t *rill = ira->ira_rill;
13762 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
13763 /* This is the SOCK_RAW, IPPROTO_SCTP case. */
13764 ira->ira_ill = ira->ira_rill = NULL;
13765 (connp->conn_recv)(connp, mp, NULL, ira);
13766 ira->ira_ill = ill;
13767 ira->ira_rill = rill;
13769 CONN_DEC_REF(connp);
13773 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
13774 * header before the ip payload.
13776 static void
13777 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
13779 int len = (mp->b_wptr - mp->b_rptr);
13780 mblk_t *ip_mp;
13782 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13783 if (is_fp_mp || len != fp_mp_len) {
13784 if (len > fp_mp_len) {
13786 * fastpath header and ip header in the first mblk
13788 mp->b_rptr += fp_mp_len;
13789 } else {
13791 * ip_xmit_attach_llhdr had to prepend an mblk to
13792 * attach the fastpath header before ip header.
13794 ip_mp = mp->b_cont;
13795 freeb(mp);
13796 mp = ip_mp;
13797 mp->b_rptr += (fp_mp_len - len);
13799 } else {
13800 ip_mp = mp->b_cont;
13801 freeb(mp);
13802 mp = ip_mp;
13804 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
13805 freemsg(mp);
13809 * Normal post fragmentation function.
13811 * Send a packet using the passed in nce. This handles both IPv4 and IPv6
13812 * using the same state machine.
13814 * We return an error on failure. In particular we return EWOULDBLOCK
13815 * when the driver flow controls. In that case this ensures that ip_wsrv runs
13816 * (currently by canputnext failure resulting in backenabling from GLD.)
13817 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
13818 * indication that they can flow control until ip_wsrv() tells then to restart.
13820 * If the nce passed by caller is incomplete, this function
13821 * queues the packet and if necessary, sends ARP request and bails.
13822 * If the Neighbor Cache passed is fully resolved, we simply prepend
13823 * the link-layer header to the packet, do ipsec hw acceleration
13824 * work if necessary, and send the packet out on the wire.
13826 /* ARGSUSED6 */
13828 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
13829 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
13831 queue_t *wq;
13832 ill_t *ill = nce->nce_ill;
13833 ip_stack_t *ipst = ill->ill_ipst;
13834 uint64_t delta;
13835 boolean_t isv6 = ill->ill_isv6;
13836 boolean_t fp_mp;
13837 ncec_t *ncec = nce->nce_common;
13838 int64_t now = LBOLT_FASTPATH64;
13839 boolean_t is_probe;
13841 DTRACE_PROBE1(ip__xmit, nce_t *, nce);
13843 ASSERT(mp != NULL);
13844 ASSERT(mp->b_datap->db_type == M_DATA);
13845 ASSERT(pkt_len == msgdsize(mp));
13848 * If we have already been here and are coming back after ARP/ND.
13849 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
13850 * in that case since they have seen the packet when it came here
13851 * the first time.
13853 if (ixaflags & IXAF_NO_TRACE)
13854 goto sendit;
13856 if (ixaflags & IXAF_IS_IPV4) {
13857 ipha_t *ipha = (ipha_t *)mp->b_rptr;
13859 ASSERT(!isv6);
13860 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
13861 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
13862 !(ixaflags & IXAF_NO_PFHOOK)) {
13863 int error;
13865 FW_HOOKS(ipst->ips_ip4_physical_out_event,
13866 ipst->ips_ipv4firewall_physical_out,
13867 NULL, ill, ipha, mp, mp, 0, ipst, error);
13868 DTRACE_PROBE1(ip4__physical__out__end,
13869 mblk_t *, mp);
13870 if (mp == NULL)
13871 return (error);
13873 /* The length could have changed */
13874 pkt_len = msgdsize(mp);
13876 if (ipst->ips_ip4_observe.he_interested) {
13878 * Note that for TX the zoneid is the sending
13879 * zone, whether or not MLP is in play.
13880 * Since the szone argument is the IP zoneid (i.e.,
13881 * zero for exclusive-IP zones) and ipobs wants
13882 * the system zoneid, we map it here.
13884 szone = IP_REAL_ZONEID(szone, ipst);
13887 * On the outbound path the destination zone will be
13888 * unknown as we're sending this packet out on the
13889 * wire.
13891 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
13892 ill, ipst);
13894 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
13895 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill,
13896 ipha_t *, ipha, ip6_t *, NULL, int, 0);
13897 } else {
13898 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
13900 ASSERT(isv6);
13901 ASSERT(pkt_len ==
13902 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
13903 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
13904 !(ixaflags & IXAF_NO_PFHOOK)) {
13905 int error;
13907 FW_HOOKS6(ipst->ips_ip6_physical_out_event,
13908 ipst->ips_ipv6firewall_physical_out,
13909 NULL, ill, ip6h, mp, mp, 0, ipst, error);
13910 DTRACE_PROBE1(ip6__physical__out__end,
13911 mblk_t *, mp);
13912 if (mp == NULL)
13913 return (error);
13915 /* The length could have changed */
13916 pkt_len = msgdsize(mp);
13918 if (ipst->ips_ip6_observe.he_interested) {
13919 /* See above */
13920 szone = IP_REAL_ZONEID(szone, ipst);
13922 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
13923 ill, ipst);
13925 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
13926 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill,
13927 ipha_t *, NULL, ip6_t *, ip6h, int, 0);
13930 sendit:
13932 * We check the state without a lock because the state can never
13933 * move "backwards" to initial or incomplete.
13935 switch (ncec->ncec_state) {
13936 case ND_REACHABLE:
13937 case ND_STALE:
13938 case ND_DELAY:
13939 case ND_PROBE:
13940 mp = ip_xmit_attach_llhdr(mp, nce);
13941 if (mp == NULL) {
13943 * ip_xmit_attach_llhdr has increased
13944 * ipIfStatsOutDiscards and called ip_drop_output()
13946 return (ENOBUFS);
13949 * check if nce_fastpath completed and we tagged on a
13950 * copy of nce_fp_mp in ip_xmit_attach_llhdr().
13952 fp_mp = (mp->b_datap->db_type == M_DATA);
13954 if (fp_mp &&
13955 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
13956 ill_dld_direct_t *idd;
13958 idd = &ill->ill_dld_capab->idc_direct;
13960 * Send the packet directly to DLD, where it
13961 * may be queued depending on the availability
13962 * of transmit resources at the media layer.
13963 * Return value should be taken into
13964 * account and flow control the TCP.
13966 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
13967 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
13968 pkt_len);
13970 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
13971 (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
13972 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
13973 } else {
13974 uintptr_t cookie;
13976 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
13977 mp, (uintptr_t)xmit_hint, 0)) != 0) {
13978 if (ixacookie != NULL)
13979 *ixacookie = cookie;
13980 return (EWOULDBLOCK);
13983 } else {
13984 wq = ill->ill_wq;
13986 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
13987 !canputnext(wq)) {
13988 if (ixacookie != NULL)
13989 *ixacookie = 0;
13990 ip_xmit_flowctl_drop(ill, mp, fp_mp,
13991 nce->nce_fp_mp != NULL ?
13992 MBLKL(nce->nce_fp_mp) : 0);
13993 return (EWOULDBLOCK);
13995 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
13996 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
13997 pkt_len);
13998 putnext(wq, mp);
14002 * The rest of this function implements Neighbor Unreachability
14003 * detection. Determine if the ncec is eligible for NUD.
14005 if (ncec->ncec_flags & NCE_F_NONUD)
14006 return (0);
14008 ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14011 * Check for upper layer advice
14013 if (ixaflags & IXAF_REACH_CONF) {
14014 timeout_id_t tid;
14017 * It should be o.k. to check the state without
14018 * a lock here, at most we lose an advice.
14020 ncec->ncec_last = TICK_TO_MSEC(now);
14021 if (ncec->ncec_state != ND_REACHABLE) {
14022 mutex_enter(&ncec->ncec_lock);
14023 ncec->ncec_state = ND_REACHABLE;
14024 tid = ncec->ncec_timeout_id;
14025 ncec->ncec_timeout_id = 0;
14026 mutex_exit(&ncec->ncec_lock);
14027 (void) untimeout(tid);
14028 if (ip_debug > 2) {
14029 /* ip1dbg */
14030 pr_addr_dbg("ip_xmit: state"
14031 " for %s changed to"
14032 " REACHABLE\n", AF_INET6,
14033 &ncec->ncec_addr);
14036 return (0);
14039 delta = TICK_TO_MSEC(now) - ncec->ncec_last;
14040 ip1dbg(("ip_xmit: delta = %" PRId64
14041 " ill_reachable_time = %d \n", delta,
14042 ill->ill_reachable_time));
14043 if (delta > (uint64_t)ill->ill_reachable_time) {
14044 mutex_enter(&ncec->ncec_lock);
14045 switch (ncec->ncec_state) {
14046 case ND_REACHABLE:
14047 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14048 /* FALLTHROUGH */
14049 case ND_STALE:
14051 * ND_REACHABLE is identical to
14052 * ND_STALE in this specific case. If
14053 * reachable time has expired for this
14054 * neighbor (delta is greater than
14055 * reachable time), conceptually, the
14056 * neighbor cache is no longer in
14057 * REACHABLE state, but already in
14058 * STALE state. So the correct
14059 * transition here is to ND_DELAY.
14061 ncec->ncec_state = ND_DELAY;
14062 mutex_exit(&ncec->ncec_lock);
14063 nce_restart_timer(ncec,
14064 ipst->ips_delay_first_probe_time);
14065 if (ip_debug > 3) {
14066 /* ip2dbg */
14067 pr_addr_dbg("ip_xmit: state"
14068 " for %s changed to"
14069 " DELAY\n", AF_INET6,
14070 &ncec->ncec_addr);
14072 break;
14073 case ND_DELAY:
14074 case ND_PROBE:
14075 mutex_exit(&ncec->ncec_lock);
14076 /* Timers have already started */
14077 break;
14078 case ND_UNREACHABLE:
14080 * nce_timer has detected that this ncec
14081 * is unreachable and initiated deleting
14082 * this ncec.
14083 * This is a harmless race where we found the
14084 * ncec before it was deleted and have
14085 * just sent out a packet using this
14086 * unreachable ncec.
14088 mutex_exit(&ncec->ncec_lock);
14089 break;
14090 default:
14091 ASSERT(0);
14092 mutex_exit(&ncec->ncec_lock);
14095 return (0);
14097 case ND_INCOMPLETE:
14099 * the state could have changed since we didn't hold the lock.
14100 * Re-verify state under lock.
14102 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14103 mutex_enter(&ncec->ncec_lock);
14104 if (NCE_ISREACHABLE(ncec)) {
14105 mutex_exit(&ncec->ncec_lock);
14106 goto sendit;
14108 /* queue the packet */
14109 nce_queue_mp(ncec, mp, is_probe);
14110 mutex_exit(&ncec->ncec_lock);
14111 DTRACE_PROBE2(ip__xmit__incomplete,
14112 (ncec_t *), ncec, (mblk_t *), mp);
14113 return (0);
14115 case ND_INITIAL:
14117 * State could have changed since we didn't hold the lock, so
14118 * re-verify state.
14120 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14121 mutex_enter(&ncec->ncec_lock);
14122 if (NCE_ISREACHABLE(ncec)) {
14123 mutex_exit(&ncec->ncec_lock);
14124 goto sendit;
14126 nce_queue_mp(ncec, mp, is_probe);
14127 if (ncec->ncec_state == ND_INITIAL) {
14128 ncec->ncec_state = ND_INCOMPLETE;
14129 mutex_exit(&ncec->ncec_lock);
14131 * figure out the source we want to use
14132 * and resolve it.
14134 ip_ndp_resolve(ncec);
14135 } else {
14136 mutex_exit(&ncec->ncec_lock);
14138 return (0);
14140 case ND_UNREACHABLE:
14141 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14142 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14143 mp, ill);
14144 freemsg(mp);
14145 return (0);
14147 default:
14148 ASSERT(0);
14149 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14150 ip_drop_output("ipIfStatsOutDiscards - ND_other",
14151 mp, ill);
14152 freemsg(mp);
14153 return (ENETUNREACH);
14158 * Return B_TRUE if the buffers differ in length or content.
14159 * This is used for comparing extension header buffers.
14160 * Note that an extension header would be declared different
14161 * even if all that changed was the next header value in that header i.e.
14162 * what really changed is the next extension header.
14164 boolean_t
14165 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14166 uint_t blen)
14168 if (!b_valid)
14169 blen = 0;
14171 if (alen != blen)
14172 return (B_TRUE);
14173 if (alen == 0)
14174 return (B_FALSE); /* Both zero length */
14175 return (bcmp(abuf, bbuf, alen));
14179 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14180 * Return B_FALSE if memory allocation fails - don't change any state!
14182 boolean_t
14183 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14184 const void *src, uint_t srclen)
14186 void *dst;
14188 if (!src_valid)
14189 srclen = 0;
14191 ASSERT(*dstlenp == 0);
14192 if (src != NULL && srclen != 0) {
14193 dst = mi_alloc(srclen, BPRI_MED);
14194 if (dst == NULL)
14195 return (B_FALSE);
14196 } else {
14197 dst = NULL;
14199 if (*dstp != NULL)
14200 mi_free(*dstp);
14201 *dstp = dst;
14202 *dstlenp = dst == NULL ? 0 : srclen;
14203 return (B_TRUE);
14207 * Replace what is in *dst, *dstlen with the source.
14208 * Assumes ip_allocbuf has already been called.
14210 void
14211 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14212 const void *src, uint_t srclen)
14214 if (!src_valid)
14215 srclen = 0;
14217 ASSERT(*dstlenp == srclen);
14218 if (src != NULL && srclen != 0)
14219 bcopy(src, *dstp, srclen);
14223 * Free the storage pointed to by the members of an ip_pkt_t.
14225 void
14226 ip_pkt_free(ip_pkt_t *ipp)
14228 uint_t fields = ipp->ipp_fields;
14230 if (fields & IPPF_HOPOPTS) {
14231 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14232 ipp->ipp_hopopts = NULL;
14233 ipp->ipp_hopoptslen = 0;
14235 if (fields & IPPF_RTHDRDSTOPTS) {
14236 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14237 ipp->ipp_rthdrdstopts = NULL;
14238 ipp->ipp_rthdrdstoptslen = 0;
14240 if (fields & IPPF_DSTOPTS) {
14241 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14242 ipp->ipp_dstopts = NULL;
14243 ipp->ipp_dstoptslen = 0;
14245 if (fields & IPPF_RTHDR) {
14246 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14247 ipp->ipp_rthdr = NULL;
14248 ipp->ipp_rthdrlen = 0;
14250 if (fields & IPPF_IPV4_OPTIONS) {
14251 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14252 ipp->ipp_ipv4_options = NULL;
14253 ipp->ipp_ipv4_options_len = 0;
14255 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14256 IPPF_RTHDR | IPPF_IPV4_OPTIONS);
14260 * Copy from src to dst and allocate as needed.
14261 * Returns zero or ENOMEM.
14263 * The caller must initialize dst to zero.
14266 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14268 uint_t fields = src->ipp_fields;
14270 /* Start with fields that don't require memory allocation */
14271 dst->ipp_fields = fields &
14272 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14273 IPPF_RTHDR | IPPF_IPV4_OPTIONS);
14275 dst->ipp_addr = src->ipp_addr;
14276 dst->ipp_unicast_hops = src->ipp_unicast_hops;
14277 dst->ipp_hoplimit = src->ipp_hoplimit;
14278 dst->ipp_tclass = src->ipp_tclass;
14279 dst->ipp_type_of_service = src->ipp_type_of_service;
14281 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14282 IPPF_RTHDR | IPPF_IPV4_OPTIONS)))
14283 return (0);
14285 if (fields & IPPF_HOPOPTS) {
14286 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14287 if (dst->ipp_hopopts == NULL) {
14288 ip_pkt_free(dst);
14289 return (ENOMEM);
14291 dst->ipp_fields |= IPPF_HOPOPTS;
14292 bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14293 src->ipp_hopoptslen);
14294 dst->ipp_hopoptslen = src->ipp_hopoptslen;
14296 if (fields & IPPF_RTHDRDSTOPTS) {
14297 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14298 kmflag);
14299 if (dst->ipp_rthdrdstopts == NULL) {
14300 ip_pkt_free(dst);
14301 return (ENOMEM);
14303 dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14304 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14305 src->ipp_rthdrdstoptslen);
14306 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14308 if (fields & IPPF_DSTOPTS) {
14309 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14310 if (dst->ipp_dstopts == NULL) {
14311 ip_pkt_free(dst);
14312 return (ENOMEM);
14314 dst->ipp_fields |= IPPF_DSTOPTS;
14315 bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14316 src->ipp_dstoptslen);
14317 dst->ipp_dstoptslen = src->ipp_dstoptslen;
14319 if (fields & IPPF_RTHDR) {
14320 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14321 if (dst->ipp_rthdr == NULL) {
14322 ip_pkt_free(dst);
14323 return (ENOMEM);
14325 dst->ipp_fields |= IPPF_RTHDR;
14326 bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14327 src->ipp_rthdrlen);
14328 dst->ipp_rthdrlen = src->ipp_rthdrlen;
14330 if (fields & IPPF_IPV4_OPTIONS) {
14331 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14332 kmflag);
14333 if (dst->ipp_ipv4_options == NULL) {
14334 ip_pkt_free(dst);
14335 return (ENOMEM);
14337 dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14338 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14339 src->ipp_ipv4_options_len);
14340 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14342 if (fields & IPPF_FRAGHDR) {
14343 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14344 if (dst->ipp_fraghdr == NULL) {
14345 ip_pkt_free(dst);
14346 return (ENOMEM);
14348 dst->ipp_fields |= IPPF_FRAGHDR;
14349 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14350 src->ipp_fraghdrlen);
14351 dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14353 return (0);
14357 * Returns INADDR_ANY if no source route
14359 ipaddr_t
14360 ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14362 ipaddr_t nexthop = INADDR_ANY;
14363 ipoptp_t opts;
14364 uchar_t *opt;
14365 uint8_t optval;
14366 uint8_t optlen;
14367 uint32_t totallen;
14369 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14370 return (INADDR_ANY);
14372 totallen = ipp->ipp_ipv4_options_len;
14373 if (totallen & 0x3)
14374 return (INADDR_ANY);
14376 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14377 optval != IPOPT_EOL;
14378 optval = ipoptp_next(&opts)) {
14379 opt = opts.ipoptp_cur;
14380 switch (optval) {
14381 uint8_t off;
14382 case IPOPT_SSRR:
14383 case IPOPT_LSRR:
14384 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
14385 break;
14387 optlen = opts.ipoptp_len;
14388 off = opt[IPOPT_OFFSET];
14389 off--;
14390 if (optlen < IP_ADDR_LEN ||
14391 off > optlen - IP_ADDR_LEN) {
14392 /* End of source route */
14393 break;
14395 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
14396 if (nexthop == htonl(INADDR_LOOPBACK)) {
14397 /* Ignore */
14398 nexthop = INADDR_ANY;
14399 break;
14401 break;
14404 return (nexthop);
14408 * Reverse a source route.
14410 void
14411 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
14413 ipaddr_t tmp;
14414 ipoptp_t opts;
14415 uchar_t *opt;
14416 uint8_t optval;
14417 uint32_t totallen;
14419 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14420 return;
14422 totallen = ipp->ipp_ipv4_options_len;
14423 if (totallen & 0x3)
14424 return;
14426 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14427 optval != IPOPT_EOL;
14428 optval = ipoptp_next(&opts)) {
14429 uint8_t off1, off2;
14431 opt = opts.ipoptp_cur;
14432 switch (optval) {
14433 case IPOPT_SSRR:
14434 case IPOPT_LSRR:
14435 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
14436 break;
14438 off1 = IPOPT_MINOFF_SR - 1;
14439 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
14440 while (off2 > off1) {
14441 bcopy(opt + off2, &tmp, IP_ADDR_LEN);
14442 bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
14443 bcopy(&tmp, opt + off2, IP_ADDR_LEN);
14444 off2 -= IP_ADDR_LEN;
14445 off1 += IP_ADDR_LEN;
14447 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
14448 break;
14454 * Returns NULL if no routing header
14456 in6_addr_t *
14457 ip_pkt_source_route_v6(const ip_pkt_t *ipp)
14459 in6_addr_t *nexthop = NULL;
14460 ip6_rthdr0_t *rthdr;
14462 if (!(ipp->ipp_fields & IPPF_RTHDR))
14463 return (NULL);
14465 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
14466 if (rthdr->ip6r0_segleft == 0)
14467 return (NULL);
14469 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
14470 return (nexthop);
14473 zoneid_t
14474 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
14475 zoneid_t lookup_zoneid)
14477 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
14478 ire_t *ire;
14479 int ire_flags = MATCH_IRE_TYPE;
14480 zoneid_t zoneid = ALL_ZONES;
14482 if (lookup_zoneid != ALL_ZONES)
14483 ire_flags |= MATCH_IRE_ZONEONLY;
14484 ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_LOCAL | IRE_LOOPBACK,
14485 NULL, lookup_zoneid, ire_flags, 0, ipst, NULL);
14486 if (ire != NULL) {
14487 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
14488 ire_refrele(ire);
14490 return (zoneid);
14493 zoneid_t
14494 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
14495 ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
14497 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
14498 ire_t *ire;
14499 int ire_flags = MATCH_IRE_TYPE;
14500 zoneid_t zoneid = ALL_ZONES;
14502 if (IN6_IS_ADDR_LINKLOCAL(addr))
14503 ire_flags |= MATCH_IRE_ILL;
14505 if (lookup_zoneid != ALL_ZONES)
14506 ire_flags |= MATCH_IRE_ZONEONLY;
14507 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
14508 ill, lookup_zoneid, ire_flags, 0, ipst, NULL);
14509 if (ire != NULL) {
14510 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
14511 ire_refrele(ire);
14513 return (zoneid);
14517 * IP obserability hook support functions.
14519 static void
14520 ipobs_init(ip_stack_t *ipst)
14522 netid_t id;
14524 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
14526 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
14527 VERIFY(ipst->ips_ip4_observe_pr != NULL);
14529 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
14530 VERIFY(ipst->ips_ip6_observe_pr != NULL);
14533 static void
14534 ipobs_fini(ip_stack_t *ipst)
14537 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
14538 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
14542 * hook_pkt_observe_t is composed in network byte order so that the
14543 * entire mblk_t chain handed into hook_run can be used as-is.
14544 * The caveat is that use of the fields, such as the zone fields,
14545 * requires conversion into host byte order first.
14547 void
14548 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
14549 const ill_t *ill, ip_stack_t *ipst)
14551 hook_pkt_observe_t *hdr;
14552 uint64_t grifindex;
14553 mblk_t *imp;
14555 imp = allocb(sizeof (*hdr), BPRI_HI);
14556 if (imp == NULL)
14557 return;
14559 hdr = (hook_pkt_observe_t *)imp->b_rptr;
14561 * b_wptr is set to make the apparent size of the data in the mblk_t
14562 * to exclude the pointers at the end of hook_pkt_observer_t.
14564 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
14565 imp->b_cont = mp;
14567 ASSERT(DB_TYPE(mp) == M_DATA);
14569 if (IS_UNDER_IPMP(ill))
14570 grifindex = ipmp_ill_get_ipmp_ifindex(ill);
14571 else
14572 grifindex = 0;
14574 hdr->hpo_version = 1;
14575 hdr->hpo_htype = htons(htype);
14576 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
14577 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
14578 hdr->hpo_grifindex = htonl(grifindex);
14579 hdr->hpo_zsrc = htonl(zsrc);
14580 hdr->hpo_zdst = htonl(zdst);
14581 hdr->hpo_pkt = imp;
14582 hdr->hpo_ctx = ipst->ips_netstack;
14584 if (ill->ill_isv6) {
14585 hdr->hpo_family = AF_INET6;
14586 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
14587 ipst->ips_ipv6observing, (hook_data_t)hdr);
14588 } else {
14589 hdr->hpo_family = AF_INET;
14590 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
14591 ipst->ips_ipv4observing, (hook_data_t)hdr);
14594 imp->b_cont = NULL;
14595 freemsg(imp);
14599 * Utility routine that checks if `v4srcp' is a valid address on underlying
14600 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif
14601 * associated with `v4srcp' on success. NOTE: if this is not called from
14602 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
14603 * group during or after this lookup.
14605 boolean_t
14606 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
14608 ipif_t *ipif;
14610 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
14611 if (ipif != NULL) {
14612 if (ipifp != NULL)
14613 *ipifp = ipif;
14614 else
14615 ipif_refrele(ipif);
14616 return (B_TRUE);
14619 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
14620 *v4srcp));
14621 return (B_FALSE);
14625 * Transport protocol call back function for CPU state change.
14627 /* ARGSUSED */
14628 static int
14629 ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
14631 processorid_t cpu_seqid;
14632 netstack_handle_t nh;
14633 netstack_t *ns;
14635 ASSERT(MUTEX_HELD(&cpu_lock));
14637 switch (what) {
14638 case CPU_CONFIG:
14639 case CPU_ON:
14640 case CPU_INIT:
14641 case CPU_CPUPART_IN:
14642 cpu_seqid = cpu[id]->cpu_seqid;
14643 netstack_next_init(&nh);
14644 while ((ns = netstack_next(&nh)) != NULL) {
14645 tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
14646 sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
14647 udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
14648 netstack_rele(ns);
14650 netstack_next_fini(&nh);
14651 break;
14652 case CPU_UNCONFIG:
14653 case CPU_OFF:
14654 case CPU_CPUPART_OUT:
14656 * Nothing to do. We don't remove the per CPU stats from
14657 * the IP stack even when the CPU goes offline.
14659 break;
14660 default:
14661 break;
14663 return (0);