xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_if.c (revision ab82c29b6e890d0f1241f9cd0cefda3430f46bd5)
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  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 1990 Mentat Inc.
24  * Copyright (c) 2013 by Delphix. All rights reserved.
25  * Copyright (c) 2016, Joyent, Inc. All rights reserved.
26  * Copyright (c) 2014, OmniTI Computer Consulting, Inc. All rights reserved.
27  */
28 
29 /*
30  * This file contains the interface control functions for IP.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stream.h>
35 #include <sys/dlpi.h>
36 #include <sys/stropts.h>
37 #include <sys/strsun.h>
38 #include <sys/sysmacros.h>
39 #include <sys/strsubr.h>
40 #include <sys/strlog.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/cmn_err.h>
44 #include <sys/kstat.h>
45 #include <sys/debug.h>
46 #include <sys/zone.h>
47 #include <sys/sunldi.h>
48 #include <sys/file.h>
49 #include <sys/bitmap.h>
50 #include <sys/cpuvar.h>
51 #include <sys/time.h>
52 #include <sys/ctype.h>
53 #include <sys/kmem.h>
54 #include <sys/systm.h>
55 #include <sys/param.h>
56 #include <sys/socket.h>
57 #include <sys/isa_defs.h>
58 #include <net/if.h>
59 #include <net/if_arp.h>
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/route.h>
63 #include <sys/sockio.h>
64 #include <netinet/in.h>
65 #include <netinet/ip6.h>
66 #include <netinet/icmp6.h>
67 #include <netinet/igmp_var.h>
68 #include <sys/policy.h>
69 #include <sys/ethernet.h>
70 #include <sys/callb.h>
71 #include <sys/md5.h>
72 
73 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
74 #include <inet/mi.h>
75 #include <inet/nd.h>
76 #include <inet/tunables.h>
77 #include <inet/arp.h>
78 #include <inet/ip_arp.h>
79 #include <inet/mib2.h>
80 #include <inet/ip.h>
81 #include <inet/ip6.h>
82 #include <inet/ip6_asp.h>
83 #include <inet/tcp.h>
84 #include <inet/ip_multi.h>
85 #include <inet/ip_ire.h>
86 #include <inet/ip_ftable.h>
87 #include <inet/ip_rts.h>
88 #include <inet/ip_ndp.h>
89 #include <inet/ip_if.h>
90 #include <inet/ip_impl.h>
91 #include <inet/sctp_ip.h>
92 #include <inet/ip_netinfo.h>
93 #include <inet/ilb_ip.h>
94 
95 #include <netinet/igmp.h>
96 #include <inet/ip_listutils.h>
97 #include <inet/ipclassifier.h>
98 #include <sys/mac_client.h>
99 #include <sys/dld.h>
100 #include <sys/mac_flow.h>
101 
102 #include <sys/systeminfo.h>
103 #include <sys/bootconf.h>
104 
105 #include <sys/tsol/tndb.h>
106 #include <sys/tsol/tnet.h>
107 
108 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
109 #include <inet/udp_impl.h> /* needed for udp_stack_t */
110 
111 /* The character which tells where the ill_name ends */
112 #define	IPIF_SEPARATOR_CHAR	':'
113 
114 /* IP ioctl function table entry */
115 typedef struct ipft_s {
116 	int	ipft_cmd;
117 	pfi_t	ipft_pfi;
118 	int	ipft_min_size;
119 	int	ipft_flags;
120 } ipft_t;
121 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
122 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
123 
124 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
125 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
126 		    char *value, caddr_t cp, cred_t *ioc_cr);
127 
128 static boolean_t ill_is_quiescent(ill_t *);
129 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
130 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
131 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
132     mblk_t *mp, boolean_t need_up);
133 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
134     mblk_t *mp, boolean_t need_up);
135 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
136     queue_t *q, mblk_t *mp, boolean_t need_up);
137 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
138     mblk_t *mp);
139 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
140     mblk_t *mp);
141 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
142     queue_t *q, mblk_t *mp, boolean_t need_up);
143 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
144     int ioccmd, struct linkblk *li);
145 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
146 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
147 static void	ipsq_flush(ill_t *ill);
148 
149 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
150     queue_t *q, mblk_t *mp, boolean_t need_up);
151 static void	ipsq_delete(ipsq_t *);
152 
153 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
154     boolean_t initialize, boolean_t insert, int *errorp);
155 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
156 static void	ipif_delete_bcast_ires(ipif_t *ipif);
157 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
158 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
159 		    boolean_t isv6);
160 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
161 static void	ipif_free(ipif_t *ipif);
162 static void	ipif_free_tail(ipif_t *ipif);
163 static void	ipif_set_default(ipif_t *ipif);
164 static int	ipif_set_values(queue_t *q, mblk_t *mp,
165     char *interf_name, uint_t *ppa);
166 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
167     queue_t *q);
168 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
169     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
170     ip_stack_t *);
171 static ipif_t	*ipif_lookup_on_name_async(char *name, size_t namelen,
172     boolean_t isv6, zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func,
173     int *error, ip_stack_t *);
174 
175 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
176 static void	ill_delete_interface_type(ill_if_t *);
177 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
178 static void	ill_dl_down(ill_t *ill);
179 static void	ill_down(ill_t *ill);
180 static void	ill_down_ipifs(ill_t *, boolean_t);
181 static void	ill_free_mib(ill_t *ill);
182 static void	ill_glist_delete(ill_t *);
183 static void	ill_phyint_reinit(ill_t *ill);
184 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
185 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
186 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
187 
188 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
189 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
190 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
191 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
192 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
193 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
194 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
195 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
196 static ip_v4mapinfo_func_t ip_mbcast_mapping;
197 static void	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
198 static void	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
199 static void	phyint_free(phyint_t *);
200 
201 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
202 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
203 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
204 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
205 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
206 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
207     dl_capability_sub_t *);
208 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
209 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
210 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
211 		    dl_capability_sub_t *);
212 static void	ill_capability_dld_enable(ill_t *);
213 static void	ill_capability_ack_thr(void *);
214 static void	ill_capability_lso_enable(ill_t *);
215 
216 static ill_t	*ill_prev_usesrc(ill_t *);
217 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
218 static void	ill_disband_usesrc_group(ill_t *);
219 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
220 
221 #ifdef DEBUG
222 static	void	ill_trace_cleanup(const ill_t *);
223 static	void	ipif_trace_cleanup(const ipif_t *);
224 #endif
225 
226 static	void	ill_dlpi_clear_deferred(ill_t *ill);
227 
228 static	void	phyint_flags_init(phyint_t *, t_uscalar_t);
229 
230 /*
231  * if we go over the memory footprint limit more than once in this msec
232  * interval, we'll start pruning aggressively.
233  */
234 int ip_min_frag_prune_time = 0;
235 
236 static ipft_t	ip_ioctl_ftbl[] = {
237 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
238 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
239 		IPFT_F_NO_REPLY },
240 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
241 	{ 0 }
242 };
243 
244 /* Simple ICMP IP Header Template */
245 static ipha_t icmp_ipha = {
246 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
247 };
248 
249 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
250 
251 static ip_m_t   ip_m_tbl[] = {
252 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
254 	    ip_nodef_v6intfid },
255 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
257 	    ip_nodef_v6intfid },
258 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
259 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
260 	    ip_nodef_v6intfid },
261 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
262 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
263 	    ip_nodef_v6intfid },
264 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
265 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
266 	    ip_nodef_v6intfid },
267 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
268 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
269 	    ip_nodef_v6intfid },
270 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
271 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
272 	    ip_ipv4_v6destintfid },
273 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
274 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
275 	    ip_ipv6_v6destintfid },
276 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
277 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
278 	    ip_nodef_v6intfid },
279 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
280 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
281 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
282 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
283 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
284 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
285 	    ip_nodef_v6intfid }
286 };
287 
288 char	ipif_loopback_name[] = "lo0";
289 
290 /* These are used by all IP network modules. */
291 sin6_t	sin6_null;	/* Zero address for quick clears */
292 sin_t	sin_null;	/* Zero address for quick clears */
293 
294 /* When set search for unused ipif_seqid */
295 static ipif_t	ipif_zero;
296 
297 /*
298  * ppa arena is created after these many
299  * interfaces have been plumbed.
300  */
301 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
302 
303 /*
304  * Allocate per-interface mibs.
305  * Returns true if ok. False otherwise.
306  *  ipsq  may not yet be allocated (loopback case ).
307  */
308 static boolean_t
309 ill_allocate_mibs(ill_t *ill)
310 {
311 	/* Already allocated? */
312 	if (ill->ill_ip_mib != NULL) {
313 		if (ill->ill_isv6)
314 			ASSERT(ill->ill_icmp6_mib != NULL);
315 		return (B_TRUE);
316 	}
317 
318 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
319 	    KM_NOSLEEP);
320 	if (ill->ill_ip_mib == NULL) {
321 		return (B_FALSE);
322 	}
323 
324 	/* Setup static information */
325 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
326 	    sizeof (mib2_ipIfStatsEntry_t));
327 	if (ill->ill_isv6) {
328 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
329 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
330 		    sizeof (mib2_ipv6AddrEntry_t));
331 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
332 		    sizeof (mib2_ipv6RouteEntry_t));
333 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
334 		    sizeof (mib2_ipv6NetToMediaEntry_t));
335 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
336 		    sizeof (ipv6_member_t));
337 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
338 		    sizeof (ipv6_grpsrc_t));
339 	} else {
340 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
341 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
342 		    sizeof (mib2_ipAddrEntry_t));
343 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
344 		    sizeof (mib2_ipRouteEntry_t));
345 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
346 		    sizeof (mib2_ipNetToMediaEntry_t));
347 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
348 		    sizeof (ip_member_t));
349 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
350 		    sizeof (ip_grpsrc_t));
351 
352 		/*
353 		 * For a v4 ill, we are done at this point, because per ill
354 		 * icmp mibs are only used for v6.
355 		 */
356 		return (B_TRUE);
357 	}
358 
359 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
360 	    KM_NOSLEEP);
361 	if (ill->ill_icmp6_mib == NULL) {
362 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
363 		ill->ill_ip_mib = NULL;
364 		return (B_FALSE);
365 	}
366 	/* static icmp info */
367 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
368 	    sizeof (mib2_ipv6IfIcmpEntry_t);
369 	/*
370 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
371 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
372 	 * -> ill_phyint_reinit
373 	 */
374 	return (B_TRUE);
375 }
376 
377 /*
378  * Completely vaporize a lower level tap and all associated interfaces.
379  * ill_delete is called only out of ip_close when the device control
380  * stream is being closed.
381  */
382 void
383 ill_delete(ill_t *ill)
384 {
385 	ipif_t	*ipif;
386 	ill_t	*prev_ill;
387 	ip_stack_t	*ipst = ill->ill_ipst;
388 
389 	/*
390 	 * ill_delete may be forcibly entering the ipsq. The previous
391 	 * ioctl may not have completed and may need to be aborted.
392 	 * ipsq_flush takes care of it. If we don't need to enter the
393 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
394 	 * ill_delete_tail is sufficient.
395 	 */
396 	ipsq_flush(ill);
397 
398 	/*
399 	 * Nuke all interfaces.  ipif_free will take down the interface,
400 	 * remove it from the list, and free the data structure.
401 	 * Walk down the ipif list and remove the logical interfaces
402 	 * first before removing the main ipif. We can't unplumb
403 	 * zeroth interface first in the case of IPv6 as update_conn_ill
404 	 * -> ip_ll_multireq de-references ill_ipif for checking
405 	 * POINTOPOINT.
406 	 *
407 	 * If ill_ipif was not properly initialized (i.e low on memory),
408 	 * then no interfaces to clean up. In this case just clean up the
409 	 * ill.
410 	 */
411 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
412 		ipif_free(ipif);
413 
414 	/*
415 	 * clean out all the nce_t entries that depend on this
416 	 * ill for the ill_phys_addr.
417 	 */
418 	nce_flush(ill, B_TRUE);
419 
420 	/* Clean up msgs on pending upcalls for mrouted */
421 	reset_mrt_ill(ill);
422 
423 	update_conn_ill(ill, ipst);
424 
425 	/*
426 	 * Remove multicast references added as a result of calls to
427 	 * ip_join_allmulti().
428 	 */
429 	ip_purge_allmulti(ill);
430 
431 	/*
432 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
433 	 */
434 	if (IS_UNDER_IPMP(ill))
435 		ipmp_ill_leave_illgrp(ill);
436 
437 	/*
438 	 * ill_down will arrange to blow off any IRE's dependent on this
439 	 * ILL, and shut down fragmentation reassembly.
440 	 */
441 	ill_down(ill);
442 
443 	/* Let SCTP know, so that it can remove this from its list. */
444 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
445 
446 	/*
447 	 * Walk all CONNs that can have a reference on an ire or nce for this
448 	 * ill (we actually walk all that now have stale references).
449 	 */
450 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
451 
452 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
453 	if (ill->ill_isv6)
454 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
455 
456 	/*
457 	 * If an address on this ILL is being used as a source address then
458 	 * clear out the pointers in other ILLs that point to this ILL.
459 	 */
460 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
461 	if (ill->ill_usesrc_grp_next != NULL) {
462 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
463 			ill_disband_usesrc_group(ill);
464 		} else {	/* consumer of the usesrc ILL */
465 			prev_ill = ill_prev_usesrc(ill);
466 			prev_ill->ill_usesrc_grp_next =
467 			    ill->ill_usesrc_grp_next;
468 		}
469 	}
470 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
471 }
472 
473 static void
474 ipif_non_duplicate(ipif_t *ipif)
475 {
476 	ill_t *ill = ipif->ipif_ill;
477 	mutex_enter(&ill->ill_lock);
478 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
479 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
480 		ASSERT(ill->ill_ipif_dup_count > 0);
481 		ill->ill_ipif_dup_count--;
482 	}
483 	mutex_exit(&ill->ill_lock);
484 }
485 
486 /*
487  * ill_delete_tail is called from ip_modclose after all references
488  * to the closing ill are gone. The wait is done in ip_modclose
489  */
490 void
491 ill_delete_tail(ill_t *ill)
492 {
493 	mblk_t	**mpp;
494 	ipif_t	*ipif;
495 	ip_stack_t *ipst = ill->ill_ipst;
496 
497 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
498 		ipif_non_duplicate(ipif);
499 		(void) ipif_down_tail(ipif);
500 	}
501 
502 	ASSERT(ill->ill_ipif_dup_count == 0);
503 
504 	/*
505 	 * If polling capability is enabled (which signifies direct
506 	 * upcall into IP and driver has ill saved as a handle),
507 	 * we need to make sure that unbind has completed before we
508 	 * let the ill disappear and driver no longer has any reference
509 	 * to this ill.
510 	 */
511 	mutex_enter(&ill->ill_lock);
512 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
513 		cv_wait(&ill->ill_cv, &ill->ill_lock);
514 	mutex_exit(&ill->ill_lock);
515 	ASSERT(!(ill->ill_capabilities &
516 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
517 
518 	if (ill->ill_net_type != IRE_LOOPBACK)
519 		qprocsoff(ill->ill_rq);
520 
521 	/*
522 	 * We do an ipsq_flush once again now. New messages could have
523 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
524 	 * could also have landed up if an ioctl thread had looked up
525 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
526 	 * enqueued the ioctl when we did the ipsq_flush last time.
527 	 */
528 	ipsq_flush(ill);
529 
530 	/*
531 	 * Free capabilities.
532 	 */
533 	if (ill->ill_hcksum_capab != NULL) {
534 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
535 		ill->ill_hcksum_capab = NULL;
536 	}
537 
538 	if (ill->ill_zerocopy_capab != NULL) {
539 		kmem_free(ill->ill_zerocopy_capab,
540 		    sizeof (ill_zerocopy_capab_t));
541 		ill->ill_zerocopy_capab = NULL;
542 	}
543 
544 	if (ill->ill_lso_capab != NULL) {
545 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
546 		ill->ill_lso_capab = NULL;
547 	}
548 
549 	if (ill->ill_dld_capab != NULL) {
550 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
551 		ill->ill_dld_capab = NULL;
552 	}
553 
554 	/* Clean up ill_allowed_ips* related state */
555 	if (ill->ill_allowed_ips != NULL) {
556 		ASSERT(ill->ill_allowed_ips_cnt > 0);
557 		kmem_free(ill->ill_allowed_ips,
558 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
559 		ill->ill_allowed_ips = NULL;
560 		ill->ill_allowed_ips_cnt = 0;
561 	}
562 
563 	while (ill->ill_ipif != NULL)
564 		ipif_free_tail(ill->ill_ipif);
565 
566 	/*
567 	 * We have removed all references to ilm from conn and the ones joined
568 	 * within the kernel.
569 	 *
570 	 * We don't walk conns, mrts and ires because
571 	 *
572 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
573 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
574 	 *    ill references.
575 	 */
576 
577 	/*
578 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
579 	 * is safe to do because the illgrp has already been unlinked from the
580 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
581 	 */
582 	if (IS_IPMP(ill)) {
583 		ipmp_illgrp_destroy(ill->ill_grp);
584 		ill->ill_grp = NULL;
585 	}
586 
587 	if (ill->ill_mphysaddr_list != NULL) {
588 		multiphysaddr_t *mpa, *tmpa;
589 
590 		mpa = ill->ill_mphysaddr_list;
591 		ill->ill_mphysaddr_list = NULL;
592 		while (mpa) {
593 			tmpa = mpa->mpa_next;
594 			kmem_free(mpa, sizeof (*mpa));
595 			mpa = tmpa;
596 		}
597 	}
598 	/*
599 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
600 	 * could free the phyint. No more reference to the phyint after this
601 	 * point.
602 	 */
603 	(void) ill_glist_delete(ill);
604 
605 	if (ill->ill_frag_ptr != NULL) {
606 		uint_t count;
607 
608 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
609 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
610 		}
611 		mi_free(ill->ill_frag_ptr);
612 		ill->ill_frag_ptr = NULL;
613 		ill->ill_frag_hash_tbl = NULL;
614 	}
615 
616 	freemsg(ill->ill_nd_lla_mp);
617 	/* Free all retained control messages. */
618 	mpp = &ill->ill_first_mp_to_free;
619 	do {
620 		while (mpp[0]) {
621 			mblk_t  *mp;
622 			mblk_t  *mp1;
623 
624 			mp = mpp[0];
625 			mpp[0] = mp->b_next;
626 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
627 				mp1->b_next = NULL;
628 				mp1->b_prev = NULL;
629 			}
630 			freemsg(mp);
631 		}
632 	} while (mpp++ != &ill->ill_last_mp_to_free);
633 
634 	ill_free_mib(ill);
635 
636 #ifdef DEBUG
637 	ill_trace_cleanup(ill);
638 #endif
639 
640 	/* The default multicast interface might have changed */
641 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
642 
643 	/* Drop refcnt here */
644 	netstack_rele(ill->ill_ipst->ips_netstack);
645 	ill->ill_ipst = NULL;
646 }
647 
648 static void
649 ill_free_mib(ill_t *ill)
650 {
651 	ip_stack_t *ipst = ill->ill_ipst;
652 
653 	/*
654 	 * MIB statistics must not be lost, so when an interface
655 	 * goes away the counter values will be added to the global
656 	 * MIBs.
657 	 */
658 	if (ill->ill_ip_mib != NULL) {
659 		if (ill->ill_isv6) {
660 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
661 			    ill->ill_ip_mib);
662 		} else {
663 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
664 			    ill->ill_ip_mib);
665 		}
666 
667 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
668 		ill->ill_ip_mib = NULL;
669 	}
670 	if (ill->ill_icmp6_mib != NULL) {
671 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
672 		    ill->ill_icmp6_mib);
673 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
674 		ill->ill_icmp6_mib = NULL;
675 	}
676 }
677 
678 /*
679  * Concatenate together a physical address and a sap.
680  *
681  * Sap_lengths are interpreted as follows:
682  *   sap_length == 0	==>	no sap
683  *   sap_length > 0	==>	sap is at the head of the dlpi address
684  *   sap_length < 0	==>	sap is at the tail of the dlpi address
685  */
686 static void
687 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
688     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
689 {
690 	uint16_t sap_addr = (uint16_t)sap_src;
691 
692 	if (sap_length == 0) {
693 		if (phys_src == NULL)
694 			bzero(dst, phys_length);
695 		else
696 			bcopy(phys_src, dst, phys_length);
697 	} else if (sap_length < 0) {
698 		if (phys_src == NULL)
699 			bzero(dst, phys_length);
700 		else
701 			bcopy(phys_src, dst, phys_length);
702 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
703 	} else {
704 		bcopy(&sap_addr, dst, sizeof (sap_addr));
705 		if (phys_src == NULL)
706 			bzero((char *)dst + sap_length, phys_length);
707 		else
708 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
709 	}
710 }
711 
712 /*
713  * Generate a dl_unitdata_req mblk for the device and address given.
714  * addr_length is the length of the physical portion of the address.
715  * If addr is NULL include an all zero address of the specified length.
716  * TRUE? In any case, addr_length is taken to be the entire length of the
717  * dlpi address, including the absolute value of sap_length.
718  */
719 mblk_t *
720 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
721     t_scalar_t sap_length)
722 {
723 	dl_unitdata_req_t *dlur;
724 	mblk_t	*mp;
725 	t_scalar_t	abs_sap_length;		/* absolute value */
726 
727 	abs_sap_length = ABS(sap_length);
728 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
729 	    DL_UNITDATA_REQ);
730 	if (mp == NULL)
731 		return (NULL);
732 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
733 	/* HACK: accomodate incompatible DLPI drivers */
734 	if (addr_length == 8)
735 		addr_length = 6;
736 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
737 	dlur->dl_dest_addr_offset = sizeof (*dlur);
738 	dlur->dl_priority.dl_min = 0;
739 	dlur->dl_priority.dl_max = 0;
740 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
741 	    (uchar_t *)&dlur[1]);
742 	return (mp);
743 }
744 
745 /*
746  * Add the pending mp to the list. There can be only 1 pending mp
747  * in the list. Any exclusive ioctl that needs to wait for a response
748  * from another module or driver needs to use this function to set
749  * the ipx_pending_mp to the ioctl mblk and wait for the response from
750  * the other module/driver. This is also used while waiting for the
751  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
752  */
753 boolean_t
754 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
755     int waitfor)
756 {
757 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
758 
759 	ASSERT(IAM_WRITER_IPIF(ipif));
760 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
761 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
762 	ASSERT(ipx->ipx_pending_mp == NULL);
763 	/*
764 	 * The caller may be using a different ipif than the one passed into
765 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
766 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
767 	 * that `ipx_current_ipif == ipif'.
768 	 */
769 	ASSERT(ipx->ipx_current_ipif != NULL);
770 
771 	/*
772 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
773 	 * driver.
774 	 */
775 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
776 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
777 	    (DB_TYPE(add_mp) == M_PCPROTO));
778 
779 	if (connp != NULL) {
780 		ASSERT(MUTEX_HELD(&connp->conn_lock));
781 		/*
782 		 * Return error if the conn has started closing. The conn
783 		 * could have finished cleaning up the pending mp list,
784 		 * If so we should not add another mp to the list negating
785 		 * the cleanup.
786 		 */
787 		if (connp->conn_state_flags & CONN_CLOSING)
788 			return (B_FALSE);
789 	}
790 	mutex_enter(&ipx->ipx_lock);
791 	ipx->ipx_pending_ipif = ipif;
792 	/*
793 	 * Note down the queue in b_queue. This will be returned by
794 	 * ipsq_pending_mp_get. Caller will then use these values to restart
795 	 * the processing
796 	 */
797 	add_mp->b_next = NULL;
798 	add_mp->b_queue = q;
799 	ipx->ipx_pending_mp = add_mp;
800 	ipx->ipx_waitfor = waitfor;
801 	mutex_exit(&ipx->ipx_lock);
802 
803 	if (connp != NULL)
804 		connp->conn_oper_pending_ill = ipif->ipif_ill;
805 
806 	return (B_TRUE);
807 }
808 
809 /*
810  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
811  * queued in the list.
812  */
813 mblk_t *
814 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
815 {
816 	mblk_t	*curr = NULL;
817 	ipxop_t	*ipx = ipsq->ipsq_xop;
818 
819 	*connpp = NULL;
820 	mutex_enter(&ipx->ipx_lock);
821 	if (ipx->ipx_pending_mp == NULL) {
822 		mutex_exit(&ipx->ipx_lock);
823 		return (NULL);
824 	}
825 
826 	/* There can be only 1 such excl message */
827 	curr = ipx->ipx_pending_mp;
828 	ASSERT(curr->b_next == NULL);
829 	ipx->ipx_pending_ipif = NULL;
830 	ipx->ipx_pending_mp = NULL;
831 	ipx->ipx_waitfor = 0;
832 	mutex_exit(&ipx->ipx_lock);
833 
834 	if (CONN_Q(curr->b_queue)) {
835 		/*
836 		 * This mp did a refhold on the conn, at the start of the ioctl.
837 		 * So we can safely return a pointer to the conn to the caller.
838 		 */
839 		*connpp = Q_TO_CONN(curr->b_queue);
840 	} else {
841 		*connpp = NULL;
842 	}
843 	curr->b_next = NULL;
844 	curr->b_prev = NULL;
845 	return (curr);
846 }
847 
848 /*
849  * Cleanup the ioctl mp queued in ipx_pending_mp
850  * - Called in the ill_delete path
851  * - Called in the M_ERROR or M_HANGUP path on the ill.
852  * - Called in the conn close path.
853  *
854  * Returns success on finding the pending mblk associated with the ioctl or
855  * exclusive operation in progress, failure otherwise.
856  */
857 boolean_t
858 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
859 {
860 	mblk_t	*mp;
861 	ipxop_t	*ipx;
862 	queue_t	*q;
863 	ipif_t	*ipif;
864 	int	cmd;
865 
866 	ASSERT(IAM_WRITER_ILL(ill));
867 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
868 
869 	mutex_enter(&ipx->ipx_lock);
870 	mp = ipx->ipx_pending_mp;
871 	if (connp != NULL) {
872 		if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
873 			/*
874 			 * Nothing to clean since the conn that is closing
875 			 * does not have a matching pending mblk in
876 			 * ipx_pending_mp.
877 			 */
878 			mutex_exit(&ipx->ipx_lock);
879 			return (B_FALSE);
880 		}
881 	} else {
882 		/*
883 		 * A non-zero ill_error signifies we are called in the
884 		 * M_ERROR or M_HANGUP path and we need to unconditionally
885 		 * abort any current ioctl and do the corresponding cleanup.
886 		 * A zero ill_error means we are in the ill_delete path and
887 		 * we do the cleanup only if there is a pending mp.
888 		 */
889 		if (mp == NULL && ill->ill_error == 0) {
890 			mutex_exit(&ipx->ipx_lock);
891 			return (B_FALSE);
892 		}
893 	}
894 
895 	/* Now remove from the ipx_pending_mp */
896 	ipx->ipx_pending_mp = NULL;
897 	ipif = ipx->ipx_pending_ipif;
898 	ipx->ipx_pending_ipif = NULL;
899 	ipx->ipx_waitfor = 0;
900 	ipx->ipx_current_ipif = NULL;
901 	cmd = ipx->ipx_current_ioctl;
902 	ipx->ipx_current_ioctl = 0;
903 	ipx->ipx_current_done = B_TRUE;
904 	mutex_exit(&ipx->ipx_lock);
905 
906 	if (mp == NULL)
907 		return (B_FALSE);
908 
909 	q = mp->b_queue;
910 	mp->b_next = NULL;
911 	mp->b_prev = NULL;
912 	mp->b_queue = NULL;
913 
914 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
915 		DTRACE_PROBE4(ipif__ioctl,
916 		    char *, "ipsq_pending_mp_cleanup",
917 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
918 		    ipif_t *, ipif);
919 		if (connp == NULL) {
920 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
921 		} else {
922 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
923 			mutex_enter(&ipif->ipif_ill->ill_lock);
924 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
925 			mutex_exit(&ipif->ipif_ill->ill_lock);
926 		}
927 	} else {
928 		inet_freemsg(mp);
929 	}
930 	return (B_TRUE);
931 }
932 
933 /*
934  * Called in the conn close path and ill delete path
935  */
936 static void
937 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
938 {
939 	ipsq_t	*ipsq;
940 	mblk_t	*prev;
941 	mblk_t	*curr;
942 	mblk_t	*next;
943 	queue_t	*wq, *rq = NULL;
944 	mblk_t	*tmp_list = NULL;
945 
946 	ASSERT(IAM_WRITER_ILL(ill));
947 	if (connp != NULL)
948 		wq = CONNP_TO_WQ(connp);
949 	else
950 		wq = ill->ill_wq;
951 
952 	/*
953 	 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
954 	 * against this here.
955 	 */
956 	if (wq != NULL)
957 		rq = RD(wq);
958 
959 	ipsq = ill->ill_phyint->phyint_ipsq;
960 	/*
961 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
962 	 * In the case of ioctl from a conn, there can be only 1 mp
963 	 * queued on the ipsq. If an ill is being unplumbed flush all
964 	 * the messages.
965 	 */
966 	mutex_enter(&ipsq->ipsq_lock);
967 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
968 	    curr = next) {
969 		next = curr->b_next;
970 		if (connp == NULL ||
971 		    (curr->b_queue == wq || curr->b_queue == rq)) {
972 			/* Unlink the mblk from the pending mp list */
973 			if (prev != NULL) {
974 				prev->b_next = curr->b_next;
975 			} else {
976 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
977 				ipsq->ipsq_xopq_mphead = curr->b_next;
978 			}
979 			if (ipsq->ipsq_xopq_mptail == curr)
980 				ipsq->ipsq_xopq_mptail = prev;
981 			/*
982 			 * Create a temporary list and release the ipsq lock
983 			 * New elements are added to the head of the tmp_list
984 			 */
985 			curr->b_next = tmp_list;
986 			tmp_list = curr;
987 		} else {
988 			prev = curr;
989 		}
990 	}
991 	mutex_exit(&ipsq->ipsq_lock);
992 
993 	while (tmp_list != NULL) {
994 		curr = tmp_list;
995 		tmp_list = curr->b_next;
996 		curr->b_next = NULL;
997 		curr->b_prev = NULL;
998 		wq = curr->b_queue;
999 		curr->b_queue = NULL;
1000 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
1001 			DTRACE_PROBE4(ipif__ioctl,
1002 			    char *, "ipsq_xopq_mp_cleanup",
1003 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
1004 			ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
1005 			    CONN_CLOSE : NO_COPYOUT, NULL);
1006 		} else {
1007 			/*
1008 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1009 			 * this can't be just inet_freemsg. we have to
1010 			 * restart it otherwise the thread will be stuck.
1011 			 */
1012 			inet_freemsg(curr);
1013 		}
1014 	}
1015 }
1016 
1017 /*
1018  * This conn has started closing. Cleanup any pending ioctl from this conn.
1019  * STREAMS ensures that there can be at most 1 active ioctl on a stream.
1020  */
1021 void
1022 conn_ioctl_cleanup(conn_t *connp)
1023 {
1024 	ipsq_t	*ipsq;
1025 	ill_t	*ill;
1026 	boolean_t refheld;
1027 
1028 	/*
1029 	 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1030 	 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1031 	 * started the mp could be present in ipx_pending_mp. Note that if
1032 	 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1033 	 * not yet queued anywhere. In this case, the conn close code will wait
1034 	 * until the conn_ref is dropped. If the stream was a tcp stream, then
1035 	 * tcp_close will wait first until all ioctls have completed for this
1036 	 * conn.
1037 	 */
1038 	mutex_enter(&connp->conn_lock);
1039 	ill = connp->conn_oper_pending_ill;
1040 	if (ill == NULL) {
1041 		mutex_exit(&connp->conn_lock);
1042 		return;
1043 	}
1044 
1045 	/*
1046 	 * We may not be able to refhold the ill if the ill/ipif
1047 	 * is changing. But we need to make sure that the ill will
1048 	 * not vanish. So we just bump up the ill_waiter count.
1049 	 */
1050 	refheld = ill_waiter_inc(ill);
1051 	mutex_exit(&connp->conn_lock);
1052 	if (refheld) {
1053 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1054 			ill_waiter_dcr(ill);
1055 			/*
1056 			 * Check whether this ioctl has started and is
1057 			 * pending. If it is not found there then check
1058 			 * whether this ioctl has not even started and is in
1059 			 * the ipsq_xopq list.
1060 			 */
1061 			if (!ipsq_pending_mp_cleanup(ill, connp))
1062 				ipsq_xopq_mp_cleanup(ill, connp);
1063 			ipsq = ill->ill_phyint->phyint_ipsq;
1064 			ipsq_exit(ipsq);
1065 			return;
1066 		}
1067 	}
1068 
1069 	/*
1070 	 * The ill is also closing and we could not bump up the
1071 	 * ill_waiter_count or we could not enter the ipsq. Leave
1072 	 * the cleanup to ill_delete
1073 	 */
1074 	mutex_enter(&connp->conn_lock);
1075 	while (connp->conn_oper_pending_ill != NULL)
1076 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1077 	mutex_exit(&connp->conn_lock);
1078 	if (refheld)
1079 		ill_waiter_dcr(ill);
1080 }
1081 
1082 /*
1083  * ipcl_walk function for cleaning up conn_*_ill fields.
1084  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1085  * conn_bound_if in place. We prefer dropping
1086  * packets instead of sending them out the wrong interface, or accepting
1087  * packets from the wrong ifindex.
1088  */
1089 static void
1090 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1091 {
1092 	ill_t	*ill = (ill_t *)arg;
1093 
1094 	mutex_enter(&connp->conn_lock);
1095 	if (connp->conn_dhcpinit_ill == ill) {
1096 		connp->conn_dhcpinit_ill = NULL;
1097 		ASSERT(ill->ill_dhcpinit != 0);
1098 		atomic_dec_32(&ill->ill_dhcpinit);
1099 		ill_set_inputfn(ill);
1100 	}
1101 	mutex_exit(&connp->conn_lock);
1102 }
1103 
1104 static int
1105 ill_down_ipifs_tail(ill_t *ill)
1106 {
1107 	ipif_t	*ipif;
1108 	int err;
1109 
1110 	ASSERT(IAM_WRITER_ILL(ill));
1111 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1112 		ipif_non_duplicate(ipif);
1113 		/*
1114 		 * ipif_down_tail will call arp_ll_down on the last ipif
1115 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1116 		 */
1117 		if ((err = ipif_down_tail(ipif)) != 0)
1118 			return (err);
1119 	}
1120 	return (0);
1121 }
1122 
1123 /* ARGSUSED */
1124 void
1125 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1126 {
1127 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1128 	(void) ill_down_ipifs_tail(q->q_ptr);
1129 	freemsg(mp);
1130 	ipsq_current_finish(ipsq);
1131 }
1132 
1133 /*
1134  * ill_down_start is called when we want to down this ill and bring it up again
1135  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1136  * all interfaces, but don't tear down any plumbing.
1137  */
1138 boolean_t
1139 ill_down_start(queue_t *q, mblk_t *mp)
1140 {
1141 	ill_t	*ill = q->q_ptr;
1142 	ipif_t	*ipif;
1143 
1144 	ASSERT(IAM_WRITER_ILL(ill));
1145 	/*
1146 	 * It is possible that some ioctl is already in progress while we
1147 	 * received the M_ERROR / M_HANGUP in which case, we need to abort
1148 	 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1149 	 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1150 	 * the in progress ioctl from ever completing.
1151 	 *
1152 	 * The thread that started the ioctl (if any) must have returned,
1153 	 * since we are now executing as writer. After the 2 calls below,
1154 	 * the state of the ipsq and the ill would reflect no trace of any
1155 	 * pending operation. Subsequently if there is any response to the
1156 	 * original ioctl from the driver, it would be discarded as an
1157 	 * unsolicited message from the driver.
1158 	 */
1159 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1160 	ill_dlpi_clear_deferred(ill);
1161 
1162 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1163 		(void) ipif_down(ipif, NULL, NULL);
1164 
1165 	ill_down(ill);
1166 
1167 	/*
1168 	 * Walk all CONNs that can have a reference on an ire or nce for this
1169 	 * ill (we actually walk all that now have stale references).
1170 	 */
1171 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1172 
1173 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1174 	if (ill->ill_isv6)
1175 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1176 
1177 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1178 
1179 	/*
1180 	 * Atomically test and add the pending mp if references are active.
1181 	 */
1182 	mutex_enter(&ill->ill_lock);
1183 	if (!ill_is_quiescent(ill)) {
1184 		/* call cannot fail since `conn_t *' argument is NULL */
1185 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1186 		    mp, ILL_DOWN);
1187 		mutex_exit(&ill->ill_lock);
1188 		return (B_FALSE);
1189 	}
1190 	mutex_exit(&ill->ill_lock);
1191 	return (B_TRUE);
1192 }
1193 
1194 static void
1195 ill_down(ill_t *ill)
1196 {
1197 	mblk_t	*mp;
1198 	ip_stack_t	*ipst = ill->ill_ipst;
1199 
1200 	/*
1201 	 * Blow off any IREs dependent on this ILL.
1202 	 * The caller needs to handle conn_ixa_cleanup
1203 	 */
1204 	ill_delete_ires(ill);
1205 
1206 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1207 
1208 	/* Remove any conn_*_ill depending on this ill */
1209 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1210 
1211 	/*
1212 	 * Free state for additional IREs.
1213 	 */
1214 	mutex_enter(&ill->ill_saved_ire_lock);
1215 	mp = ill->ill_saved_ire_mp;
1216 	ill->ill_saved_ire_mp = NULL;
1217 	ill->ill_saved_ire_cnt = 0;
1218 	mutex_exit(&ill->ill_saved_ire_lock);
1219 	freemsg(mp);
1220 }
1221 
1222 /*
1223  * ire_walk routine used to delete every IRE that depends on
1224  * 'ill'.  (Always called as writer, and may only be called from ire_walk.)
1225  *
1226  * Note: since the routes added by the kernel are deleted separately,
1227  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1228  *
1229  * We also remove references on ire_nce_cache entries that refer to the ill.
1230  */
1231 void
1232 ill_downi(ire_t *ire, char *ill_arg)
1233 {
1234 	ill_t	*ill = (ill_t *)ill_arg;
1235 	nce_t	*nce;
1236 
1237 	mutex_enter(&ire->ire_lock);
1238 	nce = ire->ire_nce_cache;
1239 	if (nce != NULL && nce->nce_ill == ill)
1240 		ire->ire_nce_cache = NULL;
1241 	else
1242 		nce = NULL;
1243 	mutex_exit(&ire->ire_lock);
1244 	if (nce != NULL)
1245 		nce_refrele(nce);
1246 	if (ire->ire_ill == ill) {
1247 		/*
1248 		 * The existing interface binding for ire must be
1249 		 * deleted before trying to bind the route to another
1250 		 * interface. However, since we are using the contents of the
1251 		 * ire after ire_delete, the caller has to ensure that
1252 		 * CONDEMNED (deleted) ire's are not removed from the list
1253 		 * when ire_delete() returns. Currently ill_downi() is
1254 		 * only called as part of ire_walk*() routines, so that
1255 		 * the irb_refhold() done by ire_walk*() will ensure that
1256 		 * ire_delete() does not lead to ire_inactive().
1257 		 */
1258 		ASSERT(ire->ire_bucket->irb_refcnt > 0);
1259 		ire_delete(ire);
1260 		if (ire->ire_unbound)
1261 			ire_rebind(ire);
1262 	}
1263 }
1264 
1265 /* Remove IRE_IF_CLONE on this ill */
1266 void
1267 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1268 {
1269 	ill_t	*ill = (ill_t *)ill_arg;
1270 
1271 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1272 	if (ire->ire_ill == ill)
1273 		ire_delete(ire);
1274 }
1275 
1276 /* Consume an M_IOCACK of the fastpath probe. */
1277 void
1278 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1279 {
1280 	mblk_t	*mp1 = mp;
1281 
1282 	/*
1283 	 * If this was the first attempt turn on the fastpath probing.
1284 	 */
1285 	mutex_enter(&ill->ill_lock);
1286 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1287 		ill->ill_dlpi_fastpath_state = IDS_OK;
1288 	mutex_exit(&ill->ill_lock);
1289 
1290 	/* Free the M_IOCACK mblk, hold on to the data */
1291 	mp = mp->b_cont;
1292 	freeb(mp1);
1293 	if (mp == NULL)
1294 		return;
1295 	if (mp->b_cont != NULL)
1296 		nce_fastpath_update(ill, mp);
1297 	else
1298 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1299 	freemsg(mp);
1300 }
1301 
1302 /*
1303  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1304  * The data portion of the request is a dl_unitdata_req_t template for
1305  * what we would send downstream in the absence of a fastpath confirmation.
1306  */
1307 int
1308 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1309 {
1310 	struct iocblk	*ioc;
1311 	mblk_t	*mp;
1312 
1313 	if (dlur_mp == NULL)
1314 		return (EINVAL);
1315 
1316 	mutex_enter(&ill->ill_lock);
1317 	switch (ill->ill_dlpi_fastpath_state) {
1318 	case IDS_FAILED:
1319 		/*
1320 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1321 		 * support it.
1322 		 */
1323 		mutex_exit(&ill->ill_lock);
1324 		return (ENOTSUP);
1325 	case IDS_UNKNOWN:
1326 		/* This is the first probe */
1327 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1328 		break;
1329 	default:
1330 		break;
1331 	}
1332 	mutex_exit(&ill->ill_lock);
1333 
1334 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1335 		return (EAGAIN);
1336 
1337 	mp->b_cont = copyb(dlur_mp);
1338 	if (mp->b_cont == NULL) {
1339 		freeb(mp);
1340 		return (EAGAIN);
1341 	}
1342 
1343 	ioc = (struct iocblk *)mp->b_rptr;
1344 	ioc->ioc_count = msgdsize(mp->b_cont);
1345 
1346 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1347 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1348 	putnext(ill->ill_wq, mp);
1349 	return (0);
1350 }
1351 
1352 void
1353 ill_capability_probe(ill_t *ill)
1354 {
1355 	mblk_t	*mp;
1356 
1357 	ASSERT(IAM_WRITER_ILL(ill));
1358 
1359 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1360 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1361 		return;
1362 
1363 	/*
1364 	 * We are starting a new cycle of capability negotiation.
1365 	 * Free up the capab reset messages of any previous incarnation.
1366 	 * We will do a fresh allocation when we get the response to our probe
1367 	 */
1368 	if (ill->ill_capab_reset_mp != NULL) {
1369 		freemsg(ill->ill_capab_reset_mp);
1370 		ill->ill_capab_reset_mp = NULL;
1371 	}
1372 
1373 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1374 
1375 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1376 	if (mp == NULL)
1377 		return;
1378 
1379 	ill_capability_send(ill, mp);
1380 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1381 }
1382 
1383 void
1384 ill_capability_reset(ill_t *ill, boolean_t reneg)
1385 {
1386 	ASSERT(IAM_WRITER_ILL(ill));
1387 
1388 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1389 		return;
1390 
1391 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1392 
1393 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1394 	ill->ill_capab_reset_mp = NULL;
1395 	/*
1396 	 * We turn off all capabilities except those pertaining to
1397 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1398 	 * which will be turned off by the corresponding reset functions.
1399 	 */
1400 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1401 }
1402 
1403 static void
1404 ill_capability_reset_alloc(ill_t *ill)
1405 {
1406 	mblk_t *mp;
1407 	size_t	size = 0;
1408 	int	err;
1409 	dl_capability_req_t	*capb;
1410 
1411 	ASSERT(IAM_WRITER_ILL(ill));
1412 	ASSERT(ill->ill_capab_reset_mp == NULL);
1413 
1414 	if (ILL_HCKSUM_CAPABLE(ill)) {
1415 		size += sizeof (dl_capability_sub_t) +
1416 		    sizeof (dl_capab_hcksum_t);
1417 	}
1418 
1419 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1420 		size += sizeof (dl_capability_sub_t) +
1421 		    sizeof (dl_capab_zerocopy_t);
1422 	}
1423 
1424 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1425 		size += sizeof (dl_capability_sub_t) +
1426 		    sizeof (dl_capab_dld_t);
1427 	}
1428 
1429 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1430 	    STR_NOSIG, &err);
1431 
1432 	mp->b_datap->db_type = M_PROTO;
1433 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1434 
1435 	capb = (dl_capability_req_t *)mp->b_rptr;
1436 	capb->dl_primitive = DL_CAPABILITY_REQ;
1437 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1438 	capb->dl_sub_length = size;
1439 
1440 	mp->b_wptr += sizeof (dl_capability_req_t);
1441 
1442 	/*
1443 	 * Each handler fills in the corresponding dl_capability_sub_t
1444 	 * inside the mblk,
1445 	 */
1446 	ill_capability_hcksum_reset_fill(ill, mp);
1447 	ill_capability_zerocopy_reset_fill(ill, mp);
1448 	ill_capability_dld_reset_fill(ill, mp);
1449 
1450 	ill->ill_capab_reset_mp = mp;
1451 }
1452 
1453 static void
1454 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1455 {
1456 	dl_capab_id_t *id_ic;
1457 	uint_t sub_dl_cap = outers->dl_cap;
1458 	dl_capability_sub_t *inners;
1459 	uint8_t *capend;
1460 
1461 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1462 
1463 	/*
1464 	 * Note: range checks here are not absolutely sufficient to
1465 	 * make us robust against malformed messages sent by drivers;
1466 	 * this is in keeping with the rest of IP's dlpi handling.
1467 	 * (Remember, it's coming from something else in the kernel
1468 	 * address space)
1469 	 */
1470 
1471 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1472 	if (capend > mp->b_wptr) {
1473 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1474 		    "malformed sub-capability too long for mblk");
1475 		return;
1476 	}
1477 
1478 	id_ic = (dl_capab_id_t *)(outers + 1);
1479 
1480 	inners = &id_ic->id_subcap;
1481 	if (outers->dl_length < sizeof (*id_ic) ||
1482 	    inners->dl_length > (outers->dl_length - sizeof (*inners))) {
1483 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1484 		    "encapsulated capab type %d too long for mblk",
1485 		    inners->dl_cap);
1486 		return;
1487 	}
1488 
1489 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1490 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1491 		    "isn't as expected; pass-thru module(s) detected, "
1492 		    "discarding capability\n", inners->dl_cap));
1493 		return;
1494 	}
1495 
1496 	/* Process the encapsulated sub-capability */
1497 	ill_capability_dispatch(ill, mp, inners);
1498 }
1499 
1500 static void
1501 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1502 {
1503 	dl_capability_sub_t *dl_subcap;
1504 
1505 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1506 		return;
1507 
1508 	/*
1509 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1510 	 * initialized below since it is not used by DLD.
1511 	 */
1512 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1513 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1514 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1515 
1516 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1517 }
1518 
1519 static void
1520 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1521 {
1522 	/*
1523 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1524 	 * is only to get the VRRP capability.
1525 	 *
1526 	 * Note that we cannot check ill_ipif_up_count here since
1527 	 * ill_ipif_up_count is only incremented when the resolver is setup.
1528 	 * That is done asynchronously, and can race with this function.
1529 	 */
1530 	if (!ill->ill_dl_up) {
1531 		if (subp->dl_cap == DL_CAPAB_VRRP)
1532 			ill_capability_vrrp_ack(ill, mp, subp);
1533 		return;
1534 	}
1535 
1536 	switch (subp->dl_cap) {
1537 	case DL_CAPAB_HCKSUM:
1538 		ill_capability_hcksum_ack(ill, mp, subp);
1539 		break;
1540 	case DL_CAPAB_ZEROCOPY:
1541 		ill_capability_zerocopy_ack(ill, mp, subp);
1542 		break;
1543 	case DL_CAPAB_DLD:
1544 		ill_capability_dld_ack(ill, mp, subp);
1545 		break;
1546 	case DL_CAPAB_VRRP:
1547 		break;
1548 	default:
1549 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1550 		    subp->dl_cap));
1551 	}
1552 }
1553 
1554 /*
1555  * Process the vrrp capability received from a DLS Provider. isub must point
1556  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1557  */
1558 static void
1559 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1560 {
1561 	dl_capab_vrrp_t	*vrrp;
1562 	uint_t		sub_dl_cap = isub->dl_cap;
1563 	uint8_t		*capend;
1564 
1565 	ASSERT(IAM_WRITER_ILL(ill));
1566 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1567 
1568 	/*
1569 	 * Note: range checks here are not absolutely sufficient to
1570 	 * make us robust against malformed messages sent by drivers;
1571 	 * this is in keeping with the rest of IP's dlpi handling.
1572 	 * (Remember, it's coming from something else in the kernel
1573 	 * address space)
1574 	 */
1575 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1576 	if (capend > mp->b_wptr) {
1577 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1578 		    "malformed sub-capability too long for mblk");
1579 		return;
1580 	}
1581 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1582 
1583 	/*
1584 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1585 	 */
1586 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1587 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1588 		ill->ill_flags |= ILLF_VRRP;
1589 	}
1590 }
1591 
1592 /*
1593  * Process a hardware checksum offload capability negotiation ack received
1594  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1595  * of a DL_CAPABILITY_ACK message.
1596  */
1597 static void
1598 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1599 {
1600 	dl_capability_req_t	*ocap;
1601 	dl_capab_hcksum_t	*ihck, *ohck;
1602 	ill_hcksum_capab_t	**ill_hcksum;
1603 	mblk_t			*nmp = NULL;
1604 	uint_t			sub_dl_cap = isub->dl_cap;
1605 	uint8_t			*capend;
1606 
1607 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1608 
1609 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1610 
1611 	/*
1612 	 * Note: range checks here are not absolutely sufficient to
1613 	 * make us robust against malformed messages sent by drivers;
1614 	 * this is in keeping with the rest of IP's dlpi handling.
1615 	 * (Remember, it's coming from something else in the kernel
1616 	 * address space)
1617 	 */
1618 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1619 	if (capend > mp->b_wptr) {
1620 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1621 		    "malformed sub-capability too long for mblk");
1622 		return;
1623 	}
1624 
1625 	/*
1626 	 * There are two types of acks we process here:
1627 	 * 1. acks in reply to a (first form) generic capability req
1628 	 *    (no ENABLE flag set)
1629 	 * 2. acks in reply to a ENABLE capability req.
1630 	 *    (ENABLE flag set)
1631 	 */
1632 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1633 
1634 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1635 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1636 		    "unsupported hardware checksum "
1637 		    "sub-capability (version %d, expected %d)",
1638 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1639 		return;
1640 	}
1641 
1642 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1643 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1644 		    "checksum capability isn't as expected; pass-thru "
1645 		    "module(s) detected, discarding capability\n"));
1646 		return;
1647 	}
1648 
1649 #define	CURR_HCKSUM_CAPAB				\
1650 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1651 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1652 
1653 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1654 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1655 		/* do ENABLE processing */
1656 		if (*ill_hcksum == NULL) {
1657 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1658 			    KM_NOSLEEP);
1659 
1660 			if (*ill_hcksum == NULL) {
1661 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1662 				    "could not enable hcksum version %d "
1663 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1664 				    ill->ill_name);
1665 				return;
1666 			}
1667 		}
1668 
1669 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1670 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1671 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1672 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1673 		    "has enabled hardware checksumming\n ",
1674 		    ill->ill_name));
1675 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1676 		/*
1677 		 * Enabling hardware checksum offload
1678 		 * Currently IP supports {TCP,UDP}/IPv4
1679 		 * partial and full cksum offload and
1680 		 * IPv4 header checksum offload.
1681 		 * Allocate new mblk which will
1682 		 * contain a new capability request
1683 		 * to enable hardware checksum offload.
1684 		 */
1685 		uint_t	size;
1686 		uchar_t	*rptr;
1687 
1688 		size = sizeof (dl_capability_req_t) +
1689 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1690 
1691 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1692 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1693 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1694 			    ill->ill_name);
1695 			return;
1696 		}
1697 
1698 		rptr = nmp->b_rptr;
1699 		/* initialize dl_capability_req_t */
1700 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1701 		ocap->dl_sub_offset =
1702 		    sizeof (dl_capability_req_t);
1703 		ocap->dl_sub_length =
1704 		    sizeof (dl_capability_sub_t) +
1705 		    isub->dl_length;
1706 		nmp->b_rptr += sizeof (dl_capability_req_t);
1707 
1708 		/* initialize dl_capability_sub_t */
1709 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1710 		nmp->b_rptr += sizeof (*isub);
1711 
1712 		/* initialize dl_capab_hcksum_t */
1713 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1714 		bcopy(ihck, ohck, sizeof (*ihck));
1715 
1716 		nmp->b_rptr = rptr;
1717 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1718 
1719 		/* Set ENABLE flag */
1720 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1721 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1722 
1723 		/*
1724 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1725 		 * hardware checksum acceleration.
1726 		 */
1727 		ill_capability_send(ill, nmp);
1728 	} else {
1729 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1730 		    "advertised %x hardware checksum capability flags\n",
1731 		    ill->ill_name, ihck->hcksum_txflags));
1732 	}
1733 }
1734 
1735 static void
1736 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1737 {
1738 	dl_capab_hcksum_t *hck_subcap;
1739 	dl_capability_sub_t *dl_subcap;
1740 
1741 	if (!ILL_HCKSUM_CAPABLE(ill))
1742 		return;
1743 
1744 	ASSERT(ill->ill_hcksum_capab != NULL);
1745 
1746 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1747 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1748 	dl_subcap->dl_length = sizeof (*hck_subcap);
1749 
1750 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1751 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1752 	hck_subcap->hcksum_txflags = 0;
1753 
1754 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1755 }
1756 
1757 static void
1758 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1759 {
1760 	mblk_t *nmp = NULL;
1761 	dl_capability_req_t *oc;
1762 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1763 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1764 	uint_t sub_dl_cap = isub->dl_cap;
1765 	uint8_t *capend;
1766 
1767 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1768 
1769 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1770 
1771 	/*
1772 	 * Note: range checks here are not absolutely sufficient to
1773 	 * make us robust against malformed messages sent by drivers;
1774 	 * this is in keeping with the rest of IP's dlpi handling.
1775 	 * (Remember, it's coming from something else in the kernel
1776 	 * address space)
1777 	 */
1778 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1779 	if (capend > mp->b_wptr) {
1780 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1781 		    "malformed sub-capability too long for mblk");
1782 		return;
1783 	}
1784 
1785 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1786 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1787 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1788 		    "unsupported ZEROCOPY sub-capability (version %d, "
1789 		    "expected %d)", zc_ic->zerocopy_version,
1790 		    ZEROCOPY_VERSION_1);
1791 		return;
1792 	}
1793 
1794 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1795 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1796 		    "capability isn't as expected; pass-thru module(s) "
1797 		    "detected, discarding capability\n"));
1798 		return;
1799 	}
1800 
1801 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1802 		if (*ill_zerocopy_capab == NULL) {
1803 			*ill_zerocopy_capab =
1804 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1805 			    KM_NOSLEEP);
1806 
1807 			if (*ill_zerocopy_capab == NULL) {
1808 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1809 				    "could not enable Zero-copy version %d "
1810 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1811 				    ill->ill_name);
1812 				return;
1813 			}
1814 		}
1815 
1816 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1817 		    "supports Zero-copy version %d\n", ill->ill_name,
1818 		    ZEROCOPY_VERSION_1));
1819 
1820 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1821 		    zc_ic->zerocopy_version;
1822 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1823 		    zc_ic->zerocopy_flags;
1824 
1825 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1826 	} else {
1827 		uint_t size;
1828 		uchar_t *rptr;
1829 
1830 		size = sizeof (dl_capability_req_t) +
1831 		    sizeof (dl_capability_sub_t) +
1832 		    sizeof (dl_capab_zerocopy_t);
1833 
1834 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1835 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1836 			    "could not enable zerocopy for %s (ENOMEM)\n",
1837 			    ill->ill_name);
1838 			return;
1839 		}
1840 
1841 		rptr = nmp->b_rptr;
1842 		/* initialize dl_capability_req_t */
1843 		oc = (dl_capability_req_t *)rptr;
1844 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1845 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1846 		    sizeof (dl_capab_zerocopy_t);
1847 		rptr += sizeof (dl_capability_req_t);
1848 
1849 		/* initialize dl_capability_sub_t */
1850 		bcopy(isub, rptr, sizeof (*isub));
1851 		rptr += sizeof (*isub);
1852 
1853 		/* initialize dl_capab_zerocopy_t */
1854 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1855 		*zc_oc = *zc_ic;
1856 
1857 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1858 		    "to enable zero-copy version %d\n", ill->ill_name,
1859 		    ZEROCOPY_VERSION_1));
1860 
1861 		/* set VMSAFE_MEM flag */
1862 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1863 
1864 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1865 		ill_capability_send(ill, nmp);
1866 	}
1867 }
1868 
1869 static void
1870 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1871 {
1872 	dl_capab_zerocopy_t *zerocopy_subcap;
1873 	dl_capability_sub_t *dl_subcap;
1874 
1875 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1876 		return;
1877 
1878 	ASSERT(ill->ill_zerocopy_capab != NULL);
1879 
1880 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1881 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1882 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1883 
1884 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1885 	zerocopy_subcap->zerocopy_version =
1886 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1887 	zerocopy_subcap->zerocopy_flags = 0;
1888 
1889 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1890 }
1891 
1892 /*
1893  * DLD capability
1894  * Refer to dld.h for more information regarding the purpose and usage
1895  * of this capability.
1896  */
1897 static void
1898 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1899 {
1900 	dl_capab_dld_t		*dld_ic, dld;
1901 	uint_t			sub_dl_cap = isub->dl_cap;
1902 	uint8_t			*capend;
1903 	ill_dld_capab_t		*idc;
1904 
1905 	ASSERT(IAM_WRITER_ILL(ill));
1906 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1907 
1908 	/*
1909 	 * Note: range checks here are not absolutely sufficient to
1910 	 * make us robust against malformed messages sent by drivers;
1911 	 * this is in keeping with the rest of IP's dlpi handling.
1912 	 * (Remember, it's coming from something else in the kernel
1913 	 * address space)
1914 	 */
1915 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1916 	if (capend > mp->b_wptr) {
1917 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1918 		    "malformed sub-capability too long for mblk");
1919 		return;
1920 	}
1921 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1922 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1923 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1924 		    "unsupported DLD sub-capability (version %d, "
1925 		    "expected %d)", dld_ic->dld_version,
1926 		    DLD_CURRENT_VERSION);
1927 		return;
1928 	}
1929 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1930 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1931 		    "capability isn't as expected; pass-thru module(s) "
1932 		    "detected, discarding capability\n"));
1933 		return;
1934 	}
1935 
1936 	/*
1937 	 * Copy locally to ensure alignment.
1938 	 */
1939 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1940 
1941 	if ((idc = ill->ill_dld_capab) == NULL) {
1942 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1943 		if (idc == NULL) {
1944 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1945 			    "could not enable DLD version %d "
1946 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1947 			    ill->ill_name);
1948 			return;
1949 		}
1950 		ill->ill_dld_capab = idc;
1951 	}
1952 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1953 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1954 	ip1dbg(("ill_capability_dld_ack: interface %s "
1955 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1956 
1957 	ill_capability_dld_enable(ill);
1958 }
1959 
1960 /*
1961  * Typically capability negotiation between IP and the driver happens via
1962  * DLPI message exchange. However GLD also offers a direct function call
1963  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1964  * But arbitrary function calls into IP or GLD are not permitted, since both
1965  * of them are protected by their own perimeter mechanism. The perimeter can
1966  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1967  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1968  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1969  * to enter the mac perimeter and then do the direct function calls into
1970  * GLD to enable squeue polling. The ring related callbacks from the mac into
1971  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1972  * protected by the mac perimeter.
1973  */
1974 static void
1975 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1976 {
1977 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1978 	int			err;
1979 
1980 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1981 	    DLD_ENABLE);
1982 	ASSERT(err == 0);
1983 }
1984 
1985 static void
1986 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1987 {
1988 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1989 	int			err;
1990 
1991 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1992 	    DLD_DISABLE);
1993 	ASSERT(err == 0);
1994 }
1995 
1996 boolean_t
1997 ill_mac_perim_held(ill_t *ill)
1998 {
1999 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2000 
2001 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
2002 	    DLD_QUERY));
2003 }
2004 
2005 static void
2006 ill_capability_direct_enable(ill_t *ill)
2007 {
2008 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2009 	ill_dld_direct_t	*idd = &idc->idc_direct;
2010 	dld_capab_direct_t	direct;
2011 	int			rc;
2012 
2013 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2014 
2015 	bzero(&direct, sizeof (direct));
2016 	direct.di_rx_cf = (uintptr_t)ip_input;
2017 	direct.di_rx_ch = ill;
2018 
2019 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
2020 	    DLD_ENABLE);
2021 	if (rc == 0) {
2022 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2023 		idd->idd_tx_dh = direct.di_tx_dh;
2024 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2025 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2026 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2027 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2028 		ASSERT(idd->idd_tx_cb_df != NULL);
2029 		ASSERT(idd->idd_tx_fctl_df != NULL);
2030 		ASSERT(idd->idd_tx_df != NULL);
2031 		/*
2032 		 * One time registration of flow enable callback function
2033 		 */
2034 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2035 		    ill_flow_enable, ill);
2036 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2037 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
2038 	} else {
2039 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
2040 		    "capability, rc = %d\n", rc);
2041 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2042 	}
2043 }
2044 
2045 static void
2046 ill_capability_poll_enable(ill_t *ill)
2047 {
2048 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
2049 	dld_capab_poll_t	poll;
2050 	int			rc;
2051 
2052 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2053 
2054 	bzero(&poll, sizeof (poll));
2055 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2056 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2057 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2058 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2059 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2060 	poll.poll_ring_ch = ill;
2061 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2062 	    DLD_ENABLE);
2063 	if (rc == 0) {
2064 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2065 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
2066 	} else {
2067 		ip1dbg(("warning: could not enable POLL "
2068 		    "capability, rc = %d\n", rc));
2069 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2070 	}
2071 }
2072 
2073 /*
2074  * Enable the LSO capability.
2075  */
2076 static void
2077 ill_capability_lso_enable(ill_t *ill)
2078 {
2079 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2080 	dld_capab_lso_t	lso;
2081 	int rc;
2082 
2083 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2084 
2085 	if (ill->ill_lso_capab == NULL) {
2086 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2087 		    KM_NOSLEEP);
2088 		if (ill->ill_lso_capab == NULL) {
2089 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2090 			    "could not enable LSO for %s (ENOMEM)\n",
2091 			    ill->ill_name);
2092 			return;
2093 		}
2094 	}
2095 
2096 	bzero(&lso, sizeof (lso));
2097 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2098 	    DLD_ENABLE)) == 0) {
2099 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2100 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2101 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2102 		ip1dbg(("ill_capability_lso_enable: interface %s "
2103 		    "has enabled LSO\n ", ill->ill_name));
2104 	} else {
2105 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2106 		ill->ill_lso_capab = NULL;
2107 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2108 	}
2109 }
2110 
2111 static void
2112 ill_capability_dld_enable(ill_t *ill)
2113 {
2114 	mac_perim_handle_t mph;
2115 
2116 	ASSERT(IAM_WRITER_ILL(ill));
2117 
2118 	if (ill->ill_isv6)
2119 		return;
2120 
2121 	ill_mac_perim_enter(ill, &mph);
2122 	if (!ill->ill_isv6) {
2123 		ill_capability_direct_enable(ill);
2124 		ill_capability_poll_enable(ill);
2125 		ill_capability_lso_enable(ill);
2126 	}
2127 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2128 	ill_mac_perim_exit(ill, mph);
2129 }
2130 
2131 static void
2132 ill_capability_dld_disable(ill_t *ill)
2133 {
2134 	ill_dld_capab_t	*idc;
2135 	ill_dld_direct_t *idd;
2136 	mac_perim_handle_t	mph;
2137 
2138 	ASSERT(IAM_WRITER_ILL(ill));
2139 
2140 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2141 		return;
2142 
2143 	ill_mac_perim_enter(ill, &mph);
2144 
2145 	idc = ill->ill_dld_capab;
2146 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2147 		/*
2148 		 * For performance we avoid locks in the transmit data path
2149 		 * and don't maintain a count of the number of threads using
2150 		 * direct calls. Thus some threads could be using direct
2151 		 * transmit calls to GLD, even after the capability mechanism
2152 		 * turns it off. This is still safe since the handles used in
2153 		 * the direct calls continue to be valid until the unplumb is
2154 		 * completed. Remove the callback that was added (1-time) at
2155 		 * capab enable time.
2156 		 */
2157 		mutex_enter(&ill->ill_lock);
2158 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2159 		mutex_exit(&ill->ill_lock);
2160 		if (ill->ill_flownotify_mh != NULL) {
2161 			idd = &idc->idc_direct;
2162 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2163 			    ill->ill_flownotify_mh);
2164 			ill->ill_flownotify_mh = NULL;
2165 		}
2166 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2167 		    NULL, DLD_DISABLE);
2168 	}
2169 
2170 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2171 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2172 		ip_squeue_clean_all(ill);
2173 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2174 		    NULL, DLD_DISABLE);
2175 	}
2176 
2177 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2178 		ASSERT(ill->ill_lso_capab != NULL);
2179 		/*
2180 		 * Clear the capability flag for LSO but retain the
2181 		 * ill_lso_capab structure since it's possible that another
2182 		 * thread is still referring to it.  The structure only gets
2183 		 * deallocated when we destroy the ill.
2184 		 */
2185 
2186 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2187 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2188 		    NULL, DLD_DISABLE);
2189 	}
2190 
2191 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2192 	ill_mac_perim_exit(ill, mph);
2193 }
2194 
2195 /*
2196  * Capability Negotiation protocol
2197  *
2198  * We don't wait for DLPI capability operations to finish during interface
2199  * bringup or teardown. Doing so would introduce more asynchrony and the
2200  * interface up/down operations will need multiple return and restarts.
2201  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2202  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2203  * exclusive operation won't start until the DLPI operations of the previous
2204  * exclusive operation complete.
2205  *
2206  * The capability state machine is shown below.
2207  *
2208  * state		next state		event, action
2209  *
2210  * IDCS_UNKNOWN		IDCS_PROBE_SENT		ill_capability_probe
2211  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2212  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2213  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2214  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2215  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2216  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2217  *						    ill_capability_probe.
2218  */
2219 
2220 /*
2221  * Dedicated thread started from ip_stack_init that handles capability
2222  * disable. This thread ensures the taskq dispatch does not fail by waiting
2223  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2224  * that direct calls to DLD are done in a cv_waitable context.
2225  */
2226 void
2227 ill_taskq_dispatch(ip_stack_t *ipst)
2228 {
2229 	callb_cpr_t cprinfo;
2230 	char	name[64];
2231 	mblk_t	*mp;
2232 
2233 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2234 	    ipst->ips_netstack->netstack_stackid);
2235 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2236 	    name);
2237 	mutex_enter(&ipst->ips_capab_taskq_lock);
2238 
2239 	for (;;) {
2240 		mp = ipst->ips_capab_taskq_head;
2241 		while (mp != NULL) {
2242 			ipst->ips_capab_taskq_head = mp->b_next;
2243 			if (ipst->ips_capab_taskq_head == NULL)
2244 				ipst->ips_capab_taskq_tail = NULL;
2245 			mutex_exit(&ipst->ips_capab_taskq_lock);
2246 			mp->b_next = NULL;
2247 
2248 			VERIFY(taskq_dispatch(system_taskq,
2249 			    ill_capability_ack_thr, mp, TQ_SLEEP) !=
2250 			    TASKQID_INVALID);
2251 			mutex_enter(&ipst->ips_capab_taskq_lock);
2252 			mp = ipst->ips_capab_taskq_head;
2253 		}
2254 
2255 		if (ipst->ips_capab_taskq_quit)
2256 			break;
2257 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2258 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2259 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2260 	}
2261 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2262 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2263 	CALLB_CPR_EXIT(&cprinfo);
2264 	thread_exit();
2265 }
2266 
2267 /*
2268  * Consume a new-style hardware capabilities negotiation ack.
2269  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2270  */
2271 static void
2272 ill_capability_ack_thr(void *arg)
2273 {
2274 	mblk_t	*mp = arg;
2275 	dl_capability_ack_t *capp;
2276 	dl_capability_sub_t *subp, *endp;
2277 	ill_t	*ill;
2278 	boolean_t reneg;
2279 
2280 	ill = (ill_t *)mp->b_prev;
2281 	mp->b_prev = NULL;
2282 
2283 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2284 
2285 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2286 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2287 		/*
2288 		 * We have received the ack for our DL_CAPAB reset request.
2289 		 * There isnt' anything in the message that needs processing.
2290 		 * All message based capabilities have been disabled, now
2291 		 * do the function call based capability disable.
2292 		 */
2293 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2294 		ill_capability_dld_disable(ill);
2295 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2296 		if (reneg)
2297 			ill_capability_probe(ill);
2298 		goto done;
2299 	}
2300 
2301 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2302 		ill->ill_dlpi_capab_state = IDCS_OK;
2303 
2304 	capp = (dl_capability_ack_t *)mp->b_rptr;
2305 
2306 	if (capp->dl_sub_length == 0) {
2307 		/* no new-style capabilities */
2308 		goto done;
2309 	}
2310 
2311 	/* make sure the driver supplied correct dl_sub_length */
2312 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2313 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2314 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2315 		goto done;
2316 	}
2317 
2318 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2319 	/*
2320 	 * There are sub-capabilities. Process the ones we know about.
2321 	 * Loop until we don't have room for another sub-cap header..
2322 	 */
2323 	for (subp = SC(capp, capp->dl_sub_offset),
2324 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2325 	    subp <= endp;
2326 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2327 
2328 		switch (subp->dl_cap) {
2329 		case DL_CAPAB_ID_WRAPPER:
2330 			ill_capability_id_ack(ill, mp, subp);
2331 			break;
2332 		default:
2333 			ill_capability_dispatch(ill, mp, subp);
2334 			break;
2335 		}
2336 	}
2337 #undef SC
2338 done:
2339 	inet_freemsg(mp);
2340 	ill_capability_done(ill);
2341 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2342 }
2343 
2344 /*
2345  * This needs to be started in a taskq thread to provide a cv_waitable
2346  * context.
2347  */
2348 void
2349 ill_capability_ack(ill_t *ill, mblk_t *mp)
2350 {
2351 	ip_stack_t	*ipst = ill->ill_ipst;
2352 
2353 	mp->b_prev = (mblk_t *)ill;
2354 	ASSERT(mp->b_next == NULL);
2355 
2356 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2357 	    TQ_NOSLEEP) != TASKQID_INVALID)
2358 		return;
2359 
2360 	/*
2361 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2362 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2363 	 */
2364 	mutex_enter(&ipst->ips_capab_taskq_lock);
2365 	if (ipst->ips_capab_taskq_head == NULL) {
2366 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2367 		ipst->ips_capab_taskq_head = mp;
2368 	} else {
2369 		ipst->ips_capab_taskq_tail->b_next = mp;
2370 	}
2371 	ipst->ips_capab_taskq_tail = mp;
2372 
2373 	cv_signal(&ipst->ips_capab_taskq_cv);
2374 	mutex_exit(&ipst->ips_capab_taskq_lock);
2375 }
2376 
2377 /*
2378  * This routine is called to scan the fragmentation reassembly table for
2379  * the specified ILL for any packets that are starting to smell.
2380  * dead_interval is the maximum time in seconds that will be tolerated.  It
2381  * will either be the value specified in ip_g_frag_timeout, or zero if the
2382  * ILL is shutting down and it is time to blow everything off.
2383  *
2384  * It returns the number of seconds (as a time_t) that the next frag timer
2385  * should be scheduled for, 0 meaning that the timer doesn't need to be
2386  * re-started.  Note that the method of calculating next_timeout isn't
2387  * entirely accurate since time will flow between the time we grab
2388  * current_time and the time we schedule the next timeout.  This isn't a
2389  * big problem since this is the timer for sending an ICMP reassembly time
2390  * exceeded messages, and it doesn't have to be exactly accurate.
2391  *
2392  * This function is
2393  * sometimes called as writer, although this is not required.
2394  */
2395 time_t
2396 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2397 {
2398 	ipfb_t	*ipfb;
2399 	ipfb_t	*endp;
2400 	ipf_t	*ipf;
2401 	ipf_t	*ipfnext;
2402 	mblk_t	*mp;
2403 	time_t	current_time = gethrestime_sec();
2404 	time_t	next_timeout = 0;
2405 	uint32_t	hdr_length;
2406 	mblk_t	*send_icmp_head;
2407 	mblk_t	*send_icmp_head_v6;
2408 	ip_stack_t *ipst = ill->ill_ipst;
2409 	ip_recv_attr_t iras;
2410 
2411 	bzero(&iras, sizeof (iras));
2412 	iras.ira_flags = 0;
2413 	iras.ira_ill = iras.ira_rill = ill;
2414 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2415 	iras.ira_rifindex = iras.ira_ruifindex;
2416 
2417 	ipfb = ill->ill_frag_hash_tbl;
2418 	if (ipfb == NULL)
2419 		return (B_FALSE);
2420 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2421 	/* Walk the frag hash table. */
2422 	for (; ipfb < endp; ipfb++) {
2423 		send_icmp_head = NULL;
2424 		send_icmp_head_v6 = NULL;
2425 		mutex_enter(&ipfb->ipfb_lock);
2426 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2427 			time_t frag_time = current_time - ipf->ipf_timestamp;
2428 			time_t frag_timeout;
2429 
2430 			if (frag_time < dead_interval) {
2431 				/*
2432 				 * There are some outstanding fragments
2433 				 * that will timeout later.  Make note of
2434 				 * the time so that we can reschedule the
2435 				 * next timeout appropriately.
2436 				 */
2437 				frag_timeout = dead_interval - frag_time;
2438 				if (next_timeout == 0 ||
2439 				    frag_timeout < next_timeout) {
2440 					next_timeout = frag_timeout;
2441 				}
2442 				break;
2443 			}
2444 			/* Time's up.  Get it out of here. */
2445 			hdr_length = ipf->ipf_nf_hdr_len;
2446 			ipfnext = ipf->ipf_hash_next;
2447 			if (ipfnext)
2448 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2449 			*ipf->ipf_ptphn = ipfnext;
2450 			mp = ipf->ipf_mp->b_cont;
2451 			for (; mp; mp = mp->b_cont) {
2452 				/* Extra points for neatness. */
2453 				IP_REASS_SET_START(mp, 0);
2454 				IP_REASS_SET_END(mp, 0);
2455 			}
2456 			mp = ipf->ipf_mp->b_cont;
2457 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2458 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2459 			ipfb->ipfb_count -= ipf->ipf_count;
2460 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2461 			ipfb->ipfb_frag_pkts--;
2462 			/*
2463 			 * We do not send any icmp message from here because
2464 			 * we currently are holding the ipfb_lock for this
2465 			 * hash chain. If we try and send any icmp messages
2466 			 * from here we may end up via a put back into ip
2467 			 * trying to get the same lock, causing a recursive
2468 			 * mutex panic. Instead we build a list and send all
2469 			 * the icmp messages after we have dropped the lock.
2470 			 */
2471 			if (ill->ill_isv6) {
2472 				if (hdr_length != 0) {
2473 					mp->b_next = send_icmp_head_v6;
2474 					send_icmp_head_v6 = mp;
2475 				} else {
2476 					freemsg(mp);
2477 				}
2478 			} else {
2479 				if (hdr_length != 0) {
2480 					mp->b_next = send_icmp_head;
2481 					send_icmp_head = mp;
2482 				} else {
2483 					freemsg(mp);
2484 				}
2485 			}
2486 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2487 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2488 			freeb(ipf->ipf_mp);
2489 		}
2490 		mutex_exit(&ipfb->ipfb_lock);
2491 		/*
2492 		 * Now need to send any icmp messages that we delayed from
2493 		 * above.
2494 		 */
2495 		while (send_icmp_head_v6 != NULL) {
2496 			ip6_t *ip6h;
2497 
2498 			mp = send_icmp_head_v6;
2499 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2500 			mp->b_next = NULL;
2501 			ip6h = (ip6_t *)mp->b_rptr;
2502 			iras.ira_flags = 0;
2503 			/*
2504 			 * This will result in an incorrect ALL_ZONES zoneid
2505 			 * for multicast packets, but we
2506 			 * don't send ICMP errors for those in any case.
2507 			 */
2508 			iras.ira_zoneid =
2509 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2510 			    ill, ipst);
2511 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2512 			icmp_time_exceeded_v6(mp,
2513 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2514 			    &iras);
2515 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2516 		}
2517 		while (send_icmp_head != NULL) {
2518 			ipaddr_t dst;
2519 
2520 			mp = send_icmp_head;
2521 			send_icmp_head = send_icmp_head->b_next;
2522 			mp->b_next = NULL;
2523 
2524 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2525 
2526 			iras.ira_flags = IRAF_IS_IPV4;
2527 			/*
2528 			 * This will result in an incorrect ALL_ZONES zoneid
2529 			 * for broadcast and multicast packets, but we
2530 			 * don't send ICMP errors for those in any case.
2531 			 */
2532 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2533 			    ill, ipst);
2534 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2535 			icmp_time_exceeded(mp,
2536 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2537 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2538 		}
2539 	}
2540 	/*
2541 	 * A non-dying ILL will use the return value to decide whether to
2542 	 * restart the frag timer, and for how long.
2543 	 */
2544 	return (next_timeout);
2545 }
2546 
2547 /*
2548  * This routine is called when the approximate count of mblk memory used
2549  * for the specified ILL has exceeded max_count.
2550  */
2551 void
2552 ill_frag_prune(ill_t *ill, uint_t max_count)
2553 {
2554 	ipfb_t	*ipfb;
2555 	ipf_t	*ipf;
2556 	size_t	count;
2557 	clock_t now;
2558 
2559 	/*
2560 	 * If we are here within ip_min_frag_prune_time msecs remove
2561 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2562 	 * ill_frag_free_num_pkts.
2563 	 */
2564 	mutex_enter(&ill->ill_lock);
2565 	now = ddi_get_lbolt();
2566 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2567 	    (ip_min_frag_prune_time != 0 ?
2568 	    ip_min_frag_prune_time : msec_per_tick)) {
2569 
2570 		ill->ill_frag_free_num_pkts++;
2571 
2572 	} else {
2573 		ill->ill_frag_free_num_pkts = 0;
2574 	}
2575 	ill->ill_last_frag_clean_time = now;
2576 	mutex_exit(&ill->ill_lock);
2577 
2578 	/*
2579 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2580 	 */
2581 	if (ill->ill_frag_free_num_pkts != 0) {
2582 		int ix;
2583 
2584 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2585 			ipfb = &ill->ill_frag_hash_tbl[ix];
2586 			mutex_enter(&ipfb->ipfb_lock);
2587 			if (ipfb->ipfb_ipf != NULL) {
2588 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2589 				    ill->ill_frag_free_num_pkts);
2590 			}
2591 			mutex_exit(&ipfb->ipfb_lock);
2592 		}
2593 	}
2594 	/*
2595 	 * While the reassembly list for this ILL is too big, prune a fragment
2596 	 * queue by age, oldest first.
2597 	 */
2598 	while (ill->ill_frag_count > max_count) {
2599 		int	ix;
2600 		ipfb_t	*oipfb = NULL;
2601 		uint_t	oldest = UINT_MAX;
2602 
2603 		count = 0;
2604 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2605 			ipfb = &ill->ill_frag_hash_tbl[ix];
2606 			mutex_enter(&ipfb->ipfb_lock);
2607 			ipf = ipfb->ipfb_ipf;
2608 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2609 				oldest = ipf->ipf_gen;
2610 				oipfb = ipfb;
2611 			}
2612 			count += ipfb->ipfb_count;
2613 			mutex_exit(&ipfb->ipfb_lock);
2614 		}
2615 		if (oipfb == NULL)
2616 			break;
2617 
2618 		if (count <= max_count)
2619 			return;	/* Somebody beat us to it, nothing to do */
2620 		mutex_enter(&oipfb->ipfb_lock);
2621 		ipf = oipfb->ipfb_ipf;
2622 		if (ipf != NULL) {
2623 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2624 		}
2625 		mutex_exit(&oipfb->ipfb_lock);
2626 	}
2627 }
2628 
2629 /*
2630  * free 'free_cnt' fragmented packets starting at ipf.
2631  */
2632 void
2633 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2634 {
2635 	size_t	count;
2636 	mblk_t	*mp;
2637 	mblk_t	*tmp;
2638 	ipf_t **ipfp = ipf->ipf_ptphn;
2639 
2640 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2641 	ASSERT(ipfp != NULL);
2642 	ASSERT(ipf != NULL);
2643 
2644 	while (ipf != NULL && free_cnt-- > 0) {
2645 		count = ipf->ipf_count;
2646 		mp = ipf->ipf_mp;
2647 		ipf = ipf->ipf_hash_next;
2648 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2649 			IP_REASS_SET_START(tmp, 0);
2650 			IP_REASS_SET_END(tmp, 0);
2651 		}
2652 		atomic_add_32(&ill->ill_frag_count, -count);
2653 		ASSERT(ipfb->ipfb_count >= count);
2654 		ipfb->ipfb_count -= count;
2655 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2656 		ipfb->ipfb_frag_pkts--;
2657 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2658 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2659 		freemsg(mp);
2660 	}
2661 
2662 	if (ipf)
2663 		ipf->ipf_ptphn = ipfp;
2664 	ipfp[0] = ipf;
2665 }
2666 
2667 /*
2668  * Helper function for ill_forward_set().
2669  */
2670 static void
2671 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2672 {
2673 	ip_stack_t	*ipst = ill->ill_ipst;
2674 
2675 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2676 
2677 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2678 	    (enable ? "Enabling" : "Disabling"),
2679 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2680 	mutex_enter(&ill->ill_lock);
2681 	if (enable)
2682 		ill->ill_flags |= ILLF_ROUTER;
2683 	else
2684 		ill->ill_flags &= ~ILLF_ROUTER;
2685 	mutex_exit(&ill->ill_lock);
2686 	if (ill->ill_isv6)
2687 		ill_set_nce_router_flags(ill, enable);
2688 	/* Notify routing socket listeners of this change. */
2689 	if (ill->ill_ipif != NULL)
2690 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2691 }
2692 
2693 /*
2694  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2695  * socket messages for each interface whose flags we change.
2696  */
2697 int
2698 ill_forward_set(ill_t *ill, boolean_t enable)
2699 {
2700 	ipmp_illgrp_t *illg;
2701 	ip_stack_t *ipst = ill->ill_ipst;
2702 
2703 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2704 
2705 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2706 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2707 		return (0);
2708 
2709 	if (IS_LOOPBACK(ill))
2710 		return (EINVAL);
2711 
2712 	if (enable && ill->ill_allowed_ips_cnt > 0)
2713 		return (EPERM);
2714 
2715 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2716 		/*
2717 		 * Update all of the interfaces in the group.
2718 		 */
2719 		illg = ill->ill_grp;
2720 		ill = list_head(&illg->ig_if);
2721 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2722 			ill_forward_set_on_ill(ill, enable);
2723 
2724 		/*
2725 		 * Update the IPMP meta-interface.
2726 		 */
2727 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2728 		return (0);
2729 	}
2730 
2731 	ill_forward_set_on_ill(ill, enable);
2732 	return (0);
2733 }
2734 
2735 /*
2736  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2737  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2738  * set or clear.
2739  */
2740 static void
2741 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2742 {
2743 	ipif_t *ipif;
2744 	ncec_t *ncec;
2745 	nce_t *nce;
2746 
2747 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2748 		/*
2749 		 * NOTE: we match across the illgrp because nce's for
2750 		 * addresses on IPMP interfaces have an nce_ill that points to
2751 		 * the bound underlying ill.
2752 		 */
2753 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2754 		if (nce != NULL) {
2755 			ncec = nce->nce_common;
2756 			mutex_enter(&ncec->ncec_lock);
2757 			if (enable)
2758 				ncec->ncec_flags |= NCE_F_ISROUTER;
2759 			else
2760 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2761 			mutex_exit(&ncec->ncec_lock);
2762 			nce_refrele(nce);
2763 		}
2764 	}
2765 }
2766 
2767 /*
2768  * Intializes the context structure and returns the first ill in the list
2769  * cuurently start_list and end_list can have values:
2770  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2771  * IP_V4_G_HEAD		Traverse IPV4 list only.
2772  * IP_V6_G_HEAD		Traverse IPV6 list only.
2773  */
2774 
2775 /*
2776  * We don't check for CONDEMNED ills here. Caller must do that if
2777  * necessary under the ill lock.
2778  */
2779 ill_t *
2780 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2781     ip_stack_t *ipst)
2782 {
2783 	ill_if_t *ifp;
2784 	ill_t *ill;
2785 	avl_tree_t *avl_tree;
2786 
2787 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2788 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2789 
2790 	/*
2791 	 * setup the lists to search
2792 	 */
2793 	if (end_list != MAX_G_HEADS) {
2794 		ctx->ctx_current_list = start_list;
2795 		ctx->ctx_last_list = end_list;
2796 	} else {
2797 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2798 		ctx->ctx_current_list = 0;
2799 	}
2800 
2801 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2802 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2803 		if (ifp != (ill_if_t *)
2804 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2805 			avl_tree = &ifp->illif_avl_by_ppa;
2806 			ill = avl_first(avl_tree);
2807 			/*
2808 			 * ill is guaranteed to be non NULL or ifp should have
2809 			 * not existed.
2810 			 */
2811 			ASSERT(ill != NULL);
2812 			return (ill);
2813 		}
2814 		ctx->ctx_current_list++;
2815 	}
2816 
2817 	return (NULL);
2818 }
2819 
2820 /*
2821  * returns the next ill in the list. ill_first() must have been called
2822  * before calling ill_next() or bad things will happen.
2823  */
2824 
2825 /*
2826  * We don't check for CONDEMNED ills here. Caller must do that if
2827  * necessary under the ill lock.
2828  */
2829 ill_t *
2830 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2831 {
2832 	ill_if_t *ifp;
2833 	ill_t *ill;
2834 	ip_stack_t	*ipst = lastill->ill_ipst;
2835 
2836 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2837 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2838 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2839 	    AVL_AFTER)) != NULL) {
2840 		return (ill);
2841 	}
2842 
2843 	/* goto next ill_ifp in the list. */
2844 	ifp = lastill->ill_ifptr->illif_next;
2845 
2846 	/* make sure not at end of circular list */
2847 	while (ifp ==
2848 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2849 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2850 			return (NULL);
2851 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2852 	}
2853 
2854 	return (avl_first(&ifp->illif_avl_by_ppa));
2855 }
2856 
2857 /*
2858  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2859  * The final number (PPA) must not have any leading zeros.  Upon success, a
2860  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2861  */
2862 static char *
2863 ill_get_ppa_ptr(char *name)
2864 {
2865 	int namelen = strlen(name);
2866 	int end_ndx = namelen - 1;
2867 	int ppa_ndx, i;
2868 
2869 	/*
2870 	 * Check that the first character is [a-zA-Z], and that the last
2871 	 * character is [0-9].
2872 	 */
2873 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2874 		return (NULL);
2875 
2876 	/*
2877 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2878 	 */
2879 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2880 		if (!isdigit(name[ppa_ndx - 1]))
2881 			break;
2882 
2883 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2884 		return (NULL);
2885 
2886 	/*
2887 	 * Check that the intermediate characters are [a-z0-9.]
2888 	 */
2889 	for (i = 1; i < ppa_ndx; i++) {
2890 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2891 		    name[i] != '.' && name[i] != '_') {
2892 			return (NULL);
2893 		}
2894 	}
2895 
2896 	return (name + ppa_ndx);
2897 }
2898 
2899 /*
2900  * use avl tree to locate the ill.
2901  */
2902 static ill_t *
2903 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2904 {
2905 	char *ppa_ptr = NULL;
2906 	int len;
2907 	uint_t ppa;
2908 	ill_t *ill = NULL;
2909 	ill_if_t *ifp;
2910 	int list;
2911 
2912 	/*
2913 	 * get ppa ptr
2914 	 */
2915 	if (isv6)
2916 		list = IP_V6_G_HEAD;
2917 	else
2918 		list = IP_V4_G_HEAD;
2919 
2920 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2921 		return (NULL);
2922 	}
2923 
2924 	len = ppa_ptr - name + 1;
2925 
2926 	ppa = stoi(&ppa_ptr);
2927 
2928 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2929 
2930 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2931 		/*
2932 		 * match is done on len - 1 as the name is not null
2933 		 * terminated it contains ppa in addition to the interface
2934 		 * name.
2935 		 */
2936 		if ((ifp->illif_name_len == len) &&
2937 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2938 			break;
2939 		} else {
2940 			ifp = ifp->illif_next;
2941 		}
2942 	}
2943 
2944 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2945 		/*
2946 		 * Even the interface type does not exist.
2947 		 */
2948 		return (NULL);
2949 	}
2950 
2951 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2952 	if (ill != NULL) {
2953 		mutex_enter(&ill->ill_lock);
2954 		if (ILL_CAN_LOOKUP(ill)) {
2955 			ill_refhold_locked(ill);
2956 			mutex_exit(&ill->ill_lock);
2957 			return (ill);
2958 		}
2959 		mutex_exit(&ill->ill_lock);
2960 	}
2961 	return (NULL);
2962 }
2963 
2964 /*
2965  * comparison function for use with avl.
2966  */
2967 static int
2968 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2969 {
2970 	uint_t ppa;
2971 	uint_t ill_ppa;
2972 
2973 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2974 
2975 	ppa = *((uint_t *)ppa_ptr);
2976 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2977 	/*
2978 	 * We want the ill with the lowest ppa to be on the
2979 	 * top.
2980 	 */
2981 	if (ill_ppa < ppa)
2982 		return (1);
2983 	if (ill_ppa > ppa)
2984 		return (-1);
2985 	return (0);
2986 }
2987 
2988 /*
2989  * remove an interface type from the global list.
2990  */
2991 static void
2992 ill_delete_interface_type(ill_if_t *interface)
2993 {
2994 	ASSERT(interface != NULL);
2995 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2996 
2997 	avl_destroy(&interface->illif_avl_by_ppa);
2998 	if (interface->illif_ppa_arena != NULL)
2999 		vmem_destroy(interface->illif_ppa_arena);
3000 
3001 	remque(interface);
3002 
3003 	mi_free(interface);
3004 }
3005 
3006 /*
3007  * remove ill from the global list.
3008  */
3009 static void
3010 ill_glist_delete(ill_t *ill)
3011 {
3012 	ip_stack_t	*ipst;
3013 	phyint_t	*phyi;
3014 
3015 	if (ill == NULL)
3016 		return;
3017 	ipst = ill->ill_ipst;
3018 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3019 
3020 	/*
3021 	 * If the ill was never inserted into the AVL tree
3022 	 * we skip the if branch.
3023 	 */
3024 	if (ill->ill_ifptr != NULL) {
3025 		/*
3026 		 * remove from AVL tree and free ppa number
3027 		 */
3028 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3029 
3030 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3031 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3032 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3033 		}
3034 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3035 			ill_delete_interface_type(ill->ill_ifptr);
3036 		}
3037 
3038 		/*
3039 		 * Indicate ill is no longer in the list.
3040 		 */
3041 		ill->ill_ifptr = NULL;
3042 		ill->ill_name_length = 0;
3043 		ill->ill_name[0] = '\0';
3044 		ill->ill_ppa = UINT_MAX;
3045 	}
3046 
3047 	/* Generate one last event for this ill. */
3048 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3049 	    ill->ill_name_length);
3050 
3051 	ASSERT(ill->ill_phyint != NULL);
3052 	phyi = ill->ill_phyint;
3053 	ill->ill_phyint = NULL;
3054 
3055 	/*
3056 	 * ill_init allocates a phyint always to store the copy
3057 	 * of flags relevant to phyint. At that point in time, we could
3058 	 * not assign the name and hence phyint_illv4/v6 could not be
3059 	 * initialized. Later in ipif_set_values, we assign the name to
3060 	 * the ill, at which point in time we assign phyint_illv4/v6.
3061 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3062 	 */
3063 	if (ill->ill_flags & ILLF_IPV6)
3064 		phyi->phyint_illv6 = NULL;
3065 	else
3066 		phyi->phyint_illv4 = NULL;
3067 
3068 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3069 		rw_exit(&ipst->ips_ill_g_lock);
3070 		return;
3071 	}
3072 
3073 	/*
3074 	 * There are no ills left on this phyint; pull it out of the phyint
3075 	 * avl trees, and free it.
3076 	 */
3077 	if (phyi->phyint_ifindex > 0) {
3078 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3079 		    phyi);
3080 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3081 		    phyi);
3082 	}
3083 	rw_exit(&ipst->ips_ill_g_lock);
3084 
3085 	phyint_free(phyi);
3086 }
3087 
3088 /*
3089  * allocate a ppa, if the number of plumbed interfaces of this type are
3090  * less than ill_no_arena do a linear search to find a unused ppa.
3091  * When the number goes beyond ill_no_arena switch to using an arena.
3092  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3093  * is the return value for an error condition, so allocation starts at one
3094  * and is decremented by one.
3095  */
3096 static int
3097 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3098 {
3099 	ill_t *tmp_ill;
3100 	uint_t start, end;
3101 	int ppa;
3102 
3103 	if (ifp->illif_ppa_arena == NULL &&
3104 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3105 		/*
3106 		 * Create an arena.
3107 		 */
3108 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3109 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3110 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3111 			/* allocate what has already been assigned */
3112 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3113 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3114 		    tmp_ill, AVL_AFTER)) {
3115 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3116 			    1,		/* size */
3117 			    1,		/* align/quantum */
3118 			    0,		/* phase */
3119 			    0,		/* nocross */
3120 			    /* minaddr */
3121 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3122 			    /* maxaddr */
3123 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3124 			    VM_NOSLEEP|VM_FIRSTFIT);
3125 			if (ppa == 0) {
3126 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3127 				    " failed while switching"));
3128 				vmem_destroy(ifp->illif_ppa_arena);
3129 				ifp->illif_ppa_arena = NULL;
3130 				break;
3131 			}
3132 		}
3133 	}
3134 
3135 	if (ifp->illif_ppa_arena != NULL) {
3136 		if (ill->ill_ppa == UINT_MAX) {
3137 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3138 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3139 			if (ppa == 0)
3140 				return (EAGAIN);
3141 			ill->ill_ppa = --ppa;
3142 		} else {
3143 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3144 			    1,		/* size */
3145 			    1,		/* align/quantum */
3146 			    0,		/* phase */
3147 			    0,		/* nocross */
3148 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3149 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3150 			    VM_NOSLEEP|VM_FIRSTFIT);
3151 			/*
3152 			 * Most likely the allocation failed because
3153 			 * the requested ppa was in use.
3154 			 */
3155 			if (ppa == 0)
3156 				return (EEXIST);
3157 		}
3158 		return (0);
3159 	}
3160 
3161 	/*
3162 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3163 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3164 	 */
3165 	if (ill->ill_ppa == UINT_MAX) {
3166 		end = UINT_MAX - 1;
3167 		start = 0;
3168 	} else {
3169 		end = start = ill->ill_ppa;
3170 	}
3171 
3172 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3173 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3174 		if (start++ >= end) {
3175 			if (ill->ill_ppa == UINT_MAX)
3176 				return (EAGAIN);
3177 			else
3178 				return (EEXIST);
3179 		}
3180 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3181 	}
3182 	ill->ill_ppa = start;
3183 	return (0);
3184 }
3185 
3186 /*
3187  * Insert ill into the list of configured ill's. Once this function completes,
3188  * the ill is globally visible and is available through lookups. More precisely
3189  * this happens after the caller drops the ill_g_lock.
3190  */
3191 static int
3192 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3193 {
3194 	ill_if_t *ill_interface;
3195 	avl_index_t where = 0;
3196 	int error;
3197 	int name_length;
3198 	int index;
3199 	boolean_t check_length = B_FALSE;
3200 	ip_stack_t	*ipst = ill->ill_ipst;
3201 
3202 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3203 
3204 	name_length = mi_strlen(name) + 1;
3205 
3206 	if (isv6)
3207 		index = IP_V6_G_HEAD;
3208 	else
3209 		index = IP_V4_G_HEAD;
3210 
3211 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3212 	/*
3213 	 * Search for interface type based on name
3214 	 */
3215 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3216 		if ((ill_interface->illif_name_len == name_length) &&
3217 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3218 			break;
3219 		}
3220 		ill_interface = ill_interface->illif_next;
3221 	}
3222 
3223 	/*
3224 	 * Interface type not found, create one.
3225 	 */
3226 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3227 		ill_g_head_t ghead;
3228 
3229 		/*
3230 		 * allocate ill_if_t structure
3231 		 */
3232 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3233 		if (ill_interface == NULL) {
3234 			return (ENOMEM);
3235 		}
3236 
3237 		(void) strcpy(ill_interface->illif_name, name);
3238 		ill_interface->illif_name_len = name_length;
3239 
3240 		avl_create(&ill_interface->illif_avl_by_ppa,
3241 		    ill_compare_ppa, sizeof (ill_t),
3242 		    offsetof(struct ill_s, ill_avl_byppa));
3243 
3244 		/*
3245 		 * link the structure in the back to maintain order
3246 		 * of configuration for ifconfig output.
3247 		 */
3248 		ghead = ipst->ips_ill_g_heads[index];
3249 		insque(ill_interface, ghead.ill_g_list_tail);
3250 	}
3251 
3252 	if (ill->ill_ppa == UINT_MAX)
3253 		check_length = B_TRUE;
3254 
3255 	error = ill_alloc_ppa(ill_interface, ill);
3256 	if (error != 0) {
3257 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3258 			ill_delete_interface_type(ill->ill_ifptr);
3259 		return (error);
3260 	}
3261 
3262 	/*
3263 	 * When the ppa is choosen by the system, check that there is
3264 	 * enough space to insert ppa. if a specific ppa was passed in this
3265 	 * check is not required as the interface name passed in will have
3266 	 * the right ppa in it.
3267 	 */
3268 	if (check_length) {
3269 		/*
3270 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3271 		 */
3272 		char buf[sizeof (uint_t) * 3];
3273 
3274 		/*
3275 		 * convert ppa to string to calculate the amount of space
3276 		 * required for it in the name.
3277 		 */
3278 		numtos(ill->ill_ppa, buf);
3279 
3280 		/* Do we have enough space to insert ppa ? */
3281 
3282 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3283 			/* Free ppa and interface type struct */
3284 			if (ill_interface->illif_ppa_arena != NULL) {
3285 				vmem_free(ill_interface->illif_ppa_arena,
3286 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3287 			}
3288 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3289 				ill_delete_interface_type(ill->ill_ifptr);
3290 
3291 			return (EINVAL);
3292 		}
3293 	}
3294 
3295 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3296 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3297 
3298 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3299 	    &where);
3300 	ill->ill_ifptr = ill_interface;
3301 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3302 
3303 	ill_phyint_reinit(ill);
3304 	return (0);
3305 }
3306 
3307 /* Initialize the per phyint ipsq used for serialization */
3308 static boolean_t
3309 ipsq_init(ill_t *ill, boolean_t enter)
3310 {
3311 	ipsq_t  *ipsq;
3312 	ipxop_t	*ipx;
3313 
3314 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3315 		return (B_FALSE);
3316 
3317 	ill->ill_phyint->phyint_ipsq = ipsq;
3318 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3319 	ipx->ipx_ipsq = ipsq;
3320 	ipsq->ipsq_next = ipsq;
3321 	ipsq->ipsq_phyint = ill->ill_phyint;
3322 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3323 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3324 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3325 	if (enter) {
3326 		ipx->ipx_writer = curthread;
3327 		ipx->ipx_forced = B_FALSE;
3328 		ipx->ipx_reentry_cnt = 1;
3329 #ifdef DEBUG
3330 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3331 #endif
3332 	}
3333 	return (B_TRUE);
3334 }
3335 
3336 /*
3337  * Here we perform initialisation of the ill_t common to both regular
3338  * interface ILLs and the special loopback ILL created by ill_lookup_on_name.
3339  */
3340 static int
3341 ill_init_common(ill_t *ill, queue_t *q, boolean_t isv6, boolean_t is_loopback,
3342     boolean_t ipsq_enter)
3343 {
3344 	int count;
3345 	uchar_t *frag_ptr;
3346 
3347 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3348 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3349 	ill->ill_saved_ire_cnt = 0;
3350 
3351 	if (is_loopback) {
3352 		ill->ill_max_frag = isv6 ? ip_loopback_mtu_v6plus :
3353 		    ip_loopback_mtuplus;
3354 		/*
3355 		 * No resolver here.
3356 		 */
3357 		ill->ill_net_type = IRE_LOOPBACK;
3358 	} else {
3359 		ill->ill_rq = q;
3360 		ill->ill_wq = WR(q);
3361 		ill->ill_ppa = UINT_MAX;
3362 	}
3363 
3364 	ill->ill_isv6 = isv6;
3365 
3366 	/*
3367 	 * Allocate sufficient space to contain our fragment hash table and
3368 	 * the device name.
3369 	 */
3370 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3371 	if (frag_ptr == NULL)
3372 		return (ENOMEM);
3373 	ill->ill_frag_ptr = frag_ptr;
3374 	ill->ill_frag_free_num_pkts = 0;
3375 	ill->ill_last_frag_clean_time = 0;
3376 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3377 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3378 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3379 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3380 		    NULL, MUTEX_DEFAULT, NULL);
3381 	}
3382 
3383 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3384 	if (ill->ill_phyint == NULL) {
3385 		mi_free(frag_ptr);
3386 		return (ENOMEM);
3387 	}
3388 
3389 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3390 	if (isv6) {
3391 		ill->ill_phyint->phyint_illv6 = ill;
3392 	} else {
3393 		ill->ill_phyint->phyint_illv4 = ill;
3394 	}
3395 	if (is_loopback) {
3396 		phyint_flags_init(ill->ill_phyint, DL_LOOP);
3397 	}
3398 
3399 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3400 
3401 	ill_set_inputfn(ill);
3402 
3403 	if (!ipsq_init(ill, ipsq_enter)) {
3404 		mi_free(frag_ptr);
3405 		mi_free(ill->ill_phyint);
3406 		return (ENOMEM);
3407 	}
3408 
3409 	/* Frag queue limit stuff */
3410 	ill->ill_frag_count = 0;
3411 	ill->ill_ipf_gen = 0;
3412 
3413 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3414 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3415 	ill->ill_global_timer = INFINITY;
3416 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3417 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3418 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3419 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3420 
3421 	/*
3422 	 * Initialize IPv6 configuration variables.  The IP module is always
3423 	 * opened as an IPv4 module.  Instead tracking down the cases where
3424 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3425 	 * here for convenience, this has no effect until the ill is set to do
3426 	 * IPv6.
3427 	 */
3428 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3429 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3430 	ill->ill_max_buf = ND_MAX_Q;
3431 	ill->ill_refcnt = 0;
3432 
3433 	return (0);
3434 }
3435 
3436 /*
3437  * ill_init is called by ip_open when a device control stream is opened.
3438  * It does a few initializations, and shoots a DL_INFO_REQ message down
3439  * to the driver.  The response is later picked up in ip_rput_dlpi and
3440  * used to set up default mechanisms for talking to the driver.  (Always
3441  * called as writer.)
3442  *
3443  * If this function returns error, ip_open will call ip_close which in
3444  * turn will call ill_delete to clean up any memory allocated here that
3445  * is not yet freed.
3446  *
3447  * Note: ill_ipst and ill_zoneid must be set before calling ill_init.
3448  */
3449 int
3450 ill_init(queue_t *q, ill_t *ill)
3451 {
3452 	int ret;
3453 	dl_info_req_t	*dlir;
3454 	mblk_t	*info_mp;
3455 
3456 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3457 	    BPRI_HI);
3458 	if (info_mp == NULL)
3459 		return (ENOMEM);
3460 
3461 	/*
3462 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3463 	 * at this point because of the following reason. If we can't
3464 	 * enter the ipsq at some point and cv_wait, the writer that
3465 	 * wakes us up tries to locate us using the list of all phyints
3466 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3467 	 * If we don't set it now, we risk a missed wakeup.
3468 	 */
3469 	if ((ret = ill_init_common(ill, q, B_FALSE, B_FALSE, B_TRUE)) != 0) {
3470 		freemsg(info_mp);
3471 		return (ret);
3472 	}
3473 
3474 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3475 
3476 	/* Send down the Info Request to the driver. */
3477 	info_mp->b_datap->db_type = M_PCPROTO;
3478 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3479 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3480 	dlir->dl_primitive = DL_INFO_REQ;
3481 
3482 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3483 
3484 	qprocson(q);
3485 	ill_dlpi_send(ill, info_mp);
3486 
3487 	return (0);
3488 }
3489 
3490 /*
3491  * ill_dls_info
3492  * creates datalink socket info from the device.
3493  */
3494 int
3495 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3496 {
3497 	size_t	len;
3498 
3499 	sdl->sdl_family = AF_LINK;
3500 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3501 	sdl->sdl_type = ill->ill_type;
3502 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3503 	len = strlen(sdl->sdl_data);
3504 	ASSERT(len < 256);
3505 	sdl->sdl_nlen = (uchar_t)len;
3506 	sdl->sdl_alen = ill->ill_phys_addr_length;
3507 	sdl->sdl_slen = 0;
3508 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3509 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3510 
3511 	return (sizeof (struct sockaddr_dl));
3512 }
3513 
3514 /*
3515  * ill_xarp_info
3516  * creates xarp info from the device.
3517  */
3518 static int
3519 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3520 {
3521 	sdl->sdl_family = AF_LINK;
3522 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3523 	sdl->sdl_type = ill->ill_type;
3524 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3525 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3526 	sdl->sdl_alen = ill->ill_phys_addr_length;
3527 	sdl->sdl_slen = 0;
3528 	return (sdl->sdl_nlen);
3529 }
3530 
3531 static int
3532 loopback_kstat_update(kstat_t *ksp, int rw)
3533 {
3534 	kstat_named_t *kn;
3535 	netstackid_t	stackid;
3536 	netstack_t	*ns;
3537 	ip_stack_t	*ipst;
3538 
3539 	if (ksp == NULL || ksp->ks_data == NULL)
3540 		return (EIO);
3541 
3542 	if (rw == KSTAT_WRITE)
3543 		return (EACCES);
3544 
3545 	kn = KSTAT_NAMED_PTR(ksp);
3546 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3547 
3548 	ns = netstack_find_by_stackid(stackid);
3549 	if (ns == NULL)
3550 		return (-1);
3551 
3552 	ipst = ns->netstack_ip;
3553 	if (ipst == NULL) {
3554 		netstack_rele(ns);
3555 		return (-1);
3556 	}
3557 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3558 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3559 	netstack_rele(ns);
3560 	return (0);
3561 }
3562 
3563 /*
3564  * Has ifindex been plumbed already?
3565  */
3566 static boolean_t
3567 phyint_exists(uint_t index, ip_stack_t *ipst)
3568 {
3569 	ASSERT(index != 0);
3570 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3571 
3572 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3573 	    &index, NULL) != NULL);
3574 }
3575 
3576 /*
3577  * Pick a unique ifindex.
3578  * When the index counter passes IF_INDEX_MAX for the first time, the wrap
3579  * flag is set so that next time time ip_assign_ifindex() is called, it
3580  * falls through and resets the index counter back to 1, the minimum value
3581  * for the interface index. The logic below assumes that ips_ill_index
3582  * can hold a value of IF_INDEX_MAX+1 without there being any loss
3583  * (i.e. reset back to 0.)
3584  */
3585 boolean_t
3586 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3587 {
3588 	uint_t loops;
3589 
3590 	if (!ipst->ips_ill_index_wrap) {
3591 		*indexp = ipst->ips_ill_index++;
3592 		if (ipst->ips_ill_index > IF_INDEX_MAX) {
3593 			/*
3594 			 * Reached the maximum ifindex value, set the wrap
3595 			 * flag to indicate that it is no longer possible
3596 			 * to assume that a given index is unallocated.
3597 			 */
3598 			ipst->ips_ill_index_wrap = B_TRUE;
3599 		}
3600 		return (B_TRUE);
3601 	}
3602 
3603 	if (ipst->ips_ill_index > IF_INDEX_MAX)
3604 		ipst->ips_ill_index = 1;
3605 
3606 	/*
3607 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3608 	 * at this point and don't want to call any function that attempts
3609 	 * to get the lock again.
3610 	 */
3611 	for (loops = IF_INDEX_MAX; loops > 0; loops--) {
3612 		if (!phyint_exists(ipst->ips_ill_index, ipst)) {
3613 			/* found unused index - use it */
3614 			*indexp = ipst->ips_ill_index;
3615 			return (B_TRUE);
3616 		}
3617 
3618 		ipst->ips_ill_index++;
3619 		if (ipst->ips_ill_index > IF_INDEX_MAX)
3620 			ipst->ips_ill_index = 1;
3621 	}
3622 
3623 	/*
3624 	 * all interface indicies are inuse.
3625 	 */
3626 	return (B_FALSE);
3627 }
3628 
3629 /*
3630  * Assign a unique interface index for the phyint.
3631  */
3632 static boolean_t
3633 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3634 {
3635 	ASSERT(phyi->phyint_ifindex == 0);
3636 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3637 }
3638 
3639 /*
3640  * Initialize the flags on `phyi' as per the provided mactype.
3641  */
3642 static void
3643 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3644 {
3645 	uint64_t flags = 0;
3646 
3647 	/*
3648 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3649 	 * we always presume the underlying hardware is working and set
3650 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3651 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3652 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3653 	 */
3654 	if (mactype == SUNW_DL_IPMP)
3655 		flags |= PHYI_FAILED;
3656 	else
3657 		flags |= PHYI_RUNNING;
3658 
3659 	switch (mactype) {
3660 	case SUNW_DL_VNI:
3661 		flags |= PHYI_VIRTUAL;
3662 		break;
3663 	case SUNW_DL_IPMP:
3664 		flags |= PHYI_IPMP;
3665 		break;
3666 	case DL_LOOP:
3667 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3668 		break;
3669 	}
3670 
3671 	mutex_enter(&phyi->phyint_lock);
3672 	phyi->phyint_flags |= flags;
3673 	mutex_exit(&phyi->phyint_lock);
3674 }
3675 
3676 /*
3677  * Return a pointer to the ill which matches the supplied name.  Note that
3678  * the ill name length includes the null termination character.  (May be
3679  * called as writer.)
3680  * If do_alloc and the interface is "lo0" it will be automatically created.
3681  * Cannot bump up reference on condemned ills. So dup detect can't be done
3682  * using this func.
3683  */
3684 ill_t *
3685 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3686     boolean_t *did_alloc, ip_stack_t *ipst)
3687 {
3688 	ill_t	*ill;
3689 	ipif_t	*ipif;
3690 	ipsq_t	*ipsq;
3691 	kstat_named_t	*kn;
3692 	boolean_t isloopback;
3693 	in6_addr_t ov6addr;
3694 
3695 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3696 
3697 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3698 	ill = ill_find_by_name(name, isv6, ipst);
3699 	rw_exit(&ipst->ips_ill_g_lock);
3700 	if (ill != NULL)
3701 		return (ill);
3702 
3703 	/*
3704 	 * Couldn't find it.  Does this happen to be a lookup for the
3705 	 * loopback device and are we allowed to allocate it?
3706 	 */
3707 	if (!isloopback || !do_alloc)
3708 		return (NULL);
3709 
3710 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3711 	ill = ill_find_by_name(name, isv6, ipst);
3712 	if (ill != NULL) {
3713 		rw_exit(&ipst->ips_ill_g_lock);
3714 		return (ill);
3715 	}
3716 
3717 	/* Create the loopback device on demand */
3718 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3719 	    sizeof (ipif_loopback_name), BPRI_MED));
3720 	if (ill == NULL)
3721 		goto done;
3722 
3723 	bzero(ill, sizeof (*ill));
3724 	ill->ill_ipst = ipst;
3725 	netstack_hold(ipst->ips_netstack);
3726 	/*
3727 	 * For exclusive stacks we set the zoneid to zero
3728 	 * to make IP operate as if in the global zone.
3729 	 */
3730 	ill->ill_zoneid = GLOBAL_ZONEID;
3731 
3732 	if (ill_init_common(ill, NULL, isv6, B_TRUE, B_FALSE) != 0)
3733 		goto done;
3734 
3735 	if (!ill_allocate_mibs(ill))
3736 		goto done;
3737 
3738 	ill->ill_current_frag = ill->ill_max_frag;
3739 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3740 	ill->ill_mc_mtu = ill->ill_mtu;
3741 	/*
3742 	 * ipif_loopback_name can't be pointed at directly because its used
3743 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3744 	 * from the glist, ill_glist_delete() sets the first character of
3745 	 * ill_name to '\0'.
3746 	 */
3747 	ill->ill_name = (char *)ill + sizeof (*ill);
3748 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3749 	ill->ill_name_length = sizeof (ipif_loopback_name);
3750 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3751 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3752 
3753 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3754 	if (ipif == NULL)
3755 		goto done;
3756 
3757 	ill->ill_flags = ILLF_MULTICAST;
3758 
3759 	ov6addr = ipif->ipif_v6lcl_addr;
3760 	/* Set up default loopback address and mask. */
3761 	if (!isv6) {
3762 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3763 
3764 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3765 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3766 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3767 		    ipif->ipif_v6subnet);
3768 		ill->ill_flags |= ILLF_IPV4;
3769 	} else {
3770 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3771 		ipif->ipif_v6net_mask = ipv6_all_ones;
3772 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3773 		    ipif->ipif_v6subnet);
3774 		ill->ill_flags |= ILLF_IPV6;
3775 	}
3776 
3777 	/*
3778 	 * Chain us in at the end of the ill list. hold the ill
3779 	 * before we make it globally visible. 1 for the lookup.
3780 	 */
3781 	ill_refhold(ill);
3782 
3783 	ipsq = ill->ill_phyint->phyint_ipsq;
3784 
3785 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3786 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3787 
3788 	/* Let SCTP know so that it can add this to its list */
3789 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3790 
3791 	/*
3792 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3793 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3794 	 * requires to be after ill_glist_insert() since we need the
3795 	 * ill_index set. Pass on ipv6_loopback as the old address.
3796 	 */
3797 	sctp_update_ipif_addr(ipif, ov6addr);
3798 
3799 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3800 
3801 	/*
3802 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3803 	 * If so, free our original one.
3804 	 */
3805 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3806 		ipsq_delete(ipsq);
3807 
3808 	if (ipst->ips_loopback_ksp == NULL) {
3809 		/* Export loopback interface statistics */
3810 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3811 		    ipif_loopback_name, "net",
3812 		    KSTAT_TYPE_NAMED, 2, 0,
3813 		    ipst->ips_netstack->netstack_stackid);
3814 		if (ipst->ips_loopback_ksp != NULL) {
3815 			ipst->ips_loopback_ksp->ks_update =
3816 			    loopback_kstat_update;
3817 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3818 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3819 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3820 			ipst->ips_loopback_ksp->ks_private =
3821 			    (void *)(uintptr_t)ipst->ips_netstack->
3822 			    netstack_stackid;
3823 			kstat_install(ipst->ips_loopback_ksp);
3824 		}
3825 	}
3826 
3827 	*did_alloc = B_TRUE;
3828 	rw_exit(&ipst->ips_ill_g_lock);
3829 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3830 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3831 	return (ill);
3832 done:
3833 	if (ill != NULL) {
3834 		if (ill->ill_phyint != NULL) {
3835 			ipsq = ill->ill_phyint->phyint_ipsq;
3836 			if (ipsq != NULL) {
3837 				ipsq->ipsq_phyint = NULL;
3838 				ipsq_delete(ipsq);
3839 			}
3840 			mi_free(ill->ill_phyint);
3841 		}
3842 		ill_free_mib(ill);
3843 		if (ill->ill_ipst != NULL)
3844 			netstack_rele(ill->ill_ipst->ips_netstack);
3845 		mi_free(ill);
3846 	}
3847 	rw_exit(&ipst->ips_ill_g_lock);
3848 	return (NULL);
3849 }
3850 
3851 /*
3852  * For IPP calls - use the ip_stack_t for global stack.
3853  */
3854 ill_t *
3855 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3856 {
3857 	ip_stack_t	*ipst;
3858 	ill_t		*ill;
3859 	netstack_t	*ns;
3860 
3861 	ns = netstack_find_by_stackid(GLOBAL_NETSTACKID);
3862 
3863 	if ((ipst = ns->netstack_ip) == NULL) {
3864 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3865 		netstack_rele(ns);
3866 		return (NULL);
3867 	}
3868 
3869 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3870 	netstack_rele(ns);
3871 	return (ill);
3872 }
3873 
3874 /*
3875  * Return a pointer to the ill which matches the index and IP version type.
3876  */
3877 ill_t *
3878 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3879 {
3880 	ill_t	*ill;
3881 	phyint_t *phyi;
3882 
3883 	/*
3884 	 * Indexes are stored in the phyint - a common structure
3885 	 * to both IPv4 and IPv6.
3886 	 */
3887 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3888 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3889 	    (void *) &index, NULL);
3890 	if (phyi != NULL) {
3891 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3892 		if (ill != NULL) {
3893 			mutex_enter(&ill->ill_lock);
3894 			if (!ILL_IS_CONDEMNED(ill)) {
3895 				ill_refhold_locked(ill);
3896 				mutex_exit(&ill->ill_lock);
3897 				rw_exit(&ipst->ips_ill_g_lock);
3898 				return (ill);
3899 			}
3900 			mutex_exit(&ill->ill_lock);
3901 		}
3902 	}
3903 	rw_exit(&ipst->ips_ill_g_lock);
3904 	return (NULL);
3905 }
3906 
3907 /*
3908  * Verify whether or not an interface index is valid for the specified zoneid
3909  * to transmit packets.
3910  * It can be zero (meaning "reset") or an interface index assigned
3911  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3912  */
3913 boolean_t
3914 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3915     ip_stack_t *ipst)
3916 {
3917 	ill_t		*ill;
3918 
3919 	if (ifindex == 0)
3920 		return (B_TRUE);
3921 
3922 	ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3923 	if (ill == NULL)
3924 		return (B_FALSE);
3925 	if (IS_VNI(ill)) {
3926 		ill_refrele(ill);
3927 		return (B_FALSE);
3928 	}
3929 	ill_refrele(ill);
3930 	return (B_TRUE);
3931 }
3932 
3933 /*
3934  * Return the ifindex next in sequence after the passed in ifindex.
3935  * If there is no next ifindex for the given protocol, return 0.
3936  */
3937 uint_t
3938 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3939 {
3940 	phyint_t *phyi;
3941 	phyint_t *phyi_initial;
3942 	uint_t   ifindex;
3943 
3944 	phyi_initial = NULL;
3945 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3946 
3947 	if (index == 0) {
3948 		phyi = avl_first(
3949 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3950 	} else {
3951 		phyi = phyi_initial = avl_find(
3952 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3953 		    (void *) &index, NULL);
3954 	}
3955 
3956 	for (; phyi != NULL;
3957 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3958 	    phyi, AVL_AFTER)) {
3959 		/*
3960 		 * If we're not returning the first interface in the tree
3961 		 * and we still haven't moved past the phyint_t that
3962 		 * corresponds to index, avl_walk needs to be called again
3963 		 */
3964 		if (!((index != 0) && (phyi == phyi_initial))) {
3965 			if (isv6) {
3966 				if ((phyi->phyint_illv6) &&
3967 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3968 				    (phyi->phyint_illv6->ill_isv6 == 1))
3969 					break;
3970 			} else {
3971 				if ((phyi->phyint_illv4) &&
3972 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3973 				    (phyi->phyint_illv4->ill_isv6 == 0))
3974 					break;
3975 			}
3976 		}
3977 	}
3978 
3979 	rw_exit(&ipst->ips_ill_g_lock);
3980 
3981 	if (phyi != NULL)
3982 		ifindex = phyi->phyint_ifindex;
3983 	else
3984 		ifindex = 0;
3985 
3986 	return (ifindex);
3987 }
3988 
3989 /*
3990  * Return the ifindex for the named interface.
3991  * If there is no next ifindex for the interface, return 0.
3992  */
3993 uint_t
3994 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3995 {
3996 	phyint_t	*phyi;
3997 	avl_index_t	where = 0;
3998 	uint_t		ifindex;
3999 
4000 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4001 
4002 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4003 	    name, &where)) == NULL) {
4004 		rw_exit(&ipst->ips_ill_g_lock);
4005 		return (0);
4006 	}
4007 
4008 	ifindex = phyi->phyint_ifindex;
4009 
4010 	rw_exit(&ipst->ips_ill_g_lock);
4011 
4012 	return (ifindex);
4013 }
4014 
4015 /*
4016  * Return the ifindex to be used by upper layer protocols for instance
4017  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4018  */
4019 uint_t
4020 ill_get_upper_ifindex(const ill_t *ill)
4021 {
4022 	if (IS_UNDER_IPMP(ill))
4023 		return (ipmp_ill_get_ipmp_ifindex(ill));
4024 	else
4025 		return (ill->ill_phyint->phyint_ifindex);
4026 }
4027 
4028 
4029 /*
4030  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4031  * that gives a running thread a reference to the ill. This reference must be
4032  * released by the thread when it is done accessing the ill and related
4033  * objects. ill_refcnt can not be used to account for static references
4034  * such as other structures pointing to an ill. Callers must generally
4035  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4036  * or be sure that the ill is not being deleted or changing state before
4037  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4038  * ill won't change any of its critical state such as address, netmask etc.
4039  */
4040 void
4041 ill_refhold(ill_t *ill)
4042 {
4043 	mutex_enter(&ill->ill_lock);
4044 	ill->ill_refcnt++;
4045 	ILL_TRACE_REF(ill);
4046 	mutex_exit(&ill->ill_lock);
4047 }
4048 
4049 void
4050 ill_refhold_locked(ill_t *ill)
4051 {
4052 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4053 	ill->ill_refcnt++;
4054 	ILL_TRACE_REF(ill);
4055 }
4056 
4057 /* Returns true if we managed to get a refhold */
4058 boolean_t
4059 ill_check_and_refhold(ill_t *ill)
4060 {
4061 	mutex_enter(&ill->ill_lock);
4062 	if (!ILL_IS_CONDEMNED(ill)) {
4063 		ill_refhold_locked(ill);
4064 		mutex_exit(&ill->ill_lock);
4065 		return (B_TRUE);
4066 	}
4067 	mutex_exit(&ill->ill_lock);
4068 	return (B_FALSE);
4069 }
4070 
4071 /*
4072  * Must not be called while holding any locks. Otherwise if this is
4073  * the last reference to be released, there is a chance of recursive mutex
4074  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4075  * to restart an ioctl.
4076  */
4077 void
4078 ill_refrele(ill_t *ill)
4079 {
4080 	mutex_enter(&ill->ill_lock);
4081 	ASSERT(ill->ill_refcnt != 0);
4082 	ill->ill_refcnt--;
4083 	ILL_UNTRACE_REF(ill);
4084 	if (ill->ill_refcnt != 0) {
4085 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4086 		mutex_exit(&ill->ill_lock);
4087 		return;
4088 	}
4089 
4090 	/* Drops the ill_lock */
4091 	ipif_ill_refrele_tail(ill);
4092 }
4093 
4094 /*
4095  * Obtain a weak reference count on the ill. This reference ensures the
4096  * ill won't be freed, but the ill may change any of its critical state
4097  * such as netmask, address etc. Returns an error if the ill has started
4098  * closing.
4099  */
4100 boolean_t
4101 ill_waiter_inc(ill_t *ill)
4102 {
4103 	mutex_enter(&ill->ill_lock);
4104 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4105 		mutex_exit(&ill->ill_lock);
4106 		return (B_FALSE);
4107 	}
4108 	ill->ill_waiters++;
4109 	mutex_exit(&ill->ill_lock);
4110 	return (B_TRUE);
4111 }
4112 
4113 void
4114 ill_waiter_dcr(ill_t *ill)
4115 {
4116 	mutex_enter(&ill->ill_lock);
4117 	ill->ill_waiters--;
4118 	if (ill->ill_waiters == 0)
4119 		cv_broadcast(&ill->ill_cv);
4120 	mutex_exit(&ill->ill_lock);
4121 }
4122 
4123 /*
4124  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4125  * driver.  We construct best guess defaults for lower level information that
4126  * we need.  If an interface is brought up without injection of any overriding
4127  * information from outside, we have to be ready to go with these defaults.
4128  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4129  * we primarely want the dl_provider_style.
4130  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4131  * at which point we assume the other part of the information is valid.
4132  */
4133 void
4134 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4135 {
4136 	uchar_t		*brdcst_addr;
4137 	uint_t		brdcst_addr_length, phys_addr_length;
4138 	t_scalar_t	sap_length;
4139 	dl_info_ack_t	*dlia;
4140 	ip_m_t		*ipm;
4141 	dl_qos_cl_sel1_t *sel1;
4142 	int		min_mtu;
4143 
4144 	ASSERT(IAM_WRITER_ILL(ill));
4145 
4146 	/*
4147 	 * Till the ill is fully up  the ill is not globally visible.
4148 	 * So no need for a lock.
4149 	 */
4150 	dlia = (dl_info_ack_t *)mp->b_rptr;
4151 	ill->ill_mactype = dlia->dl_mac_type;
4152 
4153 	ipm = ip_m_lookup(dlia->dl_mac_type);
4154 	if (ipm == NULL) {
4155 		ipm = ip_m_lookup(DL_OTHER);
4156 		ASSERT(ipm != NULL);
4157 	}
4158 	ill->ill_media = ipm;
4159 
4160 	/*
4161 	 * When the new DLPI stuff is ready we'll pull lengths
4162 	 * from dlia.
4163 	 */
4164 	if (dlia->dl_version == DL_VERSION_2) {
4165 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4166 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4167 		    brdcst_addr_length);
4168 		if (brdcst_addr == NULL) {
4169 			brdcst_addr_length = 0;
4170 		}
4171 		sap_length = dlia->dl_sap_length;
4172 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4173 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4174 		    brdcst_addr_length, sap_length, phys_addr_length));
4175 	} else {
4176 		brdcst_addr_length = 6;
4177 		brdcst_addr = ip_six_byte_all_ones;
4178 		sap_length = -2;
4179 		phys_addr_length = brdcst_addr_length;
4180 	}
4181 
4182 	ill->ill_bcast_addr_length = brdcst_addr_length;
4183 	ill->ill_phys_addr_length = phys_addr_length;
4184 	ill->ill_sap_length = sap_length;
4185 
4186 	/*
4187 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4188 	 * but we must ensure a minimum IP MTU is used since other bits of
4189 	 * IP will fly apart otherwise.
4190 	 */
4191 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4192 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4193 	ill->ill_current_frag = ill->ill_max_frag;
4194 	ill->ill_mtu = ill->ill_max_frag;
4195 	ill->ill_mc_mtu = ill->ill_mtu;	/* Overridden by DL_NOTE_SDU_SIZE2 */
4196 
4197 	ill->ill_type = ipm->ip_m_type;
4198 
4199 	if (!ill->ill_dlpi_style_set) {
4200 		if (dlia->dl_provider_style == DL_STYLE2)
4201 			ill->ill_needs_attach = 1;
4202 
4203 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4204 
4205 		/*
4206 		 * Allocate the first ipif on this ill.  We don't delay it
4207 		 * further as ioctl handling assumes at least one ipif exists.
4208 		 *
4209 		 * At this point we don't know whether the ill is v4 or v6.
4210 		 * We will know this whan the SIOCSLIFNAME happens and
4211 		 * the correct value for ill_isv6 will be assigned in
4212 		 * ipif_set_values(). We need to hold the ill lock and
4213 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4214 		 * the wakeup.
4215 		 */
4216 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4217 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4218 		mutex_enter(&ill->ill_lock);
4219 		ASSERT(ill->ill_dlpi_style_set == 0);
4220 		ill->ill_dlpi_style_set = 1;
4221 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4222 		cv_broadcast(&ill->ill_cv);
4223 		mutex_exit(&ill->ill_lock);
4224 		freemsg(mp);
4225 		return;
4226 	}
4227 	ASSERT(ill->ill_ipif != NULL);
4228 	/*
4229 	 * We know whether it is IPv4 or IPv6 now, as this is the
4230 	 * second DL_INFO_ACK we are recieving in response to the
4231 	 * DL_INFO_REQ sent in ipif_set_values.
4232 	 */
4233 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4234 	/*
4235 	 * Clear all the flags that were set based on ill_bcast_addr_length
4236 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4237 	 * changed now and we need to re-evaluate.
4238 	 */
4239 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4240 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4241 
4242 	/*
4243 	 * Free ill_bcast_mp as things could have changed now.
4244 	 *
4245 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4246 	 * with no underlying interfaces (and thus an unknown broadcast
4247 	 * address length), but we enforce that an interface is broadcast-
4248 	 * capable as part of allowing it to join a group.
4249 	 */
4250 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4251 		if (ill->ill_bcast_mp != NULL)
4252 			freemsg(ill->ill_bcast_mp);
4253 		ill->ill_net_type = IRE_IF_NORESOLVER;
4254 
4255 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4256 		    ill->ill_phys_addr_length,
4257 		    ill->ill_sap,
4258 		    ill->ill_sap_length);
4259 
4260 		if (ill->ill_isv6)
4261 			/*
4262 			 * Note: xresolv interfaces will eventually need NOARP
4263 			 * set here as well, but that will require those
4264 			 * external resolvers to have some knowledge of
4265 			 * that flag and act appropriately. Not to be changed
4266 			 * at present.
4267 			 */
4268 			ill->ill_flags |= ILLF_NONUD;
4269 		else
4270 			ill->ill_flags |= ILLF_NOARP;
4271 
4272 		if (ill->ill_mactype == SUNW_DL_VNI) {
4273 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4274 		} else if (ill->ill_phys_addr_length == 0 ||
4275 		    ill->ill_mactype == DL_IPV4 ||
4276 		    ill->ill_mactype == DL_IPV6) {
4277 			/*
4278 			 * The underying link is point-to-point, so mark the
4279 			 * interface as such.  We can do IP multicast over
4280 			 * such a link since it transmits all network-layer
4281 			 * packets to the remote side the same way.
4282 			 */
4283 			ill->ill_flags |= ILLF_MULTICAST;
4284 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4285 		}
4286 	} else {
4287 		ill->ill_net_type = IRE_IF_RESOLVER;
4288 		if (ill->ill_bcast_mp != NULL)
4289 			freemsg(ill->ill_bcast_mp);
4290 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4291 		    ill->ill_bcast_addr_length, ill->ill_sap,
4292 		    ill->ill_sap_length);
4293 		/*
4294 		 * Later detect lack of DLPI driver multicast
4295 		 * capability by catching DL_ENABMULTI errors in
4296 		 * ip_rput_dlpi.
4297 		 */
4298 		ill->ill_flags |= ILLF_MULTICAST;
4299 		if (!ill->ill_isv6)
4300 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4301 	}
4302 
4303 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4304 	if (ill->ill_mactype == SUNW_DL_IPMP)
4305 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4306 
4307 	/* By default an interface does not support any CoS marking */
4308 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4309 
4310 	/*
4311 	 * If we get QoS information in DL_INFO_ACK, the device supports
4312 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4313 	 */
4314 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4315 	    dlia->dl_qos_length);
4316 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4317 		ill->ill_flags |= ILLF_COS_ENABLED;
4318 	}
4319 
4320 	/* Clear any previous error indication. */
4321 	ill->ill_error = 0;
4322 	freemsg(mp);
4323 }
4324 
4325 /*
4326  * Perform various checks to verify that an address would make sense as a
4327  * local, remote, or subnet interface address.
4328  */
4329 static boolean_t
4330 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4331 {
4332 	ipaddr_t	net_mask;
4333 
4334 	/*
4335 	 * Don't allow all zeroes, or all ones, but allow
4336 	 * all ones netmask.
4337 	 */
4338 	if ((net_mask = ip_net_mask(addr)) == 0)
4339 		return (B_FALSE);
4340 	/* A given netmask overrides the "guess" netmask */
4341 	if (subnet_mask != 0)
4342 		net_mask = subnet_mask;
4343 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4344 	    (addr == (addr | ~net_mask)))) {
4345 		return (B_FALSE);
4346 	}
4347 
4348 	/*
4349 	 * Even if the netmask is all ones, we do not allow address to be
4350 	 * 255.255.255.255
4351 	 */
4352 	if (addr == INADDR_BROADCAST)
4353 		return (B_FALSE);
4354 
4355 	if (CLASSD(addr))
4356 		return (B_FALSE);
4357 
4358 	return (B_TRUE);
4359 }
4360 
4361 #define	V6_IPIF_LINKLOCAL(p)	\
4362 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4363 
4364 /*
4365  * Compare two given ipifs and check if the second one is better than
4366  * the first one using the order of preference (not taking deprecated
4367  * into acount) specified in ipif_lookup_multicast().
4368  */
4369 static boolean_t
4370 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4371 {
4372 	/* Check the least preferred first. */
4373 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4374 		/* If both ipifs are the same, use the first one. */
4375 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4376 			return (B_FALSE);
4377 		else
4378 			return (B_TRUE);
4379 	}
4380 
4381 	/* For IPv6, check for link local address. */
4382 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4383 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4384 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4385 			/* The second one is equal or less preferred. */
4386 			return (B_FALSE);
4387 		} else {
4388 			return (B_TRUE);
4389 		}
4390 	}
4391 
4392 	/* Then check for point to point interface. */
4393 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4394 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4395 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4396 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4397 			return (B_FALSE);
4398 		} else {
4399 			return (B_TRUE);
4400 		}
4401 	}
4402 
4403 	/* old_ipif is a normal interface, so no need to use the new one. */
4404 	return (B_FALSE);
4405 }
4406 
4407 /*
4408  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4409  * The ipif must be up, and its ill must multicast-capable, not
4410  * condemned, not an underlying interface in an IPMP group, and
4411  * not a VNI interface.  Order of preference:
4412  *
4413  *	1a. normal
4414  *	1b. normal, but deprecated
4415  *	2a. point to point
4416  *	2b. point to point, but deprecated
4417  *	3a. link local
4418  *	3b. link local, but deprecated
4419  *	4. loopback.
4420  */
4421 static ipif_t *
4422 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4423 {
4424 	ill_t			*ill;
4425 	ill_walk_context_t	ctx;
4426 	ipif_t			*ipif;
4427 	ipif_t			*saved_ipif = NULL;
4428 	ipif_t			*dep_ipif = NULL;
4429 
4430 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4431 	if (isv6)
4432 		ill = ILL_START_WALK_V6(&ctx, ipst);
4433 	else
4434 		ill = ILL_START_WALK_V4(&ctx, ipst);
4435 
4436 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4437 		mutex_enter(&ill->ill_lock);
4438 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4439 		    ILL_IS_CONDEMNED(ill) ||
4440 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4441 			mutex_exit(&ill->ill_lock);
4442 			continue;
4443 		}
4444 		for (ipif = ill->ill_ipif; ipif != NULL;
4445 		    ipif = ipif->ipif_next) {
4446 			if (zoneid != ipif->ipif_zoneid &&
4447 			    zoneid != ALL_ZONES &&
4448 			    ipif->ipif_zoneid != ALL_ZONES) {
4449 				continue;
4450 			}
4451 			if (!(ipif->ipif_flags & IPIF_UP) ||
4452 			    IPIF_IS_CONDEMNED(ipif)) {
4453 				continue;
4454 			}
4455 
4456 			/*
4457 			 * Found one candidate.  If it is deprecated,
4458 			 * remember it in dep_ipif.  If it is not deprecated,
4459 			 * remember it in saved_ipif.
4460 			 */
4461 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4462 				if (dep_ipif == NULL) {
4463 					dep_ipif = ipif;
4464 				} else if (ipif_comp_multi(dep_ipif, ipif,
4465 				    isv6)) {
4466 					/*
4467 					 * If the previous dep_ipif does not
4468 					 * belong to the same ill, we've done
4469 					 * a ipif_refhold() on it.  So we need
4470 					 * to release it.
4471 					 */
4472 					if (dep_ipif->ipif_ill != ill)
4473 						ipif_refrele(dep_ipif);
4474 					dep_ipif = ipif;
4475 				}
4476 				continue;
4477 			}
4478 			if (saved_ipif == NULL) {
4479 				saved_ipif = ipif;
4480 			} else {
4481 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4482 					if (saved_ipif->ipif_ill != ill)
4483 						ipif_refrele(saved_ipif);
4484 					saved_ipif = ipif;
4485 				}
4486 			}
4487 		}
4488 		/*
4489 		 * Before going to the next ill, do a ipif_refhold() on the
4490 		 * saved ones.
4491 		 */
4492 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4493 			ipif_refhold_locked(saved_ipif);
4494 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4495 			ipif_refhold_locked(dep_ipif);
4496 		mutex_exit(&ill->ill_lock);
4497 	}
4498 	rw_exit(&ipst->ips_ill_g_lock);
4499 
4500 	/*
4501 	 * If we have only the saved_ipif, return it.  But if we have both
4502 	 * saved_ipif and dep_ipif, check to see which one is better.
4503 	 */
4504 	if (saved_ipif != NULL) {
4505 		if (dep_ipif != NULL) {
4506 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4507 				ipif_refrele(saved_ipif);
4508 				return (dep_ipif);
4509 			} else {
4510 				ipif_refrele(dep_ipif);
4511 				return (saved_ipif);
4512 			}
4513 		}
4514 		return (saved_ipif);
4515 	} else {
4516 		return (dep_ipif);
4517 	}
4518 }
4519 
4520 ill_t *
4521 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4522 {
4523 	ipif_t *ipif;
4524 	ill_t *ill;
4525 
4526 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4527 	if (ipif == NULL)
4528 		return (NULL);
4529 
4530 	ill = ipif->ipif_ill;
4531 	ill_refhold(ill);
4532 	ipif_refrele(ipif);
4533 	return (ill);
4534 }
4535 
4536 /*
4537  * This function is called when an application does not specify an interface
4538  * to be used for multicast traffic (joining a group/sending data).  It
4539  * calls ire_lookup_multi() to look for an interface route for the
4540  * specified multicast group.  Doing this allows the administrator to add
4541  * prefix routes for multicast to indicate which interface to be used for
4542  * multicast traffic in the above scenario.  The route could be for all
4543  * multicast (224.0/4), for a single multicast group (a /32 route) or
4544  * anything in between.  If there is no such multicast route, we just find
4545  * any multicast capable interface and return it.  The returned ipif
4546  * is refhold'ed.
4547  *
4548  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4549  * unicast table. This is used by CGTP.
4550  */
4551 ill_t *
4552 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4553     boolean_t *multirtp, ipaddr_t *setsrcp)
4554 {
4555 	ill_t			*ill;
4556 
4557 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4558 	if (ill != NULL)
4559 		return (ill);
4560 
4561 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4562 }
4563 
4564 /*
4565  * Look for an ipif with the specified interface address and destination.
4566  * The destination address is used only for matching point-to-point interfaces.
4567  */
4568 ipif_t *
4569 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4570 {
4571 	ipif_t	*ipif;
4572 	ill_t	*ill;
4573 	ill_walk_context_t ctx;
4574 
4575 	/*
4576 	 * First match all the point-to-point interfaces
4577 	 * before looking at non-point-to-point interfaces.
4578 	 * This is done to avoid returning non-point-to-point
4579 	 * ipif instead of unnumbered point-to-point ipif.
4580 	 */
4581 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4582 	ill = ILL_START_WALK_V4(&ctx, ipst);
4583 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4584 		mutex_enter(&ill->ill_lock);
4585 		for (ipif = ill->ill_ipif; ipif != NULL;
4586 		    ipif = ipif->ipif_next) {
4587 			/* Allow the ipif to be down */
4588 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4589 			    (ipif->ipif_lcl_addr == if_addr) &&
4590 			    (ipif->ipif_pp_dst_addr == dst)) {
4591 				if (!IPIF_IS_CONDEMNED(ipif)) {
4592 					ipif_refhold_locked(ipif);
4593 					mutex_exit(&ill->ill_lock);
4594 					rw_exit(&ipst->ips_ill_g_lock);
4595 					return (ipif);
4596 				}
4597 			}
4598 		}
4599 		mutex_exit(&ill->ill_lock);
4600 	}
4601 	rw_exit(&ipst->ips_ill_g_lock);
4602 
4603 	/* lookup the ipif based on interface address */
4604 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4605 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4606 	return (ipif);
4607 }
4608 
4609 /*
4610  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4611  */
4612 static ipif_t *
4613 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4614     zoneid_t zoneid, ip_stack_t *ipst)
4615 {
4616 	ipif_t  *ipif;
4617 	ill_t   *ill;
4618 	boolean_t ptp = B_FALSE;
4619 	ill_walk_context_t	ctx;
4620 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4621 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4622 
4623 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4624 	/*
4625 	 * Repeat twice, first based on local addresses and
4626 	 * next time for pointopoint.
4627 	 */
4628 repeat:
4629 	ill = ILL_START_WALK_V4(&ctx, ipst);
4630 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4631 		if (match_ill != NULL && ill != match_ill &&
4632 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4633 			continue;
4634 		}
4635 		mutex_enter(&ill->ill_lock);
4636 		for (ipif = ill->ill_ipif; ipif != NULL;
4637 		    ipif = ipif->ipif_next) {
4638 			if (zoneid != ALL_ZONES &&
4639 			    zoneid != ipif->ipif_zoneid &&
4640 			    ipif->ipif_zoneid != ALL_ZONES)
4641 				continue;
4642 
4643 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4644 				continue;
4645 
4646 			/* Allow the ipif to be down */
4647 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4648 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4649 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4650 			    (ipif->ipif_pp_dst_addr == addr))) {
4651 				if (!IPIF_IS_CONDEMNED(ipif)) {
4652 					ipif_refhold_locked(ipif);
4653 					mutex_exit(&ill->ill_lock);
4654 					rw_exit(&ipst->ips_ill_g_lock);
4655 					return (ipif);
4656 				}
4657 			}
4658 		}
4659 		mutex_exit(&ill->ill_lock);
4660 	}
4661 
4662 	/* If we already did the ptp case, then we are done */
4663 	if (ptp) {
4664 		rw_exit(&ipst->ips_ill_g_lock);
4665 		return (NULL);
4666 	}
4667 	ptp = B_TRUE;
4668 	goto repeat;
4669 }
4670 
4671 /*
4672  * Lookup an ipif with the specified address.  For point-to-point links we
4673  * look for matches on either the destination address or the local address,
4674  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4675  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4676  * (or illgrp if `match_ill' is in an IPMP group).
4677  */
4678 ipif_t *
4679 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4680     ip_stack_t *ipst)
4681 {
4682 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4683 	    zoneid, ipst));
4684 }
4685 
4686 /*
4687  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4688  * except that we will only return an address if it is not marked as
4689  * IPIF_DUPLICATE
4690  */
4691 ipif_t *
4692 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4693     ip_stack_t *ipst)
4694 {
4695 	return (ipif_lookup_addr_common(addr, match_ill,
4696 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4697 	    zoneid, ipst));
4698 }
4699 
4700 /*
4701  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4702  * `match_ill' across the IPMP group.  This function is only needed in some
4703  * corner-cases; almost everything should use ipif_lookup_addr().
4704  */
4705 ipif_t *
4706 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4707 {
4708 	ASSERT(match_ill != NULL);
4709 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4710 	    ipst));
4711 }
4712 
4713 /*
4714  * Look for an ipif with the specified address. For point-point links
4715  * we look for matches on either the destination address and the local
4716  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4717  * is set.
4718  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4719  * ill (or illgrp if `match_ill' is in an IPMP group).
4720  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4721  */
4722 zoneid_t
4723 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4724 {
4725 	zoneid_t zoneid;
4726 	ipif_t  *ipif;
4727 	ill_t   *ill;
4728 	boolean_t ptp = B_FALSE;
4729 	ill_walk_context_t	ctx;
4730 
4731 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4732 	/*
4733 	 * Repeat twice, first based on local addresses and
4734 	 * next time for pointopoint.
4735 	 */
4736 repeat:
4737 	ill = ILL_START_WALK_V4(&ctx, ipst);
4738 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4739 		if (match_ill != NULL && ill != match_ill &&
4740 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4741 			continue;
4742 		}
4743 		mutex_enter(&ill->ill_lock);
4744 		for (ipif = ill->ill_ipif; ipif != NULL;
4745 		    ipif = ipif->ipif_next) {
4746 			/* Allow the ipif to be down */
4747 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4748 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4749 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4750 			    (ipif->ipif_pp_dst_addr == addr)) &&
4751 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4752 				zoneid = ipif->ipif_zoneid;
4753 				mutex_exit(&ill->ill_lock);
4754 				rw_exit(&ipst->ips_ill_g_lock);
4755 				/*
4756 				 * If ipif_zoneid was ALL_ZONES then we have
4757 				 * a trusted extensions shared IP address.
4758 				 * In that case GLOBAL_ZONEID works to send.
4759 				 */
4760 				if (zoneid == ALL_ZONES)
4761 					zoneid = GLOBAL_ZONEID;
4762 				return (zoneid);
4763 			}
4764 		}
4765 		mutex_exit(&ill->ill_lock);
4766 	}
4767 
4768 	/* If we already did the ptp case, then we are done */
4769 	if (ptp) {
4770 		rw_exit(&ipst->ips_ill_g_lock);
4771 		return (ALL_ZONES);
4772 	}
4773 	ptp = B_TRUE;
4774 	goto repeat;
4775 }
4776 
4777 /*
4778  * Look for an ipif that matches the specified remote address i.e. the
4779  * ipif that would receive the specified packet.
4780  * First look for directly connected interfaces and then do a recursive
4781  * IRE lookup and pick the first ipif corresponding to the source address in the
4782  * ire.
4783  * Returns: held ipif
4784  *
4785  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4786  */
4787 ipif_t *
4788 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4789 {
4790 	ipif_t	*ipif;
4791 
4792 	ASSERT(!ill->ill_isv6);
4793 
4794 	/*
4795 	 * Someone could be changing this ipif currently or change it
4796 	 * after we return this. Thus  a few packets could use the old
4797 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4798 	 * will atomically be updated or cleaned up with the new value
4799 	 * Thus we don't need a lock to check the flags or other attrs below.
4800 	 */
4801 	mutex_enter(&ill->ill_lock);
4802 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4803 		if (IPIF_IS_CONDEMNED(ipif))
4804 			continue;
4805 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4806 		    ipif->ipif_zoneid != ALL_ZONES)
4807 			continue;
4808 		/* Allow the ipif to be down */
4809 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4810 			if ((ipif->ipif_pp_dst_addr == addr) ||
4811 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4812 			    ipif->ipif_lcl_addr == addr)) {
4813 				ipif_refhold_locked(ipif);
4814 				mutex_exit(&ill->ill_lock);
4815 				return (ipif);
4816 			}
4817 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4818 			ipif_refhold_locked(ipif);
4819 			mutex_exit(&ill->ill_lock);
4820 			return (ipif);
4821 		}
4822 	}
4823 	mutex_exit(&ill->ill_lock);
4824 	/*
4825 	 * For a remote destination it isn't possible to nail down a particular
4826 	 * ipif.
4827 	 */
4828 
4829 	/* Pick the first interface */
4830 	ipif = ipif_get_next_ipif(NULL, ill);
4831 	return (ipif);
4832 }
4833 
4834 /*
4835  * This func does not prevent refcnt from increasing. But if
4836  * the caller has taken steps to that effect, then this func
4837  * can be used to determine whether the ill has become quiescent
4838  */
4839 static boolean_t
4840 ill_is_quiescent(ill_t *ill)
4841 {
4842 	ipif_t	*ipif;
4843 
4844 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4845 
4846 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4847 		if (ipif->ipif_refcnt != 0)
4848 			return (B_FALSE);
4849 	}
4850 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4851 		return (B_FALSE);
4852 	}
4853 	return (B_TRUE);
4854 }
4855 
4856 boolean_t
4857 ill_is_freeable(ill_t *ill)
4858 {
4859 	ipif_t	*ipif;
4860 
4861 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4862 
4863 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4864 		if (ipif->ipif_refcnt != 0) {
4865 			return (B_FALSE);
4866 		}
4867 	}
4868 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4869 		return (B_FALSE);
4870 	}
4871 	return (B_TRUE);
4872 }
4873 
4874 /*
4875  * This func does not prevent refcnt from increasing. But if
4876  * the caller has taken steps to that effect, then this func
4877  * can be used to determine whether the ipif has become quiescent
4878  */
4879 static boolean_t
4880 ipif_is_quiescent(ipif_t *ipif)
4881 {
4882 	ill_t *ill;
4883 
4884 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4885 
4886 	if (ipif->ipif_refcnt != 0)
4887 		return (B_FALSE);
4888 
4889 	ill = ipif->ipif_ill;
4890 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4891 	    ill->ill_logical_down) {
4892 		return (B_TRUE);
4893 	}
4894 
4895 	/* This is the last ipif going down or being deleted on this ill */
4896 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4897 		return (B_FALSE);
4898 	}
4899 
4900 	return (B_TRUE);
4901 }
4902 
4903 /*
4904  * return true if the ipif can be destroyed: the ipif has to be quiescent
4905  * with zero references from ire/ilm to it.
4906  */
4907 static boolean_t
4908 ipif_is_freeable(ipif_t *ipif)
4909 {
4910 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4911 	ASSERT(ipif->ipif_id != 0);
4912 	return (ipif->ipif_refcnt == 0);
4913 }
4914 
4915 /*
4916  * The ipif/ill/ire has been refreled. Do the tail processing.
4917  * Determine if the ipif or ill in question has become quiescent and if so
4918  * wakeup close and/or restart any queued pending ioctl that is waiting
4919  * for the ipif_down (or ill_down)
4920  */
4921 void
4922 ipif_ill_refrele_tail(ill_t *ill)
4923 {
4924 	mblk_t	*mp;
4925 	conn_t	*connp;
4926 	ipsq_t	*ipsq;
4927 	ipxop_t	*ipx;
4928 	ipif_t	*ipif;
4929 	dl_notify_ind_t *dlindp;
4930 
4931 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4932 
4933 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4934 		/* ip_modclose() may be waiting */
4935 		cv_broadcast(&ill->ill_cv);
4936 	}
4937 
4938 	ipsq = ill->ill_phyint->phyint_ipsq;
4939 	mutex_enter(&ipsq->ipsq_lock);
4940 	ipx = ipsq->ipsq_xop;
4941 	mutex_enter(&ipx->ipx_lock);
4942 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4943 		goto unlock;
4944 
4945 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4946 
4947 	ipif = ipx->ipx_pending_ipif;
4948 	if (ipif->ipif_ill != ill)	/* wait is for another ill; bail */
4949 		goto unlock;
4950 
4951 	switch (ipx->ipx_waitfor) {
4952 	case IPIF_DOWN:
4953 		if (!ipif_is_quiescent(ipif))
4954 			goto unlock;
4955 		break;
4956 	case IPIF_FREE:
4957 		if (!ipif_is_freeable(ipif))
4958 			goto unlock;
4959 		break;
4960 	case ILL_DOWN:
4961 		if (!ill_is_quiescent(ill))
4962 			goto unlock;
4963 		break;
4964 	case ILL_FREE:
4965 		/*
4966 		 * ILL_FREE is only for loopback; normal ill teardown waits
4967 		 * synchronously in ip_modclose() without using ipx_waitfor,
4968 		 * handled by the cv_broadcast() at the top of this function.
4969 		 */
4970 		if (!ill_is_freeable(ill))
4971 			goto unlock;
4972 		break;
4973 	default:
4974 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4975 		    (void *)ipsq, ipx->ipx_waitfor);
4976 	}
4977 
4978 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4979 	mutex_exit(&ipx->ipx_lock);
4980 	mp = ipsq_pending_mp_get(ipsq, &connp);
4981 	mutex_exit(&ipsq->ipsq_lock);
4982 	mutex_exit(&ill->ill_lock);
4983 
4984 	ASSERT(mp != NULL);
4985 	/*
4986 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4987 	 * we can only get here when the current operation decides it
4988 	 * it needs to quiesce via ipsq_pending_mp_add().
4989 	 */
4990 	switch (mp->b_datap->db_type) {
4991 	case M_PCPROTO:
4992 	case M_PROTO:
4993 		/*
4994 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4995 		 */
4996 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4997 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4998 
4999 		switch (dlindp->dl_notification) {
5000 		case DL_NOTE_PHYS_ADDR:
5001 			qwriter_ip(ill, ill->ill_rq, mp,
5002 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5003 			return;
5004 		case DL_NOTE_REPLUMB:
5005 			qwriter_ip(ill, ill->ill_rq, mp,
5006 			    ill_replumb_tail, CUR_OP, B_TRUE);
5007 			return;
5008 		default:
5009 			ASSERT(0);
5010 			ill_refrele(ill);
5011 		}
5012 		break;
5013 
5014 	case M_ERROR:
5015 	case M_HANGUP:
5016 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5017 		    B_TRUE);
5018 		return;
5019 
5020 	case M_IOCTL:
5021 	case M_IOCDATA:
5022 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5023 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5024 		return;
5025 
5026 	default:
5027 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5028 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5029 	}
5030 	return;
5031 unlock:
5032 	mutex_exit(&ipsq->ipsq_lock);
5033 	mutex_exit(&ipx->ipx_lock);
5034 	mutex_exit(&ill->ill_lock);
5035 }
5036 
5037 #ifdef DEBUG
5038 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5039 static void
5040 th_trace_rrecord(th_trace_t *th_trace)
5041 {
5042 	tr_buf_t *tr_buf;
5043 	uint_t lastref;
5044 
5045 	lastref = th_trace->th_trace_lastref;
5046 	lastref++;
5047 	if (lastref == TR_BUF_MAX)
5048 		lastref = 0;
5049 	th_trace->th_trace_lastref = lastref;
5050 	tr_buf = &th_trace->th_trbuf[lastref];
5051 	tr_buf->tr_time = ddi_get_lbolt();
5052 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5053 }
5054 
5055 static void
5056 th_trace_free(void *value)
5057 {
5058 	th_trace_t *th_trace = value;
5059 
5060 	ASSERT(th_trace->th_refcnt == 0);
5061 	kmem_free(th_trace, sizeof (*th_trace));
5062 }
5063 
5064 /*
5065  * Find or create the per-thread hash table used to track object references.
5066  * The ipst argument is NULL if we shouldn't allocate.
5067  *
5068  * Accesses per-thread data, so there's no need to lock here.
5069  */
5070 static mod_hash_t *
5071 th_trace_gethash(ip_stack_t *ipst)
5072 {
5073 	th_hash_t *thh;
5074 
5075 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5076 		mod_hash_t *mh;
5077 		char name[256];
5078 		size_t objsize, rshift;
5079 		int retv;
5080 
5081 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5082 			return (NULL);
5083 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5084 		    (void *)curthread);
5085 
5086 		/*
5087 		 * We use mod_hash_create_extended here rather than the more
5088 		 * obvious mod_hash_create_ptrhash because the latter has a
5089 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5090 		 * block.
5091 		 */
5092 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5093 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5094 		rshift = highbit(objsize);
5095 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5096 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5097 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5098 		if (mh == NULL) {
5099 			kmem_free(thh, sizeof (*thh));
5100 			return (NULL);
5101 		}
5102 		thh->thh_hash = mh;
5103 		thh->thh_ipst = ipst;
5104 		/*
5105 		 * We trace ills, ipifs, ires, and nces.  All of these are
5106 		 * per-IP-stack, so the lock on the thread list is as well.
5107 		 */
5108 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5109 		list_insert_tail(&ip_thread_list, thh);
5110 		rw_exit(&ip_thread_rwlock);
5111 		retv = tsd_set(ip_thread_data, thh);
5112 		ASSERT(retv == 0);
5113 	}
5114 	return (thh != NULL ? thh->thh_hash : NULL);
5115 }
5116 
5117 boolean_t
5118 th_trace_ref(const void *obj, ip_stack_t *ipst)
5119 {
5120 	th_trace_t *th_trace;
5121 	mod_hash_t *mh;
5122 	mod_hash_val_t val;
5123 
5124 	if ((mh = th_trace_gethash(ipst)) == NULL)
5125 		return (B_FALSE);
5126 
5127 	/*
5128 	 * Attempt to locate the trace buffer for this obj and thread.
5129 	 * If it does not exist, then allocate a new trace buffer and
5130 	 * insert into the hash.
5131 	 */
5132 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5133 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5134 		if (th_trace == NULL)
5135 			return (B_FALSE);
5136 
5137 		th_trace->th_id = curthread;
5138 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5139 		    (mod_hash_val_t)th_trace) != 0) {
5140 			kmem_free(th_trace, sizeof (th_trace_t));
5141 			return (B_FALSE);
5142 		}
5143 	} else {
5144 		th_trace = (th_trace_t *)val;
5145 	}
5146 
5147 	ASSERT(th_trace->th_refcnt >= 0 &&
5148 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5149 
5150 	th_trace->th_refcnt++;
5151 	th_trace_rrecord(th_trace);
5152 	return (B_TRUE);
5153 }
5154 
5155 /*
5156  * For the purpose of tracing a reference release, we assume that global
5157  * tracing is always on and that the same thread initiated the reference hold
5158  * is releasing.
5159  */
5160 void
5161 th_trace_unref(const void *obj)
5162 {
5163 	int retv;
5164 	mod_hash_t *mh;
5165 	th_trace_t *th_trace;
5166 	mod_hash_val_t val;
5167 
5168 	mh = th_trace_gethash(NULL);
5169 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5170 	ASSERT(retv == 0);
5171 	th_trace = (th_trace_t *)val;
5172 
5173 	ASSERT(th_trace->th_refcnt > 0);
5174 	th_trace->th_refcnt--;
5175 	th_trace_rrecord(th_trace);
5176 }
5177 
5178 /*
5179  * If tracing has been disabled, then we assume that the reference counts are
5180  * now useless, and we clear them out before destroying the entries.
5181  */
5182 void
5183 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5184 {
5185 	th_hash_t	*thh;
5186 	mod_hash_t	*mh;
5187 	mod_hash_val_t	val;
5188 	th_trace_t	*th_trace;
5189 	int		retv;
5190 
5191 	rw_enter(&ip_thread_rwlock, RW_READER);
5192 	for (thh = list_head(&ip_thread_list); thh != NULL;
5193 	    thh = list_next(&ip_thread_list, thh)) {
5194 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5195 		    &val) == 0) {
5196 			th_trace = (th_trace_t *)val;
5197 			if (trace_disable)
5198 				th_trace->th_refcnt = 0;
5199 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5200 			ASSERT(retv == 0);
5201 		}
5202 	}
5203 	rw_exit(&ip_thread_rwlock);
5204 }
5205 
5206 void
5207 ipif_trace_ref(ipif_t *ipif)
5208 {
5209 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5210 
5211 	if (ipif->ipif_trace_disable)
5212 		return;
5213 
5214 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5215 		ipif->ipif_trace_disable = B_TRUE;
5216 		ipif_trace_cleanup(ipif);
5217 	}
5218 }
5219 
5220 void
5221 ipif_untrace_ref(ipif_t *ipif)
5222 {
5223 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5224 
5225 	if (!ipif->ipif_trace_disable)
5226 		th_trace_unref(ipif);
5227 }
5228 
5229 void
5230 ill_trace_ref(ill_t *ill)
5231 {
5232 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5233 
5234 	if (ill->ill_trace_disable)
5235 		return;
5236 
5237 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5238 		ill->ill_trace_disable = B_TRUE;
5239 		ill_trace_cleanup(ill);
5240 	}
5241 }
5242 
5243 void
5244 ill_untrace_ref(ill_t *ill)
5245 {
5246 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5247 
5248 	if (!ill->ill_trace_disable)
5249 		th_trace_unref(ill);
5250 }
5251 
5252 /*
5253  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5254  * failure, ipif_trace_disable is set.
5255  */
5256 static void
5257 ipif_trace_cleanup(const ipif_t *ipif)
5258 {
5259 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5260 }
5261 
5262 /*
5263  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5264  * failure, ill_trace_disable is set.
5265  */
5266 static void
5267 ill_trace_cleanup(const ill_t *ill)
5268 {
5269 	th_trace_cleanup(ill, ill->ill_trace_disable);
5270 }
5271 #endif /* DEBUG */
5272 
5273 void
5274 ipif_refhold_locked(ipif_t *ipif)
5275 {
5276 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5277 	ipif->ipif_refcnt++;
5278 	IPIF_TRACE_REF(ipif);
5279 }
5280 
5281 void
5282 ipif_refhold(ipif_t *ipif)
5283 {
5284 	ill_t	*ill;
5285 
5286 	ill = ipif->ipif_ill;
5287 	mutex_enter(&ill->ill_lock);
5288 	ipif->ipif_refcnt++;
5289 	IPIF_TRACE_REF(ipif);
5290 	mutex_exit(&ill->ill_lock);
5291 }
5292 
5293 /*
5294  * Must not be called while holding any locks. Otherwise if this is
5295  * the last reference to be released there is a chance of recursive mutex
5296  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5297  * to restart an ioctl.
5298  */
5299 void
5300 ipif_refrele(ipif_t *ipif)
5301 {
5302 	ill_t	*ill;
5303 
5304 	ill = ipif->ipif_ill;
5305 
5306 	mutex_enter(&ill->ill_lock);
5307 	ASSERT(ipif->ipif_refcnt != 0);
5308 	ipif->ipif_refcnt--;
5309 	IPIF_UNTRACE_REF(ipif);
5310 	if (ipif->ipif_refcnt != 0) {
5311 		mutex_exit(&ill->ill_lock);
5312 		return;
5313 	}
5314 
5315 	/* Drops the ill_lock */
5316 	ipif_ill_refrele_tail(ill);
5317 }
5318 
5319 ipif_t *
5320 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5321 {
5322 	ipif_t	*ipif;
5323 
5324 	mutex_enter(&ill->ill_lock);
5325 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5326 	    ipif != NULL; ipif = ipif->ipif_next) {
5327 		if (IPIF_IS_CONDEMNED(ipif))
5328 			continue;
5329 		ipif_refhold_locked(ipif);
5330 		mutex_exit(&ill->ill_lock);
5331 		return (ipif);
5332 	}
5333 	mutex_exit(&ill->ill_lock);
5334 	return (NULL);
5335 }
5336 
5337 /*
5338  * TODO: make this table extendible at run time
5339  * Return a pointer to the mac type info for 'mac_type'
5340  */
5341 static ip_m_t *
5342 ip_m_lookup(t_uscalar_t mac_type)
5343 {
5344 	ip_m_t	*ipm;
5345 
5346 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5347 		if (ipm->ip_m_mac_type == mac_type)
5348 			return (ipm);
5349 	return (NULL);
5350 }
5351 
5352 /*
5353  * Make a link layer address from the multicast IP address *addr.
5354  * To form the link layer address, invoke the ip_m_v*mapping function
5355  * associated with the link-layer type.
5356  */
5357 void
5358 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5359 {
5360 	ip_m_t *ipm;
5361 
5362 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5363 		return;
5364 
5365 	ASSERT(addr != NULL);
5366 
5367 	ipm = ip_m_lookup(ill->ill_mactype);
5368 	if (ipm == NULL ||
5369 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5370 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5371 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5372 		    ill->ill_name, ill->ill_mactype));
5373 		return;
5374 	}
5375 	if (ill->ill_isv6)
5376 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5377 	else
5378 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5379 }
5380 
5381 /*
5382  * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5383  * Otherwise returns B_TRUE.
5384  *
5385  * The netmask can be verified to be contiguous with 32 shifts and or
5386  * operations. Take the contiguous mask (in host byte order) and compute
5387  *	mask | mask << 1 | mask << 2 | ... | mask << 31
5388  * the result will be the same as the 'mask' for contiguous mask.
5389  */
5390 static boolean_t
5391 ip_contiguous_mask(uint32_t mask)
5392 {
5393 	uint32_t	m = mask;
5394 	int		i;
5395 
5396 	for (i = 1; i < 32; i++)
5397 		m |= (mask << i);
5398 
5399 	return (m == mask);
5400 }
5401 
5402 /*
5403  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5404  * ill is passed in to associate it with the correct interface.
5405  * If ire_arg is set, then we return the held IRE in that location.
5406  */
5407 int
5408 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5409     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5410     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5411 {
5412 	ire_t	*ire, *nire;
5413 	ire_t	*gw_ire = NULL;
5414 	ipif_t	*ipif = NULL;
5415 	uint_t	type;
5416 	int	match_flags = MATCH_IRE_TYPE;
5417 	tsol_gc_t *gc = NULL;
5418 	tsol_gcgrp_t *gcgrp = NULL;
5419 	boolean_t gcgrp_xtraref = B_FALSE;
5420 	boolean_t cgtp_broadcast;
5421 	boolean_t unbound = B_FALSE;
5422 
5423 	ip1dbg(("ip_rt_add:"));
5424 
5425 	if (ire_arg != NULL)
5426 		*ire_arg = NULL;
5427 
5428 	/* disallow non-contiguous netmasks */
5429 	if (!ip_contiguous_mask(ntohl(mask)))
5430 		return (ENOTSUP);
5431 
5432 	/*
5433 	 * If this is the case of RTF_HOST being set, then we set the netmask
5434 	 * to all ones (regardless if one was supplied).
5435 	 */
5436 	if (flags & RTF_HOST)
5437 		mask = IP_HOST_MASK;
5438 
5439 	/*
5440 	 * Prevent routes with a zero gateway from being created (since
5441 	 * interfaces can currently be plumbed and brought up no assigned
5442 	 * address).
5443 	 */
5444 	if (gw_addr == 0)
5445 		return (ENETUNREACH);
5446 	/*
5447 	 * Get the ipif, if any, corresponding to the gw_addr
5448 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5449 	 * we match on the gatway and destination to handle unnumbered pt-pt
5450 	 * interfaces.
5451 	 */
5452 	if (ill != NULL)
5453 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5454 	else
5455 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5456 	if (ipif != NULL) {
5457 		if (IS_VNI(ipif->ipif_ill)) {
5458 			ipif_refrele(ipif);
5459 			return (EINVAL);
5460 		}
5461 	}
5462 
5463 	/*
5464 	 * GateD will attempt to create routes with a loopback interface
5465 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5466 	 * these routes to be added, but create them as interface routes
5467 	 * since the gateway is an interface address.
5468 	 */
5469 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5470 		flags &= ~RTF_GATEWAY;
5471 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5472 		    mask == IP_HOST_MASK) {
5473 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5474 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5475 			    NULL);
5476 			if (ire != NULL) {
5477 				ire_refrele(ire);
5478 				ipif_refrele(ipif);
5479 				return (EEXIST);
5480 			}
5481 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5482 			    "for 0x%x\n", (void *)ipif,
5483 			    ipif->ipif_ire_type,
5484 			    ntohl(ipif->ipif_lcl_addr)));
5485 			ire = ire_create(
5486 			    (uchar_t *)&dst_addr,	/* dest address */
5487 			    (uchar_t *)&mask,		/* mask */
5488 			    NULL,			/* no gateway */
5489 			    ipif->ipif_ire_type,	/* LOOPBACK */
5490 			    ipif->ipif_ill,
5491 			    zoneid,
5492 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5493 			    NULL,
5494 			    ipst);
5495 
5496 			if (ire == NULL) {
5497 				ipif_refrele(ipif);
5498 				return (ENOMEM);
5499 			}
5500 			/* src address assigned by the caller? */
5501 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5502 				ire->ire_setsrc_addr = src_addr;
5503 
5504 			nire = ire_add(ire);
5505 			if (nire == NULL) {
5506 				/*
5507 				 * In the result of failure, ire_add() will have
5508 				 * already deleted the ire in question, so there
5509 				 * is no need to do that here.
5510 				 */
5511 				ipif_refrele(ipif);
5512 				return (ENOMEM);
5513 			}
5514 			/*
5515 			 * Check if it was a duplicate entry. This handles
5516 			 * the case of two racing route adds for the same route
5517 			 */
5518 			if (nire != ire) {
5519 				ASSERT(nire->ire_identical_ref > 1);
5520 				ire_delete(nire);
5521 				ire_refrele(nire);
5522 				ipif_refrele(ipif);
5523 				return (EEXIST);
5524 			}
5525 			ire = nire;
5526 			goto save_ire;
5527 		}
5528 	}
5529 
5530 	/*
5531 	 * The routes for multicast with CGTP are quite special in that
5532 	 * the gateway is the local interface address, yet RTF_GATEWAY
5533 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5534 	 * this undocumented and unusual use of multicast routes.
5535 	 */
5536 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5537 		flags &= ~RTF_GATEWAY;
5538 
5539 	/*
5540 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5541 	 * and the gateway address provided is one of the system's interface
5542 	 * addresses.  By using the routing socket interface and supplying an
5543 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5544 	 * specifying an interface route to be created is available which uses
5545 	 * the interface index that specifies the outgoing interface rather than
5546 	 * the address of an outgoing interface (which may not be able to
5547 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5548 	 * flag, routes can be specified which not only specify the next-hop to
5549 	 * be used when routing to a certain prefix, but also which outgoing
5550 	 * interface should be used.
5551 	 *
5552 	 * Previously, interfaces would have unique addresses assigned to them
5553 	 * and so the address assigned to a particular interface could be used
5554 	 * to identify a particular interface.  One exception to this was the
5555 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5556 	 *
5557 	 * With the advent of IPv6 and its link-local addresses, this
5558 	 * restriction was relaxed and interfaces could share addresses between
5559 	 * themselves.  In fact, typically all of the link-local interfaces on
5560 	 * an IPv6 node or router will have the same link-local address.  In
5561 	 * order to differentiate between these interfaces, the use of an
5562 	 * interface index is necessary and this index can be carried inside a
5563 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5564 	 * of using the interface index, however, is that all of the ipif's that
5565 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5566 	 * cannot be used to differentiate between ipif's (or logical
5567 	 * interfaces) that belong to the same ill (physical interface).
5568 	 *
5569 	 * For example, in the following case involving IPv4 interfaces and
5570 	 * logical interfaces
5571 	 *
5572 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5573 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5574 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5575 	 *
5576 	 * the ipif's corresponding to each of these interface routes can be
5577 	 * uniquely identified by the "gateway" (actually interface address).
5578 	 *
5579 	 * In this case involving multiple IPv6 default routes to a particular
5580 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5581 	 * default route is of interest:
5582 	 *
5583 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5584 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5585 	 */
5586 
5587 	/* RTF_GATEWAY not set */
5588 	if (!(flags & RTF_GATEWAY)) {
5589 		if (sp != NULL) {
5590 			ip2dbg(("ip_rt_add: gateway security attributes "
5591 			    "cannot be set with interface route\n"));
5592 			if (ipif != NULL)
5593 				ipif_refrele(ipif);
5594 			return (EINVAL);
5595 		}
5596 
5597 		/*
5598 		 * Whether or not ill (RTA_IFP) is set, we require that
5599 		 * the gateway is one of our local addresses.
5600 		 */
5601 		if (ipif == NULL)
5602 			return (ENETUNREACH);
5603 
5604 		/*
5605 		 * We use MATCH_IRE_ILL here. If the caller specified an
5606 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5607 		 * we use the ill derived from the gateway address.
5608 		 * We can always match the gateway address since we record it
5609 		 * in ire_gateway_addr.
5610 		 * We don't allow RTA_IFP to specify a different ill than the
5611 		 * one matching the ipif to make sure we can delete the route.
5612 		 */
5613 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5614 		if (ill == NULL) {
5615 			ill = ipif->ipif_ill;
5616 		} else if (ill != ipif->ipif_ill) {
5617 			ipif_refrele(ipif);
5618 			return (EINVAL);
5619 		}
5620 
5621 		/*
5622 		 * We check for an existing entry at this point.
5623 		 *
5624 		 * Since a netmask isn't passed in via the ioctl interface
5625 		 * (SIOCADDRT), we don't check for a matching netmask in that
5626 		 * case.
5627 		 */
5628 		if (!ioctl_msg)
5629 			match_flags |= MATCH_IRE_MASK;
5630 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5631 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5632 		    NULL);
5633 		if (ire != NULL) {
5634 			ire_refrele(ire);
5635 			ipif_refrele(ipif);
5636 			return (EEXIST);
5637 		}
5638 
5639 		/*
5640 		 * Some software (for example, GateD and Sun Cluster) attempts
5641 		 * to create (what amount to) IRE_PREFIX routes with the
5642 		 * loopback address as the gateway.  This is primarily done to
5643 		 * set up prefixes with the RTF_REJECT flag set (for example,
5644 		 * when generating aggregate routes.)
5645 		 *
5646 		 * If the IRE type (as defined by ill->ill_net_type) would be
5647 		 * IRE_LOOPBACK, then we map the request into a
5648 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5649 		 * these interface routes, by definition, can only be that.
5650 		 *
5651 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5652 		 * routine, but rather using ire_create() directly.
5653 		 *
5654 		 */
5655 		type = ill->ill_net_type;
5656 		if (type == IRE_LOOPBACK) {
5657 			type = IRE_IF_NORESOLVER;
5658 			flags |= RTF_BLACKHOLE;
5659 		}
5660 
5661 		/*
5662 		 * Create a copy of the IRE_IF_NORESOLVER or
5663 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5664 		 * gateway.
5665 		 */
5666 		ire = ire_create(
5667 		    (uchar_t *)&dst_addr,
5668 		    (uint8_t *)&mask,
5669 		    (uint8_t *)&gw_addr,
5670 		    type,
5671 		    ill,
5672 		    zoneid,
5673 		    flags,
5674 		    NULL,
5675 		    ipst);
5676 		if (ire == NULL) {
5677 			ipif_refrele(ipif);
5678 			return (ENOMEM);
5679 		}
5680 
5681 		/* src address assigned by the caller? */
5682 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5683 			ire->ire_setsrc_addr = src_addr;
5684 
5685 		nire = ire_add(ire);
5686 		if (nire == NULL) {
5687 			/*
5688 			 * In the result of failure, ire_add() will have
5689 			 * already deleted the ire in question, so there
5690 			 * is no need to do that here.
5691 			 */
5692 			ipif_refrele(ipif);
5693 			return (ENOMEM);
5694 		}
5695 		/*
5696 		 * Check if it was a duplicate entry. This handles
5697 		 * the case of two racing route adds for the same route
5698 		 */
5699 		if (nire != ire) {
5700 			ire_delete(nire);
5701 			ire_refrele(nire);
5702 			ipif_refrele(ipif);
5703 			return (EEXIST);
5704 		}
5705 		ire = nire;
5706 		goto save_ire;
5707 	}
5708 
5709 	/*
5710 	 * Get an interface IRE for the specified gateway.
5711 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5712 	 * gateway, it is currently unreachable and we fail the request
5713 	 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5714 	 * is an IRE_LOCAL or IRE_LOOPBACK.
5715 	 * If RTA_IFP was specified we look on that particular ill.
5716 	 */
5717 	if (ill != NULL)
5718 		match_flags |= MATCH_IRE_ILL;
5719 
5720 	/* Check whether the gateway is reachable. */
5721 again:
5722 	type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5723 	if (flags & RTF_INDIRECT)
5724 		type |= IRE_OFFLINK;
5725 
5726 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5727 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5728 	if (gw_ire == NULL) {
5729 		/*
5730 		 * With IPMP, we allow host routes to influence in.mpathd's
5731 		 * target selection.  However, if the test addresses are on
5732 		 * their own network, the above lookup will fail since the
5733 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5734 		 * hidden test IREs to be found and try again.
5735 		 */
5736 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5737 			match_flags |= MATCH_IRE_TESTHIDDEN;
5738 			goto again;
5739 		}
5740 		if (ipif != NULL)
5741 			ipif_refrele(ipif);
5742 		return (ENETUNREACH);
5743 	}
5744 	if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5745 		ire_refrele(gw_ire);
5746 		if (ipif != NULL)
5747 			ipif_refrele(ipif);
5748 		return (ENETUNREACH);
5749 	}
5750 
5751 	if (ill == NULL && !(flags & RTF_INDIRECT)) {
5752 		unbound = B_TRUE;
5753 		if (ipst->ips_ip_strict_src_multihoming > 0)
5754 			ill = gw_ire->ire_ill;
5755 	}
5756 
5757 	/*
5758 	 * We create one of three types of IREs as a result of this request
5759 	 * based on the netmask.  A netmask of all ones (which is automatically
5760 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5761 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5762 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5763 	 * destination prefix.
5764 	 */
5765 	if (mask == IP_HOST_MASK)
5766 		type = IRE_HOST;
5767 	else if (mask == 0)
5768 		type = IRE_DEFAULT;
5769 	else
5770 		type = IRE_PREFIX;
5771 
5772 	/* check for a duplicate entry */
5773 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5774 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5775 	    0, ipst, NULL);
5776 	if (ire != NULL) {
5777 		if (ipif != NULL)
5778 			ipif_refrele(ipif);
5779 		ire_refrele(gw_ire);
5780 		ire_refrele(ire);
5781 		return (EEXIST);
5782 	}
5783 
5784 	/* Security attribute exists */
5785 	if (sp != NULL) {
5786 		tsol_gcgrp_addr_t ga;
5787 
5788 		/* find or create the gateway credentials group */
5789 		ga.ga_af = AF_INET;
5790 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5791 
5792 		/* we hold reference to it upon success */
5793 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5794 		if (gcgrp == NULL) {
5795 			if (ipif != NULL)
5796 				ipif_refrele(ipif);
5797 			ire_refrele(gw_ire);
5798 			return (ENOMEM);
5799 		}
5800 
5801 		/*
5802 		 * Create and add the security attribute to the group; a
5803 		 * reference to the group is made upon allocating a new
5804 		 * entry successfully.  If it finds an already-existing
5805 		 * entry for the security attribute in the group, it simply
5806 		 * returns it and no new reference is made to the group.
5807 		 */
5808 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5809 		if (gc == NULL) {
5810 			if (ipif != NULL)
5811 				ipif_refrele(ipif);
5812 			/* release reference held by gcgrp_lookup */
5813 			GCGRP_REFRELE(gcgrp);
5814 			ire_refrele(gw_ire);
5815 			return (ENOMEM);
5816 		}
5817 	}
5818 
5819 	/* Create the IRE. */
5820 	ire = ire_create(
5821 	    (uchar_t *)&dst_addr,		/* dest address */
5822 	    (uchar_t *)&mask,			/* mask */
5823 	    (uchar_t *)&gw_addr,		/* gateway address */
5824 	    (ushort_t)type,			/* IRE type */
5825 	    ill,
5826 	    zoneid,
5827 	    flags,
5828 	    gc,					/* security attribute */
5829 	    ipst);
5830 
5831 	/*
5832 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5833 	 * reference to the 'gcgrp'. We can now release the extra reference
5834 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5835 	 */
5836 	if (gcgrp_xtraref)
5837 		GCGRP_REFRELE(gcgrp);
5838 	if (ire == NULL) {
5839 		if (gc != NULL)
5840 			GC_REFRELE(gc);
5841 		if (ipif != NULL)
5842 			ipif_refrele(ipif);
5843 		ire_refrele(gw_ire);
5844 		return (ENOMEM);
5845 	}
5846 
5847 	/* Before we add, check if an extra CGTP broadcast is needed */
5848 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5849 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5850 
5851 	/* src address assigned by the caller? */
5852 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5853 		ire->ire_setsrc_addr = src_addr;
5854 
5855 	ire->ire_unbound = unbound;
5856 
5857 	/*
5858 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5859 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5860 	 */
5861 
5862 	/* Add the new IRE. */
5863 	nire = ire_add(ire);
5864 	if (nire == NULL) {
5865 		/*
5866 		 * In the result of failure, ire_add() will have
5867 		 * already deleted the ire in question, so there
5868 		 * is no need to do that here.
5869 		 */
5870 		if (ipif != NULL)
5871 			ipif_refrele(ipif);
5872 		ire_refrele(gw_ire);
5873 		return (ENOMEM);
5874 	}
5875 	/*
5876 	 * Check if it was a duplicate entry. This handles
5877 	 * the case of two racing route adds for the same route
5878 	 */
5879 	if (nire != ire) {
5880 		ire_delete(nire);
5881 		ire_refrele(nire);
5882 		if (ipif != NULL)
5883 			ipif_refrele(ipif);
5884 		ire_refrele(gw_ire);
5885 		return (EEXIST);
5886 	}
5887 	ire = nire;
5888 
5889 	if (flags & RTF_MULTIRT) {
5890 		/*
5891 		 * Invoke the CGTP (multirouting) filtering module
5892 		 * to add the dst address in the filtering database.
5893 		 * Replicated inbound packets coming from that address
5894 		 * will be filtered to discard the duplicates.
5895 		 * It is not necessary to call the CGTP filter hook
5896 		 * when the dst address is a broadcast or multicast,
5897 		 * because an IP source address cannot be a broadcast
5898 		 * or a multicast.
5899 		 */
5900 		if (cgtp_broadcast) {
5901 			ip_cgtp_bcast_add(ire, ipst);
5902 			goto save_ire;
5903 		}
5904 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5905 		    !CLASSD(ire->ire_addr)) {
5906 			int res;
5907 			ipif_t *src_ipif;
5908 
5909 			/* Find the source address corresponding to gw_ire */
5910 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5911 			    NULL, zoneid, ipst);
5912 			if (src_ipif != NULL) {
5913 				res = ipst->ips_ip_cgtp_filter_ops->
5914 				    cfo_add_dest_v4(
5915 				    ipst->ips_netstack->netstack_stackid,
5916 				    ire->ire_addr,
5917 				    ire->ire_gateway_addr,
5918 				    ire->ire_setsrc_addr,
5919 				    src_ipif->ipif_lcl_addr);
5920 				ipif_refrele(src_ipif);
5921 			} else {
5922 				res = EADDRNOTAVAIL;
5923 			}
5924 			if (res != 0) {
5925 				if (ipif != NULL)
5926 					ipif_refrele(ipif);
5927 				ire_refrele(gw_ire);
5928 				ire_delete(ire);
5929 				ire_refrele(ire);	/* Held in ire_add */
5930 				return (res);
5931 			}
5932 		}
5933 	}
5934 
5935 save_ire:
5936 	if (gw_ire != NULL) {
5937 		ire_refrele(gw_ire);
5938 		gw_ire = NULL;
5939 	}
5940 	if (ill != NULL) {
5941 		/*
5942 		 * Save enough information so that we can recreate the IRE if
5943 		 * the interface goes down and then up.  The metrics associated
5944 		 * with the route will be saved as well when rts_setmetrics() is
5945 		 * called after the IRE has been created.  In the case where
5946 		 * memory cannot be allocated, none of this information will be
5947 		 * saved.
5948 		 */
5949 		ill_save_ire(ill, ire);
5950 	}
5951 	if (ioctl_msg)
5952 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5953 	if (ire_arg != NULL) {
5954 		/*
5955 		 * Store the ire that was successfully added into where ire_arg
5956 		 * points to so that callers don't have to look it up
5957 		 * themselves (but they are responsible for ire_refrele()ing
5958 		 * the ire when they are finished with it).
5959 		 */
5960 		*ire_arg = ire;
5961 	} else {
5962 		ire_refrele(ire);		/* Held in ire_add */
5963 	}
5964 	if (ipif != NULL)
5965 		ipif_refrele(ipif);
5966 	return (0);
5967 }
5968 
5969 /*
5970  * ip_rt_delete is called to delete an IPv4 route.
5971  * ill is passed in to associate it with the correct interface.
5972  */
5973 /* ARGSUSED4 */
5974 int
5975 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5976     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5977     ip_stack_t *ipst, zoneid_t zoneid)
5978 {
5979 	ire_t	*ire = NULL;
5980 	ipif_t	*ipif;
5981 	uint_t	type;
5982 	uint_t	match_flags = MATCH_IRE_TYPE;
5983 	int	err = 0;
5984 
5985 	ip1dbg(("ip_rt_delete:"));
5986 	/*
5987 	 * If this is the case of RTF_HOST being set, then we set the netmask
5988 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5989 	 */
5990 	if (flags & RTF_HOST) {
5991 		mask = IP_HOST_MASK;
5992 		match_flags |= MATCH_IRE_MASK;
5993 	} else if (rtm_addrs & RTA_NETMASK) {
5994 		match_flags |= MATCH_IRE_MASK;
5995 	}
5996 
5997 	/*
5998 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5999 	 * we check if the gateway address is one of our interfaces first,
6000 	 * and fall back on RTF_GATEWAY routes.
6001 	 *
6002 	 * This makes it possible to delete an original
6003 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6004 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
6005 	 * and those can not be deleted here.
6006 	 *
6007 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
6008 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
6009 	 * otherwise we use the ill derived from the gateway address.
6010 	 * We can always match the gateway address since we record it
6011 	 * in ire_gateway_addr.
6012 	 *
6013 	 * For more detail on specifying routes by gateway address and by
6014 	 * interface index, see the comments in ip_rt_add().
6015 	 */
6016 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6017 	if (ipif != NULL) {
6018 		ill_t	*ill_match;
6019 
6020 		if (ill != NULL)
6021 			ill_match = ill;
6022 		else
6023 			ill_match = ipif->ipif_ill;
6024 
6025 		match_flags |= MATCH_IRE_ILL;
6026 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6027 			ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
6028 			    IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
6029 			    match_flags, 0, ipst, NULL);
6030 		}
6031 		if (ire == NULL) {
6032 			match_flags |= MATCH_IRE_GW;
6033 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6034 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6035 			    match_flags, 0, ipst, NULL);
6036 		}
6037 		/* Avoid deleting routes created by kernel from an ipif */
6038 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6039 			ire_refrele(ire);
6040 			ire = NULL;
6041 		}
6042 
6043 		/* Restore in case we didn't find a match */
6044 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6045 	}
6046 
6047 	if (ire == NULL) {
6048 		/*
6049 		 * At this point, the gateway address is not one of our own
6050 		 * addresses or a matching interface route was not found.  We
6051 		 * set the IRE type to lookup based on whether
6052 		 * this is a host route, a default route or just a prefix.
6053 		 *
6054 		 * If an ill was passed in, then the lookup is based on an
6055 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6056 		 */
6057 		match_flags |= MATCH_IRE_GW;
6058 		if (ill != NULL)
6059 			match_flags |= MATCH_IRE_ILL;
6060 		if (mask == IP_HOST_MASK)
6061 			type = IRE_HOST;
6062 		else if (mask == 0)
6063 			type = IRE_DEFAULT;
6064 		else
6065 			type = IRE_PREFIX;
6066 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6067 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6068 	}
6069 
6070 	if (ipif != NULL) {
6071 		ipif_refrele(ipif);
6072 		ipif = NULL;
6073 	}
6074 
6075 	if (ire == NULL)
6076 		return (ESRCH);
6077 
6078 	if (ire->ire_flags & RTF_MULTIRT) {
6079 		/*
6080 		 * Invoke the CGTP (multirouting) filtering module
6081 		 * to remove the dst address from the filtering database.
6082 		 * Packets coming from that address will no longer be
6083 		 * filtered to remove duplicates.
6084 		 */
6085 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6086 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6087 			    ipst->ips_netstack->netstack_stackid,
6088 			    ire->ire_addr, ire->ire_gateway_addr);
6089 		}
6090 		ip_cgtp_bcast_delete(ire, ipst);
6091 	}
6092 
6093 	ill = ire->ire_ill;
6094 	if (ill != NULL)
6095 		ill_remove_saved_ire(ill, ire);
6096 	if (ioctl_msg)
6097 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6098 	ire_delete(ire);
6099 	ire_refrele(ire);
6100 	return (err);
6101 }
6102 
6103 /*
6104  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6105  */
6106 /* ARGSUSED */
6107 int
6108 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6109     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6110 {
6111 	ipaddr_t dst_addr;
6112 	ipaddr_t gw_addr;
6113 	ipaddr_t mask;
6114 	int error = 0;
6115 	mblk_t *mp1;
6116 	struct rtentry *rt;
6117 	ipif_t *ipif = NULL;
6118 	ip_stack_t	*ipst;
6119 
6120 	ASSERT(q->q_next == NULL);
6121 	ipst = CONNQ_TO_IPST(q);
6122 
6123 	ip1dbg(("ip_siocaddrt:"));
6124 	/* Existence of mp1 verified in ip_wput_nondata */
6125 	mp1 = mp->b_cont->b_cont;
6126 	rt = (struct rtentry *)mp1->b_rptr;
6127 
6128 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6129 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6130 
6131 	/*
6132 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6133 	 * to a particular host address.  In this case, we set the netmask to
6134 	 * all ones for the particular destination address.  Otherwise,
6135 	 * determine the netmask to be used based on dst_addr and the interfaces
6136 	 * in use.
6137 	 */
6138 	if (rt->rt_flags & RTF_HOST) {
6139 		mask = IP_HOST_MASK;
6140 	} else {
6141 		/*
6142 		 * Note that ip_subnet_mask returns a zero mask in the case of
6143 		 * default (an all-zeroes address).
6144 		 */
6145 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6146 	}
6147 
6148 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6149 	    B_TRUE, NULL, ipst, ALL_ZONES);
6150 	if (ipif != NULL)
6151 		ipif_refrele(ipif);
6152 	return (error);
6153 }
6154 
6155 /*
6156  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6157  */
6158 /* ARGSUSED */
6159 int
6160 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6161     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6162 {
6163 	ipaddr_t dst_addr;
6164 	ipaddr_t gw_addr;
6165 	ipaddr_t mask;
6166 	int error;
6167 	mblk_t *mp1;
6168 	struct rtentry *rt;
6169 	ipif_t *ipif = NULL;
6170 	ip_stack_t	*ipst;
6171 
6172 	ASSERT(q->q_next == NULL);
6173 	ipst = CONNQ_TO_IPST(q);
6174 
6175 	ip1dbg(("ip_siocdelrt:"));
6176 	/* Existence of mp1 verified in ip_wput_nondata */
6177 	mp1 = mp->b_cont->b_cont;
6178 	rt = (struct rtentry *)mp1->b_rptr;
6179 
6180 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6181 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6182 
6183 	/*
6184 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6185 	 * to a particular host address.  In this case, we set the netmask to
6186 	 * all ones for the particular destination address.  Otherwise,
6187 	 * determine the netmask to be used based on dst_addr and the interfaces
6188 	 * in use.
6189 	 */
6190 	if (rt->rt_flags & RTF_HOST) {
6191 		mask = IP_HOST_MASK;
6192 	} else {
6193 		/*
6194 		 * Note that ip_subnet_mask returns a zero mask in the case of
6195 		 * default (an all-zeroes address).
6196 		 */
6197 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6198 	}
6199 
6200 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6201 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6202 	    ipst, ALL_ZONES);
6203 	if (ipif != NULL)
6204 		ipif_refrele(ipif);
6205 	return (error);
6206 }
6207 
6208 /*
6209  * Enqueue the mp onto the ipsq, chained by b_next.
6210  * b_prev stores the function to be executed later, and b_queue the queue
6211  * where this mp originated.
6212  */
6213 void
6214 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6215     ill_t *pending_ill)
6216 {
6217 	conn_t	*connp;
6218 	ipxop_t *ipx = ipsq->ipsq_xop;
6219 
6220 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6221 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6222 	ASSERT(func != NULL);
6223 
6224 	mp->b_queue = q;
6225 	mp->b_prev = (void *)func;
6226 	mp->b_next = NULL;
6227 
6228 	switch (type) {
6229 	case CUR_OP:
6230 		if (ipx->ipx_mptail != NULL) {
6231 			ASSERT(ipx->ipx_mphead != NULL);
6232 			ipx->ipx_mptail->b_next = mp;
6233 		} else {
6234 			ASSERT(ipx->ipx_mphead == NULL);
6235 			ipx->ipx_mphead = mp;
6236 		}
6237 		ipx->ipx_mptail = mp;
6238 		break;
6239 
6240 	case NEW_OP:
6241 		if (ipsq->ipsq_xopq_mptail != NULL) {
6242 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6243 			ipsq->ipsq_xopq_mptail->b_next = mp;
6244 		} else {
6245 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6246 			ipsq->ipsq_xopq_mphead = mp;
6247 		}
6248 		ipsq->ipsq_xopq_mptail = mp;
6249 		ipx->ipx_ipsq_queued = B_TRUE;
6250 		break;
6251 
6252 	case SWITCH_OP:
6253 		ASSERT(ipsq->ipsq_swxop != NULL);
6254 		/* only one switch operation is currently allowed */
6255 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6256 		ipsq->ipsq_switch_mp = mp;
6257 		ipx->ipx_ipsq_queued = B_TRUE;
6258 		break;
6259 	default:
6260 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6261 	}
6262 
6263 	if (CONN_Q(q) && pending_ill != NULL) {
6264 		connp = Q_TO_CONN(q);
6265 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6266 		connp->conn_oper_pending_ill = pending_ill;
6267 	}
6268 }
6269 
6270 /*
6271  * Dequeue the next message that requested exclusive access to this IPSQ's
6272  * xop.  Specifically:
6273  *
6274  *  1. If we're still processing the current operation on `ipsq', then
6275  *     dequeue the next message for the operation (from ipx_mphead), or
6276  *     return NULL if there are no queued messages for the operation.
6277  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6278  *
6279  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6280  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6281  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6282  *     leaving IPMP groups and require a careful dance -- see the comments
6283  *     in-line below for details.  If we're leaving a group xop or if we're
6284  *     joining a group xop and become writer on it, then we proceed to (3).
6285  *     Otherwise, we return NULL and exit the xop.
6286  *
6287  *  3. For each IPSQ in the xop, return any switch operation stored on
6288  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6289  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6290  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6291  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6292  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6293  *     each phyint in the group, including the IPMP meta-interface phyint.
6294  */
6295 static mblk_t *
6296 ipsq_dq(ipsq_t *ipsq)
6297 {
6298 	ill_t	*illv4, *illv6;
6299 	mblk_t	*mp;
6300 	ipsq_t	*xopipsq;
6301 	ipsq_t	*leftipsq = NULL;
6302 	ipxop_t *ipx;
6303 	phyint_t *phyi = ipsq->ipsq_phyint;
6304 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6305 	boolean_t emptied = B_FALSE;
6306 
6307 	/*
6308 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6309 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6310 	 */
6311 	rw_enter(&ipst->ips_ill_g_lock,
6312 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6313 	mutex_enter(&ipsq->ipsq_lock);
6314 	ipx = ipsq->ipsq_xop;
6315 	mutex_enter(&ipx->ipx_lock);
6316 
6317 	/*
6318 	 * Dequeue the next message associated with the current exclusive
6319 	 * operation, if any.
6320 	 */
6321 	if ((mp = ipx->ipx_mphead) != NULL) {
6322 		ipx->ipx_mphead = mp->b_next;
6323 		if (ipx->ipx_mphead == NULL)
6324 			ipx->ipx_mptail = NULL;
6325 		mp->b_next = (void *)ipsq;
6326 		goto out;
6327 	}
6328 
6329 	if (ipx->ipx_current_ipif != NULL)
6330 		goto empty;
6331 
6332 	if (ipsq->ipsq_swxop != NULL) {
6333 		/*
6334 		 * The exclusive operation that is now being completed has
6335 		 * requested a switch to a different xop.  This happens
6336 		 * when an interface joins or leaves an IPMP group.  Joins
6337 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6338 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6339 		 * (phyint_free()), or interface plumb for an ill type
6340 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6341 		 *
6342 		 * Xop switches are not allowed on the IPMP meta-interface.
6343 		 */
6344 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6345 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6346 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6347 
6348 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6349 			/*
6350 			 * We're switching back to our own xop, so we have two
6351 			 * xop's to drain/exit: our own, and the group xop
6352 			 * that we are leaving.
6353 			 *
6354 			 * First, pull ourselves out of the group ipsq list.
6355 			 * This is safe since we're writer on ill_g_lock.
6356 			 */
6357 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6358 
6359 			xopipsq = ipx->ipx_ipsq;
6360 			while (xopipsq->ipsq_next != ipsq)
6361 				xopipsq = xopipsq->ipsq_next;
6362 
6363 			xopipsq->ipsq_next = ipsq->ipsq_next;
6364 			ipsq->ipsq_next = ipsq;
6365 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6366 			ipsq->ipsq_swxop = NULL;
6367 
6368 			/*
6369 			 * Second, prepare to exit the group xop.  The actual
6370 			 * ipsq_exit() is done at the end of this function
6371 			 * since we cannot hold any locks across ipsq_exit().
6372 			 * Note that although we drop the group's ipx_lock, no
6373 			 * threads can proceed since we're still ipx_writer.
6374 			 */
6375 			leftipsq = xopipsq;
6376 			mutex_exit(&ipx->ipx_lock);
6377 
6378 			/*
6379 			 * Third, set ipx to point to our own xop (which was
6380 			 * inactive and therefore can be entered).
6381 			 */
6382 			ipx = ipsq->ipsq_xop;
6383 			mutex_enter(&ipx->ipx_lock);
6384 			ASSERT(ipx->ipx_writer == NULL);
6385 			ASSERT(ipx->ipx_current_ipif == NULL);
6386 		} else {
6387 			/*
6388 			 * We're switching from our own xop to a group xop.
6389 			 * The requestor of the switch must ensure that the
6390 			 * group xop cannot go away (e.g. by ensuring the
6391 			 * phyint associated with the xop cannot go away).
6392 			 *
6393 			 * If we can become writer on our new xop, then we'll
6394 			 * do the drain.  Otherwise, the current writer of our
6395 			 * new xop will do the drain when it exits.
6396 			 *
6397 			 * First, splice ourselves into the group IPSQ list.
6398 			 * This is safe since we're writer on ill_g_lock.
6399 			 */
6400 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6401 
6402 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6403 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6404 				xopipsq = xopipsq->ipsq_next;
6405 
6406 			xopipsq->ipsq_next = ipsq;
6407 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6408 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6409 			ipsq->ipsq_swxop = NULL;
6410 
6411 			/*
6412 			 * Second, exit our own xop, since it's now unused.
6413 			 * This is safe since we've got the only reference.
6414 			 */
6415 			ASSERT(ipx->ipx_writer == curthread);
6416 			ipx->ipx_writer = NULL;
6417 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6418 			ipx->ipx_ipsq_queued = B_FALSE;
6419 			mutex_exit(&ipx->ipx_lock);
6420 
6421 			/*
6422 			 * Third, set ipx to point to our new xop, and check
6423 			 * if we can become writer on it.  If we cannot, then
6424 			 * the current writer will drain the IPSQ group when
6425 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6426 			 * because we're still holding ipsq_lock.
6427 			 */
6428 			ipx = ipsq->ipsq_xop;
6429 			mutex_enter(&ipx->ipx_lock);
6430 			if (ipx->ipx_writer != NULL ||
6431 			    ipx->ipx_current_ipif != NULL) {
6432 				goto out;
6433 			}
6434 		}
6435 
6436 		/*
6437 		 * Fourth, become writer on our new ipx before we continue
6438 		 * with the drain.  Note that we never dropped ipsq_lock
6439 		 * above, so no other thread could've raced with us to
6440 		 * become writer first.  Also, we're holding ipx_lock, so
6441 		 * no other thread can examine the ipx right now.
6442 		 */
6443 		ASSERT(ipx->ipx_current_ipif == NULL);
6444 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6445 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6446 		ipx->ipx_writer = curthread;
6447 		ipx->ipx_forced = B_FALSE;
6448 #ifdef DEBUG
6449 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6450 #endif
6451 	}
6452 
6453 	xopipsq = ipsq;
6454 	do {
6455 		/*
6456 		 * So that other operations operate on a consistent and
6457 		 * complete phyint, a switch message on an IPSQ must be
6458 		 * handled prior to any other operations on that IPSQ.
6459 		 */
6460 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6461 			xopipsq->ipsq_switch_mp = NULL;
6462 			ASSERT(mp->b_next == NULL);
6463 			mp->b_next = (void *)xopipsq;
6464 			goto out;
6465 		}
6466 
6467 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6468 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6469 			if (xopipsq->ipsq_xopq_mphead == NULL)
6470 				xopipsq->ipsq_xopq_mptail = NULL;
6471 			mp->b_next = (void *)xopipsq;
6472 			goto out;
6473 		}
6474 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6475 empty:
6476 	/*
6477 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6478 	 * new messages can end up on any IPSQ in the xop.
6479 	 */
6480 	ipx->ipx_writer = NULL;
6481 	ipx->ipx_forced = B_FALSE;
6482 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6483 	ipx->ipx_ipsq_queued = B_FALSE;
6484 	emptied = B_TRUE;
6485 #ifdef	DEBUG
6486 	ipx->ipx_depth = 0;
6487 #endif
6488 out:
6489 	mutex_exit(&ipx->ipx_lock);
6490 	mutex_exit(&ipsq->ipsq_lock);
6491 
6492 	/*
6493 	 * If we completely emptied the xop, then wake up any threads waiting
6494 	 * to enter any of the IPSQ's associated with it.
6495 	 */
6496 	if (emptied) {
6497 		xopipsq = ipsq;
6498 		do {
6499 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6500 				continue;
6501 
6502 			illv4 = phyi->phyint_illv4;
6503 			illv6 = phyi->phyint_illv6;
6504 
6505 			GRAB_ILL_LOCKS(illv4, illv6);
6506 			if (illv4 != NULL)
6507 				cv_broadcast(&illv4->ill_cv);
6508 			if (illv6 != NULL)
6509 				cv_broadcast(&illv6->ill_cv);
6510 			RELEASE_ILL_LOCKS(illv4, illv6);
6511 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6512 	}
6513 	rw_exit(&ipst->ips_ill_g_lock);
6514 
6515 	/*
6516 	 * Now that all locks are dropped, exit the IPSQ we left.
6517 	 */
6518 	if (leftipsq != NULL)
6519 		ipsq_exit(leftipsq);
6520 
6521 	return (mp);
6522 }
6523 
6524 /*
6525  * Return completion status of previously initiated DLPI operations on
6526  * ills in the purview of an ipsq.
6527  */
6528 static boolean_t
6529 ipsq_dlpi_done(ipsq_t *ipsq)
6530 {
6531 	ipsq_t		*ipsq_start;
6532 	phyint_t	*phyi;
6533 	ill_t		*ill;
6534 
6535 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6536 	ipsq_start = ipsq;
6537 
6538 	do {
6539 		/*
6540 		 * The only current users of this function are ipsq_try_enter
6541 		 * and ipsq_enter which have made sure that ipsq_writer is
6542 		 * NULL before we reach here. ill_dlpi_pending is modified
6543 		 * only by an ipsq writer
6544 		 */
6545 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6546 		phyi = ipsq->ipsq_phyint;
6547 		/*
6548 		 * phyi could be NULL if a phyint that is part of an
6549 		 * IPMP group is being unplumbed. A more detailed
6550 		 * comment is in ipmp_grp_update_kstats()
6551 		 */
6552 		if (phyi != NULL) {
6553 			ill = phyi->phyint_illv4;
6554 			if (ill != NULL &&
6555 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6556 			    ill->ill_arl_dlpi_pending))
6557 				return (B_FALSE);
6558 
6559 			ill = phyi->phyint_illv6;
6560 			if (ill != NULL &&
6561 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6562 				return (B_FALSE);
6563 		}
6564 
6565 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6566 
6567 	return (B_TRUE);
6568 }
6569 
6570 /*
6571  * Enter the ipsq corresponding to ill, by waiting synchronously till
6572  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6573  * will have to drain completely before ipsq_enter returns success.
6574  * ipx_current_ipif will be set if some exclusive op is in progress,
6575  * and the ipsq_exit logic will start the next enqueued op after
6576  * completion of the current op. If 'force' is used, we don't wait
6577  * for the enqueued ops. This is needed when a conn_close wants to
6578  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6579  * of an ill can also use this option. But we dont' use it currently.
6580  */
6581 #define	ENTER_SQ_WAIT_TICKS 100
6582 boolean_t
6583 ipsq_enter(ill_t *ill, boolean_t force, int type)
6584 {
6585 	ipsq_t	*ipsq;
6586 	ipxop_t *ipx;
6587 	boolean_t waited_enough = B_FALSE;
6588 	ip_stack_t *ipst = ill->ill_ipst;
6589 
6590 	/*
6591 	 * Note that the relationship between ill and ipsq is fixed as long as
6592 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6593 	 * relationship between the IPSQ and xop cannot change.  However,
6594 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6595 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6596 	 * waking up all ills in the xop when it becomes available.
6597 	 */
6598 	for (;;) {
6599 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6600 		mutex_enter(&ill->ill_lock);
6601 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6602 			mutex_exit(&ill->ill_lock);
6603 			rw_exit(&ipst->ips_ill_g_lock);
6604 			return (B_FALSE);
6605 		}
6606 
6607 		ipsq = ill->ill_phyint->phyint_ipsq;
6608 		mutex_enter(&ipsq->ipsq_lock);
6609 		ipx = ipsq->ipsq_xop;
6610 		mutex_enter(&ipx->ipx_lock);
6611 
6612 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6613 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6614 		    waited_enough))
6615 			break;
6616 
6617 		rw_exit(&ipst->ips_ill_g_lock);
6618 
6619 		if (!force || ipx->ipx_writer != NULL) {
6620 			mutex_exit(&ipx->ipx_lock);
6621 			mutex_exit(&ipsq->ipsq_lock);
6622 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6623 		} else {
6624 			mutex_exit(&ipx->ipx_lock);
6625 			mutex_exit(&ipsq->ipsq_lock);
6626 			(void) cv_reltimedwait(&ill->ill_cv,
6627 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6628 			waited_enough = B_TRUE;
6629 		}
6630 		mutex_exit(&ill->ill_lock);
6631 	}
6632 
6633 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6634 	ASSERT(ipx->ipx_reentry_cnt == 0);
6635 	ipx->ipx_writer = curthread;
6636 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6637 	ipx->ipx_reentry_cnt++;
6638 #ifdef DEBUG
6639 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6640 #endif
6641 	mutex_exit(&ipx->ipx_lock);
6642 	mutex_exit(&ipsq->ipsq_lock);
6643 	mutex_exit(&ill->ill_lock);
6644 	rw_exit(&ipst->ips_ill_g_lock);
6645 
6646 	return (B_TRUE);
6647 }
6648 
6649 /*
6650  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6651  * across the call to the core interface ipsq_try_enter() and hence calls this
6652  * function directly. This is explained more fully in ipif_set_values().
6653  * In order to support the above constraint, ipsq_try_enter is implemented as
6654  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6655  */
6656 static ipsq_t *
6657 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6658     int type, boolean_t reentry_ok)
6659 {
6660 	ipsq_t	*ipsq;
6661 	ipxop_t	*ipx;
6662 	ip_stack_t *ipst = ill->ill_ipst;
6663 
6664 	/*
6665 	 * lock ordering:
6666 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6667 	 *
6668 	 * ipx of an ipsq can't change when ipsq_lock is held.
6669 	 */
6670 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6671 	GRAB_CONN_LOCK(q);
6672 	mutex_enter(&ill->ill_lock);
6673 	ipsq = ill->ill_phyint->phyint_ipsq;
6674 	mutex_enter(&ipsq->ipsq_lock);
6675 	ipx = ipsq->ipsq_xop;
6676 	mutex_enter(&ipx->ipx_lock);
6677 
6678 	/*
6679 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6680 	 *    (Note: If the caller does not specify reentry_ok then neither
6681 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6682 	 *    again. Otherwise it can lead to an infinite loop
6683 	 * 2. Enter the ipsq if there is no current writer and this attempted
6684 	 *    entry is part of the current operation
6685 	 * 3. Enter the ipsq if there is no current writer and this is a new
6686 	 *    operation and the operation queue is empty and there is no
6687 	 *    operation currently in progress and if all previously initiated
6688 	 *    DLPI operations have completed.
6689 	 */
6690 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6691 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6692 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6693 	    ipsq_dlpi_done(ipsq))))) {
6694 		/* Success. */
6695 		ipx->ipx_reentry_cnt++;
6696 		ipx->ipx_writer = curthread;
6697 		ipx->ipx_forced = B_FALSE;
6698 		mutex_exit(&ipx->ipx_lock);
6699 		mutex_exit(&ipsq->ipsq_lock);
6700 		mutex_exit(&ill->ill_lock);
6701 		RELEASE_CONN_LOCK(q);
6702 #ifdef DEBUG
6703 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6704 #endif
6705 		return (ipsq);
6706 	}
6707 
6708 	if (func != NULL)
6709 		ipsq_enq(ipsq, q, mp, func, type, ill);
6710 
6711 	mutex_exit(&ipx->ipx_lock);
6712 	mutex_exit(&ipsq->ipsq_lock);
6713 	mutex_exit(&ill->ill_lock);
6714 	RELEASE_CONN_LOCK(q);
6715 	return (NULL);
6716 }
6717 
6718 /*
6719  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6720  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6721  * There is one ipsq per phyint. The ipsq
6722  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6723  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6724  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6725  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6726  * up the interface) and are enqueued in ipx_mphead.
6727  *
6728  * If a thread does not want to reenter the ipsq when it is already writer,
6729  * it must make sure that the specified reentry point to be called later
6730  * when the ipsq is empty, nor any code path starting from the specified reentry
6731  * point must never ever try to enter the ipsq again. Otherwise it can lead
6732  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6733  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6734  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6735  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6736  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6737  * ioctl if the current ioctl has completed. If the current ioctl is still
6738  * in progress it simply returns. The current ioctl could be waiting for
6739  * a response from another module (the driver or could be waiting for
6740  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6741  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6742  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6743  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6744  * all associated DLPI operations have completed.
6745  */
6746 
6747 /*
6748  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6749  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6750  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6751  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6752  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6753  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6754  */
6755 ipsq_t *
6756 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6757     ipsq_func_t func, int type, boolean_t reentry_ok)
6758 {
6759 	ip_stack_t	*ipst;
6760 	ipsq_t		*ipsq;
6761 
6762 	/* Only 1 of ipif or ill can be specified */
6763 	ASSERT((ipif != NULL) ^ (ill != NULL));
6764 
6765 	if (ipif != NULL)
6766 		ill = ipif->ipif_ill;
6767 	ipst = ill->ill_ipst;
6768 
6769 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6770 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6771 	rw_exit(&ipst->ips_ill_g_lock);
6772 
6773 	return (ipsq);
6774 }
6775 
6776 /*
6777  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6778  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6779  * cannot be entered, the mp is queued for completion.
6780  */
6781 void
6782 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6783     boolean_t reentry_ok)
6784 {
6785 	ipsq_t	*ipsq;
6786 
6787 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6788 
6789 	/*
6790 	 * Drop the caller's refhold on the ill.  This is safe since we either
6791 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6792 	 * IPSQ, in which case we return without accessing ill anymore.  This
6793 	 * is needed because func needs to see the correct refcount.
6794 	 * e.g. removeif can work only then.
6795 	 */
6796 	ill_refrele(ill);
6797 	if (ipsq != NULL) {
6798 		(*func)(ipsq, q, mp, NULL);
6799 		ipsq_exit(ipsq);
6800 	}
6801 }
6802 
6803 /*
6804  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6805  * prior to exiting.  Caller must be writer on the specified IPSQ.
6806  */
6807 void
6808 ipsq_exit(ipsq_t *ipsq)
6809 {
6810 	mblk_t *mp;
6811 	ipsq_t *mp_ipsq;
6812 	queue_t	*q;
6813 	phyint_t *phyi;
6814 	ipsq_func_t func;
6815 
6816 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6817 
6818 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6819 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6820 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6821 		return;
6822 	}
6823 
6824 	for (;;) {
6825 		phyi = ipsq->ipsq_phyint;
6826 		mp = ipsq_dq(ipsq);
6827 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6828 
6829 		/*
6830 		 * If we've changed to a new IPSQ, and the phyint associated
6831 		 * with the old one has gone away, free the old IPSQ.  Note
6832 		 * that this cannot happen while the IPSQ is in a group.
6833 		 */
6834 		if (mp_ipsq != ipsq && phyi == NULL) {
6835 			ASSERT(ipsq->ipsq_next == ipsq);
6836 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6837 			ipsq_delete(ipsq);
6838 		}
6839 
6840 		if (mp == NULL)
6841 			break;
6842 
6843 		q = mp->b_queue;
6844 		func = (ipsq_func_t)mp->b_prev;
6845 		ipsq = mp_ipsq;
6846 		mp->b_next = mp->b_prev = NULL;
6847 		mp->b_queue = NULL;
6848 
6849 		/*
6850 		 * If 'q' is an conn queue, it is valid, since we did a
6851 		 * a refhold on the conn at the start of the ioctl.
6852 		 * If 'q' is an ill queue, it is valid, since close of an
6853 		 * ill will clean up its IPSQ.
6854 		 */
6855 		(*func)(ipsq, q, mp, NULL);
6856 	}
6857 }
6858 
6859 /*
6860  * Used to start any igmp or mld timers that could not be started
6861  * while holding ill_mcast_lock. The timers can't be started while holding
6862  * the lock, since mld/igmp_start_timers may need to call untimeout()
6863  * which can't be done while holding the lock which the timeout handler
6864  * acquires. Otherwise
6865  * there could be a deadlock since the timeout handlers
6866  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6867  * ill_mcast_lock.
6868  */
6869 void
6870 ill_mcast_timer_start(ip_stack_t *ipst)
6871 {
6872 	int		next;
6873 
6874 	mutex_enter(&ipst->ips_igmp_timer_lock);
6875 	next = ipst->ips_igmp_deferred_next;
6876 	ipst->ips_igmp_deferred_next = INFINITY;
6877 	mutex_exit(&ipst->ips_igmp_timer_lock);
6878 
6879 	if (next != INFINITY)
6880 		igmp_start_timers(next, ipst);
6881 
6882 	mutex_enter(&ipst->ips_mld_timer_lock);
6883 	next = ipst->ips_mld_deferred_next;
6884 	ipst->ips_mld_deferred_next = INFINITY;
6885 	mutex_exit(&ipst->ips_mld_timer_lock);
6886 
6887 	if (next != INFINITY)
6888 		mld_start_timers(next, ipst);
6889 }
6890 
6891 /*
6892  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6893  * and `ioccmd'.
6894  */
6895 void
6896 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6897 {
6898 	ill_t *ill = ipif->ipif_ill;
6899 	ipxop_t *ipx = ipsq->ipsq_xop;
6900 
6901 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6902 	ASSERT(ipx->ipx_current_ipif == NULL);
6903 	ASSERT(ipx->ipx_current_ioctl == 0);
6904 
6905 	ipx->ipx_current_done = B_FALSE;
6906 	ipx->ipx_current_ioctl = ioccmd;
6907 	mutex_enter(&ipx->ipx_lock);
6908 	ipx->ipx_current_ipif = ipif;
6909 	mutex_exit(&ipx->ipx_lock);
6910 
6911 	/*
6912 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6913 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6914 	 * references to the ipif (so that the references will eventually
6915 	 * drop to zero) and also prevents any "get" operations (e.g.,
6916 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6917 	 * operation has completed and the ipif is again in a stable state.
6918 	 *
6919 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6920 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6921 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6922 	 * ipifs will be affected.
6923 	 *
6924 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6925 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6926 	 * operate on.
6927 	 */
6928 	switch (ioccmd) {
6929 	case SIOCLIFREMOVEIF:
6930 		break;
6931 	case 0:
6932 		mutex_enter(&ill->ill_lock);
6933 		ipif = ipif->ipif_ill->ill_ipif;
6934 		for (; ipif != NULL; ipif = ipif->ipif_next)
6935 			ipif->ipif_state_flags |= IPIF_CHANGING;
6936 		mutex_exit(&ill->ill_lock);
6937 		break;
6938 	default:
6939 		mutex_enter(&ill->ill_lock);
6940 		ipif->ipif_state_flags |= IPIF_CHANGING;
6941 		mutex_exit(&ill->ill_lock);
6942 	}
6943 }
6944 
6945 /*
6946  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6947  * the next exclusive operation to begin once we ipsq_exit().  However, if
6948  * pending DLPI operations remain, then we will wait for the queue to drain
6949  * before allowing the next exclusive operation to begin.  This ensures that
6950  * DLPI operations from one exclusive operation are never improperly processed
6951  * as part of a subsequent exclusive operation.
6952  */
6953 void
6954 ipsq_current_finish(ipsq_t *ipsq)
6955 {
6956 	ipxop_t	*ipx = ipsq->ipsq_xop;
6957 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6958 	ipif_t	*ipif = ipx->ipx_current_ipif;
6959 
6960 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6961 
6962 	/*
6963 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6964 	 * (but in that case, IPIF_CHANGING will already be clear and no
6965 	 * pending DLPI messages can remain).
6966 	 */
6967 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6968 		ill_t *ill = ipif->ipif_ill;
6969 
6970 		mutex_enter(&ill->ill_lock);
6971 		dlpi_pending = ill->ill_dlpi_pending;
6972 		if (ipx->ipx_current_ioctl == 0) {
6973 			ipif = ill->ill_ipif;
6974 			for (; ipif != NULL; ipif = ipif->ipif_next)
6975 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6976 		} else {
6977 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6978 		}
6979 		mutex_exit(&ill->ill_lock);
6980 	}
6981 
6982 	ASSERT(!ipx->ipx_current_done);
6983 	ipx->ipx_current_done = B_TRUE;
6984 	ipx->ipx_current_ioctl = 0;
6985 	if (dlpi_pending == DL_PRIM_INVAL) {
6986 		mutex_enter(&ipx->ipx_lock);
6987 		ipx->ipx_current_ipif = NULL;
6988 		mutex_exit(&ipx->ipx_lock);
6989 	}
6990 }
6991 
6992 /*
6993  * The ill is closing. Flush all messages on the ipsq that originated
6994  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6995  * for this ill since ipsq_enter could not have entered until then.
6996  * New messages can't be queued since the CONDEMNED flag is set.
6997  */
6998 static void
6999 ipsq_flush(ill_t *ill)
7000 {
7001 	queue_t	*q;
7002 	mblk_t	*prev;
7003 	mblk_t	*mp;
7004 	mblk_t	*mp_next;
7005 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7006 
7007 	ASSERT(IAM_WRITER_ILL(ill));
7008 
7009 	/*
7010 	 * Flush any messages sent up by the driver.
7011 	 */
7012 	mutex_enter(&ipx->ipx_lock);
7013 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7014 		mp_next = mp->b_next;
7015 		q = mp->b_queue;
7016 		if (q == ill->ill_rq || q == ill->ill_wq) {
7017 			/* dequeue mp */
7018 			if (prev == NULL)
7019 				ipx->ipx_mphead = mp->b_next;
7020 			else
7021 				prev->b_next = mp->b_next;
7022 			if (ipx->ipx_mptail == mp) {
7023 				ASSERT(mp_next == NULL);
7024 				ipx->ipx_mptail = prev;
7025 			}
7026 			inet_freemsg(mp);
7027 		} else {
7028 			prev = mp;
7029 		}
7030 	}
7031 	mutex_exit(&ipx->ipx_lock);
7032 	(void) ipsq_pending_mp_cleanup(ill, NULL);
7033 	ipsq_xopq_mp_cleanup(ill, NULL);
7034 }
7035 
7036 /*
7037  * Parse an ifreq or lifreq struct coming down ioctls and refhold
7038  * and return the associated ipif.
7039  * Return value:
7040  *	Non zero: An error has occurred. ci may not be filled out.
7041  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7042  *	a held ipif in ci.ci_ipif.
7043  */
7044 int
7045 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7046     cmd_info_t *ci)
7047 {
7048 	char		*name;
7049 	struct ifreq    *ifr;
7050 	struct lifreq    *lifr;
7051 	ipif_t		*ipif = NULL;
7052 	ill_t		*ill;
7053 	conn_t		*connp;
7054 	boolean_t	isv6;
7055 	int		err;
7056 	mblk_t		*mp1;
7057 	zoneid_t	zoneid;
7058 	ip_stack_t	*ipst;
7059 
7060 	if (q->q_next != NULL) {
7061 		ill = (ill_t *)q->q_ptr;
7062 		isv6 = ill->ill_isv6;
7063 		connp = NULL;
7064 		zoneid = ALL_ZONES;
7065 		ipst = ill->ill_ipst;
7066 	} else {
7067 		ill = NULL;
7068 		connp = Q_TO_CONN(q);
7069 		isv6 = (connp->conn_family == AF_INET6);
7070 		zoneid = connp->conn_zoneid;
7071 		if (zoneid == GLOBAL_ZONEID) {
7072 			/* global zone can access ipifs in all zones */
7073 			zoneid = ALL_ZONES;
7074 		}
7075 		ipst = connp->conn_netstack->netstack_ip;
7076 	}
7077 
7078 	/* Has been checked in ip_wput_nondata */
7079 	mp1 = mp->b_cont->b_cont;
7080 
7081 	if (ipip->ipi_cmd_type == IF_CMD) {
7082 		/* This a old style SIOC[GS]IF* command */
7083 		ifr = (struct ifreq *)mp1->b_rptr;
7084 		/*
7085 		 * Null terminate the string to protect against buffer
7086 		 * overrun. String was generated by user code and may not
7087 		 * be trusted.
7088 		 */
7089 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7090 		name = ifr->ifr_name;
7091 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7092 		ci->ci_sin6 = NULL;
7093 		ci->ci_lifr = (struct lifreq *)ifr;
7094 	} else {
7095 		/* This a new style SIOC[GS]LIF* command */
7096 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7097 		lifr = (struct lifreq *)mp1->b_rptr;
7098 		/*
7099 		 * Null terminate the string to protect against buffer
7100 		 * overrun. String was generated by user code and may not
7101 		 * be trusted.
7102 		 */
7103 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7104 		name = lifr->lifr_name;
7105 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7106 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7107 		ci->ci_lifr = lifr;
7108 	}
7109 
7110 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7111 		/*
7112 		 * The ioctl will be failed if the ioctl comes down
7113 		 * an conn stream
7114 		 */
7115 		if (ill == NULL) {
7116 			/*
7117 			 * Not an ill queue, return EINVAL same as the
7118 			 * old error code.
7119 			 */
7120 			return (ENXIO);
7121 		}
7122 		ipif = ill->ill_ipif;
7123 		ipif_refhold(ipif);
7124 	} else {
7125 		/*
7126 		 * Ensure that ioctls don't see any internal state changes
7127 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7128 		 * set.
7129 		 */
7130 		ipif = ipif_lookup_on_name_async(name, mi_strlen(name),
7131 		    isv6, zoneid, q, mp, ip_process_ioctl, &err, ipst);
7132 		if (ipif == NULL) {
7133 			if (err == EINPROGRESS)
7134 				return (err);
7135 			err = 0;	/* Ensure we don't use it below */
7136 		}
7137 	}
7138 
7139 	/*
7140 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7141 	 */
7142 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7143 		ipif_refrele(ipif);
7144 		return (ENXIO);
7145 	}
7146 
7147 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7148 	    name[0] == '\0') {
7149 		/*
7150 		 * Handle a or a SIOC?IF* with a null name
7151 		 * during plumb (on the ill queue before the I_PLINK).
7152 		 */
7153 		ipif = ill->ill_ipif;
7154 		ipif_refhold(ipif);
7155 	}
7156 
7157 	if (ipif == NULL)
7158 		return (ENXIO);
7159 
7160 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7161 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7162 
7163 	ci->ci_ipif = ipif;
7164 	return (0);
7165 }
7166 
7167 /*
7168  * Return the total number of ipifs.
7169  */
7170 static uint_t
7171 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7172 {
7173 	uint_t numifs = 0;
7174 	ill_t	*ill;
7175 	ill_walk_context_t	ctx;
7176 	ipif_t	*ipif;
7177 
7178 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7179 	ill = ILL_START_WALK_V4(&ctx, ipst);
7180 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7181 		if (IS_UNDER_IPMP(ill))
7182 			continue;
7183 		for (ipif = ill->ill_ipif; ipif != NULL;
7184 		    ipif = ipif->ipif_next) {
7185 			if (ipif->ipif_zoneid == zoneid ||
7186 			    ipif->ipif_zoneid == ALL_ZONES)
7187 				numifs++;
7188 		}
7189 	}
7190 	rw_exit(&ipst->ips_ill_g_lock);
7191 	return (numifs);
7192 }
7193 
7194 /*
7195  * Return the total number of ipifs.
7196  */
7197 static uint_t
7198 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7199 {
7200 	uint_t numifs = 0;
7201 	ill_t	*ill;
7202 	ipif_t	*ipif;
7203 	ill_walk_context_t	ctx;
7204 
7205 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7206 
7207 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7208 	if (family == AF_INET)
7209 		ill = ILL_START_WALK_V4(&ctx, ipst);
7210 	else if (family == AF_INET6)
7211 		ill = ILL_START_WALK_V6(&ctx, ipst);
7212 	else
7213 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7214 
7215 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7216 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7217 			continue;
7218 
7219 		for (ipif = ill->ill_ipif; ipif != NULL;
7220 		    ipif = ipif->ipif_next) {
7221 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7222 			    !(lifn_flags & LIFC_NOXMIT))
7223 				continue;
7224 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7225 			    !(lifn_flags & LIFC_TEMPORARY))
7226 				continue;
7227 			if (((ipif->ipif_flags &
7228 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7229 			    IPIF_DEPRECATED)) ||
7230 			    IS_LOOPBACK(ill) ||
7231 			    !(ipif->ipif_flags & IPIF_UP)) &&
7232 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7233 				continue;
7234 
7235 			if (zoneid != ipif->ipif_zoneid &&
7236 			    ipif->ipif_zoneid != ALL_ZONES &&
7237 			    (zoneid != GLOBAL_ZONEID ||
7238 			    !(lifn_flags & LIFC_ALLZONES)))
7239 				continue;
7240 
7241 			numifs++;
7242 		}
7243 	}
7244 	rw_exit(&ipst->ips_ill_g_lock);
7245 	return (numifs);
7246 }
7247 
7248 uint_t
7249 ip_get_lifsrcofnum(ill_t *ill)
7250 {
7251 	uint_t numifs = 0;
7252 	ill_t	*ill_head = ill;
7253 	ip_stack_t	*ipst = ill->ill_ipst;
7254 
7255 	/*
7256 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7257 	 * other thread may be trying to relink the ILLs in this usesrc group
7258 	 * and adjusting the ill_usesrc_grp_next pointers
7259 	 */
7260 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7261 	if ((ill->ill_usesrc_ifindex == 0) &&
7262 	    (ill->ill_usesrc_grp_next != NULL)) {
7263 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7264 		    ill = ill->ill_usesrc_grp_next)
7265 			numifs++;
7266 	}
7267 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7268 
7269 	return (numifs);
7270 }
7271 
7272 /* Null values are passed in for ipif, sin, and ifreq */
7273 /* ARGSUSED */
7274 int
7275 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7276     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7277 {
7278 	int *nump;
7279 	conn_t *connp = Q_TO_CONN(q);
7280 
7281 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7282 
7283 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7284 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7285 
7286 	*nump = ip_get_numifs(connp->conn_zoneid,
7287 	    connp->conn_netstack->netstack_ip);
7288 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7289 	return (0);
7290 }
7291 
7292 /* Null values are passed in for ipif, sin, and ifreq */
7293 /* ARGSUSED */
7294 int
7295 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7296     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7297 {
7298 	struct lifnum *lifn;
7299 	mblk_t	*mp1;
7300 	conn_t *connp = Q_TO_CONN(q);
7301 
7302 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7303 
7304 	/* Existence checked in ip_wput_nondata */
7305 	mp1 = mp->b_cont->b_cont;
7306 
7307 	lifn = (struct lifnum *)mp1->b_rptr;
7308 	switch (lifn->lifn_family) {
7309 	case AF_UNSPEC:
7310 	case AF_INET:
7311 	case AF_INET6:
7312 		break;
7313 	default:
7314 		return (EAFNOSUPPORT);
7315 	}
7316 
7317 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7318 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7319 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7320 	return (0);
7321 }
7322 
7323 /* ARGSUSED */
7324 int
7325 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7326     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7327 {
7328 	STRUCT_HANDLE(ifconf, ifc);
7329 	mblk_t *mp1;
7330 	struct iocblk *iocp;
7331 	struct ifreq *ifr;
7332 	ill_walk_context_t	ctx;
7333 	ill_t	*ill;
7334 	ipif_t	*ipif;
7335 	struct sockaddr_in *sin;
7336 	int32_t	ifclen;
7337 	zoneid_t zoneid;
7338 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7339 
7340 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7341 
7342 	ip1dbg(("ip_sioctl_get_ifconf"));
7343 	/* Existence verified in ip_wput_nondata */
7344 	mp1 = mp->b_cont->b_cont;
7345 	iocp = (struct iocblk *)mp->b_rptr;
7346 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7347 
7348 	/*
7349 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7350 	 * the user buffer address and length into which the list of struct
7351 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7352 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7353 	 * the SIOCGIFCONF operation was redefined to simply provide
7354 	 * a large output buffer into which we are supposed to jam the ifreq
7355 	 * array.  The same ioctl command code was used, despite the fact that
7356 	 * both the applications and the kernel code had to change, thus making
7357 	 * it impossible to support both interfaces.
7358 	 *
7359 	 * For reasons not good enough to try to explain, the following
7360 	 * algorithm is used for deciding what to do with one of these:
7361 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7362 	 * form with the output buffer coming down as the continuation message.
7363 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7364 	 * and we have to copy in the ifconf structure to find out how big the
7365 	 * output buffer is and where to copy out to.  Sure no problem...
7366 	 *
7367 	 */
7368 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7369 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7370 		int numifs = 0;
7371 		size_t ifc_bufsize;
7372 
7373 		/*
7374 		 * Must be (better be!) continuation of a TRANSPARENT
7375 		 * IOCTL.  We just copied in the ifconf structure.
7376 		 */
7377 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7378 		    (struct ifconf *)mp1->b_rptr);
7379 
7380 		/*
7381 		 * Allocate a buffer to hold requested information.
7382 		 *
7383 		 * If ifc_len is larger than what is needed, we only
7384 		 * allocate what we will use.
7385 		 *
7386 		 * If ifc_len is smaller than what is needed, return
7387 		 * EINVAL.
7388 		 *
7389 		 * XXX: the ill_t structure can hava 2 counters, for
7390 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7391 		 * number of interfaces for a device, so we don't need
7392 		 * to count them here...
7393 		 */
7394 		numifs = ip_get_numifs(zoneid, ipst);
7395 
7396 		ifclen = STRUCT_FGET(ifc, ifc_len);
7397 		ifc_bufsize = numifs * sizeof (struct ifreq);
7398 		if (ifc_bufsize > ifclen) {
7399 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7400 				/* old behaviour */
7401 				return (EINVAL);
7402 			} else {
7403 				ifc_bufsize = ifclen;
7404 			}
7405 		}
7406 
7407 		mp1 = mi_copyout_alloc(q, mp,
7408 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7409 		if (mp1 == NULL)
7410 			return (ENOMEM);
7411 
7412 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7413 	}
7414 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7415 	/*
7416 	 * the SIOCGIFCONF ioctl only knows about
7417 	 * IPv4 addresses, so don't try to tell
7418 	 * it about interfaces with IPv6-only
7419 	 * addresses. (Last parm 'isv6' is B_FALSE)
7420 	 */
7421 
7422 	ifr = (struct ifreq *)mp1->b_rptr;
7423 
7424 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7425 	ill = ILL_START_WALK_V4(&ctx, ipst);
7426 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7427 		if (IS_UNDER_IPMP(ill))
7428 			continue;
7429 		for (ipif = ill->ill_ipif; ipif != NULL;
7430 		    ipif = ipif->ipif_next) {
7431 			if (zoneid != ipif->ipif_zoneid &&
7432 			    ipif->ipif_zoneid != ALL_ZONES)
7433 				continue;
7434 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7435 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7436 					/* old behaviour */
7437 					rw_exit(&ipst->ips_ill_g_lock);
7438 					return (EINVAL);
7439 				} else {
7440 					goto if_copydone;
7441 				}
7442 			}
7443 			ipif_get_name(ipif, ifr->ifr_name,
7444 			    sizeof (ifr->ifr_name));
7445 			sin = (sin_t *)&ifr->ifr_addr;
7446 			*sin = sin_null;
7447 			sin->sin_family = AF_INET;
7448 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7449 			ifr++;
7450 		}
7451 	}
7452 if_copydone:
7453 	rw_exit(&ipst->ips_ill_g_lock);
7454 	mp1->b_wptr = (uchar_t *)ifr;
7455 
7456 	if (STRUCT_BUF(ifc) != NULL) {
7457 		STRUCT_FSET(ifc, ifc_len,
7458 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7459 	}
7460 	return (0);
7461 }
7462 
7463 /*
7464  * Get the interfaces using the address hosted on the interface passed in,
7465  * as a source adddress
7466  */
7467 /* ARGSUSED */
7468 int
7469 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7470     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7471 {
7472 	mblk_t *mp1;
7473 	ill_t	*ill, *ill_head;
7474 	ipif_t	*ipif, *orig_ipif;
7475 	int	numlifs = 0;
7476 	size_t	lifs_bufsize, lifsmaxlen;
7477 	struct	lifreq *lifr;
7478 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7479 	uint_t	ifindex;
7480 	zoneid_t zoneid;
7481 	boolean_t isv6 = B_FALSE;
7482 	struct	sockaddr_in	*sin;
7483 	struct	sockaddr_in6	*sin6;
7484 	STRUCT_HANDLE(lifsrcof, lifs);
7485 	ip_stack_t		*ipst;
7486 
7487 	ipst = CONNQ_TO_IPST(q);
7488 
7489 	ASSERT(q->q_next == NULL);
7490 
7491 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7492 
7493 	/* Existence verified in ip_wput_nondata */
7494 	mp1 = mp->b_cont->b_cont;
7495 
7496 	/*
7497 	 * Must be (better be!) continuation of a TRANSPARENT
7498 	 * IOCTL.  We just copied in the lifsrcof structure.
7499 	 */
7500 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7501 	    (struct lifsrcof *)mp1->b_rptr);
7502 
7503 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7504 		return (EINVAL);
7505 
7506 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7507 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7508 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7509 	if (ipif == NULL) {
7510 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7511 		    ifindex));
7512 		return (ENXIO);
7513 	}
7514 
7515 	/* Allocate a buffer to hold requested information */
7516 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7517 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7518 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7519 	/* The actual size needed is always returned in lifs_len */
7520 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7521 
7522 	/* If the amount we need is more than what is passed in, abort */
7523 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7524 		ipif_refrele(ipif);
7525 		return (0);
7526 	}
7527 
7528 	mp1 = mi_copyout_alloc(q, mp,
7529 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7530 	if (mp1 == NULL) {
7531 		ipif_refrele(ipif);
7532 		return (ENOMEM);
7533 	}
7534 
7535 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7536 	bzero(mp1->b_rptr, lifs_bufsize);
7537 
7538 	lifr = (struct lifreq *)mp1->b_rptr;
7539 
7540 	ill = ill_head = ipif->ipif_ill;
7541 	orig_ipif = ipif;
7542 
7543 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7544 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7545 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7546 
7547 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7548 	for (; (ill != NULL) && (ill != ill_head);
7549 	    ill = ill->ill_usesrc_grp_next) {
7550 
7551 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7552 			break;
7553 
7554 		ipif = ill->ill_ipif;
7555 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7556 		if (ipif->ipif_isv6) {
7557 			sin6 = (sin6_t *)&lifr->lifr_addr;
7558 			*sin6 = sin6_null;
7559 			sin6->sin6_family = AF_INET6;
7560 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7561 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7562 			    &ipif->ipif_v6net_mask);
7563 		} else {
7564 			sin = (sin_t *)&lifr->lifr_addr;
7565 			*sin = sin_null;
7566 			sin->sin_family = AF_INET;
7567 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7568 			lifr->lifr_addrlen = ip_mask_to_plen(
7569 			    ipif->ipif_net_mask);
7570 		}
7571 		lifr++;
7572 	}
7573 	rw_exit(&ipst->ips_ill_g_lock);
7574 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7575 	ipif_refrele(orig_ipif);
7576 	mp1->b_wptr = (uchar_t *)lifr;
7577 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7578 
7579 	return (0);
7580 }
7581 
7582 /* ARGSUSED */
7583 int
7584 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7585     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7586 {
7587 	mblk_t *mp1;
7588 	int	list;
7589 	ill_t	*ill;
7590 	ipif_t	*ipif;
7591 	int	flags;
7592 	int	numlifs = 0;
7593 	size_t	lifc_bufsize;
7594 	struct	lifreq *lifr;
7595 	sa_family_t	family;
7596 	struct	sockaddr_in	*sin;
7597 	struct	sockaddr_in6	*sin6;
7598 	ill_walk_context_t	ctx;
7599 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7600 	int32_t	lifclen;
7601 	zoneid_t zoneid;
7602 	STRUCT_HANDLE(lifconf, lifc);
7603 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7604 
7605 	ip1dbg(("ip_sioctl_get_lifconf"));
7606 
7607 	ASSERT(q->q_next == NULL);
7608 
7609 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7610 
7611 	/* Existence verified in ip_wput_nondata */
7612 	mp1 = mp->b_cont->b_cont;
7613 
7614 	/*
7615 	 * An extended version of SIOCGIFCONF that takes an
7616 	 * additional address family and flags field.
7617 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7618 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7619 	 * interfaces are omitted.
7620 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7621 	 * unless LIFC_TEMPORARY is specified.
7622 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7623 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7624 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7625 	 * has priority over LIFC_NOXMIT.
7626 	 */
7627 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7628 
7629 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7630 		return (EINVAL);
7631 
7632 	/*
7633 	 * Must be (better be!) continuation of a TRANSPARENT
7634 	 * IOCTL.  We just copied in the lifconf structure.
7635 	 */
7636 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7637 
7638 	family = STRUCT_FGET(lifc, lifc_family);
7639 	flags = STRUCT_FGET(lifc, lifc_flags);
7640 
7641 	switch (family) {
7642 	case AF_UNSPEC:
7643 		/*
7644 		 * walk all ILL's.
7645 		 */
7646 		list = MAX_G_HEADS;
7647 		break;
7648 	case AF_INET:
7649 		/*
7650 		 * walk only IPV4 ILL's.
7651 		 */
7652 		list = IP_V4_G_HEAD;
7653 		break;
7654 	case AF_INET6:
7655 		/*
7656 		 * walk only IPV6 ILL's.
7657 		 */
7658 		list = IP_V6_G_HEAD;
7659 		break;
7660 	default:
7661 		return (EAFNOSUPPORT);
7662 	}
7663 
7664 	/*
7665 	 * Allocate a buffer to hold requested information.
7666 	 *
7667 	 * If lifc_len is larger than what is needed, we only
7668 	 * allocate what we will use.
7669 	 *
7670 	 * If lifc_len is smaller than what is needed, return
7671 	 * EINVAL.
7672 	 */
7673 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7674 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7675 	lifclen = STRUCT_FGET(lifc, lifc_len);
7676 	if (lifc_bufsize > lifclen) {
7677 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7678 			return (EINVAL);
7679 		else
7680 			lifc_bufsize = lifclen;
7681 	}
7682 
7683 	mp1 = mi_copyout_alloc(q, mp,
7684 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7685 	if (mp1 == NULL)
7686 		return (ENOMEM);
7687 
7688 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7689 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7690 
7691 	lifr = (struct lifreq *)mp1->b_rptr;
7692 
7693 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7694 	ill = ill_first(list, list, &ctx, ipst);
7695 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7696 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7697 			continue;
7698 
7699 		for (ipif = ill->ill_ipif; ipif != NULL;
7700 		    ipif = ipif->ipif_next) {
7701 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7702 			    !(flags & LIFC_NOXMIT))
7703 				continue;
7704 
7705 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7706 			    !(flags & LIFC_TEMPORARY))
7707 				continue;
7708 
7709 			if (((ipif->ipif_flags &
7710 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7711 			    IPIF_DEPRECATED)) ||
7712 			    IS_LOOPBACK(ill) ||
7713 			    !(ipif->ipif_flags & IPIF_UP)) &&
7714 			    (flags & LIFC_EXTERNAL_SOURCE))
7715 				continue;
7716 
7717 			if (zoneid != ipif->ipif_zoneid &&
7718 			    ipif->ipif_zoneid != ALL_ZONES &&
7719 			    (zoneid != GLOBAL_ZONEID ||
7720 			    !(flags & LIFC_ALLZONES)))
7721 				continue;
7722 
7723 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7724 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7725 					rw_exit(&ipst->ips_ill_g_lock);
7726 					return (EINVAL);
7727 				} else {
7728 					goto lif_copydone;
7729 				}
7730 			}
7731 
7732 			ipif_get_name(ipif, lifr->lifr_name,
7733 			    sizeof (lifr->lifr_name));
7734 			lifr->lifr_type = ill->ill_type;
7735 			if (ipif->ipif_isv6) {
7736 				sin6 = (sin6_t *)&lifr->lifr_addr;
7737 				*sin6 = sin6_null;
7738 				sin6->sin6_family = AF_INET6;
7739 				sin6->sin6_addr =
7740 				    ipif->ipif_v6lcl_addr;
7741 				lifr->lifr_addrlen =
7742 				    ip_mask_to_plen_v6(
7743 				    &ipif->ipif_v6net_mask);
7744 			} else {
7745 				sin = (sin_t *)&lifr->lifr_addr;
7746 				*sin = sin_null;
7747 				sin->sin_family = AF_INET;
7748 				sin->sin_addr.s_addr =
7749 				    ipif->ipif_lcl_addr;
7750 				lifr->lifr_addrlen =
7751 				    ip_mask_to_plen(
7752 				    ipif->ipif_net_mask);
7753 			}
7754 			lifr++;
7755 		}
7756 	}
7757 lif_copydone:
7758 	rw_exit(&ipst->ips_ill_g_lock);
7759 
7760 	mp1->b_wptr = (uchar_t *)lifr;
7761 	if (STRUCT_BUF(lifc) != NULL) {
7762 		STRUCT_FSET(lifc, lifc_len,
7763 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7764 	}
7765 	return (0);
7766 }
7767 
7768 static void
7769 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7770 {
7771 	ip6_asp_t *table;
7772 	size_t table_size;
7773 	mblk_t *data_mp;
7774 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7775 	ip_stack_t	*ipst;
7776 
7777 	if (q->q_next == NULL)
7778 		ipst = CONNQ_TO_IPST(q);
7779 	else
7780 		ipst = ILLQ_TO_IPST(q);
7781 
7782 	/* These two ioctls are I_STR only */
7783 	if (iocp->ioc_count == TRANSPARENT) {
7784 		miocnak(q, mp, 0, EINVAL);
7785 		return;
7786 	}
7787 
7788 	data_mp = mp->b_cont;
7789 	if (data_mp == NULL) {
7790 		/* The user passed us a NULL argument */
7791 		table = NULL;
7792 		table_size = iocp->ioc_count;
7793 	} else {
7794 		/*
7795 		 * The user provided a table.  The stream head
7796 		 * may have copied in the user data in chunks,
7797 		 * so make sure everything is pulled up
7798 		 * properly.
7799 		 */
7800 		if (MBLKL(data_mp) < iocp->ioc_count) {
7801 			mblk_t *new_data_mp;
7802 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7803 			    NULL) {
7804 				miocnak(q, mp, 0, ENOMEM);
7805 				return;
7806 			}
7807 			freemsg(data_mp);
7808 			data_mp = new_data_mp;
7809 			mp->b_cont = data_mp;
7810 		}
7811 		table = (ip6_asp_t *)data_mp->b_rptr;
7812 		table_size = iocp->ioc_count;
7813 	}
7814 
7815 	switch (iocp->ioc_cmd) {
7816 	case SIOCGIP6ADDRPOLICY:
7817 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7818 		if (iocp->ioc_rval == -1)
7819 			iocp->ioc_error = EINVAL;
7820 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7821 		else if (table != NULL &&
7822 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7823 			ip6_asp_t *src = table;
7824 			ip6_asp32_t *dst = (void *)table;
7825 			int count = table_size / sizeof (ip6_asp_t);
7826 			int i;
7827 
7828 			/*
7829 			 * We need to do an in-place shrink of the array
7830 			 * to match the alignment attributes of the
7831 			 * 32-bit ABI looking at it.
7832 			 */
7833 			/* LINTED: logical expression always true: op "||" */
7834 			ASSERT(sizeof (*src) > sizeof (*dst));
7835 			for (i = 1; i < count; i++)
7836 				bcopy(src + i, dst + i, sizeof (*dst));
7837 		}
7838 #endif
7839 		break;
7840 
7841 	case SIOCSIP6ADDRPOLICY:
7842 		ASSERT(mp->b_prev == NULL);
7843 		mp->b_prev = (void *)q;
7844 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7845 		/*
7846 		 * We pass in the datamodel here so that the ip6_asp_replace()
7847 		 * routine can handle converting from 32-bit to native formats
7848 		 * where necessary.
7849 		 *
7850 		 * A better way to handle this might be to convert the inbound
7851 		 * data structure here, and hang it off a new 'mp'; thus the
7852 		 * ip6_asp_replace() logic would always be dealing with native
7853 		 * format data structures..
7854 		 *
7855 		 * (An even simpler way to handle these ioctls is to just
7856 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7857 		 * and just recompile everything that depends on it.)
7858 		 */
7859 #endif
7860 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7861 		    iocp->ioc_flag & IOC_MODELS);
7862 		return;
7863 	}
7864 
7865 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7866 	qreply(q, mp);
7867 }
7868 
7869 static void
7870 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7871 {
7872 	mblk_t		*data_mp;
7873 	struct dstinforeq	*dir;
7874 	uint8_t		*end, *cur;
7875 	in6_addr_t	*daddr, *saddr;
7876 	ipaddr_t	v4daddr;
7877 	ire_t		*ire;
7878 	ipaddr_t	v4setsrc;
7879 	in6_addr_t	v6setsrc;
7880 	char		*slabel, *dlabel;
7881 	boolean_t	isipv4;
7882 	int		match_ire;
7883 	ill_t		*dst_ill;
7884 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7885 	conn_t		*connp = Q_TO_CONN(q);
7886 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7887 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7888 	uint64_t	ipif_flags;
7889 
7890 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7891 
7892 	/*
7893 	 * This ioctl is I_STR only, and must have a
7894 	 * data mblk following the M_IOCTL mblk.
7895 	 */
7896 	data_mp = mp->b_cont;
7897 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7898 		miocnak(q, mp, 0, EINVAL);
7899 		return;
7900 	}
7901 
7902 	if (MBLKL(data_mp) < iocp->ioc_count) {
7903 		mblk_t *new_data_mp;
7904 
7905 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7906 			miocnak(q, mp, 0, ENOMEM);
7907 			return;
7908 		}
7909 		freemsg(data_mp);
7910 		data_mp = new_data_mp;
7911 		mp->b_cont = data_mp;
7912 	}
7913 	match_ire = MATCH_IRE_DSTONLY;
7914 
7915 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7916 	    end - cur >= sizeof (struct dstinforeq);
7917 	    cur += sizeof (struct dstinforeq)) {
7918 		dir = (struct dstinforeq *)cur;
7919 		daddr = &dir->dir_daddr;
7920 		saddr = &dir->dir_saddr;
7921 
7922 		/*
7923 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7924 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7925 		 * and ip_select_source_v6() do not.
7926 		 */
7927 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7928 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7929 
7930 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7931 		if (isipv4) {
7932 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7933 			v4setsrc = INADDR_ANY;
7934 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7935 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7936 			    NULL, NULL);
7937 		} else {
7938 			v6setsrc = ipv6_all_zeros;
7939 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7940 			    NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7941 			    NULL, NULL);
7942 		}
7943 		ASSERT(ire != NULL);
7944 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7945 			ire_refrele(ire);
7946 			dir->dir_dreachable = 0;
7947 
7948 			/* move on to next dst addr */
7949 			continue;
7950 		}
7951 		dir->dir_dreachable = 1;
7952 
7953 		dst_ill = ire_nexthop_ill(ire);
7954 		if (dst_ill == NULL) {
7955 			ire_refrele(ire);
7956 			continue;
7957 		}
7958 
7959 		/* With ipmp we most likely look at the ipmp ill here */
7960 		dir->dir_dmactype = dst_ill->ill_mactype;
7961 
7962 		if (isipv4) {
7963 			ipaddr_t v4saddr;
7964 
7965 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7966 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7967 			    &v4saddr, NULL, &ipif_flags) != 0) {
7968 				v4saddr = INADDR_ANY;
7969 				ipif_flags = 0;
7970 			}
7971 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7972 		} else {
7973 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7974 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7975 			    saddr, NULL, &ipif_flags) != 0) {
7976 				*saddr = ipv6_all_zeros;
7977 				ipif_flags = 0;
7978 			}
7979 		}
7980 
7981 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7982 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7983 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7984 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7985 		ire_refrele(ire);
7986 		ill_refrele(dst_ill);
7987 	}
7988 	miocack(q, mp, iocp->ioc_count, 0);
7989 }
7990 
7991 /*
7992  * Check if this is an address assigned to this machine.
7993  * Skips interfaces that are down by using ire checks.
7994  * Translates mapped addresses to v4 addresses and then
7995  * treats them as such, returning true if the v4 address
7996  * associated with this mapped address is configured.
7997  * Note: Applications will have to be careful what they do
7998  * with the response; use of mapped addresses limits
7999  * what can be done with the socket, especially with
8000  * respect to socket options and ioctls - neither IPv4
8001  * options nor IPv6 sticky options/ancillary data options
8002  * may be used.
8003  */
8004 /* ARGSUSED */
8005 int
8006 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8007     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8008 {
8009 	struct sioc_addrreq *sia;
8010 	sin_t *sin;
8011 	ire_t *ire;
8012 	mblk_t *mp1;
8013 	zoneid_t zoneid;
8014 	ip_stack_t	*ipst;
8015 
8016 	ip1dbg(("ip_sioctl_tmyaddr"));
8017 
8018 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8019 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8020 	ipst = CONNQ_TO_IPST(q);
8021 
8022 	/* Existence verified in ip_wput_nondata */
8023 	mp1 = mp->b_cont->b_cont;
8024 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8025 	sin = (sin_t *)&sia->sa_addr;
8026 	switch (sin->sin_family) {
8027 	case AF_INET6: {
8028 		sin6_t *sin6 = (sin6_t *)sin;
8029 
8030 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8031 			ipaddr_t v4_addr;
8032 
8033 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8034 			    v4_addr);
8035 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8036 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8037 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8038 		} else {
8039 			in6_addr_t v6addr;
8040 
8041 			v6addr = sin6->sin6_addr;
8042 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8043 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8044 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8045 		}
8046 		break;
8047 	}
8048 	case AF_INET: {
8049 		ipaddr_t v4addr;
8050 
8051 		v4addr = sin->sin_addr.s_addr;
8052 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8053 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8054 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8055 		break;
8056 	}
8057 	default:
8058 		return (EAFNOSUPPORT);
8059 	}
8060 	if (ire != NULL) {
8061 		sia->sa_res = 1;
8062 		ire_refrele(ire);
8063 	} else {
8064 		sia->sa_res = 0;
8065 	}
8066 	return (0);
8067 }
8068 
8069 /*
8070  * Check if this is an address assigned on-link i.e. neighbor,
8071  * and makes sure it's reachable from the current zone.
8072  * Returns true for my addresses as well.
8073  * Translates mapped addresses to v4 addresses and then
8074  * treats them as such, returning true if the v4 address
8075  * associated with this mapped address is configured.
8076  * Note: Applications will have to be careful what they do
8077  * with the response; use of mapped addresses limits
8078  * what can be done with the socket, especially with
8079  * respect to socket options and ioctls - neither IPv4
8080  * options nor IPv6 sticky options/ancillary data options
8081  * may be used.
8082  */
8083 /* ARGSUSED */
8084 int
8085 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8086     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8087 {
8088 	struct sioc_addrreq *sia;
8089 	sin_t *sin;
8090 	mblk_t	*mp1;
8091 	ire_t *ire = NULL;
8092 	zoneid_t zoneid;
8093 	ip_stack_t	*ipst;
8094 
8095 	ip1dbg(("ip_sioctl_tonlink"));
8096 
8097 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8098 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8099 	ipst = CONNQ_TO_IPST(q);
8100 
8101 	/* Existence verified in ip_wput_nondata */
8102 	mp1 = mp->b_cont->b_cont;
8103 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8104 	sin = (sin_t *)&sia->sa_addr;
8105 
8106 	/*
8107 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8108 	 * to make sure we only look at on-link unicast address.
8109 	 */
8110 	switch (sin->sin_family) {
8111 	case AF_INET6: {
8112 		sin6_t *sin6 = (sin6_t *)sin;
8113 
8114 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8115 			ipaddr_t v4_addr;
8116 
8117 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8118 			    v4_addr);
8119 			if (!CLASSD(v4_addr)) {
8120 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8121 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8122 				    0, ipst, NULL);
8123 			}
8124 		} else {
8125 			in6_addr_t v6addr;
8126 
8127 			v6addr = sin6->sin6_addr;
8128 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8129 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8130 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8131 				    ipst, NULL);
8132 			}
8133 		}
8134 		break;
8135 	}
8136 	case AF_INET: {
8137 		ipaddr_t v4addr;
8138 
8139 		v4addr = sin->sin_addr.s_addr;
8140 		if (!CLASSD(v4addr)) {
8141 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8142 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8143 		}
8144 		break;
8145 	}
8146 	default:
8147 		return (EAFNOSUPPORT);
8148 	}
8149 	sia->sa_res = 0;
8150 	if (ire != NULL) {
8151 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8152 
8153 		if ((ire->ire_type & IRE_ONLINK) &&
8154 		    !(ire->ire_type & IRE_BROADCAST))
8155 			sia->sa_res = 1;
8156 		ire_refrele(ire);
8157 	}
8158 	return (0);
8159 }
8160 
8161 /*
8162  * TBD: implement when kernel maintaines a list of site prefixes.
8163  */
8164 /* ARGSUSED */
8165 int
8166 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8167     ip_ioctl_cmd_t *ipip, void *ifreq)
8168 {
8169 	return (ENXIO);
8170 }
8171 
8172 /* ARP IOCTLs. */
8173 /* ARGSUSED */
8174 int
8175 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8176     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8177 {
8178 	int		err;
8179 	ipaddr_t	ipaddr;
8180 	struct iocblk	*iocp;
8181 	conn_t		*connp;
8182 	struct arpreq	*ar;
8183 	struct xarpreq	*xar;
8184 	int		arp_flags, flags, alength;
8185 	uchar_t		*lladdr;
8186 	ip_stack_t	*ipst;
8187 	ill_t		*ill = ipif->ipif_ill;
8188 	ill_t		*proxy_ill = NULL;
8189 	ipmp_arpent_t	*entp = NULL;
8190 	boolean_t	proxyarp = B_FALSE;
8191 	boolean_t	if_arp_ioctl = B_FALSE;
8192 	ncec_t		*ncec = NULL;
8193 	nce_t		*nce;
8194 
8195 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8196 	connp = Q_TO_CONN(q);
8197 	ipst = connp->conn_netstack->netstack_ip;
8198 	iocp = (struct iocblk *)mp->b_rptr;
8199 
8200 	if (ipip->ipi_cmd_type == XARP_CMD) {
8201 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8202 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8203 		ar = NULL;
8204 
8205 		arp_flags = xar->xarp_flags;
8206 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8207 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8208 		/*
8209 		 * Validate against user's link layer address length
8210 		 * input and name and addr length limits.
8211 		 */
8212 		alength = ill->ill_phys_addr_length;
8213 		if (ipip->ipi_cmd == SIOCSXARP) {
8214 			if (alength != xar->xarp_ha.sdl_alen ||
8215 			    (alength + xar->xarp_ha.sdl_nlen >
8216 			    sizeof (xar->xarp_ha.sdl_data)))
8217 				return (EINVAL);
8218 		}
8219 	} else {
8220 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8221 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8222 		xar = NULL;
8223 
8224 		arp_flags = ar->arp_flags;
8225 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8226 		/*
8227 		 * Theoretically, the sa_family could tell us what link
8228 		 * layer type this operation is trying to deal with. By
8229 		 * common usage AF_UNSPEC means ethernet. We'll assume
8230 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8231 		 * for now. Our new SIOC*XARP ioctls can be used more
8232 		 * generally.
8233 		 *
8234 		 * If the underlying media happens to have a non 6 byte
8235 		 * address, arp module will fail set/get, but the del
8236 		 * operation will succeed.
8237 		 */
8238 		alength = 6;
8239 		if ((ipip->ipi_cmd != SIOCDARP) &&
8240 		    (alength != ill->ill_phys_addr_length)) {
8241 			return (EINVAL);
8242 		}
8243 	}
8244 
8245 	/* Translate ATF* flags to NCE* flags */
8246 	flags = 0;
8247 	if (arp_flags & ATF_AUTHORITY)
8248 		flags |= NCE_F_AUTHORITY;
8249 	if (arp_flags & ATF_PERM)
8250 		flags |= NCE_F_NONUD; /* not subject to aging */
8251 	if (arp_flags & ATF_PUBL)
8252 		flags |= NCE_F_PUBLISH;
8253 
8254 	/*
8255 	 * IPMP ARP special handling:
8256 	 *
8257 	 * 1. Since ARP mappings must appear consistent across the group,
8258 	 *    prohibit changing ARP mappings on the underlying interfaces.
8259 	 *
8260 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8261 	 *    IP itself, prohibit changing them.
8262 	 *
8263 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8264 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8265 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8266 	 */
8267 	if (IS_UNDER_IPMP(ill)) {
8268 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8269 			return (EPERM);
8270 	}
8271 	if (IS_IPMP(ill)) {
8272 		ipmp_illgrp_t *illg = ill->ill_grp;
8273 
8274 		switch (ipip->ipi_cmd) {
8275 		case SIOCSARP:
8276 		case SIOCSXARP:
8277 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8278 			if (proxy_ill != NULL) {
8279 				proxyarp = B_TRUE;
8280 				if (!ipmp_ill_is_active(proxy_ill))
8281 					proxy_ill = ipmp_illgrp_next_ill(illg);
8282 				if (proxy_ill != NULL)
8283 					lladdr = proxy_ill->ill_phys_addr;
8284 			}
8285 			/* FALLTHRU */
8286 		}
8287 	}
8288 
8289 	ipaddr = sin->sin_addr.s_addr;
8290 	/*
8291 	 * don't match across illgrp per case (1) and (2).
8292 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8293 	 */
8294 	nce = nce_lookup_v4(ill, &ipaddr);
8295 	if (nce != NULL)
8296 		ncec = nce->nce_common;
8297 
8298 	switch (iocp->ioc_cmd) {
8299 	case SIOCDARP:
8300 	case SIOCDXARP: {
8301 		/*
8302 		 * Delete the NCE if any.
8303 		 */
8304 		if (ncec == NULL) {
8305 			iocp->ioc_error = ENXIO;
8306 			break;
8307 		}
8308 		/* Don't allow changes to arp mappings of local addresses. */
8309 		if (NCE_MYADDR(ncec)) {
8310 			nce_refrele(nce);
8311 			return (ENOTSUP);
8312 		}
8313 		iocp->ioc_error = 0;
8314 
8315 		/*
8316 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8317 		 * This will delete all the nce entries on the under_ills.
8318 		 */
8319 		ncec_delete(ncec);
8320 		/*
8321 		 * Once the NCE has been deleted, then the ire_dep* consistency
8322 		 * mechanism will find any IRE which depended on the now
8323 		 * condemned NCE (as part of sending packets).
8324 		 * That mechanism handles redirects by deleting redirects
8325 		 * that refer to UNREACHABLE nces.
8326 		 */
8327 		break;
8328 	}
8329 	case SIOCGARP:
8330 	case SIOCGXARP:
8331 		if (ncec != NULL) {
8332 			lladdr = ncec->ncec_lladdr;
8333 			flags = ncec->ncec_flags;
8334 			iocp->ioc_error = 0;
8335 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8336 		} else {
8337 			iocp->ioc_error = ENXIO;
8338 		}
8339 		break;
8340 	case SIOCSARP:
8341 	case SIOCSXARP:
8342 		/* Don't allow changes to arp mappings of local addresses. */
8343 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8344 			nce_refrele(nce);
8345 			return (ENOTSUP);
8346 		}
8347 
8348 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8349 		flags |= NCE_F_STATIC;
8350 		if (!if_arp_ioctl) {
8351 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8352 			    lladdr, alength, flags);
8353 		} else {
8354 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8355 			if (ipif != NULL) {
8356 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8357 				    lladdr, alength, flags);
8358 				ipif_refrele(ipif);
8359 			}
8360 		}
8361 		if (nce != NULL) {
8362 			nce_refrele(nce);
8363 			nce = NULL;
8364 		}
8365 		/*
8366 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8367 		 * by nce_add_common()
8368 		 */
8369 		err = nce_lookup_then_add_v4(ill, lladdr,
8370 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8371 		    &nce);
8372 		if (err == EEXIST) {
8373 			ncec = nce->nce_common;
8374 			mutex_enter(&ncec->ncec_lock);
8375 			ncec->ncec_state = ND_REACHABLE;
8376 			ncec->ncec_flags = flags;
8377 			nce_update(ncec, ND_UNCHANGED, lladdr);
8378 			mutex_exit(&ncec->ncec_lock);
8379 			err = 0;
8380 		}
8381 		if (nce != NULL) {
8382 			nce_refrele(nce);
8383 			nce = NULL;
8384 		}
8385 		if (IS_IPMP(ill) && err == 0) {
8386 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8387 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8388 			    flags);
8389 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8390 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8391 				break;
8392 			}
8393 		}
8394 		iocp->ioc_error = err;
8395 	}
8396 
8397 	if (nce != NULL) {
8398 		nce_refrele(nce);
8399 	}
8400 
8401 	/*
8402 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8403 	 */
8404 	if (entp != NULL)
8405 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8406 
8407 	return (iocp->ioc_error);
8408 }
8409 
8410 /*
8411  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8412  * the associated sin and refhold and return the associated ipif via `ci'.
8413  */
8414 int
8415 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8416     cmd_info_t *ci)
8417 {
8418 	mblk_t	*mp1;
8419 	sin_t	*sin;
8420 	conn_t	*connp;
8421 	ipif_t	*ipif;
8422 	ire_t	*ire = NULL;
8423 	ill_t	*ill = NULL;
8424 	boolean_t exists;
8425 	ip_stack_t *ipst;
8426 	struct arpreq *ar;
8427 	struct xarpreq *xar;
8428 	struct sockaddr_dl *sdl;
8429 
8430 	/* ioctl comes down on a conn */
8431 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8432 	connp = Q_TO_CONN(q);
8433 	if (connp->conn_family == AF_INET6)
8434 		return (ENXIO);
8435 
8436 	ipst = connp->conn_netstack->netstack_ip;
8437 
8438 	/* Verified in ip_wput_nondata */
8439 	mp1 = mp->b_cont->b_cont;
8440 
8441 	if (ipip->ipi_cmd_type == XARP_CMD) {
8442 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8443 		xar = (struct xarpreq *)mp1->b_rptr;
8444 		sin = (sin_t *)&xar->xarp_pa;
8445 		sdl = &xar->xarp_ha;
8446 
8447 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8448 			return (ENXIO);
8449 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8450 			return (EINVAL);
8451 	} else {
8452 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8453 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8454 		ar = (struct arpreq *)mp1->b_rptr;
8455 		sin = (sin_t *)&ar->arp_pa;
8456 	}
8457 
8458 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8459 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8460 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8461 		if (ipif == NULL)
8462 			return (ENXIO);
8463 		if (ipif->ipif_id != 0) {
8464 			ipif_refrele(ipif);
8465 			return (ENXIO);
8466 		}
8467 	} else {
8468 		/*
8469 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8470 		 * of 0: use the IP address to find the ipif.  If the IP
8471 		 * address is an IPMP test address, ire_ftable_lookup() will
8472 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8473 		 */
8474 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8475 		    ipst);
8476 		if (ipif == NULL) {
8477 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8478 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8479 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8480 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8481 				if (ire != NULL)
8482 					ire_refrele(ire);
8483 				return (ENXIO);
8484 			}
8485 			ASSERT(ire != NULL && ill != NULL);
8486 			ipif = ill->ill_ipif;
8487 			ipif_refhold(ipif);
8488 			ire_refrele(ire);
8489 		}
8490 	}
8491 
8492 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8493 		ipif_refrele(ipif);
8494 		return (ENXIO);
8495 	}
8496 
8497 	ci->ci_sin = sin;
8498 	ci->ci_ipif = ipif;
8499 	return (0);
8500 }
8501 
8502 /*
8503  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8504  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8505  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8506  * up and thus an ill can join that illgrp.
8507  *
8508  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8509  * open()/close() primarily because close() is not allowed to fail or block
8510  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8511  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8512  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8513  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8514  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8515  * state if I_UNLINK didn't occur.
8516  *
8517  * Note that for each plumb/unplumb operation, we may end up here more than
8518  * once because of the way ifconfig works.  However, it's OK to link the same
8519  * illgrp more than once, or unlink an illgrp that's already unlinked.
8520  */
8521 static int
8522 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8523 {
8524 	int err;
8525 	ip_stack_t *ipst = ill->ill_ipst;
8526 
8527 	ASSERT(IS_IPMP(ill));
8528 	ASSERT(IAM_WRITER_ILL(ill));
8529 
8530 	switch (ioccmd) {
8531 	case I_LINK:
8532 		return (ENOTSUP);
8533 
8534 	case I_PLINK:
8535 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8536 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8537 		rw_exit(&ipst->ips_ipmp_lock);
8538 		break;
8539 
8540 	case I_PUNLINK:
8541 		/*
8542 		 * Require all UP ipifs be brought down prior to unlinking the
8543 		 * illgrp so any associated IREs (and other state) is torched.
8544 		 */
8545 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8546 			return (EBUSY);
8547 
8548 		/*
8549 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8550 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8551 		 * join this group.  Specifically: ills trying to join grab
8552 		 * ipmp_lock and bump a "pending join" counter checked by
8553 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8554 		 * joins can occur (since we have ipmp_lock).  Once we drop
8555 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8556 		 * find the illgrp (since we unlinked it) and will return
8557 		 * EAFNOSUPPORT.  This will then take them back through the
8558 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8559 		 * back through I_PLINK above.
8560 		 */
8561 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8562 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8563 		rw_exit(&ipst->ips_ipmp_lock);
8564 		return (err);
8565 	default:
8566 		break;
8567 	}
8568 	return (0);
8569 }
8570 
8571 /*
8572  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8573  * atomically set/clear the muxids. Also complete the ioctl by acking or
8574  * naking it.  Note that the code is structured such that the link type,
8575  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8576  * its clones use the persistent link, while pppd(1M) and perhaps many
8577  * other daemons may use non-persistent link.  When combined with some
8578  * ill_t states, linking and unlinking lower streams may be used as
8579  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8580  */
8581 /* ARGSUSED */
8582 void
8583 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8584 {
8585 	mblk_t		*mp1;
8586 	struct linkblk	*li;
8587 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8588 	int		err = 0;
8589 
8590 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8591 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8592 
8593 	mp1 = mp->b_cont;	/* This is the linkblk info */
8594 	li = (struct linkblk *)mp1->b_rptr;
8595 
8596 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8597 	if (err == EINPROGRESS)
8598 		return;
8599 	if (err == 0)
8600 		miocack(q, mp, 0, 0);
8601 	else
8602 		miocnak(q, mp, 0, err);
8603 
8604 	/* Conn was refheld in ip_sioctl_copyin_setup */
8605 	if (CONN_Q(q)) {
8606 		CONN_DEC_IOCTLREF(Q_TO_CONN(q));
8607 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8608 	}
8609 }
8610 
8611 /*
8612  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8613  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8614  * module stream).
8615  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8616  * an error code on failure.
8617  */
8618 static int
8619 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8620     struct linkblk *li)
8621 {
8622 	int		err = 0;
8623 	ill_t		*ill;
8624 	queue_t		*ipwq, *dwq;
8625 	const char	*name;
8626 	struct qinit	*qinfo;
8627 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8628 	boolean_t	entered_ipsq = B_FALSE;
8629 	boolean_t	is_ip = B_FALSE;
8630 	arl_t		*arl;
8631 
8632 	/*
8633 	 * Walk the lower stream to verify it's the IP module stream.
8634 	 * The IP module is identified by its name, wput function,
8635 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8636 	 * (li->l_qbot) will not vanish until this ioctl completes.
8637 	 */
8638 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8639 		qinfo = ipwq->q_qinfo;
8640 		name = qinfo->qi_minfo->mi_idname;
8641 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8642 		    qinfo->qi_putp != ip_lwput && ipwq->q_next != NULL) {
8643 			is_ip = B_TRUE;
8644 			break;
8645 		}
8646 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8647 		    qinfo->qi_putp != ip_lwput && ipwq->q_next != NULL) {
8648 			break;
8649 		}
8650 	}
8651 
8652 	/*
8653 	 * If this isn't an IP module stream, bail.
8654 	 */
8655 	if (ipwq == NULL)
8656 		return (0);
8657 
8658 	if (!is_ip) {
8659 		arl = (arl_t *)ipwq->q_ptr;
8660 		ill = arl_to_ill(arl);
8661 		if (ill == NULL)
8662 			return (0);
8663 	} else {
8664 		ill = ipwq->q_ptr;
8665 	}
8666 	ASSERT(ill != NULL);
8667 
8668 	if (ipsq == NULL) {
8669 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8670 		    NEW_OP, B_FALSE);
8671 		if (ipsq == NULL) {
8672 			if (!is_ip)
8673 				ill_refrele(ill);
8674 			return (EINPROGRESS);
8675 		}
8676 		entered_ipsq = B_TRUE;
8677 	}
8678 	ASSERT(IAM_WRITER_ILL(ill));
8679 	mutex_enter(&ill->ill_lock);
8680 	if (!is_ip) {
8681 		if (islink && ill->ill_muxid == 0) {
8682 			/*
8683 			 * Plumbing has to be done with IP plumbed first, arp
8684 			 * second, but here we have arp being plumbed first.
8685 			 */
8686 			mutex_exit(&ill->ill_lock);
8687 			if (entered_ipsq)
8688 				ipsq_exit(ipsq);
8689 			ill_refrele(ill);
8690 			return (EINVAL);
8691 		}
8692 	}
8693 	mutex_exit(&ill->ill_lock);
8694 	if (!is_ip) {
8695 		arl->arl_muxid = islink ? li->l_index : 0;
8696 		ill_refrele(ill);
8697 		goto done;
8698 	}
8699 
8700 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8701 		goto done;
8702 
8703 	/*
8704 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8705 	 * the read queue of the module immediately below IP in the ill.
8706 	 * These are used during the capability negotiation below.
8707 	 */
8708 	ill->ill_lmod_rq = NULL;
8709 	ill->ill_lmod_cnt = 0;
8710 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8711 		ill->ill_lmod_rq = RD(dwq);
8712 		for (; dwq != NULL; dwq = dwq->q_next)
8713 			ill->ill_lmod_cnt++;
8714 	}
8715 
8716 	ill->ill_muxid = islink ? li->l_index : 0;
8717 
8718 	/*
8719 	 * Mark the ipsq busy until the capability operations initiated below
8720 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8721 	 * returns, but the capability operation may complete asynchronously
8722 	 * much later.
8723 	 */
8724 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8725 	/*
8726 	 * If there's at least one up ipif on this ill, then we're bound to
8727 	 * the underlying driver via DLPI.  In that case, renegotiate
8728 	 * capabilities to account for any possible change in modules
8729 	 * interposed between IP and the driver.
8730 	 */
8731 	if (ill->ill_ipif_up_count > 0) {
8732 		if (islink)
8733 			ill_capability_probe(ill);
8734 		else
8735 			ill_capability_reset(ill, B_FALSE);
8736 	}
8737 	ipsq_current_finish(ipsq);
8738 done:
8739 	if (entered_ipsq)
8740 		ipsq_exit(ipsq);
8741 
8742 	return (err);
8743 }
8744 
8745 /*
8746  * Search the ioctl command in the ioctl tables and return a pointer
8747  * to the ioctl command information. The ioctl command tables are
8748  * static and fully populated at compile time.
8749  */
8750 ip_ioctl_cmd_t *
8751 ip_sioctl_lookup(int ioc_cmd)
8752 {
8753 	int index;
8754 	ip_ioctl_cmd_t *ipip;
8755 	ip_ioctl_cmd_t *ipip_end;
8756 
8757 	if (ioc_cmd == IPI_DONTCARE)
8758 		return (NULL);
8759 
8760 	/*
8761 	 * Do a 2 step search. First search the indexed table
8762 	 * based on the least significant byte of the ioctl cmd.
8763 	 * If we don't find a match, then search the misc table
8764 	 * serially.
8765 	 */
8766 	index = ioc_cmd & 0xFF;
8767 	if (index < ip_ndx_ioctl_count) {
8768 		ipip = &ip_ndx_ioctl_table[index];
8769 		if (ipip->ipi_cmd == ioc_cmd) {
8770 			/* Found a match in the ndx table */
8771 			return (ipip);
8772 		}
8773 	}
8774 
8775 	/* Search the misc table */
8776 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8777 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8778 		if (ipip->ipi_cmd == ioc_cmd)
8779 			/* Found a match in the misc table */
8780 			return (ipip);
8781 	}
8782 
8783 	return (NULL);
8784 }
8785 
8786 /*
8787  * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8788  */
8789 static boolean_t
8790 getset_ioctl_checks(mblk_t *mp)
8791 {
8792 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8793 	mblk_t		*mp1 = mp->b_cont;
8794 	mod_ioc_prop_t	*pioc;
8795 	uint_t		flags;
8796 	uint_t		pioc_size;
8797 
8798 	/* do sanity checks on various arguments */
8799 	if (mp1 == NULL || iocp->ioc_count == 0 ||
8800 	    iocp->ioc_count == TRANSPARENT) {
8801 		return (B_FALSE);
8802 	}
8803 	if (msgdsize(mp1) < iocp->ioc_count) {
8804 		if (!pullupmsg(mp1, iocp->ioc_count))
8805 			return (B_FALSE);
8806 	}
8807 
8808 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8809 
8810 	/* sanity checks on mpr_valsize */
8811 	pioc_size = sizeof (mod_ioc_prop_t);
8812 	if (pioc->mpr_valsize != 0)
8813 		pioc_size += pioc->mpr_valsize - 1;
8814 
8815 	if (iocp->ioc_count != pioc_size)
8816 		return (B_FALSE);
8817 
8818 	flags = pioc->mpr_flags;
8819 	if (iocp->ioc_cmd == SIOCSETPROP) {
8820 		/*
8821 		 * One can either reset the value to it's default value or
8822 		 * change the current value or append/remove the value from
8823 		 * a multi-valued properties.
8824 		 */
8825 		if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8826 		    flags != MOD_PROP_ACTIVE &&
8827 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8828 		    flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8829 			return (B_FALSE);
8830 	} else {
8831 		ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8832 
8833 		/*
8834 		 * One can retrieve only one kind of property information
8835 		 * at a time.
8836 		 */
8837 		if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8838 		    (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8839 		    (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8840 		    (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8841 			return (B_FALSE);
8842 	}
8843 
8844 	return (B_TRUE);
8845 }
8846 
8847 /*
8848  * process the SIOC{SET|GET}PROP ioctl's
8849  */
8850 /* ARGSUSED */
8851 static void
8852 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8853 {
8854 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8855 	mblk_t		*mp1 = mp->b_cont;
8856 	mod_ioc_prop_t	*pioc;
8857 	mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8858 	ip_stack_t	*ipst;
8859 	netstack_t	*stack;
8860 	cred_t		*cr;
8861 	boolean_t	set;
8862 	int		err;
8863 
8864 	ASSERT(q->q_next == NULL);
8865 	ASSERT(CONN_Q(q));
8866 
8867 	if (!getset_ioctl_checks(mp)) {
8868 		miocnak(q, mp, 0, EINVAL);
8869 		return;
8870 	}
8871 	ipst = CONNQ_TO_IPST(q);
8872 	stack = ipst->ips_netstack;
8873 	pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8874 
8875 	switch (pioc->mpr_proto) {
8876 	case MOD_PROTO_IP:
8877 	case MOD_PROTO_IPV4:
8878 	case MOD_PROTO_IPV6:
8879 		ptbl = ipst->ips_propinfo_tbl;
8880 		break;
8881 	case MOD_PROTO_RAWIP:
8882 		ptbl = stack->netstack_icmp->is_propinfo_tbl;
8883 		break;
8884 	case MOD_PROTO_TCP:
8885 		ptbl = stack->netstack_tcp->tcps_propinfo_tbl;
8886 		break;
8887 	case MOD_PROTO_UDP:
8888 		ptbl = stack->netstack_udp->us_propinfo_tbl;
8889 		break;
8890 	case MOD_PROTO_SCTP:
8891 		ptbl = stack->netstack_sctp->sctps_propinfo_tbl;
8892 		break;
8893 	default:
8894 		miocnak(q, mp, 0, EINVAL);
8895 		return;
8896 	}
8897 
8898 	pinfo = mod_prop_lookup(ptbl, pioc->mpr_name, pioc->mpr_proto);
8899 	if (pinfo == NULL) {
8900 		miocnak(q, mp, 0, ENOENT);
8901 		return;
8902 	}
8903 
8904 	set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8905 	if (set && pinfo->mpi_setf != NULL) {
8906 		cr = msg_getcred(mp, NULL);
8907 		if (cr == NULL)
8908 			cr = iocp->ioc_cr;
8909 		err = pinfo->mpi_setf(stack, cr, pinfo, pioc->mpr_ifname,
8910 		    pioc->mpr_val, pioc->mpr_flags);
8911 	} else if (!set && pinfo->mpi_getf != NULL) {
8912 		err = pinfo->mpi_getf(stack, pinfo, pioc->mpr_ifname,
8913 		    pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8914 	} else {
8915 		err = EPERM;
8916 	}
8917 
8918 	if (err != 0) {
8919 		miocnak(q, mp, 0, err);
8920 	} else {
8921 		if (set)
8922 			miocack(q, mp, 0, 0);
8923 		else    /* For get, we need to return back the data */
8924 			miocack(q, mp, iocp->ioc_count, 0);
8925 	}
8926 }
8927 
8928 /*
8929  * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8930  * as several routing daemons have unfortunately used this 'unpublished'
8931  * but well-known ioctls.
8932  */
8933 /* ARGSUSED */
8934 static void
8935 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8936 {
8937 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8938 	mblk_t		*mp1 = mp->b_cont;
8939 	char		*pname, *pval, *buf;
8940 	uint_t		bufsize, proto;
8941 	mod_prop_info_t *pinfo = NULL;
8942 	ip_stack_t	*ipst;
8943 	int		err = 0;
8944 
8945 	ASSERT(CONN_Q(q));
8946 	ipst = CONNQ_TO_IPST(q);
8947 
8948 	if (iocp->ioc_count == 0 || mp1 == NULL) {
8949 		miocnak(q, mp, 0, EINVAL);
8950 		return;
8951 	}
8952 
8953 	mp1->b_datap->db_lim[-1] = '\0';	/* Force null termination */
8954 	pval = buf = pname = (char *)mp1->b_rptr;
8955 	bufsize = MBLKL(mp1);
8956 
8957 	if (strcmp(pname, "ip_forwarding") == 0) {
8958 		pname = "forwarding";
8959 		proto = MOD_PROTO_IPV4;
8960 	} else if (strcmp(pname, "ip6_forwarding") == 0) {
8961 		pname = "forwarding";
8962 		proto = MOD_PROTO_IPV6;
8963 	} else {
8964 		miocnak(q, mp, 0, EINVAL);
8965 		return;
8966 	}
8967 
8968 	pinfo = mod_prop_lookup(ipst->ips_propinfo_tbl, pname, proto);
8969 
8970 	switch (iocp->ioc_cmd) {
8971 	case ND_GET:
8972 		if ((err = pinfo->mpi_getf(ipst->ips_netstack, pinfo, NULL, buf,
8973 		    bufsize, 0)) == 0) {
8974 			miocack(q, mp, iocp->ioc_count, 0);
8975 			return;
8976 		}
8977 		break;
8978 	case ND_SET:
8979 		/*
8980 		 * buffer will have property name and value in the following
8981 		 * format,
8982 		 * <property name>'\0'<property value>'\0', extract them;
8983 		 */
8984 		while (*pval++)
8985 			noop;
8986 
8987 		if (!*pval || pval >= (char *)mp1->b_wptr) {
8988 			err = EINVAL;
8989 		} else if ((err = pinfo->mpi_setf(ipst->ips_netstack, NULL,
8990 		    pinfo, NULL, pval, 0)) == 0) {
8991 			miocack(q, mp, 0, 0);
8992 			return;
8993 		}
8994 		break;
8995 	default:
8996 		err = EINVAL;
8997 		break;
8998 	}
8999 	miocnak(q, mp, 0, err);
9000 }
9001 
9002 /*
9003  * Wrapper function for resuming deferred ioctl processing
9004  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9005  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9006  */
9007 /* ARGSUSED */
9008 void
9009 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9010     void *dummy_arg)
9011 {
9012 	ip_sioctl_copyin_setup(q, mp);
9013 }
9014 
9015 /*
9016  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
9017  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
9018  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9019  * We establish here the size of the block to be copied in.  mi_copyin
9020  * arranges for this to happen, an processing continues in ip_wput_nondata with
9021  * an M_IOCDATA message.
9022  */
9023 void
9024 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9025 {
9026 	int	copyin_size;
9027 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9028 	ip_ioctl_cmd_t *ipip;
9029 	cred_t *cr;
9030 	ip_stack_t	*ipst;
9031 
9032 	if (CONN_Q(q))
9033 		ipst = CONNQ_TO_IPST(q);
9034 	else
9035 		ipst = ILLQ_TO_IPST(q);
9036 
9037 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9038 	if (ipip == NULL) {
9039 		/*
9040 		 * The ioctl is not one we understand or own.
9041 		 * Pass it along to be processed down stream,
9042 		 * if this is a module instance of IP, else nak
9043 		 * the ioctl.
9044 		 */
9045 		if (q->q_next == NULL) {
9046 			goto nak;
9047 		} else {
9048 			putnext(q, mp);
9049 			return;
9050 		}
9051 	}
9052 
9053 	/*
9054 	 * If this is deferred, then we will do all the checks when we
9055 	 * come back.
9056 	 */
9057 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9058 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9059 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9060 		return;
9061 	}
9062 
9063 	/*
9064 	 * Only allow a very small subset of IP ioctls on this stream if
9065 	 * IP is a module and not a driver. Allowing ioctls to be processed
9066 	 * in this case may cause assert failures or data corruption.
9067 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9068 	 * ioctls allowed on an IP module stream, after which this stream
9069 	 * normally becomes a multiplexor (at which time the stream head
9070 	 * will fail all ioctls).
9071 	 */
9072 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9073 		goto nak;
9074 	}
9075 
9076 	/* Make sure we have ioctl data to process. */
9077 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9078 		goto nak;
9079 
9080 	/*
9081 	 * Prefer dblk credential over ioctl credential; some synthesized
9082 	 * ioctls have kcred set because there's no way to crhold()
9083 	 * a credential in some contexts.  (ioc_cr is not crfree() by
9084 	 * the framework; the caller of ioctl needs to hold the reference
9085 	 * for the duration of the call).
9086 	 */
9087 	cr = msg_getcred(mp, NULL);
9088 	if (cr == NULL)
9089 		cr = iocp->ioc_cr;
9090 
9091 	/* Make sure normal users don't send down privileged ioctls */
9092 	if ((ipip->ipi_flags & IPI_PRIV) &&
9093 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9094 		/* We checked the privilege earlier but log it here */
9095 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9096 		return;
9097 	}
9098 
9099 	/*
9100 	 * The ioctl command tables can only encode fixed length
9101 	 * ioctl data. If the length is variable, the table will
9102 	 * encode the length as zero. Such special cases are handled
9103 	 * below in the switch.
9104 	 */
9105 	if (ipip->ipi_copyin_size != 0) {
9106 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9107 		return;
9108 	}
9109 
9110 	switch (iocp->ioc_cmd) {
9111 	case O_SIOCGIFCONF:
9112 	case SIOCGIFCONF:
9113 		/*
9114 		 * This IOCTL is hilarious.  See comments in
9115 		 * ip_sioctl_get_ifconf for the story.
9116 		 */
9117 		if (iocp->ioc_count == TRANSPARENT)
9118 			copyin_size = SIZEOF_STRUCT(ifconf,
9119 			    iocp->ioc_flag);
9120 		else
9121 			copyin_size = iocp->ioc_count;
9122 		mi_copyin(q, mp, NULL, copyin_size);
9123 		return;
9124 
9125 	case O_SIOCGLIFCONF:
9126 	case SIOCGLIFCONF:
9127 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9128 		mi_copyin(q, mp, NULL, copyin_size);
9129 		return;
9130 
9131 	case SIOCGLIFSRCOF:
9132 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9133 		mi_copyin(q, mp, NULL, copyin_size);
9134 		return;
9135 
9136 	case SIOCGIP6ADDRPOLICY:
9137 		ip_sioctl_ip6addrpolicy(q, mp);
9138 		ip6_asp_table_refrele(ipst);
9139 		return;
9140 
9141 	case SIOCSIP6ADDRPOLICY:
9142 		ip_sioctl_ip6addrpolicy(q, mp);
9143 		return;
9144 
9145 	case SIOCGDSTINFO:
9146 		ip_sioctl_dstinfo(q, mp);
9147 		ip6_asp_table_refrele(ipst);
9148 		return;
9149 
9150 	case ND_SET:
9151 	case ND_GET:
9152 		ip_process_legacy_nddprop(q, mp);
9153 		return;
9154 
9155 	case SIOCSETPROP:
9156 	case SIOCGETPROP:
9157 		ip_sioctl_getsetprop(q, mp);
9158 		return;
9159 
9160 	case I_PLINK:
9161 	case I_PUNLINK:
9162 	case I_LINK:
9163 	case I_UNLINK:
9164 		/*
9165 		 * We treat non-persistent link similarly as the persistent
9166 		 * link case, in terms of plumbing/unplumbing, as well as
9167 		 * dynamic re-plumbing events indicator.  See comments
9168 		 * in ip_sioctl_plink() for more.
9169 		 *
9170 		 * Request can be enqueued in the 'ipsq' while waiting
9171 		 * to become exclusive. So bump up the conn ref.
9172 		 */
9173 		if (CONN_Q(q)) {
9174 			CONN_INC_REF(Q_TO_CONN(q));
9175 			CONN_INC_IOCTLREF(Q_TO_CONN(q))
9176 		}
9177 		ip_sioctl_plink(NULL, q, mp, NULL);
9178 		return;
9179 
9180 	case IP_IOCTL:
9181 		ip_wput_ioctl(q, mp);
9182 		return;
9183 
9184 	case SIOCILB:
9185 		/* The ioctl length varies depending on the ILB command. */
9186 		copyin_size = iocp->ioc_count;
9187 		if (copyin_size < sizeof (ilb_cmd_t))
9188 			goto nak;
9189 		mi_copyin(q, mp, NULL, copyin_size);
9190 		return;
9191 
9192 	default:
9193 		cmn_err(CE_WARN, "Unknown ioctl %d/0x%x slipped through.",
9194 		    iocp->ioc_cmd, iocp->ioc_cmd);
9195 		/* FALLTHRU */
9196 	}
9197 nak:
9198 	if (mp->b_cont != NULL) {
9199 		freemsg(mp->b_cont);
9200 		mp->b_cont = NULL;
9201 	}
9202 	iocp->ioc_error = EINVAL;
9203 	mp->b_datap->db_type = M_IOCNAK;
9204 	iocp->ioc_count = 0;
9205 	qreply(q, mp);
9206 }
9207 
9208 static void
9209 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9210 {
9211 	struct arpreq *ar;
9212 	struct xarpreq *xar;
9213 	mblk_t	*tmp;
9214 	struct iocblk *iocp;
9215 	int x_arp_ioctl = B_FALSE;
9216 	int *flagsp;
9217 	char *storage = NULL;
9218 
9219 	ASSERT(ill != NULL);
9220 
9221 	iocp = (struct iocblk *)mp->b_rptr;
9222 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9223 
9224 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9225 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9226 	    (iocp->ioc_cmd == SIOCSXARP)) {
9227 		x_arp_ioctl = B_TRUE;
9228 		xar = (struct xarpreq *)tmp->b_rptr;
9229 		flagsp = &xar->xarp_flags;
9230 		storage = xar->xarp_ha.sdl_data;
9231 	} else {
9232 		ar = (struct arpreq *)tmp->b_rptr;
9233 		flagsp = &ar->arp_flags;
9234 		storage = ar->arp_ha.sa_data;
9235 	}
9236 
9237 	/*
9238 	 * We're done if this is not an SIOCG{X}ARP
9239 	 */
9240 	if (x_arp_ioctl) {
9241 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9242 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9243 		    sizeof (xar->xarp_ha.sdl_data)) {
9244 			iocp->ioc_error = EINVAL;
9245 			return;
9246 		}
9247 	}
9248 	*flagsp = ATF_INUSE;
9249 	/*
9250 	 * If /sbin/arp told us we are the authority using the "permanent"
9251 	 * flag, or if this is one of my addresses print "permanent"
9252 	 * in the /sbin/arp output.
9253 	 */
9254 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9255 		*flagsp |= ATF_AUTHORITY;
9256 	if (flags & NCE_F_NONUD)
9257 		*flagsp |= ATF_PERM; /* not subject to aging */
9258 	if (flags & NCE_F_PUBLISH)
9259 		*flagsp |= ATF_PUBL;
9260 	if (hwaddr != NULL) {
9261 		*flagsp |= ATF_COM;
9262 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9263 	}
9264 }
9265 
9266 /*
9267  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9268  * interface) create the next available logical interface for this
9269  * physical interface.
9270  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9271  * ipif with the specified name.
9272  *
9273  * If the address family is not AF_UNSPEC then set the address as well.
9274  *
9275  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9276  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9277  *
9278  * Executed as a writer on the ill.
9279  * So no lock is needed to traverse the ipif chain, or examine the
9280  * phyint flags.
9281  */
9282 /* ARGSUSED */
9283 int
9284 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9285     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9286 {
9287 	mblk_t	*mp1;
9288 	struct lifreq *lifr;
9289 	boolean_t	isv6;
9290 	boolean_t	exists;
9291 	char	*name;
9292 	char	*endp;
9293 	char	*cp;
9294 	int	namelen;
9295 	ipif_t	*ipif;
9296 	long	id;
9297 	ipsq_t	*ipsq;
9298 	ill_t	*ill;
9299 	sin_t	*sin;
9300 	int	err = 0;
9301 	boolean_t found_sep = B_FALSE;
9302 	conn_t	*connp;
9303 	zoneid_t zoneid;
9304 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9305 
9306 	ASSERT(q->q_next == NULL);
9307 	ip1dbg(("ip_sioctl_addif\n"));
9308 	/* Existence of mp1 has been checked in ip_wput_nondata */
9309 	mp1 = mp->b_cont->b_cont;
9310 	/*
9311 	 * Null terminate the string to protect against buffer
9312 	 * overrun. String was generated by user code and may not
9313 	 * be trusted.
9314 	 */
9315 	lifr = (struct lifreq *)mp1->b_rptr;
9316 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9317 	name = lifr->lifr_name;
9318 	ASSERT(CONN_Q(q));
9319 	connp = Q_TO_CONN(q);
9320 	isv6 = (connp->conn_family == AF_INET6);
9321 	zoneid = connp->conn_zoneid;
9322 	namelen = mi_strlen(name);
9323 	if (namelen == 0)
9324 		return (EINVAL);
9325 
9326 	exists = B_FALSE;
9327 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9328 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9329 		/*
9330 		 * Allow creating lo0 using SIOCLIFADDIF.
9331 		 * can't be any other writer thread. So can pass null below
9332 		 * for the last 4 args to ipif_lookup_name.
9333 		 */
9334 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9335 		    &exists, isv6, zoneid, ipst);
9336 		/* Prevent any further action */
9337 		if (ipif == NULL) {
9338 			return (ENOBUFS);
9339 		} else if (!exists) {
9340 			/* We created the ipif now and as writer */
9341 			ipif_refrele(ipif);
9342 			return (0);
9343 		} else {
9344 			ill = ipif->ipif_ill;
9345 			ill_refhold(ill);
9346 			ipif_refrele(ipif);
9347 		}
9348 	} else {
9349 		/* Look for a colon in the name. */
9350 		endp = &name[namelen];
9351 		for (cp = endp; --cp > name; ) {
9352 			if (*cp == IPIF_SEPARATOR_CHAR) {
9353 				found_sep = B_TRUE;
9354 				/*
9355 				 * Reject any non-decimal aliases for plumbing
9356 				 * of logical interfaces. Aliases with leading
9357 				 * zeroes are also rejected as they introduce
9358 				 * ambiguity in the naming of the interfaces.
9359 				 * Comparing with "0" takes care of all such
9360 				 * cases.
9361 				 */
9362 				if ((strncmp("0", cp+1, 1)) == 0)
9363 					return (EINVAL);
9364 
9365 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9366 				    id <= 0 || *endp != '\0') {
9367 					return (EINVAL);
9368 				}
9369 				*cp = '\0';
9370 				break;
9371 			}
9372 		}
9373 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9374 		if (found_sep)
9375 			*cp = IPIF_SEPARATOR_CHAR;
9376 		if (ill == NULL)
9377 			return (ENXIO);
9378 	}
9379 
9380 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9381 	    B_TRUE);
9382 
9383 	/*
9384 	 * Release the refhold due to the lookup, now that we are excl
9385 	 * or we are just returning
9386 	 */
9387 	ill_refrele(ill);
9388 
9389 	if (ipsq == NULL)
9390 		return (EINPROGRESS);
9391 
9392 	/* We are now exclusive on the IPSQ */
9393 	ASSERT(IAM_WRITER_ILL(ill));
9394 
9395 	if (found_sep) {
9396 		/* Now see if there is an IPIF with this unit number. */
9397 		for (ipif = ill->ill_ipif; ipif != NULL;
9398 		    ipif = ipif->ipif_next) {
9399 			if (ipif->ipif_id == id) {
9400 				err = EEXIST;
9401 				goto done;
9402 			}
9403 		}
9404 	}
9405 
9406 	/*
9407 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9408 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9409 	 * instead.
9410 	 */
9411 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9412 	    B_TRUE, B_TRUE, &err)) == NULL) {
9413 		goto done;
9414 	}
9415 
9416 	/* Return created name with ioctl */
9417 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9418 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9419 	ip1dbg(("created %s\n", lifr->lifr_name));
9420 
9421 	/* Set address */
9422 	sin = (sin_t *)&lifr->lifr_addr;
9423 	if (sin->sin_family != AF_UNSPEC) {
9424 		err = ip_sioctl_addr(ipif, sin, q, mp,
9425 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9426 	}
9427 
9428 done:
9429 	ipsq_exit(ipsq);
9430 	return (err);
9431 }
9432 
9433 /*
9434  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9435  * interface) delete it based on the IP address (on this physical interface).
9436  * Otherwise delete it based on the ipif_id.
9437  * Also, special handling to allow a removeif of lo0.
9438  */
9439 /* ARGSUSED */
9440 int
9441 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9442     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9443 {
9444 	conn_t		*connp;
9445 	ill_t		*ill = ipif->ipif_ill;
9446 	boolean_t	 success;
9447 	ip_stack_t	*ipst;
9448 
9449 	ipst = CONNQ_TO_IPST(q);
9450 
9451 	ASSERT(q->q_next == NULL);
9452 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9453 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9454 	ASSERT(IAM_WRITER_IPIF(ipif));
9455 
9456 	connp = Q_TO_CONN(q);
9457 	/*
9458 	 * Special case for unplumbing lo0 (the loopback physical interface).
9459 	 * If unplumbing lo0, the incoming address structure has been
9460 	 * initialized to all zeros. When unplumbing lo0, all its logical
9461 	 * interfaces must be removed too.
9462 	 *
9463 	 * Note that this interface may be called to remove a specific
9464 	 * loopback logical interface (eg, lo0:1). But in that case
9465 	 * ipif->ipif_id != 0 so that the code path for that case is the
9466 	 * same as any other interface (meaning it skips the code directly
9467 	 * below).
9468 	 */
9469 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9470 		if (sin->sin_family == AF_UNSPEC &&
9471 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9472 			/*
9473 			 * Mark it condemned. No new ref. will be made to ill.
9474 			 */
9475 			mutex_enter(&ill->ill_lock);
9476 			ill->ill_state_flags |= ILL_CONDEMNED;
9477 			for (ipif = ill->ill_ipif; ipif != NULL;
9478 			    ipif = ipif->ipif_next) {
9479 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9480 			}
9481 			mutex_exit(&ill->ill_lock);
9482 
9483 			ipif = ill->ill_ipif;
9484 			/* unplumb the loopback interface */
9485 			ill_delete(ill);
9486 			mutex_enter(&connp->conn_lock);
9487 			mutex_enter(&ill->ill_lock);
9488 
9489 			/* Are any references to this ill active */
9490 			if (ill_is_freeable(ill)) {
9491 				mutex_exit(&ill->ill_lock);
9492 				mutex_exit(&connp->conn_lock);
9493 				ill_delete_tail(ill);
9494 				mi_free(ill);
9495 				return (0);
9496 			}
9497 			success = ipsq_pending_mp_add(connp, ipif,
9498 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9499 			mutex_exit(&connp->conn_lock);
9500 			mutex_exit(&ill->ill_lock);
9501 			if (success)
9502 				return (EINPROGRESS);
9503 			else
9504 				return (EINTR);
9505 		}
9506 	}
9507 
9508 	if (ipif->ipif_id == 0) {
9509 		ipsq_t *ipsq;
9510 
9511 		/* Find based on address */
9512 		if (ipif->ipif_isv6) {
9513 			sin6_t *sin6;
9514 
9515 			if (sin->sin_family != AF_INET6)
9516 				return (EAFNOSUPPORT);
9517 
9518 			sin6 = (sin6_t *)sin;
9519 			/* We are a writer, so we should be able to lookup */
9520 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9521 			    ipst);
9522 		} else {
9523 			if (sin->sin_family != AF_INET)
9524 				return (EAFNOSUPPORT);
9525 
9526 			/* We are a writer, so we should be able to lookup */
9527 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9528 			    ipst);
9529 		}
9530 		if (ipif == NULL) {
9531 			return (EADDRNOTAVAIL);
9532 		}
9533 
9534 		/*
9535 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9536 		 * lifr_name of the physical interface but with an ip address
9537 		 * lifr_addr of a logical interface plumbed over it.
9538 		 * So update ipx_current_ipif now that ipif points to the
9539 		 * correct one.
9540 		 */
9541 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9542 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9543 
9544 		/* This is a writer */
9545 		ipif_refrele(ipif);
9546 	}
9547 
9548 	/*
9549 	 * Can not delete instance zero since it is tied to the ill.
9550 	 */
9551 	if (ipif->ipif_id == 0)
9552 		return (EBUSY);
9553 
9554 	mutex_enter(&ill->ill_lock);
9555 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9556 	mutex_exit(&ill->ill_lock);
9557 
9558 	ipif_free(ipif);
9559 
9560 	mutex_enter(&connp->conn_lock);
9561 	mutex_enter(&ill->ill_lock);
9562 
9563 	/* Are any references to this ipif active */
9564 	if (ipif_is_freeable(ipif)) {
9565 		mutex_exit(&ill->ill_lock);
9566 		mutex_exit(&connp->conn_lock);
9567 		ipif_non_duplicate(ipif);
9568 		(void) ipif_down_tail(ipif);
9569 		ipif_free_tail(ipif); /* frees ipif */
9570 		return (0);
9571 	}
9572 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9573 	    IPIF_FREE);
9574 	mutex_exit(&ill->ill_lock);
9575 	mutex_exit(&connp->conn_lock);
9576 	if (success)
9577 		return (EINPROGRESS);
9578 	else
9579 		return (EINTR);
9580 }
9581 
9582 /*
9583  * Restart the removeif ioctl. The refcnt has gone down to 0.
9584  * The ipif is already condemned. So can't find it thru lookups.
9585  */
9586 /* ARGSUSED */
9587 int
9588 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9589     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9590 {
9591 	ill_t *ill = ipif->ipif_ill;
9592 
9593 	ASSERT(IAM_WRITER_IPIF(ipif));
9594 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9595 
9596 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9597 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9598 
9599 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9600 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9601 		ill_delete_tail(ill);
9602 		mi_free(ill);
9603 		return (0);
9604 	}
9605 
9606 	ipif_non_duplicate(ipif);
9607 	(void) ipif_down_tail(ipif);
9608 	ipif_free_tail(ipif);
9609 
9610 	return (0);
9611 }
9612 
9613 /*
9614  * Set the local interface address using the given prefix and ill_token.
9615  */
9616 /* ARGSUSED */
9617 int
9618 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9619     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9620 {
9621 	int err;
9622 	in6_addr_t v6addr;
9623 	sin6_t *sin6;
9624 	ill_t *ill;
9625 	int i;
9626 
9627 	ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9628 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9629 
9630 	ASSERT(IAM_WRITER_IPIF(ipif));
9631 
9632 	if (!ipif->ipif_isv6)
9633 		return (EINVAL);
9634 
9635 	if (sin->sin_family != AF_INET6)
9636 		return (EAFNOSUPPORT);
9637 
9638 	sin6 = (sin6_t *)sin;
9639 	v6addr = sin6->sin6_addr;
9640 	ill = ipif->ipif_ill;
9641 
9642 	if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9643 	    IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9644 		return (EADDRNOTAVAIL);
9645 
9646 	for (i = 0; i < 4; i++)
9647 		sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9648 
9649 	err = ip_sioctl_addr(ipif, sin, q, mp,
9650 	    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9651 	return (err);
9652 }
9653 
9654 /*
9655  * Restart entry point to restart the address set operation after the
9656  * refcounts have dropped to zero.
9657  */
9658 /* ARGSUSED */
9659 int
9660 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9661     ip_ioctl_cmd_t *ipip, void *ifreq)
9662 {
9663 	ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9664 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9665 	return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9666 }
9667 
9668 /*
9669  * Set the local interface address.
9670  * Allow an address of all zero when the interface is down.
9671  */
9672 /* ARGSUSED */
9673 int
9674 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9675     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9676 {
9677 	int err = 0;
9678 	in6_addr_t v6addr;
9679 	boolean_t need_up = B_FALSE;
9680 	ill_t *ill;
9681 	int i;
9682 
9683 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9684 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9685 
9686 	ASSERT(IAM_WRITER_IPIF(ipif));
9687 
9688 	ill = ipif->ipif_ill;
9689 	if (ipif->ipif_isv6) {
9690 		sin6_t *sin6;
9691 		phyint_t *phyi;
9692 
9693 		if (sin->sin_family != AF_INET6)
9694 			return (EAFNOSUPPORT);
9695 
9696 		sin6 = (sin6_t *)sin;
9697 		v6addr = sin6->sin6_addr;
9698 		phyi = ill->ill_phyint;
9699 
9700 		/*
9701 		 * Enforce that true multicast interfaces have a link-local
9702 		 * address for logical unit 0.
9703 		 *
9704 		 * However for those ipif's for which link-local address was
9705 		 * not created by default, also allow setting :: as the address.
9706 		 * This scenario would arise, when we delete an address on ipif
9707 		 * with logical unit 0, we would want to set :: as the address.
9708 		 */
9709 		if (ipif->ipif_id == 0 &&
9710 		    (ill->ill_flags & ILLF_MULTICAST) &&
9711 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9712 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9713 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9714 
9715 			/*
9716 			 * if default link-local was not created by kernel for
9717 			 * this ill, allow setting :: as the address on ipif:0.
9718 			 */
9719 			if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9720 				if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9721 					return (EADDRNOTAVAIL);
9722 			} else {
9723 				return (EADDRNOTAVAIL);
9724 			}
9725 		}
9726 
9727 		/*
9728 		 * up interfaces shouldn't have the unspecified address
9729 		 * unless they also have the IPIF_NOLOCAL flags set and
9730 		 * have a subnet assigned.
9731 		 */
9732 		if ((ipif->ipif_flags & IPIF_UP) &&
9733 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9734 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9735 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9736 			return (EADDRNOTAVAIL);
9737 		}
9738 
9739 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9740 			return (EADDRNOTAVAIL);
9741 	} else {
9742 		ipaddr_t addr;
9743 
9744 		if (sin->sin_family != AF_INET)
9745 			return (EAFNOSUPPORT);
9746 
9747 		addr = sin->sin_addr.s_addr;
9748 
9749 		/* Allow INADDR_ANY as the local address. */
9750 		if (addr != INADDR_ANY &&
9751 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9752 			return (EADDRNOTAVAIL);
9753 
9754 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9755 	}
9756 	/*
9757 	 * verify that the address being configured is permitted by the
9758 	 * ill_allowed_ips[] for the interface.
9759 	 */
9760 	if (ill->ill_allowed_ips_cnt > 0) {
9761 		for (i = 0; i < ill->ill_allowed_ips_cnt; i++) {
9762 			if (IN6_ARE_ADDR_EQUAL(&ill->ill_allowed_ips[i],
9763 			    &v6addr))
9764 				break;
9765 		}
9766 		if (i == ill->ill_allowed_ips_cnt) {
9767 			pr_addr_dbg("!allowed addr %s\n", AF_INET6, &v6addr);
9768 			return (EPERM);
9769 		}
9770 	}
9771 	/*
9772 	 * Even if there is no change we redo things just to rerun
9773 	 * ipif_set_default.
9774 	 */
9775 	if (ipif->ipif_flags & IPIF_UP) {
9776 		/*
9777 		 * Setting a new local address, make sure
9778 		 * we have net and subnet bcast ire's for
9779 		 * the old address if we need them.
9780 		 */
9781 		/*
9782 		 * If the interface is already marked up,
9783 		 * we call ipif_down which will take care
9784 		 * of ditching any IREs that have been set
9785 		 * up based on the old interface address.
9786 		 */
9787 		err = ipif_logical_down(ipif, q, mp);
9788 		if (err == EINPROGRESS)
9789 			return (err);
9790 		(void) ipif_down_tail(ipif);
9791 		need_up = 1;
9792 	}
9793 
9794 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9795 	return (err);
9796 }
9797 
9798 int
9799 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9800     boolean_t need_up)
9801 {
9802 	in6_addr_t v6addr;
9803 	in6_addr_t ov6addr;
9804 	ipaddr_t addr;
9805 	sin6_t	*sin6;
9806 	int	sinlen;
9807 	int	err = 0;
9808 	ill_t	*ill = ipif->ipif_ill;
9809 	boolean_t need_dl_down;
9810 	boolean_t need_arp_down;
9811 	struct iocblk *iocp;
9812 
9813 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9814 
9815 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9816 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9817 	ASSERT(IAM_WRITER_IPIF(ipif));
9818 
9819 	/* Must cancel any pending timer before taking the ill_lock */
9820 	if (ipif->ipif_recovery_id != 0)
9821 		(void) untimeout(ipif->ipif_recovery_id);
9822 	ipif->ipif_recovery_id = 0;
9823 
9824 	if (ipif->ipif_isv6) {
9825 		sin6 = (sin6_t *)sin;
9826 		v6addr = sin6->sin6_addr;
9827 		sinlen = sizeof (struct sockaddr_in6);
9828 	} else {
9829 		addr = sin->sin_addr.s_addr;
9830 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9831 		sinlen = sizeof (struct sockaddr_in);
9832 	}
9833 	mutex_enter(&ill->ill_lock);
9834 	ov6addr = ipif->ipif_v6lcl_addr;
9835 	ipif->ipif_v6lcl_addr = v6addr;
9836 	sctp_update_ipif_addr(ipif, ov6addr);
9837 	ipif->ipif_addr_ready = 0;
9838 
9839 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9840 
9841 	/*
9842 	 * If the interface was previously marked as a duplicate, then since
9843 	 * we've now got a "new" address, it should no longer be considered a
9844 	 * duplicate -- even if the "new" address is the same as the old one.
9845 	 * Note that if all ipifs are down, we may have a pending ARP down
9846 	 * event to handle.  This is because we want to recover from duplicates
9847 	 * and thus delay tearing down ARP until the duplicates have been
9848 	 * removed or disabled.
9849 	 */
9850 	need_dl_down = need_arp_down = B_FALSE;
9851 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9852 		need_arp_down = !need_up;
9853 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9854 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9855 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9856 			need_dl_down = B_TRUE;
9857 		}
9858 	}
9859 
9860 	ipif_set_default(ipif);
9861 
9862 	/*
9863 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9864 	 * tag the ill so that future updates to the interface ID don't result
9865 	 * in this address getting automatically reconfigured from under the
9866 	 * administrator.
9867 	 */
9868 	if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9869 		if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9870 		    !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9871 			ill->ill_manual_linklocal = 1;
9872 	}
9873 
9874 	/*
9875 	 * When publishing an interface address change event, we only notify
9876 	 * the event listeners of the new address.  It is assumed that if they
9877 	 * actively care about the addresses assigned that they will have
9878 	 * already discovered the previous address assigned (if there was one.)
9879 	 *
9880 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9881 	 */
9882 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9883 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9884 		    NE_ADDRESS_CHANGE, sin, sinlen);
9885 	}
9886 
9887 	mutex_exit(&ill->ill_lock);
9888 
9889 	if (need_up) {
9890 		/*
9891 		 * Now bring the interface back up.  If this
9892 		 * is the only IPIF for the ILL, ipif_up
9893 		 * will have to re-bind to the device, so
9894 		 * we may get back EINPROGRESS, in which
9895 		 * case, this IOCTL will get completed in
9896 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9897 		 */
9898 		err = ipif_up(ipif, q, mp);
9899 	} else {
9900 		/* Perhaps ilgs should use this ill */
9901 		update_conn_ill(NULL, ill->ill_ipst);
9902 	}
9903 
9904 	if (need_dl_down)
9905 		ill_dl_down(ill);
9906 
9907 	if (need_arp_down && !ill->ill_isv6)
9908 		(void) ipif_arp_down(ipif);
9909 
9910 	/*
9911 	 * The default multicast interface might have changed (for
9912 	 * instance if the IPv6 scope of the address changed)
9913 	 */
9914 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9915 
9916 	return (err);
9917 }
9918 
9919 /*
9920  * Restart entry point to restart the address set operation after the
9921  * refcounts have dropped to zero.
9922  */
9923 /* ARGSUSED */
9924 int
9925 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9926     ip_ioctl_cmd_t *ipip, void *ifreq)
9927 {
9928 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9929 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9930 	ASSERT(IAM_WRITER_IPIF(ipif));
9931 	(void) ipif_down_tail(ipif);
9932 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9933 }
9934 
9935 /* ARGSUSED */
9936 int
9937 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9938     ip_ioctl_cmd_t *ipip, void *if_req)
9939 {
9940 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9941 	struct lifreq *lifr = (struct lifreq *)if_req;
9942 
9943 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9944 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9945 	/*
9946 	 * The net mask and address can't change since we have a
9947 	 * reference to the ipif. So no lock is necessary.
9948 	 */
9949 	if (ipif->ipif_isv6) {
9950 		*sin6 = sin6_null;
9951 		sin6->sin6_family = AF_INET6;
9952 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9953 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
9954 			sin6->sin6_scope_id =
9955 			    ipif->ipif_ill->ill_phyint->phyint_ifindex;
9956 		}
9957 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9958 		lifr->lifr_addrlen =
9959 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9960 	} else {
9961 		*sin = sin_null;
9962 		sin->sin_family = AF_INET;
9963 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9964 		if (ipip->ipi_cmd_type == LIF_CMD) {
9965 			lifr->lifr_addrlen =
9966 			    ip_mask_to_plen(ipif->ipif_net_mask);
9967 		}
9968 	}
9969 	return (0);
9970 }
9971 
9972 /*
9973  * Set the destination address for a pt-pt interface.
9974  */
9975 /* ARGSUSED */
9976 int
9977 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9978     ip_ioctl_cmd_t *ipip, void *if_req)
9979 {
9980 	int err = 0;
9981 	in6_addr_t v6addr;
9982 	boolean_t need_up = B_FALSE;
9983 
9984 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9985 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9986 	ASSERT(IAM_WRITER_IPIF(ipif));
9987 
9988 	if (ipif->ipif_isv6) {
9989 		sin6_t *sin6;
9990 
9991 		if (sin->sin_family != AF_INET6)
9992 			return (EAFNOSUPPORT);
9993 
9994 		sin6 = (sin6_t *)sin;
9995 		v6addr = sin6->sin6_addr;
9996 
9997 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9998 			return (EADDRNOTAVAIL);
9999 	} else {
10000 		ipaddr_t addr;
10001 
10002 		if (sin->sin_family != AF_INET)
10003 			return (EAFNOSUPPORT);
10004 
10005 		addr = sin->sin_addr.s_addr;
10006 		if (addr != INADDR_ANY &&
10007 		    !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
10008 			return (EADDRNOTAVAIL);
10009 		}
10010 
10011 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10012 	}
10013 
10014 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10015 		return (0);	/* No change */
10016 
10017 	if (ipif->ipif_flags & IPIF_UP) {
10018 		/*
10019 		 * If the interface is already marked up,
10020 		 * we call ipif_down which will take care
10021 		 * of ditching any IREs that have been set
10022 		 * up based on the old pp dst address.
10023 		 */
10024 		err = ipif_logical_down(ipif, q, mp);
10025 		if (err == EINPROGRESS)
10026 			return (err);
10027 		(void) ipif_down_tail(ipif);
10028 		need_up = B_TRUE;
10029 	}
10030 	/*
10031 	 * could return EINPROGRESS. If so ioctl will complete in
10032 	 * ip_rput_dlpi_writer
10033 	 */
10034 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10035 	return (err);
10036 }
10037 
10038 static int
10039 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10040     boolean_t need_up)
10041 {
10042 	in6_addr_t v6addr;
10043 	ill_t	*ill = ipif->ipif_ill;
10044 	int	err = 0;
10045 	boolean_t need_dl_down;
10046 	boolean_t need_arp_down;
10047 
10048 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10049 	    ipif->ipif_id, (void *)ipif));
10050 
10051 	/* Must cancel any pending timer before taking the ill_lock */
10052 	if (ipif->ipif_recovery_id != 0)
10053 		(void) untimeout(ipif->ipif_recovery_id);
10054 	ipif->ipif_recovery_id = 0;
10055 
10056 	if (ipif->ipif_isv6) {
10057 		sin6_t *sin6;
10058 
10059 		sin6 = (sin6_t *)sin;
10060 		v6addr = sin6->sin6_addr;
10061 	} else {
10062 		ipaddr_t addr;
10063 
10064 		addr = sin->sin_addr.s_addr;
10065 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10066 	}
10067 	mutex_enter(&ill->ill_lock);
10068 	/* Set point to point destination address. */
10069 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10070 		/*
10071 		 * Allow this as a means of creating logical
10072 		 * pt-pt interfaces on top of e.g. an Ethernet.
10073 		 * XXX Undocumented HACK for testing.
10074 		 * pt-pt interfaces are created with NUD disabled.
10075 		 */
10076 		ipif->ipif_flags |= IPIF_POINTOPOINT;
10077 		ipif->ipif_flags &= ~IPIF_BROADCAST;
10078 		if (ipif->ipif_isv6)
10079 			ill->ill_flags |= ILLF_NONUD;
10080 	}
10081 
10082 	/*
10083 	 * If the interface was previously marked as a duplicate, then since
10084 	 * we've now got a "new" address, it should no longer be considered a
10085 	 * duplicate -- even if the "new" address is the same as the old one.
10086 	 * Note that if all ipifs are down, we may have a pending ARP down
10087 	 * event to handle.
10088 	 */
10089 	need_dl_down = need_arp_down = B_FALSE;
10090 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
10091 		need_arp_down = !need_up;
10092 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
10093 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10094 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10095 			need_dl_down = B_TRUE;
10096 		}
10097 	}
10098 
10099 	/*
10100 	 * If we've just manually set the IPv6 destination link-local address
10101 	 * (0th ipif), tag the ill so that future updates to the destination
10102 	 * interface ID (as can happen with interfaces over IP tunnels) don't
10103 	 * result in this address getting automatically reconfigured from
10104 	 * under the administrator.
10105 	 */
10106 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10107 		ill->ill_manual_dst_linklocal = 1;
10108 
10109 	/* Set the new address. */
10110 	ipif->ipif_v6pp_dst_addr = v6addr;
10111 	/* Make sure subnet tracks pp_dst */
10112 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10113 	mutex_exit(&ill->ill_lock);
10114 
10115 	if (need_up) {
10116 		/*
10117 		 * Now bring the interface back up.  If this
10118 		 * is the only IPIF for the ILL, ipif_up
10119 		 * will have to re-bind to the device, so
10120 		 * we may get back EINPROGRESS, in which
10121 		 * case, this IOCTL will get completed in
10122 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10123 		 */
10124 		err = ipif_up(ipif, q, mp);
10125 	}
10126 
10127 	if (need_dl_down)
10128 		ill_dl_down(ill);
10129 	if (need_arp_down && !ipif->ipif_isv6)
10130 		(void) ipif_arp_down(ipif);
10131 
10132 	return (err);
10133 }
10134 
10135 /*
10136  * Restart entry point to restart the dstaddress set operation after the
10137  * refcounts have dropped to zero.
10138  */
10139 /* ARGSUSED */
10140 int
10141 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10142     ip_ioctl_cmd_t *ipip, void *ifreq)
10143 {
10144 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10145 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10146 	(void) ipif_down_tail(ipif);
10147 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10148 }
10149 
10150 /* ARGSUSED */
10151 int
10152 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10153     ip_ioctl_cmd_t *ipip, void *if_req)
10154 {
10155 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
10156 
10157 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10158 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10159 	/*
10160 	 * Get point to point destination address. The addresses can't
10161 	 * change since we hold a reference to the ipif.
10162 	 */
10163 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10164 		return (EADDRNOTAVAIL);
10165 
10166 	if (ipif->ipif_isv6) {
10167 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10168 		*sin6 = sin6_null;
10169 		sin6->sin6_family = AF_INET6;
10170 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10171 	} else {
10172 		*sin = sin_null;
10173 		sin->sin_family = AF_INET;
10174 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10175 	}
10176 	return (0);
10177 }
10178 
10179 /*
10180  * Check which flags will change by the given flags being set
10181  * silently ignore flags which userland is not allowed to control.
10182  * (Because these flags may change between SIOCGLIFFLAGS and
10183  * SIOCSLIFFLAGS, and that's outside of userland's control,
10184  * we need to silently ignore them rather than fail.)
10185  */
10186 static void
10187 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10188     uint64_t *offp)
10189 {
10190 	ill_t		*ill = ipif->ipif_ill;
10191 	phyint_t	*phyi = ill->ill_phyint;
10192 	uint64_t	cantchange_flags, intf_flags;
10193 	uint64_t	turn_on, turn_off;
10194 
10195 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10196 	cantchange_flags = IFF_CANTCHANGE;
10197 	if (IS_IPMP(ill))
10198 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
10199 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10200 	turn_off = intf_flags & turn_on;
10201 	turn_on ^= turn_off;
10202 	*onp = turn_on;
10203 	*offp = turn_off;
10204 }
10205 
10206 /*
10207  * Set interface flags.  Many flags require special handling (e.g.,
10208  * bringing the interface down); see below for details.
10209  *
10210  * NOTE : We really don't enforce that ipif_id zero should be used
10211  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
10212  *	  is because applications generally does SICGLIFFLAGS and
10213  *	  ORs in the new flags (that affects the logical) and does a
10214  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10215  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10216  *	  flags that will be turned on is correct with respect to
10217  *	  ipif_id 0. For backward compatibility reasons, it is not done.
10218  */
10219 /* ARGSUSED */
10220 int
10221 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10222     ip_ioctl_cmd_t *ipip, void *if_req)
10223 {
10224 	uint64_t turn_on;
10225 	uint64_t turn_off;
10226 	int	err = 0;
10227 	phyint_t *phyi;
10228 	ill_t *ill;
10229 	conn_t *connp;
10230 	uint64_t intf_flags;
10231 	boolean_t phyint_flags_modified = B_FALSE;
10232 	uint64_t flags;
10233 	struct ifreq *ifr;
10234 	struct lifreq *lifr;
10235 	boolean_t set_linklocal = B_FALSE;
10236 
10237 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10238 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10239 
10240 	ASSERT(IAM_WRITER_IPIF(ipif));
10241 
10242 	ill = ipif->ipif_ill;
10243 	phyi = ill->ill_phyint;
10244 
10245 	if (ipip->ipi_cmd_type == IF_CMD) {
10246 		ifr = (struct ifreq *)if_req;
10247 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10248 	} else {
10249 		lifr = (struct lifreq *)if_req;
10250 		flags = lifr->lifr_flags;
10251 	}
10252 
10253 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10254 
10255 	/*
10256 	 * Have the flags been set correctly until now?
10257 	 */
10258 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10259 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10260 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10261 	/*
10262 	 * Compare the new flags to the old, and partition
10263 	 * into those coming on and those going off.
10264 	 * For the 16 bit command keep the bits above bit 16 unchanged.
10265 	 */
10266 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
10267 		flags |= intf_flags & ~0xFFFF;
10268 
10269 	/*
10270 	 * Explicitly fail attempts to change flags that are always invalid on
10271 	 * an IPMP meta-interface.
10272 	 */
10273 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10274 		return (EINVAL);
10275 
10276 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10277 	if ((turn_on|turn_off) == 0)
10278 		return (0);	/* No change */
10279 
10280 	/*
10281 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
10282 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10283 	 * allow it to be turned off.
10284 	 */
10285 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10286 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
10287 		return (EINVAL);
10288 
10289 	if ((connp = Q_TO_CONN(q)) == NULL)
10290 		return (EINVAL);
10291 
10292 	/*
10293 	 * Only vrrp control socket is allowed to change IFF_UP and
10294 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
10295 	 */
10296 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10297 		if (!connp->conn_isvrrp)
10298 			return (EINVAL);
10299 	}
10300 
10301 	/*
10302 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10303 	 * VRRP control socket.
10304 	 */
10305 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
10306 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10307 			return (EINVAL);
10308 	}
10309 
10310 	if (turn_on & IFF_NOFAILOVER) {
10311 		turn_on |= IFF_DEPRECATED;
10312 		flags |= IFF_DEPRECATED;
10313 	}
10314 
10315 	/*
10316 	 * On underlying interfaces, only allow applications to manage test
10317 	 * addresses -- otherwise, they may get confused when the address
10318 	 * moves as part of being brought up.  Likewise, prevent an
10319 	 * application-managed test address from being converted to a data
10320 	 * address.  To prevent migration of administratively up addresses in
10321 	 * the kernel, we don't allow them to be converted either.
10322 	 */
10323 	if (IS_UNDER_IPMP(ill)) {
10324 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10325 
10326 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10327 			return (EINVAL);
10328 
10329 		if ((turn_off & IFF_NOFAILOVER) &&
10330 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10331 			return (EINVAL);
10332 	}
10333 
10334 	/*
10335 	 * Only allow IFF_TEMPORARY flag to be set on
10336 	 * IPv6 interfaces.
10337 	 */
10338 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10339 		return (EINVAL);
10340 
10341 	/*
10342 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10343 	 */
10344 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10345 		return (EINVAL);
10346 
10347 	/*
10348 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10349 	 * interfaces.  It makes no sense in that context.
10350 	 */
10351 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10352 		return (EINVAL);
10353 
10354 	/*
10355 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10356 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10357 	 * If the link local address isn't set, and can be set, it will get
10358 	 * set later on in this function.
10359 	 */
10360 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10361 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10362 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10363 		if (ipif_cant_setlinklocal(ipif))
10364 			return (EINVAL);
10365 		set_linklocal = B_TRUE;
10366 	}
10367 
10368 	/*
10369 	 * If we modify physical interface flags, we'll potentially need to
10370 	 * send up two routing socket messages for the changes (one for the
10371 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10372 	 */
10373 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10374 		phyint_flags_modified = B_TRUE;
10375 
10376 	/*
10377 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10378 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10379 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10380 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10381 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10382 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10383 	 * will not be honored.
10384 	 */
10385 	if (turn_on & PHYI_STANDBY) {
10386 		/*
10387 		 * No need to grab ill_g_usesrc_lock here; see the
10388 		 * synchronization notes in ip.c.
10389 		 */
10390 		if (ill->ill_usesrc_grp_next != NULL ||
10391 		    intf_flags & PHYI_INACTIVE)
10392 			return (EINVAL);
10393 		if (!(flags & PHYI_FAILED)) {
10394 			flags |= PHYI_INACTIVE;
10395 			turn_on |= PHYI_INACTIVE;
10396 		}
10397 	}
10398 
10399 	if (turn_off & PHYI_STANDBY) {
10400 		flags &= ~PHYI_INACTIVE;
10401 		turn_off |= PHYI_INACTIVE;
10402 	}
10403 
10404 	/*
10405 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10406 	 * would end up on.
10407 	 */
10408 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10409 	    (PHYI_FAILED | PHYI_INACTIVE))
10410 		return (EINVAL);
10411 
10412 	/*
10413 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10414 	 * status of the interface.
10415 	 */
10416 	if ((turn_on | turn_off) & ILLF_ROUTER) {
10417 		err = ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10418 		if (err != 0)
10419 			return (err);
10420 	}
10421 
10422 	/*
10423 	 * If the interface is not UP and we are not going to
10424 	 * bring it UP, record the flags and return. When the
10425 	 * interface comes UP later, the right actions will be
10426 	 * taken.
10427 	 */
10428 	if (!(ipif->ipif_flags & IPIF_UP) &&
10429 	    !(turn_on & IPIF_UP)) {
10430 		/* Record new flags in their respective places. */
10431 		mutex_enter(&ill->ill_lock);
10432 		mutex_enter(&ill->ill_phyint->phyint_lock);
10433 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10434 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10435 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10436 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10437 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10438 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10439 		mutex_exit(&ill->ill_lock);
10440 		mutex_exit(&ill->ill_phyint->phyint_lock);
10441 
10442 		/*
10443 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10444 		 * same to the kernel: if any of them has been set by
10445 		 * userland, the interface cannot be used for data traffic.
10446 		 */
10447 		if ((turn_on|turn_off) &
10448 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10449 			ASSERT(!IS_IPMP(ill));
10450 			/*
10451 			 * It's possible the ill is part of an "anonymous"
10452 			 * IPMP group rather than a real group.  In that case,
10453 			 * there are no other interfaces in the group and thus
10454 			 * no need to call ipmp_phyint_refresh_active().
10455 			 */
10456 			if (IS_UNDER_IPMP(ill))
10457 				ipmp_phyint_refresh_active(phyi);
10458 		}
10459 
10460 		if (phyint_flags_modified) {
10461 			if (phyi->phyint_illv4 != NULL) {
10462 				ip_rts_ifmsg(phyi->phyint_illv4->
10463 				    ill_ipif, RTSQ_DEFAULT);
10464 			}
10465 			if (phyi->phyint_illv6 != NULL) {
10466 				ip_rts_ifmsg(phyi->phyint_illv6->
10467 				    ill_ipif, RTSQ_DEFAULT);
10468 			}
10469 		}
10470 		/* The default multicast interface might have changed */
10471 		ire_increment_multicast_generation(ill->ill_ipst,
10472 		    ill->ill_isv6);
10473 
10474 		return (0);
10475 	} else if (set_linklocal) {
10476 		mutex_enter(&ill->ill_lock);
10477 		if (set_linklocal)
10478 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10479 		mutex_exit(&ill->ill_lock);
10480 	}
10481 
10482 	/*
10483 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10484 	 * or point-to-point interfaces with an unspecified destination. We do
10485 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10486 	 * have a subnet assigned, which is how in.ndpd currently manages its
10487 	 * onlink prefix list when no addresses are configured with those
10488 	 * prefixes.
10489 	 */
10490 	if (ipif->ipif_isv6 &&
10491 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10492 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10493 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10494 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10495 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10496 		return (EINVAL);
10497 	}
10498 
10499 	/*
10500 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10501 	 * from being brought up.
10502 	 */
10503 	if (!ipif->ipif_isv6 &&
10504 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10505 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10506 		return (EINVAL);
10507 	}
10508 
10509 	/*
10510 	 * If we are going to change one or more of the flags that are
10511 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10512 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10513 	 * IPIF_NOFAILOVER, we will take special action.  This is
10514 	 * done by bring the ipif down, changing the flags and bringing
10515 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10516 	 * back up will trigger the address to be moved.
10517 	 *
10518 	 * If we are going to change IFF_NOACCEPT, we need to bring
10519 	 * all the ipifs down then bring them up again.	 The act of
10520 	 * bringing all the ipifs back up will trigger the local
10521 	 * ires being recreated with "no_accept" set/cleared.
10522 	 *
10523 	 * Note that ILLF_NOACCEPT is always set separately from the
10524 	 * other flags.
10525 	 */
10526 	if ((turn_on|turn_off) &
10527 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10528 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10529 	    IPIF_NOFAILOVER)) {
10530 		/*
10531 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10532 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10533 		 * entries shared between multiple ipifs on the same subnet.
10534 		 */
10535 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10536 		    !(turn_off & IPIF_UP)) {
10537 			if (ipif->ipif_flags & IPIF_UP)
10538 				ill->ill_logical_down = 1;
10539 			turn_on &= ~IPIF_UP;
10540 		}
10541 		err = ipif_down(ipif, q, mp);
10542 		ip1dbg(("ipif_down returns %d err ", err));
10543 		if (err == EINPROGRESS)
10544 			return (err);
10545 		(void) ipif_down_tail(ipif);
10546 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10547 		/*
10548 		 * If we can quiesce the ill, then continue.  If not, then
10549 		 * ip_sioctl_flags_tail() will be called from
10550 		 * ipif_ill_refrele_tail().
10551 		 */
10552 		ill_down_ipifs(ill, B_TRUE);
10553 
10554 		mutex_enter(&connp->conn_lock);
10555 		mutex_enter(&ill->ill_lock);
10556 		if (!ill_is_quiescent(ill)) {
10557 			boolean_t success;
10558 
10559 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10560 			    q, mp, ILL_DOWN);
10561 			mutex_exit(&ill->ill_lock);
10562 			mutex_exit(&connp->conn_lock);
10563 			return (success ? EINPROGRESS : EINTR);
10564 		}
10565 		mutex_exit(&ill->ill_lock);
10566 		mutex_exit(&connp->conn_lock);
10567 	}
10568 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10569 }
10570 
10571 static int
10572 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10573 {
10574 	ill_t	*ill;
10575 	phyint_t *phyi;
10576 	uint64_t turn_on, turn_off;
10577 	boolean_t phyint_flags_modified = B_FALSE;
10578 	int	err = 0;
10579 	boolean_t set_linklocal = B_FALSE;
10580 
10581 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10582 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10583 
10584 	ASSERT(IAM_WRITER_IPIF(ipif));
10585 
10586 	ill = ipif->ipif_ill;
10587 	phyi = ill->ill_phyint;
10588 
10589 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10590 
10591 	/*
10592 	 * IFF_UP is handled separately.
10593 	 */
10594 	turn_on &= ~IFF_UP;
10595 	turn_off &= ~IFF_UP;
10596 
10597 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10598 		phyint_flags_modified = B_TRUE;
10599 
10600 	/*
10601 	 * Now we change the flags. Track current value of
10602 	 * other flags in their respective places.
10603 	 */
10604 	mutex_enter(&ill->ill_lock);
10605 	mutex_enter(&phyi->phyint_lock);
10606 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10607 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10608 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10609 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10610 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10611 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10612 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10613 		set_linklocal = B_TRUE;
10614 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10615 	}
10616 
10617 	mutex_exit(&ill->ill_lock);
10618 	mutex_exit(&phyi->phyint_lock);
10619 
10620 	if (set_linklocal)
10621 		(void) ipif_setlinklocal(ipif);
10622 
10623 	/*
10624 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10625 	 * the kernel: if any of them has been set by userland, the interface
10626 	 * cannot be used for data traffic.
10627 	 */
10628 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10629 		ASSERT(!IS_IPMP(ill));
10630 		/*
10631 		 * It's possible the ill is part of an "anonymous" IPMP group
10632 		 * rather than a real group.  In that case, there are no other
10633 		 * interfaces in the group and thus no need for us to call
10634 		 * ipmp_phyint_refresh_active().
10635 		 */
10636 		if (IS_UNDER_IPMP(ill))
10637 			ipmp_phyint_refresh_active(phyi);
10638 	}
10639 
10640 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10641 		/*
10642 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10643 		 * ipifs that were brought down.
10644 		 *
10645 		 * The routing sockets messages are sent as the result
10646 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10647 		 * as well.
10648 		 */
10649 		err = ill_up_ipifs(ill, q, mp);
10650 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10651 		/*
10652 		 * XXX ipif_up really does not know whether a phyint flags
10653 		 * was modified or not. So, it sends up information on
10654 		 * only one routing sockets message. As we don't bring up
10655 		 * the interface and also set PHYI_ flags simultaneously
10656 		 * it should be okay.
10657 		 */
10658 		err = ipif_up(ipif, q, mp);
10659 	} else {
10660 		/*
10661 		 * Make sure routing socket sees all changes to the flags.
10662 		 * ipif_up_done* handles this when we use ipif_up.
10663 		 */
10664 		if (phyint_flags_modified) {
10665 			if (phyi->phyint_illv4 != NULL) {
10666 				ip_rts_ifmsg(phyi->phyint_illv4->
10667 				    ill_ipif, RTSQ_DEFAULT);
10668 			}
10669 			if (phyi->phyint_illv6 != NULL) {
10670 				ip_rts_ifmsg(phyi->phyint_illv6->
10671 				    ill_ipif, RTSQ_DEFAULT);
10672 			}
10673 		} else {
10674 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10675 		}
10676 		/*
10677 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10678 		 * this in need_up case.
10679 		 */
10680 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10681 	}
10682 
10683 	/* The default multicast interface might have changed */
10684 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10685 	return (err);
10686 }
10687 
10688 /*
10689  * Restart the flags operation now that the refcounts have dropped to zero.
10690  */
10691 /* ARGSUSED */
10692 int
10693 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10694     ip_ioctl_cmd_t *ipip, void *if_req)
10695 {
10696 	uint64_t flags;
10697 	struct ifreq *ifr = if_req;
10698 	struct lifreq *lifr = if_req;
10699 	uint64_t turn_on, turn_off;
10700 
10701 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10702 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10703 
10704 	if (ipip->ipi_cmd_type == IF_CMD) {
10705 		/* cast to uint16_t prevents unwanted sign extension */
10706 		flags = (uint16_t)ifr->ifr_flags;
10707 	} else {
10708 		flags = lifr->lifr_flags;
10709 	}
10710 
10711 	/*
10712 	 * If this function call is a result of the ILLF_NOACCEPT flag
10713 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10714 	 */
10715 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10716 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10717 		(void) ipif_down_tail(ipif);
10718 
10719 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10720 }
10721 
10722 /*
10723  * Can operate on either a module or a driver queue.
10724  */
10725 /* ARGSUSED */
10726 int
10727 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10728     ip_ioctl_cmd_t *ipip, void *if_req)
10729 {
10730 	/*
10731 	 * Has the flags been set correctly till now ?
10732 	 */
10733 	ill_t *ill = ipif->ipif_ill;
10734 	phyint_t *phyi = ill->ill_phyint;
10735 
10736 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10737 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10738 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10739 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10740 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10741 
10742 	/*
10743 	 * Need a lock since some flags can be set even when there are
10744 	 * references to the ipif.
10745 	 */
10746 	mutex_enter(&ill->ill_lock);
10747 	if (ipip->ipi_cmd_type == IF_CMD) {
10748 		struct ifreq *ifr = (struct ifreq *)if_req;
10749 
10750 		/* Get interface flags (low 16 only). */
10751 		ifr->ifr_flags = ((ipif->ipif_flags |
10752 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10753 	} else {
10754 		struct lifreq *lifr = (struct lifreq *)if_req;
10755 
10756 		/* Get interface flags. */
10757 		lifr->lifr_flags = ipif->ipif_flags |
10758 		    ill->ill_flags | phyi->phyint_flags;
10759 	}
10760 	mutex_exit(&ill->ill_lock);
10761 	return (0);
10762 }
10763 
10764 /*
10765  * We allow the MTU to be set on an ILL, but not have it be different
10766  * for different IPIFs since we don't actually send packets on IPIFs.
10767  */
10768 /* ARGSUSED */
10769 int
10770 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10771     ip_ioctl_cmd_t *ipip, void *if_req)
10772 {
10773 	int mtu;
10774 	int ip_min_mtu;
10775 	struct ifreq	*ifr;
10776 	struct lifreq *lifr;
10777 	ill_t	*ill;
10778 
10779 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10780 	    ipif->ipif_id, (void *)ipif));
10781 	if (ipip->ipi_cmd_type == IF_CMD) {
10782 		ifr = (struct ifreq *)if_req;
10783 		mtu = ifr->ifr_metric;
10784 	} else {
10785 		lifr = (struct lifreq *)if_req;
10786 		mtu = lifr->lifr_mtu;
10787 	}
10788 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10789 	if (ipif->ipif_id != 0)
10790 		return (EINVAL);
10791 
10792 	ill = ipif->ipif_ill;
10793 	if (ipif->ipif_isv6)
10794 		ip_min_mtu = IPV6_MIN_MTU;
10795 	else
10796 		ip_min_mtu = IP_MIN_MTU;
10797 
10798 	mutex_enter(&ill->ill_lock);
10799 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10800 		mutex_exit(&ill->ill_lock);
10801 		return (EINVAL);
10802 	}
10803 	/* Avoid increasing ill_mc_mtu */
10804 	if (ill->ill_mc_mtu > mtu)
10805 		ill->ill_mc_mtu = mtu;
10806 
10807 	/*
10808 	 * The dce and fragmentation code can handle changes to ill_mtu
10809 	 * concurrent with sending/fragmenting packets.
10810 	 */
10811 	ill->ill_mtu = mtu;
10812 	ill->ill_flags |= ILLF_FIXEDMTU;
10813 	mutex_exit(&ill->ill_lock);
10814 
10815 	/*
10816 	 * Make sure all dce_generation checks find out
10817 	 * that ill_mtu/ill_mc_mtu has changed.
10818 	 */
10819 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10820 
10821 	/*
10822 	 * Refresh IPMP meta-interface MTU if necessary.
10823 	 */
10824 	if (IS_UNDER_IPMP(ill))
10825 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
10826 
10827 	/* Update the MTU in SCTP's list */
10828 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10829 	return (0);
10830 }
10831 
10832 /* Get interface MTU. */
10833 /* ARGSUSED */
10834 int
10835 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10836     ip_ioctl_cmd_t *ipip, void *if_req)
10837 {
10838 	struct ifreq	*ifr;
10839 	struct lifreq	*lifr;
10840 
10841 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10842 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10843 
10844 	/*
10845 	 * We allow a get on any logical interface even though the set
10846 	 * can only be done on logical unit 0.
10847 	 */
10848 	if (ipip->ipi_cmd_type == IF_CMD) {
10849 		ifr = (struct ifreq *)if_req;
10850 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10851 	} else {
10852 		lifr = (struct lifreq *)if_req;
10853 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10854 	}
10855 	return (0);
10856 }
10857 
10858 /* Set interface broadcast address. */
10859 /* ARGSUSED2 */
10860 int
10861 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10862     ip_ioctl_cmd_t *ipip, void *if_req)
10863 {
10864 	ipaddr_t addr;
10865 	ire_t	*ire;
10866 	ill_t		*ill = ipif->ipif_ill;
10867 	ip_stack_t	*ipst = ill->ill_ipst;
10868 
10869 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10870 	    ipif->ipif_id));
10871 
10872 	ASSERT(IAM_WRITER_IPIF(ipif));
10873 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10874 		return (EADDRNOTAVAIL);
10875 
10876 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10877 
10878 	if (sin->sin_family != AF_INET)
10879 		return (EAFNOSUPPORT);
10880 
10881 	addr = sin->sin_addr.s_addr;
10882 
10883 	if (ipif->ipif_flags & IPIF_UP) {
10884 		/*
10885 		 * If we are already up, make sure the new
10886 		 * broadcast address makes sense.  If it does,
10887 		 * there should be an IRE for it already.
10888 		 */
10889 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10890 		    ill, ipif->ipif_zoneid, NULL,
10891 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10892 		if (ire == NULL) {
10893 			return (EINVAL);
10894 		} else {
10895 			ire_refrele(ire);
10896 		}
10897 	}
10898 	/*
10899 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10900 	 * needs to already exist we never need to change the set of
10901 	 * IRE_BROADCASTs when we are UP.
10902 	 */
10903 	if (addr != ipif->ipif_brd_addr)
10904 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10905 
10906 	return (0);
10907 }
10908 
10909 /* Get interface broadcast address. */
10910 /* ARGSUSED */
10911 int
10912 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10913     ip_ioctl_cmd_t *ipip, void *if_req)
10914 {
10915 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10916 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10917 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10918 		return (EADDRNOTAVAIL);
10919 
10920 	/* IPIF_BROADCAST not possible with IPv6 */
10921 	ASSERT(!ipif->ipif_isv6);
10922 	*sin = sin_null;
10923 	sin->sin_family = AF_INET;
10924 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10925 	return (0);
10926 }
10927 
10928 /*
10929  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10930  */
10931 /* ARGSUSED */
10932 int
10933 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10934     ip_ioctl_cmd_t *ipip, void *if_req)
10935 {
10936 	int err = 0;
10937 	in6_addr_t v6mask;
10938 
10939 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10940 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10941 
10942 	ASSERT(IAM_WRITER_IPIF(ipif));
10943 
10944 	if (ipif->ipif_isv6) {
10945 		sin6_t *sin6;
10946 
10947 		if (sin->sin_family != AF_INET6)
10948 			return (EAFNOSUPPORT);
10949 
10950 		sin6 = (sin6_t *)sin;
10951 		v6mask = sin6->sin6_addr;
10952 	} else {
10953 		ipaddr_t mask;
10954 
10955 		if (sin->sin_family != AF_INET)
10956 			return (EAFNOSUPPORT);
10957 
10958 		mask = sin->sin_addr.s_addr;
10959 		if (!ip_contiguous_mask(ntohl(mask)))
10960 			return (ENOTSUP);
10961 		V4MASK_TO_V6(mask, v6mask);
10962 	}
10963 
10964 	/*
10965 	 * No big deal if the interface isn't already up, or the mask
10966 	 * isn't really changing, or this is pt-pt.
10967 	 */
10968 	if (!(ipif->ipif_flags & IPIF_UP) ||
10969 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10970 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10971 		ipif->ipif_v6net_mask = v6mask;
10972 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10973 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10974 			    ipif->ipif_v6net_mask,
10975 			    ipif->ipif_v6subnet);
10976 		}
10977 		return (0);
10978 	}
10979 	/*
10980 	 * Make sure we have valid net and subnet broadcast ire's
10981 	 * for the old netmask, if needed by other logical interfaces.
10982 	 */
10983 	err = ipif_logical_down(ipif, q, mp);
10984 	if (err == EINPROGRESS)
10985 		return (err);
10986 	(void) ipif_down_tail(ipif);
10987 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10988 	return (err);
10989 }
10990 
10991 static int
10992 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10993 {
10994 	in6_addr_t v6mask;
10995 	int err = 0;
10996 
10997 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10998 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10999 
11000 	if (ipif->ipif_isv6) {
11001 		sin6_t *sin6;
11002 
11003 		sin6 = (sin6_t *)sin;
11004 		v6mask = sin6->sin6_addr;
11005 	} else {
11006 		ipaddr_t mask;
11007 
11008 		mask = sin->sin_addr.s_addr;
11009 		V4MASK_TO_V6(mask, v6mask);
11010 	}
11011 
11012 	ipif->ipif_v6net_mask = v6mask;
11013 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11014 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11015 		    ipif->ipif_v6subnet);
11016 	}
11017 	err = ipif_up(ipif, q, mp);
11018 
11019 	if (err == 0 || err == EINPROGRESS) {
11020 		/*
11021 		 * The interface must be DL_BOUND if this packet has to
11022 		 * go out on the wire. Since we only go through a logical
11023 		 * down and are bound with the driver during an internal
11024 		 * down/up that is satisfied.
11025 		 */
11026 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11027 			/* Potentially broadcast an address mask reply. */
11028 			ipif_mask_reply(ipif);
11029 		}
11030 	}
11031 	return (err);
11032 }
11033 
11034 /* ARGSUSED */
11035 int
11036 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11037     ip_ioctl_cmd_t *ipip, void *if_req)
11038 {
11039 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11040 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11041 	(void) ipif_down_tail(ipif);
11042 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11043 }
11044 
11045 /* Get interface net mask. */
11046 /* ARGSUSED */
11047 int
11048 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11049     ip_ioctl_cmd_t *ipip, void *if_req)
11050 {
11051 	struct lifreq *lifr = (struct lifreq *)if_req;
11052 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11053 
11054 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11055 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11056 
11057 	/*
11058 	 * net mask can't change since we have a reference to the ipif.
11059 	 */
11060 	if (ipif->ipif_isv6) {
11061 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11062 		*sin6 = sin6_null;
11063 		sin6->sin6_family = AF_INET6;
11064 		sin6->sin6_addr = ipif->ipif_v6net_mask;
11065 		lifr->lifr_addrlen =
11066 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11067 	} else {
11068 		*sin = sin_null;
11069 		sin->sin_family = AF_INET;
11070 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
11071 		if (ipip->ipi_cmd_type == LIF_CMD) {
11072 			lifr->lifr_addrlen =
11073 			    ip_mask_to_plen(ipif->ipif_net_mask);
11074 		}
11075 	}
11076 	return (0);
11077 }
11078 
11079 /* ARGSUSED */
11080 int
11081 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11082     ip_ioctl_cmd_t *ipip, void *if_req)
11083 {
11084 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11085 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11086 
11087 	/*
11088 	 * Since no applications should ever be setting metrics on underlying
11089 	 * interfaces, we explicitly fail to smoke 'em out.
11090 	 */
11091 	if (IS_UNDER_IPMP(ipif->ipif_ill))
11092 		return (EINVAL);
11093 
11094 	/*
11095 	 * Set interface metric.  We don't use this for
11096 	 * anything but we keep track of it in case it is
11097 	 * important to routing applications or such.
11098 	 */
11099 	if (ipip->ipi_cmd_type == IF_CMD) {
11100 		struct ifreq    *ifr;
11101 
11102 		ifr = (struct ifreq *)if_req;
11103 		ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11104 	} else {
11105 		struct lifreq   *lifr;
11106 
11107 		lifr = (struct lifreq *)if_req;
11108 		ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11109 	}
11110 	return (0);
11111 }
11112 
11113 /* ARGSUSED */
11114 int
11115 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11116     ip_ioctl_cmd_t *ipip, void *if_req)
11117 {
11118 	/* Get interface metric. */
11119 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11120 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11121 
11122 	if (ipip->ipi_cmd_type == IF_CMD) {
11123 		struct ifreq    *ifr;
11124 
11125 		ifr = (struct ifreq *)if_req;
11126 		ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11127 	} else {
11128 		struct lifreq   *lifr;
11129 
11130 		lifr = (struct lifreq *)if_req;
11131 		lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11132 	}
11133 
11134 	return (0);
11135 }
11136 
11137 /* ARGSUSED */
11138 int
11139 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11140     ip_ioctl_cmd_t *ipip, void *if_req)
11141 {
11142 	int	arp_muxid;
11143 
11144 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11145 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11146 	/*
11147 	 * Set the muxid returned from I_PLINK.
11148 	 */
11149 	if (ipip->ipi_cmd_type == IF_CMD) {
11150 		struct ifreq *ifr = (struct ifreq *)if_req;
11151 
11152 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11153 		arp_muxid = ifr->ifr_arp_muxid;
11154 	} else {
11155 		struct lifreq *lifr = (struct lifreq *)if_req;
11156 
11157 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11158 		arp_muxid = lifr->lifr_arp_muxid;
11159 	}
11160 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
11161 	return (0);
11162 }
11163 
11164 /* ARGSUSED */
11165 int
11166 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11167     ip_ioctl_cmd_t *ipip, void *if_req)
11168 {
11169 	int	arp_muxid = 0;
11170 
11171 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11172 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11173 	/*
11174 	 * Get the muxid saved in ill for I_PUNLINK.
11175 	 */
11176 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
11177 	if (ipip->ipi_cmd_type == IF_CMD) {
11178 		struct ifreq *ifr = (struct ifreq *)if_req;
11179 
11180 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11181 		ifr->ifr_arp_muxid = arp_muxid;
11182 	} else {
11183 		struct lifreq *lifr = (struct lifreq *)if_req;
11184 
11185 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11186 		lifr->lifr_arp_muxid = arp_muxid;
11187 	}
11188 	return (0);
11189 }
11190 
11191 /*
11192  * Set the subnet prefix. Does not modify the broadcast address.
11193  */
11194 /* ARGSUSED */
11195 int
11196 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11197     ip_ioctl_cmd_t *ipip, void *if_req)
11198 {
11199 	int err = 0;
11200 	in6_addr_t v6addr;
11201 	in6_addr_t v6mask;
11202 	boolean_t need_up = B_FALSE;
11203 	int addrlen;
11204 
11205 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11206 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11207 
11208 	ASSERT(IAM_WRITER_IPIF(ipif));
11209 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11210 
11211 	if (ipif->ipif_isv6) {
11212 		sin6_t *sin6;
11213 
11214 		if (sin->sin_family != AF_INET6)
11215 			return (EAFNOSUPPORT);
11216 
11217 		sin6 = (sin6_t *)sin;
11218 		v6addr = sin6->sin6_addr;
11219 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11220 			return (EADDRNOTAVAIL);
11221 	} else {
11222 		ipaddr_t addr;
11223 
11224 		if (sin->sin_family != AF_INET)
11225 			return (EAFNOSUPPORT);
11226 
11227 		addr = sin->sin_addr.s_addr;
11228 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11229 			return (EADDRNOTAVAIL);
11230 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11231 		/* Add 96 bits */
11232 		addrlen += IPV6_ABITS - IP_ABITS;
11233 	}
11234 
11235 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11236 		return (EINVAL);
11237 
11238 	/* Check if bits in the address is set past the mask */
11239 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11240 		return (EINVAL);
11241 
11242 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11243 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11244 		return (0);	/* No change */
11245 
11246 	if (ipif->ipif_flags & IPIF_UP) {
11247 		/*
11248 		 * If the interface is already marked up,
11249 		 * we call ipif_down which will take care
11250 		 * of ditching any IREs that have been set
11251 		 * up based on the old interface address.
11252 		 */
11253 		err = ipif_logical_down(ipif, q, mp);
11254 		if (err == EINPROGRESS)
11255 			return (err);
11256 		(void) ipif_down_tail(ipif);
11257 		need_up = B_TRUE;
11258 	}
11259 
11260 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11261 	return (err);
11262 }
11263 
11264 static int
11265 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11266     queue_t *q, mblk_t *mp, boolean_t need_up)
11267 {
11268 	ill_t	*ill = ipif->ipif_ill;
11269 	int	err = 0;
11270 
11271 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11272 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11273 
11274 	/* Set the new address. */
11275 	mutex_enter(&ill->ill_lock);
11276 	ipif->ipif_v6net_mask = v6mask;
11277 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11278 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11279 		    ipif->ipif_v6subnet);
11280 	}
11281 	mutex_exit(&ill->ill_lock);
11282 
11283 	if (need_up) {
11284 		/*
11285 		 * Now bring the interface back up.  If this
11286 		 * is the only IPIF for the ILL, ipif_up
11287 		 * will have to re-bind to the device, so
11288 		 * we may get back EINPROGRESS, in which
11289 		 * case, this IOCTL will get completed in
11290 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11291 		 */
11292 		err = ipif_up(ipif, q, mp);
11293 		if (err == EINPROGRESS)
11294 			return (err);
11295 	}
11296 	return (err);
11297 }
11298 
11299 /* ARGSUSED */
11300 int
11301 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11302     ip_ioctl_cmd_t *ipip, void *if_req)
11303 {
11304 	int	addrlen;
11305 	in6_addr_t v6addr;
11306 	in6_addr_t v6mask;
11307 	struct lifreq *lifr = (struct lifreq *)if_req;
11308 
11309 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11310 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11311 	(void) ipif_down_tail(ipif);
11312 
11313 	addrlen = lifr->lifr_addrlen;
11314 	if (ipif->ipif_isv6) {
11315 		sin6_t *sin6;
11316 
11317 		sin6 = (sin6_t *)sin;
11318 		v6addr = sin6->sin6_addr;
11319 	} else {
11320 		ipaddr_t addr;
11321 
11322 		addr = sin->sin_addr.s_addr;
11323 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11324 		addrlen += IPV6_ABITS - IP_ABITS;
11325 	}
11326 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
11327 
11328 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11329 }
11330 
11331 /* ARGSUSED */
11332 int
11333 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11334     ip_ioctl_cmd_t *ipip, void *if_req)
11335 {
11336 	struct lifreq *lifr = (struct lifreq *)if_req;
11337 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11338 
11339 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11340 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11341 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11342 
11343 	if (ipif->ipif_isv6) {
11344 		*sin6 = sin6_null;
11345 		sin6->sin6_family = AF_INET6;
11346 		sin6->sin6_addr = ipif->ipif_v6subnet;
11347 		lifr->lifr_addrlen =
11348 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11349 	} else {
11350 		*sin = sin_null;
11351 		sin->sin_family = AF_INET;
11352 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11353 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11354 	}
11355 	return (0);
11356 }
11357 
11358 /*
11359  * Set the IPv6 address token.
11360  */
11361 /* ARGSUSED */
11362 int
11363 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11364     ip_ioctl_cmd_t *ipi, void *if_req)
11365 {
11366 	ill_t *ill = ipif->ipif_ill;
11367 	int err;
11368 	in6_addr_t v6addr;
11369 	in6_addr_t v6mask;
11370 	boolean_t need_up = B_FALSE;
11371 	int i;
11372 	sin6_t *sin6 = (sin6_t *)sin;
11373 	struct lifreq *lifr = (struct lifreq *)if_req;
11374 	int addrlen;
11375 
11376 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11377 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11378 	ASSERT(IAM_WRITER_IPIF(ipif));
11379 
11380 	addrlen = lifr->lifr_addrlen;
11381 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11382 	if (ipif->ipif_id != 0)
11383 		return (EINVAL);
11384 
11385 	if (!ipif->ipif_isv6)
11386 		return (EINVAL);
11387 
11388 	if (addrlen > IPV6_ABITS)
11389 		return (EINVAL);
11390 
11391 	v6addr = sin6->sin6_addr;
11392 
11393 	/*
11394 	 * The length of the token is the length from the end.  To get
11395 	 * the proper mask for this, compute the mask of the bits not
11396 	 * in the token; ie. the prefix, and then xor to get the mask.
11397 	 */
11398 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11399 		return (EINVAL);
11400 	for (i = 0; i < 4; i++) {
11401 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11402 	}
11403 
11404 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11405 	    ill->ill_token_length == addrlen)
11406 		return (0);	/* No change */
11407 
11408 	if (ipif->ipif_flags & IPIF_UP) {
11409 		err = ipif_logical_down(ipif, q, mp);
11410 		if (err == EINPROGRESS)
11411 			return (err);
11412 		(void) ipif_down_tail(ipif);
11413 		need_up = B_TRUE;
11414 	}
11415 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11416 	return (err);
11417 }
11418 
11419 static int
11420 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11421     mblk_t *mp, boolean_t need_up)
11422 {
11423 	in6_addr_t v6addr;
11424 	in6_addr_t v6mask;
11425 	ill_t	*ill = ipif->ipif_ill;
11426 	int	i;
11427 	int	err = 0;
11428 
11429 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11430 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11431 	v6addr = sin6->sin6_addr;
11432 	/*
11433 	 * The length of the token is the length from the end.  To get
11434 	 * the proper mask for this, compute the mask of the bits not
11435 	 * in the token; ie. the prefix, and then xor to get the mask.
11436 	 */
11437 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11438 	for (i = 0; i < 4; i++)
11439 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11440 
11441 	mutex_enter(&ill->ill_lock);
11442 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11443 	ill->ill_token_length = addrlen;
11444 	ill->ill_manual_token = 1;
11445 
11446 	/* Reconfigure the link-local address based on this new token */
11447 	ipif_setlinklocal(ill->ill_ipif);
11448 
11449 	mutex_exit(&ill->ill_lock);
11450 
11451 	if (need_up) {
11452 		/*
11453 		 * Now bring the interface back up.  If this
11454 		 * is the only IPIF for the ILL, ipif_up
11455 		 * will have to re-bind to the device, so
11456 		 * we may get back EINPROGRESS, in which
11457 		 * case, this IOCTL will get completed in
11458 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11459 		 */
11460 		err = ipif_up(ipif, q, mp);
11461 		if (err == EINPROGRESS)
11462 			return (err);
11463 	}
11464 	return (err);
11465 }
11466 
11467 /* ARGSUSED */
11468 int
11469 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11470     ip_ioctl_cmd_t *ipi, void *if_req)
11471 {
11472 	ill_t *ill;
11473 	sin6_t *sin6 = (sin6_t *)sin;
11474 	struct lifreq *lifr = (struct lifreq *)if_req;
11475 
11476 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11477 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11478 	if (ipif->ipif_id != 0)
11479 		return (EINVAL);
11480 
11481 	ill = ipif->ipif_ill;
11482 	if (!ill->ill_isv6)
11483 		return (ENXIO);
11484 
11485 	*sin6 = sin6_null;
11486 	sin6->sin6_family = AF_INET6;
11487 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11488 	sin6->sin6_addr = ill->ill_token;
11489 	lifr->lifr_addrlen = ill->ill_token_length;
11490 	return (0);
11491 }
11492 
11493 /*
11494  * Set (hardware) link specific information that might override
11495  * what was acquired through the DL_INFO_ACK.
11496  */
11497 /* ARGSUSED */
11498 int
11499 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11500     ip_ioctl_cmd_t *ipi, void *if_req)
11501 {
11502 	ill_t		*ill = ipif->ipif_ill;
11503 	int		ip_min_mtu;
11504 	struct lifreq	*lifr = (struct lifreq *)if_req;
11505 	lif_ifinfo_req_t *lir;
11506 
11507 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11508 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11509 	lir = &lifr->lifr_ifinfo;
11510 	ASSERT(IAM_WRITER_IPIF(ipif));
11511 
11512 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11513 	if (ipif->ipif_id != 0)
11514 		return (EINVAL);
11515 
11516 	/* Set interface MTU. */
11517 	if (ipif->ipif_isv6)
11518 		ip_min_mtu = IPV6_MIN_MTU;
11519 	else
11520 		ip_min_mtu = IP_MIN_MTU;
11521 
11522 	/*
11523 	 * Verify values before we set anything. Allow zero to
11524 	 * mean unspecified.
11525 	 *
11526 	 * XXX We should be able to set the user-defined lir_mtu to some value
11527 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11528 	 * ill_max_frag value tells us the max MTU that can be handled by the
11529 	 * datalink, whereas the ill_current_frag is dynamically computed for
11530 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11531 	 * since there is currently no way of distinguishing between
11532 	 * administratively fixed link mtu values (e.g., those set via
11533 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11534 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11535 	 * upper-bound.
11536 	 */
11537 	if (lir->lir_maxmtu != 0 &&
11538 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11539 	    lir->lir_maxmtu < ip_min_mtu))
11540 		return (EINVAL);
11541 	if (lir->lir_reachtime != 0 &&
11542 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11543 		return (EINVAL);
11544 	if (lir->lir_reachretrans != 0 &&
11545 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11546 		return (EINVAL);
11547 
11548 	mutex_enter(&ill->ill_lock);
11549 	/*
11550 	 * The dce and fragmentation code can handle changes to ill_mtu
11551 	 * concurrent with sending/fragmenting packets.
11552 	 */
11553 	if (lir->lir_maxmtu != 0)
11554 		ill->ill_user_mtu = lir->lir_maxmtu;
11555 
11556 	if (lir->lir_reachtime != 0)
11557 		ill->ill_reachable_time = lir->lir_reachtime;
11558 
11559 	if (lir->lir_reachretrans != 0)
11560 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11561 
11562 	ill->ill_max_hops = lir->lir_maxhops;
11563 	ill->ill_max_buf = ND_MAX_Q;
11564 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11565 		/*
11566 		 * ill_mtu is the actual interface MTU, obtained as the min
11567 		 * of user-configured mtu and the value announced by the
11568 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11569 		 * we have already made the choice of requiring
11570 		 * ill_user_mtu < ill_current_frag by the time we get here,
11571 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11572 		 * here.
11573 		 */
11574 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11575 		ill->ill_mc_mtu = MIN(ill->ill_mc_mtu, ill->ill_user_mtu);
11576 	}
11577 	mutex_exit(&ill->ill_lock);
11578 
11579 	/*
11580 	 * Make sure all dce_generation checks find out
11581 	 * that ill_mtu/ill_mc_mtu has changed.
11582 	 */
11583 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11584 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11585 
11586 	/*
11587 	 * Refresh IPMP meta-interface MTU if necessary.
11588 	 */
11589 	if (IS_UNDER_IPMP(ill))
11590 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11591 
11592 	return (0);
11593 }
11594 
11595 /* ARGSUSED */
11596 int
11597 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11598     ip_ioctl_cmd_t *ipi, void *if_req)
11599 {
11600 	struct lif_ifinfo_req *lir;
11601 	ill_t *ill = ipif->ipif_ill;
11602 
11603 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11604 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11605 	if (ipif->ipif_id != 0)
11606 		return (EINVAL);
11607 
11608 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11609 	lir->lir_maxhops = ill->ill_max_hops;
11610 	lir->lir_reachtime = ill->ill_reachable_time;
11611 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11612 	lir->lir_maxmtu = ill->ill_mtu;
11613 
11614 	return (0);
11615 }
11616 
11617 /*
11618  * Return best guess as to the subnet mask for the specified address.
11619  * Based on the subnet masks for all the configured interfaces.
11620  *
11621  * We end up returning a zero mask in the case of default, multicast or
11622  * experimental.
11623  */
11624 static ipaddr_t
11625 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11626 {
11627 	ipaddr_t net_mask;
11628 	ill_t	*ill;
11629 	ipif_t	*ipif;
11630 	ill_walk_context_t ctx;
11631 	ipif_t	*fallback_ipif = NULL;
11632 
11633 	net_mask = ip_net_mask(addr);
11634 	if (net_mask == 0) {
11635 		*ipifp = NULL;
11636 		return (0);
11637 	}
11638 
11639 	/* Let's check to see if this is maybe a local subnet route. */
11640 	/* this function only applies to IPv4 interfaces */
11641 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11642 	ill = ILL_START_WALK_V4(&ctx, ipst);
11643 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11644 		mutex_enter(&ill->ill_lock);
11645 		for (ipif = ill->ill_ipif; ipif != NULL;
11646 		    ipif = ipif->ipif_next) {
11647 			if (IPIF_IS_CONDEMNED(ipif))
11648 				continue;
11649 			if (!(ipif->ipif_flags & IPIF_UP))
11650 				continue;
11651 			if ((ipif->ipif_subnet & net_mask) ==
11652 			    (addr & net_mask)) {
11653 				/*
11654 				 * Don't trust pt-pt interfaces if there are
11655 				 * other interfaces.
11656 				 */
11657 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11658 					if (fallback_ipif == NULL) {
11659 						ipif_refhold_locked(ipif);
11660 						fallback_ipif = ipif;
11661 					}
11662 					continue;
11663 				}
11664 
11665 				/*
11666 				 * Fine. Just assume the same net mask as the
11667 				 * directly attached subnet interface is using.
11668 				 */
11669 				ipif_refhold_locked(ipif);
11670 				mutex_exit(&ill->ill_lock);
11671 				rw_exit(&ipst->ips_ill_g_lock);
11672 				if (fallback_ipif != NULL)
11673 					ipif_refrele(fallback_ipif);
11674 				*ipifp = ipif;
11675 				return (ipif->ipif_net_mask);
11676 			}
11677 		}
11678 		mutex_exit(&ill->ill_lock);
11679 	}
11680 	rw_exit(&ipst->ips_ill_g_lock);
11681 
11682 	*ipifp = fallback_ipif;
11683 	return ((fallback_ipif != NULL) ?
11684 	    fallback_ipif->ipif_net_mask : net_mask);
11685 }
11686 
11687 /*
11688  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11689  */
11690 static void
11691 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11692 {
11693 	IOCP	iocp;
11694 	ipft_t	*ipft;
11695 	ipllc_t	*ipllc;
11696 	mblk_t	*mp1;
11697 	cred_t	*cr;
11698 	int	error = 0;
11699 	conn_t	*connp;
11700 
11701 	ip1dbg(("ip_wput_ioctl"));
11702 	iocp = (IOCP)mp->b_rptr;
11703 	mp1 = mp->b_cont;
11704 	if (mp1 == NULL) {
11705 		iocp->ioc_error = EINVAL;
11706 		mp->b_datap->db_type = M_IOCNAK;
11707 		iocp->ioc_count = 0;
11708 		qreply(q, mp);
11709 		return;
11710 	}
11711 
11712 	/*
11713 	 * These IOCTLs provide various control capabilities to
11714 	 * upstream agents such as ULPs and processes.	There
11715 	 * are currently two such IOCTLs implemented.  They
11716 	 * are used by TCP to provide update information for
11717 	 * existing IREs and to forcibly delete an IRE for a
11718 	 * host that is not responding, thereby forcing an
11719 	 * attempt at a new route.
11720 	 */
11721 	iocp->ioc_error = EINVAL;
11722 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11723 		goto done;
11724 
11725 	ipllc = (ipllc_t *)mp1->b_rptr;
11726 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11727 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11728 			break;
11729 	}
11730 	/*
11731 	 * prefer credential from mblk over ioctl;
11732 	 * see ip_sioctl_copyin_setup
11733 	 */
11734 	cr = msg_getcred(mp, NULL);
11735 	if (cr == NULL)
11736 		cr = iocp->ioc_cr;
11737 
11738 	/*
11739 	 * Refhold the conn in case the request gets queued up in some lookup
11740 	 */
11741 	ASSERT(CONN_Q(q));
11742 	connp = Q_TO_CONN(q);
11743 	CONN_INC_REF(connp);
11744 	CONN_INC_IOCTLREF(connp);
11745 	if (ipft->ipft_pfi &&
11746 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11747 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11748 		error = (*ipft->ipft_pfi)(q,
11749 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11750 	}
11751 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11752 		/*
11753 		 * CONN_OPER_PENDING_DONE happens in the function called
11754 		 * through ipft_pfi above.
11755 		 */
11756 		return;
11757 	}
11758 
11759 	CONN_DEC_IOCTLREF(connp);
11760 	CONN_OPER_PENDING_DONE(connp);
11761 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11762 		freemsg(mp);
11763 		return;
11764 	}
11765 	iocp->ioc_error = error;
11766 
11767 done:
11768 	mp->b_datap->db_type = M_IOCACK;
11769 	if (iocp->ioc_error)
11770 		iocp->ioc_count = 0;
11771 	qreply(q, mp);
11772 }
11773 
11774 /*
11775  * Assign a unique id for the ipif. This is used by sctp_addr.c
11776  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11777  */
11778 static void
11779 ipif_assign_seqid(ipif_t *ipif)
11780 {
11781 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11782 
11783 	ipif->ipif_seqid = atomic_inc_64_nv(&ipst->ips_ipif_g_seqid);
11784 }
11785 
11786 /*
11787  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11788  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11789  * that the clone is complete -- including the seqid -- and the expectation is
11790  * that the caller will either free or overwrite `sipif' before it's unlocked.
11791  */
11792 static void
11793 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11794 {
11795 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11796 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11797 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11798 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11799 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11800 
11801 	dipif->ipif_flags = sipif->ipif_flags;
11802 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11803 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11804 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11805 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11806 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11807 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11808 
11809 	/*
11810 	 * As per the comment atop the function, we assume that these sipif
11811 	 * fields will be changed before sipif is unlocked.
11812 	 */
11813 	dipif->ipif_seqid = sipif->ipif_seqid;
11814 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11815 }
11816 
11817 /*
11818  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11819  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11820  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11821  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11822  * down (i.e., no DAD), of the same type, and unlocked.
11823  */
11824 static void
11825 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11826 {
11827 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11828 	ipxop_t *ipx = ipsq->ipsq_xop;
11829 
11830 	ASSERT(sipif != dipif);
11831 	ASSERT(sipif != virgipif);
11832 
11833 	/*
11834 	 * Grab all of the locks that protect the ipif in a defined order.
11835 	 */
11836 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11837 
11838 	ipif_clone(sipif, dipif);
11839 	if (virgipif != NULL) {
11840 		ipif_clone(virgipif, sipif);
11841 		mi_free(virgipif);
11842 	}
11843 
11844 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11845 
11846 	/*
11847 	 * Transfer ownership of the current xop, if necessary.
11848 	 */
11849 	if (ipx->ipx_current_ipif == sipif) {
11850 		ASSERT(ipx->ipx_pending_ipif == NULL);
11851 		mutex_enter(&ipx->ipx_lock);
11852 		ipx->ipx_current_ipif = dipif;
11853 		mutex_exit(&ipx->ipx_lock);
11854 	}
11855 
11856 	if (virgipif == NULL)
11857 		mi_free(sipif);
11858 }
11859 
11860 /*
11861  * checks if:
11862  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11863  *	- logical interface is within the allowed range
11864  */
11865 static int
11866 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11867 {
11868 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11869 		return (ENAMETOOLONG);
11870 
11871 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11872 		return (ERANGE);
11873 	return (0);
11874 }
11875 
11876 /*
11877  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11878  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11879  * be inserted into the first space available in the list. The value of
11880  * ipif_id will then be set to the appropriate value for its position.
11881  */
11882 static int
11883 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11884 {
11885 	ill_t *ill;
11886 	ipif_t *tipif;
11887 	ipif_t **tipifp;
11888 	int id, err;
11889 	ip_stack_t	*ipst;
11890 
11891 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11892 	    IAM_WRITER_IPIF(ipif));
11893 
11894 	ill = ipif->ipif_ill;
11895 	ASSERT(ill != NULL);
11896 	ipst = ill->ill_ipst;
11897 
11898 	/*
11899 	 * In the case of lo0:0 we already hold the ill_g_lock.
11900 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11901 	 * ipif_insert.
11902 	 */
11903 	if (acquire_g_lock)
11904 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11905 	mutex_enter(&ill->ill_lock);
11906 	id = ipif->ipif_id;
11907 	tipifp = &(ill->ill_ipif);
11908 	if (id == -1) {	/* need to find a real id */
11909 		id = 0;
11910 		while ((tipif = *tipifp) != NULL) {
11911 			ASSERT(tipif->ipif_id >= id);
11912 			if (tipif->ipif_id != id)
11913 				break; /* non-consecutive id */
11914 			id++;
11915 			tipifp = &(tipif->ipif_next);
11916 		}
11917 		if ((err = is_lifname_valid(ill, id)) != 0) {
11918 			mutex_exit(&ill->ill_lock);
11919 			if (acquire_g_lock)
11920 				rw_exit(&ipst->ips_ill_g_lock);
11921 			return (err);
11922 		}
11923 		ipif->ipif_id = id; /* assign new id */
11924 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11925 		/* we have a real id; insert ipif in the right place */
11926 		while ((tipif = *tipifp) != NULL) {
11927 			ASSERT(tipif->ipif_id != id);
11928 			if (tipif->ipif_id > id)
11929 				break; /* found correct location */
11930 			tipifp = &(tipif->ipif_next);
11931 		}
11932 	} else {
11933 		mutex_exit(&ill->ill_lock);
11934 		if (acquire_g_lock)
11935 			rw_exit(&ipst->ips_ill_g_lock);
11936 		return (err);
11937 	}
11938 
11939 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11940 
11941 	ipif->ipif_next = tipif;
11942 	*tipifp = ipif;
11943 	mutex_exit(&ill->ill_lock);
11944 	if (acquire_g_lock)
11945 		rw_exit(&ipst->ips_ill_g_lock);
11946 
11947 	return (0);
11948 }
11949 
11950 static void
11951 ipif_remove(ipif_t *ipif)
11952 {
11953 	ipif_t	**ipifp;
11954 	ill_t	*ill = ipif->ipif_ill;
11955 
11956 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11957 
11958 	mutex_enter(&ill->ill_lock);
11959 	ipifp = &ill->ill_ipif;
11960 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11961 		if (*ipifp == ipif) {
11962 			*ipifp = ipif->ipif_next;
11963 			break;
11964 		}
11965 	}
11966 	mutex_exit(&ill->ill_lock);
11967 }
11968 
11969 /*
11970  * Allocate and initialize a new interface control structure.  (Always
11971  * called as writer.)
11972  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11973  * is not part of the global linked list of ills. ipif_seqid is unique
11974  * in the system and to preserve the uniqueness, it is assigned only
11975  * when ill becomes part of the global list. At that point ill will
11976  * have a name. If it doesn't get assigned here, it will get assigned
11977  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11978  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11979  * the interface flags or any other information from the DL_INFO_ACK for
11980  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11981  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11982  * second DL_INFO_ACK comes in from the driver.
11983  */
11984 static ipif_t *
11985 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11986     boolean_t insert, int *errorp)
11987 {
11988 	int err;
11989 	ipif_t	*ipif;
11990 	ip_stack_t *ipst = ill->ill_ipst;
11991 
11992 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11993 	    ill->ill_name, id, (void *)ill));
11994 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11995 
11996 	if (errorp != NULL)
11997 		*errorp = 0;
11998 
11999 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
12000 		if (errorp != NULL)
12001 			*errorp = ENOMEM;
12002 		return (NULL);
12003 	}
12004 	*ipif = ipif_zero;	/* start clean */
12005 
12006 	ipif->ipif_ill = ill;
12007 	ipif->ipif_id = id;	/* could be -1 */
12008 	/*
12009 	 * Inherit the zoneid from the ill; for the shared stack instance
12010 	 * this is always the global zone
12011 	 */
12012 	ipif->ipif_zoneid = ill->ill_zoneid;
12013 
12014 	ipif->ipif_refcnt = 0;
12015 
12016 	if (insert) {
12017 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
12018 			mi_free(ipif);
12019 			if (errorp != NULL)
12020 				*errorp = err;
12021 			return (NULL);
12022 		}
12023 		/* -1 id should have been replaced by real id */
12024 		id = ipif->ipif_id;
12025 		ASSERT(id >= 0);
12026 	}
12027 
12028 	if (ill->ill_name[0] != '\0')
12029 		ipif_assign_seqid(ipif);
12030 
12031 	/*
12032 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
12033 	 * (which must not exist yet because the zeroth ipif is created once
12034 	 * per ill).  However, do not not link it to the ipmp_grp_t until
12035 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
12036 	 */
12037 	if (id == 0 && IS_IPMP(ill)) {
12038 		if (ipmp_illgrp_create(ill) == NULL) {
12039 			if (insert) {
12040 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
12041 				ipif_remove(ipif);
12042 				rw_exit(&ipst->ips_ill_g_lock);
12043 			}
12044 			mi_free(ipif);
12045 			if (errorp != NULL)
12046 				*errorp = ENOMEM;
12047 			return (NULL);
12048 		}
12049 	}
12050 
12051 	/*
12052 	 * We grab ill_lock to protect the flag changes.  The ipif is still
12053 	 * not up and can't be looked up until the ioctl completes and the
12054 	 * IPIF_CHANGING flag is cleared.
12055 	 */
12056 	mutex_enter(&ill->ill_lock);
12057 
12058 	ipif->ipif_ire_type = ire_type;
12059 
12060 	if (ipif->ipif_isv6) {
12061 		ill->ill_flags |= ILLF_IPV6;
12062 	} else {
12063 		ipaddr_t inaddr_any = INADDR_ANY;
12064 
12065 		ill->ill_flags |= ILLF_IPV4;
12066 
12067 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12068 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12069 		    &ipif->ipif_v6lcl_addr);
12070 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12071 		    &ipif->ipif_v6subnet);
12072 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12073 		    &ipif->ipif_v6net_mask);
12074 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12075 		    &ipif->ipif_v6brd_addr);
12076 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12077 		    &ipif->ipif_v6pp_dst_addr);
12078 	}
12079 
12080 	/*
12081 	 * Don't set the interface flags etc. now, will do it in
12082 	 * ip_ll_subnet_defaults.
12083 	 */
12084 	if (!initialize)
12085 		goto out;
12086 
12087 	/*
12088 	 * NOTE: The IPMP meta-interface is special-cased because it starts
12089 	 * with no underlying interfaces (and thus an unknown broadcast
12090 	 * address length), but all interfaces that can be placed into an IPMP
12091 	 * group are required to be broadcast-capable.
12092 	 */
12093 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12094 		/*
12095 		 * Later detect lack of DLPI driver multicast capability by
12096 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12097 		 */
12098 		ill->ill_flags |= ILLF_MULTICAST;
12099 		if (!ipif->ipif_isv6)
12100 			ipif->ipif_flags |= IPIF_BROADCAST;
12101 	} else {
12102 		if (ill->ill_net_type != IRE_LOOPBACK) {
12103 			if (ipif->ipif_isv6)
12104 				/*
12105 				 * Note: xresolv interfaces will eventually need
12106 				 * NOARP set here as well, but that will require
12107 				 * those external resolvers to have some
12108 				 * knowledge of that flag and act appropriately.
12109 				 * Not to be changed at present.
12110 				 */
12111 				ill->ill_flags |= ILLF_NONUD;
12112 			else
12113 				ill->ill_flags |= ILLF_NOARP;
12114 		}
12115 		if (ill->ill_phys_addr_length == 0) {
12116 			if (IS_VNI(ill)) {
12117 				ipif->ipif_flags |= IPIF_NOXMIT;
12118 			} else {
12119 				/* pt-pt supports multicast. */
12120 				ill->ill_flags |= ILLF_MULTICAST;
12121 				if (ill->ill_net_type != IRE_LOOPBACK)
12122 					ipif->ipif_flags |= IPIF_POINTOPOINT;
12123 			}
12124 		}
12125 	}
12126 out:
12127 	mutex_exit(&ill->ill_lock);
12128 	return (ipif);
12129 }
12130 
12131 /*
12132  * Remove the neighbor cache entries associated with this logical
12133  * interface.
12134  */
12135 int
12136 ipif_arp_down(ipif_t *ipif)
12137 {
12138 	ill_t	*ill = ipif->ipif_ill;
12139 	int	err = 0;
12140 
12141 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12142 	ASSERT(IAM_WRITER_IPIF(ipif));
12143 
12144 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12145 	    ill_t *, ill, ipif_t *, ipif);
12146 	ipif_nce_down(ipif);
12147 
12148 	/*
12149 	 * If this is the last ipif that is going down and there are no
12150 	 * duplicate addresses we may yet attempt to re-probe, then we need to
12151 	 * clean up ARP completely.
12152 	 */
12153 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12154 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12155 		/*
12156 		 * If this was the last ipif on an IPMP interface, purge any
12157 		 * static ARP entries associated with it.
12158 		 */
12159 		if (IS_IPMP(ill))
12160 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
12161 
12162 		/* UNBIND, DETACH */
12163 		err = arp_ll_down(ill);
12164 	}
12165 
12166 	return (err);
12167 }
12168 
12169 /*
12170  * Get the resolver set up for a new IP address.  (Always called as writer.)
12171  * Called both for IPv4 and IPv6 interfaces, though it only does some
12172  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12173  *
12174  * The enumerated value res_act tunes the behavior:
12175  *	* Res_act_initial: set up all the resolver structures for a new
12176  *	  IP address.
12177  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
12178  *	  ARP message in defense of the address.
12179  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
12180  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
12181  *
12182  * Returns zero on success, or an errno upon failure.
12183  */
12184 int
12185 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12186 {
12187 	ill_t		*ill = ipif->ipif_ill;
12188 	int		err;
12189 	boolean_t	was_dup;
12190 
12191 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12192 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12193 	ASSERT(IAM_WRITER_IPIF(ipif));
12194 
12195 	was_dup = B_FALSE;
12196 	if (res_act == Res_act_initial) {
12197 		ipif->ipif_addr_ready = 0;
12198 		/*
12199 		 * We're bringing an interface up here.  There's no way that we
12200 		 * should need to shut down ARP now.
12201 		 */
12202 		mutex_enter(&ill->ill_lock);
12203 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
12204 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
12205 			ill->ill_ipif_dup_count--;
12206 			was_dup = B_TRUE;
12207 		}
12208 		mutex_exit(&ill->ill_lock);
12209 	}
12210 	if (ipif->ipif_recovery_id != 0)
12211 		(void) untimeout(ipif->ipif_recovery_id);
12212 	ipif->ipif_recovery_id = 0;
12213 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
12214 		ipif->ipif_addr_ready = 1;
12215 		return (0);
12216 	}
12217 	/* NDP will set the ipif_addr_ready flag when it's ready */
12218 	if (ill->ill_isv6)
12219 		return (0);
12220 
12221 	err = ipif_arp_up(ipif, res_act, was_dup);
12222 	return (err);
12223 }
12224 
12225 /*
12226  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12227  * when a link has just gone back up.
12228  */
12229 static void
12230 ipif_nce_start_dad(ipif_t *ipif)
12231 {
12232 	ncec_t *ncec;
12233 	ill_t *ill = ipif->ipif_ill;
12234 	boolean_t isv6 = ill->ill_isv6;
12235 
12236 	if (isv6) {
12237 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12238 		    &ipif->ipif_v6lcl_addr);
12239 	} else {
12240 		ipaddr_t v4addr;
12241 
12242 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
12243 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12244 		    ipif->ipif_lcl_addr == INADDR_ANY) {
12245 			/*
12246 			 * If we can't contact ARP for some reason,
12247 			 * that's not really a problem.  Just send
12248 			 * out the routing socket notification that
12249 			 * DAD completion would have done, and continue.
12250 			 */
12251 			ipif_mask_reply(ipif);
12252 			ipif_up_notify(ipif);
12253 			ipif->ipif_addr_ready = 1;
12254 			return;
12255 		}
12256 
12257 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12258 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12259 	}
12260 
12261 	if (ncec == NULL) {
12262 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12263 		    (void *)ipif));
12264 		return;
12265 	}
12266 	if (!nce_restart_dad(ncec)) {
12267 		/*
12268 		 * If we can't restart DAD for some reason, that's not really a
12269 		 * problem.  Just send out the routing socket notification that
12270 		 * DAD completion would have done, and continue.
12271 		 */
12272 		ipif_up_notify(ipif);
12273 		ipif->ipif_addr_ready = 1;
12274 	}
12275 	ncec_refrele(ncec);
12276 }
12277 
12278 /*
12279  * Restart duplicate address detection on all interfaces on the given ill.
12280  *
12281  * This is called when an interface transitions from down to up
12282  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12283  *
12284  * Note that since the underlying physical link has transitioned, we must cause
12285  * at least one routing socket message to be sent here, either via DAD
12286  * completion or just by default on the first ipif.  (If we don't do this, then
12287  * in.mpathd will see long delays when doing link-based failure recovery.)
12288  */
12289 void
12290 ill_restart_dad(ill_t *ill, boolean_t went_up)
12291 {
12292 	ipif_t *ipif;
12293 
12294 	if (ill == NULL)
12295 		return;
12296 
12297 	/*
12298 	 * If layer two doesn't support duplicate address detection, then just
12299 	 * send the routing socket message now and be done with it.
12300 	 */
12301 	if (!ill->ill_isv6 && arp_no_defense) {
12302 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12303 		return;
12304 	}
12305 
12306 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12307 		if (went_up) {
12308 
12309 			if (ipif->ipif_flags & IPIF_UP) {
12310 				ipif_nce_start_dad(ipif);
12311 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12312 				/*
12313 				 * kick off the bring-up process now.
12314 				 */
12315 				ipif_do_recovery(ipif);
12316 			} else {
12317 				/*
12318 				 * Unfortunately, the first ipif is "special"
12319 				 * and represents the underlying ill in the
12320 				 * routing socket messages.  Thus, when this
12321 				 * one ipif is down, we must still notify so
12322 				 * that the user knows the IFF_RUNNING status
12323 				 * change.  (If the first ipif is up, then
12324 				 * we'll handle eventual routing socket
12325 				 * notification via DAD completion.)
12326 				 */
12327 				if (ipif == ill->ill_ipif) {
12328 					ip_rts_ifmsg(ill->ill_ipif,
12329 					    RTSQ_DEFAULT);
12330 				}
12331 			}
12332 		} else {
12333 			/*
12334 			 * After link down, we'll need to send a new routing
12335 			 * message when the link comes back, so clear
12336 			 * ipif_addr_ready.
12337 			 */
12338 			ipif->ipif_addr_ready = 0;
12339 		}
12340 	}
12341 
12342 	/*
12343 	 * If we've torn down links, then notify the user right away.
12344 	 */
12345 	if (!went_up)
12346 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12347 }
12348 
12349 static void
12350 ipsq_delete(ipsq_t *ipsq)
12351 {
12352 	ipxop_t *ipx = ipsq->ipsq_xop;
12353 
12354 	ipsq->ipsq_ipst = NULL;
12355 	ASSERT(ipsq->ipsq_phyint == NULL);
12356 	ASSERT(ipsq->ipsq_xop != NULL);
12357 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12358 	ASSERT(ipx->ipx_pending_mp == NULL);
12359 	kmem_free(ipsq, sizeof (ipsq_t));
12360 }
12361 
12362 static int
12363 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12364 {
12365 	int err = 0;
12366 	ipif_t *ipif;
12367 
12368 	if (ill == NULL)
12369 		return (0);
12370 
12371 	ASSERT(IAM_WRITER_ILL(ill));
12372 	ill->ill_up_ipifs = B_TRUE;
12373 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12374 		if (ipif->ipif_was_up) {
12375 			if (!(ipif->ipif_flags & IPIF_UP))
12376 				err = ipif_up(ipif, q, mp);
12377 			ipif->ipif_was_up = B_FALSE;
12378 			if (err != 0) {
12379 				ASSERT(err == EINPROGRESS);
12380 				return (err);
12381 			}
12382 		}
12383 	}
12384 	ill->ill_up_ipifs = B_FALSE;
12385 	return (0);
12386 }
12387 
12388 /*
12389  * This function is called to bring up all the ipifs that were up before
12390  * bringing the ill down via ill_down_ipifs().
12391  */
12392 int
12393 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12394 {
12395 	int err;
12396 
12397 	ASSERT(IAM_WRITER_ILL(ill));
12398 
12399 	if (ill->ill_replumbing) {
12400 		ill->ill_replumbing = 0;
12401 		/*
12402 		 * Send down REPLUMB_DONE notification followed by the
12403 		 * BIND_REQ on the arp stream.
12404 		 */
12405 		if (!ill->ill_isv6)
12406 			arp_send_replumb_conf(ill);
12407 	}
12408 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12409 	if (err != 0)
12410 		return (err);
12411 
12412 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12413 }
12414 
12415 /*
12416  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12417  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12418  */
12419 static void
12420 ill_down_ipifs(ill_t *ill, boolean_t logical)
12421 {
12422 	ipif_t *ipif;
12423 
12424 	ASSERT(IAM_WRITER_ILL(ill));
12425 
12426 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12427 		/*
12428 		 * We go through the ipif_down logic even if the ipif
12429 		 * is already down, since routes can be added based
12430 		 * on down ipifs. Going through ipif_down once again
12431 		 * will delete any IREs created based on these routes.
12432 		 */
12433 		if (ipif->ipif_flags & IPIF_UP)
12434 			ipif->ipif_was_up = B_TRUE;
12435 
12436 		if (logical) {
12437 			(void) ipif_logical_down(ipif, NULL, NULL);
12438 			ipif_non_duplicate(ipif);
12439 			(void) ipif_down_tail(ipif);
12440 		} else {
12441 			(void) ipif_down(ipif, NULL, NULL);
12442 		}
12443 	}
12444 }
12445 
12446 /*
12447  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12448  * a look again at valid source addresses.
12449  * This should be called each time after the set of source addresses has been
12450  * changed.
12451  */
12452 void
12453 ip_update_source_selection(ip_stack_t *ipst)
12454 {
12455 	/* We skip past SRC_GENERATION_VERIFY */
12456 	if (atomic_inc_32_nv(&ipst->ips_src_generation) ==
12457 	    SRC_GENERATION_VERIFY)
12458 		atomic_inc_32(&ipst->ips_src_generation);
12459 }
12460 
12461 /*
12462  * Finish the group join started in ip_sioctl_groupname().
12463  */
12464 /* ARGSUSED */
12465 static void
12466 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12467 {
12468 	ill_t		*ill = q->q_ptr;
12469 	phyint_t	*phyi = ill->ill_phyint;
12470 	ipmp_grp_t	*grp = phyi->phyint_grp;
12471 	ip_stack_t	*ipst = ill->ill_ipst;
12472 
12473 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12474 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12475 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12476 
12477 	if (phyi->phyint_illv4 != NULL) {
12478 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12479 		VERIFY(grp->gr_pendv4-- > 0);
12480 		rw_exit(&ipst->ips_ipmp_lock);
12481 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12482 	}
12483 	if (phyi->phyint_illv6 != NULL) {
12484 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12485 		VERIFY(grp->gr_pendv6-- > 0);
12486 		rw_exit(&ipst->ips_ipmp_lock);
12487 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12488 	}
12489 	freemsg(mp);
12490 }
12491 
12492 /*
12493  * Process an SIOCSLIFGROUPNAME request.
12494  */
12495 /* ARGSUSED */
12496 int
12497 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12498     ip_ioctl_cmd_t *ipip, void *ifreq)
12499 {
12500 	struct lifreq	*lifr = ifreq;
12501 	ill_t		*ill = ipif->ipif_ill;
12502 	ip_stack_t	*ipst = ill->ill_ipst;
12503 	phyint_t	*phyi = ill->ill_phyint;
12504 	ipmp_grp_t	*grp = phyi->phyint_grp;
12505 	mblk_t		*ipsq_mp;
12506 	int		err = 0;
12507 
12508 	/*
12509 	 * Note that phyint_grp can only change here, where we're exclusive.
12510 	 */
12511 	ASSERT(IAM_WRITER_ILL(ill));
12512 
12513 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12514 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12515 		return (EINVAL);
12516 
12517 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12518 
12519 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12520 
12521 	/*
12522 	 * If the name hasn't changed, there's nothing to do.
12523 	 */
12524 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12525 		goto unlock;
12526 
12527 	/*
12528 	 * Handle requests to rename an IPMP meta-interface.
12529 	 *
12530 	 * Note that creation of the IPMP meta-interface is handled in
12531 	 * userland through the standard plumbing sequence.  As part of the
12532 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12533 	 * the name of the interface (see ipif_set_values_tail()).
12534 	 */
12535 	if (IS_IPMP(ill)) {
12536 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12537 		goto unlock;
12538 	}
12539 
12540 	/*
12541 	 * Handle requests to add or remove an IP interface from a group.
12542 	 */
12543 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12544 		/*
12545 		 * Moves are handled by first removing the interface from
12546 		 * its existing group, and then adding it to another group.
12547 		 * So, fail if it's already in a group.
12548 		 */
12549 		if (IS_UNDER_IPMP(ill)) {
12550 			err = EALREADY;
12551 			goto unlock;
12552 		}
12553 
12554 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12555 		if (grp == NULL) {
12556 			err = ENOENT;
12557 			goto unlock;
12558 		}
12559 
12560 		/*
12561 		 * Check if the phyint and its ills are suitable for
12562 		 * inclusion into the group.
12563 		 */
12564 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12565 			goto unlock;
12566 
12567 		/*
12568 		 * Checks pass; join the group, and enqueue the remaining
12569 		 * illgrp joins for when we've become part of the group xop
12570 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12571 		 * requires an mblk_t to scribble on, and since `mp' will be
12572 		 * freed as part of completing the ioctl, allocate another.
12573 		 */
12574 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12575 			err = ENOMEM;
12576 			goto unlock;
12577 		}
12578 
12579 		/*
12580 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12581 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12582 		 * before ip_join_illgrps() is called back.  See the comments
12583 		 * in ip_sioctl_plink_ipmp() for more.
12584 		 */
12585 		if (phyi->phyint_illv4 != NULL)
12586 			grp->gr_pendv4++;
12587 		if (phyi->phyint_illv6 != NULL)
12588 			grp->gr_pendv6++;
12589 
12590 		rw_exit(&ipst->ips_ipmp_lock);
12591 
12592 		ipmp_phyint_join_grp(phyi, grp);
12593 		ill_refhold(ill);
12594 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12595 		    SWITCH_OP, B_FALSE);
12596 		return (0);
12597 	} else {
12598 		/*
12599 		 * Request to remove the interface from a group.  If the
12600 		 * interface is not in a group, this trivially succeeds.
12601 		 */
12602 		rw_exit(&ipst->ips_ipmp_lock);
12603 		if (IS_UNDER_IPMP(ill))
12604 			ipmp_phyint_leave_grp(phyi);
12605 		return (0);
12606 	}
12607 unlock:
12608 	rw_exit(&ipst->ips_ipmp_lock);
12609 	return (err);
12610 }
12611 
12612 /*
12613  * Process an SIOCGLIFBINDING request.
12614  */
12615 /* ARGSUSED */
12616 int
12617 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12618     ip_ioctl_cmd_t *ipip, void *ifreq)
12619 {
12620 	ill_t		*ill;
12621 	struct lifreq	*lifr = ifreq;
12622 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12623 
12624 	if (!IS_IPMP(ipif->ipif_ill))
12625 		return (EINVAL);
12626 
12627 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12628 	if ((ill = ipif->ipif_bound_ill) == NULL)
12629 		lifr->lifr_binding[0] = '\0';
12630 	else
12631 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12632 	rw_exit(&ipst->ips_ipmp_lock);
12633 	return (0);
12634 }
12635 
12636 /*
12637  * Process an SIOCGLIFGROUPNAME request.
12638  */
12639 /* ARGSUSED */
12640 int
12641 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12642     ip_ioctl_cmd_t *ipip, void *ifreq)
12643 {
12644 	ipmp_grp_t	*grp;
12645 	struct lifreq	*lifr = ifreq;
12646 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12647 
12648 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12649 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12650 		lifr->lifr_groupname[0] = '\0';
12651 	else
12652 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12653 	rw_exit(&ipst->ips_ipmp_lock);
12654 	return (0);
12655 }
12656 
12657 /*
12658  * Process an SIOCGLIFGROUPINFO request.
12659  */
12660 /* ARGSUSED */
12661 int
12662 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12663     ip_ioctl_cmd_t *ipip, void *dummy)
12664 {
12665 	ipmp_grp_t	*grp;
12666 	lifgroupinfo_t	*lifgr;
12667 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12668 
12669 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12670 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12671 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12672 
12673 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12674 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12675 		rw_exit(&ipst->ips_ipmp_lock);
12676 		return (ENOENT);
12677 	}
12678 	ipmp_grp_info(grp, lifgr);
12679 	rw_exit(&ipst->ips_ipmp_lock);
12680 	return (0);
12681 }
12682 
12683 static void
12684 ill_dl_down(ill_t *ill)
12685 {
12686 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12687 
12688 	/*
12689 	 * The ill is down; unbind but stay attached since we're still
12690 	 * associated with a PPA. If we have negotiated DLPI capabilites
12691 	 * with the data link service provider (IDS_OK) then reset them.
12692 	 * The interval between unbinding and rebinding is potentially
12693 	 * unbounded hence we cannot assume things will be the same.
12694 	 * The DLPI capabilities will be probed again when the data link
12695 	 * is brought up.
12696 	 */
12697 	mblk_t	*mp = ill->ill_unbind_mp;
12698 
12699 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12700 
12701 	if (!ill->ill_replumbing) {
12702 		/* Free all ilms for this ill */
12703 		update_conn_ill(ill, ill->ill_ipst);
12704 	} else {
12705 		ill_leave_multicast(ill);
12706 	}
12707 
12708 	ill->ill_unbind_mp = NULL;
12709 	if (mp != NULL) {
12710 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12711 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12712 		    ill->ill_name));
12713 		mutex_enter(&ill->ill_lock);
12714 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12715 		mutex_exit(&ill->ill_lock);
12716 		/*
12717 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12718 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12719 		 * ill_capability_dld_disable disable rightaway. If this is not
12720 		 * an unplumb operation then the disable happens on receipt of
12721 		 * the capab ack via ip_rput_dlpi_writer ->
12722 		 * ill_capability_ack_thr. In both cases the order of
12723 		 * the operations seen by DLD is capability disable followed
12724 		 * by DL_UNBIND. Also the DLD capability disable needs a
12725 		 * cv_wait'able context.
12726 		 */
12727 		if (ill->ill_state_flags & ILL_CONDEMNED)
12728 			ill_capability_dld_disable(ill);
12729 		ill_capability_reset(ill, B_FALSE);
12730 		ill_dlpi_send(ill, mp);
12731 	}
12732 	mutex_enter(&ill->ill_lock);
12733 	ill->ill_dl_up = 0;
12734 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12735 	mutex_exit(&ill->ill_lock);
12736 }
12737 
12738 void
12739 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12740 {
12741 	union DL_primitives *dlp;
12742 	t_uscalar_t prim;
12743 	boolean_t waitack = B_FALSE;
12744 
12745 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12746 
12747 	dlp = (union DL_primitives *)mp->b_rptr;
12748 	prim = dlp->dl_primitive;
12749 
12750 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12751 	    dl_primstr(prim), prim, ill->ill_name));
12752 
12753 	switch (prim) {
12754 	case DL_PHYS_ADDR_REQ:
12755 	{
12756 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12757 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12758 		break;
12759 	}
12760 	case DL_BIND_REQ:
12761 		mutex_enter(&ill->ill_lock);
12762 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12763 		mutex_exit(&ill->ill_lock);
12764 		break;
12765 	}
12766 
12767 	/*
12768 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12769 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12770 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12771 	 */
12772 	mutex_enter(&ill->ill_lock);
12773 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12774 	    (prim == DL_UNBIND_REQ)) {
12775 		ill->ill_dlpi_pending = prim;
12776 		waitack = B_TRUE;
12777 	}
12778 
12779 	mutex_exit(&ill->ill_lock);
12780 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12781 	    char *, dl_primstr(prim), ill_t *, ill);
12782 	putnext(ill->ill_wq, mp);
12783 
12784 	/*
12785 	 * There is no ack for DL_NOTIFY_CONF messages
12786 	 */
12787 	if (waitack && prim == DL_NOTIFY_CONF)
12788 		ill_dlpi_done(ill, prim);
12789 }
12790 
12791 /*
12792  * Helper function for ill_dlpi_send().
12793  */
12794 /* ARGSUSED */
12795 static void
12796 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12797 {
12798 	ill_dlpi_send(q->q_ptr, mp);
12799 }
12800 
12801 /*
12802  * Send a DLPI control message to the driver but make sure there
12803  * is only one outstanding message. Uses ill_dlpi_pending to tell
12804  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12805  * when an ACK or a NAK is received to process the next queued message.
12806  */
12807 void
12808 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12809 {
12810 	mblk_t **mpp;
12811 
12812 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12813 
12814 	/*
12815 	 * To ensure that any DLPI requests for current exclusive operation
12816 	 * are always completely sent before any DLPI messages for other
12817 	 * operations, require writer access before enqueuing.
12818 	 */
12819 	if (!IAM_WRITER_ILL(ill)) {
12820 		ill_refhold(ill);
12821 		/* qwriter_ip() does the ill_refrele() */
12822 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12823 		    NEW_OP, B_TRUE);
12824 		return;
12825 	}
12826 
12827 	mutex_enter(&ill->ill_lock);
12828 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12829 		/* Must queue message. Tail insertion */
12830 		mpp = &ill->ill_dlpi_deferred;
12831 		while (*mpp != NULL)
12832 			mpp = &((*mpp)->b_next);
12833 
12834 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12835 		    "while %s pending\n", ill->ill_name,
12836 		    dl_primstr(ill->ill_dlpi_pending)));
12837 
12838 		*mpp = mp;
12839 		mutex_exit(&ill->ill_lock);
12840 		return;
12841 	}
12842 	mutex_exit(&ill->ill_lock);
12843 	ill_dlpi_dispatch(ill, mp);
12844 }
12845 
12846 void
12847 ill_capability_send(ill_t *ill, mblk_t *mp)
12848 {
12849 	ill->ill_capab_pending_cnt++;
12850 	ill_dlpi_send(ill, mp);
12851 }
12852 
12853 void
12854 ill_capability_done(ill_t *ill)
12855 {
12856 	ASSERT(ill->ill_capab_pending_cnt != 0);
12857 
12858 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12859 
12860 	ill->ill_capab_pending_cnt--;
12861 	if (ill->ill_capab_pending_cnt == 0 &&
12862 	    ill->ill_dlpi_capab_state == IDCS_OK)
12863 		ill_capability_reset_alloc(ill);
12864 }
12865 
12866 /*
12867  * Send all deferred DLPI messages without waiting for their ACKs.
12868  */
12869 void
12870 ill_dlpi_send_deferred(ill_t *ill)
12871 {
12872 	mblk_t *mp, *nextmp;
12873 
12874 	/*
12875 	 * Clear ill_dlpi_pending so that the message is not queued in
12876 	 * ill_dlpi_send().
12877 	 */
12878 	mutex_enter(&ill->ill_lock);
12879 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12880 	mp = ill->ill_dlpi_deferred;
12881 	ill->ill_dlpi_deferred = NULL;
12882 	mutex_exit(&ill->ill_lock);
12883 
12884 	for (; mp != NULL; mp = nextmp) {
12885 		nextmp = mp->b_next;
12886 		mp->b_next = NULL;
12887 		ill_dlpi_send(ill, mp);
12888 	}
12889 }
12890 
12891 /*
12892  * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12893  * or M_HANGUP
12894  */
12895 static void
12896 ill_dlpi_clear_deferred(ill_t *ill)
12897 {
12898 	mblk_t	*mp, *nextmp;
12899 
12900 	mutex_enter(&ill->ill_lock);
12901 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12902 	mp = ill->ill_dlpi_deferred;
12903 	ill->ill_dlpi_deferred = NULL;
12904 	mutex_exit(&ill->ill_lock);
12905 
12906 	for (; mp != NULL; mp = nextmp) {
12907 		nextmp = mp->b_next;
12908 		inet_freemsg(mp);
12909 	}
12910 }
12911 
12912 /*
12913  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12914  */
12915 boolean_t
12916 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12917 {
12918 	t_uscalar_t pending;
12919 
12920 	mutex_enter(&ill->ill_lock);
12921 	if (ill->ill_dlpi_pending == prim) {
12922 		mutex_exit(&ill->ill_lock);
12923 		return (B_TRUE);
12924 	}
12925 
12926 	/*
12927 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12928 	 * without waiting, so don't print any warnings in that case.
12929 	 */
12930 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12931 		mutex_exit(&ill->ill_lock);
12932 		return (B_FALSE);
12933 	}
12934 	pending = ill->ill_dlpi_pending;
12935 	mutex_exit(&ill->ill_lock);
12936 
12937 	if (pending == DL_PRIM_INVAL) {
12938 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12939 		    "received unsolicited ack for %s on %s\n",
12940 		    dl_primstr(prim), ill->ill_name);
12941 	} else {
12942 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12943 		    "received unexpected ack for %s on %s (expecting %s)\n",
12944 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12945 	}
12946 	return (B_FALSE);
12947 }
12948 
12949 /*
12950  * Complete the current DLPI operation associated with `prim' on `ill' and
12951  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12952  * operations and the ill's current exclusive IPSQ operation has finished
12953  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12954  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12955  * the comments above ipsq_current_finish() for details.
12956  */
12957 void
12958 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12959 {
12960 	mblk_t *mp;
12961 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12962 	ipxop_t *ipx = ipsq->ipsq_xop;
12963 
12964 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12965 	mutex_enter(&ill->ill_lock);
12966 
12967 	ASSERT(prim != DL_PRIM_INVAL);
12968 	ASSERT(ill->ill_dlpi_pending == prim);
12969 
12970 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12971 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12972 
12973 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12974 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12975 		if (ipx->ipx_current_done) {
12976 			mutex_enter(&ipx->ipx_lock);
12977 			ipx->ipx_current_ipif = NULL;
12978 			mutex_exit(&ipx->ipx_lock);
12979 		}
12980 		cv_signal(&ill->ill_cv);
12981 		mutex_exit(&ill->ill_lock);
12982 		return;
12983 	}
12984 
12985 	ill->ill_dlpi_deferred = mp->b_next;
12986 	mp->b_next = NULL;
12987 	mutex_exit(&ill->ill_lock);
12988 
12989 	ill_dlpi_dispatch(ill, mp);
12990 }
12991 
12992 /*
12993  * Queue a (multicast) DLPI control message to be sent to the driver by
12994  * later calling ill_dlpi_send_queued.
12995  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12996  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12997  * for the same group to race.
12998  * We send DLPI control messages in order using ill_lock.
12999  * For IPMP we should be called on the cast_ill.
13000  */
13001 void
13002 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
13003 {
13004 	mblk_t **mpp;
13005 
13006 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
13007 
13008 	mutex_enter(&ill->ill_lock);
13009 	/* Must queue message. Tail insertion */
13010 	mpp = &ill->ill_dlpi_deferred;
13011 	while (*mpp != NULL)
13012 		mpp = &((*mpp)->b_next);
13013 
13014 	*mpp = mp;
13015 	mutex_exit(&ill->ill_lock);
13016 }
13017 
13018 /*
13019  * Send the messages that were queued. Make sure there is only
13020  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
13021  * when an ACK or a NAK is received to process the next queued message.
13022  * For IPMP we are called on the upper ill, but when send what is queued
13023  * on the cast_ill.
13024  */
13025 void
13026 ill_dlpi_send_queued(ill_t *ill)
13027 {
13028 	mblk_t	*mp;
13029 	union DL_primitives *dlp;
13030 	t_uscalar_t prim;
13031 	ill_t *release_ill = NULL;
13032 
13033 	if (IS_IPMP(ill)) {
13034 		/* On the upper IPMP ill. */
13035 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13036 		if (release_ill == NULL) {
13037 			/* Avoid ever sending anything down to the ipmpstub */
13038 			return;
13039 		}
13040 		ill = release_ill;
13041 	}
13042 	mutex_enter(&ill->ill_lock);
13043 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
13044 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
13045 			/* Can't send. Somebody else will send it */
13046 			mutex_exit(&ill->ill_lock);
13047 			goto done;
13048 		}
13049 		ill->ill_dlpi_deferred = mp->b_next;
13050 		mp->b_next = NULL;
13051 		if (!ill->ill_dl_up) {
13052 			/*
13053 			 * Nobody there. All multicast addresses will be
13054 			 * re-joined when we get the DL_BIND_ACK bringing the
13055 			 * interface up.
13056 			 */
13057 			freemsg(mp);
13058 			continue;
13059 		}
13060 		dlp = (union DL_primitives *)mp->b_rptr;
13061 		prim = dlp->dl_primitive;
13062 
13063 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13064 		    (prim == DL_UNBIND_REQ)) {
13065 			ill->ill_dlpi_pending = prim;
13066 		}
13067 		mutex_exit(&ill->ill_lock);
13068 
13069 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13070 		    char *, dl_primstr(prim), ill_t *, ill);
13071 		putnext(ill->ill_wq, mp);
13072 		mutex_enter(&ill->ill_lock);
13073 	}
13074 	mutex_exit(&ill->ill_lock);
13075 done:
13076 	if (release_ill != NULL)
13077 		ill_refrele(release_ill);
13078 }
13079 
13080 /*
13081  * Queue an IP (IGMP/MLD) message to be sent by IP from
13082  * ill_mcast_send_queued
13083  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13084  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13085  * group to race.
13086  * We send them in order using ill_lock.
13087  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13088  */
13089 void
13090 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13091 {
13092 	mblk_t **mpp;
13093 	ill_t *release_ill = NULL;
13094 
13095 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13096 
13097 	if (IS_IPMP(ill)) {
13098 		/* On the upper IPMP ill. */
13099 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13100 		if (release_ill == NULL) {
13101 			/* Discard instead of queuing for the ipmp interface */
13102 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13103 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13104 			    mp, ill);
13105 			freemsg(mp);
13106 			return;
13107 		}
13108 		ill = release_ill;
13109 	}
13110 
13111 	mutex_enter(&ill->ill_lock);
13112 	/* Must queue message. Tail insertion */
13113 	mpp = &ill->ill_mcast_deferred;
13114 	while (*mpp != NULL)
13115 		mpp = &((*mpp)->b_next);
13116 
13117 	*mpp = mp;
13118 	mutex_exit(&ill->ill_lock);
13119 	if (release_ill != NULL)
13120 		ill_refrele(release_ill);
13121 }
13122 
13123 /*
13124  * Send the IP packets that were queued by ill_mcast_queue.
13125  * These are IGMP/MLD packets.
13126  *
13127  * For IPMP we are called on the upper ill, but when send what is queued
13128  * on the cast_ill.
13129  *
13130  * Request loopback of the report if we are acting as a multicast
13131  * router, so that the process-level routing demon can hear it.
13132  * This will run multiple times for the same group if there are members
13133  * on the same group for multiple ipif's on the same ill. The
13134  * igmp_input/mld_input code will suppress this due to the loopback thus we
13135  * always loopback membership report.
13136  *
13137  * We also need to make sure that this does not get load balanced
13138  * by IPMP. We do this by passing an ill to ip_output_simple.
13139  */
13140 void
13141 ill_mcast_send_queued(ill_t *ill)
13142 {
13143 	mblk_t	*mp;
13144 	ip_xmit_attr_t ixas;
13145 	ill_t *release_ill = NULL;
13146 
13147 	if (IS_IPMP(ill)) {
13148 		/* On the upper IPMP ill. */
13149 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13150 		if (release_ill == NULL) {
13151 			/*
13152 			 * We should have no messages on the ipmp interface
13153 			 * but no point in trying to send them.
13154 			 */
13155 			return;
13156 		}
13157 		ill = release_ill;
13158 	}
13159 	bzero(&ixas, sizeof (ixas));
13160 	ixas.ixa_zoneid = ALL_ZONES;
13161 	ixas.ixa_cred = kcred;
13162 	ixas.ixa_cpid = NOPID;
13163 	ixas.ixa_tsl = NULL;
13164 	/*
13165 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13166 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13167 	 * That is necessary to handle IGMP/MLD snooping switches.
13168 	 */
13169 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13170 	ixas.ixa_ipst = ill->ill_ipst;
13171 
13172 	mutex_enter(&ill->ill_lock);
13173 	while ((mp = ill->ill_mcast_deferred) != NULL) {
13174 		ill->ill_mcast_deferred = mp->b_next;
13175 		mp->b_next = NULL;
13176 		if (!ill->ill_dl_up) {
13177 			/*
13178 			 * Nobody there. Just drop the ip packets.
13179 			 * IGMP/MLD will resend later, if this is a replumb.
13180 			 */
13181 			freemsg(mp);
13182 			continue;
13183 		}
13184 		mutex_enter(&ill->ill_phyint->phyint_lock);
13185 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13186 			/*
13187 			 * When the ill is getting deactivated, we only want to
13188 			 * send the DLPI messages, so drop IGMP/MLD packets.
13189 			 * DLPI messages are handled by ill_dlpi_send_queued()
13190 			 */
13191 			mutex_exit(&ill->ill_phyint->phyint_lock);
13192 			freemsg(mp);
13193 			continue;
13194 		}
13195 		mutex_exit(&ill->ill_phyint->phyint_lock);
13196 		mutex_exit(&ill->ill_lock);
13197 
13198 		/* Check whether we are sending IPv4 or IPv6. */
13199 		if (ill->ill_isv6) {
13200 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
13201 
13202 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13203 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13204 		} else {
13205 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
13206 
13207 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13208 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13209 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13210 		}
13211 		ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13212 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13213 		(void) ip_output_simple(mp, &ixas);
13214 		ixa_cleanup(&ixas);
13215 
13216 		mutex_enter(&ill->ill_lock);
13217 	}
13218 	mutex_exit(&ill->ill_lock);
13219 
13220 done:
13221 	if (release_ill != NULL)
13222 		ill_refrele(release_ill);
13223 }
13224 
13225 /*
13226  * Take down a specific interface, but don't lose any information about it.
13227  * (Always called as writer.)
13228  * This function goes through the down sequence even if the interface is
13229  * already down. There are 2 reasons.
13230  * a. Currently we permit interface routes that depend on down interfaces
13231  *    to be added. This behaviour itself is questionable. However it appears
13232  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13233  *    time. We go thru the cleanup in order to remove these routes.
13234  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13235  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
13236  *    down, but we need to cleanup i.e. do ill_dl_down and
13237  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13238  *
13239  * IP-MT notes:
13240  *
13241  * Model of reference to interfaces.
13242  *
13243  * The following members in ipif_t track references to the ipif.
13244  *	int     ipif_refcnt;    Active reference count
13245  *
13246  * The following members in ill_t track references to the ill.
13247  *	int             ill_refcnt;     active refcnt
13248  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
13249  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
13250  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
13251  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
13252  *
13253  * Reference to an ipif or ill can be obtained in any of the following ways.
13254  *
13255  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13256  * Pointers to ipif / ill from other data structures viz ire and conn.
13257  * Implicit reference to the ipif / ill by holding a reference to the ire.
13258  *
13259  * The ipif/ill lookup functions return a reference held ipif / ill.
13260  * ipif_refcnt and ill_refcnt track the reference counts respectively.
13261  * This is a purely dynamic reference count associated with threads holding
13262  * references to the ipif / ill. Pointers from other structures do not
13263  * count towards this reference count.
13264  *
13265  * ill_ire_cnt is the number of ire's associated with the
13266  * ill. This is incremented whenever a new ire is created referencing the
13267  * ill. This is done atomically inside ire_add_v[46] where the ire is
13268  * actually added to the ire hash table. The count is decremented in
13269  * ire_inactive where the ire is destroyed.
13270  *
13271  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13272  * This is incremented atomically in
13273  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13274  * table. Similarly it is decremented in ncec_inactive() where the ncec
13275  * is destroyed.
13276  *
13277  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13278  * incremented atomically in nce_add() where the nce is actually added to the
13279  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13280  * is destroyed.
13281  *
13282  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13283  * ilm_add() and decremented before the ilm is freed in ilm_delete().
13284  *
13285  * Flow of ioctls involving interface down/up
13286  *
13287  * The following is the sequence of an attempt to set some critical flags on an
13288  * up interface.
13289  * ip_sioctl_flags
13290  * ipif_down
13291  * wait for ipif to be quiescent
13292  * ipif_down_tail
13293  * ip_sioctl_flags_tail
13294  *
13295  * All set ioctls that involve down/up sequence would have a skeleton similar
13296  * to the above. All the *tail functions are called after the refcounts have
13297  * dropped to the appropriate values.
13298  *
13299  * SIOC ioctls during the IPIF_CHANGING interval.
13300  *
13301  * Threads handling SIOC set ioctls serialize on the squeue, but this
13302  * is not done for SIOC get ioctls. Since a set ioctl can cause several
13303  * steps of internal changes to the state, some of which are visible in
13304  * ipif_flags (such as IFF_UP being cleared and later set), and we want
13305  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13306  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13307  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13308  * the current exclusive operation completes. The IPIF_CHANGING check
13309  * and enqueue is atomic using the ill_lock and ipsq_lock. The
13310  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13311  * change while the ill_lock is held. Before dropping the ill_lock we acquire
13312  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13313  * until we release the ipsq_lock, even though the ill/ipif state flags
13314  * can change after we drop the ill_lock.
13315  */
13316 int
13317 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13318 {
13319 	ill_t		*ill = ipif->ipif_ill;
13320 	conn_t		*connp;
13321 	boolean_t	success;
13322 	boolean_t	ipif_was_up = B_FALSE;
13323 	ip_stack_t	*ipst = ill->ill_ipst;
13324 
13325 	ASSERT(IAM_WRITER_IPIF(ipif));
13326 
13327 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13328 
13329 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13330 	    ill_t *, ill, ipif_t *, ipif);
13331 
13332 	if (ipif->ipif_flags & IPIF_UP) {
13333 		mutex_enter(&ill->ill_lock);
13334 		ipif->ipif_flags &= ~IPIF_UP;
13335 		ASSERT(ill->ill_ipif_up_count > 0);
13336 		--ill->ill_ipif_up_count;
13337 		mutex_exit(&ill->ill_lock);
13338 		ipif_was_up = B_TRUE;
13339 		/* Update status in SCTP's list */
13340 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13341 		ill_nic_event_dispatch(ipif->ipif_ill,
13342 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13343 	}
13344 
13345 	/*
13346 	 * Removal of the last ipif from an ill may result in a DL_UNBIND
13347 	 * being sent to the driver, and we must not send any data packets to
13348 	 * the driver after the DL_UNBIND_REQ. To ensure this, all the
13349 	 * ire and nce entries used in the data path will be cleaned
13350 	 * up, and we also set  the ILL_DOWN_IN_PROGRESS bit to make
13351 	 * sure on new entries will be added until the ill is bound
13352 	 * again. The ILL_DOWN_IN_PROGRESS bit is turned off upon
13353 	 * receipt of a DL_BIND_ACK.
13354 	 */
13355 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13356 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13357 	    ill->ill_dl_up) {
13358 		ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
13359 	}
13360 
13361 	/*
13362 	 * Blow away memberships we established in ipif_multicast_up().
13363 	 */
13364 	ipif_multicast_down(ipif);
13365 
13366 	/*
13367 	 * Remove from the mapping for __sin6_src_id. We insert only
13368 	 * when the address is not INADDR_ANY. As IPv4 addresses are
13369 	 * stored as mapped addresses, we need to check for mapped
13370 	 * INADDR_ANY also.
13371 	 */
13372 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13373 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13374 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13375 		int err;
13376 
13377 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13378 		    ipif->ipif_zoneid, ipst);
13379 		if (err != 0) {
13380 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13381 		}
13382 	}
13383 
13384 	if (ipif_was_up) {
13385 		/* only delete if we'd added ire's before */
13386 		if (ipif->ipif_isv6)
13387 			ipif_delete_ires_v6(ipif);
13388 		else
13389 			ipif_delete_ires_v4(ipif);
13390 	}
13391 
13392 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13393 		/*
13394 		 * Since the interface is now down, it may have just become
13395 		 * inactive.  Note that this needs to be done even for a
13396 		 * lll_logical_down(), or ARP entries will not get correctly
13397 		 * restored when the interface comes back up.
13398 		 */
13399 		if (IS_UNDER_IPMP(ill))
13400 			ipmp_ill_refresh_active(ill);
13401 	}
13402 
13403 	/*
13404 	 * neighbor-discovery or arp entries for this interface. The ipif
13405 	 * has to be quiesced, so we walk all the nce's and delete those
13406 	 * that point at the ipif->ipif_ill. At the same time, we also
13407 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13408 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13409 	 * that for ipif_down_tail()
13410 	 */
13411 	ipif_nce_down(ipif);
13412 
13413 	/*
13414 	 * If this is the last ipif on the ill, we also need to remove
13415 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13416 	 * never succeed.
13417 	 */
13418 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13419 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13420 
13421 	/*
13422 	 * Walk all CONNs that can have a reference on an ire for this
13423 	 * ipif (we actually walk all that now have stale references).
13424 	 */
13425 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13426 
13427 	/*
13428 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13429 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13430 	 * and ill_delete -> ipif_free -> ipif_down
13431 	 */
13432 	if (mp == NULL) {
13433 		ASSERT(q == NULL);
13434 		return (0);
13435 	}
13436 
13437 	if (CONN_Q(q)) {
13438 		connp = Q_TO_CONN(q);
13439 		mutex_enter(&connp->conn_lock);
13440 	} else {
13441 		connp = NULL;
13442 	}
13443 	mutex_enter(&ill->ill_lock);
13444 	/*
13445 	 * Are there any ire's pointing to this ipif that are still active ?
13446 	 * If this is the last ipif going down, are there any ire's pointing
13447 	 * to this ill that are still active ?
13448 	 */
13449 	if (ipif_is_quiescent(ipif)) {
13450 		mutex_exit(&ill->ill_lock);
13451 		if (connp != NULL)
13452 			mutex_exit(&connp->conn_lock);
13453 		return (0);
13454 	}
13455 
13456 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13457 	    ill->ill_name, (void *)ill));
13458 	/*
13459 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13460 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13461 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13462 	 */
13463 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13464 	if (!success) {
13465 		/* The conn is closing. So just return */
13466 		ASSERT(connp != NULL);
13467 		mutex_exit(&ill->ill_lock);
13468 		mutex_exit(&connp->conn_lock);
13469 		return (EINTR);
13470 	}
13471 
13472 	mutex_exit(&ill->ill_lock);
13473 	if (connp != NULL)
13474 		mutex_exit(&connp->conn_lock);
13475 	return (EINPROGRESS);
13476 }
13477 
13478 int
13479 ipif_down_tail(ipif_t *ipif)
13480 {
13481 	ill_t	*ill = ipif->ipif_ill;
13482 	int	err = 0;
13483 
13484 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13485 	    ill_t *, ill, ipif_t *, ipif);
13486 
13487 	/*
13488 	 * Skip any loopback interface (null wq).
13489 	 * If this is the last logical interface on the ill
13490 	 * have ill_dl_down tell the driver we are gone (unbind)
13491 	 * Note that lun 0 can ipif_down even though
13492 	 * there are other logical units that are up.
13493 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13494 	 */
13495 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13496 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13497 	    ill->ill_dl_up) {
13498 		ill_dl_down(ill);
13499 	}
13500 	if (!ipif->ipif_isv6)
13501 		err = ipif_arp_down(ipif);
13502 
13503 	ill->ill_logical_down = 0;
13504 
13505 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13506 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13507 	return (err);
13508 }
13509 
13510 /*
13511  * Bring interface logically down without bringing the physical interface
13512  * down e.g. when the netmask is changed. This avoids long lasting link
13513  * negotiations between an ethernet interface and a certain switches.
13514  */
13515 static int
13516 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13517 {
13518 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13519 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13520 
13521 	/*
13522 	 * The ill_logical_down flag is a transient flag. It is set here
13523 	 * and is cleared once the down has completed in ipif_down_tail.
13524 	 * This flag does not indicate whether the ill stream is in the
13525 	 * DL_BOUND state with the driver. Instead this flag is used by
13526 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13527 	 * the driver. The state of the ill stream i.e. whether it is
13528 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13529 	 */
13530 	ipif->ipif_ill->ill_logical_down = 1;
13531 	return (ipif_down(ipif, q, mp));
13532 }
13533 
13534 /*
13535  * Initiate deallocate of an IPIF. Always called as writer. Called by
13536  * ill_delete or ip_sioctl_removeif.
13537  */
13538 static void
13539 ipif_free(ipif_t *ipif)
13540 {
13541 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13542 
13543 	ASSERT(IAM_WRITER_IPIF(ipif));
13544 
13545 	if (ipif->ipif_recovery_id != 0)
13546 		(void) untimeout(ipif->ipif_recovery_id);
13547 	ipif->ipif_recovery_id = 0;
13548 
13549 	/*
13550 	 * Take down the interface. We can be called either from ill_delete
13551 	 * or from ip_sioctl_removeif.
13552 	 */
13553 	(void) ipif_down(ipif, NULL, NULL);
13554 
13555 	/*
13556 	 * Now that the interface is down, there's no chance it can still
13557 	 * become a duplicate.  Cancel any timer that may have been set while
13558 	 * tearing down.
13559 	 */
13560 	if (ipif->ipif_recovery_id != 0)
13561 		(void) untimeout(ipif->ipif_recovery_id);
13562 	ipif->ipif_recovery_id = 0;
13563 
13564 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13565 	/* Remove pointers to this ill in the multicast routing tables */
13566 	reset_mrt_vif_ipif(ipif);
13567 	/* If necessary, clear the cached source ipif rotor. */
13568 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13569 		ipif->ipif_ill->ill_src_ipif = NULL;
13570 	rw_exit(&ipst->ips_ill_g_lock);
13571 }
13572 
13573 static void
13574 ipif_free_tail(ipif_t *ipif)
13575 {
13576 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13577 
13578 	/*
13579 	 * Need to hold both ill_g_lock and ill_lock while
13580 	 * inserting or removing an ipif from the linked list
13581 	 * of ipifs hanging off the ill.
13582 	 */
13583 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13584 
13585 #ifdef DEBUG
13586 	ipif_trace_cleanup(ipif);
13587 #endif
13588 
13589 	/* Ask SCTP to take it out of it list */
13590 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13591 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13592 
13593 	/* Get it out of the ILL interface list. */
13594 	ipif_remove(ipif);
13595 	rw_exit(&ipst->ips_ill_g_lock);
13596 
13597 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13598 	ASSERT(ipif->ipif_recovery_id == 0);
13599 	ASSERT(ipif->ipif_ire_local == NULL);
13600 	ASSERT(ipif->ipif_ire_if == NULL);
13601 
13602 	/* Free the memory. */
13603 	mi_free(ipif);
13604 }
13605 
13606 /*
13607  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13608  * is zero.
13609  */
13610 void
13611 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13612 {
13613 	char	lbuf[LIFNAMSIZ];
13614 	char	*name;
13615 	size_t	name_len;
13616 
13617 	buf[0] = '\0';
13618 	name = ipif->ipif_ill->ill_name;
13619 	name_len = ipif->ipif_ill->ill_name_length;
13620 	if (ipif->ipif_id != 0) {
13621 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13622 		    ipif->ipif_id);
13623 		name = lbuf;
13624 		name_len = mi_strlen(name) + 1;
13625 	}
13626 	len -= 1;
13627 	buf[len] = '\0';
13628 	len = MIN(len, name_len);
13629 	bcopy(name, buf, len);
13630 }
13631 
13632 /*
13633  * Sets `buf' to an ill name.
13634  */
13635 void
13636 ill_get_name(const ill_t *ill, char *buf, int len)
13637 {
13638 	char	*name;
13639 	size_t	name_len;
13640 
13641 	name = ill->ill_name;
13642 	name_len = ill->ill_name_length;
13643 	len -= 1;
13644 	buf[len] = '\0';
13645 	len = MIN(len, name_len);
13646 	bcopy(name, buf, len);
13647 }
13648 
13649 /*
13650  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13651  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13652  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13653  * (May be called as writer.)
13654  */
13655 static ipif_t *
13656 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13657     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13658 {
13659 	char	*cp;
13660 	char	*endp;
13661 	long	id;
13662 	ill_t	*ill;
13663 	ipif_t	*ipif;
13664 	uint_t	ire_type;
13665 	boolean_t did_alloc = B_FALSE;
13666 	char	last;
13667 
13668 	/*
13669 	 * If the caller wants to us to create the ipif, make sure we have a
13670 	 * valid zoneid
13671 	 */
13672 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13673 
13674 	if (namelen == 0) {
13675 		return (NULL);
13676 	}
13677 
13678 	*exists = B_FALSE;
13679 	/* Look for a colon in the name. */
13680 	endp = &name[namelen];
13681 	for (cp = endp; --cp > name; ) {
13682 		if (*cp == IPIF_SEPARATOR_CHAR)
13683 			break;
13684 	}
13685 
13686 	if (*cp == IPIF_SEPARATOR_CHAR) {
13687 		/*
13688 		 * Reject any non-decimal aliases for logical
13689 		 * interfaces. Aliases with leading zeroes
13690 		 * are also rejected as they introduce ambiguity
13691 		 * in the naming of the interfaces.
13692 		 * In order to confirm with existing semantics,
13693 		 * and to not break any programs/script relying
13694 		 * on that behaviour, if<0>:0 is considered to be
13695 		 * a valid interface.
13696 		 *
13697 		 * If alias has two or more digits and the first
13698 		 * is zero, fail.
13699 		 */
13700 		if (&cp[2] < endp && cp[1] == '0') {
13701 			return (NULL);
13702 		}
13703 	}
13704 
13705 	if (cp <= name) {
13706 		cp = endp;
13707 	}
13708 	last = *cp;
13709 	*cp = '\0';
13710 
13711 	/*
13712 	 * Look up the ILL, based on the portion of the name
13713 	 * before the slash. ill_lookup_on_name returns a held ill.
13714 	 * Temporary to check whether ill exists already. If so
13715 	 * ill_lookup_on_name will clear it.
13716 	 */
13717 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13718 	    &did_alloc, ipst);
13719 	*cp = last;
13720 	if (ill == NULL)
13721 		return (NULL);
13722 
13723 	/* Establish the unit number in the name. */
13724 	id = 0;
13725 	if (cp < endp && *endp == '\0') {
13726 		/* If there was a colon, the unit number follows. */
13727 		cp++;
13728 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13729 			ill_refrele(ill);
13730 			return (NULL);
13731 		}
13732 	}
13733 
13734 	mutex_enter(&ill->ill_lock);
13735 	/* Now see if there is an IPIF with this unit number. */
13736 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13737 		if (ipif->ipif_id == id) {
13738 			if (zoneid != ALL_ZONES &&
13739 			    zoneid != ipif->ipif_zoneid &&
13740 			    ipif->ipif_zoneid != ALL_ZONES) {
13741 				mutex_exit(&ill->ill_lock);
13742 				ill_refrele(ill);
13743 				return (NULL);
13744 			}
13745 			if (IPIF_CAN_LOOKUP(ipif)) {
13746 				ipif_refhold_locked(ipif);
13747 				mutex_exit(&ill->ill_lock);
13748 				if (!did_alloc)
13749 					*exists = B_TRUE;
13750 				/*
13751 				 * Drop locks before calling ill_refrele
13752 				 * since it can potentially call into
13753 				 * ipif_ill_refrele_tail which can end up
13754 				 * in trying to acquire any lock.
13755 				 */
13756 				ill_refrele(ill);
13757 				return (ipif);
13758 			}
13759 		}
13760 	}
13761 
13762 	if (!do_alloc) {
13763 		mutex_exit(&ill->ill_lock);
13764 		ill_refrele(ill);
13765 		return (NULL);
13766 	}
13767 
13768 	/*
13769 	 * If none found, atomically allocate and return a new one.
13770 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13771 	 * to support "receive only" use of lo0:1 etc. as is still done
13772 	 * below as an initial guess.
13773 	 * However, this is now likely to be overriden later in ipif_up_done()
13774 	 * when we know for sure what address has been configured on the
13775 	 * interface, since we might have more than one loopback interface
13776 	 * with a loopback address, e.g. in the case of zones, and all the
13777 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13778 	 */
13779 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13780 		ire_type = IRE_LOOPBACK;
13781 	else
13782 		ire_type = IRE_LOCAL;
13783 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13784 	if (ipif != NULL)
13785 		ipif_refhold_locked(ipif);
13786 	mutex_exit(&ill->ill_lock);
13787 	ill_refrele(ill);
13788 	return (ipif);
13789 }
13790 
13791 /*
13792  * Variant of the above that queues the request on the ipsq when
13793  * IPIF_CHANGING is set.
13794  */
13795 static ipif_t *
13796 ipif_lookup_on_name_async(char *name, size_t namelen, boolean_t isv6,
13797     zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
13798     ip_stack_t *ipst)
13799 {
13800 	char	*cp;
13801 	char	*endp;
13802 	long	id;
13803 	ill_t	*ill;
13804 	ipif_t	*ipif;
13805 	boolean_t did_alloc = B_FALSE;
13806 	ipsq_t	*ipsq;
13807 
13808 	if (error != NULL)
13809 		*error = 0;
13810 
13811 	if (namelen == 0) {
13812 		if (error != NULL)
13813 			*error = ENXIO;
13814 		return (NULL);
13815 	}
13816 
13817 	/* Look for a colon in the name. */
13818 	endp = &name[namelen];
13819 	for (cp = endp; --cp > name; ) {
13820 		if (*cp == IPIF_SEPARATOR_CHAR)
13821 			break;
13822 	}
13823 
13824 	if (*cp == IPIF_SEPARATOR_CHAR) {
13825 		/*
13826 		 * Reject any non-decimal aliases for logical
13827 		 * interfaces. Aliases with leading zeroes
13828 		 * are also rejected as they introduce ambiguity
13829 		 * in the naming of the interfaces.
13830 		 * In order to confirm with existing semantics,
13831 		 * and to not break any programs/script relying
13832 		 * on that behaviour, if<0>:0 is considered to be
13833 		 * a valid interface.
13834 		 *
13835 		 * If alias has two or more digits and the first
13836 		 * is zero, fail.
13837 		 */
13838 		if (&cp[2] < endp && cp[1] == '0') {
13839 			if (error != NULL)
13840 				*error = EINVAL;
13841 			return (NULL);
13842 		}
13843 	}
13844 
13845 	if (cp <= name) {
13846 		cp = endp;
13847 	} else {
13848 		*cp = '\0';
13849 	}
13850 
13851 	/*
13852 	 * Look up the ILL, based on the portion of the name
13853 	 * before the slash. ill_lookup_on_name returns a held ill.
13854 	 * Temporary to check whether ill exists already. If so
13855 	 * ill_lookup_on_name will clear it.
13856 	 */
13857 	ill = ill_lookup_on_name(name, B_FALSE, isv6, &did_alloc, ipst);
13858 	if (cp != endp)
13859 		*cp = IPIF_SEPARATOR_CHAR;
13860 	if (ill == NULL)
13861 		return (NULL);
13862 
13863 	/* Establish the unit number in the name. */
13864 	id = 0;
13865 	if (cp < endp && *endp == '\0') {
13866 		/* If there was a colon, the unit number follows. */
13867 		cp++;
13868 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13869 			ill_refrele(ill);
13870 			if (error != NULL)
13871 				*error = ENXIO;
13872 			return (NULL);
13873 		}
13874 	}
13875 
13876 	GRAB_CONN_LOCK(q);
13877 	mutex_enter(&ill->ill_lock);
13878 	/* Now see if there is an IPIF with this unit number. */
13879 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13880 		if (ipif->ipif_id == id) {
13881 			if (zoneid != ALL_ZONES &&
13882 			    zoneid != ipif->ipif_zoneid &&
13883 			    ipif->ipif_zoneid != ALL_ZONES) {
13884 				mutex_exit(&ill->ill_lock);
13885 				RELEASE_CONN_LOCK(q);
13886 				ill_refrele(ill);
13887 				if (error != NULL)
13888 					*error = ENXIO;
13889 				return (NULL);
13890 			}
13891 
13892 			if (!(IPIF_IS_CHANGING(ipif) ||
13893 			    IPIF_IS_CONDEMNED(ipif)) ||
13894 			    IAM_WRITER_IPIF(ipif)) {
13895 				ipif_refhold_locked(ipif);
13896 				mutex_exit(&ill->ill_lock);
13897 				/*
13898 				 * Drop locks before calling ill_refrele
13899 				 * since it can potentially call into
13900 				 * ipif_ill_refrele_tail which can end up
13901 				 * in trying to acquire any lock.
13902 				 */
13903 				RELEASE_CONN_LOCK(q);
13904 				ill_refrele(ill);
13905 				return (ipif);
13906 			} else if (q != NULL && !IPIF_IS_CONDEMNED(ipif)) {
13907 				ipsq = ill->ill_phyint->phyint_ipsq;
13908 				mutex_enter(&ipsq->ipsq_lock);
13909 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
13910 				mutex_exit(&ill->ill_lock);
13911 				ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
13912 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
13913 				mutex_exit(&ipsq->ipsq_lock);
13914 				RELEASE_CONN_LOCK(q);
13915 				ill_refrele(ill);
13916 				if (error != NULL)
13917 					*error = EINPROGRESS;
13918 				return (NULL);
13919 			}
13920 		}
13921 	}
13922 	RELEASE_CONN_LOCK(q);
13923 	mutex_exit(&ill->ill_lock);
13924 	ill_refrele(ill);
13925 	if (error != NULL)
13926 		*error = ENXIO;
13927 	return (NULL);
13928 }
13929 
13930 /*
13931  * This routine is called whenever a new address comes up on an ipif.  If
13932  * we are configured to respond to address mask requests, then we are supposed
13933  * to broadcast an address mask reply at this time.  This routine is also
13934  * called if we are already up, but a netmask change is made.  This is legal
13935  * but might not make the system manager very popular.	(May be called
13936  * as writer.)
13937  */
13938 void
13939 ipif_mask_reply(ipif_t *ipif)
13940 {
13941 	icmph_t	*icmph;
13942 	ipha_t	*ipha;
13943 	mblk_t	*mp;
13944 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13945 	ip_xmit_attr_t ixas;
13946 
13947 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13948 
13949 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13950 		return;
13951 
13952 	/* ICMP mask reply is IPv4 only */
13953 	ASSERT(!ipif->ipif_isv6);
13954 	/* ICMP mask reply is not for a loopback interface */
13955 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13956 
13957 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13958 		return;
13959 
13960 	mp = allocb(REPLY_LEN, BPRI_HI);
13961 	if (mp == NULL)
13962 		return;
13963 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13964 
13965 	ipha = (ipha_t *)mp->b_rptr;
13966 	bzero(ipha, REPLY_LEN);
13967 	*ipha = icmp_ipha;
13968 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13969 	ipha->ipha_src = ipif->ipif_lcl_addr;
13970 	ipha->ipha_dst = ipif->ipif_brd_addr;
13971 	ipha->ipha_length = htons(REPLY_LEN);
13972 	ipha->ipha_ident = 0;
13973 
13974 	icmph = (icmph_t *)&ipha[1];
13975 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13976 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13977 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13978 
13979 	bzero(&ixas, sizeof (ixas));
13980 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13981 	ixas.ixa_zoneid = ALL_ZONES;
13982 	ixas.ixa_ifindex = 0;
13983 	ixas.ixa_ipst = ipst;
13984 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13985 	(void) ip_output_simple(mp, &ixas);
13986 	ixa_cleanup(&ixas);
13987 #undef	REPLY_LEN
13988 }
13989 
13990 /*
13991  * Join the ipif specific multicast groups.
13992  * Must be called after a mapping has been set up in the resolver.  (Always
13993  * called as writer.)
13994  */
13995 void
13996 ipif_multicast_up(ipif_t *ipif)
13997 {
13998 	int err;
13999 	ill_t *ill;
14000 	ilm_t *ilm;
14001 
14002 	ASSERT(IAM_WRITER_IPIF(ipif));
14003 
14004 	ill = ipif->ipif_ill;
14005 
14006 	ip1dbg(("ipif_multicast_up\n"));
14007 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
14008 	    ipif->ipif_allhosts_ilm != NULL)
14009 		return;
14010 
14011 	if (ipif->ipif_isv6) {
14012 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
14013 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
14014 
14015 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
14016 
14017 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
14018 			return;
14019 
14020 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14021 
14022 		/*
14023 		 * Join the all hosts multicast address.  We skip this for
14024 		 * underlying IPMP interfaces since they should be invisible.
14025 		 */
14026 		if (!IS_UNDER_IPMP(ill)) {
14027 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
14028 			    &err);
14029 			if (ilm == NULL) {
14030 				ASSERT(err != 0);
14031 				ip0dbg(("ipif_multicast_up: "
14032 				    "all_hosts_mcast failed %d\n", err));
14033 				return;
14034 			}
14035 			ipif->ipif_allhosts_ilm = ilm;
14036 		}
14037 
14038 		/*
14039 		 * Enable multicast for the solicited node multicast address.
14040 		 * If IPMP we need to put the membership on the upper ill.
14041 		 */
14042 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
14043 			ill_t *mcast_ill = NULL;
14044 			boolean_t need_refrele;
14045 
14046 			if (IS_UNDER_IPMP(ill) &&
14047 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
14048 				need_refrele = B_TRUE;
14049 			} else {
14050 				mcast_ill = ill;
14051 				need_refrele = B_FALSE;
14052 			}
14053 
14054 			ilm = ip_addmulti(&v6solmc, mcast_ill,
14055 			    ipif->ipif_zoneid, &err);
14056 			if (need_refrele)
14057 				ill_refrele(mcast_ill);
14058 
14059 			if (ilm == NULL) {
14060 				ASSERT(err != 0);
14061 				ip0dbg(("ipif_multicast_up: solicited MC"
14062 				    " failed %d\n", err));
14063 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
14064 					ipif->ipif_allhosts_ilm = NULL;
14065 					(void) ip_delmulti(ilm);
14066 				}
14067 				return;
14068 			}
14069 			ipif->ipif_solmulti_ilm = ilm;
14070 		}
14071 	} else {
14072 		in6_addr_t v6group;
14073 
14074 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
14075 			return;
14076 
14077 		/* Join the all hosts multicast address */
14078 		ip1dbg(("ipif_multicast_up - addmulti\n"));
14079 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
14080 
14081 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
14082 		if (ilm == NULL) {
14083 			ASSERT(err != 0);
14084 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
14085 			return;
14086 		}
14087 		ipif->ipif_allhosts_ilm = ilm;
14088 	}
14089 }
14090 
14091 /*
14092  * Blow away any multicast groups that we joined in ipif_multicast_up().
14093  * (ilms from explicit memberships are handled in conn_update_ill.)
14094  */
14095 void
14096 ipif_multicast_down(ipif_t *ipif)
14097 {
14098 	ASSERT(IAM_WRITER_IPIF(ipif));
14099 
14100 	ip1dbg(("ipif_multicast_down\n"));
14101 
14102 	if (ipif->ipif_allhosts_ilm != NULL) {
14103 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
14104 		ipif->ipif_allhosts_ilm = NULL;
14105 	}
14106 	if (ipif->ipif_solmulti_ilm != NULL) {
14107 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
14108 		ipif->ipif_solmulti_ilm = NULL;
14109 	}
14110 }
14111 
14112 /*
14113  * Used when an interface comes up to recreate any extra routes on this
14114  * interface.
14115  */
14116 int
14117 ill_recover_saved_ire(ill_t *ill)
14118 {
14119 	mblk_t		*mp;
14120 	ip_stack_t	*ipst = ill->ill_ipst;
14121 
14122 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
14123 
14124 	mutex_enter(&ill->ill_saved_ire_lock);
14125 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14126 		ire_t		*ire, *nire;
14127 		ifrt_t		*ifrt;
14128 
14129 		ifrt = (ifrt_t *)mp->b_rptr;
14130 		/*
14131 		 * Create a copy of the IRE with the saved address and netmask.
14132 		 */
14133 		if (ill->ill_isv6) {
14134 			ire = ire_create_v6(
14135 			    &ifrt->ifrt_v6addr,
14136 			    &ifrt->ifrt_v6mask,
14137 			    &ifrt->ifrt_v6gateway_addr,
14138 			    ifrt->ifrt_type,
14139 			    ill,
14140 			    ifrt->ifrt_zoneid,
14141 			    ifrt->ifrt_flags,
14142 			    NULL,
14143 			    ipst);
14144 		} else {
14145 			ire = ire_create(
14146 			    (uint8_t *)&ifrt->ifrt_addr,
14147 			    (uint8_t *)&ifrt->ifrt_mask,
14148 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
14149 			    ifrt->ifrt_type,
14150 			    ill,
14151 			    ifrt->ifrt_zoneid,
14152 			    ifrt->ifrt_flags,
14153 			    NULL,
14154 			    ipst);
14155 		}
14156 		if (ire == NULL) {
14157 			mutex_exit(&ill->ill_saved_ire_lock);
14158 			return (ENOMEM);
14159 		}
14160 
14161 		if (ifrt->ifrt_flags & RTF_SETSRC) {
14162 			if (ill->ill_isv6) {
14163 				ire->ire_setsrc_addr_v6 =
14164 				    ifrt->ifrt_v6setsrc_addr;
14165 			} else {
14166 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
14167 			}
14168 		}
14169 
14170 		/*
14171 		 * Some software (for example, GateD and Sun Cluster) attempts
14172 		 * to create (what amount to) IRE_PREFIX routes with the
14173 		 * loopback address as the gateway.  This is primarily done to
14174 		 * set up prefixes with the RTF_REJECT flag set (for example,
14175 		 * when generating aggregate routes.)
14176 		 *
14177 		 * If the IRE type (as defined by ill->ill_net_type) is
14178 		 * IRE_LOOPBACK, then we map the request into a
14179 		 * IRE_IF_NORESOLVER.
14180 		 */
14181 		if (ill->ill_net_type == IRE_LOOPBACK)
14182 			ire->ire_type = IRE_IF_NORESOLVER;
14183 
14184 		/*
14185 		 * ire held by ire_add, will be refreled' towards the
14186 		 * the end of ipif_up_done
14187 		 */
14188 		nire = ire_add(ire);
14189 		/*
14190 		 * Check if it was a duplicate entry. This handles
14191 		 * the case of two racing route adds for the same route
14192 		 */
14193 		if (nire == NULL) {
14194 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14195 		} else if (nire != ire) {
14196 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14197 			    (void *)nire));
14198 			ire_delete(nire);
14199 		} else {
14200 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14201 			    (void *)nire));
14202 		}
14203 		if (nire != NULL)
14204 			ire_refrele(nire);
14205 	}
14206 	mutex_exit(&ill->ill_saved_ire_lock);
14207 	return (0);
14208 }
14209 
14210 /*
14211  * Used to set the netmask and broadcast address to default values when the
14212  * interface is brought up.  (Always called as writer.)
14213  */
14214 static void
14215 ipif_set_default(ipif_t *ipif)
14216 {
14217 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14218 
14219 	if (!ipif->ipif_isv6) {
14220 		/*
14221 		 * Interface holds an IPv4 address. Default
14222 		 * mask is the natural netmask.
14223 		 */
14224 		if (!ipif->ipif_net_mask) {
14225 			ipaddr_t	v4mask;
14226 
14227 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14228 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14229 		}
14230 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14231 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14232 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14233 		} else {
14234 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14235 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14236 		}
14237 		/*
14238 		 * NOTE: SunOS 4.X does this even if the broadcast address
14239 		 * has been already set thus we do the same here.
14240 		 */
14241 		if (ipif->ipif_flags & IPIF_BROADCAST) {
14242 			ipaddr_t	v4addr;
14243 
14244 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14245 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14246 		}
14247 	} else {
14248 		/*
14249 		 * Interface holds an IPv6-only address.  Default
14250 		 * mask is all-ones.
14251 		 */
14252 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14253 			ipif->ipif_v6net_mask = ipv6_all_ones;
14254 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14255 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14256 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14257 		} else {
14258 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14259 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14260 		}
14261 	}
14262 }
14263 
14264 /*
14265  * Return 0 if this address can be used as local address without causing
14266  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14267  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14268  * Note that the same IPv6 link-local address is allowed as long as the ills
14269  * are not on the same link.
14270  */
14271 int
14272 ip_addr_availability_check(ipif_t *new_ipif)
14273 {
14274 	in6_addr_t our_v6addr;
14275 	ill_t *ill;
14276 	ipif_t *ipif;
14277 	ill_walk_context_t ctx;
14278 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
14279 
14280 	ASSERT(IAM_WRITER_IPIF(new_ipif));
14281 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14282 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14283 
14284 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14285 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14286 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14287 		return (0);
14288 
14289 	our_v6addr = new_ipif->ipif_v6lcl_addr;
14290 
14291 	if (new_ipif->ipif_isv6)
14292 		ill = ILL_START_WALK_V6(&ctx, ipst);
14293 	else
14294 		ill = ILL_START_WALK_V4(&ctx, ipst);
14295 
14296 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14297 		for (ipif = ill->ill_ipif; ipif != NULL;
14298 		    ipif = ipif->ipif_next) {
14299 			if ((ipif == new_ipif) ||
14300 			    !(ipif->ipif_flags & IPIF_UP) ||
14301 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14302 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14303 			    &our_v6addr))
14304 				continue;
14305 
14306 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14307 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14308 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14309 				ipif->ipif_flags |= IPIF_UNNUMBERED;
14310 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14311 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14312 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14313 				continue;
14314 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14315 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14316 				continue;
14317 			else if (new_ipif->ipif_ill == ill)
14318 				return (EADDRINUSE);
14319 			else
14320 				return (EADDRNOTAVAIL);
14321 		}
14322 	}
14323 
14324 	return (0);
14325 }
14326 
14327 /*
14328  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14329  * IREs for the ipif.
14330  * When the routine returns EINPROGRESS then mp has been consumed and
14331  * the ioctl will be acked from ip_rput_dlpi.
14332  */
14333 int
14334 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14335 {
14336 	ill_t		*ill = ipif->ipif_ill;
14337 	boolean_t	isv6 = ipif->ipif_isv6;
14338 	int		err = 0;
14339 	boolean_t	success;
14340 	uint_t		ipif_orig_id;
14341 	ip_stack_t	*ipst = ill->ill_ipst;
14342 
14343 	ASSERT(IAM_WRITER_IPIF(ipif));
14344 
14345 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14346 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14347 	    ill_t *, ill, ipif_t *, ipif);
14348 
14349 	/* Shouldn't get here if it is already up. */
14350 	if (ipif->ipif_flags & IPIF_UP)
14351 		return (EALREADY);
14352 
14353 	/*
14354 	 * If this is a request to bring up a data address on an interface
14355 	 * under IPMP, then move the address to its IPMP meta-interface and
14356 	 * try to bring it up.  One complication is that the zeroth ipif for
14357 	 * an ill is special, in that every ill always has one, and that code
14358 	 * throughout IP deferences ill->ill_ipif without holding any locks.
14359 	 */
14360 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14361 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14362 		ipif_t	*stubipif = NULL, *moveipif = NULL;
14363 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14364 
14365 		/*
14366 		 * The ipif being brought up should be quiesced.  If it's not,
14367 		 * something has gone amiss and we need to bail out.  (If it's
14368 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14369 		 */
14370 		mutex_enter(&ill->ill_lock);
14371 		if (!ipif_is_quiescent(ipif)) {
14372 			mutex_exit(&ill->ill_lock);
14373 			return (EINVAL);
14374 		}
14375 		mutex_exit(&ill->ill_lock);
14376 
14377 		/*
14378 		 * If we're going to need to allocate ipifs, do it prior
14379 		 * to starting the move (and grabbing locks).
14380 		 */
14381 		if (ipif->ipif_id == 0) {
14382 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14383 			    B_FALSE, &err)) == NULL) {
14384 				return (err);
14385 			}
14386 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14387 			    B_FALSE, &err)) == NULL) {
14388 				mi_free(moveipif);
14389 				return (err);
14390 			}
14391 		}
14392 
14393 		/*
14394 		 * Grab or transfer the ipif to move.  During the move, keep
14395 		 * ill_g_lock held to prevent any ill walker threads from
14396 		 * seeing things in an inconsistent state.
14397 		 */
14398 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14399 		if (ipif->ipif_id != 0) {
14400 			ipif_remove(ipif);
14401 		} else {
14402 			ipif_transfer(ipif, moveipif, stubipif);
14403 			ipif = moveipif;
14404 		}
14405 
14406 		/*
14407 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
14408 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
14409 		 * replace that one.  Otherwise, pick the next available slot.
14410 		 */
14411 		ipif->ipif_ill = ipmp_ill;
14412 		ipif_orig_id = ipif->ipif_id;
14413 
14414 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14415 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14416 			ipif = ipmp_ill->ill_ipif;
14417 		} else {
14418 			ipif->ipif_id = -1;
14419 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14420 				/*
14421 				 * No more available ipif_id's -- put it back
14422 				 * on the original ill and fail the operation.
14423 				 * Since we're writer on the ill, we can be
14424 				 * sure our old slot is still available.
14425 				 */
14426 				ipif->ipif_id = ipif_orig_id;
14427 				ipif->ipif_ill = ill;
14428 				if (ipif_orig_id == 0) {
14429 					ipif_transfer(ipif, ill->ill_ipif,
14430 					    NULL);
14431 				} else {
14432 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14433 				}
14434 				rw_exit(&ipst->ips_ill_g_lock);
14435 				return (err);
14436 			}
14437 		}
14438 		rw_exit(&ipst->ips_ill_g_lock);
14439 
14440 		/*
14441 		 * Tell SCTP that the ipif has moved.  Note that even if we
14442 		 * had to allocate a new ipif, the original sequence id was
14443 		 * preserved and therefore SCTP won't know.
14444 		 */
14445 		sctp_move_ipif(ipif, ill, ipmp_ill);
14446 
14447 		/*
14448 		 * If the ipif being brought up was on slot zero, then we
14449 		 * first need to bring up the placeholder we stuck there.  In
14450 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14451 		 * call to ipif_up() itself, if we successfully bring up the
14452 		 * placeholder, we'll check ill_move_ipif and bring it up too.
14453 		 */
14454 		if (ipif_orig_id == 0) {
14455 			ASSERT(ill->ill_move_ipif == NULL);
14456 			ill->ill_move_ipif = ipif;
14457 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14458 				ASSERT(ill->ill_move_ipif == NULL);
14459 			if (err != EINPROGRESS)
14460 				ill->ill_move_ipif = NULL;
14461 			return (err);
14462 		}
14463 
14464 		/*
14465 		 * Bring it up on the IPMP ill.
14466 		 */
14467 		return (ipif_up(ipif, q, mp));
14468 	}
14469 
14470 	/* Skip arp/ndp for any loopback interface. */
14471 	if (ill->ill_wq != NULL) {
14472 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14473 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
14474 
14475 		if (!ill->ill_dl_up) {
14476 			/*
14477 			 * ill_dl_up is not yet set. i.e. we are yet to
14478 			 * DL_BIND with the driver and this is the first
14479 			 * logical interface on the ill to become "up".
14480 			 * Tell the driver to get going (via DL_BIND_REQ).
14481 			 * Note that changing "significant" IFF_ flags
14482 			 * address/netmask etc cause a down/up dance, but
14483 			 * does not cause an unbind (DL_UNBIND) with the driver
14484 			 */
14485 			return (ill_dl_up(ill, ipif, mp, q));
14486 		}
14487 
14488 		/*
14489 		 * ipif_resolver_up may end up needeing to bind/attach
14490 		 * the ARP stream, which in turn necessitates a
14491 		 * DLPI message exchange with the driver. ioctls are
14492 		 * serialized and so we cannot send more than one
14493 		 * interface up message at a time. If ipif_resolver_up
14494 		 * does need to wait for the DLPI handshake for the ARP stream,
14495 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
14496 		 */
14497 
14498 		ASSERT(connp != NULL || !CONN_Q(q));
14499 		if (connp != NULL)
14500 			mutex_enter(&connp->conn_lock);
14501 		mutex_enter(&ill->ill_lock);
14502 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14503 		mutex_exit(&ill->ill_lock);
14504 		if (connp != NULL)
14505 			mutex_exit(&connp->conn_lock);
14506 		if (!success)
14507 			return (EINTR);
14508 
14509 		/*
14510 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14511 		 * complete when ipif_ndp_up returns.
14512 		 */
14513 		err = ipif_resolver_up(ipif, Res_act_initial);
14514 		if (err == EINPROGRESS) {
14515 			/* We will complete it in arp_bringup_done() */
14516 			return (err);
14517 		}
14518 
14519 		if (isv6 && err == 0)
14520 			err = ipif_ndp_up(ipif, B_TRUE);
14521 
14522 		ASSERT(err != EINPROGRESS);
14523 		mp = ipsq_pending_mp_get(ipsq, &connp);
14524 		ASSERT(mp != NULL);
14525 		if (err != 0)
14526 			return (err);
14527 	} else {
14528 		/*
14529 		 * Interfaces without underlying hardware don't do duplicate
14530 		 * address detection.
14531 		 */
14532 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14533 		ipif->ipif_addr_ready = 1;
14534 		err = ill_add_ires(ill);
14535 		/* allocation failure? */
14536 		if (err != 0)
14537 			return (err);
14538 	}
14539 
14540 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14541 	if (err == 0 && ill->ill_move_ipif != NULL) {
14542 		ipif = ill->ill_move_ipif;
14543 		ill->ill_move_ipif = NULL;
14544 		return (ipif_up(ipif, q, mp));
14545 	}
14546 	return (err);
14547 }
14548 
14549 /*
14550  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14551  * The identical set of IREs need to be removed in ill_delete_ires().
14552  */
14553 int
14554 ill_add_ires(ill_t *ill)
14555 {
14556 	ire_t	*ire;
14557 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14558 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14559 
14560 	if (ill->ill_ire_multicast != NULL)
14561 		return (0);
14562 
14563 	/*
14564 	 * provide some dummy ire_addr for creating the ire.
14565 	 */
14566 	if (ill->ill_isv6) {
14567 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14568 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14569 	} else {
14570 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14571 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14572 	}
14573 	if (ire == NULL)
14574 		return (ENOMEM);
14575 
14576 	ill->ill_ire_multicast = ire;
14577 	return (0);
14578 }
14579 
14580 void
14581 ill_delete_ires(ill_t *ill)
14582 {
14583 	if (ill->ill_ire_multicast != NULL) {
14584 		/*
14585 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14586 		 * which was taken without any th_tracing enabled.
14587 		 * We also mark it as condemned (note that it was never added)
14588 		 * so that caching conn's can move off of it.
14589 		 */
14590 		ire_make_condemned(ill->ill_ire_multicast);
14591 		ire_refrele_notr(ill->ill_ire_multicast);
14592 		ill->ill_ire_multicast = NULL;
14593 	}
14594 }
14595 
14596 /*
14597  * Perform a bind for the physical device.
14598  * When the routine returns EINPROGRESS then mp has been consumed and
14599  * the ioctl will be acked from ip_rput_dlpi.
14600  * Allocate an unbind message and save it until ipif_down.
14601  */
14602 static int
14603 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14604 {
14605 	mblk_t	*bind_mp = NULL;
14606 	mblk_t	*unbind_mp = NULL;
14607 	conn_t	*connp;
14608 	boolean_t success;
14609 	int	err;
14610 
14611 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14612 
14613 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14614 	ASSERT(IAM_WRITER_ILL(ill));
14615 	ASSERT(mp != NULL);
14616 
14617 	/*
14618 	 * Make sure we have an IRE_MULTICAST in case we immediately
14619 	 * start receiving packets.
14620 	 */
14621 	err = ill_add_ires(ill);
14622 	if (err != 0)
14623 		goto bad;
14624 
14625 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14626 	    DL_BIND_REQ);
14627 	if (bind_mp == NULL)
14628 		goto bad;
14629 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14630 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14631 
14632 	/*
14633 	 * ill_unbind_mp would be non-null if the following sequence had
14634 	 * happened:
14635 	 * - send DL_BIND_REQ to driver, wait for response
14636 	 * - multiple ioctls that need to bring the ipif up are encountered,
14637 	 *   but they cannot enter the ipsq due to the outstanding DL_BIND_REQ.
14638 	 *   These ioctls will then be enqueued on the ipsq
14639 	 * - a DL_ERROR_ACK is returned for the DL_BIND_REQ
14640 	 * At this point, the pending ioctls in the ipsq will be drained, and
14641 	 * since ill->ill_dl_up was not set, ill_dl_up would be invoked with
14642 	 * a non-null ill->ill_unbind_mp
14643 	 */
14644 	if (ill->ill_unbind_mp == NULL) {
14645 		unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t),
14646 		    DL_UNBIND_REQ);
14647 		if (unbind_mp == NULL)
14648 			goto bad;
14649 	}
14650 	/*
14651 	 * Record state needed to complete this operation when the
14652 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14653 	 */
14654 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14655 	ASSERT(connp != NULL || !CONN_Q(q));
14656 	GRAB_CONN_LOCK(q);
14657 	mutex_enter(&ipif->ipif_ill->ill_lock);
14658 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14659 	mutex_exit(&ipif->ipif_ill->ill_lock);
14660 	RELEASE_CONN_LOCK(q);
14661 	if (!success)
14662 		goto bad;
14663 
14664 	/*
14665 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14666 	 * the interface goes down.
14667 	 */
14668 	if (ill->ill_unbind_mp == NULL)
14669 		ill->ill_unbind_mp = unbind_mp;
14670 
14671 	ill_dlpi_send(ill, bind_mp);
14672 	/* Send down link-layer capabilities probe if not already done. */
14673 	ill_capability_probe(ill);
14674 
14675 	/*
14676 	 * Sysid used to rely on the fact that netboots set domainname
14677 	 * and the like. Now that miniroot boots aren't strictly netboots
14678 	 * and miniroot network configuration is driven from userland
14679 	 * these things still need to be set. This situation can be detected
14680 	 * by comparing the interface being configured here to the one
14681 	 * dhcifname was set to reference by the boot loader. Once sysid is
14682 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14683 	 */
14684 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14685 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14686 	    (strlen(srpc_domain) == 0)) {
14687 		if (dhcpinit() != 0)
14688 			cmn_err(CE_WARN, "no cached dhcp response");
14689 	}
14690 
14691 	/*
14692 	 * This operation will complete in ip_rput_dlpi with either
14693 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14694 	 */
14695 	return (EINPROGRESS);
14696 bad:
14697 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14698 
14699 	freemsg(bind_mp);
14700 	freemsg(unbind_mp);
14701 	return (ENOMEM);
14702 }
14703 
14704 /* Add room for tcp+ip headers */
14705 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14706 
14707 /*
14708  * DLPI and ARP is up.
14709  * Create all the IREs associated with an interface. Bring up multicast.
14710  * Set the interface flag and finish other initialization
14711  * that potentially had to be deferred to after DL_BIND_ACK.
14712  */
14713 int
14714 ipif_up_done(ipif_t *ipif)
14715 {
14716 	ill_t		*ill = ipif->ipif_ill;
14717 	int		err = 0;
14718 	boolean_t	loopback = B_FALSE;
14719 	boolean_t	update_src_selection = B_TRUE;
14720 	ipif_t		*tmp_ipif;
14721 
14722 	ip1dbg(("ipif_up_done(%s:%u)\n",
14723 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14724 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14725 	    ill_t *, ill, ipif_t *, ipif);
14726 
14727 	/* Check if this is a loopback interface */
14728 	if (ipif->ipif_ill->ill_wq == NULL)
14729 		loopback = B_TRUE;
14730 
14731 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14732 
14733 	/*
14734 	 * If all other interfaces for this ill are down or DEPRECATED,
14735 	 * or otherwise unsuitable for source address selection,
14736 	 * reset the src generation numbers to make sure source
14737 	 * address selection gets to take this new ipif into account.
14738 	 * No need to hold ill_lock while traversing the ipif list since
14739 	 * we are writer
14740 	 */
14741 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14742 	    tmp_ipif = tmp_ipif->ipif_next) {
14743 		if (((tmp_ipif->ipif_flags &
14744 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14745 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14746 		    (tmp_ipif == ipif))
14747 			continue;
14748 		/* first useable pre-existing interface */
14749 		update_src_selection = B_FALSE;
14750 		break;
14751 	}
14752 	if (update_src_selection)
14753 		ip_update_source_selection(ill->ill_ipst);
14754 
14755 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14756 		nce_t *loop_nce = NULL;
14757 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14758 
14759 		/*
14760 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14761 		 * ipif_lookup_on_name(), but in the case of zones we can have
14762 		 * several loopback addresses on lo0. So all the interfaces with
14763 		 * loopback addresses need to be marked IRE_LOOPBACK.
14764 		 */
14765 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14766 		    htonl(INADDR_LOOPBACK))
14767 			ipif->ipif_ire_type = IRE_LOOPBACK;
14768 		else
14769 			ipif->ipif_ire_type = IRE_LOCAL;
14770 		if (ill->ill_net_type != IRE_LOOPBACK)
14771 			flags |= NCE_F_PUBLISH;
14772 
14773 		/* add unicast nce for the local addr */
14774 		err = nce_lookup_then_add_v4(ill, NULL,
14775 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14776 		    ND_REACHABLE, &loop_nce);
14777 		/* A shared-IP zone sees EEXIST for lo0:N */
14778 		if (err == 0 || err == EEXIST) {
14779 			ipif->ipif_added_nce = 1;
14780 			loop_nce->nce_ipif_cnt++;
14781 			nce_refrele(loop_nce);
14782 			err = 0;
14783 		} else {
14784 			ASSERT(loop_nce == NULL);
14785 			return (err);
14786 		}
14787 	}
14788 
14789 	/* Create all the IREs associated with this interface */
14790 	err = ipif_add_ires_v4(ipif, loopback);
14791 	if (err != 0) {
14792 		/*
14793 		 * see comments about return value from
14794 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14795 		 */
14796 		if (err != EADDRINUSE) {
14797 			(void) ipif_arp_down(ipif);
14798 		} else {
14799 			/*
14800 			 * Make IPMP aware of the deleted ipif so that
14801 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14802 			 * can be completed. Note that we do not want to
14803 			 * destroy the nce that was created on the ipmp_ill
14804 			 * for the active copy of the duplicate address in
14805 			 * use.
14806 			 */
14807 			if (IS_IPMP(ill))
14808 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14809 			err = EADDRNOTAVAIL;
14810 		}
14811 		return (err);
14812 	}
14813 
14814 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14815 		/* Recover any additional IREs entries for this ill */
14816 		(void) ill_recover_saved_ire(ill);
14817 	}
14818 
14819 	if (ill->ill_need_recover_multicast) {
14820 		/*
14821 		 * Need to recover all multicast memberships in the driver.
14822 		 * This had to be deferred until we had attached.  The same
14823 		 * code exists in ipif_up_done_v6() to recover IPv6
14824 		 * memberships.
14825 		 *
14826 		 * Note that it would be preferable to unconditionally do the
14827 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14828 		 * that since ill_join_allmulti() depends on ill_dl_up being
14829 		 * set, and it is not set until we receive a DL_BIND_ACK after
14830 		 * having called ill_dl_up().
14831 		 */
14832 		ill_recover_multicast(ill);
14833 	}
14834 
14835 	if (ill->ill_ipif_up_count == 1) {
14836 		/*
14837 		 * Since the interface is now up, it may now be active.
14838 		 */
14839 		if (IS_UNDER_IPMP(ill))
14840 			ipmp_ill_refresh_active(ill);
14841 
14842 		/*
14843 		 * If this is an IPMP interface, we may now be able to
14844 		 * establish ARP entries.
14845 		 */
14846 		if (IS_IPMP(ill))
14847 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14848 	}
14849 
14850 	/* Join the allhosts multicast address */
14851 	ipif_multicast_up(ipif);
14852 
14853 	if (!loopback && !update_src_selection &&
14854 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14855 		ip_update_source_selection(ill->ill_ipst);
14856 
14857 	if (!loopback && ipif->ipif_addr_ready) {
14858 		/* Broadcast an address mask reply. */
14859 		ipif_mask_reply(ipif);
14860 	}
14861 	/* Perhaps ilgs should use this ill */
14862 	update_conn_ill(NULL, ill->ill_ipst);
14863 
14864 	/*
14865 	 * This had to be deferred until we had bound.  Tell routing sockets and
14866 	 * others that this interface is up if it looks like the address has
14867 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14868 	 * duplicate address detection to do its thing.
14869 	 */
14870 	if (ipif->ipif_addr_ready)
14871 		ipif_up_notify(ipif);
14872 	return (0);
14873 }
14874 
14875 /*
14876  * Add the IREs associated with the ipif.
14877  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14878  */
14879 static int
14880 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14881 {
14882 	ill_t		*ill = ipif->ipif_ill;
14883 	ip_stack_t	*ipst = ill->ill_ipst;
14884 	ire_t		*ire_array[20];
14885 	ire_t		**irep = ire_array;
14886 	ire_t		**irep1;
14887 	ipaddr_t	net_mask = 0;
14888 	ipaddr_t	subnet_mask, route_mask;
14889 	int		err;
14890 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14891 	ire_t		*ire_if = NULL;
14892 	uchar_t		*gw;
14893 
14894 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14895 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14896 		/*
14897 		 * If we're on a labeled system then make sure that zone-
14898 		 * private addresses have proper remote host database entries.
14899 		 */
14900 		if (is_system_labeled() &&
14901 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14902 		    !tsol_check_interface_address(ipif))
14903 			return (EINVAL);
14904 
14905 		/* Register the source address for __sin6_src_id */
14906 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14907 		    ipif->ipif_zoneid, ipst);
14908 		if (err != 0) {
14909 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14910 			return (err);
14911 		}
14912 
14913 		if (loopback)
14914 			gw = (uchar_t *)&ipif->ipif_lcl_addr;
14915 		else
14916 			gw = NULL;
14917 
14918 		/* If the interface address is set, create the local IRE. */
14919 		ire_local = ire_create(
14920 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14921 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14922 		    gw,					/* gateway */
14923 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14924 		    ipif->ipif_ill,
14925 		    ipif->ipif_zoneid,
14926 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14927 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14928 		    NULL,
14929 		    ipst);
14930 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14931 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14932 		    ipif->ipif_ire_type,
14933 		    ntohl(ipif->ipif_lcl_addr)));
14934 		if (ire_local == NULL) {
14935 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14936 			err = ENOMEM;
14937 			goto bad;
14938 		}
14939 	} else {
14940 		ip1dbg((
14941 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14942 		    ipif->ipif_ire_type,
14943 		    ntohl(ipif->ipif_lcl_addr),
14944 		    (uint_t)ipif->ipif_flags));
14945 	}
14946 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14947 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14948 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14949 	} else {
14950 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14951 	}
14952 
14953 	subnet_mask = ipif->ipif_net_mask;
14954 
14955 	/*
14956 	 * If mask was not specified, use natural netmask of
14957 	 * interface address. Also, store this mask back into the
14958 	 * ipif struct.
14959 	 */
14960 	if (subnet_mask == 0) {
14961 		subnet_mask = net_mask;
14962 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14963 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14964 		    ipif->ipif_v6subnet);
14965 	}
14966 
14967 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14968 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14969 	    ipif->ipif_subnet != INADDR_ANY) {
14970 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14971 
14972 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14973 			route_mask = IP_HOST_MASK;
14974 		} else {
14975 			route_mask = subnet_mask;
14976 		}
14977 
14978 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14979 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14980 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14981 		    ntohl(ipif->ipif_subnet)));
14982 		ire_if = ire_create(
14983 		    (uchar_t *)&ipif->ipif_subnet,
14984 		    (uchar_t *)&route_mask,
14985 		    (uchar_t *)&ipif->ipif_lcl_addr,
14986 		    ill->ill_net_type,
14987 		    ill,
14988 		    ipif->ipif_zoneid,
14989 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14990 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14991 		    NULL,
14992 		    ipst);
14993 		if (ire_if == NULL) {
14994 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14995 			err = ENOMEM;
14996 			goto bad;
14997 		}
14998 	}
14999 
15000 	/*
15001 	 * Create any necessary broadcast IREs.
15002 	 */
15003 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15004 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15005 		irep = ipif_create_bcast_ires(ipif, irep);
15006 
15007 	/* If an earlier ire_create failed, get out now */
15008 	for (irep1 = irep; irep1 > ire_array; ) {
15009 		irep1--;
15010 		if (*irep1 == NULL) {
15011 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
15012 			err = ENOMEM;
15013 			goto bad;
15014 		}
15015 	}
15016 
15017 	/*
15018 	 * Need to atomically check for IP address availability under
15019 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
15020 	 * ills or new ipifs can be added while we are checking availability.
15021 	 */
15022 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15023 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
15024 	/* Mark it up, and increment counters. */
15025 	ipif->ipif_flags |= IPIF_UP;
15026 	ill->ill_ipif_up_count++;
15027 	err = ip_addr_availability_check(ipif);
15028 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
15029 	rw_exit(&ipst->ips_ill_g_lock);
15030 
15031 	if (err != 0) {
15032 		/*
15033 		 * Our address may already be up on the same ill. In this case,
15034 		 * the ARP entry for our ipif replaced the one for the other
15035 		 * ipif. So we don't want to delete it (otherwise the other ipif
15036 		 * would be unable to send packets).
15037 		 * ip_addr_availability_check() identifies this case for us and
15038 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
15039 		 * which is the expected error code.
15040 		 */
15041 		ill->ill_ipif_up_count--;
15042 		ipif->ipif_flags &= ~IPIF_UP;
15043 		goto bad;
15044 	}
15045 
15046 	/*
15047 	 * Add in all newly created IREs.  ire_create_bcast() has
15048 	 * already checked for duplicates of the IRE_BROADCAST type.
15049 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
15050 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
15051 	 * a /32 route.
15052 	 */
15053 	if (ire_if != NULL) {
15054 		ire_if = ire_add(ire_if);
15055 		if (ire_if == NULL) {
15056 			err = ENOMEM;
15057 			goto bad2;
15058 		}
15059 #ifdef DEBUG
15060 		ire_refhold_notr(ire_if);
15061 		ire_refrele(ire_if);
15062 #endif
15063 	}
15064 	if (ire_local != NULL) {
15065 		ire_local = ire_add(ire_local);
15066 		if (ire_local == NULL) {
15067 			err = ENOMEM;
15068 			goto bad2;
15069 		}
15070 #ifdef DEBUG
15071 		ire_refhold_notr(ire_local);
15072 		ire_refrele(ire_local);
15073 #endif
15074 	}
15075 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15076 	if (ire_local != NULL)
15077 		ipif->ipif_ire_local = ire_local;
15078 	if (ire_if != NULL)
15079 		ipif->ipif_ire_if = ire_if;
15080 	rw_exit(&ipst->ips_ill_g_lock);
15081 	ire_local = NULL;
15082 	ire_if = NULL;
15083 
15084 	/*
15085 	 * We first add all of them, and if that succeeds we refrele the
15086 	 * bunch. That enables us to delete all of them should any of the
15087 	 * ire_adds fail.
15088 	 */
15089 	for (irep1 = irep; irep1 > ire_array; ) {
15090 		irep1--;
15091 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
15092 		*irep1 = ire_add(*irep1);
15093 		if (*irep1 == NULL) {
15094 			err = ENOMEM;
15095 			goto bad2;
15096 		}
15097 	}
15098 
15099 	for (irep1 = irep; irep1 > ire_array; ) {
15100 		irep1--;
15101 		/* refheld by ire_add. */
15102 		if (*irep1 != NULL) {
15103 			ire_refrele(*irep1);
15104 			*irep1 = NULL;
15105 		}
15106 	}
15107 
15108 	if (!loopback) {
15109 		/*
15110 		 * If the broadcast address has been set, make sure it makes
15111 		 * sense based on the interface address.
15112 		 * Only match on ill since we are sharing broadcast addresses.
15113 		 */
15114 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
15115 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
15116 			ire_t	*ire;
15117 
15118 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
15119 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
15120 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
15121 
15122 			if (ire == NULL) {
15123 				/*
15124 				 * If there isn't a matching broadcast IRE,
15125 				 * revert to the default for this netmask.
15126 				 */
15127 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
15128 				mutex_enter(&ipif->ipif_ill->ill_lock);
15129 				ipif_set_default(ipif);
15130 				mutex_exit(&ipif->ipif_ill->ill_lock);
15131 			} else {
15132 				ire_refrele(ire);
15133 			}
15134 		}
15135 
15136 	}
15137 	return (0);
15138 
15139 bad2:
15140 	ill->ill_ipif_up_count--;
15141 	ipif->ipif_flags &= ~IPIF_UP;
15142 
15143 bad:
15144 	ip1dbg(("ipif_add_ires: FAILED \n"));
15145 	if (ire_local != NULL)
15146 		ire_delete(ire_local);
15147 	if (ire_if != NULL)
15148 		ire_delete(ire_if);
15149 
15150 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15151 	ire_local = ipif->ipif_ire_local;
15152 	ipif->ipif_ire_local = NULL;
15153 	ire_if = ipif->ipif_ire_if;
15154 	ipif->ipif_ire_if = NULL;
15155 	rw_exit(&ipst->ips_ill_g_lock);
15156 	if (ire_local != NULL) {
15157 		ire_delete(ire_local);
15158 		ire_refrele_notr(ire_local);
15159 	}
15160 	if (ire_if != NULL) {
15161 		ire_delete(ire_if);
15162 		ire_refrele_notr(ire_if);
15163 	}
15164 
15165 	while (irep > ire_array) {
15166 		irep--;
15167 		if (*irep != NULL) {
15168 			ire_delete(*irep);
15169 		}
15170 	}
15171 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
15172 
15173 	return (err);
15174 }
15175 
15176 /* Remove all the IREs created by ipif_add_ires_v4 */
15177 void
15178 ipif_delete_ires_v4(ipif_t *ipif)
15179 {
15180 	ill_t		*ill = ipif->ipif_ill;
15181 	ip_stack_t	*ipst = ill->ill_ipst;
15182 	ire_t		*ire;
15183 
15184 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15185 	ire = ipif->ipif_ire_local;
15186 	ipif->ipif_ire_local = NULL;
15187 	rw_exit(&ipst->ips_ill_g_lock);
15188 	if (ire != NULL) {
15189 		/*
15190 		 * Move count to ipif so we don't loose the count due to
15191 		 * a down/up dance.
15192 		 */
15193 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
15194 
15195 		ire_delete(ire);
15196 		ire_refrele_notr(ire);
15197 	}
15198 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15199 	ire = ipif->ipif_ire_if;
15200 	ipif->ipif_ire_if = NULL;
15201 	rw_exit(&ipst->ips_ill_g_lock);
15202 	if (ire != NULL) {
15203 		ire_delete(ire);
15204 		ire_refrele_notr(ire);
15205 	}
15206 
15207 	/*
15208 	 * Delete the broadcast IREs.
15209 	 */
15210 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15211 	    !(ipif->ipif_flags & IPIF_NOXMIT))
15212 		ipif_delete_bcast_ires(ipif);
15213 }
15214 
15215 /*
15216  * Checks for availbility of a usable source address (if there is one) when the
15217  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15218  * this selection is done regardless of the destination.
15219  */
15220 boolean_t
15221 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15222     ip_stack_t *ipst)
15223 {
15224 	ipif_t		*ipif = NULL;
15225 	ill_t		*uill;
15226 
15227 	ASSERT(ifindex != 0);
15228 
15229 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15230 	if (uill == NULL)
15231 		return (B_FALSE);
15232 
15233 	mutex_enter(&uill->ill_lock);
15234 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15235 		if (IPIF_IS_CONDEMNED(ipif))
15236 			continue;
15237 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15238 			continue;
15239 		if (!(ipif->ipif_flags & IPIF_UP))
15240 			continue;
15241 		if (ipif->ipif_zoneid != zoneid)
15242 			continue;
15243 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15244 		    ipif->ipif_lcl_addr == INADDR_ANY)
15245 			continue;
15246 		mutex_exit(&uill->ill_lock);
15247 		ill_refrele(uill);
15248 		return (B_TRUE);
15249 	}
15250 	mutex_exit(&uill->ill_lock);
15251 	ill_refrele(uill);
15252 	return (B_FALSE);
15253 }
15254 
15255 /*
15256  * Find an ipif with a good local address on the ill+zoneid.
15257  */
15258 ipif_t *
15259 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15260 {
15261 	ipif_t		*ipif;
15262 
15263 	mutex_enter(&ill->ill_lock);
15264 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15265 		if (IPIF_IS_CONDEMNED(ipif))
15266 			continue;
15267 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15268 			continue;
15269 		if (!(ipif->ipif_flags & IPIF_UP))
15270 			continue;
15271 		if (ipif->ipif_zoneid != zoneid &&
15272 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15273 			continue;
15274 		if (ill->ill_isv6 ?
15275 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15276 		    ipif->ipif_lcl_addr == INADDR_ANY)
15277 			continue;
15278 		ipif_refhold_locked(ipif);
15279 		mutex_exit(&ill->ill_lock);
15280 		return (ipif);
15281 	}
15282 	mutex_exit(&ill->ill_lock);
15283 	return (NULL);
15284 }
15285 
15286 /*
15287  * IP source address type, sorted from worst to best.  For a given type,
15288  * always prefer IP addresses on the same subnet.  All-zones addresses are
15289  * suboptimal because they pose problems with unlabeled destinations.
15290  */
15291 typedef enum {
15292 	IPIF_NONE,
15293 	IPIF_DIFFNET_DEPRECATED,	/* deprecated and different subnet */
15294 	IPIF_SAMENET_DEPRECATED,	/* deprecated and same subnet */
15295 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
15296 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
15297 	IPIF_DIFFNET,			/* normal and different subnet */
15298 	IPIF_SAMENET,			/* normal and same subnet */
15299 	IPIF_LOCALADDR			/* local loopback */
15300 } ipif_type_t;
15301 
15302 /*
15303  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15304  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
15305  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
15306  * the first one, unless IPMP is used in which case we round-robin among them;
15307  * see below for more.
15308  *
15309  * Returns NULL if there is no suitable source address for the ill.
15310  * This only occurs when there is no valid source address for the ill.
15311  */
15312 ipif_t *
15313 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15314     boolean_t allow_usesrc, boolean_t *notreadyp)
15315 {
15316 	ill_t	*usill = NULL;
15317 	ill_t	*ipmp_ill = NULL;
15318 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
15319 	ipif_type_t type, best_type;
15320 	tsol_tpc_t *src_rhtp, *dst_rhtp;
15321 	ip_stack_t *ipst = ill->ill_ipst;
15322 	boolean_t samenet;
15323 
15324 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15325 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15326 		    B_FALSE, ipst);
15327 		if (usill != NULL)
15328 			ill = usill;	/* Select source from usesrc ILL */
15329 		else
15330 			return (NULL);
15331 	}
15332 
15333 	/*
15334 	 * Test addresses should never be used for source address selection,
15335 	 * so if we were passed one, switch to the IPMP meta-interface.
15336 	 */
15337 	if (IS_UNDER_IPMP(ill)) {
15338 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15339 			ill = ipmp_ill;	/* Select source from IPMP ill */
15340 		else
15341 			return (NULL);
15342 	}
15343 
15344 	/*
15345 	 * If we're dealing with an unlabeled destination on a labeled system,
15346 	 * make sure that we ignore source addresses that are incompatible with
15347 	 * the destination's default label.  That destination's default label
15348 	 * must dominate the minimum label on the source address.
15349 	 */
15350 	dst_rhtp = NULL;
15351 	if (is_system_labeled()) {
15352 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15353 		if (dst_rhtp == NULL)
15354 			return (NULL);
15355 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15356 			TPC_RELE(dst_rhtp);
15357 			dst_rhtp = NULL;
15358 		}
15359 	}
15360 
15361 	/*
15362 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15363 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15364 	 * After selecting the right ipif, under ill_lock make sure ipif is
15365 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15366 	 * we retry. Inside the loop we still need to check for CONDEMNED,
15367 	 * but not under a lock.
15368 	 */
15369 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15370 retry:
15371 	/*
15372 	 * For source address selection, we treat the ipif list as circular
15373 	 * and continue until we get back to where we started.  This allows
15374 	 * IPMP to vary source address selection (which improves inbound load
15375 	 * spreading) by caching its last ending point and starting from
15376 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
15377 	 * ills since that can't happen on the IPMP ill.
15378 	 */
15379 	start_ipif = ill->ill_ipif;
15380 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15381 		start_ipif = ill->ill_src_ipif;
15382 
15383 	ipif = start_ipif;
15384 	best_ipif = NULL;
15385 	best_type = IPIF_NONE;
15386 	do {
15387 		if ((next_ipif = ipif->ipif_next) == NULL)
15388 			next_ipif = ill->ill_ipif;
15389 
15390 		if (IPIF_IS_CONDEMNED(ipif))
15391 			continue;
15392 		/* Always skip NOLOCAL and ANYCAST interfaces */
15393 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15394 			continue;
15395 		/* Always skip NOACCEPT interfaces */
15396 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15397 			continue;
15398 		if (!(ipif->ipif_flags & IPIF_UP))
15399 			continue;
15400 
15401 		if (!ipif->ipif_addr_ready) {
15402 			if (notreadyp != NULL)
15403 				*notreadyp = B_TRUE;
15404 			continue;
15405 		}
15406 
15407 		if (zoneid != ALL_ZONES &&
15408 		    ipif->ipif_zoneid != zoneid &&
15409 		    ipif->ipif_zoneid != ALL_ZONES)
15410 			continue;
15411 
15412 		/*
15413 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15414 		 * are not valid as source addresses.
15415 		 */
15416 		if (ipif->ipif_lcl_addr == INADDR_ANY)
15417 			continue;
15418 
15419 		/*
15420 		 * Check compatibility of local address for destination's
15421 		 * default label if we're on a labeled system.	Incompatible
15422 		 * addresses can't be used at all.
15423 		 */
15424 		if (dst_rhtp != NULL) {
15425 			boolean_t incompat;
15426 
15427 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15428 			    IPV4_VERSION, B_FALSE);
15429 			if (src_rhtp == NULL)
15430 				continue;
15431 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15432 			    src_rhtp->tpc_tp.tp_doi !=
15433 			    dst_rhtp->tpc_tp.tp_doi ||
15434 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15435 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15436 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15437 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
15438 			TPC_RELE(src_rhtp);
15439 			if (incompat)
15440 				continue;
15441 		}
15442 
15443 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15444 
15445 		if (ipif->ipif_lcl_addr == dst) {
15446 			type = IPIF_LOCALADDR;
15447 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15448 			type = samenet ? IPIF_SAMENET_DEPRECATED :
15449 			    IPIF_DIFFNET_DEPRECATED;
15450 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
15451 			type = samenet ? IPIF_SAMENET_ALLZONES :
15452 			    IPIF_DIFFNET_ALLZONES;
15453 		} else {
15454 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15455 		}
15456 
15457 		if (type > best_type) {
15458 			best_type = type;
15459 			best_ipif = ipif;
15460 			if (best_type == IPIF_LOCALADDR)
15461 				break; /* can't get better */
15462 		}
15463 	} while ((ipif = next_ipif) != start_ipif);
15464 
15465 	if ((ipif = best_ipif) != NULL) {
15466 		mutex_enter(&ipif->ipif_ill->ill_lock);
15467 		if (IPIF_IS_CONDEMNED(ipif)) {
15468 			mutex_exit(&ipif->ipif_ill->ill_lock);
15469 			goto retry;
15470 		}
15471 		ipif_refhold_locked(ipif);
15472 
15473 		/*
15474 		 * For IPMP, update the source ipif rotor to the next ipif,
15475 		 * provided we can look it up.  (We must not use it if it's
15476 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15477 		 * ipif_free() checked ill_src_ipif.)
15478 		 */
15479 		if (IS_IPMP(ill) && ipif != NULL) {
15480 			next_ipif = ipif->ipif_next;
15481 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15482 				ill->ill_src_ipif = next_ipif;
15483 			else
15484 				ill->ill_src_ipif = NULL;
15485 		}
15486 		mutex_exit(&ipif->ipif_ill->ill_lock);
15487 	}
15488 
15489 	rw_exit(&ipst->ips_ill_g_lock);
15490 	if (usill != NULL)
15491 		ill_refrele(usill);
15492 	if (ipmp_ill != NULL)
15493 		ill_refrele(ipmp_ill);
15494 	if (dst_rhtp != NULL)
15495 		TPC_RELE(dst_rhtp);
15496 
15497 #ifdef DEBUG
15498 	if (ipif == NULL) {
15499 		char buf1[INET6_ADDRSTRLEN];
15500 
15501 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15502 		    ill->ill_name,
15503 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15504 	} else {
15505 		char buf1[INET6_ADDRSTRLEN];
15506 		char buf2[INET6_ADDRSTRLEN];
15507 
15508 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15509 		    ipif->ipif_ill->ill_name,
15510 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15511 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15512 		    buf2, sizeof (buf2))));
15513 	}
15514 #endif /* DEBUG */
15515 	return (ipif);
15516 }
15517 
15518 /*
15519  * Pick a source address based on the destination ill and an optional setsrc
15520  * address.
15521  * The result is stored in srcp. If generation is set, then put the source
15522  * generation number there before we look for the source address (to avoid
15523  * missing changes in the set of source addresses.
15524  * If flagsp is set, then us it to pass back ipif_flags.
15525  *
15526  * If the caller wants to cache the returned source address and detect when
15527  * that might be stale, the caller should pass in a generation argument,
15528  * which the caller can later compare against ips_src_generation
15529  *
15530  * The precedence order for selecting an IPv4 source address is:
15531  *  - RTF_SETSRC on the offlink ire always wins.
15532  *  - If usrsrc is set, swap the ill to be the usesrc one.
15533  *  - If IPMP is used on the ill, select a random address from the most
15534  *    preferred ones below:
15535  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15536  * 2. Not deprecated, not ALL_ZONES
15537  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15538  * 4. Not deprecated, ALL_ZONES
15539  * 5. If onlink destination, same subnet and deprecated
15540  * 6. Deprecated.
15541  *
15542  * We have lower preference for ALL_ZONES IP addresses,
15543  * as they pose problems with unlabeled destinations.
15544  *
15545  * Note that when multiple IP addresses match e.g., #1 we pick
15546  * the first one if IPMP is not in use. With IPMP we randomize.
15547  */
15548 int
15549 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15550     ipaddr_t multicast_ifaddr,
15551     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15552     uint32_t *generation, uint64_t *flagsp)
15553 {
15554 	ipif_t *ipif;
15555 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15556 
15557 	if (flagsp != NULL)
15558 		*flagsp = 0;
15559 
15560 	/*
15561 	 * Need to grab the generation number before we check to
15562 	 * avoid a race with a change to the set of local addresses.
15563 	 * No lock needed since the thread which updates the set of local
15564 	 * addresses use ipif/ill locks and exit those (hence a store memory
15565 	 * barrier) before doing the atomic increase of ips_src_generation.
15566 	 */
15567 	if (generation != NULL) {
15568 		*generation = ipst->ips_src_generation;
15569 	}
15570 
15571 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15572 		*srcp = multicast_ifaddr;
15573 		return (0);
15574 	}
15575 
15576 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15577 	if (setsrc != INADDR_ANY) {
15578 		*srcp = setsrc;
15579 		return (0);
15580 	}
15581 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15582 	if (ipif == NULL) {
15583 		if (notready)
15584 			return (ENETDOWN);
15585 		else
15586 			return (EADDRNOTAVAIL);
15587 	}
15588 	*srcp = ipif->ipif_lcl_addr;
15589 	if (flagsp != NULL)
15590 		*flagsp = ipif->ipif_flags;
15591 	ipif_refrele(ipif);
15592 	return (0);
15593 }
15594 
15595 /* ARGSUSED */
15596 int
15597 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15598     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15599 {
15600 	/*
15601 	 * ill_phyint_reinit merged the v4 and v6 into a single
15602 	 * ipsq.  We might not have been able to complete the
15603 	 * operation in ipif_set_values, if we could not become
15604 	 * exclusive.  If so restart it here.
15605 	 */
15606 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15607 }
15608 
15609 /*
15610  * Can operate on either a module or a driver queue.
15611  * Returns an error if not a module queue.
15612  */
15613 /* ARGSUSED */
15614 int
15615 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15616     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15617 {
15618 	queue_t		*q1 = q;
15619 	char		*cp;
15620 	char		interf_name[LIFNAMSIZ];
15621 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15622 
15623 	if (q->q_next == NULL) {
15624 		ip1dbg((
15625 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15626 		return (EINVAL);
15627 	}
15628 
15629 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15630 		return (EALREADY);
15631 
15632 	do {
15633 		q1 = q1->q_next;
15634 	} while (q1->q_next);
15635 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15636 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15637 
15638 	/*
15639 	 * Here we are not going to delay the ioack until after
15640 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15641 	 * original ioctl message before sending the requests.
15642 	 */
15643 	return (ipif_set_values(q, mp, interf_name, &ppa));
15644 }
15645 
15646 /* ARGSUSED */
15647 int
15648 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15649     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15650 {
15651 	return (ENXIO);
15652 }
15653 
15654 /*
15655  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15656  * `irep'.  Returns a pointer to the next free `irep' entry
15657  * A mirror exists in ipif_delete_bcast_ires().
15658  *
15659  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15660  * done in ire_add.
15661  */
15662 static ire_t **
15663 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15664 {
15665 	ipaddr_t addr;
15666 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15667 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15668 	ill_t *ill = ipif->ipif_ill;
15669 	zoneid_t zoneid = ipif->ipif_zoneid;
15670 
15671 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15672 
15673 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15674 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15675 
15676 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15677 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15678 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15679 
15680 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15681 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15682 
15683 	/*
15684 	 * For backward compatibility, we create net broadcast IREs based on
15685 	 * the old "IP address class system", since some old machines only
15686 	 * respond to these class derived net broadcast.  However, we must not
15687 	 * create these net broadcast IREs if the subnetmask is shorter than
15688 	 * the IP address class based derived netmask.  Otherwise, we may
15689 	 * create a net broadcast address which is the same as an IP address
15690 	 * on the subnet -- and then TCP will refuse to talk to that address.
15691 	 */
15692 	if (netmask < subnetmask) {
15693 		addr = netmask & ipif->ipif_subnet;
15694 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15695 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15696 	}
15697 
15698 	/*
15699 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15700 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15701 	 * created.  Creating these broadcast IREs will only create confusion
15702 	 * as `addr' will be the same as the IP address.
15703 	 */
15704 	if (subnetmask != 0xFFFFFFFF) {
15705 		addr = ipif->ipif_subnet;
15706 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15707 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15708 	}
15709 
15710 	return (irep);
15711 }
15712 
15713 /*
15714  * Mirror of ipif_create_bcast_ires()
15715  */
15716 static void
15717 ipif_delete_bcast_ires(ipif_t *ipif)
15718 {
15719 	ipaddr_t	addr;
15720 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15721 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15722 	ill_t		*ill = ipif->ipif_ill;
15723 	zoneid_t	zoneid = ipif->ipif_zoneid;
15724 	ire_t		*ire;
15725 
15726 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15727 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15728 
15729 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15730 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15731 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15732 
15733 	ire = ire_lookup_bcast(ill, 0, zoneid);
15734 	ASSERT(ire != NULL);
15735 	ire_delete(ire); ire_refrele(ire);
15736 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15737 	ASSERT(ire != NULL);
15738 	ire_delete(ire); ire_refrele(ire);
15739 
15740 	/*
15741 	 * For backward compatibility, we create net broadcast IREs based on
15742 	 * the old "IP address class system", since some old machines only
15743 	 * respond to these class derived net broadcast.  However, we must not
15744 	 * create these net broadcast IREs if the subnetmask is shorter than
15745 	 * the IP address class based derived netmask.  Otherwise, we may
15746 	 * create a net broadcast address which is the same as an IP address
15747 	 * on the subnet -- and then TCP will refuse to talk to that address.
15748 	 */
15749 	if (netmask < subnetmask) {
15750 		addr = netmask & ipif->ipif_subnet;
15751 		ire = ire_lookup_bcast(ill, addr, zoneid);
15752 		ASSERT(ire != NULL);
15753 		ire_delete(ire); ire_refrele(ire);
15754 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15755 		ASSERT(ire != NULL);
15756 		ire_delete(ire); ire_refrele(ire);
15757 	}
15758 
15759 	/*
15760 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15761 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15762 	 * created.  Creating these broadcast IREs will only create confusion
15763 	 * as `addr' will be the same as the IP address.
15764 	 */
15765 	if (subnetmask != 0xFFFFFFFF) {
15766 		addr = ipif->ipif_subnet;
15767 		ire = ire_lookup_bcast(ill, addr, zoneid);
15768 		ASSERT(ire != NULL);
15769 		ire_delete(ire); ire_refrele(ire);
15770 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15771 		ASSERT(ire != NULL);
15772 		ire_delete(ire); ire_refrele(ire);
15773 	}
15774 }
15775 
15776 /*
15777  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15778  * from lifr_flags and the name from lifr_name.
15779  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15780  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15781  * Returns EINPROGRESS when mp has been consumed by queueing it on
15782  * ipx_pending_mp and the ioctl will complete in ip_rput.
15783  *
15784  * Can operate on either a module or a driver queue.
15785  * Returns an error if not a module queue.
15786  */
15787 /* ARGSUSED */
15788 int
15789 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15790     ip_ioctl_cmd_t *ipip, void *if_req)
15791 {
15792 	ill_t	*ill = q->q_ptr;
15793 	phyint_t *phyi;
15794 	ip_stack_t *ipst;
15795 	struct lifreq *lifr = if_req;
15796 	uint64_t new_flags;
15797 
15798 	ASSERT(ipif != NULL);
15799 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15800 
15801 	if (q->q_next == NULL) {
15802 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15803 		return (EINVAL);
15804 	}
15805 
15806 	/*
15807 	 * If we are not writer on 'q' then this interface exists already
15808 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15809 	 * so return EALREADY.
15810 	 */
15811 	if (ill != ipif->ipif_ill)
15812 		return (EALREADY);
15813 
15814 	if (ill->ill_name[0] != '\0')
15815 		return (EALREADY);
15816 
15817 	/*
15818 	 * If there's another ill already with the requested name, ensure
15819 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15820 	 * fuse together two unrelated ills, which will cause chaos.
15821 	 */
15822 	ipst = ill->ill_ipst;
15823 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15824 	    lifr->lifr_name, NULL);
15825 	if (phyi != NULL) {
15826 		ill_t *ill_mate = phyi->phyint_illv4;
15827 
15828 		if (ill_mate == NULL)
15829 			ill_mate = phyi->phyint_illv6;
15830 		ASSERT(ill_mate != NULL);
15831 
15832 		if (ill_mate->ill_media->ip_m_mac_type !=
15833 		    ill->ill_media->ip_m_mac_type) {
15834 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15835 			    "use the same ill name on differing media\n"));
15836 			return (EINVAL);
15837 		}
15838 	}
15839 
15840 	/*
15841 	 * We start off as IFF_IPV4 in ipif_allocate and become
15842 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15843 	 * The only flags that we read from user space are IFF_IPV4,
15844 	 * IFF_IPV6, and IFF_BROADCAST.
15845 	 *
15846 	 * This ill has not been inserted into the global list.
15847 	 * So we are still single threaded and don't need any lock
15848 	 *
15849 	 * Saniy check the flags.
15850 	 */
15851 
15852 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15853 	    ((lifr->lifr_flags & IFF_IPV6) ||
15854 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15855 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15856 		    "or IPv6 i.e., no broadcast \n"));
15857 		return (EINVAL);
15858 	}
15859 
15860 	new_flags =
15861 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15862 
15863 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15864 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15865 		    "IFF_IPV4 or IFF_IPV6\n"));
15866 		return (EINVAL);
15867 	}
15868 
15869 	/*
15870 	 * We always start off as IPv4, so only need to check for IPv6.
15871 	 */
15872 	if ((new_flags & IFF_IPV6) != 0) {
15873 		ill->ill_flags |= ILLF_IPV6;
15874 		ill->ill_flags &= ~ILLF_IPV4;
15875 
15876 		if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15877 			ill->ill_flags |= ILLF_NOLINKLOCAL;
15878 	}
15879 
15880 	if ((new_flags & IFF_BROADCAST) != 0)
15881 		ipif->ipif_flags |= IPIF_BROADCAST;
15882 	else
15883 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15884 
15885 	/* We started off as V4. */
15886 	if (ill->ill_flags & ILLF_IPV6) {
15887 		ill->ill_phyint->phyint_illv6 = ill;
15888 		ill->ill_phyint->phyint_illv4 = NULL;
15889 	}
15890 
15891 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15892 }
15893 
15894 /* ARGSUSED */
15895 int
15896 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15897     ip_ioctl_cmd_t *ipip, void *if_req)
15898 {
15899 	/*
15900 	 * ill_phyint_reinit merged the v4 and v6 into a single
15901 	 * ipsq.  We might not have been able to complete the
15902 	 * slifname in ipif_set_values, if we could not become
15903 	 * exclusive.  If so restart it here
15904 	 */
15905 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15906 }
15907 
15908 /*
15909  * Return a pointer to the ipif which matches the index, IP version type and
15910  * zoneid.
15911  */
15912 ipif_t *
15913 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15914     ip_stack_t *ipst)
15915 {
15916 	ill_t	*ill;
15917 	ipif_t	*ipif = NULL;
15918 
15919 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15920 	if (ill != NULL) {
15921 		mutex_enter(&ill->ill_lock);
15922 		for (ipif = ill->ill_ipif; ipif != NULL;
15923 		    ipif = ipif->ipif_next) {
15924 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15925 			    zoneid == ipif->ipif_zoneid ||
15926 			    ipif->ipif_zoneid == ALL_ZONES)) {
15927 				ipif_refhold_locked(ipif);
15928 				break;
15929 			}
15930 		}
15931 		mutex_exit(&ill->ill_lock);
15932 		ill_refrele(ill);
15933 	}
15934 	return (ipif);
15935 }
15936 
15937 /*
15938  * Change an existing physical interface's index. If the new index
15939  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15940  * Finally, we update other systems which may have a dependence on the
15941  * index value.
15942  */
15943 /* ARGSUSED */
15944 int
15945 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15946     ip_ioctl_cmd_t *ipip, void *ifreq)
15947 {
15948 	ill_t		*ill;
15949 	phyint_t	*phyi;
15950 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15951 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15952 	uint_t	old_index, index;
15953 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15954 	avl_index_t	where;
15955 
15956 	if (ipip->ipi_cmd_type == IF_CMD)
15957 		index = ifr->ifr_index;
15958 	else
15959 		index = lifr->lifr_index;
15960 
15961 	/*
15962 	 * Only allow on physical interface. Also, index zero is illegal.
15963 	 */
15964 	ill = ipif->ipif_ill;
15965 	phyi = ill->ill_phyint;
15966 	if (ipif->ipif_id != 0 || index == 0 || index > IF_INDEX_MAX) {
15967 		return (EINVAL);
15968 	}
15969 
15970 	/* If the index is not changing, no work to do */
15971 	if (phyi->phyint_ifindex == index)
15972 		return (0);
15973 
15974 	/*
15975 	 * Use phyint_exists() to determine if the new interface index
15976 	 * is already in use. If the index is unused then we need to
15977 	 * change the phyint's position in the phyint_list_avl_by_index
15978 	 * tree. If we do not do this, subsequent lookups (using the new
15979 	 * index value) will not find the phyint.
15980 	 */
15981 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15982 	if (phyint_exists(index, ipst)) {
15983 		rw_exit(&ipst->ips_ill_g_lock);
15984 		return (EEXIST);
15985 	}
15986 
15987 	/*
15988 	 * The new index is unused. Set it in the phyint. However we must not
15989 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15990 	 * changes. The event must be bound to old ifindex value.
15991 	 */
15992 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15993 	    &index, sizeof (index));
15994 
15995 	old_index = phyi->phyint_ifindex;
15996 	phyi->phyint_ifindex = index;
15997 
15998 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15999 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16000 	    &index, &where);
16001 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16002 	    phyi, where);
16003 	rw_exit(&ipst->ips_ill_g_lock);
16004 
16005 	/* Update SCTP's ILL list */
16006 	sctp_ill_reindex(ill, old_index);
16007 
16008 	/* Send the routing sockets message */
16009 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
16010 	if (ILL_OTHER(ill))
16011 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
16012 
16013 	/* Perhaps ilgs should use this ill */
16014 	update_conn_ill(NULL, ill->ill_ipst);
16015 	return (0);
16016 }
16017 
16018 /* ARGSUSED */
16019 int
16020 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16021     ip_ioctl_cmd_t *ipip, void *ifreq)
16022 {
16023 	struct ifreq	*ifr = (struct ifreq *)ifreq;
16024 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16025 
16026 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
16027 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16028 	/* Get the interface index */
16029 	if (ipip->ipi_cmd_type == IF_CMD) {
16030 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16031 	} else {
16032 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16033 	}
16034 	return (0);
16035 }
16036 
16037 /* ARGSUSED */
16038 int
16039 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16040     ip_ioctl_cmd_t *ipip, void *ifreq)
16041 {
16042 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16043 
16044 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
16045 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16046 	/* Get the interface zone */
16047 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16048 	lifr->lifr_zoneid = ipif->ipif_zoneid;
16049 	return (0);
16050 }
16051 
16052 /*
16053  * Set the zoneid of an interface.
16054  */
16055 /* ARGSUSED */
16056 int
16057 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16058     ip_ioctl_cmd_t *ipip, void *ifreq)
16059 {
16060 	struct lifreq	*lifr = (struct lifreq *)ifreq;
16061 	int err = 0;
16062 	boolean_t need_up = B_FALSE;
16063 	zone_t *zptr;
16064 	zone_status_t status;
16065 	zoneid_t zoneid;
16066 
16067 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16068 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
16069 		if (!is_system_labeled())
16070 			return (ENOTSUP);
16071 		zoneid = GLOBAL_ZONEID;
16072 	}
16073 
16074 	/* cannot assign instance zero to a non-global zone */
16075 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
16076 		return (ENOTSUP);
16077 
16078 	/*
16079 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
16080 	 * the event of a race with the zone shutdown processing, since IP
16081 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
16082 	 * interface will be cleaned up even if the zone is shut down
16083 	 * immediately after the status check. If the interface can't be brought
16084 	 * down right away, and the zone is shut down before the restart
16085 	 * function is called, we resolve the possible races by rechecking the
16086 	 * zone status in the restart function.
16087 	 */
16088 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
16089 		return (EINVAL);
16090 	status = zone_status_get(zptr);
16091 	zone_rele(zptr);
16092 
16093 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
16094 		return (EINVAL);
16095 
16096 	if (ipif->ipif_flags & IPIF_UP) {
16097 		/*
16098 		 * If the interface is already marked up,
16099 		 * we call ipif_down which will take care
16100 		 * of ditching any IREs that have been set
16101 		 * up based on the old interface address.
16102 		 */
16103 		err = ipif_logical_down(ipif, q, mp);
16104 		if (err == EINPROGRESS)
16105 			return (err);
16106 		(void) ipif_down_tail(ipif);
16107 		need_up = B_TRUE;
16108 	}
16109 
16110 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
16111 	return (err);
16112 }
16113 
16114 static int
16115 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
16116     queue_t *q, mblk_t *mp, boolean_t need_up)
16117 {
16118 	int	err = 0;
16119 	ip_stack_t	*ipst;
16120 
16121 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
16122 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16123 
16124 	if (CONN_Q(q))
16125 		ipst = CONNQ_TO_IPST(q);
16126 	else
16127 		ipst = ILLQ_TO_IPST(q);
16128 
16129 	/*
16130 	 * For exclusive stacks we don't allow a different zoneid than
16131 	 * global.
16132 	 */
16133 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
16134 	    zoneid != GLOBAL_ZONEID)
16135 		return (EINVAL);
16136 
16137 	/* Set the new zone id. */
16138 	ipif->ipif_zoneid = zoneid;
16139 
16140 	/* Update sctp list */
16141 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
16142 
16143 	/* The default multicast interface might have changed */
16144 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
16145 
16146 	if (need_up) {
16147 		/*
16148 		 * Now bring the interface back up.  If this
16149 		 * is the only IPIF for the ILL, ipif_up
16150 		 * will have to re-bind to the device, so
16151 		 * we may get back EINPROGRESS, in which
16152 		 * case, this IOCTL will get completed in
16153 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
16154 		 */
16155 		err = ipif_up(ipif, q, mp);
16156 	}
16157 	return (err);
16158 }
16159 
16160 /* ARGSUSED */
16161 int
16162 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16163     ip_ioctl_cmd_t *ipip, void *if_req)
16164 {
16165 	struct lifreq *lifr = (struct lifreq *)if_req;
16166 	zoneid_t zoneid;
16167 	zone_t *zptr;
16168 	zone_status_t status;
16169 
16170 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16171 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
16172 		zoneid = GLOBAL_ZONEID;
16173 
16174 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
16175 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16176 
16177 	/*
16178 	 * We recheck the zone status to resolve the following race condition:
16179 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
16180 	 * 2) hme0:1 is up and can't be brought down right away;
16181 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
16182 	 * 3) zone "myzone" is halted; the zone status switches to
16183 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
16184 	 * the interfaces to remove - hme0:1 is not returned because it's not
16185 	 * yet in "myzone", so it won't be removed;
16186 	 * 4) the restart function for SIOCSLIFZONE is called; without the
16187 	 * status check here, we would have hme0:1 in "myzone" after it's been
16188 	 * destroyed.
16189 	 * Note that if the status check fails, we need to bring the interface
16190 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
16191 	 * ipif_up_done[_v6]().
16192 	 */
16193 	status = ZONE_IS_UNINITIALIZED;
16194 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
16195 		status = zone_status_get(zptr);
16196 		zone_rele(zptr);
16197 	}
16198 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16199 		if (ipif->ipif_isv6) {
16200 			(void) ipif_up_done_v6(ipif);
16201 		} else {
16202 			(void) ipif_up_done(ipif);
16203 		}
16204 		return (EINVAL);
16205 	}
16206 
16207 	(void) ipif_down_tail(ipif);
16208 
16209 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16210 	    B_TRUE));
16211 }
16212 
16213 /*
16214  * Return the number of addresses on `ill' with one or more of the values
16215  * in `set' set and all of the values in `clear' clear.
16216  */
16217 static uint_t
16218 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16219 {
16220 	ipif_t	*ipif;
16221 	uint_t	cnt = 0;
16222 
16223 	ASSERT(IAM_WRITER_ILL(ill));
16224 
16225 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16226 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16227 			cnt++;
16228 
16229 	return (cnt);
16230 }
16231 
16232 /*
16233  * Return the number of migratable addresses on `ill' that are under
16234  * application control.
16235  */
16236 uint_t
16237 ill_appaddr_cnt(const ill_t *ill)
16238 {
16239 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16240 	    IPIF_NOFAILOVER));
16241 }
16242 
16243 /*
16244  * Return the number of point-to-point addresses on `ill'.
16245  */
16246 uint_t
16247 ill_ptpaddr_cnt(const ill_t *ill)
16248 {
16249 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16250 }
16251 
16252 /* ARGSUSED */
16253 int
16254 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16255     ip_ioctl_cmd_t *ipip, void *ifreq)
16256 {
16257 	struct lifreq	*lifr = ifreq;
16258 
16259 	ASSERT(q->q_next == NULL);
16260 	ASSERT(CONN_Q(q));
16261 
16262 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16263 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16264 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16265 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16266 
16267 	return (0);
16268 }
16269 
16270 /* Find the previous ILL in this usesrc group */
16271 static ill_t *
16272 ill_prev_usesrc(ill_t *uill)
16273 {
16274 	ill_t *ill;
16275 
16276 	for (ill = uill->ill_usesrc_grp_next;
16277 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16278 	    ill = ill->ill_usesrc_grp_next)
16279 		/* do nothing */;
16280 	return (ill);
16281 }
16282 
16283 /*
16284  * Release all members of the usesrc group. This routine is called
16285  * from ill_delete when the interface being unplumbed is the
16286  * group head.
16287  *
16288  * This silently clears the usesrc that ifconfig setup.
16289  * An alternative would be to keep that ifindex, and drop packets on the floor
16290  * since no source address can be selected.
16291  * Even if we keep the current semantics, don't need a lock and a linked list.
16292  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16293  * the one that is being removed. Issue is how we return the usesrc users
16294  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16295  * ill_usesrc_ifindex matching a target ill. We could also do that with an
16296  * ill walk, but the walker would need to insert in the ioctl response.
16297  */
16298 static void
16299 ill_disband_usesrc_group(ill_t *uill)
16300 {
16301 	ill_t *next_ill, *tmp_ill;
16302 	ip_stack_t	*ipst = uill->ill_ipst;
16303 
16304 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16305 	next_ill = uill->ill_usesrc_grp_next;
16306 
16307 	do {
16308 		ASSERT(next_ill != NULL);
16309 		tmp_ill = next_ill->ill_usesrc_grp_next;
16310 		ASSERT(tmp_ill != NULL);
16311 		next_ill->ill_usesrc_grp_next = NULL;
16312 		next_ill->ill_usesrc_ifindex = 0;
16313 		next_ill = tmp_ill;
16314 	} while (next_ill->ill_usesrc_ifindex != 0);
16315 	uill->ill_usesrc_grp_next = NULL;
16316 }
16317 
16318 /*
16319  * Remove the client usesrc ILL from the list and relink to a new list
16320  */
16321 int
16322 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16323 {
16324 	ill_t *ill, *tmp_ill;
16325 	ip_stack_t	*ipst = ucill->ill_ipst;
16326 
16327 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16328 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16329 
16330 	/*
16331 	 * Check if the usesrc client ILL passed in is not already
16332 	 * in use as a usesrc ILL i.e one whose source address is
16333 	 * in use OR a usesrc ILL is not already in use as a usesrc
16334 	 * client ILL
16335 	 */
16336 	if ((ucill->ill_usesrc_ifindex == 0) ||
16337 	    (uill->ill_usesrc_ifindex != 0)) {
16338 		return (-1);
16339 	}
16340 
16341 	ill = ill_prev_usesrc(ucill);
16342 	ASSERT(ill->ill_usesrc_grp_next != NULL);
16343 
16344 	/* Remove from the current list */
16345 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16346 		/* Only two elements in the list */
16347 		ASSERT(ill->ill_usesrc_ifindex == 0);
16348 		ill->ill_usesrc_grp_next = NULL;
16349 	} else {
16350 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16351 	}
16352 
16353 	if (ifindex == 0) {
16354 		ucill->ill_usesrc_ifindex = 0;
16355 		ucill->ill_usesrc_grp_next = NULL;
16356 		return (0);
16357 	}
16358 
16359 	ucill->ill_usesrc_ifindex = ifindex;
16360 	tmp_ill = uill->ill_usesrc_grp_next;
16361 	uill->ill_usesrc_grp_next = ucill;
16362 	ucill->ill_usesrc_grp_next =
16363 	    (tmp_ill != NULL) ? tmp_ill : uill;
16364 	return (0);
16365 }
16366 
16367 /*
16368  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16369  * ip.c for locking details.
16370  */
16371 /* ARGSUSED */
16372 int
16373 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16374     ip_ioctl_cmd_t *ipip, void *ifreq)
16375 {
16376 	struct lifreq *lifr = (struct lifreq *)ifreq;
16377 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16378 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16379 	int err = 0, ret;
16380 	uint_t ifindex;
16381 	ipsq_t *ipsq = NULL;
16382 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
16383 
16384 	ASSERT(IAM_WRITER_IPIF(ipif));
16385 	ASSERT(q->q_next == NULL);
16386 	ASSERT(CONN_Q(q));
16387 
16388 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16389 
16390 	ifindex = lifr->lifr_index;
16391 	if (ifindex == 0) {
16392 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16393 			/* non usesrc group interface, nothing to reset */
16394 			return (0);
16395 		}
16396 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16397 		/* valid reset request */
16398 		reset_flg = B_TRUE;
16399 	}
16400 
16401 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16402 	if (usesrc_ill == NULL)
16403 		return (ENXIO);
16404 	if (usesrc_ill == ipif->ipif_ill) {
16405 		ill_refrele(usesrc_ill);
16406 		return (EINVAL);
16407 	}
16408 
16409 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16410 	    NEW_OP, B_TRUE);
16411 	if (ipsq == NULL) {
16412 		err = EINPROGRESS;
16413 		/* Operation enqueued on the ipsq of the usesrc ILL */
16414 		goto done;
16415 	}
16416 
16417 	/* USESRC isn't currently supported with IPMP */
16418 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16419 		err = ENOTSUP;
16420 		goto done;
16421 	}
16422 
16423 	/*
16424 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
16425 	 * used by IPMP underlying interfaces, but someone might think it's
16426 	 * more general and try to use it independently with VNI.)
16427 	 */
16428 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16429 		err = ENOTSUP;
16430 		goto done;
16431 	}
16432 
16433 	/*
16434 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16435 	 * already a client then return EINVAL
16436 	 */
16437 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16438 		err = EINVAL;
16439 		goto done;
16440 	}
16441 
16442 	/*
16443 	 * If the ill_usesrc_ifindex field is already set to what it needs to
16444 	 * be then this is a duplicate operation.
16445 	 */
16446 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16447 		err = 0;
16448 		goto done;
16449 	}
16450 
16451 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16452 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16453 	    usesrc_ill->ill_isv6));
16454 
16455 	/*
16456 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16457 	 * and the ill_usesrc_ifindex fields
16458 	 */
16459 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16460 
16461 	if (reset_flg) {
16462 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16463 		if (ret != 0) {
16464 			err = EINVAL;
16465 		}
16466 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
16467 		goto done;
16468 	}
16469 
16470 	/*
16471 	 * Four possibilities to consider:
16472 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16473 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16474 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16475 	 * 4. Both are part of their respective usesrc groups
16476 	 */
16477 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16478 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16479 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16480 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16481 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16482 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16483 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16484 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16485 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16486 		/* Insert at head of list */
16487 		usesrc_cli_ill->ill_usesrc_grp_next =
16488 		    usesrc_ill->ill_usesrc_grp_next;
16489 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16490 	} else {
16491 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16492 		    ifindex);
16493 		if (ret != 0)
16494 			err = EINVAL;
16495 	}
16496 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
16497 
16498 done:
16499 	if (ipsq != NULL)
16500 		ipsq_exit(ipsq);
16501 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16502 	ill_refrele(usesrc_ill);
16503 
16504 	/* Let conn_ixa caching know that source address selection changed */
16505 	ip_update_source_selection(ipst);
16506 
16507 	return (err);
16508 }
16509 
16510 /* ARGSUSED */
16511 int
16512 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16513     ip_ioctl_cmd_t *ipip, void *if_req)
16514 {
16515 	struct lifreq	*lifr = (struct lifreq *)if_req;
16516 	ill_t		*ill = ipif->ipif_ill;
16517 
16518 	/*
16519 	 * Need a lock since IFF_UP can be set even when there are
16520 	 * references to the ipif.
16521 	 */
16522 	mutex_enter(&ill->ill_lock);
16523 	if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16524 		lifr->lifr_dadstate = DAD_IN_PROGRESS;
16525 	else
16526 		lifr->lifr_dadstate = DAD_DONE;
16527 	mutex_exit(&ill->ill_lock);
16528 	return (0);
16529 }
16530 
16531 /*
16532  * comparison function used by avl.
16533  */
16534 static int
16535 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16536 {
16537 
16538 	uint_t index;
16539 
16540 	ASSERT(phyip != NULL && index_ptr != NULL);
16541 
16542 	index = *((uint_t *)index_ptr);
16543 	/*
16544 	 * let the phyint with the lowest index be on top.
16545 	 */
16546 	if (((phyint_t *)phyip)->phyint_ifindex < index)
16547 		return (1);
16548 	if (((phyint_t *)phyip)->phyint_ifindex > index)
16549 		return (-1);
16550 	return (0);
16551 }
16552 
16553 /*
16554  * comparison function used by avl.
16555  */
16556 static int
16557 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16558 {
16559 	ill_t *ill;
16560 	int res = 0;
16561 
16562 	ASSERT(phyip != NULL && name_ptr != NULL);
16563 
16564 	if (((phyint_t *)phyip)->phyint_illv4)
16565 		ill = ((phyint_t *)phyip)->phyint_illv4;
16566 	else
16567 		ill = ((phyint_t *)phyip)->phyint_illv6;
16568 	ASSERT(ill != NULL);
16569 
16570 	res = strcmp(ill->ill_name, (char *)name_ptr);
16571 	if (res > 0)
16572 		return (1);
16573 	else if (res < 0)
16574 		return (-1);
16575 	return (0);
16576 }
16577 
16578 /*
16579  * This function is called on the unplumb path via ill_glist_delete() when
16580  * there are no ills left on the phyint and thus the phyint can be freed.
16581  */
16582 static void
16583 phyint_free(phyint_t *phyi)
16584 {
16585 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16586 
16587 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16588 
16589 	/*
16590 	 * If this phyint was an IPMP meta-interface, blow away the group.
16591 	 * This is safe to do because all of the illgrps have already been
16592 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16593 	 * If we're cleaning up as a result of failed initialization,
16594 	 * phyint_grp may be NULL.
16595 	 */
16596 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16597 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16598 		ipmp_grp_destroy(phyi->phyint_grp);
16599 		phyi->phyint_grp = NULL;
16600 		rw_exit(&ipst->ips_ipmp_lock);
16601 	}
16602 
16603 	/*
16604 	 * If this interface was under IPMP, take it out of the group.
16605 	 */
16606 	if (phyi->phyint_grp != NULL)
16607 		ipmp_phyint_leave_grp(phyi);
16608 
16609 	/*
16610 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16611 	 * will be freed in ipsq_exit().
16612 	 */
16613 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16614 	phyi->phyint_name[0] = '\0';
16615 
16616 	mi_free(phyi);
16617 }
16618 
16619 /*
16620  * Attach the ill to the phyint structure which can be shared by both
16621  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16622  * function is called from ipif_set_values and ill_lookup_on_name (for
16623  * loopback) where we know the name of the ill. We lookup the ill and if
16624  * there is one present already with the name use that phyint. Otherwise
16625  * reuse the one allocated by ill_init.
16626  */
16627 static void
16628 ill_phyint_reinit(ill_t *ill)
16629 {
16630 	boolean_t isv6 = ill->ill_isv6;
16631 	phyint_t *phyi_old;
16632 	phyint_t *phyi;
16633 	avl_index_t where = 0;
16634 	ill_t	*ill_other = NULL;
16635 	ip_stack_t	*ipst = ill->ill_ipst;
16636 
16637 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16638 
16639 	phyi_old = ill->ill_phyint;
16640 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16641 	    phyi_old->phyint_illv6 == NULL));
16642 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16643 	    phyi_old->phyint_illv4 == NULL));
16644 	ASSERT(phyi_old->phyint_ifindex == 0);
16645 
16646 	/*
16647 	 * Now that our ill has a name, set it in the phyint.
16648 	 */
16649 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16650 
16651 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16652 	    ill->ill_name, &where);
16653 
16654 	/*
16655 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16656 	 *    the global list of ills. So no other thread could have located
16657 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16658 	 * 2. Now locate the other protocol instance of this ill.
16659 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16660 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16661 	 *    of neither ill can change.
16662 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16663 	 *    other ill.
16664 	 * 5. Release all locks.
16665 	 */
16666 
16667 	/*
16668 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16669 	 * we are initializing IPv4.
16670 	 */
16671 	if (phyi != NULL) {
16672 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16673 		ASSERT(ill_other->ill_phyint != NULL);
16674 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16675 		    (!isv6 && ill_other->ill_isv6));
16676 		GRAB_ILL_LOCKS(ill, ill_other);
16677 		/*
16678 		 * We are potentially throwing away phyint_flags which
16679 		 * could be different from the one that we obtain from
16680 		 * ill_other->ill_phyint. But it is okay as we are assuming
16681 		 * that the state maintained within IP is correct.
16682 		 */
16683 		mutex_enter(&phyi->phyint_lock);
16684 		if (isv6) {
16685 			ASSERT(phyi->phyint_illv6 == NULL);
16686 			phyi->phyint_illv6 = ill;
16687 		} else {
16688 			ASSERT(phyi->phyint_illv4 == NULL);
16689 			phyi->phyint_illv4 = ill;
16690 		}
16691 
16692 		/*
16693 		 * Delete the old phyint and make its ipsq eligible
16694 		 * to be freed in ipsq_exit().
16695 		 */
16696 		phyi_old->phyint_illv4 = NULL;
16697 		phyi_old->phyint_illv6 = NULL;
16698 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16699 		phyi_old->phyint_name[0] = '\0';
16700 		mi_free(phyi_old);
16701 	} else {
16702 		mutex_enter(&ill->ill_lock);
16703 		/*
16704 		 * We don't need to acquire any lock, since
16705 		 * the ill is not yet visible globally  and we
16706 		 * have not yet released the ill_g_lock.
16707 		 */
16708 		phyi = phyi_old;
16709 		mutex_enter(&phyi->phyint_lock);
16710 		/* XXX We need a recovery strategy here. */
16711 		if (!phyint_assign_ifindex(phyi, ipst))
16712 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16713 
16714 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16715 		    (void *)phyi, where);
16716 
16717 		(void) avl_find(&ipst->ips_phyint_g_list->
16718 		    phyint_list_avl_by_index,
16719 		    &phyi->phyint_ifindex, &where);
16720 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16721 		    (void *)phyi, where);
16722 	}
16723 
16724 	/*
16725 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16726 	 * pending mp is not affected because that is per ill basis.
16727 	 */
16728 	ill->ill_phyint = phyi;
16729 
16730 	/*
16731 	 * Now that the phyint's ifindex has been assigned, complete the
16732 	 * remaining
16733 	 */
16734 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16735 	if (ill->ill_isv6) {
16736 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16737 		    ill->ill_phyint->phyint_ifindex;
16738 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16739 	} else {
16740 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16741 	}
16742 
16743 	/*
16744 	 * Generate an event within the hooks framework to indicate that
16745 	 * a new interface has just been added to IP.  For this event to
16746 	 * be generated, the network interface must, at least, have an
16747 	 * ifindex assigned to it.  (We don't generate the event for
16748 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16749 	 *
16750 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16751 	 * that the ordering of delivered events to listeners matches the
16752 	 * order of them in the kernel.
16753 	 */
16754 	if (!IS_LOOPBACK(ill)) {
16755 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16756 		    ill->ill_name_length);
16757 	}
16758 	RELEASE_ILL_LOCKS(ill, ill_other);
16759 	mutex_exit(&phyi->phyint_lock);
16760 }
16761 
16762 /*
16763  * Notify any downstream modules of the name of this interface.
16764  * An M_IOCTL is used even though we don't expect a successful reply.
16765  * Any reply message from the driver (presumably an M_IOCNAK) will
16766  * eventually get discarded somewhere upstream.  The message format is
16767  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16768  * to IP.
16769  */
16770 static void
16771 ip_ifname_notify(ill_t *ill, queue_t *q)
16772 {
16773 	mblk_t *mp1, *mp2;
16774 	struct iocblk *iocp;
16775 	struct lifreq *lifr;
16776 
16777 	mp1 = mkiocb(SIOCSLIFNAME);
16778 	if (mp1 == NULL)
16779 		return;
16780 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16781 	if (mp2 == NULL) {
16782 		freeb(mp1);
16783 		return;
16784 	}
16785 
16786 	mp1->b_cont = mp2;
16787 	iocp = (struct iocblk *)mp1->b_rptr;
16788 	iocp->ioc_count = sizeof (struct lifreq);
16789 
16790 	lifr = (struct lifreq *)mp2->b_rptr;
16791 	mp2->b_wptr += sizeof (struct lifreq);
16792 	bzero(lifr, sizeof (struct lifreq));
16793 
16794 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16795 	lifr->lifr_ppa = ill->ill_ppa;
16796 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16797 
16798 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16799 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16800 	putnext(q, mp1);
16801 }
16802 
16803 static int
16804 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16805 {
16806 	int		err;
16807 	ip_stack_t	*ipst = ill->ill_ipst;
16808 	phyint_t	*phyi = ill->ill_phyint;
16809 
16810 	/*
16811 	 * Now that ill_name is set, the configuration for the IPMP
16812 	 * meta-interface can be performed.
16813 	 */
16814 	if (IS_IPMP(ill)) {
16815 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16816 		/*
16817 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16818 		 * meta-interface and we need to create the IPMP group.
16819 		 */
16820 		if (phyi->phyint_grp == NULL) {
16821 			/*
16822 			 * If someone has renamed another IPMP group to have
16823 			 * the same name as our interface, bail.
16824 			 */
16825 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16826 				rw_exit(&ipst->ips_ipmp_lock);
16827 				return (EEXIST);
16828 			}
16829 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16830 			if (phyi->phyint_grp == NULL) {
16831 				rw_exit(&ipst->ips_ipmp_lock);
16832 				return (ENOMEM);
16833 			}
16834 		}
16835 		rw_exit(&ipst->ips_ipmp_lock);
16836 	}
16837 
16838 	/* Tell downstream modules where they are. */
16839 	ip_ifname_notify(ill, q);
16840 
16841 	/*
16842 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16843 	 * Error cases are ENOMEM ...
16844 	 */
16845 	err = ill_dl_phys(ill, ipif, mp, q);
16846 
16847 	if (ill->ill_isv6) {
16848 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16849 		if (ipst->ips_mld_slowtimeout_id == 0) {
16850 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16851 			    (void *)ipst,
16852 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16853 		}
16854 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16855 	} else {
16856 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16857 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16858 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16859 			    (void *)ipst,
16860 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16861 		}
16862 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16863 	}
16864 
16865 	return (err);
16866 }
16867 
16868 /*
16869  * Common routine for ppa and ifname setting. Should be called exclusive.
16870  *
16871  * Returns EINPROGRESS when mp has been consumed by queueing it on
16872  * ipx_pending_mp and the ioctl will complete in ip_rput.
16873  *
16874  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16875  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16876  * For SLIFNAME, we pass these values back to the userland.
16877  */
16878 static int
16879 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16880 {
16881 	ill_t	*ill;
16882 	ipif_t	*ipif;
16883 	ipsq_t	*ipsq;
16884 	char	*ppa_ptr;
16885 	char	*old_ptr;
16886 	char	old_char;
16887 	int	error;
16888 	ip_stack_t	*ipst;
16889 
16890 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16891 	ASSERT(q->q_next != NULL);
16892 	ASSERT(interf_name != NULL);
16893 
16894 	ill = (ill_t *)q->q_ptr;
16895 	ipst = ill->ill_ipst;
16896 
16897 	ASSERT(ill->ill_ipst != NULL);
16898 	ASSERT(ill->ill_name[0] == '\0');
16899 	ASSERT(IAM_WRITER_ILL(ill));
16900 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16901 	ASSERT(ill->ill_ppa == UINT_MAX);
16902 
16903 	ill->ill_defend_start = ill->ill_defend_count = 0;
16904 	/* The ppa is sent down by ifconfig or is chosen */
16905 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16906 		return (EINVAL);
16907 	}
16908 
16909 	/*
16910 	 * make sure ppa passed in is same as ppa in the name.
16911 	 * This check is not made when ppa == UINT_MAX in that case ppa
16912 	 * in the name could be anything. System will choose a ppa and
16913 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16914 	 */
16915 	if (*new_ppa_ptr != UINT_MAX) {
16916 		/* stoi changes the pointer */
16917 		old_ptr = ppa_ptr;
16918 		/*
16919 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16920 		 * (they don't have an externally visible ppa).  We assign one
16921 		 * here so that we can manage the interface.  Note that in
16922 		 * the past this value was always 0 for DLPI 1 drivers.
16923 		 */
16924 		if (*new_ppa_ptr == 0)
16925 			*new_ppa_ptr = stoi(&old_ptr);
16926 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16927 			return (EINVAL);
16928 	}
16929 	/*
16930 	 * terminate string before ppa
16931 	 * save char at that location.
16932 	 */
16933 	old_char = ppa_ptr[0];
16934 	ppa_ptr[0] = '\0';
16935 
16936 	ill->ill_ppa = *new_ppa_ptr;
16937 	/*
16938 	 * Finish as much work now as possible before calling ill_glist_insert
16939 	 * which makes the ill globally visible and also merges it with the
16940 	 * other protocol instance of this phyint. The remaining work is
16941 	 * done after entering the ipsq which may happen sometime later.
16942 	 */
16943 	ipif = ill->ill_ipif;
16944 
16945 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16946 	ipif_assign_seqid(ipif);
16947 
16948 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16949 		ill->ill_flags |= ILLF_IPV4;
16950 
16951 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16952 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16953 
16954 	if (ill->ill_flags & ILLF_IPV6) {
16955 
16956 		ill->ill_isv6 = B_TRUE;
16957 		ill_set_inputfn(ill);
16958 		if (ill->ill_rq != NULL) {
16959 			ill->ill_rq->q_qinfo = &iprinitv6;
16960 		}
16961 
16962 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16963 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16964 		ipif->ipif_v6subnet = ipv6_all_zeros;
16965 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16966 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16967 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16968 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16969 		/*
16970 		 * point-to-point or Non-mulicast capable
16971 		 * interfaces won't do NUD unless explicitly
16972 		 * configured to do so.
16973 		 */
16974 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16975 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16976 			ill->ill_flags |= ILLF_NONUD;
16977 		}
16978 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16979 		if (ill->ill_flags & ILLF_NOARP) {
16980 			/*
16981 			 * Note: xresolv interfaces will eventually need
16982 			 * NOARP set here as well, but that will require
16983 			 * those external resolvers to have some
16984 			 * knowledge of that flag and act appropriately.
16985 			 * Not to be changed at present.
16986 			 */
16987 			ill->ill_flags &= ~ILLF_NOARP;
16988 		}
16989 		/*
16990 		 * Set the ILLF_ROUTER flag according to the global
16991 		 * IPv6 forwarding policy.
16992 		 */
16993 		if (ipst->ips_ipv6_forwarding != 0)
16994 			ill->ill_flags |= ILLF_ROUTER;
16995 	} else if (ill->ill_flags & ILLF_IPV4) {
16996 		ill->ill_isv6 = B_FALSE;
16997 		ill_set_inputfn(ill);
16998 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16999 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
17000 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
17001 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
17002 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
17003 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
17004 		/*
17005 		 * Set the ILLF_ROUTER flag according to the global
17006 		 * IPv4 forwarding policy.
17007 		 */
17008 		if (ipst->ips_ip_forwarding != 0)
17009 			ill->ill_flags |= ILLF_ROUTER;
17010 	}
17011 
17012 	ASSERT(ill->ill_phyint != NULL);
17013 
17014 	/*
17015 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
17016 	 * be completed in ill_glist_insert -> ill_phyint_reinit
17017 	 */
17018 	if (!ill_allocate_mibs(ill))
17019 		return (ENOMEM);
17020 
17021 	/*
17022 	 * Pick a default sap until we get the DL_INFO_ACK back from
17023 	 * the driver.
17024 	 */
17025 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
17026 	    ill->ill_media->ip_m_ipv4sap;
17027 
17028 	ill->ill_ifname_pending = 1;
17029 	ill->ill_ifname_pending_err = 0;
17030 
17031 	/*
17032 	 * When the first ipif comes up in ipif_up_done(), multicast groups
17033 	 * that were joined while this ill was not bound to the DLPI link need
17034 	 * to be recovered by ill_recover_multicast().
17035 	 */
17036 	ill->ill_need_recover_multicast = 1;
17037 
17038 	ill_refhold(ill);
17039 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17040 	if ((error = ill_glist_insert(ill, interf_name,
17041 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
17042 		ill->ill_ppa = UINT_MAX;
17043 		ill->ill_name[0] = '\0';
17044 		/*
17045 		 * undo null termination done above.
17046 		 */
17047 		ppa_ptr[0] = old_char;
17048 		rw_exit(&ipst->ips_ill_g_lock);
17049 		ill_refrele(ill);
17050 		return (error);
17051 	}
17052 
17053 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
17054 
17055 	/*
17056 	 * When we return the buffer pointed to by interf_name should contain
17057 	 * the same name as in ill_name.
17058 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
17059 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
17060 	 * so copy full name and update the ppa ptr.
17061 	 * When ppa passed in != UINT_MAX all values are correct just undo
17062 	 * null termination, this saves a bcopy.
17063 	 */
17064 	if (*new_ppa_ptr == UINT_MAX) {
17065 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
17066 		*new_ppa_ptr = ill->ill_ppa;
17067 	} else {
17068 		/*
17069 		 * undo null termination done above.
17070 		 */
17071 		ppa_ptr[0] = old_char;
17072 	}
17073 
17074 	/* Let SCTP know about this ILL */
17075 	sctp_update_ill(ill, SCTP_ILL_INSERT);
17076 
17077 	/*
17078 	 * ill_glist_insert has made the ill visible globally, and
17079 	 * ill_phyint_reinit could have changed the ipsq. At this point,
17080 	 * we need to hold the ips_ill_g_lock across the call to enter the
17081 	 * ipsq to enforce atomicity and prevent reordering. In the event
17082 	 * the ipsq has changed, and if the new ipsq is currently busy,
17083 	 * we need to make sure that this half-completed ioctl is ahead of
17084 	 * any subsequent ioctl. We achieve this by not dropping the
17085 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
17086 	 * ensuring that new ioctls can't start.
17087 	 */
17088 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
17089 	    B_TRUE);
17090 
17091 	rw_exit(&ipst->ips_ill_g_lock);
17092 	ill_refrele(ill);
17093 	if (ipsq == NULL)
17094 		return (EINPROGRESS);
17095 
17096 	/*
17097 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
17098 	 */
17099 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
17100 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
17101 	else
17102 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
17103 
17104 	error = ipif_set_values_tail(ill, ipif, mp, q);
17105 	ipsq_exit(ipsq);
17106 	if (error != 0 && error != EINPROGRESS) {
17107 		/*
17108 		 * restore previous values
17109 		 */
17110 		ill->ill_isv6 = B_FALSE;
17111 		ill_set_inputfn(ill);
17112 	}
17113 	return (error);
17114 }
17115 
17116 void
17117 ipif_init(ip_stack_t *ipst)
17118 {
17119 	int i;
17120 
17121 	for (i = 0; i < MAX_G_HEADS; i++) {
17122 		ipst->ips_ill_g_heads[i].ill_g_list_head =
17123 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17124 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
17125 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
17126 	}
17127 
17128 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17129 	    ill_phyint_compare_index,
17130 	    sizeof (phyint_t),
17131 	    offsetof(struct phyint, phyint_avl_by_index));
17132 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17133 	    ill_phyint_compare_name,
17134 	    sizeof (phyint_t),
17135 	    offsetof(struct phyint, phyint_avl_by_name));
17136 }
17137 
17138 /*
17139  * Save enough information so that we can recreate the IRE if
17140  * the interface goes down and then up.
17141  */
17142 void
17143 ill_save_ire(ill_t *ill, ire_t *ire)
17144 {
17145 	mblk_t	*save_mp;
17146 
17147 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
17148 	if (save_mp != NULL) {
17149 		ifrt_t	*ifrt;
17150 
17151 		save_mp->b_wptr += sizeof (ifrt_t);
17152 		ifrt = (ifrt_t *)save_mp->b_rptr;
17153 		bzero(ifrt, sizeof (ifrt_t));
17154 		ifrt->ifrt_type = ire->ire_type;
17155 		if (ire->ire_ipversion == IPV4_VERSION) {
17156 			ASSERT(!ill->ill_isv6);
17157 			ifrt->ifrt_addr = ire->ire_addr;
17158 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
17159 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
17160 			ifrt->ifrt_mask = ire->ire_mask;
17161 		} else {
17162 			ASSERT(ill->ill_isv6);
17163 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
17164 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
17165 			mutex_enter(&ire->ire_lock);
17166 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
17167 			mutex_exit(&ire->ire_lock);
17168 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
17169 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
17170 		}
17171 		ifrt->ifrt_flags = ire->ire_flags;
17172 		ifrt->ifrt_zoneid = ire->ire_zoneid;
17173 		mutex_enter(&ill->ill_saved_ire_lock);
17174 		save_mp->b_cont = ill->ill_saved_ire_mp;
17175 		ill->ill_saved_ire_mp = save_mp;
17176 		ill->ill_saved_ire_cnt++;
17177 		mutex_exit(&ill->ill_saved_ire_lock);
17178 	}
17179 }
17180 
17181 /*
17182  * Remove one entry from ill_saved_ire_mp.
17183  */
17184 void
17185 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
17186 {
17187 	mblk_t	**mpp;
17188 	mblk_t	*mp;
17189 	ifrt_t	*ifrt;
17190 
17191 	/* Remove from ill_saved_ire_mp list if it is there */
17192 	mutex_enter(&ill->ill_saved_ire_lock);
17193 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
17194 	    mpp = &(*mpp)->b_cont) {
17195 		in6_addr_t	gw_addr_v6;
17196 
17197 		/*
17198 		 * On a given ill, the tuple of address, gateway, mask,
17199 		 * ire_type, and zoneid is unique for each saved IRE.
17200 		 */
17201 		mp = *mpp;
17202 		ifrt = (ifrt_t *)mp->b_rptr;
17203 		/* ire_gateway_addr_v6 can change - need lock */
17204 		mutex_enter(&ire->ire_lock);
17205 		gw_addr_v6 = ire->ire_gateway_addr_v6;
17206 		mutex_exit(&ire->ire_lock);
17207 
17208 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17209 		    ifrt->ifrt_type != ire->ire_type)
17210 			continue;
17211 
17212 		if (ill->ill_isv6 ?
17213 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17214 		    &ire->ire_addr_v6) &&
17215 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17216 		    &gw_addr_v6) &&
17217 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17218 		    &ire->ire_mask_v6)) :
17219 		    (ifrt->ifrt_addr == ire->ire_addr &&
17220 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17221 		    ifrt->ifrt_mask == ire->ire_mask)) {
17222 			*mpp = mp->b_cont;
17223 			ill->ill_saved_ire_cnt--;
17224 			freeb(mp);
17225 			break;
17226 		}
17227 	}
17228 	mutex_exit(&ill->ill_saved_ire_lock);
17229 }
17230 
17231 /*
17232  * IP multirouting broadcast routes handling
17233  * Append CGTP broadcast IREs to regular ones created
17234  * at ifconfig time.
17235  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17236  * the destination and the gateway are broadcast addresses.
17237  * The caller has verified that the destination is an IRE_BROADCAST and that
17238  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17239  * we create a MULTIRT IRE_BROADCAST.
17240  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17241  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17242  */
17243 static void
17244 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17245 {
17246 	ire_t *ire_prim;
17247 
17248 	ASSERT(ire != NULL);
17249 
17250 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17251 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17252 	    NULL);
17253 	if (ire_prim != NULL) {
17254 		/*
17255 		 * We are in the special case of broadcasts for
17256 		 * CGTP. We add an IRE_BROADCAST that holds
17257 		 * the RTF_MULTIRT flag, the destination
17258 		 * address and the low level
17259 		 * info of ire_prim. In other words, CGTP
17260 		 * broadcast is added to the redundant ipif.
17261 		 */
17262 		ill_t *ill_prim;
17263 		ire_t  *bcast_ire;
17264 
17265 		ill_prim = ire_prim->ire_ill;
17266 
17267 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17268 		    (void *)ire_prim, (void *)ill_prim));
17269 
17270 		bcast_ire = ire_create(
17271 		    (uchar_t *)&ire->ire_addr,
17272 		    (uchar_t *)&ip_g_all_ones,
17273 		    (uchar_t *)&ire->ire_gateway_addr,
17274 		    IRE_BROADCAST,
17275 		    ill_prim,
17276 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
17277 		    ire->ire_flags | RTF_KERNEL,
17278 		    NULL,
17279 		    ipst);
17280 
17281 		/*
17282 		 * Here we assume that ire_add does head insertion so that
17283 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17284 		 */
17285 		if (bcast_ire != NULL) {
17286 			if (ire->ire_flags & RTF_SETSRC) {
17287 				bcast_ire->ire_setsrc_addr =
17288 				    ire->ire_setsrc_addr;
17289 			}
17290 			bcast_ire = ire_add(bcast_ire);
17291 			if (bcast_ire != NULL) {
17292 				ip2dbg(("ip_cgtp_filter_bcast_add: "
17293 				    "added bcast_ire %p\n",
17294 				    (void *)bcast_ire));
17295 
17296 				ill_save_ire(ill_prim, bcast_ire);
17297 				ire_refrele(bcast_ire);
17298 			}
17299 		}
17300 		ire_refrele(ire_prim);
17301 	}
17302 }
17303 
17304 /*
17305  * IP multirouting broadcast routes handling
17306  * Remove the broadcast ire.
17307  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17308  * the destination and the gateway are broadcast addresses.
17309  * The caller has only verified that RTF_MULTIRT was set. We check
17310  * that the destination is broadcast and that the gateway is a broadcast
17311  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17312  */
17313 static void
17314 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17315 {
17316 	ASSERT(ire != NULL);
17317 
17318 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17319 		ire_t *ire_prim;
17320 
17321 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17322 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17323 		    ipst, NULL);
17324 		if (ire_prim != NULL) {
17325 			ill_t *ill_prim;
17326 			ire_t  *bcast_ire;
17327 
17328 			ill_prim = ire_prim->ire_ill;
17329 
17330 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
17331 			    "ire_prim %p, ill_prim %p\n",
17332 			    (void *)ire_prim, (void *)ill_prim));
17333 
17334 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17335 			    ire->ire_gateway_addr, IRE_BROADCAST,
17336 			    ill_prim, ALL_ZONES, NULL,
17337 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17338 			    MATCH_IRE_MASK, 0, ipst, NULL);
17339 
17340 			if (bcast_ire != NULL) {
17341 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
17342 				    "looked up bcast_ire %p\n",
17343 				    (void *)bcast_ire));
17344 				ill_remove_saved_ire(bcast_ire->ire_ill,
17345 				    bcast_ire);
17346 				ire_delete(bcast_ire);
17347 				ire_refrele(bcast_ire);
17348 			}
17349 			ire_refrele(ire_prim);
17350 		}
17351 	}
17352 }
17353 
17354 /*
17355  * Derive an interface id from the link layer address.
17356  * Knows about IEEE 802 and IEEE EUI-64 mappings.
17357  */
17358 static void
17359 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17360 {
17361 	char		*addr;
17362 
17363 	/*
17364 	 * Note that some IPv6 interfaces get plumbed over links that claim to
17365 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17366 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
17367 	 * interface ID on IPv6 interfaces above links that actually have real
17368 	 * Ethernet addresses.
17369 	 */
17370 	if (ill->ill_phys_addr_length == ETHERADDRL) {
17371 		/* Form EUI-64 like address */
17372 		addr = (char *)&v6addr->s6_addr32[2];
17373 		bcopy(ill->ill_phys_addr, addr, 3);
17374 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
17375 		addr[3] = (char)0xff;
17376 		addr[4] = (char)0xfe;
17377 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17378 	}
17379 }
17380 
17381 /* ARGSUSED */
17382 static void
17383 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17384 {
17385 }
17386 
17387 typedef struct ipmp_ifcookie {
17388 	uint32_t	ic_hostid;
17389 	char		ic_ifname[LIFNAMSIZ];
17390 	char		ic_zonename[ZONENAME_MAX];
17391 } ipmp_ifcookie_t;
17392 
17393 /*
17394  * Construct a pseudo-random interface ID for the IPMP interface that's both
17395  * predictable and (almost) guaranteed to be unique.
17396  */
17397 static void
17398 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17399 {
17400 	zone_t		*zp;
17401 	uint8_t		*addr;
17402 	uchar_t		hash[16];
17403 	ulong_t		hostid;
17404 	MD5_CTX		ctx;
17405 	ipmp_ifcookie_t	ic = { 0 };
17406 
17407 	ASSERT(IS_IPMP(ill));
17408 
17409 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17410 	ic.ic_hostid = htonl((uint32_t)hostid);
17411 
17412 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17413 
17414 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17415 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17416 		zone_rele(zp);
17417 	}
17418 
17419 	MD5Init(&ctx);
17420 	MD5Update(&ctx, &ic, sizeof (ic));
17421 	MD5Final(hash, &ctx);
17422 
17423 	/*
17424 	 * Map the hash to an interface ID per the basic approach in RFC3041.
17425 	 */
17426 	addr = &v6addr->s6_addr8[8];
17427 	bcopy(hash + 8, addr, sizeof (uint64_t));
17428 	addr[0] &= ~0x2;				/* set local bit */
17429 }
17430 
17431 /*
17432  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17433  */
17434 static void
17435 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17436 {
17437 	phyint_t *phyi = ill->ill_phyint;
17438 
17439 	/*
17440 	 * Check PHYI_MULTI_BCAST and length of physical
17441 	 * address to determine if we use the mapping or the
17442 	 * broadcast address.
17443 	 */
17444 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17445 	    ill->ill_phys_addr_length != ETHERADDRL) {
17446 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17447 		return;
17448 	}
17449 	m_physaddr[0] = 0x33;
17450 	m_physaddr[1] = 0x33;
17451 	m_physaddr[2] = m_ip6addr[12];
17452 	m_physaddr[3] = m_ip6addr[13];
17453 	m_physaddr[4] = m_ip6addr[14];
17454 	m_physaddr[5] = m_ip6addr[15];
17455 }
17456 
17457 /*
17458  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17459  */
17460 static void
17461 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17462 {
17463 	phyint_t *phyi = ill->ill_phyint;
17464 
17465 	/*
17466 	 * Check PHYI_MULTI_BCAST and length of physical
17467 	 * address to determine if we use the mapping or the
17468 	 * broadcast address.
17469 	 */
17470 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17471 	    ill->ill_phys_addr_length != ETHERADDRL) {
17472 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17473 		return;
17474 	}
17475 	m_physaddr[0] = 0x01;
17476 	m_physaddr[1] = 0x00;
17477 	m_physaddr[2] = 0x5e;
17478 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
17479 	m_physaddr[4] = m_ipaddr[2];
17480 	m_physaddr[5] = m_ipaddr[3];
17481 }
17482 
17483 /* ARGSUSED */
17484 static void
17485 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17486 {
17487 	/*
17488 	 * for the MULTI_BCAST case and other cases when we want to
17489 	 * use the link-layer broadcast address for multicast.
17490 	 */
17491 	uint8_t	*bphys_addr;
17492 	dl_unitdata_req_t *dlur;
17493 
17494 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17495 	if (ill->ill_sap_length < 0) {
17496 		bphys_addr = (uchar_t *)dlur +
17497 		    dlur->dl_dest_addr_offset;
17498 	} else  {
17499 		bphys_addr = (uchar_t *)dlur +
17500 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
17501 	}
17502 
17503 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17504 }
17505 
17506 /*
17507  * Derive IPoIB interface id from the link layer address.
17508  */
17509 static void
17510 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17511 {
17512 	char		*addr;
17513 
17514 	ASSERT(ill->ill_phys_addr_length == 20);
17515 	addr = (char *)&v6addr->s6_addr32[2];
17516 	bcopy(ill->ill_phys_addr + 12, addr, 8);
17517 	/*
17518 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17519 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17520 	 * rules. In these cases, the IBA considers these GUIDs to be in
17521 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17522 	 * required; vendors are required not to assign global EUI-64's
17523 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
17524 	 * of the interface identifier. Whether the GUID is in modified
17525 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17526 	 * bit set to 1.
17527 	 */
17528 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
17529 }
17530 
17531 /*
17532  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17533  * Note on mapping from multicast IP addresses to IPoIB multicast link
17534  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17535  * The format of an IPoIB multicast address is:
17536  *
17537  *  4 byte QPN      Scope Sign.  Pkey
17538  * +--------------------------------------------+
17539  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17540  * +--------------------------------------------+
17541  *
17542  * The Scope and Pkey components are properties of the IBA port and
17543  * network interface. They can be ascertained from the broadcast address.
17544  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17545  */
17546 static void
17547 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17548 {
17549 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17550 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17551 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17552 	uint8_t	*bphys_addr;
17553 	dl_unitdata_req_t *dlur;
17554 
17555 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17556 
17557 	/*
17558 	 * RFC 4391: IPv4 MGID is 28-bit long.
17559 	 */
17560 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17561 	m_physaddr[17] = m_ipaddr[1];
17562 	m_physaddr[18] = m_ipaddr[2];
17563 	m_physaddr[19] = m_ipaddr[3];
17564 
17565 
17566 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17567 	if (ill->ill_sap_length < 0) {
17568 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17569 	} else  {
17570 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17571 		    ill->ill_sap_length;
17572 	}
17573 	/*
17574 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17575 	 */
17576 	m_physaddr[5] = bphys_addr[5];
17577 	m_physaddr[8] = bphys_addr[8];
17578 	m_physaddr[9] = bphys_addr[9];
17579 }
17580 
17581 static void
17582 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17583 {
17584 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17585 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17586 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17587 	uint8_t	*bphys_addr;
17588 	dl_unitdata_req_t *dlur;
17589 
17590 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17591 
17592 	/*
17593 	 * RFC 4391: IPv4 MGID is 80-bit long.
17594 	 */
17595 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17596 
17597 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17598 	if (ill->ill_sap_length < 0) {
17599 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17600 	} else  {
17601 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17602 		    ill->ill_sap_length;
17603 	}
17604 	/*
17605 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17606 	 */
17607 	m_physaddr[5] = bphys_addr[5];
17608 	m_physaddr[8] = bphys_addr[8];
17609 	m_physaddr[9] = bphys_addr[9];
17610 }
17611 
17612 /*
17613  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17614  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17615  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17616  * of RFC4213.
17617  */
17618 static void
17619 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17620 {
17621 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17622 	v6addr->s6_addr32[2] = 0;
17623 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17624 }
17625 
17626 /*
17627  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17628  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17629  * id.
17630  */
17631 static void
17632 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17633 {
17634 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17635 
17636 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17637 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17638 }
17639 
17640 static void
17641 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17642 {
17643 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17644 }
17645 
17646 static void
17647 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17648 {
17649 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17650 }
17651 
17652 static void
17653 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17654 {
17655 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17656 }
17657 
17658 static void
17659 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17660 {
17661 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17662 }
17663 
17664 /*
17665  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17666  * Returns an held ill, or NULL.
17667  */
17668 ill_t *
17669 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17670     ip_stack_t *ipst)
17671 {
17672 	ill_t	*ill;
17673 	ipif_t	*ipif;
17674 
17675 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17676 	if (ill == NULL)
17677 		return (NULL);
17678 
17679 	mutex_enter(&ill->ill_lock);
17680 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17681 		if (IPIF_IS_CONDEMNED(ipif))
17682 			continue;
17683 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17684 		    ipif->ipif_zoneid != ALL_ZONES)
17685 			continue;
17686 
17687 		mutex_exit(&ill->ill_lock);
17688 		return (ill);
17689 	}
17690 	mutex_exit(&ill->ill_lock);
17691 	ill_refrele(ill);
17692 	return (NULL);
17693 }
17694 
17695 /*
17696  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17697  * If a pointer to an ipif_t is returned then the caller will need to do
17698  * an ill_refrele().
17699  */
17700 ipif_t *
17701 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17702     ip_stack_t *ipst)
17703 {
17704 	ipif_t *ipif;
17705 	ill_t *ill;
17706 
17707 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17708 	if (ill == NULL)
17709 		return (NULL);
17710 
17711 	mutex_enter(&ill->ill_lock);
17712 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17713 		mutex_exit(&ill->ill_lock);
17714 		ill_refrele(ill);
17715 		return (NULL);
17716 	}
17717 
17718 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17719 		if (!IPIF_CAN_LOOKUP(ipif))
17720 			continue;
17721 		if (lifidx == ipif->ipif_id) {
17722 			ipif_refhold_locked(ipif);
17723 			break;
17724 		}
17725 	}
17726 
17727 	mutex_exit(&ill->ill_lock);
17728 	ill_refrele(ill);
17729 	return (ipif);
17730 }
17731 
17732 /*
17733  * Set ill_inputfn based on the current know state.
17734  * This needs to be called when any of the factors taken into
17735  * account changes.
17736  */
17737 void
17738 ill_set_inputfn(ill_t *ill)
17739 {
17740 	ip_stack_t	*ipst = ill->ill_ipst;
17741 
17742 	if (ill->ill_isv6) {
17743 		if (is_system_labeled())
17744 			ill->ill_inputfn = ill_input_full_v6;
17745 		else
17746 			ill->ill_inputfn = ill_input_short_v6;
17747 	} else {
17748 		if (is_system_labeled())
17749 			ill->ill_inputfn = ill_input_full_v4;
17750 		else if (ill->ill_dhcpinit != 0)
17751 			ill->ill_inputfn = ill_input_full_v4;
17752 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17753 		    != NULL)
17754 			ill->ill_inputfn = ill_input_full_v4;
17755 		else if (ipst->ips_ip_cgtp_filter &&
17756 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17757 			ill->ill_inputfn = ill_input_full_v4;
17758 		else
17759 			ill->ill_inputfn = ill_input_short_v4;
17760 	}
17761 }
17762 
17763 /*
17764  * Re-evaluate ill_inputfn for all the IPv4 ills.
17765  * Used when RSVP and CGTP comes and goes.
17766  */
17767 void
17768 ill_set_inputfn_all(ip_stack_t *ipst)
17769 {
17770 	ill_walk_context_t	ctx;
17771 	ill_t			*ill;
17772 
17773 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17774 	ill = ILL_START_WALK_V4(&ctx, ipst);
17775 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17776 		ill_set_inputfn(ill);
17777 
17778 	rw_exit(&ipst->ips_ill_g_lock);
17779 }
17780 
17781 /*
17782  * Set the physical address information for `ill' to the contents of the
17783  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17784  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17785  * EINPROGRESS will be returned.
17786  */
17787 int
17788 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17789 {
17790 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17791 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17792 
17793 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17794 
17795 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17796 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17797 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17798 		/* Changing DL_IPV6_TOKEN is not yet supported */
17799 		return (0);
17800 	}
17801 
17802 	/*
17803 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17804 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17805 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17806 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17807 	 */
17808 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17809 		freemsg(mp);
17810 		return (ENOMEM);
17811 	}
17812 
17813 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17814 
17815 	/*
17816 	 * Since we'll only do a logical down, we can't rely on ipif_down
17817 	 * to turn on ILL_DOWN_IN_PROGRESS, or for the DL_BIND_ACK to reset
17818 	 * ILL_DOWN_IN_PROGRESS. We instead manage this separately for this
17819 	 * case, to quiesce ire's and nce's for ill_is_quiescent.
17820 	 */
17821 	mutex_enter(&ill->ill_lock);
17822 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17823 	/* no more ire/nce addition allowed */
17824 	mutex_exit(&ill->ill_lock);
17825 
17826 	/*
17827 	 * If we can quiesce the ill, then set the address.  If not, then
17828 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17829 	 */
17830 	ill_down_ipifs(ill, B_TRUE);
17831 	mutex_enter(&ill->ill_lock);
17832 	if (!ill_is_quiescent(ill)) {
17833 		/* call cannot fail since `conn_t *' argument is NULL */
17834 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17835 		    mp, ILL_DOWN);
17836 		mutex_exit(&ill->ill_lock);
17837 		return (EINPROGRESS);
17838 	}
17839 	mutex_exit(&ill->ill_lock);
17840 
17841 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17842 	return (0);
17843 }
17844 
17845 /*
17846  * When the allowed-ips link property is set on the datalink, IP receives a
17847  * DL_NOTE_ALLOWED_IPS notification that is processed in ill_set_allowed_ips()
17848  * to initialize the ill_allowed_ips[] array in the ill_t. This array is then
17849  * used to vet addresses passed to ip_sioctl_addr() and to ensure that the
17850  * only IP addresses configured on the ill_t are those in the ill_allowed_ips[]
17851  * array.
17852  */
17853 void
17854 ill_set_allowed_ips(ill_t *ill, mblk_t *mp)
17855 {
17856 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17857 	dl_notify_ind_t	*dlip = (dl_notify_ind_t *)mp->b_rptr;
17858 	mac_protect_t *mrp;
17859 	int i;
17860 
17861 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17862 	mrp = (mac_protect_t *)&dlip[1];
17863 
17864 	if (mrp->mp_ipaddrcnt == 0) { /* reset allowed-ips */
17865 		kmem_free(ill->ill_allowed_ips,
17866 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17867 		ill->ill_allowed_ips_cnt = 0;
17868 		ill->ill_allowed_ips = NULL;
17869 		mutex_enter(&ill->ill_phyint->phyint_lock);
17870 		ill->ill_phyint->phyint_flags &= ~PHYI_L3PROTECT;
17871 		mutex_exit(&ill->ill_phyint->phyint_lock);
17872 		return;
17873 	}
17874 
17875 	if (ill->ill_allowed_ips != NULL) {
17876 		kmem_free(ill->ill_allowed_ips,
17877 		    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17878 	}
17879 	ill->ill_allowed_ips_cnt = mrp->mp_ipaddrcnt;
17880 	ill->ill_allowed_ips = kmem_alloc(
17881 	    ill->ill_allowed_ips_cnt * sizeof (in6_addr_t), KM_SLEEP);
17882 	for (i = 0; i < mrp->mp_ipaddrcnt;  i++)
17883 		ill->ill_allowed_ips[i] = mrp->mp_ipaddrs[i].ip_addr;
17884 
17885 	mutex_enter(&ill->ill_phyint->phyint_lock);
17886 	ill->ill_phyint->phyint_flags |= PHYI_L3PROTECT;
17887 	mutex_exit(&ill->ill_phyint->phyint_lock);
17888 }
17889 
17890 /*
17891  * Once the ill associated with `q' has quiesced, set its physical address
17892  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17893  * are passed (linked by b_cont), since we sometimes need to save two distinct
17894  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17895  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17896  * is quiesced, we know any stale nce's with the old address information have
17897  * already been removed, so we don't need to call nce_flush().
17898  */
17899 /* ARGSUSED */
17900 static void
17901 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17902 {
17903 	ill_t		*ill = q->q_ptr;
17904 	mblk_t		*addrmp2 = unlinkb(addrmp);
17905 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17906 	uint_t		addrlen, addroff;
17907 	int		status;
17908 
17909 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17910 
17911 	addroff	= dlindp->dl_addr_offset;
17912 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17913 
17914 	switch (dlindp->dl_data) {
17915 	case DL_IPV6_LINK_LAYER_ADDR:
17916 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17917 		freemsg(addrmp2);
17918 		break;
17919 
17920 	case DL_CURR_DEST_ADDR:
17921 		freemsg(ill->ill_dest_addr_mp);
17922 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17923 		ill->ill_dest_addr_mp = addrmp;
17924 		if (ill->ill_isv6) {
17925 			ill_setdesttoken(ill);
17926 			ipif_setdestlinklocal(ill->ill_ipif);
17927 		}
17928 		freemsg(addrmp2);
17929 		break;
17930 
17931 	case DL_CURR_PHYS_ADDR:
17932 		freemsg(ill->ill_phys_addr_mp);
17933 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17934 		ill->ill_phys_addr_mp = addrmp;
17935 		ill->ill_phys_addr_length = addrlen;
17936 		if (ill->ill_isv6)
17937 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17938 		else
17939 			freemsg(addrmp2);
17940 		if (ill->ill_isv6) {
17941 			ill_setdefaulttoken(ill);
17942 			ipif_setlinklocal(ill->ill_ipif);
17943 		}
17944 		break;
17945 	default:
17946 		ASSERT(0);
17947 	}
17948 
17949 	/*
17950 	 * reset ILL_DOWN_IN_PROGRESS so that we can successfully add ires
17951 	 * as we bring the ipifs up again.
17952 	 */
17953 	mutex_enter(&ill->ill_lock);
17954 	ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17955 	mutex_exit(&ill->ill_lock);
17956 	/*
17957 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17958 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17959 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17960 	 * brought up.
17961 	 */
17962 	status = ill_up_ipifs(ill, q, addrmp);
17963 	if (status != EINPROGRESS)
17964 		ipsq_current_finish(ipsq);
17965 }
17966 
17967 /*
17968  * Helper routine for setting the ill_nd_lla fields.
17969  */
17970 void
17971 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17972 {
17973 	freemsg(ill->ill_nd_lla_mp);
17974 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17975 	ill->ill_nd_lla_mp = ndmp;
17976 	ill->ill_nd_lla_len = addrlen;
17977 }
17978 
17979 /*
17980  * Replumb the ill.
17981  */
17982 int
17983 ill_replumb(ill_t *ill, mblk_t *mp)
17984 {
17985 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17986 
17987 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17988 
17989 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17990 
17991 	/*
17992 	 * If we can quiesce the ill, then continue.  If not, then
17993 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17994 	 */
17995 	ill_down_ipifs(ill, B_FALSE);
17996 
17997 	mutex_enter(&ill->ill_lock);
17998 	if (!ill_is_quiescent(ill)) {
17999 		/* call cannot fail since `conn_t *' argument is NULL */
18000 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
18001 		    mp, ILL_DOWN);
18002 		mutex_exit(&ill->ill_lock);
18003 		return (EINPROGRESS);
18004 	}
18005 	mutex_exit(&ill->ill_lock);
18006 
18007 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
18008 	return (0);
18009 }
18010 
18011 /* ARGSUSED */
18012 static void
18013 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
18014 {
18015 	ill_t *ill = q->q_ptr;
18016 	int err;
18017 	conn_t *connp = NULL;
18018 
18019 	ASSERT(IAM_WRITER_IPSQ(ipsq));
18020 	freemsg(ill->ill_replumb_mp);
18021 	ill->ill_replumb_mp = copyb(mp);
18022 
18023 	if (ill->ill_replumb_mp == NULL) {
18024 		/* out of memory */
18025 		ipsq_current_finish(ipsq);
18026 		return;
18027 	}
18028 
18029 	mutex_enter(&ill->ill_lock);
18030 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
18031 	    ill->ill_rq, ill->ill_replumb_mp, 0);
18032 	mutex_exit(&ill->ill_lock);
18033 
18034 	if (!ill->ill_up_ipifs) {
18035 		/* already closing */
18036 		ipsq_current_finish(ipsq);
18037 		return;
18038 	}
18039 	ill->ill_replumbing = 1;
18040 	err = ill_down_ipifs_tail(ill);
18041 
18042 	/*
18043 	 * Successfully quiesced and brought down the interface, now we send
18044 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
18045 	 * DL_NOTE_REPLUMB message.
18046 	 */
18047 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
18048 	    DL_NOTIFY_CONF);
18049 	ASSERT(mp != NULL);
18050 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
18051 	    DL_NOTE_REPLUMB_DONE;
18052 	ill_dlpi_send(ill, mp);
18053 
18054 	/*
18055 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
18056 	 * streams have to be unbound. When all the DLPI exchanges are done,
18057 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
18058 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
18059 	 * arp_bringup_done().
18060 	 */
18061 	ASSERT(ill->ill_replumb_mp != NULL);
18062 	if (err == EINPROGRESS)
18063 		return;
18064 	else
18065 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
18066 	ASSERT(connp == NULL);
18067 	if (err == 0 && ill->ill_replumb_mp != NULL &&
18068 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
18069 		return;
18070 	}
18071 	ipsq_current_finish(ipsq);
18072 }
18073 
18074 /*
18075  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
18076  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
18077  * as per the ioctl.  On failure, an errno is returned.
18078  */
18079 static int
18080 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
18081 {
18082 	int rval;
18083 	struct strioctl iocb;
18084 
18085 	iocb.ic_cmd = cmd;
18086 	iocb.ic_timout = 15;
18087 	iocb.ic_len = bufsize;
18088 	iocb.ic_dp = buf;
18089 
18090 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
18091 }
18092 
18093 /*
18094  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
18095  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
18096  */
18097 static int
18098 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
18099     uint_t *bufsizep, cred_t *cr)
18100 {
18101 	int err;
18102 	struct lifnum lifn;
18103 
18104 	bzero(&lifn, sizeof (lifn));
18105 	lifn.lifn_family = af;
18106 	lifn.lifn_flags = LIFC_UNDER_IPMP;
18107 
18108 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
18109 		return (err);
18110 
18111 	/*
18112 	 * Pad the interface count to account for additional interfaces that
18113 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
18114 	 */
18115 	lifn.lifn_count += 4;
18116 	bzero(lifcp, sizeof (*lifcp));
18117 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
18118 	lifcp->lifc_family = af;
18119 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
18120 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
18121 
18122 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
18123 	if (err != 0) {
18124 		kmem_free(lifcp->lifc_buf, *bufsizep);
18125 		return (err);
18126 	}
18127 
18128 	return (0);
18129 }
18130 
18131 /*
18132  * Helper for ip_interface_cleanup() that removes the loopback interface.
18133  */
18134 static void
18135 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18136 {
18137 	int err;
18138 	struct lifreq lifr;
18139 
18140 	bzero(&lifr, sizeof (lifr));
18141 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
18142 
18143 	/*
18144 	 * Attempt to remove the interface.  It may legitimately not exist
18145 	 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
18146 	 */
18147 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
18148 	if (err != 0 && err != ENXIO) {
18149 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
18150 		    "error %d\n", isv6 ? "v6" : "v4", err));
18151 	}
18152 }
18153 
18154 /*
18155  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
18156  * groups and that IPMP data addresses are down.  These conditions must be met
18157  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
18158  */
18159 static void
18160 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18161 {
18162 	int af = isv6 ? AF_INET6 : AF_INET;
18163 	int i, nifs;
18164 	int err;
18165 	uint_t bufsize;
18166 	uint_t lifrsize = sizeof (struct lifreq);
18167 	struct lifconf lifc;
18168 	struct lifreq *lifrp;
18169 
18170 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
18171 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
18172 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
18173 		return;
18174 	}
18175 
18176 	nifs = lifc.lifc_len / lifrsize;
18177 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
18178 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18179 		if (err != 0) {
18180 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
18181 			    "flags: error %d", lifrp->lifr_name, err);
18182 			continue;
18183 		}
18184 
18185 		if (lifrp->lifr_flags & IFF_IPMP) {
18186 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
18187 				continue;
18188 
18189 			lifrp->lifr_flags &= ~IFF_UP;
18190 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
18191 			if (err != 0) {
18192 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18193 				    "bring down (error %d); IPMP interface may "
18194 				    "not be shutdown", lifrp->lifr_name, err);
18195 			}
18196 
18197 			/*
18198 			 * Check if IFF_DUPLICATE is still set -- and if so,
18199 			 * reset the address to clear it.
18200 			 */
18201 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18202 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
18203 				continue;
18204 
18205 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
18206 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
18207 			    lifrp, lifrsize, cr)) != 0) {
18208 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18209 				    "reset DAD (error %d); IPMP interface may "
18210 				    "not be shutdown", lifrp->lifr_name, err);
18211 			}
18212 			continue;
18213 		}
18214 
18215 		if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
18216 			lifrp->lifr_groupname[0] = '\0';
18217 			if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
18218 			    lifrsize, cr)) != 0) {
18219 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18220 				    "leave IPMP group (error %d); associated "
18221 				    "IPMP interface may not be shutdown",
18222 				    lifrp->lifr_name, err);
18223 				continue;
18224 			}
18225 		}
18226 	}
18227 
18228 	kmem_free(lifc.lifc_buf, bufsize);
18229 }
18230 
18231 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
18232 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
18233 
18234 /*
18235  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
18236  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
18237  * when the user-level processes in the zone are killed and the latter are
18238  * cleaned up by str_stack_shutdown().
18239  */
18240 void
18241 ip_interface_cleanup(ip_stack_t *ipst)
18242 {
18243 	ldi_handle_t	lh;
18244 	ldi_ident_t	li;
18245 	cred_t		*cr;
18246 	int		err;
18247 	int		i;
18248 	char		*devs[] = { UDP6DEV, UDPDEV };
18249 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
18250 
18251 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18252 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18253 		    " error %d", err);
18254 		return;
18255 	}
18256 
18257 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18258 	ASSERT(cr != NULL);
18259 
18260 	/*
18261 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
18262 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
18263 	 * the loop.)
18264 	 */
18265 	for (i = 0; i < 2; i++) {
18266 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18267 		if (err != 0) {
18268 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18269 			    " error %d", devs[i], err);
18270 			continue;
18271 		}
18272 
18273 		ip_loopback_removeif(lh, i == 0, cr);
18274 		ip_ipmp_cleanup(lh, i == 0, cr);
18275 
18276 		(void) ldi_close(lh, FREAD|FWRITE, cr);
18277 	}
18278 
18279 	ldi_ident_release(li);
18280 	crfree(cr);
18281 }
18282 
18283 /*
18284  * This needs to be in-sync with nic_event_t definition
18285  */
18286 static const char *
18287 ill_hook_event2str(nic_event_t event)
18288 {
18289 	switch (event) {
18290 	case NE_PLUMB:
18291 		return ("PLUMB");
18292 	case NE_UNPLUMB:
18293 		return ("UNPLUMB");
18294 	case NE_UP:
18295 		return ("UP");
18296 	case NE_DOWN:
18297 		return ("DOWN");
18298 	case NE_ADDRESS_CHANGE:
18299 		return ("ADDRESS_CHANGE");
18300 	case NE_LIF_UP:
18301 		return ("LIF_UP");
18302 	case NE_LIF_DOWN:
18303 		return ("LIF_DOWN");
18304 	case NE_IFINDEX_CHANGE:
18305 		return ("IFINDEX_CHANGE");
18306 	default:
18307 		return ("UNKNOWN");
18308 	}
18309 }
18310 
18311 void
18312 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18313     nic_event_data_t data, size_t datalen)
18314 {
18315 	ip_stack_t		*ipst = ill->ill_ipst;
18316 	hook_nic_event_int_t	*info;
18317 	const char		*str = NULL;
18318 
18319 	/* create a new nic event info */
18320 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18321 		goto fail;
18322 
18323 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18324 	info->hnei_event.hne_lif = lif;
18325 	info->hnei_event.hne_event = event;
18326 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
18327 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18328 	info->hnei_event.hne_data = NULL;
18329 	info->hnei_event.hne_datalen = 0;
18330 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18331 
18332 	if (data != NULL && datalen != 0) {
18333 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18334 		if (info->hnei_event.hne_data == NULL)
18335 			goto fail;
18336 		bcopy(data, info->hnei_event.hne_data, datalen);
18337 		info->hnei_event.hne_datalen = datalen;
18338 	}
18339 
18340 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18341 	    DDI_NOSLEEP) == DDI_SUCCESS)
18342 		return;
18343 
18344 fail:
18345 	if (info != NULL) {
18346 		if (info->hnei_event.hne_data != NULL) {
18347 			kmem_free(info->hnei_event.hne_data,
18348 			    info->hnei_event.hne_datalen);
18349 		}
18350 		kmem_free(info, sizeof (hook_nic_event_t));
18351 	}
18352 	str = ill_hook_event2str(event);
18353 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18354 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
18355 }
18356 
18357 static int
18358 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18359 {
18360 	int		err = 0;
18361 	const in_addr_t	*addr = NULL;
18362 	nce_t		*nce = NULL;
18363 	ill_t		*ill = ipif->ipif_ill;
18364 	ill_t		*bound_ill;
18365 	boolean_t	added_ipif = B_FALSE;
18366 	uint16_t	state;
18367 	uint16_t	flags;
18368 
18369 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18370 	    ill_t *, ill, ipif_t *, ipif);
18371 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
18372 		addr = &ipif->ipif_lcl_addr;
18373 	}
18374 
18375 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18376 		if (res_act != Res_act_initial)
18377 			return (EINVAL);
18378 	}
18379 
18380 	if (addr != NULL) {
18381 		ipmp_illgrp_t	*illg = ill->ill_grp;
18382 
18383 		/* add unicast nce for the local addr */
18384 
18385 		if (IS_IPMP(ill)) {
18386 			/*
18387 			 * If we're here via ipif_up(), then the ipif
18388 			 * won't be bound yet -- add it to the group,
18389 			 * which will bind it if possible. (We would
18390 			 * add it in ipif_up(), but deleting on failure
18391 			 * there is gruesome.)  If we're here via
18392 			 * ipmp_ill_bind_ipif(), then the ipif has
18393 			 * already been added to the group and we
18394 			 * just need to use the binding.
18395 			 */
18396 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18397 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
18398 				if (bound_ill == NULL) {
18399 					/*
18400 					 * We couldn't bind the ipif to an ill
18401 					 * yet, so we have nothing to publish.
18402 					 * Mark the address as ready and return.
18403 					 */
18404 					ipif->ipif_addr_ready = 1;
18405 					return (0);
18406 				}
18407 				added_ipif = B_TRUE;
18408 			}
18409 		} else {
18410 			bound_ill = ill;
18411 		}
18412 
18413 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18414 		    NCE_F_NONUD);
18415 		/*
18416 		 * If this is an initial bring-up (or the ipif was never
18417 		 * completely brought up), do DAD.  Otherwise, we're here
18418 		 * because IPMP has rebound an address to this ill: send
18419 		 * unsolicited advertisements (ARP announcements) to
18420 		 * inform others.
18421 		 */
18422 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18423 			state = ND_UNCHANGED; /* compute in nce_add_common() */
18424 		} else {
18425 			state = ND_REACHABLE;
18426 			flags |= NCE_F_UNSOL_ADV;
18427 		}
18428 
18429 retry:
18430 		err = nce_lookup_then_add_v4(ill,
18431 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18432 		    addr, flags, state, &nce);
18433 
18434 		/*
18435 		 * note that we may encounter EEXIST if we are moving
18436 		 * the nce as a result of a rebind operation.
18437 		 */
18438 		switch (err) {
18439 		case 0:
18440 			ipif->ipif_added_nce = 1;
18441 			nce->nce_ipif_cnt++;
18442 			break;
18443 		case EEXIST:
18444 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18445 			    ill->ill_name));
18446 			if (!NCE_MYADDR(nce->nce_common)) {
18447 				/*
18448 				 * A leftover nce from before this address
18449 				 * existed
18450 				 */
18451 				ncec_delete(nce->nce_common);
18452 				nce_refrele(nce);
18453 				nce = NULL;
18454 				goto retry;
18455 			}
18456 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18457 				nce_refrele(nce);
18458 				nce = NULL;
18459 				ip1dbg(("ipif_arp_up: NCE already exists "
18460 				    "for %s:%u\n", ill->ill_name,
18461 				    ipif->ipif_id));
18462 				goto arp_up_done;
18463 			}
18464 			/*
18465 			 * Duplicate local addresses are permissible for
18466 			 * IPIF_POINTOPOINT interfaces which will get marked
18467 			 * IPIF_UNNUMBERED later in
18468 			 * ip_addr_availability_check().
18469 			 *
18470 			 * The nce_ipif_cnt field tracks the number of
18471 			 * ipifs that have nce_addr as their local address.
18472 			 */
18473 			ipif->ipif_addr_ready = 1;
18474 			ipif->ipif_added_nce = 1;
18475 			nce->nce_ipif_cnt++;
18476 			err = 0;
18477 			break;
18478 		default:
18479 			ASSERT(nce == NULL);
18480 			goto arp_up_done;
18481 		}
18482 		if (arp_no_defense) {
18483 			if ((ipif->ipif_flags & IPIF_UP) &&
18484 			    !ipif->ipif_addr_ready)
18485 				ipif_up_notify(ipif);
18486 			ipif->ipif_addr_ready = 1;
18487 		}
18488 	} else {
18489 		/* zero address. nothing to publish */
18490 		ipif->ipif_addr_ready = 1;
18491 	}
18492 	if (nce != NULL)
18493 		nce_refrele(nce);
18494 arp_up_done:
18495 	if (added_ipif && err != 0)
18496 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18497 	return (err);
18498 }
18499 
18500 int
18501 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18502 {
18503 	int		err = 0;
18504 	ill_t		*ill = ipif->ipif_ill;
18505 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
18506 
18507 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18508 	    ill_t *, ill, ipif_t *, ipif);
18509 
18510 	/*
18511 	 * need to bring up ARP or setup mcast mapping only
18512 	 * when the first interface is coming UP.
18513 	 */
18514 	first_interface = (ill->ill_ipif_up_count == 0 &&
18515 	    ill->ill_ipif_dup_count == 0 && !was_dup);
18516 
18517 	if (res_act == Res_act_initial && first_interface) {
18518 		/*
18519 		 * Send ATTACH + BIND
18520 		 */
18521 		err = arp_ll_up(ill);
18522 		if (err != EINPROGRESS && err != 0)
18523 			return (err);
18524 
18525 		/*
18526 		 * Add NCE for local address. Start DAD.
18527 		 * we'll wait to hear that DAD has finished
18528 		 * before using the interface.
18529 		 */
18530 		if (err == EINPROGRESS)
18531 			wait_for_dlpi = B_TRUE;
18532 	}
18533 
18534 	if (!wait_for_dlpi)
18535 		(void) ipif_arp_up_done_tail(ipif, res_act);
18536 
18537 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
18538 }
18539 
18540 /*
18541  * Finish processing of "arp_up" after all the DLPI message
18542  * exchanges have completed between arp and the driver.
18543  */
18544 void
18545 arp_bringup_done(ill_t *ill, int err)
18546 {
18547 	mblk_t	*mp1;
18548 	ipif_t  *ipif;
18549 	conn_t *connp = NULL;
18550 	ipsq_t	*ipsq;
18551 	queue_t *q;
18552 
18553 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18554 
18555 	ASSERT(IAM_WRITER_ILL(ill));
18556 
18557 	ipsq = ill->ill_phyint->phyint_ipsq;
18558 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18559 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18560 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18561 	if (mp1 == NULL) /* bringup was aborted by the user */
18562 		return;
18563 
18564 	/*
18565 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18566 	 * must have an associated conn_t.  Otherwise, we're bringing this
18567 	 * interface back up as part of handling an asynchronous event (e.g.,
18568 	 * physical address change).
18569 	 */
18570 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18571 		ASSERT(connp != NULL);
18572 		q = CONNP_TO_WQ(connp);
18573 	} else {
18574 		ASSERT(connp == NULL);
18575 		q = ill->ill_rq;
18576 	}
18577 	if (err == 0) {
18578 		if (ipif->ipif_isv6) {
18579 			if ((err = ipif_up_done_v6(ipif)) != 0)
18580 				ip0dbg(("arp_bringup_done: init failed\n"));
18581 		} else {
18582 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18583 			if (err != 0 ||
18584 			    (err = ipif_up_done(ipif)) != 0) {
18585 				ip0dbg(("arp_bringup_done: "
18586 				    "init failed err %x\n", err));
18587 				(void) ipif_arp_down(ipif);
18588 			}
18589 
18590 		}
18591 	} else {
18592 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18593 	}
18594 
18595 	if ((err == 0) && (ill->ill_up_ipifs)) {
18596 		err = ill_up_ipifs(ill, q, mp1);
18597 		if (err == EINPROGRESS)
18598 			return;
18599 	}
18600 
18601 	/*
18602 	 * If we have a moved ipif to bring up, and everything has succeeded
18603 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
18604 	 * down -- the admin can try to bring it up by hand if need be.
18605 	 */
18606 	if (ill->ill_move_ipif != NULL) {
18607 		ipif = ill->ill_move_ipif;
18608 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18609 		    ipif->ipif_ill->ill_name));
18610 		ill->ill_move_ipif = NULL;
18611 		if (err == 0) {
18612 			err = ipif_up(ipif, q, mp1);
18613 			if (err == EINPROGRESS)
18614 				return;
18615 		}
18616 	}
18617 
18618 	/*
18619 	 * The operation must complete without EINPROGRESS since
18620 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18621 	 * Otherwise, the operation will be stuck forever in the ipsq.
18622 	 */
18623 	ASSERT(err != EINPROGRESS);
18624 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18625 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18626 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18627 		    ill_t *, ill, ipif_t *, ipif);
18628 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18629 	} else {
18630 		ipsq_current_finish(ipsq);
18631 	}
18632 }
18633 
18634 /*
18635  * Finish processing of arp replumb after all the DLPI message
18636  * exchanges have completed between arp and the driver.
18637  */
18638 void
18639 arp_replumb_done(ill_t *ill, int err)
18640 {
18641 	mblk_t	*mp1;
18642 	ipif_t  *ipif;
18643 	conn_t *connp = NULL;
18644 	ipsq_t	*ipsq;
18645 	queue_t *q;
18646 
18647 	ASSERT(IAM_WRITER_ILL(ill));
18648 
18649 	ipsq = ill->ill_phyint->phyint_ipsq;
18650 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18651 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18652 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18653 	if (mp1 == NULL) {
18654 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18655 		    ipsq->ipsq_xop->ipx_current_ioctl));
18656 		/* bringup was aborted by the user */
18657 		return;
18658 	}
18659 	/*
18660 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18661 	 * must have an associated conn_t.  Otherwise, we're bringing this
18662 	 * interface back up as part of handling an asynchronous event (e.g.,
18663 	 * physical address change).
18664 	 */
18665 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18666 		ASSERT(connp != NULL);
18667 		q = CONNP_TO_WQ(connp);
18668 	} else {
18669 		ASSERT(connp == NULL);
18670 		q = ill->ill_rq;
18671 	}
18672 	if ((err == 0) && (ill->ill_up_ipifs)) {
18673 		err = ill_up_ipifs(ill, q, mp1);
18674 		if (err == EINPROGRESS)
18675 			return;
18676 	}
18677 	/*
18678 	 * The operation must complete without EINPROGRESS since
18679 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18680 	 * Otherwise, the operation will be stuck forever in the ipsq.
18681 	 */
18682 	ASSERT(err != EINPROGRESS);
18683 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18684 		DTRACE_PROBE4(ipif__ioctl, char *,
18685 		    "arp_replumb_done finish",
18686 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18687 		    ill_t *, ill, ipif_t *, ipif);
18688 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18689 	} else {
18690 		ipsq_current_finish(ipsq);
18691 	}
18692 }
18693 
18694 void
18695 ipif_up_notify(ipif_t *ipif)
18696 {
18697 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18698 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18699 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18700 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18701 	    NE_LIF_UP, NULL, 0);
18702 }
18703 
18704 /*
18705  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18706  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18707  * TPI end points with STREAMS modules pushed above.  This is assured by not
18708  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18709  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18710  * while unwinding from the ispq and that could be a thread from the bottom.
18711  */
18712 /* ARGSUSED */
18713 int
18714 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18715     ip_ioctl_cmd_t *ipip, void *arg)
18716 {
18717 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18718 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18719 	int ret = 0;
18720 	int i;
18721 	size_t size;
18722 	ip_stack_t *ipst;
18723 	zoneid_t zoneid;
18724 	ilb_stack_t *ilbs;
18725 
18726 	ipst = CONNQ_TO_IPST(q);
18727 	ilbs = ipst->ips_netstack->netstack_ilb;
18728 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18729 
18730 	switch (command) {
18731 	case ILB_CREATE_RULE: {
18732 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18733 
18734 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18735 			ret = EINVAL;
18736 			break;
18737 		}
18738 
18739 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18740 		break;
18741 	}
18742 	case ILB_DESTROY_RULE:
18743 	case ILB_ENABLE_RULE:
18744 	case ILB_DISABLE_RULE: {
18745 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18746 
18747 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18748 			ret = EINVAL;
18749 			break;
18750 		}
18751 
18752 		if (cmd->flags & ILB_RULE_ALLRULES) {
18753 			if (command == ILB_DESTROY_RULE) {
18754 				ilb_rule_del_all(ilbs, zoneid);
18755 				break;
18756 			} else if (command == ILB_ENABLE_RULE) {
18757 				ilb_rule_enable_all(ilbs, zoneid);
18758 				break;
18759 			} else if (command == ILB_DISABLE_RULE) {
18760 				ilb_rule_disable_all(ilbs, zoneid);
18761 				break;
18762 			}
18763 		} else {
18764 			if (command == ILB_DESTROY_RULE) {
18765 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18766 			} else if (command == ILB_ENABLE_RULE) {
18767 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18768 				    NULL);
18769 			} else if (command == ILB_DISABLE_RULE) {
18770 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18771 				    NULL);
18772 			}
18773 		}
18774 		break;
18775 	}
18776 	case ILB_NUM_RULES: {
18777 		ilb_num_rules_cmd_t *cmd;
18778 
18779 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18780 			ret = EINVAL;
18781 			break;
18782 		}
18783 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18784 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18785 		break;
18786 	}
18787 	case ILB_RULE_NAMES: {
18788 		ilb_rule_names_cmd_t *cmd;
18789 
18790 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18791 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18792 		    cmd->num_names == 0) {
18793 			ret = EINVAL;
18794 			break;
18795 		}
18796 		size = cmd->num_names * ILB_RULE_NAMESZ;
18797 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18798 		    size != cmd_mp->b_wptr) {
18799 			ret = EINVAL;
18800 			break;
18801 		}
18802 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18803 		break;
18804 	}
18805 	case ILB_NUM_SERVERS: {
18806 		ilb_num_servers_cmd_t *cmd;
18807 
18808 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18809 			ret = EINVAL;
18810 			break;
18811 		}
18812 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18813 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18814 		    &(cmd->num));
18815 		break;
18816 	}
18817 	case ILB_LIST_RULE: {
18818 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18819 
18820 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18821 			ret = EINVAL;
18822 			break;
18823 		}
18824 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18825 		break;
18826 	}
18827 	case ILB_LIST_SERVERS: {
18828 		ilb_servers_info_cmd_t *cmd;
18829 
18830 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18831 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18832 		    cmd->num_servers == 0) {
18833 			ret = EINVAL;
18834 			break;
18835 		}
18836 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18837 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18838 		    size != cmd_mp->b_wptr) {
18839 			ret = EINVAL;
18840 			break;
18841 		}
18842 
18843 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18844 		    &cmd->num_servers);
18845 		break;
18846 	}
18847 	case ILB_ADD_SERVERS: {
18848 		ilb_servers_info_cmd_t *cmd;
18849 		ilb_rule_t *rule;
18850 
18851 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18852 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18853 			ret = EINVAL;
18854 			break;
18855 		}
18856 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18857 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18858 		    size != cmd_mp->b_wptr) {
18859 			ret = EINVAL;
18860 			break;
18861 		}
18862 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18863 		if (rule == NULL) {
18864 			ASSERT(ret != 0);
18865 			break;
18866 		}
18867 		for (i = 0; i < cmd->num_servers; i++) {
18868 			ilb_server_info_t *s;
18869 
18870 			s = &cmd->servers[i];
18871 			s->err = ilb_server_add(ilbs, rule, s);
18872 		}
18873 		ILB_RULE_REFRELE(rule);
18874 		break;
18875 	}
18876 	case ILB_DEL_SERVERS:
18877 	case ILB_ENABLE_SERVERS:
18878 	case ILB_DISABLE_SERVERS: {
18879 		ilb_servers_cmd_t *cmd;
18880 		ilb_rule_t *rule;
18881 		int (*f)();
18882 
18883 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18884 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18885 			ret = EINVAL;
18886 			break;
18887 		}
18888 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18889 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18890 		    size != cmd_mp->b_wptr) {
18891 			ret = EINVAL;
18892 			break;
18893 		}
18894 
18895 		if (command == ILB_DEL_SERVERS)
18896 			f = ilb_server_del;
18897 		else if (command == ILB_ENABLE_SERVERS)
18898 			f = ilb_server_enable;
18899 		else if (command == ILB_DISABLE_SERVERS)
18900 			f = ilb_server_disable;
18901 
18902 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18903 		if (rule == NULL) {
18904 			ASSERT(ret != 0);
18905 			break;
18906 		}
18907 
18908 		for (i = 0; i < cmd->num_servers; i++) {
18909 			ilb_server_arg_t *s;
18910 
18911 			s = &cmd->servers[i];
18912 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18913 		}
18914 		ILB_RULE_REFRELE(rule);
18915 		break;
18916 	}
18917 	case ILB_LIST_NAT_TABLE: {
18918 		ilb_list_nat_cmd_t *cmd;
18919 
18920 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18921 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18922 			ret = EINVAL;
18923 			break;
18924 		}
18925 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18926 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18927 		    size != cmd_mp->b_wptr) {
18928 			ret = EINVAL;
18929 			break;
18930 		}
18931 
18932 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18933 		    &cmd->flags);
18934 		break;
18935 	}
18936 	case ILB_LIST_STICKY_TABLE: {
18937 		ilb_list_sticky_cmd_t *cmd;
18938 
18939 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18940 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18941 			ret = EINVAL;
18942 			break;
18943 		}
18944 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18945 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18946 		    size != cmd_mp->b_wptr) {
18947 			ret = EINVAL;
18948 			break;
18949 		}
18950 
18951 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18952 		    &cmd->num_sticky, &cmd->flags);
18953 		break;
18954 	}
18955 	default:
18956 		ret = EINVAL;
18957 		break;
18958 	}
18959 done:
18960 	return (ret);
18961 }
18962 
18963 /* Remove all cache entries for this logical interface */
18964 void
18965 ipif_nce_down(ipif_t *ipif)
18966 {
18967 	ill_t *ill = ipif->ipif_ill;
18968 	nce_t *nce;
18969 
18970 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18971 	    ill_t *, ill, ipif_t *, ipif);
18972 	if (ipif->ipif_added_nce) {
18973 		if (ipif->ipif_isv6)
18974 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18975 		else
18976 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18977 		if (nce != NULL) {
18978 			if (--nce->nce_ipif_cnt == 0)
18979 				ncec_delete(nce->nce_common);
18980 			ipif->ipif_added_nce = 0;
18981 			nce_refrele(nce);
18982 		} else {
18983 			/*
18984 			 * nce may already be NULL because it was already
18985 			 * flushed, e.g., due to a call to nce_flush
18986 			 */
18987 			ipif->ipif_added_nce = 0;
18988 		}
18989 	}
18990 	/*
18991 	 * Make IPMP aware of the deleted data address.
18992 	 */
18993 	if (IS_IPMP(ill))
18994 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18995 
18996 	/*
18997 	 * Remove all other nces dependent on this ill when the last ipif
18998 	 * is going away.
18999 	 */
19000 	if (ill->ill_ipif_up_count == 0) {
19001 		ncec_walk(ill, ncec_delete_per_ill, ill, ill->ill_ipst);
19002 		if (IS_UNDER_IPMP(ill))
19003 			nce_flush(ill, B_TRUE);
19004 	}
19005 }
19006 
19007 /*
19008  * find the first interface that uses usill for its source address.
19009  */
19010 ill_t *
19011 ill_lookup_usesrc(ill_t *usill)
19012 {
19013 	ip_stack_t *ipst = usill->ill_ipst;
19014 	ill_t *ill;
19015 
19016 	ASSERT(usill != NULL);
19017 
19018 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
19019 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
19020 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
19021 	for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
19022 	    ill = ill->ill_usesrc_grp_next) {
19023 		if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
19024 		    !ILL_IS_CONDEMNED(ill)) {
19025 			ill_refhold(ill);
19026 			break;
19027 		}
19028 	}
19029 	rw_exit(&ipst->ips_ill_g_lock);
19030 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
19031 	return (ill);
19032 }
19033 
19034 /*
19035  * This comment applies to both ip_sioctl_get_ifhwaddr and
19036  * ip_sioctl_get_lifhwaddr as the basic function of these two functions
19037  * is the same.
19038  *
19039  * The goal here is to find an IP interface that corresponds to the name
19040  * provided by the caller in the ifreq/lifreq structure held in the mblk_t
19041  * chain and to fill out a sockaddr/sockaddr_storage structure with the
19042  * mac address.
19043  *
19044  * The SIOCGIFHWADDR/SIOCGLIFHWADDR ioctl may return an error for a number
19045  * of different reasons:
19046  * ENXIO - the device name is not known to IP.
19047  * EADDRNOTAVAIL - the device has no hardware address. This is indicated
19048  * by ill_phys_addr not pointing to an actual address.
19049  * EPFNOSUPPORT - this will indicate that a request is being made for a
19050  * mac address that will not fit in the data structure supplier (struct
19051  * sockaddr).
19052  *
19053  */
19054 /* ARGSUSED */
19055 int
19056 ip_sioctl_get_ifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19057     ip_ioctl_cmd_t *ipip, void *if_req)
19058 {
19059 	struct sockaddr *sock;
19060 	struct ifreq *ifr;
19061 	mblk_t *mp1;
19062 	ill_t *ill;
19063 
19064 	ASSERT(ipif != NULL);
19065 	ill = ipif->ipif_ill;
19066 
19067 	if (ill->ill_phys_addr == NULL) {
19068 		return (EADDRNOTAVAIL);
19069 	}
19070 	if (ill->ill_phys_addr_length > sizeof (sock->sa_data)) {
19071 		return (EPFNOSUPPORT);
19072 	}
19073 
19074 	ip1dbg(("ip_sioctl_get_hwaddr(%s)\n", ill->ill_name));
19075 
19076 	/* Existence of mp1 has been checked in ip_wput_nondata */
19077 	mp1 = mp->b_cont->b_cont;
19078 	ifr = (struct ifreq *)mp1->b_rptr;
19079 
19080 	sock = &ifr->ifr_addr;
19081 	/*
19082 	 * The "family" field in the returned structure is set to a value
19083 	 * that represents the type of device to which the address belongs.
19084 	 * The value returned may differ to that on Linux but it will still
19085 	 * represent the correct symbol on Solaris.
19086 	 */
19087 	sock->sa_family = arp_hw_type(ill->ill_mactype);
19088 	bcopy(ill->ill_phys_addr, &sock->sa_data, ill->ill_phys_addr_length);
19089 
19090 	return (0);
19091 }
19092 
19093 /*
19094  * The expection of applications using SIOCGIFHWADDR is that data will
19095  * be returned in the sa_data field of the sockaddr structure. With
19096  * SIOCGLIFHWADDR, we're breaking new ground as there is no Linux
19097  * equivalent. In light of this, struct sockaddr_dl is used as it
19098  * offers more space for address storage in sll_data.
19099  */
19100 /* ARGSUSED */
19101 int
19102 ip_sioctl_get_lifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19103     ip_ioctl_cmd_t *ipip, void *if_req)
19104 {
19105 	struct sockaddr_dl *sock;
19106 	struct lifreq *lifr;
19107 	mblk_t *mp1;
19108 	ill_t *ill;
19109 
19110 	ASSERT(ipif != NULL);
19111 	ill = ipif->ipif_ill;
19112 
19113 	if (ill->ill_phys_addr == NULL) {
19114 		return (EADDRNOTAVAIL);
19115 	}
19116 	if (ill->ill_phys_addr_length > sizeof (sock->sdl_data)) {
19117 		return (EPFNOSUPPORT);
19118 	}
19119 
19120 	ip1dbg(("ip_sioctl_get_lifhwaddr(%s)\n", ill->ill_name));
19121 
19122 	/* Existence of mp1 has been checked in ip_wput_nondata */
19123 	mp1 = mp->b_cont->b_cont;
19124 	lifr = (struct lifreq *)mp1->b_rptr;
19125 
19126 	/*
19127 	 * sockaddr_ll is used here because it is also the structure used in
19128 	 * responding to the same ioctl in sockpfp. The only other choice is
19129 	 * sockaddr_dl which contains fields that are not required here
19130 	 * because its purpose is different.
19131 	 */
19132 	lifr->lifr_type = ill->ill_type;
19133 	sock = (struct sockaddr_dl *)&lifr->lifr_addr;
19134 	sock->sdl_family = AF_LINK;
19135 	sock->sdl_index = ill->ill_phyint->phyint_ifindex;
19136 	sock->sdl_type = ill->ill_mactype;
19137 	sock->sdl_nlen = 0;
19138 	sock->sdl_slen = 0;
19139 	sock->sdl_alen = ill->ill_phys_addr_length;
19140 	bcopy(ill->ill_phys_addr, sock->sdl_data, ill->ill_phys_addr_length);
19141 
19142 	return (0);
19143 }
19144