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 CONN_DEC_REF(connp);
6702 freemsg(first_mp);
6703 return;
6704 }
6705 if (flags & TH_ACK) {
6706 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid,
6707 ipst->ips_netstack->netstack_tcp, connp);
6708 CONN_DEC_REF(connp);
6709 return;
6710 }
6711
6712 CONN_DEC_REF(connp);
6713 freemsg(first_mp);
6714 return;
6715 }
6716
6717 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) {
6718 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha,
6719 NULL, mctl_present);
6720 if (first_mp == NULL) {
6721 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
6722 CONN_DEC_REF(connp);
6723 return;
6724 }
6725 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) {
6726 ASSERT(syn_present);
6727 if (mctl_present) {
6728 ASSERT(first_mp != mp);
6729 first_mp->b_datap->db_struioflag |=
6730 STRUIO_POLICY;
6731 } else {
6732 ASSERT(first_mp == mp);
6733 mp->b_datap->db_struioflag &=
6734 ~STRUIO_EAGER;
6735 mp->b_datap->db_struioflag |=
6736 STRUIO_POLICY;
6737 }
6738 } else {
6739 /*
6740 * Discard first_mp early since we're dealing with a
6741 * fully-connected conn_t and tcp doesn't do policy in
6742 * this case.
6743 */
6744 if (mctl_present) {
6745 freeb(first_mp);
6746 mctl_present = B_FALSE;
6747 }
6748 first_mp = mp;
6749 }
6750 }
6751
6752 /*
6753 * Initiate policy processing here if needed. If we get here from
6754 * icmp_inbound_error_fanout, ip_policy is false.
6755 */
6756 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
6757 ill_index = recv_ill->ill_phyint->phyint_ifindex;
6758 ip_process(IPP_LOCAL_IN, &mp, ill_index);
6759 if (mp == NULL) {
6760 CONN_DEC_REF(connp);
6761 if (mctl_present)
6762 freeb(first_mp);
6763 return;
6764 } else if (mctl_present) {
6765 ASSERT(first_mp != mp);
6766 first_mp->b_cont = mp;
6767 } else {
6768 first_mp = mp;
6769 }
6770 }
6771
6772
6773
6774 /* Handle socket options. */
6775 if (!syn_present &&
6776 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) {
6777 /* Add header */
6778 ASSERT(recv_ill != NULL);
6779 /*
6780 * Since tcp does not support IP_RECVPKTINFO for V4, only pass
6781 * IPF_RECVIF.
6782 */
6783 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp),
6784 ipst);
6785 if (mp == NULL) {
6786 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
6787 CONN_DEC_REF(connp);
6788 if (mctl_present)
6789 freeb(first_mp);
6790 return;
6791 } else if (mctl_present) {
6792 /*
6793 * ip_add_info might return a new mp.
6794 */
6795 ASSERT(first_mp != mp);
6796 first_mp->b_cont = mp;
6797 } else {
6798 first_mp = mp;
6799 }
6800 }
6801 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
6802 if (IPCL_IS_TCP(connp)) {
6803 /* do not drain, certain use cases can blow the stack */
6804 squeue_enter_nodrain(connp->conn_sqp, first_mp,
6805 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP);
6806 } else {
6807 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
6808 (connp->conn_recv)(connp, first_mp, NULL);
6809 CONN_DEC_REF(connp);
6810 }
6811 }
6812
6813 /*
6814 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
6815 * pass it along to ESP if the SPI is non-zero. Returns TRUE if the mblk
6816 * is not consumed.
6817 *
6818 * One of four things can happen, all of which affect the passed-in mblk:
6819 *
6820 * 1.) ICMP messages that go through here just get returned TRUE.
6821 *
6822 * 2.) The packet is stock UDP and gets its zero-SPI stripped. Return TRUE.
6823 *
6824 * 3.) The packet is ESP-in-UDP, gets transformed into an equivalent
6825 * ESP packet, and is passed along to ESP for consumption. Return FALSE.
6826 *
6827 * 4.) The packet is an ESP-in-UDP Keepalive. Drop it and return FALSE.
6828 */
6829 static boolean_t
6830 zero_spi_check(queue_t *q, mblk_t *mp, ire_t *ire, ill_t *recv_ill,
6831 ipsec_stack_t *ipss)
6832 {
6833 int shift, plen, iph_len;
6834 ipha_t *ipha;
6835 udpha_t *udpha;
6836 uint32_t *spi;
6837 uint32_t esp_ports;
6838 uint8_t *orptr;
6839 boolean_t free_ire;
6840
6841 if (DB_TYPE(mp) == M_CTL) {
6842 /*
6843 * ICMP message with UDP inside. Don't bother stripping, just
6844 * send it up.
6845 *
6846 * NOTE: Any app with UDP_NAT_T_ENDPOINT set is probably going
6847 * to ignore errors set by ICMP anyway ('cause they might be
6848 * forged), but that's the app's decision, not ours.
6849 */
6850
6851 /* Bunch of reality checks for DEBUG kernels... */
6852 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
6853 ASSERT(((ipha_t *)mp->b_rptr)->ipha_protocol == IPPROTO_ICMP);
6854
6855 return (B_TRUE);
6856 }
6857
6858 ipha = (ipha_t *)mp->b_rptr;
6859 iph_len = IPH_HDR_LENGTH(ipha);
6860 plen = ntohs(ipha->ipha_length);
6861
6862 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
6863 /*
6864 * Most likely a keepalive for the benefit of an intervening
6865 * NAT. These aren't for us, per se, so drop it.
6866 *
6867 * RFC 3947/8 doesn't say for sure what to do for 2-3
6868 * byte packets (keepalives are 1-byte), but we'll drop them
6869 * also.
6870 */
6871 ip_drop_packet(mp, B_TRUE, recv_ill, NULL,
6872 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
6873 return (B_FALSE);
6874 }
6875
6876 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
6877 /* might as well pull it all up - it might be ESP. */
6878 if (!pullupmsg(mp, -1)) {
6879 ip_drop_packet(mp, B_TRUE, recv_ill, NULL,
6880 DROPPER(ipss, ipds_esp_nomem),
6881 &ipss->ipsec_dropper);
6882 return (B_FALSE);
6883 }
6884
6885 ipha = (ipha_t *)mp->b_rptr;
6886 }
6887 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
6888 if (*spi == 0) {
6889 /* UDP packet - remove 0-spi. */
6890 shift = sizeof (uint32_t);
6891 } else {
6892 /* ESP-in-UDP packet - reduce to ESP. */
6893 ipha->ipha_protocol = IPPROTO_ESP;
6894 shift = sizeof (udpha_t);
6895 }
6896
6897 /* Fix IP header */
6898 ipha->ipha_length = htons(plen - shift);
6899 ipha->ipha_hdr_checksum = 0;
6900
6901 orptr = mp->b_rptr;
6902 mp->b_rptr += shift;
6903
6904 udpha = (udpha_t *)(orptr + iph_len);
6905 if (*spi == 0) {
6906 ASSERT((uint8_t *)ipha == orptr);
6907 udpha->uha_length = htons(plen - shift - iph_len);
6908 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
6909 esp_ports = 0;
6910 } else {
6911 esp_ports = *((uint32_t *)udpha);
6912 ASSERT(esp_ports != 0);
6913 }
6914 ovbcopy(orptr, orptr + shift, iph_len);
6915 if (esp_ports != 0) /* Punt up for ESP processing. */ {
6916 ipha = (ipha_t *)(orptr + shift);
6917
6918 free_ire = (ire == NULL);
6919 if (free_ire) {
6920 /* Re-acquire ire. */
6921 ire = ire_cache_lookup(ipha->ipha_dst, ALL_ZONES, NULL,
6922 ipss->ipsec_netstack->netstack_ip);
6923 if (ire == NULL || !(ire->ire_type & IRE_LOCAL)) {
6924 if (ire != NULL)
6925 ire_refrele(ire);
6926 /*
6927 * Do a regular freemsg(), as this is an IP
6928 * error (no local route) not an IPsec one.
6929 */
6930 freemsg(mp);
6931 }
6932 }
6933
6934 ip_proto_input(q, mp, ipha, ire, recv_ill, esp_ports);
6935 if (free_ire)
6936 ire_refrele(ire);
6937 }
6938
6939 return (esp_ports == 0);
6940 }
6941
6942 /*
6943 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
6944 * We are responsible for disposing of mp, such as by freemsg() or putnext()
6945 * Caller is responsible for dropping references to the conn, and freeing
6946 * first_mp.
6947 *
6948 * IPQoS Notes
6949 * Before sending it to the client, invoke IPPF processing. Policy processing
6950 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and
6951 * ip_policy is true. If we get here from icmp_inbound_error_fanout or
6952 * ip_wput_local, ip_policy is false.
6953 */
6954 static void
6955 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp,
6956 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill,
6957 boolean_t ip_policy)
6958 {
6959 boolean_t mctl_present = (first_mp != NULL);
6960 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */
6961 uint32_t ill_index;
6962 ip_stack_t *ipst = recv_ill->ill_ipst;
6963 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6964
6965 ASSERT(ill != NULL);
6966
6967 if (mctl_present)
6968 first_mp->b_cont = mp;
6969 else
6970 first_mp = mp;
6971
6972 if (CONN_UDP_FLOWCTLD(connp)) {
6973 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
6974 freemsg(first_mp);
6975 return;
6976 }
6977
6978 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) {
6979 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha,
6980 NULL, mctl_present);
6981 if (first_mp == NULL) {
6982 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
6983 return; /* Freed by ipsec_check_inbound_policy(). */
6984 }
6985 }
6986 if (mctl_present)
6987 freeb(first_mp);
6988
6989 /* Let's hope the compilers utter "branch, predict-not-taken..." ;) */
6990 if (connp->conn_udp->udp_nat_t_endpoint) {
6991 if (mctl_present) {
6992 /* mctl_present *shouldn't* happen. */
6993 ip_drop_packet(mp, B_TRUE, NULL, NULL,
6994 DROPPER(ipss, ipds_esp_nat_t_ipsec),
6995 &ipss->ipsec_dropper);
6996 return;
6997 }
6998
6999 if (!zero_spi_check(ill->ill_rq, mp, NULL, recv_ill, ipss))
7000 return;
7001 }
7002
7003 /* Handle options. */
7004 if (connp->conn_recvif)
7005 in_flags = IPF_RECVIF;
7006 /*
7007 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag
7008 * passed to ip_add_info is based on IP version of connp.
7009 */
7010 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) {
7011 if (connp->conn_af_isv6) {
7012 /*
7013 * V6 only needs index
7014 */
7015 in_flags |= IPF_RECVIF;
7016 } else {
7017 /*
7018 * V4 needs index + matching address.
7019 */
7020 in_flags |= IPF_RECVADDR;
7021 }
7022 }
7023
7024 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA))
7025 in_flags |= IPF_RECVSLLA;
7026
7027 /*
7028 * Initiate IPPF processing here, if needed. Note first_mp won't be
7029 * freed if the packet is dropped. The caller will do so.
7030 */
7031 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
7032 ill_index = recv_ill->ill_phyint->phyint_ifindex;
7033 ip_process(IPP_LOCAL_IN, &mp, ill_index);
7034 if (mp == NULL) {
7035 return;
7036 }
7037 }
7038 if ((in_flags != 0) &&
7039 (mp->b_datap->db_type != M_CTL)) {
7040 /*
7041 * The actual data will be contained in b_cont
7042 * upon successful return of the following call
7043 * else original mblk is returned
7044 */
7045 ASSERT(recv_ill != NULL);
7046 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp),
7047 ipst);
7048 }
7049 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
7050 /* Send it upstream */
7051 (connp->conn_recv)(connp, mp, NULL);
7052 }
7053
7054 /*
7055 * Fanout for UDP packets.
7056 * The caller puts <fport, lport> in the ports parameter.
7057 *
7058 * If SO_REUSEADDR is set all multicast and broadcast packets
7059 * will be delivered to all streams bound to the same port.
7060 *
7061 * Zones notes:
7062 * Multicast and broadcast packets will be distributed to streams in all zones.
7063 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
7064 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
7065 * packets. To maintain this behavior with multiple zones, the conns are grouped
7066 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in
7067 * each zone. If unset, all the following conns in the same zone are skipped.
7068 */
7069 static void
7070 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha,
7071 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present,
7072 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid)
7073 {
7074 uint32_t dstport, srcport;
7075 ipaddr_t dst;
7076 mblk_t *first_mp;
7077 boolean_t secure;
7078 in6_addr_t v6src;
7079 conn_t *connp;
7080 connf_t *connfp;
7081 conn_t *first_connp;
7082 conn_t *next_connp;
7083 mblk_t *mp1, *first_mp1;
7084 ipaddr_t src;
7085 zoneid_t last_zoneid;
7086 boolean_t reuseaddr;
7087 boolean_t shared_addr;
7088 boolean_t unlabeled;
7089 ip_stack_t *ipst;
7090
7091 ASSERT(recv_ill != NULL);
7092 ipst = recv_ill->ill_ipst;
7093
7094 first_mp = mp;
7095 if (mctl_present) {
7096 mp = first_mp->b_cont;
7097 first_mp->b_cont = NULL;
7098 secure = ipsec_in_is_secure(first_mp);
7099 ASSERT(mp != NULL);
7100 } else {
7101 first_mp = NULL;
7102 secure = B_FALSE;
7103 }
7104
7105 /* Extract ports in net byte order */
7106 dstport = htons(ntohl(ports) & 0xFFFF);
7107 srcport = htons(ntohl(ports) >> 16);
7108 dst = ipha->ipha_dst;
7109 src = ipha->ipha_src;
7110
7111 unlabeled = B_FALSE;
7112 if (is_system_labeled())
7113 /* Cred cannot be null on IPv4 */
7114 unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
7115 TSLF_UNLABELED) != 0;
7116 shared_addr = (zoneid == ALL_ZONES);
7117 if (shared_addr) {
7118 /*
7119 * No need to handle exclusive-stack zones since ALL_ZONES
7120 * only applies to the shared stack.
7121 */
7122 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport);
7123 /*
7124 * If no shared MLP is found, tsol_mlp_findzone returns
7125 * ALL_ZONES. In that case, we assume it's SLP, and
7126 * search for the zone based on the packet label.
7127 *
7128 * If there is such a zone, we prefer to find a
7129 * connection in it. Otherwise, we look for a
7130 * MAC-exempt connection in any zone whose label
7131 * dominates the default label on the packet.
7132 */
7133 if (zoneid == ALL_ZONES)
7134 zoneid = tsol_packet_to_zoneid(mp);
7135 else
7136 unlabeled = B_FALSE;
7137 }
7138
7139 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)];
7140 mutex_enter(&connfp->connf_lock);
7141 connp = connfp->connf_head;
7142 if (!broadcast && !CLASSD(dst)) {
7143 /*
7144 * Not broadcast or multicast. Send to the one (first)
7145 * client we find. No need to check conn_wantpacket()
7146 * since IP_BOUND_IF/conn_incoming_ill does not apply to
7147 * IPv4 unicast packets.
7148 */
7149 while ((connp != NULL) &&
7150 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) ||
7151 (!IPCL_ZONE_MATCH(connp, zoneid) &&
7152 !(unlabeled && connp->conn_mac_exempt)))) {
7153 /*
7154 * We keep searching since the conn did not match,
7155 * or its zone did not match and it is not either
7156 * an allzones conn or a mac exempt conn (if the
7157 * sender is unlabeled.)
7158 */
7159 connp = connp->conn_next;
7160 }
7161
7162 if (connp == NULL || connp->conn_upq == NULL)
7163 goto notfound;
7164
7165 if (is_system_labeled() &&
7166 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7167 connp))
7168 goto notfound;
7169
7170 CONN_INC_REF(connp);
7171 mutex_exit(&connfp->connf_lock);
7172 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha,
7173 flags, recv_ill, ip_policy);
7174 IP_STAT(ipst, ip_udp_fannorm);
7175 CONN_DEC_REF(connp);
7176 return;
7177 }
7178
7179 /*
7180 * Broadcast and multicast case
7181 *
7182 * Need to check conn_wantpacket().
7183 * If SO_REUSEADDR has been set on the first we send the
7184 * packet to all clients that have joined the group and
7185 * match the port.
7186 */
7187
7188 while (connp != NULL) {
7189 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) &&
7190 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7191 (!is_system_labeled() ||
7192 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7193 connp)))
7194 break;
7195 connp = connp->conn_next;
7196 }
7197
7198 if (connp == NULL || connp->conn_upq == NULL)
7199 goto notfound;
7200
7201 first_connp = connp;
7202 /*
7203 * When SO_REUSEADDR is not set, send the packet only to the first
7204 * matching connection in its zone by keeping track of the zoneid.
7205 */
7206 reuseaddr = first_connp->conn_reuseaddr;
7207 last_zoneid = first_connp->conn_zoneid;
7208
7209 CONN_INC_REF(connp);
7210 connp = connp->conn_next;
7211 for (;;) {
7212 while (connp != NULL) {
7213 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) &&
7214 (reuseaddr || connp->conn_zoneid != last_zoneid) &&
7215 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7216 (!is_system_labeled() ||
7217 tsol_receive_local(mp, &dst, IPV4_VERSION,
7218 shared_addr, connp)))
7219 break;
7220 connp = connp->conn_next;
7221 }
7222 /*
7223 * Just copy the data part alone. The mctl part is
7224 * needed just for verifying policy and it is never
7225 * sent up.
7226 */
7227 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) &&
7228 ((mp1 = copymsg(mp)) == NULL))) {
7229 /*
7230 * No more interested clients or memory
7231 * allocation failed
7232 */
7233 connp = first_connp;
7234 break;
7235 }
7236 if (connp->conn_zoneid != last_zoneid) {
7237 /*
7238 * Update the zoneid so that the packet isn't sent to
7239 * any more conns in the same zone unless SO_REUSEADDR
7240 * is set.
7241 */
7242 reuseaddr = connp->conn_reuseaddr;
7243 last_zoneid = connp->conn_zoneid;
7244 }
7245 if (first_mp != NULL) {
7246 ASSERT(((ipsec_info_t *)first_mp->b_rptr)->
7247 ipsec_info_type == IPSEC_IN);
7248 first_mp1 = ipsec_in_tag(first_mp, NULL,
7249 ipst->ips_netstack);
7250 if (first_mp1 == NULL) {
7251 freemsg(mp1);
7252 connp = first_connp;
7253 break;
7254 }
7255 } else {
7256 first_mp1 = NULL;
7257 }
7258 CONN_INC_REF(connp);
7259 mutex_exit(&connfp->connf_lock);
7260 /*
7261 * IPQoS notes: We don't send the packet for policy
7262 * processing here, will do it for the last one (below).
7263 * i.e. we do it per-packet now, but if we do policy
7264 * processing per-conn, then we would need to do it
7265 * here too.
7266 */
7267 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill,
7268 ipha, flags, recv_ill, B_FALSE);
7269 mutex_enter(&connfp->connf_lock);
7270 /* Follow the next pointer before releasing the conn. */
7271 next_connp = connp->conn_next;
7272 IP_STAT(ipst, ip_udp_fanmb);
7273 CONN_DEC_REF(connp);
7274 connp = next_connp;
7275 }
7276
7277 /* Last one. Send it upstream. */
7278 mutex_exit(&connfp->connf_lock);
7279 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags,
7280 recv_ill, ip_policy);
7281 IP_STAT(ipst, ip_udp_fanmb);
7282 CONN_DEC_REF(connp);
7283 return;
7284
7285 notfound:
7286
7287 mutex_exit(&connfp->connf_lock);
7288 IP_STAT(ipst, ip_udp_fanothers);
7289 /*
7290 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses
7291 * have already been matched above, since they live in the IPv4
7292 * fanout tables. This implies we only need to
7293 * check for IPv6 in6addr_any endpoints here.
7294 * Thus we compare using ipv6_all_zeros instead of the destination
7295 * address, except for the multicast group membership lookup which
7296 * uses the IPv4 destination.
7297 */
7298 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src);
7299 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)];
7300 mutex_enter(&connfp->connf_lock);
7301 connp = connfp->connf_head;
7302 if (!broadcast && !CLASSD(dst)) {
7303 while (connp != NULL) {
7304 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros,
7305 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) &&
7306 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7307 !connp->conn_ipv6_v6only)
7308 break;
7309 connp = connp->conn_next;
7310 }
7311
7312 if (connp != NULL && is_system_labeled() &&
7313 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7314 connp))
7315 connp = NULL;
7316
7317 if (connp == NULL || connp->conn_upq == NULL) {
7318 /*
7319 * No one bound to this port. Is
7320 * there a client that wants all
7321 * unclaimed datagrams?
7322 */
7323 mutex_exit(&connfp->connf_lock);
7324
7325 if (mctl_present)
7326 first_mp->b_cont = mp;
7327 else
7328 first_mp = mp;
7329 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].
7330 connf_head != NULL) {
7331 ip_fanout_proto(q, first_mp, ill, ipha,
7332 flags | IP_FF_RAWIP, mctl_present,
7333 ip_policy, recv_ill, zoneid);
7334 } else {
7335 if (ip_fanout_send_icmp(q, first_mp, flags,
7336 ICMP_DEST_UNREACHABLE,
7337 ICMP_PORT_UNREACHABLE,
7338 mctl_present, zoneid, ipst)) {
7339 BUMP_MIB(ill->ill_ip_mib,
7340 udpIfStatsNoPorts);
7341 }
7342 }
7343 return;
7344 }
7345
7346 CONN_INC_REF(connp);
7347 mutex_exit(&connfp->connf_lock);
7348 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha,
7349 flags, recv_ill, ip_policy);
7350 CONN_DEC_REF(connp);
7351 return;
7352 }
7353 /*
7354 * IPv4 multicast packet being delivered to an AF_INET6
7355 * in6addr_any endpoint.
7356 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
7357 * and not conn_wantpacket_v6() since any multicast membership is
7358 * for an IPv4-mapped multicast address.
7359 * The packet is sent to all clients in all zones that have joined the
7360 * group and match the port.
7361 */
7362 while (connp != NULL) {
7363 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros,
7364 srcport, v6src) &&
7365 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7366 (!is_system_labeled() ||
7367 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7368 connp)))
7369 break;
7370 connp = connp->conn_next;
7371 }
7372
7373 if (connp == NULL || connp->conn_upq == NULL) {
7374 /*
7375 * No one bound to this port. Is
7376 * there a client that wants all
7377 * unclaimed datagrams?
7378 */
7379 mutex_exit(&connfp->connf_lock);
7380
7381 if (mctl_present)
7382 first_mp->b_cont = mp;
7383 else
7384 first_mp = mp;
7385 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head !=
7386 NULL) {
7387 ip_fanout_proto(q, first_mp, ill, ipha,
7388 flags | IP_FF_RAWIP, mctl_present, ip_policy,
7389 recv_ill, zoneid);
7390 } else {
7391 /*
7392 * We used to attempt to send an icmp error here, but
7393 * since this is known to be a multicast packet
7394 * and we don't send icmp errors in response to
7395 * multicast, just drop the packet and give up sooner.
7396 */
7397 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
7398 freemsg(first_mp);
7399 }
7400 return;
7401 }
7402
7403 first_connp = connp;
7404
7405 CONN_INC_REF(connp);
7406 connp = connp->conn_next;
7407 for (;;) {
7408 while (connp != NULL) {
7409 if (IPCL_UDP_MATCH_V6(connp, dstport,
7410 ipv6_all_zeros, srcport, v6src) &&
7411 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7412 (!is_system_labeled() ||
7413 tsol_receive_local(mp, &dst, IPV4_VERSION,
7414 shared_addr, connp)))
7415 break;
7416 connp = connp->conn_next;
7417 }
7418 /*
7419 * Just copy the data part alone. The mctl part is
7420 * needed just for verifying policy and it is never
7421 * sent up.
7422 */
7423 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) &&
7424 ((mp1 = copymsg(mp)) == NULL))) {
7425 /*
7426 * No more intested clients or memory
7427 * allocation failed
7428 */
7429 connp = first_connp;
7430 break;
7431 }
7432 if (first_mp != NULL) {
7433 ASSERT(((ipsec_info_t *)first_mp->b_rptr)->
7434 ipsec_info_type == IPSEC_IN);
7435 first_mp1 = ipsec_in_tag(first_mp, NULL,
7436 ipst->ips_netstack);
7437 if (first_mp1 == NULL) {
7438 freemsg(mp1);
7439 connp = first_connp;
7440 break;
7441 }
7442 } else {
7443 first_mp1 = NULL;
7444 }
7445 CONN_INC_REF(connp);
7446 mutex_exit(&connfp->connf_lock);
7447 /*
7448 * IPQoS notes: We don't send the packet for policy
7449 * processing here, will do it for the last one (below).
7450 * i.e. we do it per-packet now, but if we do policy
7451 * processing per-conn, then we would need to do it
7452 * here too.
7453 */
7454 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill,
7455 ipha, flags, recv_ill, B_FALSE);
7456 mutex_enter(&connfp->connf_lock);
7457 /* Follow the next pointer before releasing the conn. */
7458 next_connp = connp->conn_next;
7459 CONN_DEC_REF(connp);
7460 connp = next_connp;
7461 }
7462
7463 /* Last one. Send it upstream. */
7464 mutex_exit(&connfp->connf_lock);
7465 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags,
7466 recv_ill, ip_policy);
7467 CONN_DEC_REF(connp);
7468 }
7469
7470 /*
7471 * Complete the ip_wput header so that it
7472 * is possible to generate ICMP
7473 * errors.
7474 */
7475 int
7476 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst)
7477 {
7478 ire_t *ire;
7479
7480 if (ipha->ipha_src == INADDR_ANY) {
7481 ire = ire_lookup_local(zoneid, ipst);
7482 if (ire == NULL) {
7483 ip1dbg(("ip_hdr_complete: no source IRE\n"));
7484 return (1);
7485 }
7486 ipha->ipha_src = ire->ire_addr;
7487 ire_refrele(ire);
7488 }
7489 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
7490 ipha->ipha_hdr_checksum = 0;
7491 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
7492 return (0);
7493 }
7494
7495 /*
7496 * Nobody should be sending
7497 * packets up this stream
7498 */
7499 static void
7500 ip_lrput(queue_t *q, mblk_t *mp)
7501 {
7502 mblk_t *mp1;
7503
7504 switch (mp->b_datap->db_type) {
7505 case M_FLUSH:
7506 /* Turn around */
7507 if (*mp->b_rptr & FLUSHW) {
7508 *mp->b_rptr &= ~FLUSHR;
7509 qreply(q, mp);
7510 return;
7511 }
7512 break;
7513 }
7514 /* Could receive messages that passed through ar_rput */
7515 for (mp1 = mp; mp1; mp1 = mp1->b_cont)
7516 mp1->b_prev = mp1->b_next = NULL;
7517 freemsg(mp);
7518 }
7519
7520 /* Nobody should be sending packets down this stream */
7521 /* ARGSUSED */
7522 void
7523 ip_lwput(queue_t *q, mblk_t *mp)
7524 {
7525 freemsg(mp);
7526 }
7527
7528 /*
7529 * Move the first hop in any source route to ipha_dst and remove that part of
7530 * the source route. Called by other protocols. Errors in option formatting
7531 * are ignored - will be handled by ip_wput_options Return the final
7532 * destination (either ipha_dst or the last entry in a source route.)
7533 */
7534 ipaddr_t
7535 ip_massage_options(ipha_t *ipha, netstack_t *ns)
7536 {
7537 ipoptp_t opts;
7538 uchar_t *opt;
7539 uint8_t optval;
7540 uint8_t optlen;
7541 ipaddr_t dst;
7542 int i;
7543 ire_t *ire;
7544 ip_stack_t *ipst = ns->netstack_ip;
7545
7546 ip2dbg(("ip_massage_options\n"));
7547 dst = ipha->ipha_dst;
7548 for (optval = ipoptp_first(&opts, ipha);
7549 optval != IPOPT_EOL;
7550 optval = ipoptp_next(&opts)) {
7551 opt = opts.ipoptp_cur;
7552 switch (optval) {
7553 uint8_t off;
7554 case IPOPT_SSRR:
7555 case IPOPT_LSRR:
7556 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
7557 ip1dbg(("ip_massage_options: bad src route\n"));
7558 break;
7559 }
7560 optlen = opts.ipoptp_len;
7561 off = opt[IPOPT_OFFSET];
7562 off--;
7563 redo_srr:
7564 if (optlen < IP_ADDR_LEN ||
7565 off > optlen - IP_ADDR_LEN) {
7566 /* End of source route */
7567 ip1dbg(("ip_massage_options: end of SR\n"));
7568 break;
7569 }
7570 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
7571 ip1dbg(("ip_massage_options: next hop 0x%x\n",
7572 ntohl(dst)));
7573 /*
7574 * Check if our address is present more than
7575 * once as consecutive hops in source route.
7576 * XXX verify per-interface ip_forwarding
7577 * for source route?
7578 */
7579 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL,
7580 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7581 if (ire != NULL) {
7582 ire_refrele(ire);
7583 off += IP_ADDR_LEN;
7584 goto redo_srr;
7585 }
7586 if (dst == htonl(INADDR_LOOPBACK)) {
7587 ip1dbg(("ip_massage_options: loopback addr in "
7588 "source route!\n"));
7589 break;
7590 }
7591 /*
7592 * Update ipha_dst to be the first hop and remove the
7593 * first hop from the source route (by overwriting
7594 * part of the option with NOP options).
7595 */
7596 ipha->ipha_dst = dst;
7597 /* Put the last entry in dst */
7598 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
7599 3;
7600 bcopy(&opt[off], &dst, IP_ADDR_LEN);
7601
7602 ip1dbg(("ip_massage_options: last hop 0x%x\n",
7603 ntohl(dst)));
7604 /* Move down and overwrite */
7605 opt[IP_ADDR_LEN] = opt[0];
7606 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
7607 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
7608 for (i = 0; i < IP_ADDR_LEN; i++)
7609 opt[i] = IPOPT_NOP;
7610 break;
7611 }
7612 }
7613 return (dst);
7614 }
7615
7616 /*
7617 * Return the network mask
7618 * associated with the specified address.
7619 */
7620 ipaddr_t
7621 ip_net_mask(ipaddr_t addr)
7622 {
7623 uchar_t *up = (uchar_t *)&addr;
7624 ipaddr_t mask = 0;
7625 uchar_t *maskp = (uchar_t *)&mask;
7626
7627 #if defined(__i386) || defined(__amd64)
7628 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
7629 #endif
7630 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
7631 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
7632 #endif
7633 if (CLASSD(addr)) {
7634 maskp[0] = 0xF0;
7635 return (mask);
7636 }
7637
7638 /* We assume Class E default netmask to be 32 */
7639 if (CLASSE(addr))
7640 return (0xffffffffU);
7641
7642 if (addr == 0)
7643 return (0);
7644 maskp[0] = 0xFF;
7645 if ((up[0] & 0x80) == 0)
7646 return (mask);
7647
7648 maskp[1] = 0xFF;
7649 if ((up[0] & 0xC0) == 0x80)
7650 return (mask);
7651
7652 maskp[2] = 0xFF;
7653 if ((up[0] & 0xE0) == 0xC0)
7654 return (mask);
7655
7656 /* Otherwise return no mask */
7657 return ((ipaddr_t)0);
7658 }
7659
7660 /*
7661 * Select an ill for the packet by considering load spreading across
7662 * a different ill in the group if dst_ill is part of some group.
7663 */
7664 ill_t *
7665 ip_newroute_get_dst_ill(ill_t *dst_ill)
7666 {
7667 ill_t *ill;
7668
7669 /*
7670 * We schedule irrespective of whether the source address is
7671 * INADDR_ANY or not. illgrp_scheduler returns a held ill.
7672 */
7673 ill = illgrp_scheduler(dst_ill);
7674 if (ill == NULL)
7675 return (NULL);
7676
7677 /*
7678 * For groups with names ip_sioctl_groupname ensures that all
7679 * ills are of same type. For groups without names, ifgrp_insert
7680 * ensures this.
7681 */
7682 ASSERT(dst_ill->ill_type == ill->ill_type);
7683
7684 return (ill);
7685 }
7686
7687 /*
7688 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case.
7689 */
7690 ill_t *
7691 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6,
7692 ip_stack_t *ipst)
7693 {
7694 ill_t *ret_ill;
7695
7696 ASSERT(ifindex != 0);
7697 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
7698 ipst);
7699 if (ret_ill == NULL ||
7700 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) {
7701 if (isv6) {
7702 if (ill != NULL) {
7703 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
7704 } else {
7705 BUMP_MIB(&ipst->ips_ip6_mib,
7706 ipIfStatsOutDiscards);
7707 }
7708 ip1dbg(("ip_grab_attach_ill (IPv6): "
7709 "bad ifindex %d.\n", ifindex));
7710 } else {
7711 if (ill != NULL) {
7712 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
7713 } else {
7714 BUMP_MIB(&ipst->ips_ip_mib,
7715 ipIfStatsOutDiscards);
7716 }
7717 ip1dbg(("ip_grab_attach_ill (IPv4): "
7718 "bad ifindex %d.\n", ifindex));
7719 }
7720 if (ret_ill != NULL)
7721 ill_refrele(ret_ill);
7722 freemsg(first_mp);
7723 return (NULL);
7724 }
7725
7726 return (ret_ill);
7727 }
7728
7729 /*
7730 * IPv4 -
7731 * ip_newroute is called by ip_rput or ip_wput whenever we need to send
7732 * out a packet to a destination address for which we do not have specific
7733 * (or sufficient) routing information.
7734 *
7735 * NOTE : These are the scopes of some of the variables that point at IRE,
7736 * which needs to be followed while making any future modifications
7737 * to avoid memory leaks.
7738 *
7739 * - ire and sire are the entries looked up initially by
7740 * ire_ftable_lookup.
7741 * - ipif_ire is used to hold the interface ire associated with
7742 * the new cache ire. But it's scope is limited, so we always REFRELE
7743 * it before branching out to error paths.
7744 * - save_ire is initialized before ire_create, so that ire returned
7745 * by ire_create will not over-write the ire. We REFRELE save_ire
7746 * before breaking out of the switch.
7747 *
7748 * Thus on failures, we have to REFRELE only ire and sire, if they
7749 * are not NULL.
7750 */
7751 void
7752 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, conn_t *connp,
7753 zoneid_t zoneid, ip_stack_t *ipst)
7754 {
7755 areq_t *areq;
7756 ipaddr_t gw = 0;
7757 ire_t *ire = NULL;
7758 mblk_t *res_mp;
7759 ipaddr_t *addrp;
7760 ipaddr_t nexthop_addr;
7761 ipif_t *src_ipif = NULL;
7762 ill_t *dst_ill = NULL;
7763 ipha_t *ipha;
7764 ire_t *sire = NULL;
7765 mblk_t *first_mp;
7766 ire_t *save_ire;
7767 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */
7768 ushort_t ire_marks = 0;
7769 boolean_t mctl_present;
7770 ipsec_out_t *io;
7771 mblk_t *saved_mp;
7772 ire_t *first_sire = NULL;
7773 mblk_t *copy_mp = NULL;
7774 mblk_t *xmit_mp = NULL;
7775 ipaddr_t save_dst;
7776 uint32_t multirt_flags =
7777 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP;
7778 boolean_t multirt_is_resolvable;
7779 boolean_t multirt_resolve_next;
7780 boolean_t unspec_src;
7781 boolean_t do_attach_ill = B_FALSE;
7782 boolean_t ip_nexthop = B_FALSE;
7783 tsol_ire_gw_secattr_t *attrp = NULL;
7784 tsol_gcgrp_t *gcgrp = NULL;
7785 tsol_gcgrp_addr_t ga;
7786
7787 if (ip_debug > 2) {
7788 /* ip1dbg */
7789 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst);
7790 }
7791
7792 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
7793 if (mctl_present) {
7794 io = (ipsec_out_t *)first_mp->b_rptr;
7795 ASSERT(io->ipsec_out_type == IPSEC_OUT);
7796 ASSERT(zoneid == io->ipsec_out_zoneid);
7797 ASSERT(zoneid != ALL_ZONES);
7798 }
7799
7800 ipha = (ipha_t *)mp->b_rptr;
7801
7802 /* All multicast lookups come through ip_newroute_ipif() */
7803 if (CLASSD(dst)) {
7804 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n",
7805 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next));
7806 freemsg(first_mp);
7807 return;
7808 }
7809
7810 if (mctl_present && io->ipsec_out_attach_if) {
7811 /* ip_grab_attach_ill returns a held ill */
7812 attach_ill = ip_grab_attach_ill(NULL, first_mp,
7813 io->ipsec_out_ill_index, B_FALSE, ipst);
7814
7815 /* Failure case frees things for us. */
7816 if (attach_ill == NULL)
7817 return;
7818
7819 /*
7820 * Check if we need an ire that will not be
7821 * looked up by anybody else i.e. HIDDEN.
7822 */
7823 if (ill_is_probeonly(attach_ill))
7824 ire_marks = IRE_MARK_HIDDEN;
7825 }
7826 if (mctl_present && io->ipsec_out_ip_nexthop) {
7827 ip_nexthop = B_TRUE;
7828 nexthop_addr = io->ipsec_out_nexthop_addr;
7829 }
7830 /*
7831 * If this IRE is created for forwarding or it is not for
7832 * traffic for congestion controlled protocols, mark it as temporary.
7833 */
7834 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol))
7835 ire_marks |= IRE_MARK_TEMPORARY;
7836
7837 /*
7838 * Get what we can from ire_ftable_lookup which will follow an IRE
7839 * chain until it gets the most specific information available.
7840 * For example, we know that there is no IRE_CACHE for this dest,
7841 * but there may be an IRE_OFFSUBNET which specifies a gateway.
7842 * ire_ftable_lookup will look up the gateway, etc.
7843 * Otherwise, given ire_ftable_lookup algorithm, only one among routes
7844 * to the destination, of equal netmask length in the forward table,
7845 * will be recursively explored. If no information is available
7846 * for the final gateway of that route, we force the returned ire
7847 * to be equal to sire using MATCH_IRE_PARENT.
7848 * At least, in this case we have a starting point (in the buckets)
7849 * to look for other routes to the destination in the forward table.
7850 * This is actually used only for multirouting, where a list
7851 * of routes has to be processed in sequence.
7852 *
7853 * In the process of coming up with the most specific information,
7854 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry
7855 * for the gateway (i.e., one for which the ire_nce->nce_state is
7856 * not yet ND_REACHABLE, and is in the middle of arp resolution).
7857 * Two caveats when handling incomplete ire's in ip_newroute:
7858 * - we should be careful when accessing its ire_nce (specifically
7859 * the nce_res_mp) ast it might change underneath our feet, and,
7860 * - not all legacy code path callers are prepared to handle
7861 * incomplete ire's, so we should not create/add incomplete
7862 * ire_cache entries here. (See discussion about temporary solution
7863 * further below).
7864 *
7865 * In order to minimize packet dropping, and to preserve existing
7866 * behavior, we treat this case as if there were no IRE_CACHE for the
7867 * gateway, and instead use the IF_RESOLVER ire to send out
7868 * another request to ARP (this is achieved by passing the
7869 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the
7870 * arp response comes back in ip_wput_nondata, we will create
7871 * a per-dst ire_cache that has an ND_COMPLETE ire.
7872 *
7873 * Note that this is a temporary solution; the correct solution is
7874 * to create an incomplete per-dst ire_cache entry, and send the
7875 * packet out when the gw's nce is resolved. In order to achieve this,
7876 * all packet processing must have been completed prior to calling
7877 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need
7878 * to be modified to accomodate this solution.
7879 */
7880 if (ip_nexthop) {
7881 /*
7882 * The first time we come here, we look for an IRE_INTERFACE
7883 * entry for the specified nexthop, set the dst to be the
7884 * nexthop address and create an IRE_CACHE entry for the
7885 * nexthop. The next time around, we are able to find an
7886 * IRE_CACHE entry for the nexthop, set the gateway to be the
7887 * nexthop address and create an IRE_CACHE entry for the
7888 * destination address via the specified nexthop.
7889 */
7890 ire = ire_cache_lookup(nexthop_addr, zoneid,
7891 MBLK_GETLABEL(mp), ipst);
7892 if (ire != NULL) {
7893 gw = nexthop_addr;
7894 ire_marks |= IRE_MARK_PRIVATE_ADDR;
7895 } else {
7896 ire = ire_ftable_lookup(nexthop_addr, 0, 0,
7897 IRE_INTERFACE, NULL, NULL, zoneid, 0,
7898 MBLK_GETLABEL(mp),
7899 MATCH_IRE_TYPE | MATCH_IRE_SECATTR,
7900 ipst);
7901 if (ire != NULL) {
7902 dst = nexthop_addr;
7903 }
7904 }
7905 } else if (attach_ill == NULL) {
7906 ire = ire_ftable_lookup(dst, 0, 0, 0,
7907 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp),
7908 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
7909 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT |
7910 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE,
7911 ipst);
7912 } else {
7913 /*
7914 * attach_ill is set only for communicating with
7915 * on-link hosts. So, don't look for DEFAULT.
7916 */
7917 ipif_t *attach_ipif;
7918
7919 attach_ipif = ipif_get_next_ipif(NULL, attach_ill);
7920 if (attach_ipif == NULL) {
7921 ill_refrele(attach_ill);
7922 goto icmp_err_ret;
7923 }
7924 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif,
7925 &sire, zoneid, 0, MBLK_GETLABEL(mp),
7926 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL |
7927 MATCH_IRE_SECATTR, ipst);
7928 ipif_refrele(attach_ipif);
7929 }
7930 ip3dbg(("ip_newroute: ire_ftable_lookup() "
7931 "returned ire %p, sire %p\n", (void *)ire, (void *)sire));
7932
7933 /*
7934 * This loop is run only once in most cases.
7935 * We loop to resolve further routes only when the destination
7936 * can be reached through multiple RTF_MULTIRT-flagged ires.
7937 */
7938 do {
7939 /* Clear the previous iteration's values */
7940 if (src_ipif != NULL) {
7941 ipif_refrele(src_ipif);
7942 src_ipif = NULL;
7943 }
7944 if (dst_ill != NULL) {
7945 ill_refrele(dst_ill);
7946 dst_ill = NULL;
7947 }
7948
7949 multirt_resolve_next = B_FALSE;
7950 /*
7951 * We check if packets have to be multirouted.
7952 * In this case, given the current <ire, sire> couple,
7953 * we look for the next suitable <ire, sire>.
7954 * This check is done in ire_multirt_lookup(),
7955 * which applies various criteria to find the next route
7956 * to resolve. ire_multirt_lookup() leaves <ire, sire>
7957 * unchanged if it detects it has not been tried yet.
7958 */
7959 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
7960 ip3dbg(("ip_newroute: starting next_resolution "
7961 "with first_mp %p, tag %d\n",
7962 (void *)first_mp,
7963 MULTIRT_DEBUG_TAGGED(first_mp)));
7964
7965 ASSERT(sire != NULL);
7966 multirt_is_resolvable =
7967 ire_multirt_lookup(&ire, &sire, multirt_flags,
7968 MBLK_GETLABEL(mp), ipst);
7969
7970 ip3dbg(("ip_newroute: multirt_is_resolvable %d, "
7971 "ire %p, sire %p\n",
7972 multirt_is_resolvable,
7973 (void *)ire, (void *)sire));
7974
7975 if (!multirt_is_resolvable) {
7976 /*
7977 * No more multirt route to resolve; give up
7978 * (all routes resolved or no more
7979 * resolvable routes).
7980 */
7981 if (ire != NULL) {
7982 ire_refrele(ire);
7983 ire = NULL;
7984 }
7985 } else {
7986 ASSERT(sire != NULL);
7987 ASSERT(ire != NULL);
7988 /*
7989 * We simply use first_sire as a flag that
7990 * indicates if a resolvable multirt route
7991 * has already been found.
7992 * If it is not the case, we may have to send
7993 * an ICMP error to report that the
7994 * destination is unreachable.
7995 * We do not IRE_REFHOLD first_sire.
7996 */
7997 if (first_sire == NULL) {
7998 first_sire = sire;
7999 }
8000 }
8001 }
8002 if (ire == NULL) {
8003 if (ip_debug > 3) {
8004 /* ip2dbg */
8005 pr_addr_dbg("ip_newroute: "
8006 "can't resolve %s\n", AF_INET, &dst);
8007 }
8008 ip3dbg(("ip_newroute: "
8009 "ire %p, sire %p, first_sire %p\n",
8010 (void *)ire, (void *)sire, (void *)first_sire));
8011
8012 if (sire != NULL) {
8013 ire_refrele(sire);
8014 sire = NULL;
8015 }
8016
8017 if (first_sire != NULL) {
8018 /*
8019 * At least one multirt route has been found
8020 * in the same call to ip_newroute();
8021 * there is no need to report an ICMP error.
8022 * first_sire was not IRE_REFHOLDed.
8023 */
8024 MULTIRT_DEBUG_UNTAG(first_mp);
8025 freemsg(first_mp);
8026 return;
8027 }
8028 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0,
8029 RTA_DST, ipst);
8030 if (attach_ill != NULL)
8031 ill_refrele(attach_ill);
8032 goto icmp_err_ret;
8033 }
8034
8035 /*
8036 * Verify that the returned IRE does not have either
8037 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
8038 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
8039 */
8040 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
8041 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
8042 if (attach_ill != NULL)
8043 ill_refrele(attach_ill);
8044 goto icmp_err_ret;
8045 }
8046 /*
8047 * Increment the ire_ob_pkt_count field for ire if it is an
8048 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
8049 * increment the same for the parent IRE, sire, if it is some
8050 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST)
8051 */
8052 if ((ire->ire_type & IRE_INTERFACE) != 0) {
8053 UPDATE_OB_PKT_COUNT(ire);
8054 ire->ire_last_used_time = lbolt;
8055 }
8056
8057 if (sire != NULL) {
8058 gw = sire->ire_gateway_addr;
8059 ASSERT((sire->ire_type & (IRE_CACHETABLE |
8060 IRE_INTERFACE)) == 0);
8061 UPDATE_OB_PKT_COUNT(sire);
8062 sire->ire_last_used_time = lbolt;
8063 }
8064 /*
8065 * We have a route to reach the destination.
8066 *
8067 * 1) If the interface is part of ill group, try to get a new
8068 * ill taking load spreading into account.
8069 *
8070 * 2) After selecting the ill, get a source address that
8071 * might create good inbound load spreading.
8072 * ipif_select_source does this for us.
8073 *
8074 * If the application specified the ill (ifindex), we still
8075 * load spread. Only if the packets needs to go out
8076 * specifically on a given ill e.g. binding to
8077 * IPIF_NOFAILOVER address, then we don't try to use a
8078 * different ill for load spreading.
8079 */
8080 if (attach_ill == NULL) {
8081 /*
8082 * Don't perform outbound load spreading in the
8083 * case of an RTF_MULTIRT route, as we actually
8084 * typically want to replicate outgoing packets
8085 * through particular interfaces.
8086 */
8087 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
8088 dst_ill = ire->ire_ipif->ipif_ill;
8089 /* for uniformity */
8090 ill_refhold(dst_ill);
8091 } else {
8092 /*
8093 * If we are here trying to create an IRE_CACHE
8094 * for an offlink destination and have the
8095 * IRE_CACHE for the next hop and the latter is
8096 * using virtual IP source address selection i.e
8097 * it's ire->ire_ipif is pointing to a virtual
8098 * network interface (vni) then
8099 * ip_newroute_get_dst_ll() will return the vni
8100 * interface as the dst_ill. Since the vni is
8101 * virtual i.e not associated with any physical
8102 * interface, it cannot be the dst_ill, hence
8103 * in such a case call ip_newroute_get_dst_ll()
8104 * with the stq_ill instead of the ire_ipif ILL.
8105 * The function returns a refheld ill.
8106 */
8107 if ((ire->ire_type == IRE_CACHE) &&
8108 IS_VNI(ire->ire_ipif->ipif_ill))
8109 dst_ill = ip_newroute_get_dst_ill(
8110 ire->ire_stq->q_ptr);
8111 else
8112 dst_ill = ip_newroute_get_dst_ill(
8113 ire->ire_ipif->ipif_ill);
8114 }
8115 if (dst_ill == NULL) {
8116 if (ip_debug > 2) {
8117 pr_addr_dbg("ip_newroute: "
8118 "no dst ill for dst"
8119 " %s\n", AF_INET, &dst);
8120 }
8121 goto icmp_err_ret;
8122 }
8123 } else {
8124 dst_ill = ire->ire_ipif->ipif_ill;
8125 /* for uniformity */
8126 ill_refhold(dst_ill);
8127 /*
8128 * We should have found a route matching ill as we
8129 * called ire_ftable_lookup with MATCH_IRE_ILL.
8130 * Rather than asserting, when there is a mismatch,
8131 * we just drop the packet.
8132 */
8133 if (dst_ill != attach_ill) {
8134 ip0dbg(("ip_newroute: Packet dropped as "
8135 "IPIF_NOFAILOVER ill is %s, "
8136 "ire->ire_ipif->ipif_ill is %s\n",
8137 attach_ill->ill_name,
8138 dst_ill->ill_name));
8139 ill_refrele(attach_ill);
8140 goto icmp_err_ret;
8141 }
8142 }
8143 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */
8144 if (attach_ill != NULL) {
8145 ill_refrele(attach_ill);
8146 attach_ill = NULL;
8147 do_attach_ill = B_TRUE;
8148 }
8149 ASSERT(dst_ill != NULL);
8150 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name));
8151
8152 /*
8153 * Pick the best source address from dst_ill.
8154 *
8155 * 1) If it is part of a multipathing group, we would
8156 * like to spread the inbound packets across different
8157 * interfaces. ipif_select_source picks a random source
8158 * across the different ills in the group.
8159 *
8160 * 2) If it is not part of a multipathing group, we try
8161 * to pick the source address from the destination
8162 * route. Clustering assumes that when we have multiple
8163 * prefixes hosted on an interface, the prefix of the
8164 * source address matches the prefix of the destination
8165 * route. We do this only if the address is not
8166 * DEPRECATED.
8167 *
8168 * 3) If the conn is in a different zone than the ire, we
8169 * need to pick a source address from the right zone.
8170 *
8171 * NOTE : If we hit case (1) above, the prefix of the source
8172 * address picked may not match the prefix of the
8173 * destination routes prefix as ipif_select_source
8174 * does not look at "dst" while picking a source
8175 * address.
8176 * If we want the same behavior as (2), we will need
8177 * to change the behavior of ipif_select_source.
8178 */
8179 ASSERT(src_ipif == NULL);
8180 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
8181 /*
8182 * The RTF_SETSRC flag is set in the parent ire (sire).
8183 * Check that the ipif matching the requested source
8184 * address still exists.
8185 */
8186 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
8187 zoneid, NULL, NULL, NULL, NULL, ipst);
8188 }
8189
8190 unspec_src = (connp != NULL && connp->conn_unspec_src);
8191
8192 if (src_ipif == NULL &&
8193 (!unspec_src || ipha->ipha_src != INADDR_ANY)) {
8194 ire_marks |= IRE_MARK_USESRC_CHECK;
8195 if ((dst_ill->ill_group != NULL) ||
8196 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
8197 (connp != NULL && ire->ire_zoneid != zoneid &&
8198 ire->ire_zoneid != ALL_ZONES) ||
8199 (dst_ill->ill_usesrc_ifindex != 0)) {
8200 /*
8201 * If the destination is reachable via a
8202 * given gateway, the selected source address
8203 * should be in the same subnet as the gateway.
8204 * Otherwise, the destination is not reachable.
8205 *
8206 * If there are no interfaces on the same subnet
8207 * as the destination, ipif_select_source gives
8208 * first non-deprecated interface which might be
8209 * on a different subnet than the gateway.
8210 * This is not desirable. Hence pass the dst_ire
8211 * source address to ipif_select_source.
8212 * It is sure that the destination is reachable
8213 * with the dst_ire source address subnet.
8214 * So passing dst_ire source address to
8215 * ipif_select_source will make sure that the
8216 * selected source will be on the same subnet
8217 * as dst_ire source address.
8218 */
8219 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr;
8220 src_ipif = ipif_select_source(dst_ill, saddr,
8221 zoneid);
8222 if (src_ipif == NULL) {
8223 if (ip_debug > 2) {
8224 pr_addr_dbg("ip_newroute: "
8225 "no src for dst %s ",
8226 AF_INET, &dst);
8227 printf("through interface %s\n",
8228 dst_ill->ill_name);
8229 }
8230 goto icmp_err_ret;
8231 }
8232 } else {
8233 src_ipif = ire->ire_ipif;
8234 ASSERT(src_ipif != NULL);
8235 /* hold src_ipif for uniformity */
8236 ipif_refhold(src_ipif);
8237 }
8238 }
8239
8240 /*
8241 * Assign a source address while we have the conn.
8242 * We can't have ip_wput_ire pick a source address when the
8243 * packet returns from arp since we need to look at
8244 * conn_unspec_src and conn_zoneid, and we lose the conn when
8245 * going through arp.
8246 *
8247 * NOTE : ip_newroute_v6 does not have this piece of code as
8248 * it uses ip6i to store this information.
8249 */
8250 if (ipha->ipha_src == INADDR_ANY && !unspec_src)
8251 ipha->ipha_src = src_ipif->ipif_src_addr;
8252
8253 if (ip_debug > 3) {
8254 /* ip2dbg */
8255 pr_addr_dbg("ip_newroute: first hop %s\n",
8256 AF_INET, &gw);
8257 }
8258 ip2dbg(("\tire type %s (%d)\n",
8259 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type));
8260
8261 /*
8262 * The TTL of multirouted packets is bounded by the
8263 * ip_multirt_ttl ndd variable.
8264 */
8265 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
8266 /* Force TTL of multirouted packets */
8267 if ((ipst->ips_ip_multirt_ttl > 0) &&
8268 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) {
8269 ip2dbg(("ip_newroute: forcing multirt TTL "
8270 "to %d (was %d), dst 0x%08x\n",
8271 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl,
8272 ntohl(sire->ire_addr)));
8273 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl;
8274 }
8275 }
8276 /*
8277 * At this point in ip_newroute(), ire is either the
8278 * IRE_CACHE of the next-hop gateway for an off-subnet
8279 * destination or an IRE_INTERFACE type that should be used
8280 * to resolve an on-subnet destination or an on-subnet
8281 * next-hop gateway.
8282 *
8283 * In the IRE_CACHE case, we have the following :
8284 *
8285 * 1) src_ipif - used for getting a source address.
8286 *
8287 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
8288 * means packets using this IRE_CACHE will go out on
8289 * dst_ill.
8290 *
8291 * 3) The IRE sire will point to the prefix that is the
8292 * longest matching route for the destination. These
8293 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST.
8294 *
8295 * The newly created IRE_CACHE entry for the off-subnet
8296 * destination is tied to both the prefix route and the
8297 * interface route used to resolve the next-hop gateway
8298 * via the ire_phandle and ire_ihandle fields,
8299 * respectively.
8300 *
8301 * In the IRE_INTERFACE case, we have the following :
8302 *
8303 * 1) src_ipif - used for getting a source address.
8304 *
8305 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
8306 * means packets using the IRE_CACHE that we will build
8307 * here will go out on dst_ill.
8308 *
8309 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
8310 * to be created will only be tied to the IRE_INTERFACE
8311 * that was derived from the ire_ihandle field.
8312 *
8313 * If sire is non-NULL, it means the destination is
8314 * off-link and we will first create the IRE_CACHE for the
8315 * gateway. Next time through ip_newroute, we will create
8316 * the IRE_CACHE for the final destination as described
8317 * above.
8318 *
8319 * In both cases, after the current resolution has been
8320 * completed (or possibly initialised, in the IRE_INTERFACE
8321 * case), the loop may be re-entered to attempt the resolution
8322 * of another RTF_MULTIRT route.
8323 *
8324 * When an IRE_CACHE entry for the off-subnet destination is
8325 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire,
8326 * for further processing in emission loops.
8327 */
8328 save_ire = ire;
8329 switch (ire->ire_type) {
8330 case IRE_CACHE: {
8331 ire_t *ipif_ire;
8332
8333 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE);
8334 if (gw == 0)
8335 gw = ire->ire_gateway_addr;
8336 /*
8337 * We need 3 ire's to create a new cache ire for an
8338 * off-link destination from the cache ire of the
8339 * gateway.
8340 *
8341 * 1. The prefix ire 'sire' (Note that this does
8342 * not apply to the conn_nexthop_set case)
8343 * 2. The cache ire of the gateway 'ire'
8344 * 3. The interface ire 'ipif_ire'
8345 *
8346 * We have (1) and (2). We lookup (3) below.
8347 *
8348 * If there is no interface route to the gateway,
8349 * it is a race condition, where we found the cache
8350 * but the interface route has been deleted.
8351 */
8352 if (ip_nexthop) {
8353 ipif_ire = ire_ihandle_lookup_onlink(ire);
8354 } else {
8355 ipif_ire =
8356 ire_ihandle_lookup_offlink(ire, sire);
8357 }
8358 if (ipif_ire == NULL) {
8359 ip1dbg(("ip_newroute: "
8360 "ire_ihandle_lookup_offlink failed\n"));
8361 goto icmp_err_ret;
8362 }
8363
8364 /*
8365 * Check cached gateway IRE for any security
8366 * attributes; if found, associate the gateway
8367 * credentials group to the destination IRE.
8368 */
8369 if ((attrp = save_ire->ire_gw_secattr) != NULL) {
8370 mutex_enter(&attrp->igsa_lock);
8371 if ((gcgrp = attrp->igsa_gcgrp) != NULL)
8372 GCGRP_REFHOLD(gcgrp);
8373 mutex_exit(&attrp->igsa_lock);
8374 }
8375
8376 /*
8377 * XXX For the source of the resolver mp,
8378 * we are using the same DL_UNITDATA_REQ
8379 * (from save_ire->ire_nce->nce_res_mp)
8380 * though the save_ire is not pointing at the same ill.
8381 * This is incorrect. We need to send it up to the
8382 * resolver to get the right res_mp. For ethernets
8383 * this may be okay (ill_type == DL_ETHER).
8384 */
8385
8386 ire = ire_create(
8387 (uchar_t *)&dst, /* dest address */
8388 (uchar_t *)&ip_g_all_ones, /* mask */
8389 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8390 (uchar_t *)&gw, /* gateway address */
8391 &save_ire->ire_max_frag,
8392 save_ire->ire_nce, /* src nce */
8393 dst_ill->ill_rq, /* recv-from queue */
8394 dst_ill->ill_wq, /* send-to queue */
8395 IRE_CACHE, /* IRE type */
8396 src_ipif,
8397 (sire != NULL) ?
8398 sire->ire_mask : 0, /* Parent mask */
8399 (sire != NULL) ?
8400 sire->ire_phandle : 0, /* Parent handle */
8401 ipif_ire->ire_ihandle, /* Interface handle */
8402 (sire != NULL) ? (sire->ire_flags &
8403 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */
8404 (sire != NULL) ?
8405 &(sire->ire_uinfo) : &(save_ire->ire_uinfo),
8406 NULL,
8407 gcgrp,
8408 ipst);
8409
8410 if (ire == NULL) {
8411 if (gcgrp != NULL) {
8412 GCGRP_REFRELE(gcgrp);
8413 gcgrp = NULL;
8414 }
8415 ire_refrele(ipif_ire);
8416 ire_refrele(save_ire);
8417 break;
8418 }
8419
8420 /* reference now held by IRE */
8421 gcgrp = NULL;
8422
8423 ire->ire_marks |= ire_marks;
8424
8425 /*
8426 * Prevent sire and ipif_ire from getting deleted.
8427 * The newly created ire is tied to both of them via
8428 * the phandle and ihandle respectively.
8429 */
8430 if (sire != NULL) {
8431 IRB_REFHOLD(sire->ire_bucket);
8432 /* Has it been removed already ? */
8433 if (sire->ire_marks & IRE_MARK_CONDEMNED) {
8434 IRB_REFRELE(sire->ire_bucket);
8435 ire_refrele(ipif_ire);
8436 ire_refrele(save_ire);
8437 break;
8438 }
8439 }
8440
8441 IRB_REFHOLD(ipif_ire->ire_bucket);
8442 /* Has it been removed already ? */
8443 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) {
8444 IRB_REFRELE(ipif_ire->ire_bucket);
8445 if (sire != NULL)
8446 IRB_REFRELE(sire->ire_bucket);
8447 ire_refrele(ipif_ire);
8448 ire_refrele(save_ire);
8449 break;
8450 }
8451
8452 xmit_mp = first_mp;
8453 /*
8454 * In the case of multirouting, a copy
8455 * of the packet is done before its sending.
8456 * The copy is used to attempt another
8457 * route resolution, in a next loop.
8458 */
8459 if (ire->ire_flags & RTF_MULTIRT) {
8460 copy_mp = copymsg(first_mp);
8461 if (copy_mp != NULL) {
8462 xmit_mp = copy_mp;
8463 MULTIRT_DEBUG_TAG(first_mp);
8464 }
8465 }
8466 ire_add_then_send(q, ire, xmit_mp);
8467 ire_refrele(save_ire);
8468
8469 /* Assert that sire is not deleted yet. */
8470 if (sire != NULL) {
8471 ASSERT(sire->ire_ptpn != NULL);
8472 IRB_REFRELE(sire->ire_bucket);
8473 }
8474
8475 /* Assert that ipif_ire is not deleted yet. */
8476 ASSERT(ipif_ire->ire_ptpn != NULL);
8477 IRB_REFRELE(ipif_ire->ire_bucket);
8478 ire_refrele(ipif_ire);
8479
8480 /*
8481 * If copy_mp is not NULL, multirouting was
8482 * requested. We loop to initiate a next
8483 * route resolution attempt, starting from sire.
8484 */
8485 if (copy_mp != NULL) {
8486 /*
8487 * Search for the next unresolved
8488 * multirt route.
8489 */
8490 copy_mp = NULL;
8491 ipif_ire = NULL;
8492 ire = NULL;
8493 multirt_resolve_next = B_TRUE;
8494 continue;
8495 }
8496 if (sire != NULL)
8497 ire_refrele(sire);
8498 ipif_refrele(src_ipif);
8499 ill_refrele(dst_ill);
8500 return;
8501 }
8502 case IRE_IF_NORESOLVER: {
8503
8504 if (dst_ill->ill_phys_addr_length != IP_ADDR_LEN &&
8505 dst_ill->ill_resolver_mp == NULL) {
8506 ip1dbg(("ip_newroute: dst_ill %p "
8507 "for IRE_IF_NORESOLVER ire %p has "
8508 "no ill_resolver_mp\n",
8509 (void *)dst_ill, (void *)ire));
8510 break;
8511 }
8512
8513 /*
8514 * TSol note: We are creating the ire cache for the
8515 * destination 'dst'. If 'dst' is offlink, going
8516 * through the first hop 'gw', the security attributes
8517 * of 'dst' must be set to point to the gateway
8518 * credentials of gateway 'gw'. If 'dst' is onlink, it
8519 * is possible that 'dst' is a potential gateway that is
8520 * referenced by some route that has some security
8521 * attributes. Thus in the former case, we need to do a
8522 * gcgrp_lookup of 'gw' while in the latter case we
8523 * need to do gcgrp_lookup of 'dst' itself.
8524 */
8525 ga.ga_af = AF_INET;
8526 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst,
8527 &ga.ga_addr);
8528 gcgrp = gcgrp_lookup(&ga, B_FALSE);
8529
8530 ire = ire_create(
8531 (uchar_t *)&dst, /* dest address */
8532 (uchar_t *)&ip_g_all_ones, /* mask */
8533 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8534 (uchar_t *)&gw, /* gateway address */
8535 &save_ire->ire_max_frag,
8536 NULL, /* no src nce */
8537 dst_ill->ill_rq, /* recv-from queue */
8538 dst_ill->ill_wq, /* send-to queue */
8539 IRE_CACHE,
8540 src_ipif,
8541 save_ire->ire_mask, /* Parent mask */
8542 (sire != NULL) ? /* Parent handle */
8543 sire->ire_phandle : 0,
8544 save_ire->ire_ihandle, /* Interface handle */
8545 (sire != NULL) ? sire->ire_flags &
8546 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */
8547 &(save_ire->ire_uinfo),
8548 NULL,
8549 gcgrp,
8550 ipst);
8551
8552 if (ire == NULL) {
8553 if (gcgrp != NULL) {
8554 GCGRP_REFRELE(gcgrp);
8555 gcgrp = NULL;
8556 }
8557 ire_refrele(save_ire);
8558 break;
8559 }
8560
8561 /* reference now held by IRE */
8562 gcgrp = NULL;
8563
8564 ire->ire_marks |= ire_marks;
8565
8566 /* Prevent save_ire from getting deleted */
8567 IRB_REFHOLD(save_ire->ire_bucket);
8568 /* Has it been removed already ? */
8569 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
8570 IRB_REFRELE(save_ire->ire_bucket);
8571 ire_refrele(save_ire);
8572 break;
8573 }
8574
8575 /*
8576 * In the case of multirouting, a copy
8577 * of the packet is made before it is sent.
8578 * The copy is used in the next
8579 * loop to attempt another resolution.
8580 */
8581 xmit_mp = first_mp;
8582 if ((sire != NULL) &&
8583 (sire->ire_flags & RTF_MULTIRT)) {
8584 copy_mp = copymsg(first_mp);
8585 if (copy_mp != NULL) {
8586 xmit_mp = copy_mp;
8587 MULTIRT_DEBUG_TAG(first_mp);
8588 }
8589 }
8590 ire_add_then_send(q, ire, xmit_mp);
8591
8592 /* Assert that it is not deleted yet. */
8593 ASSERT(save_ire->ire_ptpn != NULL);
8594 IRB_REFRELE(save_ire->ire_bucket);
8595 ire_refrele(save_ire);
8596
8597 if (copy_mp != NULL) {
8598 /*
8599 * If we found a (no)resolver, we ignore any
8600 * trailing top priority IRE_CACHE in further
8601 * loops. This ensures that we do not omit any
8602 * (no)resolver.
8603 * This IRE_CACHE, if any, will be processed
8604 * by another thread entering ip_newroute().
8605 * IRE_CACHE entries, if any, will be processed
8606 * by another thread entering ip_newroute(),
8607 * (upon resolver response, for instance).
8608 * This aims to force parallel multirt
8609 * resolutions as soon as a packet must be sent.
8610 * In the best case, after the tx of only one
8611 * packet, all reachable routes are resolved.
8612 * Otherwise, the resolution of all RTF_MULTIRT
8613 * routes would require several emissions.
8614 */
8615 multirt_flags &= ~MULTIRT_CACHEGW;
8616
8617 /*
8618 * Search for the next unresolved multirt
8619 * route.
8620 */
8621 copy_mp = NULL;
8622 save_ire = NULL;
8623 ire = NULL;
8624 multirt_resolve_next = B_TRUE;
8625 continue;
8626 }
8627
8628 /*
8629 * Don't need sire anymore
8630 */
8631 if (sire != NULL)
8632 ire_refrele(sire);
8633
8634 ipif_refrele(src_ipif);
8635 ill_refrele(dst_ill);
8636 return;
8637 }
8638 case IRE_IF_RESOLVER:
8639 /*
8640 * We can't build an IRE_CACHE yet, but at least we
8641 * found a resolver that can help.
8642 */
8643 res_mp = dst_ill->ill_resolver_mp;
8644 if (!OK_RESOLVER_MP(res_mp))
8645 break;
8646
8647 /*
8648 * To be at this point in the code with a non-zero gw
8649 * means that dst is reachable through a gateway that
8650 * we have never resolved. By changing dst to the gw
8651 * addr we resolve the gateway first.
8652 * When ire_add_then_send() tries to put the IP dg
8653 * to dst, it will reenter ip_newroute() at which
8654 * time we will find the IRE_CACHE for the gw and
8655 * create another IRE_CACHE in case IRE_CACHE above.
8656 */
8657 if (gw != INADDR_ANY) {
8658 /*
8659 * The source ipif that was determined above was
8660 * relative to the destination address, not the
8661 * gateway's. If src_ipif was not taken out of
8662 * the IRE_IF_RESOLVER entry, we'll need to call
8663 * ipif_select_source() again.
8664 */
8665 if (src_ipif != ire->ire_ipif) {
8666 ipif_refrele(src_ipif);
8667 src_ipif = ipif_select_source(dst_ill,
8668 gw, zoneid);
8669 if (src_ipif == NULL) {
8670 if (ip_debug > 2) {
8671 pr_addr_dbg(
8672 "ip_newroute: no "
8673 "src for gw %s ",
8674 AF_INET, &gw);
8675 printf("through "
8676 "interface %s\n",
8677 dst_ill->ill_name);
8678 }
8679 goto icmp_err_ret;
8680 }
8681 }
8682 save_dst = dst;
8683 dst = gw;
8684 gw = INADDR_ANY;
8685 }
8686
8687 /*
8688 * We obtain a partial IRE_CACHE which we will pass
8689 * along with the resolver query. When the response
8690 * comes back it will be there ready for us to add.
8691 * The ire_max_frag is atomically set under the
8692 * irebucket lock in ire_add_v[46].
8693 */
8694
8695 ire = ire_create_mp(
8696 (uchar_t *)&dst, /* dest address */
8697 (uchar_t *)&ip_g_all_ones, /* mask */
8698 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8699 (uchar_t *)&gw, /* gateway address */
8700 NULL, /* ire_max_frag */
8701 NULL, /* no src nce */
8702 dst_ill->ill_rq, /* recv-from queue */
8703 dst_ill->ill_wq, /* send-to queue */
8704 IRE_CACHE,
8705 src_ipif, /* Interface ipif */
8706 save_ire->ire_mask, /* Parent mask */
8707 0,
8708 save_ire->ire_ihandle, /* Interface handle */
8709 0, /* flags if any */
8710 &(save_ire->ire_uinfo),
8711 NULL,
8712 NULL,
8713 ipst);
8714
8715 if (ire == NULL) {
8716 ire_refrele(save_ire);
8717 break;
8718 }
8719
8720 if ((sire != NULL) &&
8721 (sire->ire_flags & RTF_MULTIRT)) {
8722 copy_mp = copymsg(first_mp);
8723 if (copy_mp != NULL)
8724 MULTIRT_DEBUG_TAG(copy_mp);
8725 }
8726
8727 ire->ire_marks |= ire_marks;
8728
8729 /*
8730 * Construct message chain for the resolver
8731 * of the form:
8732 * ARP_REQ_MBLK-->IRE_MBLK-->Packet
8733 * Packet could contain a IPSEC_OUT mp.
8734 *
8735 * NOTE : ire will be added later when the response
8736 * comes back from ARP. If the response does not
8737 * come back, ARP frees the packet. For this reason,
8738 * we can't REFHOLD the bucket of save_ire to prevent
8739 * deletions. We may not be able to REFRELE the bucket
8740 * if the response never comes back. Thus, before
8741 * adding the ire, ire_add_v4 will make sure that the
8742 * interface route does not get deleted. This is the
8743 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6
8744 * where we can always prevent deletions because of
8745 * the synchronous nature of adding IRES i.e
8746 * ire_add_then_send is called after creating the IRE.
8747 */
8748 ASSERT(ire->ire_mp != NULL);
8749 ire->ire_mp->b_cont = first_mp;
8750 /* Have saved_mp handy, for cleanup if canput fails */
8751 saved_mp = mp;
8752 mp = copyb(res_mp);
8753 if (mp == NULL) {
8754 /* Prepare for cleanup */
8755 mp = saved_mp; /* pkt */
8756 ire_delete(ire); /* ire_mp */
8757 ire = NULL;
8758 ire_refrele(save_ire);
8759 if (copy_mp != NULL) {
8760 MULTIRT_DEBUG_UNTAG(copy_mp);
8761 freemsg(copy_mp);
8762 copy_mp = NULL;
8763 }
8764 break;
8765 }
8766 linkb(mp, ire->ire_mp);
8767
8768 /*
8769 * Fill in the source and dest addrs for the resolver.
8770 * NOTE: this depends on memory layouts imposed by
8771 * ill_init().
8772 */
8773 areq = (areq_t *)mp->b_rptr;
8774 addrp = (ipaddr_t *)((char *)areq +
8775 areq->areq_sender_addr_offset);
8776 if (do_attach_ill) {
8777 /*
8778 * This is bind to no failover case.
8779 * arp packet also must go out on attach_ill.
8780 */
8781 ASSERT(ipha->ipha_src != NULL);
8782 *addrp = ipha->ipha_src;
8783 } else {
8784 *addrp = save_ire->ire_src_addr;
8785 }
8786
8787 ire_refrele(save_ire);
8788 addrp = (ipaddr_t *)((char *)areq +
8789 areq->areq_target_addr_offset);
8790 *addrp = dst;
8791 /* Up to the resolver. */
8792 if (canputnext(dst_ill->ill_rq) &&
8793 !(dst_ill->ill_arp_closing)) {
8794 putnext(dst_ill->ill_rq, mp);
8795 ire = NULL;
8796 if (copy_mp != NULL) {
8797 /*
8798 * If we found a resolver, we ignore
8799 * any trailing top priority IRE_CACHE
8800 * in the further loops. This ensures
8801 * that we do not omit any resolver.
8802 * IRE_CACHE entries, if any, will be
8803 * processed next time we enter
8804 * ip_newroute().
8805 */
8806 multirt_flags &= ~MULTIRT_CACHEGW;
8807 /*
8808 * Search for the next unresolved
8809 * multirt route.
8810 */
8811 first_mp = copy_mp;
8812 copy_mp = NULL;
8813 /* Prepare the next resolution loop. */
8814 mp = first_mp;
8815 EXTRACT_PKT_MP(mp, first_mp,
8816 mctl_present);
8817 if (mctl_present)
8818 io = (ipsec_out_t *)
8819 first_mp->b_rptr;
8820 ipha = (ipha_t *)mp->b_rptr;
8821
8822 ASSERT(sire != NULL);
8823
8824 dst = save_dst;
8825 multirt_resolve_next = B_TRUE;
8826 continue;
8827 }
8828
8829 if (sire != NULL)
8830 ire_refrele(sire);
8831
8832 /*
8833 * The response will come back in ip_wput
8834 * with db_type IRE_DB_TYPE.
8835 */
8836 ipif_refrele(src_ipif);
8837 ill_refrele(dst_ill);
8838 return;
8839 } else {
8840 /* Prepare for cleanup */
8841 DTRACE_PROBE1(ip__newroute__drop, mblk_t *,
8842 mp);
8843 mp->b_cont = NULL;
8844 freeb(mp); /* areq */
8845 /*
8846 * this is an ire that is not added to the
8847 * cache. ire_freemblk will handle the release
8848 * of any resources associated with the ire.
8849 */
8850 ire_delete(ire); /* ire_mp */
8851 mp = saved_mp; /* pkt */
8852 ire = NULL;
8853 if (copy_mp != NULL) {
8854 MULTIRT_DEBUG_UNTAG(copy_mp);
8855 freemsg(copy_mp);
8856 copy_mp = NULL;
8857 }
8858 break;
8859 }
8860 default:
8861 break;
8862 }
8863 } while (multirt_resolve_next);
8864
8865 ip1dbg(("ip_newroute: dropped\n"));
8866 /* Did this packet originate externally? */
8867 if (mp->b_prev) {
8868 mp->b_next = NULL;
8869 mp->b_prev = NULL;
8870 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
8871 } else {
8872 if (dst_ill != NULL) {
8873 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards);
8874 } else {
8875 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
8876 }
8877 }
8878 ASSERT(copy_mp == NULL);
8879 MULTIRT_DEBUG_UNTAG(first_mp);
8880 freemsg(first_mp);
8881 if (ire != NULL)
8882 ire_refrele(ire);
8883 if (sire != NULL)
8884 ire_refrele(sire);
8885 if (src_ipif != NULL)
8886 ipif_refrele(src_ipif);
8887 if (dst_ill != NULL)
8888 ill_refrele(dst_ill);
8889 return;
8890
8891 icmp_err_ret:
8892 ip1dbg(("ip_newroute: no route\n"));
8893 if (src_ipif != NULL)
8894 ipif_refrele(src_ipif);
8895 if (dst_ill != NULL)
8896 ill_refrele(dst_ill);
8897 if (sire != NULL)
8898 ire_refrele(sire);
8899 /* Did this packet originate externally? */
8900 if (mp->b_prev) {
8901 mp->b_next = NULL;
8902 mp->b_prev = NULL;
8903 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes);
8904 q = WR(q);
8905 } else {
8906 /*
8907 * There is no outgoing ill, so just increment the
8908 * system MIB.
8909 */
8910 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes);
8911 /*
8912 * Since ip_wput() isn't close to finished, we fill
8913 * in enough of the header for credible error reporting.
8914 */
8915 if (ip_hdr_complete(ipha, zoneid, ipst)) {
8916 /* Failed */
8917 MULTIRT_DEBUG_UNTAG(first_mp);
8918 freemsg(first_mp);
8919 if (ire != NULL)
8920 ire_refrele(ire);
8921 return;
8922 }
8923 }
8924
8925 /*
8926 * At this point we will have ire only if RTF_BLACKHOLE
8927 * or RTF_REJECT flags are set on the IRE. It will not
8928 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set.
8929 */
8930 if (ire != NULL) {
8931 if (ire->ire_flags & RTF_BLACKHOLE) {
8932 ire_refrele(ire);
8933 MULTIRT_DEBUG_UNTAG(first_mp);
8934 freemsg(first_mp);
8935 return;
8936 }
8937 ire_refrele(ire);
8938 }
8939 if (ip_source_routed(ipha, ipst)) {
8940 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED,
8941 zoneid, ipst);
8942 return;
8943 }
8944 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst);
8945 }
8946
8947 ip_opt_info_t zero_info;
8948
8949 /*
8950 * IPv4 -
8951 * ip_newroute_ipif is called by ip_wput_multicast and
8952 * ip_rput_forward_multicast whenever we need to send
8953 * out a packet to a destination address for which we do not have specific
8954 * routing information. It is used when the packet will be sent out
8955 * on a specific interface. It is also called by ip_wput() when IP_BOUND_IF
8956 * socket option is set or icmp error message wants to go out on a particular
8957 * interface for a unicast packet.
8958 *
8959 * In most cases, the destination address is resolved thanks to the ipif
8960 * intrinsic resolver. However, there are some cases where the call to
8961 * ip_newroute_ipif must take into account the potential presence of
8962 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire
8963 * that uses the interface. This is specified through flags,
8964 * which can be a combination of:
8965 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC
8966 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address
8967 * and flags. Additionally, the packet source address has to be set to
8968 * the specified address. The caller is thus expected to set this flag
8969 * if the packet has no specific source address yet.
8970 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT
8971 * flag, the resulting ire will inherit the flag. All unresolved routes
8972 * to the destination must be explored in the same call to
8973 * ip_newroute_ipif().
8974 */
8975 static void
8976 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst,
8977 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop)
8978 {
8979 areq_t *areq;
8980 ire_t *ire = NULL;
8981 mblk_t *res_mp;
8982 ipaddr_t *addrp;
8983 mblk_t *first_mp;
8984 ire_t *save_ire = NULL;
8985 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */
8986 ipif_t *src_ipif = NULL;
8987 ushort_t ire_marks = 0;
8988 ill_t *dst_ill = NULL;
8989 boolean_t mctl_present;
8990 ipsec_out_t *io;
8991 ipha_t *ipha;
8992 int ihandle = 0;
8993 mblk_t *saved_mp;
8994 ire_t *fire = NULL;
8995 mblk_t *copy_mp = NULL;
8996 boolean_t multirt_resolve_next;
8997 boolean_t unspec_src;
8998 ipaddr_t ipha_dst;
8999 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
9000
9001 /*
9002 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold
9003 * here for uniformity
9004 */
9005 ipif_refhold(ipif);
9006
9007 /*
9008 * This loop is run only once in most cases.
9009 * We loop to resolve further routes only when the destination
9010 * can be reached through multiple RTF_MULTIRT-flagged ires.
9011 */
9012 do {
9013 if (dst_ill != NULL) {
9014 ill_refrele(dst_ill);
9015 dst_ill = NULL;
9016 }
9017 if (src_ipif != NULL) {
9018 ipif_refrele(src_ipif);
9019 src_ipif = NULL;
9020 }
9021 multirt_resolve_next = B_FALSE;
9022
9023 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst),
9024 ipif->ipif_ill->ill_name));
9025
9026 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
9027 if (mctl_present)
9028 io = (ipsec_out_t *)first_mp->b_rptr;
9029
9030 ipha = (ipha_t *)mp->b_rptr;
9031
9032 /*
9033 * Save the packet destination address, we may need it after
9034 * the packet has been consumed.
9035 */
9036 ipha_dst = ipha->ipha_dst;
9037
9038 /*
9039 * If the interface is a pt-pt interface we look for an
9040 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the
9041 * local_address and the pt-pt destination address. Otherwise
9042 * we just match the local address.
9043 * NOTE: dst could be different than ipha->ipha_dst in case
9044 * of sending igmp multicast packets over a point-to-point
9045 * connection.
9046 * Thus we must be careful enough to check ipha_dst to be a
9047 * multicast address, otherwise it will take xmit_if path for
9048 * multicast packets resulting into kernel stack overflow by
9049 * repeated calls to ip_newroute_ipif from ire_send().
9050 */
9051 if (CLASSD(ipha_dst) &&
9052 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) {
9053 goto err_ret;
9054 }
9055
9056 /*
9057 * We check if an IRE_OFFSUBNET for the addr that goes through
9058 * ipif exists. We need it to determine if the RTF_SETSRC and/or
9059 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may
9060 * propagate its flags to the new ire.
9061 */
9062 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) {
9063 fire = ipif_lookup_multi_ire(ipif, ipha_dst);
9064 ip2dbg(("ip_newroute_ipif: "
9065 "ipif_lookup_multi_ire("
9066 "ipif %p, dst %08x) = fire %p\n",
9067 (void *)ipif, ntohl(dst), (void *)fire));
9068 }
9069
9070 if (mctl_present && io->ipsec_out_attach_if) {
9071 attach_ill = ip_grab_attach_ill(NULL, first_mp,
9072 io->ipsec_out_ill_index, B_FALSE, ipst);
9073
9074 /* Failure case frees things for us. */
9075 if (attach_ill == NULL) {
9076 ipif_refrele(ipif);
9077 if (fire != NULL)
9078 ire_refrele(fire);
9079 return;
9080 }
9081
9082 /*
9083 * Check if we need an ire that will not be
9084 * looked up by anybody else i.e. HIDDEN.
9085 */
9086 if (ill_is_probeonly(attach_ill)) {
9087 ire_marks = IRE_MARK_HIDDEN;
9088 }
9089 /*
9090 * ip_wput passes the right ipif for IPIF_NOFAILOVER
9091 * case.
9092 */
9093 dst_ill = ipif->ipif_ill;
9094 /* attach_ill has been refheld by ip_grab_attach_ill */
9095 ASSERT(dst_ill == attach_ill);
9096 } else {
9097 /*
9098 * If the interface belongs to an interface group,
9099 * make sure the next possible interface in the group
9100 * is used. This encourages load spreading among
9101 * peers in an interface group.
9102 * Note: load spreading is disabled for RTF_MULTIRT
9103 * routes.
9104 */
9105 if ((flags & RTF_MULTIRT) && (fire != NULL) &&
9106 (fire->ire_flags & RTF_MULTIRT)) {
9107 /*
9108 * Don't perform outbound load spreading
9109 * in the case of an RTF_MULTIRT issued route,
9110 * we actually typically want to replicate
9111 * outgoing packets through particular
9112 * interfaces.
9113 */
9114 dst_ill = ipif->ipif_ill;
9115 ill_refhold(dst_ill);
9116 } else {
9117 dst_ill = ip_newroute_get_dst_ill(
9118 ipif->ipif_ill);
9119 }
9120 if (dst_ill == NULL) {
9121 if (ip_debug > 2) {
9122 pr_addr_dbg("ip_newroute_ipif: "
9123 "no dst ill for dst %s\n",
9124 AF_INET, &dst);
9125 }
9126 goto err_ret;
9127 }
9128 }
9129
9130 /*
9131 * Pick a source address preferring non-deprecated ones.
9132 * Unlike ip_newroute, we don't do any source address
9133 * selection here since for multicast it really does not help
9134 * in inbound load spreading as in the unicast case.
9135 */
9136 if ((flags & RTF_SETSRC) && (fire != NULL) &&
9137 (fire->ire_flags & RTF_SETSRC)) {
9138 /*
9139 * As requested by flags, an IRE_OFFSUBNET was looked up
9140 * on that interface. This ire has RTF_SETSRC flag, so
9141 * the source address of the packet must be changed.
9142 * Check that the ipif matching the requested source
9143 * address still exists.
9144 */
9145 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL,
9146 zoneid, NULL, NULL, NULL, NULL, ipst);
9147 }
9148
9149 unspec_src = (connp != NULL && connp->conn_unspec_src);
9150
9151 if (((!ipif->ipif_isv6 && ipif->ipif_lcl_addr == INADDR_ANY) ||
9152 (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_UP)) != IPIF_UP ||
9153 (connp != NULL && ipif->ipif_zoneid != zoneid &&
9154 ipif->ipif_zoneid != ALL_ZONES)) &&
9155 (src_ipif == NULL) &&
9156 (!unspec_src || ipha->ipha_src != INADDR_ANY)) {
9157 src_ipif = ipif_select_source(dst_ill, dst, zoneid);
9158 if (src_ipif == NULL) {
9159 if (ip_debug > 2) {
9160 /* ip1dbg */
9161 pr_addr_dbg("ip_newroute_ipif: "
9162 "no src for dst %s",
9163 AF_INET, &dst);
9164 }
9165 ip1dbg((" through interface %s\n",
9166 dst_ill->ill_name));
9167 goto err_ret;
9168 }
9169 ipif_refrele(ipif);
9170 ipif = src_ipif;
9171 ipif_refhold(ipif);
9172 }
9173 if (src_ipif == NULL) {
9174 src_ipif = ipif;
9175 ipif_refhold(src_ipif);
9176 }
9177
9178 /*
9179 * Assign a source address while we have the conn.
9180 * We can't have ip_wput_ire pick a source address when the
9181 * packet returns from arp since conn_unspec_src might be set
9182 * and we lose the conn when going through arp.
9183 */
9184 if (ipha->ipha_src == INADDR_ANY && !unspec_src)
9185 ipha->ipha_src = src_ipif->ipif_src_addr;
9186
9187 /*
9188 * In the case of IP_BOUND_IF and IP_PKTINFO, it is possible
9189 * that the outgoing interface does not have an interface ire.
9190 */
9191 if (CLASSD(ipha_dst) && (connp == NULL ||
9192 connp->conn_outgoing_ill == NULL) &&
9193 infop->ip_opt_ill_index == 0) {
9194 /* ipif_to_ire returns an held ire */
9195 ire = ipif_to_ire(ipif);
9196 if (ire == NULL)
9197 goto err_ret;
9198 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
9199 goto err_ret;
9200 /*
9201 * ihandle is needed when the ire is added to
9202 * cache table.
9203 */
9204 save_ire = ire;
9205 ihandle = save_ire->ire_ihandle;
9206
9207 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, "
9208 "flags %04x\n",
9209 (void *)ire, (void *)ipif, flags));
9210 if ((flags & RTF_MULTIRT) && (fire != NULL) &&
9211 (fire->ire_flags & RTF_MULTIRT)) {
9212 /*
9213 * As requested by flags, an IRE_OFFSUBNET was
9214 * looked up on that interface. This ire has
9215 * RTF_MULTIRT flag, so the resolution loop will
9216 * be re-entered to resolve additional routes on
9217 * other interfaces. For that purpose, a copy of
9218 * the packet is performed at this point.
9219 */
9220 fire->ire_last_used_time = lbolt;
9221 copy_mp = copymsg(first_mp);
9222 if (copy_mp) {
9223 MULTIRT_DEBUG_TAG(copy_mp);
9224 }
9225 }
9226 if ((flags & RTF_SETSRC) && (fire != NULL) &&
9227 (fire->ire_flags & RTF_SETSRC)) {
9228 /*
9229 * As requested by flags, an IRE_OFFSUBET was
9230 * looked up on that interface. This ire has
9231 * RTF_SETSRC flag, so the source address of the
9232 * packet must be changed.
9233 */
9234 ipha->ipha_src = fire->ire_src_addr;
9235 }
9236 } else {
9237 ASSERT((connp == NULL) ||
9238 (connp->conn_outgoing_ill != NULL) ||
9239 (connp->conn_dontroute) ||
9240 infop->ip_opt_ill_index != 0);
9241 /*
9242 * The only ways we can come here are:
9243 * 1) IP_BOUND_IF socket option is set
9244 * 2) SO_DONTROUTE socket option is set
9245 * 3) IP_PKTINFO option is passed in as ancillary data.
9246 * In all cases, the new ire will not be added
9247 * into cache table.
9248 */
9249 ire_marks |= IRE_MARK_NOADD;
9250 }
9251
9252 switch (ipif->ipif_net_type) {
9253 case IRE_IF_NORESOLVER: {
9254 /* We have what we need to build an IRE_CACHE. */
9255
9256 if ((dst_ill->ill_phys_addr_length != IP_ADDR_LEN) &&
9257 (dst_ill->ill_resolver_mp == NULL)) {
9258 ip1dbg(("ip_newroute_ipif: dst_ill %p "
9259 "for IRE_IF_NORESOLVER ire %p has "
9260 "no ill_resolver_mp\n",
9261 (void *)dst_ill, (void *)ire));
9262 break;
9263 }
9264
9265 /*
9266 * The new ire inherits the IRE_OFFSUBNET flags
9267 * and source address, if this was requested.
9268 */
9269 ire = ire_create(
9270 (uchar_t *)&dst, /* dest address */
9271 (uchar_t *)&ip_g_all_ones, /* mask */
9272 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
9273 NULL, /* gateway address */
9274 &ipif->ipif_mtu,
9275 NULL, /* no src nce */
9276 dst_ill->ill_rq, /* recv-from queue */
9277 dst_ill->ill_wq, /* send-to queue */
9278 IRE_CACHE,
9279 src_ipif,
9280 (save_ire != NULL ? save_ire->ire_mask : 0),
9281 (fire != NULL) ? /* Parent handle */
9282 fire->ire_phandle : 0,
9283 ihandle, /* Interface handle */
9284 (fire != NULL) ?
9285 (fire->ire_flags &
9286 (RTF_SETSRC | RTF_MULTIRT)) : 0,
9287 (save_ire == NULL ? &ire_uinfo_null :
9288 &save_ire->ire_uinfo),
9289 NULL,
9290 NULL,
9291 ipst);
9292
9293 if (ire == NULL) {
9294 if (save_ire != NULL)
9295 ire_refrele(save_ire);
9296 break;
9297 }
9298
9299 ire->ire_marks |= ire_marks;
9300
9301 /*
9302 * If IRE_MARK_NOADD is set then we need to convert
9303 * the max_fragp to a useable value now. This is
9304 * normally done in ire_add_v[46]. We also need to
9305 * associate the ire with an nce (normally would be
9306 * done in ip_wput_nondata()).
9307 *
9308 * Note that IRE_MARK_NOADD packets created here
9309 * do not have a non-null ire_mp pointer. The null
9310 * value of ire_bucket indicates that they were
9311 * never added.
9312 */
9313 if (ire->ire_marks & IRE_MARK_NOADD) {
9314 uint_t max_frag;
9315
9316 max_frag = *ire->ire_max_fragp;
9317 ire->ire_max_fragp = NULL;
9318 ire->ire_max_frag = max_frag;
9319
9320 if ((ire->ire_nce = ndp_lookup_v4(
9321 ire_to_ill(ire),
9322 (ire->ire_gateway_addr != INADDR_ANY ?
9323 &ire->ire_gateway_addr : &ire->ire_addr),
9324 B_FALSE)) == NULL) {
9325 if (save_ire != NULL)
9326 ire_refrele(save_ire);
9327 break;
9328 }
9329 ASSERT(ire->ire_nce->nce_state ==
9330 ND_REACHABLE);
9331 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
9332 }
9333
9334 /* Prevent save_ire from getting deleted */
9335 if (save_ire != NULL) {
9336 IRB_REFHOLD(save_ire->ire_bucket);
9337 /* Has it been removed already ? */
9338 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
9339 IRB_REFRELE(save_ire->ire_bucket);
9340 ire_refrele(save_ire);
9341 break;
9342 }
9343 }
9344
9345 ire_add_then_send(q, ire, first_mp);
9346
9347 /* Assert that save_ire is not deleted yet. */
9348 if (save_ire != NULL) {
9349 ASSERT(save_ire->ire_ptpn != NULL);
9350 IRB_REFRELE(save_ire->ire_bucket);
9351 ire_refrele(save_ire);
9352 save_ire = NULL;
9353 }
9354 if (fire != NULL) {
9355 ire_refrele(fire);
9356 fire = NULL;
9357 }
9358
9359 /*
9360 * the resolution loop is re-entered if this
9361 * was requested through flags and if we
9362 * actually are in a multirouting case.
9363 */
9364 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) {
9365 boolean_t need_resolve =
9366 ire_multirt_need_resolve(ipha_dst,
9367 MBLK_GETLABEL(copy_mp), ipst);
9368 if (!need_resolve) {
9369 MULTIRT_DEBUG_UNTAG(copy_mp);
9370 freemsg(copy_mp);
9371 copy_mp = NULL;
9372 } else {
9373 /*
9374 * ipif_lookup_group() calls
9375 * ire_lookup_multi() that uses
9376 * ire_ftable_lookup() to find
9377 * an IRE_INTERFACE for the group.
9378 * In the multirt case,
9379 * ire_lookup_multi() then invokes
9380 * ire_multirt_lookup() to find
9381 * the next resolvable ire.
9382 * As a result, we obtain an new
9383 * interface, derived from the
9384 * next ire.
9385 */
9386 ipif_refrele(ipif);
9387 ipif = ipif_lookup_group(ipha_dst,
9388 zoneid, ipst);
9389 ip2dbg(("ip_newroute_ipif: "
9390 "multirt dst %08x, ipif %p\n",
9391 htonl(dst), (void *)ipif));
9392 if (ipif != NULL) {
9393 mp = copy_mp;
9394 copy_mp = NULL;
9395 multirt_resolve_next = B_TRUE;
9396 continue;
9397 } else {
9398 freemsg(copy_mp);
9399 }
9400 }
9401 }
9402 if (ipif != NULL)
9403 ipif_refrele(ipif);
9404 ill_refrele(dst_ill);
9405 ipif_refrele(src_ipif);
9406 return;
9407 }
9408 case IRE_IF_RESOLVER:
9409 /*
9410 * We can't build an IRE_CACHE yet, but at least
9411 * we found a resolver that can help.
9412 */
9413 res_mp = dst_ill->ill_resolver_mp;
9414 if (!OK_RESOLVER_MP(res_mp))
9415 break;
9416
9417 /*
9418 * We obtain a partial IRE_CACHE which we will pass
9419 * along with the resolver query. When the response
9420 * comes back it will be there ready for us to add.
9421 * The new ire inherits the IRE_OFFSUBNET flags
9422 * and source address, if this was requested.
9423 * The ire_max_frag is atomically set under the
9424 * irebucket lock in ire_add_v[46]. Only in the
9425 * case of IRE_MARK_NOADD, we set it here itself.
9426 */
9427 ire = ire_create_mp(
9428 (uchar_t *)&dst, /* dest address */
9429 (uchar_t *)&ip_g_all_ones, /* mask */
9430 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
9431 NULL, /* gateway address */
9432 (ire_marks & IRE_MARK_NOADD) ?
9433 ipif->ipif_mtu : 0, /* max_frag */
9434 NULL, /* no src nce */
9435 dst_ill->ill_rq, /* recv-from queue */
9436 dst_ill->ill_wq, /* send-to queue */
9437 IRE_CACHE,
9438 src_ipif,
9439 (save_ire != NULL ? save_ire->ire_mask : 0),
9440 (fire != NULL) ? /* Parent handle */
9441 fire->ire_phandle : 0,
9442 ihandle, /* Interface handle */
9443 (fire != NULL) ? /* flags if any */
9444 (fire->ire_flags &
9445 (RTF_SETSRC | RTF_MULTIRT)) : 0,
9446 (save_ire == NULL ? &ire_uinfo_null :
9447 &save_ire->ire_uinfo),
9448 NULL,
9449 NULL,
9450 ipst);
9451
9452 if (save_ire != NULL) {
9453 ire_refrele(save_ire);
9454 save_ire = NULL;
9455 }
9456 if (ire == NULL)
9457 break;
9458
9459 ire->ire_marks |= ire_marks;
9460 /*
9461 * Construct message chain for the resolver of the
9462 * form:
9463 * ARP_REQ_MBLK-->IRE_MBLK-->Packet
9464 *
9465 * NOTE : ire will be added later when the response
9466 * comes back from ARP. If the response does not
9467 * come back, ARP frees the packet. For this reason,
9468 * we can't REFHOLD the bucket of save_ire to prevent
9469 * deletions. We may not be able to REFRELE the
9470 * bucket if the response never comes back.
9471 * Thus, before adding the ire, ire_add_v4 will make
9472 * sure that the interface route does not get deleted.
9473 * This is the only case unlike ip_newroute_v6,
9474 * ip_newroute_ipif_v6 where we can always prevent
9475 * deletions because ire_add_then_send is called after
9476 * creating the IRE.
9477 * If IRE_MARK_NOADD is set, then ire_add_then_send
9478 * does not add this IRE into the IRE CACHE.
9479 */
9480 ASSERT(ire->ire_mp != NULL);
9481 ire->ire_mp->b_cont = first_mp;
9482 /* Have saved_mp handy, for cleanup if canput fails */
9483 saved_mp = mp;
9484 mp = copyb(res_mp);
9485 if (mp == NULL) {
9486 /* Prepare for cleanup */
9487 mp = saved_mp; /* pkt */
9488 ire_delete(ire); /* ire_mp */
9489 ire = NULL;
9490 if (copy_mp != NULL) {
9491 MULTIRT_DEBUG_UNTAG(copy_mp);
9492 freemsg(copy_mp);
9493 copy_mp = NULL;
9494 }
9495 break;
9496 }
9497 linkb(mp, ire->ire_mp);
9498
9499 /*
9500 * Fill in the source and dest addrs for the resolver.
9501 * NOTE: this depends on memory layouts imposed by
9502 * ill_init().
9503 */
9504 areq = (areq_t *)mp->b_rptr;
9505 addrp = (ipaddr_t *)((char *)areq +
9506 areq->areq_sender_addr_offset);
9507 *addrp = ire->ire_src_addr;
9508 addrp = (ipaddr_t *)((char *)areq +
9509 areq->areq_target_addr_offset);
9510 *addrp = dst;
9511 /* Up to the resolver. */
9512 if (canputnext(dst_ill->ill_rq) &&
9513 !(dst_ill->ill_arp_closing)) {
9514 putnext(dst_ill->ill_rq, mp);
9515 /*
9516 * The response will come back in ip_wput
9517 * with db_type IRE_DB_TYPE.
9518 */
9519 } else {
9520 mp->b_cont = NULL;
9521 freeb(mp); /* areq */
9522 ire_delete(ire); /* ire_mp */
9523 saved_mp->b_next = NULL;
9524 saved_mp->b_prev = NULL;
9525 freemsg(first_mp); /* pkt */
9526 ip2dbg(("ip_newroute_ipif: dropped\n"));
9527 }
9528
9529 if (fire != NULL) {
9530 ire_refrele(fire);
9531 fire = NULL;
9532 }
9533
9534
9535 /*
9536 * The resolution loop is re-entered if this was
9537 * requested through flags and we actually are
9538 * in a multirouting case.
9539 */
9540 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) {
9541 boolean_t need_resolve =
9542 ire_multirt_need_resolve(ipha_dst,
9543 MBLK_GETLABEL(copy_mp), ipst);
9544 if (!need_resolve) {
9545 MULTIRT_DEBUG_UNTAG(copy_mp);
9546 freemsg(copy_mp);
9547 copy_mp = NULL;
9548 } else {
9549 /*
9550 * ipif_lookup_group() calls
9551 * ire_lookup_multi() that uses
9552 * ire_ftable_lookup() to find
9553 * an IRE_INTERFACE for the group.
9554 * In the multirt case,
9555 * ire_lookup_multi() then invokes
9556 * ire_multirt_lookup() to find
9557 * the next resolvable ire.
9558 * As a result, we obtain an new
9559 * interface, derived from the
9560 * next ire.
9561 */
9562 ipif_refrele(ipif);
9563 ipif = ipif_lookup_group(ipha_dst,
9564 zoneid, ipst);
9565 if (ipif != NULL) {
9566 mp = copy_mp;
9567 copy_mp = NULL;
9568 multirt_resolve_next = B_TRUE;
9569 continue;
9570 } else {
9571 freemsg(copy_mp);
9572 }
9573 }
9574 }
9575 if (ipif != NULL)
9576 ipif_refrele(ipif);
9577 ill_refrele(dst_ill);
9578 ipif_refrele(src_ipif);
9579 return;
9580 default:
9581 break;
9582 }
9583 } while (multirt_resolve_next);
9584
9585 err_ret:
9586 ip2dbg(("ip_newroute_ipif: dropped\n"));
9587 if (fire != NULL)
9588 ire_refrele(fire);
9589 ipif_refrele(ipif);
9590 /* Did this packet originate externally? */
9591 if (dst_ill != NULL)
9592 ill_refrele(dst_ill);
9593 if (src_ipif != NULL)
9594 ipif_refrele(src_ipif);
9595 if (mp->b_prev || mp->b_next) {
9596 mp->b_next = NULL;
9597 mp->b_prev = NULL;
9598 } else {
9599 /*
9600 * Since ip_wput() isn't close to finished, we fill
9601 * in enough of the header for credible error reporting.
9602 */
9603 if (ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid, ipst)) {
9604 /* Failed */
9605 freemsg(first_mp);
9606 if (ire != NULL)
9607 ire_refrele(ire);
9608 return;
9609 }
9610 }
9611 /*
9612 * At this point we will have ire only if RTF_BLACKHOLE
9613 * or RTF_REJECT flags are set on the IRE. It will not
9614 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set.
9615 */
9616 if (ire != NULL) {
9617 if (ire->ire_flags & RTF_BLACKHOLE) {
9618 ire_refrele(ire);
9619 freemsg(first_mp);
9620 return;
9621 }
9622 ire_refrele(ire);
9623 }
9624 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst);
9625 }
9626
9627 /* Name/Value Table Lookup Routine */
9628 char *
9629 ip_nv_lookup(nv_t *nv, int value)
9630 {
9631 if (!nv)
9632 return (NULL);
9633 for (; nv->nv_name; nv++) {
9634 if (nv->nv_value == value)
9635 return (nv->nv_name);
9636 }
9637 return ("unknown");
9638 }
9639
9640 /*
9641 * This is a module open, i.e. this is a control stream for access
9642 * to a DLPI device. We allocate an ill_t as the instance data in
9643 * this case.
9644 */
9645 int
9646 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
9647 {
9648 ill_t *ill;
9649 int err;
9650 zoneid_t zoneid;
9651 netstack_t *ns;
9652 ip_stack_t *ipst;
9653
9654 /*
9655 * Prevent unprivileged processes from pushing IP so that
9656 * they can't send raw IP.
9657 */
9658 if (secpolicy_net_rawaccess(credp) != 0)
9659 return (EPERM);
9660
9661 ns = netstack_find_by_cred(credp);
9662 ASSERT(ns != NULL);
9663 ipst = ns->netstack_ip;
9664 ASSERT(ipst != NULL);
9665
9666 /*
9667 * For exclusive stacks we set the zoneid to zero
9668 * to make IP operate as if in the global zone.
9669 */
9670 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
9671 zoneid = GLOBAL_ZONEID;
9672 else
9673 zoneid = crgetzoneid(credp);
9674
9675 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
9676 q->q_ptr = WR(q)->q_ptr = ill;
9677 ill->ill_ipst = ipst;
9678 ill->ill_zoneid = zoneid;
9679
9680 /*
9681 * ill_init initializes the ill fields and then sends down
9682 * down a DL_INFO_REQ after calling qprocson.
9683 */
9684 err = ill_init(q, ill);
9685 if (err != 0) {
9686 mi_free(ill);
9687 netstack_rele(ipst->ips_netstack);
9688 q->q_ptr = NULL;
9689 WR(q)->q_ptr = NULL;
9690 return (err);
9691 }
9692
9693 /* ill_init initializes the ipsq marking this thread as writer */
9694 ipsq_exit(ill->ill_phyint->phyint_ipsq);
9695 /* Wait for the DL_INFO_ACK */
9696 mutex_enter(&ill->ill_lock);
9697 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
9698 /*
9699 * Return value of 0 indicates a pending signal.
9700 */
9701 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
9702 if (err == 0) {
9703 mutex_exit(&ill->ill_lock);
9704 (void) ip_close(q, 0);
9705 return (EINTR);
9706 }
9707 }
9708 mutex_exit(&ill->ill_lock);
9709
9710 /*
9711 * ip_rput_other could have set an error in ill_error on
9712 * receipt of M_ERROR.
9713 */
9714
9715 err = ill->ill_error;
9716 if (err != 0) {
9717 (void) ip_close(q, 0);
9718 return (err);
9719 }
9720
9721 ill->ill_credp = credp;
9722 crhold(credp);
9723
9724 mutex_enter(&ipst->ips_ip_mi_lock);
9725 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)ill, devp, flag, sflag,
9726 credp);
9727 mutex_exit(&ipst->ips_ip_mi_lock);
9728 if (err) {
9729 (void) ip_close(q, 0);
9730 return (err);
9731 }
9732 return (0);
9733 }
9734
9735 /* For /dev/ip aka AF_INET open */
9736 int
9737 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
9738 {
9739 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
9740 }
9741
9742 /* For /dev/ip6 aka AF_INET6 open */
9743 int
9744 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
9745 {
9746 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
9747 }
9748
9749 /* IP open routine. */
9750 int
9751 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
9752 boolean_t isv6)
9753 {
9754 conn_t *connp;
9755 major_t maj;
9756 zoneid_t zoneid;
9757 netstack_t *ns;
9758 ip_stack_t *ipst;
9759
9760 TRACE_1(TR_FAC_IP, TR_IP_OPEN, "ip_open: q %p", q);
9761
9762 /* Allow reopen. */
9763 if (q->q_ptr != NULL)
9764 return (0);
9765
9766 if (sflag & MODOPEN) {
9767 /* This is a module open */
9768 return (ip_modopen(q, devp, flag, sflag, credp));
9769 }
9770
9771 ns = netstack_find_by_cred(credp);
9772 ASSERT(ns != NULL);
9773 ipst = ns->netstack_ip;
9774 ASSERT(ipst != NULL);
9775
9776 /*
9777 * For exclusive stacks we set the zoneid to zero
9778 * to make IP operate as if in the global zone.
9779 */
9780 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
9781 zoneid = GLOBAL_ZONEID;
9782 else
9783 zoneid = crgetzoneid(credp);
9784
9785 /*
9786 * We are opening as a device. This is an IP client stream, and we
9787 * allocate an conn_t as the instance data.
9788 */
9789 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
9790
9791 /*
9792 * ipcl_conn_create did a netstack_hold. Undo the hold that was
9793 * done by netstack_find_by_cred()
9794 */
9795 netstack_rele(ipst->ips_netstack);
9796
9797 connp->conn_zoneid = zoneid;
9798
9799 connp->conn_upq = q;
9800 q->q_ptr = WR(q)->q_ptr = connp;
9801
9802 if (flag & SO_SOCKSTR)
9803 connp->conn_flags |= IPCL_SOCKET;
9804
9805 /* Minor tells us which /dev entry was opened */
9806 if (isv6) {
9807 connp->conn_flags |= IPCL_ISV6;
9808 connp->conn_af_isv6 = B_TRUE;
9809 ip_setpktversion(connp, isv6, B_FALSE, ipst);
9810 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT;
9811 } else {
9812 connp->conn_af_isv6 = B_FALSE;
9813 connp->conn_pkt_isv6 = B_FALSE;
9814 }
9815
9816 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
9817 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
9818 connp->conn_minor_arena = ip_minor_arena_la;
9819 } else {
9820 /*
9821 * Either minor numbers in the large arena were exhausted
9822 * or a non socket application is doing the open.
9823 * Try to allocate from the small arena.
9824 */
9825 if ((connp->conn_dev =
9826 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
9827 /* CONN_DEC_REF takes care of netstack_rele() */
9828 q->q_ptr = WR(q)->q_ptr = NULL;
9829 CONN_DEC_REF(connp);
9830 return (EBUSY);
9831 }
9832 connp->conn_minor_arena = ip_minor_arena_sa;
9833 }
9834
9835 maj = getemajor(*devp);
9836 *devp = makedevice(maj, (minor_t)connp->conn_dev);
9837
9838 /*
9839 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
9840 */
9841 connp->conn_cred = credp;
9842
9843 /*
9844 * Handle IP_RTS_REQUEST and other ioctls which use conn_recv
9845 */
9846 connp->conn_recv = ip_conn_input;
9847
9848 crhold(connp->conn_cred);
9849
9850 /*
9851 * If the caller has the process-wide flag set, then default to MAC
9852 * exempt mode. This allows read-down to unlabeled hosts.
9853 */
9854 if (getpflags(NET_MAC_AWARE, credp) != 0)
9855 connp->conn_mac_exempt = B_TRUE;
9856
9857 connp->conn_rq = q;
9858 connp->conn_wq = WR(q);
9859
9860 /* Non-zero default values */
9861 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
9862
9863 /*
9864 * Make the conn globally visible to walkers
9865 */
9866 ASSERT(connp->conn_ref == 1);
9867 mutex_enter(&connp->conn_lock);
9868 connp->conn_state_flags &= ~CONN_INCIPIENT;
9869 mutex_exit(&connp->conn_lock);
9870
9871 qprocson(q);
9872
9873 return (0);
9874 }
9875
9876 /*
9877 * Change the output format (IPv4 vs. IPv6) for a conn_t.
9878 * Note that there is no race since either ip_output function works - it
9879 * is just an optimization to enter the best ip_output routine directly.
9880 */
9881 void
9882 ip_setpktversion(conn_t *connp, boolean_t isv6, boolean_t bump_mib,
9883 ip_stack_t *ipst)
9884 {
9885 if (isv6) {
9886 if (bump_mib) {
9887 BUMP_MIB(&ipst->ips_ip6_mib,
9888 ipIfStatsOutSwitchIPVersion);
9889 }
9890 connp->conn_send = ip_output_v6;
9891 connp->conn_pkt_isv6 = B_TRUE;
9892 } else {
9893 if (bump_mib) {
9894 BUMP_MIB(&ipst->ips_ip_mib,
9895 ipIfStatsOutSwitchIPVersion);
9896 }
9897 connp->conn_send = ip_output;
9898 connp->conn_pkt_isv6 = B_FALSE;
9899 }
9900
9901 }
9902
9903 /*
9904 * See if IPsec needs loading because of the options in mp.
9905 */
9906 static boolean_t
9907 ipsec_opt_present(mblk_t *mp)
9908 {
9909 uint8_t *optcp, *next_optcp, *opt_endcp;
9910 struct opthdr *opt;
9911 struct T_opthdr *topt;
9912 int opthdr_len;
9913 t_uscalar_t optname, optlevel;
9914 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr;
9915 ipsec_req_t *ipsr;
9916
9917 /*
9918 * Walk through the mess, and find IP_SEC_OPT. If it's there,
9919 * return TRUE.
9920 */
9921
9922 optcp = mi_offset_param(mp, tor->OPT_offset, tor->OPT_length);
9923 opt_endcp = optcp + tor->OPT_length;
9924 if (tor->PRIM_type == T_OPTMGMT_REQ) {
9925 opthdr_len = sizeof (struct T_opthdr);
9926 } else { /* O_OPTMGMT_REQ */
9927 ASSERT(tor->PRIM_type == T_SVR4_OPTMGMT_REQ);
9928 opthdr_len = sizeof (struct opthdr);
9929 }
9930 for (; optcp < opt_endcp; optcp = next_optcp) {
9931 if (optcp + opthdr_len > opt_endcp)
9932 return (B_FALSE); /* Not enough option header. */
9933 if (tor->PRIM_type == T_OPTMGMT_REQ) {
9934 topt = (struct T_opthdr *)optcp;
9935 optlevel = topt->level;
9936 optname = topt->name;
9937 next_optcp = optcp + _TPI_ALIGN_TOPT(topt->len);
9938 } else {
9939 opt = (struct opthdr *)optcp;
9940 optlevel = opt->level;
9941 optname = opt->name;
9942 next_optcp = optcp + opthdr_len +
9943 _TPI_ALIGN_OPT(opt->len);
9944 }
9945 if ((next_optcp < optcp) || /* wraparound pointer space */
9946 ((next_optcp >= opt_endcp) && /* last option bad len */
9947 ((next_optcp - opt_endcp) >= __TPI_ALIGN_SIZE)))
9948 return (B_FALSE); /* bad option buffer */
9949 if ((optlevel == IPPROTO_IP && optname == IP_SEC_OPT) ||
9950 (optlevel == IPPROTO_IPV6 && optname == IPV6_SEC_OPT)) {
9951 /*
9952 * Check to see if it's an all-bypass or all-zeroes
9953 * IPsec request. Don't bother loading IPsec if
9954 * the socket doesn't want to use it. (A good example
9955 * is a bypass request.)
9956 *
9957 * Basically, if any of the non-NEVER bits are set,
9958 * load IPsec.
9959 */
9960 ipsr = (ipsec_req_t *)(optcp + opthdr_len);
9961 if ((ipsr->ipsr_ah_req & ~IPSEC_PREF_NEVER) != 0 ||
9962 (ipsr->ipsr_esp_req & ~IPSEC_PREF_NEVER) != 0 ||
9963 (ipsr->ipsr_self_encap_req & ~IPSEC_PREF_NEVER)
9964 != 0)
9965 return (B_TRUE);
9966 }
9967 }
9968 return (B_FALSE);
9969 }
9970
9971 /*
9972 * If conn is is waiting for ipsec to finish loading, kick it.
9973 */
9974 /* ARGSUSED */
9975 static void
9976 conn_restart_ipsec_waiter(conn_t *connp, void *arg)
9977 {
9978 t_scalar_t optreq_prim;
9979 mblk_t *mp;
9980 cred_t *cr;
9981 int err = 0;
9982
9983 /*
9984 * This function is called, after ipsec loading is complete.
9985 * Since IP checks exclusively and atomically (i.e it prevents
9986 * ipsec load from completing until ip_optcom_req completes)
9987 * whether ipsec load is complete, there cannot be a race with IP
9988 * trying to set the CONN_IPSEC_LOAD_WAIT flag on any conn now.
9989 */
9990 mutex_enter(&connp->conn_lock);
9991 if (connp->conn_state_flags & CONN_IPSEC_LOAD_WAIT) {
9992 ASSERT(connp->conn_ipsec_opt_mp != NULL);
9993 mp = connp->conn_ipsec_opt_mp;
9994 connp->conn_ipsec_opt_mp = NULL;
9995 connp->conn_state_flags &= ~CONN_IPSEC_LOAD_WAIT;
9996 cr = DB_CREDDEF(mp, GET_QUEUE_CRED(CONNP_TO_WQ(connp)));
9997 mutex_exit(&connp->conn_lock);
9998
9999 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
10000
10001 optreq_prim = ((union T_primitives *)mp->b_rptr)->type;
10002 if (optreq_prim == T_OPTMGMT_REQ) {
10003 err = tpi_optcom_req(CONNP_TO_WQ(connp), mp, cr,
10004 &ip_opt_obj, B_FALSE);
10005 } else {
10006 ASSERT(optreq_prim == T_SVR4_OPTMGMT_REQ);
10007 err = svr4_optcom_req(CONNP_TO_WQ(connp), mp, cr,
10008 &ip_opt_obj, B_FALSE);
10009 }
10010 if (err != EINPROGRESS)
10011 CONN_OPER_PENDING_DONE(connp);
10012 return;
10013 }
10014 mutex_exit(&connp->conn_lock);
10015 }
10016
10017 /*
10018 * Called from the ipsec_loader thread, outside any perimeter, to tell
10019 * ip qenable any of the queues waiting for the ipsec loader to
10020 * complete.
10021 */
10022 void
10023 ip_ipsec_load_complete(ipsec_stack_t *ipss)
10024 {
10025 netstack_t *ns = ipss->ipsec_netstack;
10026
10027 ipcl_walk(conn_restart_ipsec_waiter, NULL, ns->netstack_ip);
10028 }
10029
10030 /*
10031 * Can't be used. Need to call svr4* -> optset directly. the leaf routine
10032 * determines the grp on which it has to become exclusive, queues the mp
10033 * and sq draining restarts the optmgmt
10034 */
10035 static boolean_t
10036 ip_check_for_ipsec_opt(queue_t *q, mblk_t *mp)
10037 {
10038 conn_t *connp = Q_TO_CONN(q);
10039 ipsec_stack_t *ipss = connp->conn_netstack->netstack_ipsec;
10040
10041 /*
10042 * Take IPsec requests and treat them special.
10043 */
10044 if (ipsec_opt_present(mp)) {
10045 /* First check if IPsec is loaded. */
10046 mutex_enter(&ipss->ipsec_loader_lock);
10047 if (ipss->ipsec_loader_state != IPSEC_LOADER_WAIT) {
10048 mutex_exit(&ipss->ipsec_loader_lock);
10049 return (B_FALSE);
10050 }
10051 mutex_enter(&connp->conn_lock);
10052 connp->conn_state_flags |= CONN_IPSEC_LOAD_WAIT;
10053
10054 ASSERT(connp->conn_ipsec_opt_mp == NULL);
10055 connp->conn_ipsec_opt_mp = mp;
10056 mutex_exit(&connp->conn_lock);
10057 mutex_exit(&ipss->ipsec_loader_lock);
10058
10059 ipsec_loader_loadnow(ipss);
10060 return (B_TRUE);
10061 }
10062 return (B_FALSE);
10063 }
10064
10065 /*
10066 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
10067 * all of them are copied to the conn_t. If the req is "zero", the policy is
10068 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
10069 * fields.
10070 * We keep only the latest setting of the policy and thus policy setting
10071 * is not incremental/cumulative.
10072 *
10073 * Requests to set policies with multiple alternative actions will
10074 * go through a different API.
10075 */
10076 int
10077 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
10078 {
10079 uint_t ah_req = 0;
10080 uint_t esp_req = 0;
10081 uint_t se_req = 0;
10082 ipsec_selkey_t sel;
10083 ipsec_act_t *actp = NULL;
10084 uint_t nact;
10085 ipsec_policy_t *pin4 = NULL, *pout4 = NULL;
10086 ipsec_policy_t *pin6 = NULL, *pout6 = NULL;
10087 ipsec_policy_root_t *pr;
10088 ipsec_policy_head_t *ph;
10089 int fam;
10090 boolean_t is_pol_reset;
10091 int error = 0;
10092 netstack_t *ns = connp->conn_netstack;
10093 ip_stack_t *ipst = ns->netstack_ip;
10094 ipsec_stack_t *ipss = ns->netstack_ipsec;
10095
10096 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
10097
10098 /*
10099 * The IP_SEC_OPT option does not allow variable length parameters,
10100 * hence a request cannot be NULL.
10101 */
10102 if (req == NULL)
10103 return (EINVAL);
10104
10105 ah_req = req->ipsr_ah_req;
10106 esp_req = req->ipsr_esp_req;
10107 se_req = req->ipsr_self_encap_req;
10108
10109 /* Don't allow setting self-encap without one or more of AH/ESP. */
10110 if (se_req != 0 && esp_req == 0 && ah_req == 0)
10111 return (EINVAL);
10112
10113 /*
10114 * Are we dealing with a request to reset the policy (i.e.
10115 * zero requests).
10116 */
10117 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
10118 (esp_req & REQ_MASK) == 0 &&
10119 (se_req & REQ_MASK) == 0);
10120
10121 if (!is_pol_reset) {
10122 /*
10123 * If we couldn't load IPsec, fail with "protocol
10124 * not supported".
10125 * IPsec may not have been loaded for a request with zero
10126 * policies, so we don't fail in this case.
10127 */
10128 mutex_enter(&ipss->ipsec_loader_lock);
10129 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
10130 mutex_exit(&ipss->ipsec_loader_lock);
10131 return (EPROTONOSUPPORT);
10132 }
10133 mutex_exit(&ipss->ipsec_loader_lock);
10134
10135 /*
10136 * Test for valid requests. Invalid algorithms
10137 * need to be tested by IPsec code because new
10138 * algorithms can be added dynamically.
10139 */
10140 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
10141 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
10142 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
10143 return (EINVAL);
10144 }
10145
10146 /*
10147 * Only privileged users can issue these
10148 * requests.
10149 */
10150 if (((ah_req & IPSEC_PREF_NEVER) ||
10151 (esp_req & IPSEC_PREF_NEVER) ||
10152 (se_req & IPSEC_PREF_NEVER)) &&
10153 secpolicy_ip_config(cr, B_FALSE) != 0) {
10154 return (EPERM);
10155 }
10156
10157 /*
10158 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
10159 * are mutually exclusive.
10160 */
10161 if (((ah_req & REQ_MASK) == REQ_MASK) ||
10162 ((esp_req & REQ_MASK) == REQ_MASK) ||
10163 ((se_req & REQ_MASK) == REQ_MASK)) {
10164 /* Both of them are set */
10165 return (EINVAL);
10166 }
10167 }
10168
10169 mutex_enter(&connp->conn_lock);
10170
10171 /*
10172 * If we have already cached policies in ip_bind_connected*(), don't
10173 * let them change now. We cache policies for connections
10174 * whose src,dst [addr, port] is known.
10175 */
10176 if (connp->conn_policy_cached) {
10177 mutex_exit(&connp->conn_lock);
10178 return (EINVAL);
10179 }
10180
10181 /*
10182 * We have a zero policies, reset the connection policy if already
10183 * set. This will cause the connection to inherit the
10184 * global policy, if any.
10185 */
10186 if (is_pol_reset) {
10187 if (connp->conn_policy != NULL) {
10188 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
10189 connp->conn_policy = NULL;
10190 }
10191 connp->conn_flags &= ~IPCL_CHECK_POLICY;
10192 connp->conn_in_enforce_policy = B_FALSE;
10193 connp->conn_out_enforce_policy = B_FALSE;
10194 mutex_exit(&connp->conn_lock);
10195 return (0);
10196 }
10197
10198 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
10199 ipst->ips_netstack);
10200 if (ph == NULL)
10201 goto enomem;
10202
10203 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
10204 if (actp == NULL)
10205 goto enomem;
10206
10207 /*
10208 * Always allocate IPv4 policy entries, since they can also
10209 * apply to ipv6 sockets being used in ipv4-compat mode.
10210 */
10211 bzero(&sel, sizeof (sel));
10212 sel.ipsl_valid = IPSL_IPV4;
10213
10214 pin4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL,
10215 ipst->ips_netstack);
10216 if (pin4 == NULL)
10217 goto enomem;
10218
10219 pout4 = ipsec_policy_create(&sel, actp, nact, IPSEC_PRIO_SOCKET, NULL,
10220 ipst->ips_netstack);
10221 if (pout4 == NULL)
10222 goto enomem;
10223
10224 if (connp->conn_af_isv6) {
10225 /*
10226 * We're looking at a v6 socket, also allocate the
10227 * v6-specific entries...
10228 */
10229 sel.ipsl_valid = IPSL_IPV6;
10230 pin6 = ipsec_policy_create(&sel, actp, nact,
10231 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack);
10232 if (pin6 == NULL)
10233 goto enomem;
10234
10235 pout6 = ipsec_policy_create(&sel, actp, nact,
10236 IPSEC_PRIO_SOCKET, NULL, ipst->ips_netstack);
10237 if (pout6 == NULL)
10238 goto enomem;
10239
10240 /*
10241 * .. and file them away in the right place.
10242 */
10243 fam = IPSEC_AF_V6;
10244 pr = &ph->iph_root[IPSEC_TYPE_INBOUND];
10245 HASHLIST_INSERT(pin6, ipsp_hash, pr->ipr_nonhash[fam]);
10246 ipsec_insert_always(&ph->iph_rulebyid, pin6);
10247 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND];
10248 HASHLIST_INSERT(pout6, ipsp_hash, pr->ipr_nonhash[fam]);
10249 ipsec_insert_always(&ph->iph_rulebyid, pout6);
10250 }
10251
10252 ipsec_actvec_free(actp, nact);
10253
10254 /*
10255 * File the v4 policies.
10256 */
10257 fam = IPSEC_AF_V4;
10258 pr = &ph->iph_root[IPSEC_TYPE_INBOUND];
10259 HASHLIST_INSERT(pin4, ipsp_hash, pr->ipr_nonhash[fam]);
10260 ipsec_insert_always(&ph->iph_rulebyid, pin4);
10261
10262 pr = &ph->iph_root[IPSEC_TYPE_OUTBOUND];
10263 HASHLIST_INSERT(pout4, ipsp_hash, pr->ipr_nonhash[fam]);
10264 ipsec_insert_always(&ph->iph_rulebyid, pout4);
10265
10266 /*
10267 * If the requests need security, set enforce_policy.
10268 * If the requests are IPSEC_PREF_NEVER, one should
10269 * still set conn_out_enforce_policy so that an ipsec_out
10270 * gets attached in ip_wput. This is needed so that
10271 * for connections that we don't cache policy in ip_bind,
10272 * if global policy matches in ip_wput_attach_policy, we
10273 * don't wrongly inherit global policy. Similarly, we need
10274 * to set conn_in_enforce_policy also so that we don't verify
10275 * policy wrongly.
10276 */
10277 if ((ah_req & REQ_MASK) != 0 ||
10278 (esp_req & REQ_MASK) != 0 ||
10279 (se_req & REQ_MASK) != 0) {
10280 connp->conn_in_enforce_policy = B_TRUE;
10281 connp->conn_out_enforce_policy = B_TRUE;
10282 connp->conn_flags |= IPCL_CHECK_POLICY;
10283 }
10284
10285 mutex_exit(&connp->conn_lock);
10286 return (error);
10287 #undef REQ_MASK
10288
10289 /*
10290 * Common memory-allocation-failure exit path.
10291 */
10292 enomem:
10293 mutex_exit(&connp->conn_lock);
10294 if (actp != NULL)
10295 ipsec_actvec_free(actp, nact);
10296 if (pin4 != NULL)
10297 IPPOL_REFRELE(pin4, ipst->ips_netstack);
10298 if (pout4 != NULL)
10299 IPPOL_REFRELE(pout4, ipst->ips_netstack);
10300 if (pin6 != NULL)
10301 IPPOL_REFRELE(pin6, ipst->ips_netstack);
10302 if (pout6 != NULL)
10303 IPPOL_REFRELE(pout6, ipst->ips_netstack);
10304 return (ENOMEM);
10305 }
10306
10307 /*
10308 * Only for options that pass in an IP addr. Currently only V4 options
10309 * pass in an ipif. V6 options always pass an ifindex specifying the ill.
10310 * So this function assumes level is IPPROTO_IP
10311 */
10312 int
10313 ip_opt_set_ipif(conn_t *connp, ipaddr_t addr, boolean_t checkonly, int option,
10314 mblk_t *first_mp)
10315 {
10316 ipif_t *ipif = NULL;
10317 int error;
10318 ill_t *ill;
10319 int zoneid;
10320 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
10321
10322 ip2dbg(("ip_opt_set_ipif: ipaddr %X\n", addr));
10323
10324 if (addr != INADDR_ANY || checkonly) {
10325 ASSERT(connp != NULL);
10326 zoneid = IPCL_ZONEID(connp);
10327 if (option == IP_NEXTHOP) {
10328 ipif = ipif_lookup_onlink_addr(addr,
10329 connp->conn_zoneid, ipst);
10330 } else {
10331 ipif = ipif_lookup_addr(addr, NULL, zoneid,
10332 CONNP_TO_WQ(connp), first_mp, ip_restart_optmgmt,
10333 &error, ipst);
10334 }
10335 if (ipif == NULL) {
10336 if (error == EINPROGRESS)
10337 return (error);
10338 else if ((option == IP_MULTICAST_IF) ||
10339 (option == IP_NEXTHOP))
10340 return (EHOSTUNREACH);
10341 else
10342 return (EINVAL);
10343 } else if (checkonly) {
10344 if (option == IP_MULTICAST_IF) {
10345 ill = ipif->ipif_ill;
10346 /* not supported by the virtual network iface */
10347 if (IS_VNI(ill)) {
10348 ipif_refrele(ipif);
10349 return (EINVAL);
10350 }
10351 }
10352 ipif_refrele(ipif);
10353 return (0);
10354 }
10355 ill = ipif->ipif_ill;
10356 mutex_enter(&connp->conn_lock);
10357 mutex_enter(&ill->ill_lock);
10358 if ((ill->ill_state_flags & ILL_CONDEMNED) ||
10359 (ipif->ipif_state_flags & IPIF_CONDEMNED)) {
10360 mutex_exit(&ill->ill_lock);
10361 mutex_exit(&connp->conn_lock);
10362 ipif_refrele(ipif);
10363 return (option == IP_MULTICAST_IF ?
10364 EHOSTUNREACH : EINVAL);
10365 }
10366 } else {
10367 mutex_enter(&connp->conn_lock);
10368 }
10369
10370 /* None of the options below are supported on the VNI */
10371 if (ipif != NULL && IS_VNI(ipif->ipif_ill)) {
10372 mutex_exit(&ill->ill_lock);
10373 mutex_exit(&connp->conn_lock);
10374 ipif_refrele(ipif);
10375 return (EINVAL);
10376 }
10377
10378 switch (option) {
10379 case IP_DONTFAILOVER_IF:
10380 /*
10381 * This option is used by in.mpathd to ensure
10382 * that IPMP probe packets only go out on the
10383 * test interfaces. in.mpathd sets this option
10384 * on the non-failover interfaces.
10385 * For backward compatibility, this option
10386 * implicitly sets IP_MULTICAST_IF, as used
10387 * be done in bind(), so that ip_wput gets
10388 * this ipif to send mcast packets.
10389 */
10390 if (ipif != NULL) {
10391 ASSERT(addr != INADDR_ANY);
10392 connp->conn_nofailover_ill = ipif->ipif_ill;
10393 connp->conn_multicast_ipif = ipif;
10394 } else {
10395 ASSERT(addr == INADDR_ANY);
10396 connp->conn_nofailover_ill = NULL;
10397 connp->conn_multicast_ipif = NULL;
10398 }
10399 break;
10400
10401 case IP_MULTICAST_IF:
10402 connp->conn_multicast_ipif = ipif;
10403 break;
10404 case IP_NEXTHOP:
10405 connp->conn_nexthop_v4 = addr;
10406 connp->conn_nexthop_set = B_TRUE;
10407 break;
10408 }
10409
10410 if (ipif != NULL) {
10411 mutex_exit(&ill->ill_lock);
10412 mutex_exit(&connp->conn_lock);
10413 ipif_refrele(ipif);
10414 return (0);
10415 }
10416 mutex_exit(&connp->conn_lock);
10417 /* We succeded in cleared the option */
10418 return (0);
10419 }
10420
10421 /*
10422 * For options that pass in an ifindex specifying the ill. V6 options always
10423 * pass in an ill. Some v4 options also pass in ifindex specifying the ill.
10424 */
10425 int
10426 ip_opt_set_ill(conn_t *connp, int ifindex, boolean_t isv6, boolean_t checkonly,
10427 int level, int option, mblk_t *first_mp)
10428 {
10429 ill_t *ill = NULL;
10430 int error = 0;
10431 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
10432
10433 ip2dbg(("ip_opt_set_ill: ifindex %d\n", ifindex));
10434 if (ifindex != 0) {
10435 ASSERT(connp != NULL);
10436 ill = ill_lookup_on_ifindex(ifindex, isv6, CONNP_TO_WQ(connp),
10437 first_mp, ip_restart_optmgmt, &error, ipst);
10438 if (ill != NULL) {
10439 if (checkonly) {
10440 /* not supported by the virtual network iface */
10441 if (IS_VNI(ill)) {
10442 ill_refrele(ill);
10443 return (EINVAL);
10444 }
10445 ill_refrele(ill);
10446 return (0);
10447 }
10448 if (!ipif_lookup_zoneid_group(ill, connp->conn_zoneid,
10449 0, NULL)) {
10450 ill_refrele(ill);
10451 ill = NULL;
10452 mutex_enter(&connp->conn_lock);
10453 goto setit;
10454 }
10455 mutex_enter(&connp->conn_lock);
10456 mutex_enter(&ill->ill_lock);
10457 if (ill->ill_state_flags & ILL_CONDEMNED) {
10458 mutex_exit(&ill->ill_lock);
10459 mutex_exit(&connp->conn_lock);
10460 ill_refrele(ill);
10461 ill = NULL;
10462 mutex_enter(&connp->conn_lock);
10463 }
10464 goto setit;
10465 } else if (error == EINPROGRESS) {
10466 return (error);
10467 } else {
10468 error = 0;
10469 }
10470 }
10471 mutex_enter(&connp->conn_lock);
10472 setit:
10473 ASSERT((level == IPPROTO_IP || level == IPPROTO_IPV6));
10474
10475 /*
10476 * The options below assume that the ILL (if any) transmits and/or
10477 * receives traffic. Neither of which is true for the virtual network
10478 * interface, so fail setting these on a VNI.
10479 */
10480 if (IS_VNI(ill)) {
10481 ASSERT(ill != NULL);
10482 mutex_exit(&ill->ill_lock);
10483 mutex_exit(&connp->conn_lock);
10484 ill_refrele(ill);
10485 return (EINVAL);
10486 }
10487
10488 if (level == IPPROTO_IP) {
10489 switch (option) {
10490 case IP_BOUND_IF:
10491 connp->conn_incoming_ill = ill;
10492 connp->conn_outgoing_ill = ill;
10493 connp->conn_orig_bound_ifindex = (ill == NULL) ?
10494 0 : ifindex;
10495 break;
10496
10497 case IP_MULTICAST_IF:
10498 /*
10499 * This option is an internal special. The socket
10500 * level IP_MULTICAST_IF specifies an 'ipaddr' and
10501 * is handled in ip_opt_set_ipif. IPV6_MULTICAST_IF
10502 * specifies an ifindex and we try first on V6 ill's.
10503 * If we don't find one, we they try using on v4 ill's
10504 * intenally and we come here.
10505 */
10506 if (!checkonly && ill != NULL) {
10507 ipif_t *ipif;
10508 ipif = ill->ill_ipif;
10509
10510 if (ipif->ipif_state_flags & IPIF_CONDEMNED) {
10511 mutex_exit(&ill->ill_lock);
10512 mutex_exit(&connp->conn_lock);
10513 ill_refrele(ill);
10514 ill = NULL;
10515 mutex_enter(&connp->conn_lock);
10516 } else {
10517 connp->conn_multicast_ipif = ipif;
10518 }
10519 }
10520 break;
10521
10522 case IP_DHCPINIT_IF:
10523 if (connp->conn_dhcpinit_ill != NULL) {
10524 /*
10525 * We've locked the conn so conn_cleanup_ill()
10526 * cannot clear conn_dhcpinit_ill -- so it's
10527 * safe to access the ill.
10528 */
10529 ill_t *oill = connp->conn_dhcpinit_ill;
10530
10531 ASSERT(oill->ill_dhcpinit != 0);
10532 atomic_dec_32(&oill->ill_dhcpinit);
10533 connp->conn_dhcpinit_ill = NULL;
10534 }
10535
10536 if (ill != NULL) {
10537 connp->conn_dhcpinit_ill = ill;
10538 atomic_inc_32(&ill->ill_dhcpinit);
10539 }
10540 break;
10541 }
10542 } else {
10543 switch (option) {
10544 case IPV6_BOUND_IF:
10545 connp->conn_incoming_ill = ill;
10546 connp->conn_outgoing_ill = ill;
10547 connp->conn_orig_bound_ifindex = (ill == NULL) ?
10548 0 : ifindex;
10549 break;
10550
10551 case IPV6_BOUND_PIF:
10552 /*
10553 * Limit all transmit to this ill.
10554 * Unlike IPV6_BOUND_IF, using this option
10555 * prevents load spreading and failover from
10556 * happening when the interface is part of the
10557 * group. That's why we don't need to remember
10558 * the ifindex in orig_bound_ifindex as in
10559 * IPV6_BOUND_IF.
10560 */
10561 connp->conn_outgoing_pill = ill;
10562 break;
10563
10564 case IPV6_DONTFAILOVER_IF:
10565 /*
10566 * This option is used by in.mpathd to ensure
10567 * that IPMP probe packets only go out on the
10568 * test interfaces. in.mpathd sets this option
10569 * on the non-failover interfaces.
10570 */
10571 connp->conn_nofailover_ill = ill;
10572 /*
10573 * For backward compatibility, this option
10574 * implicitly sets ip_multicast_ill as used in
10575 * IPV6_MULTICAST_IF so that ip_wput gets
10576 * this ill to send mcast packets.
10577 */
10578 connp->conn_multicast_ill = ill;
10579 connp->conn_orig_multicast_ifindex = (ill == NULL) ?
10580 0 : ifindex;
10581 break;
10582
10583 case IPV6_MULTICAST_IF:
10584 /*
10585 * Set conn_multicast_ill to be the IPv6 ill.
10586 * Set conn_multicast_ipif to be an IPv4 ipif
10587 * for ifindex to make IPv4 mapped addresses
10588 * on PF_INET6 sockets honor IPV6_MULTICAST_IF.
10589 * Even if no IPv6 ill exists for the ifindex
10590 * we need to check for an IPv4 ifindex in order
10591 * for this to work with mapped addresses. In that
10592 * case only set conn_multicast_ipif.
10593 */
10594 if (!checkonly) {
10595 if (ifindex == 0) {
10596 connp->conn_multicast_ill = NULL;
10597 connp->conn_orig_multicast_ifindex = 0;
10598 connp->conn_multicast_ipif = NULL;
10599 } else if (ill != NULL) {
10600 connp->conn_multicast_ill = ill;
10601 connp->conn_orig_multicast_ifindex =
10602 ifindex;
10603 }
10604 }
10605 break;
10606 }
10607 }
10608
10609 if (ill != NULL) {
10610 mutex_exit(&ill->ill_lock);
10611 mutex_exit(&connp->conn_lock);
10612 ill_refrele(ill);
10613 return (0);
10614 }
10615 mutex_exit(&connp->conn_lock);
10616 /*
10617 * We succeeded in clearing the option (ifindex == 0) or failed to
10618 * locate the ill and could not set the option (ifindex != 0)
10619 */
10620 return (ifindex == 0 ? 0 : EINVAL);
10621 }
10622
10623 /* This routine sets socket options. */
10624 /* ARGSUSED */
10625 int
10626 ip_opt_set(queue_t *q, uint_t optset_context, int level, int name,
10627 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
10628 void *dummy, cred_t *cr, mblk_t *first_mp)
10629 {
10630 int *i1 = (int *)invalp;
10631 conn_t *connp = Q_TO_CONN(q);
10632 int error = 0;
10633 boolean_t checkonly;
10634 ire_t *ire;
10635 boolean_t found;
10636 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
10637
10638 switch (optset_context) {
10639
10640 case SETFN_OPTCOM_CHECKONLY:
10641 checkonly = B_TRUE;
10642 /*
10643 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
10644 * inlen != 0 implies value supplied and
10645 * we have to "pretend" to set it.
10646 * inlen == 0 implies that there is no
10647 * value part in T_CHECK request and just validation
10648 * done elsewhere should be enough, we just return here.
10649 */
10650 if (inlen == 0) {
10651 *outlenp = 0;
10652 return (0);
10653 }
10654 break;
10655 case SETFN_OPTCOM_NEGOTIATE:
10656 case SETFN_UD_NEGOTIATE:
10657 case SETFN_CONN_NEGOTIATE:
10658 checkonly = B_FALSE;
10659 break;
10660 default:
10661 /*
10662 * We should never get here
10663 */
10664 *outlenp = 0;
10665 return (EINVAL);
10666 }
10667
10668 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
10669 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
10670
10671 /*
10672 * For fixed length options, no sanity check
10673 * of passed in length is done. It is assumed *_optcom_req()
10674 * routines do the right thing.
10675 */
10676
10677 switch (level) {
10678 case SOL_SOCKET:
10679 /*
10680 * conn_lock protects the bitfields, and is used to
10681 * set the fields atomically.
10682 */
10683 switch (name) {
10684 case SO_BROADCAST:
10685 if (!checkonly) {
10686 /* TODO: use value someplace? */
10687 mutex_enter(&connp->conn_lock);
10688 connp->conn_broadcast = *i1 ? 1 : 0;
10689 mutex_exit(&connp->conn_lock);
10690 }
10691 break; /* goto sizeof (int) option return */
10692 case SO_USELOOPBACK:
10693 if (!checkonly) {
10694 /* TODO: use value someplace? */
10695 mutex_enter(&connp->conn_lock);
10696 connp->conn_loopback = *i1 ? 1 : 0;
10697 mutex_exit(&connp->conn_lock);
10698 }
10699 break; /* goto sizeof (int) option return */
10700 case SO_DONTROUTE:
10701 if (!checkonly) {
10702 mutex_enter(&connp->conn_lock);
10703 connp->conn_dontroute = *i1 ? 1 : 0;
10704 mutex_exit(&connp->conn_lock);
10705 }
10706 break; /* goto sizeof (int) option return */
10707 case SO_REUSEADDR:
10708 if (!checkonly) {
10709 mutex_enter(&connp->conn_lock);
10710 connp->conn_reuseaddr = *i1 ? 1 : 0;
10711 mutex_exit(&connp->conn_lock);
10712 }
10713 break; /* goto sizeof (int) option return */
10714 case SO_PROTOTYPE:
10715 if (!checkonly) {
10716 mutex_enter(&connp->conn_lock);
10717 connp->conn_proto = *i1;
10718 mutex_exit(&connp->conn_lock);
10719 }
10720 break; /* goto sizeof (int) option return */
10721 case SO_ALLZONES:
10722 if (!checkonly) {
10723 mutex_enter(&connp->conn_lock);
10724 if (IPCL_IS_BOUND(connp)) {
10725 mutex_exit(&connp->conn_lock);
10726 return (EINVAL);
10727 }
10728 connp->conn_allzones = *i1 != 0 ? 1 : 0;
10729 mutex_exit(&connp->conn_lock);
10730 }
10731 break; /* goto sizeof (int) option return */
10732 case SO_ANON_MLP:
10733 if (!checkonly) {
10734 mutex_enter(&connp->conn_lock);
10735 connp->conn_anon_mlp = *i1 != 0 ? 1 : 0;
10736 mutex_exit(&connp->conn_lock);
10737 }
10738 break; /* goto sizeof (int) option return */
10739 case SO_MAC_EXEMPT:
10740 if (secpolicy_net_mac_aware(cr) != 0 ||
10741 IPCL_IS_BOUND(connp))
10742 return (EACCES);
10743 if (!checkonly) {
10744 mutex_enter(&connp->conn_lock);
10745 connp->conn_mac_exempt = *i1 != 0 ? 1 : 0;
10746 mutex_exit(&connp->conn_lock);
10747 }
10748 break; /* goto sizeof (int) option return */
10749 default:
10750 /*
10751 * "soft" error (negative)
10752 * option not handled at this level
10753 * Note: Do not modify *outlenp
10754