1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (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) 2019 Joyent, Inc. All rights reserved.
28 */
29
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>
52
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>
67
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>
76
77 #include <netinet/igmp_var.h>
78 #include <netinet/ip6.h>
79 #include <netinet/icmp6.h>
80 #include <netinet/sctp.h>
81
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>
98 #include <inet/cc.h>
99
100 #include <net/pfkeyv2.h>
101 #include <inet/sadb.h>
102 #include <inet/ipsec_impl.h>
103 #include <inet/iptun/iptun_impl.h>
104 #include <inet/ipdrop.h>
105 #include <inet/ip_netinfo.h>
106 #include <inet/ilb_ip.h>
107
108 #include <sys/ethernet.h>
109 #include <net/if_types.h>
110 #include <sys/cpuvar.h>
111
112 #include <ipp/ipp.h>
113 #include <ipp/ipp_impl.h>
114 #include <ipp/ipgpc/ipgpc.h>
115
116 #include <sys/pattr.h>
117 #include <inet/ipclassifier.h>
118 #include <inet/sctp_ip.h>
119 #include <inet/sctp/sctp_impl.h>
120 #include <inet/udp_impl.h>
121 #include <inet/rawip_impl.h>
122 #include <inet/rts_impl.h>
123
124 #include <sys/tsol/label.h>
125 #include <sys/tsol/tnet.h>
126
127 #include <sys/squeue_impl.h>
128 #include <inet/ip_arp.h>
129
130 #include <sys/clock_impl.h> /* For LBOLT_FASTPATH{,64} */
131
132 /*
133 * Values for squeue switch:
134 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
135 * IP_SQUEUE_ENTER: SQ_PROCESS
136 * IP_SQUEUE_FILL: SQ_FILL
137 */
138 int ip_squeue_enter = IP_SQUEUE_ENTER; /* Setable in /etc/system */
139
140 int ip_squeue_flag;
141
142 /*
143 * Setable in /etc/system
144 */
145 int ip_poll_normal_ms = 100;
146 int ip_poll_normal_ticks = 0;
147 int ip_modclose_ackwait_ms = 3000;
148
149 /*
150 * It would be nice to have these present only in DEBUG systems, but the
151 * current design of the global symbol checking logic requires them to be
152 * unconditionally present.
153 */
154 uint_t ip_thread_data; /* TSD key for debug support */
155 krwlock_t ip_thread_rwlock;
156 list_t ip_thread_list;
157
158 /*
159 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
160 */
161
162 struct listptr_s {
163 mblk_t *lp_head; /* pointer to the head of the list */
164 mblk_t *lp_tail; /* pointer to the tail of the list */
165 };
166
167 typedef struct listptr_s listptr_t;
168
169 /*
170 * This is used by ip_snmp_get_mib2_ip_route_media and
171 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
172 */
173 typedef struct iproutedata_s {
174 uint_t ird_idx;
175 uint_t ird_flags; /* see below */
176 listptr_t ird_route; /* ipRouteEntryTable */
177 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
178 listptr_t ird_attrs; /* ipRouteAttributeTable */
179 } iproutedata_t;
180
181 /* Include ire_testhidden and IRE_IF_CLONE routes */
182 #define IRD_REPORT_ALL 0x01
183
184 /*
185 * Cluster specific hooks. These should be NULL when booted as a non-cluster
186 */
187
188 /*
189 * Hook functions to enable cluster networking
190 * On non-clustered systems these vectors must always be NULL.
191 *
192 * Hook function to Check ip specified ip address is a shared ip address
193 * in the cluster
194 *
195 */
196 int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol,
197 sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL;
198
199 /*
200 * Hook function to generate cluster wide ip fragment identifier
201 */
202 uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol,
203 sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp,
204 void *args) = NULL;
205
206 /*
207 * Hook function to generate cluster wide SPI.
208 */
209 void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t,
210 void *) = NULL;
211
212 /*
213 * Hook function to verify if the SPI is already utlized.
214 */
215
216 int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
217
218 /*
219 * Hook function to delete the SPI from the cluster wide repository.
220 */
221
222 void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
223
224 /*
225 * Hook function to inform the cluster when packet received on an IDLE SA
226 */
227
228 void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t,
229 in6_addr_t, in6_addr_t, void *) = NULL;
230
231 /*
232 * Synchronization notes:
233 *
234 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
235 * MT level protection given by STREAMS. IP uses a combination of its own
236 * internal serialization mechanism and standard Solaris locking techniques.
237 * The internal serialization is per phyint. This is used to serialize
238 * plumbing operations, IPMP operations, most set ioctls, etc.
239 *
240 * Plumbing is a long sequence of operations involving message
241 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
242 * involved in plumbing operations. A natural model is to serialize these
243 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
244 * parallel without any interference. But various set ioctls on hme0 are best
245 * serialized, along with IPMP operations and processing of DLPI control
246 * messages received from drivers on a per phyint basis. This serialization is
247 * provided by the ipsq_t and primitives operating on this. Details can
248 * be found in ip_if.c above the core primitives operating on ipsq_t.
249 *
250 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
251 * Simiarly lookup of an ire by a thread also returns a refheld ire.
252 * In addition ipif's and ill's referenced by the ire are also indirectly
253 * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
254 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
255 * address of an ipif has to go through the ipsq_t. This ensures that only
256 * one such exclusive operation proceeds at any time on the ipif. It then
257 * waits for all refcnts
258 * associated with this ipif to come down to zero. The address is changed
259 * only after the ipif has been quiesced. Then the ipif is brought up again.
260 * More details are described above the comment in ip_sioctl_flags.
261 *
262 * Packet processing is based mostly on IREs and are fully multi-threaded
263 * using standard Solaris MT techniques.
264 *
265 * There are explicit locks in IP to handle:
266 * - The ip_g_head list maintained by mi_open_link() and friends.
267 *
268 * - The reassembly data structures (one lock per hash bucket)
269 *
270 * - conn_lock is meant to protect conn_t fields. The fields actually
271 * protected by conn_lock are documented in the conn_t definition.
272 *
273 * - ire_lock to protect some of the fields of the ire, IRE tables
274 * (one lock per hash bucket). Refer to ip_ire.c for details.
275 *
276 * - ndp_g_lock and ncec_lock for protecting NCEs.
277 *
278 * - ill_lock protects fields of the ill and ipif. Details in ip.h
279 *
280 * - ill_g_lock: This is a global reader/writer lock. Protects the following
281 * * The AVL tree based global multi list of all ills.
282 * * The linked list of all ipifs of an ill
283 * * The <ipsq-xop> mapping
284 * * <ill-phyint> association
285 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
286 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the
287 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
288 * writer for the actual duration of the insertion/deletion/change.
289 *
290 * - ill_lock: This is a per ill mutex.
291 * It protects some members of the ill_t struct; see ip.h for details.
292 * It also protects the <ill-phyint> assoc.
293 * It also protects the list of ipifs hanging off the ill.
294 *
295 * - ipsq_lock: This is a per ipsq_t mutex lock.
296 * This protects some members of the ipsq_t struct; see ip.h for details.
297 * It also protects the <ipsq-ipxop> mapping
298 *
299 * - ipx_lock: This is a per ipxop_t mutex lock.
300 * This protects some members of the ipxop_t struct; see ip.h for details.
301 *
302 * - phyint_lock: This is a per phyint mutex lock. Protects just the
303 * phyint_flags
304 *
305 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
306 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
307 * uniqueness check also done atomically.
308 *
309 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
310 * group list linked by ill_usesrc_grp_next. It also protects the
311 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
312 * group is being added or deleted. This lock is taken as a reader when
313 * walking the list/group(eg: to get the number of members in a usesrc group).
314 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
315 * field is changing state i.e from NULL to non-NULL or vice-versa. For
316 * example, it is not necessary to take this lock in the initial portion
317 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
318 * operations are executed exclusively and that ensures that the "usesrc
319 * group state" cannot change. The "usesrc group state" change can happen
320 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
321 *
322 * Changing <ill-phyint>, <ipsq-xop> assocications:
323 *
324 * To change the <ill-phyint> association, the ill_g_lock must be held
325 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
326 * must be held.
327 *
328 * To change the <ipsq-xop> association, the ill_g_lock must be held as
329 * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
330 * This is only done when ills are added or removed from IPMP groups.
331 *
332 * To add or delete an ipif from the list of ipifs hanging off the ill,
333 * ill_g_lock (writer) and ill_lock must be held and the thread must be
334 * a writer on the associated ipsq.
335 *
336 * To add or delete an ill to the system, the ill_g_lock must be held as
337 * writer and the thread must be a writer on the associated ipsq.
338 *
339 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
340 * must be a writer on the associated ipsq.
341 *
342 * Lock hierarchy
343 *
344 * Some lock hierarchy scenarios are listed below.
345 *
346 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
347 * ill_g_lock -> ill_lock(s) -> phyint_lock
348 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
349 * ill_g_lock -> ip_addr_avail_lock
350 * conn_lock -> irb_lock -> ill_lock -> ire_lock
351 * ill_g_lock -> ip_g_nd_lock
352 * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
353 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
354 * arl_lock -> ill_lock
355 * ips_ire_dep_lock -> irb_lock
356 *
357 * When more than 1 ill lock is needed to be held, all ill lock addresses
358 * are sorted on address and locked starting from highest addressed lock
359 * downward.
360 *
361 * Multicast scenarios
362 * ips_ill_g_lock -> ill_mcast_lock
363 * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
364 * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
365 * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
366 * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
367 * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
368 *
369 * IPsec scenarios
370 *
371 * ipsa_lock -> ill_g_lock -> ill_lock
372 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
373 *
374 * Trusted Solaris scenarios
375 *
376 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
377 * igsa_lock -> gcdb_lock
378 * gcgrp_rwlock -> ire_lock
379 * gcgrp_rwlock -> gcdb_lock
380 *
381 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
382 *
383 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
384 * sq_lock -> conn_lock -> QLOCK(q)
385 * ill_lock -> ft_lock -> fe_lock
386 *
387 * Routing/forwarding table locking notes:
388 *
389 * Lock acquisition order: Radix tree lock, irb_lock.
390 * Requirements:
391 * i. Walker must not hold any locks during the walker callback.
392 * ii Walker must not see a truncated tree during the walk because of any node
393 * deletion.
394 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
395 * in many places in the code to walk the irb list. Thus even if all the
396 * ires in a bucket have been deleted, we still can't free the radix node
397 * until the ires have actually been inactive'd (freed).
398 *
399 * Tree traversal - Need to hold the global tree lock in read mode.
400 * Before dropping the global tree lock, need to either increment the ire_refcnt
401 * to ensure that the radix node can't be deleted.
402 *
403 * Tree add - Need to hold the global tree lock in write mode to add a
404 * radix node. To prevent the node from being deleted, increment the
405 * irb_refcnt, after the node is added to the tree. The ire itself is
406 * added later while holding the irb_lock, but not the tree lock.
407 *
408 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
409 * All associated ires must be inactive (i.e. freed), and irb_refcnt
410 * must be zero.
411 *
412 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
413 * global tree lock (read mode) for traversal.
414 *
415 * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
416 * hence we will acquire irb_lock while holding ips_ire_dep_lock.
417 *
418 * IPsec notes :
419 *
420 * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
421 * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
422 * ip_xmit_attr_t has the
423 * information used by the IPsec code for applying the right level of
424 * protection. The information initialized by IP in the ip_xmit_attr_t
425 * is determined by the per-socket policy or global policy in the system.
426 * For inbound datagrams, the ip_recv_attr_t
427 * starts out with nothing in it. It gets filled
428 * with the right information if it goes through the AH/ESP code, which
429 * happens if the incoming packet is secure. The information initialized
430 * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
431 * the policy requirements needed by per-socket policy or global policy
432 * is met or not.
433 *
434 * For fully connected sockets i.e dst, src [addr, port] is known,
435 * conn_policy_cached is set indicating that policy has been cached.
436 * conn_in_enforce_policy may or may not be set depending on whether
437 * there is a global policy match or per-socket policy match.
438 * Policy inheriting happpens in ip_policy_set once the destination is known.
439 * Once the right policy is set on the conn_t, policy cannot change for
440 * this socket. This makes life simpler for TCP (UDP ?) where
441 * re-transmissions go out with the same policy. For symmetry, policy
442 * is cached for fully connected UDP sockets also. Thus if policy is cached,
443 * it also implies that policy is latched i.e policy cannot change
444 * on these sockets. As we have the right policy on the conn, we don't
445 * have to lookup global policy for every outbound and inbound datagram
446 * and thus serving as an optimization. Note that a global policy change
447 * does not affect fully connected sockets if they have policy. If fully
448 * connected sockets did not have any policy associated with it, global
449 * policy change may affect them.
450 *
451 * IP Flow control notes:
452 * ---------------------
453 * Non-TCP streams are flow controlled by IP. The way this is accomplished
454 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
455 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
456 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
457 * functions.
458 *
459 * Per Tx ring udp flow control:
460 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
461 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
462 *
463 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
464 * To achieve best performance, outgoing traffic need to be fanned out among
465 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
466 * traffic out of the NIC and it takes a fanout hint. UDP connections pass
467 * the address of connp as fanout hint to mac_tx(). Under flow controlled
468 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
469 * cookie points to a specific Tx ring that is blocked. The cookie is used to
470 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
471 * point to drain_lists (idl_t's). These drain list will store the blocked UDP
472 * connp's. The drain list is not a single list but a configurable number of
473 * lists.
474 *
475 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
476 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
477 * which is equal to 128. This array in turn contains a pointer to idl_t[],
478 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
479 * list will point to the list of connp's that are flow controlled.
480 *
481 * --------------- ------- ------- -------
482 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
483 * | --------------- ------- ------- -------
484 * | --------------- ------- ------- -------
485 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
486 * ---------------- | --------------- ------- ------- -------
487 * |idl_tx_list[0]|->| --------------- ------- ------- -------
488 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
489 * | --------------- ------- ------- -------
490 * . . . . .
491 * | --------------- ------- ------- -------
492 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
493 * --------------- ------- ------- -------
494 * --------------- ------- ------- -------
495 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
496 * | --------------- ------- ------- -------
497 * | --------------- ------- ------- -------
498 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
499 * |idl_tx_list[1]|->| --------------- ------- ------- -------
500 * ---------------- | . . . .
501 * | --------------- ------- ------- -------
502 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
503 * --------------- ------- ------- -------
504 * .....
505 * ----------------
506 * |idl_tx_list[n]|-> ...
507 * ----------------
508 *
509 * When mac_tx() returns a cookie, the cookie is hashed into an index into
510 * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
511 * to insert the conn onto. conn_drain_insert() asserts flow control for the
512 * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
513 * Further, conn_blocked is set to indicate that the conn is blocked.
514 *
515 * GLDv3 calls ill_flow_enable() when flow control is relieved. The cookie
516 * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
517 * is again hashed to locate the appropriate idl_tx_list, which is then
518 * drained via conn_walk_drain(). conn_walk_drain() goes through each conn in
519 * the drain list and calls conn_drain_remove() to clear flow control (via
520 * calling su_txq_full() or clearing QFULL), and remove the conn from the
521 * drain list.
522 *
523 * Note that the drain list is not a single list but a (configurable) array of
524 * lists (8 elements by default). Synchronization between drain insertion and
525 * flow control wakeup is handled by using idl_txl->txl_lock, and only
526 * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
527 *
528 * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
529 * On the send side, if the packet cannot be sent down to the driver by IP
530 * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
531 * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
532 * the 0'th drain list. When ip_wsrv() runs on the ill_wq because flow
533 * control has been relieved, the blocked conns in the 0'th drain list are
534 * drained as in the non-STREAMS case.
535 *
536 * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
537 * is done when the conn is inserted into the drain list (conn_drain_insert())
538 * and cleared when the conn is removed from the it (conn_drain_remove()).
539 *
540 * IPQOS notes:
541 *
542 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
543 * and IPQoS modules. IPPF includes hooks in IP at different control points
544 * (callout positions) which direct packets to IPQoS modules for policy
545 * processing. Policies, if present, are global.
546 *
547 * The callout positions are located in the following paths:
548 * o local_in (packets destined for this host)
549 * o local_out (packets orginating from this host )
550 * o fwd_in (packets forwarded by this m/c - inbound)
551 * o fwd_out (packets forwarded by this m/c - outbound)
552 * Hooks at these callout points can be enabled/disabled using the ndd variable
553 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
554 * By default all the callout positions are enabled.
555 *
556 * Outbound (local_out)
557 * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
558 *
559 * Inbound (local_in)
560 * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
561 *
562 * Forwarding (in and out)
563 * Hooks are placed in ire_recv_forward_v4/v6.
564 *
565 * IP Policy Framework processing (IPPF processing)
566 * Policy processing for a packet is initiated by ip_process, which ascertains
567 * that the classifier (ipgpc) is loaded and configured, failing which the
568 * packet resumes normal processing in IP. If the clasifier is present, the
569 * packet is acted upon by one or more IPQoS modules (action instances), per
570 * filters configured in ipgpc and resumes normal IP processing thereafter.
571 * An action instance can drop a packet in course of its processing.
572 *
573 * Zones notes:
574 *
575 * The partitioning rules for networking are as follows:
576 * 1) Packets coming from a zone must have a source address belonging to that
577 * zone.
578 * 2) Packets coming from a zone can only be sent on a physical interface on
579 * which the zone has an IP address.
580 * 3) Between two zones on the same machine, packet delivery is only allowed if
581 * there's a matching route for the destination and zone in the forwarding
582 * table.
583 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
584 * different zones can bind to the same port with the wildcard address
585 * (INADDR_ANY).
586 *
587 * The granularity of interface partitioning is at the logical interface level.
588 * Therefore, every zone has its own IP addresses, and incoming packets can be
589 * attributed to a zone unambiguously. A logical interface is placed into a zone
590 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
591 * structure. Rule (1) is implemented by modifying the source address selection
592 * algorithm so that the list of eligible addresses is filtered based on the
593 * sending process zone.
594 *
595 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
596 * across all zones, depending on their type. Here is the break-up:
597 *
598 * IRE type Shared/exclusive
599 * -------- ----------------
600 * IRE_BROADCAST Exclusive
601 * IRE_DEFAULT (default routes) Shared (*)
602 * IRE_LOCAL Exclusive (x)
603 * IRE_LOOPBACK Exclusive
604 * IRE_PREFIX (net routes) Shared (*)
605 * IRE_IF_NORESOLVER (interface routes) Exclusive
606 * IRE_IF_RESOLVER (interface routes) Exclusive
607 * IRE_IF_CLONE (interface routes) Exclusive
608 * IRE_HOST (host routes) Shared (*)
609 *
610 * (*) A zone can only use a default or off-subnet route if the gateway is
611 * directly reachable from the zone, that is, if the gateway's address matches
612 * one of the zone's logical interfaces.
613 *
614 * (x) IRE_LOCAL are handled a bit differently.
615 * When ip_restrict_interzone_loopback is set (the default),
616 * ire_route_recursive restricts loopback using an IRE_LOCAL
617 * between zone to the case when L2 would have conceptually looped the packet
618 * back, i.e. the loopback which is required since neither Ethernet drivers
619 * nor Ethernet hardware loops them back. This is the case when the normal
620 * routes (ignoring IREs with different zoneids) would send out the packet on
621 * the same ill as the ill with which is IRE_LOCAL is associated.
622 *
623 * Multiple zones can share a common broadcast address; typically all zones
624 * share the 255.255.255.255 address. Incoming as well as locally originated
625 * broadcast packets must be dispatched to all the zones on the broadcast
626 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
627 * since some zones may not be on the 10.16.72/24 network. To handle this, each
628 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
629 * sent to every zone that has an IRE_BROADCAST entry for the destination
630 * address on the input ill, see ip_input_broadcast().
631 *
632 * Applications in different zones can join the same multicast group address.
633 * The same logic applies for multicast as for broadcast. ip_input_multicast
634 * dispatches packets to all zones that have members on the physical interface.
635 */
636
637 /*
638 * Squeue Fanout flags:
639 * 0: No fanout.
640 * 1: Fanout across all squeues
641 */
642 boolean_t ip_squeue_fanout = 0;
643
644 /*
645 * Maximum dups allowed per packet.
646 */
647 uint_t ip_max_frag_dups = 10;
648
649 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
650 cred_t *credp, boolean_t isv6);
651 static mblk_t *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
652
653 static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
654 static void icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
655 static void icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
656 ip_recv_attr_t *);
657 static void icmp_options_update(ipha_t *);
658 static void icmp_param_problem(mblk_t *, uint8_t, ip_recv_attr_t *);
659 static void icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
660 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
661 static void icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
662 ip_recv_attr_t *);
663 static void icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
664 static void icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
665 ip_recv_attr_t *);
666
667 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
668 char *ip_dot_addr(ipaddr_t, char *);
669 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
670 static char *ip_dot_saddr(uchar_t *, char *);
671 static int ip_lrput(queue_t *, mblk_t *);
672 ipaddr_t ip_net_mask(ipaddr_t);
673 char *ip_nv_lookup(nv_t *, int);
674 int ip_rput(queue_t *, mblk_t *);
675 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
676 void *dummy_arg);
677 int ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
678 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
679 mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
680 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
681 ip_stack_t *, boolean_t);
682 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
683 boolean_t);
684 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
685 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
686 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
687 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
688 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
689 ip_stack_t *ipst, boolean_t);
690 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
691 ip_stack_t *ipst, boolean_t);
692 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
693 ip_stack_t *ipst);
694 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
695 ip_stack_t *ipst);
696 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
697 ip_stack_t *ipst);
698 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
699 ip_stack_t *ipst);
700 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
701 ip_stack_t *ipst);
702 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
703 ip_stack_t *ipst);
704 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
705 ip_stack_t *ipst);
706 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
707 ip_stack_t *ipst);
708 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
709 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
710 static void ip_snmp_get2_v4_media(ncec_t *, void *);
711 static void ip_snmp_get2_v6_media(ncec_t *, void *);
712 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
713
714 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
715 mblk_t *);
716
717 static void conn_drain_init(ip_stack_t *);
718 static void conn_drain_fini(ip_stack_t *);
719 static void conn_drain(conn_t *connp, boolean_t closing);
720
721 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
722 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
723
724 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
725 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
726 static void ip_stack_fini(netstackid_t stackid, void *arg);
727
728 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
729 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
730 ire_t *, conn_t *, boolean_t, const in6_addr_t *, mcast_record_t,
731 const in6_addr_t *);
732
733 static int ip_squeue_switch(int);
734
735 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
736 static void ip_kstat_fini(netstackid_t, kstat_t *);
737 static int ip_kstat_update(kstat_t *kp, int rw);
738 static void *icmp_kstat_init(netstackid_t);
739 static void icmp_kstat_fini(netstackid_t, kstat_t *);
740 static int icmp_kstat_update(kstat_t *kp, int rw);
741 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
742 static void ip_kstat2_fini(netstackid_t, kstat_t *);
743
744 static void ipobs_init(ip_stack_t *);
745 static void ipobs_fini(ip_stack_t *);
746
747 static int ip_tp_cpu_update(cpu_setup_t, int, void *);
748
749 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
750
751 static long ip_rput_pullups;
752 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
753
754 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
755 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
756
757 int ip_debug;
758
759 /*
760 * Multirouting/CGTP stuff
761 */
762 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */
763
764 /*
765 * IP tunables related declarations. Definitions are in ip_tunables.c
766 */
767 extern mod_prop_info_t ip_propinfo_tbl[];
768 extern int ip_propinfo_count;
769
770 /*
771 * Table of IP ioctls encoding the various properties of the ioctl and
772 * indexed based on the last byte of the ioctl command. Occasionally there
773 * is a clash, and there is more than 1 ioctl with the same last byte.
774 * In such a case 1 ioctl is encoded in the ndx table and the remaining
775 * ioctls are encoded in the misc table. An entry in the ndx table is
776 * retrieved by indexing on the last byte of the ioctl command and comparing
777 * the ioctl command with the value in the ndx table. In the event of a
778 * mismatch the misc table is then searched sequentially for the desired
779 * ioctl command.
780 *
781 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
782 */
783 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
784 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
785 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
786 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
787 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
788 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
789 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
790 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
791 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
792 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
793 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
794
795 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
796 MISC_CMD, ip_siocaddrt, NULL },
797 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
798 MISC_CMD, ip_siocdelrt, NULL },
799
800 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
801 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
802 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
803 IF_CMD, ip_sioctl_get_addr, NULL },
804
805 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
806 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
807 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
808 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
809
810 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
811 IPI_PRIV | IPI_WR,
812 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
813 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
814 IPI_MODOK | IPI_GET_CMD,
815 IF_CMD, ip_sioctl_get_flags, NULL },
816
817 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
818 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
819
820 /* copyin size cannot be coded for SIOCGIFCONF */
821 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
822 MISC_CMD, ip_sioctl_get_ifconf, NULL },
823
824 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
825 IF_CMD, ip_sioctl_mtu, NULL },
826 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
827 IF_CMD, ip_sioctl_get_mtu, NULL },
828 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
829 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
830 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
831 IF_CMD, ip_sioctl_brdaddr, NULL },
832 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
833 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
834 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
835 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
836 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
837 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
838 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
839 IF_CMD, ip_sioctl_metric, NULL },
840 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
841
842 /* See 166-168 below for extended SIOC*XARP ioctls */
843 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
844 ARP_CMD, ip_sioctl_arp, NULL },
845 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
846 ARP_CMD, ip_sioctl_arp, NULL },
847 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
848 ARP_CMD, ip_sioctl_arp, NULL },
849
850 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
851 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
852 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
853 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
854 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
855 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
856 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
857 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
858 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
859 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
860 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
861 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
862 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
863 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
864 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
865 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
866 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
867 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
868 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
869 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
870 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
871
872 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
873 MISC_CMD, if_unitsel, if_unitsel_restart },
874
875 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
876 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
877 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
878 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
879 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
880 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
881 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
882 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
883 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
884 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
885 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
886 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
887 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
888 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
889 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
890 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
891 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
892 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
893
894 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
895 IPI_PRIV | IPI_WR | IPI_MODOK,
896 IF_CMD, ip_sioctl_sifname, NULL },
897
898 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
899 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
900 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
901 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
902 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
903 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
904 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
905 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
906 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
907 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
908 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
909 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
910 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
911
912 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
913 MISC_CMD, ip_sioctl_get_ifnum, NULL },
914 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
915 IF_CMD, ip_sioctl_get_muxid, NULL },
916 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
917 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
918
919 /* Both if and lif variants share same func */
920 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
921 IF_CMD, ip_sioctl_get_lifindex, NULL },
922 /* Both if and lif variants share same func */
923 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
924 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
925
926 /* copyin size cannot be coded for SIOCGIFCONF */
927 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
928 MISC_CMD, ip_sioctl_get_ifconf, NULL },
929 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
930 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
931 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
932 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
933 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
934 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
935 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
936 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
937 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
938 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
939 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
940 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
941 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
942 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
943 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
944 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
945 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
946
947 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
948 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
949 ip_sioctl_removeif_restart },
950 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
951 IPI_GET_CMD | IPI_PRIV | IPI_WR,
952 LIF_CMD, ip_sioctl_addif, NULL },
953 #define SIOCLIFADDR_NDX 112
954 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
955 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
956 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
957 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
958 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
959 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
960 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
961 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
962 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
963 IPI_PRIV | IPI_WR,
964 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
965 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
966 IPI_GET_CMD | IPI_MODOK,
967 LIF_CMD, ip_sioctl_get_flags, NULL },
968
969 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
970 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
971
972 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
973 ip_sioctl_get_lifconf, NULL },
974 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
975 LIF_CMD, ip_sioctl_mtu, NULL },
976 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
977 LIF_CMD, ip_sioctl_get_mtu, NULL },
978 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
979 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
980 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
981 LIF_CMD, ip_sioctl_brdaddr, NULL },
982 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
983 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
984 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
985 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
986 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
987 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
988 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
989 LIF_CMD, ip_sioctl_metric, NULL },
990 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
991 IPI_PRIV | IPI_WR | IPI_MODOK,
992 LIF_CMD, ip_sioctl_slifname,
993 ip_sioctl_slifname_restart },
994
995 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
996 MISC_CMD, ip_sioctl_get_lifnum, NULL },
997 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
998 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
999 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
1000 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
1001 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1002 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
1003 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1004 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
1005 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1006 LIF_CMD, ip_sioctl_token, NULL },
1007 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1008 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
1009 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1010 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1011 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1012 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
1013 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1014 LIF_CMD, ip_sioctl_lnkinfo, NULL },
1015
1016 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1017 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1018 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1019 LIF_CMD, ip_siocdelndp_v6, NULL },
1020 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1021 LIF_CMD, ip_siocqueryndp_v6, NULL },
1022 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1023 LIF_CMD, ip_siocsetndp_v6, NULL },
1024 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1025 MISC_CMD, ip_sioctl_tmyaddr, NULL },
1026 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1027 MISC_CMD, ip_sioctl_tonlink, NULL },
1028 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1029 MISC_CMD, ip_sioctl_tmysite, NULL },
1030 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1031 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1032
1033 /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
1034 /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1035 /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1036 /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1038
1039 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1040
1041 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
1042 LIF_CMD, ip_sioctl_get_binding, NULL },
1043 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1044 IPI_PRIV | IPI_WR,
1045 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1046 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1047 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
1048 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
1049 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
1050
1051 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1052 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1053 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1054 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1055
1056 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1057
1058 /* These are handled in ip_sioctl_copyin_setup itself */
1059 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1060 MISC_CMD, NULL, NULL },
1061 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1062 MISC_CMD, NULL, NULL },
1063 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1064
1065 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1066 ip_sioctl_get_lifconf, NULL },
1067
1068 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1069 XARP_CMD, ip_sioctl_arp, NULL },
1070 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1071 XARP_CMD, ip_sioctl_arp, NULL },
1072 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1073 XARP_CMD, ip_sioctl_arp, NULL },
1074
1075 /* SIOCPOPSOCKFS is not handled by IP */
1076 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1077
1078 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1079 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1080 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1081 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1082 ip_sioctl_slifzone_restart },
1083 /* 172-174 are SCTP ioctls and not handled by IP */
1084 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1085 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1086 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1087 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1088 IPI_GET_CMD, LIF_CMD,
1089 ip_sioctl_get_lifusesrc, 0 },
1090 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1091 IPI_PRIV | IPI_WR,
1092 LIF_CMD, ip_sioctl_slifusesrc,
1093 NULL },
1094 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1095 ip_sioctl_get_lifsrcof, NULL },
1096 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1097 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1098 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1099 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1100 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1101 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1102 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1103 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1104 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1105 /* SIOCSENABLESDP is handled by SDP */
1106 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1107 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1108 /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1109 IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1110 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1111 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1112 ip_sioctl_ilb_cmd, NULL },
1113 /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL },
1114 /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL},
1115 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1116 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1117 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1118 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1119 /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1120 LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1121 };
1122
1123 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1124
1125 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1126 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1127 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1128 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1129 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1130 { ND_GET, 0, 0, 0, NULL, NULL },
1131 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1132 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1133 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1134 MISC_CMD, mrt_ioctl},
1135 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1136 MISC_CMD, mrt_ioctl},
1137 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1138 MISC_CMD, mrt_ioctl}
1139 };
1140
1141 int ip_misc_ioctl_count =
1142 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1143
1144 int conn_drain_nthreads; /* Number of drainers reqd. */
1145 /* Settable in /etc/system */
1146 /* Defined in ip_ire.c */
1147 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1148 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1149 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1150
1151 static nv_t ire_nv_arr[] = {
1152 { IRE_BROADCAST, "BROADCAST" },
1153 { IRE_LOCAL, "LOCAL" },
1154 { IRE_LOOPBACK, "LOOPBACK" },
1155 { IRE_DEFAULT, "DEFAULT" },
1156 { IRE_PREFIX, "PREFIX" },
1157 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1158 { IRE_IF_RESOLVER, "IF_RESOLV" },
1159 { IRE_IF_CLONE, "IF_CLONE" },
1160 { IRE_HOST, "HOST" },
1161 { IRE_MULTICAST, "MULTICAST" },
1162 { IRE_NOROUTE, "NOROUTE" },
1163 { 0 }
1164 };
1165
1166 nv_t *ire_nv_tbl = ire_nv_arr;
1167
1168 /* Simple ICMP IP Header Template */
1169 static ipha_t icmp_ipha = {
1170 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1171 };
1172
1173 struct module_info ip_mod_info = {
1174 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1175 IP_MOD_LOWAT
1176 };
1177
1178 /*
1179 * Duplicate static symbols within a module confuses mdb; so we avoid the
1180 * problem by making the symbols here distinct from those in udp.c.
1181 */
1182
1183 /*
1184 * Entry points for IP as a device and as a module.
1185 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1186 */
1187 static struct qinit iprinitv4 = {
1188 ip_rput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1189 };
1190
1191 struct qinit iprinitv6 = {
1192 ip_rput_v6, NULL, ip_openv6, ip_close, NULL, &ip_mod_info
1193 };
1194
1195 static struct qinit ipwinit = {
1196 ip_wput_nondata, ip_wsrv, NULL, NULL, NULL, &ip_mod_info
1197 };
1198
1199 static struct qinit iplrinit = {
1200 ip_lrput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1201 };
1202
1203 static struct qinit iplwinit = {
1204 ip_lwput, NULL, NULL, NULL, NULL, &ip_mod_info
1205 };
1206
1207 /* For AF_INET aka /dev/ip */
1208 struct streamtab ipinfov4 = {
1209 &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1210 };
1211
1212 /* For AF_INET6 aka /dev/ip6 */
1213 struct streamtab ipinfov6 = {
1214 &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1215 };
1216
1217 #ifdef DEBUG
1218 boolean_t skip_sctp_cksum = B_FALSE;
1219 #endif
1220
1221 /*
1222 * Generate an ICMP fragmentation needed message.
1223 * When called from ip_output side a minimal ip_recv_attr_t needs to be
1224 * constructed by the caller.
1225 */
1226 void
1227 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1228 {
1229 icmph_t icmph;
1230 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1231
1232 mp = icmp_pkt_err_ok(mp, ira);
1233 if (mp == NULL)
1234 return;
1235
1236 bzero(&icmph, sizeof (icmph_t));
1237 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1238 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1239 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1240 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1241 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1242
1243 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1244 }
1245
1246 /*
1247 * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1248 * If the ICMP message is consumed by IP, i.e., it should not be delivered
1249 * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1250 * Likewise, if the ICMP error is misformed (too short, etc), then it
1251 * returns NULL. The caller uses this to determine whether or not to send
1252 * to raw sockets.
1253 *
1254 * All error messages are passed to the matching transport stream.
1255 *
1256 * The following cases are handled by icmp_inbound:
1257 * 1) It needs to send a reply back and possibly delivering it
1258 * to the "interested" upper clients.
1259 * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1260 * 3) It needs to change some values in IP only.
1261 * 4) It needs to change some values in IP and upper layers e.g TCP
1262 * by delivering an error to the upper layers.
1263 *
1264 * We handle the above three cases in the context of IPsec in the
1265 * following way :
1266 *
1267 * 1) Send the reply back in the same way as the request came in.
1268 * If it came in encrypted, it goes out encrypted. If it came in
1269 * clear, it goes out in clear. Thus, this will prevent chosen
1270 * plain text attack.
1271 * 2) The client may or may not expect things to come in secure.
1272 * If it comes in secure, the policy constraints are checked
1273 * before delivering it to the upper layers. If it comes in
1274 * clear, ipsec_inbound_accept_clear will decide whether to
1275 * accept this in clear or not. In both the cases, if the returned
1276 * message (IP header + 8 bytes) that caused the icmp message has
1277 * AH/ESP headers, it is sent up to AH/ESP for validation before
1278 * sending up. If there are only 8 bytes of returned message, then
1279 * upper client will not be notified.
1280 * 3) Check with global policy to see whether it matches the constaints.
1281 * But this will be done only if icmp_accept_messages_in_clear is
1282 * zero.
1283 * 4) If we need to change both in IP and ULP, then the decision taken
1284 * while affecting the values in IP and while delivering up to TCP
1285 * should be the same.
1286 *
1287 * There are two cases.
1288 *
1289 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1290 * failed), we will not deliver it to the ULP, even though they
1291 * are *willing* to accept in *clear*. This is fine as our global
1292 * disposition to icmp messages asks us reject the datagram.
1293 *
1294 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1295 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1296 * to deliver it to ULP (policy failed), it can lead to
1297 * consistency problems. The cases known at this time are
1298 * ICMP_DESTINATION_UNREACHABLE messages with following code
1299 * values :
1300 *
1301 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1302 * and Upper layer rejects. Then the communication will
1303 * come to a stop. This is solved by making similar decisions
1304 * at both levels. Currently, when we are unable to deliver
1305 * to the Upper Layer (due to policy failures) while IP has
1306 * adjusted dce_pmtu, the next outbound datagram would
1307 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1308 * will be with the right level of protection. Thus the right
1309 * value will be communicated even if we are not able to
1310 * communicate when we get from the wire initially. But this
1311 * assumes there would be at least one outbound datagram after
1312 * IP has adjusted its dce_pmtu value. To make things
1313 * simpler, we accept in clear after the validation of
1314 * AH/ESP headers.
1315 *
1316 * - Other ICMP ERRORS : We may not be able to deliver it to the
1317 * upper layer depending on the level of protection the upper
1318 * layer expects and the disposition in ipsec_inbound_accept_clear().
1319 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1320 * should be accepted in clear when the Upper layer expects secure.
1321 * Thus the communication may get aborted by some bad ICMP
1322 * packets.
1323 */
1324 mblk_t *
1325 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1326 {
1327 icmph_t *icmph;
1328 ipha_t *ipha; /* Outer header */
1329 int ip_hdr_length; /* Outer header length */
1330 boolean_t interested;
1331 ipif_t *ipif;
1332 uint32_t ts;
1333 uint32_t *tsp;
1334 timestruc_t now;
1335 ill_t *ill = ira->ira_ill;
1336 ip_stack_t *ipst = ill->ill_ipst;
1337 zoneid_t zoneid = ira->ira_zoneid;
1338 int len_needed;
1339 mblk_t *mp_ret = NULL;
1340
1341 ipha = (ipha_t *)mp->b_rptr;
1342
1343 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1344
1345 ip_hdr_length = ira->ira_ip_hdr_length;
1346 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1347 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1348 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1349 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1350 freemsg(mp);
1351 return (NULL);
1352 }
1353 /* Last chance to get real. */
1354 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1355 if (ipha == NULL) {
1356 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1357 freemsg(mp);
1358 return (NULL);
1359 }
1360 }
1361
1362 /* The IP header will always be a multiple of four bytes */
1363 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1364 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1365 icmph->icmph_code));
1366
1367 /*
1368 * We will set "interested" to "true" if we should pass a copy to
1369 * the transport or if we handle the packet locally.
1370 */
1371 interested = B_FALSE;
1372 switch (icmph->icmph_type) {
1373 case ICMP_ECHO_REPLY:
1374 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1375 break;
1376 case ICMP_DEST_UNREACHABLE:
1377 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1378 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1379 interested = B_TRUE; /* Pass up to transport */
1380 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1381 break;
1382 case ICMP_SOURCE_QUENCH:
1383 interested = B_TRUE; /* Pass up to transport */
1384 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1385 break;
1386 case ICMP_REDIRECT:
1387 if (!ipst->ips_ip_ignore_redirect)
1388 interested = B_TRUE;
1389 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1390 break;
1391 case ICMP_ECHO_REQUEST:
1392 /*
1393 * Whether to respond to echo requests that come in as IP
1394 * broadcasts or as IP multicast is subject to debate
1395 * (what isn't?). We aim to please, you pick it.
1396 * Default is do it.
1397 */
1398 if (ira->ira_flags & IRAF_MULTICAST) {
1399 /* multicast: respond based on tunable */
1400 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1401 } else if (ira->ira_flags & IRAF_BROADCAST) {
1402 /* broadcast: respond based on tunable */
1403 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1404 } else {
1405 /* unicast: always respond */
1406 interested = B_TRUE;
1407 }
1408 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1409 if (!interested) {
1410 /* We never pass these to RAW sockets */
1411 freemsg(mp);
1412 return (NULL);
1413 }
1414
1415 /* Check db_ref to make sure we can modify the packet. */
1416 if (mp->b_datap->db_ref > 1) {
1417 mblk_t *mp1;
1418
1419 mp1 = copymsg(mp);
1420 freemsg(mp);
1421 if (!mp1) {
1422 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1423 return (NULL);
1424 }
1425 mp = mp1;
1426 ipha = (ipha_t *)mp->b_rptr;
1427 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1428 }
1429 icmph->icmph_type = ICMP_ECHO_REPLY;
1430 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1431 icmp_send_reply_v4(mp, ipha, icmph, ira);
1432 return (NULL);
1433
1434 case ICMP_ROUTER_ADVERTISEMENT:
1435 case ICMP_ROUTER_SOLICITATION:
1436 break;
1437 case ICMP_TIME_EXCEEDED:
1438 interested = B_TRUE; /* Pass up to transport */
1439 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1440 break;
1441 case ICMP_PARAM_PROBLEM:
1442 interested = B_TRUE; /* Pass up to transport */
1443 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1444 break;
1445 case ICMP_TIME_STAMP_REQUEST:
1446 /* Response to Time Stamp Requests is local policy. */
1447 if (ipst->ips_ip_g_resp_to_timestamp) {
1448 if (ira->ira_flags & IRAF_MULTIBROADCAST)
1449 interested =
1450 ipst->ips_ip_g_resp_to_timestamp_bcast;
1451 else
1452 interested = B_TRUE;
1453 }
1454 if (!interested) {
1455 /* We never pass these to RAW sockets */
1456 freemsg(mp);
1457 return (NULL);
1458 }
1459
1460 /* Make sure we have enough of the packet */
1461 len_needed = ip_hdr_length + ICMPH_SIZE +
1462 3 * sizeof (uint32_t);
1463
1464 if (mp->b_wptr - mp->b_rptr < len_needed) {
1465 ipha = ip_pullup(mp, len_needed, ira);
1466 if (ipha == NULL) {
1467 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1468 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1469 mp, ill);
1470 freemsg(mp);
1471 return (NULL);
1472 }
1473 /* Refresh following the pullup. */
1474 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1475 }
1476 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1477 /* Check db_ref to make sure we can modify the packet. */
1478 if (mp->b_datap->db_ref > 1) {
1479 mblk_t *mp1;
1480
1481 mp1 = copymsg(mp);
1482 freemsg(mp);
1483 if (!mp1) {
1484 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1485 return (NULL);
1486 }
1487 mp = mp1;
1488 ipha = (ipha_t *)mp->b_rptr;
1489 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1490 }
1491 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1492 tsp = (uint32_t *)&icmph[1];
1493 tsp++; /* Skip past 'originate time' */
1494 /* Compute # of milliseconds since midnight */
1495 gethrestime(&now);
1496 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1497 NSEC2MSEC(now.tv_nsec);
1498 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1499 *tsp++ = htonl(ts); /* Lay in 'send time' */
1500 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1501 icmp_send_reply_v4(mp, ipha, icmph, ira);
1502 return (NULL);
1503
1504 case ICMP_TIME_STAMP_REPLY:
1505 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1506 break;
1507 case ICMP_INFO_REQUEST:
1508 /* Per RFC 1122 3.2.2.7, ignore this. */
1509 case ICMP_INFO_REPLY:
1510 break;
1511 case ICMP_ADDRESS_MASK_REQUEST:
1512 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1513 interested =
1514 ipst->ips_ip_respond_to_address_mask_broadcast;
1515 } else {
1516 interested = B_TRUE;
1517 }
1518 if (!interested) {
1519 /* We never pass these to RAW sockets */
1520 freemsg(mp);
1521 return (NULL);
1522 }
1523 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1524 if (mp->b_wptr - mp->b_rptr < len_needed) {
1525 ipha = ip_pullup(mp, len_needed, ira);
1526 if (ipha == NULL) {
1527 BUMP_MIB(ill->ill_ip_mib,
1528 ipIfStatsInTruncatedPkts);
1529 ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1530 ill);
1531 freemsg(mp);
1532 return (NULL);
1533 }
1534 /* Refresh following the pullup. */
1535 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1536 }
1537 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1538 /* Check db_ref to make sure we can modify the packet. */
1539 if (mp->b_datap->db_ref > 1) {
1540 mblk_t *mp1;
1541
1542 mp1 = copymsg(mp);
1543 freemsg(mp);
1544 if (!mp1) {
1545 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1546 return (NULL);
1547 }
1548 mp = mp1;
1549 ipha = (ipha_t *)mp->b_rptr;
1550 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1551 }
1552 /*
1553 * Need the ipif with the mask be the same as the source
1554 * address of the mask reply. For unicast we have a specific
1555 * ipif. For multicast/broadcast we only handle onlink
1556 * senders, and use the source address to pick an ipif.
1557 */
1558 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1559 if (ipif == NULL) {
1560 /* Broadcast or multicast */
1561 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1562 if (ipif == NULL) {
1563 freemsg(mp);
1564 return (NULL);
1565 }
1566 }
1567 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1568 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1569 ipif_refrele(ipif);
1570 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1571 icmp_send_reply_v4(mp, ipha, icmph, ira);
1572 return (NULL);
1573
1574 case ICMP_ADDRESS_MASK_REPLY:
1575 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1576 break;
1577 default:
1578 interested = B_TRUE; /* Pass up to transport */
1579 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1580 break;
1581 }
1582 /*
1583 * See if there is an ICMP client to avoid an extra copymsg/freemsg
1584 * if there isn't one.
1585 */
1586 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1587 /* If there is an ICMP client and we want one too, copy it. */
1588
1589 if (!interested) {
1590 /* Caller will deliver to RAW sockets */
1591 return (mp);
1592 }
1593 mp_ret = copymsg(mp);
1594 if (mp_ret == NULL) {
1595 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1596 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1597 }
1598 } else if (!interested) {
1599 /* Neither we nor raw sockets are interested. Drop packet now */
1600 freemsg(mp);
1601 return (NULL);
1602 }
1603
1604 /*
1605 * ICMP error or redirect packet. Make sure we have enough of
1606 * the header and that db_ref == 1 since we might end up modifying
1607 * the packet.
1608 */
1609 if (mp->b_cont != NULL) {
1610 if (ip_pullup(mp, -1, ira) == NULL) {
1611 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1612 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1613 mp, ill);
1614 freemsg(mp);
1615 return (mp_ret);
1616 }
1617 }
1618
1619 if (mp->b_datap->db_ref > 1) {
1620 mblk_t *mp1;
1621
1622 mp1 = copymsg(mp);
1623 if (mp1 == NULL) {
1624 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1625 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1626 freemsg(mp);
1627 return (mp_ret);
1628 }
1629 freemsg(mp);
1630 mp = mp1;
1631 }
1632
1633 /*
1634 * In case mp has changed, verify the message before any further
1635 * processes.
1636 */
1637 ipha = (ipha_t *)mp->b_rptr;
1638 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1639 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1640 freemsg(mp);
1641 return (mp_ret);
1642 }
1643
1644 switch (icmph->icmph_type) {
1645 case ICMP_REDIRECT:
1646 icmp_redirect_v4(mp, ipha, icmph, ira);
1647 break;
1648 case ICMP_DEST_UNREACHABLE:
1649 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1650 /* Update DCE and adjust MTU is icmp header if needed */
1651 icmp_inbound_too_big_v4(icmph, ira);
1652 }
1653 /* FALLTHROUGH */
1654 default:
1655 icmp_inbound_error_fanout_v4(mp, icmph, ira);
1656 break;
1657 }
1658 return (mp_ret);
1659 }
1660
1661 /*
1662 * Send an ICMP echo, timestamp or address mask reply.
1663 * The caller has already updated the payload part of the packet.
1664 * We handle the ICMP checksum, IP source address selection and feed
1665 * the packet into ip_output_simple.
1666 */
1667 static void
1668 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1669 ip_recv_attr_t *ira)
1670 {
1671 uint_t ip_hdr_length = ira->ira_ip_hdr_length;
1672 ill_t *ill = ira->ira_ill;
1673 ip_stack_t *ipst = ill->ill_ipst;
1674 ip_xmit_attr_t ixas;
1675
1676 /* Send out an ICMP packet */
1677 icmph->icmph_checksum = 0;
1678 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1679 /* Reset time to live. */
1680 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1681 {
1682 /* Swap source and destination addresses */
1683 ipaddr_t tmp;
1684
1685 tmp = ipha->ipha_src;
1686 ipha->ipha_src = ipha->ipha_dst;
1687 ipha->ipha_dst = tmp;
1688 }
1689 ipha->ipha_ident = 0;
1690 if (!IS_SIMPLE_IPH(ipha))
1691 icmp_options_update(ipha);
1692
1693 bzero(&ixas, sizeof (ixas));
1694 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1695 ixas.ixa_zoneid = ira->ira_zoneid;
1696 ixas.ixa_cred = kcred;
1697 ixas.ixa_cpid = NOPID;
1698 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
1699 ixas.ixa_ifindex = 0;
1700 ixas.ixa_ipst = ipst;
1701 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1702
1703 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1704 /*
1705 * This packet should go out the same way as it
1706 * came in i.e in clear, independent of the IPsec policy
1707 * for transmitting packets.
1708 */
1709 ixas.ixa_flags |= IXAF_NO_IPSEC;
1710 } else {
1711 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1712 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1713 /* Note: mp already consumed and ip_drop_packet done */
1714 return;
1715 }
1716 }
1717 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1718 /*
1719 * Not one or our addresses (IRE_LOCALs), thus we let
1720 * ip_output_simple pick the source.
1721 */
1722 ipha->ipha_src = INADDR_ANY;
1723 ixas.ixa_flags |= IXAF_SET_SOURCE;
1724 }
1725 /* Should we send with DF and use dce_pmtu? */
1726 if (ipst->ips_ipv4_icmp_return_pmtu) {
1727 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1728 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1729 }
1730
1731 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1732
1733 (void) ip_output_simple(mp, &ixas);
1734 ixa_cleanup(&ixas);
1735 }
1736
1737 /*
1738 * Verify the ICMP messages for either for ICMP error or redirect packet.
1739 * The caller should have fully pulled up the message. If it's a redirect
1740 * packet, only basic checks on IP header will be done; otherwise, verify
1741 * the packet by looking at the included ULP header.
1742 *
1743 * Called before icmp_inbound_error_fanout_v4 is called.
1744 */
1745 static boolean_t
1746 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1747 {
1748 ill_t *ill = ira->ira_ill;
1749 int hdr_length;
1750 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1751 conn_t *connp;
1752 ipha_t *ipha; /* Inner IP header */
1753
1754 ipha = (ipha_t *)&icmph[1];
1755 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1756 goto truncated;
1757
1758 hdr_length = IPH_HDR_LENGTH(ipha);
1759
1760 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1761 goto discard_pkt;
1762
1763 if (hdr_length < sizeof (ipha_t))
1764 goto truncated;
1765
1766 if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1767 goto truncated;
1768
1769 /*
1770 * Stop here for ICMP_REDIRECT.
1771 */
1772 if (icmph->icmph_type == ICMP_REDIRECT)
1773 return (B_TRUE);
1774
1775 /*
1776 * ICMP errors only.
1777 */
1778 switch (ipha->ipha_protocol) {
1779 case IPPROTO_UDP:
1780 /*
1781 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1782 * transport header.
1783 */
1784 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1785 mp->b_wptr)
1786 goto truncated;
1787 break;
1788 case IPPROTO_TCP: {
1789 tcpha_t *tcpha;
1790
1791 /*
1792 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1793 * transport header.
1794 */
1795 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1796 mp->b_wptr)
1797 goto truncated;
1798
1799 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1800 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1801 ipst);
1802 if (connp == NULL)
1803 goto discard_pkt;
1804
1805 if ((connp->conn_verifyicmp != NULL) &&
1806 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1807 CONN_DEC_REF(connp);
1808 goto discard_pkt;
1809 }
1810 CONN_DEC_REF(connp);
1811 break;
1812 }
1813 case IPPROTO_SCTP:
1814 /*
1815 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1816 * transport header.
1817 */
1818 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1819 mp->b_wptr)
1820 goto truncated;
1821 break;
1822 case IPPROTO_ESP:
1823 case IPPROTO_AH:
1824 break;
1825 case IPPROTO_ENCAP:
1826 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1827 mp->b_wptr)
1828 goto truncated;
1829 break;
1830 default:
1831 break;
1832 }
1833
1834 return (B_TRUE);
1835
1836 discard_pkt:
1837 /* Bogus ICMP error. */
1838 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1839 return (B_FALSE);
1840
1841 truncated:
1842 /* We pulled up everthing already. Must be truncated */
1843 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1844 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1845 return (B_FALSE);
1846 }
1847
1848 /* Table from RFC 1191 */
1849 static int icmp_frag_size_table[] =
1850 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1851
1852 /*
1853 * Process received ICMP Packet too big.
1854 * Just handles the DCE create/update, including using the above table of
1855 * PMTU guesses. The caller is responsible for validating the packet before
1856 * passing it in and also to fanout the ICMP error to any matching transport
1857 * conns. Assumes the message has been fully pulled up and verified.
1858 *
1859 * Before getting here, the caller has called icmp_inbound_verify_v4()
1860 * that should have verified with ULP to prevent undoing the changes we're
1861 * going to make to DCE. For example, TCP might have verified that the packet
1862 * which generated error is in the send window.
1863 *
1864 * In some cases modified this MTU in the ICMP header packet; the caller
1865 * should pass to the matching ULP after this returns.
1866 */
1867 static void
1868 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1869 {
1870 dce_t *dce;
1871 int old_mtu;
1872 int mtu, orig_mtu;
1873 ipaddr_t dst;
1874 boolean_t disable_pmtud;
1875 ill_t *ill = ira->ira_ill;
1876 ip_stack_t *ipst = ill->ill_ipst;
1877 uint_t hdr_length;
1878 ipha_t *ipha;
1879
1880 /* Caller already pulled up everything. */
1881 ipha = (ipha_t *)&icmph[1];
1882 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1883 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1884 ASSERT(ill != NULL);
1885
1886 hdr_length = IPH_HDR_LENGTH(ipha);
1887
1888 /*
1889 * We handle path MTU for source routed packets since the DCE
1890 * is looked up using the final destination.
1891 */
1892 dst = ip_get_dst(ipha);
1893
1894 dce = dce_lookup_and_add_v4(dst, ipst);
1895 if (dce == NULL) {
1896 /* Couldn't add a unique one - ENOMEM */
1897 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1898 ntohl(dst)));
1899 return;
1900 }
1901
1902 /* Check for MTU discovery advice as described in RFC 1191 */
1903 mtu = ntohs(icmph->icmph_du_mtu);
1904 orig_mtu = mtu;
1905 disable_pmtud = B_FALSE;
1906
1907 mutex_enter(&dce->dce_lock);
1908 if (dce->dce_flags & DCEF_PMTU)
1909 old_mtu = dce->dce_pmtu;
1910 else
1911 old_mtu = ill->ill_mtu;
1912
1913 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1914 uint32_t length;
1915 int i;
1916
1917 /*
1918 * Use the table from RFC 1191 to figure out
1919 * the next "plateau" based on the length in
1920 * the original IP packet.
1921 */
1922 length = ntohs(ipha->ipha_length);
1923 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1924 uint32_t, length);
1925 if (old_mtu <= length &&
1926 old_mtu >= length - hdr_length) {
1927 /*
1928 * Handle broken BSD 4.2 systems that
1929 * return the wrong ipha_length in ICMP
1930 * errors.
1931 */
1932 ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1933 length, old_mtu));
1934 length -= hdr_length;
1935 }
1936 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1937 if (length > icmp_frag_size_table[i])
1938 break;
1939 }
1940 if (i == A_CNT(icmp_frag_size_table)) {
1941 /* Smaller than IP_MIN_MTU! */
1942 ip1dbg(("Too big for packet size %d\n",
1943 length));
1944 disable_pmtud = B_TRUE;
1945 mtu = ipst->ips_ip_pmtu_min;
1946 } else {
1947 mtu = icmp_frag_size_table[i];
1948 ip1dbg(("Calculated mtu %d, packet size %d, "
1949 "before %d\n", mtu, length, old_mtu));
1950 if (mtu < ipst->ips_ip_pmtu_min) {
1951 mtu = ipst->ips_ip_pmtu_min;
1952 disable_pmtud = B_TRUE;
1953 }
1954 }
1955 }
1956 if (disable_pmtud)
1957 dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1958 else
1959 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1960
1961 dce->dce_pmtu = MIN(old_mtu, mtu);
1962 /* Prepare to send the new max frag size for the ULP. */
1963 icmph->icmph_du_zero = 0;
1964 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu);
1965 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1966 dce, int, orig_mtu, int, mtu);
1967
1968 /* We now have a PMTU for sure */
1969 dce->dce_flags |= DCEF_PMTU;
1970 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1971 mutex_exit(&dce->dce_lock);
1972 /*
1973 * After dropping the lock the new value is visible to everyone.
1974 * Then we bump the generation number so any cached values reinspect
1975 * the dce_t.
1976 */
1977 dce_increment_generation(dce);
1978 dce_refrele(dce);
1979 }
1980
1981 /*
1982 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1983 * calls this function.
1984 */
1985 static mblk_t *
1986 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1987 {
1988 int length;
1989
1990 ASSERT(mp->b_datap->db_type == M_DATA);
1991
1992 /* icmp_inbound_v4 has already pulled up the whole error packet */
1993 ASSERT(mp->b_cont == NULL);
1994
1995 /*
1996 * The length that we want to overlay is the inner header
1997 * and what follows it.
1998 */
1999 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
2000
2001 /*
2002 * Overlay the inner header and whatever follows it over the
2003 * outer header.
2004 */
2005 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2006
2007 /* Adjust for what we removed */
2008 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
2009 return (mp);
2010 }
2011
2012 /*
2013 * Try to pass the ICMP message upstream in case the ULP cares.
2014 *
2015 * If the packet that caused the ICMP error is secure, we send
2016 * it to AH/ESP to make sure that the attached packet has a
2017 * valid association. ipha in the code below points to the
2018 * IP header of the packet that caused the error.
2019 *
2020 * For IPsec cases, we let the next-layer-up (which has access to
2021 * cached policy on the conn_t, or can query the SPD directly)
2022 * subtract out any IPsec overhead if they must. We therefore make no
2023 * adjustments here for IPsec overhead.
2024 *
2025 * IFN could have been generated locally or by some router.
2026 *
2027 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
2028 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
2029 * This happens because IP adjusted its value of MTU on an
2030 * earlier IFN message and could not tell the upper layer,
2031 * the new adjusted value of MTU e.g. Packet was encrypted
2032 * or there was not enough information to fanout to upper
2033 * layers. Thus on the next outbound datagram, ire_send_wire
2034 * generates the IFN, where IPsec processing has *not* been
2035 * done.
2036 *
2037 * Note that we retain ixa_fragsize across IPsec thus once
2038 * we have picking ixa_fragsize and entered ipsec_out_process we do
2039 * no change the fragsize even if the path MTU changes before
2040 * we reach ip_output_post_ipsec.
2041 *
2042 * In the local case, IRAF_LOOPBACK will be set indicating
2043 * that IFN was generated locally.
2044 *
2045 * ROUTER : IFN could be secure or non-secure.
2046 *
2047 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2048 * packet in error has AH/ESP headers to validate the AH/ESP
2049 * headers. AH/ESP will verify whether there is a valid SA or
2050 * not and send it back. We will fanout again if we have more
2051 * data in the packet.
2052 *
2053 * If the packet in error does not have AH/ESP, we handle it
2054 * like any other case.
2055 *
2056 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it
2057 * up to AH/ESP for validation. AH/ESP will verify whether there is a
2058 * valid SA or not and send it back. We will fanout again if
2059 * we have more data in the packet.
2060 *
2061 * If the packet in error does not have AH/ESP, we handle it
2062 * like any other case.
2063 *
2064 * The caller must have called icmp_inbound_verify_v4.
2065 */
2066 static void
2067 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2068 {
2069 uint16_t *up; /* Pointer to ports in ULP header */
2070 uint32_t ports; /* reversed ports for fanout */
2071 ipha_t ripha; /* With reversed addresses */
2072 ipha_t *ipha; /* Inner IP header */
2073 uint_t hdr_length; /* Inner IP header length */
2074 tcpha_t *tcpha;
2075 conn_t *connp;
2076 ill_t *ill = ira->ira_ill;
2077 ip_stack_t *ipst = ill->ill_ipst;
2078 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2079 ill_t *rill = ira->ira_rill;
2080
2081 /* Caller already pulled up everything. */
2082 ipha = (ipha_t *)&icmph[1];
2083 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2084 ASSERT(mp->b_cont == NULL);
2085
2086 hdr_length = IPH_HDR_LENGTH(ipha);
2087 ira->ira_protocol = ipha->ipha_protocol;
2088
2089 /*
2090 * We need a separate IP header with the source and destination
2091 * addresses reversed to do fanout/classification because the ipha in
2092 * the ICMP error is in the form we sent it out.
2093 */
2094 ripha.ipha_src = ipha->ipha_dst;
2095 ripha.ipha_dst = ipha->ipha_src;
2096 ripha.ipha_protocol = ipha->ipha_protocol;
2097 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2098
2099 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2100 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2101 ntohl(ipha->ipha_dst),
2102 icmph->icmph_type, icmph->icmph_code));
2103
2104 switch (ipha->ipha_protocol) {
2105 case IPPROTO_UDP:
2106 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2107
2108 /* Attempt to find a client stream based on port. */
2109 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2110 ntohs(up[0]), ntohs(up[1])));
2111
2112 /* Note that we send error to all matches. */
2113 ira->ira_flags |= IRAF_ICMP_ERROR;
2114 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2115 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2116 return;
2117
2118 case IPPROTO_TCP:
2119 /*
2120 * Find a TCP client stream for this packet.
2121 * Note that we do a reverse lookup since the header is
2122 * in the form we sent it out.
2123 */
2124 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2125 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2126 ipst);
2127 if (connp == NULL)
2128 goto discard_pkt;
2129
2130 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2131 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2132 mp = ipsec_check_inbound_policy(mp, connp,
2133 ipha, NULL, ira);
2134 if (mp == NULL) {
2135 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2136 /* Note that mp is NULL */
2137 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2138 CONN_DEC_REF(connp);
2139 return;
2140 }
2141 }
2142
2143 ira->ira_flags |= IRAF_ICMP_ERROR;
2144 ira->ira_ill = ira->ira_rill = NULL;
2145 if (IPCL_IS_TCP(connp)) {
2146 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2147 connp->conn_recvicmp, connp, ira, SQ_FILL,
2148 SQTAG_TCP_INPUT_ICMP_ERR);
2149 } else {
2150 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2151 (connp->conn_recv)(connp, mp, NULL, ira);
2152 CONN_DEC_REF(connp);
2153 }
2154 ira->ira_ill = ill;
2155 ira->ira_rill = rill;
2156 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2157 return;
2158
2159 case IPPROTO_SCTP:
2160 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2161 /* Find a SCTP client stream for this packet. */
2162 ((uint16_t *)&ports)[0] = up[1];
2163 ((uint16_t *)&ports)[1] = up[0];
2164
2165 ira->ira_flags |= IRAF_ICMP_ERROR;
2166 ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2167 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2168 return;
2169
2170 case IPPROTO_ESP:
2171 case IPPROTO_AH:
2172 if (!ipsec_loaded(ipss)) {
2173 ip_proto_not_sup(mp, ira);
2174 return;
2175 }
2176
2177 if (ipha->ipha_protocol == IPPROTO_ESP)
2178 mp = ipsecesp_icmp_error(mp, ira);
2179 else
2180 mp = ipsecah_icmp_error(mp, ira);
2181 if (mp == NULL)
2182 return;
2183
2184 /* Just in case ipsec didn't preserve the NULL b_cont */
2185 if (mp->b_cont != NULL) {
2186 if (!pullupmsg(mp, -1))
2187 goto discard_pkt;
2188 }
2189
2190 /*
2191 * Note that ira_pktlen and ira_ip_hdr_length are no longer
2192 * correct, but we don't use them any more here.
2193 *
2194 * If succesful, the mp has been modified to not include
2195 * the ESP/AH header so we can fanout to the ULP's icmp
2196 * error handler.
2197 */
2198 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2199 goto truncated;
2200
2201 /* Verify the modified message before any further processes. */
2202 ipha = (ipha_t *)mp->b_rptr;
2203 hdr_length = IPH_HDR_LENGTH(ipha);
2204 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2205 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2206 freemsg(mp);
2207 return;
2208 }
2209
2210 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2211 return;
2212
2213 case IPPROTO_ENCAP: {
2214 /* Look for self-encapsulated packets that caused an error */
2215 ipha_t *in_ipha;
2216
2217 /*
2218 * Caller has verified that length has to be
2219 * at least the size of IP header.
2220 */
2221 ASSERT(hdr_length >= sizeof (ipha_t));
2222 /*
2223 * Check the sanity of the inner IP header like
2224 * we did for the outer header.
2225 */
2226 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2227 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2228 goto discard_pkt;
2229 }
2230 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2231 goto discard_pkt;
2232 }
2233 /* Check for Self-encapsulated tunnels */
2234 if (in_ipha->ipha_src == ipha->ipha_src &&
2235 in_ipha->ipha_dst == ipha->ipha_dst) {
2236
2237 mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2238 in_ipha);
2239 if (mp == NULL)
2240 goto discard_pkt;
2241
2242 /*
2243 * Just in case self_encap didn't preserve the NULL
2244 * b_cont
2245 */
2246 if (mp->b_cont != NULL) {
2247 if (!pullupmsg(mp, -1))
2248 goto discard_pkt;
2249 }
2250 /*
2251 * Note that ira_pktlen and ira_ip_hdr_length are no
2252 * longer correct, but we don't use them any more here.
2253 */
2254 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2255 goto truncated;
2256
2257 /*
2258 * Verify the modified message before any further
2259 * processes.
2260 */
2261 ipha = (ipha_t *)mp->b_rptr;
2262 hdr_length = IPH_HDR_LENGTH(ipha);
2263 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2264 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2265 freemsg(mp);
2266 return;
2267 }
2268
2269 /*
2270 * The packet in error is self-encapsualted.
2271 * And we are finding it further encapsulated
2272 * which we could not have possibly generated.
2273 */
2274 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2275 goto discard_pkt;
2276 }
2277 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2278 return;
2279 }
2280 /* No self-encapsulated */
2281 }
2282 /* FALLTHROUGH */
2283 case IPPROTO_IPV6:
2284 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2285 &ripha.ipha_dst, ipst)) != NULL) {
2286 ira->ira_flags |= IRAF_ICMP_ERROR;
2287 connp->conn_recvicmp(connp, mp, NULL, ira);
2288 CONN_DEC_REF(connp);
2289 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2290 return;
2291 }
2292 /*
2293 * No IP tunnel is interested, fallthrough and see
2294 * if a raw socket will want it.
2295 */
2296 /* FALLTHROUGH */
2297 default:
2298 ira->ira_flags |= IRAF_ICMP_ERROR;
2299 ip_fanout_proto_v4(mp, &ripha, ira);
2300 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2301 return;
2302 }
2303 /* NOTREACHED */
2304 discard_pkt:
2305 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2306 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2307 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2308 freemsg(mp);
2309 return;
2310
2311 truncated:
2312 /* We pulled up everthing already. Must be truncated */
2313 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2314 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2315 freemsg(mp);
2316 }
2317
2318 /*
2319 * Common IP options parser.
2320 *
2321 * Setup routine: fill in *optp with options-parsing state, then
2322 * tail-call ipoptp_next to return the first option.
2323 */
2324 uint8_t
2325 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2326 {
2327 uint32_t totallen; /* total length of all options */
2328
2329 totallen = ipha->ipha_version_and_hdr_length -
2330 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2331 totallen <<= 2;
2332 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2333 optp->ipoptp_end = optp->ipoptp_next + totallen;
2334 optp->ipoptp_flags = 0;
2335 return (ipoptp_next(optp));
2336 }
2337
2338 /* Like above but without an ipha_t */
2339 uint8_t
2340 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2341 {
2342 optp->ipoptp_next = opt;
2343 optp->ipoptp_end = optp->ipoptp_next + totallen;
2344 optp->ipoptp_flags = 0;
2345 return (ipoptp_next(optp));
2346 }
2347
2348 /*
2349 * Common IP options parser: extract next option.
2350 */
2351 uint8_t
2352 ipoptp_next(ipoptp_t *optp)
2353 {
2354 uint8_t *end = optp->ipoptp_end;
2355 uint8_t *cur = optp->ipoptp_next;
2356 uint8_t opt, len, pointer;
2357
2358 /*
2359 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2360 * has been corrupted.
2361 */
2362 ASSERT(cur <= end);
2363
2364 if (cur == end)
2365 return (IPOPT_EOL);
2366
2367 opt = cur[IPOPT_OPTVAL];
2368
2369 /*
2370 * Skip any NOP options.
2371 */
2372 while (opt == IPOPT_NOP) {
2373 cur++;
2374 if (cur == end)
2375 return (IPOPT_EOL);
2376 opt = cur[IPOPT_OPTVAL];
2377 }
2378
2379 if (opt == IPOPT_EOL)
2380 return (IPOPT_EOL);
2381
2382 /*
2383 * Option requiring a length.
2384 */
2385 if ((cur + 1) >= end) {
2386 optp->ipoptp_flags |= IPOPTP_ERROR;
2387 return (IPOPT_EOL);
2388 }
2389 len = cur[IPOPT_OLEN];
2390 if (len < 2) {
2391 optp->ipoptp_flags |= IPOPTP_ERROR;
2392 return (IPOPT_EOL);
2393 }
2394 optp->ipoptp_cur = cur;
2395 optp->ipoptp_len = len;
2396 optp->ipoptp_next = cur + len;
2397 if (cur + len > end) {
2398 optp->ipoptp_flags |= IPOPTP_ERROR;
2399 return (IPOPT_EOL);
2400 }
2401
2402 /*
2403 * For the options which require a pointer field, make sure
2404 * its there, and make sure it points to either something
2405 * inside this option, or the end of the option.
2406 */
2407 switch (opt) {
2408 case IPOPT_RR:
2409 case IPOPT_TS:
2410 case IPOPT_LSRR:
2411 case IPOPT_SSRR:
2412 if (len <= IPOPT_OFFSET) {
2413 optp->ipoptp_flags |= IPOPTP_ERROR;
2414 return (opt);
2415 }
2416 pointer = cur[IPOPT_OFFSET];
2417 if (pointer - 1 > len) {
2418 optp->ipoptp_flags |= IPOPTP_ERROR;
2419 return (opt);
2420 }
2421 break;
2422 }
2423
2424 /*
2425 * Sanity check the pointer field based on the type of the
2426 * option.
2427 */
2428 switch (opt) {
2429 case IPOPT_RR:
2430 case IPOPT_SSRR:
2431 case IPOPT_LSRR:
2432 if (pointer < IPOPT_MINOFF_SR)
2433 optp->ipoptp_flags |= IPOPTP_ERROR;
2434 break;
2435 case IPOPT_TS:
2436 if (pointer < IPOPT_MINOFF_IT)
2437 optp->ipoptp_flags |= IPOPTP_ERROR;
2438 /*
2439 * Note that the Internet Timestamp option also
2440 * contains two four bit fields (the Overflow field,
2441 * and the Flag field), which follow the pointer
2442 * field. We don't need to check that these fields
2443 * fall within the length of the option because this
2444 * was implicitely done above. We've checked that the
2445 * pointer value is at least IPOPT_MINOFF_IT, and that
2446 * it falls within the option. Since IPOPT_MINOFF_IT >
2447 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2448 */
2449 ASSERT(len > IPOPT_POS_OV_FLG);
2450 break;
2451 }
2452
2453 return (opt);
2454 }
2455
2456 /*
2457 * Use the outgoing IP header to create an IP_OPTIONS option the way
2458 * it was passed down from the application.
2459 *
2460 * This is compatible with BSD in that it returns
2461 * the reverse source route with the final destination
2462 * as the last entry. The first 4 bytes of the option
2463 * will contain the final destination.
2464 */
2465 int
2466 ip_opt_get_user(conn_t *connp, uchar_t *buf)
2467 {
2468 ipoptp_t opts;
2469 uchar_t *opt;
2470 uint8_t optval;
2471 uint8_t optlen;
2472 uint32_t len = 0;
2473 uchar_t *buf1 = buf;
2474 uint32_t totallen;
2475 ipaddr_t dst;
2476 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
2477
2478 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2479 return (0);
2480
2481 totallen = ipp->ipp_ipv4_options_len;
2482 if (totallen & 0x3)
2483 return (0);
2484
2485 buf += IP_ADDR_LEN; /* Leave room for final destination */
2486 len += IP_ADDR_LEN;
2487 bzero(buf1, IP_ADDR_LEN);
2488
2489 dst = connp->conn_faddr_v4;
2490
2491 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2492 optval != IPOPT_EOL;
2493 optval = ipoptp_next(&opts)) {
2494 int off;
2495
2496 opt = opts.ipoptp_cur;
2497 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2498 break;
2499 }
2500 optlen = opts.ipoptp_len;
2501
2502 switch (optval) {
2503 case IPOPT_SSRR:
2504 case IPOPT_LSRR:
2505
2506 /*
2507 * Insert destination as the first entry in the source
2508 * route and move down the entries on step.
2509 * The last entry gets placed at buf1.
2510 */
2511 buf[IPOPT_OPTVAL] = optval;
2512 buf[IPOPT_OLEN] = optlen;
2513 buf[IPOPT_OFFSET] = optlen;
2514
2515 off = optlen - IP_ADDR_LEN;
2516 if (off < 0) {
2517 /* No entries in source route */
2518 break;
2519 }
2520 /* Last entry in source route if not already set */
2521 if (dst == INADDR_ANY)
2522 bcopy(opt + off, buf1, IP_ADDR_LEN);
2523 off -= IP_ADDR_LEN;
2524
2525 while (off > 0) {
2526 bcopy(opt + off,
2527 buf + off + IP_ADDR_LEN,
2528 IP_ADDR_LEN);
2529 off -= IP_ADDR_LEN;
2530 }
2531 /* ipha_dst into first slot */
2532 bcopy(&dst, buf + off + IP_ADDR_LEN,
2533 IP_ADDR_LEN);
2534 buf += optlen;
2535 len += optlen;
2536 break;
2537
2538 default:
2539 bcopy(opt, buf, optlen);
2540 buf += optlen;
2541 len += optlen;
2542 break;
2543 }
2544 }
2545 done:
2546 /* Pad the resulting options */
2547 while (len & 0x3) {
2548 *buf++ = IPOPT_EOL;
2549 len++;
2550 }
2551 return (len);
2552 }
2553
2554 /*
2555 * Update any record route or timestamp options to include this host.
2556 * Reverse any source route option.
2557 * This routine assumes that the options are well formed i.e. that they
2558 * have already been checked.
2559 */
2560 static void
2561 icmp_options_update(ipha_t *ipha)
2562 {
2563 ipoptp_t opts;
2564 uchar_t *opt;
2565 uint8_t optval;
2566 ipaddr_t src; /* Our local address */
2567 ipaddr_t dst;
2568
2569 ip2dbg(("icmp_options_update\n"));
2570 src = ipha->ipha_src;
2571 dst = ipha->ipha_dst;
2572
2573 for (optval = ipoptp_first(&opts, ipha);
2574 optval != IPOPT_EOL;
2575 optval = ipoptp_next(&opts)) {
2576 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2577 opt = opts.ipoptp_cur;
2578 ip2dbg(("icmp_options_update: opt %d, len %d\n",
2579 optval, opts.ipoptp_len));
2580 switch (optval) {
2581 int off1, off2;
2582 case IPOPT_SSRR:
2583 case IPOPT_LSRR:
2584 /*
2585 * Reverse the source route. The first entry
2586 * should be the next to last one in the current
2587 * source route (the last entry is our address).
2588 * The last entry should be the final destination.
2589 */
2590 off1 = IPOPT_MINOFF_SR - 1;
2591 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2592 if (off2 < 0) {
2593 /* No entries in source route */
2594 ip1dbg((
2595 "icmp_options_update: bad src route\n"));
2596 break;
2597 }
2598 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2599 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2600 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2601 off2 -= IP_ADDR_LEN;
2602
2603 while (off1 < off2) {
2604 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2605 bcopy((char *)opt + off2, (char *)opt + off1,
2606 IP_ADDR_LEN);
2607 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2608 off1 += IP_ADDR_LEN;
2609 off2 -= IP_ADDR_LEN;
2610 }
2611 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2612 break;
2613 }
2614 }
2615 }
2616
2617 /*
2618 * Process received ICMP Redirect messages.
2619 * Assumes the caller has verified that the headers are in the pulled up mblk.
2620 * Consumes mp.
2621 */
2622 static void
2623 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2624 {
2625 ire_t *ire, *nire;
2626 ire_t *prev_ire;
2627 ipaddr_t src, dst, gateway;
2628 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2629 ipha_t *inner_ipha; /* Inner IP header */
2630
2631 /* Caller already pulled up everything. */
2632 inner_ipha = (ipha_t *)&icmph[1];
2633 src = ipha->ipha_src;
2634 dst = inner_ipha->ipha_dst;
2635 gateway = icmph->icmph_rd_gateway;
2636 /* Make sure the new gateway is reachable somehow. */
2637 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2638 ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
2639 /*
2640 * Make sure we had a route for the dest in question and that
2641 * that route was pointing to the old gateway (the source of the
2642 * redirect packet.)
2643 * We do longest match and then compare ire_gateway_addr below.
2644 */
2645 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2646 NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
2647 /*
2648 * Check that
2649 * the redirect was not from ourselves
2650 * the new gateway and the old gateway are directly reachable
2651 */
2652 if (prev_ire == NULL || ire == NULL ||
2653 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2654 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2655 !(ire->ire_type & IRE_IF_ALL) ||
2656 prev_ire->ire_gateway_addr != src) {
2657 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2658 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2659 freemsg(mp);
2660 if (ire != NULL)
2661 ire_refrele(ire);
2662 if (prev_ire != NULL)
2663 ire_refrele(prev_ire);
2664 return;
2665 }
2666
2667 ire_refrele(prev_ire);
2668 ire_refrele(ire);
2669
2670 /*
2671 * TODO: more precise handling for cases 0, 2, 3, the latter two
2672 * require TOS routing
2673 */
2674 switch (icmph->icmph_code) {
2675 case 0:
2676 case 1:
2677 /* TODO: TOS specificity for cases 2 and 3 */
2678 case 2:
2679 case 3:
2680 break;
2681 default:
2682 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2683 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2684 freemsg(mp);
2685 return;
2686 }
2687 /*
2688 * Create a Route Association. This will allow us to remember that
2689 * someone we believe told us to use the particular gateway.
2690 */
2691 ire = ire_create(
2692 (uchar_t *)&dst, /* dest addr */
2693 (uchar_t *)&ip_g_all_ones, /* mask */
2694 (uchar_t *)&gateway, /* gateway addr */
2695 IRE_HOST,
2696 NULL, /* ill */
2697 ALL_ZONES,
2698 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2699 NULL, /* tsol_gc_t */
2700 ipst);
2701
2702 if (ire == NULL) {
2703 freemsg(mp);
2704 return;
2705 }
2706 nire = ire_add(ire);
2707 /* Check if it was a duplicate entry */
2708 if (nire != NULL && nire != ire) {
2709 ASSERT(nire->ire_identical_ref > 1);
2710 ire_delete(nire);
2711 ire_refrele(nire);
2712 nire = NULL;
2713 }
2714 ire = nire;
2715 if (ire != NULL) {
2716 ire_refrele(ire); /* Held in ire_add */
2717
2718 /* tell routing sockets that we received a redirect */
2719 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2720 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2721 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2722 }
2723
2724 /*
2725 * Delete any existing IRE_HOST type redirect ires for this destination.
2726 * This together with the added IRE has the effect of
2727 * modifying an existing redirect.
2728 */
2729 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2730 ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2731 if (prev_ire != NULL) {
2732 if (prev_ire ->ire_flags & RTF_DYNAMIC)
2733 ire_delete(prev_ire);
2734 ire_refrele(prev_ire);
2735 }
2736
2737 freemsg(mp);
2738 }
2739
2740 /*
2741 * Generate an ICMP parameter problem message.
2742 * When called from ip_output side a minimal ip_recv_attr_t needs to be
2743 * constructed by the caller.
2744 */
2745 static void
2746 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2747 {
2748 icmph_t icmph;
2749 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2750
2751 mp = icmp_pkt_err_ok(mp, ira);
2752 if (mp == NULL)
2753 return;
2754
2755 bzero(&icmph, sizeof (icmph_t));
2756 icmph.icmph_type = ICMP_PARAM_PROBLEM;
2757 icmph.icmph_pp_ptr = ptr;
2758 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2759 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2760 }
2761
2762 /*
2763 * Build and ship an IPv4 ICMP message using the packet data in mp, and
2764 * the ICMP header pointed to by "stuff". (May be called as writer.)
2765 * Note: assumes that icmp_pkt_err_ok has been called to verify that
2766 * an icmp error packet can be sent.
2767 * Assigns an appropriate source address to the packet. If ipha_dst is
2768 * one of our addresses use it for source. Otherwise let ip_output_simple
2769 * pick the source address.
2770 */
2771 static void
2772 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2773 {
2774 ipaddr_t dst;
2775 icmph_t *icmph;
2776 ipha_t *ipha;
2777 uint_t len_needed;
2778 size_t msg_len;
2779 mblk_t *mp1;
2780 ipaddr_t src;
2781 ire_t *ire;
2782 ip_xmit_attr_t ixas;
2783 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2784
2785 ipha = (ipha_t *)mp->b_rptr;
2786
2787 bzero(&ixas, sizeof (ixas));
2788 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2789 ixas.ixa_zoneid = ira->ira_zoneid;
2790 ixas.ixa_ifindex = 0;
2791 ixas.ixa_ipst = ipst;
2792 ixas.ixa_cred = kcred;
2793 ixas.ixa_cpid = NOPID;
2794 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
2795 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2796
2797 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2798 /*
2799 * Apply IPsec based on how IPsec was applied to
2800 * the packet that had the error.
2801 *
2802 * If it was an outbound packet that caused the ICMP
2803 * error, then the caller will have setup the IRA
2804 * appropriately.
2805 */
2806 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2807 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2808 /* Note: mp already consumed and ip_drop_packet done */
2809 return;
2810 }
2811 } else {
2812 /*
2813 * This is in clear. The icmp message we are building
2814 * here should go out in clear, independent of our policy.
2815 */
2816 ixas.ixa_flags |= IXAF_NO_IPSEC;
2817 }
2818
2819 /* Remember our eventual destination */
2820 dst = ipha->ipha_src;
2821
2822 /*
2823 * If the packet was for one of our unicast addresses, make
2824 * sure we respond with that as the source. Otherwise
2825 * have ip_output_simple pick the source address.
2826 */
2827 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2828 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL,
2829 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2830 if (ire != NULL) {
2831 ire_refrele(ire);
2832 src = ipha->ipha_dst;
2833 } else {
2834 src = INADDR_ANY;
2835 ixas.ixa_flags |= IXAF_SET_SOURCE;
2836 }
2837
2838 /*
2839 * Check if we can send back more then 8 bytes in addition to
2840 * the IP header. We try to send 64 bytes of data and the internal
2841 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2842 */
2843 len_needed = IPH_HDR_LENGTH(ipha);
2844 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2845 ipha->ipha_protocol == IPPROTO_IPV6) {
2846 if (!pullupmsg(mp, -1)) {
2847 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2848 ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2849 freemsg(mp);
2850 return;
2851 }
2852 ipha = (ipha_t *)mp->b_rptr;
2853
2854 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2855 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2856 len_needed));
2857 } else {
2858 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2859
2860 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2861 len_needed += ip_hdr_length_v6(mp, ip6h);
2862 }
2863 }
2864 len_needed += ipst->ips_ip_icmp_return;
2865 msg_len = msgdsize(mp);
2866 if (msg_len > len_needed) {
2867 (void) adjmsg(mp, len_needed - msg_len);
2868 msg_len = len_needed;
2869 }
2870 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2871 if (mp1 == NULL) {
2872 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2873 freemsg(mp);
2874 return;
2875 }
2876 mp1->b_cont = mp;
2877 mp = mp1;
2878
2879 /*
2880 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2881 * node generates be accepted in peace by all on-host destinations.
2882 * If we do NOT assume that all on-host destinations trust
2883 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2884 * (Look for IXAF_TRUSTED_ICMP).
2885 */
2886 ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2887
2888 ipha = (ipha_t *)mp->b_rptr;
2889 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2890 *ipha = icmp_ipha;
2891 ipha->ipha_src = src;
2892 ipha->ipha_dst = dst;
2893 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2894 msg_len += sizeof (icmp_ipha) + len;
2895 if (msg_len > IP_MAXPACKET) {
2896 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2897 msg_len = IP_MAXPACKET;
2898 }
2899 ipha->ipha_length = htons((uint16_t)msg_len);
2900 icmph = (icmph_t *)&ipha[1];
2901 bcopy(stuff, icmph, len);
2902 icmph->icmph_checksum = 0;
2903 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2904 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2905
2906 (void) ip_output_simple(mp, &ixas);
2907 ixa_cleanup(&ixas);
2908 }
2909
2910 /*
2911 * Determine if an ICMP error packet can be sent given the rate limit.
2912 * The limit consists of an average frequency (icmp_pkt_err_interval measured
2913 * in milliseconds) and a burst size. Burst size number of packets can
2914 * be sent arbitrarely closely spaced.
2915 * The state is tracked using two variables to implement an approximate
2916 * token bucket filter:
2917 * icmp_pkt_err_last - lbolt value when the last burst started
2918 * icmp_pkt_err_sent - number of packets sent in current burst
2919 */
2920 boolean_t
2921 icmp_err_rate_limit(ip_stack_t *ipst)
2922 {
2923 clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2924 uint_t refilled; /* Number of packets refilled in tbf since last */
2925 /* Guard against changes by loading into local variable */
2926 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2927
2928 if (err_interval == 0)
2929 return (B_FALSE);
2930
2931 if (ipst->ips_icmp_pkt_err_last > now) {
2932 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2933 ipst->ips_icmp_pkt_err_last = 0;
2934 ipst->ips_icmp_pkt_err_sent = 0;
2935 }
2936 /*
2937 * If we are in a burst update the token bucket filter.
2938 * Update the "last" time to be close to "now" but make sure
2939 * we don't loose precision.
2940 */
2941 if (ipst->ips_icmp_pkt_err_sent != 0) {
2942 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2943 if (refilled > ipst->ips_icmp_pkt_err_sent) {
2944 ipst->ips_icmp_pkt_err_sent = 0;
2945 } else {
2946 ipst->ips_icmp_pkt_err_sent -= refilled;
2947 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2948 }
2949 }
2950 if (ipst->ips_icmp_pkt_err_sent == 0) {
2951 /* Start of new burst */
2952 ipst->ips_icmp_pkt_err_last = now;
2953 }
2954 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2955 ipst->ips_icmp_pkt_err_sent++;
2956 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2957 ipst->ips_icmp_pkt_err_sent));
2958 return (B_FALSE);
2959 }
2960 ip1dbg(("icmp_err_rate_limit: dropped\n"));
2961 return (B_TRUE);
2962 }
2963
2964 /*
2965 * Check if it is ok to send an IPv4 ICMP error packet in
2966 * response to the IPv4 packet in mp.
2967 * Free the message and return null if no
2968 * ICMP error packet should be sent.
2969 */
2970 static mblk_t *
2971 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2972 {
2973 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2974 icmph_t *icmph;
2975 ipha_t *ipha;
2976 uint_t len_needed;
2977
2978 if (!mp)
2979 return (NULL);
2980 ipha = (ipha_t *)mp->b_rptr;
2981 if (ip_csum_hdr(ipha)) {
2982 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2983 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2984 freemsg(mp);
2985 return (NULL);
2986 }
2987 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2988 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2989 CLASSD(ipha->ipha_dst) ||
2990 CLASSD(ipha->ipha_src) ||
2991 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2992 /* Note: only errors to the fragment with offset 0 */
2993 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2994 freemsg(mp);
2995 return (NULL);
2996 }
2997 if (ipha->ipha_protocol == IPPROTO_ICMP) {
2998 /*
2999 * Check the ICMP type. RFC 1122 sez: don't send ICMP
3000 * errors in response to any ICMP errors.
3001 */
3002 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3003 if (mp->b_wptr - mp->b_rptr < len_needed) {
3004 if (!pullupmsg(mp, len_needed)) {
3005 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3006 freemsg(mp);
3007 return (NULL);
3008 }
3009 ipha = (ipha_t *)mp->b_rptr;
3010 }
3011 icmph = (icmph_t *)
3012 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3013 switch (icmph->icmph_type) {
3014 case ICMP_DEST_UNREACHABLE:
3015 case ICMP_SOURCE_QUENCH:
3016 case ICMP_TIME_EXCEEDED:
3017 case ICMP_PARAM_PROBLEM:
3018 case ICMP_REDIRECT:
3019 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3020 freemsg(mp);
3021 return (NULL);
3022 default:
3023 break;
3024 }
3025 }
3026 /*
3027 * If this is a labeled system, then check to see if we're allowed to
3028 * send a response to this particular sender. If not, then just drop.
3029 */
3030 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
3031 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3032 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3033 freemsg(mp);
3034 return (NULL);
3035 }
3036 if (icmp_err_rate_limit(ipst)) {
3037 /*
3038 * Only send ICMP error packets every so often.
3039 * This should be done on a per port/source basis,
3040 * but for now this will suffice.
3041 */
3042 freemsg(mp);
3043 return (NULL);
3044 }
3045 return (mp);
3046 }
3047
3048 /*
3049 * Called when a packet was sent out the same link that it arrived on.
3050 * Check if it is ok to send a redirect and then send it.
3051 */
3052 void
3053 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
3054 ip_recv_attr_t *ira)
3055 {
3056 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3057 ipaddr_t src, nhop;
3058 mblk_t *mp1;
3059 ire_t *nhop_ire;
3060
3061 /*
3062 * Check the source address to see if it originated
3063 * on the same logical subnet it is going back out on.
3064 * If so, we should be able to send it a redirect.
3065 * Avoid sending a redirect if the destination
3066 * is directly connected (i.e., we matched an IRE_ONLINK),
3067 * or if the packet was source routed out this interface.
3068 *
3069 * We avoid sending a redirect if the
3070 * destination is directly connected
3071 * because it is possible that multiple
3072 * IP subnets may have been configured on
3073 * the link, and the source may not
3074 * be on the same subnet as ip destination,
3075 * even though they are on the same
3076 * physical link.
3077 */
3078 if ((ire->ire_type & IRE_ONLINK) ||
3079 ip_source_routed(ipha, ipst))
3080 return;
3081
3082 nhop_ire = ire_nexthop(ire);
3083 if (nhop_ire == NULL)
3084 return;
3085
3086 nhop = nhop_ire->ire_addr;
3087
3088 if (nhop_ire->ire_type & IRE_IF_CLONE) {
3089 ire_t *ire2;
3090
3091 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3092 mutex_enter(&nhop_ire->ire_lock);
3093 ire2 = nhop_ire->ire_dep_parent;
3094 if (ire2 != NULL)
3095 ire_refhold(ire2);
3096 mutex_exit(&nhop_ire->ire_lock);
3097 ire_refrele(nhop_ire);
3098 nhop_ire = ire2;
3099 }
3100 if (nhop_ire == NULL)
3101 return;
3102
3103 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3104
3105 src = ipha->ipha_src;
3106
3107 /*
3108 * We look at the interface ire for the nexthop,
3109 * to see if ipha_src is in the same subnet
3110 * as the nexthop.
3111 */
3112 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3113 /*
3114 * The source is directly connected.
3115 */
3116 mp1 = copymsg(mp);
3117 if (mp1 != NULL) {
3118 icmp_send_redirect(mp1, nhop, ira);
3119 }
3120 }
3121 ire_refrele(nhop_ire);
3122 }
3123
3124 /*
3125 * Generate an ICMP redirect message.
3126 */
3127 static void
3128 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3129 {
3130 icmph_t icmph;
3131 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3132
3133 mp = icmp_pkt_err_ok(mp, ira);
3134 if (mp == NULL)
3135 return;
3136
3137 bzero(&icmph, sizeof (icmph_t));
3138 icmph.icmph_type = ICMP_REDIRECT;
3139 icmph.icmph_code = 1;
3140 icmph.icmph_rd_gateway = gateway;
3141 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3142 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3143 }
3144
3145 /*
3146 * Generate an ICMP time exceeded message.
3147 */
3148 void
3149 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3150 {
3151 icmph_t icmph;
3152 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3153
3154 mp = icmp_pkt_err_ok(mp, ira);
3155 if (mp == NULL)
3156 return;
3157
3158 bzero(&icmph, sizeof (icmph_t));
3159 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3160 icmph.icmph_code = code;
3161 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3162 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3163 }
3164
3165 /*
3166 * Generate an ICMP unreachable message.
3167 * When called from ip_output side a minimal ip_recv_attr_t needs to be
3168 * constructed by the caller.
3169 */
3170 void
3171 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3172 {
3173 icmph_t icmph;
3174 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3175
3176 mp = icmp_pkt_err_ok(mp, ira);
3177 if (mp == NULL)
3178 return;
3179
3180 bzero(&icmph, sizeof (icmph_t));
3181 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3182 icmph.icmph_code = code;
3183 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3184 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3185 }
3186
3187 /*
3188 * Latch in the IPsec state for a stream based the policy in the listener
3189 * and the actions in the ip_recv_attr_t.
3190 * Called directly from TCP and SCTP.
3191 */
3192 boolean_t
3193 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3194 {
3195 ASSERT(lconnp->conn_policy != NULL);
3196 ASSERT(connp->conn_policy == NULL);
3197
3198 IPPH_REFHOLD(lconnp->conn_policy);
3199 connp->conn_policy = lconnp->conn_policy;
3200
3201 if (ira->ira_ipsec_action != NULL) {
3202 if (connp->conn_latch == NULL) {
3203 connp->conn_latch = iplatch_create();
3204 if (connp->conn_latch == NULL)
3205 return (B_FALSE);
3206 }
3207 ipsec_latch_inbound(connp, ira);
3208 }
3209 return (B_TRUE);
3210 }
3211
3212 /*
3213 * Verify whether or not the IP address is a valid local address.
3214 * Could be a unicast, including one for a down interface.
3215 * If allow_mcbc then a multicast or broadcast address is also
3216 * acceptable.
3217 *
3218 * In the case of a broadcast/multicast address, however, the
3219 * upper protocol is expected to reset the src address
3220 * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3221 * no packets are emitted with broadcast/multicast address as
3222 * source address (that violates hosts requirements RFC 1122)
3223 * The addresses valid for bind are:
3224 * (1) - INADDR_ANY (0)
3225 * (2) - IP address of an UP interface
3226 * (3) - IP address of a DOWN interface
3227 * (4) - valid local IP broadcast addresses. In this case
3228 * the conn will only receive packets destined to
3229 * the specified broadcast address.
3230 * (5) - a multicast address. In this case
3231 * the conn will only receive packets destined to
3232 * the specified multicast address. Note: the
3233 * application still has to issue an
3234 * IP_ADD_MEMBERSHIP socket option.
3235 *
3236 * In all the above cases, the bound address must be valid in the current zone.
3237 * When the address is loopback, multicast or broadcast, there might be many
3238 * matching IREs so bind has to look up based on the zone.
3239 */
3240 ip_laddr_t
3241 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3242 ip_stack_t *ipst, boolean_t allow_mcbc)
3243 {
3244 ire_t *src_ire;
3245
3246 ASSERT(src_addr != INADDR_ANY);
3247
3248 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3249 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3250
3251 /*
3252 * If an address other than in6addr_any is requested,
3253 * we verify that it is a valid address for bind
3254 * Note: Following code is in if-else-if form for
3255 * readability compared to a condition check.
3256 */
3257 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3258 /*
3259 * (2) Bind to address of local UP interface
3260 */
3261 ire_refrele(src_ire);
3262 return (IPVL_UNICAST_UP);
3263 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3264 /*
3265 * (4) Bind to broadcast address
3266 */
3267 ire_refrele(src_ire);
3268 if (allow_mcbc)
3269 return (IPVL_BCAST);
3270 else
3271 return (IPVL_BAD);
3272 } else if (CLASSD(src_addr)) {
3273 /* (5) bind to multicast address. */
3274 if (src_ire != NULL)
3275 ire_refrele(src_ire);
3276
3277 if (allow_mcbc)
3278 return (IPVL_MCAST);
3279 else
3280 return (IPVL_BAD);
3281 } else {
3282 ipif_t *ipif;
3283
3284 /*
3285 * (3) Bind to address of local DOWN interface?
3286 * (ipif_lookup_addr() looks up all interfaces
3287 * but we do not get here for UP interfaces
3288 * - case (2) above)
3289 */
3290 if (src_ire != NULL)
3291 ire_refrele(src_ire);
3292
3293 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3294 if (ipif == NULL)
3295 return (IPVL_BAD);
3296
3297 /* Not a useful source? */
3298 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3299 ipif_refrele(ipif);
3300 return (IPVL_BAD);
3301 }
3302 ipif_refrele(ipif);
3303 return (IPVL_UNICAST_DOWN);
3304 }
3305 }
3306
3307 /*
3308 * Insert in the bind fanout for IPv4 and IPv6.
3309 * The caller should already have used ip_laddr_verify_v*() before calling
3310 * this.
3311 */
3312 int
3313 ip_laddr_fanout_insert(conn_t *connp)
3314 {
3315 int error;
3316
3317 /*
3318 * Allow setting new policies. For example, disconnects result
3319 * in us being called. As we would have set conn_policy_cached
3320 * to B_TRUE before, we should set it to B_FALSE, so that policy
3321 * can change after the disconnect.
3322 */
3323 connp->conn_policy_cached = B_FALSE;
3324
3325 error = ipcl_bind_insert(connp);
3326 if (error != 0) {
3327 if (connp->conn_anon_port) {
3328 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
3329 connp->conn_mlp_type, connp->conn_proto,
3330 ntohs(connp->conn_lport), B_FALSE);
3331 }
3332 connp->conn_mlp_type = mlptSingle;
3333 }
3334 return (error);
3335 }
3336
3337 /*
3338 * Verify that both the source and destination addresses are valid. If
3339 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3340 * i.e. have no route to it. Protocols like TCP want to verify destination
3341 * reachability, while tunnels do not.
3342 *
3343 * Determine the route, the interface, and (optionally) the source address
3344 * to use to reach a given destination.
3345 * Note that we allow connect to broadcast and multicast addresses when
3346 * IPDF_ALLOW_MCBC is set.
3347 * first_hop and dst_addr are normally the same, but if source routing
3348 * they will differ; in that case the first_hop is what we'll use for the
3349 * routing lookup but the dce and label checks will be done on dst_addr,
3350 *
3351 * If uinfo is set, then we fill in the best available information
3352 * we have for the destination. This is based on (in priority order) any
3353 * metrics and path MTU stored in a dce_t, route metrics, and finally the
3354 * ill_mtu/ill_mc_mtu.
3355 *
3356 * Tsol note: If we have a source route then dst_addr != firsthop. But we
3357 * always do the label check on dst_addr.
3358 */
3359 int
3360 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3361 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode)
3362 {
3363 ire_t *ire = NULL;
3364 int error = 0;
3365 ipaddr_t setsrc; /* RTF_SETSRC */
3366 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */
3367 ip_stack_t *ipst = ixa->ixa_ipst;
3368 dce_t *dce;
3369 uint_t pmtu;
3370 uint_t generation;
3371 nce_t *nce;
3372 ill_t *ill = NULL;
3373 boolean_t multirt = B_FALSE;
3374
3375 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3376
3377 /*
3378 * We never send to zero; the ULPs map it to the loopback address.
3379 * We can't allow it since we use zero to mean unitialized in some
3380 * places.
3381 */
3382 ASSERT(dst_addr != INADDR_ANY);
3383
3384 if (is_system_labeled()) {
3385 ts_label_t *tsl = NULL;
3386
3387 error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION,
3388 mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl);
3389 if (error != 0)
3390 return (error);
3391 if (tsl != NULL) {
3392 /* Update the label */
3393 ip_xmit_attr_replace_tsl(ixa, tsl);
3394 }
3395 }
3396
3397 setsrc = INADDR_ANY;
3398 /*
3399 * Select a route; For IPMP interfaces, we would only select
3400 * a "hidden" route (i.e., going through a specific under_ill)
3401 * if ixa_ifindex has been specified.
3402 */
3403 ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3404 &generation, &setsrc, &error, &multirt);
3405 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */
3406 if (error != 0)
3407 goto bad_addr;
3408
3409 /*
3410 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3411 * If IPDF_VERIFY_DST is set, the destination must be reachable;
3412 * Otherwise the destination needn't be reachable.
3413 *
3414 * If we match on a reject or black hole, then we've got a
3415 * local failure. May as well fail out the connect() attempt,
3416 * since it's never going to succeed.
3417 */
3418 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3419 /*
3420 * If we're verifying destination reachability, we always want
3421 * to complain here.
3422 *
3423 * If we're not verifying destination reachability but the
3424 * destination has a route, we still want to fail on the
3425 * temporary address and broadcast address tests.
3426 *
3427 * In both cases do we let the code continue so some reasonable
3428 * information is returned to the caller. That enables the
3429 * caller to use (and even cache) the IRE. conn_ip_ouput will
3430 * use the generation mismatch path to check for the unreachable
3431 * case thereby avoiding any specific check in the main path.
3432 */
3433 ASSERT(generation == IRE_GENERATION_VERIFY);
3434 if (flags & IPDF_VERIFY_DST) {
3435 /*
3436 * Set errno but continue to set up ixa_ire to be
3437 * the RTF_REJECT|RTF_BLACKHOLE IRE.
3438 * That allows callers to use ip_output to get an
3439 * ICMP error back.
3440 */
3441 if (!(ire->ire_type & IRE_HOST))
3442 error = ENETUNREACH;
3443 else
3444 error = EHOSTUNREACH;
3445 }
3446 }
3447
3448 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3449 !(flags & IPDF_ALLOW_MCBC)) {
3450 ire_refrele(ire);
3451 ire = ire_reject(ipst, B_FALSE);
3452 generation = IRE_GENERATION_VERIFY;
3453 error = ENETUNREACH;
3454 }
3455
3456 /* Cache things */
3457 if (ixa->ixa_ire != NULL)
3458 ire_refrele_notr(ixa->ixa_ire);
3459 #ifdef DEBUG
3460 ire_refhold_notr(ire);
3461 ire_refrele(ire);
3462 #endif
3463 ixa->ixa_ire = ire;
3464 ixa->ixa_ire_generation = generation;
3465
3466 /*
3467 * Ensure that ixa_dce is always set any time that ixa_ire is set,
3468 * since some callers will send a packet to conn_ip_output() even if
3469 * there's an error.
3470 */
3471 if (flags & IPDF_UNIQUE_DCE) {
3472 /* Fallback to the default dce if allocation fails */
3473 dce = dce_lookup_and_add_v4(dst_addr, ipst);
3474 if (dce != NULL)
3475 generation = dce->dce_generation;
3476 else
3477 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3478 } else {
3479 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3480 }
3481 ASSERT(dce != NULL);
3482 if (ixa->ixa_dce != NULL)
3483 dce_refrele_notr(ixa->ixa_dce);
3484 #ifdef DEBUG
3485 dce_refhold_notr(dce);
3486 dce_refrele(dce);
3487 #endif
3488 ixa->ixa_dce = dce;
3489 ixa->ixa_dce_generation = generation;
3490
3491 /*
3492 * For multicast with multirt we have a flag passed back from
3493 * ire_lookup_multi_ill_v4 since we don't have an IRE for each
3494 * possible multicast address.
3495 * We also need a flag for multicast since we can't check
3496 * whether RTF_MULTIRT is set in ixa_ire for multicast.
3497 */
3498 if (multirt) {
3499 ixa->ixa_postfragfn = ip_postfrag_multirt_v4;
3500 ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST;
3501 } else {
3502 ixa->ixa_postfragfn = ire->ire_postfragfn;
3503 ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST;
3504 }
3505 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3506 /* Get an nce to cache. */
3507 nce = ire_to_nce(ire, firsthop, NULL);
3508 if (nce == NULL) {
3509 /* Allocation failure? */
3510 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3511 } else {
3512 if (ixa->ixa_nce != NULL)
3513 nce_refrele(ixa->ixa_nce);
3514 ixa->ixa_nce = nce;
3515 }
3516 }
3517
3518 /*
3519 * If the source address is a loopback address, the
3520 * destination had best be local or multicast.
3521 * If we are sending to an IRE_LOCAL using a loopback source then
3522 * it had better be the same zoneid.
3523 */
3524 if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3525 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3526 ire = NULL; /* Stored in ixa_ire */
3527 error = EADDRNOTAVAIL;
3528 goto bad_addr;
3529 }
3530 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3531 ire = NULL; /* Stored in ixa_ire */
3532 error = EADDRNOTAVAIL;
3533 goto bad_addr;
3534 }
3535 }
3536 if (ire->ire_type & IRE_BROADCAST) {
3537 /*
3538 * If the ULP didn't have a specified source, then we
3539 * make sure we reselect the source when sending
3540 * broadcasts out different interfaces.
3541 */
3542 if (flags & IPDF_SELECT_SRC)
3543 ixa->ixa_flags |= IXAF_SET_SOURCE;
3544 else
3545 ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3546 }
3547
3548 /*
3549 * Does the caller want us to pick a source address?
3550 */
3551 if (flags & IPDF_SELECT_SRC) {
3552 ipaddr_t src_addr;
3553
3554 /*
3555 * We use use ire_nexthop_ill to avoid the under ipmp
3556 * interface for source address selection. Note that for ipmp
3557 * probe packets, ixa_ifindex would have been specified, and
3558 * the ip_select_route() invocation would have picked an ire
3559 * will ire_ill pointing at an under interface.
3560 */
3561 ill = ire_nexthop_ill(ire);
3562
3563 /* If unreachable we have no ill but need some source */
3564 if (ill == NULL) {
3565 src_addr = htonl(INADDR_LOOPBACK);
3566 /* Make sure we look for a better source address */
3567 generation = SRC_GENERATION_VERIFY;
3568 } else {
3569 error = ip_select_source_v4(ill, setsrc, dst_addr,
3570 ixa->ixa_multicast_ifaddr, zoneid,
3571 ipst, &src_addr, &generation, NULL);
3572 if (error != 0) {
3573 ire = NULL; /* Stored in ixa_ire */
3574 goto bad_addr;
3575 }
3576 }
3577
3578 /*
3579 * We allow the source address to to down.
3580 * However, we check that we don't use the loopback address
3581 * as a source when sending out on the wire.
3582 */
3583 if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3584 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3585 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3586 ire = NULL; /* Stored in ixa_ire */
3587 error = EADDRNOTAVAIL;
3588 goto bad_addr;
3589 }
3590
3591 *src_addrp = src_addr;
3592 ixa->ixa_src_generation = generation;
3593 }
3594
3595 /*
3596 * Make sure we don't leave an unreachable ixa_nce in place
3597 * since ip_select_route is used when we unplumb i.e., remove
3598 * references on ixa_ire, ixa_nce, and ixa_dce.
3599 */
3600 nce = ixa->ixa_nce;
3601 if (nce != NULL && nce->nce_is_condemned) {
3602 nce_refrele(nce);
3603 ixa->ixa_nce = NULL;
3604 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3605 }
3606
3607 /*
3608 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3609 * However, we can't do it for IPv4 multicast or broadcast.
3610 */
3611 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3612 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3613
3614 /*
3615 * Set initial value for fragmentation limit. Either conn_ip_output
3616 * or ULP might updates it when there are routing changes.
3617 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3618 */
3619 pmtu = ip_get_pmtu(ixa);
3620 ixa->ixa_fragsize = pmtu;
3621 /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3622 if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3623 ixa->ixa_pmtu = pmtu;
3624
3625 /*
3626 * Extract information useful for some transports.
3627 * First we look for DCE metrics. Then we take what we have in
3628 * the metrics in the route, where the offlink is used if we have
3629 * one.
3630 */
3631 if (uinfo != NULL) {
3632 bzero(uinfo, sizeof (*uinfo));
3633
3634 if (dce->dce_flags & DCEF_UINFO)
3635 *uinfo = dce->dce_uinfo;
3636
3637 rts_merge_metrics(uinfo, &ire->ire_metrics);
3638
3639 /* Allow ire_metrics to decrease the path MTU from above */
3640 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3641 uinfo->iulp_mtu = pmtu;
3642
3643 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3644 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3645 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3646 }
3647
3648 if (ill != NULL)
3649 ill_refrele(ill);
3650
3651 return (error);
3652
3653 bad_addr:
3654 if (ire != NULL)
3655 ire_refrele(ire);
3656
3657 if (ill != NULL)
3658 ill_refrele(ill);
3659
3660 /*
3661 * Make sure we don't leave an unreachable ixa_nce in place
3662 * since ip_select_route is used when we unplumb i.e., remove
3663 * references on ixa_ire, ixa_nce, and ixa_dce.
3664 */
3665 nce = ixa->ixa_nce;
3666 if (nce != NULL && nce->nce_is_condemned) {
3667 nce_refrele(nce);
3668 ixa->ixa_nce = NULL;
3669 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3670 }
3671
3672 return (error);
3673 }
3674
3675
3676 /*
3677 * Get the base MTU for the case when path MTU discovery is not used.
3678 * Takes the MTU of the IRE into account.
3679 */
3680 uint_t
3681 ip_get_base_mtu(ill_t *ill, ire_t *ire)
3682 {
3683 uint_t mtu;
3684 uint_t iremtu = ire->ire_metrics.iulp_mtu;
3685
3686 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3687 mtu = ill->ill_mc_mtu;
3688 else
3689 mtu = ill->ill_mtu;
3690
3691 if (iremtu != 0 && iremtu < mtu)
3692 mtu = iremtu;
3693
3694 return (mtu);
3695 }
3696
3697 /*
3698 * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3699 * Assumes that ixa_ire, dce, and nce have already been set up.
3700 *
3701 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3702 * We avoid path MTU discovery if it is disabled with ndd.
3703 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3704 *
3705 * NOTE: We also used to turn it off for source routed packets. That
3706 * is no longer required since the dce is per final destination.
3707 */
3708 uint_t
3709 ip_get_pmtu(ip_xmit_attr_t *ixa)
3710 {
3711 ip_stack_t *ipst = ixa->ixa_ipst;
3712 dce_t *dce;
3713 nce_t *nce;
3714 ire_t *ire;
3715 uint_t pmtu;
3716
3717 ire = ixa->ixa_ire;
3718 dce = ixa->ixa_dce;
3719 nce = ixa->ixa_nce;
3720
3721 /*
3722 * If path MTU discovery has been turned off by ndd, then we ignore
3723 * any dce_pmtu and for IPv4 we will not set DF.
3724 */
3725 if (!ipst->ips_ip_path_mtu_discovery)
3726 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3727
3728 pmtu = IP_MAXPACKET;
3729 /*
3730 * Decide whether whether IPv4 sets DF
3731 * For IPv6 "no DF" means to use the 1280 mtu
3732 */
3733 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3734 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3735 } else {
3736 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3737 if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3738 pmtu = IPV6_MIN_MTU;
3739 }
3740
3741 /* Check if the PMTU is to old before we use it */
3742 if ((dce->dce_flags & DCEF_PMTU) &&
3743 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3744 ipst->ips_ip_pathmtu_interval) {
3745 /*
3746 * Older than 20 minutes. Drop the path MTU information.
3747 */
3748 mutex_enter(&dce->dce_lock);
3749 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3750 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3751 mutex_exit(&dce->dce_lock);
3752 dce_increment_generation(dce);
3753 }
3754
3755 /* The metrics on the route can lower the path MTU */
3756 if (ire->ire_metrics.iulp_mtu != 0 &&
3757 ire->ire_metrics.iulp_mtu < pmtu)
3758 pmtu = ire->ire_metrics.iulp_mtu;
3759
3760 /*
3761 * If the path MTU is smaller than some minimum, we still use dce_pmtu
3762 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3763 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3764 */
3765 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3766 if (dce->dce_flags & DCEF_PMTU) {
3767 if (dce->dce_pmtu < pmtu)
3768 pmtu = dce->dce_pmtu;
3769
3770 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3771 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3772 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3773 } else {
3774 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3775 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3776 }
3777 } else {
3778 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3779 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3780 }
3781 }
3782
3783 /*
3784 * If we have an IRE_LOCAL we use the loopback mtu instead of
3785 * the ill for going out the wire i.e., IRE_LOCAL gets the same
3786 * mtu as IRE_LOOPBACK.
3787 */
3788 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3789 uint_t loopback_mtu;
3790
3791 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3792 ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3793
3794 if (loopback_mtu < pmtu)
3795 pmtu = loopback_mtu;
3796 } else if (nce != NULL) {
3797 /*
3798 * Make sure we don't exceed the interface MTU.
3799 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3800 * an ill. We'd use the above IP_MAXPACKET in that case just
3801 * to tell the transport something larger than zero.
3802 */
3803 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3804 if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3805 pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3806 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3807 nce->nce_ill->ill_mc_mtu < pmtu) {
3808 /*
3809 * for interfaces in an IPMP group, the mtu of
3810 * the nce_ill (under_ill) could be different
3811 * from the mtu of the ncec_ill, so we take the
3812 * min of the two.
3813 */
3814 pmtu = nce->nce_ill->ill_mc_mtu;
3815 }
3816 } else {
3817 if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3818 pmtu = nce->nce_common->ncec_ill->ill_mtu;
3819 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3820 nce->nce_ill->ill_mtu < pmtu) {
3821 /*
3822 * for interfaces in an IPMP group, the mtu of
3823 * the nce_ill (under_ill) could be different
3824 * from the mtu of the ncec_ill, so we take the
3825 * min of the two.
3826 */
3827 pmtu = nce->nce_ill->ill_mtu;
3828 }
3829 }
3830 }
3831
3832 /*
3833 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3834 * Only applies to IPv6.
3835 */
3836 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3837 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3838 switch (ixa->ixa_use_min_mtu) {
3839 case IPV6_USE_MIN_MTU_MULTICAST:
3840 if (ire->ire_type & IRE_MULTICAST)
3841 pmtu = IPV6_MIN_MTU;
3842 break;
3843 case IPV6_USE_MIN_MTU_ALWAYS:
3844 pmtu = IPV6_MIN_MTU;
3845 break;
3846 case IPV6_USE_MIN_MTU_NEVER:
3847 break;
3848 }
3849 } else {
3850 /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3851 if (ire->ire_type & IRE_MULTICAST)
3852 pmtu = IPV6_MIN_MTU;
3853 }
3854 }
3855
3856 /*
3857 * For multirouted IPv6 packets, the IP layer will insert a 8-byte
3858 * fragment header in every packet. We compensate for those cases by
3859 * returning a smaller path MTU to the ULP.
3860 *
3861 * In the case of CGTP then ip_output will add a fragment header.
3862 * Make sure there is room for it by telling a smaller number
3863 * to the transport.
3864 *
3865 * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here
3866 * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu()
3867 * which is the size of the packets it can send.
3868 */
3869 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3870 if ((ire->ire_flags & RTF_MULTIRT) ||
3871 (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) {
3872 pmtu -= sizeof (ip6_frag_t);
3873 ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR;
3874 }
3875 }
3876
3877 return (pmtu);
3878 }
3879
3880 /*
3881 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3882 * the final piece where we don't. Return a pointer to the first mblk in the
3883 * result, and update the pointer to the next mblk to chew on. If anything
3884 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3885 * NULL pointer.
3886 */
3887 mblk_t *
3888 ip_carve_mp(mblk_t **mpp, ssize_t len)
3889 {
3890 mblk_t *mp0;
3891 mblk_t *mp1;
3892 mblk_t *mp2;
3893
3894 if (!len || !mpp || !(mp0 = *mpp))
3895 return (NULL);
3896 /* If we aren't going to consume the first mblk, we need a dup. */
3897 if (mp0->b_wptr - mp0->b_rptr > len) {
3898 mp1 = dupb(mp0);
3899 if (mp1) {
3900 /* Partition the data between the two mblks. */
3901 mp1->b_wptr = mp1->b_rptr + len;
3902 mp0->b_rptr = mp1->b_wptr;
3903 /*
3904 * after adjustments if mblk not consumed is now
3905 * unaligned, try to align it. If this fails free
3906 * all messages and let upper layer recover.
3907 */
3908 if (!OK_32PTR(mp0->b_rptr)) {
3909 if (!pullupmsg(mp0, -1)) {
3910 freemsg(mp0);
3911 freemsg(mp1);
3912 *mpp = NULL;
3913 return (NULL);
3914 }
3915 }
3916 }
3917 return (mp1);
3918 }
3919 /* Eat through as many mblks as we need to get len bytes. */
3920 len -= mp0->b_wptr - mp0->b_rptr;
3921 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3922 if (mp2->b_wptr - mp2->b_rptr > len) {
3923 /*
3924 * We won't consume the entire last mblk. Like
3925 * above, dup and partition it.
3926 */
3927 mp1->b_cont = dupb(mp2);
3928 mp1 = mp1->b_cont;
3929 if (!mp1) {
3930 /*
3931 * Trouble. Rather than go to a lot of
3932 * trouble to clean up, we free the messages.
3933 * This won't be any worse than losing it on
3934 * the wire.
3935 */
3936 freemsg(mp0);
3937 freemsg(mp2);
3938 *mpp = NULL;
3939 return (NULL);
3940 }
3941 mp1->b_wptr = mp1->b_rptr + len;
3942 mp2->b_rptr = mp1->b_wptr;
3943 /*
3944 * after adjustments if mblk not consumed is now
3945 * unaligned, try to align it. If this fails free
3946 * all messages and let upper layer recover.
3947 */
3948 if (!OK_32PTR(mp2->b_rptr)) {
3949 if (!pullupmsg(mp2, -1)) {
3950 freemsg(mp0);
3951 freemsg(mp2);
3952 *mpp = NULL;
3953 return (NULL);
3954 }
3955 }
3956 *mpp = mp2;
3957 return (mp0);
3958 }
3959 /* Decrement len by the amount we just got. */
3960 len -= mp2->b_wptr - mp2->b_rptr;
3961 }
3962 /*
3963 * len should be reduced to zero now. If not our caller has
3964 * screwed up.
3965 */
3966 if (len) {
3967 /* Shouldn't happen! */
3968 freemsg(mp0);
3969 *mpp = NULL;
3970 return (NULL);
3971 }
3972 /*
3973 * We consumed up to exactly the end of an mblk. Detach the part
3974 * we are returning from the rest of the chain.
3975 */
3976 mp1->b_cont = NULL;
3977 *mpp = mp2;
3978 return (mp0);
3979 }
3980
3981 /* The ill stream is being unplumbed. Called from ip_close */
3982 int
3983 ip_modclose(ill_t *ill)
3984 {
3985 boolean_t success;
3986 ipsq_t *ipsq;
3987 ipif_t *ipif;
3988 queue_t *q = ill->ill_rq;
3989 ip_stack_t *ipst = ill->ill_ipst;
3990 int i;
3991 arl_ill_common_t *ai = ill->ill_common;
3992
3993 /*
3994 * The punlink prior to this may have initiated a capability
3995 * negotiation. But ipsq_enter will block until that finishes or
3996 * times out.
3997 */
3998 success = ipsq_enter(ill, B_FALSE, NEW_OP);
3999
4000 /*
4001 * Open/close/push/pop is guaranteed to be single threaded
4002 * per stream by STREAMS. FS guarantees that all references
4003 * from top are gone before close is called. So there can't
4004 * be another close thread that has set CONDEMNED on this ill.
4005 * and cause ipsq_enter to return failure.
4006 */
4007 ASSERT(success);
4008 ipsq = ill->ill_phyint->phyint_ipsq;
4009
4010 /*
4011 * Mark it condemned. No new reference will be made to this ill.
4012 * Lookup functions will return an error. Threads that try to
4013 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
4014 * that the refcnt will drop down to zero.
4015 */
4016 mutex_enter(&ill->ill_lock);
4017 ill->ill_state_flags |= ILL_CONDEMNED;
4018 for (ipif = ill->ill_ipif; ipif != NULL;
4019 ipif = ipif->ipif_next) {
4020 ipif->ipif_state_flags |= IPIF_CONDEMNED;
4021 }
4022 /*
4023 * Wake up anybody waiting to enter the ipsq. ipsq_enter
4024 * returns error if ILL_CONDEMNED is set
4025 */
4026 cv_broadcast(&ill->ill_cv);
4027 mutex_exit(&ill->ill_lock);
4028
4029 /*
4030 * Send all the deferred DLPI messages downstream which came in
4031 * during the small window right before ipsq_enter(). We do this
4032 * without waiting for the ACKs because all the ACKs for M_PROTO
4033 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
4034 */
4035 ill_dlpi_send_deferred(ill);
4036
4037 /*
4038 * Shut down fragmentation reassembly.
4039 * ill_frag_timer won't start a timer again.
4040 * Now cancel any existing timer
4041 */
4042 (void) untimeout(ill->ill_frag_timer_id);
4043 (void) ill_frag_timeout(ill, 0);
4044
4045 /*
4046 * Call ill_delete to bring down the ipifs, ilms and ill on
4047 * this ill. Then wait for the refcnts to drop to zero.
4048 * ill_is_freeable checks whether the ill is really quiescent.
4049 * Then make sure that threads that are waiting to enter the
4050 * ipsq have seen the error returned by ipsq_enter and have
4051 * gone away. Then we call ill_delete_tail which does the
4052 * DL_UNBIND_REQ with the driver and then qprocsoff.
4053 */
4054 ill_delete(ill);
4055 mutex_enter(&ill->ill_lock);
4056 while (!ill_is_freeable(ill))
4057 cv_wait(&ill->ill_cv, &ill->ill_lock);
4058
4059 while (ill->ill_waiters)
4060 cv_wait(&ill->ill_cv, &ill->ill_lock);
4061
4062 mutex_exit(&ill->ill_lock);
4063
4064 /*
4065 * ill_delete_tail drops reference on ill_ipst, but we need to keep
4066 * it held until the end of the function since the cleanup
4067 * below needs to be able to use the ip_stack_t.
4068 */
4069 netstack_hold(ipst->ips_netstack);
4070
4071 /* qprocsoff is done via ill_delete_tail */
4072 ill_delete_tail(ill);
4073 /*
4074 * synchronously wait for arp stream to unbind. After this, we
4075 * cannot get any data packets up from the driver.
4076 */
4077 arp_unbind_complete(ill);
4078 ASSERT(ill->ill_ipst == NULL);
4079
4080 /*
4081 * Walk through all conns and qenable those that have queued data.
4082 * Close synchronization needs this to
4083 * be done to ensure that all upper layers blocked
4084 * due to flow control to the closing device
4085 * get unblocked.
4086 */
4087 ip1dbg(("ip_wsrv: walking\n"));
4088 for (i = 0; i < TX_FANOUT_SIZE; i++) {
4089 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
4090 }
4091
4092 /*
4093 * ai can be null if this is an IPv6 ill, or if the IPv4
4094 * stream is being torn down before ARP was plumbed (e.g.,
4095 * /sbin/ifconfig plumbing a stream twice, and encountering
4096 * an error
4097 */
4098 if (ai != NULL) {
4099 ASSERT(!ill->ill_isv6);
4100 mutex_enter(&ai->ai_lock);
4101 ai->ai_ill = NULL;
4102 if (ai->ai_arl == NULL) {
4103 mutex_destroy(&ai->ai_lock);
4104 kmem_free(ai, sizeof (*ai));
4105 } else {
4106 cv_signal(&ai->ai_ill_unplumb_done);
4107 mutex_exit(&ai->ai_lock);
4108 }
4109 }
4110
4111 mutex_enter(&ipst->ips_ip_mi_lock);
4112 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
4113 mutex_exit(&ipst->ips_ip_mi_lock);
4114
4115 /*
4116 * credp could be null if the open didn't succeed and ip_modopen
4117 * itself calls ip_close.
4118 */
4119 if (ill->ill_credp != NULL)
4120 crfree(ill->ill_credp);
4121
4122 mutex_destroy(&ill->ill_saved_ire_lock);
4123 mutex_destroy(&ill->ill_lock);
4124 rw_destroy(&ill->ill_mcast_lock);
4125 mutex_destroy(&ill->ill_mcast_serializer);
4126 list_destroy(&ill->ill_nce);
4127 cv_destroy(&ill->ill_dlpi_capab_cv);
4128 mutex_destroy(&ill->ill_dlpi_capab_lock);
4129
4130 /*
4131 * Now we are done with the module close pieces that
4132 * need the netstack_t.
4133 */
4134 netstack_rele(ipst->ips_netstack);
4135
4136 mi_close_free((IDP)ill);
4137 q->q_ptr = WR(q)->q_ptr = NULL;
4138
4139 ipsq_exit(ipsq);
4140
4141 return (0);
4142 }
4143
4144 /*
4145 * This is called as part of close() for IP, UDP, ICMP, and RTS
4146 * in order to quiesce the conn.
4147 */
4148 void
4149 ip_quiesce_conn(conn_t *connp)
4150 {
4151 boolean_t drain_cleanup_reqd = B_FALSE;
4152 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
4153 boolean_t ilg_cleanup_reqd = B_FALSE;
4154 ip_stack_t *ipst;
4155
4156 ASSERT(!IPCL_IS_TCP(connp));
4157 ipst = connp->conn_netstack->netstack_ip;
4158
4159 /*
4160 * Mark the conn as closing, and this conn must not be
4161 * inserted in future into any list. Eg. conn_drain_insert(),
4162 * won't insert this conn into the conn_drain_list.
4163 *
4164 * conn_idl, and conn_ilg cannot get set henceforth.
4165 */
4166 mutex_enter(&connp->conn_lock);
4167 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4168 connp->conn_state_flags |= CONN_CLOSING;
4169 if (connp->conn_idl != NULL)
4170 drain_cleanup_reqd = B_TRUE;
4171 if (connp->conn_oper_pending_ill != NULL)
4172 conn_ioctl_cleanup_reqd = B_TRUE;
4173 if (connp->conn_dhcpinit_ill != NULL) {
4174 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4175 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4176 ill_set_inputfn(connp->conn_dhcpinit_ill);
4177 connp->conn_dhcpinit_ill = NULL;
4178 }
4179 if (connp->conn_ilg != NULL)
4180 ilg_cleanup_reqd = B_TRUE;
4181 mutex_exit(&connp->conn_lock);
4182
4183 if (conn_ioctl_cleanup_reqd)
4184 conn_ioctl_cleanup(connp);
4185
4186 if (is_system_labeled() && connp->conn_anon_port) {
4187 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4188 connp->conn_mlp_type, connp->conn_proto,
4189 ntohs(connp->conn_lport), B_FALSE);
4190 connp->conn_anon_port = 0;
4191 }
4192 connp->conn_mlp_type = mlptSingle;
4193
4194 /*
4195 * Remove this conn from any fanout list it is on.
4196 * and then wait for any threads currently operating
4197 * on this endpoint to finish
4198 */
4199 ipcl_hash_remove(connp);
4200
4201 /*
4202 * Remove this conn from the drain list, and do any other cleanup that
4203 * may be required. (TCP conns are never flow controlled, and
4204 * conn_idl will be NULL.)
4205 */
4206 if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4207 idl_t *idl = connp->conn_idl;
4208
4209 mutex_enter(&idl->idl_lock);
4210 conn_drain(connp, B_TRUE);
4211 mutex_exit(&idl->idl_lock);
4212 }
4213
4214 if (connp == ipst->ips_ip_g_mrouter)
4215 (void) ip_mrouter_done(ipst);
4216
4217 if (ilg_cleanup_reqd)
4218 ilg_delete_all(connp);
4219
4220 /*
4221 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4222 * callers from write side can't be there now because close
4223 * is in progress. The only other caller is ipcl_walk
4224 * which checks for the condemned flag.
4225 */
4226 mutex_enter(&connp->conn_lock);
4227 connp->conn_state_flags |= CONN_CONDEMNED;
4228 while (connp->conn_ref != 1)
4229 cv_wait(&connp->conn_cv, &connp->conn_lock);
4230 connp->conn_state_flags |= CONN_QUIESCED;
4231 mutex_exit(&connp->conn_lock);
4232 }
4233
4234 /* ARGSUSED */
4235 int
4236 ip_close(queue_t *q, int flags, cred_t *credp __unused)
4237 {
4238 conn_t *connp;
4239
4240 /*
4241 * Call the appropriate delete routine depending on whether this is
4242 * a module or device.
4243 */
4244 if (WR(q)->q_next != NULL) {
4245 /* This is a module close */
4246 return (ip_modclose((ill_t *)q->q_ptr));
4247 }
4248
4249 connp = q->q_ptr;
4250 ip_quiesce_conn(connp);
4251
4252 qprocsoff(q);
4253
4254 /*
4255 * Now we are truly single threaded on this stream, and can
4256 * delete the things hanging off the connp, and finally the connp.
4257 * We removed this connp from the fanout list, it cannot be
4258 * accessed thru the fanouts, and we already waited for the
4259 * conn_ref to drop to 0. We are already in close, so
4260 * there cannot be any other thread from the top. qprocsoff
4261 * has completed, and service has completed or won't run in
4262 * future.
4263 */
4264 ASSERT(connp->conn_ref == 1);
4265
4266 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4267
4268 connp->conn_ref--;
4269 ipcl_conn_destroy(connp);
4270
4271 q->q_ptr = WR(q)->q_ptr = NULL;
4272 return (0);
4273 }
4274
4275 /*
4276 * Wapper around putnext() so that ip_rts_request can merely use
4277 * conn_recv.
4278 */
4279 /*ARGSUSED2*/
4280 static void
4281 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4282 {
4283 conn_t *connp = (conn_t *)arg1;
4284
4285 putnext(connp->conn_rq, mp);
4286 }
4287
4288 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4289 /* ARGSUSED */
4290 static void
4291 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4292 {
4293 freemsg(mp);
4294 }
4295
4296 /*
4297 * Called when the module is about to be unloaded
4298 */
4299 void
4300 ip_ddi_destroy(void)
4301 {
4302 /* This needs to be called before destroying any transports. */
4303 mutex_enter(&cpu_lock);
4304 unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4305 mutex_exit(&cpu_lock);
4306
4307 tnet_fini();
4308
4309 icmp_ddi_g_destroy();
4310 rts_ddi_g_destroy();
4311 udp_ddi_g_destroy();
4312 sctp_ddi_g_destroy();
4313 tcp_ddi_g_destroy();
4314 ilb_ddi_g_destroy();
4315 dce_g_destroy();
4316 ipsec_policy_g_destroy();
4317 ipcl_g_destroy();
4318 ip_net_g_destroy();
4319 ip_ire_g_fini();
4320 inet_minor_destroy(ip_minor_arena_sa);
4321 #if defined(_LP64)
4322 inet_minor_destroy(ip_minor_arena_la);
4323 #endif
4324
4325 #ifdef DEBUG
4326 list_destroy(&ip_thread_list);
4327 rw_destroy(&ip_thread_rwlock);
4328 tsd_destroy(&ip_thread_data);
4329 #endif
4330
4331 netstack_unregister(NS_IP);
4332 }
4333
4334 /*
4335 * First step in cleanup.
4336 */
4337 /* ARGSUSED */
4338 static void
4339 ip_stack_shutdown(netstackid_t stackid, void *arg)
4340 {
4341 ip_stack_t *ipst = (ip_stack_t *)arg;
4342 kt_did_t ktid;
4343
4344 #ifdef NS_DEBUG
4345 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4346 #endif
4347
4348 /*
4349 * Perform cleanup for special interfaces (loopback and IPMP).
4350 */
4351 ip_interface_cleanup(ipst);
4352
4353 /*
4354 * The *_hook_shutdown()s start the process of notifying any
4355 * consumers that things are going away.... nothing is destroyed.
4356 */
4357 ipv4_hook_shutdown(ipst);
4358 ipv6_hook_shutdown(ipst);
4359 arp_hook_shutdown(ipst);
4360
4361 mutex_enter(&ipst->ips_capab_taskq_lock);
4362 ktid = ipst->ips_capab_taskq_thread->t_did;
4363 ipst->ips_capab_taskq_quit = B_TRUE;
4364 cv_signal(&ipst->ips_capab_taskq_cv);
4365 mutex_exit(&ipst->ips_capab_taskq_lock);
4366
4367 /*
4368 * In rare occurrences, particularly on virtual hardware where CPUs can
4369 * be de-scheduled, the thread that we just signaled will not run until
4370 * after we have gotten through parts of ip_stack_fini. If that happens
4371 * then we'll try to grab the ips_capab_taskq_lock as part of returning
4372 * from cv_wait which no longer exists.
4373 */
4374 thread_join(ktid);
4375 }
4376
4377 /*
4378 * Free the IP stack instance.
4379 */
4380 static void
4381 ip_stack_fini(netstackid_t stackid, void *arg)
4382 {
4383 ip_stack_t *ipst = (ip_stack_t *)arg;
4384 int ret;
4385
4386 #ifdef NS_DEBUG
4387 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4388 #endif
4389 /*
4390 * At this point, all of the notifications that the events and
4391 * protocols are going away have been run, meaning that we can
4392 * now set about starting to clean things up.
4393 */
4394 ipobs_fini(ipst);
4395 ipv4_hook_destroy(ipst);
4396 ipv6_hook_destroy(ipst);
4397 arp_hook_destroy(ipst);
4398 ip_net_destroy(ipst);
4399
4400 ipmp_destroy(ipst);
4401
4402 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4403 ipst->ips_ip_mibkp = NULL;
4404 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4405 ipst->ips_icmp_mibkp = NULL;
4406 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4407 ipst->ips_ip_kstat = NULL;
4408 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4409 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4410 ipst->ips_ip6_kstat = NULL;
4411 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4412
4413 kmem_free(ipst->ips_propinfo_tbl,
4414 ip_propinfo_count * sizeof (mod_prop_info_t));
4415 ipst->ips_propinfo_tbl = NULL;
4416
4417 dce_stack_destroy(ipst);
4418 ip_mrouter_stack_destroy(ipst);
4419
4420 /*
4421 * Quiesce all of our timers. Note we set the quiesce flags before we
4422 * call untimeout. The slowtimers may actually kick off another instance
4423 * of the non-slow timers.
4424 */
4425 mutex_enter(&ipst->ips_igmp_timer_lock);
4426 ipst->ips_igmp_timer_quiesce = B_TRUE;
4427 mutex_exit(&ipst->ips_igmp_timer_lock);
4428
4429 mutex_enter(&ipst->ips_mld_timer_lock);
4430 ipst->ips_mld_timer_quiesce = B_TRUE;
4431 mutex_exit(&ipst->ips_mld_timer_lock);
4432
4433 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
4434 ipst->ips_igmp_slowtimeout_quiesce = B_TRUE;
4435 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
4436
4437 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
4438 ipst->ips_mld_slowtimeout_quiesce = B_TRUE;
4439 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
4440
4441 ret = untimeout(ipst->ips_igmp_timeout_id);
4442 if (ret == -1) {
4443 ASSERT(ipst->ips_igmp_timeout_id == 0);
4444 } else {
4445 ASSERT(ipst->ips_igmp_timeout_id != 0);
4446 ipst->ips_igmp_timeout_id = 0;
4447 }
4448 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4449 if (ret == -1) {
4450 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4451 } else {
4452 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4453 ipst->ips_igmp_slowtimeout_id = 0;
4454 }
4455 ret = untimeout(ipst->ips_mld_timeout_id);
4456 if (ret == -1) {
4457 ASSERT(ipst->ips_mld_timeout_id == 0);
4458 } else {
4459 ASSERT(ipst->ips_mld_timeout_id != 0);
4460 ipst->ips_mld_timeout_id = 0;
4461 }
4462 ret = untimeout(ipst->ips_mld_slowtimeout_id);
4463 if (ret == -1) {
4464 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4465 } else {
4466 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4467 ipst->ips_mld_slowtimeout_id = 0;
4468 }
4469
4470 ip_ire_fini(ipst);
4471 ip6_asp_free(ipst);
4472 conn_drain_fini(ipst);
4473 ipcl_destroy(ipst);
4474
4475 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4476 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4477 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4478 ipst->ips_ndp4 = NULL;
4479 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4480 ipst->ips_ndp6 = NULL;
4481
4482 if (ipst->ips_loopback_ksp != NULL) {
4483 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4484 ipst->ips_loopback_ksp = NULL;
4485 }
4486
4487 mutex_destroy(&ipst->ips_capab_taskq_lock);
4488 cv_destroy(&ipst->ips_capab_taskq_cv);
4489
4490 rw_destroy(&ipst->ips_srcid_lock);
4491
4492 mutex_destroy(&ipst->ips_ip_mi_lock);
4493 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4494
4495 mutex_destroy(&ipst->ips_igmp_timer_lock);
4496 mutex_destroy(&ipst->ips_mld_timer_lock);
4497 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4498 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4499 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4500 rw_destroy(&ipst->ips_ill_g_lock);
4501
4502 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4503 ipst->ips_phyint_g_list = NULL;
4504 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4505 ipst->ips_ill_g_heads = NULL;
4506
4507 ldi_ident_release(ipst->ips_ldi_ident);
4508 kmem_free(ipst, sizeof (*ipst));
4509 }
4510
4511 /*
4512 * This function is called from the TSD destructor, and is used to debug
4513 * reference count issues in IP. See block comment in <inet/ip_if.h> for
4514 * details.
4515 */
4516 static void
4517 ip_thread_exit(void *phash)
4518 {
4519 th_hash_t *thh = phash;
4520
4521 rw_enter(&ip_thread_rwlock, RW_WRITER);
4522 list_remove(&ip_thread_list, thh);
4523 rw_exit(&ip_thread_rwlock);
4524 mod_hash_destroy_hash(thh->thh_hash);
4525 kmem_free(thh, sizeof (*thh));
4526 }
4527
4528 /*
4529 * Called when the IP kernel module is loaded into the kernel
4530 */
4531 void
4532 ip_ddi_init(void)
4533 {
4534 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4535
4536 /*
4537 * For IP and TCP the minor numbers should start from 2 since we have 4
4538 * initial devices: ip, ip6, tcp, tcp6.
4539 */
4540 /*
4541 * If this is a 64-bit kernel, then create two separate arenas -
4542 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4543 * other for socket apps in the range 2^^18 through 2^^32-1.
4544 */
4545 ip_minor_arena_la = NULL;
4546 ip_minor_arena_sa = NULL;
4547 #if defined(_LP64)
4548 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4549 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4550 cmn_err(CE_PANIC,
4551 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4552 }
4553 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4554 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4555 cmn_err(CE_PANIC,
4556 "ip_ddi_init: ip_minor_arena_la creation failed\n");
4557 }
4558 #else
4559 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4560 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4561 cmn_err(CE_PANIC,
4562 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4563 }
4564 #endif
4565 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4566
4567 ipcl_g_init();
4568 ip_ire_g_init();
4569 ip_net_g_init();
4570
4571 #ifdef DEBUG
4572 tsd_create(&ip_thread_data, ip_thread_exit);
4573 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4574 list_create(&ip_thread_list, sizeof (th_hash_t),
4575 offsetof(th_hash_t, thh_link));
4576 #endif
4577 ipsec_policy_g_init();
4578 tcp_ddi_g_init();
4579 sctp_ddi_g_init();
4580 dce_g_init();
4581
4582 /*
4583 * We want to be informed each time a stack is created or
4584 * destroyed in the kernel, so we can maintain the
4585 * set of udp_stack_t's.
4586 */
4587 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4588 ip_stack_fini);
4589
4590 tnet_init();
4591
4592 udp_ddi_g_init();
4593 rts_ddi_g_init();
4594 icmp_ddi_g_init();
4595 ilb_ddi_g_init();
4596
4597 /* This needs to be called after all transports are initialized. */
4598 mutex_enter(&cpu_lock);
4599 register_cpu_setup_func(ip_tp_cpu_update, NULL);
4600 mutex_exit(&cpu_lock);
4601 }
4602
4603 /*
4604 * Initialize the IP stack instance.
4605 */
4606 static void *
4607 ip_stack_init(netstackid_t stackid, netstack_t *ns)
4608 {
4609 ip_stack_t *ipst;
4610 size_t arrsz;
4611 major_t major;
4612
4613 #ifdef NS_DEBUG
4614 printf("ip_stack_init(stack %d)\n", stackid);
4615 #endif
4616
4617 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4618 ipst->ips_netstack = ns;
4619
4620 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4621 KM_SLEEP);
4622 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4623 KM_SLEEP);
4624 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4625 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4626 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4627 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4628
4629 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4630 ipst->ips_igmp_deferred_next = INFINITY;
4631 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4632 ipst->ips_mld_deferred_next = INFINITY;
4633 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4634 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4635 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4636 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4637 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4638 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4639
4640 ipcl_init(ipst);
4641 ip_ire_init(ipst);
4642 ip6_asp_init(ipst);
4643 ipif_init(ipst);
4644 conn_drain_init(ipst);
4645 ip_mrouter_stack_init(ipst);
4646 dce_stack_init(ipst);
4647
4648 ipst->ips_ip_multirt_log_interval = 1000;
4649
4650 ipst->ips_ill_index = 1;
4651
4652 ipst->ips_saved_ip_forwarding = -1;
4653 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
4654
4655 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4656 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4657 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4658
4659 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4660 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4661 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4662 ipst->ips_ip6_kstat =
4663 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4664
4665 ipst->ips_ip_src_id = 1;
4666 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4667
4668 ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4669
4670 ip_net_init(ipst, ns);
4671 ipv4_hook_init(ipst);
4672 ipv6_hook_init(ipst);
4673 arp_hook_init(ipst);
4674 ipmp_init(ipst);
4675 ipobs_init(ipst);
4676
4677 /*
4678 * Create the taskq dispatcher thread and initialize related stuff.
4679 */
4680 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4681 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4682 ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4683 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4684
4685 major = mod_name_to_major(INET_NAME);
4686 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4687 return (ipst);
4688 }
4689
4690 /*
4691 * Allocate and initialize a DLPI template of the specified length. (May be
4692 * called as writer.)
4693 */
4694 mblk_t *
4695 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4696 {
4697 mblk_t *mp;
4698
4699 mp = allocb(len, BPRI_MED);
4700 if (!mp)
4701 return (NULL);
4702
4703 /*
4704 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4705 * of which we don't seem to use) are sent with M_PCPROTO, and
4706 * that other DLPI are M_PROTO.
4707 */
4708 if (prim == DL_INFO_REQ) {
4709 mp->b_datap->db_type = M_PCPROTO;
4710 } else {
4711 mp->b_datap->db_type = M_PROTO;
4712 }
4713
4714 mp->b_wptr = mp->b_rptr + len;
4715 bzero(mp->b_rptr, len);
4716 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4717 return (mp);
4718 }
4719
4720 /*
4721 * Allocate and initialize a DLPI notification. (May be called as writer.)
4722 */
4723 mblk_t *
4724 ip_dlnotify_alloc(uint_t notification, uint_t data)
4725 {
4726 dl_notify_ind_t *notifyp;
4727 mblk_t *mp;
4728
4729 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4730 return (NULL);
4731
4732 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4733 notifyp->dl_notification = notification;
4734 notifyp->dl_data = data;
4735 return (mp);
4736 }
4737
4738 mblk_t *
4739 ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4740 {
4741 dl_notify_ind_t *notifyp;
4742 mblk_t *mp;
4743
4744 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4745 return (NULL);
4746
4747 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4748 notifyp->dl_notification = notification;
4749 notifyp->dl_data1 = data1;
4750 notifyp->dl_data2 = data2;
4751 return (mp);
4752 }
4753
4754 /*
4755 * Debug formatting routine. Returns a character string representation of the
4756 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
4757 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4758 *
4759 * Once the ndd table-printing interfaces are removed, this can be changed to
4760 * standard dotted-decimal form.
4761 */
4762 char *
4763 ip_dot_addr(ipaddr_t addr, char *buf)
4764 {
4765 uint8_t *ap = (uint8_t *)&addr;
4766
4767 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4768 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4769 return (buf);
4770 }
4771
4772 /*
4773 * Write the given MAC address as a printable string in the usual colon-
4774 * separated format.
4775 */
4776 const char *
4777 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4778 {
4779 char *bp;
4780
4781 if (alen == 0 || buflen < 4)
4782 return ("?");
4783 bp = buf;
4784 for (;;) {
4785 /*
4786 * If there are more MAC address bytes available, but we won't
4787 * have any room to print them, then add "..." to the string
4788 * instead. See below for the 'magic number' explanation.
4789 */
4790 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4791 (void) strcpy(bp, "...");
4792 break;
4793 }
4794 (void) sprintf(bp, "%02x", *addr++);
4795 bp += 2;
4796 if (--alen == 0)
4797 break;
4798 *bp++ = ':';
4799 buflen -= 3;
4800 /*
4801 * At this point, based on the first 'if' statement above,
4802 * either alen == 1 and buflen >= 3, or alen > 1 and
4803 * buflen >= 4. The first case leaves room for the final "xx"
4804 * number and trailing NUL byte. The second leaves room for at
4805 * least "...". Thus the apparently 'magic' numbers chosen for
4806 * that statement.
4807 */
4808 }
4809 return (buf);
4810 }
4811
4812 /*
4813 * Called when it is conceptually a ULP that would sent the packet
4814 * e.g., port unreachable and protocol unreachable. Check that the packet
4815 * would have passed the IPsec global policy before sending the error.
4816 *
4817 * Send an ICMP error after patching up the packet appropriately.
4818 * Uses ip_drop_input and bumps the appropriate MIB.
4819 */
4820 void
4821 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4822 ip_recv_attr_t *ira)
4823 {
4824 ipha_t *ipha;
4825 boolean_t secure;
4826 ill_t *ill = ira->ira_ill;
4827 ip_stack_t *ipst = ill->ill_ipst;
4828 netstack_t *ns = ipst->ips_netstack;
4829 ipsec_stack_t *ipss = ns->netstack_ipsec;
4830
4831 secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4832
4833 /*
4834 * We are generating an icmp error for some inbound packet.
4835 * Called from all ip_fanout_(udp, tcp, proto) functions.
4836 * Before we generate an error, check with global policy
4837 * to see whether this is allowed to enter the system. As
4838 * there is no "conn", we are checking with global policy.
4839 */
4840 ipha = (ipha_t *)mp->b_rptr;
4841 if (secure || ipss->ipsec_inbound_v4_policy_present) {
4842 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4843 if (mp == NULL)
4844 return;
4845 }
4846
4847 /* We never send errors for protocols that we do implement */
4848 if (ira->ira_protocol == IPPROTO_ICMP ||
4849 ira->ira_protocol == IPPROTO_IGMP) {
4850 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4851 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4852 freemsg(mp);
4853 return;
4854 }
4855 /*
4856 * Have to correct checksum since
4857 * the packet might have been
4858 * fragmented and the reassembly code in ip_rput
4859 * does not restore the IP checksum.
4860 */
4861 ipha->ipha_hdr_checksum = 0;
4862 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4863
4864 switch (icmp_type) {
4865 case ICMP_DEST_UNREACHABLE:
4866 switch (icmp_code) {
4867 case ICMP_PROTOCOL_UNREACHABLE:
4868 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4869 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4870 break;
4871 case ICMP_PORT_UNREACHABLE:
4872 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4873 ip_drop_input("ipIfStatsNoPorts", mp, ill);
4874 break;
4875 }
4876
4877 icmp_unreachable(mp, icmp_code, ira);
4878 break;
4879 default:
4880 #ifdef DEBUG
4881 panic("ip_fanout_send_icmp_v4: wrong type");
4882 /*NOTREACHED*/
4883 #else
4884 freemsg(mp);
4885 break;
4886 #endif
4887 }
4888 }
4889
4890 /*
4891 * Used to send an ICMP error message when a packet is received for
4892 * a protocol that is not supported. The mblk passed as argument
4893 * is consumed by this function.
4894 */
4895 void
4896 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4897 {
4898 ipha_t *ipha;
4899
4900 ipha = (ipha_t *)mp->b_rptr;
4901 if (ira->ira_flags & IRAF_IS_IPV4) {
4902 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4903 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4904 ICMP_PROTOCOL_UNREACHABLE, ira);
4905 } else {
4906 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4907 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4908 ICMP6_PARAMPROB_NEXTHEADER, ira);
4909 }
4910 }
4911
4912 /*
4913 * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4914 * Handles IPv4 and IPv6.
4915 * We are responsible for disposing of mp, such as by freemsg() or putnext()
4916 * Caller is responsible for dropping references to the conn.
4917 */
4918 void
4919 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4920 ip_recv_attr_t *ira)
4921 {
4922 ill_t *ill = ira->ira_ill;
4923 ip_stack_t *ipst = ill->ill_ipst;
4924 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4925 boolean_t secure;
4926 uint_t protocol = ira->ira_protocol;
4927 iaflags_t iraflags = ira->ira_flags;
4928 queue_t *rq;
4929
4930 secure = iraflags & IRAF_IPSEC_SECURE;
4931
4932 rq = connp->conn_rq;
4933 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4934 switch (protocol) {
4935 case IPPROTO_ICMPV6:
4936 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4937 break;
4938 case IPPROTO_ICMP:
4939 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4940 break;
4941 default:
4942 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4943 break;
4944 }
4945 freemsg(mp);
4946 return;
4947 }
4948
4949 ASSERT(!(IPCL_IS_IPTUN(connp)));
4950
4951 if (((iraflags & IRAF_IS_IPV4) ?
4952 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4953 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4954 secure) {
4955 mp = ipsec_check_inbound_policy(mp, connp, ipha,
4956 ip6h, ira);
4957 if (mp == NULL) {
4958 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4959 /* Note that mp is NULL */
4960 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4961 return;
4962 }
4963 }
4964
4965 if (iraflags & IRAF_ICMP_ERROR) {
4966 (connp->conn_recvicmp)(connp, mp, NULL, ira);
4967 } else {
4968 ill_t *rill = ira->ira_rill;
4969
4970 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4971 ira->ira_ill = ira->ira_rill = NULL;
4972 /* Send it upstream */
4973 (connp->conn_recv)(connp, mp, NULL, ira);
4974 ira->ira_ill = ill;
4975 ira->ira_rill = rill;
4976 }
4977 }
4978
4979 /*
4980 * Handle protocols with which IP is less intimate. There
4981 * can be more than one stream bound to a particular
4982 * protocol. When this is the case, normally each one gets a copy
4983 * of any incoming packets.
4984 *
4985 * IPsec NOTE :
4986 *
4987 * Don't allow a secure packet going up a non-secure connection.
4988 * We don't allow this because
4989 *
4990 * 1) Reply might go out in clear which will be dropped at
4991 * the sending side.
4992 * 2) If the reply goes out in clear it will give the
4993 * adversary enough information for getting the key in
4994 * most of the cases.
4995 *
4996 * Moreover getting a secure packet when we expect clear
4997 * implies that SA's were added without checking for
4998 * policy on both ends. This should not happen once ISAKMP
4999 * is used to negotiate SAs as SAs will be added only after
5000 * verifying the policy.
5001 *
5002 * Zones notes:
5003 * Earlier in ip_input on a system with multiple shared-IP zones we
5004 * duplicate the multicast and broadcast packets and send them up
5005 * with each explicit zoneid that exists on that ill.
5006 * This means that here we can match the zoneid with SO_ALLZONES being special.
5007 */
5008 void
5009 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
5010 {
5011 mblk_t *mp1;
5012 ipaddr_t laddr;
5013 conn_t *connp, *first_connp, *next_connp;
5014 connf_t *connfp;
5015 ill_t *ill = ira->ira_ill;
5016 ip_stack_t *ipst = ill->ill_ipst;
5017
5018 laddr = ipha->ipha_dst;
5019
5020 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
5021 mutex_enter(&connfp->connf_lock);
5022 connp = connfp->connf_head;
5023 for (connp = connfp->connf_head; connp != NULL;
5024 connp = connp->conn_next) {
5025 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5026 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5027 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5028 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) {
5029 break;
5030 }
5031 }
5032
5033 if (connp == NULL) {
5034 /*
5035 * No one bound to these addresses. Is
5036 * there a client that wants all
5037 * unclaimed datagrams?
5038 */
5039 mutex_exit(&connfp->connf_lock);
5040 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
5041 ICMP_PROTOCOL_UNREACHABLE, ira);
5042 return;
5043 }
5044
5045 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5046
5047 CONN_INC_REF(connp);
5048 first_connp = connp;
5049 connp = connp->conn_next;
5050
5051 for (;;) {
5052 while (connp != NULL) {
5053 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5054 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5055 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5056 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5057 ira, connp)))
5058 break;
5059 connp = connp->conn_next;
5060 }
5061
5062 if (connp == NULL) {
5063 /* No more interested clients */
5064 connp = first_connp;
5065 break;
5066 }
5067 if (((mp1 = dupmsg(mp)) == NULL) &&
5068 ((mp1 = copymsg(mp)) == NULL)) {
5069 /* Memory allocation failed */
5070 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5071 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5072 connp = first_connp;
5073 break;
5074 }
5075
5076 CONN_INC_REF(connp);
5077 mutex_exit(&connfp->connf_lock);
5078
5079 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
5080 ira);
5081
5082 mutex_enter(&connfp->connf_lock);
5083 /* Follow the next pointer before releasing the conn. */
5084 next_connp = connp->conn_next;
5085 CONN_DEC_REF(connp);
5086 connp = next_connp;
5087 }
5088
5089 /* Last one. Send it upstream. */
5090 mutex_exit(&connfp->connf_lock);
5091
5092 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
5093
5094 CONN_DEC_REF(connp);
5095 }
5096
5097 /*
5098 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
5099 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk
5100 * is not consumed.
5101 *
5102 * One of three things can happen, all of which affect the passed-in mblk:
5103 *
5104 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk..
5105 *
5106 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
5107 * ESP packet, and is passed along to ESP for consumption. Return NULL.
5108 *
5109 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL.
5110 */
5111 mblk_t *
5112 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
5113 {
5114 int shift, plen, iph_len;
5115 ipha_t *ipha;
5116 udpha_t *udpha;
5117 uint32_t *spi;
5118 uint32_t esp_ports;
5119 uint8_t *orptr;
5120 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
5121 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5122
5123 ipha = (ipha_t *)mp->b_rptr;
5124 iph_len = ira->ira_ip_hdr_length;
5125 plen = ira->ira_pktlen;
5126
5127 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
5128 /*
5129 * Most likely a keepalive for the benefit of an intervening
5130 * NAT. These aren't for us, per se, so drop it.
5131 *
5132 * RFC 3947/8 doesn't say for sure what to do for 2-3
5133 * byte packets (keepalives are 1-byte), but we'll drop them
5134 * also.
5135 */
5136 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5137 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
5138 return (NULL);
5139 }
5140
5141 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
5142 /* might as well pull it all up - it might be ESP. */
5143 if (!pullupmsg(mp, -1)) {
5144 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5145 DROPPER(ipss, ipds_esp_nomem),
5146 &ipss->ipsec_dropper);
5147 return (NULL);
5148 }
5149
5150 ipha = (ipha_t *)mp->b_rptr;
5151 }
5152 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
5153 if (*spi == 0) {
5154 /* UDP packet - remove 0-spi. */
5155 shift = sizeof (uint32_t);
5156 } else {
5157 /* ESP-in-UDP packet - reduce to ESP. */
5158 ipha->ipha_protocol = IPPROTO_ESP;
5159 shift = sizeof (udpha_t);
5160 }
5161
5162 /* Fix IP header */
5163 ira->ira_pktlen = (plen - shift);
5164 ipha->ipha_length = htons(ira->ira_pktlen);
5165 ipha->ipha_hdr_checksum = 0;
5166
5167 orptr = mp->b_rptr;
5168 mp->b_rptr += shift;
5169
5170 udpha = (udpha_t *)(orptr + iph_len);
5171 if (*spi == 0) {
5172 ASSERT((uint8_t *)ipha == orptr);
5173 udpha->uha_length = htons(plen - shift - iph_len);
5174 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
5175 esp_ports = 0;
5176 } else {
5177 esp_ports = *((uint32_t *)udpha);
5178 ASSERT(esp_ports != 0);
5179 }
5180 ovbcopy(orptr, orptr + shift, iph_len);
5181 if (esp_ports != 0) /* Punt up for ESP processing. */ {
5182 ipha = (ipha_t *)(orptr + shift);
5183
5184 ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5185 ira->ira_esp_udp_ports = esp_ports;
5186 ip_fanout_v4(mp, ipha, ira);
5187 return (NULL);
5188 }
5189 return (mp);
5190 }
5191
5192 /*
5193 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5194 * Handles IPv4 and IPv6.
5195 * We are responsible for disposing of mp, such as by freemsg() or putnext()
5196 * Caller is responsible for dropping references to the conn.
5197 */
5198 void
5199 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5200 ip_recv_attr_t *ira)
5201 {
5202 ill_t *ill = ira->ira_ill;
5203 ip_stack_t *ipst = ill->ill_ipst;
5204 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5205 boolean_t secure;
5206 iaflags_t iraflags = ira->ira_flags;
5207
5208 secure = iraflags & IRAF_IPSEC_SECURE;
5209
5210 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5211 !canputnext(connp->conn_rq)) {
5212 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5213 freemsg(mp);
5214 return;
5215 }
5216
5217 if (((iraflags & IRAF_IS_IPV4) ?
5218 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5219 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5220 secure) {
5221 mp = ipsec_check_inbound_policy(mp, connp, ipha,
5222 ip6h, ira);
5223 if (mp == NULL) {
5224 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5225 /* Note that mp is NULL */
5226 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5227 return;
5228 }
5229 }
5230
5231 /*
5232 * Since this code is not used for UDP unicast we don't need a NAT_T
5233 * check. Only ip_fanout_v4 has that check.
5234 */
5235 if (ira->ira_flags & IRAF_ICMP_ERROR) {
5236 (connp->conn_recvicmp)(connp, mp, NULL, ira);
5237 } else {
5238 ill_t *rill = ira->ira_rill;
5239
5240 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5241 ira->ira_ill = ira->ira_rill = NULL;
5242 /* Send it upstream */
5243 (connp->conn_recv)(connp, mp, NULL, ira);
5244 ira->ira_ill = ill;
5245 ira->ira_rill = rill;
5246 }
5247 }
5248
5249 /*
5250 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5251 * (Unicast fanout is handled in ip_input_v4.)
5252 *
5253 * If SO_REUSEADDR is set all multicast and broadcast packets
5254 * will be delivered to all conns bound to the same port.
5255 *
5256 * If there is at least one matching AF_INET receiver, then we will
5257 * ignore any AF_INET6 receivers.
5258 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5259 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5260 * packets.
5261 *
5262 * Zones notes:
5263 * Earlier in ip_input on a system with multiple shared-IP zones we
5264 * duplicate the multicast and broadcast packets and send them up
5265 * with each explicit zoneid that exists on that ill.
5266 * This means that here we can match the zoneid with SO_ALLZONES being special.
5267 */
5268 void
5269 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5270 ip_recv_attr_t *ira)
5271 {
5272 ipaddr_t laddr;
5273 in6_addr_t v6faddr;
5274 conn_t *connp;
5275 connf_t *connfp;
5276 ipaddr_t faddr;
5277 ill_t *ill = ira->ira_ill;
5278 ip_stack_t *ipst = ill->ill_ipst;
5279
5280 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5281
5282 laddr = ipha->ipha_dst;
5283 faddr = ipha->ipha_src;
5284
5285 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5286 mutex_enter(&connfp->connf_lock);
5287 connp = connfp->connf_head;
5288
5289 /*
5290 * If SO_REUSEADDR has been set on the first we send the
5291 * packet to all clients that have joined the group and
5292 * match the port.
5293 */
5294 while (connp != NULL) {
5295 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5296 conn_wantpacket(connp, ira, ipha) &&
5297 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5298 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5299 break;
5300 connp = connp->conn_next;
5301 }
5302
5303 if (connp == NULL)
5304 goto notfound;
5305
5306 CONN_INC_REF(connp);
5307
5308 if (connp->conn_reuseaddr) {
5309 conn_t *first_connp = connp;
5310 conn_t *next_connp;
5311 mblk_t *mp1;
5312
5313 connp = connp->conn_next;
5314 for (;;) {
5315 while (connp != NULL) {
5316 if (IPCL_UDP_MATCH(connp, lport, laddr,
5317 fport, faddr) &&
5318 conn_wantpacket(connp, ira, ipha) &&
5319 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5320 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5321 ira, connp)))
5322 break;
5323 connp = connp->conn_next;
5324 }
5325 if (connp == NULL) {
5326 /* No more interested clients */
5327 connp = first_connp;
5328 break;
5329 }
5330 if (((mp1 = dupmsg(mp)) == NULL) &&
5331 ((mp1 = copymsg(mp)) == NULL)) {
5332 /* Memory allocation failed */
5333 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5334 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5335 connp = first_connp;
5336 break;
5337 }
5338 CONN_INC_REF(connp);
5339 mutex_exit(&connfp->connf_lock);
5340
5341 IP_STAT(ipst, ip_udp_fanmb);
5342 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5343 NULL, ira);
5344 mutex_enter(&connfp->connf_lock);
5345 /* Follow the next pointer before releasing the conn */
5346 next_connp = connp->conn_next;
5347 CONN_DEC_REF(connp);
5348 connp = next_connp;
5349 }
5350 }
5351
5352 /* Last one. Send it upstream. */
5353 mutex_exit(&connfp->connf_lock);
5354 IP_STAT(ipst, ip_udp_fanmb);
5355 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5356 CONN_DEC_REF(connp);
5357 return;
5358
5359 notfound:
5360 mutex_exit(&connfp->connf_lock);
5361 /*
5362 * IPv6 endpoints bound to multicast IPv4-mapped addresses
5363 * have already been matched above, since they live in the IPv4
5364 * fanout tables. This implies we only need to
5365 * check for IPv6 in6addr_any endpoints here.
5366 * Thus we compare using ipv6_all_zeros instead of the destination
5367 * address, except for the multicast group membership lookup which
5368 * uses the IPv4 destination.
5369 */
5370 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5371 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5372 mutex_enter(&connfp->connf_lock);
5373 connp = connfp->connf_head;
5374 /*
5375 * IPv4 multicast packet being delivered to an AF_INET6
5376 * in6addr_any endpoint.
5377 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5378 * and not conn_wantpacket_v6() since any multicast membership is
5379 * for an IPv4-mapped multicast address.
5380 */
5381 while (connp != NULL) {
5382 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5383 fport, v6faddr) &&
5384 conn_wantpacket(connp, ira, ipha) &&
5385 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5386 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5387 break;
5388 connp = connp->conn_next;
5389 }
5390
5391 if (connp == NULL) {
5392 /*
5393 * No one bound to this port. Is
5394 * there a client that wants all
5395 * unclaimed datagrams?
5396 */
5397 mutex_exit(&connfp->connf_lock);
5398
5399 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5400 NULL) {
5401 ASSERT(ira->ira_protocol == IPPROTO_UDP);
5402 ip_fanout_proto_v4(mp, ipha, ira);
5403 } else {
5404 /*
5405 * We used to attempt to send an icmp error here, but
5406 * since this is known to be a multicast packet
5407 * and we don't send icmp errors in response to
5408 * multicast, just drop the packet and give up sooner.
5409 */
5410 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5411 freemsg(mp);
5412 }
5413 return;
5414 }
5415 CONN_INC_REF(connp);
5416 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5417
5418 /*
5419 * If SO_REUSEADDR has been set on the first we send the
5420 * packet to all clients that have joined the group and
5421 * match the port.
5422 */
5423 if (connp->conn_reuseaddr) {
5424 conn_t *first_connp = connp;
5425 conn_t *next_connp;
5426 mblk_t *mp1;
5427
5428 connp = connp->conn_next;
5429 for (;;) {
5430 while (connp != NULL) {
5431 if (IPCL_UDP_MATCH_V6(connp, lport,
5432 ipv6_all_zeros, fport, v6faddr) &&
5433 conn_wantpacket(connp, ira, ipha) &&
5434 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5435 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5436 ira, connp)))
5437 break;
5438 connp = connp->conn_next;
5439 }
5440 if (connp == NULL) {
5441 /* No more interested clients */
5442 connp = first_connp;
5443 break;
5444 }
5445 if (((mp1 = dupmsg(mp)) == NULL) &&
5446 ((mp1 = copymsg(mp)) == NULL)) {
5447 /* Memory allocation failed */
5448 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5449 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5450 connp = first_connp;
5451 break;
5452 }
5453 CONN_INC_REF(connp);
5454 mutex_exit(&connfp->connf_lock);
5455
5456 IP_STAT(ipst, ip_udp_fanmb);
5457 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5458 NULL, ira);
5459 mutex_enter(&connfp->connf_lock);
5460 /* Follow the next pointer before releasing the conn */
5461 next_connp = connp->conn_next;
5462 CONN_DEC_REF(connp);
5463 connp = next_connp;
5464 }
5465 }
5466
5467 /* Last one. Send it upstream. */
5468 mutex_exit(&connfp->connf_lock);
5469 IP_STAT(ipst, ip_udp_fanmb);
5470 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5471 CONN_DEC_REF(connp);
5472 }
5473
5474 /*
5475 * Split an incoming packet's IPv4 options into the label and the other options.
5476 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5477 * clearing out any leftover label or options.
5478 * Otherwise it just makes ipp point into the packet.
5479 *
5480 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5481 */
5482 int
5483 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5484 {
5485 uchar_t *opt;
5486 uint32_t totallen;
5487 uint32_t optval;
5488 uint32_t optlen;
5489
5490 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5491 ipp->ipp_hoplimit = ipha->ipha_ttl;
5492 ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5493 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5494
5495 /*
5496 * Get length (in 4 byte octets) of IP header options.
5497 */
5498 totallen = ipha->ipha_version_and_hdr_length -
5499 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5500
5501 if (totallen == 0) {
5502 if (!allocate)
5503 return (0);
5504
5505 /* Clear out anything from a previous packet */
5506 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5507 kmem_free(ipp->ipp_ipv4_options,
5508 ipp->ipp_ipv4_options_len);
5509 ipp->ipp_ipv4_options = NULL;
5510 ipp->ipp_ipv4_options_len = 0;
5511 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5512 }
5513 if (ipp->ipp_fields & IPPF_LABEL_V4) {
5514 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5515 ipp->ipp_label_v4 = NULL;
5516 ipp->ipp_label_len_v4 = 0;
5517 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5518 }
5519 return (0);
5520 }
5521
5522 totallen <<= 2;
5523 opt = (uchar_t *)&ipha[1];
5524 if (!is_system_labeled()) {
5525
5526 copyall:
5527 if (!allocate) {
5528 if (totallen != 0) {
5529 ipp->ipp_ipv4_options = opt;
5530 ipp->ipp_ipv4_options_len = totallen;
5531 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5532 }
5533 return (0);
5534 }
5535 /* Just copy all of options */
5536 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5537 if (totallen == ipp->ipp_ipv4_options_len) {
5538 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5539 return (0);
5540 }
5541 kmem_free(ipp->ipp_ipv4_options,
5542 ipp->ipp_ipv4_options_len);
5543 ipp->ipp_ipv4_options = NULL;
5544 ipp->ipp_ipv4_options_len = 0;
5545 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5546 }
5547 if (totallen == 0)
5548 return (0);
5549
5550 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5551 if (ipp->ipp_ipv4_options == NULL)
5552 return (ENOMEM);
5553 ipp->ipp_ipv4_options_len = totallen;
5554 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5555 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5556 return (0);
5557 }
5558
5559 if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) {
5560 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5561 ipp->ipp_label_v4 = NULL;
5562 ipp->ipp_label_len_v4 = 0;
5563 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5564 }
5565
5566 /*
5567 * Search for CIPSO option.
5568 * We assume CIPSO is first in options if it is present.
5569 * If it isn't, then ipp_opt_ipv4_options will not include the options
5570 * prior to the CIPSO option.
5571 */
5572 while (totallen != 0) {
5573 switch (optval = opt[IPOPT_OPTVAL]) {
5574 case IPOPT_EOL:
5575 return (0);
5576 case IPOPT_NOP:
5577 optlen = 1;
5578 break;
5579 default:
5580 if (totallen <= IPOPT_OLEN)
5581 return (EINVAL);
5582 optlen = opt[IPOPT_OLEN];
5583 if (optlen < 2)
5584 return (EINVAL);
5585 }
5586 if (optlen > totallen)
5587 return (EINVAL);
5588
5589 switch (optval) {
5590 case IPOPT_COMSEC:
5591 if (!allocate) {
5592 ipp->ipp_label_v4 = opt;
5593 ipp->ipp_label_len_v4 = optlen;
5594 ipp->ipp_fields |= IPPF_LABEL_V4;
5595 } else {
5596 ipp->ipp_label_v4 = kmem_alloc(optlen,
5597 KM_NOSLEEP);
5598 if (ipp->ipp_label_v4 == NULL)
5599 return (ENOMEM);
5600 ipp->ipp_label_len_v4 = optlen;
5601 ipp->ipp_fields |= IPPF_LABEL_V4;
5602 bcopy(opt, ipp->ipp_label_v4, optlen);
5603 }
5604 totallen -= optlen;
5605 opt += optlen;
5606
5607 /* Skip padding bytes until we get to a multiple of 4 */
5608 while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) {
5609 totallen--;
5610 opt++;
5611 }
5612 /* Remaining as ipp_ipv4_options */
5613 goto copyall;
5614 }
5615 totallen -= optlen;
5616 opt += optlen;
5617 }
5618 /* No CIPSO found; return everything as ipp_ipv4_options */
5619 totallen = ipha->ipha_version_and_hdr_length -
5620 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5621 totallen <<= 2;
5622 opt = (uchar_t *)&ipha[1];
5623 goto copyall;
5624 }
5625
5626 /*
5627 * Efficient versions of lookup for an IRE when we only
5628 * match the address.
5629 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5630 * Does not handle multicast addresses.
5631 */
5632 uint_t
5633 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5634 {
5635 ire_t *ire;
5636 uint_t result;
5637
5638 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5639 ASSERT(ire != NULL);
5640 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5641 result = IRE_NOROUTE;
5642 else
5643 result = ire->ire_type;
5644 ire_refrele(ire);
5645 return (result);
5646 }
5647
5648 /*
5649 * Efficient versions of lookup for an IRE when we only
5650 * match the address.
5651 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5652 * Does not handle multicast addresses.
5653 */
5654 uint_t
5655 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5656 {
5657 ire_t *ire;
5658 uint_t result;
5659
5660 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5661 ASSERT(ire != NULL);
5662 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5663 result = IRE_NOROUTE;
5664 else
5665 result = ire->ire_type;
5666 ire_refrele(ire);
5667 return (result);
5668 }
5669
5670 /*
5671 * Nobody should be sending
5672 * packets up this stream
5673 */
5674 static int
5675 ip_lrput(queue_t *q, mblk_t *mp)
5676 {
5677 switch (mp->b_datap->db_type) {
5678 case M_FLUSH:
5679 /* Turn around */
5680 if (*mp->b_rptr & FLUSHW) {
5681 *mp->b_rptr &= ~FLUSHR;
5682 qreply(q, mp);
5683 return (0);
5684 }
5685 break;
5686 }
5687 freemsg(mp);
5688 return (0);
5689 }
5690
5691 /* Nobody should be sending packets down this stream */
5692 /* ARGSUSED */
5693 int
5694 ip_lwput(queue_t *q, mblk_t *mp)
5695 {
5696 freemsg(mp);
5697 return (0);
5698 }
5699
5700 /*
5701 * Move the first hop in any source route to ipha_dst and remove that part of
5702 * the source route. Called by other protocols. Errors in option formatting
5703 * are ignored - will be handled by ip_output_options. Return the final
5704 * destination (either ipha_dst or the last entry in a source route.)
5705 */
5706 ipaddr_t
5707 ip_massage_options(ipha_t *ipha, netstack_t *ns)
5708 {
5709 ipoptp_t opts;
5710 uchar_t *opt;
5711 uint8_t optval;
5712 uint8_t optlen;
5713 ipaddr_t dst;
5714 int i;
5715 ip_stack_t *ipst = ns->netstack_ip;
5716
5717 ip2dbg(("ip_massage_options\n"));
5718 dst = ipha->ipha_dst;
5719 for (optval = ipoptp_first(&opts, ipha);
5720 optval != IPOPT_EOL;
5721 optval = ipoptp_next(&opts)) {
5722 opt = opts.ipoptp_cur;
5723 switch (optval) {
5724 uint8_t off;
5725 case IPOPT_SSRR:
5726 case IPOPT_LSRR:
5727 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5728 ip1dbg(("ip_massage_options: bad src route\n"));
5729 break;
5730 }
5731 optlen = opts.ipoptp_len;
5732 off = opt[IPOPT_OFFSET];
5733 off--;
5734 redo_srr:
5735 if (optlen < IP_ADDR_LEN ||
5736 off > optlen - IP_ADDR_LEN) {
5737 /* End of source route */
5738 ip1dbg(("ip_massage_options: end of SR\n"));
5739 break;
5740 }
5741 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5742 ip1dbg(("ip_massage_options: next hop 0x%x\n",
5743 ntohl(dst)));
5744 /*
5745 * Check if our address is present more than
5746 * once as consecutive hops in source route.
5747 * XXX verify per-interface ip_forwarding
5748 * for source route?
5749 */
5750 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5751 off += IP_ADDR_LEN;
5752 goto redo_srr;
5753 }
5754 if (dst == htonl(INADDR_LOOPBACK)) {
5755 ip1dbg(("ip_massage_options: loopback addr in "
5756 "source route!\n"));
5757 break;
5758 }
5759 /*
5760 * Update ipha_dst to be the first hop and remove the
5761 * first hop from the source route (by overwriting
5762 * part of the option with NOP options).
5763 */
5764 ipha->ipha_dst = dst;
5765 /* Put the last entry in dst */
5766 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5767 3;
5768 bcopy(&opt[off], &dst, IP_ADDR_LEN);
5769
5770 ip1dbg(("ip_massage_options: last hop 0x%x\n",
5771 ntohl(dst)));
5772 /* Move down and overwrite */
5773 opt[IP_ADDR_LEN] = opt[0];
5774 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5775 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5776 for (i = 0; i < IP_ADDR_LEN; i++)
5777 opt[i] = IPOPT_NOP;
5778 break;
5779 }
5780 }
5781 return (dst);
5782 }
5783
5784 /*
5785 * Return the network mask
5786 * associated with the specified address.
5787 */
5788 ipaddr_t
5789 ip_net_mask(ipaddr_t addr)
5790 {
5791 uchar_t *up = (uchar_t *)&addr;
5792 ipaddr_t mask = 0;
5793 uchar_t *maskp = (uchar_t *)&mask;
5794
5795 #if defined(__i386) || defined(__amd64)
5796 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5797 #endif
5798 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
5799 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5800 #endif
5801 if (CLASSD(addr)) {
5802 maskp[0] = 0xF0;
5803 return (mask);
5804 }
5805
5806 /* We assume Class E default netmask to be 32 */
5807 if (CLASSE(addr))
5808 return (0xffffffffU);
5809
5810 if (addr == 0)
5811 return (0);
5812 maskp[0] = 0xFF;
5813 if ((up[0] & 0x80) == 0)
5814 return (mask);
5815
5816 maskp[1] = 0xFF;
5817 if ((up[0] & 0xC0) == 0x80)
5818 return (mask);
5819
5820 maskp[2] = 0xFF;
5821 if ((up[0] & 0xE0) == 0xC0)
5822 return (mask);
5823
5824 /* Otherwise return no mask */
5825 return ((ipaddr_t)0);
5826 }
5827
5828 /* Name/Value Table Lookup Routine */
5829 char *
5830 ip_nv_lookup(nv_t *nv, int value)
5831 {
5832 if (!nv)
5833 return (NULL);
5834 for (; nv->nv_name; nv++) {
5835 if (nv->nv_value == value)
5836 return (nv->nv_name);
5837 }
5838 return ("unknown");
5839 }
5840
5841 static int
5842 ip_wait_for_info_ack(ill_t *ill)
5843 {
5844 int err;
5845
5846 mutex_enter(&ill->ill_lock);
5847 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5848 /*
5849 * Return value of 0 indicates a pending signal.
5850 */
5851 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5852 if (err == 0) {
5853 mutex_exit(&ill->ill_lock);
5854 return (EINTR);
5855 }
5856 }
5857 mutex_exit(&ill->ill_lock);
5858 /*
5859 * ip_rput_other could have set an error in ill_error on
5860 * receipt of M_ERROR.
5861 */
5862 return (ill->ill_error);
5863 }
5864
5865 /*
5866 * This is a module open, i.e. this is a control stream for access
5867 * to a DLPI device. We allocate an ill_t as the instance data in
5868 * this case.
5869 */
5870 static int
5871 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5872 {
5873 ill_t *ill;
5874 int err;
5875 zoneid_t zoneid;
5876 netstack_t *ns;
5877 ip_stack_t *ipst;
5878
5879 /*
5880 * Prevent unprivileged processes from pushing IP so that
5881 * they can't send raw IP.
5882 */
5883 if (secpolicy_net_rawaccess(credp) != 0)
5884 return (EPERM);
5885
5886 ns = netstack_find_by_cred(credp);
5887 ASSERT(ns != NULL);
5888 ipst = ns->netstack_ip;
5889 ASSERT(ipst != NULL);
5890
5891 /*
5892 * For exclusive stacks we set the zoneid to zero
5893 * to make IP operate as if in the global zone.
5894 */
5895 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5896 zoneid = GLOBAL_ZONEID;
5897 else
5898 zoneid = crgetzoneid(credp);
5899
5900 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5901 q->q_ptr = WR(q)->q_ptr = ill;
5902 ill->ill_ipst = ipst;
5903 ill->ill_zoneid = zoneid;
5904
5905 /*
5906 * ill_init initializes the ill fields and then sends down
5907 * down a DL_INFO_REQ after calling qprocson.
5908 */
5909 err = ill_init(q, ill);
5910
5911 if (err != 0) {
5912 mi_free(ill);
5913 netstack_rele(ipst->ips_netstack);
5914 q->q_ptr = NULL;
5915 WR(q)->q_ptr = NULL;
5916 return (err);
5917 }
5918
5919 /*
5920 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5921 *
5922 * ill_init initializes the ipsq marking this thread as
5923 * writer
5924 */
5925 ipsq_exit(ill->ill_phyint->phyint_ipsq);
5926 err = ip_wait_for_info_ack(ill);
5927 if (err == 0)
5928 ill->ill_credp = credp;
5929 else
5930 goto fail;
5931
5932 crhold(credp);
5933
5934 mutex_enter(&ipst->ips_ip_mi_lock);
5935 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5936 sflag, credp);
5937 mutex_exit(&ipst->ips_ip_mi_lock);
5938 fail:
5939 if (err) {
5940 (void) ip_close(q, 0, credp);
5941 return (err);
5942 }
5943 return (0);
5944 }
5945
5946 /* For /dev/ip aka AF_INET open */
5947 int
5948 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5949 {
5950 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5951 }
5952
5953 /* For /dev/ip6 aka AF_INET6 open */
5954 int
5955 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5956 {
5957 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5958 }
5959
5960 /* IP open routine. */
5961 int
5962 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5963 boolean_t isv6)
5964 {
5965 conn_t *connp;
5966 major_t maj;
5967 zoneid_t zoneid;
5968 netstack_t *ns;
5969 ip_stack_t *ipst;
5970
5971 /* Allow reopen. */
5972 if (q->q_ptr != NULL)
5973 return (0);
5974
5975 if (sflag & MODOPEN) {
5976 /* This is a module open */
5977 return (ip_modopen(q, devp, flag, sflag, credp));
5978 }
5979
5980 if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5981 /*
5982 * Non streams based socket looking for a stream
5983 * to access IP
5984 */
5985 return (ip_helper_stream_setup(q, devp, flag, sflag,
5986 credp, isv6));
5987 }
5988
5989 ns = netstack_find_by_cred(credp);
5990 ASSERT(ns != NULL);
5991 ipst = ns->netstack_ip;
5992 ASSERT(ipst != NULL);
5993
5994 /*
5995 * For exclusive stacks we set the zoneid to zero
5996 * to make IP operate as if in the global zone.
5997 */
5998 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5999 zoneid = GLOBAL_ZONEID;
6000 else
6001 zoneid = crgetzoneid(credp);
6002
6003 /*
6004 * We are opening as a device. This is an IP client stream, and we
6005 * allocate an conn_t as the instance data.
6006 */
6007 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
6008
6009 /*
6010 * ipcl_conn_create did a netstack_hold. Undo the hold that was
6011 * done by netstack_find_by_cred()
6012 */
6013 netstack_rele(ipst->ips_netstack);
6014
6015 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
6016 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
6017 connp->conn_ixa->ixa_zoneid = zoneid;
6018 connp->conn_zoneid = zoneid;
6019
6020 connp->conn_rq = q;
6021 q->q_ptr = WR(q)->q_ptr = connp;
6022
6023 /* Minor tells us which /dev entry was opened */
6024 if (isv6) {
6025 connp->conn_family = AF_INET6;
6026 connp->conn_ipversion = IPV6_VERSION;
6027 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
6028 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
6029 } else {
6030 connp->conn_family = AF_INET;
6031 connp->conn_ipversion = IPV4_VERSION;
6032 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
6033 }
6034
6035 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
6036 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
6037 connp->conn_minor_arena = ip_minor_arena_la;
6038 } else {
6039 /*
6040 * Either minor numbers in the large arena were exhausted
6041 * or a non socket application is doing the open.
6042 * Try to allocate from the small arena.
6043 */
6044 if ((connp->conn_dev =
6045 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
6046 /* CONN_DEC_REF takes care of netstack_rele() */
6047 q->q_ptr = WR(q)->q_ptr = NULL;
6048 CONN_DEC_REF(connp);
6049 return (EBUSY);
6050 }
6051 connp->conn_minor_arena = ip_minor_arena_sa;
6052 }
6053
6054 maj = getemajor(*devp);
6055 *devp = makedevice(maj, (minor_t)connp->conn_dev);
6056
6057 /*
6058 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
6059 */
6060 connp->conn_cred = credp;
6061 connp->conn_cpid = curproc->p_pid;
6062 /* Cache things in ixa without an extra refhold */
6063 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
6064 connp->conn_ixa->ixa_cred = connp->conn_cred;
6065 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
6066 if (is_system_labeled())
6067 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6068
6069 /*
6070 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
6071 */
6072 connp->conn_recv = ip_conn_input;
6073 connp->conn_recvicmp = ip_conn_input_icmp;
6074
6075 crhold(connp->conn_cred);
6076
6077 /*
6078 * If the caller has the process-wide flag set, then default to MAC
6079 * exempt mode. This allows read-down to unlabeled hosts.
6080 */
6081 if (getpflags(NET_MAC_AWARE, credp) != 0)
6082 connp->conn_mac_mode = CONN_MAC_AWARE;
6083
6084 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
6085
6086 connp->conn_rq = q;
6087 connp->conn_wq = WR(q);
6088
6089 /* Non-zero default values */
6090 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
6091
6092 /*
6093 * Make the conn globally visible to walkers
6094 */
6095 ASSERT(connp->conn_ref == 1);
6096 mutex_enter(&connp->conn_lock);
6097 connp->conn_state_flags &= ~CONN_INCIPIENT;
6098 mutex_exit(&connp->conn_lock);
6099
6100 qprocson(q);
6101
6102 return (0);
6103 }
6104
6105 /*
6106 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
6107 * all of them are copied to the conn_t. If the req is "zero", the policy is
6108 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
6109 * fields.
6110 * We keep only the latest setting of the policy and thus policy setting
6111 * is not incremental/cumulative.
6112 *
6113 * Requests to set policies with multiple alternative actions will
6114 * go through a different API.
6115 */
6116 int
6117 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
6118 {
6119 uint_t ah_req = 0;
6120 uint_t esp_req = 0;
6121 uint_t se_req = 0;
6122 ipsec_act_t *actp = NULL;
6123 uint_t nact;
6124 ipsec_policy_head_t *ph;
6125 boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
6126 int error = 0;
6127 netstack_t *ns = connp->conn_netstack;
6128 ip_stack_t *ipst = ns->netstack_ip;
6129 ipsec_stack_t *ipss = ns->netstack_ipsec;
6130
6131 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
6132
6133 /*
6134 * The IP_SEC_OPT option does not allow variable length parameters,
6135 * hence a request cannot be NULL.
6136 */
6137 if (req == NULL)
6138 return (EINVAL);
6139
6140 ah_req = req->ipsr_ah_req;
6141 esp_req = req->ipsr_esp_req;
6142 se_req = req->ipsr_self_encap_req;
6143
6144 /* Don't allow setting self-encap without one or more of AH/ESP. */
6145 if (se_req != 0 && esp_req == 0 && ah_req == 0)
6146 return (EINVAL);
6147
6148 /*
6149 * Are we dealing with a request to reset the policy (i.e.
6150 * zero requests).
6151 */
6152 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
6153 (esp_req & REQ_MASK) == 0 &&
6154 (se_req & REQ_MASK) == 0);
6155
6156 if (!is_pol_reset) {
6157 /*
6158 * If we couldn't load IPsec, fail with "protocol
6159 * not supported".
6160 * IPsec may not have been loaded for a request with zero
6161 * policies, so we don't fail in this case.
6162 */
6163 mutex_enter(&ipss->ipsec_loader_lock);
6164 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
6165 mutex_exit(&ipss->ipsec_loader_lock);
6166 return (EPROTONOSUPPORT);
6167 }
6168 mutex_exit(&ipss->ipsec_loader_lock);
6169
6170 /*
6171 * Test for valid requests. Invalid algorithms
6172 * need to be tested by IPsec code because new
6173 * algorithms can be added dynamically.
6174 */
6175 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6176 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6177 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
6178 return (EINVAL);
6179 }
6180
6181 /*
6182 * Only privileged users can issue these
6183 * requests.
6184 */
6185 if (((ah_req & IPSEC_PREF_NEVER) ||
6186 (esp_req & IPSEC_PREF_NEVER) ||
6187 (se_req & IPSEC_PREF_NEVER)) &&
6188 secpolicy_ip_config(cr, B_FALSE) != 0) {
6189 return (EPERM);
6190 }
6191
6192 /*
6193 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
6194 * are mutually exclusive.
6195 */
6196 if (((ah_req & REQ_MASK) == REQ_MASK) ||
6197 ((esp_req & REQ_MASK) == REQ_MASK) ||
6198 ((se_req & REQ_MASK) == REQ_MASK)) {
6199 /* Both of them are set */
6200 return (EINVAL);
6201 }
6202 }
6203
6204 ASSERT(MUTEX_HELD(&connp->conn_lock));
6205
6206 /*
6207 * If we have already cached policies in conn_connect(), don't
6208 * let them change now. We cache policies for connections
6209 * whose src,dst [addr, port] is known.
6210 */
6211 if (connp->conn_policy_cached) {
6212 return (EINVAL);
6213 }
6214
6215 /*
6216 * We have a zero policies, reset the connection policy if already
6217 * set. This will cause the connection to inherit the
6218 * global policy, if any.
6219 */
6220 if (is_pol_reset) {
6221 if (connp->conn_policy != NULL) {
6222 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
6223 connp->conn_policy = NULL;
6224 }
6225 connp->conn_in_enforce_policy = B_FALSE;
6226 connp->conn_out_enforce_policy = B_FALSE;
6227 return (0);
6228 }
6229
6230 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
6231 ipst->ips_netstack);
6232 if (ph == NULL)
6233 goto enomem;
6234
6235 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
6236 if (actp == NULL)
6237 goto enomem;
6238
6239 /*
6240 * Always insert IPv4 policy entries, since they can also apply to
6241 * ipv6 sockets being used in ipv4-compat mode.
6242 */
6243 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6244 IPSEC_TYPE_INBOUND, ns))
6245 goto enomem;
6246 is_pol_inserted = B_TRUE;
6247 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6248 IPSEC_TYPE_OUTBOUND, ns))
6249 goto enomem;
6250
6251 /*
6252 * We're looking at a v6 socket, also insert the v6-specific
6253 * entries.
6254 */
6255 if (connp->conn_family == AF_INET6) {
6256 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6257 IPSEC_TYPE_INBOUND, ns))
6258 goto enomem;
6259 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6260 IPSEC_TYPE_OUTBOUND, ns))
6261 goto enomem;
6262 }
6263
6264 ipsec_actvec_free(actp, nact);
6265
6266 /*
6267 * If the requests need security, set enforce_policy.
6268 * If the requests are IPSEC_PREF_NEVER, one should
6269 * still set conn_out_enforce_policy so that ip_set_destination
6270 * marks the ip_xmit_attr_t appropriatly. This is needed so that
6271 * for connections that we don't cache policy in at connect time,
6272 * if global policy matches in ip_output_attach_policy, we
6273 * don't wrongly inherit global policy. Similarly, we need
6274 * to set conn_in_enforce_policy also so that we don't verify
6275 * policy wrongly.
6276 */
6277 if ((ah_req & REQ_MASK) != 0 ||
6278 (esp_req & REQ_MASK) != 0 ||
6279 (se_req & REQ_MASK) != 0) {
6280 connp->conn_in_enforce_policy = B_TRUE;
6281 connp->conn_out_enforce_policy = B_TRUE;
6282 }
6283
6284 return (error);
6285 #undef REQ_MASK
6286
6287 /*
6288 * Common memory-allocation-failure exit path.
6289 */
6290 enomem:
6291 if (actp != NULL)
6292 ipsec_actvec_free(actp, nact);
6293 if (is_pol_inserted)
6294 ipsec_polhead_flush(ph, ns);
6295 return (ENOMEM);
6296 }
6297
6298 /*
6299 * Set socket options for joining and leaving multicast groups.
6300 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6301 * The caller has already check that the option name is consistent with
6302 * the address family of the socket.
6303 */
6304 int
6305 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6306 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6307 {
6308 int *i1 = (int *)invalp;
6309 int error = 0;
6310 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6311 struct ip_mreq *v4_mreqp;
6312 struct ipv6_mreq *v6_mreqp;
6313 struct group_req *greqp;
6314 ire_t *ire;
6315 boolean_t done = B_FALSE;
6316 ipaddr_t ifaddr;
6317 in6_addr_t v6group;
6318 uint_t ifindex;
6319 boolean_t mcast_opt = B_TRUE;
6320 mcast_record_t fmode;
6321 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6322 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6323
6324 switch (name) {
6325 case IP_ADD_MEMBERSHIP:
6326 case IPV6_JOIN_GROUP:
6327 mcast_opt = B_FALSE;
6328 /* FALLTHROUGH */
6329 case MCAST_JOIN_GROUP:
6330 fmode = MODE_IS_EXCLUDE;
6331 optfn = ip_opt_add_group;
6332 break;
6333
6334 case IP_DROP_MEMBERSHIP:
6335 case IPV6_LEAVE_GROUP:
6336 mcast_opt = B_FALSE;
6337 /* FALLTHROUGH */
6338 case MCAST_LEAVE_GROUP:
6339 fmode = MODE_IS_INCLUDE;
6340 optfn = ip_opt_delete_group;
6341 break;
6342 default:
6343 ASSERT(0);
6344 }
6345
6346 if (mcast_opt) {
6347 struct sockaddr_in *sin;
6348 struct sockaddr_in6 *sin6;
6349
6350 greqp = (struct group_req *)i1;
6351 if (greqp->gr_group.ss_family == AF_INET) {
6352 sin = (struct sockaddr_in *)&(greqp->gr_group);
6353 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6354 } else {
6355 if (!inet6)
6356 return (EINVAL); /* Not on INET socket */
6357
6358 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6359 v6group = sin6->sin6_addr;
6360 }
6361 ifaddr = INADDR_ANY;
6362 ifindex = greqp->gr_interface;
6363 } else if (inet6) {
6364 v6_mreqp = (struct ipv6_mreq *)i1;
6365 v6group = v6_mreqp->ipv6mr_multiaddr;
6366 ifaddr = INADDR_ANY;
6367 ifindex = v6_mreqp->ipv6mr_interface;
6368 } else {
6369 v4_mreqp = (struct ip_mreq *)i1;
6370 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6371 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6372 ifindex = 0;
6373 }
6374
6375 /*
6376 * In the multirouting case, we need to replicate
6377 * the request on all interfaces that will take part
6378 * in replication. We do so because multirouting is
6379 * reflective, thus we will probably receive multi-
6380 * casts on those interfaces.
6381 * The ip_multirt_apply_membership() succeeds if
6382 * the operation succeeds on at least one interface.
6383 */
6384 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6385 ipaddr_t group;
6386
6387 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6388
6389 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6390 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6391 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6392 } else {
6393 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6394 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6395 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6396 }
6397 if (ire != NULL) {
6398 if (ire->ire_flags & RTF_MULTIRT) {
6399 error = ip_multirt_apply_membership(optfn, ire, connp,
6400 checkonly, &v6group, fmode, &ipv6_all_zeros);
6401 done = B_TRUE;
6402 }
6403 ire_refrele(ire);
6404 }
6405
6406 if (!done) {
6407 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6408 fmode, &ipv6_all_zeros);
6409 }
6410 return (error);
6411 }
6412
6413 /*
6414 * Set socket options for joining and leaving multicast groups
6415 * for specific sources.
6416 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6417 * The caller has already check that the option name is consistent with
6418 * the address family of the socket.
6419 */
6420 int
6421 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6422 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6423 {
6424 int *i1 = (int *)invalp;
6425 int error = 0;
6426 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6427 struct ip_mreq_source *imreqp;
6428 struct group_source_req *gsreqp;
6429 in6_addr_t v6group, v6src;
6430 uint32_t ifindex;
6431 ipaddr_t ifaddr;
6432 boolean_t mcast_opt = B_TRUE;
6433 mcast_record_t fmode;
6434 ire_t *ire;
6435 boolean_t done = B_FALSE;
6436 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6437 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6438
6439 switch (name) {
6440 case IP_BLOCK_SOURCE:
6441 mcast_opt = B_FALSE;
6442 /* FALLTHROUGH */
6443 case MCAST_BLOCK_SOURCE:
6444 fmode = MODE_IS_EXCLUDE;
6445 optfn = ip_opt_add_group;
6446 break;
6447
6448 case IP_UNBLOCK_SOURCE:
6449 mcast_opt = B_FALSE;
6450 /* FALLTHROUGH */
6451 case MCAST_UNBLOCK_SOURCE:
6452 fmode = MODE_IS_EXCLUDE;
6453 optfn = ip_opt_delete_group;
6454 break;
6455
6456 case IP_ADD_SOURCE_MEMBERSHIP:
6457 mcast_opt = B_FALSE;
6458 /* FALLTHROUGH */
6459 case MCAST_JOIN_SOURCE_GROUP:
6460 fmode = MODE_IS_INCLUDE;
6461 optfn = ip_opt_add_group;
6462 break;
6463
6464 case IP_DROP_SOURCE_MEMBERSHIP:
6465 mcast_opt = B_FALSE;
6466 /* FALLTHROUGH */
6467 case MCAST_LEAVE_SOURCE_GROUP:
6468 fmode = MODE_IS_INCLUDE;
6469 optfn = ip_opt_delete_group;
6470 break;
6471 default:
6472 ASSERT(0);
6473 }
6474
6475 if (mcast_opt) {
6476 gsreqp = (struct group_source_req *)i1;
6477 ifindex = gsreqp->gsr_interface;
6478 if (gsreqp->gsr_group.ss_family == AF_INET) {
6479 struct sockaddr_in *s;
6480 s = (struct sockaddr_in *)&gsreqp->gsr_group;
6481 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6482 s = (struct sockaddr_in *)&gsreqp->gsr_source;
6483 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6484 } else {
6485 struct sockaddr_in6 *s6;
6486
6487 if (!inet6)
6488 return (EINVAL); /* Not on INET socket */
6489
6490 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6491 v6group = s6->sin6_addr;
6492 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6493 v6src = s6->sin6_addr;
6494 }
6495 ifaddr = INADDR_ANY;
6496 } else {
6497 imreqp = (struct ip_mreq_source *)i1;
6498 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6499 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6500 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6501 ifindex = 0;
6502 }
6503
6504 /*
6505 * Handle src being mapped INADDR_ANY by changing it to unspecified.
6506 */
6507 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6508 v6src = ipv6_all_zeros;
6509
6510 /*
6511 * In the multirouting case, we need to replicate
6512 * the request as noted in the mcast cases above.
6513 */
6514 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6515 ipaddr_t group;
6516
6517 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6518
6519 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6520 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6521 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6522 } else {
6523 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6524 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6525 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6526 }
6527 if (ire != NULL) {
6528 if (ire->ire_flags & RTF_MULTIRT) {
6529 error = ip_multirt_apply_membership(optfn, ire, connp,
6530 checkonly, &v6group, fmode, &v6src);
6531 done = B_TRUE;
6532 }
6533 ire_refrele(ire);
6534 }
6535 if (!done) {
6536 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6537 fmode, &v6src);
6538 }
6539 return (error);
6540 }
6541
6542 /*
6543 * Given a destination address and a pointer to where to put the information
6544 * this routine fills in the mtuinfo.
6545 * The socket must be connected.
6546 * For sctp conn_faddr is the primary address.
6547 */
6548 int
6549 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6550 {
6551 uint32_t pmtu = IP_MAXPACKET;
6552 uint_t scopeid;
6553
6554 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6555 return (-1);
6556
6557 /* In case we never sent or called ip_set_destination_v4/v6 */
6558 if (ixa->ixa_ire != NULL)
6559 pmtu = ip_get_pmtu(ixa);
6560
6561 if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6562 scopeid = ixa->ixa_scopeid;
6563 else
6564 scopeid = 0;
6565
6566 bzero(mtuinfo, sizeof (*mtuinfo));
6567 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6568 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6569 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6570 mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6571 mtuinfo->ip6m_mtu = pmtu;
6572
6573 return (sizeof (struct ip6_mtuinfo));
6574 }
6575
6576 /*
6577 * When the src multihoming is changed from weak to [strong, preferred]
6578 * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6579 * and identify routes that were created by user-applications in the
6580 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6581 * currently defined. These routes are then 'rebound', i.e., their ire_ill
6582 * is selected by finding an interface route for the gateway.
6583 */
6584 /* ARGSUSED */
6585 void
6586 ip_ire_rebind_walker(ire_t *ire, void *notused)
6587 {
6588 if (!ire->ire_unbound || ire->ire_ill != NULL)
6589 return;
6590 ire_rebind(ire);
6591 ire_delete(ire);
6592 }
6593
6594 /*
6595 * When the src multihoming is changed from [strong, preferred] to weak,
6596 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6597 * set any entries that were created by user-applications in the unbound state
6598 * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6599 */
6600 /* ARGSUSED */
6601 void
6602 ip_ire_unbind_walker(ire_t *ire, void *notused)
6603 {
6604 ire_t *new_ire;
6605
6606 if (!ire->ire_unbound || ire->ire_ill == NULL)
6607 return;
6608 if (ire->ire_ipversion == IPV6_VERSION) {
6609 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6610 &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6611 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6612 } else {
6613 new_ire = ire_create((uchar_t *)&ire->ire_addr,
6614 (uchar_t *)&ire->ire_mask,
6615 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6616 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6617 }
6618 if (new_ire == NULL)
6619 return;
6620 new_ire->ire_unbound = B_TRUE;
6621 /*
6622 * The bound ire must first be deleted so that we don't return
6623 * the existing one on the attempt to add the unbound new_ire.
6624 */
6625 ire_delete(ire);
6626 new_ire = ire_add(new_ire);
6627 if (new_ire != NULL)
6628 ire_refrele(new_ire);
6629 }
6630
6631 /*
6632 * When the settings of ip*_strict_src_multihoming tunables are changed,
6633 * all cached routes need to be recomputed. This recomputation needs to be
6634 * done when going from weaker to stronger modes so that the cached ire
6635 * for the connection does not violate the current ip*_strict_src_multihoming
6636 * setting. It also needs to be done when going from stronger to weaker modes,
6637 * so that we fall back to matching on the longest-matching-route (as opposed
6638 * to a shorter match that may have been selected in the strong mode
6639 * to satisfy src_multihoming settings).
6640 *
6641 * The cached ixa_ire entires for all conn_t entries are marked as
6642 * "verify" so that they will be recomputed for the next packet.
6643 */
6644 void
6645 conn_ire_revalidate(conn_t *connp, void *arg)
6646 {
6647 boolean_t isv6 = (boolean_t)arg;
6648
6649 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6650 (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6651 return;
6652 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6653 }
6654
6655 /*
6656 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6657 * When an ipf is passed here for the first time, if
6658 * we already have in-order fragments on the queue, we convert from the fast-
6659 * path reassembly scheme to the hard-case scheme. From then on, additional
6660 * fragments are reassembled here. We keep track of the start and end offsets
6661 * of each piece, and the number of holes in the chain. When the hole count
6662 * goes to zero, we are done!
6663 *
6664 * The ipf_count will be updated to account for any mblk(s) added (pointed to
6665 * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6666 * ipfb_count and ill_frag_count by the difference of ipf_count before and
6667 * after the call to ip_reassemble().
6668 */
6669 int
6670 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6671 size_t msg_len)
6672 {
6673 uint_t end;
6674 mblk_t *next_mp;
6675 mblk_t *mp1;
6676 uint_t offset;
6677 boolean_t incr_dups = B_TRUE;
6678 boolean_t offset_zero_seen = B_FALSE;
6679 boolean_t pkt_boundary_checked = B_FALSE;
6680
6681 /* If start == 0 then ipf_nf_hdr_len has to be set. */
6682 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6683
6684 /* Add in byte count */
6685 ipf->ipf_count += msg_len;
6686 if (ipf->ipf_end) {
6687 /*
6688 * We were part way through in-order reassembly, but now there
6689 * is a hole. We walk through messages already queued, and
6690 * mark them for hard case reassembly. We know that up till
6691 * now they were in order starting from offset zero.
6692 */
6693 offset = 0;
6694 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6695 IP_REASS_SET_START(mp1, offset);
6696 if (offset == 0) {
6697 ASSERT(ipf->ipf_nf_hdr_len != 0);
6698 offset = -ipf->ipf_nf_hdr_len;
6699 }
6700 offset += mp1->b_wptr - mp1->b_rptr;
6701 IP_REASS_SET_END(mp1, offset);
6702 }
6703 /* One hole at the end. */
6704 ipf->ipf_hole_cnt = 1;
6705 /* Brand it as a hard case, forever. */
6706 ipf->ipf_end = 0;
6707 }
6708 /* Walk through all the new pieces. */
6709 do {
6710 end = start + (mp->b_wptr - mp->b_rptr);
6711 /*
6712 * If start is 0, decrease 'end' only for the first mblk of
6713 * the fragment. Otherwise 'end' can get wrong value in the
6714 * second pass of the loop if first mblk is exactly the
6715 * size of ipf_nf_hdr_len.
6716 */
6717 if (start == 0 && !offset_zero_seen) {
6718 /* First segment */
6719 ASSERT(ipf->ipf_nf_hdr_len != 0);
6720 end -= ipf->ipf_nf_hdr_len;
6721 offset_zero_seen = B_TRUE;
6722 }
6723 next_mp = mp->b_cont;
6724 /*
6725 * We are checking to see if there is any interesing data
6726 * to process. If there isn't and the mblk isn't the
6727 * one which carries the unfragmentable header then we
6728 * drop it. It's possible to have just the unfragmentable
6729 * header come through without any data. That needs to be
6730 * saved.
6731 *
6732 * If the assert at the top of this function holds then the
6733 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code
6734 * is infrequently traveled enough that the test is left in
6735 * to protect against future code changes which break that
6736 * invariant.
6737 */
6738 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6739 /* Empty. Blast it. */
6740 IP_REASS_SET_START(mp, 0);
6741 IP_REASS_SET_END(mp, 0);
6742 /*
6743 * If the ipf points to the mblk we are about to free,
6744 * update ipf to point to the next mblk (or NULL
6745 * if none).
6746 */
6747 if (ipf->ipf_mp->b_cont == mp)
6748 ipf->ipf_mp->b_cont = next_mp;
6749 freeb(mp);
6750 continue;
6751 }
6752 mp->b_cont = NULL;
6753 IP_REASS_SET_START(mp, start);
6754 IP_REASS_SET_END(mp, end);
6755 if (!ipf->ipf_tail_mp) {
6756 ipf->ipf_tail_mp = mp;
6757 ipf->ipf_mp->b_cont = mp;
6758 if (start == 0 || !more) {
6759 ipf->ipf_hole_cnt = 1;
6760 /*
6761 * if the first fragment comes in more than one
6762 * mblk, this loop will be executed for each
6763 * mblk. Need to adjust hole count so exiting
6764 * this routine will leave hole count at 1.
6765 */
6766 if (next_mp)
6767 ipf->ipf_hole_cnt++;
6768 } else
6769 ipf->ipf_hole_cnt = 2;
6770 continue;
6771 } else if (ipf->ipf_last_frag_seen && !more &&
6772 !pkt_boundary_checked) {
6773 /*
6774 * We check datagram boundary only if this fragment
6775 * claims to be the last fragment and we have seen a
6776 * last fragment in the past too. We do this only
6777 * once for a given fragment.
6778 *
6779 * start cannot be 0 here as fragments with start=0
6780 * and MF=0 gets handled as a complete packet. These
6781 * fragments should not reach here.
6782 */
6783
6784 if (start + msgdsize(mp) !=
6785 IP_REASS_END(ipf->ipf_tail_mp)) {
6786 /*
6787 * We have two fragments both of which claim
6788 * to be the last fragment but gives conflicting
6789 * information about the whole datagram size.
6790 * Something fishy is going on. Drop the
6791 * fragment and free up the reassembly list.
6792 */
6793 return (IP_REASS_FAILED);
6794 }
6795
6796 /*
6797 * We shouldn't come to this code block again for this
6798 * particular fragment.
6799 */
6800 pkt_boundary_checked = B_TRUE;
6801 }
6802
6803 /* New stuff at or beyond tail? */
6804 offset = IP_REASS_END(ipf->ipf_tail_mp);
6805 if (start >= offset) {
6806 if (ipf->ipf_last_frag_seen) {
6807 /* current fragment is beyond last fragment */
6808 return (IP_REASS_FAILED);
6809 }
6810 /* Link it on end. */
6811 ipf->ipf_tail_mp->b_cont = mp;
6812 ipf->ipf_tail_mp = mp;
6813 if (more) {
6814 if (start != offset)
6815 ipf->ipf_hole_cnt++;
6816 } else if (start == offset && next_mp == NULL)
6817 ipf->ipf_hole_cnt--;
6818 continue;
6819 }
6820 mp1 = ipf->ipf_mp->b_cont;
6821 offset = IP_REASS_START(mp1);
6822 /* New stuff at the front? */
6823 if (start < offset) {
6824 if (start == 0) {
6825 if (end >= offset) {
6826 /* Nailed the hole at the begining. */
6827 ipf->ipf_hole_cnt--;
6828 }
6829 } else if (end < offset) {
6830 /*
6831 * A hole, stuff, and a hole where there used
6832 * to be just a hole.
6833 */
6834 ipf->ipf_hole_cnt++;
6835 }
6836 mp->b_cont = mp1;
6837 /* Check for overlap. */
6838 while (end > offset) {
6839 if (end < IP_REASS_END(mp1)) {
6840 mp->b_wptr -= end - offset;
6841 IP_REASS_SET_END(mp, offset);
6842 BUMP_MIB(ill->ill_ip_mib,
6843 ipIfStatsReasmPartDups);
6844 break;
6845 }
6846 /* Did we cover another hole? */
6847 if ((mp1->b_cont &&
6848 IP_REASS_END(mp1) !=
6849 IP_REASS_START(mp1->b_cont) &&
6850 end >= IP_REASS_START(mp1->b_cont)) ||
6851 (!ipf->ipf_last_frag_seen && !more)) {
6852 ipf->ipf_hole_cnt--;
6853 }
6854 /* Clip out mp1. */
6855 if ((mp->b_cont = mp1->b_cont) == NULL) {
6856 /*
6857 * After clipping out mp1, this guy
6858 * is now hanging off the end.
6859 */
6860 ipf->ipf_tail_mp = mp;
6861 }
6862 IP_REASS_SET_START(mp1, 0);
6863 IP_REASS_SET_END(mp1, 0);
6864 /* Subtract byte count */
6865 ipf->ipf_count -= mp1->b_datap->db_lim -
6866 mp1->b_datap->db_base;
6867 freeb(mp1);
6868 BUMP_MIB(ill->ill_ip_mib,
6869 ipIfStatsReasmPartDups);
6870 mp1 = mp->b_cont;
6871 if (!mp1)
6872 break;
6873 offset = IP_REASS_START(mp1);
6874 }
6875 ipf->ipf_mp->b_cont = mp;
6876 continue;
6877 }
6878 /*
6879 * The new piece starts somewhere between the start of the head
6880 * and before the end of the tail.
6881 */
6882 for (; mp1; mp1 = mp1->b_cont) {
6883 offset = IP_REASS_END(mp1);
6884 if (start < offset) {
6885 if (end <= offset) {
6886 /* Nothing new. */
6887 IP_REASS_SET_START(mp, 0);
6888 IP_REASS_SET_END(mp, 0);
6889 /* Subtract byte count */
6890 ipf->ipf_count -= mp->b_datap->db_lim -
6891 mp->b_datap->db_base;
6892 if (incr_dups) {
6893 ipf->ipf_num_dups++;
6894 incr_dups = B_FALSE;
6895 }
6896 freeb(mp);
6897 BUMP_MIB(ill->ill_ip_mib,
6898 ipIfStatsReasmDuplicates);
6899 break;
6900 }
6901 /*
6902 * Trim redundant stuff off beginning of new
6903 * piece.
6904 */
6905 IP_REASS_SET_START(mp, offset);
6906 mp->b_rptr += offset - start;
6907 BUMP_MIB(ill->ill_ip_mib,
6908 ipIfStatsReasmPartDups);
6909 start = offset;
6910 if (!mp1->b_cont) {
6911 /*
6912 * After trimming, this guy is now
6913 * hanging off the end.
6914 */
6915 mp1->b_cont = mp;
6916 ipf->ipf_tail_mp = mp;
6917 if (!more) {
6918 ipf->ipf_hole_cnt--;
6919 }
6920 break;
6921 }
6922 }
6923 if (start >= IP_REASS_START(mp1->b_cont))
6924 continue;
6925 /* Fill a hole */
6926 if (start > offset)
6927 ipf->ipf_hole_cnt++;
6928 mp->b_cont = mp1->b_cont;
6929 mp1->b_cont = mp;
6930 mp1 = mp->b_cont;
6931 offset = IP_REASS_START(mp1);
6932 if (end >= offset) {
6933 ipf->ipf_hole_cnt--;
6934 /* Check for overlap. */
6935 while (end > offset) {
6936 if (end < IP_REASS_END(mp1)) {
6937 mp->b_wptr -= end - offset;
6938 IP_REASS_SET_END(mp, offset);
6939 /*
6940 * TODO we might bump
6941 * this up twice if there is
6942 * overlap at both ends.
6943 */
6944 BUMP_MIB(ill->ill_ip_mib,
6945 ipIfStatsReasmPartDups);
6946 break;
6947 }
6948 /* Did we cover another hole? */
6949 if ((mp1->b_cont &&
6950 IP_REASS_END(mp1)
6951 != IP_REASS_START(mp1->b_cont) &&
6952 end >=
6953 IP_REASS_START(mp1->b_cont)) ||
6954 (!ipf->ipf_last_frag_seen &&
6955 !more)) {
6956 ipf->ipf_hole_cnt--;
6957 }
6958 /* Clip out mp1. */
6959 if ((mp->b_cont = mp1->b_cont) ==
6960 NULL) {
6961 /*
6962 * After clipping out mp1,
6963 * this guy is now hanging
6964 * off the end.
6965 */
6966 ipf->ipf_tail_mp = mp;
6967 }
6968 IP_REASS_SET_START(mp1, 0);
6969 IP_REASS_SET_END(mp1, 0);
6970 /* Subtract byte count */
6971 ipf->ipf_count -=
6972 mp1->b_datap->db_lim -
6973 mp1->b_datap->db_base;
6974 freeb(mp1);
6975 BUMP_MIB(ill->ill_ip_mib,
6976 ipIfStatsReasmPartDups);
6977 mp1 = mp->b_cont;
6978 if (!mp1)
6979 break;
6980 offset = IP_REASS_START(mp1);
6981 }
6982 }
6983 break;
6984 }
6985 } while (start = end, mp = next_mp);
6986
6987 /* Fragment just processed could be the last one. Remember this fact */
6988 if (!more)
6989 ipf->ipf_last_frag_seen = B_TRUE;
6990
6991 /* Still got holes? */
6992 if (ipf->ipf_hole_cnt)
6993 return (IP_REASS_PARTIAL);
6994 /* Clean up overloaded fields to avoid upstream disasters. */
6995 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6996 IP_REASS_SET_START(mp1, 0);
6997 IP_REASS_SET_END(mp1, 0);
6998 }
6999 return (IP_REASS_COMPLETE);
7000 }
7001
7002 /*
7003 * Fragmentation reassembly. Each ILL has a hash table for
7004 * queuing packets undergoing reassembly for all IPIFs
7005 * associated with the ILL. The hash is based on the packet
7006 * IP ident field. The ILL frag hash table was allocated
7007 * as a timer block at the time the ILL was created. Whenever
7008 * there is anything on the reassembly queue, the timer will
7009 * be running. Returns the reassembled packet if reassembly completes.
7010 */
7011 mblk_t *
7012 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
7013 {
7014 uint32_t frag_offset_flags;
7015 mblk_t *t_mp;
7016 ipaddr_t dst;
7017 uint8_t proto = ipha->ipha_protocol;
7018 uint32_t sum_val;
7019 uint16_t sum_flags;
7020 ipf_t *ipf;
7021 ipf_t **ipfp;
7022 ipfb_t *ipfb;
7023 uint16_t ident;
7024 uint32_t offset;
7025 ipaddr_t src;
7026 uint_t hdr_length;
7027 uint32_t end;
7028 mblk_t *mp1;
7029 mblk_t *tail_mp;
7030 size_t count;
7031 size_t msg_len;
7032 uint8_t ecn_info = 0;
7033 uint32_t packet_size;
7034 boolean_t pruned = B_FALSE;
7035 ill_t *ill = ira->ira_ill;
7036 ip_stack_t *ipst = ill->ill_ipst;
7037
7038 /*
7039 * Drop the fragmented as early as possible, if
7040 * we don't have resource(s) to re-assemble.
7041 */
7042 if (ipst->ips_ip_reass_queue_bytes == 0) {
7043 freemsg(mp);
7044 return (NULL);
7045 }
7046
7047 /* Check for fragmentation offset; return if there's none */
7048 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
7049 (IPH_MF | IPH_OFFSET)) == 0)
7050 return (mp);
7051
7052 /*
7053 * We utilize hardware computed checksum info only for UDP since
7054 * IP fragmentation is a normal occurrence for the protocol. In
7055 * addition, checksum offload support for IP fragments carrying
7056 * UDP payload is commonly implemented across network adapters.
7057 */
7058 ASSERT(ira->ira_rill != NULL);
7059 if (proto == IPPROTO_UDP && dohwcksum &&
7060 ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
7061 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
7062 mblk_t *mp1 = mp->b_cont;
7063 int32_t len;
7064
7065 /* Record checksum information from the packet */
7066 sum_val = (uint32_t)DB_CKSUM16(mp);
7067 sum_flags = DB_CKSUMFLAGS(mp);
7068
7069 /* IP payload offset from beginning of mblk */
7070 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
7071
7072 if ((sum_flags & HCK_PARTIALCKSUM) &&
7073 (mp1 == NULL || mp1->b_cont == NULL) &&
7074 offset >= DB_CKSUMSTART(mp) &&
7075 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
7076 uint32_t adj;
7077 /*
7078 * Partial checksum has been calculated by hardware
7079 * and attached to the packet; in addition, any
7080 * prepended extraneous data is even byte aligned.
7081 * If any such data exists, we adjust the checksum;
7082 * this would also handle any postpended data.
7083 */
7084 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
7085 mp, mp1, len, adj);
7086
7087 /* One's complement subtract extraneous checksum */
7088 if (adj >= sum_val)
7089 sum_val = ~(adj - sum_val) & 0xFFFF;
7090 else
7091 sum_val -= adj;
7092 }
7093 } else {
7094 sum_val = 0;
7095 sum_flags = 0;
7096 }
7097
7098 /* Clear hardware checksumming flag */
7099 DB_CKSUMFLAGS(mp) = 0;
7100
7101 ident = ipha->ipha_ident;
7102 offset = (frag_offset_flags << 3) & 0xFFFF;
7103 src = ipha->ipha_src;
7104 dst = ipha->ipha_dst;
7105 hdr_length = IPH_HDR_LENGTH(ipha);
7106 end = ntohs(ipha->ipha_length) - hdr_length;
7107
7108 /* If end == 0 then we have a packet with no data, so just free it */
7109 if (end == 0) {
7110 freemsg(mp);
7111 return (NULL);
7112 }
7113
7114 /* Record the ECN field info. */
7115 ecn_info = (ipha->ipha_type_of_service & 0x3);
7116 if (offset != 0) {
7117 /*
7118 * If this isn't the first piece, strip the header, and
7119 * add the offset to the end value.
7120 */
7121 mp->b_rptr += hdr_length;
7122 end += offset;
7123 }
7124
7125 /* Handle vnic loopback of fragments */
7126 if (mp->b_datap->db_ref > 2)
7127 msg_len = 0;
7128 else
7129 msg_len = MBLKSIZE(mp);
7130
7131 tail_mp = mp;
7132 while (tail_mp->b_cont != NULL) {
7133 tail_mp = tail_mp->b_cont;
7134 if (tail_mp->b_datap->db_ref <= 2)
7135 msg_len += MBLKSIZE(tail_mp);
7136 }
7137
7138 /* If the reassembly list for this ILL will get too big, prune it */
7139 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
7140 ipst->ips_ip_reass_queue_bytes) {
7141 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
7142 uint_t, ill->ill_frag_count,
7143 uint_t, ipst->ips_ip_reass_queue_bytes);
7144 ill_frag_prune(ill,
7145 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
7146 (ipst->ips_ip_reass_queue_bytes - msg_len));
7147 pruned = B_TRUE;
7148 }
7149
7150 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
7151 mutex_enter(&ipfb->ipfb_lock);
7152
7153 ipfp = &ipfb->ipfb_ipf;
7154 /* Try to find an existing fragment queue for this packet. */
7155 for (;;) {
7156 ipf = ipfp[0];
7157 if (ipf != NULL) {
7158 /*
7159 * It has to match on ident and src/dst address.
7160 */
7161 if (ipf->ipf_ident == ident &&
7162 ipf->ipf_src == src &&
7163 ipf->ipf_dst == dst &&
7164 ipf->ipf_protocol == proto) {
7165 /*
7166 * If we have received too many
7167 * duplicate fragments for this packet
7168 * free it.
7169 */
7170 if (ipf->ipf_num_dups > ip_max_frag_dups) {
7171 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7172 freemsg(mp);
7173 mutex_exit(&ipfb->ipfb_lock);
7174 return (NULL);
7175 }
7176 /* Found it. */
7177 break;
7178 }
7179 ipfp = &ipf->ipf_hash_next;
7180 continue;
7181 }
7182
7183 /*
7184 * If we pruned the list, do we want to store this new
7185 * fragment?. We apply an optimization here based on the
7186 * fact that most fragments will be received in order.
7187 * So if the offset of this incoming fragment is zero,
7188 * it is the first fragment of a new packet. We will
7189 * keep it. Otherwise drop the fragment, as we have
7190 * probably pruned the packet already (since the
7191 * packet cannot be found).
7192 */
7193 if (pruned && offset != 0) {
7194 mutex_exit(&ipfb->ipfb_lock);
7195 freemsg(mp);
7196 return (NULL);
7197 }
7198
7199 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) {
7200 /*
7201 * Too many fragmented packets in this hash
7202 * bucket. Free the oldest.
7203 */
7204 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
7205 }
7206
7207 /* New guy. Allocate a frag message. */
7208 mp1 = allocb(sizeof (*ipf), BPRI_MED);
7209 if (mp1 == NULL) {
7210 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7211 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7212 freemsg(mp);
7213 reass_done:
7214 mutex_exit(&ipfb->ipfb_lock);
7215 return (NULL);
7216 }
7217
7218 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
7219 mp1->b_cont = mp;
7220
7221 /* Initialize the fragment header. */
7222 ipf = (ipf_t *)mp1->b_rptr;
7223 ipf->ipf_mp = mp1;
7224 ipf->ipf_ptphn = ipfp;
7225 ipfp[0] = ipf;
7226 ipf->ipf_hash_next = NULL;
7227 ipf->ipf_ident = ident;
7228 ipf->ipf_protocol = proto;
7229 ipf->ipf_src = src;
7230 ipf->ipf_dst = dst;
7231 ipf->ipf_nf_hdr_len = 0;
7232 /* Record reassembly start time. */
7233 ipf->ipf_timestamp = gethrestime_sec();
7234 /* Record ipf generation and account for frag header */
7235 ipf->ipf_gen = ill->ill_ipf_gen++;
7236 ipf->ipf_count = MBLKSIZE(mp1);
7237 ipf->ipf_last_frag_seen = B_FALSE;
7238 ipf->ipf_ecn = ecn_info;
7239 ipf->ipf_num_dups = 0;
7240 ipfb->ipfb_frag_pkts++;
7241 ipf->ipf_checksum = 0;
7242 ipf->ipf_checksum_flags = 0;
7243
7244 /* Store checksum value in fragment header */
7245 if (sum_flags != 0) {
7246 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7247 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7248 ipf->ipf_checksum = sum_val;
7249 ipf->ipf_checksum_flags = sum_flags;
7250 }
7251
7252 /*
7253 * We handle reassembly two ways. In the easy case,
7254 * where all the fragments show up in order, we do
7255 * minimal bookkeeping, and just clip new pieces on
7256 * the end. If we ever see a hole, then we go off
7257 * to ip_reassemble which has to mark the pieces and
7258 * keep track of the number of holes, etc. Obviously,
7259 * the point of having both mechanisms is so we can
7260 * handle the easy case as efficiently as possible.
7261 */
7262 if (offset == 0) {
7263 /* Easy case, in-order reassembly so far. */
7264 ipf->ipf_count += msg_len;
7265 ipf->ipf_tail_mp = tail_mp;
7266 /*
7267 * Keep track of next expected offset in
7268 * ipf_end.
7269 */
7270 ipf->ipf_end = end;
7271 ipf->ipf_nf_hdr_len = hdr_length;
7272 } else {
7273 /* Hard case, hole at the beginning. */
7274 ipf->ipf_tail_mp = NULL;
7275 /*
7276 * ipf_end == 0 means that we have given up
7277 * on easy reassembly.
7278 */
7279 ipf->ipf_end = 0;
7280
7281 /* Forget checksum offload from now on */
7282 ipf->ipf_checksum_flags = 0;
7283
7284 /*
7285 * ipf_hole_cnt is set by ip_reassemble.
7286 * ipf_count is updated by ip_reassemble.
7287 * No need to check for return value here
7288 * as we don't expect reassembly to complete
7289 * or fail for the first fragment itself.
7290 */
7291 (void) ip_reassemble(mp, ipf,
7292 (frag_offset_flags & IPH_OFFSET) << 3,
7293 (frag_offset_flags & IPH_MF), ill, msg_len);
7294 }
7295 /* Update per ipfb and ill byte counts */
7296 ipfb->ipfb_count += ipf->ipf_count;
7297 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7298 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7299 /* If the frag timer wasn't already going, start it. */
7300 mutex_enter(&ill->ill_lock);
7301 ill_frag_timer_start(ill);
7302 mutex_exit(&ill->ill_lock);
7303 goto reass_done;
7304 }
7305
7306 /*
7307 * If the packet's flag has changed (it could be coming up
7308 * from an interface different than the previous, therefore
7309 * possibly different checksum capability), then forget about
7310 * any stored checksum states. Otherwise add the value to
7311 * the existing one stored in the fragment header.
7312 */
7313 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7314 sum_val += ipf->ipf_checksum;
7315 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7316 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7317 ipf->ipf_checksum = sum_val;
7318 } else if (ipf->ipf_checksum_flags != 0) {
7319 /* Forget checksum offload from now on */
7320 ipf->ipf_checksum_flags = 0;
7321 }
7322
7323 /*
7324 * We have a new piece of a datagram which is already being
7325 * reassembled. Update the ECN info if all IP fragments
7326 * are ECN capable. If there is one which is not, clear
7327 * all the info. If there is at least one which has CE
7328 * code point, IP needs to report that up to transport.
7329 */
7330 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7331 if (ecn_info == IPH_ECN_CE)
7332 ipf->ipf_ecn = IPH_ECN_CE;
7333 } else {
7334 ipf->ipf_ecn = IPH_ECN_NECT;
7335 }
7336 if (offset && ipf->ipf_end == offset) {
7337 /* The new fragment fits at the end */
7338 ipf->ipf_tail_mp->b_cont = mp;
7339 /* Update the byte count */
7340 ipf->ipf_count += msg_len;
7341 /* Update per ipfb and ill byte counts */
7342 ipfb->ipfb_count += msg_len;
7343 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7344 atomic_add_32(&ill->ill_frag_count, msg_len);
7345 if (frag_offset_flags & IPH_MF) {
7346 /* More to come. */
7347 ipf->ipf_end = end;
7348 ipf->ipf_tail_mp = tail_mp;
7349 goto reass_done;
7350 }
7351 } else {
7352 /* Go do the hard cases. */
7353 int ret;
7354
7355 if (offset == 0)
7356 ipf->ipf_nf_hdr_len = hdr_length;
7357
7358 /* Save current byte count */
7359 count = ipf->ipf_count;
7360 ret = ip_reassemble(mp, ipf,
7361 (frag_offset_flags & IPH_OFFSET) << 3,
7362 (frag_offset_flags & IPH_MF), ill, msg_len);
7363 /* Count of bytes added and subtracted (freeb()ed) */
7364 count = ipf->ipf_count - count;
7365 if (count) {
7366 /* Update per ipfb and ill byte counts */
7367 ipfb->ipfb_count += count;
7368 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7369 atomic_add_32(&ill->ill_frag_count, count);
7370 }
7371 if (ret == IP_REASS_PARTIAL) {
7372 goto reass_done;
7373 } else if (ret == IP_REASS_FAILED) {
7374 /* Reassembly failed. Free up all resources */
7375 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7376 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7377 IP_REASS_SET_START(t_mp, 0);
7378 IP_REASS_SET_END(t_mp, 0);
7379 }
7380 freemsg(mp);
7381 goto reass_done;
7382 }
7383 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7384 }
7385 /*
7386 * We have completed reassembly. Unhook the frag header from
7387 * the reassembly list.
7388 *
7389 * Before we free the frag header, record the ECN info
7390 * to report back to the transport.
7391 */
7392 ecn_info = ipf->ipf_ecn;
7393 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7394 ipfp = ipf->ipf_ptphn;
7395
7396 /* We need to supply these to caller */
7397 if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7398 sum_val = ipf->ipf_checksum;
7399 else
7400 sum_val = 0;
7401
7402 mp1 = ipf->ipf_mp;
7403 count = ipf->ipf_count;
7404 ipf = ipf->ipf_hash_next;
7405 if (ipf != NULL)
7406 ipf->ipf_ptphn = ipfp;
7407 ipfp[0] = ipf;
7408 atomic_add_32(&ill->ill_frag_count, -count);
7409 ASSERT(ipfb->ipfb_count >= count);
7410 ipfb->ipfb_count -= count;
7411 ipfb->ipfb_frag_pkts--;
7412 mutex_exit(&ipfb->ipfb_lock);
7413 /* Ditch the frag header. */
7414 mp = mp1->b_cont;
7415
7416 freeb(mp1);
7417
7418 /* Restore original IP length in header. */
7419 packet_size = (uint32_t)msgdsize(mp);
7420 if (packet_size > IP_MAXPACKET) {
7421 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7422 ip_drop_input("Reassembled packet too large", mp, ill);
7423 freemsg(mp);
7424 return (NULL);
7425 }
7426
7427 if (DB_REF(mp) > 1) {
7428 mblk_t *mp2 = copymsg(mp);
7429
7430 if (mp2 == NULL) {
7431 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7432 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7433 freemsg(mp);
7434 return (NULL);
7435 }
7436 freemsg(mp);
7437 mp = mp2;
7438 }
7439 ipha = (ipha_t *)mp->b_rptr;
7440
7441 ipha->ipha_length = htons((uint16_t)packet_size);
7442 /* We're now complete, zip the frag state */
7443 ipha->ipha_fragment_offset_and_flags = 0;
7444 /* Record the ECN info. */
7445 ipha->ipha_type_of_service &= 0xFC;
7446 ipha->ipha_type_of_service |= ecn_info;
7447
7448 /* Update the receive attributes */
7449 ira->ira_pktlen = packet_size;
7450 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7451
7452 /* Reassembly is successful; set checksum information in packet */
7453 DB_CKSUM16(mp) = (uint16_t)sum_val;
7454 DB_CKSUMFLAGS(mp) = sum_flags;
7455 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7456
7457 return (mp);
7458 }
7459
7460 /*
7461 * Pullup function that should be used for IP input in order to
7462 * ensure we do not loose the L2 source address; we need the l2 source
7463 * address for IP_RECVSLLA and for ndp_input.
7464 *
7465 * We return either NULL or b_rptr.
7466 */
7467 void *
7468 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7469 {
7470 ill_t *ill = ira->ira_ill;
7471
7472 if (ip_rput_pullups++ == 0) {
7473 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7474 "ip_pullup: %s forced us to "
7475 " pullup pkt, hdr len %ld, hdr addr %p",
7476 ill->ill_name, len, (void *)mp->b_rptr);
7477 }
7478 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7479 ip_setl2src(mp, ira, ira->ira_rill);
7480 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7481 if (!pullupmsg(mp, len))
7482 return (NULL);
7483 else
7484 return (mp->b_rptr);
7485 }
7486
7487 /*
7488 * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7489 * When called from the ULP ira_rill will be NULL hence the caller has to
7490 * pass in the ill.
7491 */
7492 /* ARGSUSED */
7493 void
7494 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7495 {
7496 const uchar_t *addr;
7497 int alen;
7498
7499 if (ira->ira_flags & IRAF_L2SRC_SET)
7500 return;
7501
7502 ASSERT(ill != NULL);
7503 alen = ill->ill_phys_addr_length;
7504 ASSERT(alen <= sizeof (ira->ira_l2src));
7505 if (ira->ira_mhip != NULL &&
7506 (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7507 bcopy(addr, ira->ira_l2src, alen);
7508 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7509 (addr = ill->ill_phys_addr) != NULL) {
7510 bcopy(addr, ira->ira_l2src, alen);
7511 } else {
7512 bzero(ira->ira_l2src, alen);
7513 }
7514 ira->ira_flags |= IRAF_L2SRC_SET;
7515 }
7516
7517 /*
7518 * check ip header length and align it.
7519 */
7520 mblk_t *
7521 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7522 {
7523 ill_t *ill = ira->ira_ill;
7524 ssize_t len;
7525
7526 len = MBLKL(mp);
7527
7528 if (!OK_32PTR(mp->b_rptr))
7529 IP_STAT(ill->ill_ipst, ip_notaligned);
7530 else
7531 IP_STAT(ill->ill_ipst, ip_recv_pullup);
7532
7533 /* Guard against bogus device drivers */
7534 if (len < 0) {
7535 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7536 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7537 freemsg(mp);
7538 return (NULL);
7539 }
7540
7541 if (len == 0) {
7542 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7543 mblk_t *mp1 = mp->b_cont;
7544
7545 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7546 ip_setl2src(mp, ira, ira->ira_rill);
7547 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7548
7549 freeb(mp);
7550 mp = mp1;
7551 if (mp == NULL)
7552 return (NULL);
7553
7554 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7555 return (mp);
7556 }
7557 if (ip_pullup(mp, min_size, ira) == NULL) {
7558 if (msgdsize(mp) < min_size) {
7559 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7560 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7561 } else {
7562 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7563 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7564 }
7565 freemsg(mp);
7566 return (NULL);
7567 }
7568 return (mp);
7569 }
7570
7571 /*
7572 * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7573 */
7574 mblk_t *
7575 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7576 uint_t min_size, ip_recv_attr_t *ira)
7577 {
7578 ill_t *ill = ira->ira_ill;
7579
7580 /*
7581 * Make sure we have data length consistent
7582 * with the IP header.
7583 */
7584 if (mp->b_cont == NULL) {
7585 /* pkt_len is based on ipha_len, not the mblk length */
7586 if (pkt_len < min_size) {
7587 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7588 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7589 freemsg(mp);
7590 return (NULL);
7591 }
7592 if (len < 0) {
7593 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7594 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7595 freemsg(mp);
7596 return (NULL);
7597 }
7598 /* Drop any pad */
7599 mp->b_wptr = rptr + pkt_len;
7600 } else if ((len += msgdsize(mp->b_cont)) != 0) {
7601 ASSERT(pkt_len >= min_size);
7602 if (pkt_len < min_size) {
7603 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7604 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7605 freemsg(mp);
7606 return (NULL);
7607 }
7608 if (len < 0) {
7609 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7610 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7611 freemsg(mp);
7612 return (NULL);
7613 }
7614 /* Drop any pad */
7615 (void) adjmsg(mp, -len);
7616 /*
7617 * adjmsg may have freed an mblk from the chain, hence
7618 * invalidate any hw checksum here. This will force IP to
7619 * calculate the checksum in sw, but only for this packet.
7620 */
7621 DB_CKSUMFLAGS(mp) = 0;
7622 IP_STAT(ill->ill_ipst, ip_multimblk);
7623 }
7624 return (mp);
7625 }
7626
7627 /*
7628 * Check that the IPv4 opt_len is consistent with the packet and pullup
7629 * the options.
7630 */
7631 mblk_t *
7632 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7633 ip_recv_attr_t *ira)
7634 {
7635 ill_t *ill = ira->ira_ill;
7636 ssize_t len;
7637
7638 /* Assume no IPv6 packets arrive over the IPv4 queue */
7639 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7640 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7641 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7642 ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7643 freemsg(mp);
7644 return (NULL);
7645 }
7646
7647 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7648 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7649 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7650 freemsg(mp);
7651 return (NULL);
7652 }
7653 /*
7654 * Recompute complete header length and make sure we
7655 * have access to all of it.
7656 */
7657 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7658 if (len > (mp->b_wptr - mp->b_rptr)) {
7659 if (len > pkt_len) {
7660 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7661 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7662 freemsg(mp);
7663 return (NULL);
7664 }
7665 if (ip_pullup(mp, len, ira) == NULL) {
7666 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7667 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7668 freemsg(mp);
7669 return (NULL);
7670 }
7671 }
7672 return (mp);
7673 }
7674
7675 /*
7676 * Returns a new ire, or the same ire, or NULL.
7677 * If a different IRE is returned, then it is held; the caller
7678 * needs to release it.
7679 * In no case is there any hold/release on the ire argument.
7680 */
7681 ire_t *
7682 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7683 {
7684 ire_t *new_ire;
7685 ill_t *ire_ill;
7686 uint_t ifindex;
7687 ip_stack_t *ipst = ill->ill_ipst;
7688 boolean_t strict_check = B_FALSE;
7689
7690 /*
7691 * IPMP common case: if IRE and ILL are in the same group, there's no
7692 * issue (e.g. packet received on an underlying interface matched an
7693 * IRE_LOCAL on its associated group interface).
7694 */
7695 ASSERT(ire->ire_ill != NULL);
7696 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7697 return (ire);
7698
7699 /*
7700 * Do another ire lookup here, using the ingress ill, to see if the
7701 * interface is in a usesrc group.
7702 * As long as the ills belong to the same group, we don't consider
7703 * them to be arriving on the wrong interface. Thus, if the switch
7704 * is doing inbound load spreading, we won't drop packets when the
7705 * ip*_strict_dst_multihoming switch is on.
7706 * We also need to check for IPIF_UNNUMBERED point2point interfaces
7707 * where the local address may not be unique. In this case we were
7708 * at the mercy of the initial ire lookup and the IRE_LOCAL it
7709 * actually returned. The new lookup, which is more specific, should
7710 * only find the IRE_LOCAL associated with the ingress ill if one
7711 * exists.
7712 */
7713 if (ire->ire_ipversion == IPV4_VERSION) {
7714 if (ipst->ips_ip_strict_dst_multihoming)
7715 strict_check = B_TRUE;
7716 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7717 IRE_LOCAL, ill, ALL_ZONES, NULL,
7718 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7719 } else {
7720 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7721 if (ipst->ips_ipv6_strict_dst_multihoming)
7722 strict_check = B_TRUE;
7723 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7724 IRE_LOCAL, ill, ALL_ZONES, NULL,
7725 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7726 }
7727 /*
7728 * If the same ire that was returned in ip_input() is found then this
7729 * is an indication that usesrc groups are in use. The packet
7730 * arrived on a different ill in the group than the one associated with
7731 * the destination address. If a different ire was found then the same
7732 * IP address must be hosted on multiple ills. This is possible with
7733 * unnumbered point2point interfaces. We switch to use this new ire in
7734 * order to have accurate interface statistics.
7735 */
7736 if (new_ire != NULL) {
7737 /* Note: held in one case but not the other? Caller handles */
7738 if (new_ire != ire)
7739 return (new_ire);
7740 /* Unchanged */
7741 ire_refrele(new_ire);
7742 return (ire);
7743 }
7744
7745 /*
7746 * Chase pointers once and store locally.
7747 */
7748 ASSERT(ire->ire_ill != NULL);
7749 ire_ill = ire->ire_ill;
7750 ifindex = ill->ill_usesrc_ifindex;
7751
7752 /*
7753 * Check if it's a legal address on the 'usesrc' interface.
7754 * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7755 * can just check phyint_ifindex.
7756 */
7757 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7758 return (ire);
7759 }
7760
7761 /*
7762 * If the ip*_strict_dst_multihoming switch is on then we can
7763 * only accept this packet if the interface is marked as routing.
7764 */
7765 if (!(strict_check))
7766 return (ire);
7767
7768 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7769 return (ire);
7770 }
7771 return (NULL);
7772 }
7773
7774 /*
7775 * This function is used to construct a mac_header_info_s from a
7776 * DL_UNITDATA_IND message.
7777 * The address fields in the mhi structure points into the message,
7778 * thus the caller can't use those fields after freeing the message.
7779 *
7780 * We determine whether the packet received is a non-unicast packet
7781 * and in doing so, determine whether or not it is broadcast vs multicast.
7782 * For it to be a broadcast packet, we must have the appropriate mblk_t
7783 * hanging off the ill_t. If this is either not present or doesn't match
7784 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7785 * to be multicast. Thus NICs that have no broadcast address (or no
7786 * capability for one, such as point to point links) cannot return as
7787 * the packet being broadcast.
7788 */
7789 void
7790 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7791 {
7792 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7793 mblk_t *bmp;
7794 uint_t extra_offset;
7795
7796 bzero(mhip, sizeof (struct mac_header_info_s));
7797
7798 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7799
7800 if (ill->ill_sap_length < 0)
7801 extra_offset = 0;
7802 else
7803 extra_offset = ill->ill_sap_length;
7804
7805 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7806 extra_offset;
7807 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7808 extra_offset;
7809
7810 if (!ind->dl_group_address)
7811 return;
7812
7813 /* Multicast or broadcast */
7814 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7815
7816 if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7817 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7818 (bmp = ill->ill_bcast_mp) != NULL) {
7819 dl_unitdata_req_t *dlur;
7820 uint8_t *bphys_addr;
7821
7822 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7823 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7824 extra_offset;
7825
7826 if (bcmp(mhip->mhi_daddr, bphys_addr,
7827 ind->dl_dest_addr_length) == 0)
7828 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7829 }
7830 }
7831
7832 /*
7833 * This function is used to construct a mac_header_info_s from a
7834 * M_DATA fastpath message from a DLPI driver.
7835 * The address fields in the mhi structure points into the message,
7836 * thus the caller can't use those fields after freeing the message.
7837 *
7838 * We determine whether the packet received is a non-unicast packet
7839 * and in doing so, determine whether or not it is broadcast vs multicast.
7840 * For it to be a broadcast packet, we must have the appropriate mblk_t
7841 * hanging off the ill_t. If this is either not present or doesn't match
7842 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7843 * to be multicast. Thus NICs that have no broadcast address (or no
7844 * capability for one, such as point to point links) cannot return as
7845 * the packet being broadcast.
7846 */
7847 void
7848 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7849 {
7850 mblk_t *bmp;
7851 struct ether_header *pether;
7852
7853 bzero(mhip, sizeof (struct mac_header_info_s));
7854
7855 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7856
7857 pether = (struct ether_header *)((char *)mp->b_rptr
7858 - sizeof (struct ether_header));
7859
7860 /*
7861 * Make sure the interface is an ethernet type, since we don't
7862 * know the header format for anything but Ethernet. Also make
7863 * sure we are pointing correctly above db_base.
7864 */
7865 if (ill->ill_type != IFT_ETHER)
7866 return;
7867
7868 retry:
7869 if ((uchar_t *)pether < mp->b_datap->db_base)
7870 return;
7871
7872 /* Is there a VLAN tag? */
7873 if (ill->ill_isv6) {
7874 if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7875 pether = (struct ether_header *)((char *)pether - 4);
7876 goto retry;
7877 }
7878 } else {
7879 if (pether->ether_type != htons(ETHERTYPE_IP)) {
7880 pether = (struct ether_header *)((char *)pether - 4);
7881 goto retry;
7882 }
7883 }
7884 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7885 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7886
7887 if (!(mhip->mhi_daddr[0] & 0x01))
7888 return;
7889
7890 /* Multicast or broadcast */
7891 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7892
7893 if ((bmp = ill->ill_bcast_mp) != NULL) {
7894 dl_unitdata_req_t *dlur;
7895 uint8_t *bphys_addr;
7896 uint_t addrlen;
7897
7898 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7899 addrlen = dlur->dl_dest_addr_length;
7900 if (ill->ill_sap_length < 0) {
7901 bphys_addr = (uchar_t *)dlur +
7902 dlur->dl_dest_addr_offset;
7903 addrlen += ill->ill_sap_length;
7904 } else {
7905 bphys_addr = (uchar_t *)dlur +
7906 dlur->dl_dest_addr_offset +
7907 ill->ill_sap_length;
7908 addrlen -= ill->ill_sap_length;
7909 }
7910 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7911 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7912 }
7913 }
7914
7915 /*
7916 * Handle anything but M_DATA messages
7917 * We see the DL_UNITDATA_IND which are part
7918 * of the data path, and also the other messages from the driver.
7919 */
7920 void
7921 ip_rput_notdata(ill_t *ill, mblk_t *mp)
7922 {
7923 mblk_t *first_mp;
7924 struct iocblk *iocp;
7925 struct mac_header_info_s mhi;
7926
7927 switch (DB_TYPE(mp)) {
7928 case M_PROTO:
7929 case M_PCPROTO: {
7930 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7931 DL_UNITDATA_IND) {
7932 /* Go handle anything other than data elsewhere. */
7933 ip_rput_dlpi(ill, mp);
7934 return;
7935 }
7936
7937 first_mp = mp;
7938 mp = first_mp->b_cont;
7939 first_mp->b_cont = NULL;
7940
7941 if (mp == NULL) {
7942 freeb(first_mp);
7943 return;
7944 }
7945 ip_dlur_to_mhi(ill, first_mp, &mhi);
7946 if (ill->ill_isv6)
7947 ip_input_v6(ill, NULL, mp, &mhi);
7948 else
7949 ip_input(ill, NULL, mp, &mhi);
7950
7951 /* Ditch the DLPI header. */
7952 freeb(first_mp);
7953 return;
7954 }
7955 case M_IOCACK:
7956 iocp = (struct iocblk *)mp->b_rptr;
7957 switch (iocp->ioc_cmd) {
7958 case DL_IOC_HDR_INFO:
7959 ill_fastpath_ack(ill, mp);
7960 return;
7961 default:
7962 putnext(ill->ill_rq, mp);
7963 return;
7964 }
7965 /* FALLTHROUGH */
7966 case M_ERROR:
7967 case M_HANGUP:
7968 mutex_enter(&ill->ill_lock);
7969 if (ill->ill_state_flags & ILL_CONDEMNED) {
7970 mutex_exit(&ill->ill_lock);
7971 freemsg(mp);
7972 return;
7973 }
7974 ill_refhold_locked(ill);
7975 mutex_exit(&ill->ill_lock);
7976 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7977 B_FALSE);
7978 return;
7979 case M_CTL:
7980 putnext(ill->ill_rq, mp);
7981 return;
7982 case M_IOCNAK:
7983 ip1dbg(("got iocnak "));
7984 iocp = (struct iocblk *)mp->b_rptr;
7985 switch (iocp->ioc_cmd) {
7986 case DL_IOC_HDR_INFO:
7987 ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7988 return;
7989 default:
7990 break;
7991 }
7992 /* FALLTHROUGH */
7993 default:
7994 putnext(ill->ill_rq, mp);
7995 return;
7996 }
7997 }
7998
7999 /* Read side put procedure. Packets coming from the wire arrive here. */
8000 int
8001 ip_rput(queue_t *q, mblk_t *mp)
8002 {
8003 ill_t *ill;
8004 union DL_primitives *dl;
8005
8006 ill = (ill_t *)q->q_ptr;
8007
8008 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
8009 /*
8010 * If things are opening or closing, only accept high-priority
8011 * DLPI messages. (On open ill->ill_ipif has not yet been
8012 * created; on close, things hanging off the ill may have been
8013 * freed already.)
8014 */
8015 dl = (union DL_primitives *)mp->b_rptr;
8016 if (DB_TYPE(mp) != M_PCPROTO ||
8017 dl->dl_primitive == DL_UNITDATA_IND) {
8018 inet_freemsg(mp);
8019 return (0);
8020 }
8021 }
8022 if (DB_TYPE(mp) == M_DATA) {
8023 struct mac_header_info_s mhi;
8024
8025 ip_mdata_to_mhi(ill, mp, &mhi);
8026 ip_input(ill, NULL, mp, &mhi);
8027 } else {
8028 ip_rput_notdata(ill, mp);
8029 }
8030 return (0);
8031 }
8032
8033 /*
8034 * Move the information to a copy.
8035 */
8036 mblk_t *
8037 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
8038 {
8039 mblk_t *mp1;
8040 ill_t *ill = ira->ira_ill;
8041 ip_stack_t *ipst = ill->ill_ipst;
8042
8043 IP_STAT(ipst, ip_db_ref);
8044
8045 /* Make sure we have ira_l2src before we loose the original mblk */
8046 if (!(ira->ira_flags & IRAF_L2SRC_SET))
8047 ip_setl2src(mp, ira, ira->ira_rill);
8048
8049 mp1 = copymsg(mp);
8050 if (mp1 == NULL) {
8051 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8052 ip_drop_input("ipIfStatsInDiscards", mp, ill);
8053 freemsg(mp);
8054 return (NULL);
8055 }
8056 /* preserve the hardware checksum flags and data, if present */
8057 if (DB_CKSUMFLAGS(mp) != 0) {
8058 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
8059 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
8060 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
8061 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
8062 DB_CKSUM16(mp1) = DB_CKSUM16(mp);
8063 }
8064 freemsg(mp);
8065 return (mp1);
8066 }
8067
8068 static void
8069 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
8070 t_uscalar_t err)
8071 {
8072 if (dl_err == DL_SYSERR) {
8073 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8074 "%s: %s failed: DL_SYSERR (errno %u)\n",
8075 ill->ill_name, dl_primstr(prim), err);
8076 return;
8077 }
8078
8079 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8080 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
8081 dl_errstr(dl_err));
8082 }
8083
8084 /*
8085 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
8086 * than DL_UNITDATA_IND messages. If we need to process this message
8087 * exclusively, we call qwriter_ip, in which case we also need to call
8088 * ill_refhold before that, since qwriter_ip does an ill_refrele.
8089 */
8090 void
8091 ip_rput_dlpi(ill_t *ill, mblk_t *mp)
8092 {
8093 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8094 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8095 queue_t *q = ill->ill_rq;
8096 t_uscalar_t prim = dloa->dl_primitive;
8097 t_uscalar_t reqprim = DL_PRIM_INVAL;
8098
8099 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
8100 char *, dl_primstr(prim), ill_t *, ill);
8101 ip1dbg(("ip_rput_dlpi"));
8102
8103 /*
8104 * If we received an ACK but didn't send a request for it, then it
8105 * can't be part of any pending operation; discard up-front.
8106 */
8107 switch (prim) {
8108 case DL_ERROR_ACK:
8109 reqprim = dlea->dl_error_primitive;
8110 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
8111 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
8112 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
8113 dlea->dl_unix_errno));
8114 break;
8115 case DL_OK_ACK:
8116 reqprim = dloa->dl_correct_primitive;
8117 break;
8118 case DL_INFO_ACK:
8119 reqprim = DL_INFO_REQ;
8120 break;
8121 case DL_BIND_ACK:
8122 reqprim = DL_BIND_REQ;
8123 break;
8124 case DL_PHYS_ADDR_ACK:
8125 reqprim = DL_PHYS_ADDR_REQ;
8126 break;
8127 case DL_NOTIFY_ACK:
8128 reqprim = DL_NOTIFY_REQ;
8129 break;
8130 case DL_CAPABILITY_ACK:
8131 reqprim = DL_CAPABILITY_REQ;
8132 break;
8133 }
8134
8135 if (prim != DL_NOTIFY_IND) {
8136 if (reqprim == DL_PRIM_INVAL ||
8137 !ill_dlpi_pending(ill, reqprim)) {
8138 /* Not a DLPI message we support or expected */
8139 freemsg(mp);
8140 return;
8141 }
8142 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
8143 dl_primstr(reqprim)));
8144 }
8145
8146 switch (reqprim) {
8147 case DL_UNBIND_REQ:
8148 /*
8149 * NOTE: we mark the unbind as complete even if we got a
8150 * DL_ERROR_ACK, since there's not much else we can do.
8151 */
8152 mutex_enter(&ill->ill_lock);
8153 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
8154 cv_signal(&ill->ill_cv);
8155 mutex_exit(&ill->ill_lock);
8156 break;
8157
8158 case DL_ENABMULTI_REQ:
8159 if (prim == DL_OK_ACK) {
8160 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8161 ill->ill_dlpi_multicast_state = IDS_OK;
8162 }
8163 break;
8164 }
8165
8166 /*
8167 * The message is one we're waiting for (or DL_NOTIFY_IND), but we
8168 * need to become writer to continue to process it. Because an
8169 * exclusive operation doesn't complete until replies to all queued
8170 * DLPI messages have been received, we know we're in the middle of an
8171 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
8172 *
8173 * As required by qwriter_ip(), we refhold the ill; it will refrele.
8174 * Since this is on the ill stream we unconditionally bump up the
8175 * refcount without doing ILL_CAN_LOOKUP().
8176 */
8177 ill_refhold(ill);
8178 if (prim == DL_NOTIFY_IND)
8179 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
8180 else
8181 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
8182 }
8183
8184 /*
8185 * Handling of DLPI messages that require exclusive access to the ipsq.
8186 *
8187 * Need to do ipsq_pending_mp_get on ioctl completion, which could
8188 * happen here. (along with mi_copy_done)
8189 */
8190 /* ARGSUSED */
8191 static void
8192 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8193 {
8194 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8195 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8196 int err = 0;
8197 ill_t *ill = (ill_t *)q->q_ptr;
8198 ipif_t *ipif = NULL;
8199 mblk_t *mp1 = NULL;
8200 conn_t *connp = NULL;
8201 t_uscalar_t paddrreq;
8202 mblk_t *mp_hw;
8203 boolean_t ioctl_aborted = B_FALSE;
8204 boolean_t log = B_TRUE;
8205
8206 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
8207 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
8208
8209 ip1dbg(("ip_rput_dlpi_writer .."));
8210 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
8211 ASSERT(IAM_WRITER_ILL(ill));
8212
8213 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
8214 /*
8215 * The current ioctl could have been aborted by the user and a new
8216 * ioctl to bring up another ill could have started. We could still
8217 * get a response from the driver later.
8218 */
8219 if (ipif != NULL && ipif->ipif_ill != ill)
8220 ioctl_aborted = B_TRUE;
8221
8222 switch (dloa->dl_primitive) {
8223 case DL_ERROR_ACK:
8224 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
8225 dl_primstr(dlea->dl_error_primitive)));
8226
8227 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
8228 char *, dl_primstr(dlea->dl_error_primitive),
8229 ill_t *, ill);
8230
8231 switch (dlea->dl_error_primitive) {
8232 case DL_DISABMULTI_REQ:
8233 ill_dlpi_done(ill, dlea->dl_error_primitive);
8234 break;
8235 case DL_PROMISCON_REQ:
8236 case DL_PROMISCOFF_REQ:
8237 case DL_UNBIND_REQ:
8238 case DL_ATTACH_REQ:
8239 case DL_INFO_REQ:
8240 ill_dlpi_done(ill, dlea->dl_error_primitive);
8241 break;
8242 case DL_NOTIFY_REQ:
8243 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8244 log = B_FALSE;
8245 break;
8246 case DL_PHYS_ADDR_REQ:
8247 /*
8248 * For IPv6 only, there are two additional
8249 * phys_addr_req's sent to the driver to get the
8250 * IPv6 token and lla. This allows IP to acquire
8251 * the hardware address format for a given interface
8252 * without having built in knowledge of the hardware
8253 * address. ill_phys_addr_pend keeps track of the last
8254 * DL_PAR sent so we know which response we are
8255 * dealing with. ill_dlpi_done will update
8256 * ill_phys_addr_pend when it sends the next req.
8257 * We don't complete the IOCTL until all three DL_PARs
8258 * have been attempted, so set *_len to 0 and break.
8259 */
8260 paddrreq = ill->ill_phys_addr_pend;
8261 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8262 if (paddrreq == DL_IPV6_TOKEN) {
8263 ill->ill_token_length = 0;
8264 log = B_FALSE;
8265 break;
8266 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8267 ill->ill_nd_lla_len = 0;
8268 log = B_FALSE;
8269 break;
8270 }
8271 /*
8272 * Something went wrong with the DL_PHYS_ADDR_REQ.
8273 * We presumably have an IOCTL hanging out waiting
8274 * for completion. Find it and complete the IOCTL
8275 * with the error noted.
8276 * However, ill_dl_phys was called on an ill queue
8277 * (from SIOCSLIFNAME), thus conn_pending_ill is not
8278 * set. But the ioctl is known to be pending on ill_wq.
8279 */
8280 if (!ill->ill_ifname_pending)
8281 break;
8282 ill->ill_ifname_pending = 0;
8283 if (!ioctl_aborted)
8284 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8285 if (mp1 != NULL) {
8286 /*
8287 * This operation (SIOCSLIFNAME) must have
8288 * happened on the ill. Assert there is no conn
8289 */
8290 ASSERT(connp == NULL);
8291 q = ill->ill_wq;
8292 }
8293 break;
8294 case DL_BIND_REQ:
8295 ill_dlpi_done(ill, DL_BIND_REQ);
8296 if (ill->ill_ifname_pending)
8297 break;
8298 mutex_enter(&ill->ill_lock);
8299 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8300 mutex_exit(&ill->ill_lock);
8301 /*
8302 * Something went wrong with the bind. If this was the
8303 * result of a DL_NOTE_REPLUMB, then we presumably
8304 * have an IOCTL hanging out waiting for completion.
8305 * Find it, take down the interface that was coming
8306 * up, and complete the IOCTL with the error noted.
8307 */
8308 if (!ioctl_aborted)
8309 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8310 if (mp1 != NULL) {
8311 /*
8312 * This might be a result of a DL_NOTE_REPLUMB
8313 * notification. In that case, connp is NULL.
8314 */
8315 if (connp != NULL)
8316 q = CONNP_TO_WQ(connp);
8317
8318 (void) ipif_down(ipif, NULL, NULL);
8319 /* error is set below the switch */
8320 } else {
8321 /*
8322 * There's no pending IOCTL, so the bind was
8323 * most likely started by ill_dl_up(). We save
8324 * the error and let it take care of responding
8325 * to the IOCTL.
8326 */
8327 ill->ill_dl_bind_err = dlea->dl_unix_errno ?
8328 dlea->dl_unix_errno : ENXIO;
8329 }
8330 break;
8331 case DL_ENABMULTI_REQ:
8332 ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8333
8334 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8335 ill->ill_dlpi_multicast_state = IDS_FAILED;
8336 if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8337
8338 printf("ip: joining multicasts failed (%d)"
8339 " on %s - will use link layer "
8340 "broadcasts for multicast\n",
8341 dlea->dl_errno, ill->ill_name);
8342
8343 /*
8344 * Set up for multi_bcast; We are the
8345 * writer, so ok to access ill->ill_ipif
8346 * without any lock.
8347 */
8348 mutex_enter(&ill->ill_phyint->phyint_lock);
8349 ill->ill_phyint->phyint_flags |=
8350 PHYI_MULTI_BCAST;
8351 mutex_exit(&ill->ill_phyint->phyint_lock);
8352
8353 }
8354 freemsg(mp); /* Don't want to pass this up */
8355 return;
8356 case DL_CAPABILITY_REQ:
8357 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8358 "DL_CAPABILITY REQ\n"));
8359 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8360 ill->ill_dlpi_capab_state = IDCS_FAILED;
8361 ill_capability_done(ill);
8362 freemsg(mp);
8363 return;
8364 }
8365 /*
8366 * Note the error for IOCTL completion (mp1 is set when
8367 * ready to complete ioctl). If ill_ifname_pending_err is
8368 * set, an error occured during plumbing (ill_ifname_pending),
8369 * so we want to report that error.
8370 *
8371 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8372 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8373 * expected to get errack'd if the driver doesn't support
8374 * these flags (e.g. ethernet). log will be set to B_FALSE
8375 * if these error conditions are encountered.
8376 */
8377 if (mp1 != NULL) {
8378 if (ill->ill_ifname_pending_err != 0) {
8379 err = ill->ill_ifname_pending_err;
8380 ill->ill_ifname_pending_err = 0;
8381 } else {
8382 err = dlea->dl_unix_errno ?
8383 dlea->dl_unix_errno : ENXIO;
8384 }
8385 /*
8386 * If we're plumbing an interface and an error hasn't already
8387 * been saved, set ill_ifname_pending_err to the error passed
8388 * up. Ignore the error if log is B_FALSE (see comment above).
8389 */
8390 } else if (log && ill->ill_ifname_pending &&
8391 ill->ill_ifname_pending_err == 0) {
8392 ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8393 dlea->dl_unix_errno : ENXIO;
8394 }
8395
8396 if (log)
8397 ip_dlpi_error(ill, dlea->dl_error_primitive,
8398 dlea->dl_errno, dlea->dl_unix_errno);
8399 break;
8400 case DL_CAPABILITY_ACK:
8401 ill_capability_ack(ill, mp);
8402 /*
8403 * The message has been handed off to ill_capability_ack
8404 * and must not be freed below
8405 */
8406 mp = NULL;
8407 break;
8408
8409 case DL_INFO_ACK:
8410 /* Call a routine to handle this one. */
8411 ill_dlpi_done(ill, DL_INFO_REQ);
8412 ip_ll_subnet_defaults(ill, mp);
8413 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8414 return;
8415 case DL_BIND_ACK:
8416 /*
8417 * We should have an IOCTL waiting on this unless
8418 * sent by ill_dl_phys, in which case just return
8419 */
8420 ill_dlpi_done(ill, DL_BIND_REQ);
8421
8422 if (ill->ill_ifname_pending) {
8423 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8424 ill_t *, ill, mblk_t *, mp);
8425 break;
8426 }
8427 mutex_enter(&ill->ill_lock);
8428 ill->ill_dl_up = 1;
8429 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8430 mutex_exit(&ill->ill_lock);
8431
8432 if (!ioctl_aborted)
8433 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8434 if (mp1 == NULL) {
8435 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8436 break;
8437 }
8438 /*
8439 * mp1 was added by ill_dl_up(). if that is a result of
8440 * a DL_NOTE_REPLUMB notification, connp could be NULL.
8441 */
8442 if (connp != NULL)
8443 q = CONNP_TO_WQ(connp);
8444 /*
8445 * We are exclusive. So nothing can change even after
8446 * we get the pending mp.
8447 */
8448 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8449 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8450 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8451
8452 if (ill->ill_up_ipifs) {
8453 err = ill_up_ipifs(ill, q, mp1);
8454 if (err == EINPROGRESS) {
8455 freemsg(mp);
8456 return;
8457 }
8458 }
8459
8460 break;
8461
8462 case DL_NOTIFY_IND: {
8463 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8464 uint_t orig_mtu, orig_mc_mtu;
8465
8466 switch (notify->dl_notification) {
8467 case DL_NOTE_PHYS_ADDR:
8468 err = ill_set_phys_addr(ill, mp);
8469 break;
8470
8471 case DL_NOTE_REPLUMB:
8472 /*
8473 * Directly return after calling ill_replumb().
8474 * Note that we should not free mp as it is reused
8475 * in the ill_replumb() function.
8476 */
8477 err = ill_replumb(ill, mp);
8478 return;
8479
8480 case DL_NOTE_FASTPATH_FLUSH:
8481 nce_flush(ill, B_FALSE);
8482 break;
8483
8484 case DL_NOTE_SDU_SIZE:
8485 case DL_NOTE_SDU_SIZE2:
8486 /*
8487 * The dce and fragmentation code can cope with
8488 * this changing while packets are being sent.
8489 * When packets are sent ip_output will discover
8490 * a change.
8491 *
8492 * Change the MTU size of the interface.
8493 */
8494 mutex_enter(&ill->ill_lock);
8495 orig_mtu = ill->ill_mtu;
8496 orig_mc_mtu = ill->ill_mc_mtu;
8497 switch (notify->dl_notification) {
8498 case DL_NOTE_SDU_SIZE:
8499 ill->ill_current_frag =
8500 (uint_t)notify->dl_data;
8501 ill->ill_mc_mtu = (uint_t)notify->dl_data;
8502 break;
8503 case DL_NOTE_SDU_SIZE2:
8504 ill->ill_current_frag =
8505 (uint_t)notify->dl_data1;
8506 ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8507 break;
8508 }
8509 if (ill->ill_current_frag > ill->ill_max_frag)
8510 ill->ill_max_frag = ill->ill_current_frag;
8511
8512 if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8513 ill->ill_mtu = ill->ill_current_frag;
8514
8515 /*
8516 * If ill_user_mtu was set (via
8517 * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8518 */
8519 if (ill->ill_user_mtu != 0 &&
8520 ill->ill_user_mtu < ill->ill_mtu)
8521 ill->ill_mtu = ill->ill_user_mtu;
8522
8523 if (ill->ill_user_mtu != 0 &&
8524 ill->ill_user_mtu < ill->ill_mc_mtu)
8525 ill->ill_mc_mtu = ill->ill_user_mtu;
8526
8527 if (ill->ill_isv6) {
8528 if (ill->ill_mtu < IPV6_MIN_MTU)
8529 ill->ill_mtu = IPV6_MIN_MTU;
8530 if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8531 ill->ill_mc_mtu = IPV6_MIN_MTU;
8532 } else {
8533 if (ill->ill_mtu < IP_MIN_MTU)
8534 ill->ill_mtu = IP_MIN_MTU;
8535 if (ill->ill_mc_mtu < IP_MIN_MTU)
8536 ill->ill_mc_mtu = IP_MIN_MTU;
8537 }
8538 } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8539 ill->ill_mc_mtu = ill->ill_mtu;
8540 }
8541
8542 mutex_exit(&ill->ill_lock);
8543 /*
8544 * Make sure all dce_generation checks find out
8545 * that ill_mtu/ill_mc_mtu has changed.
8546 */
8547 if (orig_mtu != ill->ill_mtu ||
8548 orig_mc_mtu != ill->ill_mc_mtu) {
8549 dce_increment_all_generations(ill->ill_isv6,
8550 ill->ill_ipst);
8551 }
8552
8553 /*
8554 * Refresh IPMP meta-interface MTU if necessary.
8555 */
8556 if (IS_UNDER_IPMP(ill))
8557 ipmp_illgrp_refresh_mtu(ill->ill_grp);
8558 break;
8559
8560 case DL_NOTE_LINK_UP:
8561 case DL_NOTE_LINK_DOWN: {
8562 /*
8563 * We are writer. ill / phyint / ipsq assocs stable.
8564 * The RUNNING flag reflects the state of the link.
8565 */
8566 phyint_t *phyint = ill->ill_phyint;
8567 uint64_t new_phyint_flags;
8568 boolean_t changed = B_FALSE;
8569 boolean_t went_up;
8570
8571 went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8572 mutex_enter(&phyint->phyint_lock);
8573
8574 new_phyint_flags = went_up ?
8575 phyint->phyint_flags | PHYI_RUNNING :
8576 phyint->phyint_flags & ~PHYI_RUNNING;
8577
8578 if (IS_IPMP(ill)) {
8579 new_phyint_flags = went_up ?
8580 new_phyint_flags & ~PHYI_FAILED :
8581 new_phyint_flags | PHYI_FAILED;
8582 }
8583
8584 if (new_phyint_flags != phyint->phyint_flags) {
8585 phyint->phyint_flags = new_phyint_flags;
8586 changed = B_TRUE;
8587 }
8588 mutex_exit(&phyint->phyint_lock);
8589 /*
8590 * ill_restart_dad handles the DAD restart and routing
8591 * socket notification logic.
8592 */
8593 if (changed) {
8594 ill_restart_dad(phyint->phyint_illv4, went_up);
8595 ill_restart_dad(phyint->phyint_illv6, went_up);
8596 }
8597 break;
8598 }
8599 case DL_NOTE_PROMISC_ON_PHYS: {
8600 phyint_t *phyint = ill->ill_phyint;
8601
8602 mutex_enter(&phyint->phyint_lock);
8603 phyint->phyint_flags |= PHYI_PROMISC;
8604 mutex_exit(&phyint->phyint_lock);
8605 break;
8606 }
8607 case DL_NOTE_PROMISC_OFF_PHYS: {
8608 phyint_t *phyint = ill->ill_phyint;
8609
8610 mutex_enter(&phyint->phyint_lock);
8611 phyint->phyint_flags &= ~PHYI_PROMISC;
8612 mutex_exit(&phyint->phyint_lock);
8613 break;
8614 }
8615 case DL_NOTE_CAPAB_RENEG:
8616 /*
8617 * Something changed on the driver side.
8618 * It wants us to renegotiate the capabilities
8619 * on this ill. One possible cause is the aggregation
8620 * interface under us where a port got added or
8621 * went away.
8622 *
8623 * If the capability negotiation is already done
8624 * or is in progress, reset the capabilities and
8625 * mark the ill's ill_capab_reneg to be B_TRUE,
8626 * so that when the ack comes back, we can start
8627 * the renegotiation process.
8628 *
8629 * Note that if ill_capab_reneg is already B_TRUE
8630 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8631 * the capability resetting request has been sent
8632 * and the renegotiation has not been started yet;
8633 * nothing needs to be done in this case.
8634 */
8635 ipsq_current_start(ipsq, ill->ill_ipif, 0);
8636 ill_capability_reset(ill, B_TRUE);
8637 ipsq_current_finish(ipsq);
8638 break;
8639
8640 case DL_NOTE_ALLOWED_IPS:
8641 ill_set_allowed_ips(ill, mp);
8642 break;
8643 default:
8644 ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8645 "type 0x%x for DL_NOTIFY_IND\n",
8646 notify->dl_notification));
8647 break;
8648 }
8649
8650 /*
8651 * As this is an asynchronous operation, we
8652 * should not call ill_dlpi_done
8653 */
8654 break;
8655 }
8656 case DL_NOTIFY_ACK: {
8657 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8658
8659 if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8660 ill->ill_note_link = 1;
8661 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8662 break;
8663 }
8664 case DL_PHYS_ADDR_ACK: {
8665 /*
8666 * As part of plumbing the interface via SIOCSLIFNAME,
8667 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8668 * whose answers we receive here. As each answer is received,
8669 * we call ill_dlpi_done() to dispatch the next request as
8670 * we're processing the current one. Once all answers have
8671 * been received, we use ipsq_pending_mp_get() to dequeue the
8672 * outstanding IOCTL and reply to it. (Because ill_dl_phys()
8673 * is invoked from an ill queue, conn_oper_pending_ill is not
8674 * available, but we know the ioctl is pending on ill_wq.)
8675 */
8676 uint_t paddrlen, paddroff;
8677 uint8_t *addr;
8678
8679 paddrreq = ill->ill_phys_addr_pend;
8680 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8681 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8682 addr = mp->b_rptr + paddroff;
8683
8684 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8685 if (paddrreq == DL_IPV6_TOKEN) {
8686 /*
8687 * bcopy to low-order bits of ill_token
8688 *
8689 * XXX Temporary hack - currently, all known tokens
8690 * are 64 bits, so I'll cheat for the moment.
8691 */
8692 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8693 ill->ill_token_length = paddrlen;
8694 break;
8695 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8696 ASSERT(ill->ill_nd_lla_mp == NULL);
8697 ill_set_ndmp(ill, mp, paddroff, paddrlen);
8698 mp = NULL;
8699 break;
8700 } else if (paddrreq == DL_CURR_DEST_ADDR) {
8701 ASSERT(ill->ill_dest_addr_mp == NULL);
8702 ill->ill_dest_addr_mp = mp;
8703 ill->ill_dest_addr = addr;
8704 mp = NULL;
8705 if (ill->ill_isv6) {
8706 ill_setdesttoken(ill);
8707 ipif_setdestlinklocal(ill->ill_ipif);
8708 }
8709 break;
8710 }
8711
8712 ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8713 ASSERT(ill->ill_phys_addr_mp == NULL);
8714 if (!ill->ill_ifname_pending)
8715 break;
8716 ill->ill_ifname_pending = 0;
8717 if (!ioctl_aborted)
8718 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8719 if (mp1 != NULL) {
8720 ASSERT(connp == NULL);
8721 q = ill->ill_wq;
8722 }
8723 /*
8724 * If any error acks received during the plumbing sequence,
8725 * ill_ifname_pending_err will be set. Break out and send up
8726 * the error to the pending ioctl.
8727 */
8728 if (ill->ill_ifname_pending_err != 0) {
8729 err = ill->ill_ifname_pending_err;
8730 ill->ill_ifname_pending_err = 0;
8731 break;
8732 }
8733
8734 ill->ill_phys_addr_mp = mp;
8735 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8736 mp = NULL;
8737
8738 /*
8739 * If paddrlen or ill_phys_addr_length is zero, the DLPI
8740 * provider doesn't support physical addresses. We check both
8741 * paddrlen and ill_phys_addr_length because sppp (PPP) does
8742 * not have physical addresses, but historically adversises a
8743 * physical address length of 0 in its DL_INFO_ACK, but 6 in
8744 * its DL_PHYS_ADDR_ACK.
8745 */
8746 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8747 ill->ill_phys_addr = NULL;
8748 } else if (paddrlen != ill->ill_phys_addr_length) {
8749 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8750 paddrlen, ill->ill_phys_addr_length));
8751 err = EINVAL;
8752 break;
8753 }
8754
8755 if (ill->ill_nd_lla_mp == NULL) {
8756 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8757 err = ENOMEM;
8758 break;
8759 }
8760 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8761 }
8762
8763 if (ill->ill_isv6) {
8764 ill_setdefaulttoken(ill);
8765 ipif_setlinklocal(ill->ill_ipif);
8766 }
8767 break;
8768 }
8769 case DL_OK_ACK:
8770 ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8771 dl_primstr((int)dloa->dl_correct_primitive),
8772 dloa->dl_correct_primitive));
8773 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8774 char *, dl_primstr(dloa->dl_correct_primitive),
8775 ill_t *, ill);
8776
8777 switch (dloa->dl_correct_primitive) {
8778 case DL_ENABMULTI_REQ:
8779 case DL_DISABMULTI_REQ:
8780 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8781 break;
8782 case DL_PROMISCON_REQ:
8783 case DL_PROMISCOFF_REQ:
8784 case DL_UNBIND_REQ:
8785 case DL_ATTACH_REQ:
8786 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8787 break;
8788 }
8789 break;
8790 default:
8791 break;
8792 }
8793
8794 freemsg(mp);
8795 if (mp1 == NULL)
8796 return;
8797
8798 /*
8799 * The operation must complete without EINPROGRESS since
8800 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise,
8801 * the operation will be stuck forever inside the IPSQ.
8802 */
8803 ASSERT(err != EINPROGRESS);
8804
8805 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8806 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8807 ipif_t *, NULL);
8808
8809 switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8810 case 0:
8811 ipsq_current_finish(ipsq);
8812 break;
8813
8814 case SIOCSLIFNAME:
8815 case IF_UNITSEL: {
8816 ill_t *ill_other = ILL_OTHER(ill);
8817
8818 /*
8819 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8820 * ill has a peer which is in an IPMP group, then place ill
8821 * into the same group. One catch: although ifconfig plumbs
8822 * the appropriate IPMP meta-interface prior to plumbing this
8823 * ill, it is possible for multiple ifconfig applications to
8824 * race (or for another application to adjust plumbing), in
8825 * which case the IPMP meta-interface we need will be missing.
8826 * If so, kick the phyint out of the group.
8827 */
8828 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8829 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp;
8830 ipmp_illgrp_t *illg;
8831
8832 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8833 if (illg == NULL)
8834 ipmp_phyint_leave_grp(ill->ill_phyint);
8835 else
8836 ipmp_ill_join_illgrp(ill, illg);
8837 }
8838
8839 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8840 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8841 else
8842 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8843 break;
8844 }
8845 case SIOCLIFADDIF:
8846 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8847 break;
8848
8849 default:
8850 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8851 break;
8852 }
8853 }
8854
8855 /*
8856 * ip_rput_other is called by ip_rput to handle messages modifying the global
8857 * state in IP. If 'ipsq' is non-NULL, caller is writer on it.
8858 */
8859 /* ARGSUSED */
8860 void
8861 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8862 {
8863 ill_t *ill = q->q_ptr;
8864 struct iocblk *iocp;
8865
8866 ip1dbg(("ip_rput_other "));
8867 if (ipsq != NULL) {
8868 ASSERT(IAM_WRITER_IPSQ(ipsq));
8869 ASSERT(ipsq->ipsq_xop ==
8870 ill->ill_phyint->phyint_ipsq->ipsq_xop);
8871 }
8872
8873 switch (mp->b_datap->db_type) {
8874 case M_ERROR:
8875 case M_HANGUP:
8876 /*
8877 * The device has a problem. We force the ILL down. It can
8878 * be brought up again manually using SIOCSIFFLAGS (via
8879 * ifconfig or equivalent).
8880 */
8881 ASSERT(ipsq != NULL);
8882 if (mp->b_rptr < mp->b_wptr)
8883 ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8884 if (ill->ill_error == 0)
8885 ill->ill_error = ENXIO;
8886 if (!ill_down_start(q, mp))
8887 return;
8888 ipif_all_down_tail(ipsq, q, mp, NULL);
8889 break;
8890 case M_IOCNAK: {
8891 iocp = (struct iocblk *)mp->b_rptr;
8892
8893 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8894 /*
8895 * If this was the first attempt, turn off the fastpath
8896 * probing.
8897 */
8898 mutex_enter(&ill->ill_lock);
8899 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8900 ill->ill_dlpi_fastpath_state = IDS_FAILED;
8901 mutex_exit(&ill->ill_lock);
8902 /*
8903 * don't flush the nce_t entries: we use them
8904 * as an index to the ncec itself.
8905 */
8906 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8907 ill->ill_name));
8908 } else {
8909 mutex_exit(&ill->ill_lock);
8910 }
8911 freemsg(mp);
8912 break;
8913 }
8914 default:
8915 ASSERT(0);
8916 break;
8917 }
8918 }
8919
8920 /*
8921 * Update any source route, record route or timestamp options
8922 * When it fails it has consumed the message and BUMPed the MIB.
8923 */
8924 boolean_t
8925 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8926 ip_recv_attr_t *ira)
8927 {
8928 ipoptp_t opts;
8929 uchar_t *opt;
8930 uint8_t optval;
8931 uint8_t optlen;
8932 ipaddr_t dst;
8933 ipaddr_t ifaddr;
8934 uint32_t ts;
8935 timestruc_t now;
8936 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
8937
8938 ip2dbg(("ip_forward_options\n"));
8939 dst = ipha->ipha_dst;
8940 for (optval = ipoptp_first(&opts, ipha);
8941 optval != IPOPT_EOL;
8942 optval = ipoptp_next(&opts)) {
8943 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8944 opt = opts.ipoptp_cur;
8945 optlen = opts.ipoptp_len;
8946 ip2dbg(("ip_forward_options: opt %d, len %d\n",
8947 optval, opts.ipoptp_len));
8948 switch (optval) {
8949 uint32_t off;
8950 case IPOPT_SSRR:
8951 case IPOPT_LSRR:
8952 /* Check if adminstratively disabled */
8953 if (!ipst->ips_ip_forward_src_routed) {
8954 BUMP_MIB(dst_ill->ill_ip_mib,
8955 ipIfStatsForwProhibits);
8956 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
8957 mp, dst_ill);
8958 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
8959 ira);
8960 return (B_FALSE);
8961 }
8962 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
8963 /*
8964 * Must be partial since ip_input_options
8965 * checked for strict.
8966 */
8967 break;
8968 }
8969 off = opt[IPOPT_OFFSET];
8970 off--;
8971 redo_srr:
8972 if (optlen < IP_ADDR_LEN ||
8973 off > optlen - IP_ADDR_LEN) {
8974 /* End of source route */
8975 ip1dbg((
8976 "ip_forward_options: end of SR\n"));
8977 break;
8978 }
8979 /* Pick a reasonable address on the outbound if */
8980 ASSERT(dst_ill != NULL);
8981 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
8982 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
8983 NULL) != 0) {
8984 /* No source! Shouldn't happen */
8985 ifaddr = INADDR_ANY;
8986 }
8987 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
8988 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
8989 ip1dbg(("ip_forward_options: next hop 0x%x\n",
8990 ntohl(dst)));
8991
8992 /*
8993 * Check if our address is present more than
8994 * once as consecutive hops in source route.
8995 */
8996 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
8997 off += IP_ADDR_LEN;
8998 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
8999 goto redo_srr;
9000 }
9001 ipha->ipha_dst = dst;
9002 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9003 break;
9004 case IPOPT_RR:
9005 off = opt[IPOPT_OFFSET];
9006 off--;
9007 if (optlen < IP_ADDR_LEN ||
9008 off > optlen - IP_ADDR_LEN) {
9009 /* No more room - ignore */
9010 ip1dbg((
9011 "ip_forward_options: end of RR\n"));
9012 break;
9013 }
9014 /* Pick a reasonable address on the outbound if */
9015 ASSERT(dst_ill != NULL);
9016 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9017 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9018 NULL) != 0) {
9019 /* No source! Shouldn't happen */
9020 ifaddr = INADDR_ANY;
9021 }
9022 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9023 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9024 break;
9025 case IPOPT_TS:
9026 /* Insert timestamp if there is room */
9027 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9028 case IPOPT_TS_TSONLY:
9029 off = IPOPT_TS_TIMELEN;
9030 break;
9031 case IPOPT_TS_PRESPEC:
9032 case IPOPT_TS_PRESPEC_RFC791:
9033 /* Verify that the address matched */
9034 off = opt[IPOPT_OFFSET] - 1;
9035 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9036 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9037 /* Not for us */
9038 break;
9039 }
9040 /* FALLTHROUGH */
9041 case IPOPT_TS_TSANDADDR:
9042 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9043 break;
9044 default:
9045 /*
9046 * ip_*put_options should have already
9047 * dropped this packet.
9048 */
9049 cmn_err(CE_PANIC, "ip_forward_options: "
9050 "unknown IT - bug in ip_input_options?\n");
9051 }
9052 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9053 /* Increase overflow counter */
9054 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9055 opt[IPOPT_POS_OV_FLG] =
9056 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9057 (off << 4));
9058 break;
9059 }
9060 off = opt[IPOPT_OFFSET] - 1;
9061 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9062 case IPOPT_TS_PRESPEC:
9063 case IPOPT_TS_PRESPEC_RFC791:
9064 case IPOPT_TS_TSANDADDR:
9065 /* Pick a reasonable addr on the outbound if */
9066 ASSERT(dst_ill != NULL);
9067 if (ip_select_source_v4(dst_ill, INADDR_ANY,
9068 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
9069 NULL, NULL) != 0) {
9070 /* No source! Shouldn't happen */
9071 ifaddr = INADDR_ANY;
9072 }
9073 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9074 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9075 /* FALLTHROUGH */
9076 case IPOPT_TS_TSONLY:
9077 off = opt[IPOPT_OFFSET] - 1;
9078 /* Compute # of milliseconds since midnight */
9079 gethrestime(&now);
9080 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9081 NSEC2MSEC(now.tv_nsec);
9082 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9083 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9084 break;
9085 }
9086 break;
9087 }
9088 }
9089 return (B_TRUE);
9090 }
9091
9092 /*
9093 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
9094 * returns 'true' if there are still fragments left on the queue, in
9095 * which case we restart the timer.
9096 */
9097 void
9098 ill_frag_timer(void *arg)
9099 {
9100 ill_t *ill = (ill_t *)arg;
9101 boolean_t frag_pending;
9102 ip_stack_t *ipst = ill->ill_ipst;
9103 time_t timeout;
9104
9105 mutex_enter(&ill->ill_lock);
9106 ASSERT(!ill->ill_fragtimer_executing);
9107 if (ill->ill_state_flags & ILL_CONDEMNED) {
9108 ill->ill_frag_timer_id = 0;
9109 mutex_exit(&ill->ill_lock);
9110 return;
9111 }
9112 ill->ill_fragtimer_executing = 1;
9113 mutex_exit(&ill->ill_lock);
9114
9115 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9116 ipst->ips_ip_reassembly_timeout);
9117
9118 frag_pending = ill_frag_timeout(ill, timeout);
9119
9120 /*
9121 * Restart the timer, if we have fragments pending or if someone
9122 * wanted us to be scheduled again.
9123 */
9124 mutex_enter(&ill->ill_lock);
9125 ill->ill_fragtimer_executing = 0;
9126 ill->ill_frag_timer_id = 0;
9127 if (frag_pending || ill->ill_fragtimer_needrestart)
9128 ill_frag_timer_start(ill);
9129 mutex_exit(&ill->ill_lock);
9130 }
9131
9132 void
9133 ill_frag_timer_start(ill_t *ill)
9134 {
9135 ip_stack_t *ipst = ill->ill_ipst;
9136 clock_t timeo_ms;
9137
9138 ASSERT(MUTEX_HELD(&ill->ill_lock));
9139
9140 /* If the ill is closing or opening don't proceed */
9141 if (ill->ill_state_flags & ILL_CONDEMNED)
9142 return;
9143
9144 if (ill->ill_fragtimer_executing) {
9145 /*
9146 * ill_frag_timer is currently executing. Just record the
9147 * the fact that we want the timer to be restarted.
9148 * ill_frag_timer will post a timeout before it returns,
9149 * ensuring it will be called again.
9150 */
9151 ill->ill_fragtimer_needrestart = 1;
9152 return;
9153 }
9154
9155 if (ill->ill_frag_timer_id == 0) {
9156 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9157 ipst->ips_ip_reassembly_timeout) * SECONDS;
9158
9159 /*
9160 * The timer is neither running nor is the timeout handler
9161 * executing. Post a timeout so that ill_frag_timer will be
9162 * called
9163 */
9164 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
9165 MSEC_TO_TICK(timeo_ms >> 1));
9166 ill->ill_fragtimer_needrestart = 0;
9167 }
9168 }
9169
9170 /*
9171 * Update any source route, record route or timestamp options.
9172 * Check that we are at end of strict source route.
9173 * The options have already been checked for sanity in ip_input_options().
9174 */
9175 boolean_t
9176 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
9177 {
9178 ipoptp_t opts;
9179 uchar_t *opt;
9180 uint8_t optval;
9181 uint8_t optlen;
9182 ipaddr_t dst;
9183 ipaddr_t ifaddr;
9184 uint32_t ts;
9185 timestruc_t now;
9186 ill_t *ill = ira->ira_ill;
9187 ip_stack_t *ipst = ill->ill_ipst;
9188
9189 ip2dbg(("ip_input_local_options\n"));
9190
9191 for (optval = ipoptp_first(&opts, ipha);
9192 optval != IPOPT_EOL;
9193 optval = ipoptp_next(&opts)) {
9194 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9195 opt = opts.ipoptp_cur;
9196 optlen = opts.ipoptp_len;
9197 ip2dbg(("ip_input_local_options: opt %d, len %d\n",
9198 optval, optlen));
9199 switch (optval) {
9200 uint32_t off;
9201 case IPOPT_SSRR:
9202 case IPOPT_LSRR:
9203 off = opt[IPOPT_OFFSET];
9204 off--;
9205 if (optlen < IP_ADDR_LEN ||
9206 off > optlen - IP_ADDR_LEN) {
9207 /* End of source route */
9208 ip1dbg(("ip_input_local_options: end of SR\n"));
9209 break;
9210 }
9211 /*
9212 * This will only happen if two consecutive entries
9213 * in the source route contains our address or if
9214 * it is a packet with a loose source route which
9215 * reaches us before consuming the whole source route
9216 */
9217 ip1dbg(("ip_input_local_options: not end of SR\n"));
9218 if (optval == IPOPT_SSRR) {
9219 goto bad_src_route;
9220 }
9221 /*
9222 * Hack: instead of dropping the packet truncate the
9223 * source route to what has been used by filling the
9224 * rest with IPOPT_NOP.
9225 */
9226 opt[IPOPT_OLEN] = (uint8_t)off;
9227 while (off < optlen) {
9228 opt[off++] = IPOPT_NOP;
9229 }
9230 break;
9231 case IPOPT_RR:
9232 off = opt[IPOPT_OFFSET];
9233 off--;
9234 if (optlen < IP_ADDR_LEN ||
9235 off > optlen - IP_ADDR_LEN) {
9236 /* No more room - ignore */
9237 ip1dbg((
9238 "ip_input_local_options: end of RR\n"));
9239 break;
9240 }
9241 /* Pick a reasonable address on the outbound if */
9242 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9243 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9244 NULL) != 0) {
9245 /* No source! Shouldn't happen */
9246 ifaddr = INADDR_ANY;
9247 }
9248 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9249 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9250 break;
9251 case IPOPT_TS:
9252 /* Insert timestamp if there is romm */
9253 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9254 case IPOPT_TS_TSONLY:
9255 off = IPOPT_TS_TIMELEN;
9256 break;
9257 case IPOPT_TS_PRESPEC:
9258 case IPOPT_TS_PRESPEC_RFC791:
9259 /* Verify that the address matched */
9260 off = opt[IPOPT_OFFSET] - 1;
9261 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9262 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9263 /* Not for us */
9264 break;
9265 }
9266 /* FALLTHROUGH */
9267 case IPOPT_TS_TSANDADDR:
9268 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9269 break;
9270 default:
9271 /*
9272 * ip_*put_options should have already
9273 * dropped this packet.
9274 */
9275 cmn_err(CE_PANIC, "ip_input_local_options: "
9276 "unknown IT - bug in ip_input_options?\n");
9277 }
9278 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9279 /* Increase overflow counter */
9280 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9281 opt[IPOPT_POS_OV_FLG] =
9282 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9283 (off << 4));
9284 break;
9285 }
9286 off = opt[IPOPT_OFFSET] - 1;
9287 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9288 case IPOPT_TS_PRESPEC:
9289 case IPOPT_TS_PRESPEC_RFC791:
9290 case IPOPT_TS_TSANDADDR:
9291 /* Pick a reasonable addr on the outbound if */
9292 if (ip_select_source_v4(ill, INADDR_ANY,
9293 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9294 &ifaddr, NULL, NULL) != 0) {
9295 /* No source! Shouldn't happen */
9296 ifaddr = INADDR_ANY;
9297 }
9298 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9299 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9300 /* FALLTHROUGH */
9301 case IPOPT_TS_TSONLY:
9302 off = opt[IPOPT_OFFSET] - 1;
9303 /* Compute # of milliseconds since midnight */
9304 gethrestime(&now);
9305 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9306 NSEC2MSEC(now.tv_nsec);
9307 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9308 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9309 break;
9310 }
9311 break;
9312 }
9313 }
9314 return (B_TRUE);
9315
9316 bad_src_route:
9317 /* make sure we clear any indication of a hardware checksum */
9318 DB_CKSUMFLAGS(mp) = 0;
9319 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9320 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9321 return (B_FALSE);
9322
9323 }
9324
9325 /*
9326 * Process IP options in an inbound packet. Always returns the nexthop.
9327 * Normally this is the passed in nexthop, but if there is an option
9328 * that effects the nexthop (such as a source route) that will be returned.
9329 * Sets *errorp if there is an error, in which case an ICMP error has been sent
9330 * and mp freed.
9331 */
9332 ipaddr_t
9333 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9334 ip_recv_attr_t *ira, int *errorp)
9335 {
9336 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9337 ipoptp_t opts;
9338 uchar_t *opt;
9339 uint8_t optval;
9340 uint8_t optlen;
9341 intptr_t code = 0;
9342 ire_t *ire;
9343
9344 ip2dbg(("ip_input_options\n"));
9345 *errorp = 0;
9346 for (optval = ipoptp_first(&opts, ipha);
9347 optval != IPOPT_EOL;
9348 optval = ipoptp_next(&opts)) {
9349 opt = opts.ipoptp_cur;
9350 optlen = opts.ipoptp_len;
9351 ip2dbg(("ip_input_options: opt %d, len %d\n",
9352 optval, optlen));
9353 /*
9354 * Note: we need to verify the checksum before we
9355 * modify anything thus this routine only extracts the next
9356 * hop dst from any source route.
9357 */
9358 switch (optval) {
9359 uint32_t off;
9360 case IPOPT_SSRR:
9361 case IPOPT_LSRR:
9362 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9363 if (optval == IPOPT_SSRR) {
9364 ip1dbg(("ip_input_options: not next"
9365 " strict source route 0x%x\n",
9366 ntohl(dst)));
9367 code = (char *)&ipha->ipha_dst -
9368 (char *)ipha;
9369 goto param_prob; /* RouterReq's */
9370 }
9371 ip2dbg(("ip_input_options: "
9372 "not next source route 0x%x\n",
9373 ntohl(dst)));
9374 break;
9375 }
9376
9377 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9378 ip1dbg((
9379 "ip_input_options: bad option offset\n"));
9380 code = (char *)&opt[IPOPT_OLEN] -
9381 (char *)ipha;
9382 goto param_prob;
9383 }
9384 off = opt[IPOPT_OFFSET];
9385 off--;
9386 redo_srr:
9387 if (optlen < IP_ADDR_LEN ||
9388 off > optlen - IP_ADDR_LEN) {
9389 /* End of source route */
9390 ip1dbg(("ip_input_options: end of SR\n"));
9391 break;
9392 }
9393 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9394 ip1dbg(("ip_input_options: next hop 0x%x\n",
9395 ntohl(dst)));
9396
9397 /*
9398 * Check if our address is present more than
9399 * once as consecutive hops in source route.
9400 * XXX verify per-interface ip_forwarding
9401 * for source route?
9402 */
9403 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9404 off += IP_ADDR_LEN;
9405 goto redo_srr;
9406 }
9407
9408 if (dst == htonl(INADDR_LOOPBACK)) {
9409 ip1dbg(("ip_input_options: loopback addr in "
9410 "source route!\n"));
9411 goto bad_src_route;
9412 }
9413 /*
9414 * For strict: verify that dst is directly
9415 * reachable.
9416 */
9417 if (optval == IPOPT_SSRR) {
9418 ire = ire_ftable_lookup_v4(dst, 0, 0,
9419 IRE_INTERFACE, NULL, ALL_ZONES,
9420 ira->ira_tsl,
9421 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
9422 NULL);
9423 if (ire == NULL) {
9424 ip1dbg(("ip_input_options: SSRR not "
9425 "directly reachable: 0x%x\n",
9426 ntohl(dst)));
9427 goto bad_src_route;
9428 }
9429 ire_refrele(ire);
9430 }
9431 /*
9432 * Defer update of the offset and the record route
9433 * until the packet is forwarded.
9434 */
9435 break;
9436 case IPOPT_RR:
9437 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9438 ip1dbg((
9439 "ip_input_options: bad option offset\n"));
9440 code = (char *)&opt[IPOPT_OLEN] -
9441 (char *)ipha;
9442 goto param_prob;
9443 }
9444 break;
9445 case IPOPT_TS:
9446 /*
9447 * Verify that length >= 5 and that there is either
9448 * room for another timestamp or that the overflow
9449 * counter is not maxed out.
9450 */
9451 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9452 if (optlen < IPOPT_MINLEN_IT) {
9453 goto param_prob;
9454 }
9455 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9456 ip1dbg((
9457 "ip_input_options: bad option offset\n"));
9458 code = (char *)&opt[IPOPT_OFFSET] -
9459 (char *)ipha;
9460 goto param_prob;
9461 }
9462 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9463 case IPOPT_TS_TSONLY:
9464 off = IPOPT_TS_TIMELEN;
9465 break;
9466 case IPOPT_TS_TSANDADDR:
9467 case IPOPT_TS_PRESPEC:
9468 case IPOPT_TS_PRESPEC_RFC791:
9469 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9470 break;
9471 default:
9472 code = (char *)&opt[IPOPT_POS_OV_FLG] -
9473 (char *)ipha;
9474 goto param_prob;
9475 }
9476 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9477 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9478 /*
9479 * No room and the overflow counter is 15
9480 * already.
9481 */
9482 goto param_prob;
9483 }
9484 break;
9485 }
9486 }
9487
9488 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9489 return (dst);
9490 }
9491
9492 ip1dbg(("ip_input_options: error processing IP options."));
9493 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9494
9495 param_prob:
9496 /* make sure we clear any indication of a hardware checksum */
9497 DB_CKSUMFLAGS(mp) = 0;
9498 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9499 icmp_param_problem(mp, (uint8_t)code, ira);
9500 *errorp = -1;
9501 return (dst);
9502
9503 bad_src_route:
9504 /* make sure we clear any indication of a hardware checksum */
9505 DB_CKSUMFLAGS(mp) = 0;
9506 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9507 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9508 *errorp = -1;
9509 return (dst);
9510 }
9511
9512 /*
9513 * IP & ICMP info in >=14 msg's ...
9514 * - ip fixed part (mib2_ip_t)
9515 * - icmp fixed part (mib2_icmp_t)
9516 * - ipAddrEntryTable (ip 20) all IPv4 ipifs
9517 * - ipRouteEntryTable (ip 21) all IPv4 IREs
9518 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries
9519 * - ipRouteAttributeTable (ip 102) labeled routes
9520 * - ip multicast membership (ip_member_t)
9521 * - ip multicast source filtering (ip_grpsrc_t)
9522 * - igmp fixed part (struct igmpstat)
9523 * - multicast routing stats (struct mrtstat)
9524 * - multicast routing vifs (array of struct vifctl)
9525 * - multicast routing routes (array of struct mfcctl)
9526 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9527 * One per ill plus one generic
9528 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9529 * One per ill plus one generic
9530 * - ipv6RouteEntry all IPv6 IREs
9531 * - ipv6RouteAttributeTable (ip6 102) labeled routes
9532 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries
9533 * - ipv6AddrEntry all IPv6 ipifs
9534 * - ipv6 multicast membership (ipv6_member_t)
9535 * - ipv6 multicast source filtering (ipv6_grpsrc_t)
9536 *
9537 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9538 * already filled in by the caller.
9539 * If legacy_req is true then MIB structures needs to be truncated to their
9540 * legacy sizes before being returned.
9541 * Return value of 0 indicates that no messages were sent and caller
9542 * should free mpctl.
9543 */
9544 int
9545 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9546 {
9547 ip_stack_t *ipst;
9548 sctp_stack_t *sctps;
9549
9550 if (q->q_next != NULL) {
9551 ipst = ILLQ_TO_IPST(q);
9552 } else {
9553 ipst = CONNQ_TO_IPST(q);
9554 }
9555 ASSERT(ipst != NULL);
9556 sctps = ipst->ips_netstack->netstack_sctp;
9557
9558 if (mpctl == NULL || mpctl->b_cont == NULL) {
9559 return (0);
9560 }
9561
9562 /*
9563 * For the purposes of the (broken) packet shell use
9564 * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9565 * to make TCP and UDP appear first in the list of mib items.
9566 * TBD: We could expand this and use it in netstat so that
9567 * the kernel doesn't have to produce large tables (connections,
9568 * routes, etc) when netstat only wants the statistics or a particular
9569 * table.
9570 */
9571 if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9572 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9573 return (1);
9574 }
9575 }
9576
9577 if (level != MIB2_TCP) {
9578 if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9579 return (1);
9580 }
9581 if (level == MIB2_UDP) {
9582 goto done;
9583 }
9584 }
9585
9586 if (level != MIB2_UDP) {
9587 if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9588 return (1);
9589 }
9590 if (level == MIB2_TCP) {
9591 goto done;
9592 }
9593 }
9594
9595 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9596 ipst, legacy_req)) == NULL) {
9597 return (1);
9598 }
9599
9600 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9601 legacy_req)) == NULL) {
9602 return (1);
9603 }
9604
9605 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9606 return (1);
9607 }
9608
9609 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9610 return (1);
9611 }
9612
9613 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9614 return (1);
9615 }
9616
9617 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9618 return (1);
9619 }
9620
9621 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9622 legacy_req)) == NULL) {
9623 return (1);
9624 }
9625
9626 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9627 legacy_req)) == NULL) {
9628 return (1);
9629 }
9630
9631 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9632 return (1);
9633 }
9634
9635 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9636 return (1);
9637 }
9638
9639 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9640 return (1);
9641 }
9642
9643 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9644 return (1);
9645 }
9646
9647 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9648 return (1);
9649 }
9650
9651 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9652 return (1);
9653 }
9654
9655 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9656 if (mpctl == NULL)
9657 return (1);
9658
9659 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9660 if (mpctl == NULL)
9661 return (1);
9662
9663 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9664 return (1);
9665 }
9666 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9667 return (1);
9668 }
9669 done:
9670 freemsg(mpctl);
9671 return (1);
9672 }
9673
9674 /* Get global (legacy) IPv4 statistics */
9675 static mblk_t *
9676 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9677 ip_stack_t *ipst, boolean_t legacy_req)
9678 {
9679 mib2_ip_t old_ip_mib;
9680 struct opthdr *optp;
9681 mblk_t *mp2ctl;
9682 mib2_ipAddrEntry_t mae;
9683
9684 /*
9685 * make a copy of the original message
9686 */
9687 mp2ctl = copymsg(mpctl);
9688
9689 /* fixed length IP structure... */
9690 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9691 optp->level = MIB2_IP;
9692 optp->name = 0;
9693 SET_MIB(old_ip_mib.ipForwarding,
9694 (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9695 SET_MIB(old_ip_mib.ipDefaultTTL,
9696 (uint32_t)ipst->ips_ip_def_ttl);
9697 SET_MIB(old_ip_mib.ipReasmTimeout,
9698 ipst->ips_ip_reassembly_timeout);
9699 SET_MIB(old_ip_mib.ipAddrEntrySize,
9700 (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9701 sizeof (mib2_ipAddrEntry_t));
9702 SET_MIB(old_ip_mib.ipRouteEntrySize,
9703 sizeof (mib2_ipRouteEntry_t));
9704 SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9705 sizeof (mib2_ipNetToMediaEntry_t));
9706 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9707 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9708 SET_MIB(old_ip_mib.ipRouteAttributeSize,
9709 sizeof (mib2_ipAttributeEntry_t));
9710 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t));
9711 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9712
9713 /*
9714 * Grab the statistics from the new IP MIB
9715 */
9716 SET_MIB(old_ip_mib.ipInReceives,
9717 (uint32_t)ipmib->ipIfStatsHCInReceives);
9718 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9719 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9720 SET_MIB(old_ip_mib.ipForwDatagrams,
9721 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9722 SET_MIB(old_ip_mib.ipInUnknownProtos,
9723 ipmib->ipIfStatsInUnknownProtos);
9724 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9725 SET_MIB(old_ip_mib.ipInDelivers,
9726 (uint32_t)ipmib->ipIfStatsHCInDelivers);
9727 SET_MIB(old_ip_mib.ipOutRequests,
9728 (uint32_t)ipmib->ipIfStatsHCOutRequests);
9729 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9730 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9731 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9732 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9733 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9734 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9735 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9736 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9737
9738 /* ipRoutingDiscards is not being used */
9739 SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9740 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9741 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9742 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9743 SET_MIB(old_ip_mib.ipReasmDuplicates,
9744 ipmib->ipIfStatsReasmDuplicates);
9745 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9746 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9747 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9748 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9749 SET_MIB(old_ip_mib.rawipInOverflows,
9750 ipmib->rawipIfStatsInOverflows);
9751
9752 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9753 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9754 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9755 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9756 SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9757 ipmib->ipIfStatsOutSwitchIPVersion);
9758
9759 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9760 (int)sizeof (old_ip_mib))) {
9761 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9762 (uint_t)sizeof (old_ip_mib)));
9763 }
9764
9765 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9766 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9767 (int)optp->level, (int)optp->name, (int)optp->len));
9768 qreply(q, mpctl);
9769 return (mp2ctl);
9770 }
9771
9772 /* Per interface IPv4 statistics */
9773 static mblk_t *
9774 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9775 boolean_t legacy_req)
9776 {
9777 struct opthdr *optp;
9778 mblk_t *mp2ctl;
9779 ill_t *ill;
9780 ill_walk_context_t ctx;
9781 mblk_t *mp_tail = NULL;
9782 mib2_ipIfStatsEntry_t global_ip_mib;
9783 mib2_ipAddrEntry_t mae;
9784
9785 /*
9786 * Make a copy of the original message
9787 */
9788 mp2ctl = copymsg(mpctl);
9789
9790 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9791 optp->level = MIB2_IP;
9792 optp->name = MIB2_IP_TRAFFIC_STATS;
9793 /* Include "unknown interface" ip_mib */
9794 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9795 ipst->ips_ip_mib.ipIfStatsIfIndex =
9796 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9797 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9798 (ipst->ips_ip_forwarding ? 1 : 2));
9799 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9800 (uint32_t)ipst->ips_ip_def_ttl);
9801 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9802 sizeof (mib2_ipIfStatsEntry_t));
9803 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9804 sizeof (mib2_ipAddrEntry_t));
9805 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9806 sizeof (mib2_ipRouteEntry_t));
9807 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9808 sizeof (mib2_ipNetToMediaEntry_t));
9809 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9810 sizeof (ip_member_t));
9811 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9812 sizeof (ip_grpsrc_t));
9813
9814 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9815
9816 if (legacy_req) {
9817 SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9818 LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9819 }
9820
9821 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9822 (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9823 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9824 "failed to allocate %u bytes\n",
9825 (uint_t)sizeof (global_ip_mib)));
9826 }
9827
9828 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9829 ill = ILL_START_WALK_V4(&ctx, ipst);
9830 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9831 ill->ill_ip_mib->ipIfStatsIfIndex =
9832 ill->ill_phyint->phyint_ifindex;
9833 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9834 (ipst->ips_ip_forwarding ? 1 : 2));
9835 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9836 (uint32_t)ipst->ips_ip_def_ttl);
9837
9838 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9839 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9840 (char *)ill->ill_ip_mib,
9841 (int)sizeof (*ill->ill_ip_mib))) {
9842 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9843 "failed to allocate %u bytes\n",
9844 (uint_t)sizeof (*ill->ill_ip_mib)));
9845 }
9846 }
9847 rw_exit(&ipst->ips_ill_g_lock);
9848
9849 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9850 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9851 "level %d, name %d, len %d\n",
9852 (int)optp->level, (int)optp->name, (int)optp->len));
9853 qreply(q, mpctl);
9854
9855 if (mp2ctl == NULL)
9856 return (NULL);
9857
9858 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9859 legacy_req));
9860 }
9861
9862 /* Global IPv4 ICMP statistics */
9863 static mblk_t *
9864 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9865 {
9866 struct opthdr *optp;
9867 mblk_t *mp2ctl;
9868
9869 /*
9870 * Make a copy of the original message
9871 */
9872 mp2ctl = copymsg(mpctl);
9873
9874 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9875 optp->level = MIB2_ICMP;
9876 optp->name = 0;
9877 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9878 (int)sizeof (ipst->ips_icmp_mib))) {
9879 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9880 (uint_t)sizeof (ipst->ips_icmp_mib)));
9881 }
9882 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9883 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9884 (int)optp->level, (int)optp->name, (int)optp->len));
9885 qreply(q, mpctl);
9886 return (mp2ctl);
9887 }
9888
9889 /* Global IPv4 IGMP statistics */
9890 static mblk_t *
9891 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9892 {
9893 struct opthdr *optp;
9894 mblk_t *mp2ctl;
9895
9896 /*
9897 * make a copy of the original message
9898 */
9899 mp2ctl = copymsg(mpctl);
9900
9901 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9902 optp->level = EXPER_IGMP;
9903 optp->name = 0;
9904 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9905 (int)sizeof (ipst->ips_igmpstat))) {
9906 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9907 (uint_t)sizeof (ipst->ips_igmpstat)));
9908 }
9909 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9910 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9911 (int)optp->level, (int)optp->name, (int)optp->len));
9912 qreply(q, mpctl);
9913 return (mp2ctl);
9914 }
9915
9916 /* Global IPv4 Multicast Routing statistics */
9917 static mblk_t *
9918 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9919 {
9920 struct opthdr *optp;
9921 mblk_t *mp2ctl;
9922
9923 /*
9924 * make a copy of the original message
9925 */
9926 mp2ctl = copymsg(mpctl);
9927
9928 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9929 optp->level = EXPER_DVMRP;
9930 optp->name = 0;
9931 if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9932 ip0dbg(("ip_mroute_stats: failed\n"));
9933 }
9934 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9935 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9936 (int)optp->level, (int)optp->name, (int)optp->len));
9937 qreply(q, mpctl);
9938 return (mp2ctl);
9939 }
9940
9941 /* IPv4 address information */
9942 static mblk_t *
9943 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9944 boolean_t legacy_req)
9945 {
9946 struct opthdr *optp;
9947 mblk_t *mp2ctl;
9948 mblk_t *mp_tail = NULL;
9949 ill_t *ill;
9950 ipif_t *ipif;
9951 uint_t bitval;
9952 mib2_ipAddrEntry_t mae;
9953 size_t mae_size;
9954 zoneid_t zoneid;
9955 ill_walk_context_t ctx;
9956
9957 /*
9958 * make a copy of the original message
9959 */
9960 mp2ctl = copymsg(mpctl);
9961
9962 mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9963 sizeof (mib2_ipAddrEntry_t);
9964
9965 /* ipAddrEntryTable */
9966
9967 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9968 optp->level = MIB2_IP;
9969 optp->name = MIB2_IP_ADDR;
9970 zoneid = Q_TO_CONN(q)->conn_zoneid;
9971
9972 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9973 ill = ILL_START_WALK_V4(&ctx, ipst);
9974 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9975 for (ipif = ill->ill_ipif; ipif != NULL;
9976 ipif = ipif->ipif_next) {
9977 if (ipif->ipif_zoneid != zoneid &&
9978 ipif->ipif_zoneid != ALL_ZONES)
9979 continue;
9980 /* Sum of count from dead IRE_LO* and our current */
9981 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
9982 if (ipif->ipif_ire_local != NULL) {
9983 mae.ipAdEntInfo.ae_ibcnt +=
9984 ipif->ipif_ire_local->ire_ib_pkt_count;
9985 }
9986 mae.ipAdEntInfo.ae_obcnt = 0;
9987 mae.ipAdEntInfo.ae_focnt = 0;
9988
9989 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
9990 OCTET_LENGTH);
9991 mae.ipAdEntIfIndex.o_length =
9992 mi_strlen(mae.ipAdEntIfIndex.o_bytes);
9993 mae.ipAdEntAddr = ipif->ipif_lcl_addr;
9994 mae.ipAdEntNetMask = ipif->ipif_net_mask;
9995 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
9996 mae.ipAdEntInfo.ae_subnet_len =
9997 ip_mask_to_plen(ipif->ipif_net_mask);
9998 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
9999 for (bitval = 1;
10000 bitval &&
10001 !(bitval & ipif->ipif_brd_addr);
10002 bitval <<= 1)
10003 noop;
10004 mae.ipAdEntBcastAddr = bitval;
10005 mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
10006 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10007 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric;
10008 mae.ipAdEntInfo.ae_broadcast_addr =
10009 ipif->ipif_brd_addr;
10010 mae.ipAdEntInfo.ae_pp_dst_addr =
10011 ipif->ipif_pp_dst_addr;
10012 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
10013 ill->ill_flags | ill->ill_phyint->phyint_flags;
10014 mae.ipAdEntRetransmitTime =
10015 ill->ill_reachable_retrans_time;
10016
10017 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10018 (char *)&mae, (int)mae_size)) {
10019 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
10020 "allocate %u bytes\n", (uint_t)mae_size));
10021 }
10022 }
10023 }
10024 rw_exit(&ipst->ips_ill_g_lock);
10025
10026 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10027 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
10028 (int)optp->level, (int)optp->name, (int)optp->len));
10029 qreply(q, mpctl);
10030 return (mp2ctl);
10031 }
10032
10033 /* IPv6 address information */
10034 static mblk_t *
10035 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10036 boolean_t legacy_req)
10037 {
10038 struct opthdr *optp;
10039 mblk_t *mp2ctl;
10040 mblk_t *mp_tail = NULL;
10041 ill_t *ill;
10042 ipif_t *ipif;
10043 mib2_ipv6AddrEntry_t mae6;
10044 size_t mae6_size;
10045 zoneid_t zoneid;
10046 ill_walk_context_t ctx;
10047
10048 /*
10049 * make a copy of the original message
10050 */
10051 mp2ctl = copymsg(mpctl);
10052
10053 mae6_size = (legacy_req) ?
10054 LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
10055 sizeof (mib2_ipv6AddrEntry_t);
10056
10057 /* ipv6AddrEntryTable */
10058
10059 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10060 optp->level = MIB2_IP6;
10061 optp->name = MIB2_IP6_ADDR;
10062 zoneid = Q_TO_CONN(q)->conn_zoneid;
10063
10064 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10065 ill = ILL_START_WALK_V6(&ctx, ipst);
10066 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10067 for (ipif = ill->ill_ipif; ipif != NULL;
10068 ipif = ipif->ipif_next) {
10069 if (ipif->ipif_zoneid != zoneid &&
10070 ipif->ipif_zoneid != ALL_ZONES)
10071 continue;
10072 /* Sum of count from dead IRE_LO* and our current */
10073 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10074 if (ipif->ipif_ire_local != NULL) {
10075 mae6.ipv6AddrInfo.ae_ibcnt +=
10076 ipif->ipif_ire_local->ire_ib_pkt_count;
10077 }
10078 mae6.ipv6AddrInfo.ae_obcnt = 0;
10079 mae6.ipv6AddrInfo.ae_focnt = 0;
10080
10081 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
10082 OCTET_LENGTH);
10083 mae6.ipv6AddrIfIndex.o_length =
10084 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
10085 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
10086 mae6.ipv6AddrPfxLength =
10087 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10088 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
10089 mae6.ipv6AddrInfo.ae_subnet_len =
10090 mae6.ipv6AddrPfxLength;
10091 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
10092
10093 /* Type: stateless(1), stateful(2), unknown(3) */
10094 if (ipif->ipif_flags & IPIF_ADDRCONF)
10095 mae6.ipv6AddrType = 1;
10096 else
10097 mae6.ipv6AddrType = 2;
10098 /* Anycast: true(1), false(2) */
10099 if (ipif->ipif_flags & IPIF_ANYCAST)
10100 mae6.ipv6AddrAnycastFlag = 1;
10101 else
10102 mae6.ipv6AddrAnycastFlag = 2;
10103
10104 /*
10105 * Address status: preferred(1), deprecated(2),
10106 * invalid(3), inaccessible(4), unknown(5)
10107 */
10108 if (ipif->ipif_flags & IPIF_NOLOCAL)
10109 mae6.ipv6AddrStatus = 3;
10110 else if (ipif->ipif_flags & IPIF_DEPRECATED)
10111 mae6.ipv6AddrStatus = 2;
10112 else
10113 mae6.ipv6AddrStatus = 1;
10114 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10115 mae6.ipv6AddrInfo.ae_metric =
10116 ipif->ipif_ill->ill_metric;
10117 mae6.ipv6AddrInfo.ae_pp_dst_addr =
10118 ipif->ipif_v6pp_dst_addr;
10119 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
10120 ill->ill_flags | ill->ill_phyint->phyint_flags;
10121 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
10122 mae6.ipv6AddrIdentifier = ill->ill_token;
10123 mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
10124 mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
10125 mae6.ipv6AddrRetransmitTime =
10126 ill->ill_reachable_retrans_time;
10127 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10128 (char *)&mae6, (int)mae6_size)) {
10129 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
10130 "allocate %u bytes\n",
10131 (uint_t)mae6_size));
10132 }
10133 }
10134 }
10135 rw_exit(&ipst->ips_ill_g_lock);
10136
10137 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10138 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
10139 (int)optp->level, (int)optp->name, (int)optp->len));
10140 qreply(q, mpctl);
10141 return (mp2ctl);
10142 }
10143
10144 /* IPv4 multicast group membership. */
10145 static mblk_t *
10146 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10147 {
10148 struct opthdr *optp;
10149 mblk_t *mp2ctl;
10150 ill_t *ill;
10151 ipif_t *ipif;
10152 ilm_t *ilm;
10153 ip_member_t ipm;
10154 mblk_t *mp_tail = NULL;
10155 ill_walk_context_t ctx;
10156 zoneid_t zoneid;
10157
10158 /*
10159 * make a copy of the original message
10160 */
10161 mp2ctl = copymsg(mpctl);
10162 zoneid = Q_TO_CONN(q)->conn_zoneid;
10163
10164 /* ipGroupMember table */
10165 optp = (struct opthdr *)&mpctl->b_rptr[
10166 sizeof (struct T_optmgmt_ack)];
10167 optp->level = MIB2_IP;
10168 optp->name = EXPER_IP_GROUP_MEMBERSHIP;
10169
10170 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10171 ill = ILL_START_WALK_V4(&ctx, ipst);
10172 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10173 /* Make sure the ill isn't going away. */
10174 if (!ill_check_and_refhold(ill))
10175 continue;
10176 rw_exit(&ipst->ips_ill_g_lock);
10177 rw_enter(&ill->ill_mcast_lock, RW_READER);
10178 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10179 if (ilm->ilm_zoneid != zoneid &&
10180 ilm->ilm_zoneid != ALL_ZONES)
10181 continue;
10182
10183 /* Is there an ipif for ilm_ifaddr? */
10184 for (ipif = ill->ill_ipif; ipif != NULL;
10185 ipif = ipif->ipif_next) {
10186 if (!IPIF_IS_CONDEMNED(ipif) &&
10187 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10188 ilm->ilm_ifaddr != INADDR_ANY)
10189 break;
10190 }
10191 if (ipif != NULL) {
10192 ipif_get_name(ipif,
10193 ipm.ipGroupMemberIfIndex.o_bytes,
10194 OCTET_LENGTH);
10195 } else {
10196 ill_get_name(ill,
10197 ipm.ipGroupMemberIfIndex.o_bytes,
10198 OCTET_LENGTH);
10199 }
10200 ipm.ipGroupMemberIfIndex.o_length =
10201 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
10202
10203 ipm.ipGroupMemberAddress = ilm->ilm_addr;
10204 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
10205 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
10206 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10207 (char *)&ipm, (int)sizeof (ipm))) {
10208 ip1dbg(("ip_snmp_get_mib2_ip_group: "
10209 "failed to allocate %u bytes\n",
10210 (uint_t)sizeof (ipm)));
10211 }
10212 }
10213 rw_exit(&ill->ill_mcast_lock);
10214 ill_refrele(ill);
10215 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10216 }
10217 rw_exit(&ipst->ips_ill_g_lock);
10218 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10219 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10220 (int)optp->level, (int)optp->name, (int)optp->len));
10221 qreply(q, mpctl);
10222 return (mp2ctl);
10223 }
10224
10225 /* IPv6 multicast group membership. */
10226 static mblk_t *
10227 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10228 {
10229 struct opthdr *optp;
10230 mblk_t *mp2ctl;
10231 ill_t *ill;
10232 ilm_t *ilm;
10233 ipv6_member_t ipm6;
10234 mblk_t *mp_tail = NULL;
10235 ill_walk_context_t ctx;
10236 zoneid_t zoneid;
10237
10238 /*
10239 * make a copy of the original message
10240 */
10241 mp2ctl = copymsg(mpctl);
10242 zoneid = Q_TO_CONN(q)->conn_zoneid;
10243
10244 /* ip6GroupMember table */
10245 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10246 optp->level = MIB2_IP6;
10247 optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10248
10249 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10250 ill = ILL_START_WALK_V6(&ctx, ipst);
10251 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10252 /* Make sure the ill isn't going away. */
10253 if (!ill_check_and_refhold(ill))
10254 continue;
10255 rw_exit(&ipst->ips_ill_g_lock);
10256 /*
10257 * Normally we don't have any members on under IPMP interfaces.
10258 * We report them as a debugging aid.
10259 */
10260 rw_enter(&ill->ill_mcast_lock, RW_READER);
10261 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10262 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10263 if (ilm->ilm_zoneid != zoneid &&
10264 ilm->ilm_zoneid != ALL_ZONES)
10265 continue; /* not this zone */
10266 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10267 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10268 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10269 if (!snmp_append_data2(mpctl->b_cont,
10270 &mp_tail,
10271 (char *)&ipm6, (int)sizeof (ipm6))) {
10272 ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10273 "failed to allocate %u bytes\n",
10274 (uint_t)sizeof (ipm6)));
10275 }
10276 }
10277 rw_exit(&ill->ill_mcast_lock);
10278 ill_refrele(ill);
10279 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10280 }
10281 rw_exit(&ipst->ips_ill_g_lock);
10282
10283 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10284 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10285 (int)optp->level, (int)optp->name, (int)optp->len));
10286 qreply(q, mpctl);
10287 return (mp2ctl);
10288 }
10289
10290 /* IP multicast filtered sources */
10291 static mblk_t *
10292 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10293 {
10294 struct opthdr *optp;
10295 mblk_t *mp2ctl;
10296 ill_t *ill;
10297 ipif_t *ipif;
10298 ilm_t *ilm;
10299 ip_grpsrc_t ips;
10300 mblk_t *mp_tail = NULL;
10301 ill_walk_context_t ctx;
10302 zoneid_t zoneid;
10303 int i;
10304 slist_t *sl;
10305
10306 /*
10307 * make a copy of the original message
10308 */
10309 mp2ctl = copymsg(mpctl);
10310 zoneid = Q_TO_CONN(q)->conn_zoneid;
10311
10312 /* ipGroupSource table */
10313 optp = (struct opthdr *)&mpctl->b_rptr[
10314 sizeof (struct T_optmgmt_ack)];
10315 optp->level = MIB2_IP;
10316 optp->name = EXPER_IP_GROUP_SOURCES;
10317
10318 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10319 ill = ILL_START_WALK_V4(&ctx, ipst);
10320 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10321 /* Make sure the ill isn't going away. */
10322 if (!ill_check_and_refhold(ill))
10323 continue;
10324 rw_exit(&ipst->ips_ill_g_lock);
10325 rw_enter(&ill->ill_mcast_lock, RW_READER);
10326 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10327 sl = ilm->ilm_filter;
10328 if (ilm->ilm_zoneid != zoneid &&
10329 ilm->ilm_zoneid != ALL_ZONES)
10330 continue;
10331 if (SLIST_IS_EMPTY(sl))
10332 continue;
10333
10334 /* Is there an ipif for ilm_ifaddr? */
10335 for (ipif = ill->ill_ipif; ipif != NULL;
10336 ipif = ipif->ipif_next) {
10337 if (!IPIF_IS_CONDEMNED(ipif) &&
10338 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10339 ilm->ilm_ifaddr != INADDR_ANY)
10340 break;
10341 }
10342 if (ipif != NULL) {
10343 ipif_get_name(ipif,
10344 ips.ipGroupSourceIfIndex.o_bytes,
10345 OCTET_LENGTH);
10346 } else {
10347 ill_get_name(ill,
10348 ips.ipGroupSourceIfIndex.o_bytes,
10349 OCTET_LENGTH);
10350 }
10351 ips.ipGroupSourceIfIndex.o_length =
10352 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10353
10354 ips.ipGroupSourceGroup = ilm->ilm_addr;
10355 for (i = 0; i < sl->sl_numsrc; i++) {
10356 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10357 continue;
10358 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10359 ips.ipGroupSourceAddress);
10360 if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10361 (char *)&ips, (int)sizeof (ips)) == 0) {
10362 ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10363 " failed to allocate %u bytes\n",
10364 (uint_t)sizeof (ips)));
10365 }
10366 }
10367 }
10368 rw_exit(&ill->ill_mcast_lock);
10369 ill_refrele(ill);
10370 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10371 }
10372 rw_exit(&ipst->ips_ill_g_lock);
10373 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10374 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10375 (int)optp->level, (int)optp->name, (int)optp->len));
10376 qreply(q, mpctl);
10377 return (mp2ctl);
10378 }
10379
10380 /* IPv6 multicast filtered sources. */
10381 static mblk_t *
10382 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10383 {
10384 struct opthdr *optp;
10385 mblk_t *mp2ctl;
10386 ill_t *ill;
10387 ilm_t *ilm;
10388 ipv6_grpsrc_t ips6;
10389 mblk_t *mp_tail = NULL;
10390 ill_walk_context_t ctx;
10391 zoneid_t zoneid;
10392 int i;
10393 slist_t *sl;
10394
10395 /*
10396 * make a copy of the original message
10397 */
10398 mp2ctl = copymsg(mpctl);
10399 zoneid = Q_TO_CONN(q)->conn_zoneid;
10400
10401 /* ip6GroupMember table */
10402 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10403 optp->level = MIB2_IP6;
10404 optp->name = EXPER_IP6_GROUP_SOURCES;
10405
10406 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10407 ill = ILL_START_WALK_V6(&ctx, ipst);
10408 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10409 /* Make sure the ill isn't going away. */
10410 if (!ill_check_and_refhold(ill))
10411 continue;
10412 rw_exit(&ipst->ips_ill_g_lock);
10413 /*
10414 * Normally we don't have any members on under IPMP interfaces.
10415 * We report them as a debugging aid.
10416 */
10417 rw_enter(&ill->ill_mcast_lock, RW_READER);
10418 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10419 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10420 sl = ilm->ilm_filter;
10421 if (ilm->ilm_zoneid != zoneid &&
10422 ilm->ilm_zoneid != ALL_ZONES)
10423 continue;
10424 if (SLIST_IS_EMPTY(sl))
10425 continue;
10426 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10427 for (i = 0; i < sl->sl_numsrc; i++) {
10428 ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10429 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10430 (char *)&ips6, (int)sizeof (ips6))) {
10431 ip1dbg(("ip_snmp_get_mib2_ip6_"
10432 "group_src: failed to allocate "
10433 "%u bytes\n",
10434 (uint_t)sizeof (ips6)));
10435 }
10436 }
10437 }
10438 rw_exit(&ill->ill_mcast_lock);
10439 ill_refrele(ill);
10440 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10441 }
10442 rw_exit(&ipst->ips_ill_g_lock);
10443
10444 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10445 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10446 (int)optp->level, (int)optp->name, (int)optp->len));
10447 qreply(q, mpctl);
10448 return (mp2ctl);
10449 }
10450
10451 /* Multicast routing virtual interface table. */
10452 static mblk_t *
10453 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10454 {
10455 struct opthdr *optp;
10456 mblk_t *mp2ctl;
10457
10458 /*
10459 * make a copy of the original message
10460 */
10461 mp2ctl = copymsg(mpctl);
10462
10463 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10464 optp->level = EXPER_DVMRP;
10465 optp->name = EXPER_DVMRP_VIF;
10466 if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10467 ip0dbg(("ip_mroute_vif: failed\n"));
10468 }
10469 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10470 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10471 (int)optp->level, (int)optp->name, (int)optp->len));
10472 qreply(q, mpctl);
10473 return (mp2ctl);
10474 }
10475
10476 /* Multicast routing table. */
10477 static mblk_t *
10478 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10479 {
10480 struct opthdr *optp;
10481 mblk_t *mp2ctl;
10482
10483 /*
10484 * make a copy of the original message
10485 */
10486 mp2ctl = copymsg(mpctl);
10487
10488 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10489 optp->level = EXPER_DVMRP;
10490 optp->name = EXPER_DVMRP_MRT;
10491 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10492 ip0dbg(("ip_mroute_mrt: failed\n"));
10493 }
10494 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10495 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10496 (int)optp->level, (int)optp->name, (int)optp->len));
10497 qreply(q, mpctl);
10498 return (mp2ctl);
10499 }
10500
10501 /*
10502 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable
10503 * in one IRE walk.
10504 */
10505 static mblk_t *
10506 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10507 ip_stack_t *ipst)
10508 {
10509 struct opthdr *optp;
10510 mblk_t *mp2ctl; /* Returned */
10511 mblk_t *mp3ctl; /* nettomedia */
10512 mblk_t *mp4ctl; /* routeattrs */
10513 iproutedata_t ird;
10514 zoneid_t zoneid;
10515
10516 /*
10517 * make copies of the original message
10518 * - mp2ctl is returned unchanged to the caller for its use
10519 * - mpctl is sent upstream as ipRouteEntryTable
10520 * - mp3ctl is sent upstream as ipNetToMediaEntryTable
10521 * - mp4ctl is sent upstream as ipRouteAttributeTable
10522 */
10523 mp2ctl = copymsg(mpctl);
10524 mp3ctl = copymsg(mpctl);
10525 mp4ctl = copymsg(mpctl);
10526 if (mp3ctl == NULL || mp4ctl == NULL) {
10527 freemsg(mp4ctl);
10528 freemsg(mp3ctl);
10529 freemsg(mp2ctl);
10530 freemsg(mpctl);
10531 return (NULL);
10532 }
10533
10534 bzero(&ird, sizeof (ird));
10535
10536 ird.ird_route.lp_head = mpctl->b_cont;
10537 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10538 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10539 /*
10540 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10541 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10542 * intended a temporary solution until a proper MIB API is provided
10543 * that provides complete filtering/caller-opt-in.
10544 */
10545 if (level == EXPER_IP_AND_ALL_IRES)
10546 ird.ird_flags |= IRD_REPORT_ALL;
10547
10548 zoneid = Q_TO_CONN(q)->conn_zoneid;
10549 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10550
10551 /* ipRouteEntryTable in mpctl */
10552 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10553 optp->level = MIB2_IP;
10554 optp->name = MIB2_IP_ROUTE;
10555 optp->len = msgdsize(ird.ird_route.lp_head);
10556 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10557 (int)optp->level, (int)optp->name, (int)optp->len));
10558 qreply(q, mpctl);
10559
10560 /* ipNetToMediaEntryTable in mp3ctl */
10561 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10562
10563 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10564 optp->level = MIB2_IP;
10565 optp->name = MIB2_IP_MEDIA;
10566 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10567 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10568 (int)optp->level, (int)optp->name, (int)optp->len));
10569 qreply(q, mp3ctl);
10570
10571 /* ipRouteAttributeTable in mp4ctl */
10572 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10573 optp->level = MIB2_IP;
10574 optp->name = EXPER_IP_RTATTR;
10575 optp->len = msgdsize(ird.ird_attrs.lp_head);
10576 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10577 (int)optp->level, (int)optp->name, (int)optp->len));
10578 if (optp->len == 0)
10579 freemsg(mp4ctl);
10580 else
10581 qreply(q, mp4ctl);
10582
10583 return (mp2ctl);
10584 }
10585
10586 /*
10587 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and
10588 * ipv6NetToMediaEntryTable in an NDP walk.
10589 */
10590 static mblk_t *
10591 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10592 ip_stack_t *ipst)
10593 {
10594 struct opthdr *optp;
10595 mblk_t *mp2ctl; /* Returned */
10596 mblk_t *mp3ctl; /* nettomedia */
10597 mblk_t *mp4ctl; /* routeattrs */
10598 iproutedata_t ird;
10599 zoneid_t zoneid;
10600
10601 /*
10602 * make copies of the original message
10603 * - mp2ctl is returned unchanged to the caller for its use
10604 * - mpctl is sent upstream as ipv6RouteEntryTable
10605 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10606 * - mp4ctl is sent upstream as ipv6RouteAttributeTable
10607 */
10608 mp2ctl = copymsg(mpctl);
10609 mp3ctl = copymsg(mpctl);
10610 mp4ctl = copymsg(mpctl);
10611 if (mp3ctl == NULL || mp4ctl == NULL) {
10612 freemsg(mp4ctl);
10613 freemsg(mp3ctl);
10614 freemsg(mp2ctl);
10615 freemsg(mpctl);
10616 return (NULL);
10617 }
10618
10619 bzero(&ird, sizeof (ird));
10620
10621 ird.ird_route.lp_head = mpctl->b_cont;
10622 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10623 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10624 /*
10625 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10626 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10627 * intended a temporary solution until a proper MIB API is provided
10628 * that provides complete filtering/caller-opt-in.
10629 */
10630 if (level == EXPER_IP_AND_ALL_IRES)
10631 ird.ird_flags |= IRD_REPORT_ALL;
10632
10633 zoneid = Q_TO_CONN(q)->conn_zoneid;
10634 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10635
10636 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10637 optp->level = MIB2_IP6;
10638 optp->name = MIB2_IP6_ROUTE;
10639 optp->len = msgdsize(ird.ird_route.lp_head);
10640 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10641 (int)optp->level, (int)optp->name, (int)optp->len));
10642 qreply(q, mpctl);
10643
10644 /* ipv6NetToMediaEntryTable in mp3ctl */
10645 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10646
10647 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10648 optp->level = MIB2_IP6;
10649 optp->name = MIB2_IP6_MEDIA;
10650 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10651 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10652 (int)optp->level, (int)optp->name, (int)optp->len));
10653 qreply(q, mp3ctl);
10654
10655 /* ipv6RouteAttributeTable in mp4ctl */
10656 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10657 optp->level = MIB2_IP6;
10658 optp->name = EXPER_IP_RTATTR;
10659 optp->len = msgdsize(ird.ird_attrs.lp_head);
10660 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10661 (int)optp->level, (int)optp->name, (int)optp->len));
10662 if (optp->len == 0)
10663 freemsg(mp4ctl);
10664 else
10665 qreply(q, mp4ctl);
10666
10667 return (mp2ctl);
10668 }
10669
10670 /*
10671 * IPv6 mib: One per ill
10672 */
10673 static mblk_t *
10674 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10675 boolean_t legacy_req)
10676 {
10677 struct opthdr *optp;
10678 mblk_t *mp2ctl;
10679 ill_t *ill;
10680 ill_walk_context_t ctx;
10681 mblk_t *mp_tail = NULL;
10682 mib2_ipv6AddrEntry_t mae6;
10683 mib2_ipIfStatsEntry_t *ise;
10684 size_t ise_size, iae_size;
10685
10686 /*
10687 * Make a copy of the original message
10688 */
10689 mp2ctl = copymsg(mpctl);
10690
10691 /* fixed length IPv6 structure ... */
10692
10693 if (legacy_req) {
10694 ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10695 mib2_ipIfStatsEntry_t);
10696 iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10697 } else {
10698 ise_size = sizeof (mib2_ipIfStatsEntry_t);
10699 iae_size = sizeof (mib2_ipv6AddrEntry_t);
10700 }
10701
10702 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10703 optp->level = MIB2_IP6;
10704 optp->name = 0;
10705 /* Include "unknown interface" ip6_mib */
10706 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10707 ipst->ips_ip6_mib.ipIfStatsIfIndex =
10708 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10709 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10710 ipst->ips_ipv6_forwarding ? 1 : 2);
10711 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10712 ipst->ips_ipv6_def_hops);
10713 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10714 sizeof (mib2_ipIfStatsEntry_t));
10715 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10716 sizeof (mib2_ipv6AddrEntry_t));
10717 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10718 sizeof (mib2_ipv6RouteEntry_t));
10719 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10720 sizeof (mib2_ipv6NetToMediaEntry_t));
10721 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10722 sizeof (ipv6_member_t));
10723 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10724 sizeof (ipv6_grpsrc_t));
10725
10726 /*
10727 * Synchronize 64- and 32-bit counters
10728 */
10729 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10730 ipIfStatsHCInReceives);
10731 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10732 ipIfStatsHCInDelivers);
10733 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10734 ipIfStatsHCOutRequests);
10735 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10736 ipIfStatsHCOutForwDatagrams);
10737 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10738 ipIfStatsHCOutMcastPkts);
10739 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10740 ipIfStatsHCInMcastPkts);
10741
10742 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10743 (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10744 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10745 (uint_t)ise_size));
10746 } else if (legacy_req) {
10747 /* Adjust the EntrySize fields for legacy requests. */
10748 ise =
10749 (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10750 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10751 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10752 }
10753
10754 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10755 ill = ILL_START_WALK_V6(&ctx, ipst);
10756 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10757 ill->ill_ip_mib->ipIfStatsIfIndex =
10758 ill->ill_phyint->phyint_ifindex;
10759 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10760 ipst->ips_ipv6_forwarding ? 1 : 2);
10761 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10762 ill->ill_max_hops);
10763
10764 /*
10765 * Synchronize 64- and 32-bit counters
10766 */
10767 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10768 ipIfStatsHCInReceives);
10769 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10770 ipIfStatsHCInDelivers);
10771 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10772 ipIfStatsHCOutRequests);
10773 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10774 ipIfStatsHCOutForwDatagrams);
10775 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10776 ipIfStatsHCOutMcastPkts);
10777 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10778 ipIfStatsHCInMcastPkts);
10779
10780 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10781 (char *)ill->ill_ip_mib, (int)ise_size)) {
10782 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10783 "%u bytes\n", (uint_t)ise_size));
10784 } else if (legacy_req) {
10785 /* Adjust the EntrySize fields for legacy requests. */
10786 ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10787 (int)ise_size);
10788 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10789 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10790 }
10791 }
10792 rw_exit(&ipst->ips_ill_g_lock);
10793
10794 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10795 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10796 (int)optp->level, (int)optp->name, (int)optp->len));
10797 qreply(q, mpctl);
10798 return (mp2ctl);
10799 }
10800
10801 /*
10802 * ICMPv6 mib: One per ill
10803 */
10804 static mblk_t *
10805 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10806 {
10807 struct opthdr *optp;
10808 mblk_t *mp2ctl;
10809 ill_t *ill;
10810 ill_walk_context_t ctx;
10811 mblk_t *mp_tail = NULL;
10812 /*
10813 * Make a copy of the original message
10814 */
10815 mp2ctl = copymsg(mpctl);
10816
10817 /* fixed length ICMPv6 structure ... */
10818
10819 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10820 optp->level = MIB2_ICMP6;
10821 optp->name = 0;
10822 /* Include "unknown interface" icmp6_mib */
10823 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10824 MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10825 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10826 sizeof (mib2_ipv6IfIcmpEntry_t);
10827 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10828 (char *)&ipst->ips_icmp6_mib,
10829 (int)sizeof (ipst->ips_icmp6_mib))) {
10830 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10831 (uint_t)sizeof (ipst->ips_icmp6_mib)));
10832 }
10833
10834 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10835 ill = ILL_START_WALK_V6(&ctx, ipst);
10836 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10837 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10838 ill->ill_phyint->phyint_ifindex;
10839 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10840 (char *)ill->ill_icmp6_mib,
10841 (int)sizeof (*ill->ill_icmp6_mib))) {
10842 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10843 "%u bytes\n",
10844 (uint_t)sizeof (*ill->ill_icmp6_mib)));
10845 }
10846 }
10847 rw_exit(&ipst->ips_ill_g_lock);
10848
10849 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10850 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10851 (int)optp->level, (int)optp->name, (int)optp->len));
10852 qreply(q, mpctl);
10853 return (mp2ctl);
10854 }
10855
10856 /*
10857 * ire_walk routine to create both ipRouteEntryTable and
10858 * ipRouteAttributeTable in one IRE walk
10859 */
10860 static void
10861 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10862 {
10863 ill_t *ill;
10864 mib2_ipRouteEntry_t *re;
10865 mib2_ipAttributeEntry_t iaes;
10866 tsol_ire_gw_secattr_t *attrp;
10867 tsol_gc_t *gc = NULL;
10868 tsol_gcgrp_t *gcgrp = NULL;
10869 ip_stack_t *ipst = ire->ire_ipst;
10870
10871 ASSERT(ire->ire_ipversion == IPV4_VERSION);
10872
10873 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10874 if (ire->ire_testhidden)
10875 return;
10876 if (ire->ire_type & IRE_IF_CLONE)
10877 return;
10878 }
10879
10880 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10881 return;
10882
10883 if ((attrp = ire->ire_gw_secattr) != NULL) {
10884 mutex_enter(&attrp->igsa_lock);
10885 if ((gc = attrp->igsa_gc) != NULL) {
10886 gcgrp = gc->gc_grp;
10887 ASSERT(gcgrp != NULL);
10888 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
10889 }
10890 mutex_exit(&attrp->igsa_lock);
10891 }
10892 /*
10893 * Return all IRE types for route table... let caller pick and choose
10894 */
10895 re->ipRouteDest = ire->ire_addr;
10896 ill = ire->ire_ill;
10897 re->ipRouteIfIndex.o_length = 0;
10898 if (ill != NULL) {
10899 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10900 re->ipRouteIfIndex.o_length =
10901 mi_strlen(re->ipRouteIfIndex.o_bytes);
10902 }
10903 re->ipRouteMetric1 = -1;
10904 re->ipRouteMetric2 = -1;
10905 re->ipRouteMetric3 = -1;
10906 re->ipRouteMetric4 = -1;
10907
10908 re->ipRouteNextHop = ire->ire_gateway_addr;
10909 /* indirect(4), direct(3), or invalid(2) */
10910 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10911 re->ipRouteType = 2;
10912 else if (ire->ire_type & IRE_ONLINK)
10913 re->ipRouteType = 3;
10914 else
10915 re->ipRouteType = 4;
10916
10917 re->ipRouteProto = -1;
10918 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10919 re->ipRouteMask = ire->ire_mask;
10920 re->ipRouteMetric5 = -1;
10921 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10922 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10923 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10924
10925 re->ipRouteInfo.re_frag_flag = 0;
10926 re->ipRouteInfo.re_rtt = 0;
10927 re->ipRouteInfo.re_src_addr = 0;
10928 re->ipRouteInfo.re_ref = ire->ire_refcnt;
10929 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10930 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10931 re->ipRouteInfo.re_flags = ire->ire_flags;
10932
10933 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10934 if (ire->ire_type & IRE_INTERFACE) {
10935 ire_t *child;
10936
10937 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10938 child = ire->ire_dep_children;
10939 while (child != NULL) {
10940 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10941 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10942 child = child->ire_dep_sib_next;
10943 }
10944 rw_exit(&ipst->ips_ire_dep_lock);
10945 }
10946
10947 if (ire->ire_flags & RTF_DYNAMIC) {
10948 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT;
10949 } else {
10950 re->ipRouteInfo.re_ire_type = ire->ire_type;
10951 }
10952
10953 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
10954 (char *)re, (int)sizeof (*re))) {
10955 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
10956 (uint_t)sizeof (*re)));
10957 }
10958
10959 if (gc != NULL) {
10960 iaes.iae_routeidx = ird->ird_idx;
10961 iaes.iae_doi = gc->gc_db->gcdb_doi;
10962 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
10963
10964 if (!snmp_append_data2(ird->ird_attrs.lp_head,
10965 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
10966 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u "
10967 "bytes\n", (uint_t)sizeof (iaes)));
10968 }
10969 }
10970
10971 /* bump route index for next pass */
10972 ird->ird_idx++;
10973
10974 kmem_free(re, sizeof (*re));
10975 if (gcgrp != NULL)
10976 rw_exit(&gcgrp->gcgrp_rwlock);
10977 }
10978
10979 /*
10980 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
10981 */
10982 static void
10983 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
10984 {
10985 ill_t *ill;
10986 mib2_ipv6RouteEntry_t *re;
10987 mib2_ipAttributeEntry_t iaes;
10988 tsol_ire_gw_secattr_t *attrp;
10989 tsol_gc_t *gc = NULL;
10990 tsol_gcgrp_t *gcgrp = NULL;
10991 ip_stack_t *ipst = ire->ire_ipst;
10992
10993 ASSERT(ire->ire_ipversion == IPV6_VERSION);
10994
10995 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10996 if (ire->ire_testhidden)
10997 return;
10998 if (ire->ire_type & IRE_IF_CLONE)
10999 return;
11000 }
11001
11002 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
11003 return;
11004
11005 if ((attrp = ire->ire_gw_secattr) != NULL) {
11006 mutex_enter(&attrp->igsa_lock);
11007 if ((gc = attrp->igsa_gc) != NULL) {
11008 gcgrp = gc->gc_grp;
11009 ASSERT(gcgrp != NULL);
11010 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
11011 }
11012 mutex_exit(&attrp->igsa_lock);
11013 }
11014 /*
11015 * Return all IRE types for route table... let caller pick and choose
11016 */
11017 re->ipv6RouteDest = ire->ire_addr_v6;
11018 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
11019 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
11020 re->ipv6RouteIfIndex.o_length = 0;
11021 ill = ire->ire_ill;
11022 if (ill != NULL) {
11023 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
11024 re->ipv6RouteIfIndex.o_length =
11025 mi_strlen(re->ipv6RouteIfIndex.o_bytes);
11026 }
11027
11028 ASSERT(!(ire->ire_type & IRE_BROADCAST));
11029
11030 mutex_enter(&ire->ire_lock);
11031 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
11032 mutex_exit(&ire->ire_lock);
11033
11034 /* remote(4), local(3), or discard(2) */
11035 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11036 re->ipv6RouteType = 2;
11037 else if (ire->ire_type & IRE_ONLINK)
11038 re->ipv6RouteType = 3;
11039 else
11040 re->ipv6RouteType = 4;
11041
11042 re->ipv6RouteProtocol = -1;
11043 re->ipv6RoutePolicy = 0;
11044 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time;
11045 re->ipv6RouteNextHopRDI = 0;
11046 re->ipv6RouteWeight = 0;
11047 re->ipv6RouteMetric = 0;
11048 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11049 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
11050 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11051
11052 re->ipv6RouteInfo.re_frag_flag = 0;
11053 re->ipv6RouteInfo.re_rtt = 0;
11054 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros;
11055 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count;
11056 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
11057 re->ipv6RouteInfo.re_ref = ire->ire_refcnt;
11058 re->ipv6RouteInfo.re_flags = ire->ire_flags;
11059
11060 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11061 if (ire->ire_type & IRE_INTERFACE) {
11062 ire_t *child;
11063
11064 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11065 child = ire->ire_dep_children;
11066 while (child != NULL) {
11067 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
11068 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11069 child = child->ire_dep_sib_next;
11070 }
11071 rw_exit(&ipst->ips_ire_dep_lock);
11072 }
11073 if (ire->ire_flags & RTF_DYNAMIC) {
11074 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11075 } else {
11076 re->ipv6RouteInfo.re_ire_type = ire->ire_type;
11077 }
11078
11079 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11080 (char *)re, (int)sizeof (*re))) {
11081 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
11082 (uint_t)sizeof (*re)));
11083 }
11084
11085 if (gc != NULL) {
11086 iaes.iae_routeidx = ird->ird_idx;
11087 iaes.iae_doi = gc->gc_db->gcdb_doi;
11088 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11089
11090 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11091 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11092 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u "
11093 "bytes\n", (uint_t)sizeof (iaes)));
11094 }
11095 }
11096
11097 /* bump route index for next pass */
11098 ird->ird_idx++;
11099
11100 kmem_free(re, sizeof (*re));
11101 if (gcgrp != NULL)
11102 rw_exit(&gcgrp->gcgrp_rwlock);
11103 }
11104
11105 /*
11106 * ncec_walk routine to create ipv6NetToMediaEntryTable
11107 */
11108 static void
11109 ip_snmp_get2_v6_media(ncec_t *ncec, void *ptr)
11110 {
11111 iproutedata_t *ird = ptr;
11112 ill_t *ill;
11113 mib2_ipv6NetToMediaEntry_t ntme;
11114
11115 ill = ncec->ncec_ill;
11116 /* skip arpce entries, and loopback ncec entries */
11117 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
11118 return;
11119 /*
11120 * Neighbor cache entry attached to IRE with on-link
11121 * destination.
11122 * We report all IPMP groups on ncec_ill which is normally the upper.
11123 */
11124 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
11125 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
11126 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
11127 if (ncec->ncec_lladdr != NULL) {
11128 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
11129 ntme.ipv6NetToMediaPhysAddress.o_length);
11130 }
11131 /*
11132 * Note: Returns ND_* states. Should be:
11133 * reachable(1), stale(2), delay(3), probe(4),
11134 * invalid(5), unknown(6)
11135 */
11136 ntme.ipv6NetToMediaState = ncec->ncec_state;
11137 ntme.ipv6NetToMediaLastUpdated = 0;
11138
11139 /* other(1), dynamic(2), static(3), local(4) */
11140 if (NCE_MYADDR(ncec)) {
11141 ntme.ipv6NetToMediaType = 4;
11142 } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
11143 ntme.ipv6NetToMediaType = 1; /* proxy */
11144 } else if (ncec->ncec_flags & NCE_F_STATIC) {
11145 ntme.ipv6NetToMediaType = 3;
11146 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
11147 ntme.ipv6NetToMediaType = 1;
11148 } else {
11149 ntme.ipv6NetToMediaType = 2;
11150 }
11151
11152 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11153 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11154 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
11155 (uint_t)sizeof (ntme)));
11156 }
11157 }
11158
11159 int
11160 nce2ace(ncec_t *ncec)
11161 {
11162 int flags = 0;
11163
11164 if (NCE_ISREACHABLE(ncec))
11165 flags |= ACE_F_RESOLVED;
11166 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11167 flags |= ACE_F_AUTHORITY;
11168 if (ncec->ncec_flags & NCE_F_PUBLISH)
11169 flags |= ACE_F_PUBLISH;
11170 if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
11171 flags |= ACE_F_PERMANENT;
11172 if (NCE_MYADDR(ncec))
11173 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
11174 if (ncec->ncec_flags & NCE_F_UNVERIFIED)
11175 flags |= ACE_F_UNVERIFIED;
11176 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11177 flags |= ACE_F_AUTHORITY;
11178 if (ncec->ncec_flags & NCE_F_DELAYED)
11179 flags |= ACE_F_DELAYED;
11180 return (flags);
11181 }
11182
11183 /*
11184 * ncec_walk routine to create ipNetToMediaEntryTable
11185 */
11186 static void
11187 ip_snmp_get2_v4_media(ncec_t *ncec, void *ptr)
11188 {
11189 iproutedata_t *ird = ptr;
11190 ill_t *ill;
11191 mib2_ipNetToMediaEntry_t ntme;
11192 const char *name = "unknown";
11193 ipaddr_t ncec_addr;
11194
11195 ill = ncec->ncec_ill;
11196 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
11197 ill->ill_net_type == IRE_LOOPBACK)
11198 return;
11199
11200 /* We report all IPMP groups on ncec_ill which is normally the upper. */
11201 name = ill->ill_name;
11202 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
11203 if (NCE_MYADDR(ncec)) {
11204 ntme.ipNetToMediaType = 4;
11205 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
11206 ntme.ipNetToMediaType = 1;
11207 } else {
11208 ntme.ipNetToMediaType = 3;
11209 }
11210 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
11211 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
11212 ntme.ipNetToMediaIfIndex.o_length);
11213
11214 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
11215 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
11216
11217 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
11218 ncec_addr = INADDR_BROADCAST;
11219 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
11220 sizeof (ncec_addr));
11221 /*
11222 * map all the flags to the ACE counterpart.
11223 */
11224 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
11225
11226 ntme.ipNetToMediaPhysAddress.o_length =
11227 MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
11228
11229 if (!NCE_ISREACHABLE(ncec))
11230 ntme.ipNetToMediaPhysAddress.o_length = 0;
11231 else {
11232 if (ncec->ncec_lladdr != NULL) {
11233 bcopy(ncec->ncec_lladdr,
11234 ntme.ipNetToMediaPhysAddress.o_bytes,
11235 ntme.ipNetToMediaPhysAddress.o_length);
11236 }
11237 }
11238
11239 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11240 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11241 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
11242 (uint_t)sizeof (ntme)));
11243 }
11244 }
11245
11246 /*
11247 * return (0) if invalid set request, 1 otherwise, including non-tcp requests
11248 */
11249 /* ARGSUSED */
11250 int
11251 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
11252 {
11253 switch (level) {
11254 case MIB2_IP:
11255 case MIB2_ICMP:
11256 switch (name) {
11257 default:
11258 break;
11259 }
11260 return (1);
11261 default:
11262 return (1);
11263 }
11264 }
11265
11266 /*
11267 * When there exists both a 64- and 32-bit counter of a particular type
11268 * (i.e., InReceives), only the 64-bit counters are added.
11269 */
11270 void
11271 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
11272 {
11273 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
11274 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
11275 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
11276 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
11277 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
11278 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
11279 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
11280 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
11281 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
11282 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
11283 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
11284 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
11285 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
11286 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
11287 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
11288 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
11289 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
11290 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
11291 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
11292 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
11293 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
11294 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
11295 o2->ipIfStatsInWrongIPVersion);
11296 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
11297 o2->ipIfStatsInWrongIPVersion);
11298 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
11299 o2->ipIfStatsOutSwitchIPVersion);
11300 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
11301 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
11302 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
11303 o2->ipIfStatsHCInForwDatagrams);
11304 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
11305 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
11306 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
11307 o2->ipIfStatsHCOutForwDatagrams);
11308 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
11309 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
11310 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
11311 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
11312 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
11313 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
11314 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11315 o2->ipIfStatsHCOutMcastOctets);
11316 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11317 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11318 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11319 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11320 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11321 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11322 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11323 }
11324
11325 void
11326 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11327 {
11328 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11329 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11330 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11331 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11332 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11333 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11334 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11335 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11336 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11337 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11338 o2->ipv6IfIcmpInRouterSolicits);
11339 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11340 o2->ipv6IfIcmpInRouterAdvertisements);
11341 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11342 o2->ipv6IfIcmpInNeighborSolicits);
11343 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11344 o2->ipv6IfIcmpInNeighborAdvertisements);
11345 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11346 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11347 o2->ipv6IfIcmpInGroupMembQueries);
11348 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11349 o2->ipv6IfIcmpInGroupMembResponses);
11350 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11351 o2->ipv6IfIcmpInGroupMembReductions);
11352 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11353 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11354 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11355 o2->ipv6IfIcmpOutDestUnreachs);
11356 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11357 o2->ipv6IfIcmpOutAdminProhibs);
11358 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11359 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11360 o2->ipv6IfIcmpOutParmProblems);
11361 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11362 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11363 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11364 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11365 o2->ipv6IfIcmpOutRouterSolicits);
11366 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11367 o2->ipv6IfIcmpOutRouterAdvertisements);
11368 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11369 o2->ipv6IfIcmpOutNeighborSolicits);
11370 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11371 o2->ipv6IfIcmpOutNeighborAdvertisements);
11372 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11373 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11374 o2->ipv6IfIcmpOutGroupMembQueries);
11375 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11376 o2->ipv6IfIcmpOutGroupMembResponses);
11377 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11378 o2->ipv6IfIcmpOutGroupMembReductions);
11379 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11380 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11381 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11382 o2->ipv6IfIcmpInBadNeighborAdvertisements);
11383 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11384 o2->ipv6IfIcmpInBadNeighborSolicitations);
11385 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11386 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11387 o2->ipv6IfIcmpInGroupMembTotal);
11388 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11389 o2->ipv6IfIcmpInGroupMembBadQueries);
11390 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11391 o2->ipv6IfIcmpInGroupMembBadReports);
11392 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11393 o2->ipv6IfIcmpInGroupMembOurReports);
11394 }
11395
11396 /*
11397 * Called before the options are updated to check if this packet will
11398 * be source routed from here.
11399 * This routine assumes that the options are well formed i.e. that they
11400 * have already been checked.
11401 */
11402 boolean_t
11403 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11404 {
11405 ipoptp_t opts;
11406 uchar_t *opt;
11407 uint8_t optval;
11408 uint8_t optlen;
11409 ipaddr_t dst;
11410
11411 if (IS_SIMPLE_IPH(ipha)) {
11412 ip2dbg(("not source routed\n"));
11413 return (B_FALSE);
11414 }
11415 dst = ipha->ipha_dst;
11416 for (optval = ipoptp_first(&opts, ipha);
11417 optval != IPOPT_EOL;
11418 optval = ipoptp_next(&opts)) {
11419 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11420 opt = opts.ipoptp_cur;
11421 optlen = opts.ipoptp_len;
11422 ip2dbg(("ip_source_routed: opt %d, len %d\n",
11423 optval, optlen));
11424 switch (optval) {
11425 uint32_t off;
11426 case IPOPT_SSRR:
11427 case IPOPT_LSRR:
11428 /*
11429 * If dst is one of our addresses and there are some
11430 * entries left in the source route return (true).
11431 */
11432 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11433 ip2dbg(("ip_source_routed: not next"
11434 " source route 0x%x\n",
11435 ntohl(dst)));
11436 return (B_FALSE);
11437 }
11438 off = opt[IPOPT_OFFSET];
11439 off--;
11440 if (optlen < IP_ADDR_LEN ||
11441 off > optlen - IP_ADDR_LEN) {
11442 /* End of source route */
11443 ip1dbg(("ip_source_routed: end of SR\n"));
11444 return (B_FALSE);
11445 }
11446 return (B_TRUE);
11447 }
11448 }
11449 ip2dbg(("not source routed\n"));
11450 return (B_FALSE);
11451 }
11452
11453 /*
11454 * ip_unbind is called by the transports to remove a conn from
11455 * the fanout table.
11456 */
11457 void
11458 ip_unbind(conn_t *connp)
11459 {
11460
11461 ASSERT(!MUTEX_HELD(&connp->conn_lock));
11462
11463 if (is_system_labeled() && connp->conn_anon_port) {
11464 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
11465 connp->conn_mlp_type, connp->conn_proto,
11466 ntohs(connp->conn_lport), B_FALSE);
11467 connp->conn_anon_port = 0;
11468 }
11469 connp->conn_mlp_type = mlptSingle;
11470
11471 ipcl_hash_remove(connp);
11472 }
11473
11474 /*
11475 * Used for deciding the MSS size for the upper layer. Thus
11476 * we need to check the outbound policy values in the conn.
11477 */
11478 int
11479 conn_ipsec_length(conn_t *connp)
11480 {
11481 ipsec_latch_t *ipl;
11482
11483 ipl = connp->conn_latch;
11484 if (ipl == NULL)
11485 return (0);
11486
11487 if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11488 return (0);
11489
11490 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11491 }
11492
11493 /*
11494 * Returns an estimate of the IPsec headers size. This is used if
11495 * we don't want to call into IPsec to get the exact size.
11496 */
11497 int
11498 ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11499 {
11500 ipsec_action_t *a;
11501
11502 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11503 return (0);
11504
11505 a = ixa->ixa_ipsec_action;
11506 if (a == NULL) {
11507 ASSERT(ixa->ixa_ipsec_policy != NULL);
11508 a = ixa->ixa_ipsec_policy->ipsp_act;
11509 }
11510 ASSERT(a != NULL);
11511
11512 return (a->ipa_ovhd);
11513 }
11514
11515 /*
11516 * If there are any source route options, return the true final
11517 * destination. Otherwise, return the destination.
11518 */
11519 ipaddr_t
11520 ip_get_dst(ipha_t *ipha)
11521 {
11522 ipoptp_t opts;
11523 uchar_t *opt;
11524 uint8_t optval;
11525 uint8_t optlen;
11526 ipaddr_t dst;
11527 uint32_t off;
11528
11529 dst = ipha->ipha_dst;
11530
11531 if (IS_SIMPLE_IPH(ipha))
11532 return (dst);
11533
11534 for (optval = ipoptp_first(&opts, ipha);
11535 optval != IPOPT_EOL;
11536 optval = ipoptp_next(&opts)) {
11537 opt = opts.ipoptp_cur;
11538 optlen = opts.ipoptp_len;
11539 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11540 switch (optval) {
11541 case IPOPT_SSRR:
11542 case IPOPT_LSRR:
11543 off = opt[IPOPT_OFFSET];
11544 /*
11545 * If one of the conditions is true, it means
11546 * end of options and dst already has the right
11547 * value.
11548 */
11549 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11550 off = optlen - IP_ADDR_LEN;
11551 bcopy(&opt[off], &dst, IP_ADDR_LEN);
11552 }
11553 return (dst);
11554 default:
11555 break;
11556 }
11557 }
11558
11559 return (dst);
11560 }
11561
11562 /*
11563 * Outbound IP fragmentation routine.
11564 * Assumes the caller has checked whether or not fragmentation should
11565 * be allowed. Here we copy the DF bit from the header to all the generated
11566 * fragments.
11567 */
11568 int
11569 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11570 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11571 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11572 {
11573 int i1;
11574 int hdr_len;
11575 mblk_t *hdr_mp;
11576 ipha_t *ipha;
11577 int ip_data_end;
11578 int len;
11579 mblk_t *mp = mp_orig;
11580 int offset;
11581 ill_t *ill = nce->nce_ill;
11582 ip_stack_t *ipst = ill->ill_ipst;
11583 mblk_t *carve_mp;
11584 uint32_t frag_flag;
11585 uint_t priority = mp->b_band;
11586 int error = 0;
11587
11588 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11589
11590 if (pkt_len != msgdsize(mp)) {
11591 ip0dbg(("Packet length mismatch: %d, %ld\n",
11592 pkt_len, msgdsize(mp)));
11593 freemsg(mp);
11594 return (EINVAL);
11595 }
11596
11597 if (max_frag == 0) {
11598 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11599 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11600 ip_drop_output("FragFails: zero max_frag", mp, ill);
11601 freemsg(mp);
11602 return (EINVAL);
11603 }
11604
11605 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11606 ipha = (ipha_t *)mp->b_rptr;
11607 ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11608 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11609
11610 /*
11611 * Establish the starting offset. May not be zero if we are fragging
11612 * a fragment that is being forwarded.
11613 */
11614 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11615
11616 /* TODO why is this test needed? */
11617 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11618 /* TODO: notify ulp somehow */
11619 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11620 ip_drop_output("FragFails: bad starting offset", mp, ill);
11621 freemsg(mp);
11622 return (EINVAL);
11623 }
11624
11625 hdr_len = IPH_HDR_LENGTH(ipha);
11626 ipha->ipha_hdr_checksum = 0;
11627
11628 /*
11629 * Establish the number of bytes maximum per frag, after putting
11630 * in the header.
11631 */
11632 len = (max_frag - hdr_len) & ~7;
11633
11634 /* Get a copy of the header for the trailing frags */
11635 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11636 mp);
11637 if (hdr_mp == NULL) {
11638 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11639 ip_drop_output("FragFails: no hdr_mp", mp, ill);
11640 freemsg(mp);
11641 return (ENOBUFS);
11642 }
11643
11644 /* Store the starting offset, with the MoreFrags flag. */
11645 i1 = offset | IPH_MF | frag_flag;
11646 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11647
11648 /* Establish the ending byte offset, based on the starting offset. */
11649 offset <<= 3;
11650 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11651
11652 /* Store the length of the first fragment in the IP header. */
11653 i1 = len + hdr_len;
11654 ASSERT(i1 <= IP_MAXPACKET);
11655 ipha->ipha_length = htons((uint16_t)i1);
11656
11657 /*
11658 * Compute the IP header checksum for the first frag. We have to
11659 * watch out that we stop at the end of the header.
11660 */
11661 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11662
11663 /*
11664 * Now carve off the first frag. Note that this will include the
11665 * original IP header.
11666 */
11667 if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11668 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11669 ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11670 freeb(hdr_mp);
11671 freemsg(mp_orig);
11672 return (ENOBUFS);
11673 }
11674
11675 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11676
11677 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11678 ixa_cookie);
11679 if (error != 0 && error != EWOULDBLOCK) {
11680 /* No point in sending the other fragments */
11681 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11682 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11683 freeb(hdr_mp);
11684 freemsg(mp_orig);
11685 return (error);
11686 }
11687
11688 /* No need to redo state machine in loop */
11689 ixaflags &= ~IXAF_REACH_CONF;
11690
11691 /* Advance the offset to the second frag starting point. */
11692 offset += len;
11693 /*
11694 * Update hdr_len from the copied header - there might be less options
11695 * in the later fragments.
11696 */
11697 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11698 /* Loop until done. */
11699 for (;;) {
11700 uint16_t offset_and_flags;
11701 uint16_t ip_len;
11702
11703 if (ip_data_end - offset > len) {
11704 /*
11705 * Carve off the appropriate amount from the original
11706 * datagram.
11707 */
11708 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11709 mp = NULL;
11710 break;
11711 }
11712 /*
11713 * More frags after this one. Get another copy
11714 * of the header.
11715 */
11716 if (carve_mp->b_datap->db_ref == 1 &&
11717 hdr_mp->b_wptr - hdr_mp->b_rptr <
11718 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11719 /* Inline IP header */
11720 carve_mp->b_rptr -= hdr_mp->b_wptr -
11721 hdr_mp->b_rptr;
11722 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11723 hdr_mp->b_wptr - hdr_mp->b_rptr);
11724 mp = carve_mp;
11725 } else {
11726 if (!(mp = copyb(hdr_mp))) {
11727 freemsg(carve_mp);
11728 break;
11729 }
11730 /* Get priority marking, if any. */
11731 mp->b_band = priority;
11732 mp->b_cont = carve_mp;
11733 }
11734 ipha = (ipha_t *)mp->b_rptr;
11735 offset_and_flags = IPH_MF;
11736 } else {
11737 /*
11738 * Last frag. Consume the header. Set len to
11739 * the length of this last piece.
11740 */
11741 len = ip_data_end - offset;
11742
11743 /*
11744 * Carve off the appropriate amount from the original
11745 * datagram.
11746 */
11747 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11748 mp = NULL;
11749 break;
11750 }
11751 if (carve_mp->b_datap->db_ref == 1 &&
11752 hdr_mp->b_wptr - hdr_mp->b_rptr <
11753 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11754 /* Inline IP header */
11755 carve_mp->b_rptr -= hdr_mp->b_wptr -
11756 hdr_mp->b_rptr;
11757 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11758 hdr_mp->b_wptr - hdr_mp->b_rptr);
11759 mp = carve_mp;
11760 freeb(hdr_mp);
11761 hdr_mp = mp;
11762 } else {
11763 mp = hdr_mp;
11764 /* Get priority marking, if any. */
11765 mp->b_band = priority;
11766 mp->b_cont = carve_mp;
11767 }
11768 ipha = (ipha_t *)mp->b_rptr;
11769 /* A frag of a frag might have IPH_MF non-zero */
11770 offset_and_flags =
11771 ntohs(ipha->ipha_fragment_offset_and_flags) &
11772 IPH_MF;
11773 }
11774 offset_and_flags |= (uint16_t)(offset >> 3);
11775 offset_and_flags |= (uint16_t)frag_flag;
11776 /* Store the offset and flags in the IP header. */
11777 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11778
11779 /* Store the length in the IP header. */
11780 ip_len = (uint16_t)(len + hdr_len);
11781 ipha->ipha_length = htons(ip_len);
11782
11783 /*
11784 * Set the IP header checksum. Note that mp is just
11785 * the header, so this is easy to pass to ip_csum.
11786 */
11787 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11788
11789 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11790
11791 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11792 nolzid, ixa_cookie);
11793 /* All done if we just consumed the hdr_mp. */
11794 if (mp == hdr_mp) {
11795 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11796 return (error);
11797 }
11798 if (error != 0 && error != EWOULDBLOCK) {
11799 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11800 mblk_t *, hdr_mp);
11801 /* No point in sending the other fragments */
11802 break;
11803 }
11804
11805 /* Otherwise, advance and loop. */
11806 offset += len;
11807 }
11808 /* Clean up following allocation failure. */
11809 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11810 ip_drop_output("FragFails: loop ended", NULL, ill);
11811 if (mp != hdr_mp)
11812 freeb(hdr_mp);
11813 if (mp != mp_orig)
11814 freemsg(mp_orig);
11815 return (error);
11816 }
11817
11818 /*
11819 * Copy the header plus those options which have the copy bit set
11820 */
11821 static mblk_t *
11822 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11823 mblk_t *src)
11824 {
11825 mblk_t *mp;
11826 uchar_t *up;
11827
11828 /*
11829 * Quick check if we need to look for options without the copy bit
11830 * set
11831 */
11832 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11833 if (!mp)
11834 return (mp);
11835 mp->b_rptr += ipst->ips_ip_wroff_extra;
11836 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11837 bcopy(rptr, mp->b_rptr, hdr_len);
11838 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11839 return (mp);
11840 }
11841 up = mp->b_rptr;
11842 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11843 up += IP_SIMPLE_HDR_LENGTH;
11844 rptr += IP_SIMPLE_HDR_LENGTH;
11845 hdr_len -= IP_SIMPLE_HDR_LENGTH;
11846 while (hdr_len > 0) {
11847 uint32_t optval;
11848 uint32_t optlen;
11849
11850 optval = *rptr;
11851 if (optval == IPOPT_EOL)
11852 break;
11853 if (optval == IPOPT_NOP)
11854 optlen = 1;
11855 else
11856 optlen = rptr[1];
11857 if (optval & IPOPT_COPY) {
11858 bcopy(rptr, up, optlen);
11859 up += optlen;
11860 }
11861 rptr += optlen;
11862 hdr_len -= optlen;
11863 }
11864 /*
11865 * Make sure that we drop an even number of words by filling
11866 * with EOL to the next word boundary.
11867 */
11868 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11869 hdr_len & 0x3; hdr_len++)
11870 *up++ = IPOPT_EOL;
11871 mp->b_wptr = up;
11872 /* Update header length */
11873 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11874 return (mp);
11875 }
11876
11877 /*
11878 * Update any source route, record route, or timestamp options when
11879 * sending a packet back to ourselves.
11880 * Check that we are at end of strict source route.
11881 * The options have been sanity checked by ip_output_options().
11882 */
11883 void
11884 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11885 {
11886 ipoptp_t opts;
11887 uchar_t *opt;
11888 uint8_t optval;
11889 uint8_t optlen;
11890 ipaddr_t dst;
11891 uint32_t ts;
11892 timestruc_t now;
11893
11894 for (optval = ipoptp_first(&opts, ipha);
11895 optval != IPOPT_EOL;
11896 optval = ipoptp_next(&opts)) {
11897 opt = opts.ipoptp_cur;
11898 optlen = opts.ipoptp_len;
11899 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11900 switch (optval) {
11901 uint32_t off;
11902 case IPOPT_SSRR:
11903 case IPOPT_LSRR:
11904 off = opt[IPOPT_OFFSET];
11905 off--;
11906 if (optlen < IP_ADDR_LEN ||
11907 off > optlen - IP_ADDR_LEN) {
11908 /* End of source route */
11909 break;
11910 }
11911 /*
11912 * This will only happen if two consecutive entries
11913 * in the source route contains our address or if
11914 * it is a packet with a loose source route which
11915 * reaches us before consuming the whole source route
11916 */
11917
11918 if (optval == IPOPT_SSRR) {
11919 return;
11920 }
11921 /*
11922 * Hack: instead of dropping the packet truncate the
11923 * source route to what has been used by filling the
11924 * rest with IPOPT_NOP.
11925 */
11926 opt[IPOPT_OLEN] = (uint8_t)off;
11927 while (off < optlen) {
11928 opt[off++] = IPOPT_NOP;
11929 }
11930 break;
11931 case IPOPT_RR:
11932 off = opt[IPOPT_OFFSET];
11933 off--;
11934 if (optlen < IP_ADDR_LEN ||
11935 off > optlen - IP_ADDR_LEN) {
11936 /* No more room - ignore */
11937 ip1dbg((
11938 "ip_output_local_options: end of RR\n"));
11939 break;
11940 }
11941 dst = htonl(INADDR_LOOPBACK);
11942 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11943 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11944 break;
11945 case IPOPT_TS:
11946 /* Insert timestamp if there is romm */
11947 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11948 case IPOPT_TS_TSONLY:
11949 off = IPOPT_TS_TIMELEN;
11950 break;
11951 case IPOPT_TS_PRESPEC:
11952 case IPOPT_TS_PRESPEC_RFC791:
11953 /* Verify that the address matched */
11954 off = opt[IPOPT_OFFSET] - 1;
11955 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
11956 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11957 /* Not for us */
11958 break;
11959 }
11960 /* FALLTHROUGH */
11961 case IPOPT_TS_TSANDADDR:
11962 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
11963 break;
11964 default:
11965 /*
11966 * ip_*put_options should have already
11967 * dropped this packet.
11968 */
11969 cmn_err(CE_PANIC, "ip_output_local_options: "
11970 "unknown IT - bug in ip_output_options?\n");
11971 }
11972 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
11973 /* Increase overflow counter */
11974 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
11975 opt[IPOPT_POS_OV_FLG] = (uint8_t)
11976 (opt[IPOPT_POS_OV_FLG] & 0x0F) |
11977 (off << 4);
11978 break;
11979 }
11980 off = opt[IPOPT_OFFSET] - 1;
11981 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11982 case IPOPT_TS_PRESPEC:
11983 case IPOPT_TS_PRESPEC_RFC791:
11984 case IPOPT_TS_TSANDADDR:
11985 dst = htonl(INADDR_LOOPBACK);
11986 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11987 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11988 /* FALLTHROUGH */
11989 case IPOPT_TS_TSONLY:
11990 off = opt[IPOPT_OFFSET] - 1;
11991 /* Compute # of milliseconds since midnight */
11992 gethrestime(&now);
11993 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
11994 NSEC2MSEC(now.tv_nsec);
11995 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
11996 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
11997 break;
11998 }
11999 break;
12000 }
12001 }
12002 }
12003
12004 /*
12005 * Prepend an M_DATA fastpath header, and if none present prepend a
12006 * DL_UNITDATA_REQ. Frees the mblk on failure.
12007 *
12008 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
12009 * If there is a change to them, the nce will be deleted (condemned) and
12010 * a new nce_t will be created when packets are sent. Thus we need no locks
12011 * to access those fields.
12012 *
12013 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
12014 * we place b_band in dl_priority.dl_max.
12015 */
12016 static mblk_t *
12017 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
12018 {
12019 uint_t hlen;
12020 mblk_t *mp1;
12021 uint_t priority;
12022 uchar_t *rptr;
12023
12024 rptr = mp->b_rptr;
12025
12026 ASSERT(DB_TYPE(mp) == M_DATA);
12027 priority = mp->b_band;
12028
12029 ASSERT(nce != NULL);
12030 if ((mp1 = nce->nce_fp_mp) != NULL) {
12031 hlen = MBLKL(mp1);
12032 /*
12033 * Check if we have enough room to prepend fastpath
12034 * header
12035 */
12036 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
12037 rptr -= hlen;
12038 bcopy(mp1->b_rptr, rptr, hlen);
12039 /*
12040 * Set the b_rptr to the start of the link layer
12041 * header
12042 */
12043 mp->b_rptr = rptr;
12044 return (mp);
12045 }
12046 mp1 = copyb(mp1);
12047 if (mp1 == NULL) {
12048 ill_t *ill = nce->nce_ill;
12049
12050 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12051 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12052 freemsg(mp);
12053 return (NULL);
12054 }
12055 mp1->b_band = priority;
12056 mp1->b_cont = mp;
12057 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
12058 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
12059 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
12060 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
12061 DB_LSOMSS(mp1) = DB_LSOMSS(mp);
12062 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
12063 /*
12064 * XXX disable ICK_VALID and compute checksum
12065 * here; can happen if nce_fp_mp changes and
12066 * it can't be copied now due to insufficient
12067 * space. (unlikely, fp mp can change, but it
12068 * does not increase in length)
12069 */
12070 return (mp1);
12071 }
12072 mp1 = copyb(nce->nce_dlur_mp);
12073
12074 if (mp1 == NULL) {
12075 ill_t *ill = nce->nce_ill;
12076
12077 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12078 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12079 freemsg(mp);
12080 return (NULL);
12081 }
12082 mp1->b_cont = mp;
12083 if (priority != 0) {
12084 mp1->b_band = priority;
12085 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
12086 priority;
12087 }
12088 return (mp1);
12089 }
12090
12091 /*
12092 * Finish the outbound IPsec processing. This function is called from
12093 * ipsec_out_process() if the IPsec packet was processed
12094 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12095 * asynchronously.
12096 *
12097 * This is common to IPv4 and IPv6.
12098 */
12099 int
12100 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
12101 {
12102 iaflags_t ixaflags = ixa->ixa_flags;
12103 uint_t pktlen;
12104
12105
12106 /* AH/ESP don't update ixa_pktlen when they modify the packet */
12107 if (ixaflags & IXAF_IS_IPV4) {
12108 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12109
12110 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12111 pktlen = ntohs(ipha->ipha_length);
12112 } else {
12113 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12114
12115 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12116 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12117 }
12118
12119 /*
12120 * We release any hard reference on the SAs here to make
12121 * sure the SAs can be garbage collected. ipsr_sa has a soft reference
12122 * on the SAs.
12123 * If in the future we want the hard latching of the SAs in the
12124 * ip_xmit_attr_t then we should remove this.
12125 */
12126 if (ixa->ixa_ipsec_esp_sa != NULL) {
12127 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12128 ixa->ixa_ipsec_esp_sa = NULL;
12129 }
12130 if (ixa->ixa_ipsec_ah_sa != NULL) {
12131 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12132 ixa->ixa_ipsec_ah_sa = NULL;
12133 }
12134
12135 /* Do we need to fragment? */
12136 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
12137 pktlen > ixa->ixa_fragsize) {
12138 if (ixaflags & IXAF_IS_IPV4) {
12139 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
12140 /*
12141 * We check for the DF case in ipsec_out_process
12142 * hence this only handles the non-DF case.
12143 */
12144 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
12145 pktlen, ixa->ixa_fragsize,
12146 ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12147 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
12148 &ixa->ixa_cookie));
12149 } else {
12150 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
12151 if (mp == NULL) {
12152 /* MIB and ip_drop_output already done */
12153 return (ENOMEM);
12154 }
12155 pktlen += sizeof (ip6_frag_t);
12156 if (pktlen > ixa->ixa_fragsize) {
12157 return (ip_fragment_v6(mp, ixa->ixa_nce,
12158 ixa->ixa_flags, pktlen,
12159 ixa->ixa_fragsize, ixa->ixa_xmit_hint,
12160 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
12161 ixa->ixa_postfragfn, &ixa->ixa_cookie));
12162 }
12163 }
12164 }
12165 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
12166 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12167 ixa->ixa_no_loop_zoneid, NULL));
12168 }
12169
12170 /*
12171 * Finish the inbound IPsec processing. This function is called from
12172 * ipsec_out_process() if the IPsec packet was processed
12173 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12174 * asynchronously.
12175 *
12176 * This is common to IPv4 and IPv6.
12177 */
12178 void
12179 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
12180 {
12181 iaflags_t iraflags = ira->ira_flags;
12182
12183 /* Length might have changed */
12184 if (iraflags & IRAF_IS_IPV4) {
12185 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12186
12187 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12188 ira->ira_pktlen = ntohs(ipha->ipha_length);
12189 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
12190 ira->ira_protocol = ipha->ipha_protocol;
12191
12192 ip_fanout_v4(mp, ipha, ira);
12193 } else {
12194 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12195 uint8_t *nexthdrp;
12196
12197 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12198 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12199 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
12200 &nexthdrp)) {
12201 /* Malformed packet */
12202 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
12203 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
12204 freemsg(mp);
12205 return;
12206 }
12207 ira->ira_protocol = *nexthdrp;
12208 ip_fanout_v6(mp, ip6h, ira);
12209 }
12210 }
12211
12212 /*
12213 * Select which AH & ESP SA's to use (if any) for the outbound packet.
12214 *
12215 * If this function returns B_TRUE, the requested SA's have been filled
12216 * into the ixa_ipsec_*_sa pointers.
12217 *
12218 * If the function returns B_FALSE, the packet has been "consumed", most
12219 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
12220 *
12221 * The SA references created by the protocol-specific "select"
12222 * function will be released in ip_output_post_ipsec.
12223 */
12224 static boolean_t
12225 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
12226 {
12227 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
12228 ipsec_policy_t *pp;
12229 ipsec_action_t *ap;
12230
12231 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12232 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12233 (ixa->ixa_ipsec_action != NULL));
12234
12235 ap = ixa->ixa_ipsec_action;
12236 if (ap == NULL) {
12237 pp = ixa->ixa_ipsec_policy;
12238 ASSERT(pp != NULL);
12239 ap = pp->ipsp_act;
12240 ASSERT(ap != NULL);
12241 }
12242
12243 /*
12244 * We have an action. now, let's select SA's.
12245 * A side effect of setting ixa_ipsec_*_sa is that it will
12246 * be cached in the conn_t.
12247 */
12248 if (ap->ipa_want_esp) {
12249 if (ixa->ixa_ipsec_esp_sa == NULL) {
12250 need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
12251 IPPROTO_ESP);
12252 }
12253 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
12254 }
12255
12256 if (ap->ipa_want_ah) {
12257 if (ixa->ixa_ipsec_ah_sa == NULL) {
12258 need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
12259 IPPROTO_AH);
12260 }
12261 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
12262 /*
12263 * The ESP and AH processing order needs to be preserved
12264 * when both protocols are required (ESP should be applied
12265 * before AH for an outbound packet). Force an ESP ACQUIRE
12266 * when both ESP and AH are required, and an AH ACQUIRE
12267 * is needed.
12268 */
12269 if (ap->ipa_want_esp && need_ah_acquire)
12270 need_esp_acquire = B_TRUE;
12271 }
12272
12273 /*
12274 * Send an ACQUIRE (extended, regular, or both) if we need one.
12275 * Release SAs that got referenced, but will not be used until we
12276 * acquire _all_ of the SAs we need.
12277 */
12278 if (need_ah_acquire || need_esp_acquire) {
12279 if (ixa->ixa_ipsec_ah_sa != NULL) {
12280 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12281 ixa->ixa_ipsec_ah_sa = NULL;
12282 }
12283 if (ixa->ixa_ipsec_esp_sa != NULL) {
12284 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12285 ixa->ixa_ipsec_esp_sa = NULL;
12286 }
12287
12288 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
12289 return (B_FALSE);
12290 }
12291
12292 return (B_TRUE);
12293 }
12294
12295 /*
12296 * Handle IPsec output processing.
12297 * This function is only entered once for a given packet.
12298 * We try to do things synchronously, but if we need to have user-level
12299 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
12300 * will be completed
12301 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
12302 * - when asynchronous ESP is done it will do AH
12303 *
12304 * In all cases we come back in ip_output_post_ipsec() to fragment and
12305 * send out the packet.
12306 */
12307 int
12308 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
12309 {
12310 ill_t *ill = ixa->ixa_nce->nce_ill;
12311 ip_stack_t *ipst = ixa->ixa_ipst;
12312 ipsec_stack_t *ipss;
12313 ipsec_policy_t *pp;
12314 ipsec_action_t *ap;
12315
12316 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12317
12318 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12319 (ixa->ixa_ipsec_action != NULL));
12320
12321 ipss = ipst->ips_netstack->netstack_ipsec;
12322 if (!ipsec_loaded(ipss)) {
12323 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12324 ip_drop_packet(mp, B_TRUE, ill,
12325 DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12326 &ipss->ipsec_dropper);
12327 return (ENOTSUP);
12328 }
12329
12330 ap = ixa->ixa_ipsec_action;
12331 if (ap == NULL) {
12332 pp = ixa->ixa_ipsec_policy;
12333 ASSERT(pp != NULL);
12334 ap = pp->ipsp_act;
12335 ASSERT(ap != NULL);
12336 }
12337
12338 /* Handle explicit drop action and bypass. */
12339 switch (ap->ipa_act.ipa_type) {
12340 case IPSEC_ACT_DISCARD:
12341 case IPSEC_ACT_REJECT:
12342 ip_drop_packet(mp, B_FALSE, ill,
12343 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12344 return (EHOSTUNREACH); /* IPsec policy failure */
12345 case IPSEC_ACT_BYPASS:
12346 return (ip_output_post_ipsec(mp, ixa));
12347 }
12348
12349 /*
12350 * The order of processing is first insert a IP header if needed.
12351 * Then insert the ESP header and then the AH header.
12352 */
12353 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12354 /*
12355 * First get the outer IP header before sending
12356 * it to ESP.
12357 */
12358 ipha_t *oipha, *iipha;
12359 mblk_t *outer_mp, *inner_mp;
12360
12361 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12362 (void) mi_strlog(ill->ill_rq, 0,
12363 SL_ERROR|SL_TRACE|SL_CONSOLE,
12364 "ipsec_out_process: "
12365 "Self-Encapsulation failed: Out of memory\n");
12366 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12367 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12368 freemsg(mp);
12369 return (ENOBUFS);
12370 }
12371 inner_mp = mp;
12372 ASSERT(inner_mp->b_datap->db_type == M_DATA);
12373 oipha = (ipha_t *)outer_mp->b_rptr;
12374 iipha = (ipha_t *)inner_mp->b_rptr;
12375 *oipha = *iipha;
12376 outer_mp->b_wptr += sizeof (ipha_t);
12377 oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12378 sizeof (ipha_t));
12379 oipha->ipha_protocol = IPPROTO_ENCAP;
12380 oipha->ipha_version_and_hdr_length =
12381 IP_SIMPLE_HDR_VERSION;
12382 oipha->ipha_hdr_checksum = 0;
12383 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12384 outer_mp->b_cont = inner_mp;
12385 mp = outer_mp;
12386
12387 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12388 }
12389
12390 /* If we need to wait for a SA then we can't return any errno */
12391 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12392 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12393 !ipsec_out_select_sa(mp, ixa))
12394 return (0);
12395
12396 /*
12397 * By now, we know what SA's to use. Toss over to ESP & AH
12398 * to do the heavy lifting.
12399 */
12400 if (ap->ipa_want_esp) {
12401 ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12402
12403 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12404 if (mp == NULL) {
12405 /*
12406 * Either it failed or is pending. In the former case
12407 * ipIfStatsInDiscards was increased.
12408 */
12409 return (0);
12410 }
12411 }
12412
12413 if (ap->ipa_want_ah) {
12414 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12415
12416 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12417 if (mp == NULL) {
12418 /*
12419 * Either it failed or is pending. In the former case
12420 * ipIfStatsInDiscards was increased.
12421 */
12422 return (0);
12423 }
12424 }
12425 /*
12426 * We are done with IPsec processing. Send it over
12427 * the wire.
12428 */
12429 return (ip_output_post_ipsec(mp, ixa));
12430 }
12431
12432 /*
12433 * ioctls that go through a down/up sequence may need to wait for the down
12434 * to complete. This involves waiting for the ire and ipif refcnts to go down
12435 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12436 */
12437 /* ARGSUSED */
12438 void
12439 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12440 {
12441 struct iocblk *iocp;
12442 mblk_t *mp1;
12443 ip_ioctl_cmd_t *ipip;
12444 int err;
12445 sin_t *sin;
12446 struct lifreq *lifr;
12447 struct ifreq *ifr;
12448
12449 iocp = (struct iocblk *)mp->b_rptr;
12450 ASSERT(ipsq != NULL);
12451 /* Existence of mp1 verified in ip_wput_nondata */
12452 mp1 = mp->b_cont->b_cont;
12453 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12454 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12455 /*
12456 * Special case where ipx_current_ipif is not set:
12457 * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12458 * We are here as were not able to complete the operation in
12459 * ipif_set_values because we could not become exclusive on
12460 * the new ipsq.
12461 */
12462 ill_t *ill = q->q_ptr;
12463 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12464 }
12465 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12466
12467 if (ipip->ipi_cmd_type == IF_CMD) {
12468 /* This a old style SIOC[GS]IF* command */
12469 ifr = (struct ifreq *)mp1->b_rptr;
12470 sin = (sin_t *)&ifr->ifr_addr;
12471 } else if (ipip->ipi_cmd_type == LIF_CMD) {
12472 /* This a new style SIOC[GS]LIF* command */
12473 lifr = (struct lifreq *)mp1->b_rptr;
12474 sin = (sin_t *)&lifr->lifr_addr;
12475 } else {
12476 sin = NULL;
12477 }
12478
12479 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12480 q, mp, ipip, mp1->b_rptr);
12481
12482 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12483 int, ipip->ipi_cmd,
12484 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12485 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12486
12487 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12488 }
12489
12490 /*
12491 * ioctl processing
12492 *
12493 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12494 * the ioctl command in the ioctl tables, determines the copyin data size
12495 * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12496 *
12497 * ioctl processing then continues when the M_IOCDATA makes its way down to
12498 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its
12499 * associated 'conn' is refheld till the end of the ioctl and the general
12500 * ioctl processing function ip_process_ioctl() is called to extract the
12501 * arguments and process the ioctl. To simplify extraction, ioctl commands
12502 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12503 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12504 * is used to extract the ioctl's arguments.
12505 *
12506 * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12507 * so goes thru the serialization primitive ipsq_try_enter. Then the
12508 * appropriate function to handle the ioctl is called based on the entry in
12509 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12510 * which also refreleases the 'conn' that was refheld at the start of the
12511 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12512 *
12513 * Many exclusive ioctls go thru an internal down up sequence as part of
12514 * the operation. For example an attempt to change the IP address of an
12515 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12516 * does all the cleanup such as deleting all ires that use this address.
12517 * Then we need to wait till all references to the interface go away.
12518 */
12519 void
12520 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12521 {
12522 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12523 ip_ioctl_cmd_t *ipip = arg;
12524 ip_extract_func_t *extract_funcp;
12525 ill_t *ill;
12526 cmd_info_t ci;
12527 int err;
12528 boolean_t entered_ipsq = B_FALSE;
12529
12530 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12531
12532 if (ipip == NULL)
12533 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12534
12535 /*
12536 * SIOCLIFADDIF needs to go thru a special path since the
12537 * ill may not exist yet. This happens in the case of lo0
12538 * which is created using this ioctl.
12539 */
12540 if (ipip->ipi_cmd == SIOCLIFADDIF) {
12541 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12542 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12543 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12544 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12545 return;
12546 }
12547
12548 ci.ci_ipif = NULL;
12549 switch (ipip->ipi_cmd_type) {
12550 case MISC_CMD:
12551 case MSFILT_CMD:
12552 /*
12553 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12554 */
12555 if (ipip->ipi_cmd == IF_UNITSEL) {
12556 /* ioctl comes down the ill */
12557 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12558 ipif_refhold(ci.ci_ipif);
12559 }
12560 err = 0;
12561 ci.ci_sin = NULL;
12562 ci.ci_sin6 = NULL;
12563 ci.ci_lifr = NULL;
12564 extract_funcp = NULL;
12565 break;
12566
12567 case IF_CMD:
12568 case LIF_CMD:
12569 extract_funcp = ip_extract_lifreq;
12570 break;
12571
12572 case ARP_CMD:
12573 case XARP_CMD:
12574 extract_funcp = ip_extract_arpreq;
12575 break;
12576
12577 default:
12578 ASSERT(0);
12579 }
12580
12581 if (extract_funcp != NULL) {
12582 err = (*extract_funcp)(q, mp, ipip, &ci);
12583 if (err != 0) {
12584 DTRACE_PROBE4(ipif__ioctl,
12585 char *, "ip_process_ioctl finish err",
12586 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12587 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12588 return;
12589 }
12590
12591 /*
12592 * All of the extraction functions return a refheld ipif.
12593 */
12594 ASSERT(ci.ci_ipif != NULL);
12595 }
12596
12597 if (!(ipip->ipi_flags & IPI_WR)) {
12598 /*
12599 * A return value of EINPROGRESS means the ioctl is
12600 * either queued and waiting for some reason or has
12601 * already completed.
12602 */
12603 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12604 ci.ci_lifr);
12605 if (ci.ci_ipif != NULL) {
12606 DTRACE_PROBE4(ipif__ioctl,
12607 char *, "ip_process_ioctl finish RD",
12608 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12609 ipif_t *, ci.ci_ipif);
12610 ipif_refrele(ci.ci_ipif);
12611 } else {
12612 DTRACE_PROBE4(ipif__ioctl,
12613 char *, "ip_process_ioctl finish RD",
12614 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12615 }
12616 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12617 return;
12618 }
12619
12620 ASSERT(ci.ci_ipif != NULL);
12621
12622 /*
12623 * If ipsq is non-NULL, we are already being called exclusively
12624 */
12625 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12626 if (ipsq == NULL) {
12627 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12628 NEW_OP, B_TRUE);
12629 if (ipsq == NULL) {
12630 ipif_refrele(ci.ci_ipif);
12631 return;
12632 }
12633 entered_ipsq = B_TRUE;
12634 }
12635 /*
12636 * Release the ipif so that ipif_down and friends that wait for
12637 * references to go away are not misled about the current ipif_refcnt
12638 * values. We are writer so we can access the ipif even after releasing
12639 * the ipif.
12640 */
12641 ipif_refrele(ci.ci_ipif);
12642
12643 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12644
12645 /*
12646 * We need to cache the ill_t that we're going to use as the argument
12647 * to the ipif-ioctl DTrace probe (below) because the ci_ipif can be
12648 * blown away by calling ipi_func.
12649 */
12650 ill = ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill;
12651
12652 /*
12653 * A return value of EINPROGRESS means the ioctl is
12654 * either queued and waiting for some reason or has
12655 * already completed.
12656 */
12657 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12658
12659 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12660 int, ipip->ipi_cmd, ill_t *, ill, ipif_t *, ci.ci_ipif);
12661 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12662
12663 if (entered_ipsq)
12664 ipsq_exit(ipsq);
12665 }
12666
12667 /*
12668 * Complete the ioctl. Typically ioctls use the mi package and need to
12669 * do mi_copyout/mi_copy_done.
12670 */
12671 void
12672 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12673 {
12674 conn_t *connp = NULL;
12675
12676 if (err == EINPROGRESS)
12677 return;
12678
12679 if (CONN_Q(q)) {
12680 connp = Q_TO_CONN(q);
12681 ASSERT(connp->conn_ref >= 2);
12682 }
12683
12684 switch (mode) {
12685 case COPYOUT:
12686 if (err == 0)
12687 mi_copyout(q, mp);
12688 else
12689 mi_copy_done(q, mp, err);
12690 break;
12691
12692 case NO_COPYOUT:
12693 mi_copy_done(q, mp, err);
12694 break;
12695
12696 default:
12697 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */
12698 break;
12699 }
12700
12701 /*
12702 * The conn refhold and ioctlref placed on the conn at the start of the
12703 * ioctl are released here.
12704 */
12705 if (connp != NULL) {
12706 CONN_DEC_IOCTLREF(connp);
12707 CONN_OPER_PENDING_DONE(connp);
12708 }
12709
12710 if (ipsq != NULL)
12711 ipsq_current_finish(ipsq);
12712 }
12713
12714 /* Handles all non data messages */
12715 int
12716 ip_wput_nondata(queue_t *q, mblk_t *mp)
12717 {
12718 mblk_t *mp1;
12719 struct iocblk *iocp;
12720 ip_ioctl_cmd_t *ipip;
12721 conn_t *connp;
12722 cred_t *cr;
12723 char *proto_str;
12724
12725 if (CONN_Q(q))
12726 connp = Q_TO_CONN(q);
12727 else
12728 connp = NULL;
12729
12730 switch (DB_TYPE(mp)) {
12731 case M_IOCTL:
12732 /*
12733 * IOCTL processing begins in ip_sioctl_copyin_setup which
12734 * will arrange to copy in associated control structures.
12735 */
12736 ip_sioctl_copyin_setup(q, mp);
12737 return (0);
12738 case M_IOCDATA:
12739 /*
12740 * Ensure that this is associated with one of our trans-
12741 * parent ioctls. If it's not ours, discard it if we're
12742 * running as a driver, or pass it on if we're a module.
12743 */
12744 iocp = (struct iocblk *)mp->b_rptr;
12745 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12746 if (ipip == NULL) {
12747 if (q->q_next == NULL) {
12748 goto nak;
12749 } else {
12750 putnext(q, mp);
12751 }
12752 return (0);
12753 }
12754 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12755 /*
12756 * The ioctl is one we recognise, but is not consumed
12757 * by IP as a module and we are a module, so we drop
12758 */
12759 goto nak;
12760 }
12761
12762 /* IOCTL continuation following copyin or copyout. */
12763 if (mi_copy_state(q, mp, NULL) == -1) {
12764 /*
12765 * The copy operation failed. mi_copy_state already
12766 * cleaned up, so we're out of here.
12767 */
12768 return (0);
12769 }
12770 /*
12771 * If we just completed a copy in, we become writer and
12772 * continue processing in ip_sioctl_copyin_done. If it
12773 * was a copy out, we call mi_copyout again. If there is
12774 * nothing more to copy out, it will complete the IOCTL.
12775 */
12776 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12777 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12778 mi_copy_done(q, mp, EPROTO);
12779 return (0);
12780 }
12781 /*
12782 * Check for cases that need more copying. A return
12783 * value of 0 means a second copyin has been started,
12784 * so we return; a return value of 1 means no more
12785 * copying is needed, so we continue.
12786 */
12787 if (ipip->ipi_cmd_type == MSFILT_CMD &&
12788 MI_COPY_COUNT(mp) == 1) {
12789 if (ip_copyin_msfilter(q, mp) == 0)
12790 return (0);
12791 }
12792 /*
12793 * Refhold the conn, till the ioctl completes. This is
12794 * needed in case the ioctl ends up in the pending mp
12795 * list. Every mp in the ipx_pending_mp list must have
12796 * a refhold on the conn to resume processing. The
12797 * refhold is released when the ioctl completes
12798 * (whether normally or abnormally). An ioctlref is also
12799 * placed on the conn to prevent TCP from removing the
12800 * queue needed to send the ioctl reply back.
12801 * In all cases ip_ioctl_finish is called to finish
12802 * the ioctl and release the refholds.
12803 */
12804 if (connp != NULL) {
12805 /* This is not a reentry */
12806 CONN_INC_REF(connp);
12807 CONN_INC_IOCTLREF(connp);
12808 } else {
12809 if (!(ipip->ipi_flags & IPI_MODOK)) {
12810 mi_copy_done(q, mp, EINVAL);
12811 return (0);
12812 }
12813 }
12814
12815 ip_process_ioctl(NULL, q, mp, ipip);
12816
12817 } else {
12818 mi_copyout(q, mp);
12819 }
12820 return (0);
12821
12822 case M_IOCNAK:
12823 /*
12824 * The only way we could get here is if a resolver didn't like
12825 * an IOCTL we sent it. This shouldn't happen.
12826 */
12827 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12828 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12829 ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12830 freemsg(mp);
12831 return (0);
12832 case M_IOCACK:
12833 /* /dev/ip shouldn't see this */
12834 goto nak;
12835 case M_FLUSH:
12836 if (*mp->b_rptr & FLUSHW)
12837 flushq(q, FLUSHALL);
12838 if (q->q_next) {
12839 putnext(q, mp);
12840 return (0);
12841 }
12842 if (*mp->b_rptr & FLUSHR) {
12843 *mp->b_rptr &= ~FLUSHW;
12844 qreply(q, mp);
12845 return (0);
12846 }
12847 freemsg(mp);
12848 return (0);
12849 case M_CTL:
12850 break;
12851 case M_PROTO:
12852 case M_PCPROTO:
12853 /*
12854 * The only PROTO messages we expect are SNMP-related.
12855 */
12856 switch (((union T_primitives *)mp->b_rptr)->type) {
12857 case T_SVR4_OPTMGMT_REQ:
12858 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12859 "flags %x\n",
12860 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12861
12862 if (connp == NULL) {
12863 proto_str = "T_SVR4_OPTMGMT_REQ";
12864 goto protonak;
12865 }
12866
12867 /*
12868 * All Solaris components should pass a db_credp
12869 * for this TPI message, hence we ASSERT.
12870 * But in case there is some other M_PROTO that looks
12871 * like a TPI message sent by some other kernel
12872 * component, we check and return an error.
12873 */
12874 cr = msg_getcred(mp, NULL);
12875 ASSERT(cr != NULL);
12876 if (cr == NULL) {
12877 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12878 if (mp != NULL)
12879 qreply(q, mp);
12880 return (0);
12881 }
12882
12883 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12884 proto_str = "Bad SNMPCOM request?";
12885 goto protonak;
12886 }
12887 return (0);
12888 default:
12889 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12890 (int)*(uint_t *)mp->b_rptr));
12891 freemsg(mp);
12892 return (0);
12893 }
12894 default:
12895 break;
12896 }
12897 if (q->q_next) {
12898 putnext(q, mp);
12899 } else
12900 freemsg(mp);
12901 return (0);
12902
12903 nak:
12904 iocp->ioc_error = EINVAL;
12905 mp->b_datap->db_type = M_IOCNAK;
12906 iocp->ioc_count = 0;
12907 qreply(q, mp);
12908 return (0);
12909
12910 protonak:
12911 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12912 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12913 qreply(q, mp);
12914 return (0);
12915 }
12916
12917 /*
12918 * Process IP options in an outbound packet. Verify that the nexthop in a
12919 * strict source route is onlink.
12920 * Returns non-zero if something fails in which case an ICMP error has been
12921 * sent and mp freed.
12922 *
12923 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12924 */
12925 int
12926 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12927 {
12928 ipoptp_t opts;
12929 uchar_t *opt;
12930 uint8_t optval;
12931 uint8_t optlen;
12932 ipaddr_t dst;
12933 intptr_t code = 0;
12934 ire_t *ire;
12935 ip_stack_t *ipst = ixa->ixa_ipst;
12936 ip_recv_attr_t iras;
12937
12938 ip2dbg(("ip_output_options\n"));
12939
12940 dst = ipha->ipha_dst;
12941 for (optval = ipoptp_first(&opts, ipha);
12942 optval != IPOPT_EOL;
12943 optval = ipoptp_next(&opts)) {
12944 opt = opts.ipoptp_cur;
12945 optlen = opts.ipoptp_len;
12946 ip2dbg(("ip_output_options: opt %d, len %d\n",
12947 optval, optlen));
12948 switch (optval) {
12949 uint32_t off;
12950 case IPOPT_SSRR:
12951 case IPOPT_LSRR:
12952 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12953 ip1dbg((
12954 "ip_output_options: bad option offset\n"));
12955 code = (char *)&opt[IPOPT_OLEN] -
12956 (char *)ipha;
12957 goto param_prob;
12958 }
12959 off = opt[IPOPT_OFFSET];
12960 ip1dbg(("ip_output_options: next hop 0x%x\n",
12961 ntohl(dst)));
12962 /*
12963 * For strict: verify that dst is directly
12964 * reachable.
12965 */
12966 if (optval == IPOPT_SSRR) {
12967 ire = ire_ftable_lookup_v4(dst, 0, 0,
12968 IRE_INTERFACE, NULL, ALL_ZONES,
12969 ixa->ixa_tsl,
12970 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
12971 NULL);
12972 if (ire == NULL) {
12973 ip1dbg(("ip_output_options: SSRR not"
12974 " directly reachable: 0x%x\n",
12975 ntohl(dst)));
12976 goto bad_src_route;
12977 }
12978 ire_refrele(ire);
12979 }
12980 break;
12981 case IPOPT_RR:
12982 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12983 ip1dbg((
12984 "ip_output_options: bad option offset\n"));
12985 code = (char *)&opt[IPOPT_OLEN] -
12986 (char *)ipha;
12987 goto param_prob;
12988 }
12989 break;
12990 case IPOPT_TS:
12991 /*
12992 * Verify that length >=5 and that there is either
12993 * room for another timestamp or that the overflow
12994 * counter is not maxed out.
12995 */
12996 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
12997 if (optlen < IPOPT_MINLEN_IT) {
12998 goto param_prob;
12999 }
13000 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13001 ip1dbg((
13002 "ip_output_options: bad option offset\n"));
13003 code = (char *)&opt[IPOPT_OFFSET] -
13004 (char *)ipha;
13005 goto param_prob;
13006 }
13007 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
13008 case IPOPT_TS_TSONLY:
13009 off = IPOPT_TS_TIMELEN;
13010 break;
13011 case IPOPT_TS_TSANDADDR:
13012 case IPOPT_TS_PRESPEC:
13013 case IPOPT_TS_PRESPEC_RFC791:
13014 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
13015 break;
13016 default:
13017 code = (char *)&opt[IPOPT_POS_OV_FLG] -
13018 (char *)ipha;
13019 goto param_prob;
13020 }
13021 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
13022 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
13023 /*
13024 * No room and the overflow counter is 15
13025 * already.
13026 */
13027 goto param_prob;
13028 }
13029 break;
13030 }
13031 }
13032
13033 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
13034 return (0);
13035
13036 ip1dbg(("ip_output_options: error processing IP options."));
13037 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
13038
13039 param_prob:
13040 bzero(&iras, sizeof (iras));
13041 iras.ira_ill = iras.ira_rill = ill;
13042 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13043 iras.ira_rifindex = iras.ira_ruifindex;
13044 iras.ira_flags = IRAF_IS_IPV4;
13045
13046 ip_drop_output("ip_output_options", mp, ill);
13047 icmp_param_problem(mp, (uint8_t)code, &iras);
13048 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13049 return (-1);
13050
13051 bad_src_route:
13052 bzero(&iras, sizeof (iras));
13053 iras.ira_ill = iras.ira_rill = ill;
13054 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13055 iras.ira_rifindex = iras.ira_ruifindex;
13056 iras.ira_flags = IRAF_IS_IPV4;
13057
13058 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
13059 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
13060 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13061 return (-1);
13062 }
13063
13064 /*
13065 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
13066 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
13067 * thru /etc/system.
13068 */
13069 #define CONN_MAXDRAINCNT 64
13070
13071 static void
13072 conn_drain_init(ip_stack_t *ipst)
13073 {
13074 int i, j;
13075 idl_tx_list_t *itl_tx;
13076
13077 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
13078
13079 if ((ipst->ips_conn_drain_list_cnt == 0) ||
13080 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
13081 /*
13082 * Default value of the number of drainers is the
13083 * number of cpus, subject to maximum of 8 drainers.
13084 */
13085 if (boot_max_ncpus != -1)
13086 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
13087 else
13088 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
13089 }
13090
13091 ipst->ips_idl_tx_list =
13092 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
13093 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13094 itl_tx = &ipst->ips_idl_tx_list[i];
13095 itl_tx->txl_drain_list =
13096 kmem_zalloc(ipst->ips_conn_drain_list_cnt *
13097 sizeof (idl_t), KM_SLEEP);
13098 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
13099 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
13100 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
13101 MUTEX_DEFAULT, NULL);
13102 itl_tx->txl_drain_list[j].idl_itl = itl_tx;
13103 }
13104 }
13105 }
13106
13107 static void
13108 conn_drain_fini(ip_stack_t *ipst)
13109 {
13110 int i;
13111 idl_tx_list_t *itl_tx;
13112
13113 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13114 itl_tx = &ipst->ips_idl_tx_list[i];
13115 kmem_free(itl_tx->txl_drain_list,
13116 ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
13117 }
13118 kmem_free(ipst->ips_idl_tx_list,
13119 TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
13120 ipst->ips_idl_tx_list = NULL;
13121 }
13122
13123 /*
13124 * Flow control has blocked us from proceeding. Insert the given conn in one
13125 * of the conn drain lists. When flow control is unblocked, either ip_wsrv()
13126 * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
13127 * will call conn_walk_drain(). See the flow control notes at the top of this
13128 * file for more details.
13129 */
13130 void
13131 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
13132 {
13133 idl_t *idl = tx_list->txl_drain_list;
13134 uint_t index;
13135 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
13136
13137 mutex_enter(&connp->conn_lock);
13138 if (connp->conn_state_flags & CONN_CLOSING) {
13139 /*
13140 * The conn is closing as a result of which CONN_CLOSING
13141 * is set. Return.
13142 */
13143 mutex_exit(&connp->conn_lock);
13144 return;
13145 } else if (connp->conn_idl == NULL) {
13146 /*
13147 * Assign the next drain list round robin. We dont' use
13148 * a lock, and thus it may not be strictly round robin.
13149 * Atomicity of load/stores is enough to make sure that
13150 * conn_drain_list_index is always within bounds.
13151 */
13152 index = tx_list->txl_drain_index;
13153 ASSERT(index < ipst->ips_conn_drain_list_cnt);
13154 connp->conn_idl = &tx_list->txl_drain_list[index];
13155 index++;
13156 if (index == ipst->ips_conn_drain_list_cnt)
13157 index = 0;
13158 tx_list->txl_drain_index = index;
13159 } else {
13160 ASSERT(connp->conn_idl->idl_itl == tx_list);
13161 }
13162 mutex_exit(&connp->conn_lock);
13163
13164 idl = connp->conn_idl;
13165 mutex_enter(&idl->idl_lock);
13166 if ((connp->conn_drain_prev != NULL) ||
13167 (connp->conn_state_flags & CONN_CLOSING)) {
13168 /*
13169 * The conn is either already in the drain list or closing.
13170 * (We needed to check for CONN_CLOSING again since close can
13171 * sneak in between dropping conn_lock and acquiring idl_lock.)
13172 */
13173 mutex_exit(&idl->idl_lock);
13174 return;
13175 }
13176
13177 /*
13178 * The conn is not in the drain list. Insert it at the
13179 * tail of the drain list. The drain list is circular
13180 * and doubly linked. idl_conn points to the 1st element
13181 * in the list.
13182 */
13183 if (idl->idl_conn == NULL) {
13184 idl->idl_conn = connp;
13185 connp->conn_drain_next = connp;
13186 connp->conn_drain_prev = connp;
13187 } else {
13188 conn_t *head = idl->idl_conn;
13189
13190 connp->conn_drain_next = head;
13191 connp->conn_drain_prev = head->conn_drain_prev;
13192 head->conn_drain_prev->conn_drain_next = connp;
13193 head->conn_drain_prev = connp;
13194 }
13195 /*
13196 * For non streams based sockets assert flow control.
13197 */
13198 conn_setqfull(connp, NULL);
13199 mutex_exit(&idl->idl_lock);
13200 }
13201
13202 static void
13203 conn_drain_remove(conn_t *connp)
13204 {
13205 idl_t *idl = connp->conn_idl;
13206
13207 if (idl != NULL) {
13208 /*
13209 * Remove ourself from the drain list.
13210 */
13211 if (connp->conn_drain_next == connp) {
13212 /* Singleton in the list */
13213 ASSERT(connp->conn_drain_prev == connp);
13214 idl->idl_conn = NULL;
13215 } else {
13216 connp->conn_drain_prev->conn_drain_next =
13217 connp->conn_drain_next;
13218 connp->conn_drain_next->conn_drain_prev =
13219 connp->conn_drain_prev;
13220 if (idl->idl_conn == connp)
13221 idl->idl_conn = connp->conn_drain_next;
13222 }
13223
13224 /*
13225 * NOTE: because conn_idl is associated with a specific drain
13226 * list which in turn is tied to the index the TX ring
13227 * (txl_cookie) hashes to, and because the TX ring can change
13228 * over the lifetime of the conn_t, we must clear conn_idl so
13229 * a subsequent conn_drain_insert() will set conn_idl again
13230 * based on the latest txl_cookie.
13231 */
13232 connp->conn_idl = NULL;
13233 }
13234 connp->conn_drain_next = NULL;
13235 connp->conn_drain_prev = NULL;
13236
13237 conn_clrqfull(connp, NULL);
13238 /*
13239 * For streams based sockets open up flow control.
13240 */
13241 if (!IPCL_IS_NONSTR(connp))
13242 enableok(connp->conn_wq);
13243 }
13244
13245 /*
13246 * This conn is closing, and we are called from ip_close. OR
13247 * this conn is draining because flow-control on the ill has been relieved.
13248 *
13249 * We must also need to remove conn's on this idl from the list, and also
13250 * inform the sockfs upcalls about the change in flow-control.
13251 */
13252 static void
13253 conn_drain(conn_t *connp, boolean_t closing)
13254 {
13255 idl_t *idl;
13256 conn_t *next_connp;
13257
13258 /*
13259 * connp->conn_idl is stable at this point, and no lock is needed
13260 * to check it. If we are called from ip_close, close has already
13261 * set CONN_CLOSING, thus freezing the value of conn_idl, and
13262 * called us only because conn_idl is non-null. If we are called thru
13263 * service, conn_idl could be null, but it cannot change because
13264 * service is single-threaded per queue, and there cannot be another
13265 * instance of service trying to call conn_drain_insert on this conn
13266 * now.
13267 */
13268 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
13269
13270 /*
13271 * If the conn doesn't exist or is not on a drain list, bail.
13272 */
13273 if (connp == NULL || connp->conn_idl == NULL ||
13274 connp->conn_drain_prev == NULL) {
13275 return;
13276 }
13277
13278 idl = connp->conn_idl;
13279 ASSERT(MUTEX_HELD(&idl->idl_lock));
13280
13281 if (!closing) {
13282 next_connp = connp->conn_drain_next;
13283 while (next_connp != connp) {
13284 conn_t *delconnp = next_connp;
13285
13286 next_connp = next_connp->conn_drain_next;
13287 conn_drain_remove(delconnp);
13288 }
13289 ASSERT(connp->conn_drain_next == idl->idl_conn);
13290 }
13291 conn_drain_remove(connp);
13292 }
13293
13294 /*
13295 * Write service routine. Shared perimeter entry point.
13296 * The device queue's messages has fallen below the low water mark and STREAMS
13297 * has backenabled the ill_wq. Send sockfs notification about flow-control on
13298 * each waiting conn.
13299 */
13300 int
13301 ip_wsrv(queue_t *q)
13302 {
13303 ill_t *ill;
13304
13305 ill = (ill_t *)q->q_ptr;
13306 if (ill->ill_state_flags == 0) {
13307 ip_stack_t *ipst = ill->ill_ipst;
13308
13309 /*
13310 * The device flow control has opened up.
13311 * Walk through conn drain lists and qenable the
13312 * first conn in each list. This makes sense only
13313 * if the stream is fully plumbed and setup.
13314 * Hence the ill_state_flags check above.
13315 */
13316 ip1dbg(("ip_wsrv: walking\n"));
13317 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
13318 enableok(ill->ill_wq);
13319 }
13320 return (0);
13321 }
13322
13323 /*
13324 * Callback to disable flow control in IP.
13325 *
13326 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
13327 * is enabled.
13328 *
13329 * When MAC_TX() is not able to send any more packets, dld sets its queue
13330 * to QFULL and enable the STREAMS flow control. Later, when the underlying
13331 * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13332 * function and wakes up corresponding mac worker threads, which in turn
13333 * calls this callback function, and disables flow control.
13334 */
13335 void
13336 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13337 {
13338 ill_t *ill = (ill_t *)arg;
13339 ip_stack_t *ipst = ill->ill_ipst;
13340 idl_tx_list_t *idl_txl;
13341
13342 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13343 mutex_enter(&idl_txl->txl_lock);
13344 /* add code to to set a flag to indicate idl_txl is enabled */
13345 conn_walk_drain(ipst, idl_txl);
13346 mutex_exit(&idl_txl->txl_lock);
13347 }
13348
13349 /*
13350 * Flow control has been relieved and STREAMS has backenabled us; drain
13351 * all the conn lists on `tx_list'.
13352 */
13353 static void
13354 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13355 {
13356 int i;
13357 idl_t *idl;
13358
13359 IP_STAT(ipst, ip_conn_walk_drain);
13360
13361 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13362 idl = &tx_list->txl_drain_list[i];
13363 mutex_enter(&idl->idl_lock);
13364 conn_drain(idl->idl_conn, B_FALSE);
13365 mutex_exit(&idl->idl_lock);
13366 }
13367 }
13368
13369 /*
13370 * Determine if the ill and multicast aspects of that packets
13371 * "matches" the conn.
13372 */
13373 boolean_t
13374 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13375 {
13376 ill_t *ill = ira->ira_rill;
13377 zoneid_t zoneid = ira->ira_zoneid;
13378 uint_t in_ifindex;
13379 ipaddr_t dst, src;
13380
13381 dst = ipha->ipha_dst;
13382 src = ipha->ipha_src;
13383
13384 /*
13385 * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13386 * unicast, broadcast and multicast reception to
13387 * conn_incoming_ifindex.
13388 * conn_wantpacket is called for unicast, broadcast and
13389 * multicast packets.
13390 */
13391 in_ifindex = connp->conn_incoming_ifindex;
13392
13393 /* mpathd can bind to the under IPMP interface, which we allow */
13394 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13395 if (!IS_UNDER_IPMP(ill))
13396 return (B_FALSE);
13397
13398 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13399 return (B_FALSE);
13400 }
13401
13402 if (!IPCL_ZONE_MATCH(connp, zoneid))
13403 return (B_FALSE);
13404
13405 if (!(ira->ira_flags & IRAF_MULTICAST))
13406 return (B_TRUE);
13407
13408 if (connp->conn_multi_router) {
13409 /* multicast packet and multicast router socket: send up */
13410 return (B_TRUE);
13411 }
13412
13413 if (ipha->ipha_protocol == IPPROTO_PIM ||
13414 ipha->ipha_protocol == IPPROTO_RSVP)
13415 return (B_TRUE);
13416
13417 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13418 }
13419
13420 void
13421 conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13422 {
13423 if (IPCL_IS_NONSTR(connp)) {
13424 (*connp->conn_upcalls->su_txq_full)
13425 (connp->conn_upper_handle, B_TRUE);
13426 if (flow_stopped != NULL)
13427 *flow_stopped = B_TRUE;
13428 } else {
13429 queue_t *q = connp->conn_wq;
13430
13431 ASSERT(q != NULL);
13432 if (!(q->q_flag & QFULL)) {
13433 mutex_enter(QLOCK(q));
13434 if (!(q->q_flag & QFULL)) {
13435 /* still need to set QFULL */
13436 q->q_flag |= QFULL;
13437 /* set flow_stopped to true under QLOCK */
13438 if (flow_stopped != NULL)
13439 *flow_stopped = B_TRUE;
13440 mutex_exit(QLOCK(q));
13441 } else {
13442 /* flow_stopped is left unchanged */
13443 mutex_exit(QLOCK(q));
13444 }
13445 }
13446 }
13447 }
13448
13449 void
13450 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13451 {
13452 if (IPCL_IS_NONSTR(connp)) {
13453 (*connp->conn_upcalls->su_txq_full)
13454 (connp->conn_upper_handle, B_FALSE);
13455 if (flow_stopped != NULL)
13456 *flow_stopped = B_FALSE;
13457 } else {
13458 queue_t *q = connp->conn_wq;
13459
13460 ASSERT(q != NULL);
13461 if (q->q_flag & QFULL) {
13462 mutex_enter(QLOCK(q));
13463 if (q->q_flag & QFULL) {
13464 q->q_flag &= ~QFULL;
13465 /* set flow_stopped to false under QLOCK */
13466 if (flow_stopped != NULL)
13467 *flow_stopped = B_FALSE;
13468 mutex_exit(QLOCK(q));
13469 if (q->q_flag & QWANTW)
13470 qbackenable(q, 0);
13471 } else {
13472 /* flow_stopped is left unchanged */
13473 mutex_exit(QLOCK(q));
13474 }
13475 }
13476 }
13477
13478 mutex_enter(&connp->conn_lock);
13479 connp->conn_blocked = B_FALSE;
13480 mutex_exit(&connp->conn_lock);
13481 }
13482
13483 /*
13484 * Return the length in bytes of the IPv4 headers (base header, label, and
13485 * other IP options) that will be needed based on the
13486 * ip_pkt_t structure passed by the caller.
13487 *
13488 * The returned length does not include the length of the upper level
13489 * protocol (ULP) header.
13490 * The caller needs to check that the length doesn't exceed the max for IPv4.
13491 */
13492 int
13493 ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13494 {
13495 int len;
13496
13497 len = IP_SIMPLE_HDR_LENGTH;
13498 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13499 ASSERT(ipp->ipp_label_len_v4 != 0);
13500 /* We need to round up here */
13501 len += (ipp->ipp_label_len_v4 + 3) & ~3;
13502 }
13503
13504 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13505 ASSERT(ipp->ipp_ipv4_options_len != 0);
13506 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13507 len += ipp->ipp_ipv4_options_len;
13508 }
13509 return (len);
13510 }
13511
13512 /*
13513 * All-purpose routine to build an IPv4 header with options based
13514 * on the abstract ip_pkt_t.
13515 *
13516 * The caller has to set the source and destination address as well as
13517 * ipha_length. The caller has to massage any source route and compensate
13518 * for the ULP pseudo-header checksum due to the source route.
13519 */
13520 void
13521 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13522 uint8_t protocol)
13523 {
13524 ipha_t *ipha = (ipha_t *)buf;
13525 uint8_t *cp;
13526
13527 /* Initialize IPv4 header */
13528 ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13529 ipha->ipha_length = 0; /* Caller will set later */
13530 ipha->ipha_ident = 0;
13531 ipha->ipha_fragment_offset_and_flags = 0;
13532 ipha->ipha_ttl = ipp->ipp_unicast_hops;
13533 ipha->ipha_protocol = protocol;
13534 ipha->ipha_hdr_checksum = 0;
13535
13536 if ((ipp->ipp_fields & IPPF_ADDR) &&
13537 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13538 ipha->ipha_src = ipp->ipp_addr_v4;
13539
13540 cp = (uint8_t *)&ipha[1];
13541 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13542 ASSERT(ipp->ipp_label_len_v4 != 0);
13543 bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4);
13544 cp += ipp->ipp_label_len_v4;
13545 /* We need to round up here */
13546 while ((uintptr_t)cp & 0x3) {
13547 *cp++ = IPOPT_NOP;
13548 }
13549 }
13550
13551 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13552 ASSERT(ipp->ipp_ipv4_options_len != 0);
13553 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13554 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13555 cp += ipp->ipp_ipv4_options_len;
13556 }
13557 ipha->ipha_version_and_hdr_length =
13558 (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13559
13560 ASSERT((int)(cp - buf) == buf_len);
13561 }
13562
13563 /* Allocate the private structure */
13564 static int
13565 ip_priv_alloc(void **bufp)
13566 {
13567 void *buf;
13568
13569 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13570 return (ENOMEM);
13571
13572 *bufp = buf;
13573 return (0);
13574 }
13575
13576 /* Function to delete the private structure */
13577 void
13578 ip_priv_free(void *buf)
13579 {
13580 ASSERT(buf != NULL);
13581 kmem_free(buf, sizeof (ip_priv_t));
13582 }
13583
13584 /*
13585 * The entry point for IPPF processing.
13586 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13587 * routine just returns.
13588 *
13589 * When called, ip_process generates an ipp_packet_t structure
13590 * which holds the state information for this packet and invokes the
13591 * the classifier (via ipp_packet_process). The classification, depending on
13592 * configured filters, results in a list of actions for this packet. Invoking
13593 * an action may cause the packet to be dropped, in which case we return NULL.
13594 * proc indicates the callout position for
13595 * this packet and ill is the interface this packet arrived on or will leave
13596 * on (inbound and outbound resp.).
13597 *
13598 * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13599 * on the ill corrsponding to the destination IP address.
13600 */
13601 mblk_t *
13602 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13603 {
13604 ip_priv_t *priv;
13605 ipp_action_id_t aid;
13606 int rc = 0;
13607 ipp_packet_t *pp;
13608
13609 /* If the classifier is not loaded, return */
13610 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13611 return (mp);
13612 }
13613
13614 ASSERT(mp != NULL);
13615
13616 /* Allocate the packet structure */
13617 rc = ipp_packet_alloc(&pp, "ip", aid);
13618 if (rc != 0)
13619 goto drop;
13620
13621 /* Allocate the private structure */
13622 rc = ip_priv_alloc((void **)&priv);
13623 if (rc != 0) {
13624 ipp_packet_free(pp);
13625 goto drop;
13626 }
13627 priv->proc = proc;
13628 priv->ill_index = ill_get_upper_ifindex(rill);
13629
13630 ipp_packet_set_private(pp, priv, ip_priv_free);
13631 ipp_packet_set_data(pp, mp);
13632
13633 /* Invoke the classifier */
13634 rc = ipp_packet_process(&pp);
13635 if (pp != NULL) {
13636 mp = ipp_packet_get_data(pp);
13637 ipp_packet_free(pp);
13638 if (rc != 0)
13639 goto drop;
13640 return (mp);
13641 } else {
13642 /* No mp to trace in ip_drop_input/ip_drop_output */
13643 mp = NULL;
13644 }
13645 drop:
13646 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13647 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13648 ip_drop_input("ip_process", mp, ill);
13649 } else {
13650 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13651 ip_drop_output("ip_process", mp, ill);
13652 }
13653 freemsg(mp);
13654 return (NULL);
13655 }
13656
13657 /*
13658 * Propagate a multicast group membership operation (add/drop) on
13659 * all the interfaces crossed by the related multirt routes.
13660 * The call is considered successful if the operation succeeds
13661 * on at least one interface.
13662 *
13663 * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the
13664 * multicast addresses with the ire argument being the first one.
13665 * We walk the bucket to find all the of those.
13666 *
13667 * Common to IPv4 and IPv6.
13668 */
13669 static int
13670 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
13671 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
13672 ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group,
13673 mcast_record_t fmode, const in6_addr_t *v6src)
13674 {
13675 ire_t *ire_gw;
13676 irb_t *irb;
13677 int ifindex;
13678 int error = 0;
13679 int result;
13680 ip_stack_t *ipst = ire->ire_ipst;
13681 ipaddr_t group;
13682 boolean_t isv6;
13683 int match_flags;
13684
13685 if (IN6_IS_ADDR_V4MAPPED(v6group)) {
13686 IN6_V4MAPPED_TO_IPADDR(v6group, group);
13687 isv6 = B_FALSE;
13688 } else {
13689 isv6 = B_TRUE;
13690 }
13691
13692 irb = ire->ire_bucket;
13693 ASSERT(irb != NULL);
13694
13695 result = 0;
13696 irb_refhold(irb);
13697 for (; ire != NULL; ire = ire->ire_next) {
13698 if ((ire->ire_flags & RTF_MULTIRT) == 0)
13699 continue;
13700
13701 /* We handle -ifp routes by matching on the ill if set */
13702 match_flags = MATCH_IRE_TYPE;
13703 if (ire->ire_ill != NULL)
13704 match_flags |= MATCH_IRE_ILL;
13705
13706 if (isv6) {
13707 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group))
13708 continue;
13709
13710 ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6,
13711 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13712 match_flags, 0, ipst, NULL);
13713 } else {
13714 if (ire->ire_addr != group)
13715 continue;
13716
13717 ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr,
13718 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13719 match_flags, 0, ipst, NULL);
13720 }
13721 /* No interface route exists for the gateway; skip this ire. */
13722 if (ire_gw == NULL)
13723 continue;
13724 if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
13725 ire_refrele(ire_gw);
13726 continue;
13727 }
13728 ASSERT(ire_gw->ire_ill != NULL); /* IRE_INTERFACE */
13729 ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex;
13730
13731 /*
13732 * The operation is considered a success if
13733 * it succeeds at least once on any one interface.
13734 */
13735 error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex,
13736 fmode, v6src);
13737 if (error == 0)
13738 result = CGTP_MCAST_SUCCESS;
13739
13740 ire_refrele(ire_gw);
13741 }
13742 irb_refrele(irb);
13743 /*
13744 * Consider the call as successful if we succeeded on at least
13745 * one interface. Otherwise, return the last encountered error.
13746 */
13747 return (result == CGTP_MCAST_SUCCESS ? 0 : error);
13748 }
13749
13750 /*
13751 * Return the expected CGTP hooks version number.
13752 */
13753 int
13754 ip_cgtp_filter_supported(void)
13755 {
13756 return (ip_cgtp_filter_rev);
13757 }
13758
13759 /*
13760 * CGTP hooks can be registered by invoking this function.
13761 * Checks that the version number matches.
13762 */
13763 int
13764 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops)
13765 {
13766 netstack_t *ns;
13767 ip_stack_t *ipst;
13768
13769 if (ops->cfo_filter_rev != CGTP_FILTER_REV)
13770 return (ENOTSUP);
13771
13772 ns = netstack_find_by_stackid(stackid);
13773 if (ns == NULL)
13774 return (EINVAL);
13775 ipst = ns->netstack_ip;
13776 ASSERT(ipst != NULL);
13777
13778 if (ipst->ips_ip_cgtp_filter_ops != NULL) {
13779 netstack_rele(ns);
13780 return (EALREADY);
13781 }
13782
13783 ipst->ips_ip_cgtp_filter_ops = ops;
13784
13785 ill_set_inputfn_all(ipst);
13786
13787 netstack_rele(ns);
13788 return (0);
13789 }
13790
13791 /*
13792 * CGTP hooks can be unregistered by invoking this function.
13793 * Returns ENXIO if there was no registration.
13794 * Returns EBUSY if the ndd variable has not been turned off.
13795 */
13796 int
13797 ip_cgtp_filter_unregister(netstackid_t stackid)
13798 {
13799 netstack_t *ns;
13800 ip_stack_t *ipst;
13801
13802 ns = netstack_find_by_stackid(stackid);
13803 if (ns == NULL)
13804 return (EINVAL);
13805 ipst = ns->netstack_ip;
13806 ASSERT(ipst != NULL);
13807
13808 if (ipst->ips_ip_cgtp_filter) {
13809 netstack_rele(ns);
13810 return (EBUSY);
13811 }
13812
13813 if (ipst->ips_ip_cgtp_filter_ops == NULL) {
13814 netstack_rele(ns);
13815 return (ENXIO);
13816 }
13817 ipst->ips_ip_cgtp_filter_ops = NULL;
13818
13819 ill_set_inputfn_all(ipst);
13820
13821 netstack_rele(ns);
13822 return (0);
13823 }
13824
13825 /*
13826 * Check whether there is a CGTP filter registration.
13827 * Returns non-zero if there is a registration, otherwise returns zero.
13828 * Note: returns zero if bad stackid.
13829 */
13830 int
13831 ip_cgtp_filter_is_registered(netstackid_t stackid)
13832 {
13833 netstack_t *ns;
13834 ip_stack_t *ipst;
13835 int ret;
13836
13837 ns = netstack_find_by_stackid(stackid);
13838 if (ns == NULL)
13839 return (0);
13840 ipst = ns->netstack_ip;
13841 ASSERT(ipst != NULL);
13842
13843 if (ipst->ips_ip_cgtp_filter_ops != NULL)
13844 ret = 1;
13845 else
13846 ret = 0;
13847
13848 netstack_rele(ns);
13849 return (ret);
13850 }
13851
13852 static int
13853 ip_squeue_switch(int val)
13854 {
13855 int rval;
13856
13857 switch (val) {
13858 case IP_SQUEUE_ENTER_NODRAIN:
13859 rval = SQ_NODRAIN;
13860 break;
13861 case IP_SQUEUE_ENTER:
13862 rval = SQ_PROCESS;
13863 break;
13864 case IP_SQUEUE_FILL:
13865 default:
13866 rval = SQ_FILL;
13867 break;
13868 }
13869 return (rval);
13870 }
13871
13872 static void *
13873 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13874 {
13875 kstat_t *ksp;
13876
13877 ip_stat_t template = {
13878 { "ip_udp_fannorm", KSTAT_DATA_UINT64 },
13879 { "ip_udp_fanmb", KSTAT_DATA_UINT64 },
13880 { "ip_recv_pullup", KSTAT_DATA_UINT64 },
13881 { "ip_db_ref", KSTAT_DATA_UINT64 },
13882 { "ip_notaligned", KSTAT_DATA_UINT64 },
13883 { "ip_multimblk", KSTAT_DATA_UINT64 },
13884 { "ip_opt", KSTAT_DATA_UINT64 },
13885 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 },
13886 { "ip_conn_flputbq", KSTAT_DATA_UINT64 },
13887 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 },
13888 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 },
13889 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 },
13890 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 },
13891 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 },
13892 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 },
13893 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 },
13894 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 },
13895 { "ip_nce_mcast_reclaim_calls", KSTAT_DATA_UINT64 },
13896 { "ip_nce_mcast_reclaim_deleted", KSTAT_DATA_UINT64 },
13897 { "ip_nce_mcast_reclaim_tqfail", KSTAT_DATA_UINT64 },
13898 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 },
13899 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 },
13900 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13901 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13902 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13903 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13904 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13905 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13906 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 },
13907 { "conn_in_recvopts", KSTAT_DATA_UINT64 },
13908 { "conn_in_recvif", KSTAT_DATA_UINT64 },
13909 { "conn_in_recvslla", KSTAT_DATA_UINT64 },
13910 { "conn_in_recvucred", KSTAT_DATA_UINT64 },
13911 { "conn_in_recvttl", KSTAT_DATA_UINT64 },
13912 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 },
13913 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 },
13914 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 },
13915 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 },
13916 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 },
13917 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 },
13918 { "conn_in_recvtclass", KSTAT_DATA_UINT64 },
13919 { "conn_in_timestamp", KSTAT_DATA_UINT64 },
13920 };
13921
13922 ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13923 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13924 KSTAT_FLAG_VIRTUAL, stackid);
13925
13926 if (ksp == NULL)
13927 return (NULL);
13928
13929 bcopy(&template, ip_statisticsp, sizeof (template));
13930 ksp->ks_data = (void *)ip_statisticsp;
13931 ksp->ks_private = (void *)(uintptr_t)stackid;
13932
13933 kstat_install(ksp);
13934 return (ksp);
13935 }
13936
13937 static void
13938 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13939 {
13940 if (ksp != NULL) {
13941 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13942 kstat_delete_netstack(ksp, stackid);
13943 }
13944 }
13945
13946 static void *
13947 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
13948 {
13949 kstat_t *ksp;
13950
13951 ip_named_kstat_t template = {
13952 { "forwarding", KSTAT_DATA_UINT32, 0 },
13953 { "defaultTTL", KSTAT_DATA_UINT32, 0 },
13954 { "inReceives", KSTAT_DATA_UINT64, 0 },
13955 { "inHdrErrors", KSTAT_DATA_UINT32, 0 },
13956 { "inAddrErrors", KSTAT_DATA_UINT32, 0 },
13957 { "forwDatagrams", KSTAT_DATA_UINT64, 0 },
13958 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 },
13959 { "inDiscards", KSTAT_DATA_UINT32, 0 },
13960 { "inDelivers", KSTAT_DATA_UINT64, 0 },
13961 { "outRequests", KSTAT_DATA_UINT64, 0 },
13962 { "outDiscards", KSTAT_DATA_UINT32, 0 },
13963 { "outNoRoutes", KSTAT_DATA_UINT32, 0 },
13964 { "reasmTimeout", KSTAT_DATA_UINT32, 0 },
13965 { "reasmReqds", KSTAT_DATA_UINT32, 0 },
13966 { "reasmOKs", KSTAT_DATA_UINT32, 0 },
13967 { "reasmFails", KSTAT_DATA_UINT32, 0 },
13968 { "fragOKs", KSTAT_DATA_UINT32, 0 },
13969 { "fragFails", KSTAT_DATA_UINT32, 0 },
13970 { "fragCreates", KSTAT_DATA_UINT32, 0 },
13971 { "addrEntrySize", KSTAT_DATA_INT32, 0 },
13972 { "routeEntrySize", KSTAT_DATA_INT32, 0 },
13973 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 },
13974 { "routingDiscards", KSTAT_DATA_UINT32, 0 },
13975 { "inErrs", KSTAT_DATA_UINT32, 0 },
13976 { "noPorts", KSTAT_DATA_UINT32, 0 },
13977 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
13978 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 },
13979 { "reasmPartDups", KSTAT_DATA_UINT32, 0 },
13980 { "forwProhibits", KSTAT_DATA_UINT32, 0 },
13981 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 },
13982 { "udpInOverflows", KSTAT_DATA_UINT32, 0 },
13983 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 },
13984 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 },
13985 { "ipsecInFailed", KSTAT_DATA_INT32, 0 },
13986 { "memberEntrySize", KSTAT_DATA_INT32, 0 },
13987 { "inIPv6", KSTAT_DATA_UINT32, 0 },
13988 { "outIPv6", KSTAT_DATA_UINT32, 0 },
13989 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 },
13990 };
13991
13992 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
13993 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
13994 if (ksp == NULL || ksp->ks_data == NULL)
13995 return (NULL);
13996
13997 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
13998 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
13999 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14000 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
14001 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
14002
14003 template.netToMediaEntrySize.value.i32 =
14004 sizeof (mib2_ipNetToMediaEntry_t);
14005
14006 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
14007
14008 bcopy(&template, ksp->ks_data, sizeof (template));
14009 ksp->ks_update = ip_kstat_update;
14010 ksp->ks_private = (void *)(uintptr_t)stackid;
14011
14012 kstat_install(ksp);
14013 return (ksp);
14014 }
14015
14016 static void
14017 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14018 {
14019 if (ksp != NULL) {
14020 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14021 kstat_delete_netstack(ksp, stackid);
14022 }
14023 }
14024
14025 static int
14026 ip_kstat_update(kstat_t *kp, int rw)
14027 {
14028 ip_named_kstat_t *ipkp;
14029 mib2_ipIfStatsEntry_t ipmib;
14030 ill_walk_context_t ctx;
14031 ill_t *ill;
14032 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14033 netstack_t *ns;
14034 ip_stack_t *ipst;
14035
14036 if (kp->ks_data == NULL)
14037 return (EIO);
14038
14039 if (rw == KSTAT_WRITE)
14040 return (EACCES);
14041
14042 ns = netstack_find_by_stackid(stackid);
14043 if (ns == NULL)
14044 return (-1);
14045 ipst = ns->netstack_ip;
14046 if (ipst == NULL) {
14047 netstack_rele(ns);
14048 return (-1);
14049 }
14050 ipkp = (ip_named_kstat_t *)kp->ks_data;
14051
14052 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
14053 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14054 ill = ILL_START_WALK_V4(&ctx, ipst);
14055 for (; ill != NULL; ill = ill_next(&ctx, ill))
14056 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
14057 rw_exit(&ipst->ips_ill_g_lock);
14058
14059 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding;
14060 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL;
14061 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives;
14062 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors;
14063 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors;
14064 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
14065 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos;
14066 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards;
14067 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers;
14068 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests;
14069 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards;
14070 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes;
14071 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14072 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds;
14073 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs;
14074 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails;
14075 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs;
14076 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails;
14077 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates;
14078
14079 ipkp->routingDiscards.value.ui32 = 0;
14080 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs;
14081 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts;
14082 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs;
14083 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates;
14084 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups;
14085 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits;
14086 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs;
14087 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows;
14088 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows;
14089 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded;
14090 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed;
14091
14092 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion;
14093 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion;
14094 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
14095
14096 netstack_rele(ns);
14097
14098 return (0);
14099 }
14100
14101 static void *
14102 icmp_kstat_init(netstackid_t stackid)
14103 {
14104 kstat_t *ksp;
14105
14106 icmp_named_kstat_t template = {
14107 { "inMsgs", KSTAT_DATA_UINT32 },
14108 { "inErrors", KSTAT_DATA_UINT32 },
14109 { "inDestUnreachs", KSTAT_DATA_UINT32 },
14110 { "inTimeExcds", KSTAT_DATA_UINT32 },
14111 { "inParmProbs", KSTAT_DATA_UINT32 },
14112 { "inSrcQuenchs", KSTAT_DATA_UINT32 },
14113 { "inRedirects", KSTAT_DATA_UINT32 },
14114 { "inEchos", KSTAT_DATA_UINT32 },
14115 { "inEchoReps", KSTAT_DATA_UINT32 },
14116 { "inTimestamps", KSTAT_DATA_UINT32 },
14117 { "inTimestampReps", KSTAT_DATA_UINT32 },
14118 { "inAddrMasks", KSTAT_DATA_UINT32 },
14119 { "inAddrMaskReps", KSTAT_DATA_UINT32 },
14120 { "outMsgs", KSTAT_DATA_UINT32 },
14121 { "outErrors", KSTAT_DATA_UINT32 },
14122 { "outDestUnreachs", KSTAT_DATA_UINT32 },
14123 { "outTimeExcds", KSTAT_DATA_UINT32 },
14124 { "outParmProbs", KSTAT_DATA_UINT32 },
14125 { "outSrcQuenchs", KSTAT_DATA_UINT32 },
14126 { "outRedirects", KSTAT_DATA_UINT32 },
14127 { "outEchos", KSTAT_DATA_UINT32 },
14128 { "outEchoReps", KSTAT_DATA_UINT32 },
14129 { "outTimestamps", KSTAT_DATA_UINT32 },
14130 { "outTimestampReps", KSTAT_DATA_UINT32 },
14131 { "outAddrMasks", KSTAT_DATA_UINT32 },
14132 { "outAddrMaskReps", KSTAT_DATA_UINT32 },
14133 { "inChksumErrs", KSTAT_DATA_UINT32 },
14134 { "inUnknowns", KSTAT_DATA_UINT32 },
14135 { "inFragNeeded", KSTAT_DATA_UINT32 },
14136 { "outFragNeeded", KSTAT_DATA_UINT32 },
14137 { "outDrops", KSTAT_DATA_UINT32 },
14138 { "inOverFlows", KSTAT_DATA_UINT32 },
14139 { "inBadRedirects", KSTAT_DATA_UINT32 },
14140 };
14141
14142 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
14143 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
14144 if (ksp == NULL || ksp->ks_data == NULL)
14145 return (NULL);
14146
14147 bcopy(&template, ksp->ks_data, sizeof (template));
14148
14149 ksp->ks_update = icmp_kstat_update;
14150 ksp->ks_private = (void *)(uintptr_t)stackid;
14151
14152 kstat_install(ksp);
14153 return (ksp);
14154 }
14155
14156 static void
14157 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14158 {
14159 if (ksp != NULL) {
14160 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14161 kstat_delete_netstack(ksp, stackid);
14162 }
14163 }
14164
14165 static int
14166 icmp_kstat_update(kstat_t *kp, int rw)
14167 {
14168 icmp_named_kstat_t *icmpkp;
14169 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14170 netstack_t *ns;
14171 ip_stack_t *ipst;
14172
14173 if (kp->ks_data == NULL)
14174 return (EIO);
14175
14176 if (rw == KSTAT_WRITE)
14177 return (EACCES);
14178
14179 ns = netstack_find_by_stackid(stackid);
14180 if (ns == NULL)
14181 return (-1);
14182 ipst = ns->netstack_ip;
14183 if (ipst == NULL) {
14184 netstack_rele(ns);
14185 return (-1);
14186 }
14187 icmpkp = (icmp_named_kstat_t *)kp->ks_data;
14188
14189 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs;
14190 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors;
14191 icmpkp->inDestUnreachs.value.ui32 =
14192 ipst->ips_icmp_mib.icmpInDestUnreachs;
14193 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds;
14194 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs;
14195 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs;
14196 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects;
14197 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos;
14198 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps;
14199 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps;
14200 icmpkp->inTimestampReps.value.ui32 =
14201 ipst->ips_icmp_mib.icmpInTimestampReps;
14202 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks;
14203 icmpkp->inAddrMaskReps.value.ui32 =
14204 ipst->ips_icmp_mib.icmpInAddrMaskReps;
14205 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs;
14206 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors;
14207 icmpkp->outDestUnreachs.value.ui32 =
14208 ipst->ips_icmp_mib.icmpOutDestUnreachs;
14209 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds;
14210 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs;
14211 icmpkp->outSrcQuenchs.value.ui32 =
14212 ipst->ips_icmp_mib.icmpOutSrcQuenchs;
14213 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects;
14214 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos;
14215 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps;
14216 icmpkp->outTimestamps.value.ui32 =
14217 ipst->ips_icmp_mib.icmpOutTimestamps;
14218 icmpkp->outTimestampReps.value.ui32 =
14219 ipst->ips_icmp_mib.icmpOutTimestampReps;
14220 icmpkp->outAddrMasks.value.ui32 =
14221 ipst->ips_icmp_mib.icmpOutAddrMasks;
14222 icmpkp->outAddrMaskReps.value.ui32 =
14223 ipst->ips_icmp_mib.icmpOutAddrMaskReps;
14224 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs;
14225 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns;
14226 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded;
14227 icmpkp->outFragNeeded.value.ui32 =
14228 ipst->ips_icmp_mib.icmpOutFragNeeded;
14229 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops;
14230 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows;
14231 icmpkp->inBadRedirects.value.ui32 =
14232 ipst->ips_icmp_mib.icmpInBadRedirects;
14233
14234 netstack_rele(ns);
14235 return (0);
14236 }
14237
14238 /*
14239 * This is the fanout function for raw socket opened for SCTP. Note
14240 * that it is called after SCTP checks that there is no socket which
14241 * wants a packet. Then before SCTP handles this out of the blue packet,
14242 * this function is called to see if there is any raw socket for SCTP.
14243 * If there is and it is bound to the correct address, the packet will
14244 * be sent to that socket. Note that only one raw socket can be bound to
14245 * a port. This is assured in ipcl_sctp_hash_insert();
14246 */
14247 void
14248 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
14249 ip_recv_attr_t *ira)
14250 {
14251 conn_t *connp;
14252 queue_t *rq;
14253 boolean_t secure;
14254 ill_t *ill = ira->ira_ill;
14255 ip_stack_t *ipst = ill->ill_ipst;
14256 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
14257 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp;
14258 iaflags_t iraflags = ira->ira_flags;
14259 ill_t *rill = ira->ira_rill;
14260
14261 secure = iraflags & IRAF_IPSEC_SECURE;
14262
14263 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
14264 ira, ipst);
14265 if (connp == NULL) {
14266 /*
14267 * Although raw sctp is not summed, OOB chunks must be.
14268 * Drop the packet here if the sctp checksum failed.
14269 */
14270 if (iraflags & IRAF_SCTP_CSUM_ERR) {
14271 SCTPS_BUMP_MIB(sctps, sctpChecksumError);
14272 freemsg(mp);
14273 return;
14274 }
14275 ira->ira_ill = ira->ira_rill = NULL;
14276 sctp_ootb_input(mp, ira, ipst);
14277 ira->ira_ill = ill;
14278 ira->ira_rill = rill;
14279 return;
14280 }
14281 rq = connp->conn_rq;
14282 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
14283 CONN_DEC_REF(connp);
14284 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
14285 freemsg(mp);
14286 return;
14287 }
14288 if (((iraflags & IRAF_IS_IPV4) ?
14289 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
14290 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
14291 secure) {
14292 mp = ipsec_check_inbound_policy(mp, connp, ipha,
14293 ip6h, ira);
14294 if (mp == NULL) {
14295 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
14296 /* Note that mp is NULL */
14297 ip_drop_input("ipIfStatsInDiscards", mp, ill);
14298 CONN_DEC_REF(connp);
14299 return;
14300 }
14301 }
14302
14303 if (iraflags & IRAF_ICMP_ERROR) {
14304 (connp->conn_recvicmp)(connp, mp, NULL, ira);
14305 } else {
14306 ill_t *rill = ira->ira_rill;
14307
14308 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
14309 /* This is the SOCK_RAW, IPPROTO_SCTP case. */
14310 ira->ira_ill = ira->ira_rill = NULL;
14311 (connp->conn_recv)(connp, mp, NULL, ira);
14312 ira->ira_ill = ill;
14313 ira->ira_rill = rill;
14314 }
14315 CONN_DEC_REF(connp);
14316 }
14317
14318 /*
14319 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
14320 * header before the ip payload.
14321 */
14322 static void
14323 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
14324 {
14325 int len = (mp->b_wptr - mp->b_rptr);
14326 mblk_t *ip_mp;
14327
14328 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14329 if (is_fp_mp || len != fp_mp_len) {
14330 if (len > fp_mp_len) {
14331 /*
14332 * fastpath header and ip header in the first mblk
14333 */
14334 mp->b_rptr += fp_mp_len;
14335 } else {
14336 /*
14337 * ip_xmit_attach_llhdr had to prepend an mblk to
14338 * attach the fastpath header before ip header.
14339 */
14340 ip_mp = mp->b_cont;
14341 freeb(mp);
14342 mp = ip_mp;
14343 mp->b_rptr += (fp_mp_len - len);
14344 }
14345 } else {
14346 ip_mp = mp->b_cont;
14347 freeb(mp);
14348 mp = ip_mp;
14349 }
14350 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
14351 freemsg(mp);
14352 }
14353
14354 /*
14355 * Normal post fragmentation function.
14356 *
14357 * Send a packet using the passed in nce. This handles both IPv4 and IPv6
14358 * using the same state machine.
14359 *
14360 * We return an error on failure. In particular we return EWOULDBLOCK
14361 * when the driver flow controls. In that case this ensures that ip_wsrv runs
14362 * (currently by canputnext failure resulting in backenabling from GLD.)
14363 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
14364 * indication that they can flow control until ip_wsrv() tells then to restart.
14365 *
14366 * If the nce passed by caller is incomplete, this function
14367 * queues the packet and if necessary, sends ARP request and bails.
14368 * If the Neighbor Cache passed is fully resolved, we simply prepend
14369 * the link-layer header to the packet, do ipsec hw acceleration
14370 * work if necessary, and send the packet out on the wire.
14371 */
14372 /* ARGSUSED6 */
14373 int
14374 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
14375 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
14376 {
14377 queue_t *wq;
14378 ill_t *ill = nce->nce_ill;
14379 ip_stack_t *ipst = ill->ill_ipst;
14380 uint64_t delta;
14381 boolean_t isv6 = ill->ill_isv6;
14382 boolean_t fp_mp;
14383 ncec_t *ncec = nce->nce_common;
14384 int64_t now = LBOLT_FASTPATH64;
14385 boolean_t is_probe;
14386
14387 DTRACE_PROBE1(ip__xmit, nce_t *, nce);
14388
14389 ASSERT(mp != NULL);
14390 ASSERT(mp->b_datap->db_type == M_DATA);
14391 ASSERT(pkt_len == msgdsize(mp));
14392
14393 /*
14394 * If we have already been here and are coming back after ARP/ND.
14395 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
14396 * in that case since they have seen the packet when it came here
14397 * the first time.
14398 */
14399 if (ixaflags & IXAF_NO_TRACE)
14400 goto sendit;
14401
14402 if (ixaflags & IXAF_IS_IPV4) {
14403 ipha_t *ipha = (ipha_t *)mp->b_rptr;
14404
14405 ASSERT(!isv6);
14406 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
14407 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
14408 !(ixaflags & IXAF_NO_PFHOOK)) {
14409 int error;
14410
14411 FW_HOOKS(ipst->ips_ip4_physical_out_event,
14412 ipst->ips_ipv4firewall_physical_out,
14413 NULL, ill, ipha, mp, mp, 0, ipst, error);
14414 DTRACE_PROBE1(ip4__physical__out__end,
14415 mblk_t *, mp);
14416 if (mp == NULL)
14417 return (error);
14418
14419 /* The length could have changed */
14420 pkt_len = msgdsize(mp);
14421 }
14422 if (ipst->ips_ip4_observe.he_interested) {
14423 /*
14424 * Note that for TX the zoneid is the sending
14425 * zone, whether or not MLP is in play.
14426 * Since the szone argument is the IP zoneid (i.e.,
14427 * zero for exclusive-IP zones) and ipobs wants
14428 * the system zoneid, we map it here.
14429 */
14430 szone = IP_REAL_ZONEID(szone, ipst);
14431
14432 /*
14433 * On the outbound path the destination zone will be
14434 * unknown as we're sending this packet out on the
14435 * wire.
14436 */
14437 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14438 ill, ipst);
14439 }
14440 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14441 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill,
14442 ipha_t *, ipha, ip6_t *, NULL, int, 0);
14443 } else {
14444 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
14445
14446 ASSERT(isv6);
14447 ASSERT(pkt_len ==
14448 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
14449 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
14450 !(ixaflags & IXAF_NO_PFHOOK)) {
14451 int error;
14452
14453 FW_HOOKS6(ipst->ips_ip6_physical_out_event,
14454 ipst->ips_ipv6firewall_physical_out,
14455 NULL, ill, ip6h, mp, mp, 0, ipst, error);
14456 DTRACE_PROBE1(ip6__physical__out__end,
14457 mblk_t *, mp);
14458 if (mp == NULL)
14459 return (error);
14460
14461 /* The length could have changed */
14462 pkt_len = msgdsize(mp);
14463 }
14464 if (ipst->ips_ip6_observe.he_interested) {
14465 /* See above */
14466 szone = IP_REAL_ZONEID(szone, ipst);
14467
14468 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14469 ill, ipst);
14470 }
14471 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14472 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill,
14473 ipha_t *, NULL, ip6_t *, ip6h, int, 0);
14474 }
14475
14476 sendit:
14477 /*
14478 * We check the state without a lock because the state can never
14479 * move "backwards" to initial or incomplete.
14480 */
14481 switch (ncec->ncec_state) {
14482 case ND_REACHABLE:
14483 case ND_STALE:
14484 case ND_DELAY:
14485 case ND_PROBE:
14486 mp = ip_xmit_attach_llhdr(mp, nce);
14487 if (mp == NULL) {
14488 /*
14489 * ip_xmit_attach_llhdr has increased
14490 * ipIfStatsOutDiscards and called ip_drop_output()
14491 */
14492 return (ENOBUFS);
14493 }
14494 /*
14495 * check if nce_fastpath completed and we tagged on a
14496 * copy of nce_fp_mp in ip_xmit_attach_llhdr().
14497 */
14498 fp_mp = (mp->b_datap->db_type == M_DATA);
14499
14500 if (fp_mp &&
14501 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
14502 ill_dld_direct_t *idd;
14503
14504 idd = &ill->ill_dld_capab->idc_direct;
14505 /*
14506 * Send the packet directly to DLD, where it
14507 * may be queued depending on the availability
14508 * of transmit resources at the media layer.
14509 * Return value should be taken into
14510 * account and flow control the TCP.
14511 */
14512 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14513 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14514 pkt_len);
14515
14516 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
14517 (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
14518 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
14519 } else {
14520 uintptr_t cookie;
14521
14522 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
14523 mp, (uintptr_t)xmit_hint, 0)) != 0) {
14524 if (ixacookie != NULL)
14525 *ixacookie = cookie;
14526 return (EWOULDBLOCK);
14527 }
14528 }
14529 } else {
14530 wq = ill->ill_wq;
14531
14532 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
14533 !canputnext(wq)) {
14534 if (ixacookie != NULL)
14535 *ixacookie = 0;
14536 ip_xmit_flowctl_drop(ill, mp, fp_mp,
14537 nce->nce_fp_mp != NULL ?
14538 MBLKL(nce->nce_fp_mp) : 0);
14539 return (EWOULDBLOCK);
14540 }
14541 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14542 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14543 pkt_len);
14544 putnext(wq, mp);
14545 }
14546
14547 /*
14548 * The rest of this function implements Neighbor Unreachability
14549 * detection. Determine if the ncec is eligible for NUD.
14550 */
14551 if (ncec->ncec_flags & NCE_F_NONUD)
14552 return (0);
14553
14554 ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14555
14556 /*
14557 * Check for upper layer advice
14558 */
14559 if (ixaflags & IXAF_REACH_CONF) {
14560 timeout_id_t tid;
14561
14562 /*
14563 * It should be o.k. to check the state without
14564 * a lock here, at most we lose an advice.
14565 */
14566 ncec->ncec_last = TICK_TO_MSEC(now);
14567 if (ncec->ncec_state != ND_REACHABLE) {
14568 mutex_enter(&ncec->ncec_lock);
14569 ncec->ncec_state = ND_REACHABLE;
14570 tid = ncec->ncec_timeout_id;
14571 ncec->ncec_timeout_id = 0;
14572 mutex_exit(&ncec->ncec_lock);
14573 (void) untimeout(tid);
14574 if (ip_debug > 2) {
14575 /* ip1dbg */
14576 pr_addr_dbg("ip_xmit: state"
14577 " for %s changed to"
14578 " REACHABLE\n", AF_INET6,
14579 &ncec->ncec_addr);
14580 }
14581 }
14582 return (0);
14583 }
14584
14585 delta = TICK_TO_MSEC(now) - ncec->ncec_last;
14586 ip1dbg(("ip_xmit: delta = %" PRId64
14587 " ill_reachable_time = %d \n", delta,
14588 ill->ill_reachable_time));
14589 if (delta > (uint64_t)ill->ill_reachable_time) {
14590 mutex_enter(&ncec->ncec_lock);
14591 switch (ncec->ncec_state) {
14592 case ND_REACHABLE:
14593 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14594 /* FALLTHROUGH */
14595 case ND_STALE:
14596 /*
14597 * ND_REACHABLE is identical to
14598 * ND_STALE in this specific case. If
14599 * reachable time has expired for this
14600 * neighbor (delta is greater than
14601 * reachable time), conceptually, the
14602 * neighbor cache is no longer in
14603 * REACHABLE state, but already in
14604 * STALE state. So the correct
14605 * transition here is to ND_DELAY.
14606 */
14607 ncec->ncec_state = ND_DELAY;
14608 mutex_exit(&ncec->ncec_lock);
14609 nce_restart_timer(ncec,
14610 ipst->ips_delay_first_probe_time);
14611 if (ip_debug > 3) {
14612 /* ip2dbg */
14613 pr_addr_dbg("ip_xmit: state"
14614 " for %s changed to"
14615 " DELAY\n", AF_INET6,
14616 &ncec->ncec_addr);
14617 }
14618 break;
14619 case ND_DELAY:
14620 case ND_PROBE:
14621 mutex_exit(&ncec->ncec_lock);
14622 /* Timers have already started */
14623 break;
14624 case ND_UNREACHABLE:
14625 /*
14626 * nce_timer has detected that this ncec
14627 * is unreachable and initiated deleting
14628 * this ncec.
14629 * This is a harmless race where we found the
14630 * ncec before it was deleted and have
14631 * just sent out a packet using this
14632 * unreachable ncec.
14633 */
14634 mutex_exit(&ncec->ncec_lock);
14635 break;
14636 default:
14637 ASSERT(0);
14638 mutex_exit(&ncec->ncec_lock);
14639 }
14640 }
14641 return (0);
14642
14643 case ND_INCOMPLETE:
14644 /*
14645 * the state could have changed since we didn't hold the lock.
14646 * Re-verify state under lock.
14647 */
14648 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14649 mutex_enter(&ncec->ncec_lock);
14650 if (NCE_ISREACHABLE(ncec)) {
14651 mutex_exit(&ncec->ncec_lock);
14652 goto sendit;
14653 }
14654 /* queue the packet */
14655 nce_queue_mp(ncec, mp, is_probe);
14656 mutex_exit(&ncec->ncec_lock);
14657 DTRACE_PROBE2(ip__xmit__incomplete,
14658 (ncec_t *), ncec, (mblk_t *), mp);
14659 return (0);
14660
14661 case ND_INITIAL:
14662 /*
14663 * State could have changed since we didn't hold the lock, so
14664 * re-verify state.
14665 */
14666 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14667 mutex_enter(&ncec->ncec_lock);
14668 if (NCE_ISREACHABLE(ncec)) {
14669 mutex_exit(&ncec->ncec_lock);
14670 goto sendit;
14671 }
14672 nce_queue_mp(ncec, mp, is_probe);
14673 if (ncec->ncec_state == ND_INITIAL) {
14674 ncec->ncec_state = ND_INCOMPLETE;
14675 mutex_exit(&ncec->ncec_lock);
14676 /*
14677 * figure out the source we want to use
14678 * and resolve it.
14679 */
14680 ip_ndp_resolve(ncec);
14681 } else {
14682 mutex_exit(&ncec->ncec_lock);
14683 }
14684 return (0);
14685
14686 case ND_UNREACHABLE:
14687 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14688 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14689 mp, ill);
14690 freemsg(mp);
14691 return (0);
14692
14693 default:
14694 ASSERT(0);
14695 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14696 ip_drop_output("ipIfStatsOutDiscards - ND_other",
14697 mp, ill);
14698 freemsg(mp);
14699 return (ENETUNREACH);
14700 }
14701 }
14702
14703 /*
14704 * Return B_TRUE if the buffers differ in length or content.
14705 * This is used for comparing extension header buffers.
14706 * Note that an extension header would be declared different
14707 * even if all that changed was the next header value in that header i.e.
14708 * what really changed is the next extension header.
14709 */
14710 boolean_t
14711 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14712 uint_t blen)
14713 {
14714 if (!b_valid)
14715 blen = 0;
14716
14717 if (alen != blen)
14718 return (B_TRUE);
14719 if (alen == 0)
14720 return (B_FALSE); /* Both zero length */
14721 return (bcmp(abuf, bbuf, alen));
14722 }
14723
14724 /*
14725 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14726 * Return B_FALSE if memory allocation fails - don't change any state!
14727 */
14728 boolean_t
14729 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14730 const void *src, uint_t srclen)
14731 {
14732 void *dst;
14733
14734 if (!src_valid)
14735 srclen = 0;
14736
14737 ASSERT(*dstlenp == 0);
14738 if (src != NULL && srclen != 0) {
14739 dst = mi_alloc(srclen, BPRI_MED);
14740 if (dst == NULL)
14741 return (B_FALSE);
14742 } else {
14743 dst = NULL;
14744 }
14745 if (*dstp != NULL)
14746 mi_free(*dstp);
14747 *dstp = dst;
14748 *dstlenp = dst == NULL ? 0 : srclen;
14749 return (B_TRUE);
14750 }
14751
14752 /*
14753 * Replace what is in *dst, *dstlen with the source.
14754 * Assumes ip_allocbuf has already been called.
14755 */
14756 void
14757 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14758 const void *src, uint_t srclen)
14759 {
14760 if (!src_valid)
14761 srclen = 0;
14762
14763 ASSERT(*dstlenp == srclen);
14764 if (src != NULL && srclen != 0)
14765 bcopy(src, *dstp, srclen);
14766 }
14767
14768 /*
14769 * Free the storage pointed to by the members of an ip_pkt_t.
14770 */
14771 void
14772 ip_pkt_free(ip_pkt_t *ipp)
14773 {
14774 uint_t fields = ipp->ipp_fields;
14775
14776 if (fields & IPPF_HOPOPTS) {
14777 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14778 ipp->ipp_hopopts = NULL;
14779 ipp->ipp_hopoptslen = 0;
14780 }
14781 if (fields & IPPF_RTHDRDSTOPTS) {
14782 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14783 ipp->ipp_rthdrdstopts = NULL;
14784 ipp->ipp_rthdrdstoptslen = 0;
14785 }
14786 if (fields & IPPF_DSTOPTS) {
14787 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14788 ipp->ipp_dstopts = NULL;
14789 ipp->ipp_dstoptslen = 0;
14790 }
14791 if (fields & IPPF_RTHDR) {
14792 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14793 ipp->ipp_rthdr = NULL;
14794 ipp->ipp_rthdrlen = 0;
14795 }
14796 if (fields & IPPF_IPV4_OPTIONS) {
14797 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14798 ipp->ipp_ipv4_options = NULL;
14799 ipp->ipp_ipv4_options_len = 0;
14800 }
14801 if (fields & IPPF_LABEL_V4) {
14802 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
14803 ipp->ipp_label_v4 = NULL;
14804 ipp->ipp_label_len_v4 = 0;
14805 }
14806 if (fields & IPPF_LABEL_V6) {
14807 kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6);
14808 ipp->ipp_label_v6 = NULL;
14809 ipp->ipp_label_len_v6 = 0;
14810 }
14811 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14812 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14813 }
14814
14815 /*
14816 * Copy from src to dst and allocate as needed.
14817 * Returns zero or ENOMEM.
14818 *
14819 * The caller must initialize dst to zero.
14820 */
14821 int
14822 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14823 {
14824 uint_t fields = src->ipp_fields;
14825
14826 /* Start with fields that don't require memory allocation */
14827 dst->ipp_fields = fields &
14828 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14829 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14830
14831 dst->ipp_addr = src->ipp_addr;
14832 dst->ipp_unicast_hops = src->ipp_unicast_hops;
14833 dst->ipp_hoplimit = src->ipp_hoplimit;
14834 dst->ipp_tclass = src->ipp_tclass;
14835 dst->ipp_type_of_service = src->ipp_type_of_service;
14836
14837 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14838 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6)))
14839 return (0);
14840
14841 if (fields & IPPF_HOPOPTS) {
14842 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14843 if (dst->ipp_hopopts == NULL) {
14844 ip_pkt_free(dst);
14845 return (ENOMEM);
14846 }
14847 dst->ipp_fields |= IPPF_HOPOPTS;
14848 bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14849 src->ipp_hopoptslen);
14850 dst->ipp_hopoptslen = src->ipp_hopoptslen;
14851 }
14852 if (fields & IPPF_RTHDRDSTOPTS) {
14853 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14854 kmflag);
14855 if (dst->ipp_rthdrdstopts == NULL) {
14856 ip_pkt_free(dst);
14857 return (ENOMEM);
14858 }
14859 dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14860 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14861 src->ipp_rthdrdstoptslen);
14862 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14863 }
14864 if (fields & IPPF_DSTOPTS) {
14865 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14866 if (dst->ipp_dstopts == NULL) {
14867 ip_pkt_free(dst);
14868 return (ENOMEM);
14869 }
14870 dst->ipp_fields |= IPPF_DSTOPTS;
14871 bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14872 src->ipp_dstoptslen);
14873 dst->ipp_dstoptslen = src->ipp_dstoptslen;
14874 }
14875 if (fields & IPPF_RTHDR) {
14876 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14877 if (dst->ipp_rthdr == NULL) {
14878 ip_pkt_free(dst);
14879 return (ENOMEM);
14880 }
14881 dst->ipp_fields |= IPPF_RTHDR;
14882 bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14883 src->ipp_rthdrlen);
14884 dst->ipp_rthdrlen = src->ipp_rthdrlen;
14885 }
14886 if (fields & IPPF_IPV4_OPTIONS) {
14887 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14888 kmflag);
14889 if (dst->ipp_ipv4_options == NULL) {
14890 ip_pkt_free(dst);
14891 return (ENOMEM);
14892 }
14893 dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14894 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14895 src->ipp_ipv4_options_len);
14896 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14897 }
14898 if (fields & IPPF_LABEL_V4) {
14899 dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag);
14900 if (dst->ipp_label_v4 == NULL) {
14901 ip_pkt_free(dst);
14902 return (ENOMEM);
14903 }
14904 dst->ipp_fields |= IPPF_LABEL_V4;
14905 bcopy(src->ipp_label_v4, dst->ipp_label_v4,
14906 src->ipp_label_len_v4);
14907 dst->ipp_label_len_v4 = src->ipp_label_len_v4;
14908 }
14909 if (fields & IPPF_LABEL_V6) {
14910 dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag);
14911 if (dst->ipp_label_v6 == NULL) {
14912 ip_pkt_free(dst);
14913 return (ENOMEM);
14914 }
14915 dst->ipp_fields |= IPPF_LABEL_V6;
14916 bcopy(src->ipp_label_v6, dst->ipp_label_v6,
14917 src->ipp_label_len_v6);
14918 dst->ipp_label_len_v6 = src->ipp_label_len_v6;
14919 }
14920 if (fields & IPPF_FRAGHDR) {
14921 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14922 if (dst->ipp_fraghdr == NULL) {
14923 ip_pkt_free(dst);
14924 return (ENOMEM);
14925 }
14926 dst->ipp_fields |= IPPF_FRAGHDR;
14927 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14928 src->ipp_fraghdrlen);
14929 dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14930 }
14931 return (0);
14932 }
14933
14934 /*
14935 * Returns INADDR_ANY if no source route
14936 */
14937 ipaddr_t
14938 ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14939 {
14940 ipaddr_t nexthop = INADDR_ANY;
14941 ipoptp_t opts;
14942 uchar_t *opt;
14943 uint8_t optval;
14944 uint8_t optlen;
14945 uint32_t totallen;
14946
14947 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14948 return (INADDR_ANY);
14949
14950 totallen = ipp->ipp_ipv4_options_len;
14951 if (totallen & 0x3)
14952 return (INADDR_ANY);
14953
14954 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14955 optval != IPOPT_EOL;
14956 optval = ipoptp_next(&opts)) {
14957 opt = opts.ipoptp_cur;
14958 switch (optval) {
14959 uint8_t off;
14960 case IPOPT_SSRR:
14961 case IPOPT_LSRR:
14962 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
14963 break;
14964 }
14965 optlen = opts.ipoptp_len;
14966 off = opt[IPOPT_OFFSET];
14967 off--;
14968 if (optlen < IP_ADDR_LEN ||
14969 off > optlen - IP_ADDR_LEN) {
14970 /* End of source route */
14971 break;
14972 }
14973 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
14974 if (nexthop == htonl(INADDR_LOOPBACK)) {
14975 /* Ignore */
14976 nexthop = INADDR_ANY;
14977 break;
14978 }
14979 break;
14980 }
14981 }
14982 return (nexthop);
14983 }
14984
14985 /*
14986 * Reverse a source route.
14987 */
14988 void
14989 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
14990 {
14991 ipaddr_t tmp;
14992 ipoptp_t opts;
14993 uchar_t *opt;
14994 uint8_t optval;
14995 uint32_t totallen;
14996
14997 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14998 return;
14999
15000 totallen = ipp->ipp_ipv4_options_len;
15001 if (totallen & 0x3)
15002 return;
15003
15004 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15005 optval != IPOPT_EOL;
15006 optval = ipoptp_next(&opts)) {
15007 uint8_t off1, off2;
15008
15009 opt = opts.ipoptp_cur;
15010 switch (optval) {
15011 case IPOPT_SSRR:
15012 case IPOPT_LSRR:
15013 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15014 break;
15015 }
15016 off1 = IPOPT_MINOFF_SR - 1;
15017 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
15018 while (off2 > off1) {
15019 bcopy(opt + off2, &tmp, IP_ADDR_LEN);
15020 bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
15021 bcopy(&tmp, opt + off2, IP_ADDR_LEN);
15022 off2 -= IP_ADDR_LEN;
15023 off1 += IP_ADDR_LEN;
15024 }
15025 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
15026 break;
15027 }
15028 }
15029 }
15030
15031 /*
15032 * Returns NULL if no routing header
15033 */
15034 in6_addr_t *
15035 ip_pkt_source_route_v6(const ip_pkt_t *ipp)
15036 {
15037 in6_addr_t *nexthop = NULL;
15038 ip6_rthdr0_t *rthdr;
15039
15040 if (!(ipp->ipp_fields & IPPF_RTHDR))
15041 return (NULL);
15042
15043 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
15044 if (rthdr->ip6r0_segleft == 0)
15045 return (NULL);
15046
15047 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
15048 return (nexthop);
15049 }
15050
15051 zoneid_t
15052 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
15053 zoneid_t lookup_zoneid)
15054 {
15055 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15056 ire_t *ire;
15057 int ire_flags = MATCH_IRE_TYPE;
15058 zoneid_t zoneid = ALL_ZONES;
15059
15060 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15061 return (ALL_ZONES);
15062
15063 if (lookup_zoneid != ALL_ZONES)
15064 ire_flags |= MATCH_IRE_ZONEONLY;
15065 ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_LOCAL | IRE_LOOPBACK,
15066 NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15067 if (ire != NULL) {
15068 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15069 ire_refrele(ire);
15070 }
15071 return (zoneid);
15072 }
15073
15074 zoneid_t
15075 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
15076 ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
15077 {
15078 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15079 ire_t *ire;
15080 int ire_flags = MATCH_IRE_TYPE;
15081 zoneid_t zoneid = ALL_ZONES;
15082
15083 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15084 return (ALL_ZONES);
15085
15086 if (IN6_IS_ADDR_LINKLOCAL(addr))
15087 ire_flags |= MATCH_IRE_ILL;
15088
15089 if (lookup_zoneid != ALL_ZONES)
15090 ire_flags |= MATCH_IRE_ZONEONLY;
15091 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15092 ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15093 if (ire != NULL) {
15094 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15095 ire_refrele(ire);
15096 }
15097 return (zoneid);
15098 }
15099
15100 /*
15101 * IP obserability hook support functions.
15102 */
15103 static void
15104 ipobs_init(ip_stack_t *ipst)
15105 {
15106 netid_t id;
15107
15108 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
15109
15110 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
15111 VERIFY(ipst->ips_ip4_observe_pr != NULL);
15112
15113 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
15114 VERIFY(ipst->ips_ip6_observe_pr != NULL);
15115 }
15116
15117 static void
15118 ipobs_fini(ip_stack_t *ipst)
15119 {
15120
15121 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
15122 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
15123 }
15124
15125 /*
15126 * hook_pkt_observe_t is composed in network byte order so that the
15127 * entire mblk_t chain handed into hook_run can be used as-is.
15128 * The caveat is that use of the fields, such as the zone fields,
15129 * requires conversion into host byte order first.
15130 */
15131 void
15132 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
15133 const ill_t *ill, ip_stack_t *ipst)
15134 {
15135 hook_pkt_observe_t *hdr;
15136 uint64_t grifindex;
15137 mblk_t *imp;
15138
15139 imp = allocb(sizeof (*hdr), BPRI_HI);
15140 if (imp == NULL)
15141 return;
15142
15143 hdr = (hook_pkt_observe_t *)imp->b_rptr;
15144 /*
15145 * b_wptr is set to make the apparent size of the data in the mblk_t
15146 * to exclude the pointers at the end of hook_pkt_observer_t.
15147 */
15148 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
15149 imp->b_cont = mp;
15150
15151 ASSERT(DB_TYPE(mp) == M_DATA);
15152
15153 if (IS_UNDER_IPMP(ill))
15154 grifindex = ipmp_ill_get_ipmp_ifindex(ill);
15155 else
15156 grifindex = 0;
15157
15158 hdr->hpo_version = 1;
15159 hdr->hpo_htype = htons(htype);
15160 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
15161 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
15162 hdr->hpo_grifindex = htonl(grifindex);
15163 hdr->hpo_zsrc = htonl(zsrc);
15164 hdr->hpo_zdst = htonl(zdst);
15165 hdr->hpo_pkt = imp;
15166 hdr->hpo_ctx = ipst->ips_netstack;
15167
15168 if (ill->ill_isv6) {
15169 hdr->hpo_family = AF_INET6;
15170 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
15171 ipst->ips_ipv6observing, (hook_data_t)hdr);
15172 } else {
15173 hdr->hpo_family = AF_INET;
15174 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
15175 ipst->ips_ipv4observing, (hook_data_t)hdr);
15176 }
15177
15178 imp->b_cont = NULL;
15179 freemsg(imp);
15180 }
15181
15182 /*
15183 * Utility routine that checks if `v4srcp' is a valid address on underlying
15184 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif
15185 * associated with `v4srcp' on success. NOTE: if this is not called from
15186 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
15187 * group during or after this lookup.
15188 */
15189 boolean_t
15190 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
15191 {
15192 ipif_t *ipif;
15193
15194 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
15195 if (ipif != NULL) {
15196 if (ipifp != NULL)
15197 *ipifp = ipif;
15198 else
15199 ipif_refrele(ipif);
15200 return (B_TRUE);
15201 }
15202
15203 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
15204 *v4srcp));
15205 return (B_FALSE);
15206 }
15207
15208 /*
15209 * Transport protocol call back function for CPU state change.
15210 */
15211 /* ARGSUSED */
15212 static int
15213 ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
15214 {
15215 processorid_t cpu_seqid;
15216 netstack_handle_t nh;
15217 netstack_t *ns;
15218
15219 ASSERT(MUTEX_HELD(&cpu_lock));
15220
15221 switch (what) {
15222 case CPU_CONFIG:
15223 case CPU_ON:
15224 case CPU_INIT:
15225 case CPU_CPUPART_IN:
15226 cpu_seqid = cpu[id]->cpu_seqid;
15227 netstack_next_init(&nh);
15228 while ((ns = netstack_next(&nh)) != NULL) {
15229 tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
15230 sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
15231 udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
15232 netstack_rele(ns);
15233 }
15234 netstack_next_fini(&nh);
15235 break;
15236 case CPU_UNCONFIG:
15237 case CPU_OFF:
15238 case CPU_CPUPART_OUT:
15239 /*
15240 * Nothing to do. We don't remove the per CPU stats from
15241 * the IP stack even when the CPU goes offline.
15242 */
15243 break;
15244 default:
15245 break;
15246 }
15247 return (0);
15248 }