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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26 /* Copyright (c) 1990 Mentat Inc. */
27
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/dlpi.h>
31 #include <sys/stropts.h>
32 #include <sys/sysmacros.h>
33 #include <sys/strsubr.h>
34 #include <sys/strlog.h>
35 #include <sys/strsun.h>
36 #include <sys/zone.h>
37 #define _SUN_TPI_VERSION 2
38 #include <sys/tihdr.h>
39 #include <sys/xti_inet.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/debug.h>
44 #include <sys/kobj.h>
45 #include <sys/modctl.h>
46 #include <sys/atomic.h>
47 #include <sys/policy.h>
48 #include <sys/priv.h>
49
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/kmem.h>
53 #include <sys/sdt.h>
54 #include <sys/socket.h>
55 #include <sys/vtrace.h>
56 #include <sys/isa_defs.h>
57 #include <sys/mac.h>
58 #include <net/if.h>
59 #include <net/if_arp.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <net/if_dl.h>
64
65 #include <inet/common.h>
66 #include <inet/mi.h>
67 #include <inet/mib2.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/snmpcom.h>
71 #include <inet/optcom.h>
72 #include <inet/kstatcom.h>
73
74 #include <netinet/igmp_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet/icmp6.h>
77 #include <netinet/sctp.h>
78
79 #include <inet/ip.h>
80 #include <inet/ip_impl.h>
81 #include <inet/ip6.h>
82 #include <inet/ip6_asp.h>
83 #include <inet/tcp.h>
84 #include <inet/tcp_impl.h>
85 #include <inet/ip_multi.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_ire.h>
88 #include <inet/ip_ftable.h>
89 #include <inet/ip_rts.h>
90 #include <inet/ip_ndp.h>
91 #include <inet/ip_listutils.h>
92 #include <netinet/igmp.h>
93 #include <netinet/ip_mroute.h>
94 #include <inet/ipp_common.h>
95
96 #include <net/pfkeyv2.h>
97 #include <inet/ipsec_info.h>
98 #include <inet/sadb.h>
99 #include <inet/ipsec_impl.h>
100 #include <sys/iphada.h>
101 #include <inet/tun.h>
102 #include <inet/ipdrop.h>
103 #include <inet/ip_netinfo.h>
104
105 #include <sys/ethernet.h>
106 #include <net/if_types.h>
107 #include <sys/cpuvar.h>
108
109 #include <ipp/ipp.h>
110 #include <ipp/ipp_impl.h>
111 #include <ipp/ipgpc/ipgpc.h>
112
113 #include <sys/multidata.h>
114 #include <sys/pattr.h>
115
116 #include <inet/ipclassifier.h>
117 #include <inet/sctp_ip.h>
118 #include <inet/sctp/sctp_impl.h>
119 #include <inet/udp_impl.h>
120 #include <inet/rawip_impl.h>
121 #include <inet/rts_impl.h>
122 #include <sys/sunddi.h>
123
124 #include <sys/tsol/label.h>
125 #include <sys/tsol/tnet.h>
126
127 #include <rpc/pmap_prot.h>
128
129 /*
130 * Values for squeue switch:
131 * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain
132 * IP_SQUEUE_ENTER: squeue_enter
133 * IP_SQUEUE_FILL: squeue_fill
134 */
135 int ip_squeue_enter = 2; /* Setable in /etc/system */
136
137 squeue_func_t ip_input_proc;
138 #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x))
139
140 /*
141 * Setable in /etc/system
142 */
143 int ip_poll_normal_ms = 100;
144 int ip_poll_normal_ticks = 0;
145 int ip_modclose_ackwait_ms = 3000;
146
147 /*
148 * It would be nice to have these present only in DEBUG systems, but the
149 * current design of the global symbol checking logic requires them to be
150 * unconditionally present.
151 */
152 uint_t ip_thread_data; /* TSD key for debug support */
153 krwlock_t ip_thread_rwlock;
154 list_t ip_thread_list;
155
156 /*
157 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
158 */
159
160 struct listptr_s {
161 mblk_t *lp_head; /* pointer to the head of the list */
162 mblk_t *lp_tail; /* pointer to the tail of the list */
163 };
164
165 typedef struct listptr_s listptr_t;
166
167 /*
168 * This is used by ip_snmp_get_mib2_ip_route_media and
169 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
170 */
171 typedef struct iproutedata_s {
172 uint_t ird_idx;
173 listptr_t ird_route; /* ipRouteEntryTable */
174 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
175 listptr_t ird_attrs; /* ipRouteAttributeTable */
176 } iproutedata_t;
177
178 /*
179 * Cluster specific hooks. These should be NULL when booted as a non-cluster
180 */
181
182 /*
183 * Hook functions to enable cluster networking
184 * On non-clustered systems these vectors must always be NULL.
185 *
186 * Hook function to Check ip specified ip address is a shared ip address
187 * in the cluster
188 *
189 */
190 int (*cl_inet_isclusterwide)(uint8_t protocol,
191 sa_family_t addr_family, uint8_t *laddrp) = NULL;
192
193 /*
194 * Hook function to generate cluster wide ip fragment identifier
195 */
196 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family,
197 uint8_t *laddrp, uint8_t *faddrp) = NULL;
198
199 /*
200 * Synchronization notes:
201 *
202 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
203 * MT level protection given by STREAMS. IP uses a combination of its own
204 * internal serialization mechanism and standard Solaris locking techniques.
205 * The internal serialization is per phyint (no IPMP) or per IPMP group.
206 * This is used to serialize plumbing operations, IPMP operations, certain
207 * multicast operations, most set ioctls, igmp/mld timers etc.
208 *
209 * Plumbing is a long sequence of operations involving message
210 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
211 * involved in plumbing operations. A natural model is to serialize these
212 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
213 * parallel without any interference. But various set ioctls on hme0 are best
214 * serialized. However if the system uses IPMP, the operations are easier if
215 * they are serialized on a per IPMP group basis since IPMP operations
216 * happen across ill's of a group. Thus the lowest common denominator is to
217 * serialize most set ioctls, multicast join/leave operations, IPMP operations
218 * igmp/mld timer operations, and processing of DLPI control messages received
219 * from drivers on a per IPMP group basis. If the system does not employ
220 * IPMP the serialization is on a per phyint basis. This serialization is
221 * provided by the ipsq_t and primitives operating on this. Details can
222 * be found in ip_if.c above the core primitives operating on ipsq_t.
223 *
224 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
225 * Simiarly lookup of an ire by a thread also returns a refheld ire.
226 * In addition ipif's and ill's referenced by the ire are also indirectly
227 * refheld. Thus no ipif or ill can vanish nor can critical parameters like
228 * the ipif's address or netmask change as long as an ipif is refheld
229 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the
230 * address of an ipif has to go through the ipsq_t. This ensures that only
231 * 1 such exclusive operation proceeds at any time on the ipif. It then
232 * deletes all ires associated with this ipif, and waits for all refcnts
233 * associated with this ipif to come down to zero. The address is changed
234 * only after the ipif has been quiesced. Then the ipif is brought up again.
235 * More details are described above the comment in ip_sioctl_flags.
236 *
237 * Packet processing is based mostly on IREs and are fully multi-threaded
238 * using standard Solaris MT techniques.
239 *
240 * There are explicit locks in IP to handle:
241 * - The ip_g_head list maintained by mi_open_link() and friends.
242 *
243 * - The reassembly data structures (one lock per hash bucket)
244 *
245 * - conn_lock is meant to protect conn_t fields. The fields actually
246 * protected by conn_lock are documented in the conn_t definition.
247 *
248 * - ire_lock to protect some of the fields of the ire, IRE tables
249 * (one lock per hash bucket). Refer to ip_ire.c for details.
250 *
251 * - ndp_g_lock and nce_lock for protecting NCEs.
252 *
253 * - ill_lock protects fields of the ill and ipif. Details in ip.h
254 *
255 * - ill_g_lock: This is a global reader/writer lock. Protects the following
256 * * The AVL tree based global multi list of all ills.
257 * * The linked list of all ipifs of an ill
258 * * The <ill-ipsq> mapping
259 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next
260 * * The illgroup list threaded by ill_group_next.
261 * * <ill-phyint> association
262 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
263 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion
264 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill
265 * will all have to hold the ill_g_lock as writer for the actual duration
266 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping
267 * may be found in the IPMP section.
268 *
269 * - ill_lock: This is a per ill mutex.
270 * It protects some members of the ill and is documented below.
271 * It also protects the <ill-ipsq> mapping
272 * It also protects the illgroup list threaded by ill_group_next.
273 * It also protects the <ill-phyint> assoc.
274 * It also protects the list of ipifs hanging off the ill.
275 *
276 * - ipsq_lock: This is a per ipsq_t mutex lock.
277 * This protects all the other members of the ipsq struct except
278 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock
279 *
280 * - illgrp_lock: This is a per ill_group mutex lock.
281 * The only thing it protects is the illgrp_ill_schednext member of ill_group
282 * which dictates which is the next ill in an ill_group that is to be chosen
283 * for sending outgoing packets, through creation of an IRE_CACHE that
284 * references this ill.
285 *
286 * - phyint_lock: This is a per phyint mutex lock. Protects just the
287 * phyint_flags
288 *
289 * - ip_g_nd_lock: This is a global reader/writer lock.
290 * Any call to nd_load to load a new parameter to the ND table must hold the
291 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock
292 * as reader.
293 *
294 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
295 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
296 * uniqueness check also done atomically.
297 *
298 * - ipsec_capab_ills_lock: This readers/writer lock protects the global
299 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken
300 * as a writer when adding or deleting elements from these lists, and
301 * as a reader when walking these lists to send a SADB update to the
302 * IPsec capable ills.
303 *
304 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
305 * group list linked by ill_usesrc_grp_next. It also protects the
306 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
307 * group is being added or deleted. This lock is taken as a reader when
308 * walking the list/group(eg: to get the number of members in a usesrc group).
309 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
310 * field is changing state i.e from NULL to non-NULL or vice-versa. For
311 * example, it is not necessary to take this lock in the initial portion
312 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and
313 * ip_sioctl_flags since the these operations are executed exclusively and
314 * that ensures that the "usesrc group state" cannot change. The "usesrc
315 * group state" change can happen only in the latter part of
316 * ip_sioctl_slifusesrc and in ill_delete.
317 *
318 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications.
319 *
320 * To change the <ill-phyint> association, the ill_g_lock must be held
321 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
322 * must be held.
323 *
324 * To change the <ill-ipsq> association the ill_g_lock must be held as writer
325 * and the ill_lock of the ill in question must be held.
326 *
327 * To change the <ill-illgroup> association the ill_g_lock must be held as
328 * writer and the ill_lock of the ill in question must be held.
329 *
330 * To add or delete an ipif from the list of ipifs hanging off the ill,
331 * ill_g_lock (writer) and ill_lock must be held and the thread must be
332 * a writer on the associated ipsq,.
333 *
334 * To add or delete an ill to the system, the ill_g_lock must be held as
335 * writer and the thread must be a writer on the associated ipsq.
336 *
337 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
338 * must be a writer on the associated ipsq.
339 *
340 * Lock hierarchy
341 *
342 * Some lock hierarchy scenarios are listed below.
343 *
344 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
345 * ill_g_lock -> illgrp_lock -> ill_lock
346 * ill_g_lock -> ill_lock(s) -> phyint_lock
347 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock
348 * ill_g_lock -> ip_addr_avail_lock
349 * conn_lock -> irb_lock -> ill_lock -> ire_lock
350 * ill_g_lock -> ip_g_nd_lock
351 *
352 * When more than 1 ill lock is needed to be held, all ill lock addresses
353 * are sorted on address and locked starting from highest addressed lock
354 * downward.
355 *
356 * IPsec scenarios
357 *
358 * ipsa_lock -> ill_g_lock -> ill_lock
359 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock
360 * ipsec_capab_ills_lock -> ipsa_lock
361 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
362 *
363 * Trusted Solaris scenarios
364 *
365 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
366 * igsa_lock -> gcdb_lock
367 * gcgrp_rwlock -> ire_lock
368 * gcgrp_rwlock -> gcdb_lock
369 *
370 *
371 * Routing/forwarding table locking notes:
372 *
373 * Lock acquisition order: Radix tree lock, irb_lock.
374 * Requirements:
375 * i. Walker must not hold any locks during the walker callback.
376 * ii Walker must not see a truncated tree during the walk because of any node
377 * deletion.
378 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
379 * in many places in the code to walk the irb list. Thus even if all the
380 * ires in a bucket have been deleted, we still can't free the radix node
381 * until the ires have actually been inactive'd (freed).
382 *
383 * Tree traversal - Need to hold the global tree lock in read mode.
384 * Before dropping the global tree lock, need to either increment the ire_refcnt
385 * to ensure that the radix node can't be deleted.
386 *
387 * Tree add - Need to hold the global tree lock in write mode to add a
388 * radix node. To prevent the node from being deleted, increment the
389 * irb_refcnt, after the node is added to the tree. The ire itself is
390 * added later while holding the irb_lock, but not the tree lock.
391 *
392 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
393 * All associated ires must be inactive (i.e. freed), and irb_refcnt
394 * must be zero.
395 *
396 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
397 * global tree lock (read mode) for traversal.
398 *
399 * IPsec notes :
400 *
401 * IP interacts with the IPsec code (AH/ESP) by tagging a M_CTL message
402 * in front of the actual packet. For outbound datagrams, the M_CTL
403 * contains a ipsec_out_t (defined in ipsec_info.h), which has the
404 * information used by the IPsec code for applying the right level of
405 * protection. The information initialized by IP in the ipsec_out_t
406 * is determined by the per-socket policy or global policy in the system.
407 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in
408 * ipsec_info.h) which starts out with nothing in it. It gets filled
409 * with the right information if it goes through the AH/ESP code, which
410 * happens if the incoming packet is secure. The information initialized
411 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether
412 * the policy requirements needed by per-socket policy or global policy
413 * is met or not.
414 *
415 * If there is both per-socket policy (set using setsockopt) and there
416 * is also global policy match for the 5 tuples of the socket,
417 * ipsec_override_policy() makes the decision of which one to use.
418 *
419 * For fully connected sockets i.e dst, src [addr, port] is known,
420 * conn_policy_cached is set indicating that policy has been cached.
421 * conn_in_enforce_policy may or may not be set depending on whether
422 * there is a global policy match or per-socket policy match.
423 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind.
424 * Once the right policy is set on the conn_t, policy cannot change for
425 * this socket. This makes life simpler for TCP (UDP ?) where
426 * re-transmissions go out with the same policy. For symmetry, policy
427 * is cached for fully connected UDP sockets also. Thus if policy is cached,
428 * it also implies that policy is latched i.e policy cannot change
429 * on these sockets. As we have the right policy on the conn, we don't
430 * have to lookup global policy for every outbound and inbound datagram
431 * and thus serving as an optimization. Note that a global policy change
432 * does not affect fully connected sockets if they have policy. If fully
433 * connected sockets did not have any policy associated with it, global
434 * policy change may affect them.
435 *
436 * IP Flow control notes:
437 *
438 * Non-TCP streams are flow controlled by IP. On the send side, if the packet
439 * cannot be sent down to the driver by IP, because of a canput failure, IP
440 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq.
441 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained
442 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the
443 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the
444 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains
445 * the queued messages, and removes the conn from the drain list, if all
446 * messages were drained. It also qenables the next conn in the drain list to
447 * continue the drain process.
448 *
449 * In reality the drain list is not a single list, but a configurable number
450 * of lists. The ip_wsrv on the IP module, qenables the first conn in each
451 * list. If the ip_wsrv of the next qenabled conn does not run, because the
452 * stream closes, ip_close takes responsibility to qenable the next conn in
453 * the drain list. The directly called ip_wput path always does a putq, if
454 * it cannot putnext. Thus synchronization problems are handled between
455 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only
456 * functions that manipulate this drain list. Furthermore conn_drain_insert
457 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv
458 * running on a queue at any time. conn_drain_tail can be simultaneously called
459 * from both ip_wsrv and ip_close.
460 *
461 * IPQOS notes:
462 *
463 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
464 * and IPQoS modules. IPPF includes hooks in IP at different control points
465 * (callout positions) which direct packets to IPQoS modules for policy
466 * processing. Policies, if present, are global.
467 *
468 * The callout positions are located in the following paths:
469 * o local_in (packets destined for this host)
470 * o local_out (packets orginating from this host )
471 * o fwd_in (packets forwarded by this m/c - inbound)
472 * o fwd_out (packets forwarded by this m/c - outbound)
473 * Hooks at these callout points can be enabled/disabled using the ndd variable
474 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
475 * By default all the callout positions are enabled.
476 *
477 * Outbound (local_out)
478 * Hooks are placed in ip_wput_ire and ipsec_out_process.
479 *
480 * Inbound (local_in)
481 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and
482 * TCP and UDP fanout routines.
483 *
484 * Forwarding (in and out)
485 * Hooks are placed in ip_rput_forward.
486 *
487 * IP Policy Framework processing (IPPF processing)
488 * Policy processing for a packet is initiated by ip_process, which ascertains
489 * that the classifier (ipgpc) is loaded and configured, failing which the
490 * packet resumes normal processing in IP. If the clasifier is present, the
491 * packet is acted upon by one or more IPQoS modules (action instances), per
492 * filters configured in ipgpc and resumes normal IP processing thereafter.
493 * An action instance can drop a packet in course of its processing.
494 *
495 * A boolean variable, ip_policy, is used in all the fanout routines that can
496 * invoke ip_process for a packet. This variable indicates if the packet should
497 * to be sent for policy processing. The variable is set to B_TRUE by default,
498 * i.e. when the routines are invoked in the normal ip procesing path for a
499 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout;
500 * ip_policy is set to B_FALSE for all the routines called in these two
501 * functions because, in the former case, we don't process loopback traffic
502 * currently while in the latter, the packets have already been processed in
503 * icmp_inbound.
504 *
505 * Zones notes:
506 *
507 * The partitioning rules for networking are as follows:
508 * 1) Packets coming from a zone must have a source address belonging to that
509 * zone.
510 * 2) Packets coming from a zone can only be sent on a physical interface on
511 * which the zone has an IP address.
512 * 3) Between two zones on the same machine, packet delivery is only allowed if
513 * there's a matching route for the destination and zone in the forwarding
514 * table.
515 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
516 * different zones can bind to the same port with the wildcard address
517 * (INADDR_ANY).
518 *
519 * The granularity of interface partitioning is at the logical interface level.
520 * Therefore, every zone has its own IP addresses, and incoming packets can be
521 * attributed to a zone unambiguously. A logical interface is placed into a zone
522 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
523 * structure. Rule (1) is implemented by modifying the source address selection
524 * algorithm so that the list of eligible addresses is filtered based on the
525 * sending process zone.
526 *
527 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
528 * across all zones, depending on their type. Here is the break-up:
529 *
530 * IRE type Shared/exclusive
531 * -------- ----------------
532 * IRE_BROADCAST Exclusive
533 * IRE_DEFAULT (default routes) Shared (*)
534 * IRE_LOCAL Exclusive (x)
535 * IRE_LOOPBACK Exclusive
536 * IRE_PREFIX (net routes) Shared (*)
537 * IRE_CACHE Exclusive
538 * IRE_IF_NORESOLVER (interface routes) Exclusive
539 * IRE_IF_RESOLVER (interface routes) Exclusive
540 * IRE_HOST (host routes) Shared (*)
541 *
542 * (*) A zone can only use a default or off-subnet route if the gateway is
543 * directly reachable from the zone, that is, if the gateway's address matches
544 * one of the zone's logical interfaces.
545 *
546 * (x) IRE_LOCAL are handled a bit differently, since for all other entries
547 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source
548 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP
549 * address of the zone itself (the destination). Since IRE_LOCAL is used
550 * for communication between zones, ip_wput_ire has special logic to set
551 * the right source address when sending using an IRE_LOCAL.
552 *
553 * Furthermore, when ip_restrict_interzone_loopback is set (the default),
554 * ire_cache_lookup restricts loopback using an IRE_LOCAL
555 * between zone to the case when L2 would have conceptually looped the packet
556 * back, i.e. the loopback which is required since neither Ethernet drivers
557 * nor Ethernet hardware loops them back. This is the case when the normal
558 * routes (ignoring IREs with different zoneids) would send out the packet on
559 * the same ill (or ill group) as the ill with which is IRE_LOCAL is
560 * associated.
561 *
562 * Multiple zones can share a common broadcast address; typically all zones
563 * share the 255.255.255.255 address. Incoming as well as locally originated
564 * broadcast packets must be dispatched to all the zones on the broadcast
565 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
566 * since some zones may not be on the 10.16.72/24 network. To handle this, each
567 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
568 * sent to every zone that has an IRE_BROADCAST entry for the destination
569 * address on the input ill, see conn_wantpacket().
570 *
571 * Applications in different zones can join the same multicast group address.
572 * For IPv4, group memberships are per-logical interface, so they're already
573 * inherently part of a zone. For IPv6, group memberships are per-physical
574 * interface, so we distinguish IPv6 group memberships based on group address,
575 * interface and zoneid. In both cases, received multicast packets are sent to
576 * every zone for which a group membership entry exists. On IPv6 we need to
577 * check that the target zone still has an address on the receiving physical
578 * interface; it could have been removed since the application issued the
579 * IPV6_JOIN_GROUP.
580 */
581
582 /*
583 * Squeue Fanout flags:
584 * 0: No fanout.
585 * 1: Fanout across all squeues
586 */
587 boolean_t ip_squeue_fanout = 0;
588
589 /*
590 * Maximum dups allowed per packet.
591 */
592 uint_t ip_max_frag_dups = 10;
593
594 #define IS_SIMPLE_IPH(ipha) \
595 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION)
596
597 /* RFC1122 Conformance */
598 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER
599
600 #define ILL_MAX_NAMELEN LIFNAMSIZ
601
602 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *);
603
604 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
605 cred_t *credp, boolean_t isv6);
606 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t,
607 ipha_t **);
608
609 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t,
610 ip_stack_t *);
611 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int,
612 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t);
613 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp);
614 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t,
615 mblk_t *, int, ip_stack_t *);
616 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *,
617 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t,
618 ill_t *, zoneid_t);
619 static void icmp_options_update(ipha_t *);
620 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t,
621 ip_stack_t *);
622 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t,
623 zoneid_t zoneid, ip_stack_t *);
624 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *);
625 static void icmp_redirect(ill_t *, mblk_t *);
626 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t,
627 ip_stack_t *);
628
629 static void ip_arp_news(queue_t *, mblk_t *);
630 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *,
631 ip_stack_t *);
632 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
633 char *ip_dot_addr(ipaddr_t, char *);
634 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
635 int ip_close(queue_t *, int);
636 static char *ip_dot_saddr(uchar_t *, char *);
637 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t,
638 boolean_t, boolean_t, ill_t *, zoneid_t);
639 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t,
640 boolean_t, boolean_t, zoneid_t);
641 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t,
642 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t);
643 static void ip_lrput(queue_t *, mblk_t *);
644 ipaddr_t ip_net_mask(ipaddr_t);
645 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, conn_t *, zoneid_t,
646 ip_stack_t *);
647 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t,
648 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *);
649 char *ip_nv_lookup(nv_t *, int);
650 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *);
651 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *);
652 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *);
653 static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t,
654 ipndp_t *, size_t);
655 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *);
656 void ip_rput(queue_t *, mblk_t *);
657 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
658 void *dummy_arg);
659 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *);
660 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *,
661 ip_stack_t *);
662 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *,
663 ire_t *, ip_stack_t *);
664 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *,
665 mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *);
666 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *,
667 ip_stack_t *);
668 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *,
669 uint16_t *);
670 int ip_snmp_get(queue_t *, mblk_t *, int);
671 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
672 mib2_ipIfStatsEntry_t *, ip_stack_t *);
673 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
674 ip_stack_t *);
675 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *);
676 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
677 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
678 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
679 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
680 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
681 ip_stack_t *ipst);
682 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
683 ip_stack_t *ipst);
684 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
685 ip_stack_t *ipst);
686 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
687 ip_stack_t *ipst);
688 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
689 ip_stack_t *ipst);
690 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
691 ip_stack_t *ipst);
692 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
693 ip_stack_t *ipst);
694 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
695 ip_stack_t *ipst);
696 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *,
697 ip_stack_t *ipst);
698 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *,
699 ip_stack_t *ipst);
700 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
701 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
702 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *);
703 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
704 static boolean_t ip_source_routed(ipha_t *, ip_stack_t *);
705 static boolean_t ip_source_route_included(ipha_t *);
706 static void ip_trash_ire_reclaim_stack(ip_stack_t *);
707
708 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t,
709 zoneid_t, ip_stack_t *);
710 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *);
711 static void ip_wput_local_options(ipha_t *, ip_stack_t *);
712 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t,
713 zoneid_t, ip_stack_t *);
714
715 static void conn_drain_init(ip_stack_t *);
716 static void conn_drain_fini(ip_stack_t *);
717 static void conn_drain_tail(conn_t *connp, boolean_t closing);
718
719 static void conn_walk_drain(ip_stack_t *);
720 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *,
721 zoneid_t);
722
723 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
724 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
725 static void ip_stack_fini(netstackid_t stackid, void *arg);
726
727 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int,
728 zoneid_t);
729 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
730 void *dummy_arg);
731
732 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *);
733
734 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
735 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *,
736 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *);
737 static void ip_multirt_bad_mtu(ire_t *, uint32_t);
738
739 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *);
740 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *,
741 caddr_t, cred_t *);
742 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value,
743 caddr_t cp, cred_t *cr);
744 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t,
745 cred_t *);
746 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value,
747 caddr_t cp, cred_t *cr);
748 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t,
749 cred_t *);
750 static int ipmp_hook_emulation_set(queue_t *, mblk_t *, char *, caddr_t,
751 cred_t *);
752 static squeue_func_t ip_squeue_switch(int);
753
754 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
755 static void ip_kstat_fini(netstackid_t, kstat_t *);
756 static int ip_kstat_update(kstat_t *kp, int rw);
757 static void *icmp_kstat_init(netstackid_t);
758 static void icmp_kstat_fini(netstackid_t, kstat_t *);
759 static int icmp_kstat_update(kstat_t *kp, int rw);
760 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
761 static void ip_kstat2_fini(netstackid_t, kstat_t *);
762
763 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *);
764
765 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t,
766 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *);
767
768 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *,
769 ipha_t *, ill_t *, boolean_t);
770 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
771
772 /* How long, in seconds, we allow frags to hang around. */
773 #define IP_FRAG_TIMEOUT 60
774
775 /*
776 * Threshold which determines whether MDT should be used when
777 * generating IP fragments; payload size must be greater than
778 * this threshold for MDT to take place.
779 */
780 #define IP_WPUT_FRAG_MDT_MIN 32768
781
782 /* Setable in /etc/system only */
783 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN;
784
785 static long ip_rput_pullups;
786 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
787
788 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
789 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
790
791 int ip_debug;
792
793 #ifdef DEBUG
794 uint32_t ipsechw_debug = 0;
795 #endif
796
797 /*
798 * Multirouting/CGTP stuff
799 */
800 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */
801
802 /*
803 * XXX following really should only be in a header. Would need more
804 * header and .c clean up first.
805 */
806 extern optdb_obj_t ip_opt_obj;
807
808 ulong_t ip_squeue_enter_unbound = 0;
809
810 /*
811 * Named Dispatch Parameter Table.
812 * All of these are alterable, within the min/max values given, at run time.
813 */
814 static ipparam_t lcl_param_arr[] = {
815 /* min max value name */
816 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"},
817 { 0, 1, 1, "ip_respond_to_echo_broadcast"},
818 { 0, 1, 1, "ip_respond_to_echo_multicast"},
819 { 0, 1, 0, "ip_respond_to_timestamp"},
820 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"},
821 { 0, 1, 1, "ip_send_redirects"},
822 { 0, 1, 0, "ip_forward_directed_broadcasts"},
823 { 0, 10, 0, "ip_mrtdebug"},
824 { 5000, 999999999, 60000, "ip_ire_timer_interval" },
825 { 60000, 999999999, 1200000, "ip_ire_arp_interval" },
826 { 60000, 999999999, 60000, "ip_ire_redirect_interval" },
827 { 1, 255, 255, "ip_def_ttl" },
828 { 0, 1, 0, "ip_forward_src_routed"},
829 { 0, 256, 32, "ip_wroff_extra" },
830 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" },
831 { 8, 65536, 64, "ip_icmp_return_data_bytes" },
832 { 0, 1, 1, "ip_path_mtu_discovery" },
833 { 0, 240, 30, "ip_ignore_delete_time" },
834 { 0, 1, 0, "ip_ignore_redirect" },
835 { 0, 1, 1, "ip_output_queue" },
836 { 1, 254, 1, "ip_broadcast_ttl" },
837 { 0, 99999, 100, "ip_icmp_err_interval" },
838 { 1, 99999, 10, "ip_icmp_err_burst" },
839 { 0, 999999999, 1000000, "ip_reass_queue_bytes" },
840 { 0, 1, 0, "ip_strict_dst_multihoming" },
841 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"},
842 { 0, 1, 0, "ipsec_override_persocket_policy" },
843 { 0, 1, 1, "icmp_accept_clear_messages" },
844 { 0, 1, 1, "igmp_accept_clear_messages" },
845 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME,
846 "ip_ndp_delay_first_probe_time"},
847 { 1, 999999999, ND_MAX_UNICAST_SOLICIT,
848 "ip_ndp_max_unicast_solicit"},
849 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" },
850 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" },
851 { 0, 1, 0, "ip6_forward_src_routed"},
852 { 0, 1, 1, "ip6_respond_to_echo_multicast"},
853 { 0, 1, 1, "ip6_send_redirects"},
854 { 0, 1, 0, "ip6_ignore_redirect" },
855 { 0, 1, 0, "ip6_strict_dst_multihoming" },
856
857 { 1, 8, 3, "ip_ire_reclaim_fraction" },
858
859 { 0, 999999, 1000, "ipsec_policy_log_interval" },
860
861 { 0, 1, 1, "pim_accept_clear_messages" },
862 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" },
863 { 1, 20, 3, "ip_ndp_unsolicit_count" },
864 { 0, 1, 1, "ip6_ignore_home_address_opt" },
865 { 0, 15, 0, "ip_policy_mask" },
866 { 1000, 60000, 1000, "ip_multirt_resolution_interval" },
867 { 0, 255, 1, "ip_multirt_ttl" },
868 { 0, 1, 1, "ip_multidata_outbound" },
869 { 0, 3600000, 300000, "ip_ndp_defense_interval" },
870 { 0, 999999, 60*60*24, "ip_max_temp_idle" },
871 { 0, 1000, 1, "ip_max_temp_defend" },
872 { 0, 1000, 3, "ip_max_defend" },
873 { 0, 999999, 30, "ip_defend_interval" },
874 { 0, 3600000, 300000, "ip_dup_recovery" },
875 { 0, 1, 1, "ip_restrict_interzone_loopback" },
876 { 0, 1, 1, "ip_lso_outbound" },
877 { IGMP_V1_ROUTER, IGMP_V3_ROUTER, IGMP_V3_ROUTER, "igmp_max_version" },
878 { MLD_V1_ROUTER, MLD_V2_ROUTER, MLD_V2_ROUTER, "mld_max_version" },
879 #ifdef DEBUG
880 { 0, 1, 0, "ip6_drop_inbound_icmpv6" },
881 #else
882 { 0, 0, 0, "" },
883 #endif
884 };
885
886 /*
887 * Extended NDP table
888 * The addresses for the first two are filled in to be ips_ip_g_forward
889 * and ips_ipv6_forward at init time.
890 */
891 static ipndp_t lcl_ndp_arr[] = {
892 /* getf setf data name */
893 #define IPNDP_IP_FORWARDING_OFFSET 0
894 { ip_param_generic_get, ip_forward_set, NULL,
895 "ip_forwarding" },
896 #define IPNDP_IP6_FORWARDING_OFFSET 1
897 { ip_param_generic_get, ip_forward_set, NULL,
898 "ip6_forwarding" },
899 { ip_ill_report, NULL, NULL,
900 "ip_ill_status" },
901 { ip_ipif_report, NULL, NULL,
902 "ip_ipif_status" },
903 { ip_conn_report, NULL, NULL,
904 "ip_conn_status" },
905 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups,
906 "ip_rput_pullups" },
907 { ip_srcid_report, NULL, NULL,
908 "ip_srcid_status" },
909 { ip_param_generic_get, ip_squeue_profile_set,
910 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" },
911 { ip_param_generic_get, ip_squeue_bind_set,
912 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" },
913 { ip_param_generic_get, ip_input_proc_set,
914 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" },
915 { ip_param_generic_get, ip_int_set,
916 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" },
917 #define IPNDP_CGTP_FILTER_OFFSET 11
918 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL,
919 "ip_cgtp_filter" },
920 { ip_param_generic_get, ip_int_set,
921 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" },
922 #define IPNDP_IPMP_HOOK_OFFSET 13
923 { ip_param_generic_get, ipmp_hook_emulation_set, NULL,
924 "ipmp_hook_emulation" },
925 { ip_param_generic_get, ip_int_set, (caddr_t)&ip_debug,
926 "ip_debug" },
927 };
928
929 /*
930 * Table of IP ioctls encoding the various properties of the ioctl and
931 * indexed based on the last byte of the ioctl command. Occasionally there
932 * is a clash, and there is more than 1 ioctl with the same last byte.
933 * In such a case 1 ioctl is encoded in the ndx table and the remaining
934 * ioctls are encoded in the misc table. An entry in the ndx table is
935 * retrieved by indexing on the last byte of the ioctl command and comparing
936 * the ioctl command with the value in the ndx table. In the event of a
937 * mismatch the misc table is then searched sequentially for the desired
938 * ioctl command.
939 *
940 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
941 */
942 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
943 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
944 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
945 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
946 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
947 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
948 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
949 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
950 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
951 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
952 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
953
954 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
955 MISC_CMD, ip_siocaddrt, NULL },
956 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
957 MISC_CMD, ip_siocdelrt, NULL },
958
959 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
960 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
961 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
962 IF_CMD, ip_sioctl_get_addr, NULL },
963
964 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
965 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
966 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
967 IPI_GET_CMD | IPI_REPL,
968 IF_CMD, ip_sioctl_get_dstaddr, NULL },
969
970 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
971 IPI_PRIV | IPI_WR | IPI_REPL,
972 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
973 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
974 IPI_MODOK | IPI_GET_CMD | IPI_REPL,
975 IF_CMD, ip_sioctl_get_flags, NULL },
976
977 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
978 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
979
980 /* copyin size cannot be coded for SIOCGIFCONF */
981 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
982 MISC_CMD, ip_sioctl_get_ifconf, NULL },
983
984 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
985 IF_CMD, ip_sioctl_mtu, NULL },
986 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
987 IF_CMD, ip_sioctl_get_mtu, NULL },
988 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
989 IPI_GET_CMD | IPI_REPL,
990 IF_CMD, ip_sioctl_get_brdaddr, NULL },
991 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
992 IF_CMD, ip_sioctl_brdaddr, NULL },
993 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
994 IPI_GET_CMD | IPI_REPL,
995 IF_CMD, ip_sioctl_get_netmask, NULL },
996 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
997 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
998 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
999 IPI_GET_CMD | IPI_REPL,
1000 IF_CMD, ip_sioctl_get_metric, NULL },
1001 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
1002 IF_CMD, ip_sioctl_metric, NULL },
1003 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1004
1005 /* See 166-168 below for extended SIOC*XARP ioctls */
1006 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV,
1007 ARP_CMD, ip_sioctl_arp, NULL },
1008 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL,
1009 ARP_CMD, ip_sioctl_arp, NULL },
1010 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV,
1011 ARP_CMD, ip_sioctl_arp, NULL },
1012
1013 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1014 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1015 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1016 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1017 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1018 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1019 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1020 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1021 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1022 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1023 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1024 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1025 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1026 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1027 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1028 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1029 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1030 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1031 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1032 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1033 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1034
1035 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
1036 MISC_CMD, if_unitsel, if_unitsel_restart },
1037
1038 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1039 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1040 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1041 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1042 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1043 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1044 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1045 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1046 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1047 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1048 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1049 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1050 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1051 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1052 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1053 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1054 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1055 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1056
1057 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
1058 IPI_PRIV | IPI_WR | IPI_MODOK,
1059 IF_CMD, ip_sioctl_sifname, NULL },
1060
1061 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1062 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1063 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1064 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1065 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1066 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1067 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1068 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1069 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1070 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1071 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1072 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1073 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1074
1075 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL,
1076 MISC_CMD, ip_sioctl_get_ifnum, NULL },
1077 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
1078 IF_CMD, ip_sioctl_get_muxid, NULL },
1079 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
1080 IPI_PRIV | IPI_WR | IPI_REPL,
1081 IF_CMD, ip_sioctl_muxid, NULL },
1082
1083 /* Both if and lif variants share same func */
1084 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
1085 IF_CMD, ip_sioctl_get_lifindex, NULL },
1086 /* Both if and lif variants share same func */
1087 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
1088 IPI_PRIV | IPI_WR | IPI_REPL,
1089 IF_CMD, ip_sioctl_slifindex, NULL },
1090
1091 /* copyin size cannot be coded for SIOCGIFCONF */
1092 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
1093 MISC_CMD, ip_sioctl_get_ifconf, NULL },
1094 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1095 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1096 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1097 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1098 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1099 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1100 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1101 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1102 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1103 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1104 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1105 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1106 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1107 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1108 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1109 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1110 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1111
1112 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
1113 IPI_PRIV | IPI_WR | IPI_REPL,
1114 LIF_CMD, ip_sioctl_removeif,
1115 ip_sioctl_removeif_restart },
1116 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
1117 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL,
1118 LIF_CMD, ip_sioctl_addif, NULL },
1119 #define SIOCLIFADDR_NDX 112
1120 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1121 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
1122 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
1123 IPI_GET_CMD | IPI_REPL,
1124 LIF_CMD, ip_sioctl_get_addr, NULL },
1125 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1126 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
1127 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
1128 IPI_GET_CMD | IPI_REPL,
1129 LIF_CMD, ip_sioctl_get_dstaddr, NULL },
1130 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
1131 IPI_PRIV | IPI_WR | IPI_REPL,
1132 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
1133 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
1134 IPI_GET_CMD | IPI_MODOK | IPI_REPL,
1135 LIF_CMD, ip_sioctl_get_flags, NULL },
1136
1137 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1138 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1139
1140 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1141 ip_sioctl_get_lifconf, NULL },
1142 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1143 LIF_CMD, ip_sioctl_mtu, NULL },
1144 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL,
1145 LIF_CMD, ip_sioctl_get_mtu, NULL },
1146 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
1147 IPI_GET_CMD | IPI_REPL,
1148 LIF_CMD, ip_sioctl_get_brdaddr, NULL },
1149 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1150 LIF_CMD, ip_sioctl_brdaddr, NULL },
1151 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
1152 IPI_GET_CMD | IPI_REPL,
1153 LIF_CMD, ip_sioctl_get_netmask, NULL },
1154 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1155 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
1156 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
1157 IPI_GET_CMD | IPI_REPL,
1158 LIF_CMD, ip_sioctl_get_metric, NULL },
1159 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1160 LIF_CMD, ip_sioctl_metric, NULL },
1161 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
1162 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL,
1163 LIF_CMD, ip_sioctl_slifname,
1164 ip_sioctl_slifname_restart },
1165
1166 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL,
1167 MISC_CMD, ip_sioctl_get_lifnum, NULL },
1168 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
1169 IPI_GET_CMD | IPI_REPL,
1170 LIF_CMD, ip_sioctl_get_muxid, NULL },
1171 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
1172 IPI_PRIV | IPI_WR | IPI_REPL,
1173 LIF_CMD, ip_sioctl_muxid, NULL },
1174 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1175 IPI_GET_CMD | IPI_REPL,
1176 LIF_CMD, ip_sioctl_get_lifindex, 0 },
1177 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1178 IPI_PRIV | IPI_WR | IPI_REPL,
1179 LIF_CMD, ip_sioctl_slifindex, 0 },
1180 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1181 LIF_CMD, ip_sioctl_token, NULL },
1182 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1183 IPI_GET_CMD | IPI_REPL,
1184 LIF_CMD, ip_sioctl_get_token, NULL },
1185 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1186 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1187 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1188 IPI_GET_CMD | IPI_REPL,
1189 LIF_CMD, ip_sioctl_get_subnet, NULL },
1190 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1191 LIF_CMD, ip_sioctl_lnkinfo, NULL },
1192
1193 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1194 IPI_GET_CMD | IPI_REPL,
1195 LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1196 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1197 LIF_CMD, ip_siocdelndp_v6, NULL },
1198 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1199 LIF_CMD, ip_siocqueryndp_v6, NULL },
1200 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1201 LIF_CMD, ip_siocsetndp_v6, NULL },
1202 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1203 MISC_CMD, ip_sioctl_tmyaddr, NULL },
1204 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1205 MISC_CMD, ip_sioctl_tonlink, NULL },
1206 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1207 MISC_CMD, ip_sioctl_tmysite, NULL },
1208 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL,
1209 TUN_CMD, ip_sioctl_tunparam, NULL },
1210 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req),
1211 IPI_PRIV | IPI_WR,
1212 TUN_CMD, ip_sioctl_tunparam, NULL },
1213
1214 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */
1215 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1216 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1217 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1218 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1219
1220 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq),
1221 IPI_PRIV | IPI_WR | IPI_REPL,
1222 LIF_CMD, ip_sioctl_move, ip_sioctl_move },
1223 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq),
1224 IPI_PRIV | IPI_WR | IPI_REPL,
1225 LIF_CMD, ip_sioctl_move, ip_sioctl_move },
1226 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1227 IPI_PRIV | IPI_WR | IPI_REPL,
1228 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1229 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1230 IPI_GET_CMD | IPI_REPL,
1231 LIF_CMD, ip_sioctl_get_groupname, NULL },
1232 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq),
1233 IPI_GET_CMD | IPI_REPL,
1234 LIF_CMD, ip_sioctl_get_oindex, NULL },
1235
1236 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1237 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1238 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1239 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1240
1241 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1242 LIF_CMD, ip_sioctl_slifoindex, NULL },
1243
1244 /* These are handled in ip_sioctl_copyin_setup itself */
1245 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1246 MISC_CMD, NULL, NULL },
1247 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1248 MISC_CMD, NULL, NULL },
1249 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1250
1251 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1252 ip_sioctl_get_lifconf, NULL },
1253
1254 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV,
1255 XARP_CMD, ip_sioctl_arp, NULL },
1256 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL,
1257 XARP_CMD, ip_sioctl_arp, NULL },
1258 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV,
1259 XARP_CMD, ip_sioctl_arp, NULL },
1260
1261 /* SIOCPOPSOCKFS is not handled by IP */
1262 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1263
1264 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1265 IPI_GET_CMD | IPI_REPL,
1266 LIF_CMD, ip_sioctl_get_lifzone, NULL },
1267 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1268 IPI_PRIV | IPI_WR | IPI_REPL,
1269 LIF_CMD, ip_sioctl_slifzone,
1270 ip_sioctl_slifzone_restart },
1271 /* 172-174 are SCTP ioctls and not handled by IP */
1272 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1273 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1274 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1275 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1276 IPI_GET_CMD, LIF_CMD,
1277 ip_sioctl_get_lifusesrc, 0 },
1278 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1279 IPI_PRIV | IPI_WR,
1280 LIF_CMD, ip_sioctl_slifusesrc,
1281 NULL },
1282 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1283 ip_sioctl_get_lifsrcof, NULL },
1284 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1285 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1286 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR,
1287 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1288 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1289 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1290 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR,
1291 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1292 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD,
1293 ip_sioctl_set_ipmpfailback, NULL },
1294 /* SIOCSENABLESDP is handled by SDP */
1295 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1296 };
1297
1298 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1299
1300 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1301 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req),
1302 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL },
1303 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR,
1304 TUN_CMD, ip_sioctl_tunparam, NULL },
1305 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1306 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1307 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1308 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1309 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL },
1310 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1311 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1312 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD,
1313 MISC_CMD, mrt_ioctl},
1314 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD,
1315 MISC_CMD, mrt_ioctl},
1316 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD,
1317 MISC_CMD, mrt_ioctl}
1318 };
1319
1320 int ip_misc_ioctl_count =
1321 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1322
1323 int conn_drain_nthreads; /* Number of drainers reqd. */
1324 /* Settable in /etc/system */
1325 /* Defined in ip_ire.c */
1326 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1327 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1328 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1329
1330 static nv_t ire_nv_arr[] = {
1331 { IRE_BROADCAST, "BROADCAST" },
1332 { IRE_LOCAL, "LOCAL" },
1333 { IRE_LOOPBACK, "LOOPBACK" },
1334 { IRE_CACHE, "CACHE" },
1335 { IRE_DEFAULT, "DEFAULT" },
1336 { IRE_PREFIX, "PREFIX" },
1337 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1338 { IRE_IF_RESOLVER, "IF_RESOLV" },
1339 { IRE_HOST, "HOST" },
1340 { 0 }
1341 };
1342
1343 nv_t *ire_nv_tbl = ire_nv_arr;
1344
1345 /* Simple ICMP IP Header Template */
1346 static ipha_t icmp_ipha = {
1347 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1348 };
1349
1350 struct module_info ip_mod_info = {
1351 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024
1352 };
1353
1354 /*
1355 * Duplicate static symbols within a module confuses mdb; so we avoid the
1356 * problem by making the symbols here distinct from those in udp.c.
1357 */
1358
1359 /*
1360 * Entry points for IP as a device and as a module.
1361 * FIXME: down the road we might want a separate module and driver qinit.
1362 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1363 */
1364 static struct qinit iprinitv4 = {
1365 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL,
1366 &ip_mod_info
1367 };
1368
1369 struct qinit iprinitv6 = {
1370 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL,
1371 &ip_mod_info
1372 };
1373
1374 static struct qinit ipwinitv4 = {
1375 (pfi_t)ip_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1376 &ip_mod_info
1377 };
1378
1379 struct qinit ipwinitv6 = {
1380 (pfi_t)ip_wput_v6, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1381 &ip_mod_info
1382 };
1383
1384 static struct qinit iplrinit = {
1385 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL,
1386 &ip_mod_info
1387 };
1388
1389 static struct qinit iplwinit = {
1390 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL,
1391 &ip_mod_info
1392 };
1393
1394 /* For AF_INET aka /dev/ip */
1395 struct streamtab ipinfov4 = {
1396 &iprinitv4, &ipwinitv4, &iplrinit, &iplwinit
1397 };
1398
1399 /* For AF_INET6 aka /dev/ip6 */
1400 struct streamtab ipinfov6 = {
1401 &iprinitv6, &ipwinitv6, &iplrinit, &iplwinit
1402 };
1403
1404 #ifdef DEBUG
1405 static boolean_t skip_sctp_cksum = B_FALSE;
1406 #endif
1407
1408 /*
1409 * Prepend the zoneid using an ipsec_out_t for later use by functions like
1410 * ip_rput_v6(), ip_output(), etc. If the message
1411 * block already has a M_CTL at the front of it, then simply set the zoneid
1412 * appropriately.
1413 */
1414 mblk_t *
1415 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst)
1416 {
1417 mblk_t *first_mp;
1418 ipsec_out_t *io;
1419
1420 ASSERT(zoneid != ALL_ZONES);
1421 if (mp->b_datap->db_type == M_CTL) {
1422 io = (ipsec_out_t *)mp->b_rptr;
1423 ASSERT(io->ipsec_out_type == IPSEC_OUT);
1424 io->ipsec_out_zoneid = zoneid;
1425 return (mp);
1426 }
1427
1428 first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack);
1429 if (first_mp == NULL)
1430 return (NULL);
1431 io = (ipsec_out_t *)first_mp->b_rptr;
1432 /* This is not a secure packet */
1433 io->ipsec_out_secure = B_FALSE;
1434 io->ipsec_out_zoneid = zoneid;
1435 first_mp->b_cont = mp;
1436 return (first_mp);
1437 }
1438
1439 /*
1440 * Copy an M_CTL-tagged message, preserving reference counts appropriately.
1441 */
1442 mblk_t *
1443 ip_copymsg(mblk_t *mp)
1444 {
1445 mblk_t *nmp;
1446 ipsec_info_t *in;
1447
1448 if (mp->b_datap->db_type != M_CTL)
1449 return (copymsg(mp));
1450
1451 in = (ipsec_info_t *)mp->b_rptr;
1452
1453 /*
1454 * Note that M_CTL is also used for delivering ICMP error messages
1455 * upstream to transport layers.
1456 */
1457 if (in->ipsec_info_type != IPSEC_OUT &&
1458 in->ipsec_info_type != IPSEC_IN)
1459 return (copymsg(mp));
1460
1461 nmp = copymsg(mp->b_cont);
1462
1463 if (in->ipsec_info_type == IPSEC_OUT) {
1464 return (ipsec_out_tag(mp, nmp,
1465 ((ipsec_out_t *)in)->ipsec_out_ns));
1466 } else {
1467 return (ipsec_in_tag(mp, nmp,
1468 ((ipsec_in_t *)in)->ipsec_in_ns));
1469 }
1470 }
1471
1472 /* Generate an ICMP fragmentation needed message. */
1473 static void
1474 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid,
1475 ip_stack_t *ipst)
1476 {
1477 icmph_t icmph;
1478 mblk_t *first_mp;
1479 boolean_t mctl_present;
1480
1481 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
1482
1483 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
1484 if (mctl_present)
1485 freeb(first_mp);
1486 return;
1487 }
1488
1489 bzero(&icmph, sizeof (icmph_t));
1490 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1491 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1492 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1493 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1494 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1495 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid,
1496 ipst);
1497 }
1498
1499 /*
1500 * icmp_inbound deals with ICMP messages in the following ways.
1501 *
1502 * 1) It needs to send a reply back and possibly delivering it
1503 * to the "interested" upper clients.
1504 * 2) It needs to send it to the upper clients only.
1505 * 3) It needs to change some values in IP only.
1506 * 4) It needs to change some values in IP and upper layers e.g TCP.
1507 *
1508 * We need to accomodate icmp messages coming in clear until we get
1509 * everything secure from the wire. If icmp_accept_clear_messages
1510 * is zero we check with the global policy and act accordingly. If
1511 * it is non-zero, we accept the message without any checks. But
1512 * *this does not mean* that this will be delivered to the upper
1513 * clients. By accepting we might send replies back, change our MTU
1514 * value etc. but delivery to the ULP/clients depends on their policy
1515 * dispositions.
1516 *
1517 * We handle the above 4 cases in the context of IPsec in the
1518 * following way :
1519 *
1520 * 1) Send the reply back in the same way as the request came in.
1521 * If it came in encrypted, it goes out encrypted. If it came in
1522 * clear, it goes out in clear. Thus, this will prevent chosen
1523 * plain text attack.
1524 * 2) The client may or may not expect things to come in secure.
1525 * If it comes in secure, the policy constraints are checked
1526 * before delivering it to the upper layers. If it comes in
1527 * clear, ipsec_inbound_accept_clear will decide whether to
1528 * accept this in clear or not. In both the cases, if the returned
1529 * message (IP header + 8 bytes) that caused the icmp message has
1530 * AH/ESP headers, it is sent up to AH/ESP for validation before
1531 * sending up. If there are only 8 bytes of returned message, then
1532 * upper client will not be notified.
1533 * 3) Check with global policy to see whether it matches the constaints.
1534 * But this will be done only if icmp_accept_messages_in_clear is
1535 * zero.
1536 * 4) If we need to change both in IP and ULP, then the decision taken
1537 * while affecting the values in IP and while delivering up to TCP
1538 * should be the same.
1539 *
1540 * There are two cases.
1541 *
1542 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1543 * failed), we will not deliver it to the ULP, even though they
1544 * are *willing* to accept in *clear*. This is fine as our global
1545 * disposition to icmp messages asks us reject the datagram.
1546 *
1547 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1548 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1549 * to deliver it to ULP (policy failed), it can lead to
1550 * consistency problems. The cases known at this time are
1551 * ICMP_DESTINATION_UNREACHABLE messages with following code
1552 * values :
1553 *
1554 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1555 * and Upper layer rejects. Then the communication will
1556 * come to a stop. This is solved by making similar decisions
1557 * at both levels. Currently, when we are unable to deliver
1558 * to the Upper Layer (due to policy failures) while IP has
1559 * adjusted ire_max_frag, the next outbound datagram would
1560 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1561 * will be with the right level of protection. Thus the right
1562 * value will be communicated even if we are not able to
1563 * communicate when we get from the wire initially. But this
1564 * assumes there would be at least one outbound datagram after
1565 * IP has adjusted its ire_max_frag value. To make things
1566 * simpler, we accept in clear after the validation of
1567 * AH/ESP headers.
1568 *
1569 * - Other ICMP ERRORS : We may not be able to deliver it to the
1570 * upper layer depending on the level of protection the upper
1571 * layer expects and the disposition in ipsec_inbound_accept_clear().
1572 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1573 * should be accepted in clear when the Upper layer expects secure.
1574 * Thus the communication may get aborted by some bad ICMP
1575 * packets.
1576 *
1577 * IPQoS Notes:
1578 * The only instance when a packet is sent for processing is when there
1579 * isn't an ICMP client and if we are interested in it.
1580 * If there is a client, IPPF processing will take place in the
1581 * ip_fanout_proto routine.
1582 *
1583 * Zones notes:
1584 * The packet is only processed in the context of the specified zone: typically
1585 * only this zone will reply to an echo request, and only interested clients in
1586 * this zone will receive a copy of the packet. This means that the caller must
1587 * call icmp_inbound() for each relevant zone.
1588 */
1589 static void
1590 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill,
1591 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy,
1592 ill_t *recv_ill, zoneid_t zoneid)
1593 {
1594 icmph_t *icmph;
1595 ipha_t *ipha;
1596 int iph_hdr_length;
1597 int hdr_length;
1598 boolean_t interested;
1599 uint32_t ts;
1600 uchar_t *wptr;
1601 ipif_t *ipif;
1602 mblk_t *first_mp;
1603 ipsec_in_t *ii;
1604 ire_t *src_ire;
1605 boolean_t onlink;
1606 timestruc_t now;
1607 uint32_t ill_index;
1608 ip_stack_t *ipst;
1609
1610 ASSERT(ill != NULL);
1611 ipst = ill->ill_ipst;
1612
1613 first_mp = mp;
1614 if (mctl_present) {
1615 mp = first_mp->b_cont;
1616 ASSERT(mp != NULL);
1617 }
1618
1619 ipha = (ipha_t *)mp->b_rptr;
1620 if (ipst->ips_icmp_accept_clear_messages == 0) {
1621 first_mp = ipsec_check_global_policy(first_mp, NULL,
1622 ipha, NULL, mctl_present, ipst->ips_netstack);
1623 if (first_mp == NULL)
1624 return;
1625 }
1626
1627 /*
1628 * On a labeled system, we have to check whether the zone itself is
1629 * permitted to receive raw traffic.
1630 */
1631 if (is_system_labeled()) {
1632 if (zoneid == ALL_ZONES)
1633 zoneid = tsol_packet_to_zoneid(mp);
1634 if (!tsol_can_accept_raw(mp, B_FALSE)) {
1635 ip1dbg(("icmp_inbound: zone %d can't receive raw",
1636 zoneid));
1637 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1638 freemsg(first_mp);
1639 return;
1640 }
1641 }
1642
1643 /*
1644 * We have accepted the ICMP message. It means that we will
1645 * respond to the packet if needed. It may not be delivered
1646 * to the upper client depending on the policy constraints
1647 * and the disposition in ipsec_inbound_accept_clear.
1648 */
1649
1650 ASSERT(ill != NULL);
1651
1652 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1653 iph_hdr_length = IPH_HDR_LENGTH(ipha);
1654 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) {
1655 /* Last chance to get real. */
1656 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) {
1657 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1658 freemsg(first_mp);
1659 return;
1660 }
1661 /* Refresh iph following the pullup. */
1662 ipha = (ipha_t *)mp->b_rptr;
1663 }
1664 /* ICMP header checksum, including checksum field, should be zero. */
1665 if (sum_valid ? (sum != 0 && sum != 0xFFFF) :
1666 IP_CSUM(mp, iph_hdr_length, 0)) {
1667 BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs);
1668 freemsg(first_mp);
1669 return;
1670 }
1671 /* The IP header will always be a multiple of four bytes */
1672 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1673 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type,
1674 icmph->icmph_code));
1675 wptr = (uchar_t *)icmph + ICMPH_SIZE;
1676 /* We will set "interested" to "true" if we want a copy */
1677 interested = B_FALSE;
1678 switch (icmph->icmph_type) {
1679 case ICMP_ECHO_REPLY:
1680 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1681 break;
1682 case ICMP_DEST_UNREACHABLE:
1683 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1684 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1685 interested = B_TRUE; /* Pass up to transport */
1686 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1687 break;
1688 case ICMP_SOURCE_QUENCH:
1689 interested = B_TRUE; /* Pass up to transport */
1690 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1691 break;
1692 case ICMP_REDIRECT:
1693 if (!ipst->ips_ip_ignore_redirect)
1694 interested = B_TRUE;
1695 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1696 break;
1697 case ICMP_ECHO_REQUEST:
1698 /*
1699 * Whether to respond to echo requests that come in as IP
1700 * broadcasts or as IP multicast is subject to debate
1701 * (what isn't?). We aim to please, you pick it.
1702 * Default is do it.
1703 */
1704 if (!broadcast && !CLASSD(ipha->ipha_dst)) {
1705 /* unicast: always respond */
1706 interested = B_TRUE;
1707 } else if (CLASSD(ipha->ipha_dst)) {
1708 /* multicast: respond based on tunable */
1709 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1710 } else if (broadcast) {
1711 /* broadcast: respond based on tunable */
1712 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1713 }
1714 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1715 break;
1716 case ICMP_ROUTER_ADVERTISEMENT:
1717 case ICMP_ROUTER_SOLICITATION:
1718 break;
1719 case ICMP_TIME_EXCEEDED:
1720 interested = B_TRUE; /* Pass up to transport */
1721 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1722 break;
1723 case ICMP_PARAM_PROBLEM:
1724 interested = B_TRUE; /* Pass up to transport */
1725 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1726 break;
1727 case ICMP_TIME_STAMP_REQUEST:
1728 /* Response to Time Stamp Requests is local policy. */
1729 if (ipst->ips_ip_g_resp_to_timestamp &&
1730 /* So is whether to respond if it was an IP broadcast. */
1731 (!broadcast || ipst->ips_ip_g_resp_to_timestamp_bcast)) {
1732 int tstamp_len = 3 * sizeof (uint32_t);
1733
1734 if (wptr + tstamp_len > mp->b_wptr) {
1735 if (!pullupmsg(mp, wptr + tstamp_len -
1736 mp->b_rptr)) {
1737 BUMP_MIB(ill->ill_ip_mib,
1738 ipIfStatsInDiscards);
1739 freemsg(first_mp);
1740 return;
1741 }
1742 /* Refresh ipha following the pullup. */
1743 ipha = (ipha_t *)mp->b_rptr;
1744 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1745 wptr = (uchar_t *)icmph + ICMPH_SIZE;
1746 }
1747 interested = B_TRUE;
1748 }
1749 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1750 break;
1751 case ICMP_TIME_STAMP_REPLY:
1752 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1753 break;
1754 case ICMP_INFO_REQUEST:
1755 /* Per RFC 1122 3.2.2.7, ignore this. */
1756 case ICMP_INFO_REPLY:
1757 break;
1758 case ICMP_ADDRESS_MASK_REQUEST:
1759 if ((ipst->ips_ip_respond_to_address_mask_broadcast ||
1760 !broadcast) &&
1761 /* TODO m_pullup of complete header? */
1762 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) {
1763 interested = B_TRUE;
1764 }
1765 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1766 break;
1767 case ICMP_ADDRESS_MASK_REPLY:
1768 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1769 break;
1770 default:
1771 interested = B_TRUE; /* Pass up to transport */
1772 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1773 break;
1774 }
1775 /* See if there is an ICMP client. */
1776 if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) {
1777 /* If there is an ICMP client and we want one too, copy it. */
1778 mblk_t *first_mp1;
1779
1780 if (!interested) {
1781 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present,
1782 ip_policy, recv_ill, zoneid);
1783 return;
1784 }
1785 first_mp1 = ip_copymsg(first_mp);
1786 if (first_mp1 != NULL) {
1787 ip_fanout_proto(q, first_mp1, ill, ipha,
1788 0, mctl_present, ip_policy, recv_ill, zoneid);
1789 }
1790 } else if (!interested) {
1791 freemsg(first_mp);
1792 return;
1793 } else {
1794 /*
1795 * Initiate policy processing for this packet if ip_policy
1796 * is true.
1797 */
1798 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
1799 ill_index = ill->ill_phyint->phyint_ifindex;
1800 ip_process(IPP_LOCAL_IN, &mp, ill_index);
1801 if (mp == NULL) {
1802 if (mctl_present) {
1803 freeb(first_mp);
1804 }
1805 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1806 return;
1807 }
1808 }
1809 }
1810 /* We want to do something with it. */
1811 /* Check db_ref to make sure we can modify the packet. */
1812 if (mp->b_datap->db_ref > 1) {
1813 mblk_t *first_mp1;
1814
1815 first_mp1 = ip_copymsg(first_mp);
1816 freemsg(first_mp);
1817 if (!first_mp1) {
1818 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1819 return;
1820 }
1821 first_mp = first_mp1;
1822 if (mctl_present) {
1823 mp = first_mp->b_cont;
1824 ASSERT(mp != NULL);
1825 } else {
1826 mp = first_mp;
1827 }
1828 ipha = (ipha_t *)mp->b_rptr;
1829 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1830 wptr = (uchar_t *)icmph + ICMPH_SIZE;
1831 }
1832 switch (icmph->icmph_type) {
1833 case ICMP_ADDRESS_MASK_REQUEST:
1834 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1835 if (ipif == NULL) {
1836 freemsg(first_mp);
1837 return;
1838 }
1839 /*
1840 * outging interface must be IPv4
1841 */
1842 ASSERT(ipif != NULL && !ipif->ipif_isv6);
1843 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1844 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN);
1845 ipif_refrele(ipif);
1846 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1847 break;
1848 case ICMP_ECHO_REQUEST:
1849 icmph->icmph_type = ICMP_ECHO_REPLY;
1850 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1851 break;
1852 case ICMP_TIME_STAMP_REQUEST: {
1853 uint32_t *tsp;
1854
1855 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1856 tsp = (uint32_t *)wptr;
1857 tsp++; /* Skip past 'originate time' */
1858 /* Compute # of milliseconds since midnight */
1859 gethrestime(&now);
1860 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1861 now.tv_nsec / (NANOSEC / MILLISEC);
1862 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1863 *tsp++ = htonl(ts); /* Lay in 'send time' */
1864 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1865 break;
1866 }
1867 default:
1868 ipha = (ipha_t *)&icmph[1];
1869 if ((uchar_t *)&ipha[1] > mp->b_wptr) {
1870 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) {
1871 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1872 freemsg(first_mp);
1873 return;
1874 }
1875 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1876 ipha = (ipha_t *)&icmph[1];
1877 }
1878 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) {
1879 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1880 freemsg(first_mp);
1881 return;
1882 }
1883 hdr_length = IPH_HDR_LENGTH(ipha);
1884 if (hdr_length < sizeof (ipha_t)) {
1885 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1886 freemsg(first_mp);
1887 return;
1888 }
1889 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) {
1890 if (!pullupmsg(mp,
1891 (uchar_t *)ipha + hdr_length - mp->b_rptr)) {
1892 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1893 freemsg(first_mp);
1894 return;
1895 }
1896 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1897 ipha = (ipha_t *)&icmph[1];
1898 }
1899 switch (icmph->icmph_type) {
1900 case ICMP_REDIRECT:
1901 /*
1902 * As there is no upper client to deliver, we don't
1903 * need the first_mp any more.
1904 */
1905 if (mctl_present) {
1906 freeb(first_mp);
1907 }
1908 icmp_redirect(ill, mp);
1909 return;
1910 case ICMP_DEST_UNREACHABLE:
1911 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1912 if (!icmp_inbound_too_big(icmph, ipha, ill,
1913 zoneid, mp, iph_hdr_length, ipst)) {
1914 freemsg(first_mp);
1915 return;
1916 }
1917 /*
1918 * icmp_inbound_too_big() may alter mp.
1919 * Resynch ipha and icmph accordingly.
1920 */
1921 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1922 ipha = (ipha_t *)&icmph[1];
1923 }
1924 /* FALLTHRU */
1925 default :
1926 /*
1927 * IPQoS notes: Since we have already done IPQoS
1928 * processing we don't want to do it again in
1929 * the fanout routines called by
1930 * icmp_inbound_error_fanout, hence the last
1931 * argument, ip_policy, is B_FALSE.
1932 */
1933 icmp_inbound_error_fanout(q, ill, first_mp, icmph,
1934 ipha, iph_hdr_length, hdr_length, mctl_present,
1935 B_FALSE, recv_ill, zoneid);
1936 }
1937 return;
1938 }
1939 /* Send out an ICMP packet */
1940 icmph->icmph_checksum = 0;
1941 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0);
1942 if (broadcast || CLASSD(ipha->ipha_dst)) {
1943 ipif_t *ipif_chosen;
1944 /*
1945 * Make it look like it was directed to us, so we don't look
1946 * like a fool with a broadcast or multicast source address.
1947 */
1948 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1949 /*
1950 * Make sure that we haven't grabbed an interface that's DOWN.
1951 */
1952 if (ipif != NULL) {
1953 ipif_chosen = ipif_select_source(ipif->ipif_ill,
1954 ipha->ipha_src, zoneid);
1955 if (ipif_chosen != NULL) {
1956 ipif_refrele(ipif);
1957 ipif = ipif_chosen;
1958 }
1959 }
1960 if (ipif == NULL) {
1961 ip0dbg(("icmp_inbound: "
1962 "No source for broadcast/multicast:\n"
1963 "\tsrc 0x%x dst 0x%x ill %p "
1964 "ipif_lcl_addr 0x%x\n",
1965 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst),
1966 (void *)ill,
1967 ill->ill_ipif->ipif_lcl_addr));
1968 freemsg(first_mp);
1969 return;
1970 }
1971 ASSERT(ipif != NULL && !ipif->ipif_isv6);
1972 ipha->ipha_dst = ipif->ipif_src_addr;
1973 ipif_refrele(ipif);
1974 }
1975 /* Reset time to live. */
1976 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1977 {
1978 /* Swap source and destination addresses */
1979 ipaddr_t tmp;
1980
1981 tmp = ipha->ipha_src;
1982 ipha->ipha_src = ipha->ipha_dst;
1983 ipha->ipha_dst = tmp;
1984 }
1985 ipha->ipha_ident = 0;
1986 if (!IS_SIMPLE_IPH(ipha))
1987 icmp_options_update(ipha);
1988
1989 /*
1990 * ICMP echo replies should go out on the same interface
1991 * the request came on as probes used by in.mpathd for detecting
1992 * NIC failures are ECHO packets. We turn-off load spreading
1993 * by setting ipsec_in_attach_if to B_TRUE, which is copied
1994 * to ipsec_out_attach_if by ipsec_in_to_out called later in this
1995 * function. This is in turn handled by ip_wput and ip_newroute
1996 * to make sure that the packet goes out on the interface it came
1997 * in on. If we don't turnoff load spreading, the packets might get
1998 * dropped if there are no non-FAILED/INACTIVE interfaces for it
1999 * to go out and in.mpathd would wrongly detect a failure or
2000 * mis-detect a NIC failure for link failure. As load spreading
2001 * can happen only if ill_group is not NULL, we do only for
2002 * that case and this does not affect the normal case.
2003 *
2004 * We turn off load spreading only on echo packets that came from
2005 * on-link hosts. If the interface route has been deleted, this will
2006 * not be enforced as we can't do much. For off-link hosts, as the
2007 * default routes in IPv4 does not typically have an ire_ipif
2008 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute.
2009 * Moreover, expecting a default route through this interface may
2010 * not be correct. We use ipha_dst because of the swap above.
2011 */
2012 onlink = B_FALSE;
2013 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) {
2014 /*
2015 * First, we need to make sure that it is not one of our
2016 * local addresses. If we set onlink when it is one of
2017 * our local addresses, we will end up creating IRE_CACHES
2018 * for one of our local addresses. Then, we will never
2019 * accept packets for them afterwards.
2020 */
2021 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL,
2022 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
2023 if (src_ire == NULL) {
2024 ipif = ipif_get_next_ipif(NULL, ill);
2025 if (ipif == NULL) {
2026 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2027 freemsg(mp);
2028 return;
2029 }
2030 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0,
2031 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
2032 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE, ipst);
2033 ipif_refrele(ipif);
2034 if (src_ire != NULL) {
2035 onlink = B_TRUE;
2036 ire_refrele(src_ire);
2037 }
2038 } else {
2039 ire_refrele(src_ire);
2040 }
2041 }
2042 if (!mctl_present) {
2043 /*
2044 * This packet should go out the same way as it
2045 * came in i.e in clear. To make sure that global
2046 * policy will not be applied to this in ip_wput_ire,
2047 * we attach a IPSEC_IN mp and clear ipsec_in_secure.
2048 */
2049 ASSERT(first_mp == mp);
2050 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack);
2051 if (first_mp == NULL) {
2052 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2053 freemsg(mp);
2054 return;
2055 }
2056 ii = (ipsec_in_t *)first_mp->b_rptr;
2057
2058 /* This is not a secure packet */
2059 ii->ipsec_in_secure = B_FALSE;
2060 if (onlink) {
2061 ii->ipsec_in_attach_if = B_TRUE;
2062 ii->ipsec_in_ill_index =
2063 ill->ill_phyint->phyint_ifindex;
2064 ii->ipsec_in_rill_index =
2065 recv_ill->ill_phyint->phyint_ifindex;
2066 }
2067 first_mp->b_cont = mp;
2068 } else if (onlink) {
2069 ii = (ipsec_in_t *)first_mp->b_rptr;
2070 ii->ipsec_in_attach_if = B_TRUE;
2071 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex;
2072 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex;
2073 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */
2074 } else {
2075 ii = (ipsec_in_t *)first_mp->b_rptr;
2076 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */
2077 }
2078 ii->ipsec_in_zoneid = zoneid;
2079 ASSERT(zoneid != ALL_ZONES);
2080 if (!ipsec_in_to_out(first_mp, ipha, NULL)) {
2081 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2082 return;
2083 }
2084 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2085 put(WR(q), first_mp);
2086 }
2087
2088 static ipaddr_t
2089 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp)
2090 {
2091 conn_t *connp;
2092 connf_t *connfp;
2093 ipaddr_t nexthop_addr = INADDR_ANY;
2094 int hdr_length = IPH_HDR_LENGTH(ipha);
2095 uint16_t *up;
2096 uint32_t ports;
2097 ip_stack_t *ipst = ill->ill_ipst;
2098
2099 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2100 switch (ipha->ipha_protocol) {
2101 case IPPROTO_TCP:
2102 {
2103 tcph_t *tcph;
2104
2105 /* do a reverse lookup */
2106 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length);
2107 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph,
2108 TCPS_LISTEN, ipst);
2109 break;
2110 }
2111 case IPPROTO_UDP:
2112 {
2113 uint32_t dstport, srcport;
2114
2115 ((uint16_t *)&ports)[0] = up[1];
2116 ((uint16_t *)&ports)[1] = up[0];
2117
2118 /* Extract ports in net byte order */
2119 dstport = htons(ntohl(ports) & 0xFFFF);
2120 srcport = htons(ntohl(ports) >> 16);
2121
2122 connfp = &ipst->ips_ipcl_udp_fanout[
2123 IPCL_UDP_HASH(dstport, ipst)];
2124 mutex_enter(&connfp->connf_lock);
2125 connp = connfp->connf_head;
2126
2127 /* do a reverse lookup */
2128 while ((connp != NULL) &&
2129 (!IPCL_UDP_MATCH(connp, dstport,
2130 ipha->ipha_src, srcport, ipha->ipha_dst) ||
2131 !IPCL_ZONE_MATCH(connp, zoneid))) {
2132 connp = connp->conn_next;
2133 }
2134 if (connp != NULL)
2135 CONN_INC_REF(connp);
2136 mutex_exit(&connfp->connf_lock);
2137 break;
2138 }
2139 case IPPROTO_SCTP:
2140 {
2141 in6_addr_t map_src, map_dst;
2142
2143 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src);
2144 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst);
2145 ((uint16_t *)&ports)[0] = up[1];
2146 ((uint16_t *)&ports)[1] = up[0];
2147
2148 connp = sctp_find_conn(&map_src, &map_dst, ports,
2149 zoneid, ipst->ips_netstack->netstack_sctp);
2150 if (connp == NULL) {
2151 connp = ipcl_classify_raw(mp, IPPROTO_SCTP,
2152 zoneid, ports, ipha, ipst);
2153 } else {
2154 CONN_INC_REF(connp);
2155 SCTP_REFRELE(CONN2SCTP(connp));
2156 }
2157 break;
2158 }
2159 default:
2160 {
2161 ipha_t ripha;
2162
2163 ripha.ipha_src = ipha->ipha_dst;
2164 ripha.ipha_dst = ipha->ipha_src;
2165 ripha.ipha_protocol = ipha->ipha_protocol;
2166
2167 connfp = &ipst->ips_ipcl_proto_fanout[
2168 ipha->ipha_protocol];
2169 mutex_enter(&connfp->connf_lock);
2170 connp = connfp->connf_head;
2171 for (connp = connfp->connf_head; connp != NULL;
2172 connp = connp->conn_next) {
2173 if (IPCL_PROTO_MATCH(connp,
2174 ipha->ipha_protocol, &ripha, ill,
2175 0, zoneid)) {
2176 CONN_INC_REF(connp);
2177 break;
2178 }
2179 }
2180 mutex_exit(&connfp->connf_lock);
2181 }
2182 }
2183 if (connp != NULL) {
2184 if (connp->conn_nexthop_set)
2185 nexthop_addr = connp->conn_nexthop_v4;
2186 CONN_DEC_REF(connp);
2187 }
2188 return (nexthop_addr);
2189 }
2190
2191 /* Table from RFC 1191 */
2192 static int icmp_frag_size_table[] =
2193 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
2194
2195 /*
2196 * Process received ICMP Packet too big.
2197 * After updating any IRE it does the fanout to any matching transport streams.
2198 * Assumes the message has been pulled up till the IP header that caused
2199 * the error.
2200 *
2201 * Returns B_FALSE on failure and B_TRUE on success.
2202 */
2203 static boolean_t
2204 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill,
2205 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length,
2206 ip_stack_t *ipst)
2207 {
2208 ire_t *ire, *first_ire;
2209 int mtu;
2210 int hdr_length;
2211 ipaddr_t nexthop_addr;
2212
2213 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
2214 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
2215 ASSERT(ill != NULL);
2216
2217 hdr_length = IPH_HDR_LENGTH(ipha);
2218
2219 /* Drop if the original packet contained a source route */
2220 if (ip_source_route_included(ipha)) {
2221 return (B_FALSE);
2222 }
2223 /*
2224 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport
2225 * header.
2226 */
2227 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2228 mp->b_wptr) {
2229 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2230 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2231 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2232 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n"));
2233 return (B_FALSE);
2234 }
2235 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2236 ipha = (ipha_t *)&icmph[1];
2237 }
2238 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp);
2239 if (nexthop_addr != INADDR_ANY) {
2240 /* nexthop set */
2241 first_ire = ire_ctable_lookup(ipha->ipha_dst,
2242 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp),
2243 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, ipst);
2244 } else {
2245 /* nexthop not set */
2246 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE,
2247 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
2248 }
2249
2250 if (!first_ire) {
2251 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n",
2252 ntohl(ipha->ipha_dst)));
2253 return (B_FALSE);
2254 }
2255 /* Check for MTU discovery advice as described in RFC 1191 */
2256 mtu = ntohs(icmph->icmph_du_mtu);
2257 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER);
2258 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst;
2259 ire = ire->ire_next) {
2260 /*
2261 * Look for the connection to which this ICMP message is
2262 * directed. If it has the IP_NEXTHOP option set, then the
2263 * search is limited to IREs with the MATCH_IRE_PRIVATE
2264 * option. Else the search is limited to regular IREs.
2265 */
2266 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) &&
2267 (nexthop_addr != ire->ire_gateway_addr)) ||
2268 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) &&
2269 (nexthop_addr != INADDR_ANY)))
2270 continue;
2271
2272 mutex_enter(&ire->ire_lock);
2273 if (icmph->icmph_du_zero == 0 && mtu > 68) {
2274 /* Reduce the IRE max frag value as advised. */
2275 ip1dbg(("Received mtu from router: %d (was %d)\n",
2276 mtu, ire->ire_max_frag));
2277 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu);
2278 } else {
2279 uint32_t length;
2280 int i;
2281
2282 /*
2283 * Use the table from RFC 1191 to figure out
2284 * the next "plateau" based on the length in
2285 * the original IP packet.
2286 */
2287 length = ntohs(ipha->ipha_length);
2288 if (ire->ire_max_frag <= length &&
2289 ire->ire_max_frag >= length - hdr_length) {
2290 /*
2291 * Handle broken BSD 4.2 systems that
2292 * return the wrong iph_length in ICMP
2293 * errors.
2294 */
2295 ip1dbg(("Wrong mtu: sent %d, ire %d\n",
2296 length, ire->ire_max_frag));
2297 length -= hdr_length;
2298 }
2299 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
2300 if (length > icmp_frag_size_table[i])
2301 break;
2302 }
2303 if (i == A_CNT(icmp_frag_size_table)) {
2304 /* Smaller than 68! */
2305 ip1dbg(("Too big for packet size %d\n",
2306 length));
2307 ire->ire_max_frag = MIN(ire->ire_max_frag, 576);
2308 ire->ire_frag_flag = 0;
2309 } else {
2310 mtu = icmp_frag_size_table[i];
2311 ip1dbg(("Calculated mtu %d, packet size %d, "
2312 "before %d", mtu, length,
2313 ire->ire_max_frag));
2314 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu);
2315 ip1dbg((", after %d\n", ire->ire_max_frag));
2316 }
2317 /* Record the new max frag size for the ULP. */
2318 icmph->icmph_du_zero = 0;
2319 icmph->icmph_du_mtu =
2320 htons((uint16_t)ire->ire_max_frag);
2321 }
2322 mutex_exit(&ire->ire_lock);
2323 }
2324 rw_exit(&first_ire->ire_bucket->irb_lock);
2325 ire_refrele(first_ire);
2326 return (B_TRUE);
2327 }
2328
2329 /*
2330 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout
2331 * calls this function.
2332 */
2333 static mblk_t *
2334 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length)
2335 {
2336 ipha_t *ipha;
2337 icmph_t *icmph;
2338 ipha_t *in_ipha;
2339 int length;
2340
2341 ASSERT(mp->b_datap->db_type == M_DATA);
2342
2343 /*
2344 * For Self-encapsulated packets, we added an extra IP header
2345 * without the options. Inner IP header is the one from which
2346 * the outer IP header was formed. Thus, we need to remove the
2347 * outer IP header. To do this, we pullup the whole message
2348 * and overlay whatever follows the outer IP header over the
2349 * outer IP header.
2350 */
2351
2352 if (!pullupmsg(mp, -1))
2353 return (NULL);
2354
2355 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2356 ipha = (ipha_t *)&icmph[1];
2357 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2358
2359 /*
2360 * The length that we want to overlay is following the inner
2361 * IP header. Subtracting the IP header + icmp header + outer
2362 * IP header's length should give us the length that we want to
2363 * overlay.
2364 */
2365 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) -
2366 hdr_length;
2367 /*
2368 * Overlay whatever follows the inner header over the
2369 * outer header.
2370 */
2371 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2372
2373 /* Set the wptr to account for the outer header */
2374 mp->b_wptr -= hdr_length;
2375 return (mp);
2376 }
2377
2378 /*
2379 * Try to pass the ICMP message upstream in case the ULP cares.
2380 *
2381 * If the packet that caused the ICMP error is secure, we send
2382 * it to AH/ESP to make sure that the attached packet has a
2383 * valid association. ipha in the code below points to the
2384 * IP header of the packet that caused the error.
2385 *
2386 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently
2387 * in the context of IPsec. Normally we tell the upper layer
2388 * whenever we send the ire (including ip_bind), the IPsec header
2389 * length in ire_ipsec_overhead. TCP can deduce the MSS as it
2390 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead.
2391 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the
2392 * same thing. As TCP has the IPsec options size that needs to be
2393 * adjusted, we just pass the MTU unchanged.
2394 *
2395 * IFN could have been generated locally or by some router.
2396 *
2397 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this.
2398 * This happens because IP adjusted its value of MTU on an
2399 * earlier IFN message and could not tell the upper layer,
2400 * the new adjusted value of MTU e.g. Packet was encrypted
2401 * or there was not enough information to fanout to upper
2402 * layers. Thus on the next outbound datagram, ip_wput_ire
2403 * generates the IFN, where IPsec processing has *not* been
2404 * done.
2405 *
2406 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed
2407 * could have generated this. This happens because ire_max_frag
2408 * value in IP was set to a new value, while the IPsec processing
2409 * was being done and after we made the fragmentation check in
2410 * ip_wput_ire. Thus on return from IPsec processing,
2411 * ip_wput_ipsec_out finds that the new length is > ire_max_frag
2412 * and generates the IFN. As IPsec processing is over, we fanout
2413 * to AH/ESP to remove the header.
2414 *
2415 * In both these cases, ipsec_in_loopback will be set indicating
2416 * that IFN was generated locally.
2417 *
2418 * ROUTER : IFN could be secure or non-secure.
2419 *
2420 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2421 * packet in error has AH/ESP headers to validate the AH/ESP
2422 * headers. AH/ESP will verify whether there is a valid SA or
2423 * not and send it back. We will fanout again if we have more
2424 * data in the packet.
2425 *
2426 * If the packet in error does not have AH/ESP, we handle it
2427 * like any other case.
2428 *
2429 * * NON_SECURE : If the packet in error has AH/ESP headers,
2430 * we attach a dummy ipsec_in and send it up to AH/ESP
2431 * for validation. AH/ESP will verify whether there is a
2432 * valid SA or not and send it back. We will fanout again if
2433 * we have more data in the packet.
2434 *
2435 * If the packet in error does not have AH/ESP, we handle it
2436 * like any other case.
2437 */
2438 static void
2439 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp,
2440 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length,
2441 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill,
2442 zoneid_t zoneid)
2443 {
2444 uint16_t *up; /* Pointer to ports in ULP header */
2445 uint32_t ports; /* reversed ports for fanout */
2446 ipha_t ripha; /* With reversed addresses */
2447 mblk_t *first_mp;
2448 ipsec_in_t *ii;
2449 tcph_t *tcph;
2450 conn_t *connp;
2451 ip_stack_t *ipst;
2452
2453 ASSERT(ill != NULL);
2454
2455 ASSERT(recv_ill != NULL);
2456 ipst = recv_ill->ill_ipst;
2457
2458 first_mp = mp;
2459 if (mctl_present) {
2460 mp = first_mp->b_cont;
2461 ASSERT(mp != NULL);
2462
2463 ii = (ipsec_in_t *)first_mp->b_rptr;
2464 ASSERT(ii->ipsec_in_type == IPSEC_IN);
2465 } else {
2466 ii = NULL;
2467 }
2468
2469 switch (ipha->ipha_protocol) {
2470 case IPPROTO_UDP:
2471 /*
2472 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
2473 * transport header.
2474 */
2475 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2476 mp->b_wptr) {
2477 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2478 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2479 goto discard_pkt;
2480 }
2481 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2482 ipha = (ipha_t *)&icmph[1];
2483 }
2484 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2485
2486 /*
2487 * Attempt to find a client stream based on port.
2488 * Note that we do a reverse lookup since the header is
2489 * in the form we sent it out.
2490 * The ripha header is only used for the IP_UDP_MATCH and we
2491 * only set the src and dst addresses and protocol.
2492 */
2493 ripha.ipha_src = ipha->ipha_dst;
2494 ripha.ipha_dst = ipha->ipha_src;
2495 ripha.ipha_protocol = ipha->ipha_protocol;
2496 ((uint16_t *)&ports)[0] = up[1];
2497 ((uint16_t *)&ports)[1] = up[0];
2498 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n",
2499 ntohl(ipha->ipha_src), ntohs(up[0]),
2500 ntohl(ipha->ipha_dst), ntohs(up[1]),
2501 icmph->icmph_type, icmph->icmph_code));
2502
2503 /* Have to change db_type after any pullupmsg */
2504 DB_TYPE(mp) = M_CTL;
2505
2506 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0,
2507 mctl_present, ip_policy, recv_ill, zoneid);
2508 return;
2509
2510 case IPPROTO_TCP:
2511 /*
2512 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
2513 * transport header.
2514 */
2515 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2516 mp->b_wptr) {
2517 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2518 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2519 goto discard_pkt;
2520 }
2521 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2522 ipha = (ipha_t *)&icmph[1];
2523 }
2524 /*
2525 * Find a TCP client stream for this packet.
2526 * Note that we do a reverse lookup since the header is
2527 * in the form we sent it out.
2528 */
2529 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length);
2530 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN,
2531 ipst);
2532 if (connp == NULL)
2533 goto discard_pkt;
2534
2535 /* Have to change db_type after any pullupmsg */
2536 DB_TYPE(mp) = M_CTL;
2537 squeue_fill(connp->conn_sqp, first_mp, tcp_input,
2538 connp, SQTAG_TCP_INPUT_ICMP_ERR);
2539 return;
2540
2541 case IPPROTO_SCTP:
2542 /*
2543 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
2544 * transport header.
2545 */
2546 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2547 mp->b_wptr) {
2548 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2549 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2550 goto discard_pkt;
2551 }
2552 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2553 ipha = (ipha_t *)&icmph[1];
2554 }
2555 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2556 /*
2557 * Find a SCTP client stream for this packet.
2558 * Note that we do a reverse lookup since the header is
2559 * in the form we sent it out.
2560 * The ripha header is only used for the matching and we
2561 * only set the src and dst addresses, protocol, and version.
2562 */
2563 ripha.ipha_src = ipha->ipha_dst;
2564 ripha.ipha_dst = ipha->ipha_src;
2565 ripha.ipha_protocol = ipha->ipha_protocol;
2566 ripha.ipha_version_and_hdr_length =
2567 ipha->ipha_version_and_hdr_length;
2568 ((uint16_t *)&ports)[0] = up[1];
2569 ((uint16_t *)&ports)[1] = up[0];
2570
2571 /* Have to change db_type after any pullupmsg */
2572 DB_TYPE(mp) = M_CTL;
2573 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0,
2574 mctl_present, ip_policy, zoneid);
2575 return;
2576
2577 case IPPROTO_ESP:
2578 case IPPROTO_AH: {
2579 int ipsec_rc;
2580 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2581
2582 /*
2583 * We need a IPSEC_IN in the front to fanout to AH/ESP.
2584 * We will re-use the IPSEC_IN if it is already present as
2585 * AH/ESP will not affect any fields in the IPSEC_IN for
2586 * ICMP errors. If there is no IPSEC_IN, allocate a new
2587 * one and attach it in the front.
2588 */
2589 if (ii != NULL) {
2590 /*
2591 * ip_fanout_proto_again converts the ICMP errors
2592 * that come back from AH/ESP to M_DATA so that
2593 * if it is non-AH/ESP and we do a pullupmsg in
2594 * this function, it would work. Convert it back
2595 * to M_CTL before we send up as this is a ICMP
2596 * error. This could have been generated locally or
2597 * by some router. Validate the inner IPsec
2598 * headers.
2599 *
2600 * NOTE : ill_index is used by ip_fanout_proto_again
2601 * to locate the ill.
2602 */
2603 ASSERT(ill != NULL);
2604 ii->ipsec_in_ill_index =
2605 ill->ill_phyint->phyint_ifindex;
2606 ii->ipsec_in_rill_index =
2607 recv_ill->ill_phyint->phyint_ifindex;
2608 DB_TYPE(first_mp->b_cont) = M_CTL;
2609 } else {
2610 /*
2611 * IPSEC_IN is not present. We attach a ipsec_in
2612 * message and send up to IPsec for validating
2613 * and removing the IPsec headers. Clear
2614 * ipsec_in_secure so that when we return
2615 * from IPsec, we don't mistakenly think that this
2616 * is a secure packet came from the network.
2617 *
2618 * NOTE : ill_index is used by ip_fanout_proto_again
2619 * to locate the ill.
2620 */
2621 ASSERT(first_mp == mp);
2622 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack);
2623 if (first_mp == NULL) {
2624 freemsg(mp);
2625 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2626 return;
2627 }
2628 ii = (ipsec_in_t *)first_mp->b_rptr;
2629
2630 /* This is not a secure packet */
2631 ii->ipsec_in_secure = B_FALSE;
2632 first_mp->b_cont = mp;
2633 DB_TYPE(mp) = M_CTL;
2634 ASSERT(ill != NULL);
2635 ii->ipsec_in_ill_index =
2636 ill->ill_phyint->phyint_ifindex;
2637 ii->ipsec_in_rill_index =
2638 recv_ill->ill_phyint->phyint_ifindex;
2639 }
2640 ip2dbg(("icmp_inbound_error: ipsec\n"));
2641
2642 if (!ipsec_loaded(ipss)) {
2643 ip_proto_not_sup(q, first_mp, 0, zoneid, ipst);
2644 return;
2645 }
2646
2647 if (ipha->ipha_protocol == IPPROTO_ESP)
2648 ipsec_rc = ipsecesp_icmp_error(first_mp);
2649 else
2650 ipsec_rc = ipsecah_icmp_error(first_mp);
2651 if (ipsec_rc == IPSEC_STATUS_FAILED)
2652 return;
2653
2654 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL);
2655 return;
2656 }
2657 default:
2658 /*
2659 * The ripha header is only used for the lookup and we
2660 * only set the src and dst addresses and protocol.
2661 */
2662 ripha.ipha_src = ipha->ipha_dst;
2663 ripha.ipha_dst = ipha->ipha_src;
2664 ripha.ipha_protocol = ipha->ipha_protocol;
2665 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n",
2666 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2667 ntohl(ipha->ipha_dst),
2668 icmph->icmph_type, icmph->icmph_code));
2669 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2670 ipha_t *in_ipha;
2671
2672 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
2673 mp->b_wptr) {
2674 if (!pullupmsg(mp, (uchar_t *)ipha +
2675 hdr_length + sizeof (ipha_t) -
2676 mp->b_rptr)) {
2677 goto discard_pkt;
2678 }
2679 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2680 ipha = (ipha_t *)&icmph[1];
2681 }
2682 /*
2683 * Caller has verified that length has to be
2684 * at least the size of IP header.
2685 */
2686 ASSERT(hdr_length >= sizeof (ipha_t));
2687 /*
2688 * Check the sanity of the inner IP header like
2689 * we did for the outer header.
2690 */
2691 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2692 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2693 goto discard_pkt;
2694 }
2695 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2696 goto discard_pkt;
2697 }
2698 /* Check for Self-encapsulated tunnels */
2699 if (in_ipha->ipha_src == ipha->ipha_src &&
2700 in_ipha->ipha_dst == ipha->ipha_dst) {
2701
2702 mp = icmp_inbound_self_encap_error(mp,
2703 iph_hdr_length, hdr_length);
2704 if (mp == NULL)
2705 goto discard_pkt;
2706 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2707 ipha = (ipha_t *)&icmph[1];
2708 hdr_length = IPH_HDR_LENGTH(ipha);
2709 /*
2710 * The packet in error is self-encapsualted.
2711 * And we are finding it further encapsulated
2712 * which we could not have possibly generated.
2713 */
2714 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2715 goto discard_pkt;
2716 }
2717 icmp_inbound_error_fanout(q, ill, first_mp,
2718 icmph, ipha, iph_hdr_length, hdr_length,
2719 mctl_present, ip_policy, recv_ill, zoneid);
2720 return;
2721 }
2722 }
2723 if ((ipha->ipha_protocol == IPPROTO_ENCAP ||
2724 ipha->ipha_protocol == IPPROTO_IPV6) &&
2725 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED &&
2726 ii != NULL &&
2727 ii->ipsec_in_loopback &&
2728 ii->ipsec_in_secure) {
2729 /*
2730 * For IP tunnels that get a looped-back
2731 * ICMP_FRAGMENTATION_NEEDED message, adjust the
2732 * reported new MTU to take into account the IPsec
2733 * headers protecting this configured tunnel.
2734 *
2735 * This allows the tunnel module (tun.c) to blindly
2736 * accept the MTU reported in an ICMP "too big"
2737 * message.
2738 *
2739 * Non-looped back ICMP messages will just be
2740 * handled by the security protocols (if needed),
2741 * and the first subsequent packet will hit this
2742 * path.
2743 */
2744 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) -
2745 ipsec_in_extra_length(first_mp));
2746 }
2747 /* Have to change db_type after any pullupmsg */
2748 DB_TYPE(mp) = M_CTL;
2749
2750 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present,
2751 ip_policy, recv_ill, zoneid);
2752 return;
2753 }
2754 /* NOTREACHED */
2755 discard_pkt:
2756 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2757 drop_pkt:;
2758 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n"));
2759 freemsg(first_mp);
2760 }
2761
2762 /*
2763 * Common IP options parser.
2764 *
2765 * Setup routine: fill in *optp with options-parsing state, then
2766 * tail-call ipoptp_next to return the first option.
2767 */
2768 uint8_t
2769 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2770 {
2771 uint32_t totallen; /* total length of all options */
2772
2773 totallen = ipha->ipha_version_and_hdr_length -
2774 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2775 totallen <<= 2;
2776 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2777 optp->ipoptp_end = optp->ipoptp_next + totallen;
2778 optp->ipoptp_flags = 0;
2779 return (ipoptp_next(optp));
2780 }
2781
2782 /*
2783 * Common IP options parser: extract next option.
2784 */
2785 uint8_t
2786 ipoptp_next(ipoptp_t *optp)
2787 {
2788 uint8_t *end = optp->ipoptp_end;
2789 uint8_t *cur = optp->ipoptp_next;
2790 uint8_t opt, len, pointer;
2791
2792 /*
2793 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2794 * has been corrupted.
2795 */
2796 ASSERT(cur <= end);
2797
2798 if (cur == end)
2799 return (IPOPT_EOL);
2800
2801 opt = cur[IPOPT_OPTVAL];
2802
2803 /*
2804 * Skip any NOP options.
2805 */
2806 while (opt == IPOPT_NOP) {
2807 cur++;
2808 if (cur == end)
2809 return (IPOPT_EOL);
2810 opt = cur[IPOPT_OPTVAL];
2811 }
2812
2813 if (opt == IPOPT_EOL)
2814 return (IPOPT_EOL);
2815
2816 /*
2817 * Option requiring a length.
2818 */
2819 if ((cur + 1) >= end) {
2820 optp->ipoptp_flags |= IPOPTP_ERROR;
2821 return (IPOPT_EOL);
2822 }
2823 len = cur[IPOPT_OLEN];
2824 if (len < 2) {
2825 optp->ipoptp_flags |= IPOPTP_ERROR;
2826 return (IPOPT_EOL);
2827 }
2828 optp->ipoptp_cur = cur;
2829 optp->ipoptp_len = len;
2830 optp->ipoptp_next = cur + len;
2831 if (cur + len > end) {
2832 optp->ipoptp_flags |= IPOPTP_ERROR;
2833 return (IPOPT_EOL);
2834 }
2835
2836 /*
2837 * For the options which require a pointer field, make sure
2838 * its there, and make sure it points to either something
2839 * inside this option, or the end of the option.
2840 */
2841 switch (opt) {
2842 case IPOPT_RR:
2843 case IPOPT_TS:
2844 case IPOPT_LSRR:
2845 case IPOPT_SSRR:
2846 if (len <= IPOPT_OFFSET) {
2847 optp->ipoptp_flags |= IPOPTP_ERROR;
2848 return (opt);
2849 }
2850 pointer = cur[IPOPT_OFFSET];
2851 if (pointer - 1 > len) {
2852 optp->ipoptp_flags |= IPOPTP_ERROR;
2853 return (opt);
2854 }
2855 break;
2856 }
2857
2858 /*
2859 * Sanity check the pointer field based on the type of the
2860 * option.
2861 */
2862 switch (opt) {
2863 case IPOPT_RR:
2864 case IPOPT_SSRR:
2865 case IPOPT_LSRR:
2866 if (pointer < IPOPT_MINOFF_SR)
2867 optp->ipoptp_flags |= IPOPTP_ERROR;
2868 break;
2869 case IPOPT_TS:
2870 if (pointer < IPOPT_MINOFF_IT)
2871 optp->ipoptp_flags |= IPOPTP_ERROR;
2872 /*
2873 * Note that the Internet Timestamp option also
2874 * contains two four bit fields (the Overflow field,
2875 * and the Flag field), which follow the pointer
2876 * field. We don't need to check that these fields
2877 * fall within the length of the option because this
2878 * was implicitely done above. We've checked that the
2879 * pointer value is at least IPOPT_MINOFF_IT, and that
2880 * it falls within the option. Since IPOPT_MINOFF_IT >
2881 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2882 */
2883 ASSERT(len > IPOPT_POS_OV_FLG);
2884 break;
2885 }
2886
2887 return (opt);
2888 }
2889
2890 /*
2891 * Use the outgoing IP header to create an IP_OPTIONS option the way
2892 * it was passed down from the application.
2893 */
2894 int
2895 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf)
2896 {
2897 ipoptp_t opts;
2898 const uchar_t *opt;
2899 uint8_t optval;
2900 uint8_t optlen;
2901 uint32_t len = 0;
2902 uchar_t *buf1 = buf;
2903
2904 buf += IP_ADDR_LEN; /* Leave room for final destination */
2905 len += IP_ADDR_LEN;
2906 bzero(buf1, IP_ADDR_LEN);
2907
2908 /*
2909 * OK to cast away const here, as we don't store through the returned
2910 * opts.ipoptp_cur pointer.
2911 */
2912 for (optval = ipoptp_first(&opts, (ipha_t *)ipha);
2913 optval != IPOPT_EOL;
2914 optval = ipoptp_next(&opts)) {
2915 int off;
2916
2917 opt = opts.ipoptp_cur;
2918 optlen = opts.ipoptp_len;
2919 switch (optval) {
2920 case IPOPT_SSRR:
2921 case IPOPT_LSRR:
2922
2923 /*
2924 * Insert ipha_dst as the first entry in the source
2925 * route and move down the entries on step.
2926 * The last entry gets placed at buf1.
2927 */
2928 buf[IPOPT_OPTVAL] = optval;
2929 buf[IPOPT_OLEN] = optlen;
2930 buf[IPOPT_OFFSET] = optlen;
2931
2932 off = optlen - IP_ADDR_LEN;
2933 if (off < 0) {
2934 /* No entries in source route */
2935 break;
2936 }
2937 /* Last entry in source route */
2938 bcopy(opt + off, buf1, IP_ADDR_LEN);
2939 off -= IP_ADDR_LEN;
2940
2941 while (off > 0) {
2942 bcopy(opt + off,
2943 buf + off + IP_ADDR_LEN,
2944 IP_ADDR_LEN);
2945 off -= IP_ADDR_LEN;
2946 }
2947 /* ipha_dst into first slot */
2948 bcopy(&ipha->ipha_dst,
2949 buf + off + IP_ADDR_LEN,
2950 IP_ADDR_LEN);
2951 buf += optlen;
2952 len += optlen;
2953 break;
2954
2955 case IPOPT_COMSEC:
2956 case IPOPT_SECURITY:
2957 /* if passing up a label is not ok, then remove */
2958 if (is_system_labeled())
2959 break;
2960 /* FALLTHROUGH */
2961 default:
2962 bcopy(opt, buf, optlen);
2963 buf += optlen;
2964 len += optlen;
2965 break;
2966 }
2967 }
2968 done:
2969 /* Pad the resulting options */
2970 while (len & 0x3) {
2971 *buf++ = IPOPT_EOL;
2972 len++;
2973 }
2974 return (len);
2975 }
2976
2977 /*
2978 * Update any record route or timestamp options to include this host.
2979 * Reverse any source route option.
2980 * This routine assumes that the options are well formed i.e. that they
2981 * have already been checked.
2982 */
2983 static void
2984 icmp_options_update(ipha_t *ipha)
2985 {
2986 ipoptp_t opts;
2987 uchar_t *opt;
2988 uint8_t optval;
2989 ipaddr_t src; /* Our local address */
2990 ipaddr_t dst;
2991
2992 ip2dbg(("icmp_options_update\n"));
2993 src = ipha->ipha_src;
2994 dst = ipha->ipha_dst;
2995
2996 for (optval = ipoptp_first(&opts, ipha);
2997 optval != IPOPT_EOL;
2998 optval = ipoptp_next(&opts)) {
2999 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
3000 opt = opts.ipoptp_cur;
3001 ip2dbg(("icmp_options_update: opt %d, len %d\n",
3002 optval, opts.ipoptp_len));
3003 switch (optval) {
3004 int off1, off2;
3005 case IPOPT_SSRR:
3006 case IPOPT_LSRR:
3007 /*
3008 * Reverse the source route. The first entry
3009 * should be the next to last one in the current
3010 * source route (the last entry is our address).
3011 * The last entry should be the final destination.
3012 */
3013 off1 = IPOPT_MINOFF_SR - 1;
3014 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
3015 if (off2 < 0) {
3016 /* No entries in source route */
3017 ip1dbg((
3018 "icmp_options_update: bad src route\n"));
3019 break;
3020 }
3021 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
3022 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
3023 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
3024 off2 -= IP_ADDR_LEN;
3025
3026 while (off1 < off2) {
3027 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
3028 bcopy((char *)opt + off2, (char *)opt + off1,
3029 IP_ADDR_LEN);
3030 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
3031 off1 += IP_ADDR_LEN;
3032 off2 -= IP_ADDR_LEN;
3033 }
3034 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
3035 break;
3036 }
3037 }
3038 }
3039
3040 /*
3041 * Process received ICMP Redirect messages.
3042 */
3043 static void
3044 icmp_redirect(ill_t *ill, mblk_t *mp)
3045 {
3046 ipha_t *ipha;
3047 int iph_hdr_length;
3048 icmph_t *icmph;
3049 ipha_t *ipha_err;
3050 ire_t *ire;
3051 ire_t *prev_ire;
3052 ire_t *save_ire;
3053 ipaddr_t src, dst, gateway;
3054 iulp_t ulp_info = { 0 };
3055 int error;
3056 ip_stack_t *ipst;
3057
3058 ASSERT(ill != NULL);
3059 ipst = ill->ill_ipst;
3060
3061 ipha = (ipha_t *)mp->b_rptr;
3062 iph_hdr_length = IPH_HDR_LENGTH(ipha);
3063 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) <
3064 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) {
3065 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3066 freemsg(mp);
3067 return;
3068 }
3069 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
3070 ipha_err = (ipha_t *)&icmph[1];
3071 src = ipha->ipha_src;
3072 dst = ipha_err->ipha_dst;
3073 gateway = icmph->icmph_rd_gateway;
3074 /* Make sure the new gateway is reachable somehow. */
3075 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL,
3076 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
3077 /*
3078 * Make sure we had a route for the dest in question and that
3079 * that route was pointing to the old gateway (the source of the
3080 * redirect packet.)
3081 */
3082 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES,
3083 NULL, MATCH_IRE_GW, ipst);
3084 /*
3085 * Check that
3086 * the redirect was not from ourselves
3087 * the new gateway and the old gateway are directly reachable
3088 */
3089 if (!prev_ire ||
3090 !ire ||
3091 ire->ire_type == IRE_LOCAL) {
3092 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
3093 freemsg(mp);
3094 if (ire != NULL)
3095 ire_refrele(ire);
3096 if (prev_ire != NULL)
3097 ire_refrele(prev_ire);
3098 return;
3099 }
3100
3101 /*
3102 * Should we use the old ULP info to create the new gateway? From
3103 * a user's perspective, we should inherit the info so that it
3104 * is a "smooth" transition. If we do not do that, then new
3105 * connections going thru the new gateway will have no route metrics,
3106 * which is counter-intuitive to user. From a network point of
3107 * view, this may or may not make sense even though the new gateway
3108 * is still directly connected to us so the route metrics should not
3109 * change much.
3110 *
3111 * But if the old ire_uinfo is not initialized, we do another
3112 * recursive lookup on the dest using the new gateway. There may
3113 * be a route to that. If so, use it to initialize the redirect
3114 * route.
3115 */
3116 if (prev_ire->ire_uinfo.iulp_set) {
3117 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t));
3118 } else {
3119 ire_t *tmp_ire;
3120 ire_t *sire;
3121
3122 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire,
3123 ALL_ZONES, 0, NULL,
3124 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT),
3125 ipst);
3126 if (sire != NULL) {
3127 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t));
3128 /*
3129 * If sire != NULL, ire_ftable_lookup() should not
3130 * return a NULL value.
3131 */
3132 ASSERT(tmp_ire != NULL);
3133 ire_refrele(tmp_ire);
3134 ire_refrele(sire);
3135 } else if (tmp_ire != NULL) {
3136 bcopy(&tmp_ire->ire_uinfo, &ulp_info,
3137 sizeof (iulp_t));
3138 ire_refrele(tmp_ire);
3139 }
3140 }
3141 if (prev_ire->ire_type == IRE_CACHE)
3142 ire_delete(prev_ire);
3143 ire_refrele(prev_ire);
3144 /*
3145 * TODO: more precise handling for cases 0, 2, 3, the latter two
3146 * require TOS routing
3147 */
3148 switch (icmph->icmph_code) {
3149 case 0:
3150 case 1:
3151 /* TODO: TOS specificity for cases 2 and 3 */
3152 case 2:
3153 case 3:
3154 break;
3155 default:
3156 freemsg(mp);
3157 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
3158 ire_refrele(ire);
3159 return;
3160 }
3161 /*
3162 * Create a Route Association. This will allow us to remember that
3163 * someone we believe told us to use the particular gateway.
3164 */
3165 save_ire = ire;
3166 ire = ire_create(
3167 (uchar_t *)&dst, /* dest addr */
3168 (uchar_t *)&ip_g_all_ones, /* mask */
3169 (uchar_t *)&save_ire->ire_src_addr, /* source addr */
3170 (uchar_t *)&gateway, /* gateway addr */
3171 &save_ire->ire_max_frag, /* max frag */
3172 NULL, /* no src nce */
3173 NULL, /* no rfq */
3174 NULL, /* no stq */
3175 IRE_HOST,
3176 NULL, /* ipif */
3177 0, /* cmask */
3178 0, /* phandle */
3179 0, /* ihandle */
3180 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
3181 &ulp_info,
3182 NULL, /* tsol_gc_t */
3183 NULL, /* gcgrp */
3184 ipst);
3185
3186 if (ire == NULL) {
3187 freemsg(mp);
3188 ire_refrele(save_ire);
3189 return;
3190 }
3191 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE);
3192 ire_refrele(save_ire);
3193 atomic_inc_32(&ipst->ips_ip_redirect_cnt);
3194
3195 if (error == 0) {
3196 ire_refrele(ire); /* Held in ire_add_v4 */
3197 /* tell routing sockets that we received a redirect */
3198 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
3199 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
3200 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
3201 }
3202
3203 /*
3204 * Delete any existing IRE_HOST type redirect ires for this destination.
3205 * This together with the added IRE has the effect of
3206 * modifying an existing redirect.
3207 */
3208 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL,
3209 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst);
3210 if (prev_ire != NULL) {
3211 if (prev_ire ->ire_flags & RTF_DYNAMIC)
3212 ire_delete(prev_ire);
3213 ire_refrele(prev_ire);
3214 }
3215
3216 freemsg(mp);
3217 }
3218
3219 /*
3220 * Generate an ICMP parameter problem message.
3221 */
3222 static void
3223 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid,
3224 ip_stack_t *ipst)
3225 {
3226 icmph_t icmph;
3227 boolean_t mctl_present;
3228 mblk_t *first_mp;
3229
3230 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
3231
3232 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3233 if (mctl_present)
3234 freeb(first_mp);
3235 return;
3236 }
3237
3238 bzero(&icmph, sizeof (icmph_t));
3239 icmph.icmph_type = ICMP_PARAM_PROBLEM;
3240 icmph.icmph_pp_ptr = ptr;
3241 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
3242 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid,
3243 ipst);
3244 }
3245
3246 /*
3247 * Build and ship an IPv4 ICMP message using the packet data in mp, and
3248 * the ICMP header pointed to by "stuff". (May be called as writer.)
3249 * Note: assumes that icmp_pkt_err_ok has been called to verify that
3250 * an icmp error packet can be sent.
3251 * Assigns an appropriate source address to the packet. If ipha_dst is
3252 * one of our addresses use it for source. Otherwise pick a source based
3253 * on a route lookup back to ipha_src.
3254 * Note that ipha_src must be set here since the
3255 * packet is likely to arrive on an ill queue in ip_wput() which will
3256 * not set a source address.
3257 */
3258 static void
3259 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len,
3260 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst)
3261 {
3262 ipaddr_t dst;
3263 icmph_t *icmph;
3264 ipha_t *ipha;
3265 uint_t len_needed;
3266 size_t msg_len;
3267 mblk_t *mp1;
3268 ipaddr_t src;
3269 ire_t *ire;
3270 mblk_t *ipsec_mp;
3271 ipsec_out_t *io = NULL;
3272
3273 if (mctl_present) {
3274 /*
3275 * If it is :
3276 *
3277 * 1) a IPSEC_OUT, then this is caused by outbound
3278 * datagram originating on this host. IPsec processing
3279 * may or may not have been done. Refer to comments above
3280 * icmp_inbound_error_fanout for details.
3281 *
3282 * 2) a IPSEC_IN if we are generating a icmp_message
3283 * for an incoming datagram destined for us i.e called
3284 * from ip_fanout_send_icmp.
3285 */
3286 ipsec_info_t *in;
3287 ipsec_mp = mp;
3288 mp = ipsec_mp->b_cont;
3289
3290 in = (ipsec_info_t *)ipsec_mp->b_rptr;
3291 ipha = (ipha_t *)mp->b_rptr;
3292
3293 ASSERT(in->ipsec_info_type == IPSEC_OUT ||
3294 in->ipsec_info_type == IPSEC_IN);
3295
3296 if (in->ipsec_info_type == IPSEC_IN) {
3297 /*
3298 * Convert the IPSEC_IN to IPSEC_OUT.
3299 */
3300 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) {
3301 BUMP_MIB(&ipst->ips_ip_mib,
3302 ipIfStatsOutDiscards);
3303 return;
3304 }
3305 io = (ipsec_out_t *)ipsec_mp->b_rptr;
3306 } else {
3307 ASSERT(in->ipsec_info_type == IPSEC_OUT);
3308 io = (ipsec_out_t *)in;
3309 /*
3310 * Clear out ipsec_out_proc_begin, so we do a fresh
3311 * ire lookup.
3312 */
3313 io->ipsec_out_proc_begin = B_FALSE;
3314 }
3315 ASSERT(zoneid == io->ipsec_out_zoneid);
3316 ASSERT(zoneid != ALL_ZONES);
3317 } else {
3318 /*
3319 * This is in clear. The icmp message we are building
3320 * here should go out in clear.
3321 *
3322 * Pardon the convolution of it all, but it's easier to
3323 * allocate a "use cleartext" IPSEC_IN message and convert
3324 * it than it is to allocate a new one.
3325 */
3326 ipsec_in_t *ii;
3327 ASSERT(DB_TYPE(mp) == M_DATA);
3328 ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack);
3329 if (ipsec_mp == NULL) {
3330 freemsg(mp);
3331 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
3332 return;
3333 }
3334 ii = (ipsec_in_t *)ipsec_mp->b_rptr;
3335
3336 /* This is not a secure packet */
3337 ii->ipsec_in_secure = B_FALSE;
3338 /*
3339 * For trusted extensions using a shared IP address we can
3340 * send using any zoneid.
3341 */
3342 if (zoneid == ALL_ZONES)
3343 ii->ipsec_in_zoneid = GLOBAL_ZONEID;
3344 else
3345 ii->ipsec_in_zoneid = zoneid;
3346 ipsec_mp->b_cont = mp;
3347 ipha = (ipha_t *)mp->b_rptr;
3348 /*
3349 * Convert the IPSEC_IN to IPSEC_OUT.
3350 */
3351 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) {
3352 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
3353 return;
3354 }
3355 io = (ipsec_out_t *)ipsec_mp->b_rptr;
3356 }
3357
3358 /* Remember our eventual destination */
3359 dst = ipha->ipha_src;
3360
3361 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK),
3362 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst);
3363 if (ire != NULL &&
3364 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) {
3365 src = ipha->ipha_dst;
3366 } else {
3367 if (ire != NULL)
3368 ire_refrele(ire);
3369 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL,
3370 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY),
3371 ipst);
3372 if (ire == NULL) {
3373 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes);
3374 freemsg(ipsec_mp);
3375 return;
3376 }
3377 src = ire->ire_src_addr;
3378 }
3379
3380 if (ire != NULL)
3381 ire_refrele(ire);
3382
3383 /*
3384 * Check if we can send back more then 8 bytes in addition to
3385 * the IP header. We try to send 64 bytes of data and the internal
3386 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
3387 */
3388 len_needed = IPH_HDR_LENGTH(ipha);
3389 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
3390 ipha->ipha_protocol == IPPROTO_IPV6) {
3391
3392 if (!pullupmsg(mp, -1)) {
3393 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
3394 freemsg(ipsec_mp);
3395 return;
3396 }
3397 ipha = (ipha_t *)mp->b_rptr;
3398
3399 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
3400 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
3401 len_needed));
3402 } else {
3403 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
3404
3405 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
3406 len_needed += ip_hdr_length_v6(mp, ip6h);
3407 }
3408 }
3409 len_needed += ipst->ips_ip_icmp_return;
3410 msg_len = msgdsize(mp);
3411 if (msg_len > len_needed) {
3412 (void) adjmsg(mp, len_needed - msg_len);
3413 msg_len = len_needed;
3414 }
3415 mp1 = allocb_tmpl(sizeof (icmp_ipha) + len, mp);
3416 if (mp1 == NULL) {
3417 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
3418 freemsg(ipsec_mp);
3419 return;
3420 }
3421 mp1->b_cont = mp;
3422 mp = mp1;
3423 ASSERT(ipsec_mp->b_datap->db_type == M_CTL &&
3424 ipsec_mp->b_rptr == (uint8_t *)io &&
3425 io->ipsec_out_type == IPSEC_OUT);
3426 ipsec_mp->b_cont = mp;
3427
3428 /*
3429 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this
3430 * node generates be accepted in peace by all on-host destinations.
3431 * If we do NOT assume that all on-host destinations trust
3432 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
3433 * (Look for ipsec_out_icmp_loopback).
3434 */
3435 io->ipsec_out_icmp_loopback = B_TRUE;
3436
3437 ipha = (ipha_t *)mp->b_rptr;
3438 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
3439 *ipha = icmp_ipha;
3440 ipha->ipha_src = src;
3441 ipha->ipha_dst = dst;
3442 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
3443 msg_len += sizeof (icmp_ipha) + len;
3444 if (msg_len > IP_MAXPACKET) {
3445 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
3446 msg_len = IP_MAXPACKET;
3447 }
3448 ipha->ipha_length = htons((uint16_t)msg_len);
3449 icmph = (icmph_t *)&ipha[1];
3450 bcopy(stuff, icmph, len);
3451 icmph->icmph_checksum = 0;
3452 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
3453 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
3454 put(q, ipsec_mp);
3455 }
3456
3457 /*
3458 * Determine if an ICMP error packet can be sent given the rate limit.
3459 * The limit consists of an average frequency (icmp_pkt_err_interval measured
3460 * in milliseconds) and a burst size. Burst size number of packets can
3461 * be sent arbitrarely closely spaced.
3462 * The state is tracked using two variables to implement an approximate
3463 * token bucket filter:
3464 * icmp_pkt_err_last - lbolt value when the last burst started
3465 * icmp_pkt_err_sent - number of packets sent in current burst
3466 */
3467 boolean_t
3468 icmp_err_rate_limit(ip_stack_t *ipst)
3469 {
3470 clock_t now = TICK_TO_MSEC(lbolt);
3471 uint_t refilled; /* Number of packets refilled in tbf since last */
3472 /* Guard against changes by loading into local variable */
3473 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
3474
3475 if (err_interval == 0)
3476 return (B_FALSE);
3477
3478 if (ipst->ips_icmp_pkt_err_last > now) {
3479 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
3480 ipst->ips_icmp_pkt_err_last = 0;
3481 ipst->ips_icmp_pkt_err_sent = 0;
3482 }
3483 /*
3484 * If we are in a burst update the token bucket filter.
3485 * Update the "last" time to be close to "now" but make sure
3486 * we don't loose precision.
3487 */
3488 if (ipst->ips_icmp_pkt_err_sent != 0) {
3489 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
3490 if (refilled > ipst->ips_icmp_pkt_err_sent) {
3491 ipst->ips_icmp_pkt_err_sent = 0;
3492 } else {
3493 ipst->ips_icmp_pkt_err_sent -= refilled;
3494 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
3495 }
3496 }
3497 if (ipst->ips_icmp_pkt_err_sent == 0) {
3498 /* Start of new burst */
3499 ipst->ips_icmp_pkt_err_last = now;
3500 }
3501 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
3502 ipst->ips_icmp_pkt_err_sent++;
3503 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
3504 ipst->ips_icmp_pkt_err_sent));
3505 return (B_FALSE);
3506 }
3507 ip1dbg(("icmp_err_rate_limit: dropped\n"));
3508 return (B_TRUE);
3509 }
3510
3511 /*
3512 * Check if it is ok to send an IPv4 ICMP error packet in
3513 * response to the IPv4 packet in mp.
3514 * Free the message and return null if no
3515 * ICMP error packet should be sent.
3516 */
3517 static mblk_t *
3518 icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst)
3519 {
3520 icmph_t *icmph;
3521 ipha_t *ipha;
3522 uint_t len_needed;
3523 ire_t *src_ire;
3524 ire_t *dst_ire;
3525
3526 if (!mp)
3527 return (NULL);
3528 ipha = (ipha_t *)mp->b_rptr;
3529 if (ip_csum_hdr(ipha)) {
3530 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
3531 freemsg(mp);
3532 return (NULL);
3533 }
3534 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST,
3535 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
3536 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST,
3537 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
3538 if (src_ire != NULL || dst_ire != NULL ||
3539 CLASSD(ipha->ipha_dst) ||
3540 CLASSD(ipha->ipha_src) ||
3541 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
3542 /* Note: only errors to the fragment with offset 0 */
3543 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3544 freemsg(mp);
3545 if (src_ire != NULL)
3546 ire_refrele(src_ire);
3547 if (dst_ire != NULL)
3548 ire_refrele(dst_ire);
3549 return (NULL);
3550 }
3551 if (ipha->ipha_protocol == IPPROTO_ICMP) {
3552 /*
3553 * Check the ICMP type. RFC 1122 sez: don't send ICMP
3554 * errors in response to any ICMP errors.
3555 */
3556 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3557 if (mp->b_wptr - mp->b_rptr < len_needed) {
3558 if (!pullupmsg(mp, len_needed)) {
3559 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3560 freemsg(mp);
3561 return (NULL);
3562 }
3563 ipha = (ipha_t *)mp->b_rptr;
3564 }
3565 icmph = (icmph_t *)
3566 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3567 switch (icmph->icmph_type) {
3568 case ICMP_DEST_UNREACHABLE:
3569 case ICMP_SOURCE_QUENCH:
3570 case ICMP_TIME_EXCEEDED:
3571 case ICMP_PARAM_PROBLEM:
3572 case ICMP_REDIRECT:
3573 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3574 freemsg(mp);
3575 return (NULL);
3576 default:
3577 break;
3578 }
3579 }
3580 /*
3581 * If this is a labeled system, then check to see if we're allowed to
3582 * send a response to this particular sender. If not, then just drop.
3583 */
3584 if (is_system_labeled() && !tsol_can_reply_error(mp)) {
3585 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3586 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3587 freemsg(mp);
3588 return (NULL);
3589 }
3590 if (icmp_err_rate_limit(ipst)) {
3591 /*
3592 * Only send ICMP error packets every so often.
3593 * This should be done on a per port/source basis,
3594 * but for now this will suffice.
3595 */
3596 freemsg(mp);
3597 return (NULL);
3598 }
3599 return (mp);
3600 }
3601
3602 /*
3603 * Generate an ICMP redirect message.
3604 */
3605 static void
3606 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst)
3607 {
3608 icmph_t icmph;
3609
3610 /*
3611 * We are called from ip_rput where we could
3612 * not have attached an IPSEC_IN.
3613 */
3614 ASSERT(mp->b_datap->db_type == M_DATA);
3615
3616 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3617 return;
3618 }
3619
3620 bzero(&icmph, sizeof (icmph_t));
3621 icmph.icmph_type = ICMP_REDIRECT;
3622 icmph.icmph_code = 1;
3623 icmph.icmph_rd_gateway = gateway;
3624 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3625 /* Redirects sent by router, and router is global zone */
3626 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst);
3627 }
3628
3629 /*
3630 * Generate an ICMP time exceeded message.
3631 */
3632 void
3633 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid,
3634 ip_stack_t *ipst)
3635 {
3636 icmph_t icmph;
3637 boolean_t mctl_present;
3638 mblk_t *first_mp;
3639
3640 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
3641
3642 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3643 if (mctl_present)
3644 freeb(first_mp);
3645 return;
3646 }
3647
3648 bzero(&icmph, sizeof (icmph_t));
3649 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3650 icmph.icmph_code = code;
3651 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3652 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid,
3653 ipst);
3654 }
3655
3656 /*
3657 * Generate an ICMP unreachable message.
3658 */
3659 void
3660 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid,
3661 ip_stack_t *ipst)
3662 {
3663 icmph_t icmph;
3664 mblk_t *first_mp;
3665 boolean_t mctl_present;
3666
3667 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
3668
3669 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3670 if (mctl_present)
3671 freeb(first_mp);
3672 return;
3673 }
3674
3675 bzero(&icmph, sizeof (icmph_t));
3676 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3677 icmph.icmph_code = code;
3678 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3679 ip2dbg(("send icmp destination unreachable code %d\n", code));
3680 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present,
3681 zoneid, ipst);
3682 }
3683
3684 /*
3685 * Attempt to start recovery of an IPv4 interface that's been shut down as a
3686 * duplicate. As long as someone else holds the address, the interface will
3687 * stay down. When that conflict goes away, the interface is brought back up.
3688 * This is done so that accidental shutdowns of addresses aren't made
3689 * permanent. Your server will recover from a failure.
3690 *
3691 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a
3692 * user space process (dhcpagent).
3693 *
3694 * Recovery completes if ARP reports that the address is now ours (via
3695 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation.
3696 *
3697 * This function is entered on a timer expiry; the ID is in ipif_recovery_id.
3698 */
3699 static void
3700 ipif_dup_recovery(void *arg)
3701 {
3702 ipif_t *ipif = arg;
3703 ill_t *ill = ipif->ipif_ill;
3704 mblk_t *arp_add_mp;
3705 mblk_t *arp_del_mp;
3706 area_t *area;
3707 ip_stack_t *ipst = ill->ill_ipst;
3708
3709 ipif->ipif_recovery_id = 0;
3710
3711 /*
3712 * No lock needed for moving or condemned check, as this is just an
3713 * optimization.
3714 */
3715 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) ||
3716 (ipif->ipif_flags & IPIF_POINTOPOINT) ||
3717 (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) {
3718 /* No reason to try to bring this address back. */
3719 return;
3720 }
3721
3722 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL)
3723 goto alloc_fail;
3724
3725 if (ipif->ipif_arp_del_mp == NULL) {
3726 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
3727 goto alloc_fail;
3728 ipif->ipif_arp_del_mp = arp_del_mp;
3729 }
3730
3731 /* Setting the 'unverified' flag restarts DAD */
3732 area = (area_t *)arp_add_mp->b_rptr;
3733 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
3734 ACE_F_UNVERIFIED;
3735 putnext(ill->ill_rq, arp_add_mp);
3736 return;
3737
3738 alloc_fail:
3739 /*
3740 * On allocation failure, just restart the timer. Note that the ipif
3741 * is down here, so no other thread could be trying to start a recovery
3742 * timer. The ill_lock protects the condemned flag and the recovery
3743 * timer ID.
3744 */
3745 freemsg(arp_add_mp);
3746 mutex_enter(&ill->ill_lock);
3747 if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 &&
3748 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
3749 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif,
3750 MSEC_TO_TICK(ipst->ips_ip_dup_recovery));
3751 }
3752 mutex_exit(&ill->ill_lock);
3753 }
3754
3755 /*
3756 * This is for exclusive changes due to ARP. Either tear down an interface due
3757 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery.
3758 */
3759 /* ARGSUSED */
3760 static void
3761 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg)
3762 {
3763 ill_t *ill = rq->q_ptr;
3764 arh_t *arh;
3765 ipaddr_t src;
3766 ipif_t *ipif;
3767 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */
3768 char hbuf[MAC_STR_LEN];
3769 char sbuf[INET_ADDRSTRLEN];
3770 const char *failtype;
3771 boolean_t bring_up;
3772 ip_stack_t *ipst = ill->ill_ipst;
3773
3774 switch (((arcn_t *)mp->b_rptr)->arcn_code) {
3775 case AR_CN_READY:
3776 failtype = NULL;
3777 bring_up = B_TRUE;
3778 break;
3779 case AR_CN_FAILED:
3780 failtype = "in use";
3781 bring_up = B_FALSE;
3782 break;
3783 default:
3784 failtype = "claimed";
3785 bring_up = B_FALSE;
3786 break;
3787 }
3788
3789 arh = (arh_t *)mp->b_cont->b_rptr;
3790 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN);
3791
3792 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf,
3793 sizeof (hbuf));
3794 (void) ip_dot_addr(src, sbuf);
3795 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3796
3797 if ((ipif->ipif_flags & IPIF_POINTOPOINT) ||
3798 ipif->ipif_lcl_addr != src) {
3799 continue;
3800 }
3801
3802 /*
3803 * If we failed on a recovery probe, then restart the timer to
3804 * try again later.
3805 */
3806 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) &&
3807 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) &&
3808 ill->ill_net_type == IRE_IF_RESOLVER &&
3809 !(ipif->ipif_state_flags & IPIF_CONDEMNED) &&
3810 ipst->ips_ip_dup_recovery > 0 &&
3811 ipif->ipif_recovery_id == 0) {
3812 ipif->ipif_recovery_id = timeout(ipif_dup_recovery,
3813 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery));
3814 continue;
3815 }
3816
3817 /*
3818 * If what we're trying to do has already been done, then do
3819 * nothing.
3820 */
3821 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0))
3822 continue;
3823
3824 ipif_get_name(ipif, ibuf, sizeof (ibuf));
3825
3826 if (failtype == NULL) {
3827 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf,
3828 ibuf);
3829 } else {
3830 cmn_err(CE_WARN, "%s has duplicate address %s (%s "
3831 "by %s); disabled", ibuf, sbuf, failtype, hbuf);
3832 }
3833
3834 if (bring_up) {
3835 ASSERT(ill->ill_dl_up);
3836 /*
3837 * Free up the ARP delete message so we can allocate
3838 * a fresh one through the normal path.
3839 */
3840 freemsg(ipif->ipif_arp_del_mp);
3841 ipif->ipif_arp_del_mp = NULL;
3842 if (ipif_resolver_up(ipif, Res_act_initial) !=
3843 EINPROGRESS) {
3844 ipif->ipif_addr_ready = 1;
3845 (void) ipif_up_done(ipif);
3846 }
3847 continue;
3848 }
3849
3850 mutex_enter(&ill->ill_lock);
3851 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
3852 ipif->ipif_flags |= IPIF_DUPLICATE;
3853 ill->ill_ipif_dup_count++;
3854 mutex_exit(&ill->ill_lock);
3855 /*
3856 * Already exclusive on the ill; no need to handle deferred
3857 * processing here.
3858 */
3859 (void) ipif_down(ipif, NULL, NULL);
3860 ipif_down_tail(ipif);
3861 mutex_enter(&ill->ill_lock);
3862 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) &&
3863 ill->ill_net_type == IRE_IF_RESOLVER &&
3864 !(ipif->ipif_state_flags & IPIF_CONDEMNED) &&
3865 ipst->ips_ip_dup_recovery > 0) {
3866 ipif->ipif_recovery_id = timeout(ipif_dup_recovery,
3867 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery));
3868 }
3869 mutex_exit(&ill->ill_lock);
3870 }
3871 freemsg(mp);
3872 }
3873
3874 /* ARGSUSED */
3875 static void
3876 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg)
3877 {
3878 ill_t *ill = rq->q_ptr;
3879 arh_t *arh;
3880 ipaddr_t src;
3881 ipif_t *ipif;
3882
3883 arh = (arh_t *)mp->b_cont->b_rptr;
3884 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN);
3885 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3886 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src)
3887 (void) ipif_resolver_up(ipif, Res_act_defend);
3888 }
3889 freemsg(mp);
3890 }
3891
3892 /*
3893 * News from ARP. ARP sends notification of interesting events down
3894 * to its clients using M_CTL messages with the interesting ARP packet
3895 * attached via b_cont.
3896 * The interesting event from a device comes up the corresponding ARP-IP-DEV
3897 * queue as opposed to ARP sending the message to all the clients, i.e. all
3898 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache
3899 * table if a cache IRE is found to delete all the entries for the address in
3900 * the packet.
3901 */
3902 static void
3903 ip_arp_news(queue_t *q, mblk_t *mp)
3904 {
3905 arcn_t *arcn;
3906 arh_t *arh;
3907 ire_t *ire = NULL;
3908 char hbuf[MAC_STR_LEN];
3909 char sbuf[INET_ADDRSTRLEN];
3910 ipaddr_t src;
3911 in6_addr_t v6src;
3912 boolean_t isv6 = B_FALSE;
3913 ipif_t *ipif;
3914 ill_t *ill;
3915 ip_stack_t *ipst;
3916
3917 if (CONN_Q(q)) {
3918 conn_t *connp = Q_TO_CONN(q);
3919
3920 ipst = connp->conn_netstack->netstack_ip;
3921 } else {
3922 ill_t *ill = (ill_t *)q->q_ptr;
3923
3924 ipst = ill->ill_ipst;
3925 }
3926
3927 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) {
3928 if (q->q_next) {
3929 putnext(q, mp);
3930 } else
3931 freemsg(mp);
3932 return;
3933 }
3934 arh = (arh_t *)mp->b_cont->b_rptr;
3935 /* Is it one we are interested in? */
3936 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) {
3937 isv6 = B_TRUE;
3938 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src,
3939 IPV6_ADDR_LEN);
3940 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) {
3941 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src,
3942 IP_ADDR_LEN);
3943 } else {
3944 freemsg(mp);
3945 return;
3946 }
3947
3948 ill = q->q_ptr;
3949
3950 arcn = (arcn_t *)mp->b_rptr;
3951 switch (arcn->arcn_code) {
3952 case AR_CN_BOGON:
3953 /*
3954 * Someone is sending ARP packets with a source protocol
3955 * address that we have published and for which we believe our
3956 * entry is authoritative and (when ill_arp_extend is set)
3957 * verified to be unique on the network.
3958 *
3959 * The ARP module internally handles the cases where the sender
3960 * is just probing (for DAD) and where the hardware address of
3961 * a non-authoritative entry has changed. Thus, these are the
3962 * real conflicts, and we have to do resolution.
3963 *
3964 * We back away quickly from the address if it's from DHCP or
3965 * otherwise temporary and hasn't been used recently (or at
3966 * all). We'd like to include "deprecated" addresses here as
3967 * well (as there's no real reason to defend something we're
3968 * discarding), but IPMP "reuses" this flag to mean something
3969 * other than the standard meaning.
3970 *
3971 * If the ARP module above is not extended (meaning that it
3972 * doesn't know how to defend the address), then we just log
3973 * the problem as we always did and continue on. It's not
3974 * right, but there's little else we can do, and those old ATM
3975 * users are going away anyway.
3976 */
3977 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen,
3978 hbuf, sizeof (hbuf));
3979 (void) ip_dot_addr(src, sbuf);
3980 if (isv6) {
3981 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL,
3982 ipst);
3983 } else {
3984 ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst);
3985 }
3986 if (ire != NULL && IRE_IS_LOCAL(ire)) {
3987 uint32_t now;
3988 uint32_t maxage;
3989 clock_t lused;
3990 uint_t maxdefense;
3991 uint_t defs;
3992
3993 /*
3994 * First, figure out if this address hasn't been used
3995 * in a while. If it hasn't, then it's a better
3996 * candidate for abandoning.
3997 */
3998 ipif = ire->ire_ipif;
3999 ASSERT(ipif != NULL);
4000 now = gethrestime_sec();
4001 maxage = now - ire->ire_create_time;
4002 if (maxage > ipst->ips_ip_max_temp_idle)
4003 maxage = ipst->ips_ip_max_temp_idle;
4004 lused = drv_hztousec(ddi_get_lbolt() -
4005 ire->ire_last_used_time) / MICROSEC + 1;
4006 if (lused >= maxage && (ipif->ipif_flags &
4007 (IPIF_DHCPRUNNING | IPIF_TEMPORARY)))
4008 maxdefense = ipst->ips_ip_max_temp_defend;
4009 else
4010 maxdefense = ipst->ips_ip_max_defend;
4011
4012 /*
4013 * Now figure out how many times we've defended
4014 * ourselves. Ignore defenses that happened long in
4015 * the past.
4016 */
4017 mutex_enter(&ire->ire_lock);
4018 if ((defs = ire->ire_defense_count) > 0 &&
4019 now - ire->ire_defense_time >
4020 ipst->ips_ip_defend_interval) {
4021 ire->ire_defense_count = defs = 0;
4022 }
4023 ire->ire_defense_count++;
4024 ire->ire_defense_time = now;
4025 mutex_exit(&ire->ire_lock);
4026 ill_refhold(ill);
4027 ire_refrele(ire);
4028
4029 /*
4030 * If we've defended ourselves too many times already,
4031 * then give up and tear down the interface(s) using
4032 * this address. Otherwise, defend by sending out a
4033 * gratuitous ARP.
4034 */
4035 if (defs >= maxdefense && ill->ill_arp_extend) {
4036 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP,
4037 B_FALSE);
4038 } else {
4039 cmn_err(CE_WARN,
4040 "node %s is using our IP address %s on %s",
4041 hbuf, sbuf, ill->ill_name);
4042 /*
4043 * If this is an old (ATM) ARP module, then
4044 * don't try to defend the address. Remain
4045 * compatible with the old behavior. Defend
4046 * only with new ARP.
4047 */
4048 if (ill->ill_arp_extend) {
4049 qwriter_ip(ill, q, mp, ip_arp_defend,
4050 NEW_OP, B_FALSE);
4051 } else {
4052 ill_refrele(ill);
4053 }
4054 }
4055 return;
4056 }
4057 cmn_err(CE_WARN,
4058 "proxy ARP problem? Node '%s' is using %s on %s",
4059 hbuf, sbuf, ill->ill_name);
4060 if (ire != NULL)
4061 ire_refrele(ire);
4062 break;
4063 case AR_CN_ANNOUNCE:
4064 if (isv6) {
4065 /*
4066 * For XRESOLV interfaces.
4067 * Delete the IRE cache entry and NCE for this
4068 * v6 address
4069 */
4070 ip_ire_clookup_and_delete_v6(&v6src, ipst);
4071 /*
4072 * If v6src is a non-zero, it's a router address
4073 * as below. Do the same sort of thing to clean
4074 * out off-net IRE_CACHE entries that go through
4075 * the router.
4076 */
4077 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) {
4078 ire_walk_v6(ire_delete_cache_gw_v6,
4079 (char *)&v6src, ALL_ZONES, ipst);
4080 }
4081 } else {
4082 nce_hw_map_t hwm;
4083
4084 /*
4085 * ARP gives us a copy of any packet where it thinks
4086 * the address has changed, so that we can update our
4087 * caches. We're responsible for caching known answers
4088 * in the current design. We check whether the
4089 * hardware address really has changed in all of our
4090 * entries that have cached this mapping, and if so, we
4091 * blow them away. This way we will immediately pick
4092 * up the rare case of a host changing hardware
4093 * address.
4094 */
4095 if (src == 0)
4096 break;
4097 hwm.hwm_addr = src;
4098 hwm.hwm_hwlen = arh->arh_hlen;
4099 hwm.hwm_hwaddr = (uchar_t *)(arh + 1);
4100 NDP_HW_CHANGE_INCR(ipst->ips_ndp4);
4101 ndp_walk_common(ipst->ips_ndp4, NULL,
4102 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES);
4103 NDP_HW_CHANGE_DECR(ipst->ips_ndp4);
4104 }
4105 break;
4106 case AR_CN_READY:
4107 /* No external v6 resolver has a contract to use this */
4108 if (isv6)
4109 break;
4110 /* If the link is down, we'll retry this later */
4111 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING))
4112 break;
4113 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL,
4114 NULL, NULL, ipst);
4115 if (ipif != NULL) {
4116 /*
4117 * If this is a duplicate recovery, then we now need to
4118 * go exclusive to bring this thing back up.
4119 */
4120 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) ==
4121 IPIF_DUPLICATE) {
4122 ipif_refrele(ipif);
4123 ill_refhold(ill);
4124 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP,
4125 B_FALSE);
4126 return;
4127 }
4128 /*
4129 * If this is the first notice that this address is
4130 * ready, then let the user know now.
4131 */
4132 if ((ipif->ipif_flags & IPIF_UP) &&
4133 !ipif->ipif_addr_ready) {
4134 ipif_mask_reply(ipif);
4135 ip_rts_ifmsg(ipif);
4136 ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
4137 sctp_update_ipif(ipif, SCTP_IPIF_UP);
4138 }
4139 ipif->ipif_addr_ready = 1;
4140 ipif_refrele(ipif);
4141 }
4142 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp), ipst);
4143 if (ire != NULL) {
4144 ire->ire_defense_count = 0;
4145 ire_refrele(ire);
4146 }
4147 break;
4148 case AR_CN_FAILED:
4149 /* No external v6 resolver has a contract to use this */
4150 if (isv6)
4151 break;
4152 ill_refhold(ill);
4153 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE);
4154 return;
4155 }
4156 freemsg(mp);
4157 }
4158
4159 /*
4160 * Create a mblk suitable for carrying the interface index and/or source link
4161 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used
4162 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user
4163 * application.
4164 */
4165 mblk_t *
4166 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid,
4167 ip_stack_t *ipst)
4168 {
4169 mblk_t *mp;
4170 ip_pktinfo_t *pinfo;
4171 ipha_t *ipha;
4172 struct ether_header *pether;
4173
4174 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED);
4175 if (mp == NULL) {
4176 ip1dbg(("ip_add_info: allocation failure.\n"));
4177 return (data_mp);
4178 }
4179
4180 ipha = (ipha_t *)data_mp->b_rptr;
4181 pinfo = (ip_pktinfo_t *)mp->b_rptr;
4182 bzero(pinfo, sizeof (ip_pktinfo_t));
4183 pinfo->ip_pkt_flags = (uchar_t)flags;
4184 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */
4185
4186 if (flags & (IPF_RECVIF | IPF_RECVADDR))
4187 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex;
4188 if (flags & IPF_RECVADDR) {
4189 ipif_t *ipif;
4190 ire_t *ire;
4191
4192 /*
4193 * Only valid for V4
4194 */
4195 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) ==
4196 (IPV4_VERSION << 4));
4197
4198 ipif = ipif_get_next_ipif(NULL, ill);
4199 if (ipif != NULL) {
4200 /*
4201 * Since a decision has already been made to deliver the
4202 * packet, there is no need to test for SECATTR and
4203 * ZONEONLY.
4204 * When a multicast packet is transmitted
4205 * a cache entry is created for the multicast address.
4206 * When delivering a copy of the packet or when new
4207 * packets are received we do not want to match on the
4208 * cached entry so explicitly match on
4209 * IRE_LOCAL and IRE_LOOPBACK
4210 */
4211 ire = ire_ctable_lookup(ipha->ipha_dst, 0,
4212 IRE_LOCAL | IRE_LOOPBACK,
4213 ipif, zoneid, NULL,
4214 MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP, ipst);
4215 if (ire == NULL) {
4216 /*
4217 * packet must have come on a different
4218 * interface.
4219 * Since a decision has already been made to
4220 * deliver the packet, there is no need to test
4221 * for SECATTR and ZONEONLY.
4222 * Only match on local and broadcast ire's.
4223 * See detailed comment above.
4224 */
4225 ire = ire_ctable_lookup(ipha->ipha_dst, 0,
4226 IRE_LOCAL | IRE_LOOPBACK, ipif, zoneid,
4227 NULL, MATCH_IRE_TYPE, ipst);
4228 }
4229
4230 if (ire == NULL) {
4231 /*
4232 * This is either a multicast packet or
4233 * the address has been removed since
4234 * the packet was received.
4235 * Return INADDR_ANY so that normal source
4236 * selection occurs for the response.
4237 */
4238
4239 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY;
4240 } else {
4241 pinfo->ip_pkt_match_addr.s_addr =
4242 ire->ire_src_addr;
4243 ire_refrele(ire);
4244 }
4245 ipif_refrele(ipif);
4246 } else {
4247 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY;
4248 }
4249 }
4250
4251 pether = (struct ether_header *)((char *)ipha
4252 - sizeof (struct ether_header));
4253 /*
4254 * Make sure the interface is an ethernet type, since this option
4255 * is currently supported only on this type of interface. Also make
4256 * sure we are pointing correctly above db_base.
4257 */
4258
4259 if ((flags & IPF_RECVSLLA) &&
4260 ((uchar_t *)pether >= data_mp->b_datap->db_base) &&
4261 (ill->ill_type == IFT_ETHER) &&
4262 (ill->ill_net_type == IRE_IF_RESOLVER)) {
4263
4264 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER;
4265 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet,
4266 (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL);
4267 } else {
4268 /*
4269 * Clear the bit. Indicate to upper layer that IP is not
4270 * sending this ancillary info.
4271 */
4272 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA;
4273 }
4274
4275 mp->b_datap->db_type = M_CTL;
4276 mp->b_wptr += sizeof (ip_pktinfo_t);
4277 mp->b_cont = data_mp;
4278
4279 return (mp);
4280 }
4281
4282 /*
4283 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as
4284 * part of the bind request.
4285 */
4286
4287 boolean_t
4288 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp)
4289 {
4290 ipsec_in_t *ii;
4291
4292 ASSERT(policy_mp != NULL);
4293 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET);
4294
4295 ii = (ipsec_in_t *)policy_mp->b_rptr;
4296 ASSERT(ii->ipsec_in_type == IPSEC_IN);
4297
4298 connp->conn_policy = ii->ipsec_in_policy;
4299 ii->ipsec_in_policy = NULL;
4300
4301 if (ii->ipsec_in_action != NULL) {
4302 if (connp->conn_latch == NULL) {
4303 connp->conn_latch = iplatch_create();
4304 if (connp->conn_latch == NULL)
4305 return (B_FALSE);
4306 }
4307 ipsec_latch_inbound(connp->conn_latch, ii);
4308 }
4309 return (B_TRUE);
4310 }
4311
4312 /*
4313 * Upper level protocols (ULP) pass through bind requests to IP for inspection
4314 * and to arrange for power-fanout assist. The ULP is identified by
4315 * adding a single byte at the end of the original bind message.
4316 * A ULP other than UDP or TCP that wishes to be recognized passes
4317 * down a bind with a zero length address.
4318 *
4319 * The binding works as follows:
4320 * - A zero byte address means just bind to the protocol.
4321 * - A four byte address is treated as a request to validate
4322 * that the address is a valid local address, appropriate for
4323 * an application to bind to. This does not affect any fanout
4324 * information in IP.
4325 * - A sizeof sin_t byte address is used to bind to only the local address
4326 * and port.
4327 * - A sizeof ipa_conn_t byte address contains complete fanout information
4328 * consisting of local and remote addresses and ports. In
4329 * this case, the addresses are both validated as appropriate
4330 * for this operation, and, if so, the information is retained
4331 * for use in the inbound fanout.
4332 *
4333 * The ULP (except in the zero-length bind) can append an
4334 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the
4335 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants
4336 * a copy of the source or destination IRE (source for local bind;
4337 * destination for complete bind). IPSEC_POLICY_SET indicates that the
4338 * policy information contained should be copied on to the conn.
4339 *
4340 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present.
4341 */
4342 mblk_t *
4343 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp)
4344 {
4345 ssize_t len;
4346 struct T_bind_req *tbr;
4347 sin_t *sin;
4348 ipa_conn_t *ac;
4349 uchar_t *ucp;
4350 mblk_t *mp1;
4351 boolean_t ire_requested;
4352 boolean_t ipsec_policy_set = B_FALSE;
4353 int error = 0;
4354 int protocol;
4355 ipa_conn_x_t *acx;
4356
4357 ASSERT(!connp->conn_af_isv6);
4358 connp->conn_pkt_isv6 = B_FALSE;
4359
4360 len = MBLKL(mp);
4361 if (len < (sizeof (*tbr) + 1)) {
4362 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
4363 "ip_bind: bogus msg, len %ld", len);
4364 /* XXX: Need to return something better */
4365 goto bad_addr;
4366 }
4367 /* Back up and extract the protocol identifier. */
4368 mp->b_wptr--;
4369 protocol = *mp->b_wptr & 0xFF;
4370 tbr = (struct T_bind_req *)mp->b_rptr;
4371 /* Reset the message type in preparation for shipping it back. */
4372 DB_TYPE(mp) = M_PCPROTO;
4373
4374 connp->conn_ulp = (uint8_t)protocol;
4375
4376 /*
4377 * Check for a zero length address. This is from a protocol that
4378 * wants to register to receive all packets of its type.
4379 */
4380 if (tbr->ADDR_length == 0) {
4381 /*
4382 * These protocols are now intercepted in ip_bind_v6().
4383 * Reject protocol-level binds here for now.
4384 *
4385 * For SCTP raw socket, ICMP sends down a bind with sin_t
4386 * so that the protocol type cannot be SCTP.
4387 */
4388 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH ||
4389 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) {
4390 goto bad_addr;
4391 }
4392
4393 /*
4394 *
4395 * The udp module never sends down a zero-length address,
4396 * and allowing this on a labeled system will break MLP
4397 * functionality.
4398 */
4399 if (is_system_labeled() && protocol == IPPROTO_UDP)
4400 goto bad_addr;
4401
4402 if (connp->conn_mac_exempt)
4403 goto bad_addr;
4404
4405 /* No hash here really. The table is big enough. */
4406 connp->conn_srcv6 = ipv6_all_zeros;
4407
4408 ipcl_proto_insert(connp, protocol);
4409
4410 tbr->PRIM_type = T_BIND_ACK;
4411 return (mp);
4412 }
4413
4414 /* Extract the address pointer from the message. */
4415 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset,
4416 tbr->ADDR_length);
4417 if (ucp == NULL) {
4418 ip1dbg(("ip_bind: no address\n"));
4419 goto bad_addr;
4420 }
4421 if (!OK_32PTR(ucp)) {
4422 ip1dbg(("ip_bind: unaligned address\n"));
4423 goto bad_addr;
4424 }
4425 /*
4426 * Check for trailing mps.
4427 */
4428
4429 mp1 = mp->b_cont;
4430 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE);
4431 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET);
4432
4433 switch (tbr->ADDR_length) {
4434 default:
4435 ip1dbg(("ip_bind: bad address length %d\n",
4436 (int)tbr->ADDR_length));
4437 goto bad_addr;
4438
4439 case IP_ADDR_LEN:
4440 /* Verification of local address only */
4441 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0,
4442 ire_requested, ipsec_policy_set, B_FALSE);
4443 break;
4444
4445 case sizeof (sin_t):
4446 sin = (sin_t *)ucp;
4447 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr,
4448 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE);
4449 break;
4450
4451 case sizeof (ipa_conn_t):
4452 ac = (ipa_conn_t *)ucp;
4453 /* For raw socket, the local port is not set. */
4454 if (ac->ac_lport == 0)
4455 ac->ac_lport = connp->conn_lport;
4456 /* Always verify destination reachability. */
4457 error = ip_bind_connected(connp, mp, &ac->ac_laddr,
4458 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested,
4459 ipsec_policy_set, B_TRUE, B_TRUE);
4460 break;
4461
4462 case sizeof (ipa_conn_x_t):
4463 acx = (ipa_conn_x_t *)ucp;
4464 /*
4465 * Whether or not to verify destination reachability depends
4466 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags.
4467 */
4468 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr,
4469 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr,
4470 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set,
4471 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0);
4472 break;
4473 }
4474 if (error == EINPROGRESS)
4475 return (NULL);
4476 else if (error != 0)
4477 goto bad_addr;
4478 /*
4479 * Pass the IPsec headers size in ire_ipsec_overhead.
4480 * We can't do this in ip_bind_insert_ire because the policy
4481 * may not have been inherited at that point in time and hence
4482 * conn_out_enforce_policy may not be set.
4483 */
4484 mp1 = mp->b_cont;
4485 if (ire_requested && connp->conn_out_enforce_policy &&
4486 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) {
4487 ire_t *ire = (ire_t *)mp1->b_rptr;
4488 ASSERT(MBLKL(mp1) >= sizeof (ire_t));
4489 ire->ire_ipsec_overhead = conn_ipsec_length(connp);
4490 }
4491
4492 /* Send it home. */
4493 mp->b_datap->db_type = M_PCPROTO;
4494 tbr->PRIM_type = T_BIND_ACK;
4495 return (mp);
4496
4497 bad_addr:
4498 /*
4499 * If error = -1 then we generate a TBADADDR - otherwise error is
4500 * a unix errno.
4501 */
4502 if (error > 0)
4503 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error);
4504 else
4505 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0);
4506 return (mp);
4507 }
4508
4509 /*
4510 * Here address is verified to be a valid local address.
4511 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast
4512 * address is also considered a valid local address.
4513 * In the case of a broadcast/multicast address, however, the
4514 * upper protocol is expected to reset the src address
4515 * to 0 if it sees a IRE_BROADCAST type returned so that
4516 * no packets are emitted with broadcast/multicast address as
4517 * source address (that violates hosts requirements RFC1122)
4518 * The addresses valid for bind are:
4519 * (1) - INADDR_ANY (0)
4520 * (2) - IP address of an UP interface
4521 * (3) - IP address of a DOWN interface
4522 * (4) - valid local IP broadcast addresses. In this case
4523 * the conn will only receive packets destined to
4524 * the specified broadcast address.
4525 * (5) - a multicast address. In this case
4526 * the conn will only receive packets destined to
4527 * the specified multicast address. Note: the
4528 * application still has to issue an
4529 * IP_ADD_MEMBERSHIP socket option.
4530 *
4531 * On error, return -1 for TBADADDR otherwise pass the
4532 * errno with TSYSERR reply.
4533 *
4534 * In all the above cases, the bound address must be valid in the current zone.
4535 * When the address is loopback, multicast or broadcast, there might be many
4536 * matching IREs so bind has to look up based on the zone.
4537 *
4538 * Note: lport is in network byte order.
4539 */
4540 int
4541 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport,
4542 boolean_t ire_requested, boolean_t ipsec_policy_set,
4543 boolean_t fanout_insert)
4544 {
4545 int error = 0;
4546 ire_t *src_ire;
4547 mblk_t *policy_mp;
4548 ipif_t *ipif;
4549 zoneid_t zoneid;
4550 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
4551
4552 if (ipsec_policy_set) {
4553 policy_mp = mp->b_cont;
4554 }
4555
4556 /*
4557 * If it was previously connected, conn_fully_bound would have
4558 * been set.
4559 */
4560 connp->conn_fully_bound = B_FALSE;
4561
4562 src_ire = NULL;
4563 ipif = NULL;
4564
4565 zoneid = IPCL_ZONEID(connp);
4566
4567 if (src_addr) {
4568 src_ire = ire_route_lookup(src_addr, 0, 0, 0,
4569 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst);
4570 /*
4571 * If an address other than 0.0.0.0 is requested,
4572 * we verify that it is a valid address for bind
4573 * Note: Following code is in if-else-if form for
4574 * readability compared to a condition check.
4575 */
4576 /* LINTED - statement has no consequent */
4577 if (IRE_IS_LOCAL(src_ire)) {
4578 /*
4579 * (2) Bind to address of local UP interface
4580 */
4581 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) {
4582 /*
4583 * (4) Bind to broadcast address
4584 * Note: permitted only from transports that
4585 * request IRE
4586 */
4587 if (!ire_requested)
4588 error = EADDRNOTAVAIL;
4589 } else {
4590 /*
4591 * (3) Bind to address of local DOWN interface
4592 * (ipif_lookup_addr() looks up all interfaces
4593 * but we do not get here for UP interfaces
4594 * - case (2) above)
4595 * We put the protocol byte back into the mblk
4596 * since we may come back via ip_wput_nondata()
4597 * later with this mblk if ipif_lookup_addr chooses
4598 * to defer processing.
4599 */
4600 *mp->b_wptr++ = (char)connp->conn_ulp;
4601 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid,
4602 CONNP_TO_WQ(connp), mp, ip_wput_nondata,
4603 &error, ipst)) != NULL) {
4604 ipif_refrele(ipif);
4605 } else if (error == EINPROGRESS) {
4606 if (src_ire != NULL)
4607 ire_refrele(src_ire);
4608 return (EINPROGRESS);
4609 } else if (CLASSD(src_addr)) {
4610 error = 0;
4611 if (src_ire != NULL)
4612 ire_refrele(src_ire);
4613 /*
4614 * (5) bind to multicast address.
4615 * Fake out the IRE returned to upper
4616 * layer to be a broadcast IRE.
4617 */
4618 src_ire = ire_ctable_lookup(
4619 INADDR_BROADCAST, INADDR_ANY,
4620 IRE_BROADCAST, NULL, zoneid, NULL,
4621 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY),
4622 ipst);
4623 if (src_ire == NULL || !ire_requested)
4624 error = EADDRNOTAVAIL;
4625 } else {
4626 /*
4627 * Not a valid address for bind
4628 */
4629 error = EADDRNOTAVAIL;
4630 }
4631 /*
4632 * Just to keep it consistent with the processing in
4633 * ip_bind_v4()
4634 */
4635 mp->b_wptr--;
4636 }
4637 if (error) {
4638 /* Red Alert! Attempting to be a bogon! */
4639 ip1dbg(("ip_bind: bad src address 0x%x\n",
4640 ntohl(src_addr)));
4641 goto bad_addr;
4642 }
4643 }
4644
4645 /*
4646 * Allow setting new policies. For example, disconnects come
4647 * down as ipa_t bind. As we would have set conn_policy_cached
4648 * to B_TRUE before, we should set it to B_FALSE, so that policy
4649 * can change after the disconnect.
4650 */
4651 connp->conn_policy_cached = B_FALSE;
4652
4653 /*
4654 * If not fanout_insert this was just an address verification
4655 */
4656 if (fanout_insert) {
4657 /*
4658 * The addresses have been verified. Time to insert in
4659 * the correct fanout list.
4660 */
4661 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6);
4662 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6);
4663 connp->conn_lport = lport;
4664 connp->conn_fport = 0;
4665 /*
4666 * Do we need to add a check to reject Multicast packets
4667 */
4668 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport);
4669 }
4670
4671 if (error == 0) {
4672 if (ire_requested) {
4673 if (!ip_bind_insert_ire(mp, src_ire, NULL, ipst)) {
4674 error = -1;
4675 /* Falls through to bad_addr */
4676 }
4677 } else if (ipsec_policy_set) {
4678 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) {
4679 error = -1;
4680 /* Falls through to bad_addr */
4681 }
4682 }
4683 }
4684 bad_addr:
4685 if (error != 0) {
4686 if (connp->conn_anon_port) {
4687 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4688 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport),
4689 B_FALSE);
4690 }
4691 connp->conn_mlp_type = mlptSingle;
4692 }
4693 if (src_ire != NULL)
4694 IRE_REFRELE(src_ire);
4695 if (ipsec_policy_set) {
4696 ASSERT(policy_mp == mp->b_cont);
4697 ASSERT(policy_mp != NULL);
4698 freeb(policy_mp);
4699 /*
4700 * As of now assume that nothing else accompanies
4701 * IPSEC_POLICY_SET.
4702 */
4703 mp->b_cont = NULL;
4704 }
4705 return (error);
4706 }
4707
4708 /*
4709 * Verify that both the source and destination addresses
4710 * are valid. If verify_dst is false, then the destination address may be
4711 * unreachable, i.e. have no route to it. Protocols like TCP want to verify
4712 * destination reachability, while tunnels do not.
4713 * Note that we allow connect to broadcast and multicast
4714 * addresses when ire_requested is set. Thus the ULP
4715 * has to check for IRE_BROADCAST and multicast.
4716 *
4717 * Returns zero if ok.
4718 * On error: returns -1 to mean TBADADDR otherwise returns an errno
4719 * (for use with TSYSERR reply).
4720 *
4721 * Note: lport and fport are in network byte order.
4722 */
4723 int
4724 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp,
4725 uint16_t lport, ipaddr_t dst_addr, uint16_t fport,
4726 boolean_t ire_requested, boolean_t ipsec_policy_set,
4727 boolean_t fanout_insert, boolean_t verify_dst)
4728 {
4729 ire_t *src_ire;
4730 ire_t *dst_ire;
4731 int error = 0;
4732 int protocol;
4733 mblk_t *policy_mp;
4734 ire_t *sire = NULL;
4735 ire_t *md_dst_ire = NULL;
4736 ire_t *lso_dst_ire = NULL;
4737 ill_t *ill = NULL;
4738 zoneid_t zoneid;
4739 ipaddr_t src_addr = *src_addrp;
4740 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
4741
4742 src_ire = dst_ire = NULL;
4743 protocol = *mp->b_wptr & 0xFF;
4744
4745 /*
4746 * If we never got a disconnect before, clear it now.
4747 */
4748 connp->conn_fully_bound = B_FALSE;
4749
4750 if (ipsec_policy_set) {
4751 policy_mp = mp->b_cont;
4752 }
4753
4754 zoneid = IPCL_ZONEID(connp);
4755
4756 if (CLASSD(dst_addr)) {
4757 /* Pick up an IRE_BROADCAST */
4758 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL,
4759 NULL, zoneid, MBLK_GETLABEL(mp),
4760 (MATCH_IRE_RECURSIVE |
4761 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE |
4762 MATCH_IRE_SECATTR), ipst);
4763 } else {
4764 /*
4765 * If conn_dontroute is set or if conn_nexthop_set is set,
4766 * and onlink ipif is not found set ENETUNREACH error.
4767 */
4768 if (connp->conn_dontroute || connp->conn_nexthop_set) {
4769 ipif_t *ipif;
4770
4771 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ?
4772 dst_addr : connp->conn_nexthop_v4, zoneid, ipst);
4773 if (ipif == NULL) {
4774 error = ENETUNREACH;
4775 goto bad_addr;
4776 }
4777 ipif_refrele(ipif);
4778 }
4779
4780 if (connp->conn_nexthop_set) {
4781 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0,
4782 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp),
4783 MATCH_IRE_SECATTR, ipst);
4784 } else {
4785 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL,
4786 &sire, zoneid, MBLK_GETLABEL(mp),
4787 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4788 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE |
4789 MATCH_IRE_SECATTR), ipst);
4790 }
4791 }
4792 /*
4793 * dst_ire can't be a broadcast when not ire_requested.
4794 * We also prevent ire's with src address INADDR_ANY to
4795 * be used, which are created temporarily for
4796 * sending out packets from endpoints that have
4797 * conn_unspec_src set. If verify_dst is true, the destination must be
4798 * reachable. If verify_dst is false, the destination needn't be
4799 * reachable.
4800 *
4801 * If we match on a reject or black hole, then we've got a
4802 * local failure. May as well fail out the connect() attempt,
4803 * since it's never going to succeed.
4804 */
4805 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY ||
4806 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
4807 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) {
4808 /*
4809 * If we're verifying destination reachability, we always want
4810 * to complain here.
4811 *
4812 * If we're not verifying destination reachability but the
4813 * destination has a route, we still want to fail on the
4814 * temporary address and broadcast address tests.
4815 */
4816 if (verify_dst || (dst_ire != NULL)) {
4817 if (ip_debug > 2) {
4818 pr_addr_dbg("ip_bind_connected: bad connected "
4819 "dst %s\n", AF_INET, &dst_addr);
4820 }
4821 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST))
4822 error = ENETUNREACH;
4823 else
4824 error = EHOSTUNREACH;
4825 goto bad_addr;
4826 }
4827 }
4828
4829 /*
4830 * We now know that routing will allow us to reach the destination.
4831 * Check whether Trusted Solaris policy allows communication with this
4832 * host, and pretend that the destination is unreachable if not.
4833 *
4834 * This is never a problem for TCP, since that transport is known to
4835 * compute the label properly as part of the tcp_rput_other T_BIND_ACK
4836 * handling. If the remote is unreachable, it will be detected at that
4837 * point, so there's no reason to check it here.
4838 *
4839 * Note that for sendto (and other datagram-oriented friends), this
4840 * check is done as part of the data path label computation instead.
4841 * The check here is just to make non-TCP connect() report the right
4842 * error.
4843 */
4844 if (dst_ire != NULL && is_system_labeled() &&
4845 !IPCL_IS_TCP(connp) &&
4846 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL,
4847 connp->conn_mac_exempt, ipst) != 0) {
4848 error = EHOSTUNREACH;
4849 if (ip_debug > 2) {
4850 pr_addr_dbg("ip_bind_connected: no label for dst %s\n",
4851 AF_INET, &dst_addr);
4852 }
4853 goto bad_addr;
4854 }
4855
4856 /*
4857 * If the app does a connect(), it means that it will most likely
4858 * send more than 1 packet to the destination. It makes sense
4859 * to clear the temporary flag.
4860 */
4861 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE &&
4862 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) {
4863 irb_t *irb = dst_ire->ire_bucket;
4864
4865 rw_enter(&irb->irb_lock, RW_WRITER);
4866 /*
4867 * We need to recheck for IRE_MARK_TEMPORARY after acquiring
4868 * the lock to guarantee irb_tmp_ire_cnt.
4869 */
4870 if (dst_ire->ire_marks & IRE_MARK_TEMPORARY) {
4871 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY;
4872 irb->irb_tmp_ire_cnt--;
4873 }
4874 rw_exit(&irb->irb_lock);
4875 }
4876
4877 /*
4878 * See if we should notify ULP about LSO/MDT; we do this whether or not
4879 * ire_requested is TRUE, in order to handle active connects; LSO/MDT
4880 * eligibility tests for passive connects are handled separately
4881 * through tcp_adapt_ire(). We do this before the source address
4882 * selection, because dst_ire may change after a call to
4883 * ipif_select_source(). This is a best-effort check, as the
4884 * packet for this connection may not actually go through
4885 * dst_ire->ire_stq, and the exact IRE can only be known after
4886 * calling ip_newroute(). This is why we further check on the
4887 * IRE during LSO/Multidata packet transmission in
4888 * tcp_lsosend()/tcp_multisend().
4889 */
4890 if (!ipsec_policy_set && dst_ire != NULL &&
4891 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) &&
4892 (ill = ire_to_ill(dst_ire), ill != NULL)) {
4893 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) {
4894 lso_dst_ire = dst_ire;
4895 IRE_REFHOLD(lso_dst_ire);
4896 } else if (ipst->ips_ip_multidata_outbound &&
4897 ILL_MDT_CAPABLE(ill)) {
4898 md_dst_ire = dst_ire;
4899 IRE_REFHOLD(md_dst_ire);
4900 }
4901 }
4902
4903 if (dst_ire != NULL &&
4904 dst_ire->ire_type == IRE_LOCAL &&
4905 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) {
4906 /*
4907 * If the IRE belongs to a different zone, look for a matching
4908 * route in the forwarding table and use the source address from
4909 * that route.
4910 */
4911 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL,
4912 zoneid, 0, NULL,
4913 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4914 MATCH_IRE_RJ_BHOLE, ipst);
4915 if (src_ire == NULL) {
4916 error = EHOSTUNREACH;
4917 goto bad_addr;
4918 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
4919 if (!(src_ire->ire_type & IRE_HOST))
4920 error = ENETUNREACH;
4921 else
4922 error = EHOSTUNREACH;
4923 goto bad_addr;
4924 }
4925 if (src_addr == INADDR_ANY)
4926 src_addr = src_ire->ire_src_addr;
4927 ire_refrele(src_ire);
4928 src_ire = NULL;
4929 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) {
4930 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
4931 src_addr = sire->ire_src_addr;
4932 ire_refrele(dst_ire);
4933 dst_ire = sire;
4934 sire = NULL;
4935 } else {
4936 /*
4937 * Pick a source address so that a proper inbound
4938 * load spreading would happen.
4939 */
4940 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill;
4941 ipif_t *src_ipif = NULL;
4942 ire_t *ipif_ire;
4943
4944 /*
4945 * Supply a local source address such that inbound
4946 * load spreading happens.
4947 *
4948 * Determine the best source address on this ill for
4949 * the destination.
4950 *
4951 * 1) For broadcast, we should return a broadcast ire
4952 * found above so that upper layers know that the
4953 * destination address is a broadcast address.
4954 *
4955 * 2) If this is part of a group, select a better
4956 * source address so that better inbound load
4957 * balancing happens. Do the same if the ipif
4958 * is DEPRECATED.
4959 *
4960 * 3) If the outgoing interface is part of a usesrc
4961 * group, then try selecting a source address from
4962 * the usesrc ILL.
4963 */
4964 if ((dst_ire->ire_zoneid != zoneid &&
4965 dst_ire->ire_zoneid != ALL_ZONES) ||
4966 (!(dst_ire->ire_flags & RTF_SETSRC)) &&
4967 (!(dst_ire->ire_type & IRE_BROADCAST) &&
4968 ((dst_ill->ill_group != NULL) ||
4969 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
4970 (dst_ill->ill_usesrc_ifindex != 0)))) {
4971 /*
4972 * If the destination is reachable via a
4973 * given gateway, the selected source address
4974 * should be in the same subnet as the gateway.
4975 * Otherwise, the destination is not reachable.
4976 *
4977 * If there are no interfaces on the same subnet
4978 * as the destination, ipif_select_source gives
4979 * first non-deprecated interface which might be
4980 * on a different subnet than the gateway.
4981 * This is not desirable. Hence pass the dst_ire
4982 * source address to ipif_select_source.
4983 * It is sure that the destination is reachable
4984 * with the dst_ire source address subnet.
4985 * So passing dst_ire source address to
4986 * ipif_select_source will make sure that the
4987 * selected source will be on the same subnet
4988 * as dst_ire source address.
4989 */
4990 ipaddr_t saddr =
4991 dst_ire->ire_ipif->ipif_src_addr;
4992 src_ipif = ipif_select_source(dst_ill,
4993 saddr, zoneid);
4994 if (src_ipif != NULL) {
4995 if (IS_VNI(src_ipif->ipif_ill)) {
4996 /*
4997 * For VNI there is no
4998 * interface route
4999 */
5000 src_addr =
5001 src_ipif->ipif_src_addr;
5002 } else {
5003 ipif_ire =
5004 ipif_to_ire(src_ipif);
5005 if (ipif_ire != NULL) {
5006 IRE_REFRELE(dst_ire);
5007 dst_ire = ipif_ire;
5008 }
5009 src_addr =
5010 dst_ire->ire_src_addr;
5011 }
5012 ipif_refrele(src_ipif);
5013 } else {
5014 src_addr = dst_ire->ire_src_addr;
5015 }
5016 } else {
5017 src_addr = dst_ire->ire_src_addr;
5018 }
5019 }
5020 }
5021
5022 /*
5023 * We do ire_route_lookup() here (and not
5024 * interface lookup as we assert that
5025 * src_addr should only come from an
5026 * UP interface for hard binding.
5027 */
5028 ASSERT(src_ire == NULL);
5029 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL,
5030 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst);
5031 /* src_ire must be a local|loopback */
5032 if (!IRE_IS_LOCAL(src_ire)) {
5033 if (ip_debug > 2) {
5034 pr_addr_dbg("ip_bind_connected: bad connected "
5035 "src %s\n", AF_INET, &src_addr);
5036 }
5037 error = EADDRNOTAVAIL;
5038 goto bad_addr;
5039 }
5040
5041 /*
5042 * If the source address is a loopback address, the
5043 * destination had best be local or multicast.
5044 * The transports that can't handle multicast will reject
5045 * those addresses.
5046 */
5047 if (src_ire->ire_type == IRE_LOOPBACK &&
5048 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) {
5049 ip1dbg(("ip_bind_connected: bad connected loopback\n"));
5050 error = -1;
5051 goto bad_addr;
5052 }
5053
5054 /*
5055 * Allow setting new policies. For example, disconnects come
5056 * down as ipa_t bind. As we would have set conn_policy_cached
5057 * to B_TRUE before, we should set it to B_FALSE, so that policy
5058 * can change after the disconnect.
5059 */
5060 connp->conn_policy_cached = B_FALSE;
5061
5062 /*
5063 * Set the conn addresses/ports immediately, so the IPsec policy calls
5064 * can handle their passed-in conn's.
5065 */
5066
5067 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6);
5068 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6);
5069 connp->conn_lport = lport;
5070 connp->conn_fport = fport;
5071 *src_addrp = src_addr;
5072
5073 ASSERT(!(ipsec_policy_set && ire_requested));
5074 if (ire_requested) {
5075 iulp_t *ulp_info = NULL;
5076
5077 /*
5078 * Note that sire will not be NULL if this is an off-link
5079 * connection and there is not cache for that dest yet.
5080 *
5081 * XXX Because of an existing bug, if there are multiple
5082 * default routes, the IRE returned now may not be the actual
5083 * default route used (default routes are chosen in a
5084 * round robin fashion). So if the metrics for different
5085 * default routes are different, we may return the wrong
5086 * metrics. This will not be a problem if the existing
5087 * bug is fixed.
5088 */
5089 if (sire != NULL) {
5090 ulp_info = &(sire->ire_uinfo);
5091 }
5092 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info, ipst)) {
5093 error = -1;
5094 goto bad_addr;
5095 }
5096 } else if (ipsec_policy_set) {
5097 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) {
5098 error = -1;
5099 goto bad_addr;
5100 }
5101 }
5102
5103 /*
5104 * Cache IPsec policy in this conn. If we have per-socket policy,
5105 * we'll cache that. If we don't, we'll inherit global policy.
5106 *
5107 * We can't insert until the conn reflects the policy. Note that
5108 * conn_policy_cached is set by ipsec_conn_cache_policy() even for
5109 * connections where we don't have a policy. This is to prevent
5110 * global policy lookups in the inbound path.
5111 *
5112 * If we insert before we set conn_policy_cached,
5113 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true
5114 * because global policy cound be non-empty. We normally call
5115 * ipsec_check_policy() for conn_policy_cached connections only if
5116 * ipc_in_enforce_policy is set. But in this case,
5117 * conn_policy_cached can get set anytime since we made the
5118 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is
5119 * called, which will make the above assumption false. Thus, we
5120 * need to insert after we set conn_policy_cached.
5121 */
5122 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0)
5123 goto bad_addr;
5124
5125 if (fanout_insert) {
5126 /*
5127 * The addresses have been verified. Time to insert in
5128 * the correct fanout list.
5129 */
5130 error = ipcl_conn_insert(connp, protocol, src_addr,
5131 dst_addr, connp->conn_ports);
5132 }
5133
5134 if (error == 0) {
5135 connp->conn_fully_bound = B_TRUE;
5136 /*
5137 * Our initial checks for LSO/MDT have passed; the IRE is not
5138 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to
5139 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into
5140 * ip_xxinfo_return(), which performs further checks
5141 * against them and upon success, returns the LSO/MDT info
5142 * mblk which we will attach to the bind acknowledgment.
5143 */
5144 if (lso_dst_ire != NULL) {
5145 mblk_t *lsoinfo_mp;
5146
5147 ASSERT(ill->ill_lso_capab != NULL);
5148 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp,
5149 ill->ill_name, ill->ill_lso_capab)) != NULL)
5150 linkb(mp, lsoinfo_mp);
5151 } else if (md_dst_ire != NULL) {
5152 mblk_t *mdinfo_mp;
5153
5154 ASSERT(ill->ill_mdt_capab != NULL);
5155 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp,
5156 ill->ill_name, ill->ill_mdt_capab)) != NULL)
5157 linkb(mp, mdinfo_mp);
5158 }
5159 }
5160 bad_addr:
5161 if (ipsec_policy_set) {
5162 ASSERT(policy_mp == mp->b_cont);
5163 ASSERT(policy_mp != NULL);
5164 freeb(policy_mp);
5165 /*
5166 * As of now assume that nothing else accompanies
5167 * IPSEC_POLICY_SET.
5168 */
5169 mp->b_cont = NULL;
5170 }
5171 if (src_ire != NULL)
5172 IRE_REFRELE(src_ire);
5173 if (dst_ire != NULL)
5174 IRE_REFRELE(dst_ire);
5175 if (sire != NULL)
5176 IRE_REFRELE(sire);
5177 if (md_dst_ire != NULL)
5178 IRE_REFRELE(md_dst_ire);
5179 if (lso_dst_ire != NULL)
5180 IRE_REFRELE(lso_dst_ire);
5181 return (error);
5182 }
5183
5184 /*
5185 * Insert the ire in b_cont. Returns false if it fails (due to lack of space).
5186 * Prefers dst_ire over src_ire.
5187 */
5188 static boolean_t
5189 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst)
5190 {
5191 mblk_t *mp1;
5192 ire_t *ret_ire = NULL;
5193
5194 mp1 = mp->b_cont;
5195 ASSERT(mp1 != NULL);
5196
5197 if (ire != NULL) {
5198 /*
5199 * mp1 initialized above to IRE_DB_REQ_TYPE
5200 * appended mblk. Its <upper protocol>'s
5201 * job to make sure there is room.
5202 */
5203 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t))
5204 return (0);
5205
5206 mp1->b_datap->db_type = IRE_DB_TYPE;
5207 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t);
5208 bcopy(ire, mp1->b_rptr, sizeof (ire_t));
5209 ret_ire = (ire_t *)mp1->b_rptr;
5210 /*
5211 * Pass the latest setting of the ip_path_mtu_discovery and
5212 * copy the ulp info if any.
5213 */
5214 ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ?
5215 IPH_DF : 0;
5216 if (ulp_info != NULL) {
5217 bcopy(ulp_info, &(ret_ire->ire_uinfo),
5218 sizeof (iulp_t));
5219 }
5220 ret_ire->ire_mp = mp1;
5221 } else {
5222 /*
5223 * No IRE was found. Remove IRE mblk.
5224 */
5225 mp->b_cont = mp1->b_cont;
5226 freeb(mp1);
5227 }
5228
5229 return (1);
5230 }
5231
5232 /*
5233 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
5234 * the final piece where we don't. Return a pointer to the first mblk in the
5235 * result, and update the pointer to the next mblk to chew on. If anything
5236 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
5237 * NULL pointer.
5238 */
5239 mblk_t *
5240 ip_carve_mp(mblk_t **mpp, ssize_t len)
5241 {
5242 mblk_t *mp0;
5243 mblk_t *mp1;
5244 mblk_t *mp2;
5245
5246 if (!len || !mpp || !(mp0 = *mpp))
5247 return (NULL);
5248 /* If we aren't going to consume the first mblk, we need a dup. */
5249 if (mp0->b_wptr - mp0->b_rptr > len) {
5250 mp1 = dupb(mp0);
5251 if (mp1) {
5252 /* Partition the data between the two mblks. */
5253 mp1->b_wptr = mp1->b_rptr + len;
5254 mp0->b_rptr = mp1->b_wptr;
5255 /*
5256 * after adjustments if mblk not consumed is now
5257 * unaligned, try to align it. If this fails free
5258 * all messages and let upper layer recover.
5259 */
5260 if (!OK_32PTR(mp0->b_rptr)) {
5261 if (!pullupmsg(mp0, -1)) {
5262 freemsg(mp0);
5263 freemsg(mp1);
5264 *mpp = NULL;
5265 return (NULL);
5266 }
5267 }
5268 }
5269 return (mp1);
5270 }
5271 /* Eat through as many mblks as we need to get len bytes. */
5272 len -= mp0->b_wptr - mp0->b_rptr;
5273 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
5274 if (mp2->b_wptr - mp2->b_rptr > len) {
5275 /*
5276 * We won't consume the entire last mblk. Like
5277 * above, dup and partition it.
5278 */
5279 mp1->b_cont = dupb(mp2);
5280 mp1 = mp1->b_cont;
5281 if (!mp1) {
5282 /*
5283 * Trouble. Rather than go to a lot of
5284 * trouble to clean up, we free the messages.
5285 * This won't be any worse than losing it on
5286 * the wire.
5287 */
5288 freemsg(mp0);
5289 freemsg(mp2);
5290 *mpp = NULL;
5291 return (NULL);
5292 }
5293 mp1->b_wptr = mp1->b_rptr + len;
5294 mp2->b_rptr = mp1->b_wptr;
5295 /*
5296 * after adjustments if mblk not consumed is now
5297 * unaligned, try to align it. If this fails free
5298 * all messages and let upper layer recover.
5299 */
5300 if (!OK_32PTR(mp2->b_rptr)) {
5301 if (!pullupmsg(mp2, -1)) {
5302 freemsg(mp0);
5303 freemsg(mp2);
5304 *mpp = NULL;
5305 return (NULL);
5306 }
5307 }
5308 *mpp = mp2;
5309 return (mp0);
5310 }
5311 /* Decrement len by the amount we just got. */
5312 len -= mp2->b_wptr - mp2->b_rptr;
5313 }
5314 /*
5315 * len should be reduced to zero now. If not our caller has
5316 * screwed up.
5317 */
5318 if (len) {
5319 /* Shouldn't happen! */
5320 freemsg(mp0);
5321 *mpp = NULL;
5322 return (NULL);
5323 }
5324 /*
5325 * We consumed up to exactly the end of an mblk. Detach the part
5326 * we are returning from the rest of the chain.
5327 */
5328 mp1->b_cont = NULL;
5329 *mpp = mp2;
5330 return (mp0);
5331 }
5332
5333 /* The ill stream is being unplumbed. Called from ip_close */
5334 int
5335 ip_modclose(ill_t *ill)
5336 {
5337 boolean_t success;
5338 ipsq_t *ipsq;
5339 ipif_t *ipif;
5340 queue_t *q = ill->ill_rq;
5341 ip_stack_t *ipst = ill->ill_ipst;
5342 clock_t timeout;
5343
5344 /*
5345 * Wait for the ACKs of all deferred control messages to be processed.
5346 * In particular, we wait for a potential capability reset initiated
5347 * in ip_sioctl_plink() to complete before proceeding.
5348 *
5349 * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms)
5350 * in case the driver never replies.
5351 */
5352 timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms);
5353 mutex_enter(&ill->ill_lock);
5354 while (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
5355 if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) {
5356 /* Timeout */
5357 break;
5358 }
5359 }
5360 mutex_exit(&ill->ill_lock);
5361
5362 /*
5363 * Forcibly enter the ipsq after some delay. This is to take
5364 * care of the case when some ioctl does not complete because
5365 * we sent a control message to the driver and it did not
5366 * send us a reply. We want to be able to at least unplumb
5367 * and replumb rather than force the user to reboot the system.
5368 */
5369 success = ipsq_enter(ill, B_FALSE);
5370
5371 /*
5372 * Open/close/push/pop is guaranteed to be single threaded
5373 * per stream by STREAMS. FS guarantees that all references
5374 * from top are gone before close is called. So there can't
5375 * be another close thread that has set CONDEMNED on this ill.
5376 * and cause ipsq_enter to return failure.
5377 */
5378 ASSERT(success);
5379 ipsq = ill->ill_phyint->phyint_ipsq;
5380
5381 /*
5382 * Mark it condemned. No new reference will be made to this ill.
5383 * Lookup functions will return an error. Threads that try to
5384 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
5385 * that the refcnt will drop down to zero.
5386 */
5387 mutex_enter(&ill->ill_lock);
5388 ill->ill_state_flags |= ILL_CONDEMNED;
5389 for (ipif = ill->ill_ipif; ipif != NULL;
5390 ipif = ipif->ipif_next) {
5391 ipif->ipif_state_flags |= IPIF_CONDEMNED;
5392 }
5393 /*
5394 * Wake up anybody waiting to enter the ipsq. ipsq_enter
5395 * returns error if ILL_CONDEMNED is set
5396 */
5397 cv_broadcast(&ill->ill_cv);
5398 mutex_exit(&ill->ill_lock);
5399
5400 /*
5401 * Send all the deferred DLPI messages downstream which came in
5402 * during the small window right before ipsq_enter(). We do this
5403 * without waiting for the ACKs because all the ACKs for M_PROTO
5404 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
5405 */
5406 ill_dlpi_send_deferred(ill);
5407
5408 /*
5409 * Shut down fragmentation reassembly.
5410 * ill_frag_timer won't start a timer again.
5411 * Now cancel any existing timer
5412 */
5413 (void) untimeout(ill->ill_frag_timer_id);
5414 (void) ill_frag_timeout(ill, 0);
5415
5416 /*
5417 * If MOVE was in progress, clear the
5418 * move_in_progress fields also.
5419 */
5420 if (ill->ill_move_in_progress) {
5421 ILL_CLEAR_MOVE(ill);
5422 }
5423
5424 /*
5425 * Call ill_delete to bring down the ipifs, ilms and ill on
5426 * this ill. Then wait for the refcnts to drop to zero.
5427 * ill_is_freeable checks whether the ill is really quiescent.
5428 * Then make sure that threads that are waiting to enter the
5429 * ipsq have seen the error returned by ipsq_enter and have
5430 * gone away. Then we call ill_delete_tail which does the
5431 * DL_UNBIND_REQ with the driver and then qprocsoff.
5432 */
5433 ill_delete(ill);
5434 mutex_enter(&ill->ill_lock);
5435 while (!ill_is_freeable(ill))
5436 cv_wait(&ill->ill_cv, &ill->ill_lock);
5437 while (ill->ill_waiters)
5438 cv_wait(&ill->ill_cv, &ill->ill_lock);
5439
5440 mutex_exit(&ill->ill_lock);
5441
5442 /*
5443 * ill_delete_tail drops reference on ill_ipst, but we need to keep
5444 * it held until the end of the function since the cleanup
5445 * below needs to be able to use the ip_stack_t.
5446 */
5447 netstack_hold(ipst->ips_netstack);
5448
5449 /* qprocsoff is called in ill_delete_tail */
5450 ill_delete_tail(ill);
5451 ASSERT(ill->ill_ipst == NULL);
5452
5453 /*
5454 * Walk through all upper (conn) streams and qenable
5455 * those that have queued data.
5456 * close synchronization needs this to
5457 * be done to ensure that all upper layers blocked
5458 * due to flow control to the closing device
5459 * get unblocked.
5460 */
5461 ip1dbg(("ip_wsrv: walking\n"));
5462 conn_walk_drain(ipst);
5463
5464 mutex_enter(&ipst->ips_ip_mi_lock);
5465 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
5466 mutex_exit(&ipst->ips_ip_mi_lock);
5467
5468 /*
5469 * credp could be null if the open didn't succeed and ip_modopen
5470 * itself calls ip_close.
5471 */
5472 if (ill->ill_credp != NULL)
5473 crfree(ill->ill_credp);
5474
5475 mutex_enter(&ill->ill_lock);
5476 ill_nic_info_dispatch(ill);
5477 mutex_exit(&ill->ill_lock);
5478
5479 /*
5480 * Now we are done with the module close pieces that
5481 * need the netstack_t.
5482 */
5483 netstack_rele(ipst->ips_netstack);
5484
5485 mi_close_free((IDP)ill);
5486 q->q_ptr = WR(q)->q_ptr = NULL;
5487
5488 ipsq_exit(ipsq);
5489
5490 return (0);
5491 }
5492
5493 /*
5494 * This is called as part of close() for IP, UDP, ICMP, and RTS
5495 * in order to quiesce the conn.
5496 */
5497 void
5498 ip_quiesce_conn(conn_t *connp)
5499 {
5500 boolean_t drain_cleanup_reqd = B_FALSE;
5501 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
5502 boolean_t ilg_cleanup_reqd = B_FALSE;
5503 ip_stack_t *ipst;
5504
5505 ASSERT(!IPCL_IS_TCP(connp));
5506 ipst = connp->conn_netstack->netstack_ip;
5507
5508 /*
5509 * Mark the conn as closing, and this conn must not be
5510 * inserted in future into any list. Eg. conn_drain_insert(),
5511 * won't insert this conn into the conn_drain_list.
5512 * Similarly ill_pending_mp_add() will not add any mp to
5513 * the pending mp list, after this conn has started closing.
5514 *
5515 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg
5516 * cannot get set henceforth.
5517 */
5518 mutex_enter(&connp->conn_lock);
5519 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
5520 connp->conn_state_flags |= CONN_CLOSING;
5521 if (connp->conn_idl != NULL)
5522 drain_cleanup_reqd = B_TRUE;
5523 if (connp->conn_oper_pending_ill != NULL)
5524 conn_ioctl_cleanup_reqd = B_TRUE;
5525 if (connp->conn_dhcpinit_ill != NULL) {
5526 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
5527 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
5528 connp->conn_dhcpinit_ill = NULL;
5529 }
5530 if (connp->conn_ilg_inuse != 0)
5531 ilg_cleanup_reqd = B_TRUE;
5532 mutex_exit(&connp->conn_lock);
5533
5534 if (conn_ioctl_cleanup_reqd)
5535 conn_ioctl_cleanup(connp);
5536
5537 if (is_system_labeled() && connp->conn_anon_port) {
5538 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
5539 connp->conn_mlp_type, connp->conn_ulp,
5540 ntohs(connp->conn_lport), B_FALSE);
5541 connp->conn_anon_port = 0;
5542 }
5543 connp->conn_mlp_type = mlptSingle;
5544
5545 /*
5546 * Remove this conn from any fanout list it is on.
5547 * and then wait for any threads currently operating
5548 * on this endpoint to finish
5549 */
5550 ipcl_hash_remove(connp);
5551
5552 /*
5553 * Remove this conn from the drain list, and do
5554 * any other cleanup that may be required.
5555 * (Only non-tcp streams may have a non-null conn_idl.
5556 * TCP streams are never flow controlled, and
5557 * conn_idl will be null)
5558 */
5559 if (drain_cleanup_reqd)
5560 conn_drain_tail(connp, B_TRUE);
5561
5562 if (connp == ipst->ips_ip_g_mrouter)
5563 (void) ip_mrouter_done(NULL, ipst);
5564
5565 if (ilg_cleanup_reqd)
5566 ilg_delete_all(connp);
5567
5568 conn_delete_ire(connp, NULL);
5569
5570 /*
5571 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
5572 * callers from write side can't be there now because close
5573 * is in progress. The only other caller is ipcl_walk
5574 * which checks for the condemned flag.
5575 */
5576 mutex_enter(&connp->conn_lock);
5577 connp->conn_state_flags |= CONN_CONDEMNED;
5578 while (connp->conn_ref != 1)
5579 cv_wait(&connp->conn_cv, &connp->conn_lock);
5580 connp->conn_state_flags |= CONN_QUIESCED;
5581 mutex_exit(&connp->conn_lock);
5582 }
5583
5584 /* ARGSUSED */
5585 int
5586 ip_close(queue_t *q, int flags)
5587 {
5588 conn_t *connp;
5589
5590 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q);
5591
5592 /*
5593 * Call the appropriate delete routine depending on whether this is
5594 * a module or device.
5595 */
5596 if (WR(q)->q_next != NULL) {
5597 /* This is a module close */
5598 return (ip_modclose((ill_t *)q->q_ptr));
5599 }
5600
5601 connp = q->q_ptr;
5602 ip_quiesce_conn(connp);
5603
5604 qprocsoff(q);
5605
5606 /*
5607 * Now we are truly single threaded on this stream, and can
5608 * delete the things hanging off the connp, and finally the connp.
5609 * We removed this connp from the fanout list, it cannot be
5610 * accessed thru the fanouts, and we already waited for the
5611 * conn_ref to drop to 0. We are already in close, so
5612 * there cannot be any other thread from the top. qprocsoff
5613 * has completed, and service has completed or won't run in
5614 * future.
5615 */
5616 ASSERT(connp->conn_ref == 1);
5617
5618 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
5619
5620 connp->conn_ref--;
5621 ipcl_conn_destroy(connp);
5622
5623 q->q_ptr = WR(q)->q_ptr = NULL;
5624 return (0);
5625 }
5626
5627 /*
5628 * Wapper around putnext() so that ip_rts_request can merely use
5629 * conn_recv.
5630 */
5631 /*ARGSUSED2*/
5632 static void
5633 ip_conn_input(void *arg1, mblk_t *mp, void *arg2)
5634 {
5635 conn_t *connp = (conn_t *)arg1;
5636
5637 putnext(connp->conn_rq, mp);
5638 }
5639
5640 /* Return the IP checksum for the IP header at "iph". */
5641 uint16_t
5642 ip_csum_hdr(ipha_t *ipha)
5643 {
5644 uint16_t *uph;
5645 uint32_t sum;
5646 int opt_len;
5647
5648 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) -
5649 IP_SIMPLE_HDR_LENGTH_IN_WORDS;
5650 uph = (uint16_t *)ipha;
5651 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] +
5652 uph[5] + uph[6] + uph[7] + uph[8] + uph[9];
5653 if (opt_len > 0) {
5654 do {
5655 sum += uph[10];
5656 sum += uph[11];
5657 uph += 2;
5658 } while (--opt_len);
5659 }
5660 sum = (sum & 0xFFFF) + (sum >> 16);
5661 sum = ~(sum + (sum >> 16)) & 0xFFFF;
5662 if (sum == 0xffff)
5663 sum = 0;
5664 return ((uint16_t)sum);
5665 }
5666
5667 /*
5668 * Called when the module is about to be unloaded
5669 */
5670 void
5671 ip_ddi_destroy(void)
5672 {
5673 tnet_fini();
5674
5675 icmp_ddi_destroy();
5676 rts_ddi_destroy();
5677 udp_ddi_destroy();
5678 sctp_ddi_g_destroy();
5679 tcp_ddi_g_destroy();
5680 ipsec_policy_g_destroy();
5681 ipcl_g_destroy();
5682 ip_net_g_destroy();
5683 ip_ire_g_fini();
5684 inet_minor_destroy(ip_minor_arena_sa);
5685 #if defined(_LP64)
5686 inet_minor_destroy(ip_minor_arena_la);
5687 #endif
5688
5689 #ifdef DEBUG
5690 list_destroy(&ip_thread_list);
5691 rw_destroy(&ip_thread_rwlock);
5692 tsd_destroy(&ip_thread_data);
5693 #endif
5694
5695 netstack_unregister(NS_IP);
5696 }
5697
5698 /*
5699 * First step in cleanup.
5700 */
5701 /* ARGSUSED */
5702 static void
5703 ip_stack_shutdown(netstackid_t stackid, void *arg)
5704 {
5705 ip_stack_t *ipst = (ip_stack_t *)arg;
5706
5707 #ifdef NS_DEBUG
5708 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
5709 #endif
5710
5711 /* Get rid of loopback interfaces and their IREs */
5712 ip_loopback_cleanup(ipst);
5713 }
5714
5715 /*
5716 * Free the IP stack instance.
5717 */
5718 static void
5719 ip_stack_fini(netstackid_t stackid, void *arg)
5720 {
5721 ip_stack_t *ipst = (ip_stack_t *)arg;
5722 int ret;
5723
5724 #ifdef NS_DEBUG
5725 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
5726 #endif
5727 ipv4_hook_destroy(ipst);
5728 ipv6_hook_destroy(ipst);
5729 ip_net_destroy(ipst);
5730
5731 rw_destroy(&ipst->ips_srcid_lock);
5732
5733 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
5734 ipst->ips_ip_mibkp = NULL;
5735 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
5736 ipst->ips_icmp_mibkp = NULL;
5737 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
5738 ipst->ips_ip_kstat = NULL;
5739 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
5740 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
5741 ipst->ips_ip6_kstat = NULL;
5742 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
5743
5744 nd_free(&ipst->ips_ip_g_nd);
5745 kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr));
5746 ipst->ips_param_arr = NULL;
5747 kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr));
5748 ipst->ips_ndp_arr = NULL;
5749
5750 ip_mrouter_stack_destroy(ipst);
5751
5752 mutex_destroy(&ipst->ips_ip_mi_lock);
5753 rw_destroy(&ipst->ips_ipsec_capab_ills_lock);
5754 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
5755 rw_destroy(&ipst->ips_ip_g_nd_lock);
5756
5757 ret = untimeout(ipst->ips_igmp_timeout_id);
5758 if (ret == -1) {
5759 ASSERT(ipst->ips_igmp_timeout_id == 0);
5760 } else {
5761 ASSERT(ipst->ips_igmp_timeout_id != 0);
5762 ipst->ips_igmp_timeout_id = 0;
5763 }
5764 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
5765 if (ret == -1) {
5766 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
5767 } else {
5768 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
5769 ipst->ips_igmp_slowtimeout_id = 0;
5770 }
5771 ret = untimeout(ipst->ips_mld_timeout_id);
5772 if (ret == -1) {
5773 ASSERT(ipst->ips_mld_timeout_id == 0);
5774 } else {
5775 ASSERT(ipst->ips_mld_timeout_id != 0);
5776 ipst->ips_mld_timeout_id = 0;
5777 }
5778 ret = untimeout(ipst->ips_mld_slowtimeout_id);
5779 if (ret == -1) {
5780 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
5781 } else {
5782 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
5783 ipst->ips_mld_slowtimeout_id = 0;
5784 }
5785 ret = untimeout(ipst->ips_ip_ire_expire_id);
5786 if (ret == -1) {
5787 ASSERT(ipst->ips_ip_ire_expire_id == 0);
5788 } else {
5789 ASSERT(ipst->ips_ip_ire_expire_id != 0);
5790 ipst->ips_ip_ire_expire_id = 0;
5791 }
5792
5793 mutex_destroy(&ipst->ips_igmp_timer_lock);
5794 mutex_destroy(&ipst->ips_mld_timer_lock);
5795 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
5796 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
5797 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
5798 rw_destroy(&ipst->ips_ill_g_lock);
5799
5800 ip_ire_fini(ipst);
5801 ip6_asp_free(ipst);
5802 conn_drain_fini(ipst);
5803 ipcl_destroy(ipst);
5804
5805 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
5806 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
5807 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
5808 ipst->ips_ndp4 = NULL;
5809 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
5810 ipst->ips_ndp6 = NULL;
5811
5812 if (ipst->ips_loopback_ksp != NULL) {
5813 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
5814 ipst->ips_loopback_ksp = NULL;
5815 }
5816
5817 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
5818 ipst->ips_phyint_g_list = NULL;
5819 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
5820 ipst->ips_ill_g_heads = NULL;
5821
5822 kmem_free(ipst, sizeof (*ipst));
5823 }
5824
5825 /*
5826 * This function is called from the TSD destructor, and is used to debug
5827 * reference count issues in IP. See block comment in <inet/ip_if.h> for
5828 * details.
5829 */
5830 static void
5831 ip_thread_exit(void *phash)
5832 {
5833 th_hash_t *thh = phash;
5834
5835 rw_enter(&ip_thread_rwlock, RW_WRITER);
5836 list_remove(&ip_thread_list, thh);
5837 rw_exit(&ip_thread_rwlock);
5838 mod_hash_destroy_hash(thh->thh_hash);
5839 kmem_free(thh, sizeof (*thh));
5840 }
5841
5842 /*
5843 * Called when the IP kernel module is loaded into the kernel
5844 */
5845 void
5846 ip_ddi_init(void)
5847 {
5848 ip_input_proc = ip_squeue_switch(ip_squeue_enter);
5849
5850 /*
5851 * For IP and TCP the minor numbers should start from 2 since we have 4
5852 * initial devices: ip, ip6, tcp, tcp6.
5853 */
5854 /*
5855 * If this is a 64-bit kernel, then create two separate arenas -
5856 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
5857 * other for socket apps in the range 2^^18 through 2^^32-1.
5858 */
5859 ip_minor_arena_la = NULL;
5860 ip_minor_arena_sa = NULL;
5861 #if defined(_LP64)
5862 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
5863 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
5864 cmn_err(CE_PANIC,
5865 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
5866 }
5867 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
5868 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
5869 cmn_err(CE_PANIC,
5870 "ip_ddi_init: ip_minor_arena_la creation failed\n");
5871 }
5872 #else
5873 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
5874 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
5875 cmn_err(CE_PANIC,
5876 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
5877 }
5878 #endif
5879 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
5880
5881 ipcl_g_init();
5882 ip_ire_g_init();
5883 ip_net_g_init();
5884
5885 #ifdef DEBUG
5886 tsd_create(&ip_thread_data, ip_thread_exit);
5887 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
5888 list_create(&ip_thread_list, sizeof (th_hash_t),
5889 offsetof(th_hash_t, thh_link));
5890 #endif
5891
5892 /*
5893 * We want to be informed each time a stack is created or
5894 * destroyed in the kernel, so we can maintain the
5895 * set of udp_stack_t's.
5896 */
5897 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
5898 ip_stack_fini);
5899
5900 ipsec_policy_g_init();
5901 tcp_ddi_g_init();
5902 sctp_ddi_g_init();
5903
5904 tnet_init();
5905
5906 udp_ddi_init();
5907 rts_ddi_init();
5908 icmp_ddi_init();
5909 }
5910
5911 /*
5912 * Initialize the IP stack instance.
5913 */
5914 static void *
5915 ip_stack_init(netstackid_t stackid, netstack_t *ns)
5916 {
5917 ip_stack_t *ipst;
5918 ipparam_t *pa;
5919 ipndp_t *na;
5920
5921 #ifdef NS_DEBUG
5922 printf("ip_stack_init(stack %d)\n", stackid);
5923 #endif
5924
5925 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
5926 ipst->ips_netstack = ns;
5927
5928 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
5929 KM_SLEEP);
5930 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
5931 KM_SLEEP);
5932 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
5933 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
5934 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
5935 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
5936
5937 rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL);
5938 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
5939 ipst->ips_igmp_deferred_next = INFINITY;
5940 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
5941 ipst->ips_mld_deferred_next = INFINITY;
5942 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
5943 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
5944 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
5945 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
5946 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
5947 rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL);
5948 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
5949
5950 ipcl_init(ipst);
5951 ip_ire_init(ipst);
5952 ip6_asp_init(ipst);
5953 ipif_init(ipst);
5954 conn_drain_init(ipst);
5955 ip_mrouter_stack_init(ipst);
5956
5957 ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT;
5958 ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000;
5959
5960 ipst->ips_ip_multirt_log_interval = 1000;
5961
5962 ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT;
5963 ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT;
5964 ipst->ips_ill_index = 1;
5965
5966 ipst->ips_saved_ip_g_forward = -1;
5967 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
5968
5969 pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
5970 ipst->ips_param_arr = pa;
5971 bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr));
5972
5973 na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP);
5974 ipst->ips_ndp_arr = na;
5975 bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr));
5976 ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data =
5977 (caddr_t)&ipst->ips_ip_g_forward;
5978 ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data =
5979 (caddr_t)&ipst->ips_ipv6_forward;
5980 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name,
5981 "ip_cgtp_filter") == 0);
5982 ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data =
5983 (caddr_t)&ipst->ips_ip_cgtp_filter;
5984 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_name,
5985 "ipmp_hook_emulation") == 0);
5986 ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_data =
5987 (caddr_t)&ipst->ips_ipmp_hook_emulation;
5988
5989 (void) ip_param_register(&ipst->ips_ip_g_nd,
5990 ipst->ips_param_arr, A_CNT(lcl_param_arr),
5991 ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr));
5992
5993 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
5994 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
5995 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
5996 ipst->ips_ip6_kstat =
5997 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
5998
5999 ipst->ips_ipmp_enable_failback = B_TRUE;
6000
6001 ipst->ips_ip_src_id = 1;
6002 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
6003
6004 ip_net_init(ipst, ns);
6005 ipv4_hook_init(ipst);
6006 ipv6_hook_init(ipst);
6007
6008 return (ipst);
6009 }
6010
6011 /*
6012 * Allocate and initialize a DLPI template of the specified length. (May be
6013 * called as writer.)
6014 */
6015 mblk_t *
6016 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
6017 {
6018 mblk_t *mp;
6019
6020 mp = allocb(len, BPRI_MED);
6021 if (!mp)
6022 return (NULL);
6023
6024 /*
6025 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
6026 * of which we don't seem to use) are sent with M_PCPROTO, and
6027 * that other DLPI are M_PROTO.
6028 */
6029 if (prim == DL_INFO_REQ) {
6030 mp->b_datap->db_type = M_PCPROTO;
6031 } else {
6032 mp->b_datap->db_type = M_PROTO;
6033 }
6034
6035 mp->b_wptr = mp->b_rptr + len;
6036 bzero(mp->b_rptr, len);
6037 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
6038 return (mp);
6039 }
6040
6041 /*
6042 * Debug formatting routine. Returns a character string representation of the
6043 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
6044 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
6045 *
6046 * Once the ndd table-printing interfaces are removed, this can be changed to
6047 * standard dotted-decimal form.
6048 */
6049 char *
6050 ip_dot_addr(ipaddr_t addr, char *buf)
6051 {
6052 uint8_t *ap = (uint8_t *)&addr;
6053
6054 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
6055 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
6056 return (buf);
6057 }
6058
6059 /*
6060 * Write the given MAC address as a printable string in the usual colon-
6061 * separated format.
6062 */
6063 const char *
6064 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
6065 {
6066 char *bp;
6067
6068 if (alen == 0 || buflen < 4)
6069 return ("?");
6070 bp = buf;
6071 for (;;) {
6072 /*
6073 * If there are more MAC address bytes available, but we won't
6074 * have any room to print them, then add "..." to the string
6075 * instead. See below for the 'magic number' explanation.
6076 */
6077 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
6078 (void) strcpy(bp, "...");
6079 break;
6080 }
6081 (void) sprintf(bp, "%02x", *addr++);
6082 bp += 2;
6083 if (--alen == 0)
6084 break;
6085 *bp++ = ':';
6086 buflen -= 3;
6087 /*
6088 * At this point, based on the first 'if' statement above,
6089 * either alen == 1 and buflen >= 3, or alen > 1 and
6090 * buflen >= 4. The first case leaves room for the final "xx"
6091 * number and trailing NUL byte. The second leaves room for at
6092 * least "...". Thus the apparently 'magic' numbers chosen for
6093 * that statement.
6094 */
6095 }
6096 return (buf);
6097 }
6098
6099 /*
6100 * Send an ICMP error after patching up the packet appropriately. Returns
6101 * non-zero if the appropriate MIB should be bumped; zero otherwise.
6102 */
6103 static boolean_t
6104 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags,
6105 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present,
6106 zoneid_t zoneid, ip_stack_t *ipst)
6107 {
6108 ipha_t *ipha;
6109 mblk_t *first_mp;
6110 boolean_t secure;
6111 unsigned char db_type;
6112 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6113
6114 first_mp = mp;
6115 if (mctl_present) {
6116 mp = mp->b_cont;
6117 secure = ipsec_in_is_secure(first_mp);
6118 ASSERT(mp != NULL);
6119 } else {
6120 /*
6121 * If this is an ICMP error being reported - which goes
6122 * up as M_CTLs, we need to convert them to M_DATA till
6123 * we finish checking with global policy because
6124 * ipsec_check_global_policy() assumes M_DATA as clear
6125 * and M_CTL as secure.
6126 */
6127 db_type = DB_TYPE(mp);
6128 DB_TYPE(mp) = M_DATA;
6129 secure = B_FALSE;
6130 }
6131 /*
6132 * We are generating an icmp error for some inbound packet.
6133 * Called from all ip_fanout_(udp, tcp, proto) functions.
6134 * Before we generate an error, check with global policy
6135 * to see whether this is allowed to enter the system. As
6136 * there is no "conn", we are checking with global policy.
6137 */
6138 ipha = (ipha_t *)mp->b_rptr;
6139 if (secure || ipss->ipsec_inbound_v4_policy_present) {
6140 first_mp = ipsec_check_global_policy(first_mp, NULL,
6141 ipha, NULL, mctl_present, ipst->ips_netstack);
6142 if (first_mp == NULL)
6143 return (B_FALSE);
6144 }
6145
6146 if (!mctl_present)
6147 DB_TYPE(mp) = db_type;
6148
6149 if (flags & IP_FF_SEND_ICMP) {
6150 if (flags & IP_FF_HDR_COMPLETE) {
6151 if (ip_hdr_complete(ipha, zoneid, ipst)) {
6152 freemsg(first_mp);
6153 return (B_TRUE);
6154 }
6155 }
6156 if (flags & IP_FF_CKSUM) {
6157 /*
6158 * Have to correct checksum since
6159 * the packet might have been
6160 * fragmented and the reassembly code in ip_rput
6161 * does not restore the IP checksum.
6162 */
6163 ipha->ipha_hdr_checksum = 0;
6164 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
6165 }
6166 switch (icmp_type) {
6167 case ICMP_DEST_UNREACHABLE:
6168 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid,
6169 ipst);
6170 break;
6171 default:
6172 freemsg(first_mp);
6173 break;
6174 }
6175 } else {
6176 freemsg(first_mp);
6177 return (B_FALSE);
6178 }
6179
6180 return (B_TRUE);
6181 }
6182
6183 /*
6184 * Used to send an ICMP error message when a packet is received for
6185 * a protocol that is not supported. The mblk passed as argument
6186 * is consumed by this function.
6187 */
6188 void
6189 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid,
6190 ip_stack_t *ipst)
6191 {
6192 mblk_t *mp;
6193 ipha_t *ipha;
6194 ill_t *ill;
6195 ipsec_in_t *ii;
6196
6197 ii = (ipsec_in_t *)ipsec_mp->b_rptr;
6198 ASSERT(ii->ipsec_in_type == IPSEC_IN);
6199
6200 mp = ipsec_mp->b_cont;
6201 ipsec_mp->b_cont = NULL;
6202 ipha = (ipha_t *)mp->b_rptr;
6203 /* Get ill from index in ipsec_in_t. */
6204 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index,
6205 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL,
6206 ipst);
6207 if (ill != NULL) {
6208 if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
6209 if (ip_fanout_send_icmp(q, mp, flags,
6210 ICMP_DEST_UNREACHABLE,
6211 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) {
6212 BUMP_MIB(ill->ill_ip_mib,
6213 ipIfStatsInUnknownProtos);
6214 }
6215 } else {
6216 if (ip_fanout_send_icmp_v6(q, mp, flags,
6217 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER,
6218 0, B_FALSE, zoneid, ipst)) {
6219 BUMP_MIB(ill->ill_ip_mib,
6220 ipIfStatsInUnknownProtos);
6221 }
6222 }
6223 ill_refrele(ill);
6224 } else { /* re-link for the freemsg() below. */
6225 ipsec_mp->b_cont = mp;
6226 }
6227
6228 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */
6229 freemsg(ipsec_mp);
6230 }
6231
6232 /*
6233 * See if the inbound datagram has had IPsec processing applied to it.
6234 */
6235 boolean_t
6236 ipsec_in_is_secure(mblk_t *ipsec_mp)
6237 {
6238 ipsec_in_t *ii;
6239
6240 ii = (ipsec_in_t *)ipsec_mp->b_rptr;
6241 ASSERT(ii->ipsec_in_type == IPSEC_IN);
6242
6243 if (ii->ipsec_in_loopback) {
6244 return (ii->ipsec_in_secure);
6245 } else {
6246 return (ii->ipsec_in_ah_sa != NULL ||
6247 ii->ipsec_in_esp_sa != NULL ||
6248 ii->ipsec_in_decaps);
6249 }
6250 }
6251
6252 /*
6253 * Handle protocols with which IP is less intimate. There
6254 * can be more than one stream bound to a particular
6255 * protocol. When this is the case, normally each one gets a copy
6256 * of any incoming packets.
6257 *
6258 * IPsec NOTE :
6259 *
6260 * Don't allow a secure packet going up a non-secure connection.
6261 * We don't allow this because
6262 *
6263 * 1) Reply might go out in clear which will be dropped at
6264 * the sending side.
6265 * 2) If the reply goes out in clear it will give the
6266 * adversary enough information for getting the key in
6267 * most of the cases.
6268 *
6269 * Moreover getting a secure packet when we expect clear
6270 * implies that SA's were added without checking for
6271 * policy on both ends. This should not happen once ISAKMP
6272 * is used to negotiate SAs as SAs will be added only after
6273 * verifying the policy.
6274 *
6275 * NOTE : If the packet was tunneled and not multicast we only send
6276 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall
6277 * back to delivering packets to AF_INET6 raw sockets.
6278 *
6279 * IPQoS Notes:
6280 * Once we have determined the client, invoke IPPF processing.
6281 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN,
6282 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local
6283 * ip_policy will be false.
6284 *
6285 * Zones notes:
6286 * Currently only applications in the global zone can create raw sockets for
6287 * protocols other than ICMP. So unlike the broadcast / multicast case of
6288 * ip_fanout_udp(), we only send a copy of the packet to streams in the
6289 * specified zone. For ICMP, this is handled by the callers of icmp_inbound().
6290 */
6291 static void
6292 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags,
6293 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill,
6294 zoneid_t zoneid)
6295 {
6296 queue_t *rq;
6297 mblk_t *mp1, *first_mp1;
6298 uint_t protocol = ipha->ipha_protocol;
6299 ipaddr_t dst;
6300 boolean_t one_only;
6301 mblk_t *first_mp = mp;
6302 boolean_t secure;
6303 uint32_t ill_index;
6304 conn_t *connp, *first_connp, *next_connp;
6305 connf_t *connfp;
6306 boolean_t shared_addr;
6307 mib2_ipIfStatsEntry_t *mibptr;
6308 ip_stack_t *ipst = recv_ill->ill_ipst;
6309 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6310
6311 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib;
6312 if (mctl_present) {
6313 mp = first_mp->b_cont;
6314 secure = ipsec_in_is_secure(first_mp);
6315 ASSERT(mp != NULL);
6316 } else {
6317 secure = B_FALSE;
6318 }
6319 dst = ipha->ipha_dst;
6320 /*
6321 * If the packet was tunneled and not multicast we only send to it
6322 * the first match.
6323 */
6324 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) &&
6325 !CLASSD(dst));
6326
6327 shared_addr = (zoneid == ALL_ZONES);
6328 if (shared_addr) {
6329 /*
6330 * We don't allow multilevel ports for raw IP, so no need to
6331 * check for that here.
6332 */
6333 zoneid = tsol_packet_to_zoneid(mp);
6334 }
6335
6336 connfp = &ipst->ips_ipcl_proto_fanout[protocol];
6337 mutex_enter(&connfp->connf_lock);
6338 connp = connfp->connf_head;
6339 for (connp = connfp->connf_head; connp != NULL;
6340 connp = connp->conn_next) {
6341 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags,
6342 zoneid) &&
6343 (!is_system_labeled() ||
6344 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
6345 connp))) {
6346 break;
6347 }
6348 }
6349
6350 if (connp == NULL || connp->conn_upq == NULL) {
6351 /*
6352 * No one bound to these addresses. Is
6353 * there a client that wants all
6354 * unclaimed datagrams?
6355 */
6356 mutex_exit(&connfp->connf_lock);
6357 /*
6358 * Check for IPPROTO_ENCAP...
6359 */
6360 if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) {
6361 /*
6362 * If an IPsec mblk is here on a multicast
6363 * tunnel (using ip_mroute stuff), check policy here,
6364 * THEN ship off to ip_mroute_decap().
6365 *
6366 * BTW, If I match a configured IP-in-IP
6367 * tunnel, this path will not be reached, and
6368 * ip_mroute_decap will never be called.
6369 */
6370 first_mp = ipsec_check_global_policy(first_mp, connp,
6371 ipha, NULL, mctl_present, ipst->ips_netstack);
6372 if (first_mp != NULL) {
6373 if (mctl_present)
6374 freeb(first_mp);
6375 ip_mroute_decap(q, mp, ill);
6376 } /* Else we already freed everything! */
6377 } else {
6378 /*
6379 * Otherwise send an ICMP protocol unreachable.
6380 */
6381 if (ip_fanout_send_icmp(q, first_mp, flags,
6382 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE,
6383 mctl_present, zoneid, ipst)) {
6384 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos);
6385 }
6386 }
6387 return;
6388 }
6389 CONN_INC_REF(connp);
6390 first_connp = connp;
6391
6392 /*
6393 * Only send message to one tunnel driver by immediately
6394 * terminating the loop.
6395 */
6396 connp = one_only ? NULL : connp->conn_next;
6397
6398 for (;;) {
6399 while (connp != NULL) {
6400 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill,
6401 flags, zoneid) &&
6402 (!is_system_labeled() ||
6403 tsol_receive_local(mp, &dst, IPV4_VERSION,
6404 shared_addr, connp)))
6405 break;
6406 connp = connp->conn_next;
6407 }
6408
6409 /*
6410 * Copy the packet.
6411 */
6412 if (connp == NULL || connp->conn_upq == NULL ||
6413 (((first_mp1 = dupmsg(first_mp)) == NULL) &&
6414 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) {
6415 /*
6416 * No more interested clients or memory
6417 * allocation failed
6418 */
6419 connp = first_connp;
6420 break;
6421 }
6422 mp1 = mctl_present ? first_mp1->b_cont : first_mp1;
6423 CONN_INC_REF(connp);
6424 mutex_exit(&connfp->connf_lock);
6425 rq = connp->conn_rq;
6426 if (!canputnext(rq)) {
6427 if (flags & IP_FF_RAWIP) {
6428 BUMP_MIB(mibptr, rawipIfStatsInOverflows);
6429 } else {
6430 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
6431 }
6432
6433 freemsg(first_mp1);
6434 } else {
6435 /*
6436 * Don't enforce here if we're an actual tunnel -
6437 * let "tun" do it instead.
6438 */
6439 if (!IPCL_IS_IPTUN(connp) &&
6440 (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
6441 secure)) {
6442 first_mp1 = ipsec_check_inbound_policy
6443 (first_mp1, connp, ipha, NULL,
6444 mctl_present);
6445 }
6446 if (first_mp1 != NULL) {
6447 int in_flags = 0;
6448 /*
6449 * ip_fanout_proto also gets called from
6450 * icmp_inbound_error_fanout, in which case
6451 * the msg type is M_CTL. Don't add info
6452 * in this case for the time being. In future
6453 * when there is a need for knowing the
6454 * inbound iface index for ICMP error msgs,
6455 * then this can be changed.
6456 */
6457 if (connp->conn_recvif)
6458 in_flags = IPF_RECVIF;
6459 /*
6460 * The ULP may support IP_RECVPKTINFO for both
6461 * IP v4 and v6 so pass the appropriate argument
6462 * based on conn IP version.
6463 */
6464 if (connp->conn_ip_recvpktinfo) {
6465 if (connp->conn_af_isv6) {
6466 /*
6467 * V6 only needs index
6468 */
6469 in_flags |= IPF_RECVIF;
6470 } else {
6471 /*
6472 * V4 needs index +
6473 * matching address.
6474 */
6475 in_flags |= IPF_RECVADDR;
6476 }
6477 }
6478 if ((in_flags != 0) &&
6479 (mp->b_datap->db_type != M_CTL)) {
6480 /*
6481 * the actual data will be
6482 * contained in b_cont upon
6483 * successful return of the
6484 * following call else
6485 * original mblk is returned
6486 */
6487 ASSERT(recv_ill != NULL);
6488 mp1 = ip_add_info(mp1, recv_ill,
6489 in_flags, IPCL_ZONEID(connp), ipst);
6490 }
6491 BUMP_MIB(mibptr, ipIfStatsHCInDelivers);
6492 if (mctl_present)
6493 freeb(first_mp1);
6494 (connp->conn_recv)(connp, mp1, NULL);
6495 }
6496 }
6497 mutex_enter(&connfp->connf_lock);
6498 /* Follow the next pointer before releasing the conn. */
6499 next_connp = connp->conn_next;
6500 CONN_DEC_REF(connp);
6501 connp = next_connp;
6502 }
6503
6504 /* Last one. Send it upstream. */
6505 mutex_exit(&connfp->connf_lock);
6506
6507 /*
6508 * If this packet is coming from icmp_inbound_error_fanout ip_policy
6509 * will be set to false.
6510 */
6511 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
6512 ill_index = ill->ill_phyint->phyint_ifindex;
6513 ip_process(IPP_LOCAL_IN, &mp, ill_index);
6514 if (mp == NULL) {
6515 CONN_DEC_REF(connp);
6516 if (mctl_present) {
6517 freeb(first_mp);
6518 }
6519 return;
6520 }
6521 }
6522
6523 rq = connp->conn_rq;
6524 if (!canputnext(rq)) {
6525 if (flags & IP_FF_RAWIP) {
6526 BUMP_MIB(mibptr, rawipIfStatsInOverflows);
6527 } else {
6528 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
6529 }
6530
6531 freemsg(first_mp);
6532 } else {
6533 if (IPCL_IS_IPTUN(connp)) {
6534 /*
6535 * Tunneled packet. We enforce policy in the tunnel
6536 * module itself.
6537 *
6538 * Send the WHOLE packet up (incl. IPSEC_IN) without
6539 * a policy check.
6540 * FIXME to use conn_recv for tun later.
6541 */
6542 putnext(rq, first_mp);
6543 CONN_DEC_REF(connp);
6544 return;
6545 }
6546
6547 if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) {
6548 first_mp = ipsec_check_inbound_policy(first_mp, connp,
6549 ipha, NULL, mctl_present);
6550 }
6551
6552 if (first_mp != NULL) {
6553 int in_flags = 0;
6554
6555 /*
6556 * ip_fanout_proto also gets called
6557 * from icmp_inbound_error_fanout, in
6558 * which case the msg type is M_CTL.
6559 * Don't add info in this case for time
6560 * being. In future when there is a
6561 * need for knowing the inbound iface
6562 * index for ICMP error msgs, then this
6563 * can be changed
6564 */
6565 if (connp->conn_recvif)
6566 in_flags = IPF_RECVIF;
6567 if (connp->conn_ip_recvpktinfo) {
6568 if (connp->conn_af_isv6) {
6569 /*
6570 * V6 only needs index
6571 */
6572 in_flags |= IPF_RECVIF;
6573 } else {
6574 /*
6575 * V4 needs index +
6576 * matching address.
6577 */
6578 in_flags |= IPF_RECVADDR;
6579 }
6580 }
6581 if ((in_flags != 0) &&
6582 (mp->b_datap->db_type != M_CTL)) {
6583
6584 /*
6585 * the actual data will be contained in
6586 * b_cont upon successful return
6587 * of the following call else original
6588 * mblk is returned
6589 */
6590 ASSERT(recv_ill != NULL);
6591 mp = ip_add_info(mp, recv_ill,
6592 in_flags, IPCL_ZONEID(connp), ipst);
6593 }
6594 BUMP_MIB(mibptr, ipIfStatsHCInDelivers);
6595 (connp->conn_recv)(connp, mp, NULL);
6596 if (mctl_present)
6597 freeb(first_mp);
6598 }
6599 }
6600 CONN_DEC_REF(connp);
6601 }
6602
6603 /*
6604 * Fanout for TCP packets
6605 * The caller puts <fport, lport> in the ports parameter.
6606 *
6607 * IPQoS Notes
6608 * Before sending it to the client, invoke IPPF processing.
6609 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN,
6610 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local
6611 * ip_policy is false.
6612 */
6613 static void
6614 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha,
6615 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid)
6616 {
6617 mblk_t *first_mp;
6618 boolean_t secure;
6619 uint32_t ill_index;
6620 int ip_hdr_len;
6621 tcph_t *tcph;
6622 boolean_t syn_present = B_FALSE;
6623 conn_t *connp;
6624 ip_stack_t *ipst = recv_ill->ill_ipst;
6625 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6626
6627 ASSERT(recv_ill != NULL);
6628
6629 first_mp = mp;
6630 if (mctl_present) {
6631 ASSERT(first_mp->b_datap->db_type == M_CTL);
6632 mp = first_mp->b_cont;
6633 secure = ipsec_in_is_secure(first_mp);
6634 ASSERT(mp != NULL);
6635 } else {
6636 secure = B_FALSE;
6637 }
6638
6639 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr);
6640
6641 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len,
6642 zoneid, ipst)) == NULL) {
6643 /*
6644 * No connected connection or listener. Send a
6645 * TH_RST via tcp_xmit_listeners_reset.
6646 */
6647
6648 /* Initiate IPPf processing, if needed. */
6649 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) {
6650 uint32_t ill_index;
6651 ill_index = recv_ill->ill_phyint->phyint_ifindex;
6652 ip_process(IPP_LOCAL_IN, &first_mp, ill_index);
6653 if (first_mp == NULL)
6654 return;
6655 }
6656 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
6657 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n",
6658 zoneid));
6659 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid,
6660 ipst->ips_netstack->netstack_tcp, NULL);
6661 return;
6662 }
6663
6664 /*
6665 * Allocate the SYN for the TCP connection here itself
6666 */
6667 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len];
6668 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) {
6669 if (IPCL_IS_TCP(connp)) {
6670 squeue_t *sqp;
6671
6672 /*
6673 * For fused tcp loopback, assign the eager's
6674 * squeue to be that of the active connect's.
6675 * Note that we don't check for IP_FF_LOOPBACK
6676 * here since this routine gets called only
6677 * for loopback (unlike the IPv6 counterpart).
6678 */
6679 ASSERT(Q_TO_CONN(q) != NULL);
6680 if (do_tcp_fusion &&
6681 !CONN_INBOUND_POLICY_PRESENT(connp, ipss) &&
6682 !secure &&
6683 !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy &&
6684 IPCL_IS_TCP(Q_TO_CONN(q))) {
6685 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL);
6686 sqp = Q_TO_CONN(q)->conn_sqp;
6687 } else {
6688 sqp = IP_SQUEUE_GET(lbolt);
6689 }
6690
6691 mp->b_datap->db_struioflag |= STRUIO_EAGER;
6692 DB_CKSUMSTART(mp) = (intptr_t)sqp;
6693 syn_present = B_TRUE;
6694 }
6695 }
6696
6697 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) {
6698 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF;
6699 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
6700 if ((flags & TH_RST) || (flags & TH_URG)) {
6701 DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL,
6702 void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph);
6703 CONN_DEC_REF(connp);
6704 freemsg(first_mp);
6705 return;
6706 }
6707 if (flags & TH_ACK) {
6708 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid,
6709 ipst->ips_netstack->netstack_tcp, connp);
6710 CONN_DEC_REF(connp);
6711 return;
6712 }
6713
6714 DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL,
6715 void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph);
6716 CONN_DEC_REF(connp);
6717 freemsg(first_mp);
6718 return;
6719 }
6720
6721 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) {
6722 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha,
6723 NULL, mctl_present);
6724 if (first_mp == NULL) {
6725 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
6726 CONN_DEC_REF(connp);
6727 return;
6728 }
6729 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) {
6730 ASSERT(syn_present);
6731 if (mctl_present) {
6732 ASSERT(first_mp != mp);
6733 first_mp->b_datap->db_struioflag |=
6734 STRUIO_POLICY;
6735 } else {
6736 ASSERT(first_mp == mp);
6737 mp->b_datap->db_struioflag &=
6738 ~STRUIO_EAGER;
6739 mp->b_datap->db_struioflag |=
6740 STRUIO_POLICY;
6741 }
6742 } else {
6743 /*
6744 * Discard first_mp early since we're dealing with a
6745 * fully-connected conn_t and tcp doesn't do policy in
6746 * this case.
6747 */
6748 if (mctl_present) {
6749 freeb(first_mp);
6750 mctl_present = B_FALSE;
6751 }
6752 first_mp = mp;
6753 }
6754 }
6755
6756 /*
6757 * Initiate policy processing here if needed. If we get here from
6758 * icmp_inbound_error_fanout, ip_policy is false.
6759 */
6760 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
6761 ill_index = recv_ill->ill_phyint->phyint_ifindex;
6762 ip_process(IPP_LOCAL_IN, &mp, ill_index);
6763 if (mp == NULL) {
6764 CONN_DEC_REF(connp);
6765 if (mctl_present)
6766 freeb(first_mp);
6767 return;
6768 } else if (mctl_present) {
6769 ASSERT(first_mp != mp);
6770 first_mp->b_cont = mp;
6771 } else {
6772 first_mp = mp;
6773 }
6774 }
6775
6776
6777
6778 /* Handle socket options. */
6779 if (!syn_present &&
6780 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) {
6781 /* Add header */
6782 ASSERT(recv_ill != NULL);
6783 /*
6784 * Since tcp does not support IP_RECVPKTINFO for V4, only pass
6785 * IPF_RECVIF.
6786 */
6787 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp),
6788 ipst);
6789 if (mp == NULL) {
6790 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
6791 CONN_DEC_REF(connp);
6792 if (mctl_present)
6793 freeb(first_mp);
6794 return;
6795 } else if (mctl_present) {
6796 /*
6797 * ip_add_info might return a new mp.
6798 */
6799 ASSERT(first_mp != mp);
6800 first_mp->b_cont = mp;
6801 } else {
6802 first_mp = mp;
6803 }
6804 }
6805 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
6806 if (IPCL_IS_TCP(connp)) {
6807 /* do not drain, certain use cases can blow the stack */
6808 squeue_enter_nodrain(connp->conn_sqp, first_mp,
6809 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP);
6810 } else {
6811 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
6812 (connp->conn_recv)(connp, first_mp, NULL);
6813 CONN_DEC_REF(connp);
6814 }
6815 }
6816
6817 /*
6818 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
6819 * pass it along to ESP if the SPI is non-zero. Returns TRUE if the mblk
6820 * is not consumed.
6821 *
6822 * One of four things can happen, all of which affect the passed-in mblk:
6823 *
6824 * 1.) ICMP messages that go through here just get returned TRUE.
6825 *
6826 * 2.) The packet is stock UDP and gets its zero-SPI stripped. Return TRUE.
6827 *
6828 * 3.) The packet is ESP-in-UDP, gets transformed into an equivalent
6829 * ESP packet, and is passed along to ESP for consumption. Return FALSE.
6830 *
6831 * 4.) The packet is an ESP-in-UDP Keepalive. Drop it and return FALSE.
6832 */
6833 static boolean_t
6834 zero_spi_check(queue_t *q, mblk_t *mp, ire_t *ire, ill_t *recv_ill,
6835 ipsec_stack_t *ipss)
6836 {
6837 int shift, plen, iph_len;
6838 ipha_t *ipha;
6839 udpha_t *udpha;
6840 uint32_t *spi;
6841 uint32_t esp_ports;
6842 uint8_t *orptr;
6843 boolean_t free_ire;
6844
6845 if (DB_TYPE(mp) == M_CTL) {
6846 /*
6847 * ICMP message with UDP inside. Don't bother stripping, just
6848 * send it up.
6849 *
6850 * NOTE: Any app with UDP_NAT_T_ENDPOINT set is probably going
6851 * to ignore errors set by ICMP anyway ('cause they might be
6852 * forged), but that's the app's decision, not ours.
6853 */
6854
6855 /* Bunch of reality checks for DEBUG kernels... */
6856 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
6857 ASSERT(((ipha_t *)mp->b_rptr)->ipha_protocol == IPPROTO_ICMP);
6858
6859 return (B_TRUE);
6860 }
6861
6862 ipha = (ipha_t *)mp->b_rptr;
6863 iph_len = IPH_HDR_LENGTH(ipha);
6864 plen = ntohs(ipha->ipha_length);
6865
6866 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
6867 /*
6868 * Most likely a keepalive for the benefit of an intervening
6869 * NAT. These aren't for us, per se, so drop it.
6870 *
6871 * RFC 3947/8 doesn't say for sure what to do for 2-3
6872 * byte packets (keepalives are 1-byte), but we'll drop them
6873 * also.
6874 */
6875 ip_drop_packet(mp, B_TRUE, recv_ill, NULL,
6876 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
6877 return (B_FALSE);
6878 }
6879
6880 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
6881 /* might as well pull it all up - it might be ESP. */
6882 if (!pullupmsg(mp, -1)) {
6883 ip_drop_packet(mp, B_TRUE, recv_ill, NULL,
6884 DROPPER(ipss, ipds_esp_nomem),
6885 &ipss->ipsec_dropper);
6886 return (B_FALSE);
6887 }
6888
6889 ipha = (ipha_t *)mp->b_rptr;
6890 }
6891 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
6892 if (*spi == 0) {
6893 /* UDP packet - remove 0-spi. */
6894 shift = sizeof (uint32_t);
6895 } else {
6896 /* ESP-in-UDP packet - reduce to ESP. */
6897 ipha->ipha_protocol = IPPROTO_ESP;
6898 shift = sizeof (udpha_t);
6899 }
6900
6901 /* Fix IP header */
6902 ipha->ipha_length = htons(plen - shift);
6903 ipha->ipha_hdr_checksum = 0;
6904
6905 orptr = mp->b_rptr;
6906 mp->b_rptr += shift;
6907
6908 udpha = (udpha_t *)(orptr + iph_len);
6909 if (*spi == 0) {
6910 ASSERT((uint8_t *)ipha == orptr);
6911 udpha->uha_length = htons(plen - shift - iph_len);
6912 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
6913 esp_ports = 0;
6914 } else {
6915 esp_ports = *((uint32_t *)udpha);
6916 ASSERT(esp_ports != 0);
6917 }
6918 ovbcopy(orptr, orptr + shift, iph_len);
6919 if (esp_ports != 0) /* Punt up for ESP processing. */ {
6920 ipha = (ipha_t *)(orptr + shift);
6921
6922 free_ire = (ire == NULL);
6923 if (free_ire) {
6924 /* Re-acquire ire. */
6925 ire = ire_cache_lookup(ipha->ipha_dst, ALL_ZONES, NULL,
6926 ipss->ipsec_netstack->netstack_ip);
6927 if (ire == NULL || !(ire->ire_type & IRE_LOCAL)) {
6928 if (ire != NULL)
6929 ire_refrele(ire);
6930 /*
6931 * Do a regular freemsg(), as this is an IP
6932 * error (no local route) not an IPsec one.
6933 */
6934 freemsg(mp);
6935 }
6936 }
6937
6938 ip_proto_input(q, mp, ipha, ire, recv_ill, esp_ports);
6939 if (free_ire)
6940 ire_refrele(ire);
6941 }
6942
6943 return (esp_ports == 0);
6944 }
6945
6946 /*
6947 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
6948 * We are responsible for disposing of mp, such as by freemsg() or putnext()
6949 * Caller is responsible for dropping references to the conn, and freeing
6950 * first_mp.
6951 *
6952 * IPQoS Notes
6953 * Before sending it to the client, invoke IPPF processing. Policy processing
6954 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and
6955 * ip_policy is true. If we get here from icmp_inbound_error_fanout or
6956 * ip_wput_local, ip_policy is false.
6957 */
6958 static void
6959 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp,
6960 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill,
6961 boolean_t ip_policy)
6962 {
6963 boolean_t mctl_present = (first_mp != NULL);
6964 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */
6965 uint32_t ill_index;
6966 ip_stack_t *ipst = recv_ill->ill_ipst;
6967 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6968
6969 ASSERT(ill != NULL);
6970
6971 if (mctl_present)
6972 first_mp->b_cont = mp;
6973 else
6974 first_mp = mp;
6975
6976 if (CONN_UDP_FLOWCTLD(connp)) {
6977 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
6978 freemsg(first_mp);
6979 return;
6980 }
6981
6982 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) {
6983 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha,
6984 NULL, mctl_present);
6985 if (first_mp == NULL) {
6986 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
6987 return; /* Freed by ipsec_check_inbound_policy(). */
6988 }
6989 }
6990 if (mctl_present)
6991 freeb(first_mp);
6992
6993 /* Let's hope the compilers utter "branch, predict-not-taken..." ;) */
6994 if (connp->conn_udp->udp_nat_t_endpoint) {
6995 if (mctl_present) {
6996 /* mctl_present *shouldn't* happen. */
6997 ip_drop_packet(mp, B_TRUE, NULL, NULL,
6998 DROPPER(ipss, ipds_esp_nat_t_ipsec),
6999 &ipss->ipsec_dropper);
7000 return;
7001 }
7002
7003 if (!zero_spi_check(ill->ill_rq, mp, NULL, recv_ill, ipss))
7004 return;
7005 }
7006
7007 /* Handle options. */
7008 if (connp->conn_recvif)
7009 in_flags = IPF_RECVIF;
7010 /*
7011 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag
7012 * passed to ip_add_info is based on IP version of connp.
7013 */
7014 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) {
7015 if (connp->conn_af_isv6) {
7016 /*
7017 * V6 only needs index
7018 */
7019 in_flags |= IPF_RECVIF;
7020 } else {
7021 /*
7022 * V4 needs index + matching address.
7023 */
7024 in_flags |= IPF_RECVADDR;
7025 }
7026 }
7027
7028 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA))
7029 in_flags |= IPF_RECVSLLA;
7030
7031 /*
7032 * Initiate IPPF processing here, if needed. Note first_mp won't be
7033 * freed if the packet is dropped. The caller will do so.
7034 */
7035 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
7036 ill_index = recv_ill->ill_phyint->phyint_ifindex;
7037 ip_process(IPP_LOCAL_IN, &mp, ill_index);
7038 if (mp == NULL) {
7039 return;
7040 }
7041 }
7042 if ((in_flags != 0) &&
7043 (mp->b_datap->db_type != M_CTL)) {
7044 /*
7045 * The actual data will be contained in b_cont
7046 * upon successful return of the following call
7047 * else original mblk is returned
7048 */
7049 ASSERT(recv_ill != NULL);
7050 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp),
7051 ipst);
7052 }
7053 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
7054 /* Send it upstream */
7055 (connp->conn_recv)(connp, mp, NULL);
7056 }
7057
7058 /*
7059 * Fanout for UDP packets.
7060 * The caller puts <fport, lport> in the ports parameter.
7061 *
7062 * If SO_REUSEADDR is set all multicast and broadcast packets
7063 * will be delivered to all streams bound to the same port.
7064 *
7065 * Zones notes:
7066 * Multicast and broadcast packets will be distributed to streams in all zones.
7067 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
7068 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
7069 * packets. To maintain this behavior with multiple zones, the conns are grouped
7070 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in
7071 * each zone. If unset, all the following conns in the same zone are skipped.
7072 */
7073 static void
7074 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha,
7075 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present,
7076 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid)
7077 {
7078 uint32_t dstport, srcport;
7079 ipaddr_t dst;
7080 mblk_t *first_mp;
7081 boolean_t secure;
7082 in6_addr_t v6src;
7083 conn_t *connp;
7084 connf_t *connfp;
7085 conn_t *first_connp;
7086 conn_t *next_connp;
7087 mblk_t *mp1, *first_mp1;
7088 ipaddr_t src;
7089 zoneid_t last_zoneid;
7090 boolean_t reuseaddr;
7091 boolean_t shared_addr;
7092 boolean_t unlabeled;
7093 ip_stack_t *ipst;
7094
7095 ASSERT(recv_ill != NULL);
7096 ipst = recv_ill->ill_ipst;
7097
7098 first_mp = mp;
7099 if (mctl_present) {
7100 mp = first_mp->b_cont;
7101 first_mp->b_cont = NULL;
7102 secure = ipsec_in_is_secure(first_mp);
7103 ASSERT(mp != NULL);
7104 } else {
7105 first_mp = NULL;
7106 secure = B_FALSE;
7107 }
7108
7109 /* Extract ports in net byte order */
7110 dstport = htons(ntohl(ports) & 0xFFFF);
7111 srcport = htons(ntohl(ports) >> 16);
7112 dst = ipha->ipha_dst;
7113 src = ipha->ipha_src;
7114
7115 unlabeled = B_FALSE;
7116 if (is_system_labeled())
7117 /* Cred cannot be null on IPv4 */
7118 unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
7119 TSLF_UNLABELED) != 0;
7120 shared_addr = (zoneid == ALL_ZONES);
7121 if (shared_addr) {
7122 /*
7123 * No need to handle exclusive-stack zones since ALL_ZONES
7124 * only applies to the shared stack.
7125 */
7126 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport);
7127 /*
7128 * If no shared MLP is found, tsol_mlp_findzone returns
7129 * ALL_ZONES. In that case, we assume it's SLP, and
7130 * search for the zone based on the packet label.
7131 *
7132 * If there is such a zone, we prefer to find a
7133 * connection in it. Otherwise, we look for a
7134 * MAC-exempt connection in any zone whose label
7135 * dominates the default label on the packet.
7136 */
7137 if (zoneid == ALL_ZONES)
7138 zoneid = tsol_packet_to_zoneid(mp);
7139 else
7140 unlabeled = B_FALSE;
7141 }
7142
7143 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)];
7144 mutex_enter(&connfp->connf_lock);
7145 connp = connfp->connf_head;
7146 if (!broadcast && !CLASSD(dst)) {
7147 /*
7148 * Not broadcast or multicast. Send to the one (first)
7149 * client we find. No need to check conn_wantpacket()
7150 * since IP_BOUND_IF/conn_incoming_ill does not apply to
7151 * IPv4 unicast packets.
7152 */
7153 while ((connp != NULL) &&
7154 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) ||
7155 (!IPCL_ZONE_MATCH(connp, zoneid) &&
7156 !(unlabeled && connp->conn_mac_exempt)))) {
7157 /*
7158 * We keep searching since the conn did not match,
7159 * or its zone did not match and it is not either
7160 * an allzones conn or a mac exempt conn (if the
7161 * sender is unlabeled.)
7162 */
7163 connp = connp->conn_next;
7164 }
7165
7166 if (connp == NULL || connp->conn_upq == NULL)
7167 goto notfound;
7168
7169 if (is_system_labeled() &&
7170 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7171 connp))
7172 goto notfound;
7173
7174 CONN_INC_REF(connp);
7175 mutex_exit(&connfp->connf_lock);
7176 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha,
7177 flags, recv_ill, ip_policy);
7178 IP_STAT(ipst, ip_udp_fannorm);
7179 CONN_DEC_REF(connp);
7180 return;
7181 }
7182
7183 /*
7184 * Broadcast and multicast case
7185 *
7186 * Need to check conn_wantpacket().
7187 * If SO_REUSEADDR has been set on the first we send the
7188 * packet to all clients that have joined the group and
7189 * match the port.
7190 */
7191
7192 while (connp != NULL) {
7193 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) &&
7194 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7195 (!is_system_labeled() ||
7196 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7197 connp)))
7198 break;
7199 connp = connp->conn_next;
7200 }
7201
7202 if (connp == NULL || connp->conn_upq == NULL)
7203 goto notfound;
7204
7205 first_connp = connp;
7206 /*
7207 * When SO_REUSEADDR is not set, send the packet only to the first
7208 * matching connection in its zone by keeping track of the zoneid.
7209 */
7210 reuseaddr = first_connp->conn_reuseaddr;
7211 last_zoneid = first_connp->conn_zoneid;
7212
7213 CONN_INC_REF(connp);
7214 connp = connp->conn_next;
7215 for (;;) {
7216 while (connp != NULL) {
7217 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) &&
7218 (reuseaddr || connp->conn_zoneid != last_zoneid) &&
7219 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7220 (!is_system_labeled() ||
7221 tsol_receive_local(mp, &dst, IPV4_VERSION,
7222 shared_addr, connp)))
7223 break;
7224 connp = connp->conn_next;
7225 }
7226 /*
7227 * Just copy the data part alone. The mctl part is
7228 * needed just for verifying policy and it is never
7229 * sent up.
7230 */
7231 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) &&
7232 ((mp1 = copymsg(mp)) == NULL))) {
7233 /*
7234 * No more interested clients or memory
7235 * allocation failed
7236 */
7237 connp = first_connp;
7238 break;
7239 }
7240 if (connp->conn_zoneid != last_zoneid) {
7241 /*
7242 * Update the zoneid so that the packet isn't sent to
7243 * any more conns in the same zone unless SO_REUSEADDR
7244 * is set.
7245 */
7246 reuseaddr = connp->conn_reuseaddr;
7247 last_zoneid = connp->conn_zoneid;
7248 }
7249 if (first_mp != NULL) {
7250 ASSERT(((ipsec_info_t *)first_mp->b_rptr)->
7251 ipsec_info_type == IPSEC_IN);
7252 first_mp1 = ipsec_in_tag(first_mp, NULL,
7253 ipst->ips_netstack);
7254 if (first_mp1 == NULL) {
7255 freemsg(mp1);
7256 connp = first_connp;
7257 break;
7258 }
7259 } else {
7260 first_mp1 = NULL;
7261 }
7262 CONN_INC_REF(connp);
7263 mutex_exit(&connfp->connf_lock);
7264 /*
7265 * IPQoS notes: We don't send the packet for policy
7266 * processing here, will do it for the last one (below).
7267 * i.e. we do it per-packet now, but if we do policy
7268 * processing per-conn, then we would need to do it
7269 * here too.
7270 */
7271 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill,
7272 ipha, flags, recv_ill, B_FALSE);
7273 mutex_enter(&connfp->connf_lock);
7274 /* Follow the next pointer before releasing the conn. */
7275 next_connp = connp->conn_next;
7276 IP_STAT(ipst, ip_udp_fanmb);
7277 CONN_DEC_REF(connp);
7278 connp = next_connp;
7279 }
7280
7281 /* Last one. Send it upstream. */
7282 mutex_exit(&connfp->connf_lock);
7283 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags,
7284 recv_ill, ip_policy);
7285 IP_STAT(ipst, ip_udp_fanmb);
7286 CONN_DEC_REF(connp);
7287 return;
7288
7289 notfound:
7290
7291 mutex_exit(&connfp->connf_lock);
7292 IP_STAT(ipst, ip_udp_fanothers);
7293 /*
7294 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses
7295 * have already been matched above, since they live in the IPv4
7296 * fanout tables. This implies we only need to
7297 * check for IPv6 in6addr_any endpoints here.
7298 * Thus we compare using ipv6_all_zeros instead of the destination
7299 * address, except for the multicast group membership lookup which
7300 * uses the IPv4 destination.
7301 */
7302 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src);
7303 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)];
7304 mutex_enter(&connfp->connf_lock);
7305 connp = connfp->connf_head;
7306 if (!broadcast && !CLASSD(dst)) {
7307 while (connp != NULL) {
7308 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros,
7309 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) &&
7310 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7311 !connp->conn_ipv6_v6only)
7312 break;
7313 connp = connp->conn_next;
7314 }
7315
7316 if (connp != NULL && is_system_labeled() &&
7317 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7318 connp))
7319 connp = NULL;
7320
7321 if (connp == NULL || connp->conn_upq == NULL) {
7322 /*
7323 * No one bound to this port. Is
7324 * there a client that wants all
7325 * unclaimed datagrams?
7326 */
7327 mutex_exit(&connfp->connf_lock);
7328
7329 if (mctl_present)
7330 first_mp->b_cont = mp;
7331 else
7332 first_mp = mp;
7333 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].
7334 connf_head != NULL) {
7335 ip_fanout_proto(q, first_mp, ill, ipha,
7336 flags | IP_FF_RAWIP, mctl_present,
7337 ip_policy, recv_ill, zoneid);
7338 } else {
7339 if (ip_fanout_send_icmp(q, first_mp, flags,
7340 ICMP_DEST_UNREACHABLE,
7341 ICMP_PORT_UNREACHABLE,
7342 mctl_present, zoneid, ipst)) {
7343 BUMP_MIB(ill->ill_ip_mib,
7344 udpIfStatsNoPorts);
7345 }
7346 }
7347 return;
7348 }
7349
7350 CONN_INC_REF(connp);
7351 mutex_exit(&connfp->connf_lock);
7352 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha,
7353 flags, recv_ill, ip_policy);
7354 CONN_DEC_REF(connp);
7355 return;
7356 }
7357 /*
7358 * IPv4 multicast packet being delivered to an AF_INET6
7359 * in6addr_any endpoint.
7360 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
7361 * and not conn_wantpacket_v6() since any multicast membership is
7362 * for an IPv4-mapped multicast address.
7363 * The packet is sent to all clients in all zones that have joined the
7364 * group and match the port.
7365 */
7366 while (connp != NULL) {
7367 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros,
7368 srcport, v6src) &&
7369 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7370 (!is_system_labeled() ||
7371 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7372 connp)))
7373 break;
7374 connp = connp->conn_next;
7375 }
7376
7377 if (connp == NULL || connp->conn_upq == NULL) {
7378 /*
7379 * No one bound to this port. Is
7380 * there a client that wants all
7381 * unclaimed datagrams?
7382 */
7383 mutex_exit(&connfp->connf_lock);
7384
7385 if (mctl_present)
7386 first_mp->b_cont = mp;
7387 else
7388 first_mp = mp;
7389 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head !=
7390 NULL) {
7391 ip_fanout_proto(q, first_mp, ill, ipha,
7392 flags | IP_FF_RAWIP, mctl_present, ip_policy,
7393 recv_ill, zoneid);
7394 } else {
7395 /*
7396 * We used to attempt to send an icmp error here, but
7397 * since this is known to be a multicast packet
7398 * and we don't send icmp errors in response to
7399 * multicast, just drop the packet and give up sooner.
7400 */
7401 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
7402 freemsg(first_mp);
7403 }
7404 return;
7405 }
7406
7407 first_connp = connp;
7408
7409 CONN_INC_REF(connp);
7410 connp = connp->conn_next;
7411 for (;;) {
7412 while (connp != NULL) {
7413 if (IPCL_UDP_MATCH_V6(connp, dstport,
7414 ipv6_all_zeros, srcport, v6src) &&
7415 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7416 (!is_system_labeled() ||
7417 tsol_receive_local(mp, &dst, IPV4_VERSION,
7418 shared_addr, connp)))
7419 break;
7420 connp = connp->conn_next;
7421 }
7422 /*
7423 * Just copy the data part alone. The mctl part is
7424 * needed just for verifying policy and it is never
7425 * sent up.
7426 */
7427 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) &&
7428 ((mp1 = copymsg(mp)) == NULL))) {
7429 /*
7430 * No more intested clients or memory
7431 * allocation failed
7432 */
7433 connp = first_connp;
7434 break;
7435 }
7436 if (first_mp != NULL) {
7437 ASSERT(((ipsec_info_t *)first_mp->b_rptr)->
7438 ipsec_info_type == IPSEC_IN);
7439 first_mp1 = ipsec_in_tag(first_mp, NULL,
7440 ipst->ips_netstack);
7441 if (first_mp1 == NULL) {
7442 freemsg(mp1);
7443 connp = first_connp;
7444 break;
7445 }
7446 } else {
7447 first_mp1 = NULL;
7448 }
7449 CONN_INC_REF(connp);
7450 mutex_exit(&connfp->connf_lock);
7451 /*
7452 * IPQoS notes: We don't send the packet for policy
7453 * processing here, will do it for the last one (below).
7454 * i.e. we do it per-packet now, but if we do policy
7455 * processing per-conn, then we would need to do it
7456 * here too.
7457 */
7458 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill,
7459 ipha, flags, recv_ill, B_FALSE);
7460 mutex_enter(&connfp->connf_lock);
7461 /* Follow the next pointer before releasing the conn. */
7462 next_connp = connp->conn_next;
7463 CONN_DEC_REF(connp);
7464 connp = next_connp;
7465 }
7466
7467 /* Last one. Send it upstream. */
7468 mutex_exit(&connfp->connf_lock);
7469 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags,
7470 recv_ill, ip_policy);
7471 CONN_DEC_REF(connp);
7472 }
7473
7474 /*
7475 * Complete the ip_wput header so that it
7476 * is possible to generate ICMP
7477 * errors.
7478 */
7479 int
7480 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst)
7481 {
7482 ire_t *ire;
7483
7484 if (ipha->ipha_src == INADDR_ANY) {
7485 ire = ire_lookup_local(zoneid, ipst);
7486 if (ire == NULL) {
7487 ip1dbg(("ip_hdr_complete: no source IRE\n"));
7488 return (1);
7489 }
7490 ipha->ipha_src = ire->ire_addr;
7491 ire_refrele(ire);
7492 }
7493 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
7494 ipha->ipha_hdr_checksum = 0;
7495 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
7496 return (0);
7497 }
7498
7499 /*
7500 * Nobody should be sending
7501 * packets up this stream
7502 */
7503 static void
7504 ip_lrput(queue_t *q, mblk_t *mp)
7505 {
7506 mblk_t *mp1;
7507
7508 switch (mp->b_datap->db_type) {
7509 case M_FLUSH:
7510 /* Turn around */
7511 if (*mp->b_rptr & FLUSHW) {
7512 *mp->b_rptr &= ~FLUSHR;
7513 qreply(q, mp);
7514 return;
7515 }
7516 break;
7517 }
7518 /* Could receive messages that passed through ar_rput */
7519 for (mp1 = mp; mp1; mp1 = mp1->b_cont)
7520 mp1->b_prev = mp1->b_next = NULL;
7521 freemsg(mp);
7522 }
7523
7524 /* Nobody should be sending packets down this stream */
7525 /* ARGSUSED */
7526 void
7527 ip_lwput(queue_t *q, mblk_t *mp)
7528 {
7529 freemsg(mp);
7530 }
7531
7532 /*
7533 * Move the first hop in any source route to ipha_dst and remove that part of
7534 * the source route. Called by other protocols. Errors in option formatting
7535 * are ignored - will be handled by ip_wput_options Return the final
7536 * destination (either ipha_dst or the last entry in a source route.)
7537 */
7538 ipaddr_t
7539 ip_massage_options(ipha_t *ipha, netstack_t *ns)
7540 {
7541 ipoptp_t opts;
7542 uchar_t *opt;
7543 uint8_t optval;
7544 uint8_t optlen;
7545 ipaddr_t dst;
7546 int i;
7547 ire_t *ire;
7548 ip_stack_t *ipst = ns->netstack_ip;
7549
7550 ip2dbg(("ip_massage_options\n"));
7551 dst = ipha->ipha_dst;
7552 for (optval = ipoptp_first(&opts, ipha);
7553 optval != IPOPT_EOL;
7554 optval = ipoptp_next(&opts)) {
7555 opt = opts.ipoptp_cur;
7556 switch (optval) {
7557 uint8_t off;
7558 case IPOPT_SSRR:
7559 case IPOPT_LSRR:
7560 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
7561 ip1dbg(("ip_massage_options: bad src route\n"));
7562 break;
7563 }
7564 optlen = opts.ipoptp_len;
7565 off = opt[IPOPT_OFFSET];
7566 off--;
7567 redo_srr:
7568 if (optlen < IP_ADDR_LEN ||
7569 off > optlen - IP_ADDR_LEN) {
7570 /* End of source route */
7571 ip1dbg(("ip_massage_options: end of SR\n"));
7572 break;
7573 }
7574 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
7575 ip1dbg(("ip_massage_options: next hop 0x%x\n",
7576 ntohl(dst)));
7577 /*
7578 * Check if our address is present more than
7579 * once as consecutive hops in source route.
7580 * XXX verify per-interface ip_forwarding
7581 * for source route?
7582 */
7583 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL,
7584 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7585 if (ire != NULL) {
7586 ire_refrele(ire);
7587 off += IP_ADDR_LEN;
7588 goto redo_srr;
7589 }
7590 if (dst == htonl(INADDR_LOOPBACK)) {
7591 ip1dbg(("ip_massage_options: loopback addr in "
7592 "source route!\n"));
7593 break;
7594 }
7595 /*
7596 * Update ipha_dst to be the first hop and remove the
7597 * first hop from the source route (by overwriting
7598 * part of the option with NOP options).
7599 */
7600 ipha->ipha_dst = dst;
7601 /* Put the last entry in dst */
7602 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
7603 3;
7604 bcopy(&opt[off], &dst, IP_ADDR_LEN);
7605
7606 ip1dbg(("ip_massage_options: last hop 0x%x\n",
7607 ntohl(dst)));
7608 /* Move down and overwrite */
7609 opt[IP_ADDR_LEN] = opt[0];
7610 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
7611 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
7612 for (i = 0; i < IP_ADDR_LEN; i++)
7613 opt[i] = IPOPT_NOP;
7614 break;
7615 }
7616 }
7617 return (dst);
7618 }
7619
7620 /*
7621 * Return the network mask
7622 * associated with the specified address.
7623 */
7624 ipaddr_t
7625 ip_net_mask(ipaddr_t addr)
7626 {
7627 uchar_t *up = (uchar_t *)&addr;
7628 ipaddr_t mask = 0;
7629 uchar_t *maskp = (uchar_t *)&mask;
7630
7631 #if defined(__i386) || defined(__amd64)
7632 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
7633 #endif
7634 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
7635 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
7636 #endif
7637 if (CLASSD(addr)) {
7638 maskp[0] = 0xF0;
7639 return (mask);
7640 }
7641
7642 /* We assume Class E default netmask to be 32 */
7643 if (CLASSE(addr))
7644 return (0xffffffffU);
7645
7646 if (addr == 0)
7647 return (0);
7648 maskp[0] = 0xFF;
7649 if ((up[0] & 0x80) == 0)
7650 return (mask);
7651
7652 maskp[1] = 0xFF;
7653 if ((up[0] & 0xC0) == 0x80)
7654 return (mask);
7655
7656 maskp[2] = 0xFF;
7657 if ((up[0] & 0xE0) == 0xC0)
7658 return (mask);
7659
7660 /* Otherwise return no mask */
7661 return ((ipaddr_t)0);
7662 }
7663
7664 /*
7665 * Select an ill for the packet by considering load spreading across
7666 * a different ill in the group if dst_ill is part of some group.
7667 */
7668 ill_t *
7669 ip_newroute_get_dst_ill(ill_t *dst_ill)
7670 {
7671 ill_t *ill;
7672
7673 /*
7674 * We schedule irrespective of whether the source address is
7675 * INADDR_ANY or not. illgrp_scheduler returns a held ill.
7676 */
7677 ill = illgrp_scheduler(dst_ill);
7678 if (ill == NULL)
7679 return (NULL);
7680
7681 /*
7682 * For groups with names ip_sioctl_groupname ensures that all
7683 * ills are of same type. For groups without names, ifgrp_insert
7684 * ensures this.
7685 */
7686 ASSERT(dst_ill->ill_type == ill->ill_type);
7687
7688 return (ill);
7689 }
7690
7691 /*
7692 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case.
7693 */
7694 ill_t *
7695 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6,
7696 ip_stack_t *ipst)
7697 {
7698 ill_t *ret_ill;
7699
7700 ASSERT(ifindex != 0);
7701 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
7702 ipst);
7703 if (ret_ill == NULL ||
7704 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) {
7705 if (isv6) {
7706 if (ill != NULL) {
7707 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
7708 } else {
7709 BUMP_MIB(&ipst->ips_ip6_mib,
7710 ipIfStatsOutDiscards);
7711 }
7712 ip1dbg(("ip_grab_attach_ill (IPv6): "
7713 "bad ifindex %d.\n", ifindex));
7714 } else {
7715 if (ill != NULL) {
7716 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
7717 } else {
7718 BUMP_MIB(&ipst->ips_ip_mib,
7719 ipIfStatsOutDiscards);
7720 }
7721 ip1dbg(("ip_grab_attach_ill (IPv4): "
7722 "bad ifindex %d.\n", ifindex));
7723 }
7724 if (ret_ill != NULL)
7725 ill_refrele(ret_ill);
7726 freemsg(first_mp);
7727 return (NULL);
7728 }
7729
7730 return (ret_ill);
7731 }
7732
7733 /*
7734 * IPv4 -
7735 * ip_newroute is called by ip_rput or ip_wput whenever we need to send
7736 * out a packet to a destination address for which we do not have specific
7737 * (or sufficient) routing information.
7738 *
7739 * NOTE : These are the scopes of some of the variables that point at IRE,
7740 * which needs to be followed while making any future modifications
7741 * to avoid memory leaks.
7742 *
7743 * - ire and sire are the entries looked up initially by
7744 * ire_ftable_lookup.
7745 * - ipif_ire is used to hold the interface ire associated with
7746 * the new cache ire. But it's scope is limited, so we always REFRELE
7747 * it before branching out to error paths.
7748 * - save_ire is initialized before ire_create, so that ire returned
7749 * by ire_create will not over-write the ire. We REFRELE save_ire
7750 * before breaking out of the switch.
7751 *
7752 * Thus on failures, we have to REFRELE only ire and sire, if they
7753 * are not NULL.
7754 */
7755 void
7756 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, conn_t *connp,
7757 zoneid_t zoneid, ip_stack_t *ipst)
7758 {
7759 areq_t *areq;
7760 ipaddr_t gw = 0;
7761 ire_t *ire = NULL;
7762 mblk_t *res_mp;
7763 ipaddr_t *addrp;
7764 ipaddr_t nexthop_addr;
7765 ipif_t *src_ipif = NULL;
7766 ill_t *dst_ill = NULL;
7767 ipha_t *ipha;
7768 ire_t *sire = NULL;
7769 mblk_t *first_mp;
7770 ire_t *save_ire;
7771 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */
7772 ushort_t ire_marks = 0;
7773 boolean_t mctl_present;
7774 ipsec_out_t *io;
7775 mblk_t *saved_mp;
7776 ire_t *first_sire = NULL;
7777 mblk_t *copy_mp = NULL;
7778 mblk_t *xmit_mp = NULL;
7779 ipaddr_t save_dst;
7780 uint32_t multirt_flags =
7781 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP;
7782 boolean_t multirt_is_resolvable;
7783 boolean_t multirt_resolve_next;
7784 boolean_t unspec_src;
7785 boolean_t do_attach_ill = B_FALSE;
7786 boolean_t ip_nexthop = B_FALSE;
7787 tsol_ire_gw_secattr_t *attrp = NULL;
7788 tsol_gcgrp_t *gcgrp = NULL;
7789 tsol_gcgrp_addr_t ga;
7790
7791 if (ip_debug > 2) {
7792 /* ip1dbg */
7793 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst);
7794 }
7795
7796 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
7797 if (mctl_present) {
7798 io = (ipsec_out_t *)first_mp->b_rptr;
7799 ASSERT(io->ipsec_out_type == IPSEC_OUT);
7800 ASSERT(zoneid == io->ipsec_out_zoneid);
7801 ASSERT(zoneid != ALL_ZONES);
7802 }
7803
7804 ipha = (ipha_t *)mp->b_rptr;
7805
7806 /* All multicast lookups come through ip_newroute_ipif() */
7807 if (CLASSD(dst)) {
7808 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n",
7809 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next));
7810 freemsg(first_mp);
7811 return;
7812 }
7813
7814 if (mctl_present && io->ipsec_out_attach_if) {
7815 /* ip_grab_attach_ill returns a held ill */
7816 attach_ill = ip_grab_attach_ill(NULL, first_mp,
7817 io->ipsec_out_ill_index, B_FALSE, ipst);
7818
7819 /* Failure case frees things for us. */
7820 if (attach_ill == NULL)
7821 return;
7822
7823 /*
7824 * Check if we need an ire that will not be
7825 * looked up by anybody else i.e. HIDDEN.
7826 */
7827 if (ill_is_probeonly(attach_ill))
7828 ire_marks = IRE_MARK_HIDDEN;
7829 }
7830 if (mctl_present && io->ipsec_out_ip_nexthop) {
7831 ip_nexthop = B_TRUE;
7832 nexthop_addr = io->ipsec_out_nexthop_addr;
7833 }
7834 /*
7835 * If this IRE is created for forwarding or it is not for
7836 * traffic for congestion controlled protocols, mark it as temporary.
7837 */
7838 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol))
7839 ire_marks |= IRE_MARK_TEMPORARY;
7840
7841 /*
7842 * Get what we can from ire_ftable_lookup which will follow an IRE
7843 * chain until it gets the most specific information available.
7844 * For example, we know that there is no IRE_CACHE for this dest,
7845 * but there may be an IRE_OFFSUBNET which specifies a gateway.
7846 * ire_ftable_lookup will look up the gateway, etc.
7847 * Otherwise, given ire_ftable_lookup algorithm, only one among routes
7848 * to the destination, of equal netmask length in the forward table,
7849 * will be recursively explored. If no information is available
7850 * for the final gateway of that route, we force the returned ire
7851 * to be equal to sire using MATCH_IRE_PARENT.
7852 * At least, in this case we have a starting point (in the buckets)
7853 * to look for other routes to the destination in the forward table.
7854 * This is actually used only for multirouting, where a list
7855 * of routes has to be processed in sequence.
7856 *
7857 * In the process of coming up with the most specific information,
7858 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry
7859 * for the gateway (i.e., one for which the ire_nce->nce_state is
7860 * not yet ND_REACHABLE, and is in the middle of arp resolution).
7861 * Two caveats when handling incomplete ire's in ip_newroute:
7862 * - we should be careful when accessing its ire_nce (specifically
7863 * the nce_res_mp) ast it might change underneath our feet, and,
7864 * - not all legacy code path callers are prepared to handle
7865 * incomplete ire's, so we should not create/add incomplete
7866 * ire_cache entries here. (See discussion about temporary solution
7867 * further below).
7868 *
7869 * In order to minimize packet dropping, and to preserve existing
7870 * behavior, we treat this case as if there were no IRE_CACHE for the
7871 * gateway, and instead use the IF_RESOLVER ire to send out
7872 * another request to ARP (this is achieved by passing the
7873 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the
7874 * arp response comes back in ip_wput_nondata, we will create
7875 * a per-dst ire_cache that has an ND_COMPLETE ire.
7876 *
7877 * Note that this is a temporary solution; the correct solution is
7878 * to create an incomplete per-dst ire_cache entry, and send the
7879 * packet out when the gw's nce is resolved. In order to achieve this,
7880 * all packet processing must have been completed prior to calling
7881 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need
7882 * to be modified to accomodate this solution.
7883 */
7884 if (ip_nexthop) {
7885 /*
7886 * The first time we come here, we look for an IRE_INTERFACE
7887 * entry for the specified nexthop, set the dst to be the
7888 * nexthop address and create an IRE_CACHE entry for the
7889 * nexthop. The next time around, we are able to find an
7890 * IRE_CACHE entry for the nexthop, set the gateway to be the
7891 * nexthop address and create an IRE_CACHE entry for the
7892 * destination address via the specified nexthop.
7893 */
7894 ire = ire_cache_lookup(nexthop_addr, zoneid,
7895 MBLK_GETLABEL(mp), ipst);
7896 if (ire != NULL) {
7897 gw = nexthop_addr;
7898 ire_marks |= IRE_MARK_PRIVATE_ADDR;
7899 } else {
7900 ire = ire_ftable_lookup(nexthop_addr, 0, 0,
7901 IRE_INTERFACE, NULL, NULL, zoneid, 0,
7902 MBLK_GETLABEL(mp),
7903 MATCH_IRE_TYPE | MATCH_IRE_SECATTR,
7904 ipst);
7905 if (ire != NULL) {
7906 dst = nexthop_addr;
7907 }
7908 }
7909 } else if (attach_ill == NULL) {
7910 ire = ire_ftable_lookup(dst, 0, 0, 0,
7911 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp),
7912 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
7913 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT |
7914 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE,
7915 ipst);
7916 } else {
7917 /*
7918 * attach_ill is set only for communicating with
7919 * on-link hosts. So, don't look for DEFAULT.
7920 */
7921 ipif_t *attach_ipif;
7922
7923 attach_ipif = ipif_get_next_ipif(NULL, attach_ill);
7924 if (attach_ipif == NULL) {
7925 ill_refrele(attach_ill);
7926 goto icmp_err_ret;
7927 }
7928 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif,
7929 &sire, zoneid, 0, MBLK_GETLABEL(mp),
7930 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL |
7931 MATCH_IRE_SECATTR, ipst);
7932 ipif_refrele(attach_ipif);
7933 }
7934 ip3dbg(("ip_newroute: ire_ftable_lookup() "
7935 "returned ire %p, sire %p\n", (void *)ire, (void *)sire));
7936
7937 /*
7938 * This loop is run only once in most cases.
7939 * We loop to resolve further routes only when the destination
7940 * can be reached through multiple RTF_MULTIRT-flagged ires.
7941 */
7942 do {
7943 /* Clear the previous iteration's values */
7944 if (src_ipif != NULL) {
7945 ipif_refrele(src_ipif);
7946 src_ipif = NULL;
7947 }
7948 if (dst_ill != NULL) {
7949 ill_refrele(dst_ill);
7950 dst_ill = NULL;
7951 }
7952
7953 multirt_resolve_next = B_FALSE;
7954 /*
7955 * We check if packets have to be multirouted.
7956 * In this case, given the current <ire, sire> couple,
7957 * we look for the next suitable <ire, sire>.
7958 * This check is done in ire_multirt_lookup(),
7959 * which applies various criteria to find the next route
7960 * to resolve. ire_multirt_lookup() leaves <ire, sire>
7961 * unchanged if it detects it has not been tried yet.
7962 */
7963 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
7964 ip3dbg(("ip_newroute: starting next_resolution "
7965 "with first_mp %p, tag %d\n",
7966 (void *)first_mp,
7967 MULTIRT_DEBUG_TAGGED(first_mp)));
7968
7969 ASSERT(sire != NULL);
7970 multirt_is_resolvable =
7971 ire_multirt_lookup(&ire, &sire, multirt_flags,
7972 MBLK_GETLABEL(mp), ipst);
7973
7974 ip3dbg(("ip_newroute: multirt_is_resolvable %d, "
7975 "ire %p, sire %p\n",
7976 multirt_is_resolvable,
7977 (void *)ire, (void *)sire));
7978
7979 if (!multirt_is_resolvable) {
7980 /*
7981 * No more multirt route to resolve; give up
7982 * (all routes resolved or no more
7983 * resolvable routes).
7984 */
7985 if (ire != NULL) {
7986 ire_refrele(ire);
7987 ire = NULL;
7988 }
7989 } else {
7990 ASSERT(sire != NULL);
7991 ASSERT(ire != NULL);
7992 /*
7993 * We simply use first_sire as a flag that
7994 * indicates if a resolvable multirt route
7995 * has already been found.
7996 * If it is not the case, we may have to send
7997 * an ICMP error to report that the
7998 * destination is unreachable.
7999 * We do not IRE_REFHOLD first_sire.
8000 */
8001 if (first_sire == NULL) {
8002 first_sire = sire;
8003 }
8004 }
8005 }
8006 if (ire == NULL) {
8007 if (ip_debug > 3) {
8008 /* ip2dbg */
8009 pr_addr_dbg("ip_newroute: "
8010 "can't resolve %s\n", AF_INET, &dst);
8011 }
8012 ip3dbg(("ip_newroute: "
8013 "ire %p, sire %p, first_sire %p\n",
8014 (void *)ire, (void *)sire, (void *)first_sire));
8015
8016 if (sire != NULL) {
8017 ire_refrele(sire);
8018 sire = NULL;
8019 }
8020
8021 if (first_sire != NULL) {
8022 /*
8023 * At least one multirt route has been found
8024 * in the same call to ip_newroute();
8025 * there is no need to report an ICMP error.
8026 * first_sire was not IRE_REFHOLDed.
8027 */
8028 MULTIRT_DEBUG_UNTAG(first_mp);
8029 freemsg(first_mp);
8030 return;
8031 }
8032 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0,
8033 RTA_DST, ipst);
8034 if (attach_ill != NULL)
8035 ill_refrele(attach_ill);
8036 goto icmp_err_ret;
8037 }
8038
8039 /*
8040 * Verify that the returned IRE does not have either
8041 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
8042 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
8043 */
8044 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
8045 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
8046 if (attach_ill != NULL)
8047 ill_refrele(attach_ill);
8048 goto icmp_err_ret;
8049 }
8050 /*
8051 * Increment the ire_ob_pkt_count field for ire if it is an
8052 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
8053 * increment the same for the parent IRE, sire, if it is some
8054 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST)
8055 */
8056 if ((ire->ire_type & IRE_INTERFACE) != 0) {
8057 UPDATE_OB_PKT_COUNT(ire);
8058 ire->ire_last_used_time = lbolt;
8059 }
8060
8061 if (sire != NULL) {
8062 gw = sire->ire_gateway_addr;
8063 ASSERT((sire->ire_type & (IRE_CACHETABLE |
8064 IRE_INTERFACE)) == 0);
8065 UPDATE_OB_PKT_COUNT(sire);
8066 sire->ire_last_used_time = lbolt;
8067 }
8068 /*
8069 * We have a route to reach the destination.
8070 *
8071 * 1) If the interface is part of ill group, try to get a new
8072 * ill taking load spreading into account.
8073 *
8074 * 2) After selecting the ill, get a source address that
8075 * might create good inbound load spreading.
8076 * ipif_select_source does this for us.
8077 *
8078 * If the application specified the ill (ifindex), we still
8079 * load spread. Only if the packets needs to go out
8080 * specifically on a given ill e.g. binding to
8081 * IPIF_NOFAILOVER address, then we don't try to use a
8082 * different ill for load spreading.
8083 */
8084 if (attach_ill == NULL) {
8085 /*
8086 * Don't perform outbound load spreading in the
8087 * case of an RTF_MULTIRT route, as we actually
8088 * typically want to replicate outgoing packets
8089 * through particular interfaces.
8090 */
8091 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
8092 dst_ill = ire->ire_ipif->ipif_ill;
8093 /* for uniformity */
8094 ill_refhold(dst_ill);
8095 } else {
8096 /*
8097 * If we are here trying to create an IRE_CACHE
8098 * for an offlink destination and have the
8099 * IRE_CACHE for the next hop and the latter is
8100 * using virtual IP source address selection i.e
8101 * it's ire->ire_ipif is pointing to a virtual
8102 * network interface (vni) then
8103 * ip_newroute_get_dst_ll() will return the vni
8104 * interface as the dst_ill. Since the vni is
8105 * virtual i.e not associated with any physical
8106 * interface, it cannot be the dst_ill, hence
8107 * in such a case call ip_newroute_get_dst_ll()
8108 * with the stq_ill instead of the ire_ipif ILL.
8109 * The function returns a refheld ill.
8110 */
8111 if ((ire->ire_type == IRE_CACHE) &&
8112 IS_VNI(ire->ire_ipif->ipif_ill))
8113 dst_ill = ip_newroute_get_dst_ill(
8114 ire->ire_stq->q_ptr);
8115 else
8116 dst_ill = ip_newroute_get_dst_ill(
8117 ire->ire_ipif->ipif_ill);
8118 }
8119 if (dst_ill == NULL) {
8120 if (ip_debug > 2) {
8121 pr_addr_dbg("ip_newroute: "
8122 "no dst ill for dst"
8123 " %s\n", AF_INET, &dst);
8124 }
8125 goto icmp_err_ret;
8126 }
8127 } else {
8128 dst_ill = ire->ire_ipif->ipif_ill;
8129 /* for uniformity */
8130 ill_refhold(dst_ill);
8131 /*
8132 * We should have found a route matching ill as we
8133 * called ire_ftable_lookup with MATCH_IRE_ILL.
8134 * Rather than asserting, when there is a mismatch,
8135 * we just drop the packet.
8136 */
8137 if (dst_ill != attach_ill) {
8138 ip0dbg(("ip_newroute: Packet dropped as "
8139 "IPIF_NOFAILOVER ill is %s, "
8140 "ire->ire_ipif->ipif_ill is %s\n",
8141 attach_ill->ill_name,
8142 dst_ill->ill_name));
8143 ill_refrele(attach_ill);
8144 goto icmp_err_ret;
8145 }
8146 }
8147 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */
8148 if (attach_ill != NULL) {
8149 ill_refrele(attach_ill);
8150 attach_ill = NULL;
8151 do_attach_ill = B_TRUE;
8152 }
8153 ASSERT(dst_ill != NULL);
8154 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name));
8155
8156 /*
8157 * Pick the best source address from dst_ill.
8158 *
8159 * 1) If it is part of a multipathing group, we would
8160 * like to spread the inbound packets across different
8161 * interfaces. ipif_select_source picks a random source
8162 * across the different ills in the group.
8163 *
8164 * 2) If it is not part of a multipathing group, we try
8165 * to pick the source address from the destination
8166 * route. Clustering assumes that when we have multiple
8167 * prefixes hosted on an interface, the prefix of the
8168 * source address matches the prefix of the destination
8169 * route. We do this only if the address is not
8170 * DEPRECATED.
8171 *
8172 * 3) If the conn is in a different zone than the ire, we
8173 * need to pick a source address from the right zone.
8174 *
8175 * NOTE : If we hit case (1) above, the prefix of the source
8176 * address picked may not match the prefix of the
8177 * destination routes prefix as ipif_select_source
8178 * does not look at "dst" while picking a source
8179 * address.
8180 * If we want the same behavior as (2), we will need
8181 * to change the behavior of ipif_select_source.
8182 */
8183 ASSERT(src_ipif == NULL);
8184 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
8185 /*
8186 * The RTF_SETSRC flag is set in the parent ire (sire).
8187 * Check that the ipif matching the requested source
8188 * address still exists.
8189 */
8190 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
8191 zoneid, NULL, NULL, NULL, NULL, ipst);
8192 }
8193
8194 unspec_src = (connp != NULL && connp->conn_unspec_src);
8195
8196 if (src_ipif == NULL &&
8197 (!unspec_src || ipha->ipha_src != INADDR_ANY)) {
8198 ire_marks |= IRE_MARK_USESRC_CHECK;
8199 if ((dst_ill->ill_group != NULL) ||
8200 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
8201 (connp != NULL && ire->ire_zoneid != zoneid &&
8202 ire->ire_zoneid != ALL_ZONES) ||
8203 (dst_ill->ill_usesrc_ifindex != 0)) {
8204 /*
8205 * If the destination is reachable via a
8206 * given gateway, the selected source address
8207 * should be in the same subnet as the gateway.
8208 * Otherwise, the destination is not reachable.
8209 *
8210 * If there are no interfaces on the same subnet
8211 * as the destination, ipif_select_source gives
8212 * first non-deprecated interface which might be
8213 * on a different subnet than the gateway.
8214 * This is not desirable. Hence pass the dst_ire
8215 * source address to ipif_select_source.
8216 * It is sure that the destination is reachable
8217 * with the dst_ire source address subnet.
8218 * So passing dst_ire source address to
8219 * ipif_select_source will make sure that the
8220 * selected source will be on the same subnet
8221 * as dst_ire source address.
8222 */
8223 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr;
8224 src_ipif = ipif_select_source(dst_ill, saddr,
8225 zoneid);
8226 if (src_ipif == NULL) {
8227 if (ip_debug > 2) {
8228 pr_addr_dbg("ip_newroute: "
8229 "no src for dst %s ",
8230 AF_INET, &dst);
8231 printf("through interface %s\n",
8232 dst_ill->ill_name);
8233 }
8234 goto icmp_err_ret;
8235 }
8236 } else {
8237 src_ipif = ire->ire_ipif;
8238 ASSERT(src_ipif != NULL);
8239 /* hold src_ipif for uniformity */
8240 ipif_refhold(src_ipif);
8241 }
8242 }
8243
8244 /*
8245 * Assign a source address while we have the conn.
8246 * We can't have ip_wput_ire pick a source address when the
8247 * packet returns from arp since we need to look at
8248 * conn_unspec_src and conn_zoneid, and we lose the conn when
8249 * going through arp.
8250 *
8251 * NOTE : ip_newroute_v6 does not have this piece of code as
8252 * it uses ip6i to store this information.
8253 */
8254 if (ipha->ipha_src == INADDR_ANY && !unspec_src)
8255 ipha->ipha_src = src_ipif->ipif_src_addr;
8256
8257 if (ip_debug > 3) {
8258 /* ip2dbg */
8259 pr_addr_dbg("ip_newroute: first hop %s\n",
8260 AF_INET, &gw);
8261 }
8262 ip2dbg(("\tire type %s (%d)\n",
8263 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type));
8264
8265 /*
8266 * The TTL of multirouted packets is bounded by the
8267 * ip_multirt_ttl ndd variable.
8268 */
8269 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
8270 /* Force TTL of multirouted packets */
8271 if ((ipst->ips_ip_multirt_ttl > 0) &&
8272 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) {
8273 ip2dbg(("ip_newroute: forcing multirt TTL "
8274 "to %d (was %d), dst 0x%08x\n",
8275 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl,
8276 ntohl(sire->ire_addr)));
8277 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl;
8278 }
8279 }
8280 /*
8281 * At this point in ip_newroute(), ire is either the
8282 * IRE_CACHE of the next-hop gateway for an off-subnet
8283 * destination or an IRE_INTERFACE type that should be used
8284 * to resolve an on-subnet destination or an on-subnet
8285 * next-hop gateway.
8286 *
8287 * In the IRE_CACHE case, we have the following :
8288 *
8289 * 1) src_ipif - used for getting a source address.
8290 *
8291 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
8292 * means packets using this IRE_CACHE will go out on
8293 * dst_ill.
8294 *
8295 * 3) The IRE sire will point to the prefix that is the
8296 * longest matching route for the destination. These
8297 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST.
8298 *
8299 * The newly created IRE_CACHE entry for the off-subnet
8300 * destination is tied to both the prefix route and the
8301 * interface route used to resolve the next-hop gateway
8302 * via the ire_phandle and ire_ihandle fields,
8303 * respectively.
8304 *
8305 * In the IRE_INTERFACE case, we have the following :
8306 *
8307 * 1) src_ipif - used for getting a source address.
8308 *
8309 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
8310 * means packets using the IRE_CACHE that we will build
8311 * here will go out on dst_ill.
8312 *
8313 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
8314 * to be created will only be tied to the IRE_INTERFACE
8315 * that was derived from the ire_ihandle field.
8316 *
8317 * If sire is non-NULL, it means the destination is
8318 * off-link and we will first create the IRE_CACHE for the
8319 * gateway. Next time through ip_newroute, we will create
8320 * the IRE_CACHE for the final destination as described
8321 * above.
8322 *
8323 * In both cases, after the current resolution has been
8324 * completed (or possibly initialised, in the IRE_INTERFACE
8325 * case), the loop may be re-entered to attempt the resolution
8326 * of another RTF_MULTIRT route.
8327 *
8328 * When an IRE_CACHE entry for the off-subnet destination is
8329 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire,
8330 * for further processing in emission loops.
8331 */
8332 save_ire = ire;
8333 switch (ire->ire_type) {
8334 case IRE_CACHE: {
8335 ire_t *ipif_ire;
8336
8337 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE);
8338 if (gw == 0)
8339 gw = ire->ire_gateway_addr;
8340 /*
8341 * We need 3 ire's to create a new cache ire for an
8342 * off-link destination from the cache ire of the
8343 * gateway.
8344 *
8345 * 1. The prefix ire 'sire' (Note that this does
8346 * not apply to the conn_nexthop_set case)
8347 * 2. The cache ire of the gateway 'ire'
8348 * 3. The interface ire 'ipif_ire'
8349 *
8350 * We have (1) and (2). We lookup (3) below.
8351 *
8352 * If there is no interface route to the gateway,
8353 * it is a race condition, where we found the cache
8354 * but the interface route has been deleted.
8355 */
8356 if (ip_nexthop) {
8357 ipif_ire = ire_ihandle_lookup_onlink(ire);
8358 } else {
8359 ipif_ire =
8360 ire_ihandle_lookup_offlink(ire, sire);
8361 }
8362 if (ipif_ire == NULL) {
8363 ip1dbg(("ip_newroute: "
8364 "ire_ihandle_lookup_offlink failed\n"));
8365 goto icmp_err_ret;
8366 }
8367
8368 /*
8369 * Check cached gateway IRE for any security
8370 * attributes; if found, associate the gateway
8371 * credentials group to the destination IRE.
8372 */
8373 if ((attrp = save_ire->ire_gw_secattr) != NULL) {
8374 mutex_enter(&attrp->igsa_lock);
8375 if ((gcgrp = attrp->igsa_gcgrp) != NULL)
8376 GCGRP_REFHOLD(gcgrp);
8377 mutex_exit(&attrp->igsa_lock);
8378 }
8379
8380 /*
8381 * XXX For the source of the resolver mp,
8382 * we are using the same DL_UNITDATA_REQ
8383 * (from save_ire->ire_nce->nce_res_mp)
8384 * though the save_ire is not pointing at the same ill.
8385 * This is incorrect. We need to send it up to the
8386 * resolver to get the right res_mp. For ethernets
8387 * this may be okay (ill_type == DL_ETHER).
8388 */
8389
8390 ire = ire_create(
8391 (uchar_t *)&dst, /* dest address */
8392 (uchar_t *)&ip_g_all_ones, /* mask */
8393 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8394 (uchar_t *)&gw, /* gateway address */
8395 &save_ire->ire_max_frag,
8396 save_ire->ire_nce, /* src nce */
8397 dst_ill->ill_rq, /* recv-from queue */
8398 dst_ill->ill_wq, /* send-to queue */
8399 IRE_CACHE, /* IRE type */
8400 src_ipif,
8401 (sire != NULL) ?
8402 sire->ire_mask : 0, /* Parent mask */
8403 (sire != NULL) ?
8404 sire->ire_phandle : 0, /* Parent handle */
8405 ipif_ire->ire_ihandle, /* Interface handle */
8406 (sire != NULL) ? (sire->ire_flags &
8407 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */
8408 (sire != NULL) ?
8409 &(sire->ire_uinfo) : &(save_ire->ire_uinfo),
8410 NULL,
8411 gcgrp,
8412 ipst);
8413
8414 if (ire == NULL) {
8415 if (gcgrp != NULL) {
8416 GCGRP_REFRELE(gcgrp);
8417 gcgrp = NULL;
8418 }
8419 ire_refrele(ipif_ire);
8420 ire_refrele(save_ire);
8421 break;
8422 }
8423
8424 /* reference now held by IRE */
8425 gcgrp = NULL;
8426
8427 ire->ire_marks |= ire_marks;
8428
8429 /*
8430 * Prevent sire and ipif_ire from getting deleted.
8431 * The newly created ire is tied to both of them via
8432 * the phandle and ihandle respectively.
8433 */
8434 if (sire != NULL) {
8435 IRB_REFHOLD(sire->ire_bucket);
8436 /* Has it been removed already ? */
8437 if (sire->ire_marks & IRE_MARK_CONDEMNED) {
8438 IRB_REFRELE(sire->ire_bucket);
8439 ire_refrele(ipif_ire);
8440 ire_refrele(save_ire);
8441 break;
8442 }
8443 }
8444
8445 IRB_REFHOLD(ipif_ire->ire_bucket);
8446 /* Has it been removed already ? */
8447 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) {
8448 IRB_REFRELE(ipif_ire->ire_bucket);
8449 if (sire != NULL)
8450 IRB_REFRELE(sire->ire_bucket);
8451 ire_refrele(ipif_ire);
8452 ire_refrele(save_ire);
8453 break;
8454 }
8455
8456 xmit_mp = first_mp;
8457 /*
8458 * In the case of multirouting, a copy
8459 * of the packet is done before its sending.
8460 * The copy is used to attempt another
8461 * route resolution, in a next loop.
8462 */
8463 if (ire->ire_flags & RTF_MULTIRT) {
8464 copy_mp = copymsg(first_mp);
8465 if (copy_mp != NULL) {
8466 xmit_mp = copy_mp;
8467 MULTIRT_DEBUG_TAG(first_mp);
8468 }
8469 }
8470 ire_add_then_send(q, ire, xmit_mp);
8471 ire_refrele(save_ire);
8472
8473 /* Assert that sire is not deleted yet. */
8474 if (sire != NULL) {
8475 ASSERT(sire->ire_ptpn != NULL);
8476 IRB_REFRELE(sire->ire_bucket);
8477 }
8478
8479 /* Assert that ipif_ire is not deleted yet. */
8480 ASSERT(ipif_ire->ire_ptpn != NULL);
8481 IRB_REFRELE(ipif_ire->ire_bucket);
8482 ire_refrele(ipif_ire);
8483
8484 /*
8485 * If copy_mp is not NULL, multirouting was
8486 * requested. We loop to initiate a next
8487 * route resolution attempt, starting from sire.
8488 */
8489 if (copy_mp != NULL) {
8490 /*
8491 * Search for the next unresolved
8492 * multirt route.
8493 */
8494 copy_mp = NULL;
8495 ipif_ire = NULL;
8496 ire = NULL;
8497 multirt_resolve_next = B_TRUE;
8498 continue;
8499 }
8500 if (sire != NULL)
8501 ire_refrele(sire);
8502 ipif_refrele(src_ipif);
8503 ill_refrele(dst_ill);
8504 return;
8505 }
8506 case IRE_IF_NORESOLVER: {
8507
8508 if (dst_ill->ill_phys_addr_length != IP_ADDR_LEN &&
8509 dst_ill->ill_resolver_mp == NULL) {
8510 ip1dbg(("ip_newroute: dst_ill %p "
8511 "for IRE_IF_NORESOLVER ire %p has "
8512 "no ill_resolver_mp\n",
8513 (void *)dst_ill, (void *)ire));
8514 break;
8515 }
8516
8517 /*
8518 * TSol note: We are creating the ire cache for the
8519 * destination 'dst'. If 'dst' is offlink, going
8520 * through the first hop 'gw', the security attributes
8521 * of 'dst' must be set to point to the gateway
8522 * credentials of gateway 'gw'. If 'dst' is onlink, it
8523 * is possible that 'dst' is a potential gateway that is
8524 * referenced by some route that has some security
8525 * attributes. Thus in the former case, we need to do a
8526 * gcgrp_lookup of 'gw' while in the latter case we
8527 * need to do gcgrp_lookup of 'dst' itself.
8528 */
8529 ga.ga_af = AF_INET;
8530 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst,
8531 &ga.ga_addr);
8532 gcgrp = gcgrp_lookup(&ga, B_FALSE);
8533
8534 ire = ire_create(
8535 (uchar_t *)&dst, /* dest address */
8536 (uchar_t *)&ip_g_all_ones, /* mask */
8537 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8538 (uchar_t *)&gw, /* gateway address */
8539 &save_ire->ire_max_frag,
8540 NULL, /* no src nce */
8541 dst_ill->ill_rq, /* recv-from queue */
8542 dst_ill->ill_wq, /* send-to queue */
8543 IRE_CACHE,
8544 src_ipif,
8545 save_ire->ire_mask, /* Parent mask */
8546 (sire != NULL) ? /* Parent handle */
8547 sire->ire_phandle : 0,
8548 save_ire->ire_ihandle, /* Interface handle */
8549 (sire != NULL) ? sire->ire_flags &
8550 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */
8551 &(save_ire->ire_uinfo),
8552 NULL,
8553 gcgrp,
8554 ipst);
8555
8556 if (ire == NULL) {
8557 if (gcgrp != NULL) {
8558 GCGRP_REFRELE(gcgrp);
8559 gcgrp = NULL;
8560 }
8561 ire_refrele(save_ire);
8562 break;
8563 }
8564
8565 /* reference now held by IRE */
8566 gcgrp = NULL;
8567
8568 ire->ire_marks |= ire_marks;
8569
8570 /* Prevent save_ire from getting deleted */
8571 IRB_REFHOLD(save_ire->ire_bucket);
8572 /* Has it been removed already ? */
8573 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
8574 IRB_REFRELE(save_ire->ire_bucket);
8575 ire_refrele(save_ire);
8576 break;
8577 }
8578
8579 /*
8580 * In the case of multirouting, a copy
8581 * of the packet is made before it is sent.
8582 * The copy is used in the next
8583 * loop to attempt another resolution.
8584 */
8585 xmit_mp = first_mp;
8586 if ((sire != NULL) &&
8587 (sire->ire_flags & RTF_MULTIRT)) {
8588 copy_mp = copymsg(first_mp);
8589 if (copy_mp != NULL) {
8590 xmit_mp = copy_mp;
8591 MULTIRT_DEBUG_TAG(first_mp);
8592 }
8593 }
8594 ire_add_then_send(q, ire, xmit_mp);
8595
8596 /* Assert that it is not deleted yet. */
8597 ASSERT(save_ire->ire_ptpn != NULL);
8598 IRB_REFRELE(save_ire->ire_bucket);
8599 ire_refrele(save_ire);
8600
8601 if (copy_mp != NULL) {
8602 /*
8603 * If we found a (no)resolver, we ignore any
8604 * trailing top priority IRE_CACHE in further
8605 * loops. This ensures that we do not omit any
8606 * (no)resolver.
8607 * This IRE_CACHE, if any, will be processed
8608 * by another thread entering ip_newroute().
8609 * IRE_CACHE entries, if any, will be processed
8610 * by another thread entering ip_newroute(),
8611 * (upon resolver response, for instance).
8612 * This aims to force parallel multirt
8613 * resolutions as soon as a packet must be sent.
8614 * In the best case, after the tx of only one
8615 * packet, all reachable routes are resolved.
8616 * Otherwise, the resolution of all RTF_MULTIRT
8617 * routes would require several emissions.
8618 */
8619 multirt_flags &= ~MULTIRT_CACHEGW;
8620
8621 /*
8622 * Search for the next unresolved multirt
8623 * route.
8624 */
8625 copy_mp = NULL;
8626 save_ire = NULL;
8627 ire = NULL;
8628 multirt_resolve_next = B_TRUE;
8629 continue;
8630 }
8631
8632 /*
8633 * Don't need sire anymore
8634 */
8635 if (sire != NULL)
8636 ire_refrele(sire);
8637
8638 ipif_refrele(src_ipif);
8639 ill_refrele(dst_ill);
8640 return;
8641 }
8642 case IRE_IF_RESOLVER:
8643 /*
8644 * We can't build an IRE_CACHE yet, but at least we
8645 * found a resolver that can help.
8646 */
8647 res_mp = dst_ill->ill_resolver_mp;
8648 if (!OK_RESOLVER_MP(res_mp))
8649 break;
8650
8651 /*
8652 * To be at this point in the code with a non-zero gw
8653 * means that dst is reachable through a gateway that
8654 * we have never resolved. By changing dst to the gw
8655 * addr we resolve the gateway first.
8656 * When ire_add_then_send() tries to put the IP dg
8657 * to dst, it will reenter ip_newroute() at which
8658 * time we will find the IRE_CACHE for the gw and
8659 * create another IRE_CACHE in case IRE_CACHE above.
8660 */
8661 if (gw != INADDR_ANY) {
8662 /*
8663 * The source ipif that was determined above was
8664 * relative to the destination address, not the
8665 * gateway's. If src_ipif was not taken out of
8666 * the IRE_IF_RESOLVER entry, we'll need to call
8667 * ipif_select_source() again.
8668 */
8669 if (src_ipif != ire->ire_ipif) {
8670 ipif_refrele(src_ipif);
8671 src_ipif = ipif_select_source(dst_ill,
8672 gw, zoneid);
8673 if (src_ipif == NULL) {
8674 if (ip_debug > 2) {
8675 pr_addr_dbg(
8676 "ip_newroute: no "
8677 "src for gw %s ",
8678 AF_INET, &gw);
8679 printf("through "
8680 "interface %s\n",
8681 dst_ill->ill_name);
8682 }
8683 goto icmp_err_ret;
8684 }
8685 }
8686 save_dst = dst;
8687 dst = gw;
8688 gw = INADDR_ANY;
8689 }
8690
8691 /*
8692 * We obtain a partial IRE_CACHE which we will pass
8693 * along with the resolver query. When the response
8694 * comes back it will be there ready for us to add.
8695 * The ire_max_frag is atomically set under the
8696 * irebucket lock in ire_add_v[46].
8697 */
8698
8699 ire = ire_create_mp(
8700 (uchar_t *)&dst, /* dest address */
8701 (uchar_t *)&ip_g_all_ones, /* mask */
8702 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8703 (uchar_t *)&gw, /* gateway address */
8704 NULL, /* ire_max_frag */
8705 NULL, /* no src nce */
8706 dst_ill->ill_rq, /* recv-from queue */
8707 dst_ill->ill_wq, /* send-to queue */
8708 IRE_CACHE,
8709 src_ipif, /* Interface ipif */
8710 save_ire->ire_mask, /* Parent mask */
8711 0,
8712 save_ire->ire_ihandle, /* Interface handle */
8713 0, /* flags if any */
8714 &(save_ire->ire_uinfo),
8715 NULL,
8716 NULL,
8717 ipst);
8718
8719 if (ire == NULL) {
8720 ire_refrele(save_ire);
8721 break;
8722 }
8723
8724 if ((sire != NULL) &&
8725 (sire->ire_flags & RTF_MULTIRT)) {
8726 copy_mp = copymsg(first_mp);
8727 if (copy_mp != NULL)
8728 MULTIRT_DEBUG_TAG(copy_mp);
8729 }
8730
8731 ire->ire_marks |= ire_marks;
8732
8733 /*
8734 * Construct message chain for the resolver
8735 * of the form:
8736 * ARP_REQ_MBLK-->IRE_MBLK-->Packet
8737 * Packet could contain a IPSEC_OUT mp.
8738 *
8739 * NOTE : ire will be added later when the response
8740 * comes back from ARP. If the response does not
8741 * come back, ARP frees the packet. For this reason,
8742 * we can't REFHOLD the bucket of save_ire to prevent
8743 * deletions. We may not be able to REFRELE the bucket
8744 * if the response never comes back. Thus, before
8745 * adding the ire, ire_add_v4 will make sure that the
8746 * interface route does not get deleted. This is the
8747 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6
8748 * where we can always prevent deletions because of
8749 * the synchronous nature of adding IRES i.e
8750 * ire_add_then_send is called after creating the IRE.
8751 */
8752 ASSERT(ire->ire_mp != NULL);
8753 ire->ire_mp->b_cont = first_mp;
8754 /* Have saved_mp handy, for cleanup if canput fails */
8755 saved_mp = mp;
8756 mp = copyb(res_mp);
8757 if (mp == NULL) {
8758 /* Prepare for cleanup */
8759 mp = saved_mp; /* pkt */
8760 ire_delete(ire); /* ire_mp */
8761 ire = NULL;
8762 ire_refrele(save_ire);
8763 if (copy_mp != NULL) {
8764 MULTIRT_DEBUG_UNTAG(copy_mp);
8765 freemsg(copy_mp);
8766 copy_mp = NULL;
8767 }
8768 break;
8769 }
8770 linkb(mp, ire->ire_mp);
8771
8772 /*
8773 * Fill in the source and dest addrs for the resolver.
8774 * NOTE: this depends on memory layouts imposed by
8775 * ill_init().
8776 */
8777 areq = (areq_t *)mp->b_rptr;
8778 addrp = (ipaddr_t *)((char *)areq +
8779 areq->areq_sender_addr_offset);
8780 if (do_attach_ill) {
8781 /*
8782 * This is bind to no failover case.
8783 * arp packet also must go out on attach_ill.
8784 */
8785 ASSERT(ipha->ipha_src != NULL);
8786 *addrp = ipha->ipha_src;
8787 } else {
8788 *addrp = save_ire->ire_src_addr;
8789 }
8790
8791 ire_refrele(save_ire);
8792 addrp = (ipaddr_t *)((char *)areq +
8793 areq->areq_target_addr_offset);
8794 *addrp = dst;
8795 /* Up to the resolver. */
8796 if (canputnext(dst_ill->ill_rq) &&
8797 !(dst_ill->ill_arp_closing)) {
8798 putnext(dst_ill->ill_rq, mp);
8799 ire = NULL;
8800 if (copy_mp != NULL) {
8801 /*
8802 * If we found a resolver, we ignore
8803 * any trailing top priority IRE_CACHE
8804 * in the further loops. This ensures
8805 * that we do not omit any resolver.
8806 * IRE_CACHE entries, if any, will be
8807 * processed next time we enter
8808 * ip_newroute().
8809 */
8810 multirt_flags &= ~MULTIRT_CACHEGW;
8811 /*
8812 * Search for the next unresolved
8813 * multirt route.
8814 */
8815 first_mp = copy_mp;
8816 copy_mp = NULL;
8817 /* Prepare the next resolution loop. */
8818 mp = first_mp;
8819 EXTRACT_PKT_MP(mp, first_mp,
8820 mctl_present);
8821 if (mctl_present)
8822 io = (ipsec_out_t *)
8823 first_mp->b_rptr;
8824 ipha = (ipha_t *)mp->b_rptr;
8825
8826 ASSERT(sire != NULL);
8827
8828 dst = save_dst;
8829 multirt_resolve_next = B_TRUE;
8830 continue;
8831 }
8832
8833 if (sire != NULL)
8834 ire_refrele(sire);
8835
8836 /*
8837 * The response will come back in ip_wput
8838 * with db_type IRE_DB_TYPE.
8839 */
8840 ipif_refrele(src_ipif);
8841 ill_refrele(dst_ill);
8842 return;
8843 } else {
8844 /* Prepare for cleanup */
8845 DTRACE_PROBE1(ip__newroute__drop, mblk_t *,
8846 mp);
8847 mp->b_cont = NULL;
8848 freeb(mp); /* areq */
8849 /*
8850 * this is an ire that is not added to the
8851 * cache. ire_freemblk will handle the release
8852 * of any resources associated with the ire.
8853 */
8854 ire_delete(ire); /* ire_mp */
8855 mp = saved_mp; /* pkt */
8856 ire = NULL;
8857 if (copy_mp != NULL) {
8858 MULTIRT_DEBUG_UNTAG(copy_mp);
8859 freemsg(copy_mp);
8860 copy_mp = NULL;
8861 }
8862 break;
8863 }
8864 default:
8865 break;
8866 }
8867 } while (multirt_resolve_next);
8868
8869 ip1dbg(("ip_newroute: dropped\n"));
8870 /* Did this packet originate externally? */
8871 if (mp->b_prev) {
8872 mp->b_next = NULL;
8873 mp->b_prev = NULL;
8874 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
8875 } else {
8876 if (dst_ill != NULL) {
8877 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards);
8878 } else {
8879 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
8880 }
8881 }
8882 ASSERT(copy_mp == NULL);
8883 MULTIRT_DEBUG_UNTAG(first_mp);
8884 freemsg(first_mp);
8885 if (ire != NULL)
8886 ire_refrele(ire);
8887 if (sire != NULL)
8888 ire_refrele(sire);
8889 if (src_ipif != NULL)
8890 ipif_refrele(src_ipif);
8891 if (dst_ill != NULL)
8892 ill_refrele(dst_ill);
8893 return;
8894
8895 icmp_err_ret:
8896 ip1dbg(("ip_newroute: no route\n"));
8897 if (src_ipif != NULL)
8898 ipif_refrele(src_ipif);
8899 if (dst_ill != NULL)
8900 ill_refrele(dst_ill);
8901 if (sire != NULL)
8902 ire_refrele(sire);
8903 /* Did this packet originate externally? */
8904 if (mp->b_prev) {
8905 mp->b_next = NULL;
8906 mp->b_prev = NULL;
8907 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes);
8908 q = WR(q);
8909 } else {
8910 /*
8911 * There is no outgoing ill, so just increment the
8912 * system MIB.
8913 */
8914 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes);
8915 /*
8916 * Since ip_wput() isn't close to finished, we fill
8917 * in enough of the header for credible error reporting.
8918 */
8919 if (ip_hdr_complete(ipha, zoneid, ipst)) {
8920 /* Failed */
8921 MULTIRT_DEBUG_UNTAG(first_mp);
8922 freemsg(first_mp);
8923 if (ire != NULL)
8924 ire_refrele(ire);
8925 return;
8926 }
8927 }
8928
8929 /*
8930 * At this point we will have ire only if RTF_BLACKHOLE
8931 * or RTF_REJECT flags are set on the IRE. It will not
8932 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set.
8933 */
8934 if (ire != NULL) {
8935 if (ire->ire_flags & RTF_BLACKHOLE) {
8936 ire_refrele(ire);
8937 MULTIRT_DEBUG_UNTAG(first_mp);
8938 freemsg(first_mp);
8939 return;
8940 }
8941 ire_refrele(ire);
8942 }
8943 if (ip_source_routed(ipha, ipst)) {
8944 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED,
8945 zoneid, ipst);
8946 return;
8947 }
8948 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst);
8949 }
8950
8951 ip_opt_info_t zero_info;
8952
8953 /*
8954 * IPv4 -
8955 * ip_newroute_ipif is called by ip_wput_multicast and
8956 * ip_rput_forward_multicast whenever we need to send
8957 * out a packet to a destination address for which we do not have specific
8958 * routing information. It is used when the packet will be sent out
8959 * on a specific interface. It is also called by ip_wput() when IP_BOUND_IF
8960 * socket option is set or icmp error message wants to go out on a particular
8961 * interface for a unicast packet.
8962 *
8963 * In most cases, the destination address is resolved thanks to the ipif
8964 * intrinsic resolver. However, there are some cases where the call to
8965 * ip_newroute_ipif must take into account the potential presence of
8966 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire
8967 * that uses the interface. This is specified through flags,
8968 * which can be a combination of:
8969 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC
8970 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address
8971 * and flags. Additionally, the packet source address has to be set to
8972 * the specified address. The caller is thus expected to set this flag
8973 * if the packet has no specific source address yet.
8974 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT
8975 * flag, the resulting ire will inherit the flag. All unresolved routes
8976 * to the destination must be explored in the same call to
8977 * ip_newroute_ipif().
8978 */
8979 static void
8980 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst,
8981 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop)
8982 {
8983 areq_t *areq;
8984 ire_t *ire = NULL;
8985 mblk_t *res_mp;
8986 ipaddr_t *addrp;
8987 mblk_t *first_mp;
8988 ire_t *save_ire = NULL;
8989 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */
8990 ipif_t *src_ipif = NULL;
8991 ushort_t ire_marks = 0;
8992 ill_t *dst_ill = NULL;
8993 boolean_t mctl_present;
8994 ipsec_out_t *io;
8995 ipha_t *ipha;
8996 int ihandle = 0;
8997 mblk_t *saved_mp;
8998 ire_t *fire = NULL;
8999 mblk_t *copy_mp = NULL;
9000 boolean_t multirt_resolve_next;
9001 boolean_t unspec_src;
9002 ipaddr_t ipha_dst;
9003 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
9004
9005 /*
9006 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold
9007 * here for uniformity
9008 */
9009 ipif_refhold(ipif);
9010
9011 /*
9012 * This loop is run only once in most cases.
9013 * We loop to resolve further routes only when the destination
9014 * can be reached through multiple RTF_MULTIRT-flagged ires.
9015 */
9016 do {
9017 if (dst_ill != NULL) {
9018 ill_refrele(dst_ill);
9019 dst_ill = NULL;
9020 }
9021 if (src_ipif != NULL) {
9022 ipif_refrele(src_ipif);
9023 src_ipif = NULL;
9024 }
9025 multirt_resolve_next = B_FALSE;
9026
9027 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst),
9028 ipif->ipif_ill->ill_name));
9029
9030 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
9031 if (mctl_present)
9032 io = (ipsec_out_t *)first_mp->b_rptr;
9033
9034 ipha = (ipha_t *)mp->b_rptr;
9035
9036 /*
9037 * Save the packet destination address, we may need it after
9038 * the packet has been consumed.
9039 */
9040 ipha_dst = ipha->ipha_dst;
9041
9042 /*
9043 * If the interface is a pt-pt interface we look for an
9044 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the
9045 * local_address and the pt-pt destination address. Otherwise
9046 * we just match the local address.
9047 * NOTE: dst could be different than ipha->ipha_dst in case
9048 * of sending igmp multicast packets over a point-to-point
9049 * connection.
9050 * Thus we must be careful enough to check ipha_dst to be a
9051 * multicast address, otherwise it will take xmit_if path for
9052 * multicast packets resulting into kernel stack overflow by
9053 * repeated calls to ip_newroute_ipif from ire_send().
9054 */
9055 if (CLASSD(ipha_dst) &&
9056 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) {
9057 goto err_ret;
9058 }
9059
9060 /*
9061 * We check if an IRE_OFFSUBNET for the addr that goes through
9062 * ipif exists. We need it to determine if the RTF_SETSRC and/or
9063 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may
9064 * propagate its flags to the new ire.
9065 */
9066 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) {
9067 fire = ipif_lookup_multi_ire(ipif, ipha_dst);
9068 ip2dbg(("ip_newroute_ipif: "
9069 "ipif_lookup_multi_ire("
9070 "ipif %p, dst %08x) = fire %p\n",
9071 (void *)ipif, ntohl(dst), (void *)fire));
9072 }
9073
9074 if (mctl_present && io->ipsec_out_attach_if) {
9075 attach_ill = ip_grab_attach_ill(NULL, first_mp,
9076 io->ipsec_out_ill_index, B_FALSE, ipst);
9077
9078 /* Failure case frees things for us. */
9079 if (attach_ill == NULL) {
9080 ipif_refrele(ipif);
9081 if (fire != NULL)
9082 ire_refrele(fire);
9083 return;
9084 }
9085
9086 /*
9087 * Check if we need an ire that will not be
9088 * looked up by anybody else i.e. HIDDEN.
9089 */
9090 if (ill_is_probeonly(attach_ill)) {
9091 ire_marks = IRE_MARK_HIDDEN;
9092 }
9093 /*
9094 * ip_wput passes the right ipif for IPIF_NOFAILOVER
9095 * case.
9096 */
9097 dst_ill = ipif->ipif_ill;
9098 /* attach_ill has been refheld by ip_grab_attach_ill */
9099 ASSERT(dst_ill == attach_ill);
9100 } else {
9101 /*
9102 * If the interface belongs to an interface group,
9103 * make sure the next possible interface in the group
9104 * is used. This encourages load spreading among
9105 * peers in an interface group.
9106 * Note: load spreading is disabled for RTF_MULTIRT
9107 * routes.
9108 */
9109 if ((flags & RTF_MULTIRT) && (fire != NULL) &&
9110 (fire->ire_flags & RTF_MULTIRT)) {
9111 /*
9112 * Don't perform outbound load spreading
9113 * in the case of an RTF_MULTIRT issued route,
9114 * we actually typically want to replicate
9115 * outgoing packets through particular
9116 * interfaces.
9117 */
9118 dst_ill = ipif->ipif_ill;
9119 ill_refhold(dst_ill);
9120 } else {
9121 dst_ill = ip_newroute_get_dst_ill(
9122 ipif->ipif_ill);
9123 }
9124 if (dst_ill == NULL) {
9125 if (ip_debug > 2) {
9126 pr_addr_dbg("ip_newroute_ipif: "
9127 "no dst ill for dst %s\n",
9128 AF_INET, &dst);
9129 }
9130 goto err_ret;
9131 }
9132 }
9133
9134 /*
9135 * Pick a source address preferring non-deprecated ones.
9136 * Unlike ip_newroute, we don't do any source address
9137 * selection here since for multicast it really does not help
9138 * in inbound load spreading as in the unicast case.
9139 */
9140 if ((flags & RTF_SETSRC) && (fire != NULL) &&
9141 (fire->ire_flags & RTF_SETSRC)) {
9142 /*
9143 * As requested by flags, an IRE_OFFSUBNET was looked up
9144 * on that interface. This ire has RTF_SETSRC flag, so
9145 * the source address of the packet must be changed.
9146 * Check that the ipif matching the requested source
9147 * address still exists.
9148 */
9149 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL,
9150 zoneid, NULL, NULL, NULL, NULL, ipst);
9151 }
9152
9153 unspec_src = (connp != NULL && connp->conn_unspec_src);
9154
9155 if (((!ipif->ipif_isv6 && ipif->ipif_lcl_addr == INADDR_ANY) ||
9156 (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_UP)) != IPIF_UP ||
9157 (connp != NULL && ipif->ipif_zoneid != zoneid &&
9158 ipif->ipif_zoneid != ALL_ZONES)) &&
9159 (src_ipif == NULL) &&
9160 (!unspec_src || ipha->ipha_src != INADDR_ANY)) {
9161 src_ipif = ipif_select_source(dst_ill, dst, zoneid);
9162 if (src_ipif == NULL) {
9163 if (ip_debug > 2) {
9164 /* ip1dbg */
9165 pr_addr_dbg("ip_newroute_ipif: "
9166 "no src for dst %s",
9167 AF_INET, &dst);
9168 }
9169 ip1dbg((" through interface %s\n",
9170 dst_ill->ill_name));
9171 goto err_ret;
9172 }
9173 ipif_refrele(ipif);
9174 ipif = src_ipif;
9175 ipif_refhold(ipif);
9176 }
9177 if (src_ipif == NULL) {
9178 src_ipif = ipif;
9179 ipif_refhold(src_ipif);
9180 }
9181
9182 /*
9183 * Assign a source address while we have the conn.
9184 * We can't have ip_wput_ire pick a source address when the
9185 * packet returns from arp since conn_unspec_src might be set
9186 * and we lose the conn when going through arp.
9187 */
9188 if (ipha->ipha_src == INADDR_ANY && !unspec_src)
9189 ipha->ipha_src = src_ipif->ipif_src_addr;
9190
9191 /*
9192 * In the case of IP_BOUND_IF and IP_PKTINFO, it is possible
9193 * that the outgoing interface does not have an interface ire.
9194 */
9195 if (CLASSD(ipha_dst) && (connp == NULL ||
9196 connp->conn_outgoing_ill == NULL) &&
9197 infop->ip_opt_ill_index == 0) {
9198 /* ipif_to_ire returns an held ire */
9199 ire = ipif_to_ire(ipif);
9200 if (ire == NULL)
9201 goto err_ret;
9202 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
9203 goto err_ret;
9204 /*
9205 * ihandle is needed when the ire is added to
9206 * cache table.
9207 */
9208 save_ire = ire;
9209 ihandle = save_ire->ire_ihandle;
9210
9211 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, "
9212 "flags %04x\n",
9213 (void *)ire, (void *)ipif, flags));
9214 if ((flags & RTF_MULTIRT) && (fire != NULL) &&
9215 (fire->ire_flags & RTF_MULTIRT)) {
9216 /*
9217 * As requested by flags, an IRE_OFFSUBNET was
9218 * looked up on that interface. This ire has
9219 * RTF_MULTIRT flag, so the resolution loop will
9220 * be re-entered to resolve additional routes on
9221 * other interfaces. For that purpose, a copy of
9222 * the packet is performed at this point.
9223 */
9224 fire->ire_last_used_time = lbolt;
9225 copy_mp = copymsg(first_mp);
9226 if (copy_mp) {
9227 MULTIRT_DEBUG_TAG(copy_mp);
9228 }
9229 }
9230 if ((flags & RTF_SETSRC) && (fire != NULL) &&
9231 (fire->ire_flags & RTF_SETSRC)) {
9232 /*
9233 * As requested by flags, an IRE_OFFSUBET was
9234 * looked up on that interface. This ire has
9235 * RTF_SETSRC flag, so the source address of the
9236 * packet must be changed.
9237 */
9238 ipha->ipha_src = fire->ire_src_addr;
9239 }
9240 } else {
9241 ASSERT((connp == NULL) ||
9242 (connp->conn_outgoing_ill != NULL) ||
9243 (connp->conn_dontroute) ||
9244 infop->ip_opt_ill_index != 0);
9245 /*
9246 * The only ways we can come here are:
9247 * 1) IP_BOUND_IF socket option is set
9248 * 2) SO_DONTROUTE socket option is set
9249 * 3) IP_PKTINFO option is passed in as ancillary data.
9250 * In all cases, the new ire will not be added
9251 * into cache table.
9252 */
9253 ire_marks |= IRE_MARK_NOADD;
9254 }
9255
9256 switch (ipif->ipif_net_type) {
9257 case IRE_IF_NORESOLVER: {
9258 /* We have what we need to build an IRE_CACHE. */
9259
9260 if ((dst_ill->ill_phys_addr_length != IP_ADDR_LEN) &&
9261 (dst_ill->ill_resolver_mp == NULL)) {
9262 ip1dbg(("ip_newroute_ipif: dst_ill %p "
9263 "for IRE_IF_NORESOLVER ire %p has "
9264 "no ill_resolver_mp\n",
9265 (void *)dst_ill, (void *)ire));
9266 break;
9267 }
9268
9269 /*
9270 * The new ire inherits the IRE_OFFSUBNET flags
9271 * and source address, if this was requested.
9272 */
9273 ire = ire_create(
9274 (uchar_t *)&dst, /* dest address */
9275 (uchar_t *)&ip_g_all_ones, /* mask */
9276 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
9277 NULL, /* gateway address */
9278 &ipif->ipif_mtu,
9279 NULL, /* no src nce */
9280 dst_ill->ill_rq, /* recv-from queue */
9281 dst_ill->ill_wq, /* send-to queue */
9282 IRE_CACHE,
9283 src_ipif,
9284 (save_ire != NULL ? save_ire->ire_mask : 0),
9285 (fire != NULL) ? /* Parent handle */
9286 fire->ire_phandle : 0,
9287 ihandle, /* Interface handle */
9288 (fire != NULL) ?
9289 (fire->ire_flags &
9290 (RTF_SETSRC | RTF_MULTIRT)) : 0,
9291 (save_ire == NULL ? &ire_uinfo_null :
9292 &save_ire->ire_uinfo),
9293 NULL,
9294 NULL,
9295 ipst);
9296
9297 if (ire == NULL) {
9298 if (save_ire != NULL)
9299 ire_refrele(save_ire);
9300 break;
9301 }
9302
9303 ire->ire_marks |= ire_marks;
9304
9305 /*
9306 * If IRE_MARK_NOADD is set then we need to convert
9307 * the max_fragp to a useable value now. This is
9308 * normally done in ire_add_v[46]. We also need to
9309 * associate the ire with an nce (normally would be
9310 * done in ip_wput_nondata()).
9311 *
9312 * Note that IRE_MARK_NOADD packets created here
9313 * do not have a non-null ire_mp pointer. The null
9314 * value of ire_bucket indicates that they were
9315 * never added.
9316 */
9317 if (ire->ire_marks & IRE_MARK_NOADD) {
9318 uint_t max_frag;
9319
9320 max_frag = *ire->ire_max_fragp;
9321 ire->ire_max_fragp = NULL;
9322 ire->ire_max_frag = max_frag;
9323
9324 if ((ire->ire_nce = ndp_lookup_v4(
9325 ire_to_ill(ire),
9326 (ire->ire_gateway_addr != INADDR_ANY ?
9327 &ire->ire_gateway_addr : &ire->ire_addr),
9328 B_FALSE)) == NULL) {
9329 if (save_ire != NULL)
9330 ire_refrele(save_ire);
9331 break;
9332 }
9333 ASSERT(ire->ire_nce->nce_state ==
9334 ND_REACHABLE);
9335 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
9336 }
9337
9338 /* Prevent save_ire from getting deleted */
9339 if (save_ire != NULL) {
9340 IRB_REFHOLD(save_ire->ire_bucket);
9341 /* Has it been removed already ? */
9342 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
9343 IRB_REFRELE(save_ire->ire_bucket);
9344 ire_refrele(save_ire);
9345 break;
9346 }
9347 }
9348
9349 ire_add_then_send(q, ire, first_mp);
9350
9351 /* Assert that save_ire is not deleted yet. */
9352 if (save_ire != NULL) {
9353 ASSERT(save_ire->ire_ptpn != NULL);
9354 IRB_REFRELE(save_ire->ire_bucket);
9355 ire_refrele(save_ire);
9356 save_ire = NULL;
9357 }
9358 if (fire != NULL) {
9359 ire_refrele(fire);
9360 fire = NULL;
9361 }
9362
9363 /*
9364 * the resolution loop is re-entered if this
9365 * was requested through flags and if we
9366 * actually are in a multirouting case.
9367 */
9368 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) {
9369 boolean_t need_resolve =
9370 ire_multirt_need_resolve(ipha_dst,
9371 MBLK_GETLABEL(copy_mp), ipst);
9372 if (!need_resolve) {
9373 MULTIRT_DEBUG_UNTAG(copy_mp);
9374 freemsg(copy_mp);
9375 copy_mp = NULL;
9376 } else {
9377 /*
9378 * ipif_lookup_group() calls
9379 * ire_lookup_multi() that uses
9380 * ire_ftable_lookup() to find
9381 * an IRE_INTERFACE for the group.
9382 * In the multirt case,
9383 * ire_lookup_multi() then invokes
9384 * ire_multirt_lookup() to find
9385 * the next resolvable ire.
9386 * As a result, we obtain an new
9387 * interface, derived from the
9388 * next ire.
9389 */
9390 ipif_refrele(ipif);
9391 ipif = ipif_lookup_group(ipha_dst,
9392 zoneid, ipst);
9393 ip2dbg(("ip_newroute_ipif: "
9394 "multirt dst %08x, ipif %p\n",
9395 htonl(dst), (void *)ipif));
9396 if (ipif != NULL) {
9397 mp = copy_mp;
9398 copy_mp = NULL;
9399 multirt_resolve_next = B_TRUE;
9400 continue;
9401 } else {
9402 freemsg(copy_mp);
9403 }
9404 }
9405 }
9406 if (ipif != NULL)
9407 ipif_refrele(ipif);
9408 ill_refrele(dst_ill);
9409 ipif_refrele(src_ipif);
9410 return;
9411 }
9412 case IRE_IF_RESOLVER:
9413 /*
9414 * We can't build an IRE_CACHE yet, but at least
9415 * we found a resolver that can help.
9416 */
9417 res_mp = dst_ill->ill_resolver_mp;
9418 if (!OK_RESOLVER_MP(res_mp))
9419 break;
9420
9421 /*
9422 * We obtain a partial IRE_CACHE which we will pass
9423 * along with the resolver query. When the response
9424 * comes back it will be there ready for us to add.
9425 * The new ire inherits the IRE_OFFSUBNET flags
9426 * and source address, if this was requested.
9427 * The ire_max_frag is atomically set under the
9428 * irebucket lock in ire_add_v[46]. Only in the
9429 * case of IRE_MARK_NOADD, we set it here itself.
9430 */
9431 ire = ire_create_mp(
9432 (uchar_t *)&dst, /* dest address */
9433 (uchar_t *)&ip_g_all_ones, /* mask */
9434 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
9435 NULL, /* gateway address */
9436 (ire_marks & IRE_MARK_NOADD) ?
9437 ipif->ipif_mtu : 0, /* max_frag */
9438 NULL, /* no src nce */
9439 dst_ill->ill_rq, /* recv-from queue */
9440 dst_ill->ill_wq, /* send-to queue */
9441 IRE_CACHE,
9442 src_ipif,
9443 (save_ire != NULL ? save_ire->ire_mask : 0),
9444 (fire != NULL) ? /* Parent handle */
9445 fire->ire_phandle : 0,
9446 ihandle, /* Interface handle */
9447 (fire != NULL) ? /* flags if any */
9448 (fire->ire_flags &
9449 (RTF_SETSRC | RTF_MULTIRT)) : 0,
9450 (save_ire == NULL ? &ire_uinfo_null :
9451 &save_ire->ire_uinfo),
9452 NULL,
9453 NULL,
9454 ipst);
9455
9456 if (save_ire != NULL) {
9457 ire_refrele(save_ire);
9458 save_ire = NULL;
9459 }
9460 if (ire == NULL)
9461 break;
9462
9463 ire->ire_marks |= ire_marks;
9464 /*
9465 * Construct message chain for the resolver of the
9466 * form:
9467 * ARP_REQ_MBLK-->IRE_MBLK-->Packet
9468 *
9469 * NOTE : ire will be added later when the response
9470 * comes back from ARP. If the response does not
9471 * come back, ARP frees the packet. For this reason,
9472 * we can't REFHOLD the bucket of save_ire to prevent
9473 * deletions. We may not be able to REFRELE the
9474 * bucket if the response never comes back.
9475 * Thus, before adding the ire, ire_add_v4 will make
9476 * sure that the interface route does not get deleted.
9477 * This is the only case unlike ip_newroute_v6,
9478 * ip_newroute_ipif_v6 where we can always prevent
9479 * deletions because ire_add_then_send is called after
9480 * creating the IRE.
9481 * If IRE_MARK_NOADD is set, then ire_add_then_send
9482 * does not add this IRE into the IRE CACHE.
9483 */
9484 ASSERT(ire->ire_mp != NULL);
9485 ire->ire_mp->b_cont = first_mp;
9486 /* Have saved_mp handy, for cleanup if canput fails */
9487 saved_mp = mp;
9488 mp = copyb(res_mp);
9489 if (mp == NULL) {
9490 /* Prepare for cleanup */
9491 mp = saved_mp; /* pkt */
9492 ire_delete(ire); /* ire_mp */
9493 ire = NULL;
9494 if (copy_mp != NULL) {
9495 MULTIRT_DEBUG_UNTAG(copy_mp);
9496 freemsg(copy_mp);
9497 copy_mp = NULL;
9498 }
9499 break;
9500 }
9501 linkb(mp, ire->ire_mp);
9502
9503 /*
9504 * Fill in the source and dest addrs for the resolver.
9505 * NOTE: this depends on memory layouts imposed by
9506 * ill_init().
9507 */
9508 areq = (areq_t *)mp->b_rptr;
9509 addrp = (ipaddr_t *)((char *)areq +
9510 areq->areq_sender_addr_offset);
9511 *addrp = ire->ire_src_addr;
9512 addrp = (ipaddr_t *)((char *)areq +
9513 areq->areq_target_addr_offset);
9514 *addrp = dst;
9515 /* Up to the resolver. */
9516 if (canputnext(dst_ill->ill_rq) &&
9517 !(dst_ill->ill_arp_closing)) {
9518 putnext(dst_ill->ill_rq, mp);
9519 /*
9520 * The response will come back in ip_wput
9521 * with db_type IRE_DB_TYPE.
9522 */
9523 } else {
9524 mp->b_cont = NULL;
9525 freeb(mp); /* areq */
9526 ire_delete(ire); /* ire_mp */
9527 saved_mp->b_next = NULL;
9528 saved_mp->b_prev = NULL;
9529 freemsg(first_mp); /* pkt */
9530 ip2dbg(("ip_newroute_ipif: dropped\n"));
9531 }
9532
9533 if (fire != NULL) {
9534 ire_refrele(fire);
9535 fire = NULL;
9536 }
9537
9538
9539 /*
9540 * The resolution loop is re-entered if this was
9541 * requested through flags and we actually are
9542 * in a multirouting case.
9543 */
9544 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) {
9545 boolean_t need_resolve =
9546 ire_multirt_need_resolve(ipha_dst,
9547 MBLK_GETLABEL(copy_mp), ipst);
9548 if (!need_resolve) {
9549 MULTIRT_DEBUG_UNTAG(copy_mp);
9550 freemsg(copy_mp);
9551 copy_mp = NULL;
9552 } else {
9553 /*
9554 * ipif_lookup_group() calls
9555 * ire_lookup_multi() that uses
9556 * ire_ftable_lookup() to find
9557 * an IRE_INTERFACE for the group.
9558 * In the multirt case,
9559 * ire_lookup_multi() then invokes
9560 * ire_multirt_lookup() to find
9561 * the next resolvable ire.
9562 * As a result, we obtain an new
9563 * interface, derived from the
9564 * next ire.
9565 */
9566 ipif_refrele(ipif);
9567 ipif = ipif_lookup_group(ipha_dst,
9568 zoneid, ipst);
9569 if (ipif != NULL) {
9570 mp = copy_mp;
9571 copy_mp = NULL;
9572 multirt_resolve_next = B_TRUE;
9573 continue;
9574 } else {
9575 freemsg(copy_mp);
9576 }
9577 }
9578 }
9579 if (ipif != NULL)
9580 ipif_refrele(ipif);
9581 ill_refrele(dst_ill);
9582 ipif_refrele(src_ipif);
9583 return;
9584 default:
9585 break;
9586 }
9587 } while (multirt_resolve_next);
9588
9589 err_ret:
9590 ip2dbg(("ip_newroute_ipif: dropped\n"));
9591 if (fire != NULL)
9592 ire_refrele(fire);
9593 ipif_refrele(ipif);
9594 /* Did this packet originate externally? */
9595 if (dst_ill != NULL)
9596 ill_refrele(dst_ill);
9597 if (src_ipif != NULL)
9598 ipif_refrele(src_ipif);
9599 if (mp->b_prev || mp->b_next) {
9600 mp->b_next = NULL;
9601 mp->b_prev = NULL;
9602 } else {
9603 /*
9604 * Since ip_wput() isn't close to finished, we fill
9605 * in enough of the header for credible error reporting.
9606 */
9607 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) {
9608 /* Failed */
9609 freemsg(first_mp);
9610 if (ire != NULL)
9611 ire_refrele(ire);
9612 return;
9613 }
9614 }
9615 /*
9616 * At this point we will have ire only if RTF_BLACKHOLE
9617 * or RTF_REJECT flags are set on the IRE. It will not
9618 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set.
9619 */
9620 if (ire != NULL) {
9621 if (ire->ire_flags & RTF_BLACKHOLE) {
9622 ire_refrele(ire);
9623 freemsg(first_mp);
9624 return;
9625 }
9626 ire_refrele(ire);
9627 }
9628 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst);
9629 }
9630
9631 /* Name/Value Table Lookup Routine */
9632 char *
9633 ip_nv_lookup(nv_t *nv, int value)
9634 {
9635 if (!nv)
9636 return (NULL);
9637 for (; nv->nv_name; nv++) {
9638 if (nv->nv_value == value)
9639 return (nv->nv_name);
9640 }
9641 return ("unknown");
9642 }
9643
9644 /*
9645 * This is a module open, i.e. this is a control stream for access
9646 * to a DLPI device. We allocate an ill_t as the instance data in
9647 * this case.
9648 */
9649 int
9650 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
9651 {
9652 ill_t *ill;
9653 int err;
9654 zoneid_t zoneid;
9655 netstack_t *ns;
9656 ip_stack_t *ipst;
9657
9658 /*
9659 * Prevent unprivileged processes from pushing IP so that
9660 * they can't send raw IP.
9661 */
9662 if (secpolicy_net_rawaccess(credp) != 0)
9663 return (EPERM);
9664
9665 ns = netstack_find_by_cred(credp);
9666 ASSERT(ns != NULL);
9667 ipst = ns->netstack_ip;
9668 ASSERT(ipst != NULL);
9669
9670 /*
9671 * For exclusive stacks we set the zoneid to zero
9672 * to make IP operate as if in the global zone.
9673 */
9674 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
9675 zoneid = GLOBAL_ZONEID;
9676 else
9677 zoneid = crgetzoneid(credp);
9678
9679 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
9680 q->q_ptr = WR(q)->q_ptr = ill;
9681 ill->ill_ipst = ipst;
9682 ill->ill_zoneid = zoneid;
9683
9684 /*
9685 * ill_init initializes the ill fields and then sends down
9686 * down a DL_INFO_REQ after calling qprocson.
9687 */
9688 err = ill_init(q, ill);
9689 if (err != 0) {
9690 mi_free(ill);
9691 netstack_rele(ipst->ips_netstack);
9692 q->q_ptr = NULL;
9693 WR(q)->q_ptr = NULL;
9694 return (err);
9695 }
9696
9697 /* ill_init initializes the ipsq marking this thread as writer */
9698 ipsq_exit(ill->ill_phyint->phyint_ipsq);
9699 /* Wait for the DL_INFO_ACK */
9700 mutex_enter(&ill->ill_lock);
9701 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
9702 /*
9703 * Return value of 0 indicates a pending signal.
9704 */
9705 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
9706 if (err == 0) {
9707 mutex_exit(&ill->ill_lock);
9708 (void) ip_close(q, 0);
9709 return (EINTR);
9710 }
9711 }
9712 mutex_exit(&ill->ill_lock);
9713
9714 /*
9715 * ip_rput_other could have set an error in ill_error on
9716 * receipt of M_ERROR.
9717 */
9718
9719 err = ill->ill_error;
9720 if (err != 0) {
9721 (void) ip_close(q, 0);
9722 return (err);
9723 }
9724
9725 ill->ill_credp = credp;
9726 crhold(credp);
9727
9728 mutex_enter(&ipst->ips_ip_mi_lock);
9729 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag,
9730 credp);
9731 mutex_exit(&ipst->ips_ip_mi_lock);
9732 if (err) {
9733 (void) ip_close(q, 0);
9734 return (err);
9735 }
9736 return (0);
9737 }
9738
9739 /* For /dev/ip aka AF_INET open */
9740 int
9741 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
9742 {
9743 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
9744 }
9745
9746 /* For /dev/ip6 aka AF_INET6 open */
9747 int
9748 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
9749 {
9750 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
9751 }
9752
9753 /* IP open routine. */
9754 int
9755 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
9756 boolean_t isv6)
9757 {
9758 conn_t *connp;
9759 major_t maj;
9760 zoneid_t zoneid;
9761 netstack_t *ns;
9762 ip_stack_t *ipst;
9763
9764 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q);
9765
9766 /* Allow reopen. */
9767 if (q->q_ptr != NULL)
9768 return (0);
9769
9770 if (sflag & MODOPEN) {
9771 /* This is a module open */
9772 return (ip_modopen(q, devp, flag, sflag, credp));
9773 }
9774
9775 ns = netstack_find_by_cred(credp);
9776 ASSERT(ns != NULL);
9777 ipst = ns->netstack_ip;
9778 ASSERT(ipst != NULL);
9779
9780 /*
9781 * For exclusive stacks we set the zoneid to zero
9782 * to make IP operate as if in the global zone.
9783 */
9784 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
9785 zoneid = GLOBAL_ZONEID;
9786 else
9787 zoneid = crgetzoneid(credp);
9788
9789 /*
9790 * We are opening as a device. This is an IP client stream, and we
9791 * allocate an conn_t as the instance data.
9792 */
9793 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
9794
9795 /*
9796 * ipcl_conn_create did a netstack_hold. Undo the hold that was
9797 * done by netstack_find_by_cred()
9798 */
9799 netstack_rele(ipst->ips_netstack);
9800
9801 connp->conn_zoneid = zoneid;
9802
9803 connp->conn_upq = q;
9804 q->q_ptr = WR(q)->q_ptr = connp;
9805
9806 if (flag & SO_SOCKSTR)
9807 connp->conn_flags |= IPCL_SOCKET;
9808
9809 /* Minor tells us which /dev entry was opened */
9810 if (isv6) {
9811 connp->conn_flags |= IPCL_ISV6;
9812 connp->conn_af_isv6 = B_TRUE;
9813 ip_setpktversion(connp, isv6, B_FALSE, ipst);
9814 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT;
9815 } else {
9816 connp->conn_af_isv6 = B_FALSE;
9817 connp->conn_pkt_isv6 = B_FALSE;
9818 }
9819
9820 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
9821 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
9822 connp->conn_minor_arena = ip_minor_arena_la;
9823 } else {
9824 /*
9825 * Either minor numbers in the large arena were exhausted
9826 * or a non socket application is doing the open.
9827 * Try to allocate from the small arena.
9828 */
9829 if ((connp->conn_dev =
9830 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
9831 /* CONN_DEC_REF takes care of netstack_rele() */
9832 q->q_ptr = WR(q)->q_ptr = NULL;
9833 CONN_DEC_REF(connp);
9834 return (EBUSY);
9835 }
9836 connp->conn_minor_arena = ip_minor_arena_sa;
9837 }
9838
9839 maj = getemajor(*devp);
9840 *devp = makedevice(maj, (minor_t)connp->conn_dev);
9841
9842 /*
9843 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
9844 */
9845 connp->conn_cred = credp;
9846
9847 /*
9848 * Handle IP_RTS_REQUEST and other ioctls which use conn_recv
9849 */
9850 connp->conn_recv = ip_conn_input;
9851
9852 crhold(connp->conn_cred);
9853
9854 /*
9855 * If the caller has the process-wide flag set, then default to MAC
9856 * exempt mode. This allows read-down to unlabeled hosts.
9857 */
9858 if (getpflags(NET_MAC_AWARE, credp) != 0)
9859 connp->conn_mac_exempt = B_TRUE;
9860
9861 connp->conn_rq = q;
9862 connp->conn_wq = WR(q);
9863
9864 /* Non-zero default values */
9865 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
9866
9867 /*
9868 * Make the conn globally visible to walkers
9869 */
9870 ASSERT(connp->conn_ref == 1);
9871 mutex_enter(&connp->conn_lock);
9872 connp->conn_state_flags &= ~CONN_INCIPIENT;
9873 mutex_exit(&connp->conn_lock);
9874
9875 qprocson(q);
9876
9877 return (0);
9878 }
9879
9880 /*
9881 * Change the output format (IPv4 vs. IPv6) for a conn_t.
9882 * Note that there is no race since either ip_output function works - it
9883 * is just an optimization to enter the best ip_output routine directly.
9884 */
9885 void
9886 ip_setpktversion(conn_t *connp, boolean_t isv6, boolean_t bump_mib,
9887 ip_stack_t *ipst)
9888 {
9889 if (isv6) {
9890 if (bump_mib) {
9891 BUMP_MIB(&ipst->ips_ip6_mib,
9892 ipIfStatsOutSwitchIPVersion);
9893 }
9894 connp->conn_send = ip_output_v6;
9895 connp->conn_pkt_isv6 = B_TRUE;
9896 } else {
9897 if (bump_mib) {
9898 BUMP_MIB(&ipst->ips_ip_mib,
9899 ipIfStatsOutSwitchIPVersion);
9900 }
9901 connp->conn_send = ip_output;
9902 connp->conn_pkt_isv6 = B_FALSE;
9903 }
9904
9905 }
9906
9907 /*
9908 * See if IPsec needs loading because of the options in mp.
9909 */
9910 static boolean_t
9911 ipsec_opt_present(mblk_t *mp)
9912 {
9913 uint8_t *optcp, *next_optcp, *opt_endcp;
9914 struct opthdr *opt;
9915 struct T_opthdr *topt;
9916 int opthdr_len;
9917 t_uscalar_t optname, optlevel;
9918 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr;
9919 ipsec_req_t *ipsr;
9920
9921 /*
9922 * Walk through the mess, and find IP_SEC_OPT. If it's there,
9923 * return TRUE.
9924 */
9925
9926 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length);
9927 opt_endcp = optcp + tor->OPT_length;
9928 if (tor->PRIM_type == T_OPTMGMT_REQ) {
9929 opthdr_len = sizeof (struct T_opthdr);
9930 } else { /* O_OPTMGMT_REQ */
9931 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ);
9932 opthdr_len = sizeof (struct opthdr);
9933 }
9934 for (; optcp < opt_endcp; optcp = next_optcp) {
9935 if (optcp + opthdr_len > opt_endcp)
9936 return (B_FALSE); /* Not enough option header. */
9937 if (tor->PRIM_type == T_OPTMGMT_REQ) {
9938 topt = (struct T_opthdr *)optcp;
9939 optlevel = topt->level;
9940 optname = topt->name;
9941 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len);
9942 } else {
9943 opt = (struct opthdr *)optcp;
9944 optlevel = opt->level;
9945 optname = opt->name;
9946 next_optcp = optcp + opthdr_len +
9947 _TPI_ALIGN_OPT(opt->len);
9948 }
9949 if ((next_optcp < optcp) || /* wraparound pointer space */
9950 ((next_optcp >= opt_endcp) && /* last option bad len */
9951 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE)))
9952 return (B_FALSE); /* bad option buffer */
9953 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) ||
9954 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) {
9955 /*
9956 * Check to see if it's an all-bypass or all-zeroes
9957 * IPsec request. Don't bother loading IPsec if
9958 * the socket doesn't want to use it. (A good example
9959 * is a bypass request.)
9960 *
9961 * Basically, if any of the non-NEVER bits are set,
9962 * load IPsec.
9963 */
9964 ipsr = (ipsec_req_t *)(optcp + opthdr_len);
9965 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 ||
9966 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 ||
9967 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER)
9968 != 0)
9969 return (B_TRUE);
9970 }
9971 }
9972 return (B_FALSE);
9973 }
9974
9975 /*
9976 * If conn is is waiting for ipsec to finish loading, kick it.
9977 */
9978 /* ARGSUSED */
9979 static void
9980 conn_restart_ipsec_waiter(conn_t *connp, void *arg)
9981 {
9982 t_scalar_t optreq_prim;
9983 mblk_t *mp;
9984 cred_t *cr;
9985 int err = 0;
9986
9987 /*
9988 * This function is called, after ipsec loading is complete.
9989 * Since IP checks exclusively and atomically (i.e it prevents
9990 * ipsec load from completing until ip_optcom_req completes)
9991 * whether ipsec load is complete, there cannot be a race with IP
9992 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now.
9993 */
9994 mutex_enter(&connp->conn_lock);
9995 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) {
9996 ASSERT(connp->conn_ipsec_opt_mp != NULL);
9997 mp = connp->conn_ipsec_opt_mp;
9998 connp->conn_ipsec_opt_mp = NULL;
9999 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT;
10000 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp)));
10001 mutex_exit(&connp->conn_lock);
10002
10003 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
10004
10005 optreq_prim = ((union T_primitives *)mp->b_rptr)->type;
10006 if (optreq_prim == T_OPTMGMT_REQ) {
10007 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr,
10008 &ip_opt_obj, B_FALSE);
10009 } else {
10010 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ);
10011 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr,
10012 &ip_opt_obj, B_FALSE);
10013 }
10014 if (err != EINPROGRESS)
10015 CONN_OPER_PENDING_DONE(connp);
10016 return;
10017 }
10018 mutex_exit(&connp->conn_lock);
10019 }
10020
10021 /*
10022 * Called from the ipsec_loader thread, outside any perimeter, to tell
10023 * ip qenable any of the queues waiting for the ipsec loader to
10024 * complete.
10025 */
10026 void
10027 ip_ipsec_load_complete(ipsec_stack_t *ipss)
10028 {
10029 netstack_t *ns = ipss->ipsec_netstack;
10030
10031 ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip);
10032 }
10033
10034 /*
10035 * Can't be used. Need to call svr4* -> optset directly. the leaf routine
10036 * determines the grp on which it has to become exclusive, queues the mp
10037 * and sq draining restarts the optmgmt
10038 */
10039 static boolean_t
10040 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp)
10041 {
10042 conn_t *connp = Q_TO_CONN(q);
10043 ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec;
10044
10045 /*
10046 * Take IPsec requests and treat them special.
10047 */
10048 if (ipsec_opt_present(mp)) {
10049 /* First check if IPsec is loaded. */
10050 mutex_enter(&ipss->ipsec_loader_lock);
10051 if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) {
10052 mutex_exit(&ipss->ipsec_loader_lock);
10053 return (B_FALSE);
10054 }
10055 mutex_enter(&connp->conn_lock);
10056 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT;
10057
10058 ASSERT(connp->conn_ipsec_opt_mp == NULL);
10059 connp->conn_ipsec_opt_mp = mp;
10060 mutex_exit(&connp->conn_lock);
10061 mutex_exit(&ipss->ipsec_loader_lock);
10062
10063 ipsec_loader_loadnow(ipss);
10064 return (B_TRUE);
10065 }
10066 return (B_FALSE);
10067 }
10068
10069 /*
10070 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
10071 * all of them are copied to the conn_t. If the req is "zero", the policy is
10072 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
10073 * fields.
10074 * We keep only the latest setting of the policy and thus policy setting
10075 * is not incremental/cumulative.
10076 *
10077 * Requests to set policies with multiple alternative actions will
10078 * go through a different API.
10079 */
10080 int
10081 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
10082 {
10083 uint_t ah_req = 0;
10084 uint_t esp_req = 0;
10085 uint_t se_req = 0;
10086 ipsec_selkey_t sel;
10087 ipsec_act_t *actp = NULL;
10088 uint_t nact;
10089 ipsec_policy_t *pin4 = NULL, *pout4 = NULL;
10090 ipsec_policy_t *pin6 = NULL, *pout6 = NULL;
10091 ipsec_policy_root_t *pr;
10092 ipsec_policy_head_t *ph;
10093 int fam;
10094 boolean_t is_pol_reset;
10095 int error = 0;
10096 netstack_t *ns = connp->conn_netstack;
10097 ip_stack_t *ipst = ns->netstack_ip;
10098 ipsec_stack_t *ipss = ns->netstack_ipsec;
10099
10100 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
10101
10102 /*
10103 * The IP_SEC_OPT option does not allow variable length parameters,
10104 * hence a request cannot be NULL.
10105 */
10106 if (req == NULL)
10107 return (EINVAL);
10108
10109 ah_req = req->ipsr_ah_req;
10110 esp_req = req->ipsr_esp_req;
10111 se_req = req->ipsr_self_encap_req;
10112
10113 /* Don't allow setting self-encap without one or more of AH/ESP. */
10114 if (se_req != 0 && esp_req == 0 && ah_req == 0)
10115 return (EINVAL);
10116
10117 /*
10118 * Are we dealing with a request to reset the policy (i.e.
10119 * zero requests).
10120 */
10121 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
10122 (esp_req & REQ_MASK) == 0 &&
10123 (se_req & REQ_MASK) == 0);
10124
10125 if (!is_pol_reset) {
10126 /*
10127 * If we couldn't load IPsec, fail with "protocol
10128 * not supported".
10129 * IPsec may not have been loaded for a request with zero
10130 * policies, so we don't fail in this case.
10131 */
10132 mutex_enter(&ipss->ipsec_loader_lock);
10133 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
10134 mutex_exit(&ipss->ipsec_loader_lock);
10135 return (EPROTONOSUPPORT);
10136 }
10137 mutex_exit(&ipss->ipsec_loader_lock);
10138
10139 /*
10140 * Test for valid requests. Invalid algorithms
10141 * need to be tested by IPsec code because new
10142 * algorithms can be added dynamically.
10143 */
10144 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
10145 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
10146 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
10147 return (EINVAL);
10148 }
10149
10150 /*
10151 * Only privileged users can issue these
10152 * requests.
10153 */
10154 if (((ah_req & IPSEC_PREF_NEVER) ||
10155 (esp_req & IPSEC_PREF_NEVER) ||
10156 (se_req & IPSEC_PREF_NEVER)) &&
10157 secpolicy_ip_config(cr, B_FALSE) != 0) {
10158 return (EPERM);
10159 }
10160
10161 /*
10162 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
10163 * are mutually exclusive.
10164 */
10165 if (((ah_req & REQ_MASK) == REQ_MASK) ||
10166 ((esp_req & REQ_MASK) == REQ_MASK) ||
10167 ((se_req & REQ_MASK) == REQ_MASK)) {
10168 /* Both of them are set */
10169 return (EINVAL);
10170 }
10171 }
10172
10173 mutex_enter(&connp->conn_lock);
10174
10175 /*
10176 * If we have already cached policies in ip_bind_connected*(), don't
10177 * let them change now. We cache policies for connections
10178 * whose src,dst [addr, port] is known.
10179 */
10180 if (connp->conn_policy_cached) {
10181 mutex_exit(&connp->conn_lock);
10182 return (EINVAL);
10183 }
10184
10185 /*
10186 * We have a zero policies, reset the connection policy if already
10187 * set. This will cause the connection to inherit the
10188 * global policy, if any.
10189 */
10190 if (is_pol_reset) {
10191 if (connp->conn_policy != NULL) {
10192 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
10193 connp->conn_policy = NULL;
10194 }
10195 connp->conn_flags &= ~IPCL_CHECK_POLICY;
10196 connp->conn_in_enforce_policy = B_FALSE;
10197 connp->conn_out_enforce_policy = B_FALSE;
10198 mutex_exit(&connp->conn_lock);
10199 return (0);
10200 }
10201
10202 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
10203 ipst->ips_netstack);
10204 if (ph == NULL)
10205 goto enomem;
10206
10207 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
10208 if (actp == NULL)
10209 goto enomem;
10210
10211 /*
10212 * Always allocate IPv4 policy entries, since they can also
10213 * apply to ipv6 sockets being used in ipv4-compat mode.
10214 */
10215 bzero(&sel, sizeof (sel));
10216 sel.ipsl_valid = IPSL_IPV4;
10217
10218 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL,
10219 ipst->ips_netstack);
10220 if (pin4 == NULL)
10221 goto enomem;
10222
10223 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL,
10224 ipst->ips_netstack);
10225 if (pout4 == NULL)
10226 goto enomem;
10227
10228 if (connp->conn_af_isv6) {
10229 /*
10230 * We're looking at a v6 socket, also allocate the
10231 * v6-specific entries...
10232 */
10233 sel.ipsl_valid = IPSL_IPV6;
10234 pin6 = ipsec_policy_create(&sel, actp, nact,
10235 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack);
10236 if (pin6 == NULL)
10237 goto enomem;
10238
10239 pout6 = ipsec_policy_create(&sel, actp, nact,
10240 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack);
10241 if (pout6 == NULL)
10242 goto enomem;
10243
10244 /*
10245 * .. and file them away in the right place.
10246 */
10247 fam = IPSEC_AF_V6;
10248 pr = &ph->iph_root[IPSEC_TYPE_INBOUND];
10249 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]);
10250 ipsec_insert_always(&ph->iph_rulebyid, pin6);
10251 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND];
10252 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]);
10253 ipsec_insert_always(&ph->iph_rulebyid, pout6);
10254 }
10255
10256 ipsec_actvec_free(actp, nact);
10257
10258 /*
10259 * File the v4 policies.
10260 */
10261 fam = IPSEC_AF_V4;
10262 pr = &ph->iph_root[IPSEC_TYPE_INBOUND];
10263 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]);
10264 ipsec_insert_always(&ph->iph_rulebyid, pin4);
10265
10266 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND];
10267 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]);
10268 ipsec_insert_always(&ph->iph_rulebyid, pout4);
10269
10270 /*
10271 * If the requests need security, set enforce_policy.
10272 * If the requests are IPSEC_PREF_NEVER, one should
10273 * still set conn_out_enforce_policy so that an ipsec_out
10274 * gets attached in ip_wput. This is needed so that
10275 * for connections that we don't cache policy in ip_bind,
10276 * if global policy matches in ip_wput_attach_policy, we
10277 * don't wrongly inherit global policy. Similarly, we need
10278 * to set conn_in_enforce_policy also so that we don't verify
10279 * policy wrongly.
10280 */
10281 if ((ah_req & REQ_MASK) != 0 ||
10282 (esp_req & REQ_MASK) != 0 ||
10283 (se_req & REQ_MASK) != 0) {
10284 connp->conn_in_enforce_policy = B_TRUE;
10285 connp->conn_out_enforce_policy = B_TRUE;
10286 connp->conn_flags |= IPCL_CHECK_POLICY;
10287 }
10288
10289 mutex_exit(&connp->conn_lock);
10290 return (error);
10291 #undef REQ_MASK
10292
10293 /*
10294 * Common memory-allocation-failure exit path.
10295 */
10296 enomem:
10297 mutex_exit(&connp->conn_lock);
10298 if (actp != NULL)
10299 ipsec_actvec_free(actp, nact);
10300 if (pin4 != NULL)
10301 IPPOL_REFRELE(pin4, ipst->ips_netstack);
10302 if (pout4 != NULL)
10303 IPPOL_REFRELE(pout4, ipst->ips_netstack);
10304 if (pin6 != NULL)
10305 IPPOL_REFRELE(pin6, ipst->ips_netstack);
10306 if (pout6 != NULL)
10307 IPPOL_REFRELE(pout6, ipst->ips_netstack);
10308 return (ENOMEM);
10309 }
10310
10311 /*
10312 * Only for options that pass in an IP addr. Currently only V4 options
10313 * pass in an ipif. V6 options always pass an ifindex specifying the ill.
10314 * So this function assumes level is IPPROTO_IP
10315 */
10316 int
10317 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option,
10318 mblk_t *first_mp)
10319 {
10320 ipif_t *ipif = NULL;
10321 int error;
10322 ill_t *ill;
10323 int zoneid;
10324 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
10325
10326 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr));
10327
10328 if (addr != INADDR_ANY || checkonly) {
10329 ASSERT(connp != NULL);
10330 zoneid = IPCL_ZONEID(connp);
10331 if (option == IP_NEXTHOP) {
10332 ipif = ipif_lookup_onlink_addr(addr,
10333 connp->conn_zoneid, ipst);
10334 } else {
10335 ipif = ipif_lookup_addr(addr, NULL, zoneid,
10336 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt,
10337 &error, ipst);
10338 }
10339 if (ipif == NULL) {
10340 if (error == EINPROGRESS)
10341 return (error);
10342 else if ((option == IP_MULTICAST_IF) ||
10343 (option == IP_NEXTHOP))
10344 return (EHOSTUNREACH);
10345 else
10346 return (EINVAL);
10347 } else if (checkonly) {
10348 if (option == IP_MULTICAST_IF) {
10349 ill = ipif->ipif_ill;
10350 /* not supported by the virtual network iface */
10351 if (IS_VNI(ill)) {
10352 ipif_refrele(ipif);
10353 return (EINVAL);
10354 }
10355 }
10356 ipif_refrele(ipif);
10357 return (0);
10358 }
10359 ill = ipif->ipif_ill;
10360 mutex_enter(&connp->conn_lock);
10361 mutex_enter(&ill->ill_lock);
10362 if ((ill->ill_state_flags & ILL_CONDEMNED) ||
10363 (ipif->ipif_state_flags & IPIF_CONDEMNED)) {
10364 mutex_exit(&ill->ill_lock);
10365 mutex_exit(&connp->conn_lock);
10366 ipif_refrele(ipif);
10367 return (option == IP_MULTICAST_IF ?
10368 EHOSTUNREACH : EINVAL);
10369 }
10370 } else {
10371 mutex_enter(&connp->conn_lock);
10372 }
10373
10374 /* None of the options below are supported on the VNI */
10375 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) {
10376 mutex_exit(&ill->ill_lock);
10377 mutex_exit(&connp->conn_lock);
10378 ipif_refrele(ipif);
10379 return (EINVAL);
10380 }
10381
10382 switch (option) {
10383 case IP_DONTFAILOVER_IF:
10384 /*
10385 * This option is used by in.mpathd to ensure
10386 * that IPMP probe packets only go out on the
10387 * test interfaces. in.mpathd sets this option
10388 * on the non-failover interfaces.
10389 * For backward compatibility, this option
10390 * implicitly sets IP_MULTICAST_IF, as used
10391 * be done in bind(), so that ip_wput gets
10392 * this ipif to send mcast packets.
10393 */
10394 if (ipif != NULL) {
10395 ASSERT(addr != INADDR_ANY);
10396 connp->conn_nofailover_ill = ipif->ipif_ill;
10397 connp->conn_multicast_ipif = ipif;
10398 } else {
10399 ASSERT(addr == INADDR_ANY);
10400 connp->conn_nofailover_ill = NULL;
10401 connp->conn_multicast_ipif = NULL;
10402 }
10403 break;
10404
10405 case IP_MULTICAST_IF:
10406 connp->conn_multicast_ipif = ipif;
10407 break;
10408 case IP_NEXTHOP:
10409 connp->conn_nexthop_v4 = addr;
10410 connp->conn_nexthop_set = B_TRUE;
10411 break;
10412 }
10413
10414 if (ipif != NULL) {
10415 mutex_exit(&ill->ill_lock);
10416 mutex_exit(&connp->conn_lock);
10417 ipif_refrele(ipif);
10418 return (0);
10419 }
10420 mutex_exit(&connp->conn_lock);
10421 /* We succeded in cleared the option */
10422 return (0);
10423 }
10424
10425 /*
10426 * For options that pass in an ifindex specifying the ill. V6 options always
10427 * pass in an ill. Some v4 options also pass in ifindex specifying the ill.
10428 */
10429 int
10430 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly,
10431 int level, int option, mblk_t *first_mp)
10432 {
10433 ill_t *ill = NULL;
10434 int error = 0;
10435 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
10436
10437 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex));
10438 if (ifindex != 0) {
10439 ASSERT(connp != NULL);
10440 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp),
10441 first_mp, ip_restart_optmgmt, &error, ipst);
10442 if (ill != NULL) {
10443 if (checkonly) {
10444 /* not supported by the virtual network iface */
10445 if (IS_VNI(ill)) {
10446 ill_refrele(ill);
10447 return (EINVAL);
10448 }
10449 ill_refrele(ill);
10450 return (0);
10451 }
10452 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid,
10453 0, NULL)) {
10454 ill_refrele(ill);
10455 ill = NULL;
10456 mutex_enter(&connp->conn_lock);
10457 goto setit;
10458 }
10459 mutex_enter(&connp->conn_lock);
10460 mutex_enter(&ill->ill_lock);
10461 if (ill->ill_state_flags & ILL_CONDEMNED) {
10462 mutex_exit(&ill->ill_lock);
10463 mutex_exit(&connp->conn_lock);
10464 ill_refrele(ill);
10465 ill = NULL;
10466 mutex_enter(&connp->conn_lock);
10467 }
10468 goto setit;
10469 } else if (error == EINPROGRESS) {
10470 return (error);
10471 } else {
10472 error = 0;
10473 }
10474 }
10475 mutex_enter(&connp->conn_lock);
10476 setit:
10477 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6));
10478
10479 /*
10480 * The options below assume that the ILL (if any) transmits and/or
10481 * receives traffic. Neither of which is true for the virtual network
10482 * interface, so fail setting these on a VNI.
10483 */
10484 if (IS_VNI(ill)) {
10485 ASSERT(ill != NULL);
10486 mutex_exit(&ill->ill_lock);
10487 mutex_exit(&connp->conn_lock);
10488 ill_refrele(ill);
10489 return (EINVAL);
10490 }
10491
10492 if (level == IPPROTO_IP) {
10493 switch (option) {
10494 case IP_BOUND_IF:
10495 connp->conn_incoming_ill = ill;
10496 connp->conn_outgoing_ill = ill;
10497 connp->conn_orig_bound_ifindex = (ill == NULL) ?
10498 0 : ifindex;
10499 break;
10500
10501 case IP_MULTICAST_IF:
10502 /*
10503 * This option is an internal special. The socket
10504 * level IP_MULTICAST_IF specifies an 'ipaddr' and
10505 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF
10506 * specifies an ifindex and we try first on V6 ill's.
10507 * If we don't find one, we they try using on v4 ill's
10508 * intenally and we come here.
10509 */
10510 if (!checkonly && ill != NULL) {
10511 ipif_t *ipif;
10512 ipif = ill->ill_ipif;
10513
10514 if (ipif->ipif_state_flags & IPIF_CONDEMNED) {
10515 mutex_exit(&ill->ill_lock);
10516 mutex_exit(&connp->conn_lock);
10517 ill_refrele(ill);
10518 ill = NULL;
10519 mutex_enter(&connp->conn_lock);
10520 } else {
10521 connp->conn_multicast_ipif = ipif;
10522 }
10523 }
10524 break;
10525
10526 case IP_DHCPINIT_IF:
10527 if (connp->conn_dhcpinit_ill != NULL) {
10528 /*
10529 * We've locked the conn so conn_cleanup_ill()
10530 * cannot clear conn_dhcpinit_ill -- so it's
10531 * safe to access the ill.
10532 */
10533 ill_t *oill = connp->conn_dhcpinit_ill;
10534
10535 ASSERT(oill->ill_dhcpinit != 0);
10536 atomic_dec_32(&oill->ill_dhcpinit);
10537 connp->conn_dhcpinit_ill = NULL;
10538 }
10539
10540 if (ill != NULL) {
10541 connp->conn_dhcpinit_ill = ill;
10542 atomic_inc_32(&ill->ill_dhcpinit);
10543 }
10544 break;
10545 }
10546 } else {
10547 switch (option) {
10548 case IPV6_BOUND_IF:
10549 connp->conn_incoming_ill = ill;
10550 connp->conn_outgoing_ill = ill;
10551 connp->conn_orig_bound_ifindex = (ill == NULL) ?
10552 0 : ifindex;
10553 break;
10554
10555 case IPV6_BOUND_PIF:
10556 /*
10557 * Limit all transmit to this ill.
10558 * Unlike IPV6_BOUND_IF, using this option
10559 * prevents load spreading and failover from
10560 * happening when the interface is part of the
10561 * group. That's why we don't need to remember
10562 * the ifindex in orig_bound_ifindex as in
10563 * IPV6_BOUND_IF.
10564 */
10565 connp->conn_outgoing_pill = ill;
10566 break;
10567
10568 case IPV6_DONTFAILOVER_IF:
10569 /*
10570 * This option is used by in.mpathd to ensure
10571 * that IPMP probe packets only go out on the
10572 * test interfaces. in.mpathd sets this option
10573 * on the non-failover interfaces.
10574 */
10575 connp->conn_nofailover_ill = ill;
10576 /*
10577 * For backward compatibility, this option
10578 * implicitly sets ip_multicast_ill as used in
10579 * IPV6_MULTICAST_IF so that ip_wput gets
10580 * this ill to send mcast packets.
10581 */
10582 connp->conn_multicast_ill = ill;
10583 connp->conn_orig_multicast_ifindex = (ill == NULL) ?
10584 0 : ifindex;
10585 break;
10586
10587 case IPV6_MULTICAST_IF:
10588 /*
10589 * Set conn_multicast_ill to be the IPv6 ill.
10590 * Set conn_multicast_ipif to be an IPv4 ipif
10591 * for ifindex to make IPv4 mapped addresses
10592 * on PF_INET6 sockets honor IPV6_MULTICAST_IF.
10593 * Even if no IPv6 ill exists for the ifindex
10594 * we need to check for an IPv4 ifindex in order
10595 * for this to work with mapped addresses. In that
10596 * case only set conn_multicast_ipif.
10597 */
10598 if (!checkonly) {
10599 if (ifindex == 0) {
10600 connp->conn_multicast_ill = NULL;
10601 connp->conn_orig_multicast_ifindex = 0;
10602 connp->conn_multicast_ipif = NULL;
10603 } else if (ill != NULL) {
10604 connp->conn_multicast_ill = ill;
10605 connp->conn_orig_multicast_ifindex =
10606 ifindex;
10607 }
10608 }
10609 break;
10610 }
10611 }
10612
10613 if (ill != NULL) {
10614 mutex_exit(&ill->ill_lock);
10615 mutex_exit(&connp->conn_lock);
10616 ill_refrele(ill);
10617 return (0);
10618 }
10619 mutex_exit(&connp->conn_lock);
10620 /*
10621 * We succeeded in clearing the option (ifindex == 0) or failed to
10622 * locate the ill and could not set the option (ifindex != 0)
10623 */
10624 return (ifindex == 0 ? 0 : EINVAL);
10625 }
10626
10627 /* This routine sets socket options. */
10628 /* ARGSUSED */
10629 int
10630 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name,
10631 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
10632 void *dummy, cred_t *cr, mblk_t *first_mp)
10633 {
10634 int *i1 = (int *)invalp;
10635 conn_t *connp = Q_TO_CONN(q);
10636 int error = 0;
10637 boolean_t checkonly;
10638 ire_t *ire;
10639 boolean_t found;
10640 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
10641
10642 switch (optset_context) {
10643
10644 case SETFN_OPTCOM_CHECKONLY:
10645 checkonly = B_TRUE;
10646 /*
10647 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
10648 * inlen != 0 implies value supplied and
10649 * we have to "pretend" to set it.
10650 * inlen == 0 implies that there is no
10651 * value part in T_CHECK request and just validation
10652 * done elsewhere should be enough, we just return here.
10653 */
10654 if (inlen == 0) {
10655 *outlenp = 0;
10656 return (0);
10657 }
10658 break;
10659 case SETFN_OPTCOM_NEGOTIATE:
10660 case SETFN_UD_NEGOTIATE:
10661 case SETFN_CONN_NEGOTIATE:
10662 checkonly = B_FALSE;
10663 break;
10664 default:
10665 /*
10666 * We should never get here
10667 */
10668 *outlenp = 0;
10669 return (EINVAL);
10670 }
10671
10672 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
10673 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
10674
10675 /*
10676 * For fixed length options, no sanity check
10677 * of passed in length is done. It is assumed *_optcom_req()
10678 * routines do the right thing.
10679 */
10680
10681 switch (level) {
10682 case SOL_SOCKET:
10683 /*
10684 * conn_lock protects the bitfields, and is used to
10685 * set the fields atomically.
10686 */
10687 switch (name) {
10688 case SO_BROADCAST:
10689 if (!checkonly) {
10690 /* TODO: use value someplace? */
10691 mutex_enter(&connp->conn_lock);
10692 connp->conn_broadcast = *i1 ? 1 : 0;
10693 mutex_exit(&connp->conn_lock);
10694 }
10695 break; /* goto sizeof (int) option return */
10696 case SO_USELOOPBACK:
10697 if (!checkonly) {
10698 /* TODO: use value someplace? */
10699 mutex_enter(&connp->conn_lock);
10700 connp->conn_loopback = *i1 ? 1 : 0;
10701 mutex_exit(&connp->conn_lock);
10702 }
10703 break; /* goto sizeof (int) option return */
10704 case SO_DONTROUTE:
10705 if (!checkonly) {
10706 mutex_enter(&connp->conn_lock);
10707 connp->conn_dontroute = *i1 ? 1 : 0;
10708 mutex_exit(&connp->conn_lock);
10709 }
10710 break; /* goto sizeof (int) option return */
10711 case SO_REUSEADDR:
10712 if (!checkonly) {
10713 mutex_enter(&connp->conn_lock);
10714 connp->conn_reuseaddr = *i1 ? 1 : 0;
10715 mutex_exit(&connp->conn_lock);
10716 }
10717 break; /* goto sizeof (int) option return */
10718 case SO_PROTOTYPE:
10719 if (!checkonly) {
10720 mutex_enter(&connp->conn_lock);
10721 connp->conn_proto = *i1;
10722 mutex_exit(&connp->conn_lock);
10723 }
10724 break; /* goto sizeof (int) option return */
10725 case SO_ALLZONES:
10726 if (!checkonly) {
10727 mutex_enter(&connp->conn_lock);
10728 if (IPCL_IS_BOUND(connp)) {
10729 mutex_exit(&connp->conn_lock);
10730 return (EINVAL);
10731 }
10732 connp->conn_allzones = *i1 != 0 ? 1 : 0;
10733 mutex_exit(&connp->conn_lock);
10734 }
10735 break; /* goto sizeof (int) option return */
10736 case SO_ANON_MLP:
10737 if (!checkonly) {
10738 mutex_enter(&connp->conn_lock);
10739 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0;
10740 mutex_exit(&connp->conn_lock);
10741 }
10742 break; /* goto sizeof (int) option return */
10743 case SO_MAC_EXEMPT:
10744 if (secpolicy_net_mac_aware(cr) != 0 ||
10745 IPCL_IS_BOUND(connp))
10746 return (EACCES);
10747 if (!checkonly) {
10748 mutex_enter(&connp->conn_lock);
10749 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0;
10750 mutex_exit(&connp->conn_lock);
10751 }
10752