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 2020 OmniOS Community Edition (OmniOSce) Association.
28 * Copyright 2021 Joyent, Inc.
29 * Copyright 2022 Oxide Computer Company
30 */
31
32 #include <sys/types.h>
33 #include <sys/stream.h>
34 #include <sys/dlpi.h>
35 #include <sys/stropts.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strsubr.h>
38 #include <sys/strlog.h>
39 #include <sys/strsun.h>
40 #include <sys/zone.h>
41 #define _SUN_TPI_VERSION 2
42 #include <sys/tihdr.h>
43 #include <sys/xti_inet.h>
44 #include <sys/ddi.h>
45 #include <sys/suntpi.h>
46 #include <sys/cmn_err.h>
47 #include <sys/debug.h>
48 #include <sys/kobj.h>
49 #include <sys/modctl.h>
50 #include <sys/atomic.h>
51 #include <sys/policy.h>
52 #include <sys/priv.h>
53 #include <sys/taskq.h>
54
55 #include <sys/systm.h>
56 #include <sys/param.h>
57 #include <sys/kmem.h>
58 #include <sys/sdt.h>
59 #include <sys/socket.h>
60 #include <sys/vtrace.h>
61 #include <sys/isa_defs.h>
62 #include <sys/mac.h>
63 #include <net/if.h>
64 #include <net/if_arp.h>
65 #include <net/route.h>
66 #include <sys/sockio.h>
67 #include <netinet/in.h>
68 #include <net/if_dl.h>
69
70 #include <inet/common.h>
71 #include <inet/mi.h>
72 #include <inet/mib2.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 #include <inet/snmpcom.h>
76 #include <inet/optcom.h>
77 #include <inet/kstatcom.h>
78
79 #include <netinet/igmp_var.h>
80 #include <netinet/ip6.h>
81 #include <netinet/icmp6.h>
82 #include <netinet/sctp.h>
83
84 #include <inet/ip.h>
85 #include <inet/ip_impl.h>
86 #include <inet/ip6.h>
87 #include <inet/ip6_asp.h>
88 #include <inet/tcp.h>
89 #include <inet/tcp_impl.h>
90 #include <inet/ip_multi.h>
91 #include <inet/ip_if.h>
92 #include <inet/ip_ire.h>
93 #include <inet/ip_ftable.h>
94 #include <inet/ip_rts.h>
95 #include <inet/ip_ndp.h>
96 #include <inet/ip_listutils.h>
97 #include <netinet/igmp.h>
98 #include <netinet/ip_mroute.h>
99 #include <inet/ipp_common.h>
100 #include <inet/cc.h>
101
102 #include <net/pfkeyv2.h>
103 #include <inet/sadb.h>
104 #include <inet/ipsec_impl.h>
105 #include <inet/iptun/iptun_impl.h>
106 #include <inet/ipdrop.h>
107 #include <inet/ip_netinfo.h>
108 #include <inet/ilb_ip.h>
109
110 #include <sys/ethernet.h>
111 #include <net/if_types.h>
112 #include <sys/cpuvar.h>
113
114 #include <ipp/ipp.h>
115 #include <ipp/ipp_impl.h>
116 #include <ipp/ipgpc/ipgpc.h>
117
118 #include <sys/pattr.h>
119 #include <inet/ipclassifier.h>
120 #include <inet/sctp_ip.h>
121 #include <inet/sctp/sctp_impl.h>
122 #include <inet/udp_impl.h>
123 #include <inet/rawip_impl.h>
124 #include <inet/rts_impl.h>
125
126 #include <sys/tsol/label.h>
127 #include <sys/tsol/tnet.h>
128
129 #include <sys/squeue_impl.h>
130 #include <inet/ip_arp.h>
131
132 #include <sys/clock_impl.h> /* For LBOLT_FASTPATH{,64} */
133
134 /*
135 * Values for squeue switch:
136 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
137 * IP_SQUEUE_ENTER: SQ_PROCESS
138 * IP_SQUEUE_FILL: SQ_FILL
139 */
140 int ip_squeue_enter = IP_SQUEUE_ENTER; /* Setable in /etc/system */
141
142 int ip_squeue_flag;
143
144 /*
145 * Setable in /etc/system
146 */
147 int ip_poll_normal_ms = 100;
148 int ip_poll_normal_ticks = 0;
149 int ip_modclose_ackwait_ms = 3000;
150
151 /*
152 * It would be nice to have these present only in DEBUG systems, but the
153 * current design of the global symbol checking logic requires them to be
154 * unconditionally present.
155 */
156 uint_t ip_thread_data; /* TSD key for debug support */
157 krwlock_t ip_thread_rwlock;
158 list_t ip_thread_list;
159
160 /*
161 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
162 */
163
164 struct listptr_s {
165 mblk_t *lp_head; /* pointer to the head of the list */
166 mblk_t *lp_tail; /* pointer to the tail of the list */
167 };
168
169 typedef struct listptr_s listptr_t;
170
171 /*
172 * This is used by ip_snmp_get_mib2_ip_route_media and
173 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
174 */
175 typedef struct iproutedata_s {
176 uint_t ird_idx;
177 uint_t ird_flags; /* see below */
178 listptr_t ird_route; /* ipRouteEntryTable */
179 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
180 listptr_t ird_attrs; /* ipRouteAttributeTable */
181 } iproutedata_t;
182
183 /* Include ire_testhidden and IRE_IF_CLONE routes */
184 #define IRD_REPORT_ALL 0x01
185
186 /*
187 * Cluster specific hooks. These should be NULL when booted as a non-cluster
188 */
189
190 /*
191 * Hook functions to enable cluster networking
192 * On non-clustered systems these vectors must always be NULL.
193 *
194 * Hook function to Check ip specified ip address is a shared ip address
195 * in the cluster
196 *
197 */
198 int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol,
199 sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL;
200
201 /*
202 * Hook function to generate cluster wide ip fragment identifier
203 */
204 uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol,
205 sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp,
206 void *args) = NULL;
207
208 /*
209 * Hook function to generate cluster wide SPI.
210 */
211 void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t,
212 void *) = NULL;
213
214 /*
215 * Hook function to verify if the SPI is already utlized.
216 */
217
218 int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
219
220 /*
221 * Hook function to delete the SPI from the cluster wide repository.
222 */
223
224 void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
225
226 /*
227 * Hook function to inform the cluster when packet received on an IDLE SA
228 */
229
230 void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t,
231 in6_addr_t, in6_addr_t, void *) = NULL;
232
233 /*
234 * Synchronization notes:
235 *
236 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
237 * MT level protection given by STREAMS. IP uses a combination of its own
238 * internal serialization mechanism and standard Solaris locking techniques.
239 * The internal serialization is per phyint. This is used to serialize
240 * plumbing operations, IPMP operations, most set ioctls, etc.
241 *
242 * Plumbing is a long sequence of operations involving message
243 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
244 * involved in plumbing operations. A natural model is to serialize these
245 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
246 * parallel without any interference. But various set ioctls on hme0 are best
247 * serialized, along with IPMP operations and processing of DLPI control
248 * messages received from drivers on a per phyint basis. This serialization is
249 * provided by the ipsq_t and primitives operating on this. Details can
250 * be found in ip_if.c above the core primitives operating on ipsq_t.
251 *
252 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
253 * Simiarly lookup of an ire by a thread also returns a refheld ire.
254 * In addition ipif's and ill's referenced by the ire are also indirectly
255 * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
256 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
257 * address of an ipif has to go through the ipsq_t. This ensures that only
258 * one such exclusive operation proceeds at any time on the ipif. It then
259 * waits for all refcnts
260 * associated with this ipif to come down to zero. The address is changed
261 * only after the ipif has been quiesced. Then the ipif is brought up again.
262 * More details are described above the comment in ip_sioctl_flags.
263 *
264 * Packet processing is based mostly on IREs and are fully multi-threaded
265 * using standard Solaris MT techniques.
266 *
267 * There are explicit locks in IP to handle:
268 * - The ip_g_head list maintained by mi_open_link() and friends.
269 *
270 * - The reassembly data structures (one lock per hash bucket)
271 *
272 * - conn_lock is meant to protect conn_t fields. The fields actually
273 * protected by conn_lock are documented in the conn_t definition.
274 *
275 * - ire_lock to protect some of the fields of the ire, IRE tables
276 * (one lock per hash bucket). Refer to ip_ire.c for details.
277 *
278 * - ndp_g_lock and ncec_lock for protecting NCEs.
279 *
280 * - ill_lock protects fields of the ill and ipif. Details in ip.h
281 *
282 * - ill_g_lock: This is a global reader/writer lock. Protects the following
283 * * The AVL tree based global multi list of all ills.
284 * * The linked list of all ipifs of an ill
285 * * The <ipsq-xop> mapping
286 * * <ill-phyint> association
287 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
288 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the
289 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
290 * writer for the actual duration of the insertion/deletion/change.
291 *
292 * - ill_lock: This is a per ill mutex.
293 * It protects some members of the ill_t struct; see ip.h for details.
294 * It also protects the <ill-phyint> assoc.
295 * It also protects the list of ipifs hanging off the ill.
296 *
297 * - ipsq_lock: This is a per ipsq_t mutex lock.
298 * This protects some members of the ipsq_t struct; see ip.h for details.
299 * It also protects the <ipsq-ipxop> mapping
300 *
301 * - ipx_lock: This is a per ipxop_t mutex lock.
302 * This protects some members of the ipxop_t struct; see ip.h for details.
303 *
304 * - phyint_lock: This is a per phyint mutex lock. Protects just the
305 * phyint_flags
306 *
307 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
308 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
309 * uniqueness check also done atomically.
310 *
311 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
312 * group list linked by ill_usesrc_grp_next. It also protects the
313 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
314 * group is being added or deleted. This lock is taken as a reader when
315 * walking the list/group(eg: to get the number of members in a usesrc group).
316 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
317 * field is changing state i.e from NULL to non-NULL or vice-versa. For
318 * example, it is not necessary to take this lock in the initial portion
319 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
320 * operations are executed exclusively and that ensures that the "usesrc
321 * group state" cannot change. The "usesrc group state" change can happen
322 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
323 *
324 * Changing <ill-phyint>, <ipsq-xop> assocications:
325 *
326 * To change the <ill-phyint> association, the ill_g_lock must be held
327 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
328 * must be held.
329 *
330 * To change the <ipsq-xop> association, the ill_g_lock must be held as
331 * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
332 * This is only done when ills are added or removed from IPMP groups.
333 *
334 * To add or delete an ipif from the list of ipifs hanging off the ill,
335 * ill_g_lock (writer) and ill_lock must be held and the thread must be
336 * a writer on the associated ipsq.
337 *
338 * To add or delete an ill to the system, the ill_g_lock must be held as
339 * writer and the thread must be a writer on the associated ipsq.
340 *
341 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
342 * must be a writer on the associated ipsq.
343 *
344 * Lock hierarchy
345 *
346 * Some lock hierarchy scenarios are listed below.
347 *
348 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
349 * ill_g_lock -> ill_lock(s) -> phyint_lock
350 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
351 * ill_g_lock -> ip_addr_avail_lock
352 * conn_lock -> irb_lock -> ill_lock -> ire_lock
353 * ill_g_lock -> ip_g_nd_lock
354 * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
355 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
356 * arl_lock -> ill_lock
357 * ips_ire_dep_lock -> irb_lock
358 *
359 * When more than 1 ill lock is needed to be held, all ill lock addresses
360 * are sorted on address and locked starting from highest addressed lock
361 * downward.
362 *
363 * Multicast scenarios
364 * ips_ill_g_lock -> ill_mcast_lock
365 * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
366 * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
367 * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
368 * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
369 * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
370 *
371 * IPsec scenarios
372 *
373 * ipsa_lock -> ill_g_lock -> ill_lock
374 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
375 *
376 * Trusted Solaris scenarios
377 *
378 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
379 * igsa_lock -> gcdb_lock
380 * gcgrp_rwlock -> ire_lock
381 * gcgrp_rwlock -> gcdb_lock
382 *
383 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
384 *
385 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
386 * sq_lock -> conn_lock -> QLOCK(q)
387 * ill_lock -> ft_lock -> fe_lock
388 *
389 * Routing/forwarding table locking notes:
390 *
391 * Lock acquisition order: Radix tree lock, irb_lock.
392 * Requirements:
393 * i. Walker must not hold any locks during the walker callback.
394 * ii Walker must not see a truncated tree during the walk because of any node
395 * deletion.
396 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
397 * in many places in the code to walk the irb list. Thus even if all the
398 * ires in a bucket have been deleted, we still can't free the radix node
399 * until the ires have actually been inactive'd (freed).
400 *
401 * Tree traversal - Need to hold the global tree lock in read mode.
402 * Before dropping the global tree lock, need to either increment the ire_refcnt
403 * to ensure that the radix node can't be deleted.
404 *
405 * Tree add - Need to hold the global tree lock in write mode to add a
406 * radix node. To prevent the node from being deleted, increment the
407 * irb_refcnt, after the node is added to the tree. The ire itself is
408 * added later while holding the irb_lock, but not the tree lock.
409 *
410 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
411 * All associated ires must be inactive (i.e. freed), and irb_refcnt
412 * must be zero.
413 *
414 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
415 * global tree lock (read mode) for traversal.
416 *
417 * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
418 * hence we will acquire irb_lock while holding ips_ire_dep_lock.
419 *
420 * IPsec notes :
421 *
422 * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
423 * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
424 * ip_xmit_attr_t has the
425 * information used by the IPsec code for applying the right level of
426 * protection. The information initialized by IP in the ip_xmit_attr_t
427 * is determined by the per-socket policy or global policy in the system.
428 * For inbound datagrams, the ip_recv_attr_t
429 * starts out with nothing in it. It gets filled
430 * with the right information if it goes through the AH/ESP code, which
431 * happens if the incoming packet is secure. The information initialized
432 * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
433 * the policy requirements needed by per-socket policy or global policy
434 * is met or not.
435 *
436 * For fully connected sockets i.e dst, src [addr, port] is known,
437 * conn_policy_cached is set indicating that policy has been cached.
438 * conn_in_enforce_policy may or may not be set depending on whether
439 * there is a global policy match or per-socket policy match.
440 * Policy inheriting happpens in ip_policy_set once the destination is known.
441 * Once the right policy is set on the conn_t, policy cannot change for
442 * this socket. This makes life simpler for TCP (UDP ?) where
443 * re-transmissions go out with the same policy. For symmetry, policy
444 * is cached for fully connected UDP sockets also. Thus if policy is cached,
445 * it also implies that policy is latched i.e policy cannot change
446 * on these sockets. As we have the right policy on the conn, we don't
447 * have to lookup global policy for every outbound and inbound datagram
448 * and thus serving as an optimization. Note that a global policy change
449 * does not affect fully connected sockets if they have policy. If fully
450 * connected sockets did not have any policy associated with it, global
451 * policy change may affect them.
452 *
453 * IP Flow control notes:
454 * ---------------------
455 * Non-TCP streams are flow controlled by IP. The way this is accomplished
456 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
457 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
458 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
459 * functions.
460 *
461 * Per Tx ring udp flow control:
462 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
463 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
464 *
465 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
466 * To achieve best performance, outgoing traffic need to be fanned out among
467 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
468 * traffic out of the NIC and it takes a fanout hint. UDP connections pass
469 * the address of connp as fanout hint to mac_tx(). Under flow controlled
470 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
471 * cookie points to a specific Tx ring that is blocked. The cookie is used to
472 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
473 * point to drain_lists (idl_t's). These drain list will store the blocked UDP
474 * connp's. The drain list is not a single list but a configurable number of
475 * lists.
476 *
477 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
478 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
479 * which is equal to 128. This array in turn contains a pointer to idl_t[],
480 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
481 * list will point to the list of connp's that are flow controlled.
482 *
483 * --------------- ------- ------- -------
484 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
485 * | --------------- ------- ------- -------
486 * | --------------- ------- ------- -------
487 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
488 * ---------------- | --------------- ------- ------- -------
489 * |idl_tx_list[0]|->| --------------- ------- ------- -------
490 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
491 * | --------------- ------- ------- -------
492 * . . . . .
493 * | --------------- ------- ------- -------
494 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
495 * --------------- ------- ------- -------
496 * --------------- ------- ------- -------
497 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
498 * | --------------- ------- ------- -------
499 * | --------------- ------- ------- -------
500 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
501 * |idl_tx_list[1]|->| --------------- ------- ------- -------
502 * ---------------- | . . . .
503 * | --------------- ------- ------- -------
504 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
505 * --------------- ------- ------- -------
506 * .....
507 * ----------------
508 * |idl_tx_list[n]|-> ...
509 * ----------------
510 *
511 * When mac_tx() returns a cookie, the cookie is hashed into an index into
512 * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
513 * to insert the conn onto. conn_drain_insert() asserts flow control for the
514 * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
515 * Further, conn_blocked is set to indicate that the conn is blocked.
516 *
517 * GLDv3 calls ill_flow_enable() when flow control is relieved. The cookie
518 * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
519 * is again hashed to locate the appropriate idl_tx_list, which is then
520 * drained via conn_walk_drain(). conn_walk_drain() goes through each conn in
521 * the drain list and calls conn_drain_remove() to clear flow control (via
522 * calling su_txq_full() or clearing QFULL), and remove the conn from the
523 * drain list.
524 *
525 * Note that the drain list is not a single list but a (configurable) array of
526 * lists (8 elements by default). Synchronization between drain insertion and
527 * flow control wakeup is handled by using idl_txl->txl_lock, and only
528 * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
529 *
530 * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
531 * On the send side, if the packet cannot be sent down to the driver by IP
532 * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
533 * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
534 * the 0'th drain list. When ip_wsrv() runs on the ill_wq because flow
535 * control has been relieved, the blocked conns in the 0'th drain list are
536 * drained as in the non-STREAMS case.
537 *
538 * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
539 * is done when the conn is inserted into the drain list (conn_drain_insert())
540 * and cleared when the conn is removed from the it (conn_drain_remove()).
541 *
542 * IPQOS notes:
543 *
544 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
545 * and IPQoS modules. IPPF includes hooks in IP at different control points
546 * (callout positions) which direct packets to IPQoS modules for policy
547 * processing. Policies, if present, are global.
548 *
549 * The callout positions are located in the following paths:
550 * o local_in (packets destined for this host)
551 * o local_out (packets orginating from this host )
552 * o fwd_in (packets forwarded by this m/c - inbound)
553 * o fwd_out (packets forwarded by this m/c - outbound)
554 * Hooks at these callout points can be enabled/disabled using the ndd variable
555 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
556 * By default all the callout positions are enabled.
557 *
558 * Outbound (local_out)
559 * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
560 *
561 * Inbound (local_in)
562 * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
563 *
564 * Forwarding (in and out)
565 * Hooks are placed in ire_recv_forward_v4/v6.
566 *
567 * IP Policy Framework processing (IPPF processing)
568 * Policy processing for a packet is initiated by ip_process, which ascertains
569 * that the classifier (ipgpc) is loaded and configured, failing which the
570 * packet resumes normal processing in IP. If the clasifier is present, the
571 * packet is acted upon by one or more IPQoS modules (action instances), per
572 * filters configured in ipgpc and resumes normal IP processing thereafter.
573 * An action instance can drop a packet in course of its processing.
574 *
575 * Zones notes:
576 *
577 * The partitioning rules for networking are as follows:
578 * 1) Packets coming from a zone must have a source address belonging to that
579 * zone.
580 * 2) Packets coming from a zone can only be sent on a physical interface on
581 * which the zone has an IP address.
582 * 3) Between two zones on the same machine, packet delivery is only allowed if
583 * there's a matching route for the destination and zone in the forwarding
584 * table.
585 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
586 * different zones can bind to the same port with the wildcard address
587 * (INADDR_ANY).
588 *
589 * The granularity of interface partitioning is at the logical interface level.
590 * Therefore, every zone has its own IP addresses, and incoming packets can be
591 * attributed to a zone unambiguously. A logical interface is placed into a zone
592 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
593 * structure. Rule (1) is implemented by modifying the source address selection
594 * algorithm so that the list of eligible addresses is filtered based on the
595 * sending process zone.
596 *
597 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
598 * across all zones, depending on their type. Here is the break-up:
599 *
600 * IRE type Shared/exclusive
601 * -------- ----------------
602 * IRE_BROADCAST Exclusive
603 * IRE_DEFAULT (default routes) Shared (*)
604 * IRE_LOCAL Exclusive (x)
605 * IRE_LOOPBACK Exclusive
606 * IRE_PREFIX (net routes) Shared (*)
607 * IRE_IF_NORESOLVER (interface routes) Exclusive
608 * IRE_IF_RESOLVER (interface routes) Exclusive
609 * IRE_IF_CLONE (interface routes) Exclusive
610 * IRE_HOST (host routes) Shared (*)
611 *
612 * (*) A zone can only use a default or off-subnet route if the gateway is
613 * directly reachable from the zone, that is, if the gateway's address matches
614 * one of the zone's logical interfaces.
615 *
616 * (x) IRE_LOCAL are handled a bit differently.
617 * When ip_restrict_interzone_loopback is set (the default),
618 * ire_route_recursive restricts loopback using an IRE_LOCAL
619 * between zone to the case when L2 would have conceptually looped the packet
620 * back, i.e. the loopback which is required since neither Ethernet drivers
621 * nor Ethernet hardware loops them back. This is the case when the normal
622 * routes (ignoring IREs with different zoneids) would send out the packet on
623 * the same ill as the ill with which is IRE_LOCAL is associated.
624 *
625 * Multiple zones can share a common broadcast address; typically all zones
626 * share the 255.255.255.255 address. Incoming as well as locally originated
627 * broadcast packets must be dispatched to all the zones on the broadcast
628 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
629 * since some zones may not be on the 10.16.72/24 network. To handle this, each
630 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
631 * sent to every zone that has an IRE_BROADCAST entry for the destination
632 * address on the input ill, see ip_input_broadcast().
633 *
634 * Applications in different zones can join the same multicast group address.
635 * The same logic applies for multicast as for broadcast. ip_input_multicast
636 * dispatches packets to all zones that have members on the physical interface.
637 */
638
639 /*
640 * Squeue Fanout flags:
641 * 0: No fanout.
642 * 1: Fanout across all squeues
643 */
644 boolean_t ip_squeue_fanout = 0;
645
646 /*
647 * Maximum dups allowed per packet.
648 */
649 uint_t ip_max_frag_dups = 10;
650
651 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
652 cred_t *credp, boolean_t isv6);
653 static mblk_t *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
654
655 static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
656 static void icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
657 static void icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
658 ip_recv_attr_t *);
659 static void icmp_options_update(ipha_t *);
660 static void icmp_param_problem(mblk_t *, uint8_t, ip_recv_attr_t *);
661 static void icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
662 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
663 static void icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
664 ip_recv_attr_t *);
665 static void icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
666 static void icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
667 ip_recv_attr_t *);
668
669 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
670 char *ip_dot_addr(ipaddr_t, char *);
671 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
672 static char *ip_dot_saddr(uchar_t *, char *);
673 static int ip_lrput(queue_t *, mblk_t *);
674 ipaddr_t ip_net_mask(ipaddr_t);
675 char *ip_nv_lookup(nv_t *, int);
676 int ip_rput(queue_t *, mblk_t *);
677 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
678 void *dummy_arg);
679 int ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
680 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
681 mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
682 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
683 ip_stack_t *, boolean_t);
684 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
685 boolean_t);
686 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
687 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
688 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
689 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
690 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
691 ip_stack_t *ipst, boolean_t);
692 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
693 ip_stack_t *ipst, boolean_t);
694 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
695 ip_stack_t *ipst);
696 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
697 ip_stack_t *ipst);
698 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
699 ip_stack_t *ipst);
700 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
701 ip_stack_t *ipst);
702 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
703 ip_stack_t *ipst);
704 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
705 ip_stack_t *ipst);
706 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
707 ip_stack_t *ipst);
708 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
709 ip_stack_t *ipst);
710 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
711 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
712 static void ip_snmp_get2_v4_media(ncec_t *, void *);
713 static void ip_snmp_get2_v6_media(ncec_t *, void *);
714 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
715
716 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
717 mblk_t *);
718
719 static void conn_drain_init(ip_stack_t *);
720 static void conn_drain_fini(ip_stack_t *);
721 static void conn_drain(conn_t *connp, boolean_t closing);
722
723 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
724 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
725
726 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
727 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
728 static void ip_stack_fini(netstackid_t stackid, void *arg);
729
730 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
731 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
732 ire_t *, conn_t *, boolean_t, const in6_addr_t *, mcast_record_t,
733 const in6_addr_t *);
734
735 static int ip_squeue_switch(int);
736
737 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
738 static void ip_kstat_fini(netstackid_t, kstat_t *);
739 static int ip_kstat_update(kstat_t *kp, int rw);
740 static void *icmp_kstat_init(netstackid_t);
741 static void icmp_kstat_fini(netstackid_t, kstat_t *);
742 static int icmp_kstat_update(kstat_t *kp, int rw);
743 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
744 static void ip_kstat2_fini(netstackid_t, kstat_t *);
745
746 static void ipobs_init(ip_stack_t *);
747 static void ipobs_fini(ip_stack_t *);
748
749 static int ip_tp_cpu_update(cpu_setup_t, int, void *);
750
751 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
752
753 static long ip_rput_pullups;
754 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
755
756 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
757 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
758
759 int ip_debug;
760
761 /*
762 * Multirouting/CGTP stuff
763 */
764 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */
765
766 /*
767 * IP tunables related declarations. Definitions are in ip_tunables.c
768 */
769 extern mod_prop_info_t ip_propinfo_tbl[];
770 extern int ip_propinfo_count;
771
772 /*
773 * Table of IP ioctls encoding the various properties of the ioctl and
774 * indexed based on the last byte of the ioctl command. Occasionally there
775 * is a clash, and there is more than 1 ioctl with the same last byte.
776 * In such a case 1 ioctl is encoded in the ndx table and the remaining
777 * ioctls are encoded in the misc table. An entry in the ndx table is
778 * retrieved by indexing on the last byte of the ioctl command and comparing
779 * the ioctl command with the value in the ndx table. In the event of a
780 * mismatch the misc table is then searched sequentially for the desired
781 * ioctl command.
782 *
783 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
784 */
785 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
786 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
787 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
788 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
789 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
790 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
791 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
792 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
793 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
794 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
795 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
796
797 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
798 MISC_CMD, ip_siocaddrt, NULL },
799 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
800 MISC_CMD, ip_siocdelrt, NULL },
801
802 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
803 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
804 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
805 IF_CMD, ip_sioctl_get_addr, NULL },
806
807 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
808 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
809 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
810 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
811
812 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
813 IPI_PRIV | IPI_WR,
814 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
815 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
816 IPI_MODOK | IPI_GET_CMD,
817 IF_CMD, ip_sioctl_get_flags, NULL },
818
819 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
820 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
821
822 /* copyin size cannot be coded for SIOCGIFCONF */
823 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
824 MISC_CMD, ip_sioctl_get_ifconf, NULL },
825
826 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
827 IF_CMD, ip_sioctl_mtu, NULL },
828 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
829 IF_CMD, ip_sioctl_get_mtu, NULL },
830 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
831 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
832 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
833 IF_CMD, ip_sioctl_brdaddr, NULL },
834 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
835 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
836 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
837 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
838 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
839 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
840 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
841 IF_CMD, ip_sioctl_metric, NULL },
842 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
843
844 /* See 166-168 below for extended SIOC*XARP ioctls */
845 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
846 ARP_CMD, ip_sioctl_arp, NULL },
847 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
848 ARP_CMD, ip_sioctl_arp, NULL },
849 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
850 ARP_CMD, ip_sioctl_arp, NULL },
851
852 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
853 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
854 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
855 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
856 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
857 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
858 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
859 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
860 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
861 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
862 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
863 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
864 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
865 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
866 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
867 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
868 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
869 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
870 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
871 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
872 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
873
874 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
875 MISC_CMD, if_unitsel, if_unitsel_restart },
876
877 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
878 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
879 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
880 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
881 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
882 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
883 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
884 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
885 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
886 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
887 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
888 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
889 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
890 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
891 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
892 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
893 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
894 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
895
896 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
897 IPI_PRIV | IPI_WR | IPI_MODOK,
898 IF_CMD, ip_sioctl_sifname, NULL },
899
900 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
901 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
902 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
903 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
904 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
905 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
906 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
907 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
908 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
909 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
910 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
911 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
912 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
913
914 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
915 MISC_CMD, ip_sioctl_get_ifnum, NULL },
916 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
917 IF_CMD, ip_sioctl_get_muxid, NULL },
918 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
919 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
920
921 /* Both if and lif variants share same func */
922 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
923 IF_CMD, ip_sioctl_get_lifindex, NULL },
924 /* Both if and lif variants share same func */
925 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
926 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
927
928 /* copyin size cannot be coded for SIOCGIFCONF */
929 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
930 MISC_CMD, ip_sioctl_get_ifconf, NULL },
931 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
932 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
933 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
934 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
935 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
936 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
937 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
938 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
939 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
940 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
941 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
942 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
943 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
944 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
945 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
946 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
947 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
948
949 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
950 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
951 ip_sioctl_removeif_restart },
952 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
953 IPI_GET_CMD | IPI_PRIV | IPI_WR,
954 LIF_CMD, ip_sioctl_addif, NULL },
955 #define SIOCLIFADDR_NDX 112
956 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
957 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
958 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
959 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
960 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
961 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
962 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
963 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
964 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
965 IPI_PRIV | IPI_WR,
966 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
967 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
968 IPI_GET_CMD | IPI_MODOK,
969 LIF_CMD, ip_sioctl_get_flags, NULL },
970
971 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
972 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
973
974 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
975 ip_sioctl_get_lifconf, NULL },
976 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
977 LIF_CMD, ip_sioctl_mtu, NULL },
978 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
979 LIF_CMD, ip_sioctl_get_mtu, NULL },
980 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
981 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
982 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
983 LIF_CMD, ip_sioctl_brdaddr, NULL },
984 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
985 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
986 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
987 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
988 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
989 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
990 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
991 LIF_CMD, ip_sioctl_metric, NULL },
992 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
993 IPI_PRIV | IPI_WR | IPI_MODOK,
994 LIF_CMD, ip_sioctl_slifname,
995 ip_sioctl_slifname_restart },
996
997 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
998 MISC_CMD, ip_sioctl_get_lifnum, NULL },
999 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
1000 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
1001 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
1002 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
1003 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1004 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
1005 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1006 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
1007 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1008 LIF_CMD, ip_sioctl_token, NULL },
1009 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1010 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
1011 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1012 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1013 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1014 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
1015 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1016 LIF_CMD, ip_sioctl_lnkinfo, NULL },
1017
1018 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1019 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1020 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1021 LIF_CMD, ip_siocdelndp_v6, NULL },
1022 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1023 LIF_CMD, ip_siocqueryndp_v6, NULL },
1024 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1025 LIF_CMD, ip_siocsetndp_v6, NULL },
1026 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1027 MISC_CMD, ip_sioctl_tmyaddr, NULL },
1028 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1029 MISC_CMD, ip_sioctl_tonlink, NULL },
1030 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1031 MISC_CMD, ip_sioctl_tmysite, NULL },
1032 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1033 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1034
1035 /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
1036 /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1038 /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1039 /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1040
1041 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1042
1043 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
1044 LIF_CMD, ip_sioctl_get_binding, NULL },
1045 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1046 IPI_PRIV | IPI_WR,
1047 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1048 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1049 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
1050 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
1051 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
1052
1053 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1054 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1055 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1056 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1057
1058 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1059
1060 /* These are handled in ip_sioctl_copyin_setup itself */
1061 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1062 MISC_CMD, NULL, NULL },
1063 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1064 MISC_CMD, NULL, NULL },
1065 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1066
1067 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1068 ip_sioctl_get_lifconf, NULL },
1069
1070 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1071 XARP_CMD, ip_sioctl_arp, NULL },
1072 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1073 XARP_CMD, ip_sioctl_arp, NULL },
1074 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1075 XARP_CMD, ip_sioctl_arp, NULL },
1076
1077 /* SIOCPOPSOCKFS is not handled by IP */
1078 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1079
1080 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1081 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1082 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1083 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1084 ip_sioctl_slifzone_restart },
1085 /* 172-174 are SCTP ioctls and not handled by IP */
1086 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1087 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1088 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1089 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1090 IPI_GET_CMD, LIF_CMD,
1091 ip_sioctl_get_lifusesrc, 0 },
1092 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1093 IPI_PRIV | IPI_WR,
1094 LIF_CMD, ip_sioctl_slifusesrc,
1095 NULL },
1096 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1097 ip_sioctl_get_lifsrcof, NULL },
1098 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1099 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1100 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1101 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1102 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1103 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1104 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1105 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1106 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1107 /* SIOCSENABLESDP is handled by SDP */
1108 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1109 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1110 /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1111 IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1112 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1113 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1114 ip_sioctl_ilb_cmd, NULL },
1115 /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL },
1116 /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL},
1117 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1118 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1119 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1120 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1121 /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1122 LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1123 };
1124
1125 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1126
1127 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1128 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1129 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1130 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1131 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1132 { ND_GET, 0, 0, 0, NULL, NULL },
1133 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1134 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1135 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1136 MISC_CMD, mrt_ioctl},
1137 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1138 MISC_CMD, mrt_ioctl},
1139 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1140 MISC_CMD, mrt_ioctl}
1141 };
1142
1143 int ip_misc_ioctl_count =
1144 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1145
1146 int conn_drain_nthreads; /* Number of drainers reqd. */
1147 /* Settable in /etc/system */
1148 /* Defined in ip_ire.c */
1149 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1150 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1151 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1152
1153 static nv_t ire_nv_arr[] = {
1154 { IRE_BROADCAST, "BROADCAST" },
1155 { IRE_LOCAL, "LOCAL" },
1156 { IRE_LOOPBACK, "LOOPBACK" },
1157 { IRE_DEFAULT, "DEFAULT" },
1158 { IRE_PREFIX, "PREFIX" },
1159 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1160 { IRE_IF_RESOLVER, "IF_RESOLV" },
1161 { IRE_IF_CLONE, "IF_CLONE" },
1162 { IRE_HOST, "HOST" },
1163 { IRE_MULTICAST, "MULTICAST" },
1164 { IRE_NOROUTE, "NOROUTE" },
1165 { 0 }
1166 };
1167
1168 nv_t *ire_nv_tbl = ire_nv_arr;
1169
1170 /* Simple ICMP IP Header Template */
1171 static ipha_t icmp_ipha = {
1172 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1173 };
1174
1175 struct module_info ip_mod_info = {
1176 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1177 IP_MOD_LOWAT
1178 };
1179
1180 /*
1181 * Duplicate static symbols within a module confuses mdb; so we avoid the
1182 * problem by making the symbols here distinct from those in udp.c.
1183 */
1184
1185 /*
1186 * Entry points for IP as a device and as a module.
1187 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1188 */
1189 static struct qinit iprinitv4 = {
1190 ip_rput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1191 };
1192
1193 struct qinit iprinitv6 = {
1194 ip_rput_v6, NULL, ip_openv6, ip_close, NULL, &ip_mod_info
1195 };
1196
1197 static struct qinit ipwinit = {
1198 ip_wput_nondata, ip_wsrv, NULL, NULL, NULL, &ip_mod_info
1199 };
1200
1201 static struct qinit iplrinit = {
1202 ip_lrput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1203 };
1204
1205 static struct qinit iplwinit = {
1206 ip_lwput, NULL, NULL, NULL, NULL, &ip_mod_info
1207 };
1208
1209 /* For AF_INET aka /dev/ip */
1210 struct streamtab ipinfov4 = {
1211 &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1212 };
1213
1214 /* For AF_INET6 aka /dev/ip6 */
1215 struct streamtab ipinfov6 = {
1216 &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1217 };
1218
1219 #ifdef DEBUG
1220 boolean_t skip_sctp_cksum = B_FALSE;
1221 #endif
1222
1223 /*
1224 * Generate an ICMP fragmentation needed message.
1225 * When called from ip_output side a minimal ip_recv_attr_t needs to be
1226 * constructed by the caller.
1227 */
1228 void
icmp_frag_needed(mblk_t * mp,int mtu,ip_recv_attr_t * ira)1229 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1230 {
1231 icmph_t icmph;
1232 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1233
1234 mp = icmp_pkt_err_ok(mp, ira);
1235 if (mp == NULL)
1236 return;
1237
1238 bzero(&icmph, sizeof (icmph_t));
1239 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1240 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1241 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1242 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1243 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1244
1245 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1246 }
1247
1248 /*
1249 * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1250 * If the ICMP message is consumed by IP, i.e., it should not be delivered
1251 * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1252 * Likewise, if the ICMP error is misformed (too short, etc), then it
1253 * returns NULL. The caller uses this to determine whether or not to send
1254 * to raw sockets.
1255 *
1256 * All error messages are passed to the matching transport stream.
1257 *
1258 * The following cases are handled by icmp_inbound:
1259 * 1) It needs to send a reply back and possibly delivering it
1260 * to the "interested" upper clients.
1261 * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1262 * 3) It needs to change some values in IP only.
1263 * 4) It needs to change some values in IP and upper layers e.g TCP
1264 * by delivering an error to the upper layers.
1265 *
1266 * We handle the above three cases in the context of IPsec in the
1267 * following way :
1268 *
1269 * 1) Send the reply back in the same way as the request came in.
1270 * If it came in encrypted, it goes out encrypted. If it came in
1271 * clear, it goes out in clear. Thus, this will prevent chosen
1272 * plain text attack.
1273 * 2) The client may or may not expect things to come in secure.
1274 * If it comes in secure, the policy constraints are checked
1275 * before delivering it to the upper layers. If it comes in
1276 * clear, ipsec_inbound_accept_clear will decide whether to
1277 * accept this in clear or not. In both the cases, if the returned
1278 * message (IP header + 8 bytes) that caused the icmp message has
1279 * AH/ESP headers, it is sent up to AH/ESP for validation before
1280 * sending up. If there are only 8 bytes of returned message, then
1281 * upper client will not be notified.
1282 * 3) Check with global policy to see whether it matches the constaints.
1283 * But this will be done only if icmp_accept_messages_in_clear is
1284 * zero.
1285 * 4) If we need to change both in IP and ULP, then the decision taken
1286 * while affecting the values in IP and while delivering up to TCP
1287 * should be the same.
1288 *
1289 * There are two cases.
1290 *
1291 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1292 * failed), we will not deliver it to the ULP, even though they
1293 * are *willing* to accept in *clear*. This is fine as our global
1294 * disposition to icmp messages asks us reject the datagram.
1295 *
1296 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1297 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1298 * to deliver it to ULP (policy failed), it can lead to
1299 * consistency problems. The cases known at this time are
1300 * ICMP_DESTINATION_UNREACHABLE messages with following code
1301 * values :
1302 *
1303 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1304 * and Upper layer rejects. Then the communication will
1305 * come to a stop. This is solved by making similar decisions
1306 * at both levels. Currently, when we are unable to deliver
1307 * to the Upper Layer (due to policy failures) while IP has
1308 * adjusted dce_pmtu, the next outbound datagram would
1309 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1310 * will be with the right level of protection. Thus the right
1311 * value will be communicated even if we are not able to
1312 * communicate when we get from the wire initially. But this
1313 * assumes there would be at least one outbound datagram after
1314 * IP has adjusted its dce_pmtu value. To make things
1315 * simpler, we accept in clear after the validation of
1316 * AH/ESP headers.
1317 *
1318 * - Other ICMP ERRORS : We may not be able to deliver it to the
1319 * upper layer depending on the level of protection the upper
1320 * layer expects and the disposition in ipsec_inbound_accept_clear().
1321 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1322 * should be accepted in clear when the Upper layer expects secure.
1323 * Thus the communication may get aborted by some bad ICMP
1324 * packets.
1325 */
1326 mblk_t *
icmp_inbound_v4(mblk_t * mp,ip_recv_attr_t * ira)1327 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1328 {
1329 icmph_t *icmph;
1330 ipha_t *ipha; /* Outer header */
1331 int ip_hdr_length; /* Outer header length */
1332 boolean_t interested;
1333 ipif_t *ipif;
1334 uint32_t ts;
1335 uint32_t *tsp;
1336 timestruc_t now;
1337 ill_t *ill = ira->ira_ill;
1338 ip_stack_t *ipst = ill->ill_ipst;
1339 zoneid_t zoneid = ira->ira_zoneid;
1340 int len_needed;
1341 mblk_t *mp_ret = NULL;
1342
1343 ipha = (ipha_t *)mp->b_rptr;
1344
1345 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1346
1347 ip_hdr_length = ira->ira_ip_hdr_length;
1348 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1349 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1350 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1351 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1352 freemsg(mp);
1353 return (NULL);
1354 }
1355 /* Last chance to get real. */
1356 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1357 if (ipha == NULL) {
1358 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1359 freemsg(mp);
1360 return (NULL);
1361 }
1362 }
1363
1364 /* The IP header will always be a multiple of four bytes */
1365 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1366 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1367 icmph->icmph_code));
1368
1369 /*
1370 * We will set "interested" to "true" if we should pass a copy to
1371 * the transport or if we handle the packet locally.
1372 */
1373 interested = B_FALSE;
1374 switch (icmph->icmph_type) {
1375 case ICMP_ECHO_REPLY:
1376 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1377 break;
1378 case ICMP_DEST_UNREACHABLE:
1379 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1380 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1381 interested = B_TRUE; /* Pass up to transport */
1382 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1383 break;
1384 case ICMP_SOURCE_QUENCH:
1385 interested = B_TRUE; /* Pass up to transport */
1386 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1387 break;
1388 case ICMP_REDIRECT:
1389 if (!ipst->ips_ip_ignore_redirect)
1390 interested = B_TRUE;
1391 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1392 break;
1393 case ICMP_ECHO_REQUEST:
1394 /*
1395 * Whether to respond to echo requests that come in as IP
1396 * broadcasts or as IP multicast is subject to debate
1397 * (what isn't?). We aim to please, you pick it.
1398 * Default is do it.
1399 */
1400 if (ira->ira_flags & IRAF_MULTICAST) {
1401 /* multicast: respond based on tunable */
1402 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1403 } else if (ira->ira_flags & IRAF_BROADCAST) {
1404 /* broadcast: respond based on tunable */
1405 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1406 } else {
1407 /* unicast: always respond */
1408 interested = B_TRUE;
1409 }
1410 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1411 if (!interested) {
1412 /* We never pass these to RAW sockets */
1413 freemsg(mp);
1414 return (NULL);
1415 }
1416
1417 /* Check db_ref to make sure we can modify the packet. */
1418 if (mp->b_datap->db_ref > 1) {
1419 mblk_t *mp1;
1420
1421 mp1 = copymsg(mp);
1422 freemsg(mp);
1423 if (!mp1) {
1424 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1425 return (NULL);
1426 }
1427 mp = mp1;
1428 ipha = (ipha_t *)mp->b_rptr;
1429 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1430 }
1431 icmph->icmph_type = ICMP_ECHO_REPLY;
1432 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1433 icmp_send_reply_v4(mp, ipha, icmph, ira);
1434 return (NULL);
1435
1436 case ICMP_ROUTER_ADVERTISEMENT:
1437 case ICMP_ROUTER_SOLICITATION:
1438 break;
1439 case ICMP_TIME_EXCEEDED:
1440 interested = B_TRUE; /* Pass up to transport */
1441 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1442 break;
1443 case ICMP_PARAM_PROBLEM:
1444 interested = B_TRUE; /* Pass up to transport */
1445 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1446 break;
1447 case ICMP_TIME_STAMP_REQUEST:
1448 /* Response to Time Stamp Requests is local policy. */
1449 if (ipst->ips_ip_g_resp_to_timestamp) {
1450 if (ira->ira_flags & IRAF_MULTIBROADCAST)
1451 interested =
1452 ipst->ips_ip_g_resp_to_timestamp_bcast;
1453 else
1454 interested = B_TRUE;
1455 }
1456 if (!interested) {
1457 /* We never pass these to RAW sockets */
1458 freemsg(mp);
1459 return (NULL);
1460 }
1461
1462 /* Make sure we have enough of the packet */
1463 len_needed = ip_hdr_length + ICMPH_SIZE +
1464 3 * sizeof (uint32_t);
1465
1466 if (mp->b_wptr - mp->b_rptr < len_needed) {
1467 ipha = ip_pullup(mp, len_needed, ira);
1468 if (ipha == NULL) {
1469 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1470 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1471 mp, ill);
1472 freemsg(mp);
1473 return (NULL);
1474 }
1475 /* Refresh following the pullup. */
1476 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1477 }
1478 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1479 /* Check db_ref to make sure we can modify the packet. */
1480 if (mp->b_datap->db_ref > 1) {
1481 mblk_t *mp1;
1482
1483 mp1 = copymsg(mp);
1484 freemsg(mp);
1485 if (!mp1) {
1486 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1487 return (NULL);
1488 }
1489 mp = mp1;
1490 ipha = (ipha_t *)mp->b_rptr;
1491 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1492 }
1493 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1494 tsp = (uint32_t *)&icmph[1];
1495 tsp++; /* Skip past 'originate time' */
1496 /* Compute # of milliseconds since midnight */
1497 gethrestime(&now);
1498 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1499 NSEC2MSEC(now.tv_nsec);
1500 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1501 *tsp++ = htonl(ts); /* Lay in 'send time' */
1502 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1503 icmp_send_reply_v4(mp, ipha, icmph, ira);
1504 return (NULL);
1505
1506 case ICMP_TIME_STAMP_REPLY:
1507 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1508 break;
1509 case ICMP_INFO_REQUEST:
1510 /* Per RFC 1122 3.2.2.7, ignore this. */
1511 case ICMP_INFO_REPLY:
1512 break;
1513 case ICMP_ADDRESS_MASK_REQUEST:
1514 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1515 interested =
1516 ipst->ips_ip_respond_to_address_mask_broadcast;
1517 } else {
1518 interested = B_TRUE;
1519 }
1520 if (!interested) {
1521 /* We never pass these to RAW sockets */
1522 freemsg(mp);
1523 return (NULL);
1524 }
1525 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1526 if (mp->b_wptr - mp->b_rptr < len_needed) {
1527 ipha = ip_pullup(mp, len_needed, ira);
1528 if (ipha == NULL) {
1529 BUMP_MIB(ill->ill_ip_mib,
1530 ipIfStatsInTruncatedPkts);
1531 ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1532 ill);
1533 freemsg(mp);
1534 return (NULL);
1535 }
1536 /* Refresh following the pullup. */
1537 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1538 }
1539 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1540 /* Check db_ref to make sure we can modify the packet. */
1541 if (mp->b_datap->db_ref > 1) {
1542 mblk_t *mp1;
1543
1544 mp1 = copymsg(mp);
1545 freemsg(mp);
1546 if (!mp1) {
1547 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1548 return (NULL);
1549 }
1550 mp = mp1;
1551 ipha = (ipha_t *)mp->b_rptr;
1552 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1553 }
1554 /*
1555 * Need the ipif with the mask be the same as the source
1556 * address of the mask reply. For unicast we have a specific
1557 * ipif. For multicast/broadcast we only handle onlink
1558 * senders, and use the source address to pick an ipif.
1559 */
1560 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1561 if (ipif == NULL) {
1562 /* Broadcast or multicast */
1563 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1564 if (ipif == NULL) {
1565 freemsg(mp);
1566 return (NULL);
1567 }
1568 }
1569 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1570 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1571 ipif_refrele(ipif);
1572 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1573 icmp_send_reply_v4(mp, ipha, icmph, ira);
1574 return (NULL);
1575
1576 case ICMP_ADDRESS_MASK_REPLY:
1577 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1578 break;
1579 default:
1580 interested = B_TRUE; /* Pass up to transport */
1581 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1582 break;
1583 }
1584 /*
1585 * See if there is an ICMP client to avoid an extra copymsg/freemsg
1586 * if there isn't one.
1587 */
1588 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1589 /* If there is an ICMP client and we want one too, copy it. */
1590
1591 if (!interested) {
1592 /* Caller will deliver to RAW sockets */
1593 return (mp);
1594 }
1595 mp_ret = copymsg(mp);
1596 if (mp_ret == NULL) {
1597 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1598 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1599 }
1600 } else if (!interested) {
1601 /* Neither we nor raw sockets are interested. Drop packet now */
1602 freemsg(mp);
1603 return (NULL);
1604 }
1605
1606 /*
1607 * ICMP error or redirect packet. Make sure we have enough of
1608 * the header and that db_ref == 1 since we might end up modifying
1609 * the packet.
1610 */
1611 if (mp->b_cont != NULL) {
1612 if (ip_pullup(mp, -1, ira) == NULL) {
1613 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1614 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1615 mp, ill);
1616 freemsg(mp);
1617 return (mp_ret);
1618 }
1619 }
1620
1621 if (mp->b_datap->db_ref > 1) {
1622 mblk_t *mp1;
1623
1624 mp1 = copymsg(mp);
1625 if (mp1 == NULL) {
1626 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1627 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1628 freemsg(mp);
1629 return (mp_ret);
1630 }
1631 freemsg(mp);
1632 mp = mp1;
1633 }
1634
1635 /*
1636 * In case mp has changed, verify the message before any further
1637 * processes.
1638 */
1639 ipha = (ipha_t *)mp->b_rptr;
1640 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1641 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1642 freemsg(mp);
1643 return (mp_ret);
1644 }
1645
1646 switch (icmph->icmph_type) {
1647 case ICMP_REDIRECT:
1648 icmp_redirect_v4(mp, ipha, icmph, ira);
1649 break;
1650 case ICMP_DEST_UNREACHABLE:
1651 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1652 /* Update DCE and adjust MTU is icmp header if needed */
1653 icmp_inbound_too_big_v4(icmph, ira);
1654 }
1655 /* FALLTHROUGH */
1656 default:
1657 icmp_inbound_error_fanout_v4(mp, icmph, ira);
1658 break;
1659 }
1660 return (mp_ret);
1661 }
1662
1663 /*
1664 * Send an ICMP echo, timestamp or address mask reply.
1665 * The caller has already updated the payload part of the packet.
1666 * We handle the ICMP checksum, IP source address selection and feed
1667 * the packet into ip_output_simple.
1668 */
1669 static void
icmp_send_reply_v4(mblk_t * mp,ipha_t * ipha,icmph_t * icmph,ip_recv_attr_t * ira)1670 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1671 ip_recv_attr_t *ira)
1672 {
1673 uint_t ip_hdr_length = ira->ira_ip_hdr_length;
1674 ill_t *ill = ira->ira_ill;
1675 ip_stack_t *ipst = ill->ill_ipst;
1676 ip_xmit_attr_t ixas;
1677
1678 /* Send out an ICMP packet */
1679 icmph->icmph_checksum = 0;
1680 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1681 /* Reset time to live. */
1682 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1683 {
1684 /* Swap source and destination addresses */
1685 ipaddr_t tmp;
1686
1687 tmp = ipha->ipha_src;
1688 ipha->ipha_src = ipha->ipha_dst;
1689 ipha->ipha_dst = tmp;
1690 }
1691 ipha->ipha_ident = 0;
1692 if (!IS_SIMPLE_IPH(ipha))
1693 icmp_options_update(ipha);
1694
1695 bzero(&ixas, sizeof (ixas));
1696 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1697 ixas.ixa_zoneid = ira->ira_zoneid;
1698 ixas.ixa_cred = kcred;
1699 ixas.ixa_cpid = NOPID;
1700 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
1701 ixas.ixa_ifindex = 0;
1702 ixas.ixa_ipst = ipst;
1703 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1704
1705 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1706 /*
1707 * This packet should go out the same way as it
1708 * came in i.e in clear, independent of the IPsec policy
1709 * for transmitting packets.
1710 */
1711 ixas.ixa_flags |= IXAF_NO_IPSEC;
1712 } else {
1713 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1714 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1715 /* Note: mp already consumed and ip_drop_packet done */
1716 return;
1717 }
1718 }
1719 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1720 /*
1721 * Not one or our addresses (IRE_LOCALs), thus we let
1722 * ip_output_simple pick the source.
1723 */
1724 ipha->ipha_src = INADDR_ANY;
1725 ixas.ixa_flags |= IXAF_SET_SOURCE;
1726 }
1727 /* Should we send with DF and use dce_pmtu? */
1728 if (ipst->ips_ipv4_icmp_return_pmtu) {
1729 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1730 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1731 }
1732
1733 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1734
1735 (void) ip_output_simple(mp, &ixas);
1736 ixa_cleanup(&ixas);
1737 }
1738
1739 /*
1740 * Verify the ICMP messages for either for ICMP error or redirect packet.
1741 * The caller should have fully pulled up the message. If it's a redirect
1742 * packet, only basic checks on IP header will be done; otherwise, verify
1743 * the packet by looking at the included ULP header.
1744 *
1745 * Called before icmp_inbound_error_fanout_v4 is called.
1746 */
1747 static boolean_t
icmp_inbound_verify_v4(mblk_t * mp,icmph_t * icmph,ip_recv_attr_t * ira)1748 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1749 {
1750 ill_t *ill = ira->ira_ill;
1751 int hdr_length;
1752 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1753 conn_t *connp;
1754 ipha_t *ipha; /* Inner IP header */
1755
1756 ipha = (ipha_t *)&icmph[1];
1757 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1758 goto truncated;
1759
1760 hdr_length = IPH_HDR_LENGTH(ipha);
1761
1762 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1763 goto discard_pkt;
1764
1765 if (hdr_length < sizeof (ipha_t))
1766 goto truncated;
1767
1768 if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1769 goto truncated;
1770
1771 /*
1772 * Stop here for ICMP_REDIRECT.
1773 */
1774 if (icmph->icmph_type == ICMP_REDIRECT)
1775 return (B_TRUE);
1776
1777 /*
1778 * ICMP errors only.
1779 */
1780 switch (ipha->ipha_protocol) {
1781 case IPPROTO_UDP:
1782 /*
1783 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1784 * transport header.
1785 */
1786 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1787 mp->b_wptr)
1788 goto truncated;
1789 break;
1790 case IPPROTO_TCP: {
1791 tcpha_t *tcpha;
1792
1793 /*
1794 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1795 * transport header.
1796 */
1797 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1798 mp->b_wptr)
1799 goto truncated;
1800
1801 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1802 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1803 ipst);
1804 if (connp == NULL)
1805 goto discard_pkt;
1806
1807 if ((connp->conn_verifyicmp != NULL) &&
1808 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1809 CONN_DEC_REF(connp);
1810 goto discard_pkt;
1811 }
1812 CONN_DEC_REF(connp);
1813 break;
1814 }
1815 case IPPROTO_SCTP:
1816 /*
1817 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1818 * transport header.
1819 */
1820 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1821 mp->b_wptr)
1822 goto truncated;
1823 break;
1824 case IPPROTO_ESP:
1825 case IPPROTO_AH:
1826 break;
1827 case IPPROTO_ENCAP:
1828 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1829 mp->b_wptr)
1830 goto truncated;
1831 break;
1832 default:
1833 break;
1834 }
1835
1836 return (B_TRUE);
1837
1838 discard_pkt:
1839 /* Bogus ICMP error. */
1840 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1841 return (B_FALSE);
1842
1843 truncated:
1844 /* We pulled up everthing already. Must be truncated */
1845 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1846 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1847 return (B_FALSE);
1848 }
1849
1850 /* Table from RFC 1191 */
1851 static int icmp_frag_size_table[] =
1852 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1853
1854 /*
1855 * Process received ICMP Packet too big.
1856 * Just handles the DCE create/update, including using the above table of
1857 * PMTU guesses. The caller is responsible for validating the packet before
1858 * passing it in and also to fanout the ICMP error to any matching transport
1859 * conns. Assumes the message has been fully pulled up and verified.
1860 *
1861 * Before getting here, the caller has called icmp_inbound_verify_v4()
1862 * that should have verified with ULP to prevent undoing the changes we're
1863 * going to make to DCE. For example, TCP might have verified that the packet
1864 * which generated error is in the send window.
1865 *
1866 * In some cases modified this MTU in the ICMP header packet; the caller
1867 * should pass to the matching ULP after this returns.
1868 */
1869 static void
icmp_inbound_too_big_v4(icmph_t * icmph,ip_recv_attr_t * ira)1870 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1871 {
1872 dce_t *dce;
1873 int old_mtu;
1874 int mtu, orig_mtu;
1875 ipaddr_t dst;
1876 boolean_t disable_pmtud;
1877 ill_t *ill = ira->ira_ill;
1878 ip_stack_t *ipst = ill->ill_ipst;
1879 uint_t hdr_length;
1880 ipha_t *ipha;
1881
1882 /* Caller already pulled up everything. */
1883 ipha = (ipha_t *)&icmph[1];
1884 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1885 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1886 ASSERT(ill != NULL);
1887
1888 hdr_length = IPH_HDR_LENGTH(ipha);
1889
1890 /*
1891 * We handle path MTU for source routed packets since the DCE
1892 * is looked up using the final destination.
1893 */
1894 dst = ip_get_dst(ipha);
1895
1896 dce = dce_lookup_and_add_v4(dst, ipst);
1897 if (dce == NULL) {
1898 /* Couldn't add a unique one - ENOMEM */
1899 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1900 ntohl(dst)));
1901 return;
1902 }
1903
1904 /* Check for MTU discovery advice as described in RFC 1191 */
1905 mtu = ntohs(icmph->icmph_du_mtu);
1906 orig_mtu = mtu;
1907 disable_pmtud = B_FALSE;
1908
1909 mutex_enter(&dce->dce_lock);
1910 if (dce->dce_flags & DCEF_PMTU)
1911 old_mtu = dce->dce_pmtu;
1912 else
1913 old_mtu = ill->ill_mtu;
1914
1915 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1916 uint32_t length;
1917 int i;
1918
1919 /*
1920 * Use the table from RFC 1191 to figure out
1921 * the next "plateau" based on the length in
1922 * the original IP packet.
1923 */
1924 length = ntohs(ipha->ipha_length);
1925 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1926 uint32_t, length);
1927 if (old_mtu <= length &&
1928 old_mtu >= length - hdr_length) {
1929 /*
1930 * Handle broken BSD 4.2 systems that
1931 * return the wrong ipha_length in ICMP
1932 * errors.
1933 */
1934 ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1935 length, old_mtu));
1936 length -= hdr_length;
1937 }
1938 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1939 if (length > icmp_frag_size_table[i])
1940 break;
1941 }
1942 if (i == A_CNT(icmp_frag_size_table)) {
1943 /* Smaller than IP_MIN_MTU! */
1944 ip1dbg(("Too big for packet size %d\n",
1945 length));
1946 disable_pmtud = B_TRUE;
1947 mtu = ipst->ips_ip_pmtu_min;
1948 } else {
1949 mtu = icmp_frag_size_table[i];
1950 ip1dbg(("Calculated mtu %d, packet size %d, "
1951 "before %d\n", mtu, length, old_mtu));
1952 if (mtu < ipst->ips_ip_pmtu_min) {
1953 mtu = ipst->ips_ip_pmtu_min;
1954 disable_pmtud = B_TRUE;
1955 }
1956 }
1957 }
1958 if (disable_pmtud)
1959 dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1960 else
1961 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1962
1963 dce->dce_pmtu = MIN(old_mtu, mtu);
1964 /* Prepare to send the new max frag size for the ULP. */
1965 icmph->icmph_du_zero = 0;
1966 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu);
1967 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1968 dce, int, orig_mtu, int, mtu);
1969
1970 /* We now have a PMTU for sure */
1971 dce->dce_flags |= DCEF_PMTU;
1972 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1973 mutex_exit(&dce->dce_lock);
1974 /*
1975 * After dropping the lock the new value is visible to everyone.
1976 * Then we bump the generation number so any cached values reinspect
1977 * the dce_t.
1978 */
1979 dce_increment_generation(dce);
1980 dce_refrele(dce);
1981 }
1982
1983 /*
1984 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1985 * calls this function.
1986 */
1987 static mblk_t *
icmp_inbound_self_encap_error_v4(mblk_t * mp,ipha_t * ipha,ipha_t * in_ipha)1988 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1989 {
1990 int length;
1991
1992 ASSERT(mp->b_datap->db_type == M_DATA);
1993
1994 /* icmp_inbound_v4 has already pulled up the whole error packet */
1995 ASSERT(mp->b_cont == NULL);
1996
1997 /*
1998 * The length that we want to overlay is the inner header
1999 * and what follows it.
2000 */
2001 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
2002
2003 /*
2004 * Overlay the inner header and whatever follows it over the
2005 * outer header.
2006 */
2007 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2008
2009 /* Adjust for what we removed */
2010 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
2011 return (mp);
2012 }
2013
2014 /*
2015 * Try to pass the ICMP message upstream in case the ULP cares.
2016 *
2017 * If the packet that caused the ICMP error is secure, we send
2018 * it to AH/ESP to make sure that the attached packet has a
2019 * valid association. ipha in the code below points to the
2020 * IP header of the packet that caused the error.
2021 *
2022 * For IPsec cases, we let the next-layer-up (which has access to
2023 * cached policy on the conn_t, or can query the SPD directly)
2024 * subtract out any IPsec overhead if they must. We therefore make no
2025 * adjustments here for IPsec overhead.
2026 *
2027 * IFN could have been generated locally or by some router.
2028 *
2029 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
2030 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
2031 * This happens because IP adjusted its value of MTU on an
2032 * earlier IFN message and could not tell the upper layer,
2033 * the new adjusted value of MTU e.g. Packet was encrypted
2034 * or there was not enough information to fanout to upper
2035 * layers. Thus on the next outbound datagram, ire_send_wire
2036 * generates the IFN, where IPsec processing has *not* been
2037 * done.
2038 *
2039 * Note that we retain ixa_fragsize across IPsec thus once
2040 * we have picking ixa_fragsize and entered ipsec_out_process we do
2041 * no change the fragsize even if the path MTU changes before
2042 * we reach ip_output_post_ipsec.
2043 *
2044 * In the local case, IRAF_LOOPBACK will be set indicating
2045 * that IFN was generated locally.
2046 *
2047 * ROUTER : IFN could be secure or non-secure.
2048 *
2049 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2050 * packet in error has AH/ESP headers to validate the AH/ESP
2051 * headers. AH/ESP will verify whether there is a valid SA or
2052 * not and send it back. We will fanout again if we have more
2053 * data in the packet.
2054 *
2055 * If the packet in error does not have AH/ESP, we handle it
2056 * like any other case.
2057 *
2058 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it
2059 * up to AH/ESP for validation. AH/ESP will verify whether there is a
2060 * valid SA or not and send it back. We will fanout again if
2061 * we have more data in the packet.
2062 *
2063 * If the packet in error does not have AH/ESP, we handle it
2064 * like any other case.
2065 *
2066 * The caller must have called icmp_inbound_verify_v4.
2067 */
2068 static void
icmp_inbound_error_fanout_v4(mblk_t * mp,icmph_t * icmph,ip_recv_attr_t * ira)2069 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2070 {
2071 uint16_t *up; /* Pointer to ports in ULP header */
2072 uint32_t ports; /* reversed ports for fanout */
2073 ipha_t ripha; /* With reversed addresses */
2074 ipha_t *ipha; /* Inner IP header */
2075 uint_t hdr_length; /* Inner IP header length */
2076 tcpha_t *tcpha;
2077 conn_t *connp;
2078 ill_t *ill = ira->ira_ill;
2079 ip_stack_t *ipst = ill->ill_ipst;
2080 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2081 ill_t *rill = ira->ira_rill;
2082
2083 /* Caller already pulled up everything. */
2084 ipha = (ipha_t *)&icmph[1];
2085 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2086 ASSERT(mp->b_cont == NULL);
2087
2088 hdr_length = IPH_HDR_LENGTH(ipha);
2089 ira->ira_protocol = ipha->ipha_protocol;
2090
2091 /*
2092 * We need a separate IP header with the source and destination
2093 * addresses reversed to do fanout/classification because the ipha in
2094 * the ICMP error is in the form we sent it out.
2095 */
2096 ripha.ipha_src = ipha->ipha_dst;
2097 ripha.ipha_dst = ipha->ipha_src;
2098 ripha.ipha_protocol = ipha->ipha_protocol;
2099 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2100
2101 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2102 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2103 ntohl(ipha->ipha_dst),
2104 icmph->icmph_type, icmph->icmph_code));
2105
2106 switch (ipha->ipha_protocol) {
2107 case IPPROTO_UDP:
2108 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2109
2110 /* Attempt to find a client stream based on port. */
2111 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2112 ntohs(up[0]), ntohs(up[1])));
2113
2114 /* Note that we send error to all matches. */
2115 ira->ira_flags |= IRAF_ICMP_ERROR;
2116 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2117 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2118 return;
2119
2120 case IPPROTO_TCP:
2121 /*
2122 * Find a TCP client stream for this packet.
2123 * Note that we do a reverse lookup since the header is
2124 * in the form we sent it out.
2125 */
2126 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2127 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2128 ipst);
2129 if (connp == NULL)
2130 goto discard_pkt;
2131
2132 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2133 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2134 mp = ipsec_check_inbound_policy(mp, connp,
2135 ipha, NULL, ira);
2136 if (mp == NULL) {
2137 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2138 /* Note that mp is NULL */
2139 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2140 CONN_DEC_REF(connp);
2141 return;
2142 }
2143 }
2144
2145 ira->ira_flags |= IRAF_ICMP_ERROR;
2146 ira->ira_ill = ira->ira_rill = NULL;
2147 if (IPCL_IS_TCP(connp)) {
2148 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2149 connp->conn_recvicmp, connp, ira, SQ_FILL,
2150 SQTAG_TCP_INPUT_ICMP_ERR);
2151 } else {
2152 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2153 (connp->conn_recv)(connp, mp, NULL, ira);
2154 CONN_DEC_REF(connp);
2155 }
2156 ira->ira_ill = ill;
2157 ira->ira_rill = rill;
2158 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2159 return;
2160
2161 case IPPROTO_SCTP:
2162 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2163 /* Find a SCTP client stream for this packet. */
2164 ((uint16_t *)&ports)[0] = up[1];
2165 ((uint16_t *)&ports)[1] = up[0];
2166
2167 ira->ira_flags |= IRAF_ICMP_ERROR;
2168 ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2169 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2170 return;
2171
2172 case IPPROTO_ESP:
2173 case IPPROTO_AH:
2174 if (!ipsec_loaded(ipss)) {
2175 ip_proto_not_sup(mp, ira);
2176 return;
2177 }
2178
2179 if (ipha->ipha_protocol == IPPROTO_ESP)
2180 mp = ipsecesp_icmp_error(mp, ira);
2181 else
2182 mp = ipsecah_icmp_error(mp, ira);
2183 if (mp == NULL)
2184 return;
2185
2186 /* Just in case ipsec didn't preserve the NULL b_cont */
2187 if (mp->b_cont != NULL) {
2188 if (!pullupmsg(mp, -1))
2189 goto discard_pkt;
2190 }
2191
2192 /*
2193 * Note that ira_pktlen and ira_ip_hdr_length are no longer
2194 * correct, but we don't use them any more here.
2195 *
2196 * If succesful, the mp has been modified to not include
2197 * the ESP/AH header so we can fanout to the ULP's icmp
2198 * error handler.
2199 */
2200 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2201 goto truncated;
2202
2203 /* Verify the modified message before any further processes. */
2204 ipha = (ipha_t *)mp->b_rptr;
2205 hdr_length = IPH_HDR_LENGTH(ipha);
2206 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2207 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2208 freemsg(mp);
2209 return;
2210 }
2211
2212 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2213 return;
2214
2215 case IPPROTO_ENCAP: {
2216 /* Look for self-encapsulated packets that caused an error */
2217 ipha_t *in_ipha;
2218
2219 /*
2220 * Caller has verified that length has to be
2221 * at least the size of IP header.
2222 */
2223 ASSERT(hdr_length >= sizeof (ipha_t));
2224 /*
2225 * Check the sanity of the inner IP header like
2226 * we did for the outer header.
2227 */
2228 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2229 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2230 goto discard_pkt;
2231 }
2232 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2233 goto discard_pkt;
2234 }
2235 /* Check for Self-encapsulated tunnels */
2236 if (in_ipha->ipha_src == ipha->ipha_src &&
2237 in_ipha->ipha_dst == ipha->ipha_dst) {
2238
2239 mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2240 in_ipha);
2241 if (mp == NULL)
2242 goto discard_pkt;
2243
2244 /*
2245 * Just in case self_encap didn't preserve the NULL
2246 * b_cont
2247 */
2248 if (mp->b_cont != NULL) {
2249 if (!pullupmsg(mp, -1))
2250 goto discard_pkt;
2251 }
2252 /*
2253 * Note that ira_pktlen and ira_ip_hdr_length are no
2254 * longer correct, but we don't use them any more here.
2255 */
2256 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2257 goto truncated;
2258
2259 /*
2260 * Verify the modified message before any further
2261 * processes.
2262 */
2263 ipha = (ipha_t *)mp->b_rptr;
2264 hdr_length = IPH_HDR_LENGTH(ipha);
2265 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2266 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2267 freemsg(mp);
2268 return;
2269 }
2270
2271 /*
2272 * The packet in error is self-encapsualted.
2273 * And we are finding it further encapsulated
2274 * which we could not have possibly generated.
2275 */
2276 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2277 goto discard_pkt;
2278 }
2279 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2280 return;
2281 }
2282 /* No self-encapsulated */
2283 }
2284 /* FALLTHROUGH */
2285 case IPPROTO_IPV6:
2286 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2287 &ripha.ipha_dst, ipst)) != NULL) {
2288 ira->ira_flags |= IRAF_ICMP_ERROR;
2289 connp->conn_recvicmp(connp, mp, NULL, ira);
2290 CONN_DEC_REF(connp);
2291 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2292 return;
2293 }
2294 /*
2295 * No IP tunnel is interested, fallthrough and see
2296 * if a raw socket will want it.
2297 */
2298 /* FALLTHROUGH */
2299 default:
2300 ira->ira_flags |= IRAF_ICMP_ERROR;
2301 ip_fanout_proto_v4(mp, &ripha, ira);
2302 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2303 return;
2304 }
2305 /* NOTREACHED */
2306 discard_pkt:
2307 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2308 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2309 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2310 freemsg(mp);
2311 return;
2312
2313 truncated:
2314 /* We pulled up everthing already. Must be truncated */
2315 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2316 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2317 freemsg(mp);
2318 }
2319
2320 /*
2321 * Common IP options parser.
2322 *
2323 * Setup routine: fill in *optp with options-parsing state, then
2324 * tail-call ipoptp_next to return the first option.
2325 */
2326 uint8_t
ipoptp_first(ipoptp_t * optp,ipha_t * ipha)2327 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2328 {
2329 uint32_t totallen; /* total length of all options */
2330
2331 totallen = ipha->ipha_version_and_hdr_length -
2332 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2333 totallen <<= 2;
2334 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2335 optp->ipoptp_end = optp->ipoptp_next + totallen;
2336 optp->ipoptp_flags = 0;
2337 return (ipoptp_next(optp));
2338 }
2339
2340 /* Like above but without an ipha_t */
2341 uint8_t
ipoptp_first2(ipoptp_t * optp,uint32_t totallen,uint8_t * opt)2342 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2343 {
2344 optp->ipoptp_next = opt;
2345 optp->ipoptp_end = optp->ipoptp_next + totallen;
2346 optp->ipoptp_flags = 0;
2347 return (ipoptp_next(optp));
2348 }
2349
2350 /*
2351 * Common IP options parser: extract next option.
2352 */
2353 uint8_t
ipoptp_next(ipoptp_t * optp)2354 ipoptp_next(ipoptp_t *optp)
2355 {
2356 uint8_t *end = optp->ipoptp_end;
2357 uint8_t *cur = optp->ipoptp_next;
2358 uint8_t opt, len, pointer;
2359
2360 /*
2361 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2362 * has been corrupted.
2363 */
2364 ASSERT(cur <= end);
2365
2366 if (cur == end)
2367 return (IPOPT_EOL);
2368
2369 opt = cur[IPOPT_OPTVAL];
2370
2371 /*
2372 * Skip any NOP options.
2373 */
2374 while (opt == IPOPT_NOP) {
2375 cur++;
2376 if (cur == end)
2377 return (IPOPT_EOL);
2378 opt = cur[IPOPT_OPTVAL];
2379 }
2380
2381 if (opt == IPOPT_EOL)
2382 return (IPOPT_EOL);
2383
2384 /*
2385 * Option requiring a length.
2386 */
2387 if ((cur + 1) >= end) {
2388 optp->ipoptp_flags |= IPOPTP_ERROR;
2389 return (IPOPT_EOL);
2390 }
2391 len = cur[IPOPT_OLEN];
2392 if (len < 2) {
2393 optp->ipoptp_flags |= IPOPTP_ERROR;
2394 return (IPOPT_EOL);
2395 }
2396 optp->ipoptp_cur = cur;
2397 optp->ipoptp_len = len;
2398 optp->ipoptp_next = cur + len;
2399 if (cur + len > end) {
2400 optp->ipoptp_flags |= IPOPTP_ERROR;
2401 return (IPOPT_EOL);
2402 }
2403
2404 /*
2405 * For the options which require a pointer field, make sure
2406 * its there, and make sure it points to either something
2407 * inside this option, or the end of the option.
2408 */
2409 pointer = IPOPT_EOL;
2410 switch (opt) {
2411 case IPOPT_RR:
2412 case IPOPT_TS:
2413 case IPOPT_LSRR:
2414 case IPOPT_SSRR:
2415 if (len <= IPOPT_OFFSET) {
2416 optp->ipoptp_flags |= IPOPTP_ERROR;
2417 return (opt);
2418 }
2419 pointer = cur[IPOPT_OFFSET];
2420 if (pointer - 1 > len) {
2421 optp->ipoptp_flags |= IPOPTP_ERROR;
2422 return (opt);
2423 }
2424 break;
2425 }
2426
2427 /*
2428 * Sanity check the pointer field based on the type of the
2429 * option.
2430 */
2431 switch (opt) {
2432 case IPOPT_RR:
2433 case IPOPT_SSRR:
2434 case IPOPT_LSRR:
2435 if (pointer < IPOPT_MINOFF_SR)
2436 optp->ipoptp_flags |= IPOPTP_ERROR;
2437 break;
2438 case IPOPT_TS:
2439 if (pointer < IPOPT_MINOFF_IT)
2440 optp->ipoptp_flags |= IPOPTP_ERROR;
2441 /*
2442 * Note that the Internet Timestamp option also
2443 * contains two four bit fields (the Overflow field,
2444 * and the Flag field), which follow the pointer
2445 * field. We don't need to check that these fields
2446 * fall within the length of the option because this
2447 * was implicitely done above. We've checked that the
2448 * pointer value is at least IPOPT_MINOFF_IT, and that
2449 * it falls within the option. Since IPOPT_MINOFF_IT >
2450 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2451 */
2452 ASSERT(len > IPOPT_POS_OV_FLG);
2453 break;
2454 }
2455
2456 return (opt);
2457 }
2458
2459 /*
2460 * Use the outgoing IP header to create an IP_OPTIONS option the way
2461 * it was passed down from the application.
2462 *
2463 * This is compatible with BSD in that it returns
2464 * the reverse source route with the final destination
2465 * as the last entry. The first 4 bytes of the option
2466 * will contain the final destination.
2467 */
2468 int
ip_opt_get_user(conn_t * connp,uchar_t * buf)2469 ip_opt_get_user(conn_t *connp, uchar_t *buf)
2470 {
2471 ipoptp_t opts;
2472 uchar_t *opt;
2473 uint8_t optval;
2474 uint8_t optlen;
2475 uint32_t len = 0;
2476 uchar_t *buf1 = buf;
2477 uint32_t totallen;
2478 ipaddr_t dst;
2479 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
2480
2481 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2482 return (0);
2483
2484 totallen = ipp->ipp_ipv4_options_len;
2485 if (totallen & 0x3)
2486 return (0);
2487
2488 buf += IP_ADDR_LEN; /* Leave room for final destination */
2489 len += IP_ADDR_LEN;
2490 bzero(buf1, IP_ADDR_LEN);
2491
2492 dst = connp->conn_faddr_v4;
2493
2494 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2495 optval != IPOPT_EOL;
2496 optval = ipoptp_next(&opts)) {
2497 int off;
2498
2499 opt = opts.ipoptp_cur;
2500 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2501 break;
2502 }
2503 optlen = opts.ipoptp_len;
2504
2505 switch (optval) {
2506 case IPOPT_SSRR:
2507 case IPOPT_LSRR:
2508
2509 /*
2510 * Insert destination as the first entry in the source
2511 * route and move down the entries on step.
2512 * The last entry gets placed at buf1.
2513 */
2514 buf[IPOPT_OPTVAL] = optval;
2515 buf[IPOPT_OLEN] = optlen;
2516 buf[IPOPT_OFFSET] = optlen;
2517
2518 off = optlen - IP_ADDR_LEN;
2519 if (off < 0) {
2520 /* No entries in source route */
2521 break;
2522 }
2523 /* Last entry in source route if not already set */
2524 if (dst == INADDR_ANY)
2525 bcopy(opt + off, buf1, IP_ADDR_LEN);
2526 off -= IP_ADDR_LEN;
2527
2528 while (off > 0) {
2529 bcopy(opt + off,
2530 buf + off + IP_ADDR_LEN,
2531 IP_ADDR_LEN);
2532 off -= IP_ADDR_LEN;
2533 }
2534 /* ipha_dst into first slot */
2535 bcopy(&dst, buf + off + IP_ADDR_LEN,
2536 IP_ADDR_LEN);
2537 buf += optlen;
2538 len += optlen;
2539 break;
2540
2541 default:
2542 bcopy(opt, buf, optlen);
2543 buf += optlen;
2544 len += optlen;
2545 break;
2546 }
2547 }
2548 done:
2549 /* Pad the resulting options */
2550 while (len & 0x3) {
2551 *buf++ = IPOPT_EOL;
2552 len++;
2553 }
2554 return (len);
2555 }
2556
2557 /*
2558 * Update any record route or timestamp options to include this host.
2559 * Reverse any source route option.
2560 * This routine assumes that the options are well formed i.e. that they
2561 * have already been checked.
2562 */
2563 static void
icmp_options_update(ipha_t * ipha)2564 icmp_options_update(ipha_t *ipha)
2565 {
2566 ipoptp_t opts;
2567 uchar_t *opt;
2568 uint8_t optval;
2569 ipaddr_t src; /* Our local address */
2570 ipaddr_t dst;
2571
2572 ip2dbg(("icmp_options_update\n"));
2573 src = ipha->ipha_src;
2574 dst = ipha->ipha_dst;
2575
2576 for (optval = ipoptp_first(&opts, ipha);
2577 optval != IPOPT_EOL;
2578 optval = ipoptp_next(&opts)) {
2579 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2580 opt = opts.ipoptp_cur;
2581 ip2dbg(("icmp_options_update: opt %d, len %d\n",
2582 optval, opts.ipoptp_len));
2583 switch (optval) {
2584 int off1, off2;
2585 case IPOPT_SSRR:
2586 case IPOPT_LSRR:
2587 /*
2588 * Reverse the source route. The first entry
2589 * should be the next to last one in the current
2590 * source route (the last entry is our address).
2591 * The last entry should be the final destination.
2592 */
2593 off1 = IPOPT_MINOFF_SR - 1;
2594 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2595 if (off2 < 0) {
2596 /* No entries in source route */
2597 ip1dbg((
2598 "icmp_options_update: bad src route\n"));
2599 break;
2600 }
2601 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2602 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2603 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2604 off2 -= IP_ADDR_LEN;
2605
2606 while (off1 < off2) {
2607 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2608 bcopy((char *)opt + off2, (char *)opt + off1,
2609 IP_ADDR_LEN);
2610 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2611 off1 += IP_ADDR_LEN;
2612 off2 -= IP_ADDR_LEN;
2613 }
2614 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2615 break;
2616 }
2617 }
2618 }
2619
2620 /*
2621 * Process received ICMP Redirect messages.
2622 * Assumes the caller has verified that the headers are in the pulled up mblk.
2623 * Consumes mp.
2624 */
2625 static void
icmp_redirect_v4(mblk_t * mp,ipha_t * ipha,icmph_t * icmph,ip_recv_attr_t * ira)2626 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2627 {
2628 ire_t *ire, *nire;
2629 ire_t *prev_ire;
2630 ipaddr_t src, dst, gateway;
2631 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2632 ipha_t *inner_ipha; /* Inner IP header */
2633
2634 /* Caller already pulled up everything. */
2635 inner_ipha = (ipha_t *)&icmph[1];
2636 src = ipha->ipha_src;
2637 dst = inner_ipha->ipha_dst;
2638 gateway = icmph->icmph_rd_gateway;
2639 /* Make sure the new gateway is reachable somehow. */
2640 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2641 ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
2642 /*
2643 * Make sure we had a route for the dest in question and that
2644 * that route was pointing to the old gateway (the source of the
2645 * redirect packet.)
2646 * We do longest match and then compare ire_gateway_addr below.
2647 */
2648 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2649 NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
2650 /*
2651 * Check that
2652 * the redirect was not from ourselves
2653 * the new gateway and the old gateway are directly reachable
2654 */
2655 if (prev_ire == NULL || ire == NULL ||
2656 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2657 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2658 !(ire->ire_type & IRE_IF_ALL) ||
2659 prev_ire->ire_gateway_addr != src) {
2660 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2661 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2662 freemsg(mp);
2663 if (ire != NULL)
2664 ire_refrele(ire);
2665 if (prev_ire != NULL)
2666 ire_refrele(prev_ire);
2667 return;
2668 }
2669
2670 ire_refrele(prev_ire);
2671 ire_refrele(ire);
2672
2673 /*
2674 * TODO: more precise handling for cases 0, 2, 3, the latter two
2675 * require TOS routing
2676 */
2677 switch (icmph->icmph_code) {
2678 case 0:
2679 case 1:
2680 /* TODO: TOS specificity for cases 2 and 3 */
2681 case 2:
2682 case 3:
2683 break;
2684 default:
2685 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2686 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2687 freemsg(mp);
2688 return;
2689 }
2690 /*
2691 * Create a Route Association. This will allow us to remember that
2692 * someone we believe told us to use the particular gateway.
2693 */
2694 ire = ire_create(
2695 (uchar_t *)&dst, /* dest addr */
2696 (uchar_t *)&ip_g_all_ones, /* mask */
2697 (uchar_t *)&gateway, /* gateway addr */
2698 IRE_HOST,
2699 NULL, /* ill */
2700 ALL_ZONES,
2701 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2702 NULL, /* tsol_gc_t */
2703 ipst);
2704
2705 if (ire == NULL) {
2706 freemsg(mp);
2707 return;
2708 }
2709 nire = ire_add(ire);
2710 /* Check if it was a duplicate entry */
2711 if (nire != NULL && nire != ire) {
2712 ASSERT(nire->ire_identical_ref > 1);
2713 ire_delete(nire);
2714 ire_refrele(nire);
2715 nire = NULL;
2716 }
2717 ire = nire;
2718 if (ire != NULL) {
2719 ire_refrele(ire); /* Held in ire_add */
2720
2721 /* tell routing sockets that we received a redirect */
2722 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2723 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2724 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2725 }
2726
2727 /*
2728 * Delete any existing IRE_HOST type redirect ires for this destination.
2729 * This together with the added IRE has the effect of
2730 * modifying an existing redirect.
2731 */
2732 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2733 ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2734 if (prev_ire != NULL) {
2735 if (prev_ire ->ire_flags & RTF_DYNAMIC)
2736 ire_delete(prev_ire);
2737 ire_refrele(prev_ire);
2738 }
2739
2740 freemsg(mp);
2741 }
2742
2743 /*
2744 * Generate an ICMP parameter problem message.
2745 * When called from ip_output side a minimal ip_recv_attr_t needs to be
2746 * constructed by the caller.
2747 */
2748 static void
icmp_param_problem(mblk_t * mp,uint8_t ptr,ip_recv_attr_t * ira)2749 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2750 {
2751 icmph_t icmph;
2752 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2753
2754 mp = icmp_pkt_err_ok(mp, ira);
2755 if (mp == NULL)
2756 return;
2757
2758 bzero(&icmph, sizeof (icmph_t));
2759 icmph.icmph_type = ICMP_PARAM_PROBLEM;
2760 icmph.icmph_pp_ptr = ptr;
2761 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2762 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2763 }
2764
2765 /*
2766 * Build and ship an IPv4 ICMP message using the packet data in mp, and
2767 * the ICMP header pointed to by "stuff". (May be called as writer.)
2768 * Note: assumes that icmp_pkt_err_ok has been called to verify that
2769 * an icmp error packet can be sent.
2770 * Assigns an appropriate source address to the packet. If ipha_dst is
2771 * one of our addresses use it for source. Otherwise let ip_output_simple
2772 * pick the source address.
2773 */
2774 static void
icmp_pkt(mblk_t * mp,void * stuff,size_t len,ip_recv_attr_t * ira)2775 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2776 {
2777 ipaddr_t dst;
2778 icmph_t *icmph;
2779 ipha_t *ipha;
2780 uint_t len_needed;
2781 size_t msg_len;
2782 mblk_t *mp1;
2783 ipaddr_t src;
2784 ire_t *ire;
2785 ip_xmit_attr_t ixas;
2786 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2787
2788 ipha = (ipha_t *)mp->b_rptr;
2789
2790 bzero(&ixas, sizeof (ixas));
2791 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2792 ixas.ixa_zoneid = ira->ira_zoneid;
2793 ixas.ixa_ifindex = 0;
2794 ixas.ixa_ipst = ipst;
2795 ixas.ixa_cred = kcred;
2796 ixas.ixa_cpid = NOPID;
2797 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
2798 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2799
2800 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2801 /*
2802 * Apply IPsec based on how IPsec was applied to
2803 * the packet that had the error.
2804 *
2805 * If it was an outbound packet that caused the ICMP
2806 * error, then the caller will have setup the IRA
2807 * appropriately.
2808 */
2809 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2810 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2811 /* Note: mp already consumed and ip_drop_packet done */
2812 return;
2813 }
2814 } else {
2815 /*
2816 * This is in clear. The icmp message we are building
2817 * here should go out in clear, independent of our policy.
2818 */
2819 ixas.ixa_flags |= IXAF_NO_IPSEC;
2820 }
2821
2822 /* Remember our eventual destination */
2823 dst = ipha->ipha_src;
2824
2825 /*
2826 * If the packet was for one of our unicast addresses, make
2827 * sure we respond with that as the source. Otherwise
2828 * have ip_output_simple pick the source address.
2829 */
2830 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2831 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL,
2832 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2833 if (ire != NULL) {
2834 ire_refrele(ire);
2835 src = ipha->ipha_dst;
2836 } else {
2837 src = INADDR_ANY;
2838 ixas.ixa_flags |= IXAF_SET_SOURCE;
2839 }
2840
2841 /*
2842 * Check if we can send back more then 8 bytes in addition to
2843 * the IP header. We try to send 64 bytes of data and the internal
2844 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2845 */
2846 len_needed = IPH_HDR_LENGTH(ipha);
2847 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2848 ipha->ipha_protocol == IPPROTO_IPV6) {
2849 /*
2850 * NOTE: It is posssible that the inner packet is poorly
2851 * formed (e.g. IP version is corrupt, or v6 extension headers
2852 * got cut off). The receiver of the ICMP message should see
2853 * what we saw. In the absence of a sane inner-packet (which
2854 * protocol types IPPPROTO_ENCAP and IPPROTO_IPV6 indicate
2855 * would be an IP header), we should send the size of what is
2856 * normally expected to be there (either sizeof (ipha_t) or
2857 * sizeof (ip6_t). It may be useful for diagnostic purposes.
2858 *
2859 * ALSO NOTE: "inner_ip6h" is the inner packet header, v4 or v6.
2860 */
2861 ip6_t *inner_ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2862
2863 if (!pullupmsg(mp, -1)) {
2864 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2865 ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2866 freemsg(mp);
2867 return;
2868 }
2869 ipha = (ipha_t *)mp->b_rptr;
2870
2871 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2872 /*
2873 * Check the inner IP version here to guard against
2874 * bogons.
2875 */
2876 if (IPH_HDR_VERSION(inner_ip6h) == IPV4_VERSION) {
2877 len_needed +=
2878 IPH_HDR_LENGTH(((uchar_t *)inner_ip6h));
2879 } else {
2880 len_needed = sizeof (ipha_t);
2881 }
2882 } else {
2883 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2884 /* function called next-line checks inner IP version */
2885 len_needed += ip_hdr_length_v6(mp, inner_ip6h);
2886 }
2887 }
2888 len_needed += ipst->ips_ip_icmp_return;
2889 msg_len = msgdsize(mp);
2890 if (msg_len > len_needed) {
2891 (void) adjmsg(mp, len_needed - msg_len);
2892 msg_len = len_needed;
2893 }
2894 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2895 if (mp1 == NULL) {
2896 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2897 freemsg(mp);
2898 return;
2899 }
2900 mp1->b_cont = mp;
2901 mp = mp1;
2902
2903 /*
2904 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2905 * node generates be accepted in peace by all on-host destinations.
2906 * If we do NOT assume that all on-host destinations trust
2907 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2908 * (Look for IXAF_TRUSTED_ICMP).
2909 */
2910 ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2911
2912 ipha = (ipha_t *)mp->b_rptr;
2913 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2914 *ipha = icmp_ipha;
2915 ipha->ipha_src = src;
2916 ipha->ipha_dst = dst;
2917 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2918 msg_len += sizeof (icmp_ipha) + len;
2919 if (msg_len > IP_MAXPACKET) {
2920 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2921 msg_len = IP_MAXPACKET;
2922 }
2923 ipha->ipha_length = htons((uint16_t)msg_len);
2924 icmph = (icmph_t *)&ipha[1];
2925 bcopy(stuff, icmph, len);
2926 icmph->icmph_checksum = 0;
2927 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2928 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2929
2930 (void) ip_output_simple(mp, &ixas);
2931 ixa_cleanup(&ixas);
2932 }
2933
2934 /*
2935 * Determine if an ICMP error packet can be sent given the rate limit.
2936 * The limit consists of an average frequency (icmp_pkt_err_interval measured
2937 * in milliseconds) and a burst size. Burst size number of packets can
2938 * be sent arbitrarely closely spaced.
2939 * The state is tracked using two variables to implement an approximate
2940 * token bucket filter:
2941 * icmp_pkt_err_last - lbolt value when the last burst started
2942 * icmp_pkt_err_sent - number of packets sent in current burst
2943 */
2944 boolean_t
icmp_err_rate_limit(ip_stack_t * ipst)2945 icmp_err_rate_limit(ip_stack_t *ipst)
2946 {
2947 clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2948 uint_t refilled; /* Number of packets refilled in tbf since last */
2949 /* Guard against changes by loading into local variable */
2950 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2951
2952 if (err_interval == 0)
2953 return (B_FALSE);
2954
2955 if (ipst->ips_icmp_pkt_err_last > now) {
2956 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2957 ipst->ips_icmp_pkt_err_last = 0;
2958 ipst->ips_icmp_pkt_err_sent = 0;
2959 }
2960 /*
2961 * If we are in a burst update the token bucket filter.
2962 * Update the "last" time to be close to "now" but make sure
2963 * we don't loose precision.
2964 */
2965 if (ipst->ips_icmp_pkt_err_sent != 0) {
2966 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2967 if (refilled > ipst->ips_icmp_pkt_err_sent) {
2968 ipst->ips_icmp_pkt_err_sent = 0;
2969 } else {
2970 ipst->ips_icmp_pkt_err_sent -= refilled;
2971 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2972 }
2973 }
2974 if (ipst->ips_icmp_pkt_err_sent == 0) {
2975 /* Start of new burst */
2976 ipst->ips_icmp_pkt_err_last = now;
2977 }
2978 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2979 ipst->ips_icmp_pkt_err_sent++;
2980 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2981 ipst->ips_icmp_pkt_err_sent));
2982 return (B_FALSE);
2983 }
2984 ip1dbg(("icmp_err_rate_limit: dropped\n"));
2985 return (B_TRUE);
2986 }
2987
2988 /*
2989 * Check if it is ok to send an IPv4 ICMP error packet in
2990 * response to the IPv4 packet in mp.
2991 * Free the message and return null if no
2992 * ICMP error packet should be sent.
2993 */
2994 static mblk_t *
icmp_pkt_err_ok(mblk_t * mp,ip_recv_attr_t * ira)2995 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2996 {
2997 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2998 icmph_t *icmph;
2999 ipha_t *ipha;
3000 uint_t len_needed;
3001
3002 if (!mp)
3003 return (NULL);
3004 ipha = (ipha_t *)mp->b_rptr;
3005 if (ip_csum_hdr(ipha)) {
3006 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
3007 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
3008 freemsg(mp);
3009 return (NULL);
3010 }
3011 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
3012 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
3013 CLASSD(ipha->ipha_dst) ||
3014 CLASSD(ipha->ipha_src) ||
3015 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
3016 /* Note: only errors to the fragment with offset 0 */
3017 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3018 freemsg(mp);
3019 return (NULL);
3020 }
3021 if (ipha->ipha_protocol == IPPROTO_ICMP) {
3022 /*
3023 * Check the ICMP type. RFC 1122 sez: don't send ICMP
3024 * errors in response to any ICMP errors.
3025 */
3026 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3027 if (mp->b_wptr - mp->b_rptr < len_needed) {
3028 if (!pullupmsg(mp, len_needed)) {
3029 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3030 freemsg(mp);
3031 return (NULL);
3032 }
3033 ipha = (ipha_t *)mp->b_rptr;
3034 }
3035 icmph = (icmph_t *)
3036 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3037 switch (icmph->icmph_type) {
3038 case ICMP_DEST_UNREACHABLE:
3039 case ICMP_SOURCE_QUENCH:
3040 case ICMP_TIME_EXCEEDED:
3041 case ICMP_PARAM_PROBLEM:
3042 case ICMP_REDIRECT:
3043 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3044 freemsg(mp);
3045 return (NULL);
3046 default:
3047 break;
3048 }
3049 }
3050 /*
3051 * If this is a labeled system, then check to see if we're allowed to
3052 * send a response to this particular sender. If not, then just drop.
3053 */
3054 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
3055 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3056 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3057 freemsg(mp);
3058 return (NULL);
3059 }
3060 if (icmp_err_rate_limit(ipst)) {
3061 /*
3062 * Only send ICMP error packets every so often.
3063 * This should be done on a per port/source basis,
3064 * but for now this will suffice.
3065 */
3066 freemsg(mp);
3067 return (NULL);
3068 }
3069 return (mp);
3070 }
3071
3072 /*
3073 * Called when a packet was sent out the same link that it arrived on.
3074 * Check if it is ok to send a redirect and then send it.
3075 */
3076 void
ip_send_potential_redirect_v4(mblk_t * mp,ipha_t * ipha,ire_t * ire,ip_recv_attr_t * ira)3077 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
3078 ip_recv_attr_t *ira)
3079 {
3080 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3081 ipaddr_t src, nhop;
3082 mblk_t *mp1;
3083 ire_t *nhop_ire;
3084
3085 /*
3086 * Check the source address to see if it originated
3087 * on the same logical subnet it is going back out on.
3088 * If so, we should be able to send it a redirect.
3089 * Avoid sending a redirect if the destination
3090 * is directly connected (i.e., we matched an IRE_ONLINK),
3091 * or if the packet was source routed out this interface.
3092 *
3093 * We avoid sending a redirect if the
3094 * destination is directly connected
3095 * because it is possible that multiple
3096 * IP subnets may have been configured on
3097 * the link, and the source may not
3098 * be on the same subnet as ip destination,
3099 * even though they are on the same
3100 * physical link.
3101 */
3102 if ((ire->ire_type & IRE_ONLINK) ||
3103 ip_source_routed(ipha, ipst))
3104 return;
3105
3106 nhop_ire = ire_nexthop(ire);
3107 if (nhop_ire == NULL)
3108 return;
3109
3110 nhop = nhop_ire->ire_addr;
3111
3112 if (nhop_ire->ire_type & IRE_IF_CLONE) {
3113 ire_t *ire2;
3114
3115 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3116 mutex_enter(&nhop_ire->ire_lock);
3117 ire2 = nhop_ire->ire_dep_parent;
3118 if (ire2 != NULL)
3119 ire_refhold(ire2);
3120 mutex_exit(&nhop_ire->ire_lock);
3121 ire_refrele(nhop_ire);
3122 nhop_ire = ire2;
3123 }
3124 if (nhop_ire == NULL)
3125 return;
3126
3127 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3128
3129 src = ipha->ipha_src;
3130
3131 /*
3132 * We look at the interface ire for the nexthop,
3133 * to see if ipha_src is in the same subnet
3134 * as the nexthop.
3135 */
3136 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3137 /*
3138 * The source is directly connected.
3139 */
3140 mp1 = copymsg(mp);
3141 if (mp1 != NULL) {
3142 icmp_send_redirect(mp1, nhop, ira);
3143 }
3144 }
3145 ire_refrele(nhop_ire);
3146 }
3147
3148 /*
3149 * Generate an ICMP redirect message.
3150 */
3151 static void
icmp_send_redirect(mblk_t * mp,ipaddr_t gateway,ip_recv_attr_t * ira)3152 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3153 {
3154 icmph_t icmph;
3155 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3156
3157 mp = icmp_pkt_err_ok(mp, ira);
3158 if (mp == NULL)
3159 return;
3160
3161 bzero(&icmph, sizeof (icmph_t));
3162 icmph.icmph_type = ICMP_REDIRECT;
3163 icmph.icmph_code = 1;
3164 icmph.icmph_rd_gateway = gateway;
3165 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3166 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3167 }
3168
3169 /*
3170 * Generate an ICMP time exceeded message.
3171 */
3172 void
icmp_time_exceeded(mblk_t * mp,uint8_t code,ip_recv_attr_t * ira)3173 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3174 {
3175 icmph_t icmph;
3176 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3177
3178 mp = icmp_pkt_err_ok(mp, ira);
3179 if (mp == NULL)
3180 return;
3181
3182 bzero(&icmph, sizeof (icmph_t));
3183 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3184 icmph.icmph_code = code;
3185 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3186 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3187 }
3188
3189 /*
3190 * Generate an ICMP unreachable message.
3191 * When called from ip_output side a minimal ip_recv_attr_t needs to be
3192 * constructed by the caller.
3193 */
3194 void
icmp_unreachable(mblk_t * mp,uint8_t code,ip_recv_attr_t * ira)3195 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3196 {
3197 icmph_t icmph;
3198 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3199
3200 mp = icmp_pkt_err_ok(mp, ira);
3201 if (mp == NULL)
3202 return;
3203
3204 bzero(&icmph, sizeof (icmph_t));
3205 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3206 icmph.icmph_code = code;
3207 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3208 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3209 }
3210
3211 /*
3212 * Latch in the IPsec state for a stream based the policy in the listener
3213 * and the actions in the ip_recv_attr_t.
3214 * Called directly from TCP and SCTP.
3215 */
3216 boolean_t
ip_ipsec_policy_inherit(conn_t * connp,conn_t * lconnp,ip_recv_attr_t * ira)3217 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3218 {
3219 ASSERT(lconnp->conn_policy != NULL);
3220 ASSERT(connp->conn_policy == NULL);
3221
3222 IPPH_REFHOLD(lconnp->conn_policy);
3223 connp->conn_policy = lconnp->conn_policy;
3224
3225 if (ira->ira_ipsec_action != NULL) {
3226 if (connp->conn_latch == NULL) {
3227 connp->conn_latch = iplatch_create();
3228 if (connp->conn_latch == NULL)
3229 return (B_FALSE);
3230 }
3231 ipsec_latch_inbound(connp, ira);
3232 }
3233 return (B_TRUE);
3234 }
3235
3236 /*
3237 * Verify whether or not the IP address is a valid local address.
3238 * Could be a unicast, including one for a down interface.
3239 * If allow_mcbc then a multicast or broadcast address is also
3240 * acceptable.
3241 *
3242 * In the case of a broadcast/multicast address, however, the
3243 * upper protocol is expected to reset the src address
3244 * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3245 * no packets are emitted with broadcast/multicast address as
3246 * source address (that violates hosts requirements RFC 1122)
3247 * The addresses valid for bind are:
3248 * (1) - INADDR_ANY (0)
3249 * (2) - IP address of an UP interface
3250 * (3) - IP address of a DOWN interface
3251 * (4) - valid local IP broadcast addresses. In this case
3252 * the conn will only receive packets destined to
3253 * the specified broadcast address.
3254 * (5) - a multicast address. In this case
3255 * the conn will only receive packets destined to
3256 * the specified multicast address. Note: the
3257 * application still has to issue an
3258 * IP_ADD_MEMBERSHIP socket option.
3259 *
3260 * In all the above cases, the bound address must be valid in the current zone.
3261 * When the address is loopback, multicast or broadcast, there might be many
3262 * matching IREs so bind has to look up based on the zone.
3263 */
3264 ip_laddr_t
ip_laddr_verify_v4(ipaddr_t src_addr,zoneid_t zoneid,ip_stack_t * ipst,boolean_t allow_mcbc)3265 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3266 ip_stack_t *ipst, boolean_t allow_mcbc)
3267 {
3268 ire_t *src_ire;
3269
3270 ASSERT(src_addr != INADDR_ANY);
3271
3272 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3273 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3274
3275 /*
3276 * If an address other than in6addr_any is requested,
3277 * we verify that it is a valid address for bind
3278 * Note: Following code is in if-else-if form for
3279 * readability compared to a condition check.
3280 */
3281 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3282 /*
3283 * (2) Bind to address of local UP interface
3284 */
3285 ire_refrele(src_ire);
3286 return (IPVL_UNICAST_UP);
3287 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3288 /*
3289 * (4) Bind to broadcast address
3290 */
3291 ire_refrele(src_ire);
3292 if (allow_mcbc)
3293 return (IPVL_BCAST);
3294 else
3295 return (IPVL_BAD);
3296 } else if (CLASSD(src_addr)) {
3297 /* (5) bind to multicast address. */
3298 if (src_ire != NULL)
3299 ire_refrele(src_ire);
3300
3301 if (allow_mcbc)
3302 return (IPVL_MCAST);
3303 else
3304 return (IPVL_BAD);
3305 } else {
3306 ipif_t *ipif;
3307
3308 /*
3309 * (3) Bind to address of local DOWN interface?
3310 * (ipif_lookup_addr() looks up all interfaces
3311 * but we do not get here for UP interfaces
3312 * - case (2) above)
3313 */
3314 if (src_ire != NULL)
3315 ire_refrele(src_ire);
3316
3317 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3318 if (ipif == NULL)
3319 return (IPVL_BAD);
3320
3321 /* Not a useful source? */
3322 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3323 ipif_refrele(ipif);
3324 return (IPVL_BAD);
3325 }
3326 ipif_refrele(ipif);
3327 return (IPVL_UNICAST_DOWN);
3328 }
3329 }
3330
3331 /*
3332 * Insert in the bind fanout for IPv4 and IPv6.
3333 * The caller should already have used ip_laddr_verify_v*() before calling
3334 * this.
3335 */
3336 int
ip_laddr_fanout_insert(conn_t * connp)3337 ip_laddr_fanout_insert(conn_t *connp)
3338 {
3339 int error;
3340
3341 /*
3342 * Allow setting new policies. For example, disconnects result
3343 * in us being called. As we would have set conn_policy_cached
3344 * to B_TRUE before, we should set it to B_FALSE, so that policy
3345 * can change after the disconnect.
3346 */
3347 connp->conn_policy_cached = B_FALSE;
3348
3349 error = ipcl_bind_insert(connp);
3350 if (error != 0) {
3351 if (connp->conn_anon_port) {
3352 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
3353 connp->conn_mlp_type, connp->conn_proto,
3354 ntohs(connp->conn_lport), B_FALSE);
3355 }
3356 connp->conn_mlp_type = mlptSingle;
3357 }
3358 return (error);
3359 }
3360
3361 /*
3362 * Verify that both the source and destination addresses are valid. If
3363 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3364 * i.e. have no route to it. Protocols like TCP want to verify destination
3365 * reachability, while tunnels do not.
3366 *
3367 * Determine the route, the interface, and (optionally) the source address
3368 * to use to reach a given destination.
3369 * Note that we allow connect to broadcast and multicast addresses when
3370 * IPDF_ALLOW_MCBC is set.
3371 * first_hop and dst_addr are normally the same, but if source routing
3372 * they will differ; in that case the first_hop is what we'll use for the
3373 * routing lookup but the dce and label checks will be done on dst_addr,
3374 *
3375 * If uinfo is set, then we fill in the best available information
3376 * we have for the destination. This is based on (in priority order) any
3377 * metrics and path MTU stored in a dce_t, route metrics, and finally the
3378 * ill_mtu/ill_mc_mtu.
3379 *
3380 * Tsol note: If we have a source route then dst_addr != firsthop. But we
3381 * always do the label check on dst_addr.
3382 */
3383 int
ip_set_destination_v4(ipaddr_t * src_addrp,ipaddr_t dst_addr,ipaddr_t firsthop,ip_xmit_attr_t * ixa,iulp_t * uinfo,uint32_t flags,uint_t mac_mode)3384 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3385 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode)
3386 {
3387 ire_t *ire = NULL;
3388 int error = 0;
3389 ipaddr_t setsrc; /* RTF_SETSRC */
3390 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */
3391 ip_stack_t *ipst = ixa->ixa_ipst;
3392 dce_t *dce;
3393 uint_t pmtu;
3394 uint_t generation;
3395 nce_t *nce;
3396 ill_t *ill = NULL;
3397 boolean_t multirt = B_FALSE;
3398
3399 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3400
3401 /*
3402 * We never send to zero; the ULPs map it to the loopback address.
3403 * We can't allow it since we use zero to mean unitialized in some
3404 * places.
3405 */
3406 ASSERT(dst_addr != INADDR_ANY);
3407
3408 if (is_system_labeled()) {
3409 ts_label_t *tsl = NULL;
3410
3411 error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION,
3412 mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl);
3413 if (error != 0)
3414 return (error);
3415 if (tsl != NULL) {
3416 /* Update the label */
3417 ip_xmit_attr_replace_tsl(ixa, tsl);
3418 }
3419 }
3420
3421 setsrc = INADDR_ANY;
3422 /*
3423 * Select a route; For IPMP interfaces, we would only select
3424 * a "hidden" route (i.e., going through a specific under_ill)
3425 * if ixa_ifindex has been specified.
3426 */
3427 ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3428 &generation, &setsrc, &error, &multirt);
3429 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */
3430 if (error != 0)
3431 goto bad_addr;
3432
3433 /*
3434 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3435 * If IPDF_VERIFY_DST is set, the destination must be reachable;
3436 * Otherwise the destination needn't be reachable.
3437 *
3438 * If we match on a reject or black hole, then we've got a
3439 * local failure. May as well fail out the connect() attempt,
3440 * since it's never going to succeed.
3441 */
3442 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3443 /*
3444 * If we're verifying destination reachability, we always want
3445 * to complain here.
3446 *
3447 * If we're not verifying destination reachability but the
3448 * destination has a route, we still want to fail on the
3449 * temporary address and broadcast address tests.
3450 *
3451 * In both cases do we let the code continue so some reasonable
3452 * information is returned to the caller. That enables the
3453 * caller to use (and even cache) the IRE. conn_ip_ouput will
3454 * use the generation mismatch path to check for the unreachable
3455 * case thereby avoiding any specific check in the main path.
3456 */
3457 ASSERT(generation == IRE_GENERATION_VERIFY);
3458 if (flags & IPDF_VERIFY_DST) {
3459 /*
3460 * Set errno but continue to set up ixa_ire to be
3461 * the RTF_REJECT|RTF_BLACKHOLE IRE.
3462 * That allows callers to use ip_output to get an
3463 * ICMP error back.
3464 */
3465 if (!(ire->ire_type & IRE_HOST))
3466 error = ENETUNREACH;
3467 else
3468 error = EHOSTUNREACH;
3469 }
3470 }
3471
3472 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3473 !(flags & IPDF_ALLOW_MCBC)) {
3474 ire_refrele(ire);
3475 ire = ire_reject(ipst, B_FALSE);
3476 generation = IRE_GENERATION_VERIFY;
3477 error = ENETUNREACH;
3478 }
3479
3480 /* Cache things */
3481 if (ixa->ixa_ire != NULL)
3482 ire_refrele_notr(ixa->ixa_ire);
3483 #ifdef DEBUG
3484 ire_refhold_notr(ire);
3485 ire_refrele(ire);
3486 #endif
3487 ixa->ixa_ire = ire;
3488 ixa->ixa_ire_generation = generation;
3489
3490 /*
3491 * Ensure that ixa_dce is always set any time that ixa_ire is set,
3492 * since some callers will send a packet to conn_ip_output() even if
3493 * there's an error.
3494 */
3495 if (flags & IPDF_UNIQUE_DCE) {
3496 /* Fallback to the default dce if allocation fails */
3497 dce = dce_lookup_and_add_v4(dst_addr, ipst);
3498 if (dce != NULL)
3499 generation = dce->dce_generation;
3500 else
3501 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3502 } else {
3503 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3504 }
3505 ASSERT(dce != NULL);
3506 if (ixa->ixa_dce != NULL)
3507 dce_refrele_notr(ixa->ixa_dce);
3508 #ifdef DEBUG
3509 dce_refhold_notr(dce);
3510 dce_refrele(dce);
3511 #endif
3512 ixa->ixa_dce = dce;
3513 ixa->ixa_dce_generation = generation;
3514
3515 /*
3516 * For multicast with multirt we have a flag passed back from
3517 * ire_lookup_multi_ill_v4 since we don't have an IRE for each
3518 * possible multicast address.
3519 * We also need a flag for multicast since we can't check
3520 * whether RTF_MULTIRT is set in ixa_ire for multicast.
3521 */
3522 if (multirt) {
3523 ixa->ixa_postfragfn = ip_postfrag_multirt_v4;
3524 ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST;
3525 } else {
3526 ixa->ixa_postfragfn = ire->ire_postfragfn;
3527 ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST;
3528 }
3529 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3530 /* Get an nce to cache. */
3531 nce = ire_to_nce(ire, firsthop, NULL);
3532 if (nce == NULL) {
3533 /* Allocation failure? */
3534 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3535 } else {
3536 if (ixa->ixa_nce != NULL)
3537 nce_refrele(ixa->ixa_nce);
3538 ixa->ixa_nce = nce;
3539 }
3540 }
3541
3542 /*
3543 * If the source address is a loopback address, the
3544 * destination had best be local or multicast.
3545 * If we are sending to an IRE_LOCAL using a loopback source then
3546 * it had better be the same zoneid.
3547 */
3548 if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3549 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3550 ire = NULL; /* Stored in ixa_ire */
3551 error = EADDRNOTAVAIL;
3552 goto bad_addr;
3553 }
3554 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3555 ire = NULL; /* Stored in ixa_ire */
3556 error = EADDRNOTAVAIL;
3557 goto bad_addr;
3558 }
3559 }
3560 if (ire->ire_type & IRE_BROADCAST) {
3561 /*
3562 * If the ULP didn't have a specified source, then we
3563 * make sure we reselect the source when sending
3564 * broadcasts out different interfaces.
3565 */
3566 if (flags & IPDF_SELECT_SRC)
3567 ixa->ixa_flags |= IXAF_SET_SOURCE;
3568 else
3569 ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3570 }
3571
3572 /*
3573 * Does the caller want us to pick a source address?
3574 */
3575 if (flags & IPDF_SELECT_SRC) {
3576 ipaddr_t src_addr;
3577
3578 /*
3579 * We use use ire_nexthop_ill to avoid the under ipmp
3580 * interface for source address selection. Note that for ipmp
3581 * probe packets, ixa_ifindex would have been specified, and
3582 * the ip_select_route() invocation would have picked an ire
3583 * will ire_ill pointing at an under interface.
3584 */
3585 ill = ire_nexthop_ill(ire);
3586
3587 /* If unreachable we have no ill but need some source */
3588 if (ill == NULL) {
3589 src_addr = htonl(INADDR_LOOPBACK);
3590 /* Make sure we look for a better source address */
3591 generation = SRC_GENERATION_VERIFY;
3592 } else {
3593 error = ip_select_source_v4(ill, setsrc, dst_addr,
3594 ixa->ixa_multicast_ifaddr, zoneid,
3595 ipst, &src_addr, &generation, NULL);
3596 if (error != 0) {
3597 ire = NULL; /* Stored in ixa_ire */
3598 goto bad_addr;
3599 }
3600 }
3601
3602 /*
3603 * We allow the source address to to down.
3604 * However, we check that we don't use the loopback address
3605 * as a source when sending out on the wire.
3606 */
3607 if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3608 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3609 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3610 ire = NULL; /* Stored in ixa_ire */
3611 error = EADDRNOTAVAIL;
3612 goto bad_addr;
3613 }
3614
3615 *src_addrp = src_addr;
3616 ixa->ixa_src_generation = generation;
3617 }
3618
3619 /*
3620 * Make sure we don't leave an unreachable ixa_nce in place
3621 * since ip_select_route is used when we unplumb i.e., remove
3622 * references on ixa_ire, ixa_nce, and ixa_dce.
3623 */
3624 nce = ixa->ixa_nce;
3625 if (nce != NULL && nce->nce_is_condemned) {
3626 nce_refrele(nce);
3627 ixa->ixa_nce = NULL;
3628 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3629 }
3630
3631 /*
3632 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3633 * However, we can't do it for IPv4 multicast or broadcast.
3634 */
3635 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3636 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3637
3638 /*
3639 * Set initial value for fragmentation limit. Either conn_ip_output
3640 * or ULP might updates it when there are routing changes.
3641 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3642 */
3643 pmtu = ip_get_pmtu(ixa);
3644 ixa->ixa_fragsize = pmtu;
3645 /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3646 if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3647 ixa->ixa_pmtu = pmtu;
3648
3649 /*
3650 * Extract information useful for some transports.
3651 * First we look for DCE metrics. Then we take what we have in
3652 * the metrics in the route, where the offlink is used if we have
3653 * one.
3654 */
3655 if (uinfo != NULL) {
3656 bzero(uinfo, sizeof (*uinfo));
3657
3658 if (dce->dce_flags & DCEF_UINFO)
3659 *uinfo = dce->dce_uinfo;
3660
3661 rts_merge_metrics(uinfo, &ire->ire_metrics);
3662
3663 /* Allow ire_metrics to decrease the path MTU from above */
3664 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3665 uinfo->iulp_mtu = pmtu;
3666
3667 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3668 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3669 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3670 }
3671
3672 if (ill != NULL)
3673 ill_refrele(ill);
3674
3675 return (error);
3676
3677 bad_addr:
3678 if (ire != NULL)
3679 ire_refrele(ire);
3680
3681 if (ill != NULL)
3682 ill_refrele(ill);
3683
3684 /*
3685 * Make sure we don't leave an unreachable ixa_nce in place
3686 * since ip_select_route is used when we unplumb i.e., remove
3687 * references on ixa_ire, ixa_nce, and ixa_dce.
3688 */
3689 nce = ixa->ixa_nce;
3690 if (nce != NULL && nce->nce_is_condemned) {
3691 nce_refrele(nce);
3692 ixa->ixa_nce = NULL;
3693 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3694 }
3695
3696 return (error);
3697 }
3698
3699
3700 /*
3701 * Get the base MTU for the case when path MTU discovery is not used.
3702 * Takes the MTU of the IRE into account.
3703 */
3704 uint_t
ip_get_base_mtu(ill_t * ill,ire_t * ire)3705 ip_get_base_mtu(ill_t *ill, ire_t *ire)
3706 {
3707 uint_t mtu;
3708 uint_t iremtu = ire->ire_metrics.iulp_mtu;
3709
3710 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3711 mtu = ill->ill_mc_mtu;
3712 else
3713 mtu = ill->ill_mtu;
3714
3715 if (iremtu != 0 && iremtu < mtu)
3716 mtu = iremtu;
3717
3718 return (mtu);
3719 }
3720
3721 /*
3722 * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3723 * Assumes that ixa_ire, dce, and nce have already been set up.
3724 *
3725 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3726 * We avoid path MTU discovery if it is disabled with ndd.
3727 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3728 *
3729 * NOTE: We also used to turn it off for source routed packets. That
3730 * is no longer required since the dce is per final destination.
3731 */
3732 uint_t
ip_get_pmtu(ip_xmit_attr_t * ixa)3733 ip_get_pmtu(ip_xmit_attr_t *ixa)
3734 {
3735 ip_stack_t *ipst = ixa->ixa_ipst;
3736 dce_t *dce;
3737 nce_t *nce;
3738 ire_t *ire;
3739 uint_t pmtu;
3740
3741 ire = ixa->ixa_ire;
3742 dce = ixa->ixa_dce;
3743 nce = ixa->ixa_nce;
3744
3745 /*
3746 * If path MTU discovery has been turned off by ndd, then we ignore
3747 * any dce_pmtu and for IPv4 we will not set DF.
3748 */
3749 if (!ipst->ips_ip_path_mtu_discovery)
3750 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3751
3752 pmtu = IP_MAXPACKET;
3753 /*
3754 * We need to determine if it is acceptable to set DF for IPv4 or not
3755 * and for IPv6 if we need to use the minimum MTU. If a connection has
3756 * opted into path MTU discovery, then we can use 'DF' in IPv4 and do
3757 * not have to constrain ourselves to the IPv6 minimum MTU. There is a
3758 * second consideration here: IXAF_DONTFRAG. This is set as a result of
3759 * someone setting the IP_DONTFRAG or IPV6_DONTFRAG socket option. In
3760 * such a case, it is acceptable to set DF for IPv4 and to use a larger
3761 * MTU. Note, the actual MTU is constrained by the ill_t later on in
3762 * this function.
3763 */
3764 if (ixa->ixa_flags & (IXAF_PMTU_DISCOVERY | IXAF_DONTFRAG)) {
3765 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3766 } else {
3767 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3768 if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3769 pmtu = IPV6_MIN_MTU;
3770 }
3771
3772 /* Check if the PMTU is to old before we use it */
3773 if ((dce->dce_flags & DCEF_PMTU) &&
3774 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3775 ipst->ips_ip_pathmtu_interval) {
3776 /*
3777 * Older than 20 minutes. Drop the path MTU information.
3778 */
3779 mutex_enter(&dce->dce_lock);
3780 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3781 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3782 mutex_exit(&dce->dce_lock);
3783 dce_increment_generation(dce);
3784 }
3785
3786 /* The metrics on the route can lower the path MTU */
3787 if (ire->ire_metrics.iulp_mtu != 0 &&
3788 ire->ire_metrics.iulp_mtu < pmtu)
3789 pmtu = ire->ire_metrics.iulp_mtu;
3790
3791 /*
3792 * If the path MTU is smaller than some minimum, we still use dce_pmtu
3793 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3794 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3795 */
3796 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3797 if (dce->dce_flags & DCEF_PMTU) {
3798 if (dce->dce_pmtu < pmtu)
3799 pmtu = dce->dce_pmtu;
3800
3801 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3802 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3803 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3804 } else {
3805 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3806 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3807 }
3808 } else {
3809 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3810 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3811 }
3812 }
3813
3814 /*
3815 * If we have an IRE_LOCAL we use the loopback mtu instead of
3816 * the ill for going out the wire i.e., IRE_LOCAL gets the same
3817 * mtu as IRE_LOOPBACK.
3818 */
3819 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3820 uint_t loopback_mtu;
3821
3822 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3823 ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3824
3825 if (loopback_mtu < pmtu)
3826 pmtu = loopback_mtu;
3827 } else if (nce != NULL) {
3828 /*
3829 * Make sure we don't exceed the interface MTU.
3830 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3831 * an ill. We'd use the above IP_MAXPACKET in that case just
3832 * to tell the transport something larger than zero.
3833 */
3834 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3835 if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3836 pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3837 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3838 nce->nce_ill->ill_mc_mtu < pmtu) {
3839 /*
3840 * for interfaces in an IPMP group, the mtu of
3841 * the nce_ill (under_ill) could be different
3842 * from the mtu of the ncec_ill, so we take the
3843 * min of the two.
3844 */
3845 pmtu = nce->nce_ill->ill_mc_mtu;
3846 }
3847 } else {
3848 if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3849 pmtu = nce->nce_common->ncec_ill->ill_mtu;
3850 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3851 nce->nce_ill->ill_mtu < pmtu) {
3852 /*
3853 * for interfaces in an IPMP group, the mtu of
3854 * the nce_ill (under_ill) could be different
3855 * from the mtu of the ncec_ill, so we take the
3856 * min of the two.
3857 */
3858 pmtu = nce->nce_ill->ill_mtu;
3859 }
3860 }
3861 }
3862
3863 /*
3864 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3865 * Only applies to IPv6.
3866 */
3867 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3868 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3869 switch (ixa->ixa_use_min_mtu) {
3870 case IPV6_USE_MIN_MTU_MULTICAST:
3871 if (ire->ire_type & IRE_MULTICAST)
3872 pmtu = IPV6_MIN_MTU;
3873 break;
3874 case IPV6_USE_MIN_MTU_ALWAYS:
3875 pmtu = IPV6_MIN_MTU;
3876 break;
3877 case IPV6_USE_MIN_MTU_NEVER:
3878 break;
3879 }
3880 } else {
3881 /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3882 if (ire->ire_type & IRE_MULTICAST)
3883 pmtu = IPV6_MIN_MTU;
3884 }
3885 }
3886
3887 /*
3888 * For multirouted IPv6 packets, the IP layer will insert a 8-byte
3889 * fragment header in every packet. We compensate for those cases by
3890 * returning a smaller path MTU to the ULP.
3891 *
3892 * In the case of CGTP then ip_output will add a fragment header.
3893 * Make sure there is room for it by telling a smaller number
3894 * to the transport.
3895 *
3896 * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here
3897 * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu()
3898 * which is the size of the packets it can send.
3899 */
3900 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3901 if ((ire->ire_flags & RTF_MULTIRT) ||
3902 (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) {
3903 pmtu -= sizeof (ip6_frag_t);
3904 ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR;
3905 }
3906 }
3907
3908 return (pmtu);
3909 }
3910
3911 /*
3912 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3913 * the final piece where we don't. Return a pointer to the first mblk in the
3914 * result, and update the pointer to the next mblk to chew on. If anything
3915 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3916 * NULL pointer.
3917 */
3918 mblk_t *
ip_carve_mp(mblk_t ** mpp,ssize_t len)3919 ip_carve_mp(mblk_t **mpp, ssize_t len)
3920 {
3921 mblk_t *mp0;
3922 mblk_t *mp1;
3923 mblk_t *mp2;
3924
3925 if (!len || !mpp || !(mp0 = *mpp))
3926 return (NULL);
3927 /* If we aren't going to consume the first mblk, we need a dup. */
3928 if (mp0->b_wptr - mp0->b_rptr > len) {
3929 mp1 = dupb(mp0);
3930 if (mp1) {
3931 /* Partition the data between the two mblks. */
3932 mp1->b_wptr = mp1->b_rptr + len;
3933 mp0->b_rptr = mp1->b_wptr;
3934 /*
3935 * after adjustments if mblk not consumed is now
3936 * unaligned, try to align it. If this fails free
3937 * all messages and let upper layer recover.
3938 */
3939 if (!OK_32PTR(mp0->b_rptr)) {
3940 if (!pullupmsg(mp0, -1)) {
3941 freemsg(mp0);
3942 freemsg(mp1);
3943 *mpp = NULL;
3944 return (NULL);
3945 }
3946 }
3947 }
3948 return (mp1);
3949 }
3950 /* Eat through as many mblks as we need to get len bytes. */
3951 len -= mp0->b_wptr - mp0->b_rptr;
3952 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3953 if (mp2->b_wptr - mp2->b_rptr > len) {
3954 /*
3955 * We won't consume the entire last mblk. Like
3956 * above, dup and partition it.
3957 */
3958 mp1->b_cont = dupb(mp2);
3959 mp1 = mp1->b_cont;
3960 if (!mp1) {
3961 /*
3962 * Trouble. Rather than go to a lot of
3963 * trouble to clean up, we free the messages.
3964 * This won't be any worse than losing it on
3965 * the wire.
3966 */
3967 freemsg(mp0);
3968 freemsg(mp2);
3969 *mpp = NULL;
3970 return (NULL);
3971 }
3972 mp1->b_wptr = mp1->b_rptr + len;
3973 mp2->b_rptr = mp1->b_wptr;
3974 /*
3975 * after adjustments if mblk not consumed is now
3976 * unaligned, try to align it. If this fails free
3977 * all messages and let upper layer recover.
3978 */
3979 if (!OK_32PTR(mp2->b_rptr)) {
3980 if (!pullupmsg(mp2, -1)) {
3981 freemsg(mp0);
3982 freemsg(mp2);
3983 *mpp = NULL;
3984 return (NULL);
3985 }
3986 }
3987 *mpp = mp2;
3988 return (mp0);
3989 }
3990 /* Decrement len by the amount we just got. */
3991 len -= mp2->b_wptr - mp2->b_rptr;
3992 }
3993 /*
3994 * len should be reduced to zero now. If not our caller has
3995 * screwed up.
3996 */
3997 if (len) {
3998 /* Shouldn't happen! */
3999 freemsg(mp0);
4000 *mpp = NULL;
4001 return (NULL);
4002 }
4003 /*
4004 * We consumed up to exactly the end of an mblk. Detach the part
4005 * we are returning from the rest of the chain.
4006 */
4007 mp1->b_cont = NULL;
4008 *mpp = mp2;
4009 return (mp0);
4010 }
4011
4012 /* The ill stream is being unplumbed. Called from ip_close */
4013 int
ip_modclose(ill_t * ill)4014 ip_modclose(ill_t *ill)
4015 {
4016 boolean_t success;
4017 ipsq_t *ipsq;
4018 ipif_t *ipif;
4019 queue_t *q = ill->ill_rq;
4020 ip_stack_t *ipst = ill->ill_ipst;
4021 int i;
4022 arl_ill_common_t *ai = ill->ill_common;
4023
4024 /*
4025 * The punlink prior to this may have initiated a capability
4026 * negotiation. But ipsq_enter will block until that finishes or
4027 * times out.
4028 */
4029 success = ipsq_enter(ill, B_FALSE, NEW_OP);
4030
4031 /*
4032 * Open/close/push/pop is guaranteed to be single threaded
4033 * per stream by STREAMS. FS guarantees that all references
4034 * from top are gone before close is called. So there can't
4035 * be another close thread that has set CONDEMNED on this ill.
4036 * and cause ipsq_enter to return failure.
4037 */
4038 ASSERT(success);
4039 ipsq = ill->ill_phyint->phyint_ipsq;
4040
4041 /*
4042 * Mark it condemned. No new reference will be made to this ill.
4043 * Lookup functions will return an error. Threads that try to
4044 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
4045 * that the refcnt will drop down to zero.
4046 */
4047 mutex_enter(&ill->ill_lock);
4048 ill->ill_state_flags |= ILL_CONDEMNED;
4049 for (ipif = ill->ill_ipif; ipif != NULL;
4050 ipif = ipif->ipif_next) {
4051 ipif->ipif_state_flags |= IPIF_CONDEMNED;
4052 }
4053 /*
4054 * Wake up anybody waiting to enter the ipsq. ipsq_enter
4055 * returns error if ILL_CONDEMNED is set
4056 */
4057 cv_broadcast(&ill->ill_cv);
4058 mutex_exit(&ill->ill_lock);
4059
4060 /*
4061 * Send all the deferred DLPI messages downstream which came in
4062 * during the small window right before ipsq_enter(). We do this
4063 * without waiting for the ACKs because all the ACKs for M_PROTO
4064 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
4065 */
4066 ill_dlpi_send_deferred(ill);
4067
4068 /*
4069 * Shut down fragmentation reassembly.
4070 * ill_frag_timer won't start a timer again.
4071 * Now cancel any existing timer
4072 */
4073 (void) untimeout(ill->ill_frag_timer_id);
4074 (void) ill_frag_timeout(ill, 0);
4075
4076 /*
4077 * Call ill_delete to bring down the ipifs, ilms and ill on
4078 * this ill. Then wait for the refcnts to drop to zero.
4079 * ill_is_freeable checks whether the ill is really quiescent.
4080 * Then make sure that threads that are waiting to enter the
4081 * ipsq have seen the error returned by ipsq_enter and have
4082 * gone away. Then we call ill_delete_tail which does the
4083 * DL_UNBIND_REQ with the driver and then qprocsoff.
4084 */
4085 ill_delete(ill);
4086 mutex_enter(&ill->ill_lock);
4087 while (!ill_is_freeable(ill))
4088 cv_wait(&ill->ill_cv, &ill->ill_lock);
4089
4090 while (ill->ill_waiters)
4091 cv_wait(&ill->ill_cv, &ill->ill_lock);
4092
4093 mutex_exit(&ill->ill_lock);
4094
4095 /*
4096 * ill_delete_tail drops reference on ill_ipst, but we need to keep
4097 * it held until the end of the function since the cleanup
4098 * below needs to be able to use the ip_stack_t.
4099 */
4100 netstack_hold(ipst->ips_netstack);
4101
4102 /* qprocsoff is done via ill_delete_tail */
4103 ill_delete_tail(ill);
4104 /*
4105 * synchronously wait for arp stream to unbind. After this, we
4106 * cannot get any data packets up from the driver.
4107 */
4108 arp_unbind_complete(ill);
4109 ASSERT(ill->ill_ipst == NULL);
4110
4111 /*
4112 * Walk through all conns and qenable those that have queued data.
4113 * Close synchronization needs this to
4114 * be done to ensure that all upper layers blocked
4115 * due to flow control to the closing device
4116 * get unblocked.
4117 */
4118 ip1dbg(("ip_wsrv: walking\n"));
4119 for (i = 0; i < TX_FANOUT_SIZE; i++) {
4120 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
4121 }
4122
4123 /*
4124 * ai can be null if this is an IPv6 ill, or if the IPv4
4125 * stream is being torn down before ARP was plumbed (e.g.,
4126 * /sbin/ifconfig plumbing a stream twice, and encountering
4127 * an error
4128 */
4129 if (ai != NULL) {
4130 ASSERT(!ill->ill_isv6);
4131 mutex_enter(&ai->ai_lock);
4132 ai->ai_ill = NULL;
4133 if (ai->ai_arl == NULL) {
4134 mutex_destroy(&ai->ai_lock);
4135 kmem_free(ai, sizeof (*ai));
4136 } else {
4137 cv_signal(&ai->ai_ill_unplumb_done);
4138 mutex_exit(&ai->ai_lock);
4139 }
4140 }
4141
4142 mutex_enter(&ipst->ips_ip_mi_lock);
4143 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
4144 mutex_exit(&ipst->ips_ip_mi_lock);
4145
4146 /*
4147 * credp could be null if the open didn't succeed and ip_modopen
4148 * itself calls ip_close.
4149 */
4150 if (ill->ill_credp != NULL)
4151 crfree(ill->ill_credp);
4152
4153 mutex_destroy(&ill->ill_saved_ire_lock);
4154 mutex_destroy(&ill->ill_lock);
4155 rw_destroy(&ill->ill_mcast_lock);
4156 mutex_destroy(&ill->ill_mcast_serializer);
4157 list_destroy(&ill->ill_nce);
4158
4159 /*
4160 * Now we are done with the module close pieces that
4161 * need the netstack_t.
4162 */
4163 netstack_rele(ipst->ips_netstack);
4164
4165 mi_close_free((IDP)ill);
4166 q->q_ptr = WR(q)->q_ptr = NULL;
4167
4168 ipsq_exit(ipsq);
4169
4170 return (0);
4171 }
4172
4173 /*
4174 * This is called as part of close() for IP, UDP, ICMP, and RTS
4175 * in order to quiesce the conn.
4176 */
4177 void
ip_quiesce_conn(conn_t * connp)4178 ip_quiesce_conn(conn_t *connp)
4179 {
4180 boolean_t drain_cleanup_reqd = B_FALSE;
4181 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
4182 boolean_t ilg_cleanup_reqd = B_FALSE;
4183 ip_stack_t *ipst;
4184
4185 ASSERT(!IPCL_IS_TCP(connp));
4186 ipst = connp->conn_netstack->netstack_ip;
4187
4188 /*
4189 * Mark the conn as closing, and this conn must not be
4190 * inserted in future into any list. Eg. conn_drain_insert(),
4191 * won't insert this conn into the conn_drain_list.
4192 *
4193 * conn_idl, and conn_ilg cannot get set henceforth.
4194 */
4195 mutex_enter(&connp->conn_lock);
4196 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4197 connp->conn_state_flags |= CONN_CLOSING;
4198 if (connp->conn_idl != NULL)
4199 drain_cleanup_reqd = B_TRUE;
4200 if (connp->conn_oper_pending_ill != NULL)
4201 conn_ioctl_cleanup_reqd = B_TRUE;
4202 if (connp->conn_dhcpinit_ill != NULL) {
4203 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4204 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4205 ill_set_inputfn(connp->conn_dhcpinit_ill);
4206 connp->conn_dhcpinit_ill = NULL;
4207 }
4208 if (connp->conn_ilg != NULL)
4209 ilg_cleanup_reqd = B_TRUE;
4210 mutex_exit(&connp->conn_lock);
4211
4212 if (conn_ioctl_cleanup_reqd)
4213 conn_ioctl_cleanup(connp);
4214
4215 if (is_system_labeled() && connp->conn_anon_port) {
4216 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4217 connp->conn_mlp_type, connp->conn_proto,
4218 ntohs(connp->conn_lport), B_FALSE);
4219 connp->conn_anon_port = 0;
4220 }
4221 connp->conn_mlp_type = mlptSingle;
4222
4223 /*
4224 * Remove this conn from any fanout list it is on.
4225 * and then wait for any threads currently operating
4226 * on this endpoint to finish
4227 */
4228 ipcl_hash_remove(connp);
4229
4230 /*
4231 * Remove this conn from the drain list, and do any other cleanup that
4232 * may be required. (TCP conns are never flow controlled, and
4233 * conn_idl will be NULL.)
4234 */
4235 if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4236 idl_t *idl = connp->conn_idl;
4237
4238 mutex_enter(&idl->idl_lock);
4239 conn_drain(connp, B_TRUE);
4240 mutex_exit(&idl->idl_lock);
4241 }
4242
4243 if (connp == ipst->ips_ip_g_mrouter)
4244 (void) ip_mrouter_done(ipst);
4245
4246 if (ilg_cleanup_reqd)
4247 ilg_delete_all(connp);
4248
4249 /*
4250 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4251 * callers from write side can't be there now because close
4252 * is in progress. The only other caller is ipcl_walk
4253 * which checks for the condemned flag.
4254 */
4255 mutex_enter(&connp->conn_lock);
4256 connp->conn_state_flags |= CONN_CONDEMNED;
4257 while (connp->conn_ref != 1)
4258 cv_wait(&connp->conn_cv, &connp->conn_lock);
4259 connp->conn_state_flags |= CONN_QUIESCED;
4260 mutex_exit(&connp->conn_lock);
4261 }
4262
4263 /* ARGSUSED */
4264 int
ip_close(queue_t * q,int flags,cred_t * credp __unused)4265 ip_close(queue_t *q, int flags, cred_t *credp __unused)
4266 {
4267 conn_t *connp;
4268
4269 /*
4270 * Call the appropriate delete routine depending on whether this is
4271 * a module or device.
4272 */
4273 if (WR(q)->q_next != NULL) {
4274 /* This is a module close */
4275 return (ip_modclose((ill_t *)q->q_ptr));
4276 }
4277
4278 connp = q->q_ptr;
4279 ip_quiesce_conn(connp);
4280
4281 qprocsoff(q);
4282
4283 /*
4284 * Now we are truly single threaded on this stream, and can
4285 * delete the things hanging off the connp, and finally the connp.
4286 * We removed this connp from the fanout list, it cannot be
4287 * accessed thru the fanouts, and we already waited for the
4288 * conn_ref to drop to 0. We are already in close, so
4289 * there cannot be any other thread from the top. qprocsoff
4290 * has completed, and service has completed or won't run in
4291 * future.
4292 */
4293 ASSERT(connp->conn_ref == 1);
4294
4295 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4296
4297 connp->conn_ref--;
4298 ipcl_conn_destroy(connp);
4299
4300 q->q_ptr = WR(q)->q_ptr = NULL;
4301 return (0);
4302 }
4303
4304 /*
4305 * Wapper around putnext() so that ip_rts_request can merely use
4306 * conn_recv.
4307 */
4308 /*ARGSUSED2*/
4309 static void
ip_conn_input(void * arg1,mblk_t * mp,void * arg2,ip_recv_attr_t * ira)4310 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4311 {
4312 conn_t *connp = (conn_t *)arg1;
4313
4314 putnext(connp->conn_rq, mp);
4315 }
4316
4317 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4318 /* ARGSUSED */
4319 static void
ip_conn_input_icmp(void * arg1,mblk_t * mp,void * arg2,ip_recv_attr_t * ira)4320 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4321 {
4322 freemsg(mp);
4323 }
4324
4325 /*
4326 * Called when the module is about to be unloaded
4327 */
4328 void
ip_ddi_destroy(void)4329 ip_ddi_destroy(void)
4330 {
4331 /* This needs to be called before destroying any transports. */
4332 mutex_enter(&cpu_lock);
4333 unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4334 mutex_exit(&cpu_lock);
4335
4336 tnet_fini();
4337
4338 icmp_ddi_g_destroy();
4339 rts_ddi_g_destroy();
4340 udp_ddi_g_destroy();
4341 sctp_ddi_g_destroy();
4342 tcp_ddi_g_destroy();
4343 ilb_ddi_g_destroy();
4344 dce_g_destroy();
4345 ipsec_policy_g_destroy();
4346 ipcl_g_destroy();
4347 ip_net_g_destroy();
4348 ip_ire_g_fini();
4349 inet_minor_destroy(ip_minor_arena_sa);
4350 #if defined(_LP64)
4351 inet_minor_destroy(ip_minor_arena_la);
4352 #endif
4353
4354 #ifdef DEBUG
4355 list_destroy(&ip_thread_list);
4356 rw_destroy(&ip_thread_rwlock);
4357 tsd_destroy(&ip_thread_data);
4358 #endif
4359
4360 netstack_unregister(NS_IP);
4361 }
4362
4363 /*
4364 * First step in cleanup.
4365 */
4366 /* ARGSUSED */
4367 static void
ip_stack_shutdown(netstackid_t stackid,void * arg)4368 ip_stack_shutdown(netstackid_t stackid, void *arg)
4369 {
4370 ip_stack_t *ipst = (ip_stack_t *)arg;
4371 kt_did_t ktid;
4372
4373 #ifdef NS_DEBUG
4374 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4375 #endif
4376
4377 /*
4378 * Perform cleanup for special interfaces (loopback and IPMP).
4379 */
4380 ip_interface_cleanup(ipst);
4381
4382 /*
4383 * The *_hook_shutdown()s start the process of notifying any
4384 * consumers that things are going away.... nothing is destroyed.
4385 */
4386 ipv4_hook_shutdown(ipst);
4387 ipv6_hook_shutdown(ipst);
4388 arp_hook_shutdown(ipst);
4389
4390 mutex_enter(&ipst->ips_capab_taskq_lock);
4391 ktid = ipst->ips_capab_taskq_thread->t_did;
4392 ipst->ips_capab_taskq_quit = B_TRUE;
4393 cv_signal(&ipst->ips_capab_taskq_cv);
4394 mutex_exit(&ipst->ips_capab_taskq_lock);
4395
4396 /*
4397 * In rare occurrences, particularly on virtual hardware where CPUs can
4398 * be de-scheduled, the thread that we just signaled will not run until
4399 * after we have gotten through parts of ip_stack_fini. If that happens
4400 * then we'll try to grab the ips_capab_taskq_lock as part of returning
4401 * from cv_wait which no longer exists.
4402 */
4403 thread_join(ktid);
4404 }
4405
4406 /*
4407 * Free the IP stack instance.
4408 */
4409 static void
ip_stack_fini(netstackid_t stackid,void * arg)4410 ip_stack_fini(netstackid_t stackid, void *arg)
4411 {
4412 ip_stack_t *ipst = (ip_stack_t *)arg;
4413 int ret;
4414
4415 #ifdef NS_DEBUG
4416 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4417 #endif
4418 /*
4419 * At this point, all of the notifications that the events and
4420 * protocols are going away have been run, meaning that we can
4421 * now set about starting to clean things up.
4422 */
4423 ipobs_fini(ipst);
4424 ipv4_hook_destroy(ipst);
4425 ipv6_hook_destroy(ipst);
4426 arp_hook_destroy(ipst);
4427 ip_net_destroy(ipst);
4428
4429 ipmp_destroy(ipst);
4430
4431 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4432 ipst->ips_ip_mibkp = NULL;
4433 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4434 ipst->ips_icmp_mibkp = NULL;
4435 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4436 ipst->ips_ip_kstat = NULL;
4437 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4438 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4439 ipst->ips_ip6_kstat = NULL;
4440 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4441
4442 kmem_free(ipst->ips_propinfo_tbl,
4443 ip_propinfo_count * sizeof (mod_prop_info_t));
4444 ipst->ips_propinfo_tbl = NULL;
4445
4446 dce_stack_destroy(ipst);
4447 ip_mrouter_stack_destroy(ipst);
4448
4449 /*
4450 * Quiesce all of our timers. Note we set the quiesce flags before we
4451 * call untimeout. The slowtimers may actually kick off another instance
4452 * of the non-slow timers.
4453 */
4454 mutex_enter(&ipst->ips_igmp_timer_lock);
4455 ipst->ips_igmp_timer_quiesce = B_TRUE;
4456 mutex_exit(&ipst->ips_igmp_timer_lock);
4457
4458 mutex_enter(&ipst->ips_mld_timer_lock);
4459 ipst->ips_mld_timer_quiesce = B_TRUE;
4460 mutex_exit(&ipst->ips_mld_timer_lock);
4461
4462 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
4463 ipst->ips_igmp_slowtimeout_quiesce = B_TRUE;
4464 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
4465
4466 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
4467 ipst->ips_mld_slowtimeout_quiesce = B_TRUE;
4468 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
4469
4470 ret = untimeout(ipst->ips_igmp_timeout_id);
4471 if (ret == -1) {
4472 ASSERT(ipst->ips_igmp_timeout_id == 0);
4473 } else {
4474 ASSERT(ipst->ips_igmp_timeout_id != 0);
4475 ipst->ips_igmp_timeout_id = 0;
4476 }
4477 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4478 if (ret == -1) {
4479 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4480 } else {
4481 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4482 ipst->ips_igmp_slowtimeout_id = 0;
4483 }
4484 ret = untimeout(ipst->ips_mld_timeout_id);
4485 if (ret == -1) {
4486 ASSERT(ipst->ips_mld_timeout_id == 0);
4487 } else {
4488 ASSERT(ipst->ips_mld_timeout_id != 0);
4489 ipst->ips_mld_timeout_id = 0;
4490 }
4491 ret = untimeout(ipst->ips_mld_slowtimeout_id);
4492 if (ret == -1) {
4493 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4494 } else {
4495 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4496 ipst->ips_mld_slowtimeout_id = 0;
4497 }
4498
4499 ip_ire_fini(ipst);
4500 ip6_asp_free(ipst);
4501 conn_drain_fini(ipst);
4502 ipcl_destroy(ipst);
4503
4504 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4505 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4506 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4507 ipst->ips_ndp4 = NULL;
4508 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4509 ipst->ips_ndp6 = NULL;
4510
4511 if (ipst->ips_loopback_ksp != NULL) {
4512 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4513 ipst->ips_loopback_ksp = NULL;
4514 }
4515
4516 mutex_destroy(&ipst->ips_capab_taskq_lock);
4517 cv_destroy(&ipst->ips_capab_taskq_cv);
4518
4519 rw_destroy(&ipst->ips_srcid_lock);
4520
4521 mutex_destroy(&ipst->ips_ip_mi_lock);
4522 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4523
4524 mutex_destroy(&ipst->ips_igmp_timer_lock);
4525 mutex_destroy(&ipst->ips_mld_timer_lock);
4526 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4527 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4528 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4529 rw_destroy(&ipst->ips_ill_g_lock);
4530
4531 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4532 ipst->ips_phyint_g_list = NULL;
4533 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4534 ipst->ips_ill_g_heads = NULL;
4535
4536 ldi_ident_release(ipst->ips_ldi_ident);
4537 kmem_free(ipst, sizeof (*ipst));
4538 }
4539
4540 /*
4541 * This function is called from the TSD destructor, and is used to debug
4542 * reference count issues in IP. See block comment in <inet/ip_if.h> for
4543 * details.
4544 */
4545 static void
ip_thread_exit(void * phash)4546 ip_thread_exit(void *phash)
4547 {
4548 th_hash_t *thh = phash;
4549
4550 rw_enter(&ip_thread_rwlock, RW_WRITER);
4551 list_remove(&ip_thread_list, thh);
4552 rw_exit(&ip_thread_rwlock);
4553 mod_hash_destroy_hash(thh->thh_hash);
4554 kmem_free(thh, sizeof (*thh));
4555 }
4556
4557 /*
4558 * Called when the IP kernel module is loaded into the kernel
4559 */
4560 void
ip_ddi_init(void)4561 ip_ddi_init(void)
4562 {
4563 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4564
4565 /*
4566 * For IP and TCP the minor numbers should start from 2 since we have 4
4567 * initial devices: ip, ip6, tcp, tcp6.
4568 */
4569 /*
4570 * If this is a 64-bit kernel, then create two separate arenas -
4571 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4572 * other for socket apps in the range 2^^18 through 2^^32-1.
4573 */
4574 ip_minor_arena_la = NULL;
4575 ip_minor_arena_sa = NULL;
4576 #if defined(_LP64)
4577 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4578 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4579 cmn_err(CE_PANIC,
4580 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4581 }
4582 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4583 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4584 cmn_err(CE_PANIC,
4585 "ip_ddi_init: ip_minor_arena_la creation failed\n");
4586 }
4587 #else
4588 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4589 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4590 cmn_err(CE_PANIC,
4591 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4592 }
4593 #endif
4594 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4595
4596 ipcl_g_init();
4597 ip_ire_g_init();
4598 ip_net_g_init();
4599
4600 #ifdef DEBUG
4601 tsd_create(&ip_thread_data, ip_thread_exit);
4602 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4603 list_create(&ip_thread_list, sizeof (th_hash_t),
4604 offsetof(th_hash_t, thh_link));
4605 #endif
4606 ipsec_policy_g_init();
4607 tcp_ddi_g_init();
4608 sctp_ddi_g_init();
4609 dce_g_init();
4610
4611 /*
4612 * We want to be informed each time a stack is created or
4613 * destroyed in the kernel, so we can maintain the
4614 * set of udp_stack_t's.
4615 */
4616 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4617 ip_stack_fini);
4618
4619 tnet_init();
4620
4621 udp_ddi_g_init();
4622 rts_ddi_g_init();
4623 icmp_ddi_g_init();
4624 ilb_ddi_g_init();
4625
4626 /* This needs to be called after all transports are initialized. */
4627 mutex_enter(&cpu_lock);
4628 register_cpu_setup_func(ip_tp_cpu_update, NULL);
4629 mutex_exit(&cpu_lock);
4630 }
4631
4632 /*
4633 * Initialize the IP stack instance.
4634 */
4635 static void *
ip_stack_init(netstackid_t stackid,netstack_t * ns)4636 ip_stack_init(netstackid_t stackid, netstack_t *ns)
4637 {
4638 ip_stack_t *ipst;
4639 size_t arrsz;
4640 major_t major;
4641
4642 #ifdef NS_DEBUG
4643 printf("ip_stack_init(stack %d)\n", stackid);
4644 #endif
4645
4646 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4647 ipst->ips_netstack = ns;
4648
4649 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4650 KM_SLEEP);
4651 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4652 KM_SLEEP);
4653 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4654 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4655 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4656 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4657
4658 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4659 ipst->ips_igmp_deferred_next = INFINITY;
4660 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4661 ipst->ips_mld_deferred_next = INFINITY;
4662 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4663 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4664 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4665 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4666 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4667 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4668
4669 ipcl_init(ipst);
4670 ip_ire_init(ipst);
4671 ip6_asp_init(ipst);
4672 ipif_init(ipst);
4673 conn_drain_init(ipst);
4674 ip_mrouter_stack_init(ipst);
4675 dce_stack_init(ipst);
4676
4677 ipst->ips_ip_multirt_log_interval = 1000;
4678
4679 ipst->ips_ill_index = 1;
4680
4681 ipst->ips_saved_ip_forwarding = -1;
4682 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
4683
4684 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4685 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4686 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4687
4688 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4689 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4690 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4691 ipst->ips_ip6_kstat =
4692 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4693
4694 ipst->ips_ip_src_id = 1;
4695 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4696
4697 ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4698
4699 ip_net_init(ipst, ns);
4700 ipv4_hook_init(ipst);
4701 ipv6_hook_init(ipst);
4702 arp_hook_init(ipst);
4703 ipmp_init(ipst);
4704 ipobs_init(ipst);
4705
4706 /*
4707 * Create the taskq dispatcher thread and initialize related stuff.
4708 */
4709 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4710 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4711 ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4712 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4713
4714 major = mod_name_to_major(INET_NAME);
4715 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4716 return (ipst);
4717 }
4718
4719 /*
4720 * Allocate and initialize a DLPI template of the specified length. (May be
4721 * called as writer.)
4722 */
4723 mblk_t *
ip_dlpi_alloc(size_t len,t_uscalar_t prim)4724 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4725 {
4726 mblk_t *mp;
4727
4728 mp = allocb(len, BPRI_MED);
4729 if (!mp)
4730 return (NULL);
4731
4732 /*
4733 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4734 * of which we don't seem to use) are sent with M_PCPROTO, and
4735 * that other DLPI are M_PROTO.
4736 */
4737 if (prim == DL_INFO_REQ) {
4738 mp->b_datap->db_type = M_PCPROTO;
4739 } else {
4740 mp->b_datap->db_type = M_PROTO;
4741 }
4742
4743 mp->b_wptr = mp->b_rptr + len;
4744 bzero(mp->b_rptr, len);
4745 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4746 return (mp);
4747 }
4748
4749 /*
4750 * Allocate and initialize a DLPI notification. (May be called as writer.)
4751 */
4752 mblk_t *
ip_dlnotify_alloc(uint_t notification,uint_t data)4753 ip_dlnotify_alloc(uint_t notification, uint_t data)
4754 {
4755 dl_notify_ind_t *notifyp;
4756 mblk_t *mp;
4757
4758 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4759 return (NULL);
4760
4761 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4762 notifyp->dl_notification = notification;
4763 notifyp->dl_data = data;
4764 return (mp);
4765 }
4766
4767 mblk_t *
ip_dlnotify_alloc2(uint_t notification,uint_t data1,uint_t data2)4768 ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4769 {
4770 dl_notify_ind_t *notifyp;
4771 mblk_t *mp;
4772
4773 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4774 return (NULL);
4775
4776 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4777 notifyp->dl_notification = notification;
4778 notifyp->dl_data1 = data1;
4779 notifyp->dl_data2 = data2;
4780 return (mp);
4781 }
4782
4783 /*
4784 * Debug formatting routine. Returns a character string representation of the
4785 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
4786 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4787 *
4788 * Once the ndd table-printing interfaces are removed, this can be changed to
4789 * standard dotted-decimal form.
4790 */
4791 char *
ip_dot_addr(ipaddr_t addr,char * buf)4792 ip_dot_addr(ipaddr_t addr, char *buf)
4793 {
4794 uint8_t *ap = (uint8_t *)&addr;
4795
4796 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4797 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4798 return (buf);
4799 }
4800
4801 /*
4802 * Write the given MAC address as a printable string in the usual colon-
4803 * separated format.
4804 */
4805 const char *
mac_colon_addr(const uint8_t * addr,size_t alen,char * buf,size_t buflen)4806 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4807 {
4808 char *bp;
4809
4810 if (alen == 0 || buflen < 4)
4811 return ("?");
4812 bp = buf;
4813 for (;;) {
4814 /*
4815 * If there are more MAC address bytes available, but we won't
4816 * have any room to print them, then add "..." to the string
4817 * instead. See below for the 'magic number' explanation.
4818 */
4819 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4820 (void) strcpy(bp, "...");
4821 break;
4822 }
4823 (void) sprintf(bp, "%02x", *addr++);
4824 bp += 2;
4825 if (--alen == 0)
4826 break;
4827 *bp++ = ':';
4828 buflen -= 3;
4829 /*
4830 * At this point, based on the first 'if' statement above,
4831 * either alen == 1 and buflen >= 3, or alen > 1 and
4832 * buflen >= 4. The first case leaves room for the final "xx"
4833 * number and trailing NUL byte. The second leaves room for at
4834 * least "...". Thus the apparently 'magic' numbers chosen for
4835 * that statement.
4836 */
4837 }
4838 return (buf);
4839 }
4840
4841 /*
4842 * Called when it is conceptually a ULP that would sent the packet
4843 * e.g., port unreachable and protocol unreachable. Check that the packet
4844 * would have passed the IPsec global policy before sending the error.
4845 *
4846 * Send an ICMP error after patching up the packet appropriately.
4847 * Uses ip_drop_input and bumps the appropriate MIB.
4848 */
4849 void
ip_fanout_send_icmp_v4(mblk_t * mp,uint_t icmp_type,uint_t icmp_code,ip_recv_attr_t * ira)4850 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4851 ip_recv_attr_t *ira)
4852 {
4853 ipha_t *ipha;
4854 boolean_t secure;
4855 ill_t *ill = ira->ira_ill;
4856 ip_stack_t *ipst = ill->ill_ipst;
4857 netstack_t *ns = ipst->ips_netstack;
4858 ipsec_stack_t *ipss = ns->netstack_ipsec;
4859
4860 secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4861
4862 /*
4863 * We are generating an icmp error for some inbound packet.
4864 * Called from all ip_fanout_(udp, tcp, proto) functions.
4865 * Before we generate an error, check with global policy
4866 * to see whether this is allowed to enter the system. As
4867 * there is no "conn", we are checking with global policy.
4868 */
4869 ipha = (ipha_t *)mp->b_rptr;
4870 if (secure || ipss->ipsec_inbound_v4_policy_present) {
4871 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4872 if (mp == NULL)
4873 return;
4874 }
4875
4876 /* We never send errors for protocols that we do implement */
4877 if (ira->ira_protocol == IPPROTO_ICMP ||
4878 ira->ira_protocol == IPPROTO_IGMP) {
4879 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4880 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4881 freemsg(mp);
4882 return;
4883 }
4884 /*
4885 * Have to correct checksum since
4886 * the packet might have been
4887 * fragmented and the reassembly code in ip_rput
4888 * does not restore the IP checksum.
4889 */
4890 ipha->ipha_hdr_checksum = 0;
4891 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4892
4893 switch (icmp_type) {
4894 case ICMP_DEST_UNREACHABLE:
4895 switch (icmp_code) {
4896 case ICMP_PROTOCOL_UNREACHABLE:
4897 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4898 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4899 break;
4900 case ICMP_PORT_UNREACHABLE:
4901 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4902 ip_drop_input("ipIfStatsNoPorts", mp, ill);
4903 break;
4904 }
4905
4906 icmp_unreachable(mp, icmp_code, ira);
4907 break;
4908 default:
4909 #ifdef DEBUG
4910 panic("ip_fanout_send_icmp_v4: wrong type");
4911 /*NOTREACHED*/
4912 #else
4913 freemsg(mp);
4914 break;
4915 #endif
4916 }
4917 }
4918
4919 /*
4920 * Used to send an ICMP error message when a packet is received for
4921 * a protocol that is not supported. The mblk passed as argument
4922 * is consumed by this function.
4923 */
4924 void
ip_proto_not_sup(mblk_t * mp,ip_recv_attr_t * ira)4925 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4926 {
4927 ipha_t *ipha;
4928
4929 ipha = (ipha_t *)mp->b_rptr;
4930 if (ira->ira_flags & IRAF_IS_IPV4) {
4931 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4932 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4933 ICMP_PROTOCOL_UNREACHABLE, ira);
4934 } else {
4935 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4936 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4937 ICMP6_PARAMPROB_NEXTHEADER, ira);
4938 }
4939 }
4940
4941 /*
4942 * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4943 * Handles IPv4 and IPv6.
4944 * We are responsible for disposing of mp, such as by freemsg() or putnext()
4945 * Caller is responsible for dropping references to the conn.
4946 */
4947 void
ip_fanout_proto_conn(conn_t * connp,mblk_t * mp,ipha_t * ipha,ip6_t * ip6h,ip_recv_attr_t * ira)4948 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4949 ip_recv_attr_t *ira)
4950 {
4951 ill_t *ill = ira->ira_ill;
4952 ip_stack_t *ipst = ill->ill_ipst;
4953 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4954 boolean_t secure;
4955 uint_t protocol = ira->ira_protocol;
4956 iaflags_t iraflags = ira->ira_flags;
4957 queue_t *rq;
4958
4959 secure = iraflags & IRAF_IPSEC_SECURE;
4960
4961 rq = connp->conn_rq;
4962 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4963 switch (protocol) {
4964 case IPPROTO_ICMPV6:
4965 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4966 break;
4967 case IPPROTO_ICMP:
4968 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4969 break;
4970 default:
4971 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4972 break;
4973 }
4974 freemsg(mp);
4975 return;
4976 }
4977
4978 ASSERT(!(IPCL_IS_IPTUN(connp)));
4979
4980 if (((iraflags & IRAF_IS_IPV4) ?
4981 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4982 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4983 secure) {
4984 mp = ipsec_check_inbound_policy(mp, connp, ipha,
4985 ip6h, ira);
4986 if (mp == NULL) {
4987 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4988 /* Note that mp is NULL */
4989 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4990 return;
4991 }
4992 }
4993
4994 if (iraflags & IRAF_ICMP_ERROR) {
4995 (connp->conn_recvicmp)(connp, mp, NULL, ira);
4996 } else {
4997 ill_t *rill = ira->ira_rill;
4998
4999 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5000 ira->ira_ill = ira->ira_rill = NULL;
5001 /* Send it upstream */
5002 (connp->conn_recv)(connp, mp, NULL, ira);
5003 ira->ira_ill = ill;
5004 ira->ira_rill = rill;
5005 }
5006 }
5007
5008 /*
5009 * Handle protocols with which IP is less intimate. There
5010 * can be more than one stream bound to a particular
5011 * protocol. When this is the case, normally each one gets a copy
5012 * of any incoming packets.
5013 *
5014 * IPsec NOTE :
5015 *
5016 * Don't allow a secure packet going up a non-secure connection.
5017 * We don't allow this because
5018 *
5019 * 1) Reply might go out in clear which will be dropped at
5020 * the sending side.
5021 * 2) If the reply goes out in clear it will give the
5022 * adversary enough information for getting the key in
5023 * most of the cases.
5024 *
5025 * Moreover getting a secure packet when we expect clear
5026 * implies that SA's were added without checking for
5027 * policy on both ends. This should not happen once ISAKMP
5028 * is used to negotiate SAs as SAs will be added only after
5029 * verifying the policy.
5030 *
5031 * Zones notes:
5032 * Earlier in ip_input on a system with multiple shared-IP zones we
5033 * duplicate the multicast and broadcast packets and send them up
5034 * with each explicit zoneid that exists on that ill.
5035 * This means that here we can match the zoneid with SO_ALLZONES being special.
5036 */
5037 void
ip_fanout_proto_v4(mblk_t * mp,ipha_t * ipha,ip_recv_attr_t * ira)5038 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
5039 {
5040 mblk_t *mp1;
5041 ipaddr_t laddr;
5042 conn_t *connp, *first_connp, *next_connp;
5043 connf_t *connfp;
5044 ill_t *ill = ira->ira_ill;
5045 ip_stack_t *ipst = ill->ill_ipst;
5046
5047 laddr = ipha->ipha_dst;
5048
5049 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
5050 mutex_enter(&connfp->connf_lock);
5051 connp = connfp->connf_head;
5052 for (connp = connfp->connf_head; connp != NULL;
5053 connp = connp->conn_next) {
5054 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5055 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5056 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5057 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) {
5058 break;
5059 }
5060 }
5061
5062 if (connp == NULL) {
5063 /*
5064 * No one bound to these addresses. Is
5065 * there a client that wants all
5066 * unclaimed datagrams?
5067 */
5068 mutex_exit(&connfp->connf_lock);
5069 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
5070 ICMP_PROTOCOL_UNREACHABLE, ira);
5071 return;
5072 }
5073
5074 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5075
5076 CONN_INC_REF(connp);
5077 first_connp = connp;
5078 connp = connp->conn_next;
5079
5080 for (;;) {
5081 while (connp != NULL) {
5082 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5083 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5084 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5085 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5086 ira, connp)))
5087 break;
5088 connp = connp->conn_next;
5089 }
5090
5091 if (connp == NULL) {
5092 /* No more interested clients */
5093 connp = first_connp;
5094 break;
5095 }
5096 if (((mp1 = dupmsg(mp)) == NULL) &&
5097 ((mp1 = copymsg(mp)) == NULL)) {
5098 /* Memory allocation failed */
5099 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5100 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5101 connp = first_connp;
5102 break;
5103 }
5104
5105 CONN_INC_REF(connp);
5106 mutex_exit(&connfp->connf_lock);
5107
5108 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
5109 ira);
5110
5111 mutex_enter(&connfp->connf_lock);
5112 /* Follow the next pointer before releasing the conn. */
5113 next_connp = connp->conn_next;
5114 CONN_DEC_REF(connp);
5115 connp = next_connp;
5116 }
5117
5118 /* Last one. Send it upstream. */
5119 mutex_exit(&connfp->connf_lock);
5120
5121 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
5122
5123 CONN_DEC_REF(connp);
5124 }
5125
5126 /*
5127 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
5128 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk
5129 * is not consumed.
5130 *
5131 * One of three things can happen, all of which affect the passed-in mblk:
5132 *
5133 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk..
5134 *
5135 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
5136 * ESP packet, and is passed along to ESP for consumption. Return NULL.
5137 *
5138 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL.
5139 */
5140 mblk_t *
zero_spi_check(mblk_t * mp,ip_recv_attr_t * ira)5141 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
5142 {
5143 int shift, plen, iph_len;
5144 ipha_t *ipha;
5145 udpha_t *udpha;
5146 uint32_t *spi;
5147 uint32_t esp_ports;
5148 uint8_t *orptr;
5149 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
5150 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5151
5152 ipha = (ipha_t *)mp->b_rptr;
5153 iph_len = ira->ira_ip_hdr_length;
5154 plen = ira->ira_pktlen;
5155
5156 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
5157 /*
5158 * Most likely a keepalive for the benefit of an intervening
5159 * NAT. These aren't for us, per se, so drop it.
5160 *
5161 * RFC 3947/8 doesn't say for sure what to do for 2-3
5162 * byte packets (keepalives are 1-byte), but we'll drop them
5163 * also.
5164 */
5165 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5166 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
5167 return (NULL);
5168 }
5169
5170 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
5171 /* might as well pull it all up - it might be ESP. */
5172 if (!pullupmsg(mp, -1)) {
5173 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5174 DROPPER(ipss, ipds_esp_nomem),
5175 &ipss->ipsec_dropper);
5176 return (NULL);
5177 }
5178
5179 ipha = (ipha_t *)mp->b_rptr;
5180 }
5181 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
5182 if (*spi == 0) {
5183 /* UDP packet - remove 0-spi. */
5184 shift = sizeof (uint32_t);
5185 } else {
5186 /* ESP-in-UDP packet - reduce to ESP. */
5187 ipha->ipha_protocol = IPPROTO_ESP;
5188 shift = sizeof (udpha_t);
5189 }
5190
5191 /* Fix IP header */
5192 ira->ira_pktlen = (plen - shift);
5193 ipha->ipha_length = htons(ira->ira_pktlen);
5194 ipha->ipha_hdr_checksum = 0;
5195
5196 orptr = mp->b_rptr;
5197 mp->b_rptr += shift;
5198
5199 udpha = (udpha_t *)(orptr + iph_len);
5200 if (*spi == 0) {
5201 ASSERT((uint8_t *)ipha == orptr);
5202 udpha->uha_length = htons(plen - shift - iph_len);
5203 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
5204 esp_ports = 0;
5205 } else {
5206 esp_ports = *((uint32_t *)udpha);
5207 ASSERT(esp_ports != 0);
5208 }
5209 ovbcopy(orptr, orptr + shift, iph_len);
5210 if (esp_ports != 0) /* Punt up for ESP processing. */ {
5211 ipha = (ipha_t *)(orptr + shift);
5212
5213 ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5214 ira->ira_esp_udp_ports = esp_ports;
5215 ip_fanout_v4(mp, ipha, ira);
5216 return (NULL);
5217 }
5218 return (mp);
5219 }
5220
5221 /*
5222 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5223 * Handles IPv4 and IPv6.
5224 * We are responsible for disposing of mp, such as by freemsg() or putnext()
5225 * Caller is responsible for dropping references to the conn.
5226 */
5227 void
ip_fanout_udp_conn(conn_t * connp,mblk_t * mp,ipha_t * ipha,ip6_t * ip6h,ip_recv_attr_t * ira)5228 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5229 ip_recv_attr_t *ira)
5230 {
5231 ill_t *ill = ira->ira_ill;
5232 ip_stack_t *ipst = ill->ill_ipst;
5233 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5234 boolean_t secure;
5235 iaflags_t iraflags = ira->ira_flags;
5236
5237 secure = iraflags & IRAF_IPSEC_SECURE;
5238
5239 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5240 !canputnext(connp->conn_rq)) {
5241 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5242 freemsg(mp);
5243 return;
5244 }
5245
5246 if (((iraflags & IRAF_IS_IPV4) ?
5247 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5248 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5249 secure) {
5250 mp = ipsec_check_inbound_policy(mp, connp, ipha,
5251 ip6h, ira);
5252 if (mp == NULL) {
5253 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5254 /* Note that mp is NULL */
5255 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5256 return;
5257 }
5258 }
5259
5260 /*
5261 * Since this code is not used for UDP unicast we don't need a NAT_T
5262 * check. Only ip_fanout_v4 has that check.
5263 */
5264 if (ira->ira_flags & IRAF_ICMP_ERROR) {
5265 (connp->conn_recvicmp)(connp, mp, NULL, ira);
5266 } else {
5267 ill_t *rill = ira->ira_rill;
5268
5269 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5270 ira->ira_ill = ira->ira_rill = NULL;
5271 /* Send it upstream */
5272 (connp->conn_recv)(connp, mp, NULL, ira);
5273 ira->ira_ill = ill;
5274 ira->ira_rill = rill;
5275 }
5276 }
5277
5278 /*
5279 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5280 * (Unicast fanout is handled in ip_input_v4.)
5281 *
5282 * If SO_REUSEADDR is set all multicast and broadcast packets
5283 * will be delivered to all conns bound to the same port.
5284 *
5285 * If there is at least one matching AF_INET receiver, then we will
5286 * ignore any AF_INET6 receivers.
5287 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5288 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5289 * packets.
5290 *
5291 * Zones notes:
5292 * Earlier in ip_input on a system with multiple shared-IP zones we
5293 * duplicate the multicast and broadcast packets and send them up
5294 * with each explicit zoneid that exists on that ill.
5295 * This means that here we can match the zoneid with SO_ALLZONES being special.
5296 */
5297 void
ip_fanout_udp_multi_v4(mblk_t * mp,ipha_t * ipha,uint16_t lport,uint16_t fport,ip_recv_attr_t * ira)5298 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5299 ip_recv_attr_t *ira)
5300 {
5301 ipaddr_t laddr;
5302 in6_addr_t v6faddr;
5303 conn_t *connp;
5304 connf_t *connfp;
5305 ipaddr_t faddr;
5306 ill_t *ill = ira->ira_ill;
5307 ip_stack_t *ipst = ill->ill_ipst;
5308
5309 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5310
5311 laddr = ipha->ipha_dst;
5312 faddr = ipha->ipha_src;
5313
5314 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5315 mutex_enter(&connfp->connf_lock);
5316 connp = connfp->connf_head;
5317
5318 /*
5319 * If SO_REUSEADDR has been set on the first we send the
5320 * packet to all clients that have joined the group and
5321 * match the port.
5322 */
5323 while (connp != NULL) {
5324 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5325 conn_wantpacket(connp, ira, ipha) &&
5326 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5327 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5328 break;
5329 connp = connp->conn_next;
5330 }
5331
5332 if (connp == NULL)
5333 goto notfound;
5334
5335 CONN_INC_REF(connp);
5336
5337 if (connp->conn_reuseaddr) {
5338 conn_t *first_connp = connp;
5339 conn_t *next_connp;
5340 mblk_t *mp1;
5341
5342 connp = connp->conn_next;
5343 for (;;) {
5344 while (connp != NULL) {
5345 if (IPCL_UDP_MATCH(connp, lport, laddr,
5346 fport, faddr) &&
5347 conn_wantpacket(connp, ira, ipha) &&
5348 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5349 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5350 ira, connp)))
5351 break;
5352 connp = connp->conn_next;
5353 }
5354 if (connp == NULL) {
5355 /* No more interested clients */
5356 connp = first_connp;
5357 break;
5358 }
5359 if (((mp1 = dupmsg(mp)) == NULL) &&
5360 ((mp1 = copymsg(mp)) == NULL)) {
5361 /* Memory allocation failed */
5362 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5363 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5364 connp = first_connp;
5365 break;
5366 }
5367 CONN_INC_REF(connp);
5368 mutex_exit(&connfp->connf_lock);
5369
5370 IP_STAT(ipst, ip_udp_fanmb);
5371 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5372 NULL, ira);
5373 mutex_enter(&connfp->connf_lock);
5374 /* Follow the next pointer before releasing the conn */
5375 next_connp = connp->conn_next;
5376 CONN_DEC_REF(connp);
5377 connp = next_connp;
5378 }
5379 }
5380
5381 /* Last one. Send it upstream. */
5382 mutex_exit(&connfp->connf_lock);
5383 IP_STAT(ipst, ip_udp_fanmb);
5384 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5385 CONN_DEC_REF(connp);
5386 return;
5387
5388 notfound:
5389 mutex_exit(&connfp->connf_lock);
5390 /*
5391 * IPv6 endpoints bound to multicast IPv4-mapped addresses
5392 * have already been matched above, since they live in the IPv4
5393 * fanout tables. This implies we only need to
5394 * check for IPv6 in6addr_any endpoints here.
5395 * Thus we compare using ipv6_all_zeros instead of the destination
5396 * address, except for the multicast group membership lookup which
5397 * uses the IPv4 destination.
5398 */
5399 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5400 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5401 mutex_enter(&connfp->connf_lock);
5402 connp = connfp->connf_head;
5403 /*
5404 * IPv4 multicast packet being delivered to an AF_INET6
5405 * in6addr_any endpoint.
5406 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5407 * and not conn_wantpacket_v6() since any multicast membership is
5408 * for an IPv4-mapped multicast address.
5409 */
5410 while (connp != NULL) {
5411 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5412 fport, v6faddr) &&
5413 conn_wantpacket(connp, ira, ipha) &&
5414 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5415 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5416 break;
5417 connp = connp->conn_next;
5418 }
5419
5420 if (connp == NULL) {
5421 /*
5422 * No one bound to this port. Is
5423 * there a client that wants all
5424 * unclaimed datagrams?
5425 */
5426 mutex_exit(&connfp->connf_lock);
5427
5428 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5429 NULL) {
5430 ASSERT(ira->ira_protocol == IPPROTO_UDP);
5431 ip_fanout_proto_v4(mp, ipha, ira);
5432 } else {
5433 /*
5434 * We used to attempt to send an icmp error here, but
5435 * since this is known to be a multicast packet
5436 * and we don't send icmp errors in response to
5437 * multicast, just drop the packet and give up sooner.
5438 */
5439 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5440 freemsg(mp);
5441 }
5442 return;
5443 }
5444 CONN_INC_REF(connp);
5445 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5446
5447 /*
5448 * If SO_REUSEADDR has been set on the first we send the
5449 * packet to all clients that have joined the group and
5450 * match the port.
5451 */
5452 if (connp->conn_reuseaddr) {
5453 conn_t *first_connp = connp;
5454 conn_t *next_connp;
5455 mblk_t *mp1;
5456
5457 connp = connp->conn_next;
5458 for (;;) {
5459 while (connp != NULL) {
5460 if (IPCL_UDP_MATCH_V6(connp, lport,
5461 ipv6_all_zeros, fport, v6faddr) &&
5462 conn_wantpacket(connp, ira, ipha) &&
5463 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5464 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5465 ira, connp)))
5466 break;
5467 connp = connp->conn_next;
5468 }
5469 if (connp == NULL) {
5470 /* No more interested clients */
5471 connp = first_connp;
5472 break;
5473 }
5474 if (((mp1 = dupmsg(mp)) == NULL) &&
5475 ((mp1 = copymsg(mp)) == NULL)) {
5476 /* Memory allocation failed */
5477 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5478 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5479 connp = first_connp;
5480 break;
5481 }
5482 CONN_INC_REF(connp);
5483 mutex_exit(&connfp->connf_lock);
5484
5485 IP_STAT(ipst, ip_udp_fanmb);
5486 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5487 NULL, ira);
5488 mutex_enter(&connfp->connf_lock);
5489 /* Follow the next pointer before releasing the conn */
5490 next_connp = connp->conn_next;
5491 CONN_DEC_REF(connp);
5492 connp = next_connp;
5493 }
5494 }
5495
5496 /* Last one. Send it upstream. */
5497 mutex_exit(&connfp->connf_lock);
5498 IP_STAT(ipst, ip_udp_fanmb);
5499 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5500 CONN_DEC_REF(connp);
5501 }
5502
5503 /*
5504 * Split an incoming packet's IPv4 options into the label and the other options.
5505 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5506 * clearing out any leftover label or options.
5507 * Otherwise it just makes ipp point into the packet.
5508 *
5509 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5510 */
5511 int
ip_find_hdr_v4(ipha_t * ipha,ip_pkt_t * ipp,boolean_t allocate)5512 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5513 {
5514 uchar_t *opt;
5515 uint32_t totallen;
5516 uint32_t optval;
5517 uint32_t optlen;
5518
5519 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5520 ipp->ipp_hoplimit = ipha->ipha_ttl;
5521 ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5522 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5523
5524 /*
5525 * Get length (in 4 byte octets) of IP header options.
5526 */
5527 totallen = ipha->ipha_version_and_hdr_length -
5528 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5529
5530 if (totallen == 0) {
5531 if (!allocate)
5532 return (0);
5533
5534 /* Clear out anything from a previous packet */
5535 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5536 kmem_free(ipp->ipp_ipv4_options,
5537 ipp->ipp_ipv4_options_len);
5538 ipp->ipp_ipv4_options = NULL;
5539 ipp->ipp_ipv4_options_len = 0;
5540 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5541 }
5542 if (ipp->ipp_fields & IPPF_LABEL_V4) {
5543 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5544 ipp->ipp_label_v4 = NULL;
5545 ipp->ipp_label_len_v4 = 0;
5546 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5547 }
5548 return (0);
5549 }
5550
5551 totallen <<= 2;
5552 opt = (uchar_t *)&ipha[1];
5553 if (!is_system_labeled()) {
5554
5555 copyall:
5556 if (!allocate) {
5557 if (totallen != 0) {
5558 ipp->ipp_ipv4_options = opt;
5559 ipp->ipp_ipv4_options_len = totallen;
5560 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5561 }
5562 return (0);
5563 }
5564 /* Just copy all of options */
5565 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5566 if (totallen == ipp->ipp_ipv4_options_len) {
5567 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5568 return (0);
5569 }
5570 kmem_free(ipp->ipp_ipv4_options,
5571 ipp->ipp_ipv4_options_len);
5572 ipp->ipp_ipv4_options = NULL;
5573 ipp->ipp_ipv4_options_len = 0;
5574 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5575 }
5576 if (totallen == 0)
5577 return (0);
5578
5579 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5580 if (ipp->ipp_ipv4_options == NULL)
5581 return (ENOMEM);
5582 ipp->ipp_ipv4_options_len = totallen;
5583 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5584 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5585 return (0);
5586 }
5587
5588 if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) {
5589 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5590 ipp->ipp_label_v4 = NULL;
5591 ipp->ipp_label_len_v4 = 0;
5592 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5593 }
5594
5595 /*
5596 * Search for CIPSO option.
5597 * We assume CIPSO is first in options if it is present.
5598 * If it isn't, then ipp_opt_ipv4_options will not include the options
5599 * prior to the CIPSO option.
5600 */
5601 while (totallen != 0) {
5602 switch (optval = opt[IPOPT_OPTVAL]) {
5603 case IPOPT_EOL:
5604 return (0);
5605 case IPOPT_NOP:
5606 optlen = 1;
5607 break;
5608 default:
5609 if (totallen <= IPOPT_OLEN)
5610 return (EINVAL);
5611 optlen = opt[IPOPT_OLEN];
5612 if (optlen < 2)
5613 return (EINVAL);
5614 }
5615 if (optlen > totallen)
5616 return (EINVAL);
5617
5618 switch (optval) {
5619 case IPOPT_COMSEC:
5620 if (!allocate) {
5621 ipp->ipp_label_v4 = opt;
5622 ipp->ipp_label_len_v4 = optlen;
5623 ipp->ipp_fields |= IPPF_LABEL_V4;
5624 } else {
5625 ipp->ipp_label_v4 = kmem_alloc(optlen,
5626 KM_NOSLEEP);
5627 if (ipp->ipp_label_v4 == NULL)
5628 return (ENOMEM);
5629 ipp->ipp_label_len_v4 = optlen;
5630 ipp->ipp_fields |= IPPF_LABEL_V4;
5631 bcopy(opt, ipp->ipp_label_v4, optlen);
5632 }
5633 totallen -= optlen;
5634 opt += optlen;
5635
5636 /* Skip padding bytes until we get to a multiple of 4 */
5637 while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) {
5638 totallen--;
5639 opt++;
5640 }
5641 /* Remaining as ipp_ipv4_options */
5642 goto copyall;
5643 }
5644 totallen -= optlen;
5645 opt += optlen;
5646 }
5647 /* No CIPSO found; return everything as ipp_ipv4_options */
5648 totallen = ipha->ipha_version_and_hdr_length -
5649 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5650 totallen <<= 2;
5651 opt = (uchar_t *)&ipha[1];
5652 goto copyall;
5653 }
5654
5655 /*
5656 * Efficient versions of lookup for an IRE when we only
5657 * match the address.
5658 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5659 * Does not handle multicast addresses.
5660 */
5661 uint_t
ip_type_v4(ipaddr_t addr,ip_stack_t * ipst)5662 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5663 {
5664 ire_t *ire;
5665 uint_t result;
5666
5667 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5668 ASSERT(ire != NULL);
5669 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5670 result = IRE_NOROUTE;
5671 else
5672 result = ire->ire_type;
5673 ire_refrele(ire);
5674 return (result);
5675 }
5676
5677 /*
5678 * Efficient versions of lookup for an IRE when we only
5679 * match the address.
5680 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5681 * Does not handle multicast addresses.
5682 */
5683 uint_t
ip_type_v6(const in6_addr_t * addr,ip_stack_t * ipst)5684 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5685 {
5686 ire_t *ire;
5687 uint_t result;
5688
5689 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5690 ASSERT(ire != NULL);
5691 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5692 result = IRE_NOROUTE;
5693 else
5694 result = ire->ire_type;
5695 ire_refrele(ire);
5696 return (result);
5697 }
5698
5699 /*
5700 * Nobody should be sending
5701 * packets up this stream
5702 */
5703 static int
ip_lrput(queue_t * q,mblk_t * mp)5704 ip_lrput(queue_t *q, mblk_t *mp)
5705 {
5706 switch (mp->b_datap->db_type) {
5707 case M_FLUSH:
5708 /* Turn around */
5709 if (*mp->b_rptr & FLUSHW) {
5710 *mp->b_rptr &= ~FLUSHR;
5711 qreply(q, mp);
5712 return (0);
5713 }
5714 break;
5715 }
5716 freemsg(mp);
5717 return (0);
5718 }
5719
5720 /* Nobody should be sending packets down this stream */
5721 /* ARGSUSED */
5722 int
ip_lwput(queue_t * q,mblk_t * mp)5723 ip_lwput(queue_t *q, mblk_t *mp)
5724 {
5725 freemsg(mp);
5726 return (0);
5727 }
5728
5729 /*
5730 * Move the first hop in any source route to ipha_dst and remove that part of
5731 * the source route. Called by other protocols. Errors in option formatting
5732 * are ignored - will be handled by ip_output_options. Return the final
5733 * destination (either ipha_dst or the last entry in a source route.)
5734 */
5735 ipaddr_t
ip_massage_options(ipha_t * ipha,netstack_t * ns)5736 ip_massage_options(ipha_t *ipha, netstack_t *ns)
5737 {
5738 ipoptp_t opts;
5739 uchar_t *opt;
5740 uint8_t optval;
5741 uint8_t optlen;
5742 ipaddr_t dst;
5743 int i;
5744 ip_stack_t *ipst = ns->netstack_ip;
5745
5746 ip2dbg(("ip_massage_options\n"));
5747 dst = ipha->ipha_dst;
5748 for (optval = ipoptp_first(&opts, ipha);
5749 optval != IPOPT_EOL;
5750 optval = ipoptp_next(&opts)) {
5751 opt = opts.ipoptp_cur;
5752 switch (optval) {
5753 uint8_t off;
5754 case IPOPT_SSRR:
5755 case IPOPT_LSRR:
5756 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5757 ip1dbg(("ip_massage_options: bad src route\n"));
5758 break;
5759 }
5760 optlen = opts.ipoptp_len;
5761 off = opt[IPOPT_OFFSET];
5762 off--;
5763 redo_srr:
5764 if (optlen < IP_ADDR_LEN ||
5765 off > optlen - IP_ADDR_LEN) {
5766 /* End of source route */
5767 ip1dbg(("ip_massage_options: end of SR\n"));
5768 break;
5769 }
5770 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5771 ip1dbg(("ip_massage_options: next hop 0x%x\n",
5772 ntohl(dst)));
5773 /*
5774 * Check if our address is present more than
5775 * once as consecutive hops in source route.
5776 * XXX verify per-interface ip_forwarding
5777 * for source route?
5778 */
5779 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5780 off += IP_ADDR_LEN;
5781 goto redo_srr;
5782 }
5783 if (dst == htonl(INADDR_LOOPBACK)) {
5784 ip1dbg(("ip_massage_options: loopback addr in "
5785 "source route!\n"));
5786 break;
5787 }
5788 /*
5789 * Update ipha_dst to be the first hop and remove the
5790 * first hop from the source route (by overwriting
5791 * part of the option with NOP options).
5792 */
5793 ipha->ipha_dst = dst;
5794 /* Put the last entry in dst */
5795 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5796 3;
5797 bcopy(&opt[off], &dst, IP_ADDR_LEN);
5798
5799 ip1dbg(("ip_massage_options: last hop 0x%x\n",
5800 ntohl(dst)));
5801 /* Move down and overwrite */
5802 opt[IP_ADDR_LEN] = opt[0];
5803 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5804 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5805 for (i = 0; i < IP_ADDR_LEN; i++)
5806 opt[i] = IPOPT_NOP;
5807 break;
5808 }
5809 }
5810 return (dst);
5811 }
5812
5813 /*
5814 * Return the network mask
5815 * associated with the specified address.
5816 */
5817 ipaddr_t
ip_net_mask(ipaddr_t addr)5818 ip_net_mask(ipaddr_t addr)
5819 {
5820 uchar_t *up = (uchar_t *)&addr;
5821 ipaddr_t mask = 0;
5822 uchar_t *maskp = (uchar_t *)&mask;
5823
5824 #if defined(__x86)
5825 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5826 #endif
5827 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
5828 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5829 #endif
5830 if (CLASSD(addr)) {
5831 maskp[0] = 0xF0;
5832 return (mask);
5833 }
5834
5835 /* We assume Class E default netmask to be 32 */
5836 if (CLASSE(addr))
5837 return (0xffffffffU);
5838
5839 if (addr == 0)
5840 return (0);
5841 maskp[0] = 0xFF;
5842 if ((up[0] & 0x80) == 0)
5843 return (mask);
5844
5845 maskp[1] = 0xFF;
5846 if ((up[0] & 0xC0) == 0x80)
5847 return (mask);
5848
5849 maskp[2] = 0xFF;
5850 if ((up[0] & 0xE0) == 0xC0)
5851 return (mask);
5852
5853 /* Otherwise return no mask */
5854 return ((ipaddr_t)0);
5855 }
5856
5857 /* Name/Value Table Lookup Routine */
5858 char *
ip_nv_lookup(nv_t * nv,int value)5859 ip_nv_lookup(nv_t *nv, int value)
5860 {
5861 if (!nv)
5862 return (NULL);
5863 for (; nv->nv_name; nv++) {
5864 if (nv->nv_value == value)
5865 return (nv->nv_name);
5866 }
5867 return ("unknown");
5868 }
5869
5870 static int
ip_wait_for_info_ack(ill_t * ill)5871 ip_wait_for_info_ack(ill_t *ill)
5872 {
5873 int err;
5874
5875 mutex_enter(&ill->ill_lock);
5876 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5877 /*
5878 * Return value of 0 indicates a pending signal.
5879 */
5880 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5881 if (err == 0) {
5882 mutex_exit(&ill->ill_lock);
5883 return (EINTR);
5884 }
5885 }
5886 mutex_exit(&ill->ill_lock);
5887 /*
5888 * ip_rput_other could have set an error in ill_error on
5889 * receipt of M_ERROR.
5890 */
5891 return (ill->ill_error);
5892 }
5893
5894 /*
5895 * This is a module open, i.e. this is a control stream for access
5896 * to a DLPI device. We allocate an ill_t as the instance data in
5897 * this case.
5898 */
5899 static int
ip_modopen(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)5900 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5901 {
5902 ill_t *ill;
5903 int err;
5904 zoneid_t zoneid;
5905 netstack_t *ns;
5906 ip_stack_t *ipst;
5907
5908 /*
5909 * Prevent unprivileged processes from pushing IP so that
5910 * they can't send raw IP.
5911 */
5912 if (secpolicy_net_rawaccess(credp) != 0)
5913 return (EPERM);
5914
5915 ns = netstack_find_by_cred(credp);
5916 ASSERT(ns != NULL);
5917 ipst = ns->netstack_ip;
5918 ASSERT(ipst != NULL);
5919
5920 /*
5921 * For exclusive stacks we set the zoneid to zero
5922 * to make IP operate as if in the global zone.
5923 */
5924 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5925 zoneid = GLOBAL_ZONEID;
5926 else
5927 zoneid = crgetzoneid(credp);
5928
5929 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5930 q->q_ptr = WR(q)->q_ptr = ill;
5931 ill->ill_ipst = ipst;
5932 ill->ill_zoneid = zoneid;
5933
5934 /*
5935 * ill_init initializes the ill fields and then sends down
5936 * down a DL_INFO_REQ after calling qprocson.
5937 */
5938 err = ill_init(q, ill);
5939
5940 if (err != 0) {
5941 mi_free(ill);
5942 netstack_rele(ipst->ips_netstack);
5943 q->q_ptr = NULL;
5944 WR(q)->q_ptr = NULL;
5945 return (err);
5946 }
5947
5948 /*
5949 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5950 *
5951 * ill_init initializes the ipsq marking this thread as
5952 * writer
5953 */
5954 ipsq_exit(ill->ill_phyint->phyint_ipsq);
5955 err = ip_wait_for_info_ack(ill);
5956 if (err == 0)
5957 ill->ill_credp = credp;
5958 else
5959 goto fail;
5960
5961 crhold(credp);
5962
5963 mutex_enter(&ipst->ips_ip_mi_lock);
5964 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5965 sflag, credp);
5966 mutex_exit(&ipst->ips_ip_mi_lock);
5967 fail:
5968 if (err) {
5969 (void) ip_close(q, 0, credp);
5970 return (err);
5971 }
5972 return (0);
5973 }
5974
5975 /* For /dev/ip aka AF_INET open */
5976 int
ip_openv4(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)5977 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5978 {
5979 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5980 }
5981
5982 /* For /dev/ip6 aka AF_INET6 open */
5983 int
ip_openv6(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)5984 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5985 {
5986 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5987 }
5988
5989 /* IP open routine. */
5990 int
ip_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp,boolean_t isv6)5991 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5992 boolean_t isv6)
5993 {
5994 conn_t *connp;
5995 major_t maj;
5996 zoneid_t zoneid;
5997 netstack_t *ns;
5998 ip_stack_t *ipst;
5999
6000 /* Allow reopen. */
6001 if (q->q_ptr != NULL)
6002 return (0);
6003
6004 if (sflag & MODOPEN) {
6005 /* This is a module open */
6006 return (ip_modopen(q, devp, flag, sflag, credp));
6007 }
6008
6009 if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
6010 /*
6011 * Non streams based socket looking for a stream
6012 * to access IP
6013 */
6014 return (ip_helper_stream_setup(q, devp, flag, sflag,
6015 credp, isv6));
6016 }
6017
6018 ns = netstack_find_by_cred(credp);
6019 ASSERT(ns != NULL);
6020 ipst = ns->netstack_ip;
6021 ASSERT(ipst != NULL);
6022
6023 /*
6024 * For exclusive stacks we set the zoneid to zero
6025 * to make IP operate as if in the global zone.
6026 */
6027 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
6028 zoneid = GLOBAL_ZONEID;
6029 else
6030 zoneid = crgetzoneid(credp);
6031
6032 /*
6033 * We are opening as a device. This is an IP client stream, and we
6034 * allocate an conn_t as the instance data.
6035 */
6036 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
6037
6038 /*
6039 * ipcl_conn_create did a netstack_hold. Undo the hold that was
6040 * done by netstack_find_by_cred()
6041 */
6042 netstack_rele(ipst->ips_netstack);
6043
6044 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
6045 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
6046 connp->conn_ixa->ixa_zoneid = zoneid;
6047 connp->conn_zoneid = zoneid;
6048
6049 connp->conn_rq = q;
6050 q->q_ptr = WR(q)->q_ptr = connp;
6051
6052 /* Minor tells us which /dev entry was opened */
6053 if (isv6) {
6054 connp->conn_family = AF_INET6;
6055 connp->conn_ipversion = IPV6_VERSION;
6056 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
6057 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
6058 } else {
6059 connp->conn_family = AF_INET;
6060 connp->conn_ipversion = IPV4_VERSION;
6061 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
6062 }
6063
6064 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
6065 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
6066 connp->conn_minor_arena = ip_minor_arena_la;
6067 } else {
6068 /*
6069 * Either minor numbers in the large arena were exhausted
6070 * or a non socket application is doing the open.
6071 * Try to allocate from the small arena.
6072 */
6073 if ((connp->conn_dev =
6074 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
6075 /* CONN_DEC_REF takes care of netstack_rele() */
6076 q->q_ptr = WR(q)->q_ptr = NULL;
6077 CONN_DEC_REF(connp);
6078 return (EBUSY);
6079 }
6080 connp->conn_minor_arena = ip_minor_arena_sa;
6081 }
6082
6083 maj = getemajor(*devp);
6084 *devp = makedevice(maj, (minor_t)connp->conn_dev);
6085
6086 /*
6087 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
6088 */
6089 connp->conn_cred = credp;
6090 connp->conn_cpid = curproc->p_pid;
6091 /* Cache things in ixa without an extra refhold */
6092 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
6093 connp->conn_ixa->ixa_cred = connp->conn_cred;
6094 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
6095 if (is_system_labeled())
6096 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6097
6098 /*
6099 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
6100 */
6101 connp->conn_recv = ip_conn_input;
6102 connp->conn_recvicmp = ip_conn_input_icmp;
6103
6104 crhold(connp->conn_cred);
6105
6106 /*
6107 * If the caller has the process-wide flag set, then default to MAC
6108 * exempt mode. This allows read-down to unlabeled hosts.
6109 */
6110 if (getpflags(NET_MAC_AWARE, credp) != 0)
6111 connp->conn_mac_mode = CONN_MAC_AWARE;
6112
6113 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
6114
6115 connp->conn_rq = q;
6116 connp->conn_wq = WR(q);
6117
6118 /* Non-zero default values */
6119 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
6120
6121 /*
6122 * Make the conn globally visible to walkers
6123 */
6124 ASSERT(connp->conn_ref == 1);
6125 mutex_enter(&connp->conn_lock);
6126 connp->conn_state_flags &= ~CONN_INCIPIENT;
6127 mutex_exit(&connp->conn_lock);
6128
6129 qprocson(q);
6130
6131 return (0);
6132 }
6133
6134 /*
6135 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
6136 * all of them are copied to the conn_t. If the req is "zero", the policy is
6137 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
6138 * fields.
6139 * We keep only the latest setting of the policy and thus policy setting
6140 * is not incremental/cumulative.
6141 *
6142 * Requests to set policies with multiple alternative actions will
6143 * go through a different API.
6144 */
6145 int
ipsec_set_req(cred_t * cr,conn_t * connp,ipsec_req_t * req)6146 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
6147 {
6148 uint_t ah_req = 0;
6149 uint_t esp_req = 0;
6150 uint_t se_req = 0;
6151 ipsec_act_t *actp = NULL;
6152 uint_t nact;
6153 ipsec_policy_head_t *ph;
6154 boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
6155 int error = 0;
6156 netstack_t *ns = connp->conn_netstack;
6157 ip_stack_t *ipst = ns->netstack_ip;
6158 ipsec_stack_t *ipss = ns->netstack_ipsec;
6159
6160 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
6161
6162 /*
6163 * The IP_SEC_OPT option does not allow variable length parameters,
6164 * hence a request cannot be NULL.
6165 */
6166 if (req == NULL)
6167 return (EINVAL);
6168
6169 ah_req = req->ipsr_ah_req;
6170 esp_req = req->ipsr_esp_req;
6171 se_req = req->ipsr_self_encap_req;
6172
6173 /* Don't allow setting self-encap without one or more of AH/ESP. */
6174 if (se_req != 0 && esp_req == 0 && ah_req == 0)
6175 return (EINVAL);
6176
6177 /*
6178 * Are we dealing with a request to reset the policy (i.e.
6179 * zero requests).
6180 */
6181 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
6182 (esp_req & REQ_MASK) == 0 &&
6183 (se_req & REQ_MASK) == 0);
6184
6185 if (!is_pol_reset) {
6186 /*
6187 * If we couldn't load IPsec, fail with "protocol
6188 * not supported".
6189 * IPsec may not have been loaded for a request with zero
6190 * policies, so we don't fail in this case.
6191 */
6192 mutex_enter(&ipss->ipsec_loader_lock);
6193 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
6194 mutex_exit(&ipss->ipsec_loader_lock);
6195 return (EPROTONOSUPPORT);
6196 }
6197 mutex_exit(&ipss->ipsec_loader_lock);
6198
6199 /*
6200 * Test for valid requests. Invalid algorithms
6201 * need to be tested by IPsec code because new
6202 * algorithms can be added dynamically.
6203 */
6204 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6205 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6206 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
6207 return (EINVAL);
6208 }
6209
6210 /*
6211 * Only privileged users can issue these
6212 * requests.
6213 */
6214 if (((ah_req & IPSEC_PREF_NEVER) ||
6215 (esp_req & IPSEC_PREF_NEVER) ||
6216 (se_req & IPSEC_PREF_NEVER)) &&
6217 secpolicy_ip_config(cr, B_FALSE) != 0) {
6218 return (EPERM);
6219 }
6220
6221 /*
6222 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
6223 * are mutually exclusive.
6224 */
6225 if (((ah_req & REQ_MASK) == REQ_MASK) ||
6226 ((esp_req & REQ_MASK) == REQ_MASK) ||
6227 ((se_req & REQ_MASK) == REQ_MASK)) {
6228 /* Both of them are set */
6229 return (EINVAL);
6230 }
6231 }
6232
6233 ASSERT(MUTEX_HELD(&connp->conn_lock));
6234
6235 /*
6236 * If we have already cached policies in conn_connect(), don't
6237 * let them change now. We cache policies for connections
6238 * whose src,dst [addr, port] is known.
6239 */
6240 if (connp->conn_policy_cached) {
6241 return (EINVAL);
6242 }
6243
6244 /*
6245 * We have a zero policies, reset the connection policy if already
6246 * set. This will cause the connection to inherit the
6247 * global policy, if any.
6248 */
6249 if (is_pol_reset) {
6250 if (connp->conn_policy != NULL) {
6251 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
6252 connp->conn_policy = NULL;
6253 }
6254 connp->conn_in_enforce_policy = B_FALSE;
6255 connp->conn_out_enforce_policy = B_FALSE;
6256 return (0);
6257 }
6258
6259 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
6260 ipst->ips_netstack);
6261 if (ph == NULL)
6262 goto enomem;
6263
6264 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
6265 if (actp == NULL)
6266 goto enomem;
6267
6268 /*
6269 * Always insert IPv4 policy entries, since they can also apply to
6270 * ipv6 sockets being used in ipv4-compat mode.
6271 */
6272 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6273 IPSEC_TYPE_INBOUND, ns))
6274 goto enomem;
6275 is_pol_inserted = B_TRUE;
6276 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6277 IPSEC_TYPE_OUTBOUND, ns))
6278 goto enomem;
6279
6280 /*
6281 * We're looking at a v6 socket, also insert the v6-specific
6282 * entries.
6283 */
6284 if (connp->conn_family == AF_INET6) {
6285 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6286 IPSEC_TYPE_INBOUND, ns))
6287 goto enomem;
6288 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6289 IPSEC_TYPE_OUTBOUND, ns))
6290 goto enomem;
6291 }
6292
6293 ipsec_actvec_free(actp, nact);
6294
6295 /*
6296 * If the requests need security, set enforce_policy.
6297 * If the requests are IPSEC_PREF_NEVER, one should
6298 * still set conn_out_enforce_policy so that ip_set_destination
6299 * marks the ip_xmit_attr_t appropriatly. This is needed so that
6300 * for connections that we don't cache policy in at connect time,
6301 * if global policy matches in ip_output_attach_policy, we
6302 * don't wrongly inherit global policy. Similarly, we need
6303 * to set conn_in_enforce_policy also so that we don't verify
6304 * policy wrongly.
6305 */
6306 if ((ah_req & REQ_MASK) != 0 ||
6307 (esp_req & REQ_MASK) != 0 ||
6308 (se_req & REQ_MASK) != 0) {
6309 connp->conn_in_enforce_policy = B_TRUE;
6310 connp->conn_out_enforce_policy = B_TRUE;
6311 }
6312
6313 return (error);
6314 #undef REQ_MASK
6315
6316 /*
6317 * Common memory-allocation-failure exit path.
6318 */
6319 enomem:
6320 if (actp != NULL)
6321 ipsec_actvec_free(actp, nact);
6322 if (is_pol_inserted)
6323 ipsec_polhead_flush(ph, ns);
6324 return (ENOMEM);
6325 }
6326
6327 /*
6328 * Set socket options for joining and leaving multicast groups.
6329 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6330 * The caller has already check that the option name is consistent with
6331 * the address family of the socket.
6332 */
6333 int
ip_opt_set_multicast_group(conn_t * connp,t_scalar_t name,uchar_t * invalp,boolean_t inet6,boolean_t checkonly)6334 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6335 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6336 {
6337 int *i1 = (int *)invalp;
6338 int error = 0;
6339 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6340 struct ip_mreq *v4_mreqp;
6341 struct ipv6_mreq *v6_mreqp;
6342 struct group_req *greqp;
6343 ire_t *ire;
6344 boolean_t done = B_FALSE;
6345 ipaddr_t ifaddr;
6346 in6_addr_t v6group;
6347 uint_t ifindex;
6348 boolean_t mcast_opt = B_TRUE;
6349 mcast_record_t fmode;
6350 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6351 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6352
6353 switch (name) {
6354 case IP_ADD_MEMBERSHIP:
6355 case IPV6_JOIN_GROUP:
6356 mcast_opt = B_FALSE;
6357 /* FALLTHROUGH */
6358 case MCAST_JOIN_GROUP:
6359 fmode = MODE_IS_EXCLUDE;
6360 optfn = ip_opt_add_group;
6361 break;
6362
6363 case IP_DROP_MEMBERSHIP:
6364 case IPV6_LEAVE_GROUP:
6365 mcast_opt = B_FALSE;
6366 /* FALLTHROUGH */
6367 case MCAST_LEAVE_GROUP:
6368 fmode = MODE_IS_INCLUDE;
6369 optfn = ip_opt_delete_group;
6370 break;
6371 default:
6372 /* Should not be reached. */
6373 fmode = MODE_IS_INCLUDE;
6374 optfn = NULL;
6375 ASSERT(0);
6376 }
6377
6378 if (mcast_opt) {
6379 struct sockaddr_in *sin;
6380 struct sockaddr_in6 *sin6;
6381
6382 greqp = (struct group_req *)i1;
6383 if (greqp->gr_group.ss_family == AF_INET) {
6384 sin = (struct sockaddr_in *)&(greqp->gr_group);
6385 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6386 } else {
6387 if (!inet6)
6388 return (EINVAL); /* Not on INET socket */
6389
6390 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6391 v6group = sin6->sin6_addr;
6392 }
6393 ifaddr = INADDR_ANY;
6394 ifindex = greqp->gr_interface;
6395 } else if (inet6) {
6396 v6_mreqp = (struct ipv6_mreq *)i1;
6397 v6group = v6_mreqp->ipv6mr_multiaddr;
6398 ifaddr = INADDR_ANY;
6399 ifindex = v6_mreqp->ipv6mr_interface;
6400 } else {
6401 v4_mreqp = (struct ip_mreq *)i1;
6402 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6403 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6404 ifindex = 0;
6405 }
6406
6407 /*
6408 * In the multirouting case, we need to replicate
6409 * the request on all interfaces that will take part
6410 * in replication. We do so because multirouting is
6411 * reflective, thus we will probably receive multi-
6412 * casts on those interfaces.
6413 * The ip_multirt_apply_membership() succeeds if
6414 * the operation succeeds on at least one interface.
6415 */
6416 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6417 ipaddr_t group;
6418
6419 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6420
6421 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6422 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6423 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6424 } else {
6425 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6426 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6427 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6428 }
6429 if (ire != NULL) {
6430 if (ire->ire_flags & RTF_MULTIRT) {
6431 error = ip_multirt_apply_membership(optfn, ire, connp,
6432 checkonly, &v6group, fmode, &ipv6_all_zeros);
6433 done = B_TRUE;
6434 }
6435 ire_refrele(ire);
6436 }
6437
6438 if (!done) {
6439 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6440 fmode, &ipv6_all_zeros);
6441 }
6442 return (error);
6443 }
6444
6445 /*
6446 * Set socket options for joining and leaving multicast groups
6447 * for specific sources.
6448 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6449 * The caller has already check that the option name is consistent with
6450 * the address family of the socket.
6451 */
6452 int
ip_opt_set_multicast_sources(conn_t * connp,t_scalar_t name,uchar_t * invalp,boolean_t inet6,boolean_t checkonly)6453 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6454 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6455 {
6456 int *i1 = (int *)invalp;
6457 int error = 0;
6458 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6459 struct ip_mreq_source *imreqp;
6460 struct group_source_req *gsreqp;
6461 in6_addr_t v6group, v6src;
6462 uint32_t ifindex;
6463 ipaddr_t ifaddr;
6464 boolean_t mcast_opt = B_TRUE;
6465 mcast_record_t fmode;
6466 ire_t *ire;
6467 boolean_t done = B_FALSE;
6468 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6469 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6470
6471 switch (name) {
6472 case IP_BLOCK_SOURCE:
6473 mcast_opt = B_FALSE;
6474 /* FALLTHROUGH */
6475 case MCAST_BLOCK_SOURCE:
6476 fmode = MODE_IS_EXCLUDE;
6477 optfn = ip_opt_add_group;
6478 break;
6479
6480 case IP_UNBLOCK_SOURCE:
6481 mcast_opt = B_FALSE;
6482 /* FALLTHROUGH */
6483 case MCAST_UNBLOCK_SOURCE:
6484 fmode = MODE_IS_EXCLUDE;
6485 optfn = ip_opt_delete_group;
6486 break;
6487
6488 case IP_ADD_SOURCE_MEMBERSHIP:
6489 mcast_opt = B_FALSE;
6490 /* FALLTHROUGH */
6491 case MCAST_JOIN_SOURCE_GROUP:
6492 fmode = MODE_IS_INCLUDE;
6493 optfn = ip_opt_add_group;
6494 break;
6495
6496 case IP_DROP_SOURCE_MEMBERSHIP:
6497 mcast_opt = B_FALSE;
6498 /* FALLTHROUGH */
6499 case MCAST_LEAVE_SOURCE_GROUP:
6500 fmode = MODE_IS_INCLUDE;
6501 optfn = ip_opt_delete_group;
6502 break;
6503 default:
6504 /* Should not be reached. */
6505 optfn = NULL;
6506 fmode = 0;
6507 ASSERT(0);
6508 }
6509
6510 if (mcast_opt) {
6511 gsreqp = (struct group_source_req *)i1;
6512 ifindex = gsreqp->gsr_interface;
6513 if (gsreqp->gsr_group.ss_family == AF_INET) {
6514 struct sockaddr_in *s;
6515 s = (struct sockaddr_in *)&gsreqp->gsr_group;
6516 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6517 s = (struct sockaddr_in *)&gsreqp->gsr_source;
6518 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6519 } else {
6520 struct sockaddr_in6 *s6;
6521
6522 if (!inet6)
6523 return (EINVAL); /* Not on INET socket */
6524
6525 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6526 v6group = s6->sin6_addr;
6527 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6528 v6src = s6->sin6_addr;
6529 }
6530 ifaddr = INADDR_ANY;
6531 } else {
6532 imreqp = (struct ip_mreq_source *)i1;
6533 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6534 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6535 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6536 ifindex = 0;
6537 }
6538
6539 /*
6540 * Handle src being mapped INADDR_ANY by changing it to unspecified.
6541 */
6542 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6543 v6src = ipv6_all_zeros;
6544
6545 /*
6546 * In the multirouting case, we need to replicate
6547 * the request as noted in the mcast cases above.
6548 */
6549 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6550 ipaddr_t group;
6551
6552 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6553
6554 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6555 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6556 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6557 } else {
6558 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6559 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6560 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6561 }
6562 if (ire != NULL) {
6563 if (ire->ire_flags & RTF_MULTIRT) {
6564 error = ip_multirt_apply_membership(optfn, ire, connp,
6565 checkonly, &v6group, fmode, &v6src);
6566 done = B_TRUE;
6567 }
6568 ire_refrele(ire);
6569 }
6570 if (!done) {
6571 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6572 fmode, &v6src);
6573 }
6574 return (error);
6575 }
6576
6577 /*
6578 * Given a destination address and a pointer to where to put the information
6579 * this routine fills in the mtuinfo.
6580 * The socket must be connected.
6581 * For sctp conn_faddr is the primary address.
6582 */
6583 int
ip_fill_mtuinfo(conn_t * connp,ip_xmit_attr_t * ixa,struct ip6_mtuinfo * mtuinfo)6584 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6585 {
6586 uint32_t pmtu = IP_MAXPACKET;
6587 uint_t scopeid;
6588
6589 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6590 return (-1);
6591
6592 /* In case we never sent or called ip_set_destination_v4/v6 */
6593 if (ixa->ixa_ire != NULL)
6594 pmtu = ip_get_pmtu(ixa);
6595
6596 if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6597 scopeid = ixa->ixa_scopeid;
6598 else
6599 scopeid = 0;
6600
6601 bzero(mtuinfo, sizeof (*mtuinfo));
6602 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6603 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6604 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6605 mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6606 mtuinfo->ip6m_mtu = pmtu;
6607
6608 return (sizeof (struct ip6_mtuinfo));
6609 }
6610
6611 /*
6612 * When the src multihoming is changed from weak to [strong, preferred]
6613 * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6614 * and identify routes that were created by user-applications in the
6615 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6616 * currently defined. These routes are then 'rebound', i.e., their ire_ill
6617 * is selected by finding an interface route for the gateway.
6618 */
6619 /* ARGSUSED */
6620 void
ip_ire_rebind_walker(ire_t * ire,void * notused)6621 ip_ire_rebind_walker(ire_t *ire, void *notused)
6622 {
6623 if (!ire->ire_unbound || ire->ire_ill != NULL)
6624 return;
6625 ire_rebind(ire);
6626 ire_delete(ire);
6627 }
6628
6629 /*
6630 * When the src multihoming is changed from [strong, preferred] to weak,
6631 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6632 * set any entries that were created by user-applications in the unbound state
6633 * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6634 */
6635 /* ARGSUSED */
6636 void
ip_ire_unbind_walker(ire_t * ire,void * notused)6637 ip_ire_unbind_walker(ire_t *ire, void *notused)
6638 {
6639 ire_t *new_ire;
6640
6641 if (!ire->ire_unbound || ire->ire_ill == NULL)
6642 return;
6643 if (ire->ire_ipversion == IPV6_VERSION) {
6644 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6645 &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6646 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6647 } else {
6648 new_ire = ire_create((uchar_t *)&ire->ire_addr,
6649 (uchar_t *)&ire->ire_mask,
6650 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6651 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6652 }
6653 if (new_ire == NULL)
6654 return;
6655 new_ire->ire_unbound = B_TRUE;
6656 /*
6657 * The bound ire must first be deleted so that we don't return
6658 * the existing one on the attempt to add the unbound new_ire.
6659 */
6660 ire_delete(ire);
6661 new_ire = ire_add(new_ire);
6662 if (new_ire != NULL)
6663 ire_refrele(new_ire);
6664 }
6665
6666 /*
6667 * When the settings of ip*_strict_src_multihoming tunables are changed,
6668 * all cached routes need to be recomputed. This recomputation needs to be
6669 * done when going from weaker to stronger modes so that the cached ire
6670 * for the connection does not violate the current ip*_strict_src_multihoming
6671 * setting. It also needs to be done when going from stronger to weaker modes,
6672 * so that we fall back to matching on the longest-matching-route (as opposed
6673 * to a shorter match that may have been selected in the strong mode
6674 * to satisfy src_multihoming settings).
6675 *
6676 * The cached ixa_ire entires for all conn_t entries are marked as
6677 * "verify" so that they will be recomputed for the next packet.
6678 */
6679 void
conn_ire_revalidate(conn_t * connp,void * arg)6680 conn_ire_revalidate(conn_t *connp, void *arg)
6681 {
6682 boolean_t isv6 = (boolean_t)arg;
6683
6684 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6685 (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6686 return;
6687 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6688 }
6689
6690 /*
6691 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6692 * When an ipf is passed here for the first time, if
6693 * we already have in-order fragments on the queue, we convert from the fast-
6694 * path reassembly scheme to the hard-case scheme. From then on, additional
6695 * fragments are reassembled here. We keep track of the start and end offsets
6696 * of each piece, and the number of holes in the chain. When the hole count
6697 * goes to zero, we are done!
6698 *
6699 * The ipf_count will be updated to account for any mblk(s) added (pointed to
6700 * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6701 * ipfb_count and ill_frag_count by the difference of ipf_count before and
6702 * after the call to ip_reassemble().
6703 */
6704 int
ip_reassemble(mblk_t * mp,ipf_t * ipf,uint_t start,boolean_t more,ill_t * ill,size_t msg_len)6705 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6706 size_t msg_len)
6707 {
6708 uint_t end;
6709 mblk_t *next_mp;
6710 mblk_t *mp1;
6711 uint_t offset;
6712 boolean_t incr_dups = B_TRUE;
6713 boolean_t offset_zero_seen = B_FALSE;
6714 boolean_t pkt_boundary_checked = B_FALSE;
6715
6716 /* If start == 0 then ipf_nf_hdr_len has to be set. */
6717 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6718
6719 /* Add in byte count */
6720 ipf->ipf_count += msg_len;
6721 if (ipf->ipf_end) {
6722 /*
6723 * We were part way through in-order reassembly, but now there
6724 * is a hole. We walk through messages already queued, and
6725 * mark them for hard case reassembly. We know that up till
6726 * now they were in order starting from offset zero.
6727 */
6728 offset = 0;
6729 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6730 IP_REASS_SET_START(mp1, offset);
6731 if (offset == 0) {
6732 ASSERT(ipf->ipf_nf_hdr_len != 0);
6733 offset = -ipf->ipf_nf_hdr_len;
6734 }
6735 offset += mp1->b_wptr - mp1->b_rptr;
6736 IP_REASS_SET_END(mp1, offset);
6737 }
6738 /* One hole at the end. */
6739 ipf->ipf_hole_cnt = 1;
6740 /* Brand it as a hard case, forever. */
6741 ipf->ipf_end = 0;
6742 }
6743 /* Walk through all the new pieces. */
6744 do {
6745 end = start + (mp->b_wptr - mp->b_rptr);
6746 /*
6747 * If start is 0, decrease 'end' only for the first mblk of
6748 * the fragment. Otherwise 'end' can get wrong value in the
6749 * second pass of the loop if first mblk is exactly the
6750 * size of ipf_nf_hdr_len.
6751 */
6752 if (start == 0 && !offset_zero_seen) {
6753 /* First segment */
6754 ASSERT(ipf->ipf_nf_hdr_len != 0);
6755 end -= ipf->ipf_nf_hdr_len;
6756 offset_zero_seen = B_TRUE;
6757 }
6758 next_mp = mp->b_cont;
6759 /*
6760 * We are checking to see if there is any interesing data
6761 * to process. If there isn't and the mblk isn't the
6762 * one which carries the unfragmentable header then we
6763 * drop it. It's possible to have just the unfragmentable
6764 * header come through without any data. That needs to be
6765 * saved.
6766 *
6767 * If the assert at the top of this function holds then the
6768 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code
6769 * is infrequently traveled enough that the test is left in
6770 * to protect against future code changes which break that
6771 * invariant.
6772 */
6773 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6774 /* Empty. Blast it. */
6775 IP_REASS_SET_START(mp, 0);
6776 IP_REASS_SET_END(mp, 0);
6777 /*
6778 * If the ipf points to the mblk we are about to free,
6779 * update ipf to point to the next mblk (or NULL
6780 * if none).
6781 */
6782 if (ipf->ipf_mp->b_cont == mp)
6783 ipf->ipf_mp->b_cont = next_mp;
6784 freeb(mp);
6785 continue;
6786 }
6787 mp->b_cont = NULL;
6788 IP_REASS_SET_START(mp, start);
6789 IP_REASS_SET_END(mp, end);
6790 if (!ipf->ipf_tail_mp) {
6791 ipf->ipf_tail_mp = mp;
6792 ipf->ipf_mp->b_cont = mp;
6793 if (start == 0 || !more) {
6794 ipf->ipf_hole_cnt = 1;
6795 /*
6796 * if the first fragment comes in more than one
6797 * mblk, this loop will be executed for each
6798 * mblk. Need to adjust hole count so exiting
6799 * this routine will leave hole count at 1.
6800 */
6801 if (next_mp)
6802 ipf->ipf_hole_cnt++;
6803 } else
6804 ipf->ipf_hole_cnt = 2;
6805 continue;
6806 } else if (ipf->ipf_last_frag_seen && !more &&
6807 !pkt_boundary_checked) {
6808 /*
6809 * We check datagram boundary only if this fragment
6810 * claims to be the last fragment and we have seen a
6811 * last fragment in the past too. We do this only
6812 * once for a given fragment.
6813 *
6814 * start cannot be 0 here as fragments with start=0
6815 * and MF=0 gets handled as a complete packet. These
6816 * fragments should not reach here.
6817 */
6818
6819 if (start + msgdsize(mp) !=
6820 IP_REASS_END(ipf->ipf_tail_mp)) {
6821 /*
6822 * We have two fragments both of which claim
6823 * to be the last fragment but gives conflicting
6824 * information about the whole datagram size.
6825 * Something fishy is going on. Drop the
6826 * fragment and free up the reassembly list.
6827 */
6828 return (IP_REASS_FAILED);
6829 }
6830
6831 /*
6832 * We shouldn't come to this code block again for this
6833 * particular fragment.
6834 */
6835 pkt_boundary_checked = B_TRUE;
6836 }
6837
6838 /* New stuff at or beyond tail? */
6839 offset = IP_REASS_END(ipf->ipf_tail_mp);
6840 if (start >= offset) {
6841 if (ipf->ipf_last_frag_seen) {
6842 /* current fragment is beyond last fragment */
6843 return (IP_REASS_FAILED);
6844 }
6845 /* Link it on end. */
6846 ipf->ipf_tail_mp->b_cont = mp;
6847 ipf->ipf_tail_mp = mp;
6848 if (more) {
6849 if (start != offset)
6850 ipf->ipf_hole_cnt++;
6851 } else if (start == offset && next_mp == NULL)
6852 ipf->ipf_hole_cnt--;
6853 continue;
6854 }
6855 mp1 = ipf->ipf_mp->b_cont;
6856 offset = IP_REASS_START(mp1);
6857 /* New stuff at the front? */
6858 if (start < offset) {
6859 if (start == 0) {
6860 if (end >= offset) {
6861 /* Nailed the hole at the begining. */
6862 ipf->ipf_hole_cnt--;
6863 }
6864 } else if (end < offset) {
6865 /*
6866 * A hole, stuff, and a hole where there used
6867 * to be just a hole.
6868 */
6869 ipf->ipf_hole_cnt++;
6870 }
6871 mp->b_cont = mp1;
6872 /* Check for overlap. */
6873 while (end > offset) {
6874 if (end < IP_REASS_END(mp1)) {
6875 mp->b_wptr -= end - offset;
6876 IP_REASS_SET_END(mp, offset);
6877 BUMP_MIB(ill->ill_ip_mib,
6878 ipIfStatsReasmPartDups);
6879 break;
6880 }
6881 /* Did we cover another hole? */
6882 if ((mp1->b_cont &&
6883 IP_REASS_END(mp1) !=
6884 IP_REASS_START(mp1->b_cont) &&
6885 end >= IP_REASS_START(mp1->b_cont)) ||
6886 (!ipf->ipf_last_frag_seen && !more)) {
6887 ipf->ipf_hole_cnt--;
6888 }
6889 /* Clip out mp1. */
6890 if ((mp->b_cont = mp1->b_cont) == NULL) {
6891 /*
6892 * After clipping out mp1, this guy
6893 * is now hanging off the end.
6894 */
6895 ipf->ipf_tail_mp = mp;
6896 }
6897 IP_REASS_SET_START(mp1, 0);
6898 IP_REASS_SET_END(mp1, 0);
6899 /* Subtract byte count */
6900 ipf->ipf_count -= mp1->b_datap->db_lim -
6901 mp1->b_datap->db_base;
6902 freeb(mp1);
6903 BUMP_MIB(ill->ill_ip_mib,
6904 ipIfStatsReasmPartDups);
6905 mp1 = mp->b_cont;
6906 if (!mp1)
6907 break;
6908 offset = IP_REASS_START(mp1);
6909 }
6910 ipf->ipf_mp->b_cont = mp;
6911 continue;
6912 }
6913 /*
6914 * The new piece starts somewhere between the start of the head
6915 * and before the end of the tail.
6916 */
6917 for (; mp1; mp1 = mp1->b_cont) {
6918 offset = IP_REASS_END(mp1);
6919 if (start < offset) {
6920 if (end <= offset) {
6921 /* Nothing new. */
6922 IP_REASS_SET_START(mp, 0);
6923 IP_REASS_SET_END(mp, 0);
6924 /* Subtract byte count */
6925 ipf->ipf_count -= mp->b_datap->db_lim -
6926 mp->b_datap->db_base;
6927 if (incr_dups) {
6928 ipf->ipf_num_dups++;
6929 incr_dups = B_FALSE;
6930 }
6931 freeb(mp);
6932 BUMP_MIB(ill->ill_ip_mib,
6933 ipIfStatsReasmDuplicates);
6934 break;
6935 }
6936 /*
6937 * Trim redundant stuff off beginning of new
6938 * piece.
6939 */
6940 IP_REASS_SET_START(mp, offset);
6941 mp->b_rptr += offset - start;
6942 BUMP_MIB(ill->ill_ip_mib,
6943 ipIfStatsReasmPartDups);
6944 start = offset;
6945 if (!mp1->b_cont) {
6946 /*
6947 * After trimming, this guy is now
6948 * hanging off the end.
6949 */
6950 mp1->b_cont = mp;
6951 ipf->ipf_tail_mp = mp;
6952 if (!more) {
6953 ipf->ipf_hole_cnt--;
6954 }
6955 break;
6956 }
6957 }
6958 if (start >= IP_REASS_START(mp1->b_cont))
6959 continue;
6960 /* Fill a hole */
6961 if (start > offset)
6962 ipf->ipf_hole_cnt++;
6963 mp->b_cont = mp1->b_cont;
6964 mp1->b_cont = mp;
6965 mp1 = mp->b_cont;
6966 offset = IP_REASS_START(mp1);
6967 if (end >= offset) {
6968 ipf->ipf_hole_cnt--;
6969 /* Check for overlap. */
6970 while (end > offset) {
6971 if (end < IP_REASS_END(mp1)) {
6972 mp->b_wptr -= end - offset;
6973 IP_REASS_SET_END(mp, offset);
6974 /*
6975 * TODO we might bump
6976 * this up twice if there is
6977 * overlap at both ends.
6978 */
6979 BUMP_MIB(ill->ill_ip_mib,
6980 ipIfStatsReasmPartDups);
6981 break;
6982 }
6983 /* Did we cover another hole? */
6984 if ((mp1->b_cont &&
6985 IP_REASS_END(mp1)
6986 != IP_REASS_START(mp1->b_cont) &&
6987 end >=
6988 IP_REASS_START(mp1->b_cont)) ||
6989 (!ipf->ipf_last_frag_seen &&
6990 !more)) {
6991 ipf->ipf_hole_cnt--;
6992 }
6993 /* Clip out mp1. */
6994 if ((mp->b_cont = mp1->b_cont) ==
6995 NULL) {
6996 /*
6997 * After clipping out mp1,
6998 * this guy is now hanging
6999 * off the end.
7000 */
7001 ipf->ipf_tail_mp = mp;
7002 }
7003 IP_REASS_SET_START(mp1, 0);
7004 IP_REASS_SET_END(mp1, 0);
7005 /* Subtract byte count */
7006 ipf->ipf_count -=
7007 mp1->b_datap->db_lim -
7008 mp1->b_datap->db_base;
7009 freeb(mp1);
7010 BUMP_MIB(ill->ill_ip_mib,
7011 ipIfStatsReasmPartDups);
7012 mp1 = mp->b_cont;
7013 if (!mp1)
7014 break;
7015 offset = IP_REASS_START(mp1);
7016 }
7017 }
7018 break;
7019 }
7020 } while (start = end, mp = next_mp);
7021
7022 /* Fragment just processed could be the last one. Remember this fact */
7023 if (!more)
7024 ipf->ipf_last_frag_seen = B_TRUE;
7025
7026 /* Still got holes? */
7027 if (ipf->ipf_hole_cnt)
7028 return (IP_REASS_PARTIAL);
7029 /* Clean up overloaded fields to avoid upstream disasters. */
7030 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
7031 IP_REASS_SET_START(mp1, 0);
7032 IP_REASS_SET_END(mp1, 0);
7033 }
7034 return (IP_REASS_COMPLETE);
7035 }
7036
7037 /*
7038 * Fragmentation reassembly. Each ILL has a hash table for
7039 * queuing packets undergoing reassembly for all IPIFs
7040 * associated with the ILL. The hash is based on the packet
7041 * IP ident field. The ILL frag hash table was allocated
7042 * as a timer block at the time the ILL was created. Whenever
7043 * there is anything on the reassembly queue, the timer will
7044 * be running. Returns the reassembled packet if reassembly completes.
7045 */
7046 mblk_t *
ip_input_fragment(mblk_t * mp,ipha_t * ipha,ip_recv_attr_t * ira)7047 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
7048 {
7049 uint32_t frag_offset_flags;
7050 mblk_t *t_mp;
7051 ipaddr_t dst;
7052 uint8_t proto = ipha->ipha_protocol;
7053 uint32_t sum_val;
7054 uint16_t sum_flags;
7055 ipf_t *ipf;
7056 ipf_t **ipfp;
7057 ipfb_t *ipfb;
7058 uint16_t ident;
7059 uint32_t offset;
7060 ipaddr_t src;
7061 uint_t hdr_length;
7062 uint32_t end;
7063 mblk_t *mp1;
7064 mblk_t *tail_mp;
7065 size_t count;
7066 size_t msg_len;
7067 uint8_t ecn_info = 0;
7068 uint32_t packet_size;
7069 boolean_t pruned = B_FALSE;
7070 ill_t *ill = ira->ira_ill;
7071 ip_stack_t *ipst = ill->ill_ipst;
7072
7073 /*
7074 * Drop the fragmented as early as possible, if
7075 * we don't have resource(s) to re-assemble.
7076 */
7077 if (ipst->ips_ip_reass_queue_bytes == 0) {
7078 freemsg(mp);
7079 return (NULL);
7080 }
7081
7082 /* Check for fragmentation offset; return if there's none */
7083 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
7084 (IPH_MF | IPH_OFFSET)) == 0)
7085 return (mp);
7086
7087 /*
7088 * We utilize hardware computed checksum info only for UDP since
7089 * IP fragmentation is a normal occurrence for the protocol. In
7090 * addition, checksum offload support for IP fragments carrying
7091 * UDP payload is commonly implemented across network adapters.
7092 */
7093 ASSERT(ira->ira_rill != NULL);
7094 if (proto == IPPROTO_UDP && dohwcksum &&
7095 ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
7096 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
7097 mblk_t *mp1 = mp->b_cont;
7098 int32_t len;
7099
7100 /* Record checksum information from the packet */
7101 sum_val = (uint32_t)DB_CKSUM16(mp);
7102 sum_flags = DB_CKSUMFLAGS(mp);
7103
7104 /* IP payload offset from beginning of mblk */
7105 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
7106
7107 if ((sum_flags & HCK_PARTIALCKSUM) &&
7108 (mp1 == NULL || mp1->b_cont == NULL) &&
7109 offset >= DB_CKSUMSTART(mp) &&
7110 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
7111 uint32_t adj;
7112 /*
7113 * Partial checksum has been calculated by hardware
7114 * and attached to the packet; in addition, any
7115 * prepended extraneous data is even byte aligned.
7116 * If any such data exists, we adjust the checksum;
7117 * this would also handle any postpended data.
7118 */
7119 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
7120 mp, mp1, len, adj);
7121
7122 /* One's complement subtract extraneous checksum */
7123 if (adj >= sum_val)
7124 sum_val = ~(adj - sum_val) & 0xFFFF;
7125 else
7126 sum_val -= adj;
7127 }
7128 } else {
7129 sum_val = 0;
7130 sum_flags = 0;
7131 }
7132
7133 /* Clear hardware checksumming flag */
7134 DB_CKSUMFLAGS(mp) = 0;
7135
7136 ident = ipha->ipha_ident;
7137 offset = (frag_offset_flags << 3) & 0xFFFF;
7138 src = ipha->ipha_src;
7139 dst = ipha->ipha_dst;
7140 hdr_length = IPH_HDR_LENGTH(ipha);
7141 end = ntohs(ipha->ipha_length) - hdr_length;
7142
7143 /* If end == 0 then we have a packet with no data, so just free it */
7144 if (end == 0) {
7145 freemsg(mp);
7146 return (NULL);
7147 }
7148
7149 /* Record the ECN field info. */
7150 ecn_info = (ipha->ipha_type_of_service & 0x3);
7151 if (offset != 0) {
7152 /*
7153 * If this isn't the first piece, strip the header, and
7154 * add the offset to the end value.
7155 */
7156 mp->b_rptr += hdr_length;
7157 end += offset;
7158 }
7159
7160 /* Handle vnic loopback of fragments */
7161 if (mp->b_datap->db_ref > 2)
7162 msg_len = 0;
7163 else
7164 msg_len = MBLKSIZE(mp);
7165
7166 tail_mp = mp;
7167 while (tail_mp->b_cont != NULL) {
7168 tail_mp = tail_mp->b_cont;
7169 if (tail_mp->b_datap->db_ref <= 2)
7170 msg_len += MBLKSIZE(tail_mp);
7171 }
7172
7173 /* If the reassembly list for this ILL will get too big, prune it */
7174 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
7175 ipst->ips_ip_reass_queue_bytes) {
7176 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
7177 uint_t, ill->ill_frag_count,
7178 uint_t, ipst->ips_ip_reass_queue_bytes);
7179 ill_frag_prune(ill,
7180 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
7181 (ipst->ips_ip_reass_queue_bytes - msg_len));
7182 pruned = B_TRUE;
7183 }
7184
7185 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
7186 mutex_enter(&ipfb->ipfb_lock);
7187
7188 ipfp = &ipfb->ipfb_ipf;
7189 /* Try to find an existing fragment queue for this packet. */
7190 for (;;) {
7191 ipf = ipfp[0];
7192 if (ipf != NULL) {
7193 /*
7194 * It has to match on ident and src/dst address.
7195 */
7196 if (ipf->ipf_ident == ident &&
7197 ipf->ipf_src == src &&
7198 ipf->ipf_dst == dst &&
7199 ipf->ipf_protocol == proto) {
7200 /*
7201 * If we have received too many
7202 * duplicate fragments for this packet
7203 * free it.
7204 */
7205 if (ipf->ipf_num_dups > ip_max_frag_dups) {
7206 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7207 freemsg(mp);
7208 mutex_exit(&ipfb->ipfb_lock);
7209 return (NULL);
7210 }
7211 /* Found it. */
7212 break;
7213 }
7214 ipfp = &ipf->ipf_hash_next;
7215 continue;
7216 }
7217
7218 /*
7219 * If we pruned the list, do we want to store this new
7220 * fragment?. We apply an optimization here based on the
7221 * fact that most fragments will be received in order.
7222 * So if the offset of this incoming fragment is zero,
7223 * it is the first fragment of a new packet. We will
7224 * keep it. Otherwise drop the fragment, as we have
7225 * probably pruned the packet already (since the
7226 * packet cannot be found).
7227 */
7228 if (pruned && offset != 0) {
7229 mutex_exit(&ipfb->ipfb_lock);
7230 freemsg(mp);
7231 return (NULL);
7232 }
7233
7234 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) {
7235 /*
7236 * Too many fragmented packets in this hash
7237 * bucket. Free the oldest.
7238 */
7239 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
7240 }
7241
7242 /* New guy. Allocate a frag message. */
7243 mp1 = allocb(sizeof (*ipf), BPRI_MED);
7244 if (mp1 == NULL) {
7245 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7246 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7247 freemsg(mp);
7248 reass_done:
7249 mutex_exit(&ipfb->ipfb_lock);
7250 return (NULL);
7251 }
7252
7253 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
7254 mp1->b_cont = mp;
7255
7256 /* Initialize the fragment header. */
7257 ipf = (ipf_t *)mp1->b_rptr;
7258 ipf->ipf_mp = mp1;
7259 ipf->ipf_ptphn = ipfp;
7260 ipfp[0] = ipf;
7261 ipf->ipf_hash_next = NULL;
7262 ipf->ipf_ident = ident;
7263 ipf->ipf_protocol = proto;
7264 ipf->ipf_src = src;
7265 ipf->ipf_dst = dst;
7266 ipf->ipf_nf_hdr_len = 0;
7267 /* Record reassembly start time. */
7268 ipf->ipf_timestamp = gethrestime_sec();
7269 /* Record ipf generation and account for frag header */
7270 ipf->ipf_gen = ill->ill_ipf_gen++;
7271 ipf->ipf_count = MBLKSIZE(mp1);
7272 ipf->ipf_last_frag_seen = B_FALSE;
7273 ipf->ipf_ecn = ecn_info;
7274 ipf->ipf_num_dups = 0;
7275 ipfb->ipfb_frag_pkts++;
7276 ipf->ipf_checksum = 0;
7277 ipf->ipf_checksum_flags = 0;
7278
7279 /* Store checksum value in fragment header */
7280 if (sum_flags != 0) {
7281 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7282 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7283 ipf->ipf_checksum = sum_val;
7284 ipf->ipf_checksum_flags = sum_flags;
7285 }
7286
7287 /*
7288 * We handle reassembly two ways. In the easy case,
7289 * where all the fragments show up in order, we do
7290 * minimal bookkeeping, and just clip new pieces on
7291 * the end. If we ever see a hole, then we go off
7292 * to ip_reassemble which has to mark the pieces and
7293 * keep track of the number of holes, etc. Obviously,
7294 * the point of having both mechanisms is so we can
7295 * handle the easy case as efficiently as possible.
7296 */
7297 if (offset == 0) {
7298 /* Easy case, in-order reassembly so far. */
7299 ipf->ipf_count += msg_len;
7300 ipf->ipf_tail_mp = tail_mp;
7301 /*
7302 * Keep track of next expected offset in
7303 * ipf_end.
7304 */
7305 ipf->ipf_end = end;
7306 ipf->ipf_nf_hdr_len = hdr_length;
7307 } else {
7308 /* Hard case, hole at the beginning. */
7309 ipf->ipf_tail_mp = NULL;
7310 /*
7311 * ipf_end == 0 means that we have given up
7312 * on easy reassembly.
7313 */
7314 ipf->ipf_end = 0;
7315
7316 /* Forget checksum offload from now on */
7317 ipf->ipf_checksum_flags = 0;
7318
7319 /*
7320 * ipf_hole_cnt is set by ip_reassemble.
7321 * ipf_count is updated by ip_reassemble.
7322 * No need to check for return value here
7323 * as we don't expect reassembly to complete
7324 * or fail for the first fragment itself.
7325 */
7326 (void) ip_reassemble(mp, ipf,
7327 (frag_offset_flags & IPH_OFFSET) << 3,
7328 (frag_offset_flags & IPH_MF), ill, msg_len);
7329 }
7330 /* Update per ipfb and ill byte counts */
7331 ipfb->ipfb_count += ipf->ipf_count;
7332 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7333 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7334 /* If the frag timer wasn't already going, start it. */
7335 mutex_enter(&ill->ill_lock);
7336 ill_frag_timer_start(ill);
7337 mutex_exit(&ill->ill_lock);
7338 goto reass_done;
7339 }
7340
7341 /*
7342 * If the packet's flag has changed (it could be coming up
7343 * from an interface different than the previous, therefore
7344 * possibly different checksum capability), then forget about
7345 * any stored checksum states. Otherwise add the value to
7346 * the existing one stored in the fragment header.
7347 */
7348 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7349 sum_val += ipf->ipf_checksum;
7350 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7351 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7352 ipf->ipf_checksum = sum_val;
7353 } else if (ipf->ipf_checksum_flags != 0) {
7354 /* Forget checksum offload from now on */
7355 ipf->ipf_checksum_flags = 0;
7356 }
7357
7358 /*
7359 * We have a new piece of a datagram which is already being
7360 * reassembled. Update the ECN info if all IP fragments
7361 * are ECN capable. If there is one which is not, clear
7362 * all the info. If there is at least one which has CE
7363 * code point, IP needs to report that up to transport.
7364 */
7365 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7366 if (ecn_info == IPH_ECN_CE)
7367 ipf->ipf_ecn = IPH_ECN_CE;
7368 } else {
7369 ipf->ipf_ecn = IPH_ECN_NECT;
7370 }
7371 if (offset && ipf->ipf_end == offset) {
7372 /* The new fragment fits at the end */
7373 ipf->ipf_tail_mp->b_cont = mp;
7374 /* Update the byte count */
7375 ipf->ipf_count += msg_len;
7376 /* Update per ipfb and ill byte counts */
7377 ipfb->ipfb_count += msg_len;
7378 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7379 atomic_add_32(&ill->ill_frag_count, msg_len);
7380 if (frag_offset_flags & IPH_MF) {
7381 /* More to come. */
7382 ipf->ipf_end = end;
7383 ipf->ipf_tail_mp = tail_mp;
7384 goto reass_done;
7385 }
7386 } else {
7387 /* Go do the hard cases. */
7388 int ret;
7389
7390 if (offset == 0)
7391 ipf->ipf_nf_hdr_len = hdr_length;
7392
7393 /* Save current byte count */
7394 count = ipf->ipf_count;
7395 ret = ip_reassemble(mp, ipf,
7396 (frag_offset_flags & IPH_OFFSET) << 3,
7397 (frag_offset_flags & IPH_MF), ill, msg_len);
7398 /* Count of bytes added and subtracted (freeb()ed) */
7399 count = ipf->ipf_count - count;
7400 if (count) {
7401 /* Update per ipfb and ill byte counts */
7402 ipfb->ipfb_count += count;
7403 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7404 atomic_add_32(&ill->ill_frag_count, count);
7405 }
7406 if (ret == IP_REASS_PARTIAL) {
7407 goto reass_done;
7408 } else if (ret == IP_REASS_FAILED) {
7409 /* Reassembly failed. Free up all resources */
7410 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7411 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7412 IP_REASS_SET_START(t_mp, 0);
7413 IP_REASS_SET_END(t_mp, 0);
7414 }
7415 freemsg(mp);
7416 goto reass_done;
7417 }
7418 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7419 }
7420 /*
7421 * We have completed reassembly. Unhook the frag header from
7422 * the reassembly list.
7423 *
7424 * Before we free the frag header, record the ECN info
7425 * to report back to the transport.
7426 */
7427 ecn_info = ipf->ipf_ecn;
7428 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7429 ipfp = ipf->ipf_ptphn;
7430
7431 /* We need to supply these to caller */
7432 if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7433 sum_val = ipf->ipf_checksum;
7434 else
7435 sum_val = 0;
7436
7437 mp1 = ipf->ipf_mp;
7438 count = ipf->ipf_count;
7439 ipf = ipf->ipf_hash_next;
7440 if (ipf != NULL)
7441 ipf->ipf_ptphn = ipfp;
7442 ipfp[0] = ipf;
7443 atomic_add_32(&ill->ill_frag_count, -count);
7444 ASSERT(ipfb->ipfb_count >= count);
7445 ipfb->ipfb_count -= count;
7446 ipfb->ipfb_frag_pkts--;
7447 mutex_exit(&ipfb->ipfb_lock);
7448 /* Ditch the frag header. */
7449 mp = mp1->b_cont;
7450
7451 freeb(mp1);
7452
7453 /* Restore original IP length in header. */
7454 packet_size = (uint32_t)msgdsize(mp);
7455 if (packet_size > IP_MAXPACKET) {
7456 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7457 ip_drop_input("Reassembled packet too large", mp, ill);
7458 freemsg(mp);
7459 return (NULL);
7460 }
7461
7462 if (DB_REF(mp) > 1) {
7463 mblk_t *mp2 = copymsg(mp);
7464
7465 if (mp2 == NULL) {
7466 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7467 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7468 freemsg(mp);
7469 return (NULL);
7470 }
7471 freemsg(mp);
7472 mp = mp2;
7473 }
7474 ipha = (ipha_t *)mp->b_rptr;
7475
7476 ipha->ipha_length = htons((uint16_t)packet_size);
7477 /* We're now complete, zip the frag state */
7478 ipha->ipha_fragment_offset_and_flags = 0;
7479 /* Record the ECN info. */
7480 ipha->ipha_type_of_service &= 0xFC;
7481 ipha->ipha_type_of_service |= ecn_info;
7482
7483 /* Update the receive attributes */
7484 ira->ira_pktlen = packet_size;
7485 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7486
7487 /* Reassembly is successful; set checksum information in packet */
7488 DB_CKSUM16(mp) = (uint16_t)sum_val;
7489 DB_CKSUMFLAGS(mp) = sum_flags;
7490 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7491
7492 return (mp);
7493 }
7494
7495 /*
7496 * Pullup function that should be used for IP input in order to
7497 * ensure we do not loose the L2 source address; we need the l2 source
7498 * address for IP_RECVSLLA and for ndp_input.
7499 *
7500 * We return either NULL or b_rptr.
7501 */
7502 void *
ip_pullup(mblk_t * mp,ssize_t len,ip_recv_attr_t * ira)7503 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7504 {
7505 ill_t *ill = ira->ira_ill;
7506
7507 if (ip_rput_pullups++ == 0) {
7508 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7509 "ip_pullup: %s forced us to "
7510 " pullup pkt, hdr len %ld, hdr addr %p",
7511 ill->ill_name, len, (void *)mp->b_rptr);
7512 }
7513 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7514 ip_setl2src(mp, ira, ira->ira_rill);
7515 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7516 if (!pullupmsg(mp, len))
7517 return (NULL);
7518 else
7519 return (mp->b_rptr);
7520 }
7521
7522 /*
7523 * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7524 * When called from the ULP ira_rill will be NULL hence the caller has to
7525 * pass in the ill.
7526 */
7527 /* ARGSUSED */
7528 void
ip_setl2src(mblk_t * mp,ip_recv_attr_t * ira,ill_t * ill)7529 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7530 {
7531 const uchar_t *addr;
7532 int alen;
7533
7534 if (ira->ira_flags & IRAF_L2SRC_SET)
7535 return;
7536
7537 ASSERT(ill != NULL);
7538 alen = ill->ill_phys_addr_length;
7539 ASSERT(alen <= sizeof (ira->ira_l2src));
7540 if (ira->ira_mhip != NULL &&
7541 (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7542 bcopy(addr, ira->ira_l2src, alen);
7543 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7544 (addr = ill->ill_phys_addr) != NULL) {
7545 bcopy(addr, ira->ira_l2src, alen);
7546 } else {
7547 bzero(ira->ira_l2src, alen);
7548 }
7549 ira->ira_flags |= IRAF_L2SRC_SET;
7550 }
7551
7552 /*
7553 * check ip header length and align it.
7554 */
7555 mblk_t *
ip_check_and_align_header(mblk_t * mp,uint_t min_size,ip_recv_attr_t * ira)7556 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7557 {
7558 ill_t *ill = ira->ira_ill;
7559 ssize_t len;
7560
7561 len = MBLKL(mp);
7562
7563 if (!OK_32PTR(mp->b_rptr))
7564 IP_STAT(ill->ill_ipst, ip_notaligned);
7565 else
7566 IP_STAT(ill->ill_ipst, ip_recv_pullup);
7567
7568 /* Guard against bogus device drivers */
7569 if (len < 0) {
7570 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7571 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7572 freemsg(mp);
7573 return (NULL);
7574 }
7575
7576 if (len == 0) {
7577 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7578 mblk_t *mp1 = mp->b_cont;
7579
7580 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7581 ip_setl2src(mp, ira, ira->ira_rill);
7582 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7583
7584 freeb(mp);
7585 mp = mp1;
7586 if (mp == NULL)
7587 return (NULL);
7588
7589 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7590 return (mp);
7591 }
7592 if (ip_pullup(mp, min_size, ira) == NULL) {
7593 if (msgdsize(mp) < min_size) {
7594 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7595 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7596 } else {
7597 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7598 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7599 }
7600 freemsg(mp);
7601 return (NULL);
7602 }
7603 return (mp);
7604 }
7605
7606 /*
7607 * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7608 */
7609 mblk_t *
ip_check_length(mblk_t * mp,uchar_t * rptr,ssize_t len,uint_t pkt_len,uint_t min_size,ip_recv_attr_t * ira)7610 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7611 uint_t min_size, ip_recv_attr_t *ira)
7612 {
7613 ill_t *ill = ira->ira_ill;
7614
7615 /*
7616 * Make sure we have data length consistent
7617 * with the IP header.
7618 */
7619 if (mp->b_cont == NULL) {
7620 /* pkt_len is based on ipha_len, not the mblk length */
7621 if (pkt_len < min_size) {
7622 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7623 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7624 freemsg(mp);
7625 return (NULL);
7626 }
7627 if (len < 0) {
7628 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7629 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7630 freemsg(mp);
7631 return (NULL);
7632 }
7633 /* Drop any pad */
7634 mp->b_wptr = rptr + pkt_len;
7635 } else if ((len += msgdsize(mp->b_cont)) != 0) {
7636 ASSERT(pkt_len >= min_size);
7637 if (pkt_len < min_size) {
7638 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7639 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7640 freemsg(mp);
7641 return (NULL);
7642 }
7643 if (len < 0) {
7644 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7645 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7646 freemsg(mp);
7647 return (NULL);
7648 }
7649 /* Drop any pad */
7650 (void) adjmsg(mp, -len);
7651 /*
7652 * adjmsg may have freed an mblk from the chain, hence
7653 * invalidate any hw checksum here. This will force IP to
7654 * calculate the checksum in sw, but only for this packet.
7655 */
7656 DB_CKSUMFLAGS(mp) = 0;
7657 IP_STAT(ill->ill_ipst, ip_multimblk);
7658 }
7659 return (mp);
7660 }
7661
7662 /*
7663 * Check that the IPv4 opt_len is consistent with the packet and pullup
7664 * the options.
7665 */
7666 mblk_t *
ip_check_optlen(mblk_t * mp,ipha_t * ipha,uint_t opt_len,uint_t pkt_len,ip_recv_attr_t * ira)7667 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7668 ip_recv_attr_t *ira)
7669 {
7670 ill_t *ill = ira->ira_ill;
7671 ssize_t len;
7672
7673 /* Assume no IPv6 packets arrive over the IPv4 queue */
7674 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7675 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7676 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7677 ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7678 freemsg(mp);
7679 return (NULL);
7680 }
7681
7682 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7683 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7684 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7685 freemsg(mp);
7686 return (NULL);
7687 }
7688 /*
7689 * Recompute complete header length and make sure we
7690 * have access to all of it.
7691 */
7692 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7693 if (len > (mp->b_wptr - mp->b_rptr)) {
7694 if (len > pkt_len) {
7695 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7696 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7697 freemsg(mp);
7698 return (NULL);
7699 }
7700 if (ip_pullup(mp, len, ira) == NULL) {
7701 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7702 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7703 freemsg(mp);
7704 return (NULL);
7705 }
7706 }
7707 return (mp);
7708 }
7709
7710 /*
7711 * Returns a new ire, or the same ire, or NULL.
7712 * If a different IRE is returned, then it is held; the caller
7713 * needs to release it.
7714 * In no case is there any hold/release on the ire argument.
7715 */
7716 ire_t *
ip_check_multihome(void * addr,ire_t * ire,ill_t * ill)7717 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7718 {
7719 ire_t *new_ire;
7720 ill_t *ire_ill;
7721 uint_t ifindex;
7722 ip_stack_t *ipst = ill->ill_ipst;
7723 boolean_t strict_check = B_FALSE;
7724
7725 /*
7726 * IPMP common case: if IRE and ILL are in the same group, there's no
7727 * issue (e.g. packet received on an underlying interface matched an
7728 * IRE_LOCAL on its associated group interface).
7729 */
7730 ASSERT(ire->ire_ill != NULL);
7731 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7732 return (ire);
7733
7734 /*
7735 * Do another ire lookup here, using the ingress ill, to see if the
7736 * interface is in a usesrc group.
7737 * As long as the ills belong to the same group, we don't consider
7738 * them to be arriving on the wrong interface. Thus, if the switch
7739 * is doing inbound load spreading, we won't drop packets when the
7740 * ip*_strict_dst_multihoming switch is on.
7741 * We also need to check for IPIF_UNNUMBERED point2point interfaces
7742 * where the local address may not be unique. In this case we were
7743 * at the mercy of the initial ire lookup and the IRE_LOCAL it
7744 * actually returned. The new lookup, which is more specific, should
7745 * only find the IRE_LOCAL associated with the ingress ill if one
7746 * exists.
7747 */
7748 if (ire->ire_ipversion == IPV4_VERSION) {
7749 if (ipst->ips_ip_strict_dst_multihoming)
7750 strict_check = B_TRUE;
7751 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7752 IRE_LOCAL, ill, ALL_ZONES, NULL,
7753 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7754 } else {
7755 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7756 if (ipst->ips_ipv6_strict_dst_multihoming)
7757 strict_check = B_TRUE;
7758 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7759 IRE_LOCAL, ill, ALL_ZONES, NULL,
7760 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7761 }
7762 /*
7763 * If the same ire that was returned in ip_input() is found then this
7764 * is an indication that usesrc groups are in use. The packet
7765 * arrived on a different ill in the group than the one associated with
7766 * the destination address. If a different ire was found then the same
7767 * IP address must be hosted on multiple ills. This is possible with
7768 * unnumbered point2point interfaces. We switch to use this new ire in
7769 * order to have accurate interface statistics.
7770 */
7771 if (new_ire != NULL) {
7772 /* Note: held in one case but not the other? Caller handles */
7773 if (new_ire != ire)
7774 return (new_ire);
7775 /* Unchanged */
7776 ire_refrele(new_ire);
7777 return (ire);
7778 }
7779
7780 /*
7781 * Chase pointers once and store locally.
7782 */
7783 ASSERT(ire->ire_ill != NULL);
7784 ire_ill = ire->ire_ill;
7785 ifindex = ill->ill_usesrc_ifindex;
7786
7787 /*
7788 * Check if it's a legal address on the 'usesrc' interface.
7789 * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7790 * can just check phyint_ifindex.
7791 */
7792 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7793 return (ire);
7794 }
7795
7796 /*
7797 * If the ip*_strict_dst_multihoming switch is on then we can
7798 * only accept this packet if the interface is marked as routing.
7799 */
7800 if (!(strict_check))
7801 return (ire);
7802
7803 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7804 return (ire);
7805 }
7806 return (NULL);
7807 }
7808
7809 /*
7810 * This function is used to construct a mac_header_info_s from a
7811 * DL_UNITDATA_IND message.
7812 * The address fields in the mhi structure points into the message,
7813 * thus the caller can't use those fields after freeing the message.
7814 *
7815 * We determine whether the packet received is a non-unicast packet
7816 * and in doing so, determine whether or not it is broadcast vs multicast.
7817 * For it to be a broadcast packet, we must have the appropriate mblk_t
7818 * hanging off the ill_t. If this is either not present or doesn't match
7819 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7820 * to be multicast. Thus NICs that have no broadcast address (or no
7821 * capability for one, such as point to point links) cannot return as
7822 * the packet being broadcast.
7823 */
7824 void
ip_dlur_to_mhi(ill_t * ill,mblk_t * mb,struct mac_header_info_s * mhip)7825 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7826 {
7827 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7828 mblk_t *bmp;
7829 uint_t extra_offset;
7830
7831 bzero(mhip, sizeof (struct mac_header_info_s));
7832
7833 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7834
7835 if (ill->ill_sap_length < 0)
7836 extra_offset = 0;
7837 else
7838 extra_offset = ill->ill_sap_length;
7839
7840 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7841 extra_offset;
7842 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7843 extra_offset;
7844
7845 if (!ind->dl_group_address)
7846 return;
7847
7848 /* Multicast or broadcast */
7849 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7850
7851 if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7852 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7853 (bmp = ill->ill_bcast_mp) != NULL) {
7854 dl_unitdata_req_t *dlur;
7855 uint8_t *bphys_addr;
7856
7857 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7858 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7859 extra_offset;
7860
7861 if (bcmp(mhip->mhi_daddr, bphys_addr,
7862 ind->dl_dest_addr_length) == 0)
7863 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7864 }
7865 }
7866
7867 /*
7868 * This function is used to construct a mac_header_info_s from a
7869 * M_DATA fastpath message from a DLPI driver.
7870 * The address fields in the mhi structure points into the message,
7871 * thus the caller can't use those fields after freeing the message.
7872 *
7873 * We determine whether the packet received is a non-unicast packet
7874 * and in doing so, determine whether or not it is broadcast vs multicast.
7875 * For it to be a broadcast packet, we must have the appropriate mblk_t
7876 * hanging off the ill_t. If this is either not present or doesn't match
7877 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7878 * to be multicast. Thus NICs that have no broadcast address (or no
7879 * capability for one, such as point to point links) cannot return as
7880 * the packet being broadcast.
7881 */
7882 void
ip_mdata_to_mhi(ill_t * ill,mblk_t * mp,struct mac_header_info_s * mhip)7883 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7884 {
7885 mblk_t *bmp;
7886 struct ether_header *pether;
7887
7888 bzero(mhip, sizeof (struct mac_header_info_s));
7889
7890 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7891
7892 pether = (struct ether_header *)((char *)mp->b_rptr
7893 - sizeof (struct ether_header));
7894
7895 /*
7896 * Make sure the interface is an ethernet type, since we don't
7897 * know the header format for anything but Ethernet. Also make
7898 * sure we are pointing correctly above db_base.
7899 */
7900 if (ill->ill_type != IFT_ETHER)
7901 return;
7902
7903 retry:
7904 if ((uchar_t *)pether < mp->b_datap->db_base)
7905 return;
7906
7907 /* Is there a VLAN tag? */
7908 if (ill->ill_isv6) {
7909 if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7910 pether = (struct ether_header *)((char *)pether - 4);
7911 goto retry;
7912 }
7913 } else {
7914 if (pether->ether_type != htons(ETHERTYPE_IP)) {
7915 pether = (struct ether_header *)((char *)pether - 4);
7916 goto retry;
7917 }
7918 }
7919 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7920 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7921
7922 if (!(mhip->mhi_daddr[0] & 0x01))
7923 return;
7924
7925 /* Multicast or broadcast */
7926 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7927
7928 if ((bmp = ill->ill_bcast_mp) != NULL) {
7929 dl_unitdata_req_t *dlur;
7930 uint8_t *bphys_addr;
7931 uint_t addrlen;
7932
7933 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7934 addrlen = dlur->dl_dest_addr_length;
7935 if (ill->ill_sap_length < 0) {
7936 bphys_addr = (uchar_t *)dlur +
7937 dlur->dl_dest_addr_offset;
7938 addrlen += ill->ill_sap_length;
7939 } else {
7940 bphys_addr = (uchar_t *)dlur +
7941 dlur->dl_dest_addr_offset +
7942 ill->ill_sap_length;
7943 addrlen -= ill->ill_sap_length;
7944 }
7945 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7946 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7947 }
7948 }
7949
7950 /*
7951 * Handle anything but M_DATA messages
7952 * We see the DL_UNITDATA_IND which are part
7953 * of the data path, and also the other messages from the driver.
7954 */
7955 void
ip_rput_notdata(ill_t * ill,mblk_t * mp)7956 ip_rput_notdata(ill_t *ill, mblk_t *mp)
7957 {
7958 mblk_t *first_mp;
7959 struct iocblk *iocp;
7960 struct mac_header_info_s mhi;
7961
7962 switch (DB_TYPE(mp)) {
7963 case M_PROTO:
7964 case M_PCPROTO: {
7965 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7966 DL_UNITDATA_IND) {
7967 /* Go handle anything other than data elsewhere. */
7968 ip_rput_dlpi(ill, mp);
7969 return;
7970 }
7971
7972 first_mp = mp;
7973 mp = first_mp->b_cont;
7974 first_mp->b_cont = NULL;
7975
7976 if (mp == NULL) {
7977 freeb(first_mp);
7978 return;
7979 }
7980 ip_dlur_to_mhi(ill, first_mp, &mhi);
7981 if (ill->ill_isv6)
7982 ip_input_v6(ill, NULL, mp, &mhi);
7983 else
7984 ip_input(ill, NULL, mp, &mhi);
7985
7986 /* Ditch the DLPI header. */
7987 freeb(first_mp);
7988 return;
7989 }
7990 case M_IOCACK:
7991 iocp = (struct iocblk *)mp->b_rptr;
7992 switch (iocp->ioc_cmd) {
7993 case DL_IOC_HDR_INFO:
7994 ill_fastpath_ack(ill, mp);
7995 return;
7996 default:
7997 putnext(ill->ill_rq, mp);
7998 return;
7999 }
8000 /* FALLTHROUGH */
8001 case M_ERROR:
8002 case M_HANGUP:
8003 mutex_enter(&ill->ill_lock);
8004 if (ill->ill_state_flags & ILL_CONDEMNED) {
8005 mutex_exit(&ill->ill_lock);
8006 freemsg(mp);
8007 return;
8008 }
8009 ill_refhold_locked(ill);
8010 mutex_exit(&ill->ill_lock);
8011 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
8012 B_FALSE);
8013 return;
8014 case M_CTL:
8015 putnext(ill->ill_rq, mp);
8016 return;
8017 case M_IOCNAK:
8018 ip1dbg(("got iocnak "));
8019 iocp = (struct iocblk *)mp->b_rptr;
8020 switch (iocp->ioc_cmd) {
8021 case DL_IOC_HDR_INFO:
8022 ip_rput_other(NULL, ill->ill_rq, mp, NULL);
8023 return;
8024 default:
8025 break;
8026 }
8027 /* FALLTHROUGH */
8028 default:
8029 putnext(ill->ill_rq, mp);
8030 return;
8031 }
8032 }
8033
8034 /* Read side put procedure. Packets coming from the wire arrive here. */
8035 int
ip_rput(queue_t * q,mblk_t * mp)8036 ip_rput(queue_t *q, mblk_t *mp)
8037 {
8038 ill_t *ill;
8039 union DL_primitives *dl;
8040
8041 ill = (ill_t *)q->q_ptr;
8042
8043 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
8044 /*
8045 * If things are opening or closing, only accept high-priority
8046 * DLPI messages. (On open ill->ill_ipif has not yet been
8047 * created; on close, things hanging off the ill may have been
8048 * freed already.)
8049 */
8050 dl = (union DL_primitives *)mp->b_rptr;
8051 if (DB_TYPE(mp) != M_PCPROTO ||
8052 dl->dl_primitive == DL_UNITDATA_IND) {
8053 inet_freemsg(mp);
8054 return (0);
8055 }
8056 }
8057 if (DB_TYPE(mp) == M_DATA) {
8058 struct mac_header_info_s mhi;
8059
8060 ip_mdata_to_mhi(ill, mp, &mhi);
8061 ip_input(ill, NULL, mp, &mhi);
8062 } else {
8063 ip_rput_notdata(ill, mp);
8064 }
8065 return (0);
8066 }
8067
8068 /*
8069 * Move the information to a copy.
8070 */
8071 mblk_t *
ip_fix_dbref(mblk_t * mp,ip_recv_attr_t * ira)8072 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
8073 {
8074 mblk_t *mp1;
8075 ill_t *ill = ira->ira_ill;
8076 ip_stack_t *ipst = ill->ill_ipst;
8077
8078 IP_STAT(ipst, ip_db_ref);
8079
8080 /* Make sure we have ira_l2src before we loose the original mblk */
8081 if (!(ira->ira_flags & IRAF_L2SRC_SET))
8082 ip_setl2src(mp, ira, ira->ira_rill);
8083
8084 mp1 = copymsg(mp);
8085 if (mp1 == NULL) {
8086 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8087 ip_drop_input("ipIfStatsInDiscards", mp, ill);
8088 freemsg(mp);
8089 return (NULL);
8090 }
8091 /* preserve the hardware checksum flags and data, if present */
8092 if (DB_CKSUMFLAGS(mp) != 0) {
8093 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
8094 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
8095 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
8096 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
8097 DB_CKSUM16(mp1) = DB_CKSUM16(mp);
8098 }
8099 freemsg(mp);
8100 return (mp1);
8101 }
8102
8103 static void
ip_dlpi_error(ill_t * ill,t_uscalar_t prim,t_uscalar_t dl_err,t_uscalar_t err)8104 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
8105 t_uscalar_t err)
8106 {
8107 if (dl_err == DL_SYSERR) {
8108 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8109 "%s: %s failed: DL_SYSERR (errno %u)\n",
8110 ill->ill_name, dl_primstr(prim), err);
8111 return;
8112 }
8113
8114 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8115 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
8116 dl_errstr(dl_err));
8117 }
8118
8119 /*
8120 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
8121 * than DL_UNITDATA_IND messages. If we need to process this message
8122 * exclusively, we call qwriter_ip, in which case we also need to call
8123 * ill_refhold before that, since qwriter_ip does an ill_refrele.
8124 */
8125 void
ip_rput_dlpi(ill_t * ill,mblk_t * mp)8126 ip_rput_dlpi(ill_t *ill, mblk_t *mp)
8127 {
8128 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8129 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8130 queue_t *q = ill->ill_rq;
8131 t_uscalar_t prim = dloa->dl_primitive;
8132 t_uscalar_t reqprim = DL_PRIM_INVAL;
8133
8134 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
8135 char *, dl_primstr(prim), ill_t *, ill);
8136 ip1dbg(("ip_rput_dlpi"));
8137
8138 /*
8139 * If we received an ACK but didn't send a request for it, then it
8140 * can't be part of any pending operation; discard up-front.
8141 */
8142 switch (prim) {
8143 case DL_ERROR_ACK:
8144 reqprim = dlea->dl_error_primitive;
8145 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
8146 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
8147 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
8148 dlea->dl_unix_errno));
8149 break;
8150 case DL_OK_ACK:
8151 reqprim = dloa->dl_correct_primitive;
8152 break;
8153 case DL_INFO_ACK:
8154 reqprim = DL_INFO_REQ;
8155 break;
8156 case DL_BIND_ACK:
8157 reqprim = DL_BIND_REQ;
8158 break;
8159 case DL_PHYS_ADDR_ACK:
8160 reqprim = DL_PHYS_ADDR_REQ;
8161 break;
8162 case DL_NOTIFY_ACK:
8163 reqprim = DL_NOTIFY_REQ;
8164 break;
8165 case DL_CAPABILITY_ACK:
8166 reqprim = DL_CAPABILITY_REQ;
8167 break;
8168 }
8169
8170 if (prim != DL_NOTIFY_IND) {
8171 if (reqprim == DL_PRIM_INVAL ||
8172 !ill_dlpi_pending(ill, reqprim)) {
8173 /* Not a DLPI message we support or expected */
8174 freemsg(mp);
8175 return;
8176 }
8177 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
8178 dl_primstr(reqprim)));
8179 }
8180
8181 switch (reqprim) {
8182 case DL_UNBIND_REQ:
8183 /*
8184 * NOTE: we mark the unbind as complete even if we got a
8185 * DL_ERROR_ACK, since there's not much else we can do.
8186 */
8187 mutex_enter(&ill->ill_lock);
8188 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
8189 cv_signal(&ill->ill_cv);
8190 mutex_exit(&ill->ill_lock);
8191 break;
8192
8193 case DL_ENABMULTI_REQ:
8194 if (prim == DL_OK_ACK) {
8195 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8196 ill->ill_dlpi_multicast_state = IDS_OK;
8197 }
8198 break;
8199 }
8200
8201 /*
8202 * The message is one we're waiting for (or DL_NOTIFY_IND), but we
8203 * need to become writer to continue to process it. Because an
8204 * exclusive operation doesn't complete until replies to all queued
8205 * DLPI messages have been received, we know we're in the middle of an
8206 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
8207 *
8208 * As required by qwriter_ip(), we refhold the ill; it will refrele.
8209 * Since this is on the ill stream we unconditionally bump up the
8210 * refcount without doing ILL_CAN_LOOKUP().
8211 */
8212 ill_refhold(ill);
8213 if (prim == DL_NOTIFY_IND)
8214 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
8215 else
8216 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
8217 }
8218
8219 /*
8220 * Handling of DLPI messages that require exclusive access to the ipsq.
8221 *
8222 * Need to do ipsq_pending_mp_get on ioctl completion, which could
8223 * happen here. (along with mi_copy_done)
8224 */
8225 /* ARGSUSED */
8226 static void
ip_rput_dlpi_writer(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * dummy_arg)8227 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8228 {
8229 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8230 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8231 int err = 0;
8232 ill_t *ill = (ill_t *)q->q_ptr;
8233 ipif_t *ipif = NULL;
8234 mblk_t *mp1 = NULL;
8235 conn_t *connp = NULL;
8236 t_uscalar_t paddrreq;
8237 mblk_t *mp_hw;
8238 boolean_t success;
8239 boolean_t ioctl_aborted = B_FALSE;
8240 boolean_t log = B_TRUE;
8241
8242 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
8243 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
8244
8245 ip1dbg(("ip_rput_dlpi_writer .."));
8246 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
8247 ASSERT(IAM_WRITER_ILL(ill));
8248
8249 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
8250 /*
8251 * The current ioctl could have been aborted by the user and a new
8252 * ioctl to bring up another ill could have started. We could still
8253 * get a response from the driver later.
8254 */
8255 if (ipif != NULL && ipif->ipif_ill != ill)
8256 ioctl_aborted = B_TRUE;
8257
8258 switch (dloa->dl_primitive) {
8259 case DL_ERROR_ACK:
8260 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
8261 dl_primstr(dlea->dl_error_primitive)));
8262
8263 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
8264 char *, dl_primstr(dlea->dl_error_primitive),
8265 ill_t *, ill);
8266
8267 switch (dlea->dl_error_primitive) {
8268 case DL_DISABMULTI_REQ:
8269 ill_dlpi_done(ill, dlea->dl_error_primitive);
8270 break;
8271 case DL_PROMISCON_REQ:
8272 case DL_PROMISCOFF_REQ:
8273 case DL_UNBIND_REQ:
8274 case DL_ATTACH_REQ:
8275 case DL_INFO_REQ:
8276 ill_dlpi_done(ill, dlea->dl_error_primitive);
8277 break;
8278 case DL_NOTIFY_REQ:
8279 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8280 log = B_FALSE;
8281 break;
8282 case DL_PHYS_ADDR_REQ:
8283 /*
8284 * For IPv6 only, there are two additional
8285 * phys_addr_req's sent to the driver to get the
8286 * IPv6 token and lla. This allows IP to acquire
8287 * the hardware address format for a given interface
8288 * without having built in knowledge of the hardware
8289 * address. ill_phys_addr_pend keeps track of the last
8290 * DL_PAR sent so we know which response we are
8291 * dealing with. ill_dlpi_done will update
8292 * ill_phys_addr_pend when it sends the next req.
8293 * We don't complete the IOCTL until all three DL_PARs
8294 * have been attempted, so set *_len to 0 and break.
8295 */
8296 paddrreq = ill->ill_phys_addr_pend;
8297 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8298 if (paddrreq == DL_IPV6_TOKEN) {
8299 ill->ill_token_length = 0;
8300 log = B_FALSE;
8301 break;
8302 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8303 ill->ill_nd_lla_len = 0;
8304 log = B_FALSE;
8305 break;
8306 }
8307 /*
8308 * Something went wrong with the DL_PHYS_ADDR_REQ.
8309 * We presumably have an IOCTL hanging out waiting
8310 * for completion. Find it and complete the IOCTL
8311 * with the error noted.
8312 * However, ill_dl_phys was called on an ill queue
8313 * (from SIOCSLIFNAME), thus conn_pending_ill is not
8314 * set. But the ioctl is known to be pending on ill_wq.
8315 */
8316 if (!ill->ill_ifname_pending)
8317 break;
8318 ill->ill_ifname_pending = 0;
8319 if (!ioctl_aborted)
8320 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8321 if (mp1 != NULL) {
8322 /*
8323 * This operation (SIOCSLIFNAME) must have
8324 * happened on the ill. Assert there is no conn
8325 */
8326 ASSERT(connp == NULL);
8327 q = ill->ill_wq;
8328 }
8329 break;
8330 case DL_BIND_REQ:
8331 ill_dlpi_done(ill, DL_BIND_REQ);
8332 if (ill->ill_ifname_pending)
8333 break;
8334 mutex_enter(&ill->ill_lock);
8335 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8336 mutex_exit(&ill->ill_lock);
8337 /*
8338 * Something went wrong with the bind. We presumably
8339 * have an IOCTL hanging out waiting for completion.
8340 * Find it, take down the interface that was coming
8341 * up, and complete the IOCTL with the error noted.
8342 */
8343 if (!ioctl_aborted)
8344 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8345 if (mp1 != NULL) {
8346 /*
8347 * This might be a result of a DL_NOTE_REPLUMB
8348 * notification. In that case, connp is NULL.
8349 */
8350 if (connp != NULL)
8351 q = CONNP_TO_WQ(connp);
8352
8353 (void) ipif_down(ipif, NULL, NULL);
8354 /* error is set below the switch */
8355 }
8356 break;
8357 case DL_ENABMULTI_REQ:
8358 ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8359
8360 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8361 ill->ill_dlpi_multicast_state = IDS_FAILED;
8362 if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8363
8364 printf("ip: joining multicasts failed (%d)"
8365 " on %s - will use link layer "
8366 "broadcasts for multicast\n",
8367 dlea->dl_errno, ill->ill_name);
8368
8369 /*
8370 * Set up for multi_bcast; We are the
8371 * writer, so ok to access ill->ill_ipif
8372 * without any lock.
8373 */
8374 mutex_enter(&ill->ill_phyint->phyint_lock);
8375 ill->ill_phyint->phyint_flags |=
8376 PHYI_MULTI_BCAST;
8377 mutex_exit(&ill->ill_phyint->phyint_lock);
8378
8379 }
8380 freemsg(mp); /* Don't want to pass this up */
8381 return;
8382 case DL_CAPABILITY_REQ:
8383 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8384 "DL_CAPABILITY REQ\n"));
8385 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8386 ill->ill_dlpi_capab_state = IDCS_FAILED;
8387 ill_capability_done(ill);
8388 freemsg(mp);
8389 return;
8390 }
8391 /*
8392 * Note the error for IOCTL completion (mp1 is set when
8393 * ready to complete ioctl). If ill_ifname_pending_err is
8394 * set, an error occured during plumbing (ill_ifname_pending),
8395 * so we want to report that error.
8396 *
8397 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8398 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8399 * expected to get errack'd if the driver doesn't support
8400 * these flags (e.g. ethernet). log will be set to B_FALSE
8401 * if these error conditions are encountered.
8402 */
8403 if (mp1 != NULL) {
8404 if (ill->ill_ifname_pending_err != 0) {
8405 err = ill->ill_ifname_pending_err;
8406 ill->ill_ifname_pending_err = 0;
8407 } else {
8408 err = dlea->dl_unix_errno ?
8409 dlea->dl_unix_errno : ENXIO;
8410 }
8411 /*
8412 * If we're plumbing an interface and an error hasn't already
8413 * been saved, set ill_ifname_pending_err to the error passed
8414 * up. Ignore the error if log is B_FALSE (see comment above).
8415 */
8416 } else if (log && ill->ill_ifname_pending &&
8417 ill->ill_ifname_pending_err == 0) {
8418 ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8419 dlea->dl_unix_errno : ENXIO;
8420 }
8421
8422 if (log)
8423 ip_dlpi_error(ill, dlea->dl_error_primitive,
8424 dlea->dl_errno, dlea->dl_unix_errno);
8425 break;
8426 case DL_CAPABILITY_ACK:
8427 ill_capability_ack(ill, mp);
8428 /*
8429 * The message has been handed off to ill_capability_ack
8430 * and must not be freed below
8431 */
8432 mp = NULL;
8433 break;
8434
8435 case DL_INFO_ACK:
8436 /* Call a routine to handle this one. */
8437 ill_dlpi_done(ill, DL_INFO_REQ);
8438 ip_ll_subnet_defaults(ill, mp);
8439 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8440 return;
8441 case DL_BIND_ACK:
8442 /*
8443 * We should have an IOCTL waiting on this unless
8444 * sent by ill_dl_phys, in which case just return
8445 */
8446 ill_dlpi_done(ill, DL_BIND_REQ);
8447
8448 if (ill->ill_ifname_pending) {
8449 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8450 ill_t *, ill, mblk_t *, mp);
8451 break;
8452 }
8453 mutex_enter(&ill->ill_lock);
8454 ill->ill_dl_up = 1;
8455 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8456 mutex_exit(&ill->ill_lock);
8457
8458 if (!ioctl_aborted)
8459 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8460 if (mp1 == NULL) {
8461 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8462 break;
8463 }
8464 /*
8465 * mp1 was added by ill_dl_up(). if that is a result of
8466 * a DL_NOTE_REPLUMB notification, connp could be NULL.
8467 */
8468 if (connp != NULL)
8469 q = CONNP_TO_WQ(connp);
8470 /*
8471 * We are exclusive. So nothing can change even after
8472 * we get the pending mp.
8473 */
8474 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8475 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8476 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8477
8478 /*
8479 * Now bring up the resolver; when that is complete, we'll
8480 * create IREs. Note that we intentionally mirror what
8481 * ipif_up() would have done, because we got here by way of
8482 * ill_dl_up(), which stopped ipif_up()'s processing.
8483 */
8484 if (ill->ill_isv6) {
8485 /*
8486 * v6 interfaces.
8487 * Unlike ARP which has to do another bind
8488 * and attach, once we get here we are
8489 * done with NDP
8490 */
8491 (void) ipif_resolver_up(ipif, Res_act_initial);
8492 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8493 err = ipif_up_done_v6(ipif);
8494 } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8495 /*
8496 * ARP and other v4 external resolvers.
8497 * Leave the pending mblk intact so that
8498 * the ioctl completes in ip_rput().
8499 */
8500 if (connp != NULL)
8501 mutex_enter(&connp->conn_lock);
8502 mutex_enter(&ill->ill_lock);
8503 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8504 mutex_exit(&ill->ill_lock);
8505 if (connp != NULL)
8506 mutex_exit(&connp->conn_lock);
8507 if (success) {
8508 err = ipif_resolver_up(ipif, Res_act_initial);
8509 if (err == EINPROGRESS) {
8510 freemsg(mp);
8511 return;
8512 }
8513 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8514 } else {
8515 /* The conn has started closing */
8516 err = EINTR;
8517 }
8518 } else {
8519 /*
8520 * This one is complete. Reply to pending ioctl.
8521 */
8522 (void) ipif_resolver_up(ipif, Res_act_initial);
8523 err = ipif_up_done(ipif);
8524 }
8525
8526 if ((err == 0) && (ill->ill_up_ipifs)) {
8527 err = ill_up_ipifs(ill, q, mp1);
8528 if (err == EINPROGRESS) {
8529 freemsg(mp);
8530 return;
8531 }
8532 }
8533
8534 /*
8535 * If we have a moved ipif to bring up, and everything has
8536 * succeeded to this point, bring it up on the IPMP ill.
8537 * Otherwise, leave it down -- the admin can try to bring it
8538 * up by hand if need be.
8539 */
8540 if (ill->ill_move_ipif != NULL) {
8541 if (err != 0) {
8542 ill->ill_move_ipif = NULL;
8543 } else {
8544 ipif = ill->ill_move_ipif;
8545 ill->ill_move_ipif = NULL;
8546 err = ipif_up(ipif, q, mp1);
8547 if (err == EINPROGRESS) {
8548 freemsg(mp);
8549 return;
8550 }
8551 }
8552 }
8553 break;
8554
8555 case DL_NOTIFY_IND: {
8556 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8557 uint_t orig_mtu, orig_mc_mtu;
8558
8559 switch (notify->dl_notification) {
8560 case DL_NOTE_PHYS_ADDR:
8561 err = ill_set_phys_addr(ill, mp);
8562 break;
8563
8564 case DL_NOTE_REPLUMB:
8565 /*
8566 * Directly return after calling ill_replumb().
8567 * Note that we should not free mp as it is reused
8568 * in the ill_replumb() function.
8569 */
8570 err = ill_replumb(ill, mp);
8571 return;
8572
8573 case DL_NOTE_FASTPATH_FLUSH:
8574 nce_flush(ill, B_FALSE);
8575 break;
8576
8577 case DL_NOTE_SDU_SIZE:
8578 case DL_NOTE_SDU_SIZE2:
8579 /*
8580 * The dce and fragmentation code can cope with
8581 * this changing while packets are being sent.
8582 * When packets are sent ip_output will discover
8583 * a change.
8584 *
8585 * Change the MTU size of the interface.
8586 */
8587 mutex_enter(&ill->ill_lock);
8588 orig_mtu = ill->ill_mtu;
8589 orig_mc_mtu = ill->ill_mc_mtu;
8590 switch (notify->dl_notification) {
8591 case DL_NOTE_SDU_SIZE:
8592 ill->ill_current_frag =
8593 (uint_t)notify->dl_data;
8594 ill->ill_mc_mtu = (uint_t)notify->dl_data;
8595 break;
8596 case DL_NOTE_SDU_SIZE2:
8597 ill->ill_current_frag =
8598 (uint_t)notify->dl_data1;
8599 ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8600 break;
8601 }
8602 if (ill->ill_current_frag > ill->ill_max_frag)
8603 ill->ill_max_frag = ill->ill_current_frag;
8604
8605 if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8606 ill->ill_mtu = ill->ill_current_frag;
8607
8608 /*
8609 * If ill_user_mtu was set (via
8610 * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8611 */
8612 if (ill->ill_user_mtu != 0 &&
8613 ill->ill_user_mtu < ill->ill_mtu)
8614 ill->ill_mtu = ill->ill_user_mtu;
8615
8616 if (ill->ill_user_mtu != 0 &&
8617 ill->ill_user_mtu < ill->ill_mc_mtu)
8618 ill->ill_mc_mtu = ill->ill_user_mtu;
8619
8620 if (ill->ill_isv6) {
8621 if (ill->ill_mtu < IPV6_MIN_MTU)
8622 ill->ill_mtu = IPV6_MIN_MTU;
8623 if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8624 ill->ill_mc_mtu = IPV6_MIN_MTU;
8625 } else {
8626 if (ill->ill_mtu < IP_MIN_MTU)
8627 ill->ill_mtu = IP_MIN_MTU;
8628 if (ill->ill_mc_mtu < IP_MIN_MTU)
8629 ill->ill_mc_mtu = IP_MIN_MTU;
8630 }
8631 } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8632 ill->ill_mc_mtu = ill->ill_mtu;
8633 }
8634
8635 mutex_exit(&ill->ill_lock);
8636 /*
8637 * Make sure all dce_generation checks find out
8638 * that ill_mtu/ill_mc_mtu has changed.
8639 */
8640 if (orig_mtu != ill->ill_mtu ||
8641 orig_mc_mtu != ill->ill_mc_mtu) {
8642 dce_increment_all_generations(ill->ill_isv6,
8643 ill->ill_ipst);
8644 }
8645
8646 /*
8647 * Refresh IPMP meta-interface MTU if necessary.
8648 */
8649 if (IS_UNDER_IPMP(ill))
8650 ipmp_illgrp_refresh_mtu(ill->ill_grp);
8651 break;
8652
8653 case DL_NOTE_LINK_UP:
8654 case DL_NOTE_LINK_DOWN: {
8655 /*
8656 * We are writer. ill / phyint / ipsq assocs stable.
8657 * The RUNNING flag reflects the state of the link.
8658 */
8659 phyint_t *phyint = ill->ill_phyint;
8660 uint64_t new_phyint_flags;
8661 boolean_t changed = B_FALSE;
8662 boolean_t went_up;
8663
8664 went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8665 mutex_enter(&phyint->phyint_lock);
8666
8667 new_phyint_flags = went_up ?
8668 phyint->phyint_flags | PHYI_RUNNING :
8669 phyint->phyint_flags & ~PHYI_RUNNING;
8670
8671 if (IS_IPMP(ill)) {
8672 new_phyint_flags = went_up ?
8673 new_phyint_flags & ~PHYI_FAILED :
8674 new_phyint_flags | PHYI_FAILED;
8675 }
8676
8677 if (new_phyint_flags != phyint->phyint_flags) {
8678 phyint->phyint_flags = new_phyint_flags;
8679 changed = B_TRUE;
8680 }
8681 mutex_exit(&phyint->phyint_lock);
8682 /*
8683 * ill_restart_dad handles the DAD restart and routing
8684 * socket notification logic.
8685 */
8686 if (changed) {
8687 ill_restart_dad(phyint->phyint_illv4, went_up);
8688 ill_restart_dad(phyint->phyint_illv6, went_up);
8689 }
8690 break;
8691 }
8692 case DL_NOTE_PROMISC_ON_PHYS: {
8693 phyint_t *phyint = ill->ill_phyint;
8694
8695 mutex_enter(&phyint->phyint_lock);
8696 phyint->phyint_flags |= PHYI_PROMISC;
8697 mutex_exit(&phyint->phyint_lock);
8698 break;
8699 }
8700 case DL_NOTE_PROMISC_OFF_PHYS: {
8701 phyint_t *phyint = ill->ill_phyint;
8702
8703 mutex_enter(&phyint->phyint_lock);
8704 phyint->phyint_flags &= ~PHYI_PROMISC;
8705 mutex_exit(&phyint->phyint_lock);
8706 break;
8707 }
8708 case DL_NOTE_CAPAB_RENEG:
8709 /*
8710 * Something changed on the driver side.
8711 * It wants us to renegotiate the capabilities
8712 * on this ill. One possible cause is the aggregation
8713 * interface under us where a port got added or
8714 * went away.
8715 *
8716 * If the capability negotiation is already done
8717 * or is in progress, reset the capabilities and
8718 * mark the ill's ill_capab_reneg to be B_TRUE,
8719 * so that when the ack comes back, we can start
8720 * the renegotiation process.
8721 *
8722 * Note that if ill_capab_reneg is already B_TRUE
8723 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8724 * the capability resetting request has been sent
8725 * and the renegotiation has not been started yet;
8726 * nothing needs to be done in this case.
8727 */
8728 ipsq_current_start(ipsq, ill->ill_ipif, 0);
8729 ill_capability_reset(ill, B_TRUE);
8730 ipsq_current_finish(ipsq);
8731 break;
8732
8733 case DL_NOTE_ALLOWED_IPS:
8734 ill_set_allowed_ips(ill, mp);
8735 break;
8736 default:
8737 ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8738 "type 0x%x for DL_NOTIFY_IND\n",
8739 notify->dl_notification));
8740 break;
8741 }
8742
8743 /*
8744 * As this is an asynchronous operation, we
8745 * should not call ill_dlpi_done
8746 */
8747 break;
8748 }
8749 case DL_NOTIFY_ACK: {
8750 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8751
8752 if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8753 ill->ill_note_link = 1;
8754 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8755 break;
8756 }
8757 case DL_PHYS_ADDR_ACK: {
8758 /*
8759 * As part of plumbing the interface via SIOCSLIFNAME,
8760 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8761 * whose answers we receive here. As each answer is received,
8762 * we call ill_dlpi_done() to dispatch the next request as
8763 * we're processing the current one. Once all answers have
8764 * been received, we use ipsq_pending_mp_get() to dequeue the
8765 * outstanding IOCTL and reply to it. (Because ill_dl_phys()
8766 * is invoked from an ill queue, conn_oper_pending_ill is not
8767 * available, but we know the ioctl is pending on ill_wq.)
8768 */
8769 uint_t paddrlen, paddroff;
8770 uint8_t *addr;
8771
8772 paddrreq = ill->ill_phys_addr_pend;
8773 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8774 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8775 addr = mp->b_rptr + paddroff;
8776
8777 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8778 if (paddrreq == DL_IPV6_TOKEN) {
8779 /*
8780 * bcopy to low-order bits of ill_token
8781 *
8782 * XXX Temporary hack - currently, all known tokens
8783 * are 64 bits, so I'll cheat for the moment.
8784 */
8785 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8786 ill->ill_token_length = paddrlen;
8787 break;
8788 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8789 ASSERT(ill->ill_nd_lla_mp == NULL);
8790 ill_set_ndmp(ill, mp, paddroff, paddrlen);
8791 mp = NULL;
8792 break;
8793 } else if (paddrreq == DL_CURR_DEST_ADDR) {
8794 ASSERT(ill->ill_dest_addr_mp == NULL);
8795 ill->ill_dest_addr_mp = mp;
8796 ill->ill_dest_addr = addr;
8797 mp = NULL;
8798 if (ill->ill_isv6) {
8799 ill_setdesttoken(ill);
8800 ipif_setdestlinklocal(ill->ill_ipif);
8801 }
8802 break;
8803 }
8804
8805 ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8806 ASSERT(ill->ill_phys_addr_mp == NULL);
8807 if (!ill->ill_ifname_pending)
8808 break;
8809 ill->ill_ifname_pending = 0;
8810 if (!ioctl_aborted)
8811 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8812 if (mp1 != NULL) {
8813 ASSERT(connp == NULL);
8814 q = ill->ill_wq;
8815 }
8816 /*
8817 * If any error acks received during the plumbing sequence,
8818 * ill_ifname_pending_err will be set. Break out and send up
8819 * the error to the pending ioctl.
8820 */
8821 if (ill->ill_ifname_pending_err != 0) {
8822 err = ill->ill_ifname_pending_err;
8823 ill->ill_ifname_pending_err = 0;
8824 break;
8825 }
8826
8827 ill->ill_phys_addr_mp = mp;
8828 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8829 mp = NULL;
8830
8831 /*
8832 * If paddrlen or ill_phys_addr_length is zero, the DLPI
8833 * provider doesn't support physical addresses. We check both
8834 * paddrlen and ill_phys_addr_length because sppp (PPP) does
8835 * not have physical addresses, but historically adversises a
8836 * physical address length of 0 in its DL_INFO_ACK, but 6 in
8837 * its DL_PHYS_ADDR_ACK.
8838 */
8839 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8840 ill->ill_phys_addr = NULL;
8841 } else if (paddrlen != ill->ill_phys_addr_length) {
8842 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8843 paddrlen, ill->ill_phys_addr_length));
8844 err = EINVAL;
8845 break;
8846 }
8847
8848 if (ill->ill_nd_lla_mp == NULL) {
8849 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8850 err = ENOMEM;
8851 break;
8852 }
8853 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8854 }
8855
8856 if (ill->ill_isv6) {
8857 ill_setdefaulttoken(ill);
8858 ipif_setlinklocal(ill->ill_ipif);
8859 }
8860 break;
8861 }
8862 case DL_OK_ACK:
8863 ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8864 dl_primstr((int)dloa->dl_correct_primitive),
8865 dloa->dl_correct_primitive));
8866 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8867 char *, dl_primstr(dloa->dl_correct_primitive),
8868 ill_t *, ill);
8869
8870 switch (dloa->dl_correct_primitive) {
8871 case DL_ENABMULTI_REQ:
8872 case DL_DISABMULTI_REQ:
8873 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8874 break;
8875 case DL_PROMISCON_REQ:
8876 case DL_PROMISCOFF_REQ:
8877 case DL_UNBIND_REQ:
8878 case DL_ATTACH_REQ:
8879 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8880 break;
8881 }
8882 break;
8883 default:
8884 break;
8885 }
8886
8887 freemsg(mp);
8888 if (mp1 == NULL)
8889 return;
8890
8891 /*
8892 * The operation must complete without EINPROGRESS since
8893 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise,
8894 * the operation will be stuck forever inside the IPSQ.
8895 */
8896 ASSERT(err != EINPROGRESS);
8897
8898 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8899 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8900 ipif_t *, NULL);
8901
8902 switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8903 case 0:
8904 ipsq_current_finish(ipsq);
8905 break;
8906
8907 case SIOCSLIFNAME:
8908 case IF_UNITSEL: {
8909 ill_t *ill_other = ILL_OTHER(ill);
8910
8911 /*
8912 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8913 * ill has a peer which is in an IPMP group, then place ill
8914 * into the same group. One catch: although ifconfig plumbs
8915 * the appropriate IPMP meta-interface prior to plumbing this
8916 * ill, it is possible for multiple ifconfig applications to
8917 * race (or for another application to adjust plumbing), in
8918 * which case the IPMP meta-interface we need will be missing.
8919 * If so, kick the phyint out of the group.
8920 */
8921 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8922 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp;
8923 ipmp_illgrp_t *illg;
8924
8925 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8926 if (illg == NULL)
8927 ipmp_phyint_leave_grp(ill->ill_phyint);
8928 else
8929 ipmp_ill_join_illgrp(ill, illg);
8930 }
8931
8932 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8933 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8934 else
8935 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8936 break;
8937 }
8938 case SIOCLIFADDIF:
8939 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8940 break;
8941
8942 default:
8943 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8944 break;
8945 }
8946 }
8947
8948 /*
8949 * ip_rput_other is called by ip_rput to handle messages modifying the global
8950 * state in IP. If 'ipsq' is non-NULL, caller is writer on it.
8951 */
8952 /* ARGSUSED */
8953 void
ip_rput_other(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * dummy_arg)8954 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8955 {
8956 ill_t *ill = q->q_ptr;
8957 struct iocblk *iocp;
8958
8959 ip1dbg(("ip_rput_other "));
8960 if (ipsq != NULL) {
8961 ASSERT(IAM_WRITER_IPSQ(ipsq));
8962 ASSERT(ipsq->ipsq_xop ==
8963 ill->ill_phyint->phyint_ipsq->ipsq_xop);
8964 }
8965
8966 switch (mp->b_datap->db_type) {
8967 case M_ERROR:
8968 case M_HANGUP:
8969 /*
8970 * The device has a problem. We force the ILL down. It can
8971 * be brought up again manually using SIOCSIFFLAGS (via
8972 * ifconfig or equivalent).
8973 */
8974 ASSERT(ipsq != NULL);
8975 if (mp->b_rptr < mp->b_wptr)
8976 ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8977 if (ill->ill_error == 0)
8978 ill->ill_error = ENXIO;
8979 if (!ill_down_start(q, mp))
8980 return;
8981 ipif_all_down_tail(ipsq, q, mp, NULL);
8982 break;
8983 case M_IOCNAK: {
8984 iocp = (struct iocblk *)mp->b_rptr;
8985
8986 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8987 /*
8988 * If this was the first attempt, turn off the fastpath
8989 * probing.
8990 */
8991 mutex_enter(&ill->ill_lock);
8992 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8993 ill->ill_dlpi_fastpath_state = IDS_FAILED;
8994 mutex_exit(&ill->ill_lock);
8995 /*
8996 * don't flush the nce_t entries: we use them
8997 * as an index to the ncec itself.
8998 */
8999 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
9000 ill->ill_name));
9001 } else {
9002 mutex_exit(&ill->ill_lock);
9003 }
9004 freemsg(mp);
9005 break;
9006 }
9007 default:
9008 ASSERT(0);
9009 break;
9010 }
9011 }
9012
9013 /*
9014 * Update any source route, record route or timestamp options
9015 * When it fails it has consumed the message and BUMPed the MIB.
9016 */
9017 boolean_t
ip_forward_options(mblk_t * mp,ipha_t * ipha,ill_t * dst_ill,ip_recv_attr_t * ira)9018 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
9019 ip_recv_attr_t *ira)
9020 {
9021 ipoptp_t opts;
9022 uchar_t *opt;
9023 uint8_t optval;
9024 uint8_t optlen;
9025 ipaddr_t dst;
9026 ipaddr_t ifaddr;
9027 uint32_t ts;
9028 timestruc_t now;
9029 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9030
9031 ip2dbg(("ip_forward_options\n"));
9032 dst = ipha->ipha_dst;
9033 opt = NULL;
9034
9035 for (optval = ipoptp_first(&opts, ipha);
9036 optval != IPOPT_EOL;
9037 optval = ipoptp_next(&opts)) {
9038 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9039 opt = opts.ipoptp_cur;
9040 optlen = opts.ipoptp_len;
9041 ip2dbg(("ip_forward_options: opt %d, len %d\n",
9042 optval, opts.ipoptp_len));
9043 switch (optval) {
9044 uint32_t off;
9045 case IPOPT_SSRR:
9046 case IPOPT_LSRR:
9047 /* Check if adminstratively disabled */
9048 if (!ipst->ips_ip_forward_src_routed) {
9049 BUMP_MIB(dst_ill->ill_ip_mib,
9050 ipIfStatsForwProhibits);
9051 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
9052 mp, dst_ill);
9053 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
9054 ira);
9055 return (B_FALSE);
9056 }
9057 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9058 /*
9059 * Must be partial since ip_input_options
9060 * checked for strict.
9061 */
9062 break;
9063 }
9064 off = opt[IPOPT_OFFSET];
9065 off--;
9066 redo_srr:
9067 if (optlen < IP_ADDR_LEN ||
9068 off > optlen - IP_ADDR_LEN) {
9069 /* End of source route */
9070 ip1dbg((
9071 "ip_forward_options: end of SR\n"));
9072 break;
9073 }
9074 /* Pick a reasonable address on the outbound if */
9075 ASSERT(dst_ill != NULL);
9076 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9077 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9078 NULL) != 0) {
9079 /* No source! Shouldn't happen */
9080 ifaddr = INADDR_ANY;
9081 }
9082 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9083 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9084 ip1dbg(("ip_forward_options: next hop 0x%x\n",
9085 ntohl(dst)));
9086
9087 /*
9088 * Check if our address is present more than
9089 * once as consecutive hops in source route.
9090 */
9091 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9092 off += IP_ADDR_LEN;
9093 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9094 goto redo_srr;
9095 }
9096 ipha->ipha_dst = dst;
9097 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9098 break;
9099 case IPOPT_RR:
9100 off = opt[IPOPT_OFFSET];
9101 off--;
9102 if (optlen < IP_ADDR_LEN ||
9103 off > optlen - IP_ADDR_LEN) {
9104 /* No more room - ignore */
9105 ip1dbg((
9106 "ip_forward_options: end of RR\n"));
9107 break;
9108 }
9109 /* Pick a reasonable address on the outbound if */
9110 ASSERT(dst_ill != NULL);
9111 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9112 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9113 NULL) != 0) {
9114 /* No source! Shouldn't happen */
9115 ifaddr = INADDR_ANY;
9116 }
9117 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9118 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9119 break;
9120 case IPOPT_TS:
9121 off = 0;
9122 /* Insert timestamp if there is room */
9123 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9124 case IPOPT_TS_TSONLY:
9125 off = IPOPT_TS_TIMELEN;
9126 break;
9127 case IPOPT_TS_PRESPEC:
9128 case IPOPT_TS_PRESPEC_RFC791:
9129 /* Verify that the address matched */
9130 off = opt[IPOPT_OFFSET] - 1;
9131 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9132 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9133 /* Not for us */
9134 break;
9135 }
9136 /* FALLTHROUGH */
9137 case IPOPT_TS_TSANDADDR:
9138 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9139 break;
9140 default:
9141 /*
9142 * ip_*put_options should have already
9143 * dropped this packet.
9144 */
9145 cmn_err(CE_PANIC, "ip_forward_options: "
9146 "unknown IT - bug in ip_input_options?\n");
9147 }
9148 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9149 /* Increase overflow counter */
9150 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9151 opt[IPOPT_POS_OV_FLG] =
9152 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9153 (off << 4));
9154 break;
9155 }
9156 off = opt[IPOPT_OFFSET] - 1;
9157 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9158 case IPOPT_TS_PRESPEC:
9159 case IPOPT_TS_PRESPEC_RFC791:
9160 case IPOPT_TS_TSANDADDR:
9161 /* Pick a reasonable addr on the outbound if */
9162 ASSERT(dst_ill != NULL);
9163 if (ip_select_source_v4(dst_ill, INADDR_ANY,
9164 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
9165 NULL, NULL) != 0) {
9166 /* No source! Shouldn't happen */
9167 ifaddr = INADDR_ANY;
9168 }
9169 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9170 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9171 /* FALLTHROUGH */
9172 case IPOPT_TS_TSONLY:
9173 off = opt[IPOPT_OFFSET] - 1;
9174 /* Compute # of milliseconds since midnight */
9175 gethrestime(&now);
9176 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9177 NSEC2MSEC(now.tv_nsec);
9178 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9179 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9180 break;
9181 }
9182 break;
9183 }
9184 }
9185 return (B_TRUE);
9186 }
9187
9188 /*
9189 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
9190 * returns 'true' if there are still fragments left on the queue, in
9191 * which case we restart the timer.
9192 */
9193 void
ill_frag_timer(void * arg)9194 ill_frag_timer(void *arg)
9195 {
9196 ill_t *ill = (ill_t *)arg;
9197 boolean_t frag_pending;
9198 ip_stack_t *ipst = ill->ill_ipst;
9199 time_t timeout;
9200
9201 mutex_enter(&ill->ill_lock);
9202 ASSERT(!ill->ill_fragtimer_executing);
9203 if (ill->ill_state_flags & ILL_CONDEMNED) {
9204 ill->ill_frag_timer_id = 0;
9205 mutex_exit(&ill->ill_lock);
9206 return;
9207 }
9208 ill->ill_fragtimer_executing = 1;
9209 mutex_exit(&ill->ill_lock);
9210
9211 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9212 ipst->ips_ip_reassembly_timeout);
9213
9214 frag_pending = ill_frag_timeout(ill, timeout);
9215
9216 /*
9217 * Restart the timer, if we have fragments pending or if someone
9218 * wanted us to be scheduled again.
9219 */
9220 mutex_enter(&ill->ill_lock);
9221 ill->ill_fragtimer_executing = 0;
9222 ill->ill_frag_timer_id = 0;
9223 if (frag_pending || ill->ill_fragtimer_needrestart)
9224 ill_frag_timer_start(ill);
9225 mutex_exit(&ill->ill_lock);
9226 }
9227
9228 void
ill_frag_timer_start(ill_t * ill)9229 ill_frag_timer_start(ill_t *ill)
9230 {
9231 ip_stack_t *ipst = ill->ill_ipst;
9232 clock_t timeo_ms;
9233
9234 ASSERT(MUTEX_HELD(&ill->ill_lock));
9235
9236 /* If the ill is closing or opening don't proceed */
9237 if (ill->ill_state_flags & ILL_CONDEMNED)
9238 return;
9239
9240 if (ill->ill_fragtimer_executing) {
9241 /*
9242 * ill_frag_timer is currently executing. Just record the
9243 * the fact that we want the timer to be restarted.
9244 * ill_frag_timer will post a timeout before it returns,
9245 * ensuring it will be called again.
9246 */
9247 ill->ill_fragtimer_needrestart = 1;
9248 return;
9249 }
9250
9251 if (ill->ill_frag_timer_id == 0) {
9252 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9253 ipst->ips_ip_reassembly_timeout) * SECONDS;
9254
9255 /*
9256 * The timer is neither running nor is the timeout handler
9257 * executing. Post a timeout so that ill_frag_timer will be
9258 * called
9259 */
9260 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
9261 MSEC_TO_TICK(timeo_ms >> 1));
9262 ill->ill_fragtimer_needrestart = 0;
9263 }
9264 }
9265
9266 /*
9267 * Update any source route, record route or timestamp options.
9268 * Check that we are at end of strict source route.
9269 * The options have already been checked for sanity in ip_input_options().
9270 */
9271 boolean_t
ip_input_local_options(mblk_t * mp,ipha_t * ipha,ip_recv_attr_t * ira)9272 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
9273 {
9274 ipoptp_t opts;
9275 uchar_t *opt;
9276 uint8_t optval;
9277 uint8_t optlen;
9278 ipaddr_t dst;
9279 ipaddr_t ifaddr;
9280 uint32_t ts;
9281 timestruc_t now;
9282 ill_t *ill = ira->ira_ill;
9283 ip_stack_t *ipst = ill->ill_ipst;
9284
9285 ip2dbg(("ip_input_local_options\n"));
9286 opt = NULL;
9287
9288 for (optval = ipoptp_first(&opts, ipha);
9289 optval != IPOPT_EOL;
9290 optval = ipoptp_next(&opts)) {
9291 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9292 opt = opts.ipoptp_cur;
9293 optlen = opts.ipoptp_len;
9294 ip2dbg(("ip_input_local_options: opt %d, len %d\n",
9295 optval, optlen));
9296 switch (optval) {
9297 uint32_t off;
9298 case IPOPT_SSRR:
9299 case IPOPT_LSRR:
9300 off = opt[IPOPT_OFFSET];
9301 off--;
9302 if (optlen < IP_ADDR_LEN ||
9303 off > optlen - IP_ADDR_LEN) {
9304 /* End of source route */
9305 ip1dbg(("ip_input_local_options: end of SR\n"));
9306 break;
9307 }
9308 /*
9309 * This will only happen if two consecutive entries
9310 * in the source route contains our address or if
9311 * it is a packet with a loose source route which
9312 * reaches us before consuming the whole source route
9313 */
9314 ip1dbg(("ip_input_local_options: not end of SR\n"));
9315 if (optval == IPOPT_SSRR) {
9316 goto bad_src_route;
9317 }
9318 /*
9319 * Hack: instead of dropping the packet truncate the
9320 * source route to what has been used by filling the
9321 * rest with IPOPT_NOP.
9322 */
9323 opt[IPOPT_OLEN] = (uint8_t)off;
9324 while (off < optlen) {
9325 opt[off++] = IPOPT_NOP;
9326 }
9327 break;
9328 case IPOPT_RR:
9329 off = opt[IPOPT_OFFSET];
9330 off--;
9331 if (optlen < IP_ADDR_LEN ||
9332 off > optlen - IP_ADDR_LEN) {
9333 /* No more room - ignore */
9334 ip1dbg((
9335 "ip_input_local_options: end of RR\n"));
9336 break;
9337 }
9338 /* Pick a reasonable address on the outbound if */
9339 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9340 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9341 NULL) != 0) {
9342 /* No source! Shouldn't happen */
9343 ifaddr = INADDR_ANY;
9344 }
9345 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9346 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9347 break;
9348 case IPOPT_TS:
9349 off = 0;
9350 /* Insert timestamp if there is romm */
9351 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9352 case IPOPT_TS_TSONLY:
9353 off = IPOPT_TS_TIMELEN;
9354 break;
9355 case IPOPT_TS_PRESPEC:
9356 case IPOPT_TS_PRESPEC_RFC791:
9357 /* Verify that the address matched */
9358 off = opt[IPOPT_OFFSET] - 1;
9359 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9360 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9361 /* Not for us */
9362 break;
9363 }
9364 /* FALLTHROUGH */
9365 case IPOPT_TS_TSANDADDR:
9366 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9367 break;
9368 default:
9369 /*
9370 * ip_*put_options should have already
9371 * dropped this packet.
9372 */
9373 cmn_err(CE_PANIC, "ip_input_local_options: "
9374 "unknown IT - bug in ip_input_options?\n");
9375 }
9376 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9377 /* Increase overflow counter */
9378 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9379 opt[IPOPT_POS_OV_FLG] =
9380 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9381 (off << 4));
9382 break;
9383 }
9384 off = opt[IPOPT_OFFSET] - 1;
9385 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9386 case IPOPT_TS_PRESPEC:
9387 case IPOPT_TS_PRESPEC_RFC791:
9388 case IPOPT_TS_TSANDADDR:
9389 /* Pick a reasonable addr on the outbound if */
9390 if (ip_select_source_v4(ill, INADDR_ANY,
9391 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9392 &ifaddr, NULL, NULL) != 0) {
9393 /* No source! Shouldn't happen */
9394 ifaddr = INADDR_ANY;
9395 }
9396 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9397 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9398 /* FALLTHROUGH */
9399 case IPOPT_TS_TSONLY:
9400 off = opt[IPOPT_OFFSET] - 1;
9401 /* Compute # of milliseconds since midnight */
9402 gethrestime(&now);
9403 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9404 NSEC2MSEC(now.tv_nsec);
9405 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9406 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9407 break;
9408 }
9409 break;
9410 }
9411 }
9412 return (B_TRUE);
9413
9414 bad_src_route:
9415 /* make sure we clear any indication of a hardware checksum */
9416 DB_CKSUMFLAGS(mp) = 0;
9417 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9418 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9419 return (B_FALSE);
9420
9421 }
9422
9423 /*
9424 * Process IP options in an inbound packet. Always returns the nexthop.
9425 * Normally this is the passed in nexthop, but if there is an option
9426 * that effects the nexthop (such as a source route) that will be returned.
9427 * Sets *errorp if there is an error, in which case an ICMP error has been sent
9428 * and mp freed.
9429 */
9430 ipaddr_t
ip_input_options(ipha_t * ipha,ipaddr_t dst,mblk_t * mp,ip_recv_attr_t * ira,int * errorp)9431 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9432 ip_recv_attr_t *ira, int *errorp)
9433 {
9434 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9435 ipoptp_t opts;
9436 uchar_t *opt;
9437 uint8_t optval;
9438 uint8_t optlen;
9439 intptr_t code = 0;
9440 ire_t *ire;
9441
9442 ip2dbg(("ip_input_options\n"));
9443 opt = NULL;
9444 *errorp = 0;
9445 for (optval = ipoptp_first(&opts, ipha);
9446 optval != IPOPT_EOL;
9447 optval = ipoptp_next(&opts)) {
9448 opt = opts.ipoptp_cur;
9449 optlen = opts.ipoptp_len;
9450 ip2dbg(("ip_input_options: opt %d, len %d\n",
9451 optval, optlen));
9452 /*
9453 * Note: we need to verify the checksum before we
9454 * modify anything thus this routine only extracts the next
9455 * hop dst from any source route.
9456 */
9457 switch (optval) {
9458 uint32_t off;
9459 case IPOPT_SSRR:
9460 case IPOPT_LSRR:
9461 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9462 if (optval == IPOPT_SSRR) {
9463 ip1dbg(("ip_input_options: not next"
9464 " strict source route 0x%x\n",
9465 ntohl(dst)));
9466 code = (char *)&ipha->ipha_dst -
9467 (char *)ipha;
9468 goto param_prob; /* RouterReq's */
9469 }
9470 ip2dbg(("ip_input_options: "
9471 "not next source route 0x%x\n",
9472 ntohl(dst)));
9473 break;
9474 }
9475
9476 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9477 ip1dbg((
9478 "ip_input_options: bad option offset\n"));
9479 code = (char *)&opt[IPOPT_OLEN] -
9480 (char *)ipha;
9481 goto param_prob;
9482 }
9483 off = opt[IPOPT_OFFSET];
9484 off--;
9485 redo_srr:
9486 if (optlen < IP_ADDR_LEN ||
9487 off > optlen - IP_ADDR_LEN) {
9488 /* End of source route */
9489 ip1dbg(("ip_input_options: end of SR\n"));
9490 break;
9491 }
9492 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9493 ip1dbg(("ip_input_options: next hop 0x%x\n",
9494 ntohl(dst)));
9495
9496 /*
9497 * Check if our address is present more than
9498 * once as consecutive hops in source route.
9499 * XXX verify per-interface ip_forwarding
9500 * for source route?
9501 */
9502 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9503 off += IP_ADDR_LEN;
9504 goto redo_srr;
9505 }
9506
9507 if (dst == htonl(INADDR_LOOPBACK)) {
9508 ip1dbg(("ip_input_options: loopback addr in "
9509 "source route!\n"));
9510 goto bad_src_route;
9511 }
9512 /*
9513 * For strict: verify that dst is directly
9514 * reachable.
9515 */
9516 if (optval == IPOPT_SSRR) {
9517 ire = ire_ftable_lookup_v4(dst, 0, 0,
9518 IRE_INTERFACE, NULL, ALL_ZONES,
9519 ira->ira_tsl,
9520 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
9521 NULL);
9522 if (ire == NULL) {
9523 ip1dbg(("ip_input_options: SSRR not "
9524 "directly reachable: 0x%x\n",
9525 ntohl(dst)));
9526 goto bad_src_route;
9527 }
9528 ire_refrele(ire);
9529 }
9530 /*
9531 * Defer update of the offset and the record route
9532 * until the packet is forwarded.
9533 */
9534 break;
9535 case IPOPT_RR:
9536 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9537 ip1dbg((
9538 "ip_input_options: bad option offset\n"));
9539 code = (char *)&opt[IPOPT_OLEN] -
9540 (char *)ipha;
9541 goto param_prob;
9542 }
9543 break;
9544 case IPOPT_TS:
9545 /*
9546 * Verify that length >= 5 and that there is either
9547 * room for another timestamp or that the overflow
9548 * counter is not maxed out.
9549 */
9550 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9551 if (optlen < IPOPT_MINLEN_IT) {
9552 goto param_prob;
9553 }
9554 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9555 ip1dbg((
9556 "ip_input_options: bad option offset\n"));
9557 code = (char *)&opt[IPOPT_OFFSET] -
9558 (char *)ipha;
9559 goto param_prob;
9560 }
9561 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9562 case IPOPT_TS_TSONLY:
9563 off = IPOPT_TS_TIMELEN;
9564 break;
9565 case IPOPT_TS_TSANDADDR:
9566 case IPOPT_TS_PRESPEC:
9567 case IPOPT_TS_PRESPEC_RFC791:
9568 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9569 break;
9570 default:
9571 code = (char *)&opt[IPOPT_POS_OV_FLG] -
9572 (char *)ipha;
9573 goto param_prob;
9574 }
9575 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9576 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9577 /*
9578 * No room and the overflow counter is 15
9579 * already.
9580 */
9581 goto param_prob;
9582 }
9583 break;
9584 }
9585 }
9586
9587 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9588 return (dst);
9589 }
9590
9591 ip1dbg(("ip_input_options: error processing IP options."));
9592 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9593
9594 param_prob:
9595 /* make sure we clear any indication of a hardware checksum */
9596 DB_CKSUMFLAGS(mp) = 0;
9597 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9598 icmp_param_problem(mp, (uint8_t)code, ira);
9599 *errorp = -1;
9600 return (dst);
9601
9602 bad_src_route:
9603 /* make sure we clear any indication of a hardware checksum */
9604 DB_CKSUMFLAGS(mp) = 0;
9605 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9606 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9607 *errorp = -1;
9608 return (dst);
9609 }
9610
9611 /*
9612 * IP & ICMP info in >=14 msg's ...
9613 * - ip fixed part (mib2_ip_t)
9614 * - icmp fixed part (mib2_icmp_t)
9615 * - ipAddrEntryTable (ip 20) all IPv4 ipifs
9616 * - ipRouteEntryTable (ip 21) all IPv4 IREs
9617 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries
9618 * - ipRouteAttributeTable (ip 102) labeled routes
9619 * - ip multicast membership (ip_member_t)
9620 * - ip multicast source filtering (ip_grpsrc_t)
9621 * - igmp fixed part (struct igmpstat)
9622 * - multicast routing stats (struct mrtstat)
9623 * - multicast routing vifs (array of struct vifctl)
9624 * - multicast routing routes (array of struct mfcctl)
9625 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9626 * One per ill plus one generic
9627 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9628 * One per ill plus one generic
9629 * - ipv6RouteEntry all IPv6 IREs
9630 * - ipv6RouteAttributeTable (ip6 102) labeled routes
9631 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries
9632 * - ipv6AddrEntry all IPv6 ipifs
9633 * - ipv6 multicast membership (ipv6_member_t)
9634 * - ipv6 multicast source filtering (ipv6_grpsrc_t)
9635 *
9636 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9637 * already filled in by the caller.
9638 * If legacy_req is true then MIB structures needs to be truncated to their
9639 * legacy sizes before being returned.
9640 * Return value of 0 indicates that no messages were sent and caller
9641 * should free mpctl.
9642 */
9643 int
ip_snmp_get(queue_t * q,mblk_t * mpctl,int level,boolean_t legacy_req)9644 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9645 {
9646 ip_stack_t *ipst;
9647 sctp_stack_t *sctps;
9648
9649 if (q->q_next != NULL) {
9650 ipst = ILLQ_TO_IPST(q);
9651 } else {
9652 ipst = CONNQ_TO_IPST(q);
9653 }
9654 ASSERT(ipst != NULL);
9655 sctps = ipst->ips_netstack->netstack_sctp;
9656
9657 if (mpctl == NULL || mpctl->b_cont == NULL) {
9658 return (0);
9659 }
9660
9661 /*
9662 * For the purposes of the (broken) packet shell use
9663 * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9664 * to make TCP and UDP appear first in the list of mib items.
9665 * TBD: We could expand this and use it in netstat so that
9666 * the kernel doesn't have to produce large tables (connections,
9667 * routes, etc) when netstat only wants the statistics or a particular
9668 * table.
9669 */
9670 if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9671 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9672 return (1);
9673 }
9674 }
9675
9676 if (level != MIB2_TCP) {
9677 if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9678 return (1);
9679 }
9680 if (level == MIB2_UDP) {
9681 goto done;
9682 }
9683 }
9684
9685 if (level != MIB2_UDP) {
9686 if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9687 return (1);
9688 }
9689 if (level == MIB2_TCP) {
9690 goto done;
9691 }
9692 }
9693
9694 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9695 ipst, legacy_req)) == NULL) {
9696 return (1);
9697 }
9698
9699 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9700 legacy_req)) == NULL) {
9701 return (1);
9702 }
9703
9704 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9705 return (1);
9706 }
9707
9708 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9709 return (1);
9710 }
9711
9712 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9713 return (1);
9714 }
9715
9716 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9717 return (1);
9718 }
9719
9720 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9721 legacy_req)) == NULL) {
9722 return (1);
9723 }
9724
9725 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9726 legacy_req)) == NULL) {
9727 return (1);
9728 }
9729
9730 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9731 return (1);
9732 }
9733
9734 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9735 return (1);
9736 }
9737
9738 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9739 return (1);
9740 }
9741
9742 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9743 return (1);
9744 }
9745
9746 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9747 return (1);
9748 }
9749
9750 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9751 return (1);
9752 }
9753
9754 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9755 if (mpctl == NULL)
9756 return (1);
9757
9758 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9759 if (mpctl == NULL)
9760 return (1);
9761
9762 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9763 return (1);
9764 }
9765 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9766 return (1);
9767 }
9768 done:
9769 freemsg(mpctl);
9770 return (1);
9771 }
9772
9773 /* Get global (legacy) IPv4 statistics */
9774 static mblk_t *
ip_snmp_get_mib2_ip(queue_t * q,mblk_t * mpctl,mib2_ipIfStatsEntry_t * ipmib,ip_stack_t * ipst,boolean_t legacy_req)9775 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9776 ip_stack_t *ipst, boolean_t legacy_req)
9777 {
9778 mib2_ip_t old_ip_mib;
9779 struct opthdr *optp;
9780 mblk_t *mp2ctl;
9781 mib2_ipAddrEntry_t mae;
9782
9783 /*
9784 * make a copy of the original message
9785 */
9786 mp2ctl = copymsg(mpctl);
9787
9788 /* fixed length IP structure... */
9789 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9790 optp->level = MIB2_IP;
9791 optp->name = 0;
9792 SET_MIB(old_ip_mib.ipForwarding,
9793 (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9794 SET_MIB(old_ip_mib.ipDefaultTTL,
9795 (uint32_t)ipst->ips_ip_def_ttl);
9796 SET_MIB(old_ip_mib.ipReasmTimeout,
9797 ipst->ips_ip_reassembly_timeout);
9798 SET_MIB(old_ip_mib.ipAddrEntrySize,
9799 (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9800 sizeof (mib2_ipAddrEntry_t));
9801 SET_MIB(old_ip_mib.ipRouteEntrySize,
9802 sizeof (mib2_ipRouteEntry_t));
9803 SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9804 sizeof (mib2_ipNetToMediaEntry_t));
9805 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9806 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9807 SET_MIB(old_ip_mib.ipRouteAttributeSize,
9808 sizeof (mib2_ipAttributeEntry_t));
9809 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t));
9810 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9811
9812 /*
9813 * Grab the statistics from the new IP MIB
9814 */
9815 SET_MIB(old_ip_mib.ipInReceives,
9816 (uint32_t)ipmib->ipIfStatsHCInReceives);
9817 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9818 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9819 SET_MIB(old_ip_mib.ipForwDatagrams,
9820 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9821 SET_MIB(old_ip_mib.ipInUnknownProtos,
9822 ipmib->ipIfStatsInUnknownProtos);
9823 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9824 SET_MIB(old_ip_mib.ipInDelivers,
9825 (uint32_t)ipmib->ipIfStatsHCInDelivers);
9826 SET_MIB(old_ip_mib.ipOutRequests,
9827 (uint32_t)ipmib->ipIfStatsHCOutRequests);
9828 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9829 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9830 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9831 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9832 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9833 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9834 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9835 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9836
9837 /* ipRoutingDiscards is not being used */
9838 SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9839 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9840 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9841 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9842 SET_MIB(old_ip_mib.ipReasmDuplicates,
9843 ipmib->ipIfStatsReasmDuplicates);
9844 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9845 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9846 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9847 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9848 SET_MIB(old_ip_mib.rawipInOverflows,
9849 ipmib->rawipIfStatsInOverflows);
9850
9851 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9852 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9853 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9854 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9855 SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9856 ipmib->ipIfStatsOutSwitchIPVersion);
9857
9858 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9859 (int)sizeof (old_ip_mib))) {
9860 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9861 (uint_t)sizeof (old_ip_mib)));
9862 }
9863
9864 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9865 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9866 (int)optp->level, (int)optp->name, (int)optp->len));
9867 qreply(q, mpctl);
9868 return (mp2ctl);
9869 }
9870
9871 /* Per interface IPv4 statistics */
9872 static mblk_t *
ip_snmp_get_mib2_ip_traffic_stats(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst,boolean_t legacy_req)9873 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9874 boolean_t legacy_req)
9875 {
9876 struct opthdr *optp;
9877 mblk_t *mp2ctl;
9878 ill_t *ill;
9879 ill_walk_context_t ctx;
9880 mblk_t *mp_tail = NULL;
9881 mib2_ipIfStatsEntry_t global_ip_mib;
9882 mib2_ipAddrEntry_t mae;
9883
9884 /*
9885 * Make a copy of the original message
9886 */
9887 mp2ctl = copymsg(mpctl);
9888
9889 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9890 optp->level = MIB2_IP;
9891 optp->name = MIB2_IP_TRAFFIC_STATS;
9892 /* Include "unknown interface" ip_mib */
9893 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9894 ipst->ips_ip_mib.ipIfStatsIfIndex =
9895 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9896 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9897 (ipst->ips_ip_forwarding ? 1 : 2));
9898 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9899 (uint32_t)ipst->ips_ip_def_ttl);
9900 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9901 sizeof (mib2_ipIfStatsEntry_t));
9902 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9903 sizeof (mib2_ipAddrEntry_t));
9904 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9905 sizeof (mib2_ipRouteEntry_t));
9906 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9907 sizeof (mib2_ipNetToMediaEntry_t));
9908 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9909 sizeof (ip_member_t));
9910 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9911 sizeof (ip_grpsrc_t));
9912
9913 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9914
9915 if (legacy_req) {
9916 SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9917 LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9918 }
9919
9920 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9921 (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9922 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9923 "failed to allocate %u bytes\n",
9924 (uint_t)sizeof (global_ip_mib)));
9925 }
9926
9927 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9928 ill = ILL_START_WALK_V4(&ctx, ipst);
9929 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9930 ill->ill_ip_mib->ipIfStatsIfIndex =
9931 ill->ill_phyint->phyint_ifindex;
9932 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9933 (ipst->ips_ip_forwarding ? 1 : 2));
9934 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9935 (uint32_t)ipst->ips_ip_def_ttl);
9936
9937 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9938 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9939 (char *)ill->ill_ip_mib,
9940 (int)sizeof (*ill->ill_ip_mib))) {
9941 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9942 "failed to allocate %u bytes\n",
9943 (uint_t)sizeof (*ill->ill_ip_mib)));
9944 }
9945 }
9946 rw_exit(&ipst->ips_ill_g_lock);
9947
9948 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9949 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9950 "level %d, name %d, len %d\n",
9951 (int)optp->level, (int)optp->name, (int)optp->len));
9952 qreply(q, mpctl);
9953
9954 if (mp2ctl == NULL)
9955 return (NULL);
9956
9957 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9958 legacy_req));
9959 }
9960
9961 /* Global IPv4 ICMP statistics */
9962 static mblk_t *
ip_snmp_get_mib2_icmp(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)9963 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9964 {
9965 struct opthdr *optp;
9966 mblk_t *mp2ctl;
9967
9968 /*
9969 * Make a copy of the original message
9970 */
9971 mp2ctl = copymsg(mpctl);
9972
9973 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9974 optp->level = MIB2_ICMP;
9975 optp->name = 0;
9976 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9977 (int)sizeof (ipst->ips_icmp_mib))) {
9978 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9979 (uint_t)sizeof (ipst->ips_icmp_mib)));
9980 }
9981 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9982 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9983 (int)optp->level, (int)optp->name, (int)optp->len));
9984 qreply(q, mpctl);
9985 return (mp2ctl);
9986 }
9987
9988 /* Global IPv4 IGMP statistics */
9989 static mblk_t *
ip_snmp_get_mib2_igmp(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)9990 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9991 {
9992 struct opthdr *optp;
9993 mblk_t *mp2ctl;
9994
9995 /*
9996 * make a copy of the original message
9997 */
9998 mp2ctl = copymsg(mpctl);
9999
10000 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10001 optp->level = EXPER_IGMP;
10002 optp->name = 0;
10003 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
10004 (int)sizeof (ipst->ips_igmpstat))) {
10005 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
10006 (uint_t)sizeof (ipst->ips_igmpstat)));
10007 }
10008 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10009 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
10010 (int)optp->level, (int)optp->name, (int)optp->len));
10011 qreply(q, mpctl);
10012 return (mp2ctl);
10013 }
10014
10015 /* Global IPv4 Multicast Routing statistics */
10016 static mblk_t *
ip_snmp_get_mib2_multi(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10017 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10018 {
10019 struct opthdr *optp;
10020 mblk_t *mp2ctl;
10021
10022 /*
10023 * make a copy of the original message
10024 */
10025 mp2ctl = copymsg(mpctl);
10026
10027 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10028 optp->level = EXPER_DVMRP;
10029 optp->name = 0;
10030 if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
10031 ip0dbg(("ip_mroute_stats: failed\n"));
10032 }
10033 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10034 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
10035 (int)optp->level, (int)optp->name, (int)optp->len));
10036 qreply(q, mpctl);
10037 return (mp2ctl);
10038 }
10039
10040 /* IPv4 address information */
10041 static mblk_t *
ip_snmp_get_mib2_ip_addr(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst,boolean_t legacy_req)10042 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10043 boolean_t legacy_req)
10044 {
10045 struct opthdr *optp;
10046 mblk_t *mp2ctl;
10047 mblk_t *mp_tail = NULL;
10048 ill_t *ill;
10049 ipif_t *ipif;
10050 uint_t bitval;
10051 mib2_ipAddrEntry_t mae;
10052 size_t mae_size;
10053 zoneid_t zoneid;
10054 ill_walk_context_t ctx;
10055
10056 /*
10057 * make a copy of the original message
10058 */
10059 mp2ctl = copymsg(mpctl);
10060
10061 mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
10062 sizeof (mib2_ipAddrEntry_t);
10063
10064 /* ipAddrEntryTable */
10065
10066 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10067 optp->level = MIB2_IP;
10068 optp->name = MIB2_IP_ADDR;
10069 zoneid = Q_TO_CONN(q)->conn_zoneid;
10070
10071 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10072 ill = ILL_START_WALK_V4(&ctx, ipst);
10073 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10074 for (ipif = ill->ill_ipif; ipif != NULL;
10075 ipif = ipif->ipif_next) {
10076 if (ipif->ipif_zoneid != zoneid &&
10077 ipif->ipif_zoneid != ALL_ZONES)
10078 continue;
10079 /* Sum of count from dead IRE_LO* and our current */
10080 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10081 if (ipif->ipif_ire_local != NULL) {
10082 mae.ipAdEntInfo.ae_ibcnt +=
10083 ipif->ipif_ire_local->ire_ib_pkt_count;
10084 }
10085 mae.ipAdEntInfo.ae_obcnt = 0;
10086 mae.ipAdEntInfo.ae_focnt = 0;
10087
10088 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
10089 OCTET_LENGTH);
10090 mae.ipAdEntIfIndex.o_length =
10091 mi_strlen(mae.ipAdEntIfIndex.o_bytes);
10092 mae.ipAdEntAddr = ipif->ipif_lcl_addr;
10093 mae.ipAdEntNetMask = ipif->ipif_net_mask;
10094 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
10095 mae.ipAdEntInfo.ae_subnet_len =
10096 ip_mask_to_plen(ipif->ipif_net_mask);
10097 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
10098 for (bitval = 1;
10099 bitval &&
10100 !(bitval & ipif->ipif_brd_addr);
10101 bitval <<= 1)
10102 noop;
10103 mae.ipAdEntBcastAddr = bitval;
10104 mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
10105 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10106 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric;
10107 mae.ipAdEntInfo.ae_broadcast_addr =
10108 ipif->ipif_brd_addr;
10109 mae.ipAdEntInfo.ae_pp_dst_addr =
10110 ipif->ipif_pp_dst_addr;
10111 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
10112 ill->ill_flags | ill->ill_phyint->phyint_flags;
10113 mae.ipAdEntRetransmitTime =
10114 ill->ill_reachable_retrans_time;
10115
10116 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10117 (char *)&mae, (int)mae_size)) {
10118 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
10119 "allocate %u bytes\n", (uint_t)mae_size));
10120 }
10121 }
10122 }
10123 rw_exit(&ipst->ips_ill_g_lock);
10124
10125 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10126 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
10127 (int)optp->level, (int)optp->name, (int)optp->len));
10128 qreply(q, mpctl);
10129 return (mp2ctl);
10130 }
10131
10132 /* IPv6 address information */
10133 static mblk_t *
ip_snmp_get_mib2_ip6_addr(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst,boolean_t legacy_req)10134 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10135 boolean_t legacy_req)
10136 {
10137 struct opthdr *optp;
10138 mblk_t *mp2ctl;
10139 mblk_t *mp_tail = NULL;
10140 ill_t *ill;
10141 ipif_t *ipif;
10142 mib2_ipv6AddrEntry_t mae6;
10143 size_t mae6_size;
10144 zoneid_t zoneid;
10145 ill_walk_context_t ctx;
10146
10147 /*
10148 * make a copy of the original message
10149 */
10150 mp2ctl = copymsg(mpctl);
10151
10152 mae6_size = (legacy_req) ?
10153 LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
10154 sizeof (mib2_ipv6AddrEntry_t);
10155
10156 /* ipv6AddrEntryTable */
10157
10158 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10159 optp->level = MIB2_IP6;
10160 optp->name = MIB2_IP6_ADDR;
10161 zoneid = Q_TO_CONN(q)->conn_zoneid;
10162
10163 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10164 ill = ILL_START_WALK_V6(&ctx, ipst);
10165 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10166 for (ipif = ill->ill_ipif; ipif != NULL;
10167 ipif = ipif->ipif_next) {
10168 if (ipif->ipif_zoneid != zoneid &&
10169 ipif->ipif_zoneid != ALL_ZONES)
10170 continue;
10171 /* Sum of count from dead IRE_LO* and our current */
10172 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10173 if (ipif->ipif_ire_local != NULL) {
10174 mae6.ipv6AddrInfo.ae_ibcnt +=
10175 ipif->ipif_ire_local->ire_ib_pkt_count;
10176 }
10177 mae6.ipv6AddrInfo.ae_obcnt = 0;
10178 mae6.ipv6AddrInfo.ae_focnt = 0;
10179
10180 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
10181 OCTET_LENGTH);
10182 mae6.ipv6AddrIfIndex.o_length =
10183 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
10184 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
10185 mae6.ipv6AddrPfxLength =
10186 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10187 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
10188 mae6.ipv6AddrInfo.ae_subnet_len =
10189 mae6.ipv6AddrPfxLength;
10190 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
10191
10192 /* Type: stateless(1), stateful(2), unknown(3) */
10193 if (ipif->ipif_flags & IPIF_ADDRCONF)
10194 mae6.ipv6AddrType = 1;
10195 else
10196 mae6.ipv6AddrType = 2;
10197 /* Anycast: true(1), false(2) */
10198 if (ipif->ipif_flags & IPIF_ANYCAST)
10199 mae6.ipv6AddrAnycastFlag = 1;
10200 else
10201 mae6.ipv6AddrAnycastFlag = 2;
10202
10203 /*
10204 * Address status: preferred(1), deprecated(2),
10205 * invalid(3), inaccessible(4), unknown(5)
10206 */
10207 if (ipif->ipif_flags & IPIF_NOLOCAL)
10208 mae6.ipv6AddrStatus = 3;
10209 else if (ipif->ipif_flags & IPIF_DEPRECATED)
10210 mae6.ipv6AddrStatus = 2;
10211 else
10212 mae6.ipv6AddrStatus = 1;
10213 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10214 mae6.ipv6AddrInfo.ae_metric =
10215 ipif->ipif_ill->ill_metric;
10216 mae6.ipv6AddrInfo.ae_pp_dst_addr =
10217 ipif->ipif_v6pp_dst_addr;
10218 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
10219 ill->ill_flags | ill->ill_phyint->phyint_flags;
10220 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
10221 mae6.ipv6AddrIdentifier = ill->ill_token;
10222 mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
10223 mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
10224 mae6.ipv6AddrRetransmitTime =
10225 ill->ill_reachable_retrans_time;
10226 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10227 (char *)&mae6, (int)mae6_size)) {
10228 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
10229 "allocate %u bytes\n",
10230 (uint_t)mae6_size));
10231 }
10232 }
10233 }
10234 rw_exit(&ipst->ips_ill_g_lock);
10235
10236 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10237 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
10238 (int)optp->level, (int)optp->name, (int)optp->len));
10239 qreply(q, mpctl);
10240 return (mp2ctl);
10241 }
10242
10243 /* IPv4 multicast group membership. */
10244 static mblk_t *
ip_snmp_get_mib2_ip_group_mem(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10245 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10246 {
10247 struct opthdr *optp;
10248 mblk_t *mp2ctl;
10249 ill_t *ill;
10250 ipif_t *ipif;
10251 ilm_t *ilm;
10252 ip_member_t ipm;
10253 mblk_t *mp_tail = NULL;
10254 ill_walk_context_t ctx;
10255 zoneid_t zoneid;
10256
10257 /*
10258 * make a copy of the original message
10259 */
10260 mp2ctl = copymsg(mpctl);
10261 zoneid = Q_TO_CONN(q)->conn_zoneid;
10262
10263 /* ipGroupMember table */
10264 optp = (struct opthdr *)&mpctl->b_rptr[
10265 sizeof (struct T_optmgmt_ack)];
10266 optp->level = MIB2_IP;
10267 optp->name = EXPER_IP_GROUP_MEMBERSHIP;
10268
10269 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10270 ill = ILL_START_WALK_V4(&ctx, ipst);
10271 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10272 /* Make sure the ill isn't going away. */
10273 if (!ill_check_and_refhold(ill))
10274 continue;
10275 rw_exit(&ipst->ips_ill_g_lock);
10276 rw_enter(&ill->ill_mcast_lock, RW_READER);
10277 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10278 if (ilm->ilm_zoneid != zoneid &&
10279 ilm->ilm_zoneid != ALL_ZONES)
10280 continue;
10281
10282 /* Is there an ipif for ilm_ifaddr? */
10283 for (ipif = ill->ill_ipif; ipif != NULL;
10284 ipif = ipif->ipif_next) {
10285 if (!IPIF_IS_CONDEMNED(ipif) &&
10286 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10287 ilm->ilm_ifaddr != INADDR_ANY)
10288 break;
10289 }
10290 if (ipif != NULL) {
10291 ipif_get_name(ipif,
10292 ipm.ipGroupMemberIfIndex.o_bytes,
10293 OCTET_LENGTH);
10294 } else {
10295 ill_get_name(ill,
10296 ipm.ipGroupMemberIfIndex.o_bytes,
10297 OCTET_LENGTH);
10298 }
10299 ipm.ipGroupMemberIfIndex.o_length =
10300 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
10301
10302 ipm.ipGroupMemberAddress = ilm->ilm_addr;
10303 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
10304 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
10305 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10306 (char *)&ipm, (int)sizeof (ipm))) {
10307 ip1dbg(("ip_snmp_get_mib2_ip_group: "
10308 "failed to allocate %u bytes\n",
10309 (uint_t)sizeof (ipm)));
10310 }
10311 }
10312 rw_exit(&ill->ill_mcast_lock);
10313 ill_refrele(ill);
10314 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10315 }
10316 rw_exit(&ipst->ips_ill_g_lock);
10317 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10318 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10319 (int)optp->level, (int)optp->name, (int)optp->len));
10320 qreply(q, mpctl);
10321 return (mp2ctl);
10322 }
10323
10324 /* IPv6 multicast group membership. */
10325 static mblk_t *
ip_snmp_get_mib2_ip6_group_mem(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10326 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10327 {
10328 struct opthdr *optp;
10329 mblk_t *mp2ctl;
10330 ill_t *ill;
10331 ilm_t *ilm;
10332 ipv6_member_t ipm6;
10333 mblk_t *mp_tail = NULL;
10334 ill_walk_context_t ctx;
10335 zoneid_t zoneid;
10336
10337 /*
10338 * make a copy of the original message
10339 */
10340 mp2ctl = copymsg(mpctl);
10341 zoneid = Q_TO_CONN(q)->conn_zoneid;
10342
10343 /* ip6GroupMember table */
10344 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10345 optp->level = MIB2_IP6;
10346 optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10347
10348 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10349 ill = ILL_START_WALK_V6(&ctx, ipst);
10350 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10351 /* Make sure the ill isn't going away. */
10352 if (!ill_check_and_refhold(ill))
10353 continue;
10354 rw_exit(&ipst->ips_ill_g_lock);
10355 /*
10356 * Normally we don't have any members on under IPMP interfaces.
10357 * We report them as a debugging aid.
10358 */
10359 rw_enter(&ill->ill_mcast_lock, RW_READER);
10360 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10361 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10362 if (ilm->ilm_zoneid != zoneid &&
10363 ilm->ilm_zoneid != ALL_ZONES)
10364 continue; /* not this zone */
10365 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10366 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10367 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10368 if (!snmp_append_data2(mpctl->b_cont,
10369 &mp_tail,
10370 (char *)&ipm6, (int)sizeof (ipm6))) {
10371 ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10372 "failed to allocate %u bytes\n",
10373 (uint_t)sizeof (ipm6)));
10374 }
10375 }
10376 rw_exit(&ill->ill_mcast_lock);
10377 ill_refrele(ill);
10378 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10379 }
10380 rw_exit(&ipst->ips_ill_g_lock);
10381
10382 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10383 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10384 (int)optp->level, (int)optp->name, (int)optp->len));
10385 qreply(q, mpctl);
10386 return (mp2ctl);
10387 }
10388
10389 /* IP multicast filtered sources */
10390 static mblk_t *
ip_snmp_get_mib2_ip_group_src(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10391 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10392 {
10393 struct opthdr *optp;
10394 mblk_t *mp2ctl;
10395 ill_t *ill;
10396 ipif_t *ipif;
10397 ilm_t *ilm;
10398 ip_grpsrc_t ips;
10399 mblk_t *mp_tail = NULL;
10400 ill_walk_context_t ctx;
10401 zoneid_t zoneid;
10402 int i;
10403 slist_t *sl;
10404
10405 /*
10406 * make a copy of the original message
10407 */
10408 mp2ctl = copymsg(mpctl);
10409 zoneid = Q_TO_CONN(q)->conn_zoneid;
10410
10411 /* ipGroupSource table */
10412 optp = (struct opthdr *)&mpctl->b_rptr[
10413 sizeof (struct T_optmgmt_ack)];
10414 optp->level = MIB2_IP;
10415 optp->name = EXPER_IP_GROUP_SOURCES;
10416
10417 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10418 ill = ILL_START_WALK_V4(&ctx, ipst);
10419 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10420 /* Make sure the ill isn't going away. */
10421 if (!ill_check_and_refhold(ill))
10422 continue;
10423 rw_exit(&ipst->ips_ill_g_lock);
10424 rw_enter(&ill->ill_mcast_lock, RW_READER);
10425 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10426 sl = ilm->ilm_filter;
10427 if (ilm->ilm_zoneid != zoneid &&
10428 ilm->ilm_zoneid != ALL_ZONES)
10429 continue;
10430 if (SLIST_IS_EMPTY(sl))
10431 continue;
10432
10433 /* Is there an ipif for ilm_ifaddr? */
10434 for (ipif = ill->ill_ipif; ipif != NULL;
10435 ipif = ipif->ipif_next) {
10436 if (!IPIF_IS_CONDEMNED(ipif) &&
10437 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10438 ilm->ilm_ifaddr != INADDR_ANY)
10439 break;
10440 }
10441 if (ipif != NULL) {
10442 ipif_get_name(ipif,
10443 ips.ipGroupSourceIfIndex.o_bytes,
10444 OCTET_LENGTH);
10445 } else {
10446 ill_get_name(ill,
10447 ips.ipGroupSourceIfIndex.o_bytes,
10448 OCTET_LENGTH);
10449 }
10450 ips.ipGroupSourceIfIndex.o_length =
10451 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10452
10453 ips.ipGroupSourceGroup = ilm->ilm_addr;
10454 for (i = 0; i < sl->sl_numsrc; i++) {
10455 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10456 continue;
10457 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10458 ips.ipGroupSourceAddress);
10459 if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10460 (char *)&ips, (int)sizeof (ips)) == 0) {
10461 ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10462 " failed to allocate %u bytes\n",
10463 (uint_t)sizeof (ips)));
10464 }
10465 }
10466 }
10467 rw_exit(&ill->ill_mcast_lock);
10468 ill_refrele(ill);
10469 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10470 }
10471 rw_exit(&ipst->ips_ill_g_lock);
10472 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10473 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10474 (int)optp->level, (int)optp->name, (int)optp->len));
10475 qreply(q, mpctl);
10476 return (mp2ctl);
10477 }
10478
10479 /* IPv6 multicast filtered sources. */
10480 static mblk_t *
ip_snmp_get_mib2_ip6_group_src(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10481 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10482 {
10483 struct opthdr *optp;
10484 mblk_t *mp2ctl;
10485 ill_t *ill;
10486 ilm_t *ilm;
10487 ipv6_grpsrc_t ips6;
10488 mblk_t *mp_tail = NULL;
10489 ill_walk_context_t ctx;
10490 zoneid_t zoneid;
10491 int i;
10492 slist_t *sl;
10493
10494 /*
10495 * make a copy of the original message
10496 */
10497 mp2ctl = copymsg(mpctl);
10498 zoneid = Q_TO_CONN(q)->conn_zoneid;
10499
10500 /* ip6GroupMember table */
10501 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10502 optp->level = MIB2_IP6;
10503 optp->name = EXPER_IP6_GROUP_SOURCES;
10504
10505 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10506 ill = ILL_START_WALK_V6(&ctx, ipst);
10507 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10508 /* Make sure the ill isn't going away. */
10509 if (!ill_check_and_refhold(ill))
10510 continue;
10511 rw_exit(&ipst->ips_ill_g_lock);
10512 /*
10513 * Normally we don't have any members on under IPMP interfaces.
10514 * We report them as a debugging aid.
10515 */
10516 rw_enter(&ill->ill_mcast_lock, RW_READER);
10517 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10518 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10519 sl = ilm->ilm_filter;
10520 if (ilm->ilm_zoneid != zoneid &&
10521 ilm->ilm_zoneid != ALL_ZONES)
10522 continue;
10523 if (SLIST_IS_EMPTY(sl))
10524 continue;
10525 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10526 for (i = 0; i < sl->sl_numsrc; i++) {
10527 ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10528 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10529 (char *)&ips6, (int)sizeof (ips6))) {
10530 ip1dbg(("ip_snmp_get_mib2_ip6_"
10531 "group_src: failed to allocate "
10532 "%u bytes\n",
10533 (uint_t)sizeof (ips6)));
10534 }
10535 }
10536 }
10537 rw_exit(&ill->ill_mcast_lock);
10538 ill_refrele(ill);
10539 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10540 }
10541 rw_exit(&ipst->ips_ill_g_lock);
10542
10543 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10544 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10545 (int)optp->level, (int)optp->name, (int)optp->len));
10546 qreply(q, mpctl);
10547 return (mp2ctl);
10548 }
10549
10550 /* Multicast routing virtual interface table. */
10551 static mblk_t *
ip_snmp_get_mib2_virt_multi(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10552 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10553 {
10554 struct opthdr *optp;
10555 mblk_t *mp2ctl;
10556
10557 /*
10558 * make a copy of the original message
10559 */
10560 mp2ctl = copymsg(mpctl);
10561
10562 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10563 optp->level = EXPER_DVMRP;
10564 optp->name = EXPER_DVMRP_VIF;
10565 if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10566 ip0dbg(("ip_mroute_vif: failed\n"));
10567 }
10568 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10569 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10570 (int)optp->level, (int)optp->name, (int)optp->len));
10571 qreply(q, mpctl);
10572 return (mp2ctl);
10573 }
10574
10575 /* Multicast routing table. */
10576 static mblk_t *
ip_snmp_get_mib2_multi_rtable(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10577 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10578 {
10579 struct opthdr *optp;
10580 mblk_t *mp2ctl;
10581
10582 /*
10583 * make a copy of the original message
10584 */
10585 mp2ctl = copymsg(mpctl);
10586
10587 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10588 optp->level = EXPER_DVMRP;
10589 optp->name = EXPER_DVMRP_MRT;
10590 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10591 ip0dbg(("ip_mroute_mrt: failed\n"));
10592 }
10593 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10594 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10595 (int)optp->level, (int)optp->name, (int)optp->len));
10596 qreply(q, mpctl);
10597 return (mp2ctl);
10598 }
10599
10600 /*
10601 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable
10602 * in one IRE walk.
10603 */
10604 static mblk_t *
ip_snmp_get_mib2_ip_route_media(queue_t * q,mblk_t * mpctl,int level,ip_stack_t * ipst)10605 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10606 ip_stack_t *ipst)
10607 {
10608 struct opthdr *optp;
10609 mblk_t *mp2ctl; /* Returned */
10610 mblk_t *mp3ctl; /* nettomedia */
10611 mblk_t *mp4ctl; /* routeattrs */
10612 iproutedata_t ird;
10613 zoneid_t zoneid;
10614
10615 /*
10616 * make copies of the original message
10617 * - mp2ctl is returned unchanged to the caller for its use
10618 * - mpctl is sent upstream as ipRouteEntryTable
10619 * - mp3ctl is sent upstream as ipNetToMediaEntryTable
10620 * - mp4ctl is sent upstream as ipRouteAttributeTable
10621 */
10622 mp2ctl = copymsg(mpctl);
10623 mp3ctl = copymsg(mpctl);
10624 mp4ctl = copymsg(mpctl);
10625 if (mp3ctl == NULL || mp4ctl == NULL) {
10626 freemsg(mp4ctl);
10627 freemsg(mp3ctl);
10628 freemsg(mp2ctl);
10629 freemsg(mpctl);
10630 return (NULL);
10631 }
10632
10633 bzero(&ird, sizeof (ird));
10634
10635 ird.ird_route.lp_head = mpctl->b_cont;
10636 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10637 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10638 /*
10639 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10640 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10641 * intended a temporary solution until a proper MIB API is provided
10642 * that provides complete filtering/caller-opt-in.
10643 */
10644 if (level == EXPER_IP_AND_ALL_IRES)
10645 ird.ird_flags |= IRD_REPORT_ALL;
10646
10647 zoneid = Q_TO_CONN(q)->conn_zoneid;
10648 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10649
10650 /* ipRouteEntryTable in mpctl */
10651 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10652 optp->level = MIB2_IP;
10653 optp->name = MIB2_IP_ROUTE;
10654 optp->len = msgdsize(ird.ird_route.lp_head);
10655 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10656 (int)optp->level, (int)optp->name, (int)optp->len));
10657 qreply(q, mpctl);
10658
10659 /* ipNetToMediaEntryTable in mp3ctl */
10660 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10661
10662 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10663 optp->level = MIB2_IP;
10664 optp->name = MIB2_IP_MEDIA;
10665 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10666 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10667 (int)optp->level, (int)optp->name, (int)optp->len));
10668 qreply(q, mp3ctl);
10669
10670 /* ipRouteAttributeTable in mp4ctl */
10671 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10672 optp->level = MIB2_IP;
10673 optp->name = EXPER_IP_RTATTR;
10674 optp->len = msgdsize(ird.ird_attrs.lp_head);
10675 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10676 (int)optp->level, (int)optp->name, (int)optp->len));
10677 if (optp->len == 0)
10678 freemsg(mp4ctl);
10679 else
10680 qreply(q, mp4ctl);
10681
10682 return (mp2ctl);
10683 }
10684
10685 /*
10686 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and
10687 * ipv6NetToMediaEntryTable in an NDP walk.
10688 */
10689 static mblk_t *
ip_snmp_get_mib2_ip6_route_media(queue_t * q,mblk_t * mpctl,int level,ip_stack_t * ipst)10690 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10691 ip_stack_t *ipst)
10692 {
10693 struct opthdr *optp;
10694 mblk_t *mp2ctl; /* Returned */
10695 mblk_t *mp3ctl; /* nettomedia */
10696 mblk_t *mp4ctl; /* routeattrs */
10697 iproutedata_t ird;
10698 zoneid_t zoneid;
10699
10700 /*
10701 * make copies of the original message
10702 * - mp2ctl is returned unchanged to the caller for its use
10703 * - mpctl is sent upstream as ipv6RouteEntryTable
10704 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10705 * - mp4ctl is sent upstream as ipv6RouteAttributeTable
10706 */
10707 mp2ctl = copymsg(mpctl);
10708 mp3ctl = copymsg(mpctl);
10709 mp4ctl = copymsg(mpctl);
10710 if (mp3ctl == NULL || mp4ctl == NULL) {
10711 freemsg(mp4ctl);
10712 freemsg(mp3ctl);
10713 freemsg(mp2ctl);
10714 freemsg(mpctl);
10715 return (NULL);
10716 }
10717
10718 bzero(&ird, sizeof (ird));
10719
10720 ird.ird_route.lp_head = mpctl->b_cont;
10721 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10722 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10723 /*
10724 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10725 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10726 * intended a temporary solution until a proper MIB API is provided
10727 * that provides complete filtering/caller-opt-in.
10728 */
10729 if (level == EXPER_IP_AND_ALL_IRES)
10730 ird.ird_flags |= IRD_REPORT_ALL;
10731
10732 zoneid = Q_TO_CONN(q)->conn_zoneid;
10733 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10734
10735 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10736 optp->level = MIB2_IP6;
10737 optp->name = MIB2_IP6_ROUTE;
10738 optp->len = msgdsize(ird.ird_route.lp_head);
10739 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10740 (int)optp->level, (int)optp->name, (int)optp->len));
10741 qreply(q, mpctl);
10742
10743 /* ipv6NetToMediaEntryTable in mp3ctl */
10744 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10745
10746 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10747 optp->level = MIB2_IP6;
10748 optp->name = MIB2_IP6_MEDIA;
10749 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10750 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10751 (int)optp->level, (int)optp->name, (int)optp->len));
10752 qreply(q, mp3ctl);
10753
10754 /* ipv6RouteAttributeTable in mp4ctl */
10755 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10756 optp->level = MIB2_IP6;
10757 optp->name = EXPER_IP_RTATTR;
10758 optp->len = msgdsize(ird.ird_attrs.lp_head);
10759 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10760 (int)optp->level, (int)optp->name, (int)optp->len));
10761 if (optp->len == 0)
10762 freemsg(mp4ctl);
10763 else
10764 qreply(q, mp4ctl);
10765
10766 return (mp2ctl);
10767 }
10768
10769 /*
10770 * IPv6 mib: One per ill
10771 */
10772 static mblk_t *
ip_snmp_get_mib2_ip6(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst,boolean_t legacy_req)10773 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10774 boolean_t legacy_req)
10775 {
10776 struct opthdr *optp;
10777 mblk_t *mp2ctl;
10778 ill_t *ill;
10779 ill_walk_context_t ctx;
10780 mblk_t *mp_tail = NULL;
10781 mib2_ipv6AddrEntry_t mae6;
10782 mib2_ipIfStatsEntry_t *ise;
10783 size_t ise_size, iae_size;
10784
10785 /*
10786 * Make a copy of the original message
10787 */
10788 mp2ctl = copymsg(mpctl);
10789
10790 /* fixed length IPv6 structure ... */
10791
10792 if (legacy_req) {
10793 ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10794 mib2_ipIfStatsEntry_t);
10795 iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10796 } else {
10797 ise_size = sizeof (mib2_ipIfStatsEntry_t);
10798 iae_size = sizeof (mib2_ipv6AddrEntry_t);
10799 }
10800
10801 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10802 optp->level = MIB2_IP6;
10803 optp->name = 0;
10804 /* Include "unknown interface" ip6_mib */
10805 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10806 ipst->ips_ip6_mib.ipIfStatsIfIndex =
10807 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10808 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10809 ipst->ips_ipv6_forwarding ? 1 : 2);
10810 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10811 ipst->ips_ipv6_def_hops);
10812 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10813 sizeof (mib2_ipIfStatsEntry_t));
10814 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10815 sizeof (mib2_ipv6AddrEntry_t));
10816 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10817 sizeof (mib2_ipv6RouteEntry_t));
10818 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10819 sizeof (mib2_ipv6NetToMediaEntry_t));
10820 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10821 sizeof (ipv6_member_t));
10822 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10823 sizeof (ipv6_grpsrc_t));
10824
10825 /*
10826 * Synchronize 64- and 32-bit counters
10827 */
10828 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10829 ipIfStatsHCInReceives);
10830 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10831 ipIfStatsHCInDelivers);
10832 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10833 ipIfStatsHCOutRequests);
10834 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10835 ipIfStatsHCOutForwDatagrams);
10836 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10837 ipIfStatsHCOutMcastPkts);
10838 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10839 ipIfStatsHCInMcastPkts);
10840
10841 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10842 (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10843 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10844 (uint_t)ise_size));
10845 } else if (legacy_req) {
10846 /* Adjust the EntrySize fields for legacy requests. */
10847 ise =
10848 (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10849 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10850 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10851 }
10852
10853 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10854 ill = ILL_START_WALK_V6(&ctx, ipst);
10855 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10856 ill->ill_ip_mib->ipIfStatsIfIndex =
10857 ill->ill_phyint->phyint_ifindex;
10858 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10859 ipst->ips_ipv6_forwarding ? 1 : 2);
10860 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10861 ill->ill_max_hops);
10862
10863 /*
10864 * Synchronize 64- and 32-bit counters
10865 */
10866 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10867 ipIfStatsHCInReceives);
10868 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10869 ipIfStatsHCInDelivers);
10870 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10871 ipIfStatsHCOutRequests);
10872 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10873 ipIfStatsHCOutForwDatagrams);
10874 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10875 ipIfStatsHCOutMcastPkts);
10876 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10877 ipIfStatsHCInMcastPkts);
10878
10879 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10880 (char *)ill->ill_ip_mib, (int)ise_size)) {
10881 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10882 "%u bytes\n", (uint_t)ise_size));
10883 } else if (legacy_req) {
10884 /* Adjust the EntrySize fields for legacy requests. */
10885 ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10886 (int)ise_size);
10887 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10888 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10889 }
10890 }
10891 rw_exit(&ipst->ips_ill_g_lock);
10892
10893 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10894 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10895 (int)optp->level, (int)optp->name, (int)optp->len));
10896 qreply(q, mpctl);
10897 return (mp2ctl);
10898 }
10899
10900 /*
10901 * ICMPv6 mib: One per ill
10902 */
10903 static mblk_t *
ip_snmp_get_mib2_icmp6(queue_t * q,mblk_t * mpctl,ip_stack_t * ipst)10904 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10905 {
10906 struct opthdr *optp;
10907 mblk_t *mp2ctl;
10908 ill_t *ill;
10909 ill_walk_context_t ctx;
10910 mblk_t *mp_tail = NULL;
10911 /*
10912 * Make a copy of the original message
10913 */
10914 mp2ctl = copymsg(mpctl);
10915
10916 /* fixed length ICMPv6 structure ... */
10917
10918 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10919 optp->level = MIB2_ICMP6;
10920 optp->name = 0;
10921 /* Include "unknown interface" icmp6_mib */
10922 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10923 MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10924 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10925 sizeof (mib2_ipv6IfIcmpEntry_t);
10926 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10927 (char *)&ipst->ips_icmp6_mib,
10928 (int)sizeof (ipst->ips_icmp6_mib))) {
10929 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10930 (uint_t)sizeof (ipst->ips_icmp6_mib)));
10931 }
10932
10933 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10934 ill = ILL_START_WALK_V6(&ctx, ipst);
10935 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10936 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10937 ill->ill_phyint->phyint_ifindex;
10938 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10939 (char *)ill->ill_icmp6_mib,
10940 (int)sizeof (*ill->ill_icmp6_mib))) {
10941 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10942 "%u bytes\n",
10943 (uint_t)sizeof (*ill->ill_icmp6_mib)));
10944 }
10945 }
10946 rw_exit(&ipst->ips_ill_g_lock);
10947
10948 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10949 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10950 (int)optp->level, (int)optp->name, (int)optp->len));
10951 qreply(q, mpctl);
10952 return (mp2ctl);
10953 }
10954
10955 /*
10956 * ire_walk routine to create both ipRouteEntryTable and
10957 * ipRouteAttributeTable in one IRE walk
10958 */
10959 static void
ip_snmp_get2_v4(ire_t * ire,iproutedata_t * ird)10960 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10961 {
10962 ill_t *ill;
10963 mib2_ipRouteEntry_t *re;
10964 mib2_ipAttributeEntry_t iaes;
10965 tsol_ire_gw_secattr_t *attrp;
10966 tsol_gc_t *gc = NULL;
10967 tsol_gcgrp_t *gcgrp = NULL;
10968 ip_stack_t *ipst = ire->ire_ipst;
10969
10970 ASSERT(ire->ire_ipversion == IPV4_VERSION);
10971
10972 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10973 if (ire->ire_testhidden)
10974 return;
10975 if (ire->ire_type & IRE_IF_CLONE)
10976 return;
10977 }
10978
10979 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10980 return;
10981
10982 if ((attrp = ire->ire_gw_secattr) != NULL) {
10983 mutex_enter(&attrp->igsa_lock);
10984 if ((gc = attrp->igsa_gc) != NULL) {
10985 gcgrp = gc->gc_grp;
10986 ASSERT(gcgrp != NULL);
10987 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
10988 }
10989 mutex_exit(&attrp->igsa_lock);
10990 }
10991 /*
10992 * Return all IRE types for route table... let caller pick and choose
10993 */
10994 re->ipRouteDest = ire->ire_addr;
10995 ill = ire->ire_ill;
10996 re->ipRouteIfIndex.o_length = 0;
10997 if (ill != NULL) {
10998 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10999 re->ipRouteIfIndex.o_length =
11000 mi_strlen(re->ipRouteIfIndex.o_bytes);
11001 }
11002 re->ipRouteMetric1 = -1;
11003 re->ipRouteMetric2 = -1;
11004 re->ipRouteMetric3 = -1;
11005 re->ipRouteMetric4 = -1;
11006
11007 re->ipRouteNextHop = ire->ire_gateway_addr;
11008 /* indirect(4), direct(3), or invalid(2) */
11009 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11010 re->ipRouteType = 2;
11011 else if (ire->ire_type & IRE_ONLINK)
11012 re->ipRouteType = 3;
11013 else
11014 re->ipRouteType = 4;
11015
11016 re->ipRouteProto = -1;
11017 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
11018 re->ipRouteMask = ire->ire_mask;
11019 re->ipRouteMetric5 = -1;
11020 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11021 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
11022 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11023
11024 re->ipRouteInfo.re_frag_flag = 0;
11025 re->ipRouteInfo.re_rtt = 0;
11026 re->ipRouteInfo.re_src_addr = 0;
11027 re->ipRouteInfo.re_ref = ire->ire_refcnt;
11028 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count;
11029 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
11030 re->ipRouteInfo.re_flags = ire->ire_flags;
11031
11032 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11033 if (ire->ire_type & IRE_INTERFACE) {
11034 ire_t *child;
11035
11036 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11037 child = ire->ire_dep_children;
11038 while (child != NULL) {
11039 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
11040 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11041 child = child->ire_dep_sib_next;
11042 }
11043 rw_exit(&ipst->ips_ire_dep_lock);
11044 }
11045
11046 if (ire->ire_flags & RTF_DYNAMIC) {
11047 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11048 } else {
11049 re->ipRouteInfo.re_ire_type = ire->ire_type;
11050 }
11051
11052 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11053 (char *)re, (int)sizeof (*re))) {
11054 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
11055 (uint_t)sizeof (*re)));
11056 }
11057
11058 if (gc != NULL) {
11059 iaes.iae_routeidx = ird->ird_idx;
11060 iaes.iae_doi = gc->gc_db->gcdb_doi;
11061 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11062
11063 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11064 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11065 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u "
11066 "bytes\n", (uint_t)sizeof (iaes)));
11067 }
11068 }
11069
11070 /* bump route index for next pass */
11071 ird->ird_idx++;
11072
11073 kmem_free(re, sizeof (*re));
11074 if (gcgrp != NULL)
11075 rw_exit(&gcgrp->gcgrp_rwlock);
11076 }
11077
11078 /*
11079 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
11080 */
11081 static void
ip_snmp_get2_v6_route(ire_t * ire,iproutedata_t * ird)11082 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
11083 {
11084 ill_t *ill;
11085 mib2_ipv6RouteEntry_t *re;
11086 mib2_ipAttributeEntry_t iaes;
11087 tsol_ire_gw_secattr_t *attrp;
11088 tsol_gc_t *gc = NULL;
11089 tsol_gcgrp_t *gcgrp = NULL;
11090 ip_stack_t *ipst = ire->ire_ipst;
11091
11092 ASSERT(ire->ire_ipversion == IPV6_VERSION);
11093
11094 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
11095 if (ire->ire_testhidden)
11096 return;
11097 if (ire->ire_type & IRE_IF_CLONE)
11098 return;
11099 }
11100
11101 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
11102 return;
11103
11104 if ((attrp = ire->ire_gw_secattr) != NULL) {
11105 mutex_enter(&attrp->igsa_lock);
11106 if ((gc = attrp->igsa_gc) != NULL) {
11107 gcgrp = gc->gc_grp;
11108 ASSERT(gcgrp != NULL);
11109 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
11110 }
11111 mutex_exit(&attrp->igsa_lock);
11112 }
11113 /*
11114 * Return all IRE types for route table... let caller pick and choose
11115 */
11116 re->ipv6RouteDest = ire->ire_addr_v6;
11117 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
11118 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
11119 re->ipv6RouteIfIndex.o_length = 0;
11120 ill = ire->ire_ill;
11121 if (ill != NULL) {
11122 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
11123 re->ipv6RouteIfIndex.o_length =
11124 mi_strlen(re->ipv6RouteIfIndex.o_bytes);
11125 }
11126
11127 ASSERT(!(ire->ire_type & IRE_BROADCAST));
11128
11129 mutex_enter(&ire->ire_lock);
11130 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
11131 mutex_exit(&ire->ire_lock);
11132
11133 /* remote(4), local(3), or discard(2) */
11134 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11135 re->ipv6RouteType = 2;
11136 else if (ire->ire_type & IRE_ONLINK)
11137 re->ipv6RouteType = 3;
11138 else
11139 re->ipv6RouteType = 4;
11140
11141 re->ipv6RouteProtocol = -1;
11142 re->ipv6RoutePolicy = 0;
11143 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time;
11144 re->ipv6RouteNextHopRDI = 0;
11145 re->ipv6RouteWeight = 0;
11146 re->ipv6RouteMetric = 0;
11147 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11148 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
11149 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11150
11151 re->ipv6RouteInfo.re_frag_flag = 0;
11152 re->ipv6RouteInfo.re_rtt = 0;
11153 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros;
11154 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count;
11155 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
11156 re->ipv6RouteInfo.re_ref = ire->ire_refcnt;
11157 re->ipv6RouteInfo.re_flags = ire->ire_flags;
11158
11159 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11160 if (ire->ire_type & IRE_INTERFACE) {
11161 ire_t *child;
11162
11163 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11164 child = ire->ire_dep_children;
11165 while (child != NULL) {
11166 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
11167 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11168 child = child->ire_dep_sib_next;
11169 }
11170 rw_exit(&ipst->ips_ire_dep_lock);
11171 }
11172 if (ire->ire_flags & RTF_DYNAMIC) {
11173 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11174 } else {
11175 re->ipv6RouteInfo.re_ire_type = ire->ire_type;
11176 }
11177
11178 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11179 (char *)re, (int)sizeof (*re))) {
11180 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
11181 (uint_t)sizeof (*re)));
11182 }
11183
11184 if (gc != NULL) {
11185 iaes.iae_routeidx = ird->ird_idx;
11186 iaes.iae_doi = gc->gc_db->gcdb_doi;
11187 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11188
11189 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11190 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11191 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u "
11192 "bytes\n", (uint_t)sizeof (iaes)));
11193 }
11194 }
11195
11196 /* bump route index for next pass */
11197 ird->ird_idx++;
11198
11199 kmem_free(re, sizeof (*re));
11200 if (gcgrp != NULL)
11201 rw_exit(&gcgrp->gcgrp_rwlock);
11202 }
11203
11204 /*
11205 * ncec_walk routine to create ipv6NetToMediaEntryTable
11206 */
11207 static void
ip_snmp_get2_v6_media(ncec_t * ncec,void * ptr)11208 ip_snmp_get2_v6_media(ncec_t *ncec, void *ptr)
11209 {
11210 iproutedata_t *ird = ptr;
11211 ill_t *ill;
11212 mib2_ipv6NetToMediaEntry_t ntme;
11213
11214 ill = ncec->ncec_ill;
11215 /* skip arpce entries, and loopback ncec entries */
11216 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
11217 return;
11218 /*
11219 * Neighbor cache entry attached to IRE with on-link
11220 * destination.
11221 * We report all IPMP groups on ncec_ill which is normally the upper.
11222 */
11223 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
11224 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
11225 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
11226 if (ncec->ncec_lladdr != NULL) {
11227 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
11228 ntme.ipv6NetToMediaPhysAddress.o_length);
11229 }
11230 /*
11231 * Note: Returns ND_* states. Should be:
11232 * reachable(1), stale(2), delay(3), probe(4),
11233 * invalid(5), unknown(6)
11234 */
11235 ntme.ipv6NetToMediaState = ncec->ncec_state;
11236 ntme.ipv6NetToMediaLastUpdated = 0;
11237
11238 /* other(1), dynamic(2), static(3), local(4) */
11239 if (NCE_MYADDR(ncec)) {
11240 ntme.ipv6NetToMediaType = 4;
11241 } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
11242 ntme.ipv6NetToMediaType = 1; /* proxy */
11243 } else if (ncec->ncec_flags & NCE_F_STATIC) {
11244 ntme.ipv6NetToMediaType = 3;
11245 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
11246 ntme.ipv6NetToMediaType = 1;
11247 } else {
11248 ntme.ipv6NetToMediaType = 2;
11249 }
11250
11251 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11252 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11253 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
11254 (uint_t)sizeof (ntme)));
11255 }
11256 }
11257
11258 int
nce2ace(ncec_t * ncec)11259 nce2ace(ncec_t *ncec)
11260 {
11261 int flags = 0;
11262
11263 if (NCE_ISREACHABLE(ncec))
11264 flags |= ACE_F_RESOLVED;
11265 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11266 flags |= ACE_F_AUTHORITY;
11267 if (ncec->ncec_flags & NCE_F_PUBLISH)
11268 flags |= ACE_F_PUBLISH;
11269 if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
11270 flags |= ACE_F_PERMANENT;
11271 if (NCE_MYADDR(ncec))
11272 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
11273 if (ncec->ncec_flags & NCE_F_UNVERIFIED)
11274 flags |= ACE_F_UNVERIFIED;
11275 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11276 flags |= ACE_F_AUTHORITY;
11277 if (ncec->ncec_flags & NCE_F_DELAYED)
11278 flags |= ACE_F_DELAYED;
11279 return (flags);
11280 }
11281
11282 /*
11283 * ncec_walk routine to create ipNetToMediaEntryTable
11284 */
11285 static void
ip_snmp_get2_v4_media(ncec_t * ncec,void * ptr)11286 ip_snmp_get2_v4_media(ncec_t *ncec, void *ptr)
11287 {
11288 iproutedata_t *ird = ptr;
11289 ill_t *ill;
11290 mib2_ipNetToMediaEntry_t ntme;
11291 const char *name = "unknown";
11292 ipaddr_t ncec_addr;
11293
11294 ill = ncec->ncec_ill;
11295 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
11296 ill->ill_net_type == IRE_LOOPBACK)
11297 return;
11298
11299 /* We report all IPMP groups on ncec_ill which is normally the upper. */
11300 name = ill->ill_name;
11301 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
11302 if (NCE_MYADDR(ncec)) {
11303 ntme.ipNetToMediaType = 4;
11304 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
11305 ntme.ipNetToMediaType = 1;
11306 } else {
11307 ntme.ipNetToMediaType = 3;
11308 }
11309 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
11310 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
11311 ntme.ipNetToMediaIfIndex.o_length);
11312
11313 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
11314 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
11315
11316 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
11317 ncec_addr = INADDR_BROADCAST;
11318 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
11319 sizeof (ncec_addr));
11320 /*
11321 * map all the flags to the ACE counterpart.
11322 */
11323 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
11324
11325 ntme.ipNetToMediaPhysAddress.o_length =
11326 MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
11327
11328 if (!NCE_ISREACHABLE(ncec))
11329 ntme.ipNetToMediaPhysAddress.o_length = 0;
11330 else {
11331 if (ncec->ncec_lladdr != NULL) {
11332 bcopy(ncec->ncec_lladdr,
11333 ntme.ipNetToMediaPhysAddress.o_bytes,
11334 ntme.ipNetToMediaPhysAddress.o_length);
11335 }
11336 }
11337
11338 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11339 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11340 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
11341 (uint_t)sizeof (ntme)));
11342 }
11343 }
11344
11345 /*
11346 * return (0) if invalid set request, 1 otherwise, including non-tcp requests
11347 */
11348 /* ARGSUSED */
11349 int
ip_snmp_set(queue_t * q,int level,int name,uchar_t * ptr,int len)11350 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
11351 {
11352 switch (level) {
11353 case MIB2_IP:
11354 case MIB2_ICMP:
11355 switch (name) {
11356 default:
11357 break;
11358 }
11359 return (1);
11360 default:
11361 return (1);
11362 }
11363 }
11364
11365 /*
11366 * When there exists both a 64- and 32-bit counter of a particular type
11367 * (i.e., InReceives), only the 64-bit counters are added.
11368 */
11369 void
ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t * o1,mib2_ipIfStatsEntry_t * o2)11370 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
11371 {
11372 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
11373 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
11374 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
11375 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
11376 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
11377 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
11378 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
11379 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
11380 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
11381 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
11382 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
11383 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
11384 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
11385 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
11386 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
11387 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
11388 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
11389 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
11390 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
11391 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
11392 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
11393 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
11394 o2->ipIfStatsInWrongIPVersion);
11395 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
11396 o2->ipIfStatsInWrongIPVersion);
11397 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
11398 o2->ipIfStatsOutSwitchIPVersion);
11399 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
11400 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
11401 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
11402 o2->ipIfStatsHCInForwDatagrams);
11403 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
11404 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
11405 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
11406 o2->ipIfStatsHCOutForwDatagrams);
11407 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
11408 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
11409 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
11410 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
11411 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
11412 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
11413 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11414 o2->ipIfStatsHCOutMcastOctets);
11415 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11416 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11417 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11418 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11419 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11420 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11421 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11422 }
11423
11424 void
ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t * o1,mib2_ipv6IfIcmpEntry_t * o2)11425 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11426 {
11427 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11428 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11429 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11430 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11431 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11432 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11433 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11434 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11435 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11436 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11437 o2->ipv6IfIcmpInRouterSolicits);
11438 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11439 o2->ipv6IfIcmpInRouterAdvertisements);
11440 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11441 o2->ipv6IfIcmpInNeighborSolicits);
11442 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11443 o2->ipv6IfIcmpInNeighborAdvertisements);
11444 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11445 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11446 o2->ipv6IfIcmpInGroupMembQueries);
11447 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11448 o2->ipv6IfIcmpInGroupMembResponses);
11449 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11450 o2->ipv6IfIcmpInGroupMembReductions);
11451 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11452 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11453 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11454 o2->ipv6IfIcmpOutDestUnreachs);
11455 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11456 o2->ipv6IfIcmpOutAdminProhibs);
11457 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11458 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11459 o2->ipv6IfIcmpOutParmProblems);
11460 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11461 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11462 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11463 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11464 o2->ipv6IfIcmpOutRouterSolicits);
11465 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11466 o2->ipv6IfIcmpOutRouterAdvertisements);
11467 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11468 o2->ipv6IfIcmpOutNeighborSolicits);
11469 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11470 o2->ipv6IfIcmpOutNeighborAdvertisements);
11471 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11472 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11473 o2->ipv6IfIcmpOutGroupMembQueries);
11474 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11475 o2->ipv6IfIcmpOutGroupMembResponses);
11476 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11477 o2->ipv6IfIcmpOutGroupMembReductions);
11478 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11479 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11480 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11481 o2->ipv6IfIcmpInBadNeighborAdvertisements);
11482 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11483 o2->ipv6IfIcmpInBadNeighborSolicitations);
11484 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11485 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11486 o2->ipv6IfIcmpInGroupMembTotal);
11487 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11488 o2->ipv6IfIcmpInGroupMembBadQueries);
11489 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11490 o2->ipv6IfIcmpInGroupMembBadReports);
11491 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11492 o2->ipv6IfIcmpInGroupMembOurReports);
11493 }
11494
11495 /*
11496 * Called before the options are updated to check if this packet will
11497 * be source routed from here.
11498 * This routine assumes that the options are well formed i.e. that they
11499 * have already been checked.
11500 */
11501 boolean_t
ip_source_routed(ipha_t * ipha,ip_stack_t * ipst)11502 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11503 {
11504 ipoptp_t opts;
11505 uchar_t *opt;
11506 uint8_t optval;
11507 uint8_t optlen;
11508 ipaddr_t dst;
11509
11510 if (IS_SIMPLE_IPH(ipha)) {
11511 ip2dbg(("not source routed\n"));
11512 return (B_FALSE);
11513 }
11514 dst = ipha->ipha_dst;
11515 for (optval = ipoptp_first(&opts, ipha);
11516 optval != IPOPT_EOL;
11517 optval = ipoptp_next(&opts)) {
11518 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11519 opt = opts.ipoptp_cur;
11520 optlen = opts.ipoptp_len;
11521 ip2dbg(("ip_source_routed: opt %d, len %d\n",
11522 optval, optlen));
11523 switch (optval) {
11524 uint32_t off;
11525 case IPOPT_SSRR:
11526 case IPOPT_LSRR:
11527 /*
11528 * If dst is one of our addresses and there are some
11529 * entries left in the source route return (true).
11530 */
11531 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11532 ip2dbg(("ip_source_routed: not next"
11533 " source route 0x%x\n",
11534 ntohl(dst)));
11535 return (B_FALSE);
11536 }
11537 off = opt[IPOPT_OFFSET];
11538 off--;
11539 if (optlen < IP_ADDR_LEN ||
11540 off > optlen - IP_ADDR_LEN) {
11541 /* End of source route */
11542 ip1dbg(("ip_source_routed: end of SR\n"));
11543 return (B_FALSE);
11544 }
11545 return (B_TRUE);
11546 }
11547 }
11548 ip2dbg(("not source routed\n"));
11549 return (B_FALSE);
11550 }
11551
11552 /*
11553 * ip_unbind is called by the transports to remove a conn from
11554 * the fanout table.
11555 */
11556 void
ip_unbind(conn_t * connp)11557 ip_unbind(conn_t *connp)
11558 {
11559
11560 ASSERT(!MUTEX_HELD(&connp->conn_lock));
11561
11562 if (is_system_labeled() && connp->conn_anon_port) {
11563 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
11564 connp->conn_mlp_type, connp->conn_proto,
11565 ntohs(connp->conn_lport), B_FALSE);
11566 connp->conn_anon_port = 0;
11567 }
11568 connp->conn_mlp_type = mlptSingle;
11569
11570 ipcl_hash_remove(connp);
11571 }
11572
11573 /*
11574 * Used for deciding the MSS size for the upper layer. Thus
11575 * we need to check the outbound policy values in the conn.
11576 */
11577 int
conn_ipsec_length(conn_t * connp)11578 conn_ipsec_length(conn_t *connp)
11579 {
11580 ipsec_latch_t *ipl;
11581
11582 ipl = connp->conn_latch;
11583 if (ipl == NULL)
11584 return (0);
11585
11586 if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11587 return (0);
11588
11589 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11590 }
11591
11592 /*
11593 * Returns an estimate of the IPsec headers size. This is used if
11594 * we don't want to call into IPsec to get the exact size.
11595 */
11596 int
ipsec_out_extra_length(ip_xmit_attr_t * ixa)11597 ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11598 {
11599 ipsec_action_t *a;
11600
11601 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11602 return (0);
11603
11604 a = ixa->ixa_ipsec_action;
11605 if (a == NULL) {
11606 ASSERT(ixa->ixa_ipsec_policy != NULL);
11607 a = ixa->ixa_ipsec_policy->ipsp_act;
11608 }
11609 ASSERT(a != NULL);
11610
11611 return (a->ipa_ovhd);
11612 }
11613
11614 /*
11615 * If there are any source route options, return the true final
11616 * destination. Otherwise, return the destination.
11617 */
11618 ipaddr_t
ip_get_dst(ipha_t * ipha)11619 ip_get_dst(ipha_t *ipha)
11620 {
11621 ipoptp_t opts;
11622 uchar_t *opt;
11623 uint8_t optval;
11624 uint8_t optlen;
11625 ipaddr_t dst;
11626 uint32_t off;
11627
11628 dst = ipha->ipha_dst;
11629
11630 if (IS_SIMPLE_IPH(ipha))
11631 return (dst);
11632
11633 for (optval = ipoptp_first(&opts, ipha);
11634 optval != IPOPT_EOL;
11635 optval = ipoptp_next(&opts)) {
11636 opt = opts.ipoptp_cur;
11637 optlen = opts.ipoptp_len;
11638 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11639 switch (optval) {
11640 case IPOPT_SSRR:
11641 case IPOPT_LSRR:
11642 off = opt[IPOPT_OFFSET];
11643 /*
11644 * If one of the conditions is true, it means
11645 * end of options and dst already has the right
11646 * value.
11647 */
11648 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11649 off = optlen - IP_ADDR_LEN;
11650 bcopy(&opt[off], &dst, IP_ADDR_LEN);
11651 }
11652 return (dst);
11653 default:
11654 break;
11655 }
11656 }
11657
11658 return (dst);
11659 }
11660
11661 /*
11662 * Outbound IP fragmentation routine.
11663 * Assumes the caller has checked whether or not fragmentation should
11664 * be allowed. Here we copy the DF bit from the header to all the generated
11665 * fragments.
11666 */
11667 int
ip_fragment_v4(mblk_t * mp_orig,nce_t * nce,iaflags_t ixaflags,uint_t pkt_len,uint32_t max_frag,uint32_t xmit_hint,zoneid_t szone,zoneid_t nolzid,pfirepostfrag_t postfragfn,uintptr_t * ixa_cookie)11668 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11669 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11670 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11671 {
11672 int i1;
11673 int hdr_len;
11674 mblk_t *hdr_mp;
11675 ipha_t *ipha;
11676 int ip_data_end;
11677 int len;
11678 mblk_t *mp = mp_orig;
11679 int offset;
11680 ill_t *ill = nce->nce_ill;
11681 ip_stack_t *ipst = ill->ill_ipst;
11682 mblk_t *carve_mp;
11683 uint32_t frag_flag;
11684 uint_t priority = mp->b_band;
11685 int error = 0;
11686
11687 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11688
11689 if (pkt_len != msgdsize(mp)) {
11690 ip0dbg(("Packet length mismatch: %d, %ld\n",
11691 pkt_len, msgdsize(mp)));
11692 freemsg(mp);
11693 return (EINVAL);
11694 }
11695
11696 if (max_frag == 0) {
11697 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11698 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11699 ip_drop_output("FragFails: zero max_frag", mp, ill);
11700 freemsg(mp);
11701 return (EINVAL);
11702 }
11703
11704 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11705 ipha = (ipha_t *)mp->b_rptr;
11706 ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11707 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11708
11709 /*
11710 * Establish the starting offset. May not be zero if we are fragging
11711 * a fragment that is being forwarded.
11712 */
11713 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11714
11715 /* TODO why is this test needed? */
11716 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11717 /* TODO: notify ulp somehow */
11718 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11719 ip_drop_output("FragFails: bad starting offset", mp, ill);
11720 freemsg(mp);
11721 return (EINVAL);
11722 }
11723
11724 hdr_len = IPH_HDR_LENGTH(ipha);
11725 ipha->ipha_hdr_checksum = 0;
11726
11727 /*
11728 * Establish the number of bytes maximum per frag, after putting
11729 * in the header.
11730 */
11731 len = (max_frag - hdr_len) & ~7;
11732
11733 /* Get a copy of the header for the trailing frags */
11734 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11735 mp);
11736 if (hdr_mp == NULL) {
11737 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11738 ip_drop_output("FragFails: no hdr_mp", mp, ill);
11739 freemsg(mp);
11740 return (ENOBUFS);
11741 }
11742
11743 /* Store the starting offset, with the MoreFrags flag. */
11744 i1 = offset | IPH_MF | frag_flag;
11745 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11746
11747 /* Establish the ending byte offset, based on the starting offset. */
11748 offset <<= 3;
11749 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11750
11751 /* Store the length of the first fragment in the IP header. */
11752 i1 = len + hdr_len;
11753 ASSERT(i1 <= IP_MAXPACKET);
11754 ipha->ipha_length = htons((uint16_t)i1);
11755
11756 /*
11757 * Compute the IP header checksum for the first frag. We have to
11758 * watch out that we stop at the end of the header.
11759 */
11760 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11761
11762 /*
11763 * Now carve off the first frag. Note that this will include the
11764 * original IP header.
11765 */
11766 if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11767 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11768 ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11769 freeb(hdr_mp);
11770 freemsg(mp_orig);
11771 return (ENOBUFS);
11772 }
11773
11774 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11775
11776 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11777 ixa_cookie);
11778 if (error != 0 && error != EWOULDBLOCK) {
11779 /* No point in sending the other fragments */
11780 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11781 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11782 freeb(hdr_mp);
11783 freemsg(mp_orig);
11784 return (error);
11785 }
11786
11787 /* No need to redo state machine in loop */
11788 ixaflags &= ~IXAF_REACH_CONF;
11789
11790 /* Advance the offset to the second frag starting point. */
11791 offset += len;
11792 /*
11793 * Update hdr_len from the copied header - there might be less options
11794 * in the later fragments.
11795 */
11796 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11797 /* Loop until done. */
11798 for (;;) {
11799 uint16_t offset_and_flags;
11800 uint16_t ip_len;
11801
11802 if (ip_data_end - offset > len) {
11803 /*
11804 * Carve off the appropriate amount from the original
11805 * datagram.
11806 */
11807 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11808 mp = NULL;
11809 break;
11810 }
11811 /*
11812 * More frags after this one. Get another copy
11813 * of the header.
11814 */
11815 if (carve_mp->b_datap->db_ref == 1 &&
11816 hdr_mp->b_wptr - hdr_mp->b_rptr <
11817 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11818 /* Inline IP header */
11819 carve_mp->b_rptr -= hdr_mp->b_wptr -
11820 hdr_mp->b_rptr;
11821 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11822 hdr_mp->b_wptr - hdr_mp->b_rptr);
11823 mp = carve_mp;
11824 } else {
11825 if (!(mp = copyb(hdr_mp))) {
11826 freemsg(carve_mp);
11827 break;
11828 }
11829 /* Get priority marking, if any. */
11830 mp->b_band = priority;
11831 mp->b_cont = carve_mp;
11832 }
11833 ipha = (ipha_t *)mp->b_rptr;
11834 offset_and_flags = IPH_MF;
11835 } else {
11836 /*
11837 * Last frag. Consume the header. Set len to
11838 * the length of this last piece.
11839 */
11840 len = ip_data_end - offset;
11841
11842 /*
11843 * Carve off the appropriate amount from the original
11844 * datagram.
11845 */
11846 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11847 mp = NULL;
11848 break;
11849 }
11850 if (carve_mp->b_datap->db_ref == 1 &&
11851 hdr_mp->b_wptr - hdr_mp->b_rptr <
11852 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11853 /* Inline IP header */
11854 carve_mp->b_rptr -= hdr_mp->b_wptr -
11855 hdr_mp->b_rptr;
11856 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11857 hdr_mp->b_wptr - hdr_mp->b_rptr);
11858 mp = carve_mp;
11859 freeb(hdr_mp);
11860 hdr_mp = mp;
11861 } else {
11862 mp = hdr_mp;
11863 /* Get priority marking, if any. */
11864 mp->b_band = priority;
11865 mp->b_cont = carve_mp;
11866 }
11867 ipha = (ipha_t *)mp->b_rptr;
11868 /* A frag of a frag might have IPH_MF non-zero */
11869 offset_and_flags =
11870 ntohs(ipha->ipha_fragment_offset_and_flags) &
11871 IPH_MF;
11872 }
11873 offset_and_flags |= (uint16_t)(offset >> 3);
11874 offset_and_flags |= (uint16_t)frag_flag;
11875 /* Store the offset and flags in the IP header. */
11876 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11877
11878 /* Store the length in the IP header. */
11879 ip_len = (uint16_t)(len + hdr_len);
11880 ipha->ipha_length = htons(ip_len);
11881
11882 /*
11883 * Set the IP header checksum. Note that mp is just
11884 * the header, so this is easy to pass to ip_csum.
11885 */
11886 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11887
11888 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11889
11890 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11891 nolzid, ixa_cookie);
11892 /* All done if we just consumed the hdr_mp. */
11893 if (mp == hdr_mp) {
11894 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11895 return (error);
11896 }
11897 if (error != 0 && error != EWOULDBLOCK) {
11898 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11899 mblk_t *, hdr_mp);
11900 /* No point in sending the other fragments */
11901 break;
11902 }
11903
11904 /* Otherwise, advance and loop. */
11905 offset += len;
11906 }
11907 /* Clean up following allocation failure. */
11908 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11909 ip_drop_output("FragFails: loop ended", NULL, ill);
11910 if (mp != hdr_mp)
11911 freeb(hdr_mp);
11912 if (mp != mp_orig)
11913 freemsg(mp_orig);
11914 return (error);
11915 }
11916
11917 /*
11918 * Copy the header plus those options which have the copy bit set
11919 */
11920 static mblk_t *
ip_fragment_copyhdr(uchar_t * rptr,int hdr_len,int offset,ip_stack_t * ipst,mblk_t * src)11921 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11922 mblk_t *src)
11923 {
11924 mblk_t *mp;
11925 uchar_t *up;
11926
11927 /*
11928 * Quick check if we need to look for options without the copy bit
11929 * set
11930 */
11931 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11932 if (!mp)
11933 return (mp);
11934 mp->b_rptr += ipst->ips_ip_wroff_extra;
11935 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11936 bcopy(rptr, mp->b_rptr, hdr_len);
11937 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11938 return (mp);
11939 }
11940 up = mp->b_rptr;
11941 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11942 up += IP_SIMPLE_HDR_LENGTH;
11943 rptr += IP_SIMPLE_HDR_LENGTH;
11944 hdr_len -= IP_SIMPLE_HDR_LENGTH;
11945 while (hdr_len > 0) {
11946 uint32_t optval;
11947 uint32_t optlen;
11948
11949 optval = *rptr;
11950 if (optval == IPOPT_EOL)
11951 break;
11952 if (optval == IPOPT_NOP)
11953 optlen = 1;
11954 else
11955 optlen = rptr[1];
11956 if (optval & IPOPT_COPY) {
11957 bcopy(rptr, up, optlen);
11958 up += optlen;
11959 }
11960 rptr += optlen;
11961 hdr_len -= optlen;
11962 }
11963 /*
11964 * Make sure that we drop an even number of words by filling
11965 * with EOL to the next word boundary.
11966 */
11967 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11968 hdr_len & 0x3; hdr_len++)
11969 *up++ = IPOPT_EOL;
11970 mp->b_wptr = up;
11971 /* Update header length */
11972 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11973 return (mp);
11974 }
11975
11976 /*
11977 * Update any source route, record route, or timestamp options when
11978 * sending a packet back to ourselves.
11979 * Check that we are at end of strict source route.
11980 * The options have been sanity checked by ip_output_options().
11981 */
11982 void
ip_output_local_options(ipha_t * ipha,ip_stack_t * ipst)11983 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11984 {
11985 ipoptp_t opts;
11986 uchar_t *opt;
11987 uint8_t optval;
11988 uint8_t optlen;
11989 ipaddr_t dst;
11990 uint32_t ts;
11991 timestruc_t now;
11992 uint32_t off = 0;
11993
11994 for (optval = ipoptp_first(&opts, ipha);
11995 optval != IPOPT_EOL;
11996 optval = ipoptp_next(&opts)) {
11997 opt = opts.ipoptp_cur;
11998 optlen = opts.ipoptp_len;
11999 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
12000 switch (optval) {
12001 case IPOPT_SSRR:
12002 case IPOPT_LSRR:
12003 off = opt[IPOPT_OFFSET];
12004 off--;
12005 if (optlen < IP_ADDR_LEN ||
12006 off > optlen - IP_ADDR_LEN) {
12007 /* End of source route */
12008 break;
12009 }
12010 /*
12011 * This will only happen if two consecutive entries
12012 * in the source route contains our address or if
12013 * it is a packet with a loose source route which
12014 * reaches us before consuming the whole source route
12015 */
12016
12017 if (optval == IPOPT_SSRR) {
12018 return;
12019 }
12020 /*
12021 * Hack: instead of dropping the packet truncate the
12022 * source route to what has been used by filling the
12023 * rest with IPOPT_NOP.
12024 */
12025 opt[IPOPT_OLEN] = (uint8_t)off;
12026 while (off < optlen) {
12027 opt[off++] = IPOPT_NOP;
12028 }
12029 break;
12030 case IPOPT_RR:
12031 off = opt[IPOPT_OFFSET];
12032 off--;
12033 if (optlen < IP_ADDR_LEN ||
12034 off > optlen - IP_ADDR_LEN) {
12035 /* No more room - ignore */
12036 ip1dbg((
12037 "ip_output_local_options: end of RR\n"));
12038 break;
12039 }
12040 dst = htonl(INADDR_LOOPBACK);
12041 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12042 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12043 break;
12044 case IPOPT_TS:
12045 /* Insert timestamp if there is romm */
12046 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12047 case IPOPT_TS_TSONLY:
12048 off = IPOPT_TS_TIMELEN;
12049 break;
12050 case IPOPT_TS_PRESPEC:
12051 case IPOPT_TS_PRESPEC_RFC791:
12052 /* Verify that the address matched */
12053 off = opt[IPOPT_OFFSET] - 1;
12054 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
12055 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
12056 /* Not for us */
12057 break;
12058 }
12059 /* FALLTHROUGH */
12060 case IPOPT_TS_TSANDADDR:
12061 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
12062 break;
12063 default:
12064 /*
12065 * ip_*put_options should have already
12066 * dropped this packet.
12067 */
12068 cmn_err(CE_PANIC, "ip_output_local_options: "
12069 "unknown IT - bug in ip_output_options?\n");
12070 }
12071 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
12072 /* Increase overflow counter */
12073 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
12074 opt[IPOPT_POS_OV_FLG] = (uint8_t)
12075 (opt[IPOPT_POS_OV_FLG] & 0x0F) |
12076 (off << 4);
12077 break;
12078 }
12079 off = opt[IPOPT_OFFSET] - 1;
12080 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12081 case IPOPT_TS_PRESPEC:
12082 case IPOPT_TS_PRESPEC_RFC791:
12083 case IPOPT_TS_TSANDADDR:
12084 dst = htonl(INADDR_LOOPBACK);
12085 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12086 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12087 /* FALLTHROUGH */
12088 case IPOPT_TS_TSONLY:
12089 off = opt[IPOPT_OFFSET] - 1;
12090 /* Compute # of milliseconds since midnight */
12091 gethrestime(&now);
12092 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
12093 NSEC2MSEC(now.tv_nsec);
12094 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
12095 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
12096 break;
12097 }
12098 break;
12099 }
12100 }
12101 }
12102
12103 /*
12104 * Prepend an M_DATA fastpath header, and if none present prepend a
12105 * DL_UNITDATA_REQ. Frees the mblk on failure.
12106 *
12107 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
12108 * If there is a change to them, the nce will be deleted (condemned) and
12109 * a new nce_t will be created when packets are sent. Thus we need no locks
12110 * to access those fields.
12111 *
12112 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
12113 * we place b_band in dl_priority.dl_max.
12114 */
12115 static mblk_t *
ip_xmit_attach_llhdr(mblk_t * mp,nce_t * nce)12116 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
12117 {
12118 uint_t hlen;
12119 mblk_t *mp1;
12120 uint_t priority;
12121 uchar_t *rptr;
12122
12123 rptr = mp->b_rptr;
12124
12125 ASSERT(DB_TYPE(mp) == M_DATA);
12126 priority = mp->b_band;
12127
12128 ASSERT(nce != NULL);
12129 if ((mp1 = nce->nce_fp_mp) != NULL) {
12130 hlen = MBLKL(mp1);
12131 /*
12132 * Check if we have enough room to prepend fastpath
12133 * header
12134 */
12135 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
12136 rptr -= hlen;
12137 bcopy(mp1->b_rptr, rptr, hlen);
12138 /*
12139 * Set the b_rptr to the start of the link layer
12140 * header
12141 */
12142 mp->b_rptr = rptr;
12143 return (mp);
12144 }
12145 mp1 = copyb(mp1);
12146 if (mp1 == NULL) {
12147 ill_t *ill = nce->nce_ill;
12148
12149 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12150 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12151 freemsg(mp);
12152 return (NULL);
12153 }
12154 mp1->b_band = priority;
12155 mp1->b_cont = mp;
12156 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
12157 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
12158 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
12159 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
12160 DB_LSOMSS(mp1) = DB_LSOMSS(mp);
12161 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
12162 /*
12163 * XXX disable ICK_VALID and compute checksum
12164 * here; can happen if nce_fp_mp changes and
12165 * it can't be copied now due to insufficient
12166 * space. (unlikely, fp mp can change, but it
12167 * does not increase in length)
12168 */
12169 return (mp1);
12170 }
12171 mp1 = copyb(nce->nce_dlur_mp);
12172
12173 if (mp1 == NULL) {
12174 ill_t *ill = nce->nce_ill;
12175
12176 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12177 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12178 freemsg(mp);
12179 return (NULL);
12180 }
12181 mp1->b_cont = mp;
12182 if (priority != 0) {
12183 mp1->b_band = priority;
12184 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
12185 priority;
12186 }
12187 return (mp1);
12188 }
12189
12190 /*
12191 * Finish the outbound IPsec processing. This function is called from
12192 * ipsec_out_process() if the IPsec packet was processed
12193 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12194 * asynchronously.
12195 *
12196 * This is common to IPv4 and IPv6.
12197 */
12198 int
ip_output_post_ipsec(mblk_t * mp,ip_xmit_attr_t * ixa)12199 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
12200 {
12201 iaflags_t ixaflags = ixa->ixa_flags;
12202 uint_t pktlen;
12203
12204
12205 /* AH/ESP don't update ixa_pktlen when they modify the packet */
12206 if (ixaflags & IXAF_IS_IPV4) {
12207 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12208
12209 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12210 pktlen = ntohs(ipha->ipha_length);
12211 } else {
12212 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12213
12214 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12215 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12216 }
12217
12218 /*
12219 * We release any hard reference on the SAs here to make
12220 * sure the SAs can be garbage collected. ipsr_sa has a soft reference
12221 * on the SAs.
12222 * If in the future we want the hard latching of the SAs in the
12223 * ip_xmit_attr_t then we should remove this.
12224 */
12225 if (ixa->ixa_ipsec_esp_sa != NULL) {
12226 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12227 ixa->ixa_ipsec_esp_sa = NULL;
12228 }
12229 if (ixa->ixa_ipsec_ah_sa != NULL) {
12230 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12231 ixa->ixa_ipsec_ah_sa = NULL;
12232 }
12233
12234 /* Do we need to fragment? */
12235 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
12236 pktlen > ixa->ixa_fragsize) {
12237 if (ixaflags & IXAF_IS_IPV4) {
12238 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
12239 /*
12240 * We check for the DF case in ipsec_out_process
12241 * hence this only handles the non-DF case.
12242 */
12243 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
12244 pktlen, ixa->ixa_fragsize,
12245 ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12246 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
12247 &ixa->ixa_cookie));
12248 } else {
12249 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
12250 if (mp == NULL) {
12251 /* MIB and ip_drop_output already done */
12252 return (ENOMEM);
12253 }
12254 pktlen += sizeof (ip6_frag_t);
12255 if (pktlen > ixa->ixa_fragsize) {
12256 return (ip_fragment_v6(mp, ixa->ixa_nce,
12257 ixa->ixa_flags, pktlen,
12258 ixa->ixa_fragsize, ixa->ixa_xmit_hint,
12259 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
12260 ixa->ixa_postfragfn, &ixa->ixa_cookie));
12261 }
12262 }
12263 }
12264 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
12265 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12266 ixa->ixa_no_loop_zoneid, NULL));
12267 }
12268
12269 /*
12270 * Finish the inbound IPsec processing. This function is called from
12271 * ipsec_out_process() if the IPsec packet was processed
12272 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12273 * asynchronously.
12274 *
12275 * This is common to IPv4 and IPv6.
12276 */
12277 void
ip_input_post_ipsec(mblk_t * mp,ip_recv_attr_t * ira)12278 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
12279 {
12280 iaflags_t iraflags = ira->ira_flags;
12281
12282 /* Length might have changed */
12283 if (iraflags & IRAF_IS_IPV4) {
12284 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12285
12286 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12287 ira->ira_pktlen = ntohs(ipha->ipha_length);
12288 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
12289 ira->ira_protocol = ipha->ipha_protocol;
12290
12291 ip_fanout_v4(mp, ipha, ira);
12292 } else {
12293 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12294 uint8_t *nexthdrp;
12295
12296 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12297 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12298 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
12299 &nexthdrp)) {
12300 /* Malformed packet */
12301 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
12302 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
12303 freemsg(mp);
12304 return;
12305 }
12306 ira->ira_protocol = *nexthdrp;
12307 ip_fanout_v6(mp, ip6h, ira);
12308 }
12309 }
12310
12311 /*
12312 * Select which AH & ESP SA's to use (if any) for the outbound packet.
12313 *
12314 * If this function returns B_TRUE, the requested SA's have been filled
12315 * into the ixa_ipsec_*_sa pointers.
12316 *
12317 * If the function returns B_FALSE, the packet has been "consumed", most
12318 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
12319 *
12320 * The SA references created by the protocol-specific "select"
12321 * function will be released in ip_output_post_ipsec.
12322 */
12323 static boolean_t
ipsec_out_select_sa(mblk_t * mp,ip_xmit_attr_t * ixa)12324 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
12325 {
12326 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
12327 ipsec_policy_t *pp;
12328 ipsec_action_t *ap;
12329
12330 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12331 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12332 (ixa->ixa_ipsec_action != NULL));
12333
12334 ap = ixa->ixa_ipsec_action;
12335 if (ap == NULL) {
12336 pp = ixa->ixa_ipsec_policy;
12337 ASSERT(pp != NULL);
12338 ap = pp->ipsp_act;
12339 ASSERT(ap != NULL);
12340 }
12341
12342 /*
12343 * We have an action. now, let's select SA's.
12344 * A side effect of setting ixa_ipsec_*_sa is that it will
12345 * be cached in the conn_t.
12346 */
12347 if (ap->ipa_want_esp) {
12348 if (ixa->ixa_ipsec_esp_sa == NULL) {
12349 need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
12350 IPPROTO_ESP);
12351 }
12352 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
12353 }
12354
12355 if (ap->ipa_want_ah) {
12356 if (ixa->ixa_ipsec_ah_sa == NULL) {
12357 need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
12358 IPPROTO_AH);
12359 }
12360 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
12361 /*
12362 * The ESP and AH processing order needs to be preserved
12363 * when both protocols are required (ESP should be applied
12364 * before AH for an outbound packet). Force an ESP ACQUIRE
12365 * when both ESP and AH are required, and an AH ACQUIRE
12366 * is needed.
12367 */
12368 if (ap->ipa_want_esp && need_ah_acquire)
12369 need_esp_acquire = B_TRUE;
12370 }
12371
12372 /*
12373 * Send an ACQUIRE (extended, regular, or both) if we need one.
12374 * Release SAs that got referenced, but will not be used until we
12375 * acquire _all_ of the SAs we need.
12376 */
12377 if (need_ah_acquire || need_esp_acquire) {
12378 if (ixa->ixa_ipsec_ah_sa != NULL) {
12379 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12380 ixa->ixa_ipsec_ah_sa = NULL;
12381 }
12382 if (ixa->ixa_ipsec_esp_sa != NULL) {
12383 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12384 ixa->ixa_ipsec_esp_sa = NULL;
12385 }
12386
12387 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
12388 return (B_FALSE);
12389 }
12390
12391 return (B_TRUE);
12392 }
12393
12394 /*
12395 * Handle IPsec output processing.
12396 * This function is only entered once for a given packet.
12397 * We try to do things synchronously, but if we need to have user-level
12398 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
12399 * will be completed
12400 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
12401 * - when asynchronous ESP is done it will do AH
12402 *
12403 * In all cases we come back in ip_output_post_ipsec() to fragment and
12404 * send out the packet.
12405 */
12406 int
ipsec_out_process(mblk_t * mp,ip_xmit_attr_t * ixa)12407 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
12408 {
12409 ill_t *ill = ixa->ixa_nce->nce_ill;
12410 ip_stack_t *ipst = ixa->ixa_ipst;
12411 ipsec_stack_t *ipss;
12412 ipsec_policy_t *pp;
12413 ipsec_action_t *ap;
12414
12415 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12416
12417 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12418 (ixa->ixa_ipsec_action != NULL));
12419
12420 ipss = ipst->ips_netstack->netstack_ipsec;
12421 if (!ipsec_loaded(ipss)) {
12422 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12423 ip_drop_packet(mp, B_TRUE, ill,
12424 DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12425 &ipss->ipsec_dropper);
12426 return (ENOTSUP);
12427 }
12428
12429 ap = ixa->ixa_ipsec_action;
12430 if (ap == NULL) {
12431 pp = ixa->ixa_ipsec_policy;
12432 ASSERT(pp != NULL);
12433 ap = pp->ipsp_act;
12434 ASSERT(ap != NULL);
12435 }
12436
12437 /* Handle explicit drop action and bypass. */
12438 switch (ap->ipa_act.ipa_type) {
12439 case IPSEC_ACT_DISCARD:
12440 case IPSEC_ACT_REJECT:
12441 ip_drop_packet(mp, B_FALSE, ill,
12442 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12443 return (EHOSTUNREACH); /* IPsec policy failure */
12444 case IPSEC_ACT_BYPASS:
12445 return (ip_output_post_ipsec(mp, ixa));
12446 }
12447
12448 /*
12449 * The order of processing is first insert a IP header if needed.
12450 * Then insert the ESP header and then the AH header.
12451 */
12452 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12453 /*
12454 * First get the outer IP header before sending
12455 * it to ESP.
12456 */
12457 ipha_t *oipha, *iipha;
12458 mblk_t *outer_mp, *inner_mp;
12459
12460 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12461 (void) mi_strlog(ill->ill_rq, 0,
12462 SL_ERROR|SL_TRACE|SL_CONSOLE,
12463 "ipsec_out_process: "
12464 "Self-Encapsulation failed: Out of memory\n");
12465 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12466 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12467 freemsg(mp);
12468 return (ENOBUFS);
12469 }
12470 inner_mp = mp;
12471 ASSERT(inner_mp->b_datap->db_type == M_DATA);
12472 oipha = (ipha_t *)outer_mp->b_rptr;
12473 iipha = (ipha_t *)inner_mp->b_rptr;
12474 *oipha = *iipha;
12475 outer_mp->b_wptr += sizeof (ipha_t);
12476 oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12477 sizeof (ipha_t));
12478 oipha->ipha_protocol = IPPROTO_ENCAP;
12479 oipha->ipha_version_and_hdr_length =
12480 IP_SIMPLE_HDR_VERSION;
12481 oipha->ipha_hdr_checksum = 0;
12482 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12483 outer_mp->b_cont = inner_mp;
12484 mp = outer_mp;
12485
12486 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12487 }
12488
12489 /* If we need to wait for a SA then we can't return any errno */
12490 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12491 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12492 !ipsec_out_select_sa(mp, ixa))
12493 return (0);
12494
12495 /*
12496 * By now, we know what SA's to use. Toss over to ESP & AH
12497 * to do the heavy lifting.
12498 */
12499 if (ap->ipa_want_esp) {
12500 ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12501
12502 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12503 if (mp == NULL) {
12504 /*
12505 * Either it failed or is pending. In the former case
12506 * ipIfStatsInDiscards was increased.
12507 */
12508 return (0);
12509 }
12510 }
12511
12512 if (ap->ipa_want_ah) {
12513 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12514
12515 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12516 if (mp == NULL) {
12517 /*
12518 * Either it failed or is pending. In the former case
12519 * ipIfStatsInDiscards was increased.
12520 */
12521 return (0);
12522 }
12523 }
12524 /*
12525 * We are done with IPsec processing. Send it over
12526 * the wire.
12527 */
12528 return (ip_output_post_ipsec(mp, ixa));
12529 }
12530
12531 /*
12532 * ioctls that go through a down/up sequence may need to wait for the down
12533 * to complete. This involves waiting for the ire and ipif refcnts to go down
12534 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12535 */
12536 /* ARGSUSED */
12537 void
ip_reprocess_ioctl(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * dummy_arg)12538 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12539 {
12540 struct iocblk *iocp;
12541 mblk_t *mp1;
12542 ip_ioctl_cmd_t *ipip;
12543 int err;
12544 sin_t *sin;
12545 struct lifreq *lifr;
12546 struct ifreq *ifr;
12547
12548 iocp = (struct iocblk *)mp->b_rptr;
12549 ASSERT(ipsq != NULL);
12550 /* Existence of mp1 verified in ip_wput_nondata */
12551 mp1 = mp->b_cont->b_cont;
12552 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12553 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12554 /*
12555 * Special case where ipx_current_ipif is not set:
12556 * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12557 * We are here as were not able to complete the operation in
12558 * ipif_set_values because we could not become exclusive on
12559 * the new ipsq.
12560 */
12561 ill_t *ill = q->q_ptr;
12562 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12563 }
12564 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12565
12566 if (ipip->ipi_cmd_type == IF_CMD) {
12567 /* This a old style SIOC[GS]IF* command */
12568 ifr = (struct ifreq *)mp1->b_rptr;
12569 sin = (sin_t *)&ifr->ifr_addr;
12570 } else if (ipip->ipi_cmd_type == LIF_CMD) {
12571 /* This a new style SIOC[GS]LIF* command */
12572 lifr = (struct lifreq *)mp1->b_rptr;
12573 sin = (sin_t *)&lifr->lifr_addr;
12574 } else {
12575 sin = NULL;
12576 }
12577
12578 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12579 q, mp, ipip, mp1->b_rptr);
12580
12581 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12582 int, ipip->ipi_cmd,
12583 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12584 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12585
12586 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12587 }
12588
12589 /*
12590 * ioctl processing
12591 *
12592 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12593 * the ioctl command in the ioctl tables, determines the copyin data size
12594 * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12595 *
12596 * ioctl processing then continues when the M_IOCDATA makes its way down to
12597 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its
12598 * associated 'conn' is refheld till the end of the ioctl and the general
12599 * ioctl processing function ip_process_ioctl() is called to extract the
12600 * arguments and process the ioctl. To simplify extraction, ioctl commands
12601 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12602 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12603 * is used to extract the ioctl's arguments.
12604 *
12605 * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12606 * so goes thru the serialization primitive ipsq_try_enter. Then the
12607 * appropriate function to handle the ioctl is called based on the entry in
12608 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12609 * which also refreleases the 'conn' that was refheld at the start of the
12610 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12611 *
12612 * Many exclusive ioctls go thru an internal down up sequence as part of
12613 * the operation. For example an attempt to change the IP address of an
12614 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12615 * does all the cleanup such as deleting all ires that use this address.
12616 * Then we need to wait till all references to the interface go away.
12617 */
12618 void
ip_process_ioctl(ipsq_t * ipsq,queue_t * q,mblk_t * mp,void * arg)12619 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12620 {
12621 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12622 ip_ioctl_cmd_t *ipip = arg;
12623 ip_extract_func_t *extract_funcp;
12624 cmd_info_t ci;
12625 int err;
12626 boolean_t entered_ipsq = B_FALSE;
12627
12628 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12629
12630 if (ipip == NULL)
12631 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12632
12633 /*
12634 * SIOCLIFADDIF needs to go thru a special path since the
12635 * ill may not exist yet. This happens in the case of lo0
12636 * which is created using this ioctl.
12637 */
12638 if (ipip->ipi_cmd == SIOCLIFADDIF) {
12639 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12640 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12641 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12642 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12643 return;
12644 }
12645
12646 ci.ci_ipif = NULL;
12647 extract_funcp = NULL;
12648 switch (ipip->ipi_cmd_type) {
12649 case MISC_CMD:
12650 case MSFILT_CMD:
12651 /*
12652 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12653 */
12654 if (ipip->ipi_cmd == IF_UNITSEL) {
12655 /* ioctl comes down the ill */
12656 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12657 ipif_refhold(ci.ci_ipif);
12658 }
12659 err = 0;
12660 ci.ci_sin = NULL;
12661 ci.ci_sin6 = NULL;
12662 ci.ci_lifr = NULL;
12663 extract_funcp = NULL;
12664 break;
12665
12666 case IF_CMD:
12667 case LIF_CMD:
12668 extract_funcp = ip_extract_lifreq;
12669 break;
12670
12671 case ARP_CMD:
12672 case XARP_CMD:
12673 extract_funcp = ip_extract_arpreq;
12674 break;
12675
12676 default:
12677 ASSERT(0);
12678 }
12679
12680 if (extract_funcp != NULL) {
12681 err = (*extract_funcp)(q, mp, ipip, &ci);
12682 if (err != 0) {
12683 DTRACE_PROBE4(ipif__ioctl,
12684 char *, "ip_process_ioctl finish err",
12685 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12686 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12687 return;
12688 }
12689
12690 /*
12691 * All of the extraction functions return a refheld ipif.
12692 */
12693 ASSERT(ci.ci_ipif != NULL);
12694 }
12695
12696 if (!(ipip->ipi_flags & IPI_WR)) {
12697 /*
12698 * A return value of EINPROGRESS means the ioctl is
12699 * either queued and waiting for some reason or has
12700 * already completed.
12701 */
12702 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12703 ci.ci_lifr);
12704 if (ci.ci_ipif != NULL) {
12705 DTRACE_PROBE4(ipif__ioctl,
12706 char *, "ip_process_ioctl finish RD",
12707 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12708 ipif_t *, ci.ci_ipif);
12709 ipif_refrele(ci.ci_ipif);
12710 } else {
12711 DTRACE_PROBE4(ipif__ioctl,
12712 char *, "ip_process_ioctl finish RD",
12713 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12714 }
12715 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12716 return;
12717 }
12718
12719 ASSERT(ci.ci_ipif != NULL);
12720
12721 /*
12722 * If ipsq is non-NULL, we are already being called exclusively
12723 */
12724 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12725 if (ipsq == NULL) {
12726 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12727 NEW_OP, B_TRUE);
12728 if (ipsq == NULL) {
12729 ipif_refrele(ci.ci_ipif);
12730 return;
12731 }
12732 entered_ipsq = B_TRUE;
12733 }
12734 /*
12735 * Release the ipif so that ipif_down and friends that wait for
12736 * references to go away are not misled about the current ipif_refcnt
12737 * values. We are writer so we can access the ipif even after releasing
12738 * the ipif.
12739 */
12740 ipif_refrele(ci.ci_ipif);
12741
12742 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12743
12744 /*
12745 * A return value of EINPROGRESS means the ioctl is
12746 * either queued and waiting for some reason or has
12747 * already completed.
12748 */
12749 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12750
12751 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12752 int, ipip->ipi_cmd,
12753 ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12754 ipif_t *, ci.ci_ipif);
12755 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12756
12757 if (entered_ipsq)
12758 ipsq_exit(ipsq);
12759 }
12760
12761 /*
12762 * Complete the ioctl. Typically ioctls use the mi package and need to
12763 * do mi_copyout/mi_copy_done.
12764 */
12765 void
ip_ioctl_finish(queue_t * q,mblk_t * mp,int err,int mode,ipsq_t * ipsq)12766 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12767 {
12768 conn_t *connp = NULL;
12769
12770 if (err == EINPROGRESS)
12771 return;
12772
12773 if (CONN_Q(q)) {
12774 connp = Q_TO_CONN(q);
12775 ASSERT(connp->conn_ref >= 2);
12776 }
12777
12778 switch (mode) {
12779 case COPYOUT:
12780 if (err == 0)
12781 mi_copyout(q, mp);
12782 else
12783 mi_copy_done(q, mp, err);
12784 break;
12785
12786 case NO_COPYOUT:
12787 mi_copy_done(q, mp, err);
12788 break;
12789
12790 default:
12791 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */
12792 break;
12793 }
12794
12795 /*
12796 * The conn refhold and ioctlref placed on the conn at the start of the
12797 * ioctl are released here.
12798 */
12799 if (connp != NULL) {
12800 CONN_DEC_IOCTLREF(connp);
12801 CONN_OPER_PENDING_DONE(connp);
12802 }
12803
12804 if (ipsq != NULL)
12805 ipsq_current_finish(ipsq);
12806 }
12807
12808 /* Handles all non data messages */
12809 int
ip_wput_nondata(queue_t * q,mblk_t * mp)12810 ip_wput_nondata(queue_t *q, mblk_t *mp)
12811 {
12812 mblk_t *mp1;
12813 struct iocblk *iocp;
12814 ip_ioctl_cmd_t *ipip;
12815 conn_t *connp;
12816 cred_t *cr;
12817 char *proto_str;
12818
12819 if (CONN_Q(q))
12820 connp = Q_TO_CONN(q);
12821 else
12822 connp = NULL;
12823
12824 iocp = NULL;
12825 switch (DB_TYPE(mp)) {
12826 case M_IOCTL:
12827 /*
12828 * IOCTL processing begins in ip_sioctl_copyin_setup which
12829 * will arrange to copy in associated control structures.
12830 */
12831 ip_sioctl_copyin_setup(q, mp);
12832 return (0);
12833 case M_IOCDATA:
12834 /*
12835 * Ensure that this is associated with one of our trans-
12836 * parent ioctls. If it's not ours, discard it if we're
12837 * running as a driver, or pass it on if we're a module.
12838 */
12839 iocp = (struct iocblk *)mp->b_rptr;
12840 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12841 if (ipip == NULL) {
12842 if (q->q_next == NULL) {
12843 goto nak;
12844 } else {
12845 putnext(q, mp);
12846 }
12847 return (0);
12848 }
12849 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12850 /*
12851 * The ioctl is one we recognise, but is not consumed
12852 * by IP as a module and we are a module, so we drop
12853 */
12854 goto nak;
12855 }
12856
12857 /* IOCTL continuation following copyin or copyout. */
12858 if (mi_copy_state(q, mp, NULL) == -1) {
12859 /*
12860 * The copy operation failed. mi_copy_state already
12861 * cleaned up, so we're out of here.
12862 */
12863 return (0);
12864 }
12865 /*
12866 * If we just completed a copy in, we become writer and
12867 * continue processing in ip_sioctl_copyin_done. If it
12868 * was a copy out, we call mi_copyout again. If there is
12869 * nothing more to copy out, it will complete the IOCTL.
12870 */
12871 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12872 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12873 mi_copy_done(q, mp, EPROTO);
12874 return (0);
12875 }
12876 /*
12877 * Check for cases that need more copying. A return
12878 * value of 0 means a second copyin has been started,
12879 * so we return; a return value of 1 means no more
12880 * copying is needed, so we continue.
12881 */
12882 if (ipip->ipi_cmd_type == MSFILT_CMD &&
12883 MI_COPY_COUNT(mp) == 1) {
12884 if (ip_copyin_msfilter(q, mp) == 0)
12885 return (0);
12886 }
12887 /*
12888 * Refhold the conn, till the ioctl completes. This is
12889 * needed in case the ioctl ends up in the pending mp
12890 * list. Every mp in the ipx_pending_mp list must have
12891 * a refhold on the conn to resume processing. The
12892 * refhold is released when the ioctl completes
12893 * (whether normally or abnormally). An ioctlref is also
12894 * placed on the conn to prevent TCP from removing the
12895 * queue needed to send the ioctl reply back.
12896 * In all cases ip_ioctl_finish is called to finish
12897 * the ioctl and release the refholds.
12898 */
12899 if (connp != NULL) {
12900 /* This is not a reentry */
12901 CONN_INC_REF(connp);
12902 CONN_INC_IOCTLREF(connp);
12903 } else {
12904 if (!(ipip->ipi_flags & IPI_MODOK)) {
12905 mi_copy_done(q, mp, EINVAL);
12906 return (0);
12907 }
12908 }
12909
12910 ip_process_ioctl(NULL, q, mp, ipip);
12911
12912 } else {
12913 mi_copyout(q, mp);
12914 }
12915 return (0);
12916
12917 case M_IOCNAK:
12918 /*
12919 * The only way we could get here is if a resolver didn't like
12920 * an IOCTL we sent it. This shouldn't happen.
12921 */
12922 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12923 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12924 ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12925 freemsg(mp);
12926 return (0);
12927 case M_IOCACK:
12928 /* /dev/ip shouldn't see this */
12929 goto nak;
12930 case M_FLUSH:
12931 if (*mp->b_rptr & FLUSHW)
12932 flushq(q, FLUSHALL);
12933 if (q->q_next) {
12934 putnext(q, mp);
12935 return (0);
12936 }
12937 if (*mp->b_rptr & FLUSHR) {
12938 *mp->b_rptr &= ~FLUSHW;
12939 qreply(q, mp);
12940 return (0);
12941 }
12942 freemsg(mp);
12943 return (0);
12944 case M_CTL:
12945 break;
12946 case M_PROTO:
12947 case M_PCPROTO:
12948 /*
12949 * The only PROTO messages we expect are SNMP-related.
12950 */
12951 switch (((union T_primitives *)mp->b_rptr)->type) {
12952 case T_SVR4_OPTMGMT_REQ:
12953 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12954 "flags %x\n",
12955 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12956
12957 if (connp == NULL) {
12958 proto_str = "T_SVR4_OPTMGMT_REQ";
12959 goto protonak;
12960 }
12961
12962 /*
12963 * All Solaris components should pass a db_credp
12964 * for this TPI message, hence we ASSERT.
12965 * But in case there is some other M_PROTO that looks
12966 * like a TPI message sent by some other kernel
12967 * component, we check and return an error.
12968 */
12969 cr = msg_getcred(mp, NULL);
12970 ASSERT(cr != NULL);
12971 if (cr == NULL) {
12972 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12973 if (mp != NULL)
12974 qreply(q, mp);
12975 return (0);
12976 }
12977
12978 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12979 proto_str = "Bad SNMPCOM request?";
12980 goto protonak;
12981 }
12982 return (0);
12983 default:
12984 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12985 (int)*(uint_t *)mp->b_rptr));
12986 freemsg(mp);
12987 return (0);
12988 }
12989 default:
12990 break;
12991 }
12992 if (q->q_next) {
12993 putnext(q, mp);
12994 } else
12995 freemsg(mp);
12996 return (0);
12997
12998 nak:
12999 iocp->ioc_error = EINVAL;
13000 mp->b_datap->db_type = M_IOCNAK;
13001 iocp->ioc_count = 0;
13002 qreply(q, mp);
13003 return (0);
13004
13005 protonak:
13006 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
13007 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
13008 qreply(q, mp);
13009 return (0);
13010 }
13011
13012 /*
13013 * Process IP options in an outbound packet. Verify that the nexthop in a
13014 * strict source route is onlink.
13015 * Returns non-zero if something fails in which case an ICMP error has been
13016 * sent and mp freed.
13017 *
13018 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
13019 */
13020 int
ip_output_options(mblk_t * mp,ipha_t * ipha,ip_xmit_attr_t * ixa,ill_t * ill)13021 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
13022 {
13023 ipoptp_t opts;
13024 uchar_t *opt;
13025 uint8_t optval;
13026 uint8_t optlen;
13027 ipaddr_t dst;
13028 intptr_t code = 0;
13029 ire_t *ire;
13030 ip_stack_t *ipst = ixa->ixa_ipst;
13031 ip_recv_attr_t iras;
13032
13033 ip2dbg(("ip_output_options\n"));
13034
13035 opt = NULL;
13036 dst = ipha->ipha_dst;
13037 for (optval = ipoptp_first(&opts, ipha);
13038 optval != IPOPT_EOL;
13039 optval = ipoptp_next(&opts)) {
13040 opt = opts.ipoptp_cur;
13041 optlen = opts.ipoptp_len;
13042 ip2dbg(("ip_output_options: opt %d, len %d\n",
13043 optval, optlen));
13044 switch (optval) {
13045 uint32_t off;
13046 case IPOPT_SSRR:
13047 case IPOPT_LSRR:
13048 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13049 ip1dbg((
13050 "ip_output_options: bad option offset\n"));
13051 code = (char *)&opt[IPOPT_OLEN] -
13052 (char *)ipha;
13053 goto param_prob;
13054 }
13055 off = opt[IPOPT_OFFSET];
13056 ip1dbg(("ip_output_options: next hop 0x%x\n",
13057 ntohl(dst)));
13058 /*
13059 * For strict: verify that dst is directly
13060 * reachable.
13061 */
13062 if (optval == IPOPT_SSRR) {
13063 ire = ire_ftable_lookup_v4(dst, 0, 0,
13064 IRE_INTERFACE, NULL, ALL_ZONES,
13065 ixa->ixa_tsl,
13066 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
13067 NULL);
13068 if (ire == NULL) {
13069 ip1dbg(("ip_output_options: SSRR not"
13070 " directly reachable: 0x%x\n",
13071 ntohl(dst)));
13072 goto bad_src_route;
13073 }
13074 ire_refrele(ire);
13075 }
13076 break;
13077 case IPOPT_RR:
13078 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13079 ip1dbg((
13080 "ip_output_options: bad option offset\n"));
13081 code = (char *)&opt[IPOPT_OLEN] -
13082 (char *)ipha;
13083 goto param_prob;
13084 }
13085 break;
13086 case IPOPT_TS:
13087 /*
13088 * Verify that length >=5 and that there is either
13089 * room for another timestamp or that the overflow
13090 * counter is not maxed out.
13091 */
13092 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
13093 if (optlen < IPOPT_MINLEN_IT) {
13094 goto param_prob;
13095 }
13096 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13097 ip1dbg((
13098 "ip_output_options: bad option offset\n"));
13099 code = (char *)&opt[IPOPT_OFFSET] -
13100 (char *)ipha;
13101 goto param_prob;
13102 }
13103 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
13104 case IPOPT_TS_TSONLY:
13105 off = IPOPT_TS_TIMELEN;
13106 break;
13107 case IPOPT_TS_TSANDADDR:
13108 case IPOPT_TS_PRESPEC:
13109 case IPOPT_TS_PRESPEC_RFC791:
13110 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
13111 break;
13112 default:
13113 code = (char *)&opt[IPOPT_POS_OV_FLG] -
13114 (char *)ipha;
13115 goto param_prob;
13116 }
13117 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
13118 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
13119 /*
13120 * No room and the overflow counter is 15
13121 * already.
13122 */
13123 goto param_prob;
13124 }
13125 break;
13126 }
13127 }
13128
13129 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
13130 return (0);
13131
13132 ip1dbg(("ip_output_options: error processing IP options."));
13133 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
13134
13135 param_prob:
13136 bzero(&iras, sizeof (iras));
13137 iras.ira_ill = iras.ira_rill = ill;
13138 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13139 iras.ira_rifindex = iras.ira_ruifindex;
13140 iras.ira_flags = IRAF_IS_IPV4;
13141
13142 ip_drop_output("ip_output_options", mp, ill);
13143 icmp_param_problem(mp, (uint8_t)code, &iras);
13144 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13145 return (-1);
13146
13147 bad_src_route:
13148 bzero(&iras, sizeof (iras));
13149 iras.ira_ill = iras.ira_rill = ill;
13150 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13151 iras.ira_rifindex = iras.ira_ruifindex;
13152 iras.ira_flags = IRAF_IS_IPV4;
13153
13154 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
13155 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
13156 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13157 return (-1);
13158 }
13159
13160 /*
13161 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
13162 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
13163 * thru /etc/system.
13164 */
13165 #define CONN_MAXDRAINCNT 64
13166
13167 static void
conn_drain_init(ip_stack_t * ipst)13168 conn_drain_init(ip_stack_t *ipst)
13169 {
13170 int i, j;
13171 idl_tx_list_t *itl_tx;
13172
13173 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
13174
13175 if ((ipst->ips_conn_drain_list_cnt == 0) ||
13176 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
13177 /*
13178 * Default value of the number of drainers is the
13179 * number of cpus, subject to maximum of 8 drainers.
13180 */
13181 if (boot_max_ncpus != -1)
13182 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
13183 else
13184 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
13185 }
13186
13187 ipst->ips_idl_tx_list =
13188 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
13189 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13190 itl_tx = &ipst->ips_idl_tx_list[i];
13191 itl_tx->txl_drain_list =
13192 kmem_zalloc(ipst->ips_conn_drain_list_cnt *
13193 sizeof (idl_t), KM_SLEEP);
13194 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
13195 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
13196 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
13197 MUTEX_DEFAULT, NULL);
13198 itl_tx->txl_drain_list[j].idl_itl = itl_tx;
13199 }
13200 }
13201 }
13202
13203 static void
conn_drain_fini(ip_stack_t * ipst)13204 conn_drain_fini(ip_stack_t *ipst)
13205 {
13206 int i;
13207 idl_tx_list_t *itl_tx;
13208
13209 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13210 itl_tx = &ipst->ips_idl_tx_list[i];
13211 kmem_free(itl_tx->txl_drain_list,
13212 ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
13213 }
13214 kmem_free(ipst->ips_idl_tx_list,
13215 TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
13216 ipst->ips_idl_tx_list = NULL;
13217 }
13218
13219 /*
13220 * Flow control has blocked us from proceeding. Insert the given conn in one
13221 * of the conn drain lists. When flow control is unblocked, either ip_wsrv()
13222 * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
13223 * will call conn_walk_drain(). See the flow control notes at the top of this
13224 * file for more details.
13225 */
13226 void
conn_drain_insert(conn_t * connp,idl_tx_list_t * tx_list)13227 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
13228 {
13229 idl_t *idl = tx_list->txl_drain_list;
13230 uint_t index;
13231 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
13232
13233 mutex_enter(&connp->conn_lock);
13234 if (connp->conn_state_flags & CONN_CLOSING) {
13235 /*
13236 * The conn is closing as a result of which CONN_CLOSING
13237 * is set. Return.
13238 */
13239 mutex_exit(&connp->conn_lock);
13240 return;
13241 } else if (connp->conn_idl == NULL) {
13242 /*
13243 * Assign the next drain list round robin. We dont' use
13244 * a lock, and thus it may not be strictly round robin.
13245 * Atomicity of load/stores is enough to make sure that
13246 * conn_drain_list_index is always within bounds.
13247 */
13248 index = tx_list->txl_drain_index;
13249 ASSERT(index < ipst->ips_conn_drain_list_cnt);
13250 connp->conn_idl = &tx_list->txl_drain_list[index];
13251 index++;
13252 if (index == ipst->ips_conn_drain_list_cnt)
13253 index = 0;
13254 tx_list->txl_drain_index = index;
13255 } else {
13256 ASSERT(connp->conn_idl->idl_itl == tx_list);
13257 }
13258 mutex_exit(&connp->conn_lock);
13259
13260 idl = connp->conn_idl;
13261 mutex_enter(&idl->idl_lock);
13262 if ((connp->conn_drain_prev != NULL) ||
13263 (connp->conn_state_flags & CONN_CLOSING)) {
13264 /*
13265 * The conn is either already in the drain list or closing.
13266 * (We needed to check for CONN_CLOSING again since close can
13267 * sneak in between dropping conn_lock and acquiring idl_lock.)
13268 */
13269 mutex_exit(&idl->idl_lock);
13270 return;
13271 }
13272
13273 /*
13274 * The conn is not in the drain list. Insert it at the
13275 * tail of the drain list. The drain list is circular
13276 * and doubly linked. idl_conn points to the 1st element
13277 * in the list.
13278 */
13279 if (idl->idl_conn == NULL) {
13280 idl->idl_conn = connp;
13281 connp->conn_drain_next = connp;
13282 connp->conn_drain_prev = connp;
13283 } else {
13284 conn_t *head = idl->idl_conn;
13285
13286 connp->conn_drain_next = head;
13287 connp->conn_drain_prev = head->conn_drain_prev;
13288 head->conn_drain_prev->conn_drain_next = connp;
13289 head->conn_drain_prev = connp;
13290 }
13291 /*
13292 * For non streams based sockets assert flow control.
13293 */
13294 conn_setqfull(connp, NULL);
13295 mutex_exit(&idl->idl_lock);
13296 }
13297
13298 static void
conn_drain_remove(conn_t * connp)13299 conn_drain_remove(conn_t *connp)
13300 {
13301 idl_t *idl = connp->conn_idl;
13302
13303 if (idl != NULL) {
13304 /*
13305 * Remove ourself from the drain list.
13306 */
13307 if (connp->conn_drain_next == connp) {
13308 /* Singleton in the list */
13309 ASSERT(connp->conn_drain_prev == connp);
13310 idl->idl_conn = NULL;
13311 } else {
13312 connp->conn_drain_prev->conn_drain_next =
13313 connp->conn_drain_next;
13314 connp->conn_drain_next->conn_drain_prev =
13315 connp->conn_drain_prev;
13316 if (idl->idl_conn == connp)
13317 idl->idl_conn = connp->conn_drain_next;
13318 }
13319
13320 /*
13321 * NOTE: because conn_idl is associated with a specific drain
13322 * list which in turn is tied to the index the TX ring
13323 * (txl_cookie) hashes to, and because the TX ring can change
13324 * over the lifetime of the conn_t, we must clear conn_idl so
13325 * a subsequent conn_drain_insert() will set conn_idl again
13326 * based on the latest txl_cookie.
13327 */
13328 connp->conn_idl = NULL;
13329 }
13330 connp->conn_drain_next = NULL;
13331 connp->conn_drain_prev = NULL;
13332
13333 conn_clrqfull(connp, NULL);
13334 /*
13335 * For streams based sockets open up flow control.
13336 */
13337 if (!IPCL_IS_NONSTR(connp))
13338 enableok(connp->conn_wq);
13339 }
13340
13341 /*
13342 * This conn is closing, and we are called from ip_close. OR
13343 * this conn is draining because flow-control on the ill has been relieved.
13344 *
13345 * We must also need to remove conn's on this idl from the list, and also
13346 * inform the sockfs upcalls about the change in flow-control.
13347 */
13348 static void
conn_drain(conn_t * connp,boolean_t closing)13349 conn_drain(conn_t *connp, boolean_t closing)
13350 {
13351 idl_t *idl;
13352 conn_t *next_connp;
13353
13354 /*
13355 * connp->conn_idl is stable at this point, and no lock is needed
13356 * to check it. If we are called from ip_close, close has already
13357 * set CONN_CLOSING, thus freezing the value of conn_idl, and
13358 * called us only because conn_idl is non-null. If we are called thru
13359 * service, conn_idl could be null, but it cannot change because
13360 * service is single-threaded per queue, and there cannot be another
13361 * instance of service trying to call conn_drain_insert on this conn
13362 * now.
13363 */
13364 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
13365
13366 /*
13367 * If the conn doesn't exist or is not on a drain list, bail.
13368 */
13369 if (connp == NULL || connp->conn_idl == NULL ||
13370 connp->conn_drain_prev == NULL) {
13371 return;
13372 }
13373
13374 idl = connp->conn_idl;
13375 ASSERT(MUTEX_HELD(&idl->idl_lock));
13376
13377 if (!closing) {
13378 next_connp = connp->conn_drain_next;
13379 while (next_connp != connp) {
13380 conn_t *delconnp = next_connp;
13381
13382 next_connp = next_connp->conn_drain_next;
13383 conn_drain_remove(delconnp);
13384 }
13385 ASSERT(connp->conn_drain_next == idl->idl_conn);
13386 }
13387 conn_drain_remove(connp);
13388 }
13389
13390 /*
13391 * Write service routine. Shared perimeter entry point.
13392 * The device queue's messages has fallen below the low water mark and STREAMS
13393 * has backenabled the ill_wq. Send sockfs notification about flow-control on
13394 * each waiting conn.
13395 */
13396 int
ip_wsrv(queue_t * q)13397 ip_wsrv(queue_t *q)
13398 {
13399 ill_t *ill;
13400
13401 ill = (ill_t *)q->q_ptr;
13402 if (ill->ill_state_flags == 0) {
13403 ip_stack_t *ipst = ill->ill_ipst;
13404
13405 /*
13406 * The device flow control has opened up.
13407 * Walk through conn drain lists and qenable the
13408 * first conn in each list. This makes sense only
13409 * if the stream is fully plumbed and setup.
13410 * Hence the ill_state_flags check above.
13411 */
13412 ip1dbg(("ip_wsrv: walking\n"));
13413 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
13414 enableok(ill->ill_wq);
13415 }
13416 return (0);
13417 }
13418
13419 /*
13420 * Callback to disable flow control in IP.
13421 *
13422 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
13423 * is enabled.
13424 *
13425 * When MAC_TX() is not able to send any more packets, dld sets its queue
13426 * to QFULL and enable the STREAMS flow control. Later, when the underlying
13427 * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13428 * function and wakes up corresponding mac worker threads, which in turn
13429 * calls this callback function, and disables flow control.
13430 */
13431 void
ill_flow_enable(void * arg,ip_mac_tx_cookie_t cookie)13432 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13433 {
13434 ill_t *ill = (ill_t *)arg;
13435 ip_stack_t *ipst = ill->ill_ipst;
13436 idl_tx_list_t *idl_txl;
13437
13438 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13439 mutex_enter(&idl_txl->txl_lock);
13440 /* add code to to set a flag to indicate idl_txl is enabled */
13441 conn_walk_drain(ipst, idl_txl);
13442 mutex_exit(&idl_txl->txl_lock);
13443 }
13444
13445 /*
13446 * Flow control has been relieved and STREAMS has backenabled us; drain
13447 * all the conn lists on `tx_list'.
13448 */
13449 static void
conn_walk_drain(ip_stack_t * ipst,idl_tx_list_t * tx_list)13450 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13451 {
13452 int i;
13453 idl_t *idl;
13454
13455 IP_STAT(ipst, ip_conn_walk_drain);
13456
13457 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13458 idl = &tx_list->txl_drain_list[i];
13459 mutex_enter(&idl->idl_lock);
13460 conn_drain(idl->idl_conn, B_FALSE);
13461 mutex_exit(&idl->idl_lock);
13462 }
13463 }
13464
13465 /*
13466 * Determine if the ill and multicast aspects of that packets
13467 * "matches" the conn.
13468 */
13469 boolean_t
conn_wantpacket(conn_t * connp,ip_recv_attr_t * ira,ipha_t * ipha)13470 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13471 {
13472 ill_t *ill = ira->ira_rill;
13473 zoneid_t zoneid = ira->ira_zoneid;
13474 uint_t in_ifindex;
13475 ipaddr_t dst, src;
13476
13477 dst = ipha->ipha_dst;
13478 src = ipha->ipha_src;
13479
13480 /*
13481 * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13482 * unicast, broadcast and multicast reception to
13483 * conn_incoming_ifindex.
13484 * conn_wantpacket is called for unicast, broadcast and
13485 * multicast packets.
13486 */
13487 in_ifindex = connp->conn_incoming_ifindex;
13488
13489 /* mpathd can bind to the under IPMP interface, which we allow */
13490 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13491 if (!IS_UNDER_IPMP(ill))
13492 return (B_FALSE);
13493
13494 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13495 return (B_FALSE);
13496 }
13497
13498 if (!IPCL_ZONE_MATCH(connp, zoneid))
13499 return (B_FALSE);
13500
13501 if (!(ira->ira_flags & IRAF_MULTICAST))
13502 return (B_TRUE);
13503
13504 if (connp->conn_multi_router) {
13505 /* multicast packet and multicast router socket: send up */
13506 return (B_TRUE);
13507 }
13508
13509 if (ipha->ipha_protocol == IPPROTO_PIM ||
13510 ipha->ipha_protocol == IPPROTO_RSVP)
13511 return (B_TRUE);
13512
13513 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13514 }
13515
13516 void
conn_setqfull(conn_t * connp,boolean_t * flow_stopped)13517 conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13518 {
13519 if (IPCL_IS_NONSTR(connp)) {
13520 (*connp->conn_upcalls->su_txq_full)
13521 (connp->conn_upper_handle, B_TRUE);
13522 if (flow_stopped != NULL)
13523 *flow_stopped = B_TRUE;
13524 } else {
13525 queue_t *q = connp->conn_wq;
13526
13527 ASSERT(q != NULL);
13528 if (!(q->q_flag & QFULL)) {
13529 mutex_enter(QLOCK(q));
13530 if (!(q->q_flag & QFULL)) {
13531 /* still need to set QFULL */
13532 q->q_flag |= QFULL;
13533 /* set flow_stopped to true under QLOCK */
13534 if (flow_stopped != NULL)
13535 *flow_stopped = B_TRUE;
13536 mutex_exit(QLOCK(q));
13537 } else {
13538 /* flow_stopped is left unchanged */
13539 mutex_exit(QLOCK(q));
13540 }
13541 }
13542 }
13543 }
13544
13545 void
conn_clrqfull(conn_t * connp,boolean_t * flow_stopped)13546 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13547 {
13548 if (IPCL_IS_NONSTR(connp)) {
13549 (*connp->conn_upcalls->su_txq_full)
13550 (connp->conn_upper_handle, B_FALSE);
13551 if (flow_stopped != NULL)
13552 *flow_stopped = B_FALSE;
13553 } else {
13554 queue_t *q = connp->conn_wq;
13555
13556 ASSERT(q != NULL);
13557 if (q->q_flag & QFULL) {
13558 mutex_enter(QLOCK(q));
13559 if (q->q_flag & QFULL) {
13560 q->q_flag &= ~QFULL;
13561 /* set flow_stopped to false under QLOCK */
13562 if (flow_stopped != NULL)
13563 *flow_stopped = B_FALSE;
13564 mutex_exit(QLOCK(q));
13565 if (q->q_flag & QWANTW)
13566 qbackenable(q, 0);
13567 } else {
13568 /* flow_stopped is left unchanged */
13569 mutex_exit(QLOCK(q));
13570 }
13571 }
13572 }
13573
13574 mutex_enter(&connp->conn_lock);
13575 connp->conn_blocked = B_FALSE;
13576 mutex_exit(&connp->conn_lock);
13577 }
13578
13579 /*
13580 * Return the length in bytes of the IPv4 headers (base header, label, and
13581 * other IP options) that will be needed based on the
13582 * ip_pkt_t structure passed by the caller.
13583 *
13584 * The returned length does not include the length of the upper level
13585 * protocol (ULP) header.
13586 * The caller needs to check that the length doesn't exceed the max for IPv4.
13587 */
13588 int
ip_total_hdrs_len_v4(const ip_pkt_t * ipp)13589 ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13590 {
13591 int len;
13592
13593 len = IP_SIMPLE_HDR_LENGTH;
13594 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13595 ASSERT(ipp->ipp_label_len_v4 != 0);
13596 /* We need to round up here */
13597 len += (ipp->ipp_label_len_v4 + 3) & ~3;
13598 }
13599
13600 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13601 ASSERT(ipp->ipp_ipv4_options_len != 0);
13602 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13603 len += ipp->ipp_ipv4_options_len;
13604 }
13605 return (len);
13606 }
13607
13608 /*
13609 * All-purpose routine to build an IPv4 header with options based
13610 * on the abstract ip_pkt_t.
13611 *
13612 * The caller has to set the source and destination address as well as
13613 * ipha_length. The caller has to massage any source route and compensate
13614 * for the ULP pseudo-header checksum due to the source route.
13615 */
13616 void
ip_build_hdrs_v4(uchar_t * buf,uint_t buf_len,const ip_pkt_t * ipp,uint8_t protocol)13617 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13618 uint8_t protocol)
13619 {
13620 ipha_t *ipha = (ipha_t *)buf;
13621 uint8_t *cp;
13622
13623 /* Initialize IPv4 header */
13624 ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13625 ipha->ipha_length = 0; /* Caller will set later */
13626 ipha->ipha_ident = 0;
13627 ipha->ipha_fragment_offset_and_flags = 0;
13628 ipha->ipha_ttl = ipp->ipp_unicast_hops;
13629 ipha->ipha_protocol = protocol;
13630 ipha->ipha_hdr_checksum = 0;
13631
13632 if ((ipp->ipp_fields & IPPF_ADDR) &&
13633 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13634 ipha->ipha_src = ipp->ipp_addr_v4;
13635
13636 cp = (uint8_t *)&ipha[1];
13637 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13638 ASSERT(ipp->ipp_label_len_v4 != 0);
13639 bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4);
13640 cp += ipp->ipp_label_len_v4;
13641 /* We need to round up here */
13642 while ((uintptr_t)cp & 0x3) {
13643 *cp++ = IPOPT_NOP;
13644 }
13645 }
13646
13647 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13648 ASSERT(ipp->ipp_ipv4_options_len != 0);
13649 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13650 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13651 cp += ipp->ipp_ipv4_options_len;
13652 }
13653 ipha->ipha_version_and_hdr_length =
13654 (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13655
13656 ASSERT((int)(cp - buf) == buf_len);
13657 }
13658
13659 /* Allocate the private structure */
13660 static int
ip_priv_alloc(void ** bufp)13661 ip_priv_alloc(void **bufp)
13662 {
13663 void *buf;
13664
13665 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13666 return (ENOMEM);
13667
13668 *bufp = buf;
13669 return (0);
13670 }
13671
13672 /* Function to delete the private structure */
13673 void
ip_priv_free(void * buf)13674 ip_priv_free(void *buf)
13675 {
13676 ASSERT(buf != NULL);
13677 kmem_free(buf, sizeof (ip_priv_t));
13678 }
13679
13680 /*
13681 * The entry point for IPPF processing.
13682 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13683 * routine just returns.
13684 *
13685 * When called, ip_process generates an ipp_packet_t structure
13686 * which holds the state information for this packet and invokes the
13687 * the classifier (via ipp_packet_process). The classification, depending on
13688 * configured filters, results in a list of actions for this packet. Invoking
13689 * an action may cause the packet to be dropped, in which case we return NULL.
13690 * proc indicates the callout position for
13691 * this packet and ill is the interface this packet arrived on or will leave
13692 * on (inbound and outbound resp.).
13693 *
13694 * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13695 * on the ill corrsponding to the destination IP address.
13696 */
13697 mblk_t *
ip_process(ip_proc_t proc,mblk_t * mp,ill_t * rill,ill_t * ill)13698 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13699 {
13700 ip_priv_t *priv;
13701 ipp_action_id_t aid;
13702 int rc = 0;
13703 ipp_packet_t *pp;
13704
13705 /* If the classifier is not loaded, return */
13706 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13707 return (mp);
13708 }
13709
13710 ASSERT(mp != NULL);
13711
13712 /* Allocate the packet structure */
13713 rc = ipp_packet_alloc(&pp, "ip", aid);
13714 if (rc != 0)
13715 goto drop;
13716
13717 /* Allocate the private structure */
13718 rc = ip_priv_alloc((void **)&priv);
13719 if (rc != 0) {
13720 ipp_packet_free(pp);
13721 goto drop;
13722 }
13723 priv->proc = proc;
13724 priv->ill_index = ill_get_upper_ifindex(rill);
13725
13726 ipp_packet_set_private(pp, priv, ip_priv_free);
13727 ipp_packet_set_data(pp, mp);
13728
13729 /* Invoke the classifier */
13730 rc = ipp_packet_process(&pp);
13731 if (pp != NULL) {
13732 mp = ipp_packet_get_data(pp);
13733 ipp_packet_free(pp);
13734 if (rc != 0)
13735 goto drop;
13736 return (mp);
13737 } else {
13738 /* No mp to trace in ip_drop_input/ip_drop_output */
13739 mp = NULL;
13740 }
13741 drop:
13742 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13743 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13744 ip_drop_input("ip_process", mp, ill);
13745 } else {
13746 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13747 ip_drop_output("ip_process", mp, ill);
13748 }
13749 freemsg(mp);
13750 return (NULL);
13751 }
13752
13753 /*
13754 * Propagate a multicast group membership operation (add/drop) on
13755 * all the interfaces crossed by the related multirt routes.
13756 * The call is considered successful if the operation succeeds
13757 * on at least one interface.
13758 *
13759 * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the
13760 * multicast addresses with the ire argument being the first one.
13761 * We walk the bucket to find all the of those.
13762 *
13763 * Common to IPv4 and IPv6.
13764 */
13765 static int
ip_multirt_apply_membership(int (* fn)(conn_t *,boolean_t,const in6_addr_t *,ipaddr_t,uint_t,mcast_record_t,const in6_addr_t *),ire_t * ire,conn_t * connp,boolean_t checkonly,const in6_addr_t * v6group,mcast_record_t fmode,const in6_addr_t * v6src)13766 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
13767 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
13768 ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group,
13769 mcast_record_t fmode, const in6_addr_t *v6src)
13770 {
13771 ire_t *ire_gw;
13772 irb_t *irb;
13773 int ifindex;
13774 int error = 0;
13775 int result;
13776 ip_stack_t *ipst = ire->ire_ipst;
13777 ipaddr_t group;
13778 boolean_t isv6;
13779 int match_flags;
13780
13781 if (IN6_IS_ADDR_V4MAPPED(v6group)) {
13782 IN6_V4MAPPED_TO_IPADDR(v6group, group);
13783 isv6 = B_FALSE;
13784 } else {
13785 isv6 = B_TRUE;
13786 }
13787
13788 irb = ire->ire_bucket;
13789 ASSERT(irb != NULL);
13790
13791 result = 0;
13792 irb_refhold(irb);
13793 for (; ire != NULL; ire = ire->ire_next) {
13794 if ((ire->ire_flags & RTF_MULTIRT) == 0)
13795 continue;
13796
13797 /* We handle -ifp routes by matching on the ill if set */
13798 match_flags = MATCH_IRE_TYPE;
13799 if (ire->ire_ill != NULL)
13800 match_flags |= MATCH_IRE_ILL;
13801
13802 if (isv6) {
13803 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group))
13804 continue;
13805
13806 ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6,
13807 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13808 match_flags, 0, ipst, NULL);
13809 } else {
13810 if (ire->ire_addr != group)
13811 continue;
13812
13813 ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr,
13814 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13815 match_flags, 0, ipst, NULL);
13816 }
13817 /* No interface route exists for the gateway; skip this ire. */
13818 if (ire_gw == NULL)
13819 continue;
13820 if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
13821 ire_refrele(ire_gw);
13822 continue;
13823 }
13824 ASSERT(ire_gw->ire_ill != NULL); /* IRE_INTERFACE */
13825 ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex;
13826
13827 /*
13828 * The operation is considered a success if
13829 * it succeeds at least once on any one interface.
13830 */
13831 error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex,
13832 fmode, v6src);
13833 if (error == 0)
13834 result = CGTP_MCAST_SUCCESS;
13835
13836 ire_refrele(ire_gw);
13837 }
13838 irb_refrele(irb);
13839 /*
13840 * Consider the call as successful if we succeeded on at least
13841 * one interface. Otherwise, return the last encountered error.
13842 */
13843 return (result == CGTP_MCAST_SUCCESS ? 0 : error);
13844 }
13845
13846 /*
13847 * Return the expected CGTP hooks version number.
13848 */
13849 int
ip_cgtp_filter_supported(void)13850 ip_cgtp_filter_supported(void)
13851 {
13852 return (ip_cgtp_filter_rev);
13853 }
13854
13855 /*
13856 * CGTP hooks can be registered by invoking this function.
13857 * Checks that the version number matches.
13858 */
13859 int
ip_cgtp_filter_register(netstackid_t stackid,cgtp_filter_ops_t * ops)13860 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops)
13861 {
13862 netstack_t *ns;
13863 ip_stack_t *ipst;
13864
13865 if (ops->cfo_filter_rev != CGTP_FILTER_REV)
13866 return (ENOTSUP);
13867
13868 ns = netstack_find_by_stackid(stackid);
13869 if (ns == NULL)
13870 return (EINVAL);
13871 ipst = ns->netstack_ip;
13872 ASSERT(ipst != NULL);
13873
13874 if (ipst->ips_ip_cgtp_filter_ops != NULL) {
13875 netstack_rele(ns);
13876 return (EALREADY);
13877 }
13878
13879 ipst->ips_ip_cgtp_filter_ops = ops;
13880
13881 ill_set_inputfn_all(ipst);
13882
13883 netstack_rele(ns);
13884 return (0);
13885 }
13886
13887 /*
13888 * CGTP hooks can be unregistered by invoking this function.
13889 * Returns ENXIO if there was no registration.
13890 * Returns EBUSY if the ndd variable has not been turned off.
13891 */
13892 int
ip_cgtp_filter_unregister(netstackid_t stackid)13893 ip_cgtp_filter_unregister(netstackid_t stackid)
13894 {
13895 netstack_t *ns;
13896 ip_stack_t *ipst;
13897
13898 ns = netstack_find_by_stackid(stackid);
13899 if (ns == NULL)
13900 return (EINVAL);
13901 ipst = ns->netstack_ip;
13902 ASSERT(ipst != NULL);
13903
13904 if (ipst->ips_ip_cgtp_filter) {
13905 netstack_rele(ns);
13906 return (EBUSY);
13907 }
13908
13909 if (ipst->ips_ip_cgtp_filter_ops == NULL) {
13910 netstack_rele(ns);
13911 return (ENXIO);
13912 }
13913 ipst->ips_ip_cgtp_filter_ops = NULL;
13914
13915 ill_set_inputfn_all(ipst);
13916
13917 netstack_rele(ns);
13918 return (0);
13919 }
13920
13921 /*
13922 * Check whether there is a CGTP filter registration.
13923 * Returns non-zero if there is a registration, otherwise returns zero.
13924 * Note: returns zero if bad stackid.
13925 */
13926 int
ip_cgtp_filter_is_registered(netstackid_t stackid)13927 ip_cgtp_filter_is_registered(netstackid_t stackid)
13928 {
13929 netstack_t *ns;
13930 ip_stack_t *ipst;
13931 int ret;
13932
13933 ns = netstack_find_by_stackid(stackid);
13934 if (ns == NULL)
13935 return (0);
13936 ipst = ns->netstack_ip;
13937 ASSERT(ipst != NULL);
13938
13939 if (ipst->ips_ip_cgtp_filter_ops != NULL)
13940 ret = 1;
13941 else
13942 ret = 0;
13943
13944 netstack_rele(ns);
13945 return (ret);
13946 }
13947
13948 static int
ip_squeue_switch(int val)13949 ip_squeue_switch(int val)
13950 {
13951 int rval;
13952
13953 switch (val) {
13954 case IP_SQUEUE_ENTER_NODRAIN:
13955 rval = SQ_NODRAIN;
13956 break;
13957 case IP_SQUEUE_ENTER:
13958 rval = SQ_PROCESS;
13959 break;
13960 case IP_SQUEUE_FILL:
13961 default:
13962 rval = SQ_FILL;
13963 break;
13964 }
13965 return (rval);
13966 }
13967
13968 static void *
ip_kstat2_init(netstackid_t stackid,ip_stat_t * ip_statisticsp)13969 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13970 {
13971 kstat_t *ksp;
13972
13973 ip_stat_t template = {
13974 { "ip_udp_fannorm", KSTAT_DATA_UINT64 },
13975 { "ip_udp_fanmb", KSTAT_DATA_UINT64 },
13976 { "ip_recv_pullup", KSTAT_DATA_UINT64 },
13977 { "ip_db_ref", KSTAT_DATA_UINT64 },
13978 { "ip_notaligned", KSTAT_DATA_UINT64 },
13979 { "ip_multimblk", KSTAT_DATA_UINT64 },
13980 { "ip_opt", KSTAT_DATA_UINT64 },
13981 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 },
13982 { "ip_conn_flputbq", KSTAT_DATA_UINT64 },
13983 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 },
13984 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 },
13985 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 },
13986 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 },
13987 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 },
13988 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 },
13989 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 },
13990 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 },
13991 { "ip_nce_mcast_reclaim_calls", KSTAT_DATA_UINT64 },
13992 { "ip_nce_mcast_reclaim_deleted", KSTAT_DATA_UINT64 },
13993 { "ip_nce_mcast_reclaim_tqfail", KSTAT_DATA_UINT64 },
13994 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 },
13995 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 },
13996 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13997 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13998 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13999 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
14000 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
14001 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
14002 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 },
14003 { "conn_in_recvopts", KSTAT_DATA_UINT64 },
14004 { "conn_in_recvif", KSTAT_DATA_UINT64 },
14005 { "conn_in_recvslla", KSTAT_DATA_UINT64 },
14006 { "conn_in_recvucred", KSTAT_DATA_UINT64 },
14007 { "conn_in_recvttl", KSTAT_DATA_UINT64 },
14008 { "conn_in_recvtos", KSTAT_DATA_UINT64 },
14009 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 },
14010 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 },
14011 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 },
14012 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 },
14013 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 },
14014 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 },
14015 { "conn_in_recvtclass", KSTAT_DATA_UINT64 },
14016 { "conn_in_timestamp", KSTAT_DATA_UINT64 },
14017 };
14018
14019 ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
14020 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
14021 KSTAT_FLAG_VIRTUAL, stackid);
14022
14023 if (ksp == NULL)
14024 return (NULL);
14025
14026 bcopy(&template, ip_statisticsp, sizeof (template));
14027 ksp->ks_data = (void *)ip_statisticsp;
14028 ksp->ks_private = (void *)(uintptr_t)stackid;
14029
14030 kstat_install(ksp);
14031 return (ksp);
14032 }
14033
14034 static void
ip_kstat2_fini(netstackid_t stackid,kstat_t * ksp)14035 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
14036 {
14037 if (ksp != NULL) {
14038 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14039 kstat_delete_netstack(ksp, stackid);
14040 }
14041 }
14042
14043 static void *
ip_kstat_init(netstackid_t stackid,ip_stack_t * ipst)14044 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
14045 {
14046 kstat_t *ksp;
14047
14048 ip_named_kstat_t template = {
14049 { "forwarding", KSTAT_DATA_UINT32, 0 },
14050 { "defaultTTL", KSTAT_DATA_UINT32, 0 },
14051 { "inReceives", KSTAT_DATA_UINT64, 0 },
14052 { "inHdrErrors", KSTAT_DATA_UINT32, 0 },
14053 { "inAddrErrors", KSTAT_DATA_UINT32, 0 },
14054 { "forwDatagrams", KSTAT_DATA_UINT64, 0 },
14055 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 },
14056 { "inDiscards", KSTAT_DATA_UINT32, 0 },
14057 { "inDelivers", KSTAT_DATA_UINT64, 0 },
14058 { "outRequests", KSTAT_DATA_UINT64, 0 },
14059 { "outDiscards", KSTAT_DATA_UINT32, 0 },
14060 { "outNoRoutes", KSTAT_DATA_UINT32, 0 },
14061 { "reasmTimeout", KSTAT_DATA_UINT32, 0 },
14062 { "reasmReqds", KSTAT_DATA_UINT32, 0 },
14063 { "reasmOKs", KSTAT_DATA_UINT32, 0 },
14064 { "reasmFails", KSTAT_DATA_UINT32, 0 },
14065 { "fragOKs", KSTAT_DATA_UINT32, 0 },
14066 { "fragFails", KSTAT_DATA_UINT32, 0 },
14067 { "fragCreates", KSTAT_DATA_UINT32, 0 },
14068 { "addrEntrySize", KSTAT_DATA_INT32, 0 },
14069 { "routeEntrySize", KSTAT_DATA_INT32, 0 },
14070 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 },
14071 { "routingDiscards", KSTAT_DATA_UINT32, 0 },
14072 { "inErrs", KSTAT_DATA_UINT32, 0 },
14073 { "noPorts", KSTAT_DATA_UINT32, 0 },
14074 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
14075 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 },
14076 { "reasmPartDups", KSTAT_DATA_UINT32, 0 },
14077 { "forwProhibits", KSTAT_DATA_UINT32, 0 },
14078 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 },
14079 { "udpInOverflows", KSTAT_DATA_UINT32, 0 },
14080 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 },
14081 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 },
14082 { "ipsecInFailed", KSTAT_DATA_INT32, 0 },
14083 { "memberEntrySize", KSTAT_DATA_INT32, 0 },
14084 { "inIPv6", KSTAT_DATA_UINT32, 0 },
14085 { "outIPv6", KSTAT_DATA_UINT32, 0 },
14086 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 },
14087 };
14088
14089 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
14090 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
14091 if (ksp == NULL || ksp->ks_data == NULL)
14092 return (NULL);
14093
14094 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
14095 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
14096 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14097 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
14098 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
14099
14100 template.netToMediaEntrySize.value.i32 =
14101 sizeof (mib2_ipNetToMediaEntry_t);
14102
14103 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
14104
14105 bcopy(&template, ksp->ks_data, sizeof (template));
14106 ksp->ks_update = ip_kstat_update;
14107 ksp->ks_private = (void *)(uintptr_t)stackid;
14108
14109 kstat_install(ksp);
14110 return (ksp);
14111 }
14112
14113 static void
ip_kstat_fini(netstackid_t stackid,kstat_t * ksp)14114 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14115 {
14116 if (ksp != NULL) {
14117 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14118 kstat_delete_netstack(ksp, stackid);
14119 }
14120 }
14121
14122 static int
ip_kstat_update(kstat_t * kp,int rw)14123 ip_kstat_update(kstat_t *kp, int rw)
14124 {
14125 ip_named_kstat_t *ipkp;
14126 mib2_ipIfStatsEntry_t ipmib;
14127 ill_walk_context_t ctx;
14128 ill_t *ill;
14129 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14130 netstack_t *ns;
14131 ip_stack_t *ipst;
14132
14133 if (kp->ks_data == NULL)
14134 return (EIO);
14135
14136 if (rw == KSTAT_WRITE)
14137 return (EACCES);
14138
14139 ns = netstack_find_by_stackid(stackid);
14140 if (ns == NULL)
14141 return (-1);
14142 ipst = ns->netstack_ip;
14143 if (ipst == NULL) {
14144 netstack_rele(ns);
14145 return (-1);
14146 }
14147 ipkp = (ip_named_kstat_t *)kp->ks_data;
14148
14149 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
14150 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14151 ill = ILL_START_WALK_V4(&ctx, ipst);
14152 for (; ill != NULL; ill = ill_next(&ctx, ill))
14153 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
14154 rw_exit(&ipst->ips_ill_g_lock);
14155
14156 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding;
14157 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL;
14158 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives;
14159 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors;
14160 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors;
14161 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
14162 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos;
14163 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards;
14164 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers;
14165 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests;
14166 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards;
14167 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes;
14168 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14169 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds;
14170 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs;
14171 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails;
14172 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs;
14173 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails;
14174 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates;
14175
14176 ipkp->routingDiscards.value.ui32 = 0;
14177 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs;
14178 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts;
14179 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs;
14180 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates;
14181 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups;
14182 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits;
14183 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs;
14184 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows;
14185 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows;
14186 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded;
14187 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed;
14188
14189 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion;
14190 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion;
14191 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
14192
14193 netstack_rele(ns);
14194
14195 return (0);
14196 }
14197
14198 static void *
icmp_kstat_init(netstackid_t stackid)14199 icmp_kstat_init(netstackid_t stackid)
14200 {
14201 kstat_t *ksp;
14202
14203 icmp_named_kstat_t template = {
14204 { "inMsgs", KSTAT_DATA_UINT32 },
14205 { "inErrors", KSTAT_DATA_UINT32 },
14206 { "inDestUnreachs", KSTAT_DATA_UINT32 },
14207 { "inTimeExcds", KSTAT_DATA_UINT32 },
14208 { "inParmProbs", KSTAT_DATA_UINT32 },
14209 { "inSrcQuenchs", KSTAT_DATA_UINT32 },
14210 { "inRedirects", KSTAT_DATA_UINT32 },
14211 { "inEchos", KSTAT_DATA_UINT32 },
14212 { "inEchoReps", KSTAT_DATA_UINT32 },
14213 { "inTimestamps", KSTAT_DATA_UINT32 },
14214 { "inTimestampReps", KSTAT_DATA_UINT32 },
14215 { "inAddrMasks", KSTAT_DATA_UINT32 },
14216 { "inAddrMaskReps", KSTAT_DATA_UINT32 },
14217 { "outMsgs", KSTAT_DATA_UINT32 },
14218 { "outErrors", KSTAT_DATA_UINT32 },
14219 { "outDestUnreachs", KSTAT_DATA_UINT32 },
14220 { "outTimeExcds", KSTAT_DATA_UINT32 },
14221 { "outParmProbs", KSTAT_DATA_UINT32 },
14222 { "outSrcQuenchs", KSTAT_DATA_UINT32 },
14223 { "outRedirects", KSTAT_DATA_UINT32 },
14224 { "outEchos", KSTAT_DATA_UINT32 },
14225 { "outEchoReps", KSTAT_DATA_UINT32 },
14226 { "outTimestamps", KSTAT_DATA_UINT32 },
14227 { "outTimestampReps", KSTAT_DATA_UINT32 },
14228 { "outAddrMasks", KSTAT_DATA_UINT32 },
14229 { "outAddrMaskReps", KSTAT_DATA_UINT32 },
14230 { "inChksumErrs", KSTAT_DATA_UINT32 },
14231 { "inUnknowns", KSTAT_DATA_UINT32 },
14232 { "inFragNeeded", KSTAT_DATA_UINT32 },
14233 { "outFragNeeded", KSTAT_DATA_UINT32 },
14234 { "outDrops", KSTAT_DATA_UINT32 },
14235 { "inOverFlows", KSTAT_DATA_UINT32 },
14236 { "inBadRedirects", KSTAT_DATA_UINT32 },
14237 };
14238
14239 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
14240 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
14241 if (ksp == NULL || ksp->ks_data == NULL)
14242 return (NULL);
14243
14244 bcopy(&template, ksp->ks_data, sizeof (template));
14245
14246 ksp->ks_update = icmp_kstat_update;
14247 ksp->ks_private = (void *)(uintptr_t)stackid;
14248
14249 kstat_install(ksp);
14250 return (ksp);
14251 }
14252
14253 static void
icmp_kstat_fini(netstackid_t stackid,kstat_t * ksp)14254 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14255 {
14256 if (ksp != NULL) {
14257 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14258 kstat_delete_netstack(ksp, stackid);
14259 }
14260 }
14261
14262 static int
icmp_kstat_update(kstat_t * kp,int rw)14263 icmp_kstat_update(kstat_t *kp, int rw)
14264 {
14265 icmp_named_kstat_t *icmpkp;
14266 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14267 netstack_t *ns;
14268 ip_stack_t *ipst;
14269
14270 if (kp->ks_data == NULL)
14271 return (EIO);
14272
14273 if (rw == KSTAT_WRITE)
14274 return (EACCES);
14275
14276 ns = netstack_find_by_stackid(stackid);
14277 if (ns == NULL)
14278 return (-1);
14279 ipst = ns->netstack_ip;
14280 if (ipst == NULL) {
14281 netstack_rele(ns);
14282 return (-1);
14283 }
14284 icmpkp = (icmp_named_kstat_t *)kp->ks_data;
14285
14286 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs;
14287 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors;
14288 icmpkp->inDestUnreachs.value.ui32 =
14289 ipst->ips_icmp_mib.icmpInDestUnreachs;
14290 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds;
14291 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs;
14292 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs;
14293 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects;
14294 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos;
14295 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps;
14296 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps;
14297 icmpkp->inTimestampReps.value.ui32 =
14298 ipst->ips_icmp_mib.icmpInTimestampReps;
14299 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks;
14300 icmpkp->inAddrMaskReps.value.ui32 =
14301 ipst->ips_icmp_mib.icmpInAddrMaskReps;
14302 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs;
14303 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors;
14304 icmpkp->outDestUnreachs.value.ui32 =
14305 ipst->ips_icmp_mib.icmpOutDestUnreachs;
14306 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds;
14307 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs;
14308 icmpkp->outSrcQuenchs.value.ui32 =
14309 ipst->ips_icmp_mib.icmpOutSrcQuenchs;
14310 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects;
14311 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos;
14312 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps;
14313 icmpkp->outTimestamps.value.ui32 =
14314 ipst->ips_icmp_mib.icmpOutTimestamps;
14315 icmpkp->outTimestampReps.value.ui32 =
14316 ipst->ips_icmp_mib.icmpOutTimestampReps;
14317 icmpkp->outAddrMasks.value.ui32 =
14318 ipst->ips_icmp_mib.icmpOutAddrMasks;
14319 icmpkp->outAddrMaskReps.value.ui32 =
14320 ipst->ips_icmp_mib.icmpOutAddrMaskReps;
14321 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs;
14322 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns;
14323 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded;
14324 icmpkp->outFragNeeded.value.ui32 =
14325 ipst->ips_icmp_mib.icmpOutFragNeeded;
14326 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops;
14327 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows;
14328 icmpkp->inBadRedirects.value.ui32 =
14329 ipst->ips_icmp_mib.icmpInBadRedirects;
14330
14331 netstack_rele(ns);
14332 return (0);
14333 }
14334
14335 /*
14336 * This is the fanout function for raw socket opened for SCTP. Note
14337 * that it is called after SCTP checks that there is no socket which
14338 * wants a packet. Then before SCTP handles this out of the blue packet,
14339 * this function is called to see if there is any raw socket for SCTP.
14340 * If there is and it is bound to the correct address, the packet will
14341 * be sent to that socket. Note that only one raw socket can be bound to
14342 * a port. This is assured in ipcl_sctp_hash_insert();
14343 */
14344 void
ip_fanout_sctp_raw(mblk_t * mp,ipha_t * ipha,ip6_t * ip6h,uint32_t ports,ip_recv_attr_t * ira)14345 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
14346 ip_recv_attr_t *ira)
14347 {
14348 conn_t *connp;
14349 queue_t *rq;
14350 boolean_t secure;
14351 ill_t *ill = ira->ira_ill;
14352 ip_stack_t *ipst = ill->ill_ipst;
14353 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
14354 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp;
14355 iaflags_t iraflags = ira->ira_flags;
14356 ill_t *rill = ira->ira_rill;
14357
14358 secure = iraflags & IRAF_IPSEC_SECURE;
14359
14360 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
14361 ira, ipst);
14362 if (connp == NULL) {
14363 /*
14364 * Although raw sctp is not summed, OOB chunks must be.
14365 * Drop the packet here if the sctp checksum failed.
14366 */
14367 if (iraflags & IRAF_SCTP_CSUM_ERR) {
14368 SCTPS_BUMP_MIB(sctps, sctpChecksumError);
14369 freemsg(mp);
14370 return;
14371 }
14372 ira->ira_ill = ira->ira_rill = NULL;
14373 sctp_ootb_input(mp, ira, ipst);
14374 ira->ira_ill = ill;
14375 ira->ira_rill = rill;
14376 return;
14377 }
14378 rq = connp->conn_rq;
14379 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
14380 CONN_DEC_REF(connp);
14381 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
14382 freemsg(mp);
14383 return;
14384 }
14385 if (((iraflags & IRAF_IS_IPV4) ?
14386 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
14387 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
14388 secure) {
14389 mp = ipsec_check_inbound_policy(mp, connp, ipha,
14390 ip6h, ira);
14391 if (mp == NULL) {
14392 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
14393 /* Note that mp is NULL */
14394 ip_drop_input("ipIfStatsInDiscards", mp, ill);
14395 CONN_DEC_REF(connp);
14396 return;
14397 }
14398 }
14399
14400 if (iraflags & IRAF_ICMP_ERROR) {
14401 (connp->conn_recvicmp)(connp, mp, NULL, ira);
14402 } else {
14403 ill_t *rill = ira->ira_rill;
14404
14405 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
14406 /* This is the SOCK_RAW, IPPROTO_SCTP case. */
14407 ira->ira_ill = ira->ira_rill = NULL;
14408 (connp->conn_recv)(connp, mp, NULL, ira);
14409 ira->ira_ill = ill;
14410 ira->ira_rill = rill;
14411 }
14412 CONN_DEC_REF(connp);
14413 }
14414
14415 /*
14416 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
14417 * header before the ip payload.
14418 */
14419 static void
ip_xmit_flowctl_drop(ill_t * ill,mblk_t * mp,boolean_t is_fp_mp,int fp_mp_len)14420 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
14421 {
14422 int len = (mp->b_wptr - mp->b_rptr);
14423 mblk_t *ip_mp;
14424
14425 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14426 if (is_fp_mp || len != fp_mp_len) {
14427 if (len > fp_mp_len) {
14428 /*
14429 * fastpath header and ip header in the first mblk
14430 */
14431 mp->b_rptr += fp_mp_len;
14432 } else {
14433 /*
14434 * ip_xmit_attach_llhdr had to prepend an mblk to
14435 * attach the fastpath header before ip header.
14436 */
14437 ip_mp = mp->b_cont;
14438 freeb(mp);
14439 mp = ip_mp;
14440 mp->b_rptr += (fp_mp_len - len);
14441 }
14442 } else {
14443 ip_mp = mp->b_cont;
14444 freeb(mp);
14445 mp = ip_mp;
14446 }
14447 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
14448 freemsg(mp);
14449 }
14450
14451 /*
14452 * Normal post fragmentation function.
14453 *
14454 * Send a packet using the passed in nce. This handles both IPv4 and IPv6
14455 * using the same state machine.
14456 *
14457 * We return an error on failure. In particular we return EWOULDBLOCK
14458 * when the driver flow controls. In that case this ensures that ip_wsrv runs
14459 * (currently by canputnext failure resulting in backenabling from GLD.)
14460 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
14461 * indication that they can flow control until ip_wsrv() tells then to restart.
14462 *
14463 * If the nce passed by caller is incomplete, this function
14464 * queues the packet and if necessary, sends ARP request and bails.
14465 * If the Neighbor Cache passed is fully resolved, we simply prepend
14466 * the link-layer header to the packet, do ipsec hw acceleration
14467 * work if necessary, and send the packet out on the wire.
14468 */
14469 /* ARGSUSED6 */
14470 int
ip_xmit(mblk_t * mp,nce_t * nce,iaflags_t ixaflags,uint_t pkt_len,uint32_t xmit_hint,zoneid_t szone,zoneid_t nolzid,uintptr_t * ixacookie)14471 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
14472 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
14473 {
14474 queue_t *wq;
14475 ill_t *ill = nce->nce_ill;
14476 ip_stack_t *ipst = ill->ill_ipst;
14477 uint64_t delta;
14478 boolean_t isv6 = ill->ill_isv6;
14479 boolean_t fp_mp;
14480 ncec_t *ncec = nce->nce_common;
14481 int64_t now = LBOLT_FASTPATH64;
14482 boolean_t is_probe;
14483
14484 DTRACE_PROBE1(ip__xmit, nce_t *, nce);
14485
14486 ASSERT(mp != NULL);
14487 ASSERT(mp->b_datap->db_type == M_DATA);
14488 ASSERT(pkt_len == msgdsize(mp));
14489
14490 /*
14491 * If we have already been here and are coming back after ARP/ND.
14492 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
14493 * in that case since they have seen the packet when it came here
14494 * the first time.
14495 */
14496 if (ixaflags & IXAF_NO_TRACE)
14497 goto sendit;
14498
14499 if (ixaflags & IXAF_IS_IPV4) {
14500 ipha_t *ipha = (ipha_t *)mp->b_rptr;
14501
14502 ASSERT(!isv6);
14503 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
14504 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
14505 !(ixaflags & IXAF_NO_PFHOOK)) {
14506 int error;
14507
14508 FW_HOOKS(ipst->ips_ip4_physical_out_event,
14509 ipst->ips_ipv4firewall_physical_out,
14510 NULL, ill, ipha, mp, mp, 0, ipst, error);
14511 DTRACE_PROBE1(ip4__physical__out__end,
14512 mblk_t *, mp);
14513 if (mp == NULL)
14514 return (error);
14515
14516 /* The length could have changed */
14517 pkt_len = msgdsize(mp);
14518 }
14519 if (ipst->ips_ip4_observe.he_interested) {
14520 /*
14521 * Note that for TX the zoneid is the sending
14522 * zone, whether or not MLP is in play.
14523 * Since the szone argument is the IP zoneid (i.e.,
14524 * zero for exclusive-IP zones) and ipobs wants
14525 * the system zoneid, we map it here.
14526 */
14527 szone = IP_REAL_ZONEID(szone, ipst);
14528
14529 /*
14530 * On the outbound path the destination zone will be
14531 * unknown as we're sending this packet out on the
14532 * wire.
14533 */
14534 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14535 ill, ipst);
14536 }
14537 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14538 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill,
14539 ipha_t *, ipha, ip6_t *, NULL, int, 0);
14540 } else {
14541 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
14542
14543 ASSERT(isv6);
14544 ASSERT(pkt_len ==
14545 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
14546 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
14547 !(ixaflags & IXAF_NO_PFHOOK)) {
14548 int error;
14549
14550 FW_HOOKS6(ipst->ips_ip6_physical_out_event,
14551 ipst->ips_ipv6firewall_physical_out,
14552 NULL, ill, ip6h, mp, mp, 0, ipst, error);
14553 DTRACE_PROBE1(ip6__physical__out__end,
14554 mblk_t *, mp);
14555 if (mp == NULL)
14556 return (error);
14557
14558 /* The length could have changed */
14559 pkt_len = msgdsize(mp);
14560 }
14561 if (ipst->ips_ip6_observe.he_interested) {
14562 /* See above */
14563 szone = IP_REAL_ZONEID(szone, ipst);
14564
14565 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14566 ill, ipst);
14567 }
14568 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14569 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill,
14570 ipha_t *, NULL, ip6_t *, ip6h, int, 0);
14571 }
14572
14573 sendit:
14574 /*
14575 * We check the state without a lock because the state can never
14576 * move "backwards" to initial or incomplete.
14577 */
14578 switch (ncec->ncec_state) {
14579 case ND_REACHABLE:
14580 case ND_STALE:
14581 case ND_DELAY:
14582 case ND_PROBE:
14583 mp = ip_xmit_attach_llhdr(mp, nce);
14584 if (mp == NULL) {
14585 /*
14586 * ip_xmit_attach_llhdr has increased
14587 * ipIfStatsOutDiscards and called ip_drop_output()
14588 */
14589 return (ENOBUFS);
14590 }
14591 /*
14592 * check if nce_fastpath completed and we tagged on a
14593 * copy of nce_fp_mp in ip_xmit_attach_llhdr().
14594 */
14595 fp_mp = (mp->b_datap->db_type == M_DATA);
14596
14597 if (fp_mp &&
14598 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
14599 ill_dld_direct_t *idd;
14600
14601 idd = &ill->ill_dld_capab->idc_direct;
14602 /*
14603 * Send the packet directly to DLD, where it
14604 * may be queued depending on the availability
14605 * of transmit resources at the media layer.
14606 * Return value should be taken into
14607 * account and flow control the TCP.
14608 */
14609 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14610 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14611 pkt_len);
14612
14613 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
14614 (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
14615 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
14616 } else {
14617 uintptr_t cookie;
14618
14619 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
14620 mp, (uintptr_t)xmit_hint, 0)) != 0) {
14621 if (ixacookie != NULL)
14622 *ixacookie = cookie;
14623 return (EWOULDBLOCK);
14624 }
14625 }
14626 } else {
14627 wq = ill->ill_wq;
14628
14629 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
14630 !canputnext(wq)) {
14631 if (ixacookie != NULL)
14632 *ixacookie = 0;
14633 ip_xmit_flowctl_drop(ill, mp, fp_mp,
14634 nce->nce_fp_mp != NULL ?
14635 MBLKL(nce->nce_fp_mp) : 0);
14636 return (EWOULDBLOCK);
14637 }
14638 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14639 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14640 pkt_len);
14641 putnext(wq, mp);
14642 }
14643
14644 /*
14645 * The rest of this function implements Neighbor Unreachability
14646 * detection. Determine if the ncec is eligible for NUD.
14647 */
14648 if (ncec->ncec_flags & NCE_F_NONUD)
14649 return (0);
14650
14651 ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14652
14653 /*
14654 * Check for upper layer advice
14655 */
14656 if (ixaflags & IXAF_REACH_CONF) {
14657 timeout_id_t tid;
14658
14659 /*
14660 * It should be o.k. to check the state without
14661 * a lock here, at most we lose an advice.
14662 */
14663 ncec->ncec_last = TICK_TO_MSEC(now);
14664 if (ncec->ncec_state != ND_REACHABLE) {
14665 mutex_enter(&ncec->ncec_lock);
14666 ncec->ncec_state = ND_REACHABLE;
14667 tid = ncec->ncec_timeout_id;
14668 ncec->ncec_timeout_id = 0;
14669 mutex_exit(&ncec->ncec_lock);
14670 (void) untimeout(tid);
14671 if (ip_debug > 2) {
14672 /* ip1dbg */
14673 pr_addr_dbg("ip_xmit: state"
14674 " for %s changed to"
14675 " REACHABLE\n", AF_INET6,
14676 &ncec->ncec_addr);
14677 }
14678 }
14679 return (0);
14680 }
14681
14682 delta = TICK_TO_MSEC(now) - ncec->ncec_last;
14683 ip1dbg(("ip_xmit: delta = %" PRId64
14684 " ill_reachable_time = %d \n", delta,
14685 ill->ill_reachable_time));
14686 if (delta > (uint64_t)ill->ill_reachable_time) {
14687 mutex_enter(&ncec->ncec_lock);
14688 switch (ncec->ncec_state) {
14689 case ND_REACHABLE:
14690 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14691 /* FALLTHROUGH */
14692 case ND_STALE:
14693 /*
14694 * ND_REACHABLE is identical to
14695 * ND_STALE in this specific case. If
14696 * reachable time has expired for this
14697 * neighbor (delta is greater than
14698 * reachable time), conceptually, the
14699 * neighbor cache is no longer in
14700 * REACHABLE state, but already in
14701 * STALE state. So the correct
14702 * transition here is to ND_DELAY.
14703 */
14704 ncec->ncec_state = ND_DELAY;
14705 mutex_exit(&ncec->ncec_lock);
14706 nce_restart_timer(ncec,
14707 ipst->ips_delay_first_probe_time);
14708 if (ip_debug > 3) {
14709 /* ip2dbg */
14710 pr_addr_dbg("ip_xmit: state"
14711 " for %s changed to"
14712 " DELAY\n", AF_INET6,
14713 &ncec->ncec_addr);
14714 }
14715 break;
14716 case ND_DELAY:
14717 case ND_PROBE:
14718 mutex_exit(&ncec->ncec_lock);
14719 /* Timers have already started */
14720 break;
14721 case ND_UNREACHABLE:
14722 /*
14723 * nce_timer has detected that this ncec
14724 * is unreachable and initiated deleting
14725 * this ncec.
14726 * This is a harmless race where we found the
14727 * ncec before it was deleted and have
14728 * just sent out a packet using this
14729 * unreachable ncec.
14730 */
14731 mutex_exit(&ncec->ncec_lock);
14732 break;
14733 default:
14734 ASSERT(0);
14735 mutex_exit(&ncec->ncec_lock);
14736 }
14737 }
14738 return (0);
14739
14740 case ND_INCOMPLETE:
14741 /*
14742 * the state could have changed since we didn't hold the lock.
14743 * Re-verify state under lock.
14744 */
14745 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14746 mutex_enter(&ncec->ncec_lock);
14747 if (NCE_ISREACHABLE(ncec)) {
14748 mutex_exit(&ncec->ncec_lock);
14749 goto sendit;
14750 }
14751 /* queue the packet */
14752 nce_queue_mp(ncec, mp, is_probe);
14753 mutex_exit(&ncec->ncec_lock);
14754 DTRACE_PROBE2(ip__xmit__incomplete,
14755 (ncec_t *), ncec, (mblk_t *), mp);
14756 return (0);
14757
14758 case ND_INITIAL:
14759 /*
14760 * State could have changed since we didn't hold the lock, so
14761 * re-verify state.
14762 */
14763 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14764 mutex_enter(&ncec->ncec_lock);
14765 if (NCE_ISREACHABLE(ncec)) {
14766 mutex_exit(&ncec->ncec_lock);
14767 goto sendit;
14768 }
14769 nce_queue_mp(ncec, mp, is_probe);
14770 if (ncec->ncec_state == ND_INITIAL) {
14771 ncec->ncec_state = ND_INCOMPLETE;
14772 mutex_exit(&ncec->ncec_lock);
14773 /*
14774 * figure out the source we want to use
14775 * and resolve it.
14776 */
14777 ip_ndp_resolve(ncec);
14778 } else {
14779 mutex_exit(&ncec->ncec_lock);
14780 }
14781 return (0);
14782
14783 case ND_UNREACHABLE:
14784 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14785 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14786 mp, ill);
14787 freemsg(mp);
14788 return (0);
14789
14790 default:
14791 ASSERT(0);
14792 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14793 ip_drop_output("ipIfStatsOutDiscards - ND_other",
14794 mp, ill);
14795 freemsg(mp);
14796 return (ENETUNREACH);
14797 }
14798 }
14799
14800 /*
14801 * Return B_TRUE if the buffers differ in length or content.
14802 * This is used for comparing extension header buffers.
14803 * Note that an extension header would be declared different
14804 * even if all that changed was the next header value in that header i.e.
14805 * what really changed is the next extension header.
14806 */
14807 boolean_t
ip_cmpbuf(const void * abuf,uint_t alen,boolean_t b_valid,const void * bbuf,uint_t blen)14808 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14809 uint_t blen)
14810 {
14811 if (!b_valid)
14812 blen = 0;
14813
14814 if (alen != blen)
14815 return (B_TRUE);
14816 if (alen == 0)
14817 return (B_FALSE); /* Both zero length */
14818 return (bcmp(abuf, bbuf, alen));
14819 }
14820
14821 /*
14822 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14823 * Return B_FALSE if memory allocation fails - don't change any state!
14824 */
14825 boolean_t
ip_allocbuf(void ** dstp,uint_t * dstlenp,boolean_t src_valid,const void * src,uint_t srclen)14826 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14827 const void *src, uint_t srclen)
14828 {
14829 void *dst;
14830
14831 if (!src_valid)
14832 srclen = 0;
14833
14834 ASSERT(*dstlenp == 0);
14835 if (src != NULL && srclen != 0) {
14836 dst = mi_alloc(srclen, BPRI_MED);
14837 if (dst == NULL)
14838 return (B_FALSE);
14839 } else {
14840 dst = NULL;
14841 }
14842 if (*dstp != NULL)
14843 mi_free(*dstp);
14844 *dstp = dst;
14845 *dstlenp = dst == NULL ? 0 : srclen;
14846 return (B_TRUE);
14847 }
14848
14849 /*
14850 * Replace what is in *dst, *dstlen with the source.
14851 * Assumes ip_allocbuf has already been called.
14852 */
14853 void
ip_savebuf(void ** dstp,uint_t * dstlenp,boolean_t src_valid,const void * src,uint_t srclen)14854 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14855 const void *src, uint_t srclen)
14856 {
14857 if (!src_valid)
14858 srclen = 0;
14859
14860 ASSERT(*dstlenp == srclen);
14861 if (src != NULL && srclen != 0)
14862 bcopy(src, *dstp, srclen);
14863 }
14864
14865 /*
14866 * Free the storage pointed to by the members of an ip_pkt_t.
14867 */
14868 void
ip_pkt_free(ip_pkt_t * ipp)14869 ip_pkt_free(ip_pkt_t *ipp)
14870 {
14871 uint_t fields = ipp->ipp_fields;
14872
14873 if (fields & IPPF_HOPOPTS) {
14874 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14875 ipp->ipp_hopopts = NULL;
14876 ipp->ipp_hopoptslen = 0;
14877 }
14878 if (fields & IPPF_RTHDRDSTOPTS) {
14879 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14880 ipp->ipp_rthdrdstopts = NULL;
14881 ipp->ipp_rthdrdstoptslen = 0;
14882 }
14883 if (fields & IPPF_DSTOPTS) {
14884 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14885 ipp->ipp_dstopts = NULL;
14886 ipp->ipp_dstoptslen = 0;
14887 }
14888 if (fields & IPPF_RTHDR) {
14889 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14890 ipp->ipp_rthdr = NULL;
14891 ipp->ipp_rthdrlen = 0;
14892 }
14893 if (fields & IPPF_IPV4_OPTIONS) {
14894 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14895 ipp->ipp_ipv4_options = NULL;
14896 ipp->ipp_ipv4_options_len = 0;
14897 }
14898 if (fields & IPPF_LABEL_V4) {
14899 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
14900 ipp->ipp_label_v4 = NULL;
14901 ipp->ipp_label_len_v4 = 0;
14902 }
14903 if (fields & IPPF_LABEL_V6) {
14904 kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6);
14905 ipp->ipp_label_v6 = NULL;
14906 ipp->ipp_label_len_v6 = 0;
14907 }
14908 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14909 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14910 }
14911
14912 /*
14913 * Copy from src to dst and allocate as needed.
14914 * Returns zero or ENOMEM.
14915 *
14916 * The caller must initialize dst to zero.
14917 */
14918 int
ip_pkt_copy(ip_pkt_t * src,ip_pkt_t * dst,int kmflag)14919 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14920 {
14921 uint_t fields = src->ipp_fields;
14922
14923 /* Start with fields that don't require memory allocation */
14924 dst->ipp_fields = fields &
14925 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14926 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14927
14928 dst->ipp_addr = src->ipp_addr;
14929 dst->ipp_unicast_hops = src->ipp_unicast_hops;
14930 dst->ipp_hoplimit = src->ipp_hoplimit;
14931 dst->ipp_tclass = src->ipp_tclass;
14932 dst->ipp_type_of_service = src->ipp_type_of_service;
14933
14934 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14935 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6)))
14936 return (0);
14937
14938 if (fields & IPPF_HOPOPTS) {
14939 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14940 if (dst->ipp_hopopts == NULL) {
14941 ip_pkt_free(dst);
14942 return (ENOMEM);
14943 }
14944 dst->ipp_fields |= IPPF_HOPOPTS;
14945 bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14946 src->ipp_hopoptslen);
14947 dst->ipp_hopoptslen = src->ipp_hopoptslen;
14948 }
14949 if (fields & IPPF_RTHDRDSTOPTS) {
14950 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14951 kmflag);
14952 if (dst->ipp_rthdrdstopts == NULL) {
14953 ip_pkt_free(dst);
14954 return (ENOMEM);
14955 }
14956 dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14957 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14958 src->ipp_rthdrdstoptslen);
14959 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14960 }
14961 if (fields & IPPF_DSTOPTS) {
14962 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14963 if (dst->ipp_dstopts == NULL) {
14964 ip_pkt_free(dst);
14965 return (ENOMEM);
14966 }
14967 dst->ipp_fields |= IPPF_DSTOPTS;
14968 bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14969 src->ipp_dstoptslen);
14970 dst->ipp_dstoptslen = src->ipp_dstoptslen;
14971 }
14972 if (fields & IPPF_RTHDR) {
14973 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14974 if (dst->ipp_rthdr == NULL) {
14975 ip_pkt_free(dst);
14976 return (ENOMEM);
14977 }
14978 dst->ipp_fields |= IPPF_RTHDR;
14979 bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14980 src->ipp_rthdrlen);
14981 dst->ipp_rthdrlen = src->ipp_rthdrlen;
14982 }
14983 if (fields & IPPF_IPV4_OPTIONS) {
14984 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14985 kmflag);
14986 if (dst->ipp_ipv4_options == NULL) {
14987 ip_pkt_free(dst);
14988 return (ENOMEM);
14989 }
14990 dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14991 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14992 src->ipp_ipv4_options_len);
14993 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14994 }
14995 if (fields & IPPF_LABEL_V4) {
14996 dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag);
14997 if (dst->ipp_label_v4 == NULL) {
14998 ip_pkt_free(dst);
14999 return (ENOMEM);
15000 }
15001 dst->ipp_fields |= IPPF_LABEL_V4;
15002 bcopy(src->ipp_label_v4, dst->ipp_label_v4,
15003 src->ipp_label_len_v4);
15004 dst->ipp_label_len_v4 = src->ipp_label_len_v4;
15005 }
15006 if (fields & IPPF_LABEL_V6) {
15007 dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag);
15008 if (dst->ipp_label_v6 == NULL) {
15009 ip_pkt_free(dst);
15010 return (ENOMEM);
15011 }
15012 dst->ipp_fields |= IPPF_LABEL_V6;
15013 bcopy(src->ipp_label_v6, dst->ipp_label_v6,
15014 src->ipp_label_len_v6);
15015 dst->ipp_label_len_v6 = src->ipp_label_len_v6;
15016 }
15017 if (fields & IPPF_FRAGHDR) {
15018 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
15019 if (dst->ipp_fraghdr == NULL) {
15020 ip_pkt_free(dst);
15021 return (ENOMEM);
15022 }
15023 dst->ipp_fields |= IPPF_FRAGHDR;
15024 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
15025 src->ipp_fraghdrlen);
15026 dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
15027 }
15028 return (0);
15029 }
15030
15031 /*
15032 * Returns INADDR_ANY if no source route
15033 */
15034 ipaddr_t
ip_pkt_source_route_v4(const ip_pkt_t * ipp)15035 ip_pkt_source_route_v4(const ip_pkt_t *ipp)
15036 {
15037 ipaddr_t nexthop = INADDR_ANY;
15038 ipoptp_t opts;
15039 uchar_t *opt;
15040 uint8_t optval;
15041 uint8_t optlen;
15042 uint32_t totallen;
15043
15044 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15045 return (INADDR_ANY);
15046
15047 totallen = ipp->ipp_ipv4_options_len;
15048 if (totallen & 0x3)
15049 return (INADDR_ANY);
15050
15051 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15052 optval != IPOPT_EOL;
15053 optval = ipoptp_next(&opts)) {
15054 opt = opts.ipoptp_cur;
15055 switch (optval) {
15056 uint8_t off;
15057 case IPOPT_SSRR:
15058 case IPOPT_LSRR:
15059 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15060 break;
15061 }
15062 optlen = opts.ipoptp_len;
15063 off = opt[IPOPT_OFFSET];
15064 off--;
15065 if (optlen < IP_ADDR_LEN ||
15066 off > optlen - IP_ADDR_LEN) {
15067 /* End of source route */
15068 break;
15069 }
15070 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
15071 if (nexthop == htonl(INADDR_LOOPBACK)) {
15072 /* Ignore */
15073 nexthop = INADDR_ANY;
15074 break;
15075 }
15076 break;
15077 }
15078 }
15079 return (nexthop);
15080 }
15081
15082 /*
15083 * Reverse a source route.
15084 */
15085 void
ip_pkt_source_route_reverse_v4(ip_pkt_t * ipp)15086 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
15087 {
15088 ipaddr_t tmp;
15089 ipoptp_t opts;
15090 uchar_t *opt;
15091 uint8_t optval;
15092 uint32_t totallen;
15093
15094 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15095 return;
15096
15097 totallen = ipp->ipp_ipv4_options_len;
15098 if (totallen & 0x3)
15099 return;
15100
15101 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15102 optval != IPOPT_EOL;
15103 optval = ipoptp_next(&opts)) {
15104 uint8_t off1, off2;
15105
15106 opt = opts.ipoptp_cur;
15107 switch (optval) {
15108 case IPOPT_SSRR:
15109 case IPOPT_LSRR:
15110 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15111 break;
15112 }
15113 off1 = IPOPT_MINOFF_SR - 1;
15114 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
15115 while (off2 > off1) {
15116 bcopy(opt + off2, &tmp, IP_ADDR_LEN);
15117 bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
15118 bcopy(&tmp, opt + off2, IP_ADDR_LEN);
15119 off2 -= IP_ADDR_LEN;
15120 off1 += IP_ADDR_LEN;
15121 }
15122 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
15123 break;
15124 }
15125 }
15126 }
15127
15128 /*
15129 * Returns NULL if no routing header
15130 */
15131 in6_addr_t *
ip_pkt_source_route_v6(const ip_pkt_t * ipp)15132 ip_pkt_source_route_v6(const ip_pkt_t *ipp)
15133 {
15134 in6_addr_t *nexthop = NULL;
15135 ip6_rthdr0_t *rthdr;
15136
15137 if (!(ipp->ipp_fields & IPPF_RTHDR))
15138 return (NULL);
15139
15140 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
15141 if (rthdr->ip6r0_segleft == 0)
15142 return (NULL);
15143
15144 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
15145 return (nexthop);
15146 }
15147
15148 zoneid_t
ip_get_zoneid_v4(ipaddr_t addr,mblk_t * mp,ip_recv_attr_t * ira,zoneid_t lookup_zoneid)15149 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
15150 zoneid_t lookup_zoneid)
15151 {
15152 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15153 ire_t *ire;
15154 int ire_flags = MATCH_IRE_TYPE;
15155 zoneid_t zoneid = ALL_ZONES;
15156
15157 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15158 return (ALL_ZONES);
15159
15160 if (lookup_zoneid != ALL_ZONES)
15161 ire_flags |= MATCH_IRE_ZONEONLY;
15162 ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_LOCAL | IRE_LOOPBACK,
15163 NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15164 if (ire != NULL) {
15165 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15166 ire_refrele(ire);
15167 }
15168 return (zoneid);
15169 }
15170
15171 zoneid_t
ip_get_zoneid_v6(in6_addr_t * addr,mblk_t * mp,const ill_t * ill,ip_recv_attr_t * ira,zoneid_t lookup_zoneid)15172 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
15173 ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
15174 {
15175 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15176 ire_t *ire;
15177 int ire_flags = MATCH_IRE_TYPE;
15178 zoneid_t zoneid = ALL_ZONES;
15179
15180 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15181 return (ALL_ZONES);
15182
15183 if (IN6_IS_ADDR_LINKLOCAL(addr))
15184 ire_flags |= MATCH_IRE_ILL;
15185
15186 if (lookup_zoneid != ALL_ZONES)
15187 ire_flags |= MATCH_IRE_ZONEONLY;
15188 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15189 ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15190 if (ire != NULL) {
15191 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15192 ire_refrele(ire);
15193 }
15194 return (zoneid);
15195 }
15196
15197 /*
15198 * IP obserability hook support functions.
15199 */
15200 static void
ipobs_init(ip_stack_t * ipst)15201 ipobs_init(ip_stack_t *ipst)
15202 {
15203 netid_t id;
15204
15205 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
15206
15207 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
15208 VERIFY(ipst->ips_ip4_observe_pr != NULL);
15209
15210 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
15211 VERIFY(ipst->ips_ip6_observe_pr != NULL);
15212 }
15213
15214 static void
ipobs_fini(ip_stack_t * ipst)15215 ipobs_fini(ip_stack_t *ipst)
15216 {
15217
15218 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
15219 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
15220 }
15221
15222 /*
15223 * hook_pkt_observe_t is composed in network byte order so that the
15224 * entire mblk_t chain handed into hook_run can be used as-is.
15225 * The caveat is that use of the fields, such as the zone fields,
15226 * requires conversion into host byte order first.
15227 */
15228 void
ipobs_hook(mblk_t * mp,int htype,zoneid_t zsrc,zoneid_t zdst,const ill_t * ill,ip_stack_t * ipst)15229 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
15230 const ill_t *ill, ip_stack_t *ipst)
15231 {
15232 hook_pkt_observe_t *hdr;
15233 uint64_t grifindex;
15234 mblk_t *imp;
15235
15236 imp = allocb(sizeof (*hdr), BPRI_HI);
15237 if (imp == NULL)
15238 return;
15239
15240 hdr = (hook_pkt_observe_t *)imp->b_rptr;
15241 /*
15242 * b_wptr is set to make the apparent size of the data in the mblk_t
15243 * to exclude the pointers at the end of hook_pkt_observer_t.
15244 */
15245 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
15246 imp->b_cont = mp;
15247
15248 ASSERT(DB_TYPE(mp) == M_DATA);
15249
15250 if (IS_UNDER_IPMP(ill))
15251 grifindex = ipmp_ill_get_ipmp_ifindex(ill);
15252 else
15253 grifindex = 0;
15254
15255 hdr->hpo_version = 1;
15256 hdr->hpo_htype = htons(htype);
15257 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
15258 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
15259 hdr->hpo_grifindex = htonl(grifindex);
15260 hdr->hpo_zsrc = htonl(zsrc);
15261 hdr->hpo_zdst = htonl(zdst);
15262 hdr->hpo_pkt = imp;
15263 hdr->hpo_ctx = ipst->ips_netstack;
15264
15265 if (ill->ill_isv6) {
15266 hdr->hpo_family = AF_INET6;
15267 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
15268 ipst->ips_ipv6observing, (hook_data_t)hdr);
15269 } else {
15270 hdr->hpo_family = AF_INET;
15271 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
15272 ipst->ips_ipv4observing, (hook_data_t)hdr);
15273 }
15274
15275 imp->b_cont = NULL;
15276 freemsg(imp);
15277 }
15278
15279 /*
15280 * Utility routine that checks if `v4srcp' is a valid address on underlying
15281 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif
15282 * associated with `v4srcp' on success. NOTE: if this is not called from
15283 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
15284 * group during or after this lookup.
15285 */
15286 boolean_t
ipif_lookup_testaddr_v4(ill_t * ill,const in_addr_t * v4srcp,ipif_t ** ipifp)15287 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
15288 {
15289 ipif_t *ipif;
15290
15291 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
15292 if (ipif != NULL) {
15293 if (ipifp != NULL)
15294 *ipifp = ipif;
15295 else
15296 ipif_refrele(ipif);
15297 return (B_TRUE);
15298 }
15299
15300 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
15301 *v4srcp));
15302 return (B_FALSE);
15303 }
15304
15305 /*
15306 * Transport protocol call back function for CPU state change.
15307 */
15308 /* ARGSUSED */
15309 static int
ip_tp_cpu_update(cpu_setup_t what,int id,void * arg)15310 ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
15311 {
15312 processorid_t cpu_seqid;
15313 netstack_handle_t nh;
15314 netstack_t *ns;
15315
15316 ASSERT(MUTEX_HELD(&cpu_lock));
15317
15318 switch (what) {
15319 case CPU_CONFIG:
15320 case CPU_ON:
15321 case CPU_INIT:
15322 case CPU_CPUPART_IN:
15323 cpu_seqid = cpu[id]->cpu_seqid;
15324 netstack_next_init(&nh);
15325 while ((ns = netstack_next(&nh)) != NULL) {
15326 tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
15327 sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
15328 udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
15329 netstack_rele(ns);
15330 }
15331 netstack_next_fini(&nh);
15332 break;
15333 case CPU_UNCONFIG:
15334 case CPU_OFF:
15335 case CPU_CPUPART_OUT:
15336 /*
15337 * Nothing to do. We don't remove the per CPU stats from
15338 * the IP stack even when the CPU goes offline.
15339 */
15340 break;
15341 default:
15342 break;
15343 }
15344 return (0);
15345 }
15346