xref: /titanic_44/usr/src/uts/common/inet/ip/ip_if.c (revision de6732348404877079e00a1631babd13d8a5ad14)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 /*
28  * This file contains the interface control functions for IP.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/dlpi.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/sysmacros.h>
37 #include <sys/strsubr.h>
38 #include <sys/strlog.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/cmn_err.h>
42 #include <sys/kstat.h>
43 #include <sys/debug.h>
44 #include <sys/zone.h>
45 #include <sys/sunldi.h>
46 #include <sys/file.h>
47 #include <sys/bitmap.h>
48 #include <sys/cpuvar.h>
49 #include <sys/time.h>
50 #include <sys/ctype.h>
51 #include <sys/kmem.h>
52 #include <sys/systm.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/isa_defs.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_types.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <netinet/ip6.h>
64 #include <netinet/icmp6.h>
65 #include <netinet/igmp_var.h>
66 #include <sys/policy.h>
67 #include <sys/ethernet.h>
68 #include <sys/callb.h>
69 #include <sys/md5.h>
70 
71 #include <inet/common.h>   /* for various inet/mi.h and inet/nd.h needs */
72 #include <inet/mi.h>
73 #include <inet/nd.h>
74 #include <inet/arp.h>
75 #include <inet/ip_arp.h>
76 #include <inet/mib2.h>
77 #include <inet/ip.h>
78 #include <inet/ip6.h>
79 #include <inet/ip6_asp.h>
80 #include <inet/tcp.h>
81 #include <inet/ip_multi.h>
82 #include <inet/ip_ire.h>
83 #include <inet/ip_ftable.h>
84 #include <inet/ip_rts.h>
85 #include <inet/ip_ndp.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_impl.h>
88 #include <inet/sctp_ip.h>
89 #include <inet/ip_netinfo.h>
90 #include <inet/ilb_ip.h>
91 
92 #include <netinet/igmp.h>
93 #include <inet/ip_listutils.h>
94 #include <inet/ipclassifier.h>
95 #include <sys/mac_client.h>
96 #include <sys/dld.h>
97 
98 #include <sys/systeminfo.h>
99 #include <sys/bootconf.h>
100 
101 #include <sys/tsol/tndb.h>
102 #include <sys/tsol/tnet.h>
103 
104 /* The character which tells where the ill_name ends */
105 #define	IPIF_SEPARATOR_CHAR	':'
106 
107 /* IP ioctl function table entry */
108 typedef struct ipft_s {
109 	int	ipft_cmd;
110 	pfi_t	ipft_pfi;
111 	int	ipft_min_size;
112 	int	ipft_flags;
113 } ipft_t;
114 #define	IPFT_F_NO_REPLY		0x1	/* IP ioctl does not expect any reply */
115 #define	IPFT_F_SELF_REPLY	0x2	/* ioctl callee does the ioctl reply */
116 
117 static int	nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
118 static int	nd_ill_forward_set(queue_t *q, mblk_t *mp,
119 		    char *value, caddr_t cp, cred_t *ioc_cr);
120 
121 static boolean_t ill_is_quiescent(ill_t *);
122 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
123 static ip_m_t	*ip_m_lookup(t_uscalar_t mac_type);
124 static int	ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
125     mblk_t *mp, boolean_t need_up);
126 static int	ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
127     mblk_t *mp, boolean_t need_up);
128 static int	ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
129     queue_t *q, mblk_t *mp, boolean_t need_up);
130 static int	ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
131     mblk_t *mp);
132 static int	ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133     mblk_t *mp);
134 static int	ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
135     queue_t *q, mblk_t *mp, boolean_t need_up);
136 static int	ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
137     int ioccmd, struct linkblk *li);
138 static ipaddr_t	ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
139 static void	ip_wput_ioctl(queue_t *q, mblk_t *mp);
140 static void	ipsq_flush(ill_t *ill);
141 
142 static	int	ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
143     queue_t *q, mblk_t *mp, boolean_t need_up);
144 static void	ipsq_delete(ipsq_t *);
145 
146 static ipif_t	*ipif_allocate(ill_t *ill, int id, uint_t ire_type,
147     boolean_t initialize, boolean_t insert, int *errorp);
148 static ire_t	**ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
149 static void	ipif_delete_bcast_ires(ipif_t *ipif);
150 static int	ipif_add_ires_v4(ipif_t *, boolean_t);
151 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
152 		    boolean_t isv6);
153 static int	ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
154 static void	ipif_free(ipif_t *ipif);
155 static void	ipif_free_tail(ipif_t *ipif);
156 static void	ipif_set_default(ipif_t *ipif);
157 static int	ipif_set_values(queue_t *q, mblk_t *mp,
158     char *interf_name, uint_t *ppa);
159 static int	ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
160     queue_t *q);
161 static ipif_t	*ipif_lookup_on_name(char *name, size_t namelen,
162     boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
163     ip_stack_t *);
164 
165 static int	ill_alloc_ppa(ill_if_t *, ill_t *);
166 static void	ill_delete_interface_type(ill_if_t *);
167 static int	ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
168 static void	ill_dl_down(ill_t *ill);
169 static void	ill_down(ill_t *ill);
170 static void	ill_down_ipifs(ill_t *, boolean_t);
171 static void	ill_free_mib(ill_t *ill);
172 static void	ill_glist_delete(ill_t *);
173 static void	ill_phyint_reinit(ill_t *ill);
174 static void	ill_set_nce_router_flags(ill_t *, boolean_t);
175 static void	ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
176 static void	ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
177 
178 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
179 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
180 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
181 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
182 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
183 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
184 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
185 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
186 static ip_v4mapinfo_func_t ip_mbcast_mapping;
187 static void 	ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
188 static void 	ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
189 static void	phyint_free(phyint_t *);
190 
191 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
192 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
193 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
194 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
195 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
196 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
197     dl_capability_sub_t *);
198 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
199 static void	ill_capability_dld_reset_fill(ill_t *, mblk_t *);
200 static void	ill_capability_dld_ack(ill_t *, mblk_t *,
201 		    dl_capability_sub_t *);
202 static void	ill_capability_dld_enable(ill_t *);
203 static void	ill_capability_ack_thr(void *);
204 static void	ill_capability_lso_enable(ill_t *);
205 
206 static ill_t	*ill_prev_usesrc(ill_t *);
207 static int	ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
208 static void	ill_disband_usesrc_group(ill_t *);
209 static void	ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
210 
211 #ifdef DEBUG
212 static	void	ill_trace_cleanup(const ill_t *);
213 static	void	ipif_trace_cleanup(const ipif_t *);
214 #endif
215 
216 /*
217  * if we go over the memory footprint limit more than once in this msec
218  * interval, we'll start pruning aggressively.
219  */
220 int ip_min_frag_prune_time = 0;
221 
222 static ipft_t	ip_ioctl_ftbl[] = {
223 	{ IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
224 	{ IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
225 		IPFT_F_NO_REPLY },
226 	{ IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
227 	{ 0 }
228 };
229 
230 /* Simple ICMP IP Header Template */
231 static ipha_t icmp_ipha = {
232 	IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
233 };
234 
235 static uchar_t	ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
236 
237 static ip_m_t   ip_m_tbl[] = {
238 	{ DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
239 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
240 	    ip_nodef_v6intfid },
241 	{ DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
242 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
243 	    ip_nodef_v6intfid },
244 	{ DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
245 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
246 	    ip_nodef_v6intfid },
247 	{ DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
248 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
249 	    ip_nodef_v6intfid },
250 	{ DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
251 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
252 	    ip_nodef_v6intfid },
253 	{ DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
254 	    ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
255 	    ip_nodef_v6intfid },
256 	{ DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
257 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
258 	    ip_ipv4_v6destintfid },
259 	{ DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
260 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
261 	    ip_ipv6_v6destintfid },
262 	{ DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
263 	    ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
264 	    ip_nodef_v6intfid },
265 	{ SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
266 	    NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
267 	{ SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
268 	    NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
269 	{ DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
270 	    ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
271 	    ip_nodef_v6intfid }
272 };
273 
274 static ill_t	ill_null;		/* Empty ILL for init. */
275 char	ipif_loopback_name[] = "lo0";
276 static char *ipv4_forward_suffix = ":ip_forwarding";
277 static char *ipv6_forward_suffix = ":ip6_forwarding";
278 static	sin6_t	sin6_null;	/* Zero address for quick clears */
279 static	sin_t	sin_null;	/* Zero address for quick clears */
280 
281 /* When set search for unused ipif_seqid */
282 static ipif_t	ipif_zero;
283 
284 /*
285  * ppa arena is created after these many
286  * interfaces have been plumbed.
287  */
288 uint_t	ill_no_arena = 12;	/* Setable in /etc/system */
289 
290 /*
291  * Allocate per-interface mibs.
292  * Returns true if ok. False otherwise.
293  *  ipsq  may not yet be allocated (loopback case ).
294  */
295 static boolean_t
296 ill_allocate_mibs(ill_t *ill)
297 {
298 	/* Already allocated? */
299 	if (ill->ill_ip_mib != NULL) {
300 		if (ill->ill_isv6)
301 			ASSERT(ill->ill_icmp6_mib != NULL);
302 		return (B_TRUE);
303 	}
304 
305 	ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
306 	    KM_NOSLEEP);
307 	if (ill->ill_ip_mib == NULL) {
308 		return (B_FALSE);
309 	}
310 
311 	/* Setup static information */
312 	SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
313 	    sizeof (mib2_ipIfStatsEntry_t));
314 	if (ill->ill_isv6) {
315 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
316 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
317 		    sizeof (mib2_ipv6AddrEntry_t));
318 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
319 		    sizeof (mib2_ipv6RouteEntry_t));
320 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
321 		    sizeof (mib2_ipv6NetToMediaEntry_t));
322 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
323 		    sizeof (ipv6_member_t));
324 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
325 		    sizeof (ipv6_grpsrc_t));
326 	} else {
327 		ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
328 		SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
329 		    sizeof (mib2_ipAddrEntry_t));
330 		SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
331 		    sizeof (mib2_ipRouteEntry_t));
332 		SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
333 		    sizeof (mib2_ipNetToMediaEntry_t));
334 		SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
335 		    sizeof (ip_member_t));
336 		SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
337 		    sizeof (ip_grpsrc_t));
338 
339 		/*
340 		 * For a v4 ill, we are done at this point, because per ill
341 		 * icmp mibs are only used for v6.
342 		 */
343 		return (B_TRUE);
344 	}
345 
346 	ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
347 	    KM_NOSLEEP);
348 	if (ill->ill_icmp6_mib == NULL) {
349 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
350 		ill->ill_ip_mib = NULL;
351 		return (B_FALSE);
352 	}
353 	/* static icmp info */
354 	ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
355 	    sizeof (mib2_ipv6IfIcmpEntry_t);
356 	/*
357 	 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
358 	 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
359 	 * -> ill_phyint_reinit
360 	 */
361 	return (B_TRUE);
362 }
363 
364 /*
365  * Completely vaporize a lower level tap and all associated interfaces.
366  * ill_delete is called only out of ip_close when the device control
367  * stream is being closed.
368  */
369 void
370 ill_delete(ill_t *ill)
371 {
372 	ipif_t	*ipif;
373 	ill_t	*prev_ill;
374 	ip_stack_t	*ipst = ill->ill_ipst;
375 
376 	/*
377 	 * ill_delete may be forcibly entering the ipsq. The previous
378 	 * ioctl may not have completed and may need to be aborted.
379 	 * ipsq_flush takes care of it. If we don't need to enter the
380 	 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
381 	 * ill_delete_tail is sufficient.
382 	 */
383 	ipsq_flush(ill);
384 
385 	/*
386 	 * Nuke all interfaces.  ipif_free will take down the interface,
387 	 * remove it from the list, and free the data structure.
388 	 * Walk down the ipif list and remove the logical interfaces
389 	 * first before removing the main ipif. We can't unplumb
390 	 * zeroth interface first in the case of IPv6 as update_conn_ill
391 	 * -> ip_ll_multireq de-references ill_ipif for checking
392 	 * POINTOPOINT.
393 	 *
394 	 * If ill_ipif was not properly initialized (i.e low on memory),
395 	 * then no interfaces to clean up. In this case just clean up the
396 	 * ill.
397 	 */
398 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
399 		ipif_free(ipif);
400 
401 	/*
402 	 * clean out all the nce_t entries that depend on this
403 	 * ill for the ill_phys_addr.
404 	 */
405 	nce_flush(ill, B_TRUE);
406 
407 	/* Clean up msgs on pending upcalls for mrouted */
408 	reset_mrt_ill(ill);
409 
410 	update_conn_ill(ill, ipst);
411 
412 	/*
413 	 * Remove multicast references added as a result of calls to
414 	 * ip_join_allmulti().
415 	 */
416 	ip_purge_allmulti(ill);
417 
418 	/*
419 	 * If the ill being deleted is under IPMP, boot it out of the illgrp.
420 	 */
421 	if (IS_UNDER_IPMP(ill))
422 		ipmp_ill_leave_illgrp(ill);
423 
424 	/*
425 	 * ill_down will arrange to blow off any IRE's dependent on this
426 	 * ILL, and shut down fragmentation reassembly.
427 	 */
428 	ill_down(ill);
429 
430 	/* Let SCTP know, so that it can remove this from its list. */
431 	sctp_update_ill(ill, SCTP_ILL_REMOVE);
432 
433 	/*
434 	 * Walk all CONNs that can have a reference on an ire or nce for this
435 	 * ill (we actually walk all that now have stale references).
436 	 */
437 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
438 
439 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
440 	if (ill->ill_isv6)
441 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
442 
443 	/*
444 	 * If an address on this ILL is being used as a source address then
445 	 * clear out the pointers in other ILLs that point to this ILL.
446 	 */
447 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
448 	if (ill->ill_usesrc_grp_next != NULL) {
449 		if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
450 			ill_disband_usesrc_group(ill);
451 		} else {	/* consumer of the usesrc ILL */
452 			prev_ill = ill_prev_usesrc(ill);
453 			prev_ill->ill_usesrc_grp_next =
454 			    ill->ill_usesrc_grp_next;
455 		}
456 	}
457 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
458 }
459 
460 static void
461 ipif_non_duplicate(ipif_t *ipif)
462 {
463 	ill_t *ill = ipif->ipif_ill;
464 	mutex_enter(&ill->ill_lock);
465 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
466 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
467 		ASSERT(ill->ill_ipif_dup_count > 0);
468 		ill->ill_ipif_dup_count--;
469 	}
470 	mutex_exit(&ill->ill_lock);
471 }
472 
473 /*
474  * ill_delete_tail is called from ip_modclose after all references
475  * to the closing ill are gone. The wait is done in ip_modclose
476  */
477 void
478 ill_delete_tail(ill_t *ill)
479 {
480 	mblk_t	**mpp;
481 	ipif_t	*ipif;
482 	ip_stack_t	*ipst = ill->ill_ipst;
483 
484 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
485 		ipif_non_duplicate(ipif);
486 		(void) ipif_down_tail(ipif);
487 	}
488 
489 	ASSERT(ill->ill_ipif_dup_count == 0);
490 
491 	/*
492 	 * If polling capability is enabled (which signifies direct
493 	 * upcall into IP and driver has ill saved as a handle),
494 	 * we need to make sure that unbind has completed before we
495 	 * let the ill disappear and driver no longer has any reference
496 	 * to this ill.
497 	 */
498 	mutex_enter(&ill->ill_lock);
499 	while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
500 		cv_wait(&ill->ill_cv, &ill->ill_lock);
501 	mutex_exit(&ill->ill_lock);
502 	ASSERT(!(ill->ill_capabilities &
503 	    (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
504 
505 	if (ill->ill_net_type != IRE_LOOPBACK)
506 		qprocsoff(ill->ill_rq);
507 
508 	/*
509 	 * We do an ipsq_flush once again now. New messages could have
510 	 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
511 	 * could also have landed up if an ioctl thread had looked up
512 	 * the ill before we set the ILL_CONDEMNED flag, but not yet
513 	 * enqueued the ioctl when we did the ipsq_flush last time.
514 	 */
515 	ipsq_flush(ill);
516 
517 	/*
518 	 * Free capabilities.
519 	 */
520 	if (ill->ill_hcksum_capab != NULL) {
521 		kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
522 		ill->ill_hcksum_capab = NULL;
523 	}
524 
525 	if (ill->ill_zerocopy_capab != NULL) {
526 		kmem_free(ill->ill_zerocopy_capab,
527 		    sizeof (ill_zerocopy_capab_t));
528 		ill->ill_zerocopy_capab = NULL;
529 	}
530 
531 	if (ill->ill_lso_capab != NULL) {
532 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
533 		ill->ill_lso_capab = NULL;
534 	}
535 
536 	if (ill->ill_dld_capab != NULL) {
537 		kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
538 		ill->ill_dld_capab = NULL;
539 	}
540 
541 	while (ill->ill_ipif != NULL)
542 		ipif_free_tail(ill->ill_ipif);
543 
544 	/*
545 	 * We have removed all references to ilm from conn and the ones joined
546 	 * within the kernel.
547 	 *
548 	 * We don't walk conns, mrts and ires because
549 	 *
550 	 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
551 	 * 2) ill_down ->ill_downi walks all the ires and cleans up
552 	 *    ill references.
553 	 */
554 
555 	/*
556 	 * If this ill is an IPMP meta-interface, blow away the illgrp.  This
557 	 * is safe to do because the illgrp has already been unlinked from the
558 	 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
559 	 */
560 	if (IS_IPMP(ill)) {
561 		ipmp_illgrp_destroy(ill->ill_grp);
562 		ill->ill_grp = NULL;
563 	}
564 
565 	/*
566 	 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
567 	 * could free the phyint. No more reference to the phyint after this
568 	 * point.
569 	 */
570 	(void) ill_glist_delete(ill);
571 
572 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
573 	if (ill->ill_ndd_name != NULL)
574 		nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name);
575 	rw_exit(&ipst->ips_ip_g_nd_lock);
576 
577 	if (ill->ill_frag_ptr != NULL) {
578 		uint_t count;
579 
580 		for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
581 			mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
582 		}
583 		mi_free(ill->ill_frag_ptr);
584 		ill->ill_frag_ptr = NULL;
585 		ill->ill_frag_hash_tbl = NULL;
586 	}
587 
588 	freemsg(ill->ill_nd_lla_mp);
589 	/* Free all retained control messages. */
590 	mpp = &ill->ill_first_mp_to_free;
591 	do {
592 		while (mpp[0]) {
593 			mblk_t  *mp;
594 			mblk_t  *mp1;
595 
596 			mp = mpp[0];
597 			mpp[0] = mp->b_next;
598 			for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
599 				mp1->b_next = NULL;
600 				mp1->b_prev = NULL;
601 			}
602 			freemsg(mp);
603 		}
604 	} while (mpp++ != &ill->ill_last_mp_to_free);
605 
606 	ill_free_mib(ill);
607 
608 #ifdef DEBUG
609 	ill_trace_cleanup(ill);
610 #endif
611 
612 	/* The default multicast interface might have changed */
613 	ire_increment_multicast_generation(ipst, ill->ill_isv6);
614 
615 	/* Drop refcnt here */
616 	netstack_rele(ill->ill_ipst->ips_netstack);
617 	ill->ill_ipst = NULL;
618 }
619 
620 static void
621 ill_free_mib(ill_t *ill)
622 {
623 	ip_stack_t *ipst = ill->ill_ipst;
624 
625 	/*
626 	 * MIB statistics must not be lost, so when an interface
627 	 * goes away the counter values will be added to the global
628 	 * MIBs.
629 	 */
630 	if (ill->ill_ip_mib != NULL) {
631 		if (ill->ill_isv6) {
632 			ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
633 			    ill->ill_ip_mib);
634 		} else {
635 			ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
636 			    ill->ill_ip_mib);
637 		}
638 
639 		kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
640 		ill->ill_ip_mib = NULL;
641 	}
642 	if (ill->ill_icmp6_mib != NULL) {
643 		ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
644 		    ill->ill_icmp6_mib);
645 		kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
646 		ill->ill_icmp6_mib = NULL;
647 	}
648 }
649 
650 /*
651  * Concatenate together a physical address and a sap.
652  *
653  * Sap_lengths are interpreted as follows:
654  *   sap_length == 0	==>	no sap
655  *   sap_length > 0	==>	sap is at the head of the dlpi address
656  *   sap_length < 0	==>	sap is at the tail of the dlpi address
657  */
658 static void
659 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
660     t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
661 {
662 	uint16_t sap_addr = (uint16_t)sap_src;
663 
664 	if (sap_length == 0) {
665 		if (phys_src == NULL)
666 			bzero(dst, phys_length);
667 		else
668 			bcopy(phys_src, dst, phys_length);
669 	} else if (sap_length < 0) {
670 		if (phys_src == NULL)
671 			bzero(dst, phys_length);
672 		else
673 			bcopy(phys_src, dst, phys_length);
674 		bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
675 	} else {
676 		bcopy(&sap_addr, dst, sizeof (sap_addr));
677 		if (phys_src == NULL)
678 			bzero((char *)dst + sap_length, phys_length);
679 		else
680 			bcopy(phys_src, (char *)dst + sap_length, phys_length);
681 	}
682 }
683 
684 /*
685  * Generate a dl_unitdata_req mblk for the device and address given.
686  * addr_length is the length of the physical portion of the address.
687  * If addr is NULL include an all zero address of the specified length.
688  * TRUE? In any case, addr_length is taken to be the entire length of the
689  * dlpi address, including the absolute value of sap_length.
690  */
691 mblk_t *
692 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
693 		t_scalar_t sap_length)
694 {
695 	dl_unitdata_req_t *dlur;
696 	mblk_t	*mp;
697 	t_scalar_t	abs_sap_length;		/* absolute value */
698 
699 	abs_sap_length = ABS(sap_length);
700 	mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
701 	    DL_UNITDATA_REQ);
702 	if (mp == NULL)
703 		return (NULL);
704 	dlur = (dl_unitdata_req_t *)mp->b_rptr;
705 	/* HACK: accomodate incompatible DLPI drivers */
706 	if (addr_length == 8)
707 		addr_length = 6;
708 	dlur->dl_dest_addr_length = addr_length + abs_sap_length;
709 	dlur->dl_dest_addr_offset = sizeof (*dlur);
710 	dlur->dl_priority.dl_min = 0;
711 	dlur->dl_priority.dl_max = 0;
712 	ill_dlur_copy_address(addr, addr_length, sap, sap_length,
713 	    (uchar_t *)&dlur[1]);
714 	return (mp);
715 }
716 
717 /*
718  * Add the pending mp to the list. There can be only 1 pending mp
719  * in the list. Any exclusive ioctl that needs to wait for a response
720  * from another module or driver needs to use this function to set
721  * the ipx_pending_mp to the ioctl mblk and wait for the response from
722  * the other module/driver. This is also used while waiting for the
723  * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
724  */
725 boolean_t
726 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
727     int waitfor)
728 {
729 	ipxop_t	*ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
730 
731 	ASSERT(IAM_WRITER_IPIF(ipif));
732 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
733 	ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
734 	ASSERT(ipx->ipx_pending_mp == NULL);
735 	/*
736 	 * The caller may be using a different ipif than the one passed into
737 	 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
738 	 * ill needs to wait for the V6 ill to quiesce).  So we can't ASSERT
739 	 * that `ipx_current_ipif == ipif'.
740 	 */
741 	ASSERT(ipx->ipx_current_ipif != NULL);
742 
743 	/*
744 	 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
745 	 * driver.
746 	 */
747 	ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
748 	    (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
749 	    (DB_TYPE(add_mp) == M_PCPROTO));
750 
751 	if (connp != NULL) {
752 		ASSERT(MUTEX_HELD(&connp->conn_lock));
753 		/*
754 		 * Return error if the conn has started closing. The conn
755 		 * could have finished cleaning up the pending mp list,
756 		 * If so we should not add another mp to the list negating
757 		 * the cleanup.
758 		 */
759 		if (connp->conn_state_flags & CONN_CLOSING)
760 			return (B_FALSE);
761 	}
762 	mutex_enter(&ipx->ipx_lock);
763 	ipx->ipx_pending_ipif = ipif;
764 	/*
765 	 * Note down the queue in b_queue. This will be returned by
766 	 * ipsq_pending_mp_get. Caller will then use these values to restart
767 	 * the processing
768 	 */
769 	add_mp->b_next = NULL;
770 	add_mp->b_queue = q;
771 	ipx->ipx_pending_mp = add_mp;
772 	ipx->ipx_waitfor = waitfor;
773 	mutex_exit(&ipx->ipx_lock);
774 
775 	if (connp != NULL)
776 		connp->conn_oper_pending_ill = ipif->ipif_ill;
777 
778 	return (B_TRUE);
779 }
780 
781 /*
782  * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
783  * queued in the list.
784  */
785 mblk_t *
786 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
787 {
788 	mblk_t	*curr = NULL;
789 	ipxop_t	*ipx = ipsq->ipsq_xop;
790 
791 	*connpp = NULL;
792 	mutex_enter(&ipx->ipx_lock);
793 	if (ipx->ipx_pending_mp == NULL) {
794 		mutex_exit(&ipx->ipx_lock);
795 		return (NULL);
796 	}
797 
798 	/* There can be only 1 such excl message */
799 	curr = ipx->ipx_pending_mp;
800 	ASSERT(curr->b_next == NULL);
801 	ipx->ipx_pending_ipif = NULL;
802 	ipx->ipx_pending_mp = NULL;
803 	ipx->ipx_waitfor = 0;
804 	mutex_exit(&ipx->ipx_lock);
805 
806 	if (CONN_Q(curr->b_queue)) {
807 		/*
808 		 * This mp did a refhold on the conn, at the start of the ioctl.
809 		 * So we can safely return a pointer to the conn to the caller.
810 		 */
811 		*connpp = Q_TO_CONN(curr->b_queue);
812 	} else {
813 		*connpp = NULL;
814 	}
815 	curr->b_next = NULL;
816 	curr->b_prev = NULL;
817 	return (curr);
818 }
819 
820 /*
821  * Cleanup the ioctl mp queued in ipx_pending_mp
822  * - Called in the ill_delete path
823  * - Called in the M_ERROR or M_HANGUP path on the ill.
824  * - Called in the conn close path.
825  */
826 boolean_t
827 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
828 {
829 	mblk_t	*mp;
830 	ipxop_t	*ipx;
831 	queue_t	*q;
832 	ipif_t	*ipif;
833 	int	cmd;
834 
835 	ASSERT(IAM_WRITER_ILL(ill));
836 	ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
837 
838 	/*
839 	 * If connp is null, unconditionally clean up the ipx_pending_mp.
840 	 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl
841 	 * even if it is meant for another ill, since we have to enqueue
842 	 * a new mp now in ipx_pending_mp to complete the ipif_down.
843 	 * If connp is non-null we are called from the conn close path.
844 	 */
845 	mutex_enter(&ipx->ipx_lock);
846 	mp = ipx->ipx_pending_mp;
847 	if (mp == NULL || (connp != NULL &&
848 	    mp->b_queue != CONNP_TO_WQ(connp))) {
849 		mutex_exit(&ipx->ipx_lock);
850 		return (B_FALSE);
851 	}
852 	/* Now remove from the ipx_pending_mp */
853 	ipx->ipx_pending_mp = NULL;
854 	q = mp->b_queue;
855 	mp->b_next = NULL;
856 	mp->b_prev = NULL;
857 	mp->b_queue = NULL;
858 
859 	ipif = ipx->ipx_pending_ipif;
860 	ipx->ipx_pending_ipif = NULL;
861 	ipx->ipx_waitfor = 0;
862 	ipx->ipx_current_ipif = NULL;
863 	cmd = ipx->ipx_current_ioctl;
864 	ipx->ipx_current_ioctl = 0;
865 	ipx->ipx_current_done = B_TRUE;
866 	mutex_exit(&ipx->ipx_lock);
867 
868 	if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
869 		DTRACE_PROBE4(ipif__ioctl,
870 		    char *, "ipsq_pending_mp_cleanup",
871 		    int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
872 		    ipif_t *, ipif);
873 		if (connp == NULL) {
874 			ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
875 		} else {
876 			ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
877 			mutex_enter(&ipif->ipif_ill->ill_lock);
878 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
879 			mutex_exit(&ipif->ipif_ill->ill_lock);
880 		}
881 	} else {
882 		/*
883 		 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't
884 		 * be just inet_freemsg. we have to restart it
885 		 * otherwise the thread will be stuck.
886 		 */
887 		inet_freemsg(mp);
888 	}
889 	return (B_TRUE);
890 }
891 
892 /*
893  * Called in the conn close path and ill delete path
894  */
895 static void
896 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
897 {
898 	ipsq_t	*ipsq;
899 	mblk_t	*prev;
900 	mblk_t	*curr;
901 	mblk_t	*next;
902 	queue_t	*q;
903 	mblk_t	*tmp_list = NULL;
904 
905 	ASSERT(IAM_WRITER_ILL(ill));
906 	if (connp != NULL)
907 		q = CONNP_TO_WQ(connp);
908 	else
909 		q = ill->ill_wq;
910 
911 	ipsq = ill->ill_phyint->phyint_ipsq;
912 	/*
913 	 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
914 	 * In the case of ioctl from a conn, there can be only 1 mp
915 	 * queued on the ipsq. If an ill is being unplumbed, only messages
916 	 * related to this ill are flushed, like M_ERROR or M_HANGUP message.
917 	 * ioctls meant for this ill form conn's are not flushed. They will
918 	 * be processed during ipsq_exit and will not find the ill and will
919 	 * return error.
920 	 */
921 	mutex_enter(&ipsq->ipsq_lock);
922 	for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
923 	    curr = next) {
924 		next = curr->b_next;
925 		if (curr->b_queue == q || curr->b_queue == RD(q)) {
926 			/* Unlink the mblk from the pending mp list */
927 			if (prev != NULL) {
928 				prev->b_next = curr->b_next;
929 			} else {
930 				ASSERT(ipsq->ipsq_xopq_mphead == curr);
931 				ipsq->ipsq_xopq_mphead = curr->b_next;
932 			}
933 			if (ipsq->ipsq_xopq_mptail == curr)
934 				ipsq->ipsq_xopq_mptail = prev;
935 			/*
936 			 * Create a temporary list and release the ipsq lock
937 			 * New elements are added to the head of the tmp_list
938 			 */
939 			curr->b_next = tmp_list;
940 			tmp_list = curr;
941 		} else {
942 			prev = curr;
943 		}
944 	}
945 	mutex_exit(&ipsq->ipsq_lock);
946 
947 	while (tmp_list != NULL) {
948 		curr = tmp_list;
949 		tmp_list = curr->b_next;
950 		curr->b_next = NULL;
951 		curr->b_prev = NULL;
952 		curr->b_queue = NULL;
953 		if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
954 			DTRACE_PROBE4(ipif__ioctl,
955 			    char *, "ipsq_xopq_mp_cleanup",
956 			    int, 0, ill_t *, NULL, ipif_t *, NULL);
957 			ip_ioctl_finish(q, curr, ENXIO, connp != NULL ?
958 			    CONN_CLOSE : NO_COPYOUT, NULL);
959 		} else {
960 			/*
961 			 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
962 			 * this can't be just inet_freemsg. we have to
963 			 * restart it otherwise the thread will be stuck.
964 			 */
965 			inet_freemsg(curr);
966 		}
967 	}
968 }
969 
970 /*
971  * This conn has started closing. Cleanup any pending ioctl from this conn.
972  * STREAMS ensures that there can be at most 1 ioctl pending on a stream.
973  */
974 void
975 conn_ioctl_cleanup(conn_t *connp)
976 {
977 	ipsq_t	*ipsq;
978 	ill_t	*ill;
979 	boolean_t refheld;
980 
981 	/*
982 	 * Is any exclusive ioctl pending ? If so clean it up. If the
983 	 * ioctl has not yet started, the mp is pending in the list headed by
984 	 * ipsq_xopq_head. If the ioctl has started the mp could be present in
985 	 * ipx_pending_mp. If the ioctl timed out in the streamhead but
986 	 * is currently executing now the mp is not queued anywhere but
987 	 * conn_oper_pending_ill is null. The conn close will wait
988 	 * till the conn_ref drops to zero.
989 	 */
990 	mutex_enter(&connp->conn_lock);
991 	ill = connp->conn_oper_pending_ill;
992 	if (ill == NULL) {
993 		mutex_exit(&connp->conn_lock);
994 		return;
995 	}
996 
997 	/*
998 	 * We may not be able to refhold the ill if the ill/ipif
999 	 * is changing. But we need to make sure that the ill will
1000 	 * not vanish. So we just bump up the ill_waiter count.
1001 	 */
1002 	refheld = ill_waiter_inc(ill);
1003 	mutex_exit(&connp->conn_lock);
1004 	if (refheld) {
1005 		if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1006 			ill_waiter_dcr(ill);
1007 			/*
1008 			 * Check whether this ioctl has started and is
1009 			 * pending. If it is not found there then check
1010 			 * whether this ioctl has not even started and is in
1011 			 * the ipsq_xopq list.
1012 			 */
1013 			if (!ipsq_pending_mp_cleanup(ill, connp))
1014 				ipsq_xopq_mp_cleanup(ill, connp);
1015 			ipsq = ill->ill_phyint->phyint_ipsq;
1016 			ipsq_exit(ipsq);
1017 			return;
1018 		}
1019 	}
1020 
1021 	/*
1022 	 * The ill is also closing and we could not bump up the
1023 	 * ill_waiter_count or we could not enter the ipsq. Leave
1024 	 * the cleanup to ill_delete
1025 	 */
1026 	mutex_enter(&connp->conn_lock);
1027 	while (connp->conn_oper_pending_ill != NULL)
1028 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
1029 	mutex_exit(&connp->conn_lock);
1030 	if (refheld)
1031 		ill_waiter_dcr(ill);
1032 }
1033 
1034 /*
1035  * ipcl_walk function for cleaning up conn_*_ill fields.
1036  * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1037  * conn_bound_if in place. We prefer dropping
1038  * packets instead of sending them out the wrong interface, or accepting
1039  * packets from the wrong ifindex.
1040  */
1041 static void
1042 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1043 {
1044 	ill_t	*ill = (ill_t *)arg;
1045 
1046 	mutex_enter(&connp->conn_lock);
1047 	if (connp->conn_dhcpinit_ill == ill) {
1048 		connp->conn_dhcpinit_ill = NULL;
1049 		ASSERT(ill->ill_dhcpinit != 0);
1050 		atomic_dec_32(&ill->ill_dhcpinit);
1051 		ill_set_inputfn(ill);
1052 	}
1053 	mutex_exit(&connp->conn_lock);
1054 }
1055 
1056 static int
1057 ill_down_ipifs_tail(ill_t *ill)
1058 {
1059 	ipif_t	*ipif;
1060 	int err;
1061 
1062 	ASSERT(IAM_WRITER_ILL(ill));
1063 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1064 		ipif_non_duplicate(ipif);
1065 		/*
1066 		 * ipif_down_tail will call arp_ll_down on the last ipif
1067 		 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1068 		 */
1069 		if ((err = ipif_down_tail(ipif)) != 0)
1070 			return (err);
1071 	}
1072 	return (0);
1073 }
1074 
1075 /* ARGSUSED */
1076 void
1077 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1078 {
1079 	ASSERT(IAM_WRITER_IPSQ(ipsq));
1080 	(void) ill_down_ipifs_tail(q->q_ptr);
1081 	freemsg(mp);
1082 	ipsq_current_finish(ipsq);
1083 }
1084 
1085 /*
1086  * ill_down_start is called when we want to down this ill and bring it up again
1087  * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1088  * all interfaces, but don't tear down any plumbing.
1089  */
1090 boolean_t
1091 ill_down_start(queue_t *q, mblk_t *mp)
1092 {
1093 	ill_t	*ill = q->q_ptr;
1094 	ipif_t	*ipif;
1095 
1096 	ASSERT(IAM_WRITER_ILL(ill));
1097 	mutex_enter(&ill->ill_lock);
1098 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
1099 	/* no more nce addition allowed */
1100 	mutex_exit(&ill->ill_lock);
1101 
1102 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1103 		(void) ipif_down(ipif, NULL, NULL);
1104 
1105 	ill_down(ill);
1106 
1107 	/*
1108 	 * Walk all CONNs that can have a reference on an ire or nce for this
1109 	 * ill (we actually walk all that now have stale references).
1110 	 */
1111 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1112 
1113 	/* With IPv6 we have dce_ifindex. Cleanup for neatness */
1114 	if (ill->ill_isv6)
1115 		dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1116 
1117 
1118 	(void) ipsq_pending_mp_cleanup(ill, NULL);
1119 
1120 	ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1121 
1122 	/*
1123 	 * Atomically test and add the pending mp if references are active.
1124 	 */
1125 	mutex_enter(&ill->ill_lock);
1126 	if (!ill_is_quiescent(ill)) {
1127 		/* call cannot fail since `conn_t *' argument is NULL */
1128 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1129 		    mp, ILL_DOWN);
1130 		mutex_exit(&ill->ill_lock);
1131 		return (B_FALSE);
1132 	}
1133 	mutex_exit(&ill->ill_lock);
1134 	return (B_TRUE);
1135 }
1136 
1137 static void
1138 ill_down(ill_t *ill)
1139 {
1140 	mblk_t	*mp;
1141 	ip_stack_t	*ipst = ill->ill_ipst;
1142 
1143 	/*
1144 	 * Blow off any IREs dependent on this ILL.
1145 	 * The caller needs to handle conn_ixa_cleanup
1146 	 */
1147 	ill_delete_ires(ill);
1148 
1149 	ire_walk_ill(0, 0, ill_downi, ill, ill);
1150 
1151 	/* Remove any conn_*_ill depending on this ill */
1152 	ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1153 
1154 	/*
1155 	 * Free state for additional IREs.
1156 	 */
1157 	mutex_enter(&ill->ill_saved_ire_lock);
1158 	mp = ill->ill_saved_ire_mp;
1159 	ill->ill_saved_ire_mp = NULL;
1160 	ill->ill_saved_ire_cnt = 0;
1161 	mutex_exit(&ill->ill_saved_ire_lock);
1162 	freemsg(mp);
1163 }
1164 
1165 /*
1166  * ire_walk routine used to delete every IRE that depends on
1167  * 'ill'.  (Always called as writer.)
1168  *
1169  * Note: since the routes added by the kernel are deleted separately,
1170  * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1171  *
1172  * We also remove references on ire_nce_cache entries that refer to the ill.
1173  */
1174 void
1175 ill_downi(ire_t *ire, char *ill_arg)
1176 {
1177 	ill_t	*ill = (ill_t *)ill_arg;
1178 	nce_t	*nce;
1179 
1180 	mutex_enter(&ire->ire_lock);
1181 	nce = ire->ire_nce_cache;
1182 	if (nce != NULL && nce->nce_ill == ill)
1183 		ire->ire_nce_cache = NULL;
1184 	else
1185 		nce = NULL;
1186 	mutex_exit(&ire->ire_lock);
1187 	if (nce != NULL)
1188 		nce_refrele(nce);
1189 	if (ire->ire_ill == ill)
1190 		ire_delete(ire);
1191 }
1192 
1193 /* Remove IRE_IF_CLONE on this ill */
1194 void
1195 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1196 {
1197 	ill_t	*ill = (ill_t *)ill_arg;
1198 
1199 	ASSERT(ire->ire_type & IRE_IF_CLONE);
1200 	if (ire->ire_ill == ill)
1201 		ire_delete(ire);
1202 }
1203 
1204 /* Consume an M_IOCACK of the fastpath probe. */
1205 void
1206 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1207 {
1208 	mblk_t	*mp1 = mp;
1209 
1210 	/*
1211 	 * If this was the first attempt turn on the fastpath probing.
1212 	 */
1213 	mutex_enter(&ill->ill_lock);
1214 	if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1215 		ill->ill_dlpi_fastpath_state = IDS_OK;
1216 	mutex_exit(&ill->ill_lock);
1217 
1218 	/* Free the M_IOCACK mblk, hold on to the data */
1219 	mp = mp->b_cont;
1220 	freeb(mp1);
1221 	if (mp == NULL)
1222 		return;
1223 	if (mp->b_cont != NULL)
1224 		nce_fastpath_update(ill, mp);
1225 	else
1226 		ip0dbg(("ill_fastpath_ack:  no b_cont\n"));
1227 	freemsg(mp);
1228 }
1229 
1230 /*
1231  * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1232  * The data portion of the request is a dl_unitdata_req_t template for
1233  * what we would send downstream in the absence of a fastpath confirmation.
1234  */
1235 int
1236 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1237 {
1238 	struct iocblk	*ioc;
1239 	mblk_t	*mp;
1240 
1241 	if (dlur_mp == NULL)
1242 		return (EINVAL);
1243 
1244 	mutex_enter(&ill->ill_lock);
1245 	switch (ill->ill_dlpi_fastpath_state) {
1246 	case IDS_FAILED:
1247 		/*
1248 		 * Driver NAKed the first fastpath ioctl - assume it doesn't
1249 		 * support it.
1250 		 */
1251 		mutex_exit(&ill->ill_lock);
1252 		return (ENOTSUP);
1253 	case IDS_UNKNOWN:
1254 		/* This is the first probe */
1255 		ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1256 		break;
1257 	default:
1258 		break;
1259 	}
1260 	mutex_exit(&ill->ill_lock);
1261 
1262 	if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1263 		return (EAGAIN);
1264 
1265 	mp->b_cont = copyb(dlur_mp);
1266 	if (mp->b_cont == NULL) {
1267 		freeb(mp);
1268 		return (EAGAIN);
1269 	}
1270 
1271 	ioc = (struct iocblk *)mp->b_rptr;
1272 	ioc->ioc_count = msgdsize(mp->b_cont);
1273 
1274 	DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1275 	    char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1276 	putnext(ill->ill_wq, mp);
1277 	return (0);
1278 }
1279 
1280 void
1281 ill_capability_probe(ill_t *ill)
1282 {
1283 	mblk_t	*mp;
1284 
1285 	ASSERT(IAM_WRITER_ILL(ill));
1286 
1287 	if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1288 	    ill->ill_dlpi_capab_state != IDCS_FAILED)
1289 		return;
1290 
1291 	/*
1292 	 * We are starting a new cycle of capability negotiation.
1293 	 * Free up the capab reset messages of any previous incarnation.
1294 	 * We will do a fresh allocation when we get the response to our probe
1295 	 */
1296 	if (ill->ill_capab_reset_mp != NULL) {
1297 		freemsg(ill->ill_capab_reset_mp);
1298 		ill->ill_capab_reset_mp = NULL;
1299 	}
1300 
1301 	ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1302 
1303 	mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1304 	if (mp == NULL)
1305 		return;
1306 
1307 	ill_capability_send(ill, mp);
1308 	ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1309 }
1310 
1311 void
1312 ill_capability_reset(ill_t *ill, boolean_t reneg)
1313 {
1314 	ASSERT(IAM_WRITER_ILL(ill));
1315 
1316 	if (ill->ill_dlpi_capab_state != IDCS_OK)
1317 		return;
1318 
1319 	ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1320 
1321 	ill_capability_send(ill, ill->ill_capab_reset_mp);
1322 	ill->ill_capab_reset_mp = NULL;
1323 	/*
1324 	 * We turn off all capabilities except those pertaining to
1325 	 * direct function call capabilities viz. ILL_CAPAB_DLD*
1326 	 * which will be turned off by the corresponding reset functions.
1327 	 */
1328 	ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM  | ILL_CAPAB_ZEROCOPY);
1329 }
1330 
1331 static void
1332 ill_capability_reset_alloc(ill_t *ill)
1333 {
1334 	mblk_t *mp;
1335 	size_t	size = 0;
1336 	int	err;
1337 	dl_capability_req_t	*capb;
1338 
1339 	ASSERT(IAM_WRITER_ILL(ill));
1340 	ASSERT(ill->ill_capab_reset_mp == NULL);
1341 
1342 	if (ILL_HCKSUM_CAPABLE(ill)) {
1343 		size += sizeof (dl_capability_sub_t) +
1344 		    sizeof (dl_capab_hcksum_t);
1345 	}
1346 
1347 	if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1348 		size += sizeof (dl_capability_sub_t) +
1349 		    sizeof (dl_capab_zerocopy_t);
1350 	}
1351 
1352 	if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1353 		size += sizeof (dl_capability_sub_t) +
1354 		    sizeof (dl_capab_dld_t);
1355 	}
1356 
1357 	mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1358 	    STR_NOSIG, &err);
1359 
1360 	mp->b_datap->db_type = M_PROTO;
1361 	bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1362 
1363 	capb = (dl_capability_req_t *)mp->b_rptr;
1364 	capb->dl_primitive = DL_CAPABILITY_REQ;
1365 	capb->dl_sub_offset = sizeof (dl_capability_req_t);
1366 	capb->dl_sub_length = size;
1367 
1368 	mp->b_wptr += sizeof (dl_capability_req_t);
1369 
1370 	/*
1371 	 * Each handler fills in the corresponding dl_capability_sub_t
1372 	 * inside the mblk,
1373 	 */
1374 	ill_capability_hcksum_reset_fill(ill, mp);
1375 	ill_capability_zerocopy_reset_fill(ill, mp);
1376 	ill_capability_dld_reset_fill(ill, mp);
1377 
1378 	ill->ill_capab_reset_mp = mp;
1379 }
1380 
1381 static void
1382 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1383 {
1384 	dl_capab_id_t *id_ic;
1385 	uint_t sub_dl_cap = outers->dl_cap;
1386 	dl_capability_sub_t *inners;
1387 	uint8_t *capend;
1388 
1389 	ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1390 
1391 	/*
1392 	 * Note: range checks here are not absolutely sufficient to
1393 	 * make us robust against malformed messages sent by drivers;
1394 	 * this is in keeping with the rest of IP's dlpi handling.
1395 	 * (Remember, it's coming from something else in the kernel
1396 	 * address space)
1397 	 */
1398 
1399 	capend = (uint8_t *)(outers + 1) + outers->dl_length;
1400 	if (capend > mp->b_wptr) {
1401 		cmn_err(CE_WARN, "ill_capability_id_ack: "
1402 		    "malformed sub-capability too long for mblk");
1403 		return;
1404 	}
1405 
1406 	id_ic = (dl_capab_id_t *)(outers + 1);
1407 
1408 	if (outers->dl_length < sizeof (*id_ic) ||
1409 	    (inners = &id_ic->id_subcap,
1410 	    inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1411 		cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1412 		    "encapsulated capab type %d too long for mblk",
1413 		    inners->dl_cap);
1414 		return;
1415 	}
1416 
1417 	if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1418 		ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1419 		    "isn't as expected; pass-thru module(s) detected, "
1420 		    "discarding capability\n", inners->dl_cap));
1421 		return;
1422 	}
1423 
1424 	/* Process the encapsulated sub-capability */
1425 	ill_capability_dispatch(ill, mp, inners);
1426 }
1427 
1428 static void
1429 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1430 {
1431 	dl_capability_sub_t *dl_subcap;
1432 
1433 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1434 		return;
1435 
1436 	/*
1437 	 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1438 	 * initialized below since it is not used by DLD.
1439 	 */
1440 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1441 	dl_subcap->dl_cap = DL_CAPAB_DLD;
1442 	dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1443 
1444 	mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1445 }
1446 
1447 static void
1448 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1449 {
1450 	/*
1451 	 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1452 	 * is only to get the VRRP capability.
1453 	 */
1454 	if (ill->ill_ipif_up_count == 0) {
1455 		if (subp->dl_cap == DL_CAPAB_VRRP)
1456 			ill_capability_vrrp_ack(ill, mp, subp);
1457 		return;
1458 	}
1459 
1460 	switch (subp->dl_cap) {
1461 	case DL_CAPAB_HCKSUM:
1462 		ill_capability_hcksum_ack(ill, mp, subp);
1463 		break;
1464 	case DL_CAPAB_ZEROCOPY:
1465 		ill_capability_zerocopy_ack(ill, mp, subp);
1466 		break;
1467 	case DL_CAPAB_DLD:
1468 		ill_capability_dld_ack(ill, mp, subp);
1469 		break;
1470 	case DL_CAPAB_VRRP:
1471 		break;
1472 	default:
1473 		ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1474 		    subp->dl_cap));
1475 	}
1476 }
1477 
1478 /*
1479  * Process the vrrp capability received from a DLS Provider. isub must point
1480  * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1481  */
1482 static void
1483 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1484 {
1485 	dl_capab_vrrp_t	*vrrp;
1486 	uint_t		sub_dl_cap = isub->dl_cap;
1487 	uint8_t		*capend;
1488 
1489 	ASSERT(IAM_WRITER_ILL(ill));
1490 	ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1491 
1492 	/*
1493 	 * Note: range checks here are not absolutely sufficient to
1494 	 * make us robust against malformed messages sent by drivers;
1495 	 * this is in keeping with the rest of IP's dlpi handling.
1496 	 * (Remember, it's coming from something else in the kernel
1497 	 * address space)
1498 	 */
1499 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1500 	if (capend > mp->b_wptr) {
1501 		cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1502 		    "malformed sub-capability too long for mblk");
1503 		return;
1504 	}
1505 	vrrp = (dl_capab_vrrp_t *)(isub + 1);
1506 
1507 	/*
1508 	 * Compare the IP address family and set ILLF_VRRP for the right ill.
1509 	 */
1510 	if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1511 	    (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1512 		ill->ill_flags |= ILLF_VRRP;
1513 	}
1514 }
1515 
1516 /*
1517  * Process a hardware checksum offload capability negotiation ack received
1518  * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1519  * of a DL_CAPABILITY_ACK message.
1520  */
1521 static void
1522 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1523 {
1524 	dl_capability_req_t	*ocap;
1525 	dl_capab_hcksum_t	*ihck, *ohck;
1526 	ill_hcksum_capab_t	**ill_hcksum;
1527 	mblk_t			*nmp = NULL;
1528 	uint_t			sub_dl_cap = isub->dl_cap;
1529 	uint8_t			*capend;
1530 
1531 	ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1532 
1533 	ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1534 
1535 	/*
1536 	 * Note: range checks here are not absolutely sufficient to
1537 	 * make us robust against malformed messages sent by drivers;
1538 	 * this is in keeping with the rest of IP's dlpi handling.
1539 	 * (Remember, it's coming from something else in the kernel
1540 	 * address space)
1541 	 */
1542 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1543 	if (capend > mp->b_wptr) {
1544 		cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1545 		    "malformed sub-capability too long for mblk");
1546 		return;
1547 	}
1548 
1549 	/*
1550 	 * There are two types of acks we process here:
1551 	 * 1. acks in reply to a (first form) generic capability req
1552 	 *    (no ENABLE flag set)
1553 	 * 2. acks in reply to a ENABLE capability req.
1554 	 *    (ENABLE flag set)
1555 	 */
1556 	ihck = (dl_capab_hcksum_t *)(isub + 1);
1557 
1558 	if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1559 		cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1560 		    "unsupported hardware checksum "
1561 		    "sub-capability (version %d, expected %d)",
1562 		    ihck->hcksum_version, HCKSUM_VERSION_1);
1563 		return;
1564 	}
1565 
1566 	if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1567 		ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1568 		    "checksum capability isn't as expected; pass-thru "
1569 		    "module(s) detected, discarding capability\n"));
1570 		return;
1571 	}
1572 
1573 #define	CURR_HCKSUM_CAPAB				\
1574 	(HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 |	\
1575 	HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1576 
1577 	if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1578 	    (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1579 		/* do ENABLE processing */
1580 		if (*ill_hcksum == NULL) {
1581 			*ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1582 			    KM_NOSLEEP);
1583 
1584 			if (*ill_hcksum == NULL) {
1585 				cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1586 				    "could not enable hcksum version %d "
1587 				    "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1588 				    ill->ill_name);
1589 				return;
1590 			}
1591 		}
1592 
1593 		(*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1594 		(*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1595 		ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1596 		ip1dbg(("ill_capability_hcksum_ack: interface %s "
1597 		    "has enabled hardware checksumming\n ",
1598 		    ill->ill_name));
1599 	} else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1600 		/*
1601 		 * Enabling hardware checksum offload
1602 		 * Currently IP supports {TCP,UDP}/IPv4
1603 		 * partial and full cksum offload and
1604 		 * IPv4 header checksum offload.
1605 		 * Allocate new mblk which will
1606 		 * contain a new capability request
1607 		 * to enable hardware checksum offload.
1608 		 */
1609 		uint_t	size;
1610 		uchar_t	*rptr;
1611 
1612 		size = sizeof (dl_capability_req_t) +
1613 		    sizeof (dl_capability_sub_t) + isub->dl_length;
1614 
1615 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1616 			cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1617 			    "could not enable hardware cksum for %s (ENOMEM)\n",
1618 			    ill->ill_name);
1619 			return;
1620 		}
1621 
1622 		rptr = nmp->b_rptr;
1623 		/* initialize dl_capability_req_t */
1624 		ocap = (dl_capability_req_t *)nmp->b_rptr;
1625 		ocap->dl_sub_offset =
1626 		    sizeof (dl_capability_req_t);
1627 		ocap->dl_sub_length =
1628 		    sizeof (dl_capability_sub_t) +
1629 		    isub->dl_length;
1630 		nmp->b_rptr += sizeof (dl_capability_req_t);
1631 
1632 		/* initialize dl_capability_sub_t */
1633 		bcopy(isub, nmp->b_rptr, sizeof (*isub));
1634 		nmp->b_rptr += sizeof (*isub);
1635 
1636 		/* initialize dl_capab_hcksum_t */
1637 		ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1638 		bcopy(ihck, ohck, sizeof (*ihck));
1639 
1640 		nmp->b_rptr = rptr;
1641 		ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1642 
1643 		/* Set ENABLE flag */
1644 		ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1645 		ohck->hcksum_txflags |= HCKSUM_ENABLE;
1646 
1647 		/*
1648 		 * nmp points to a DL_CAPABILITY_REQ message to enable
1649 		 * hardware checksum acceleration.
1650 		 */
1651 		ill_capability_send(ill, nmp);
1652 	} else {
1653 		ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1654 		    "advertised %x hardware checksum capability flags\n",
1655 		    ill->ill_name, ihck->hcksum_txflags));
1656 	}
1657 }
1658 
1659 static void
1660 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1661 {
1662 	dl_capab_hcksum_t *hck_subcap;
1663 	dl_capability_sub_t *dl_subcap;
1664 
1665 	if (!ILL_HCKSUM_CAPABLE(ill))
1666 		return;
1667 
1668 	ASSERT(ill->ill_hcksum_capab != NULL);
1669 
1670 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1671 	dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1672 	dl_subcap->dl_length = sizeof (*hck_subcap);
1673 
1674 	hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1675 	hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1676 	hck_subcap->hcksum_txflags = 0;
1677 
1678 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1679 }
1680 
1681 static void
1682 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1683 {
1684 	mblk_t *nmp = NULL;
1685 	dl_capability_req_t *oc;
1686 	dl_capab_zerocopy_t *zc_ic, *zc_oc;
1687 	ill_zerocopy_capab_t **ill_zerocopy_capab;
1688 	uint_t sub_dl_cap = isub->dl_cap;
1689 	uint8_t *capend;
1690 
1691 	ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1692 
1693 	ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1694 
1695 	/*
1696 	 * Note: range checks here are not absolutely sufficient to
1697 	 * make us robust against malformed messages sent by drivers;
1698 	 * this is in keeping with the rest of IP's dlpi handling.
1699 	 * (Remember, it's coming from something else in the kernel
1700 	 * address space)
1701 	 */
1702 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1703 	if (capend > mp->b_wptr) {
1704 		cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1705 		    "malformed sub-capability too long for mblk");
1706 		return;
1707 	}
1708 
1709 	zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1710 	if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1711 		cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1712 		    "unsupported ZEROCOPY sub-capability (version %d, "
1713 		    "expected %d)", zc_ic->zerocopy_version,
1714 		    ZEROCOPY_VERSION_1);
1715 		return;
1716 	}
1717 
1718 	if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1719 		ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1720 		    "capability isn't as expected; pass-thru module(s) "
1721 		    "detected, discarding capability\n"));
1722 		return;
1723 	}
1724 
1725 	if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1726 		if (*ill_zerocopy_capab == NULL) {
1727 			*ill_zerocopy_capab =
1728 			    kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1729 			    KM_NOSLEEP);
1730 
1731 			if (*ill_zerocopy_capab == NULL) {
1732 				cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1733 				    "could not enable Zero-copy version %d "
1734 				    "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1735 				    ill->ill_name);
1736 				return;
1737 			}
1738 		}
1739 
1740 		ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1741 		    "supports Zero-copy version %d\n", ill->ill_name,
1742 		    ZEROCOPY_VERSION_1));
1743 
1744 		(*ill_zerocopy_capab)->ill_zerocopy_version =
1745 		    zc_ic->zerocopy_version;
1746 		(*ill_zerocopy_capab)->ill_zerocopy_flags =
1747 		    zc_ic->zerocopy_flags;
1748 
1749 		ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1750 	} else {
1751 		uint_t size;
1752 		uchar_t *rptr;
1753 
1754 		size = sizeof (dl_capability_req_t) +
1755 		    sizeof (dl_capability_sub_t) +
1756 		    sizeof (dl_capab_zerocopy_t);
1757 
1758 		if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1759 			cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1760 			    "could not enable zerocopy for %s (ENOMEM)\n",
1761 			    ill->ill_name);
1762 			return;
1763 		}
1764 
1765 		rptr = nmp->b_rptr;
1766 		/* initialize dl_capability_req_t */
1767 		oc = (dl_capability_req_t *)rptr;
1768 		oc->dl_sub_offset = sizeof (dl_capability_req_t);
1769 		oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1770 		    sizeof (dl_capab_zerocopy_t);
1771 		rptr += sizeof (dl_capability_req_t);
1772 
1773 		/* initialize dl_capability_sub_t */
1774 		bcopy(isub, rptr, sizeof (*isub));
1775 		rptr += sizeof (*isub);
1776 
1777 		/* initialize dl_capab_zerocopy_t */
1778 		zc_oc = (dl_capab_zerocopy_t *)rptr;
1779 		*zc_oc = *zc_ic;
1780 
1781 		ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1782 		    "to enable zero-copy version %d\n", ill->ill_name,
1783 		    ZEROCOPY_VERSION_1));
1784 
1785 		/* set VMSAFE_MEM flag */
1786 		zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1787 
1788 		/* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1789 		ill_capability_send(ill, nmp);
1790 	}
1791 }
1792 
1793 static void
1794 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1795 {
1796 	dl_capab_zerocopy_t *zerocopy_subcap;
1797 	dl_capability_sub_t *dl_subcap;
1798 
1799 	if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1800 		return;
1801 
1802 	ASSERT(ill->ill_zerocopy_capab != NULL);
1803 
1804 	dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1805 	dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1806 	dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1807 
1808 	zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1809 	zerocopy_subcap->zerocopy_version =
1810 	    ill->ill_zerocopy_capab->ill_zerocopy_version;
1811 	zerocopy_subcap->zerocopy_flags = 0;
1812 
1813 	mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1814 }
1815 
1816 /*
1817  * DLD capability
1818  * Refer to dld.h for more information regarding the purpose and usage
1819  * of this capability.
1820  */
1821 static void
1822 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1823 {
1824 	dl_capab_dld_t		*dld_ic, dld;
1825 	uint_t			sub_dl_cap = isub->dl_cap;
1826 	uint8_t			*capend;
1827 	ill_dld_capab_t		*idc;
1828 
1829 	ASSERT(IAM_WRITER_ILL(ill));
1830 	ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1831 
1832 	/*
1833 	 * Note: range checks here are not absolutely sufficient to
1834 	 * make us robust against malformed messages sent by drivers;
1835 	 * this is in keeping with the rest of IP's dlpi handling.
1836 	 * (Remember, it's coming from something else in the kernel
1837 	 * address space)
1838 	 */
1839 	capend = (uint8_t *)(isub + 1) + isub->dl_length;
1840 	if (capend > mp->b_wptr) {
1841 		cmn_err(CE_WARN, "ill_capability_dld_ack: "
1842 		    "malformed sub-capability too long for mblk");
1843 		return;
1844 	}
1845 	dld_ic = (dl_capab_dld_t *)(isub + 1);
1846 	if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1847 		cmn_err(CE_CONT, "ill_capability_dld_ack: "
1848 		    "unsupported DLD sub-capability (version %d, "
1849 		    "expected %d)", dld_ic->dld_version,
1850 		    DLD_CURRENT_VERSION);
1851 		return;
1852 	}
1853 	if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1854 		ip1dbg(("ill_capability_dld_ack: mid token for dld "
1855 		    "capability isn't as expected; pass-thru module(s) "
1856 		    "detected, discarding capability\n"));
1857 		return;
1858 	}
1859 
1860 	/*
1861 	 * Copy locally to ensure alignment.
1862 	 */
1863 	bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1864 
1865 	if ((idc = ill->ill_dld_capab) == NULL) {
1866 		idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1867 		if (idc == NULL) {
1868 			cmn_err(CE_WARN, "ill_capability_dld_ack: "
1869 			    "could not enable DLD version %d "
1870 			    "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1871 			    ill->ill_name);
1872 			return;
1873 		}
1874 		ill->ill_dld_capab = idc;
1875 	}
1876 	idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1877 	idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1878 	ip1dbg(("ill_capability_dld_ack: interface %s "
1879 	    "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1880 
1881 	ill_capability_dld_enable(ill);
1882 }
1883 
1884 /*
1885  * Typically capability negotiation between IP and the driver happens via
1886  * DLPI message exchange. However GLD also offers a direct function call
1887  * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1888  * But arbitrary function calls into IP or GLD are not permitted, since both
1889  * of them are protected by their own perimeter mechanism. The perimeter can
1890  * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1891  * these perimeters is IP -> MAC. Thus for example to enable the squeue
1892  * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1893  * to enter the mac perimeter and then do the direct function calls into
1894  * GLD to enable squeue polling. The ring related callbacks from the mac into
1895  * the stack to add, bind, quiesce, restart or cleanup a ring are all
1896  * protected by the mac perimeter.
1897  */
1898 static void
1899 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1900 {
1901 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1902 	int			err;
1903 
1904 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1905 	    DLD_ENABLE);
1906 	ASSERT(err == 0);
1907 }
1908 
1909 static void
1910 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1911 {
1912 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1913 	int			err;
1914 
1915 	err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1916 	    DLD_DISABLE);
1917 	ASSERT(err == 0);
1918 }
1919 
1920 boolean_t
1921 ill_mac_perim_held(ill_t *ill)
1922 {
1923 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1924 
1925 	return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
1926 	    DLD_QUERY));
1927 }
1928 
1929 static void
1930 ill_capability_direct_enable(ill_t *ill)
1931 {
1932 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1933 	ill_dld_direct_t	*idd = &idc->idc_direct;
1934 	dld_capab_direct_t	direct;
1935 	int			rc;
1936 
1937 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1938 
1939 	bzero(&direct, sizeof (direct));
1940 	direct.di_rx_cf = (uintptr_t)ip_input;
1941 	direct.di_rx_ch = ill;
1942 
1943 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
1944 	    DLD_ENABLE);
1945 	if (rc == 0) {
1946 		idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
1947 		idd->idd_tx_dh = direct.di_tx_dh;
1948 		idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
1949 		idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
1950 		idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
1951 		idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
1952 		ASSERT(idd->idd_tx_cb_df != NULL);
1953 		ASSERT(idd->idd_tx_fctl_df != NULL);
1954 		ASSERT(idd->idd_tx_df != NULL);
1955 		/*
1956 		 * One time registration of flow enable callback function
1957 		 */
1958 		ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
1959 		    ill_flow_enable, ill);
1960 		ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
1961 		DTRACE_PROBE1(direct_on, (ill_t *), ill);
1962 	} else {
1963 		cmn_err(CE_WARN, "warning: could not enable DIRECT "
1964 		    "capability, rc = %d\n", rc);
1965 		DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
1966 	}
1967 }
1968 
1969 static void
1970 ill_capability_poll_enable(ill_t *ill)
1971 {
1972 	ill_dld_capab_t		*idc = ill->ill_dld_capab;
1973 	dld_capab_poll_t	poll;
1974 	int			rc;
1975 
1976 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
1977 
1978 	bzero(&poll, sizeof (poll));
1979 	poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
1980 	poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
1981 	poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
1982 	poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
1983 	poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
1984 	poll.poll_ring_ch = ill;
1985 	rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
1986 	    DLD_ENABLE);
1987 	if (rc == 0) {
1988 		ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
1989 		DTRACE_PROBE1(poll_on, (ill_t *), ill);
1990 	} else {
1991 		ip1dbg(("warning: could not enable POLL "
1992 		    "capability, rc = %d\n", rc));
1993 		DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
1994 	}
1995 }
1996 
1997 /*
1998  * Enable the LSO capability.
1999  */
2000 static void
2001 ill_capability_lso_enable(ill_t *ill)
2002 {
2003 	ill_dld_capab_t	*idc = ill->ill_dld_capab;
2004 	dld_capab_lso_t	lso;
2005 	int rc;
2006 
2007 	ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2008 
2009 	if (ill->ill_lso_capab == NULL) {
2010 		ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2011 		    KM_NOSLEEP);
2012 		if (ill->ill_lso_capab == NULL) {
2013 			cmn_err(CE_WARN, "ill_capability_lso_enable: "
2014 			    "could not enable LSO for %s (ENOMEM)\n",
2015 			    ill->ill_name);
2016 			return;
2017 		}
2018 	}
2019 
2020 	bzero(&lso, sizeof (lso));
2021 	if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2022 	    DLD_ENABLE)) == 0) {
2023 		ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2024 		ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2025 		ill->ill_capabilities |= ILL_CAPAB_LSO;
2026 		ip1dbg(("ill_capability_lso_enable: interface %s "
2027 		    "has enabled LSO\n ", ill->ill_name));
2028 	} else {
2029 		kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2030 		ill->ill_lso_capab = NULL;
2031 		DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2032 	}
2033 }
2034 
2035 static void
2036 ill_capability_dld_enable(ill_t *ill)
2037 {
2038 	mac_perim_handle_t mph;
2039 
2040 	ASSERT(IAM_WRITER_ILL(ill));
2041 
2042 	if (ill->ill_isv6)
2043 		return;
2044 
2045 	ill_mac_perim_enter(ill, &mph);
2046 	if (!ill->ill_isv6) {
2047 		ill_capability_direct_enable(ill);
2048 		ill_capability_poll_enable(ill);
2049 		ill_capability_lso_enable(ill);
2050 	}
2051 	ill->ill_capabilities |= ILL_CAPAB_DLD;
2052 	ill_mac_perim_exit(ill, mph);
2053 }
2054 
2055 static void
2056 ill_capability_dld_disable(ill_t *ill)
2057 {
2058 	ill_dld_capab_t	*idc;
2059 	ill_dld_direct_t *idd;
2060 	mac_perim_handle_t	mph;
2061 
2062 	ASSERT(IAM_WRITER_ILL(ill));
2063 
2064 	if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2065 		return;
2066 
2067 	ill_mac_perim_enter(ill, &mph);
2068 
2069 	idc = ill->ill_dld_capab;
2070 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2071 		/*
2072 		 * For performance we avoid locks in the transmit data path
2073 		 * and don't maintain a count of the number of threads using
2074 		 * direct calls. Thus some threads could be using direct
2075 		 * transmit calls to GLD, even after the capability mechanism
2076 		 * turns it off. This is still safe since the handles used in
2077 		 * the direct calls continue to be valid until the unplumb is
2078 		 * completed. Remove the callback that was added (1-time) at
2079 		 * capab enable time.
2080 		 */
2081 		mutex_enter(&ill->ill_lock);
2082 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2083 		mutex_exit(&ill->ill_lock);
2084 		if (ill->ill_flownotify_mh != NULL) {
2085 			idd = &idc->idc_direct;
2086 			idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2087 			    ill->ill_flownotify_mh);
2088 			ill->ill_flownotify_mh = NULL;
2089 		}
2090 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2091 		    NULL, DLD_DISABLE);
2092 	}
2093 
2094 	if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2095 		ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2096 		ip_squeue_clean_all(ill);
2097 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2098 		    NULL, DLD_DISABLE);
2099 	}
2100 
2101 	if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2102 		ASSERT(ill->ill_lso_capab != NULL);
2103 		/*
2104 		 * Clear the capability flag for LSO but retain the
2105 		 * ill_lso_capab structure since it's possible that another
2106 		 * thread is still referring to it.  The structure only gets
2107 		 * deallocated when we destroy the ill.
2108 		 */
2109 
2110 		ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2111 		(void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2112 		    NULL, DLD_DISABLE);
2113 	}
2114 
2115 	ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2116 	ill_mac_perim_exit(ill, mph);
2117 }
2118 
2119 /*
2120  * Capability Negotiation protocol
2121  *
2122  * We don't wait for DLPI capability operations to finish during interface
2123  * bringup or teardown. Doing so would introduce more asynchrony and the
2124  * interface up/down operations will need multiple return and restarts.
2125  * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2126  * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2127  * exclusive operation won't start until the DLPI operations of the previous
2128  * exclusive operation complete.
2129  *
2130  * The capability state machine is shown below.
2131  *
2132  * state		next state		event, action
2133  *
2134  * IDCS_UNKNOWN 	IDCS_PROBE_SENT		ill_capability_probe
2135  * IDCS_PROBE_SENT	IDCS_OK			ill_capability_ack
2136  * IDCS_PROBE_SENT	IDCS_FAILED		ip_rput_dlpi_writer (nack)
2137  * IDCS_OK		IDCS_RENEG		Receipt of DL_NOTE_CAPAB_RENEG
2138  * IDCS_OK		IDCS_RESET_SENT		ill_capability_reset
2139  * IDCS_RESET_SENT	IDCS_UNKNOWN		ill_capability_ack_thr
2140  * IDCS_RENEG		IDCS_PROBE_SENT		ill_capability_ack_thr ->
2141  *						    ill_capability_probe.
2142  */
2143 
2144 /*
2145  * Dedicated thread started from ip_stack_init that handles capability
2146  * disable. This thread ensures the taskq dispatch does not fail by waiting
2147  * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2148  * that direct calls to DLD are done in a cv_waitable context.
2149  */
2150 void
2151 ill_taskq_dispatch(ip_stack_t *ipst)
2152 {
2153 	callb_cpr_t cprinfo;
2154 	char 	name[64];
2155 	mblk_t	*mp;
2156 
2157 	(void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2158 	    ipst->ips_netstack->netstack_stackid);
2159 	CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2160 	    name);
2161 	mutex_enter(&ipst->ips_capab_taskq_lock);
2162 
2163 	for (;;) {
2164 		mp = ipst->ips_capab_taskq_head;
2165 		while (mp != NULL) {
2166 			ipst->ips_capab_taskq_head = mp->b_next;
2167 			if (ipst->ips_capab_taskq_head == NULL)
2168 				ipst->ips_capab_taskq_tail = NULL;
2169 			mutex_exit(&ipst->ips_capab_taskq_lock);
2170 			mp->b_next = NULL;
2171 
2172 			VERIFY(taskq_dispatch(system_taskq,
2173 			    ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2174 			mutex_enter(&ipst->ips_capab_taskq_lock);
2175 			mp = ipst->ips_capab_taskq_head;
2176 		}
2177 
2178 		if (ipst->ips_capab_taskq_quit)
2179 			break;
2180 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2181 		cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2182 		CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2183 	}
2184 	VERIFY(ipst->ips_capab_taskq_head == NULL);
2185 	VERIFY(ipst->ips_capab_taskq_tail == NULL);
2186 	CALLB_CPR_EXIT(&cprinfo);
2187 	thread_exit();
2188 }
2189 
2190 /*
2191  * Consume a new-style hardware capabilities negotiation ack.
2192  * Called via taskq on receipt of DL_CAPABILITY_ACK.
2193  */
2194 static void
2195 ill_capability_ack_thr(void *arg)
2196 {
2197 	mblk_t	*mp = arg;
2198 	dl_capability_ack_t *capp;
2199 	dl_capability_sub_t *subp, *endp;
2200 	ill_t	*ill;
2201 	boolean_t reneg;
2202 
2203 	ill = (ill_t *)mp->b_prev;
2204 	mp->b_prev = NULL;
2205 
2206 	VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2207 
2208 	if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2209 	    ill->ill_dlpi_capab_state == IDCS_RENEG) {
2210 		/*
2211 		 * We have received the ack for our DL_CAPAB reset request.
2212 		 * There isnt' anything in the message that needs processing.
2213 		 * All message based capabilities have been disabled, now
2214 		 * do the function call based capability disable.
2215 		 */
2216 		reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2217 		ill_capability_dld_disable(ill);
2218 		ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2219 		if (reneg)
2220 			ill_capability_probe(ill);
2221 		goto done;
2222 	}
2223 
2224 	if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2225 		ill->ill_dlpi_capab_state = IDCS_OK;
2226 
2227 	capp = (dl_capability_ack_t *)mp->b_rptr;
2228 
2229 	if (capp->dl_sub_length == 0) {
2230 		/* no new-style capabilities */
2231 		goto done;
2232 	}
2233 
2234 	/* make sure the driver supplied correct dl_sub_length */
2235 	if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2236 		ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2237 		    "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2238 		goto done;
2239 	}
2240 
2241 #define	SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2242 	/*
2243 	 * There are sub-capabilities. Process the ones we know about.
2244 	 * Loop until we don't have room for another sub-cap header..
2245 	 */
2246 	for (subp = SC(capp, capp->dl_sub_offset),
2247 	    endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2248 	    subp <= endp;
2249 	    subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2250 
2251 		switch (subp->dl_cap) {
2252 		case DL_CAPAB_ID_WRAPPER:
2253 			ill_capability_id_ack(ill, mp, subp);
2254 			break;
2255 		default:
2256 			ill_capability_dispatch(ill, mp, subp);
2257 			break;
2258 		}
2259 	}
2260 #undef SC
2261 done:
2262 	inet_freemsg(mp);
2263 	ill_capability_done(ill);
2264 	ipsq_exit(ill->ill_phyint->phyint_ipsq);
2265 }
2266 
2267 /*
2268  * This needs to be started in a taskq thread to provide a cv_waitable
2269  * context.
2270  */
2271 void
2272 ill_capability_ack(ill_t *ill, mblk_t *mp)
2273 {
2274 	ip_stack_t	*ipst = ill->ill_ipst;
2275 
2276 	mp->b_prev = (mblk_t *)ill;
2277 	ASSERT(mp->b_next == NULL);
2278 
2279 	if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2280 	    TQ_NOSLEEP) != 0)
2281 		return;
2282 
2283 	/*
2284 	 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2285 	 * which will do the dispatch using TQ_SLEEP to guarantee success.
2286 	 */
2287 	mutex_enter(&ipst->ips_capab_taskq_lock);
2288 	if (ipst->ips_capab_taskq_head == NULL) {
2289 		ASSERT(ipst->ips_capab_taskq_tail == NULL);
2290 		ipst->ips_capab_taskq_head = mp;
2291 	} else {
2292 		ipst->ips_capab_taskq_tail->b_next = mp;
2293 	}
2294 	ipst->ips_capab_taskq_tail = mp;
2295 
2296 	cv_signal(&ipst->ips_capab_taskq_cv);
2297 	mutex_exit(&ipst->ips_capab_taskq_lock);
2298 }
2299 
2300 /*
2301  * This routine is called to scan the fragmentation reassembly table for
2302  * the specified ILL for any packets that are starting to smell.
2303  * dead_interval is the maximum time in seconds that will be tolerated.  It
2304  * will either be the value specified in ip_g_frag_timeout, or zero if the
2305  * ILL is shutting down and it is time to blow everything off.
2306  *
2307  * It returns the number of seconds (as a time_t) that the next frag timer
2308  * should be scheduled for, 0 meaning that the timer doesn't need to be
2309  * re-started.  Note that the method of calculating next_timeout isn't
2310  * entirely accurate since time will flow between the time we grab
2311  * current_time and the time we schedule the next timeout.  This isn't a
2312  * big problem since this is the timer for sending an ICMP reassembly time
2313  * exceeded messages, and it doesn't have to be exactly accurate.
2314  *
2315  * This function is
2316  * sometimes called as writer, although this is not required.
2317  */
2318 time_t
2319 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2320 {
2321 	ipfb_t	*ipfb;
2322 	ipfb_t	*endp;
2323 	ipf_t	*ipf;
2324 	ipf_t	*ipfnext;
2325 	mblk_t	*mp;
2326 	time_t	current_time = gethrestime_sec();
2327 	time_t	next_timeout = 0;
2328 	uint32_t	hdr_length;
2329 	mblk_t	*send_icmp_head;
2330 	mblk_t	*send_icmp_head_v6;
2331 	ip_stack_t *ipst = ill->ill_ipst;
2332 	ip_recv_attr_t iras;
2333 
2334 	bzero(&iras, sizeof (iras));
2335 	iras.ira_flags = 0;
2336 	iras.ira_ill = iras.ira_rill = ill;
2337 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2338 	iras.ira_rifindex = iras.ira_ruifindex;
2339 
2340 	ipfb = ill->ill_frag_hash_tbl;
2341 	if (ipfb == NULL)
2342 		return (B_FALSE);
2343 	endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2344 	/* Walk the frag hash table. */
2345 	for (; ipfb < endp; ipfb++) {
2346 		send_icmp_head = NULL;
2347 		send_icmp_head_v6 = NULL;
2348 		mutex_enter(&ipfb->ipfb_lock);
2349 		while ((ipf = ipfb->ipfb_ipf) != 0) {
2350 			time_t frag_time = current_time - ipf->ipf_timestamp;
2351 			time_t frag_timeout;
2352 
2353 			if (frag_time < dead_interval) {
2354 				/*
2355 				 * There are some outstanding fragments
2356 				 * that will timeout later.  Make note of
2357 				 * the time so that we can reschedule the
2358 				 * next timeout appropriately.
2359 				 */
2360 				frag_timeout = dead_interval - frag_time;
2361 				if (next_timeout == 0 ||
2362 				    frag_timeout < next_timeout) {
2363 					next_timeout = frag_timeout;
2364 				}
2365 				break;
2366 			}
2367 			/* Time's up.  Get it out of here. */
2368 			hdr_length = ipf->ipf_nf_hdr_len;
2369 			ipfnext = ipf->ipf_hash_next;
2370 			if (ipfnext)
2371 				ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2372 			*ipf->ipf_ptphn = ipfnext;
2373 			mp = ipf->ipf_mp->b_cont;
2374 			for (; mp; mp = mp->b_cont) {
2375 				/* Extra points for neatness. */
2376 				IP_REASS_SET_START(mp, 0);
2377 				IP_REASS_SET_END(mp, 0);
2378 			}
2379 			mp = ipf->ipf_mp->b_cont;
2380 			atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2381 			ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2382 			ipfb->ipfb_count -= ipf->ipf_count;
2383 			ASSERT(ipfb->ipfb_frag_pkts > 0);
2384 			ipfb->ipfb_frag_pkts--;
2385 			/*
2386 			 * We do not send any icmp message from here because
2387 			 * we currently are holding the ipfb_lock for this
2388 			 * hash chain. If we try and send any icmp messages
2389 			 * from here we may end up via a put back into ip
2390 			 * trying to get the same lock, causing a recursive
2391 			 * mutex panic. Instead we build a list and send all
2392 			 * the icmp messages after we have dropped the lock.
2393 			 */
2394 			if (ill->ill_isv6) {
2395 				if (hdr_length != 0) {
2396 					mp->b_next = send_icmp_head_v6;
2397 					send_icmp_head_v6 = mp;
2398 				} else {
2399 					freemsg(mp);
2400 				}
2401 			} else {
2402 				if (hdr_length != 0) {
2403 					mp->b_next = send_icmp_head;
2404 					send_icmp_head = mp;
2405 				} else {
2406 					freemsg(mp);
2407 				}
2408 			}
2409 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2410 			ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2411 			freeb(ipf->ipf_mp);
2412 		}
2413 		mutex_exit(&ipfb->ipfb_lock);
2414 		/*
2415 		 * Now need to send any icmp messages that we delayed from
2416 		 * above.
2417 		 */
2418 		while (send_icmp_head_v6 != NULL) {
2419 			ip6_t *ip6h;
2420 
2421 			mp = send_icmp_head_v6;
2422 			send_icmp_head_v6 = send_icmp_head_v6->b_next;
2423 			mp->b_next = NULL;
2424 			ip6h = (ip6_t *)mp->b_rptr;
2425 			iras.ira_flags = 0;
2426 			/*
2427 			 * This will result in an incorrect ALL_ZONES zoneid
2428 			 * for multicast packets, but we
2429 			 * don't send ICMP errors for those in any case.
2430 			 */
2431 			iras.ira_zoneid =
2432 			    ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2433 			    ill, ipst);
2434 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2435 			icmp_time_exceeded_v6(mp,
2436 			    ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2437 			    &iras);
2438 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2439 		}
2440 		while (send_icmp_head != NULL) {
2441 			ipaddr_t dst;
2442 
2443 			mp = send_icmp_head;
2444 			send_icmp_head = send_icmp_head->b_next;
2445 			mp->b_next = NULL;
2446 
2447 			dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2448 
2449 			iras.ira_flags = IRAF_IS_IPV4;
2450 			/*
2451 			 * This will result in an incorrect ALL_ZONES zoneid
2452 			 * for broadcast and multicast packets, but we
2453 			 * don't send ICMP errors for those in any case.
2454 			 */
2455 			iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2456 			    ill, ipst);
2457 			ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2458 			icmp_time_exceeded(mp,
2459 			    ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2460 			ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2461 		}
2462 	}
2463 	/*
2464 	 * A non-dying ILL will use the return value to decide whether to
2465 	 * restart the frag timer, and for how long.
2466 	 */
2467 	return (next_timeout);
2468 }
2469 
2470 /*
2471  * This routine is called when the approximate count of mblk memory used
2472  * for the specified ILL has exceeded max_count.
2473  */
2474 void
2475 ill_frag_prune(ill_t *ill, uint_t max_count)
2476 {
2477 	ipfb_t	*ipfb;
2478 	ipf_t	*ipf;
2479 	size_t	count;
2480 	clock_t now;
2481 
2482 	/*
2483 	 * If we are here within ip_min_frag_prune_time msecs remove
2484 	 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2485 	 * ill_frag_free_num_pkts.
2486 	 */
2487 	mutex_enter(&ill->ill_lock);
2488 	now = ddi_get_lbolt();
2489 	if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2490 	    (ip_min_frag_prune_time != 0 ?
2491 	    ip_min_frag_prune_time : msec_per_tick)) {
2492 
2493 		ill->ill_frag_free_num_pkts++;
2494 
2495 	} else {
2496 		ill->ill_frag_free_num_pkts = 0;
2497 	}
2498 	ill->ill_last_frag_clean_time = now;
2499 	mutex_exit(&ill->ill_lock);
2500 
2501 	/*
2502 	 * free ill_frag_free_num_pkts oldest packets from each bucket.
2503 	 */
2504 	if (ill->ill_frag_free_num_pkts != 0) {
2505 		int ix;
2506 
2507 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2508 			ipfb = &ill->ill_frag_hash_tbl[ix];
2509 			mutex_enter(&ipfb->ipfb_lock);
2510 			if (ipfb->ipfb_ipf != NULL) {
2511 				ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2512 				    ill->ill_frag_free_num_pkts);
2513 			}
2514 			mutex_exit(&ipfb->ipfb_lock);
2515 		}
2516 	}
2517 	/*
2518 	 * While the reassembly list for this ILL is too big, prune a fragment
2519 	 * queue by age, oldest first.
2520 	 */
2521 	while (ill->ill_frag_count > max_count) {
2522 		int	ix;
2523 		ipfb_t	*oipfb = NULL;
2524 		uint_t	oldest = UINT_MAX;
2525 
2526 		count = 0;
2527 		for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2528 			ipfb = &ill->ill_frag_hash_tbl[ix];
2529 			mutex_enter(&ipfb->ipfb_lock);
2530 			ipf = ipfb->ipfb_ipf;
2531 			if (ipf != NULL && ipf->ipf_gen < oldest) {
2532 				oldest = ipf->ipf_gen;
2533 				oipfb = ipfb;
2534 			}
2535 			count += ipfb->ipfb_count;
2536 			mutex_exit(&ipfb->ipfb_lock);
2537 		}
2538 		if (oipfb == NULL)
2539 			break;
2540 
2541 		if (count <= max_count)
2542 			return;	/* Somebody beat us to it, nothing to do */
2543 		mutex_enter(&oipfb->ipfb_lock);
2544 		ipf = oipfb->ipfb_ipf;
2545 		if (ipf != NULL) {
2546 			ill_frag_free_pkts(ill, oipfb, ipf, 1);
2547 		}
2548 		mutex_exit(&oipfb->ipfb_lock);
2549 	}
2550 }
2551 
2552 /*
2553  * free 'free_cnt' fragmented packets starting at ipf.
2554  */
2555 void
2556 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2557 {
2558 	size_t	count;
2559 	mblk_t	*mp;
2560 	mblk_t	*tmp;
2561 	ipf_t **ipfp = ipf->ipf_ptphn;
2562 
2563 	ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2564 	ASSERT(ipfp != NULL);
2565 	ASSERT(ipf != NULL);
2566 
2567 	while (ipf != NULL && free_cnt-- > 0) {
2568 		count = ipf->ipf_count;
2569 		mp = ipf->ipf_mp;
2570 		ipf = ipf->ipf_hash_next;
2571 		for (tmp = mp; tmp; tmp = tmp->b_cont) {
2572 			IP_REASS_SET_START(tmp, 0);
2573 			IP_REASS_SET_END(tmp, 0);
2574 		}
2575 		atomic_add_32(&ill->ill_frag_count, -count);
2576 		ASSERT(ipfb->ipfb_count >= count);
2577 		ipfb->ipfb_count -= count;
2578 		ASSERT(ipfb->ipfb_frag_pkts > 0);
2579 		ipfb->ipfb_frag_pkts--;
2580 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2581 		ip_drop_input("ipIfStatsReasmFails", mp, ill);
2582 		freemsg(mp);
2583 	}
2584 
2585 	if (ipf)
2586 		ipf->ipf_ptphn = ipfp;
2587 	ipfp[0] = ipf;
2588 }
2589 
2590 #define	ND_FORWARD_WARNING	"The <if>:ip*_forwarding ndd variables are " \
2591 	"obsolete and may be removed in a future release of Solaris.  Use " \
2592 	"ifconfig(1M) to manipulate the forwarding status of an interface."
2593 
2594 /*
2595  * For obsolete per-interface forwarding configuration;
2596  * called in response to ND_GET.
2597  */
2598 /* ARGSUSED */
2599 static int
2600 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr)
2601 {
2602 	ill_t *ill = (ill_t *)cp;
2603 
2604 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2605 
2606 	(void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0);
2607 	return (0);
2608 }
2609 
2610 /*
2611  * For obsolete per-interface forwarding configuration;
2612  * called in response to ND_SET.
2613  */
2614 /* ARGSUSED */
2615 static int
2616 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp,
2617     cred_t *ioc_cr)
2618 {
2619 	long value;
2620 	int retval;
2621 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
2622 
2623 	cmn_err(CE_WARN, ND_FORWARD_WARNING);
2624 
2625 	if (ddi_strtol(valuestr, NULL, 10, &value) != 0 ||
2626 	    value < 0 || value > 1) {
2627 		return (EINVAL);
2628 	}
2629 
2630 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
2631 	retval = ill_forward_set((ill_t *)cp, (value != 0));
2632 	rw_exit(&ipst->ips_ill_g_lock);
2633 	return (retval);
2634 }
2635 
2636 /*
2637  * Helper function for ill_forward_set().
2638  */
2639 static void
2640 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2641 {
2642 	ip_stack_t	*ipst = ill->ill_ipst;
2643 
2644 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2645 
2646 	ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2647 	    (enable ? "Enabling" : "Disabling"),
2648 	    (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2649 	mutex_enter(&ill->ill_lock);
2650 	if (enable)
2651 		ill->ill_flags |= ILLF_ROUTER;
2652 	else
2653 		ill->ill_flags &= ~ILLF_ROUTER;
2654 	mutex_exit(&ill->ill_lock);
2655 	if (ill->ill_isv6)
2656 		ill_set_nce_router_flags(ill, enable);
2657 	/* Notify routing socket listeners of this change. */
2658 	if (ill->ill_ipif != NULL)
2659 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2660 }
2661 
2662 /*
2663  * Set an ill's ILLF_ROUTER flag appropriately.  Send up RTS_IFINFO routing
2664  * socket messages for each interface whose flags we change.
2665  */
2666 int
2667 ill_forward_set(ill_t *ill, boolean_t enable)
2668 {
2669 	ipmp_illgrp_t *illg;
2670 	ip_stack_t *ipst = ill->ill_ipst;
2671 
2672 	ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2673 
2674 	if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2675 	    (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2676 		return (0);
2677 
2678 	if (IS_LOOPBACK(ill))
2679 		return (EINVAL);
2680 
2681 	if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2682 		/*
2683 		 * Update all of the interfaces in the group.
2684 		 */
2685 		illg = ill->ill_grp;
2686 		ill = list_head(&illg->ig_if);
2687 		for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2688 			ill_forward_set_on_ill(ill, enable);
2689 
2690 		/*
2691 		 * Update the IPMP meta-interface.
2692 		 */
2693 		ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2694 		return (0);
2695 	}
2696 
2697 	ill_forward_set_on_ill(ill, enable);
2698 	return (0);
2699 }
2700 
2701 /*
2702  * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2703  * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2704  * set or clear.
2705  */
2706 static void
2707 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2708 {
2709 	ipif_t *ipif;
2710 	ncec_t *ncec;
2711 	nce_t *nce;
2712 
2713 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2714 		/*
2715 		 * NOTE: we match across the illgrp because nce's for
2716 		 * addresses on IPMP interfaces have an nce_ill that points to
2717 		 * the bound underlying ill.
2718 		 */
2719 		nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2720 		if (nce != NULL) {
2721 			ncec = nce->nce_common;
2722 			mutex_enter(&ncec->ncec_lock);
2723 			if (enable)
2724 				ncec->ncec_flags |= NCE_F_ISROUTER;
2725 			else
2726 				ncec->ncec_flags &= ~NCE_F_ISROUTER;
2727 			mutex_exit(&ncec->ncec_lock);
2728 			nce_refrele(nce);
2729 		}
2730 	}
2731 }
2732 
2733 /*
2734  * Given an ill with a _valid_ name, add the ip_forwarding ndd variable
2735  * for this ill.  Make sure the v6/v4 question has been answered about this
2736  * ill.  The creation of this ndd variable is only for backwards compatibility.
2737  * The preferred way to control per-interface IP forwarding is through the
2738  * ILLF_ROUTER interface flag.
2739  */
2740 static int
2741 ill_set_ndd_name(ill_t *ill)
2742 {
2743 	char *suffix;
2744 	ip_stack_t	*ipst = ill->ill_ipst;
2745 
2746 	ASSERT(IAM_WRITER_ILL(ill));
2747 
2748 	if (ill->ill_isv6)
2749 		suffix = ipv6_forward_suffix;
2750 	else
2751 		suffix = ipv4_forward_suffix;
2752 
2753 	ill->ill_ndd_name = ill->ill_name + ill->ill_name_length;
2754 	bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1);
2755 	/*
2756 	 * Copies over the '\0'.
2757 	 * Note that strlen(suffix) is always bounded.
2758 	 */
2759 	bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1,
2760 	    strlen(suffix) + 1);
2761 
2762 	/*
2763 	 * Use of the nd table requires holding the reader lock.
2764 	 * Modifying the nd table thru nd_load/nd_unload requires
2765 	 * the writer lock.
2766 	 */
2767 	rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER);
2768 	if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get,
2769 	    nd_ill_forward_set, (caddr_t)ill)) {
2770 		/*
2771 		 * If the nd_load failed, it only meant that it could not
2772 		 * allocate a new bunch of room for further NDD expansion.
2773 		 * Because of that, the ill_ndd_name will be set to 0, and
2774 		 * this interface is at the mercy of the global ip_forwarding
2775 		 * variable.
2776 		 */
2777 		rw_exit(&ipst->ips_ip_g_nd_lock);
2778 		ill->ill_ndd_name = NULL;
2779 		return (ENOMEM);
2780 	}
2781 	rw_exit(&ipst->ips_ip_g_nd_lock);
2782 	return (0);
2783 }
2784 
2785 /*
2786  * Intializes the context structure and returns the first ill in the list
2787  * cuurently start_list and end_list can have values:
2788  * MAX_G_HEADS		Traverse both IPV4 and IPV6 lists.
2789  * IP_V4_G_HEAD		Traverse IPV4 list only.
2790  * IP_V6_G_HEAD		Traverse IPV6 list only.
2791  */
2792 
2793 /*
2794  * We don't check for CONDEMNED ills here. Caller must do that if
2795  * necessary under the ill lock.
2796  */
2797 ill_t *
2798 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2799     ip_stack_t *ipst)
2800 {
2801 	ill_if_t *ifp;
2802 	ill_t *ill;
2803 	avl_tree_t *avl_tree;
2804 
2805 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2806 	ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2807 
2808 	/*
2809 	 * setup the lists to search
2810 	 */
2811 	if (end_list != MAX_G_HEADS) {
2812 		ctx->ctx_current_list = start_list;
2813 		ctx->ctx_last_list = end_list;
2814 	} else {
2815 		ctx->ctx_last_list = MAX_G_HEADS - 1;
2816 		ctx->ctx_current_list = 0;
2817 	}
2818 
2819 	while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2820 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2821 		if (ifp != (ill_if_t *)
2822 		    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2823 			avl_tree = &ifp->illif_avl_by_ppa;
2824 			ill = avl_first(avl_tree);
2825 			/*
2826 			 * ill is guaranteed to be non NULL or ifp should have
2827 			 * not existed.
2828 			 */
2829 			ASSERT(ill != NULL);
2830 			return (ill);
2831 		}
2832 		ctx->ctx_current_list++;
2833 	}
2834 
2835 	return (NULL);
2836 }
2837 
2838 /*
2839  * returns the next ill in the list. ill_first() must have been called
2840  * before calling ill_next() or bad things will happen.
2841  */
2842 
2843 /*
2844  * We don't check for CONDEMNED ills here. Caller must do that if
2845  * necessary under the ill lock.
2846  */
2847 ill_t *
2848 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2849 {
2850 	ill_if_t *ifp;
2851 	ill_t *ill;
2852 	ip_stack_t	*ipst = lastill->ill_ipst;
2853 
2854 	ASSERT(lastill->ill_ifptr != (ill_if_t *)
2855 	    &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2856 	if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2857 	    AVL_AFTER)) != NULL) {
2858 		return (ill);
2859 	}
2860 
2861 	/* goto next ill_ifp in the list. */
2862 	ifp = lastill->ill_ifptr->illif_next;
2863 
2864 	/* make sure not at end of circular list */
2865 	while (ifp ==
2866 	    (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2867 		if (++ctx->ctx_current_list > ctx->ctx_last_list)
2868 			return (NULL);
2869 		ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2870 	}
2871 
2872 	return (avl_first(&ifp->illif_avl_by_ppa));
2873 }
2874 
2875 /*
2876  * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2877  * The final number (PPA) must not have any leading zeros.  Upon success, a
2878  * pointer to the start of the PPA is returned; otherwise NULL is returned.
2879  */
2880 static char *
2881 ill_get_ppa_ptr(char *name)
2882 {
2883 	int namelen = strlen(name);
2884 	int end_ndx = namelen - 1;
2885 	int ppa_ndx, i;
2886 
2887 	/*
2888 	 * Check that the first character is [a-zA-Z], and that the last
2889 	 * character is [0-9].
2890 	 */
2891 	if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2892 		return (NULL);
2893 
2894 	/*
2895 	 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2896 	 */
2897 	for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2898 		if (!isdigit(name[ppa_ndx - 1]))
2899 			break;
2900 
2901 	if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2902 		return (NULL);
2903 
2904 	/*
2905 	 * Check that the intermediate characters are [a-z0-9.]
2906 	 */
2907 	for (i = 1; i < ppa_ndx; i++) {
2908 		if (!isalpha(name[i]) && !isdigit(name[i]) &&
2909 		    name[i] != '.' && name[i] != '_') {
2910 			return (NULL);
2911 		}
2912 	}
2913 
2914 	return (name + ppa_ndx);
2915 }
2916 
2917 /*
2918  * use avl tree to locate the ill.
2919  */
2920 static ill_t *
2921 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2922 {
2923 	char *ppa_ptr = NULL;
2924 	int len;
2925 	uint_t ppa;
2926 	ill_t *ill = NULL;
2927 	ill_if_t *ifp;
2928 	int list;
2929 
2930 	/*
2931 	 * get ppa ptr
2932 	 */
2933 	if (isv6)
2934 		list = IP_V6_G_HEAD;
2935 	else
2936 		list = IP_V4_G_HEAD;
2937 
2938 	if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2939 		return (NULL);
2940 	}
2941 
2942 	len = ppa_ptr - name + 1;
2943 
2944 	ppa = stoi(&ppa_ptr);
2945 
2946 	ifp = IP_VX_ILL_G_LIST(list, ipst);
2947 
2948 	while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2949 		/*
2950 		 * match is done on len - 1 as the name is not null
2951 		 * terminated it contains ppa in addition to the interface
2952 		 * name.
2953 		 */
2954 		if ((ifp->illif_name_len == len) &&
2955 		    bcmp(ifp->illif_name, name, len - 1) == 0) {
2956 			break;
2957 		} else {
2958 			ifp = ifp->illif_next;
2959 		}
2960 	}
2961 
2962 	if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2963 		/*
2964 		 * Even the interface type does not exist.
2965 		 */
2966 		return (NULL);
2967 	}
2968 
2969 	ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2970 	if (ill != NULL) {
2971 		mutex_enter(&ill->ill_lock);
2972 		if (ILL_CAN_LOOKUP(ill)) {
2973 			ill_refhold_locked(ill);
2974 			mutex_exit(&ill->ill_lock);
2975 			return (ill);
2976 		}
2977 		mutex_exit(&ill->ill_lock);
2978 	}
2979 	return (NULL);
2980 }
2981 
2982 /*
2983  * comparison function for use with avl.
2984  */
2985 static int
2986 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2987 {
2988 	uint_t ppa;
2989 	uint_t ill_ppa;
2990 
2991 	ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2992 
2993 	ppa = *((uint_t *)ppa_ptr);
2994 	ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2995 	/*
2996 	 * We want the ill with the lowest ppa to be on the
2997 	 * top.
2998 	 */
2999 	if (ill_ppa < ppa)
3000 		return (1);
3001 	if (ill_ppa > ppa)
3002 		return (-1);
3003 	return (0);
3004 }
3005 
3006 /*
3007  * remove an interface type from the global list.
3008  */
3009 static void
3010 ill_delete_interface_type(ill_if_t *interface)
3011 {
3012 	ASSERT(interface != NULL);
3013 	ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
3014 
3015 	avl_destroy(&interface->illif_avl_by_ppa);
3016 	if (interface->illif_ppa_arena != NULL)
3017 		vmem_destroy(interface->illif_ppa_arena);
3018 
3019 	remque(interface);
3020 
3021 	mi_free(interface);
3022 }
3023 
3024 /*
3025  * remove ill from the global list.
3026  */
3027 static void
3028 ill_glist_delete(ill_t *ill)
3029 {
3030 	ip_stack_t	*ipst;
3031 	phyint_t	*phyi;
3032 
3033 	if (ill == NULL)
3034 		return;
3035 	ipst = ill->ill_ipst;
3036 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3037 
3038 	/*
3039 	 * If the ill was never inserted into the AVL tree
3040 	 * we skip the if branch.
3041 	 */
3042 	if (ill->ill_ifptr != NULL) {
3043 		/*
3044 		 * remove from AVL tree and free ppa number
3045 		 */
3046 		avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3047 
3048 		if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3049 			vmem_free(ill->ill_ifptr->illif_ppa_arena,
3050 			    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3051 		}
3052 		if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3053 			ill_delete_interface_type(ill->ill_ifptr);
3054 		}
3055 
3056 		/*
3057 		 * Indicate ill is no longer in the list.
3058 		 */
3059 		ill->ill_ifptr = NULL;
3060 		ill->ill_name_length = 0;
3061 		ill->ill_name[0] = '\0';
3062 		ill->ill_ppa = UINT_MAX;
3063 	}
3064 
3065 	/* Generate one last event for this ill. */
3066 	ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3067 	    ill->ill_name_length);
3068 
3069 	ASSERT(ill->ill_phyint != NULL);
3070 	phyi = ill->ill_phyint;
3071 	ill->ill_phyint = NULL;
3072 
3073 	/*
3074 	 * ill_init allocates a phyint always to store the copy
3075 	 * of flags relevant to phyint. At that point in time, we could
3076 	 * not assign the name and hence phyint_illv4/v6 could not be
3077 	 * initialized. Later in ipif_set_values, we assign the name to
3078 	 * the ill, at which point in time we assign phyint_illv4/v6.
3079 	 * Thus we don't rely on phyint_illv6 to be initialized always.
3080 	 */
3081 	if (ill->ill_flags & ILLF_IPV6)
3082 		phyi->phyint_illv6 = NULL;
3083 	else
3084 		phyi->phyint_illv4 = NULL;
3085 
3086 	if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3087 		rw_exit(&ipst->ips_ill_g_lock);
3088 		return;
3089 	}
3090 
3091 	/*
3092 	 * There are no ills left on this phyint; pull it out of the phyint
3093 	 * avl trees, and free it.
3094 	 */
3095 	if (phyi->phyint_ifindex > 0) {
3096 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3097 		    phyi);
3098 		avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3099 		    phyi);
3100 	}
3101 	rw_exit(&ipst->ips_ill_g_lock);
3102 
3103 	phyint_free(phyi);
3104 }
3105 
3106 /*
3107  * allocate a ppa, if the number of plumbed interfaces of this type are
3108  * less than ill_no_arena do a linear search to find a unused ppa.
3109  * When the number goes beyond ill_no_arena switch to using an arena.
3110  * Note: ppa value of zero cannot be allocated from vmem_arena as it
3111  * is the return value for an error condition, so allocation starts at one
3112  * and is decremented by one.
3113  */
3114 static int
3115 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3116 {
3117 	ill_t *tmp_ill;
3118 	uint_t start, end;
3119 	int ppa;
3120 
3121 	if (ifp->illif_ppa_arena == NULL &&
3122 	    (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3123 		/*
3124 		 * Create an arena.
3125 		 */
3126 		ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3127 		    (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3128 		    NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3129 			/* allocate what has already been assigned */
3130 		for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3131 		    tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3132 		    tmp_ill, AVL_AFTER)) {
3133 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3134 			    1,		/* size */
3135 			    1,		/* align/quantum */
3136 			    0,		/* phase */
3137 			    0,		/* nocross */
3138 			    /* minaddr */
3139 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3140 			    /* maxaddr */
3141 			    (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3142 			    VM_NOSLEEP|VM_FIRSTFIT);
3143 			if (ppa == 0) {
3144 				ip1dbg(("ill_alloc_ppa: ppa allocation"
3145 				    " failed while switching"));
3146 				vmem_destroy(ifp->illif_ppa_arena);
3147 				ifp->illif_ppa_arena = NULL;
3148 				break;
3149 			}
3150 		}
3151 	}
3152 
3153 	if (ifp->illif_ppa_arena != NULL) {
3154 		if (ill->ill_ppa == UINT_MAX) {
3155 			ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3156 			    1, VM_NOSLEEP|VM_FIRSTFIT);
3157 			if (ppa == 0)
3158 				return (EAGAIN);
3159 			ill->ill_ppa = --ppa;
3160 		} else {
3161 			ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3162 			    1, 		/* size */
3163 			    1, 		/* align/quantum */
3164 			    0, 		/* phase */
3165 			    0, 		/* nocross */
3166 			    (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3167 			    (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3168 			    VM_NOSLEEP|VM_FIRSTFIT);
3169 			/*
3170 			 * Most likely the allocation failed because
3171 			 * the requested ppa was in use.
3172 			 */
3173 			if (ppa == 0)
3174 				return (EEXIST);
3175 		}
3176 		return (0);
3177 	}
3178 
3179 	/*
3180 	 * No arena is in use and not enough (>ill_no_arena) interfaces have
3181 	 * been plumbed to create one. Do a linear search to get a unused ppa.
3182 	 */
3183 	if (ill->ill_ppa == UINT_MAX) {
3184 		end = UINT_MAX - 1;
3185 		start = 0;
3186 	} else {
3187 		end = start = ill->ill_ppa;
3188 	}
3189 
3190 	tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3191 	while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3192 		if (start++ >= end) {
3193 			if (ill->ill_ppa == UINT_MAX)
3194 				return (EAGAIN);
3195 			else
3196 				return (EEXIST);
3197 		}
3198 		tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3199 	}
3200 	ill->ill_ppa = start;
3201 	return (0);
3202 }
3203 
3204 /*
3205  * Insert ill into the list of configured ill's. Once this function completes,
3206  * the ill is globally visible and is available through lookups. More precisely
3207  * this happens after the caller drops the ill_g_lock.
3208  */
3209 static int
3210 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3211 {
3212 	ill_if_t *ill_interface;
3213 	avl_index_t where = 0;
3214 	int error;
3215 	int name_length;
3216 	int index;
3217 	boolean_t check_length = B_FALSE;
3218 	ip_stack_t	*ipst = ill->ill_ipst;
3219 
3220 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3221 
3222 	name_length = mi_strlen(name) + 1;
3223 
3224 	if (isv6)
3225 		index = IP_V6_G_HEAD;
3226 	else
3227 		index = IP_V4_G_HEAD;
3228 
3229 	ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3230 	/*
3231 	 * Search for interface type based on name
3232 	 */
3233 	while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3234 		if ((ill_interface->illif_name_len == name_length) &&
3235 		    (strcmp(ill_interface->illif_name, name) == 0)) {
3236 			break;
3237 		}
3238 		ill_interface = ill_interface->illif_next;
3239 	}
3240 
3241 	/*
3242 	 * Interface type not found, create one.
3243 	 */
3244 	if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3245 		ill_g_head_t ghead;
3246 
3247 		/*
3248 		 * allocate ill_if_t structure
3249 		 */
3250 		ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3251 		if (ill_interface == NULL) {
3252 			return (ENOMEM);
3253 		}
3254 
3255 		(void) strcpy(ill_interface->illif_name, name);
3256 		ill_interface->illif_name_len = name_length;
3257 
3258 		avl_create(&ill_interface->illif_avl_by_ppa,
3259 		    ill_compare_ppa, sizeof (ill_t),
3260 		    offsetof(struct ill_s, ill_avl_byppa));
3261 
3262 		/*
3263 		 * link the structure in the back to maintain order
3264 		 * of configuration for ifconfig output.
3265 		 */
3266 		ghead = ipst->ips_ill_g_heads[index];
3267 		insque(ill_interface, ghead.ill_g_list_tail);
3268 	}
3269 
3270 	if (ill->ill_ppa == UINT_MAX)
3271 		check_length = B_TRUE;
3272 
3273 	error = ill_alloc_ppa(ill_interface, ill);
3274 	if (error != 0) {
3275 		if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3276 			ill_delete_interface_type(ill->ill_ifptr);
3277 		return (error);
3278 	}
3279 
3280 	/*
3281 	 * When the ppa is choosen by the system, check that there is
3282 	 * enough space to insert ppa. if a specific ppa was passed in this
3283 	 * check is not required as the interface name passed in will have
3284 	 * the right ppa in it.
3285 	 */
3286 	if (check_length) {
3287 		/*
3288 		 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3289 		 */
3290 		char buf[sizeof (uint_t) * 3];
3291 
3292 		/*
3293 		 * convert ppa to string to calculate the amount of space
3294 		 * required for it in the name.
3295 		 */
3296 		numtos(ill->ill_ppa, buf);
3297 
3298 		/* Do we have enough space to insert ppa ? */
3299 
3300 		if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3301 			/* Free ppa and interface type struct */
3302 			if (ill_interface->illif_ppa_arena != NULL) {
3303 				vmem_free(ill_interface->illif_ppa_arena,
3304 				    (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3305 			}
3306 			if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3307 				ill_delete_interface_type(ill->ill_ifptr);
3308 
3309 			return (EINVAL);
3310 		}
3311 	}
3312 
3313 	(void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3314 	ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3315 
3316 	(void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3317 	    &where);
3318 	ill->ill_ifptr = ill_interface;
3319 	avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3320 
3321 	ill_phyint_reinit(ill);
3322 	return (0);
3323 }
3324 
3325 /* Initialize the per phyint ipsq used for serialization */
3326 static boolean_t
3327 ipsq_init(ill_t *ill, boolean_t enter)
3328 {
3329 	ipsq_t  *ipsq;
3330 	ipxop_t	*ipx;
3331 
3332 	if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3333 		return (B_FALSE);
3334 
3335 	ill->ill_phyint->phyint_ipsq = ipsq;
3336 	ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3337 	ipx->ipx_ipsq = ipsq;
3338 	ipsq->ipsq_next = ipsq;
3339 	ipsq->ipsq_phyint = ill->ill_phyint;
3340 	mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3341 	mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3342 	ipsq->ipsq_ipst = ill->ill_ipst;	/* No netstack_hold */
3343 	if (enter) {
3344 		ipx->ipx_writer = curthread;
3345 		ipx->ipx_forced = B_FALSE;
3346 		ipx->ipx_reentry_cnt = 1;
3347 #ifdef DEBUG
3348 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3349 #endif
3350 	}
3351 	return (B_TRUE);
3352 }
3353 
3354 /*
3355  * ill_init is called by ip_open when a device control stream is opened.
3356  * It does a few initializations, and shoots a DL_INFO_REQ message down
3357  * to the driver.  The response is later picked up in ip_rput_dlpi and
3358  * used to set up default mechanisms for talking to the driver.  (Always
3359  * called as writer.)
3360  *
3361  * If this function returns error, ip_open will call ip_close which in
3362  * turn will call ill_delete to clean up any memory allocated here that
3363  * is not yet freed.
3364  */
3365 int
3366 ill_init(queue_t *q, ill_t *ill)
3367 {
3368 	int	count;
3369 	dl_info_req_t	*dlir;
3370 	mblk_t	*info_mp;
3371 	uchar_t *frag_ptr;
3372 
3373 	/*
3374 	 * The ill is initialized to zero by mi_alloc*(). In addition
3375 	 * some fields already contain valid values, initialized in
3376 	 * ip_open(), before we reach here.
3377 	 */
3378 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3379 	mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3380 	ill->ill_saved_ire_cnt = 0;
3381 
3382 	ill->ill_rq = q;
3383 	ill->ill_wq = WR(q);
3384 
3385 	info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3386 	    BPRI_HI);
3387 	if (info_mp == NULL)
3388 		return (ENOMEM);
3389 
3390 	/*
3391 	 * Allocate sufficient space to contain our fragment hash table and
3392 	 * the device name.
3393 	 */
3394 	frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE +
3395 	    2 * LIFNAMSIZ + strlen(ipv6_forward_suffix));
3396 	if (frag_ptr == NULL) {
3397 		freemsg(info_mp);
3398 		return (ENOMEM);
3399 	}
3400 	ill->ill_frag_ptr = frag_ptr;
3401 	ill->ill_frag_free_num_pkts = 0;
3402 	ill->ill_last_frag_clean_time = 0;
3403 	ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3404 	ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3405 	for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3406 		mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3407 		    NULL, MUTEX_DEFAULT, NULL);
3408 	}
3409 
3410 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3411 	if (ill->ill_phyint == NULL) {
3412 		freemsg(info_mp);
3413 		mi_free(frag_ptr);
3414 		return (ENOMEM);
3415 	}
3416 
3417 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3418 	/*
3419 	 * For now pretend this is a v4 ill. We need to set phyint_ill*
3420 	 * at this point because of the following reason. If we can't
3421 	 * enter the ipsq at some point and cv_wait, the writer that
3422 	 * wakes us up tries to locate us using the list of all phyints
3423 	 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3424 	 * If we don't set it now, we risk a missed wakeup.
3425 	 */
3426 	ill->ill_phyint->phyint_illv4 = ill;
3427 	ill->ill_ppa = UINT_MAX;
3428 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3429 
3430 	ill_set_inputfn(ill);
3431 
3432 	if (!ipsq_init(ill, B_TRUE)) {
3433 		freemsg(info_mp);
3434 		mi_free(frag_ptr);
3435 		mi_free(ill->ill_phyint);
3436 		return (ENOMEM);
3437 	}
3438 
3439 	ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3440 
3441 	/* Frag queue limit stuff */
3442 	ill->ill_frag_count = 0;
3443 	ill->ill_ipf_gen = 0;
3444 
3445 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3446 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3447 	ill->ill_global_timer = INFINITY;
3448 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3449 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3450 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3451 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3452 
3453 	/*
3454 	 * Initialize IPv6 configuration variables.  The IP module is always
3455 	 * opened as an IPv4 module.  Instead tracking down the cases where
3456 	 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3457 	 * here for convenience, this has no effect until the ill is set to do
3458 	 * IPv6.
3459 	 */
3460 	ill->ill_reachable_time = ND_REACHABLE_TIME;
3461 	ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3462 	ill->ill_max_buf = ND_MAX_Q;
3463 	ill->ill_refcnt = 0;
3464 
3465 	/* Send down the Info Request to the driver. */
3466 	info_mp->b_datap->db_type = M_PCPROTO;
3467 	dlir = (dl_info_req_t *)info_mp->b_rptr;
3468 	info_mp->b_wptr = (uchar_t *)&dlir[1];
3469 	dlir->dl_primitive = DL_INFO_REQ;
3470 
3471 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3472 
3473 	qprocson(q);
3474 	ill_dlpi_send(ill, info_mp);
3475 
3476 	return (0);
3477 }
3478 
3479 /*
3480  * ill_dls_info
3481  * creates datalink socket info from the device.
3482  */
3483 int
3484 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3485 {
3486 	size_t	len;
3487 
3488 	sdl->sdl_family = AF_LINK;
3489 	sdl->sdl_index = ill_get_upper_ifindex(ill);
3490 	sdl->sdl_type = ill->ill_type;
3491 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3492 	len = strlen(sdl->sdl_data);
3493 	ASSERT(len < 256);
3494 	sdl->sdl_nlen = (uchar_t)len;
3495 	sdl->sdl_alen = ill->ill_phys_addr_length;
3496 	sdl->sdl_slen = 0;
3497 	if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3498 		bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3499 
3500 	return (sizeof (struct sockaddr_dl));
3501 }
3502 
3503 /*
3504  * ill_xarp_info
3505  * creates xarp info from the device.
3506  */
3507 static int
3508 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3509 {
3510 	sdl->sdl_family = AF_LINK;
3511 	sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3512 	sdl->sdl_type = ill->ill_type;
3513 	ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3514 	sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3515 	sdl->sdl_alen = ill->ill_phys_addr_length;
3516 	sdl->sdl_slen = 0;
3517 	return (sdl->sdl_nlen);
3518 }
3519 
3520 static int
3521 loopback_kstat_update(kstat_t *ksp, int rw)
3522 {
3523 	kstat_named_t *kn;
3524 	netstackid_t	stackid;
3525 	netstack_t	*ns;
3526 	ip_stack_t	*ipst;
3527 
3528 	if (ksp == NULL || ksp->ks_data == NULL)
3529 		return (EIO);
3530 
3531 	if (rw == KSTAT_WRITE)
3532 		return (EACCES);
3533 
3534 	kn = KSTAT_NAMED_PTR(ksp);
3535 	stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3536 
3537 	ns = netstack_find_by_stackid(stackid);
3538 	if (ns == NULL)
3539 		return (-1);
3540 
3541 	ipst = ns->netstack_ip;
3542 	if (ipst == NULL) {
3543 		netstack_rele(ns);
3544 		return (-1);
3545 	}
3546 	kn[0].value.ui32 = ipst->ips_loopback_packets;
3547 	kn[1].value.ui32 = ipst->ips_loopback_packets;
3548 	netstack_rele(ns);
3549 	return (0);
3550 }
3551 
3552 /*
3553  * Has ifindex been plumbed already?
3554  */
3555 static boolean_t
3556 phyint_exists(uint_t index, ip_stack_t *ipst)
3557 {
3558 	ASSERT(index != 0);
3559 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3560 
3561 	return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3562 	    &index, NULL) != NULL);
3563 }
3564 
3565 /* Pick a unique ifindex */
3566 boolean_t
3567 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3568 {
3569 	uint_t starting_index;
3570 
3571 	if (!ipst->ips_ill_index_wrap) {
3572 		*indexp = ipst->ips_ill_index++;
3573 		if (ipst->ips_ill_index == 0) {
3574 			/* Reached the uint_t limit Next time wrap  */
3575 			ipst->ips_ill_index_wrap = B_TRUE;
3576 		}
3577 		return (B_TRUE);
3578 	}
3579 
3580 	/*
3581 	 * Start reusing unused indexes. Note that we hold the ill_g_lock
3582 	 * at this point and don't want to call any function that attempts
3583 	 * to get the lock again.
3584 	 */
3585 	starting_index = ipst->ips_ill_index++;
3586 	for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) {
3587 		if (ipst->ips_ill_index != 0 &&
3588 		    !phyint_exists(ipst->ips_ill_index, ipst)) {
3589 			/* found unused index - use it */
3590 			*indexp = ipst->ips_ill_index;
3591 			return (B_TRUE);
3592 		}
3593 	}
3594 
3595 	/*
3596 	 * all interface indicies are inuse.
3597 	 */
3598 	return (B_FALSE);
3599 }
3600 
3601 /*
3602  * Assign a unique interface index for the phyint.
3603  */
3604 static boolean_t
3605 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3606 {
3607 	ASSERT(phyi->phyint_ifindex == 0);
3608 	return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3609 }
3610 
3611 /*
3612  * Initialize the flags on `phyi' as per the provided mactype.
3613  */
3614 static void
3615 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3616 {
3617 	uint64_t flags = 0;
3618 
3619 	/*
3620 	 * Initialize PHYI_RUNNING and PHYI_FAILED.  For non-IPMP interfaces,
3621 	 * we always presume the underlying hardware is working and set
3622 	 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3623 	 * DL_NOTE_LINK_DOWN message).  For IPMP interfaces, at initialization
3624 	 * there are no active interfaces in the group so we set PHYI_FAILED.
3625 	 */
3626 	if (mactype == SUNW_DL_IPMP)
3627 		flags |= PHYI_FAILED;
3628 	else
3629 		flags |= PHYI_RUNNING;
3630 
3631 	switch (mactype) {
3632 	case SUNW_DL_VNI:
3633 		flags |= PHYI_VIRTUAL;
3634 		break;
3635 	case SUNW_DL_IPMP:
3636 		flags |= PHYI_IPMP;
3637 		break;
3638 	case DL_LOOP:
3639 		flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3640 		break;
3641 	}
3642 
3643 	mutex_enter(&phyi->phyint_lock);
3644 	phyi->phyint_flags |= flags;
3645 	mutex_exit(&phyi->phyint_lock);
3646 }
3647 
3648 /*
3649  * Return a pointer to the ill which matches the supplied name.  Note that
3650  * the ill name length includes the null termination character.  (May be
3651  * called as writer.)
3652  * If do_alloc and the interface is "lo0" it will be automatically created.
3653  * Cannot bump up reference on condemned ills. So dup detect can't be done
3654  * using this func.
3655  */
3656 ill_t *
3657 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3658     boolean_t *did_alloc, ip_stack_t *ipst)
3659 {
3660 	ill_t	*ill;
3661 	ipif_t	*ipif;
3662 	ipsq_t	*ipsq;
3663 	kstat_named_t	*kn;
3664 	boolean_t isloopback;
3665 	in6_addr_t ov6addr;
3666 
3667 	isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3668 
3669 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3670 	ill = ill_find_by_name(name, isv6, ipst);
3671 	rw_exit(&ipst->ips_ill_g_lock);
3672 	if (ill != NULL)
3673 		return (ill);
3674 
3675 	/*
3676 	 * Couldn't find it.  Does this happen to be a lookup for the
3677 	 * loopback device and are we allowed to allocate it?
3678 	 */
3679 	if (!isloopback || !do_alloc)
3680 		return (NULL);
3681 
3682 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3683 	ill = ill_find_by_name(name, isv6, ipst);
3684 	if (ill != NULL) {
3685 		rw_exit(&ipst->ips_ill_g_lock);
3686 		return (ill);
3687 	}
3688 
3689 	/* Create the loopback device on demand */
3690 	ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3691 	    sizeof (ipif_loopback_name), BPRI_MED));
3692 	if (ill == NULL)
3693 		goto done;
3694 
3695 	*ill = ill_null;
3696 	mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3697 	ill->ill_ipst = ipst;
3698 	list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3699 	netstack_hold(ipst->ips_netstack);
3700 	/*
3701 	 * For exclusive stacks we set the zoneid to zero
3702 	 * to make IP operate as if in the global zone.
3703 	 */
3704 	ill->ill_zoneid = GLOBAL_ZONEID;
3705 
3706 	ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3707 	if (ill->ill_phyint == NULL)
3708 		goto done;
3709 
3710 	if (isv6)
3711 		ill->ill_phyint->phyint_illv6 = ill;
3712 	else
3713 		ill->ill_phyint->phyint_illv4 = ill;
3714 	mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3715 	phyint_flags_init(ill->ill_phyint, DL_LOOP);
3716 
3717 	if (isv6) {
3718 		ill->ill_isv6 = B_TRUE;
3719 		ill->ill_max_frag = ip_loopback_mtu_v6plus;
3720 	} else {
3721 		ill->ill_max_frag = ip_loopback_mtuplus;
3722 	}
3723 	if (!ill_allocate_mibs(ill))
3724 		goto done;
3725 	ill->ill_current_frag = ill->ill_max_frag;
3726 	ill->ill_mtu = ill->ill_max_frag;	/* Initial value */
3727 	/*
3728 	 * ipif_loopback_name can't be pointed at directly because its used
3729 	 * by both the ipv4 and ipv6 interfaces.  When the ill is removed
3730 	 * from the glist, ill_glist_delete() sets the first character of
3731 	 * ill_name to '\0'.
3732 	 */
3733 	ill->ill_name = (char *)ill + sizeof (*ill);
3734 	(void) strcpy(ill->ill_name, ipif_loopback_name);
3735 	ill->ill_name_length = sizeof (ipif_loopback_name);
3736 	/* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3737 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
3738 
3739 	rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3740 	mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3741 	ill->ill_global_timer = INFINITY;
3742 	ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3743 	ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3744 	ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3745 	ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3746 
3747 	/* No resolver here. */
3748 	ill->ill_net_type = IRE_LOOPBACK;
3749 
3750 	/* Initialize the ipsq */
3751 	if (!ipsq_init(ill, B_FALSE))
3752 		goto done;
3753 
3754 	ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3755 	if (ipif == NULL)
3756 		goto done;
3757 
3758 	ill->ill_flags = ILLF_MULTICAST;
3759 
3760 	ov6addr = ipif->ipif_v6lcl_addr;
3761 	/* Set up default loopback address and mask. */
3762 	if (!isv6) {
3763 		ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3764 
3765 		IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3766 		V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3767 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3768 		    ipif->ipif_v6subnet);
3769 		ill->ill_flags |= ILLF_IPV4;
3770 	} else {
3771 		ipif->ipif_v6lcl_addr = ipv6_loopback;
3772 		ipif->ipif_v6net_mask = ipv6_all_ones;
3773 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3774 		    ipif->ipif_v6subnet);
3775 		ill->ill_flags |= ILLF_IPV6;
3776 	}
3777 
3778 	/*
3779 	 * Chain us in at the end of the ill list. hold the ill
3780 	 * before we make it globally visible. 1 for the lookup.
3781 	 */
3782 	ill->ill_refcnt = 0;
3783 	ill_refhold(ill);
3784 
3785 	ill->ill_frag_count = 0;
3786 	ill->ill_frag_free_num_pkts = 0;
3787 	ill->ill_last_frag_clean_time = 0;
3788 
3789 	ipsq = ill->ill_phyint->phyint_ipsq;
3790 
3791 	ill_set_inputfn(ill);
3792 
3793 	if (ill_glist_insert(ill, "lo", isv6) != 0)
3794 		cmn_err(CE_PANIC, "cannot insert loopback interface");
3795 
3796 	/* Let SCTP know so that it can add this to its list */
3797 	sctp_update_ill(ill, SCTP_ILL_INSERT);
3798 
3799 	/*
3800 	 * We have already assigned ipif_v6lcl_addr above, but we need to
3801 	 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3802 	 * requires to be after ill_glist_insert() since we need the
3803 	 * ill_index set. Pass on ipv6_loopback as the old address.
3804 	 */
3805 	sctp_update_ipif_addr(ipif, ov6addr);
3806 
3807 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3808 
3809 	/*
3810 	 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3811 	 * If so, free our original one.
3812 	 */
3813 	if (ipsq != ill->ill_phyint->phyint_ipsq)
3814 		ipsq_delete(ipsq);
3815 
3816 	if (ipst->ips_loopback_ksp == NULL) {
3817 		/* Export loopback interface statistics */
3818 		ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3819 		    ipif_loopback_name, "net",
3820 		    KSTAT_TYPE_NAMED, 2, 0,
3821 		    ipst->ips_netstack->netstack_stackid);
3822 		if (ipst->ips_loopback_ksp != NULL) {
3823 			ipst->ips_loopback_ksp->ks_update =
3824 			    loopback_kstat_update;
3825 			kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3826 			kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3827 			kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3828 			ipst->ips_loopback_ksp->ks_private =
3829 			    (void *)(uintptr_t)ipst->ips_netstack->
3830 			    netstack_stackid;
3831 			kstat_install(ipst->ips_loopback_ksp);
3832 		}
3833 	}
3834 
3835 	*did_alloc = B_TRUE;
3836 	rw_exit(&ipst->ips_ill_g_lock);
3837 	ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3838 	    NE_PLUMB, ill->ill_name, ill->ill_name_length);
3839 	return (ill);
3840 done:
3841 	if (ill != NULL) {
3842 		if (ill->ill_phyint != NULL) {
3843 			ipsq = ill->ill_phyint->phyint_ipsq;
3844 			if (ipsq != NULL) {
3845 				ipsq->ipsq_phyint = NULL;
3846 				ipsq_delete(ipsq);
3847 			}
3848 			mi_free(ill->ill_phyint);
3849 		}
3850 		ill_free_mib(ill);
3851 		if (ill->ill_ipst != NULL)
3852 			netstack_rele(ill->ill_ipst->ips_netstack);
3853 		mi_free(ill);
3854 	}
3855 	rw_exit(&ipst->ips_ill_g_lock);
3856 	return (NULL);
3857 }
3858 
3859 /*
3860  * For IPP calls - use the ip_stack_t for global stack.
3861  */
3862 ill_t *
3863 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3864 {
3865 	ip_stack_t	*ipst;
3866 	ill_t		*ill;
3867 
3868 	ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3869 	if (ipst == NULL) {
3870 		cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3871 		return (NULL);
3872 	}
3873 
3874 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
3875 	netstack_rele(ipst->ips_netstack);
3876 	return (ill);
3877 }
3878 
3879 /*
3880  * Return a pointer to the ill which matches the index and IP version type.
3881  */
3882 ill_t *
3883 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3884 {
3885 	ill_t	*ill;
3886 	phyint_t *phyi;
3887 
3888 	/*
3889 	 * Indexes are stored in the phyint - a common structure
3890 	 * to both IPv4 and IPv6.
3891 	 */
3892 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3893 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3894 	    (void *) &index, NULL);
3895 	if (phyi != NULL) {
3896 		ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3897 		if (ill != NULL) {
3898 			mutex_enter(&ill->ill_lock);
3899 			if (!ILL_IS_CONDEMNED(ill)) {
3900 				ill_refhold_locked(ill);
3901 				mutex_exit(&ill->ill_lock);
3902 				rw_exit(&ipst->ips_ill_g_lock);
3903 				return (ill);
3904 			}
3905 			mutex_exit(&ill->ill_lock);
3906 		}
3907 	}
3908 	rw_exit(&ipst->ips_ill_g_lock);
3909 	return (NULL);
3910 }
3911 
3912 /*
3913  * Verify whether or not an interface index is valid.
3914  * It can be zero (meaning "reset") or an interface index assigned
3915  * to a non-VNI interface. (We don't use VNI interface to send packets.)
3916  */
3917 boolean_t
3918 ip_ifindex_valid(uint_t ifindex, boolean_t isv6, ip_stack_t *ipst)
3919 {
3920 	ill_t		*ill;
3921 
3922 	if (ifindex == 0)
3923 		return (B_TRUE);
3924 
3925 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
3926 	if (ill == NULL)
3927 		return (B_FALSE);
3928 	if (IS_VNI(ill)) {
3929 		ill_refrele(ill);
3930 		return (B_FALSE);
3931 	}
3932 	ill_refrele(ill);
3933 	return (B_TRUE);
3934 }
3935 
3936 /*
3937  * Return the ifindex next in sequence after the passed in ifindex.
3938  * If there is no next ifindex for the given protocol, return 0.
3939  */
3940 uint_t
3941 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3942 {
3943 	phyint_t *phyi;
3944 	phyint_t *phyi_initial;
3945 	uint_t   ifindex;
3946 
3947 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3948 
3949 	if (index == 0) {
3950 		phyi = avl_first(
3951 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3952 	} else {
3953 		phyi = phyi_initial = avl_find(
3954 		    &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3955 		    (void *) &index, NULL);
3956 	}
3957 
3958 	for (; phyi != NULL;
3959 	    phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3960 	    phyi, AVL_AFTER)) {
3961 		/*
3962 		 * If we're not returning the first interface in the tree
3963 		 * and we still haven't moved past the phyint_t that
3964 		 * corresponds to index, avl_walk needs to be called again
3965 		 */
3966 		if (!((index != 0) && (phyi == phyi_initial))) {
3967 			if (isv6) {
3968 				if ((phyi->phyint_illv6) &&
3969 				    ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3970 				    (phyi->phyint_illv6->ill_isv6 == 1))
3971 					break;
3972 			} else {
3973 				if ((phyi->phyint_illv4) &&
3974 				    ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3975 				    (phyi->phyint_illv4->ill_isv6 == 0))
3976 					break;
3977 			}
3978 		}
3979 	}
3980 
3981 	rw_exit(&ipst->ips_ill_g_lock);
3982 
3983 	if (phyi != NULL)
3984 		ifindex = phyi->phyint_ifindex;
3985 	else
3986 		ifindex = 0;
3987 
3988 	return (ifindex);
3989 }
3990 
3991 /*
3992  * Return the ifindex for the named interface.
3993  * If there is no next ifindex for the interface, return 0.
3994  */
3995 uint_t
3996 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3997 {
3998 	phyint_t	*phyi;
3999 	avl_index_t	where = 0;
4000 	uint_t		ifindex;
4001 
4002 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4003 
4004 	if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4005 	    name, &where)) == NULL) {
4006 		rw_exit(&ipst->ips_ill_g_lock);
4007 		return (0);
4008 	}
4009 
4010 	ifindex = phyi->phyint_ifindex;
4011 
4012 	rw_exit(&ipst->ips_ill_g_lock);
4013 
4014 	return (ifindex);
4015 }
4016 
4017 /*
4018  * Return the ifindex to be used by upper layer protocols for instance
4019  * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4020  */
4021 uint_t
4022 ill_get_upper_ifindex(const ill_t *ill)
4023 {
4024 	if (IS_UNDER_IPMP(ill))
4025 		return (ipmp_ill_get_ipmp_ifindex(ill));
4026 	else
4027 		return (ill->ill_phyint->phyint_ifindex);
4028 }
4029 
4030 
4031 /*
4032  * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4033  * that gives a running thread a reference to the ill. This reference must be
4034  * released by the thread when it is done accessing the ill and related
4035  * objects. ill_refcnt can not be used to account for static references
4036  * such as other structures pointing to an ill. Callers must generally
4037  * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4038  * or be sure that the ill is not being deleted or changing state before
4039  * calling the refhold functions. A non-zero ill_refcnt ensures that the
4040  * ill won't change any of its critical state such as address, netmask etc.
4041  */
4042 void
4043 ill_refhold(ill_t *ill)
4044 {
4045 	mutex_enter(&ill->ill_lock);
4046 	ill->ill_refcnt++;
4047 	ILL_TRACE_REF(ill);
4048 	mutex_exit(&ill->ill_lock);
4049 }
4050 
4051 void
4052 ill_refhold_locked(ill_t *ill)
4053 {
4054 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4055 	ill->ill_refcnt++;
4056 	ILL_TRACE_REF(ill);
4057 }
4058 
4059 /* Returns true if we managed to get a refhold */
4060 boolean_t
4061 ill_check_and_refhold(ill_t *ill)
4062 {
4063 	mutex_enter(&ill->ill_lock);
4064 	if (!ILL_IS_CONDEMNED(ill)) {
4065 		ill_refhold_locked(ill);
4066 		mutex_exit(&ill->ill_lock);
4067 		return (B_TRUE);
4068 	}
4069 	mutex_exit(&ill->ill_lock);
4070 	return (B_FALSE);
4071 }
4072 
4073 /*
4074  * Must not be called while holding any locks. Otherwise if this is
4075  * the last reference to be released, there is a chance of recursive mutex
4076  * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4077  * to restart an ioctl.
4078  */
4079 void
4080 ill_refrele(ill_t *ill)
4081 {
4082 	mutex_enter(&ill->ill_lock);
4083 	ASSERT(ill->ill_refcnt != 0);
4084 	ill->ill_refcnt--;
4085 	ILL_UNTRACE_REF(ill);
4086 	if (ill->ill_refcnt != 0) {
4087 		/* Every ire pointing to the ill adds 1 to ill_refcnt */
4088 		mutex_exit(&ill->ill_lock);
4089 		return;
4090 	}
4091 
4092 	/* Drops the ill_lock */
4093 	ipif_ill_refrele_tail(ill);
4094 }
4095 
4096 /*
4097  * Obtain a weak reference count on the ill. This reference ensures the
4098  * ill won't be freed, but the ill may change any of its critical state
4099  * such as netmask, address etc. Returns an error if the ill has started
4100  * closing.
4101  */
4102 boolean_t
4103 ill_waiter_inc(ill_t *ill)
4104 {
4105 	mutex_enter(&ill->ill_lock);
4106 	if (ill->ill_state_flags & ILL_CONDEMNED) {
4107 		mutex_exit(&ill->ill_lock);
4108 		return (B_FALSE);
4109 	}
4110 	ill->ill_waiters++;
4111 	mutex_exit(&ill->ill_lock);
4112 	return (B_TRUE);
4113 }
4114 
4115 void
4116 ill_waiter_dcr(ill_t *ill)
4117 {
4118 	mutex_enter(&ill->ill_lock);
4119 	ill->ill_waiters--;
4120 	if (ill->ill_waiters == 0)
4121 		cv_broadcast(&ill->ill_cv);
4122 	mutex_exit(&ill->ill_lock);
4123 }
4124 
4125 /*
4126  * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4127  * driver.  We construct best guess defaults for lower level information that
4128  * we need.  If an interface is brought up without injection of any overriding
4129  * information from outside, we have to be ready to go with these defaults.
4130  * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4131  * we primarely want the dl_provider_style.
4132  * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4133  * at which point we assume the other part of the information is valid.
4134  */
4135 void
4136 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4137 {
4138 	uchar_t		*brdcst_addr;
4139 	uint_t		brdcst_addr_length, phys_addr_length;
4140 	t_scalar_t	sap_length;
4141 	dl_info_ack_t	*dlia;
4142 	ip_m_t		*ipm;
4143 	dl_qos_cl_sel1_t *sel1;
4144 	int		min_mtu;
4145 
4146 	ASSERT(IAM_WRITER_ILL(ill));
4147 
4148 	/*
4149 	 * Till the ill is fully up  the ill is not globally visible.
4150 	 * So no need for a lock.
4151 	 */
4152 	dlia = (dl_info_ack_t *)mp->b_rptr;
4153 	ill->ill_mactype = dlia->dl_mac_type;
4154 
4155 	ipm = ip_m_lookup(dlia->dl_mac_type);
4156 	if (ipm == NULL) {
4157 		ipm = ip_m_lookup(DL_OTHER);
4158 		ASSERT(ipm != NULL);
4159 	}
4160 	ill->ill_media = ipm;
4161 
4162 	/*
4163 	 * When the new DLPI stuff is ready we'll pull lengths
4164 	 * from dlia.
4165 	 */
4166 	if (dlia->dl_version == DL_VERSION_2) {
4167 		brdcst_addr_length = dlia->dl_brdcst_addr_length;
4168 		brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4169 		    brdcst_addr_length);
4170 		if (brdcst_addr == NULL) {
4171 			brdcst_addr_length = 0;
4172 		}
4173 		sap_length = dlia->dl_sap_length;
4174 		phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4175 		ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4176 		    brdcst_addr_length, sap_length, phys_addr_length));
4177 	} else {
4178 		brdcst_addr_length = 6;
4179 		brdcst_addr = ip_six_byte_all_ones;
4180 		sap_length = -2;
4181 		phys_addr_length = brdcst_addr_length;
4182 	}
4183 
4184 	ill->ill_bcast_addr_length = brdcst_addr_length;
4185 	ill->ill_phys_addr_length = phys_addr_length;
4186 	ill->ill_sap_length = sap_length;
4187 
4188 	/*
4189 	 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4190 	 * but we must ensure a minimum IP MTU is used since other bits of
4191 	 * IP will fly apart otherwise.
4192 	 */
4193 	min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4194 	ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4195 	ill->ill_current_frag = ill->ill_max_frag;
4196 	ill->ill_mtu = ill->ill_max_frag;
4197 
4198 	ill->ill_type = ipm->ip_m_type;
4199 
4200 	if (!ill->ill_dlpi_style_set) {
4201 		if (dlia->dl_provider_style == DL_STYLE2)
4202 			ill->ill_needs_attach = 1;
4203 
4204 		phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4205 
4206 		/*
4207 		 * Allocate the first ipif on this ill.  We don't delay it
4208 		 * further as ioctl handling assumes at least one ipif exists.
4209 		 *
4210 		 * At this point we don't know whether the ill is v4 or v6.
4211 		 * We will know this whan the SIOCSLIFNAME happens and
4212 		 * the correct value for ill_isv6 will be assigned in
4213 		 * ipif_set_values(). We need to hold the ill lock and
4214 		 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4215 		 * the wakeup.
4216 		 */
4217 		(void) ipif_allocate(ill, 0, IRE_LOCAL,
4218 		    dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4219 		mutex_enter(&ill->ill_lock);
4220 		ASSERT(ill->ill_dlpi_style_set == 0);
4221 		ill->ill_dlpi_style_set = 1;
4222 		ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4223 		cv_broadcast(&ill->ill_cv);
4224 		mutex_exit(&ill->ill_lock);
4225 		freemsg(mp);
4226 		return;
4227 	}
4228 	ASSERT(ill->ill_ipif != NULL);
4229 	/*
4230 	 * We know whether it is IPv4 or IPv6 now, as this is the
4231 	 * second DL_INFO_ACK we are recieving in response to the
4232 	 * DL_INFO_REQ sent in ipif_set_values.
4233 	 */
4234 	ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4235 	/*
4236 	 * Clear all the flags that were set based on ill_bcast_addr_length
4237 	 * and ill_phys_addr_length (in ipif_set_values) as these could have
4238 	 * changed now and we need to re-evaluate.
4239 	 */
4240 	ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4241 	ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4242 
4243 	/*
4244 	 * Free ill_bcast_mp as things could have changed now.
4245 	 *
4246 	 * NOTE: The IPMP meta-interface is special-cased because it starts
4247 	 * with no underlying interfaces (and thus an unknown broadcast
4248 	 * address length), but we enforce that an interface is broadcast-
4249 	 * capable as part of allowing it to join a group.
4250 	 */
4251 	if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4252 		if (ill->ill_bcast_mp != NULL)
4253 			freemsg(ill->ill_bcast_mp);
4254 		ill->ill_net_type = IRE_IF_NORESOLVER;
4255 
4256 		ill->ill_bcast_mp = ill_dlur_gen(NULL,
4257 		    ill->ill_phys_addr_length,
4258 		    ill->ill_sap,
4259 		    ill->ill_sap_length);
4260 
4261 		if (ill->ill_isv6)
4262 			/*
4263 			 * Note: xresolv interfaces will eventually need NOARP
4264 			 * set here as well, but that will require those
4265 			 * external resolvers to have some knowledge of
4266 			 * that flag and act appropriately. Not to be changed
4267 			 * at present.
4268 			 */
4269 			ill->ill_flags |= ILLF_NONUD;
4270 		else
4271 			ill->ill_flags |= ILLF_NOARP;
4272 
4273 		if (ill->ill_mactype == SUNW_DL_VNI) {
4274 			ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4275 		} else if (ill->ill_phys_addr_length == 0 ||
4276 		    ill->ill_mactype == DL_IPV4 ||
4277 		    ill->ill_mactype == DL_IPV6) {
4278 			/*
4279 			 * The underying link is point-to-point, so mark the
4280 			 * interface as such.  We can do IP multicast over
4281 			 * such a link since it transmits all network-layer
4282 			 * packets to the remote side the same way.
4283 			 */
4284 			ill->ill_flags |= ILLF_MULTICAST;
4285 			ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4286 		}
4287 	} else {
4288 		ill->ill_net_type = IRE_IF_RESOLVER;
4289 		if (ill->ill_bcast_mp != NULL)
4290 			freemsg(ill->ill_bcast_mp);
4291 		ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4292 		    ill->ill_bcast_addr_length, ill->ill_sap,
4293 		    ill->ill_sap_length);
4294 		/*
4295 		 * Later detect lack of DLPI driver multicast
4296 		 * capability by catching DL_ENABMULTI errors in
4297 		 * ip_rput_dlpi.
4298 		 */
4299 		ill->ill_flags |= ILLF_MULTICAST;
4300 		if (!ill->ill_isv6)
4301 			ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4302 	}
4303 
4304 	/* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4305 	if (ill->ill_mactype == SUNW_DL_IPMP)
4306 		ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4307 
4308 	/* By default an interface does not support any CoS marking */
4309 	ill->ill_flags &= ~ILLF_COS_ENABLED;
4310 
4311 	/*
4312 	 * If we get QoS information in DL_INFO_ACK, the device supports
4313 	 * some form of CoS marking, set ILLF_COS_ENABLED.
4314 	 */
4315 	sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4316 	    dlia->dl_qos_length);
4317 	if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4318 		ill->ill_flags |= ILLF_COS_ENABLED;
4319 	}
4320 
4321 	/* Clear any previous error indication. */
4322 	ill->ill_error = 0;
4323 	freemsg(mp);
4324 }
4325 
4326 /*
4327  * Perform various checks to verify that an address would make sense as a
4328  * local, remote, or subnet interface address.
4329  */
4330 static boolean_t
4331 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4332 {
4333 	ipaddr_t	net_mask;
4334 
4335 	/*
4336 	 * Don't allow all zeroes, or all ones, but allow
4337 	 * all ones netmask.
4338 	 */
4339 	if ((net_mask = ip_net_mask(addr)) == 0)
4340 		return (B_FALSE);
4341 	/* A given netmask overrides the "guess" netmask */
4342 	if (subnet_mask != 0)
4343 		net_mask = subnet_mask;
4344 	if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4345 	    (addr == (addr | ~net_mask)))) {
4346 		return (B_FALSE);
4347 	}
4348 
4349 	/*
4350 	 * Even if the netmask is all ones, we do not allow address to be
4351 	 * 255.255.255.255
4352 	 */
4353 	if (addr == INADDR_BROADCAST)
4354 		return (B_FALSE);
4355 
4356 	if (CLASSD(addr))
4357 		return (B_FALSE);
4358 
4359 	return (B_TRUE);
4360 }
4361 
4362 #define	V6_IPIF_LINKLOCAL(p)	\
4363 	IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4364 
4365 /*
4366  * Compare two given ipifs and check if the second one is better than
4367  * the first one using the order of preference (not taking deprecated
4368  * into acount) specified in ipif_lookup_multicast().
4369  */
4370 static boolean_t
4371 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4372 {
4373 	/* Check the least preferred first. */
4374 	if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4375 		/* If both ipifs are the same, use the first one. */
4376 		if (IS_LOOPBACK(new_ipif->ipif_ill))
4377 			return (B_FALSE);
4378 		else
4379 			return (B_TRUE);
4380 	}
4381 
4382 	/* For IPv6, check for link local address. */
4383 	if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4384 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4385 		    V6_IPIF_LINKLOCAL(new_ipif)) {
4386 			/* The second one is equal or less preferred. */
4387 			return (B_FALSE);
4388 		} else {
4389 			return (B_TRUE);
4390 		}
4391 	}
4392 
4393 	/* Then check for point to point interface. */
4394 	if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4395 		if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4396 		    (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4397 		    (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4398 			return (B_FALSE);
4399 		} else {
4400 			return (B_TRUE);
4401 		}
4402 	}
4403 
4404 	/* old_ipif is a normal interface, so no need to use the new one. */
4405 	return (B_FALSE);
4406 }
4407 
4408 /*
4409  * Find a mulitcast-capable ipif given an IP instance and zoneid.
4410  * The ipif must be up, and its ill must multicast-capable, not
4411  * condemned, not an underlying interface in an IPMP group, and
4412  * not a VNI interface.  Order of preference:
4413  *
4414  * 	1a. normal
4415  * 	1b. normal, but deprecated
4416  * 	2a. point to point
4417  * 	2b. point to point, but deprecated
4418  * 	3a. link local
4419  * 	3b. link local, but deprecated
4420  * 	4. loopback.
4421  */
4422 static ipif_t *
4423 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4424 {
4425 	ill_t			*ill;
4426 	ill_walk_context_t	ctx;
4427 	ipif_t			*ipif;
4428 	ipif_t			*saved_ipif = NULL;
4429 	ipif_t			*dep_ipif = NULL;
4430 
4431 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4432 	if (isv6)
4433 		ill = ILL_START_WALK_V6(&ctx, ipst);
4434 	else
4435 		ill = ILL_START_WALK_V4(&ctx, ipst);
4436 
4437 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4438 		mutex_enter(&ill->ill_lock);
4439 		if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4440 		    ILL_IS_CONDEMNED(ill) ||
4441 		    !(ill->ill_flags & ILLF_MULTICAST)) {
4442 			mutex_exit(&ill->ill_lock);
4443 			continue;
4444 		}
4445 		for (ipif = ill->ill_ipif; ipif != NULL;
4446 		    ipif = ipif->ipif_next) {
4447 			if (zoneid != ipif->ipif_zoneid &&
4448 			    zoneid != ALL_ZONES &&
4449 			    ipif->ipif_zoneid != ALL_ZONES) {
4450 				continue;
4451 			}
4452 			if (!(ipif->ipif_flags & IPIF_UP) ||
4453 			    IPIF_IS_CONDEMNED(ipif)) {
4454 				continue;
4455 			}
4456 
4457 			/*
4458 			 * Found one candidate.  If it is deprecated,
4459 			 * remember it in dep_ipif.  If it is not deprecated,
4460 			 * remember it in saved_ipif.
4461 			 */
4462 			if (ipif->ipif_flags & IPIF_DEPRECATED) {
4463 				if (dep_ipif == NULL) {
4464 					dep_ipif = ipif;
4465 				} else if (ipif_comp_multi(dep_ipif, ipif,
4466 				    isv6)) {
4467 					/*
4468 					 * If the previous dep_ipif does not
4469 					 * belong to the same ill, we've done
4470 					 * a ipif_refhold() on it.  So we need
4471 					 * to release it.
4472 					 */
4473 					if (dep_ipif->ipif_ill != ill)
4474 						ipif_refrele(dep_ipif);
4475 					dep_ipif = ipif;
4476 				}
4477 				continue;
4478 			}
4479 			if (saved_ipif == NULL) {
4480 				saved_ipif = ipif;
4481 			} else {
4482 				if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4483 					if (saved_ipif->ipif_ill != ill)
4484 						ipif_refrele(saved_ipif);
4485 					saved_ipif = ipif;
4486 				}
4487 			}
4488 		}
4489 		/*
4490 		 * Before going to the next ill, do a ipif_refhold() on the
4491 		 * saved ones.
4492 		 */
4493 		if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4494 			ipif_refhold_locked(saved_ipif);
4495 		if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4496 			ipif_refhold_locked(dep_ipif);
4497 		mutex_exit(&ill->ill_lock);
4498 	}
4499 	rw_exit(&ipst->ips_ill_g_lock);
4500 
4501 	/*
4502 	 * If we have only the saved_ipif, return it.  But if we have both
4503 	 * saved_ipif and dep_ipif, check to see which one is better.
4504 	 */
4505 	if (saved_ipif != NULL) {
4506 		if (dep_ipif != NULL) {
4507 			if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4508 				ipif_refrele(saved_ipif);
4509 				return (dep_ipif);
4510 			} else {
4511 				ipif_refrele(dep_ipif);
4512 				return (saved_ipif);
4513 			}
4514 		}
4515 		return (saved_ipif);
4516 	} else {
4517 		return (dep_ipif);
4518 	}
4519 }
4520 
4521 ill_t *
4522 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4523 {
4524 	ipif_t *ipif;
4525 	ill_t *ill;
4526 
4527 	ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4528 	if (ipif == NULL)
4529 		return (NULL);
4530 
4531 	ill = ipif->ipif_ill;
4532 	ill_refhold(ill);
4533 	ipif_refrele(ipif);
4534 	return (ill);
4535 }
4536 
4537 /*
4538  * This function is called when an application does not specify an interface
4539  * to be used for multicast traffic (joining a group/sending data).  It
4540  * calls ire_lookup_multi() to look for an interface route for the
4541  * specified multicast group.  Doing this allows the administrator to add
4542  * prefix routes for multicast to indicate which interface to be used for
4543  * multicast traffic in the above scenario.  The route could be for all
4544  * multicast (224.0/4), for a single multicast group (a /32 route) or
4545  * anything in between.  If there is no such multicast route, we just find
4546  * any multicast capable interface and return it.  The returned ipif
4547  * is refhold'ed.
4548  *
4549  * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4550  * unicast table. This is used by CGTP.
4551  */
4552 ill_t *
4553 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4554     boolean_t *multirtp, ipaddr_t *setsrcp)
4555 {
4556 	ill_t			*ill;
4557 
4558 	ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4559 	if (ill != NULL)
4560 		return (ill);
4561 
4562 	return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4563 }
4564 
4565 /*
4566  * Look for an ipif with the specified interface address and destination.
4567  * The destination address is used only for matching point-to-point interfaces.
4568  */
4569 ipif_t *
4570 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4571 {
4572 	ipif_t	*ipif;
4573 	ill_t	*ill;
4574 	ill_walk_context_t ctx;
4575 
4576 	/*
4577 	 * First match all the point-to-point interfaces
4578 	 * before looking at non-point-to-point interfaces.
4579 	 * This is done to avoid returning non-point-to-point
4580 	 * ipif instead of unnumbered point-to-point ipif.
4581 	 */
4582 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4583 	ill = ILL_START_WALK_V4(&ctx, ipst);
4584 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4585 		mutex_enter(&ill->ill_lock);
4586 		for (ipif = ill->ill_ipif; ipif != NULL;
4587 		    ipif = ipif->ipif_next) {
4588 			/* Allow the ipif to be down */
4589 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4590 			    (ipif->ipif_lcl_addr == if_addr) &&
4591 			    (ipif->ipif_pp_dst_addr == dst)) {
4592 				if (!IPIF_IS_CONDEMNED(ipif)) {
4593 					ipif_refhold_locked(ipif);
4594 					mutex_exit(&ill->ill_lock);
4595 					rw_exit(&ipst->ips_ill_g_lock);
4596 					return (ipif);
4597 				}
4598 			}
4599 		}
4600 		mutex_exit(&ill->ill_lock);
4601 	}
4602 	rw_exit(&ipst->ips_ill_g_lock);
4603 
4604 	/* lookup the ipif based on interface address */
4605 	ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4606 	ASSERT(ipif == NULL || !ipif->ipif_isv6);
4607 	return (ipif);
4608 }
4609 
4610 /*
4611  * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4612  */
4613 static ipif_t *
4614 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4615     zoneid_t zoneid, ip_stack_t *ipst)
4616 {
4617 	ipif_t  *ipif;
4618 	ill_t   *ill;
4619 	boolean_t ptp = B_FALSE;
4620 	ill_walk_context_t	ctx;
4621 	boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4622 	boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4623 
4624 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4625 	/*
4626 	 * Repeat twice, first based on local addresses and
4627 	 * next time for pointopoint.
4628 	 */
4629 repeat:
4630 	ill = ILL_START_WALK_V4(&ctx, ipst);
4631 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4632 		if (match_ill != NULL && ill != match_ill &&
4633 		    (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4634 			continue;
4635 		}
4636 		mutex_enter(&ill->ill_lock);
4637 		for (ipif = ill->ill_ipif; ipif != NULL;
4638 		    ipif = ipif->ipif_next) {
4639 			if (zoneid != ALL_ZONES &&
4640 			    zoneid != ipif->ipif_zoneid &&
4641 			    ipif->ipif_zoneid != ALL_ZONES)
4642 				continue;
4643 
4644 			if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4645 				continue;
4646 
4647 			/* Allow the ipif to be down */
4648 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4649 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4650 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4651 			    (ipif->ipif_pp_dst_addr == addr))) {
4652 				if (!IPIF_IS_CONDEMNED(ipif)) {
4653 					ipif_refhold_locked(ipif);
4654 					mutex_exit(&ill->ill_lock);
4655 					rw_exit(&ipst->ips_ill_g_lock);
4656 					return (ipif);
4657 				}
4658 			}
4659 		}
4660 		mutex_exit(&ill->ill_lock);
4661 	}
4662 
4663 	/* If we already did the ptp case, then we are done */
4664 	if (ptp) {
4665 		rw_exit(&ipst->ips_ill_g_lock);
4666 		return (NULL);
4667 	}
4668 	ptp = B_TRUE;
4669 	goto repeat;
4670 }
4671 
4672 /*
4673  * Lookup an ipif with the specified address.  For point-to-point links we
4674  * look for matches on either the destination address or the local address,
4675  * but we skip the local address check if IPIF_UNNUMBERED is set.  If the
4676  * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4677  * (or illgrp if `match_ill' is in an IPMP group).
4678  */
4679 ipif_t *
4680 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4681     ip_stack_t *ipst)
4682 {
4683 	return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4684 	    zoneid, ipst));
4685 }
4686 
4687 /*
4688  * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4689  * except that we will only return an address if it is not marked as
4690  * IPIF_DUPLICATE
4691  */
4692 ipif_t *
4693 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4694     ip_stack_t *ipst)
4695 {
4696 	return (ipif_lookup_addr_common(addr, match_ill,
4697 	    (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4698 	    zoneid, ipst));
4699 }
4700 
4701 /*
4702  * Special abbreviated version of ipif_lookup_addr() that doesn't match
4703  * `match_ill' across the IPMP group.  This function is only needed in some
4704  * corner-cases; almost everything should use ipif_lookup_addr().
4705  */
4706 ipif_t *
4707 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4708 {
4709 	ASSERT(match_ill != NULL);
4710 	return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4711 	    ipst));
4712 }
4713 
4714 /*
4715  * Look for an ipif with the specified address. For point-point links
4716  * we look for matches on either the destination address and the local
4717  * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4718  * is set.
4719  * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4720  * ill (or illgrp if `match_ill' is in an IPMP group).
4721  * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4722  */
4723 zoneid_t
4724 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4725 {
4726 	zoneid_t zoneid;
4727 	ipif_t  *ipif;
4728 	ill_t   *ill;
4729 	boolean_t ptp = B_FALSE;
4730 	ill_walk_context_t	ctx;
4731 
4732 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4733 	/*
4734 	 * Repeat twice, first based on local addresses and
4735 	 * next time for pointopoint.
4736 	 */
4737 repeat:
4738 	ill = ILL_START_WALK_V4(&ctx, ipst);
4739 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4740 		if (match_ill != NULL && ill != match_ill &&
4741 		    !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4742 			continue;
4743 		}
4744 		mutex_enter(&ill->ill_lock);
4745 		for (ipif = ill->ill_ipif; ipif != NULL;
4746 		    ipif = ipif->ipif_next) {
4747 			/* Allow the ipif to be down */
4748 			if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4749 			    ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4750 			    (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4751 			    (ipif->ipif_pp_dst_addr == addr)) &&
4752 			    !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4753 				zoneid = ipif->ipif_zoneid;
4754 				mutex_exit(&ill->ill_lock);
4755 				rw_exit(&ipst->ips_ill_g_lock);
4756 				/*
4757 				 * If ipif_zoneid was ALL_ZONES then we have
4758 				 * a trusted extensions shared IP address.
4759 				 * In that case GLOBAL_ZONEID works to send.
4760 				 */
4761 				if (zoneid == ALL_ZONES)
4762 					zoneid = GLOBAL_ZONEID;
4763 				return (zoneid);
4764 			}
4765 		}
4766 		mutex_exit(&ill->ill_lock);
4767 	}
4768 
4769 	/* If we already did the ptp case, then we are done */
4770 	if (ptp) {
4771 		rw_exit(&ipst->ips_ill_g_lock);
4772 		return (ALL_ZONES);
4773 	}
4774 	ptp = B_TRUE;
4775 	goto repeat;
4776 }
4777 
4778 /*
4779  * Look for an ipif that matches the specified remote address i.e. the
4780  * ipif that would receive the specified packet.
4781  * First look for directly connected interfaces and then do a recursive
4782  * IRE lookup and pick the first ipif corresponding to the source address in the
4783  * ire.
4784  * Returns: held ipif
4785  *
4786  * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4787  */
4788 ipif_t *
4789 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4790 {
4791 	ipif_t	*ipif;
4792 
4793 	ASSERT(!ill->ill_isv6);
4794 
4795 	/*
4796 	 * Someone could be changing this ipif currently or change it
4797 	 * after we return this. Thus  a few packets could use the old
4798 	 * old values. However structure updates/creates (ire, ilg, ilm etc)
4799 	 * will atomically be updated or cleaned up with the new value
4800 	 * Thus we don't need a lock to check the flags or other attrs below.
4801 	 */
4802 	mutex_enter(&ill->ill_lock);
4803 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4804 		if (IPIF_IS_CONDEMNED(ipif))
4805 			continue;
4806 		if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4807 		    ipif->ipif_zoneid != ALL_ZONES)
4808 			continue;
4809 		/* Allow the ipif to be down */
4810 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4811 			if ((ipif->ipif_pp_dst_addr == addr) ||
4812 			    (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4813 			    ipif->ipif_lcl_addr == addr)) {
4814 				ipif_refhold_locked(ipif);
4815 				mutex_exit(&ill->ill_lock);
4816 				return (ipif);
4817 			}
4818 		} else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4819 			ipif_refhold_locked(ipif);
4820 			mutex_exit(&ill->ill_lock);
4821 			return (ipif);
4822 		}
4823 	}
4824 	mutex_exit(&ill->ill_lock);
4825 	/*
4826 	 * For a remote destination it isn't possible to nail down a particular
4827 	 * ipif.
4828 	 */
4829 
4830 	/* Pick the first interface */
4831 	ipif = ipif_get_next_ipif(NULL, ill);
4832 	return (ipif);
4833 }
4834 
4835 /*
4836  * This func does not prevent refcnt from increasing. But if
4837  * the caller has taken steps to that effect, then this func
4838  * can be used to determine whether the ill has become quiescent
4839  */
4840 static boolean_t
4841 ill_is_quiescent(ill_t *ill)
4842 {
4843 	ipif_t	*ipif;
4844 
4845 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4846 
4847 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4848 		if (ipif->ipif_refcnt != 0)
4849 			return (B_FALSE);
4850 	}
4851 	if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4852 		return (B_FALSE);
4853 	}
4854 	return (B_TRUE);
4855 }
4856 
4857 boolean_t
4858 ill_is_freeable(ill_t *ill)
4859 {
4860 	ipif_t	*ipif;
4861 
4862 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4863 
4864 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4865 		if (ipif->ipif_refcnt != 0) {
4866 			return (B_FALSE);
4867 		}
4868 	}
4869 	if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4870 		return (B_FALSE);
4871 	}
4872 	return (B_TRUE);
4873 }
4874 
4875 /*
4876  * This func does not prevent refcnt from increasing. But if
4877  * the caller has taken steps to that effect, then this func
4878  * can be used to determine whether the ipif has become quiescent
4879  */
4880 static boolean_t
4881 ipif_is_quiescent(ipif_t *ipif)
4882 {
4883 	ill_t *ill;
4884 
4885 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4886 
4887 	if (ipif->ipif_refcnt != 0)
4888 		return (B_FALSE);
4889 
4890 	ill = ipif->ipif_ill;
4891 	if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4892 	    ill->ill_logical_down) {
4893 		return (B_TRUE);
4894 	}
4895 
4896 	/* This is the last ipif going down or being deleted on this ill */
4897 	if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4898 		return (B_FALSE);
4899 	}
4900 
4901 	return (B_TRUE);
4902 }
4903 
4904 /*
4905  * return true if the ipif can be destroyed: the ipif has to be quiescent
4906  * with zero references from ire/ilm to it.
4907  */
4908 static boolean_t
4909 ipif_is_freeable(ipif_t *ipif)
4910 {
4911 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4912 	ASSERT(ipif->ipif_id != 0);
4913 	return (ipif->ipif_refcnt == 0);
4914 }
4915 
4916 /*
4917  * The ipif/ill/ire has been refreled. Do the tail processing.
4918  * Determine if the ipif or ill in question has become quiescent and if so
4919  * wakeup close and/or restart any queued pending ioctl that is waiting
4920  * for the ipif_down (or ill_down)
4921  */
4922 void
4923 ipif_ill_refrele_tail(ill_t *ill)
4924 {
4925 	mblk_t	*mp;
4926 	conn_t	*connp;
4927 	ipsq_t	*ipsq;
4928 	ipxop_t	*ipx;
4929 	ipif_t	*ipif;
4930 	dl_notify_ind_t *dlindp;
4931 
4932 	ASSERT(MUTEX_HELD(&ill->ill_lock));
4933 
4934 	if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4935 		/* ip_modclose() may be waiting */
4936 		cv_broadcast(&ill->ill_cv);
4937 	}
4938 
4939 	ipsq = ill->ill_phyint->phyint_ipsq;
4940 	mutex_enter(&ipsq->ipsq_lock);
4941 	ipx = ipsq->ipsq_xop;
4942 	mutex_enter(&ipx->ipx_lock);
4943 	if (ipx->ipx_waitfor == 0)	/* no one's waiting; bail */
4944 		goto unlock;
4945 
4946 	ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4947 
4948 	ipif = ipx->ipx_pending_ipif;
4949 	if (ipif->ipif_ill != ill) 	/* wait is for another ill; bail */
4950 		goto unlock;
4951 
4952 	switch (ipx->ipx_waitfor) {
4953 	case IPIF_DOWN:
4954 		if (!ipif_is_quiescent(ipif))
4955 			goto unlock;
4956 		break;
4957 	case IPIF_FREE:
4958 		if (!ipif_is_freeable(ipif))
4959 			goto unlock;
4960 		break;
4961 	case ILL_DOWN:
4962 		if (!ill_is_quiescent(ill))
4963 			goto unlock;
4964 		break;
4965 	case ILL_FREE:
4966 		/*
4967 		 * ILL_FREE is only for loopback; normal ill teardown waits
4968 		 * synchronously in ip_modclose() without using ipx_waitfor,
4969 		 * handled by the cv_broadcast() at the top of this function.
4970 		 */
4971 		if (!ill_is_freeable(ill))
4972 			goto unlock;
4973 		break;
4974 	default:
4975 		cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4976 		    (void *)ipsq, ipx->ipx_waitfor);
4977 	}
4978 
4979 	ill_refhold_locked(ill);	/* for qwriter_ip() call below */
4980 	mutex_exit(&ipx->ipx_lock);
4981 	mp = ipsq_pending_mp_get(ipsq, &connp);
4982 	mutex_exit(&ipsq->ipsq_lock);
4983 	mutex_exit(&ill->ill_lock);
4984 
4985 	ASSERT(mp != NULL);
4986 	/*
4987 	 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4988 	 * we can only get here when the current operation decides it
4989 	 * it needs to quiesce via ipsq_pending_mp_add().
4990 	 */
4991 	switch (mp->b_datap->db_type) {
4992 	case M_PCPROTO:
4993 	case M_PROTO:
4994 		/*
4995 		 * For now, only DL_NOTIFY_IND messages can use this facility.
4996 		 */
4997 		dlindp = (dl_notify_ind_t *)mp->b_rptr;
4998 		ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4999 
5000 		switch (dlindp->dl_notification) {
5001 		case DL_NOTE_PHYS_ADDR:
5002 			qwriter_ip(ill, ill->ill_rq, mp,
5003 			    ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5004 			return;
5005 		case DL_NOTE_REPLUMB:
5006 			qwriter_ip(ill, ill->ill_rq, mp,
5007 			    ill_replumb_tail, CUR_OP, B_TRUE);
5008 			return;
5009 		default:
5010 			ASSERT(0);
5011 			ill_refrele(ill);
5012 		}
5013 		break;
5014 
5015 	case M_ERROR:
5016 	case M_HANGUP:
5017 		qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5018 		    B_TRUE);
5019 		return;
5020 
5021 	case M_IOCTL:
5022 	case M_IOCDATA:
5023 		qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5024 		    ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5025 		return;
5026 
5027 	default:
5028 		cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5029 		    "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5030 	}
5031 	return;
5032 unlock:
5033 	mutex_exit(&ipsq->ipsq_lock);
5034 	mutex_exit(&ipx->ipx_lock);
5035 	mutex_exit(&ill->ill_lock);
5036 }
5037 
5038 #ifdef DEBUG
5039 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5040 static void
5041 th_trace_rrecord(th_trace_t *th_trace)
5042 {
5043 	tr_buf_t *tr_buf;
5044 	uint_t lastref;
5045 
5046 	lastref = th_trace->th_trace_lastref;
5047 	lastref++;
5048 	if (lastref == TR_BUF_MAX)
5049 		lastref = 0;
5050 	th_trace->th_trace_lastref = lastref;
5051 	tr_buf = &th_trace->th_trbuf[lastref];
5052 	tr_buf->tr_time = ddi_get_lbolt();
5053 	tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5054 }
5055 
5056 static void
5057 th_trace_free(void *value)
5058 {
5059 	th_trace_t *th_trace = value;
5060 
5061 	ASSERT(th_trace->th_refcnt == 0);
5062 	kmem_free(th_trace, sizeof (*th_trace));
5063 }
5064 
5065 /*
5066  * Find or create the per-thread hash table used to track object references.
5067  * The ipst argument is NULL if we shouldn't allocate.
5068  *
5069  * Accesses per-thread data, so there's no need to lock here.
5070  */
5071 static mod_hash_t *
5072 th_trace_gethash(ip_stack_t *ipst)
5073 {
5074 	th_hash_t *thh;
5075 
5076 	if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5077 		mod_hash_t *mh;
5078 		char name[256];
5079 		size_t objsize, rshift;
5080 		int retv;
5081 
5082 		if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5083 			return (NULL);
5084 		(void) snprintf(name, sizeof (name), "th_trace_%p",
5085 		    (void *)curthread);
5086 
5087 		/*
5088 		 * We use mod_hash_create_extended here rather than the more
5089 		 * obvious mod_hash_create_ptrhash because the latter has a
5090 		 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5091 		 * block.
5092 		 */
5093 		objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5094 		    MAX(sizeof (ire_t), sizeof (ncec_t)));
5095 		rshift = highbit(objsize);
5096 		mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5097 		    th_trace_free, mod_hash_byptr, (void *)rshift,
5098 		    mod_hash_ptrkey_cmp, KM_NOSLEEP);
5099 		if (mh == NULL) {
5100 			kmem_free(thh, sizeof (*thh));
5101 			return (NULL);
5102 		}
5103 		thh->thh_hash = mh;
5104 		thh->thh_ipst = ipst;
5105 		/*
5106 		 * We trace ills, ipifs, ires, and nces.  All of these are
5107 		 * per-IP-stack, so the lock on the thread list is as well.
5108 		 */
5109 		rw_enter(&ip_thread_rwlock, RW_WRITER);
5110 		list_insert_tail(&ip_thread_list, thh);
5111 		rw_exit(&ip_thread_rwlock);
5112 		retv = tsd_set(ip_thread_data, thh);
5113 		ASSERT(retv == 0);
5114 	}
5115 	return (thh != NULL ? thh->thh_hash : NULL);
5116 }
5117 
5118 boolean_t
5119 th_trace_ref(const void *obj, ip_stack_t *ipst)
5120 {
5121 	th_trace_t *th_trace;
5122 	mod_hash_t *mh;
5123 	mod_hash_val_t val;
5124 
5125 	if ((mh = th_trace_gethash(ipst)) == NULL)
5126 		return (B_FALSE);
5127 
5128 	/*
5129 	 * Attempt to locate the trace buffer for this obj and thread.
5130 	 * If it does not exist, then allocate a new trace buffer and
5131 	 * insert into the hash.
5132 	 */
5133 	if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5134 		th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5135 		if (th_trace == NULL)
5136 			return (B_FALSE);
5137 
5138 		th_trace->th_id = curthread;
5139 		if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5140 		    (mod_hash_val_t)th_trace) != 0) {
5141 			kmem_free(th_trace, sizeof (th_trace_t));
5142 			return (B_FALSE);
5143 		}
5144 	} else {
5145 		th_trace = (th_trace_t *)val;
5146 	}
5147 
5148 	ASSERT(th_trace->th_refcnt >= 0 &&
5149 	    th_trace->th_refcnt < TR_BUF_MAX - 1);
5150 
5151 	th_trace->th_refcnt++;
5152 	th_trace_rrecord(th_trace);
5153 	return (B_TRUE);
5154 }
5155 
5156 /*
5157  * For the purpose of tracing a reference release, we assume that global
5158  * tracing is always on and that the same thread initiated the reference hold
5159  * is releasing.
5160  */
5161 void
5162 th_trace_unref(const void *obj)
5163 {
5164 	int retv;
5165 	mod_hash_t *mh;
5166 	th_trace_t *th_trace;
5167 	mod_hash_val_t val;
5168 
5169 	mh = th_trace_gethash(NULL);
5170 	retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5171 	ASSERT(retv == 0);
5172 	th_trace = (th_trace_t *)val;
5173 
5174 	ASSERT(th_trace->th_refcnt > 0);
5175 	th_trace->th_refcnt--;
5176 	th_trace_rrecord(th_trace);
5177 }
5178 
5179 /*
5180  * If tracing has been disabled, then we assume that the reference counts are
5181  * now useless, and we clear them out before destroying the entries.
5182  */
5183 void
5184 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5185 {
5186 	th_hash_t	*thh;
5187 	mod_hash_t	*mh;
5188 	mod_hash_val_t	val;
5189 	th_trace_t	*th_trace;
5190 	int		retv;
5191 
5192 	rw_enter(&ip_thread_rwlock, RW_READER);
5193 	for (thh = list_head(&ip_thread_list); thh != NULL;
5194 	    thh = list_next(&ip_thread_list, thh)) {
5195 		if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5196 		    &val) == 0) {
5197 			th_trace = (th_trace_t *)val;
5198 			if (trace_disable)
5199 				th_trace->th_refcnt = 0;
5200 			retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5201 			ASSERT(retv == 0);
5202 		}
5203 	}
5204 	rw_exit(&ip_thread_rwlock);
5205 }
5206 
5207 void
5208 ipif_trace_ref(ipif_t *ipif)
5209 {
5210 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5211 
5212 	if (ipif->ipif_trace_disable)
5213 		return;
5214 
5215 	if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5216 		ipif->ipif_trace_disable = B_TRUE;
5217 		ipif_trace_cleanup(ipif);
5218 	}
5219 }
5220 
5221 void
5222 ipif_untrace_ref(ipif_t *ipif)
5223 {
5224 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5225 
5226 	if (!ipif->ipif_trace_disable)
5227 		th_trace_unref(ipif);
5228 }
5229 
5230 void
5231 ill_trace_ref(ill_t *ill)
5232 {
5233 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5234 
5235 	if (ill->ill_trace_disable)
5236 		return;
5237 
5238 	if (!th_trace_ref(ill, ill->ill_ipst)) {
5239 		ill->ill_trace_disable = B_TRUE;
5240 		ill_trace_cleanup(ill);
5241 	}
5242 }
5243 
5244 void
5245 ill_untrace_ref(ill_t *ill)
5246 {
5247 	ASSERT(MUTEX_HELD(&ill->ill_lock));
5248 
5249 	if (!ill->ill_trace_disable)
5250 		th_trace_unref(ill);
5251 }
5252 
5253 /*
5254  * Called when ipif is unplumbed or when memory alloc fails.  Note that on
5255  * failure, ipif_trace_disable is set.
5256  */
5257 static void
5258 ipif_trace_cleanup(const ipif_t *ipif)
5259 {
5260 	th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5261 }
5262 
5263 /*
5264  * Called when ill is unplumbed or when memory alloc fails.  Note that on
5265  * failure, ill_trace_disable is set.
5266  */
5267 static void
5268 ill_trace_cleanup(const ill_t *ill)
5269 {
5270 	th_trace_cleanup(ill, ill->ill_trace_disable);
5271 }
5272 #endif /* DEBUG */
5273 
5274 void
5275 ipif_refhold_locked(ipif_t *ipif)
5276 {
5277 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5278 	ipif->ipif_refcnt++;
5279 	IPIF_TRACE_REF(ipif);
5280 }
5281 
5282 void
5283 ipif_refhold(ipif_t *ipif)
5284 {
5285 	ill_t	*ill;
5286 
5287 	ill = ipif->ipif_ill;
5288 	mutex_enter(&ill->ill_lock);
5289 	ipif->ipif_refcnt++;
5290 	IPIF_TRACE_REF(ipif);
5291 	mutex_exit(&ill->ill_lock);
5292 }
5293 
5294 /*
5295  * Must not be called while holding any locks. Otherwise if this is
5296  * the last reference to be released there is a chance of recursive mutex
5297  * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5298  * to restart an ioctl.
5299  */
5300 void
5301 ipif_refrele(ipif_t *ipif)
5302 {
5303 	ill_t	*ill;
5304 
5305 	ill = ipif->ipif_ill;
5306 
5307 	mutex_enter(&ill->ill_lock);
5308 	ASSERT(ipif->ipif_refcnt != 0);
5309 	ipif->ipif_refcnt--;
5310 	IPIF_UNTRACE_REF(ipif);
5311 	if (ipif->ipif_refcnt != 0) {
5312 		mutex_exit(&ill->ill_lock);
5313 		return;
5314 	}
5315 
5316 	/* Drops the ill_lock */
5317 	ipif_ill_refrele_tail(ill);
5318 }
5319 
5320 ipif_t *
5321 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5322 {
5323 	ipif_t	*ipif;
5324 
5325 	mutex_enter(&ill->ill_lock);
5326 	for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5327 	    ipif != NULL; ipif = ipif->ipif_next) {
5328 		if (IPIF_IS_CONDEMNED(ipif))
5329 			continue;
5330 		ipif_refhold_locked(ipif);
5331 		mutex_exit(&ill->ill_lock);
5332 		return (ipif);
5333 	}
5334 	mutex_exit(&ill->ill_lock);
5335 	return (NULL);
5336 }
5337 
5338 /*
5339  * TODO: make this table extendible at run time
5340  * Return a pointer to the mac type info for 'mac_type'
5341  */
5342 static ip_m_t *
5343 ip_m_lookup(t_uscalar_t mac_type)
5344 {
5345 	ip_m_t	*ipm;
5346 
5347 	for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5348 		if (ipm->ip_m_mac_type == mac_type)
5349 			return (ipm);
5350 	return (NULL);
5351 }
5352 
5353 /*
5354  * Make a link layer address from the multicast IP address *addr.
5355  * To form the link layer address, invoke the ip_m_v*mapping function
5356  * associated with the link-layer type.
5357  */
5358 void
5359 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5360 {
5361 	ip_m_t *ipm;
5362 
5363 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
5364 		return;
5365 
5366 	ASSERT(addr != NULL);
5367 
5368 	ipm = ip_m_lookup(ill->ill_mactype);
5369 	if (ipm == NULL ||
5370 	    (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5371 	    (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5372 		ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5373 		    ill->ill_name, ill->ill_mactype));
5374 		return;
5375 	}
5376 	if (ill->ill_isv6)
5377 		(*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5378 	else
5379 		(*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5380 }
5381 
5382 /*
5383  * ip_rt_add is called to add an IPv4 route to the forwarding table.
5384  * ill is passed in to associate it with the correct interface.
5385  * If ire_arg is set, then we return the held IRE in that location.
5386  */
5387 int
5388 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5389     ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5390     boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5391 {
5392 	ire_t	*ire, *nire;
5393 	ire_t	*gw_ire = NULL;
5394 	ipif_t	*ipif = NULL;
5395 	uint_t	type;
5396 	int	match_flags = MATCH_IRE_TYPE;
5397 	tsol_gc_t *gc = NULL;
5398 	tsol_gcgrp_t *gcgrp = NULL;
5399 	boolean_t gcgrp_xtraref = B_FALSE;
5400 	boolean_t cgtp_broadcast;
5401 
5402 	ip1dbg(("ip_rt_add:"));
5403 
5404 	if (ire_arg != NULL)
5405 		*ire_arg = NULL;
5406 
5407 	/*
5408 	 * If this is the case of RTF_HOST being set, then we set the netmask
5409 	 * to all ones (regardless if one was supplied).
5410 	 */
5411 	if (flags & RTF_HOST)
5412 		mask = IP_HOST_MASK;
5413 
5414 	/*
5415 	 * Prevent routes with a zero gateway from being created (since
5416 	 * interfaces can currently be plumbed and brought up no assigned
5417 	 * address).
5418 	 */
5419 	if (gw_addr == 0)
5420 		return (ENETUNREACH);
5421 	/*
5422 	 * Get the ipif, if any, corresponding to the gw_addr
5423 	 * If -ifp was specified we restrict ourselves to the ill, otherwise
5424 	 * we match on the gatway and destination to handle unnumbered pt-pt
5425 	 * interfaces.
5426 	 */
5427 	if (ill != NULL)
5428 		ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5429 	else
5430 		ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5431 	if (ipif != NULL) {
5432 		if (IS_VNI(ipif->ipif_ill)) {
5433 			ipif_refrele(ipif);
5434 			return (EINVAL);
5435 		}
5436 	}
5437 
5438 	/*
5439 	 * GateD will attempt to create routes with a loopback interface
5440 	 * address as the gateway and with RTF_GATEWAY set.  We allow
5441 	 * these routes to be added, but create them as interface routes
5442 	 * since the gateway is an interface address.
5443 	 */
5444 	if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5445 		flags &= ~RTF_GATEWAY;
5446 		if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5447 		    mask == IP_HOST_MASK) {
5448 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5449 			    NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5450 			    NULL);
5451 			if (ire != NULL) {
5452 				ire_refrele(ire);
5453 				ipif_refrele(ipif);
5454 				return (EEXIST);
5455 			}
5456 			ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5457 			    "for 0x%x\n", (void *)ipif,
5458 			    ipif->ipif_ire_type,
5459 			    ntohl(ipif->ipif_lcl_addr)));
5460 			ire = ire_create(
5461 			    (uchar_t *)&dst_addr,	/* dest address */
5462 			    (uchar_t *)&mask,		/* mask */
5463 			    NULL,			/* no gateway */
5464 			    ipif->ipif_ire_type,	/* LOOPBACK */
5465 			    ipif->ipif_ill,
5466 			    zoneid,
5467 			    (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5468 			    NULL,
5469 			    ipst);
5470 
5471 			if (ire == NULL) {
5472 				ipif_refrele(ipif);
5473 				return (ENOMEM);
5474 			}
5475 			/* src address assigned by the caller? */
5476 			if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5477 				ire->ire_setsrc_addr = src_addr;
5478 
5479 			nire = ire_add(ire);
5480 			if (nire == NULL) {
5481 				/*
5482 				 * In the result of failure, ire_add() will have
5483 				 * already deleted the ire in question, so there
5484 				 * is no need to do that here.
5485 				 */
5486 				ipif_refrele(ipif);
5487 				return (ENOMEM);
5488 			}
5489 			/*
5490 			 * Check if it was a duplicate entry. This handles
5491 			 * the case of two racing route adds for the same route
5492 			 */
5493 			if (nire != ire) {
5494 				ASSERT(nire->ire_identical_ref > 1);
5495 				ire_delete(nire);
5496 				ire_refrele(nire);
5497 				ipif_refrele(ipif);
5498 				return (EEXIST);
5499 			}
5500 			ire = nire;
5501 			goto save_ire;
5502 		}
5503 	}
5504 
5505 	/*
5506 	 * The routes for multicast with CGTP are quite special in that
5507 	 * the gateway is the local interface address, yet RTF_GATEWAY
5508 	 * is set. We turn off RTF_GATEWAY to provide compatibility with
5509 	 * this undocumented and unusual use of multicast routes.
5510 	 */
5511 	if ((flags & RTF_MULTIRT) && ipif != NULL)
5512 		flags &= ~RTF_GATEWAY;
5513 
5514 	/*
5515 	 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5516 	 * and the gateway address provided is one of the system's interface
5517 	 * addresses.  By using the routing socket interface and supplying an
5518 	 * RTA_IFP sockaddr with an interface index, an alternate method of
5519 	 * specifying an interface route to be created is available which uses
5520 	 * the interface index that specifies the outgoing interface rather than
5521 	 * the address of an outgoing interface (which may not be able to
5522 	 * uniquely identify an interface).  When coupled with the RTF_GATEWAY
5523 	 * flag, routes can be specified which not only specify the next-hop to
5524 	 * be used when routing to a certain prefix, but also which outgoing
5525 	 * interface should be used.
5526 	 *
5527 	 * Previously, interfaces would have unique addresses assigned to them
5528 	 * and so the address assigned to a particular interface could be used
5529 	 * to identify a particular interface.  One exception to this was the
5530 	 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5531 	 *
5532 	 * With the advent of IPv6 and its link-local addresses, this
5533 	 * restriction was relaxed and interfaces could share addresses between
5534 	 * themselves.  In fact, typically all of the link-local interfaces on
5535 	 * an IPv6 node or router will have the same link-local address.  In
5536 	 * order to differentiate between these interfaces, the use of an
5537 	 * interface index is necessary and this index can be carried inside a
5538 	 * RTA_IFP sockaddr (which is actually a sockaddr_dl).  One restriction
5539 	 * of using the interface index, however, is that all of the ipif's that
5540 	 * are part of an ill have the same index and so the RTA_IFP sockaddr
5541 	 * cannot be used to differentiate between ipif's (or logical
5542 	 * interfaces) that belong to the same ill (physical interface).
5543 	 *
5544 	 * For example, in the following case involving IPv4 interfaces and
5545 	 * logical interfaces
5546 	 *
5547 	 *	192.0.2.32	255.255.255.224	192.0.2.33	U	if0
5548 	 *	192.0.2.32	255.255.255.224	192.0.2.34	U	if0
5549 	 *	192.0.2.32	255.255.255.224	192.0.2.35	U	if0
5550 	 *
5551 	 * the ipif's corresponding to each of these interface routes can be
5552 	 * uniquely identified by the "gateway" (actually interface address).
5553 	 *
5554 	 * In this case involving multiple IPv6 default routes to a particular
5555 	 * link-local gateway, the use of RTA_IFP is necessary to specify which
5556 	 * default route is of interest:
5557 	 *
5558 	 *	default		fe80::123:4567:89ab:cdef	U	if0
5559 	 *	default		fe80::123:4567:89ab:cdef	U	if1
5560 	 */
5561 
5562 	/* RTF_GATEWAY not set */
5563 	if (!(flags & RTF_GATEWAY)) {
5564 		if (sp != NULL) {
5565 			ip2dbg(("ip_rt_add: gateway security attributes "
5566 			    "cannot be set with interface route\n"));
5567 			if (ipif != NULL)
5568 				ipif_refrele(ipif);
5569 			return (EINVAL);
5570 		}
5571 
5572 		/*
5573 		 * Whether or not ill (RTA_IFP) is set, we require that
5574 		 * the gateway is one of our local addresses.
5575 		 */
5576 		if (ipif == NULL)
5577 			return (ENETUNREACH);
5578 
5579 		/*
5580 		 * We use MATCH_IRE_ILL here. If the caller specified an
5581 		 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5582 		 * we use the ill derived from the gateway address.
5583 		 * We can always match the gateway address since we record it
5584 		 * in ire_gateway_addr.
5585 		 * We don't allow RTA_IFP to specify a different ill than the
5586 		 * one matching the ipif to make sure we can delete the route.
5587 		 */
5588 		match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5589 		if (ill == NULL) {
5590 			ill = ipif->ipif_ill;
5591 		} else if (ill != ipif->ipif_ill) {
5592 			ipif_refrele(ipif);
5593 			return (EINVAL);
5594 		}
5595 
5596 		/*
5597 		 * We check for an existing entry at this point.
5598 		 *
5599 		 * Since a netmask isn't passed in via the ioctl interface
5600 		 * (SIOCADDRT), we don't check for a matching netmask in that
5601 		 * case.
5602 		 */
5603 		if (!ioctl_msg)
5604 			match_flags |= MATCH_IRE_MASK;
5605 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5606 		    IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5607 		    NULL);
5608 		if (ire != NULL) {
5609 			ire_refrele(ire);
5610 			ipif_refrele(ipif);
5611 			return (EEXIST);
5612 		}
5613 
5614 		/*
5615 		 * Create a copy of the IRE_LOOPBACK, IRE_IF_NORESOLVER or
5616 		 * IRE_IF_RESOLVER with the modified address, netmask, and
5617 		 * gateway.
5618 		 */
5619 		ire = ire_create(
5620 		    (uchar_t *)&dst_addr,
5621 		    (uint8_t *)&mask,
5622 		    (uint8_t *)&gw_addr,
5623 		    ill->ill_net_type,
5624 		    ill,
5625 		    zoneid,
5626 		    flags,
5627 		    NULL,
5628 		    ipst);
5629 		if (ire == NULL) {
5630 			ipif_refrele(ipif);
5631 			return (ENOMEM);
5632 		}
5633 
5634 		/*
5635 		 * Some software (for example, GateD and Sun Cluster) attempts
5636 		 * to create (what amount to) IRE_PREFIX routes with the
5637 		 * loopback address as the gateway.  This is primarily done to
5638 		 * set up prefixes with the RTF_REJECT flag set (for example,
5639 		 * when generating aggregate routes.)
5640 		 *
5641 		 * If the IRE type (as defined by ill->ill_net_type) is
5642 		 * IRE_LOOPBACK, then we map the request into a
5643 		 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5644 		 * these interface routes, by definition, can only be that.
5645 		 *
5646 		 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5647 		 * routine, but rather using ire_create() directly.
5648 		 *
5649 		 */
5650 		if (ill->ill_net_type == IRE_LOOPBACK) {
5651 			ire->ire_type = IRE_IF_NORESOLVER;
5652 			ire->ire_flags |= RTF_BLACKHOLE;
5653 		}
5654 
5655 		/* src address assigned by the caller? */
5656 		if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5657 			ire->ire_setsrc_addr = src_addr;
5658 
5659 		nire = ire_add(ire);
5660 		if (nire == NULL) {
5661 			/*
5662 			 * In the result of failure, ire_add() will have
5663 			 * already deleted the ire in question, so there
5664 			 * is no need to do that here.
5665 			 */
5666 			ipif_refrele(ipif);
5667 			return (ENOMEM);
5668 		}
5669 		/*
5670 		 * Check if it was a duplicate entry. This handles
5671 		 * the case of two racing route adds for the same route
5672 		 */
5673 		if (nire != ire) {
5674 			ire_delete(nire);
5675 			ire_refrele(nire);
5676 			ipif_refrele(ipif);
5677 			return (EEXIST);
5678 		}
5679 		ire = nire;
5680 		goto save_ire;
5681 	}
5682 
5683 	/*
5684 	 * Get an interface IRE for the specified gateway.
5685 	 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5686 	 * gateway, it is currently unreachable and we fail the request
5687 	 * accordingly.
5688 	 * If RTA_IFP was specified we look on that particular ill.
5689 	 */
5690 	if (ill != NULL)
5691 		match_flags |= MATCH_IRE_ILL;
5692 
5693 	/* Check whether the gateway is reachable. */
5694 again:
5695 	type = IRE_INTERFACE;
5696 	if (flags & RTF_INDIRECT)
5697 		type |= IRE_OFFLINK;
5698 
5699 	gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5700 	    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5701 	if (gw_ire == NULL) {
5702 		/*
5703 		 * With IPMP, we allow host routes to influence in.mpathd's
5704 		 * target selection.  However, if the test addresses are on
5705 		 * their own network, the above lookup will fail since the
5706 		 * underlying IRE_INTERFACEs are marked hidden.  So allow
5707 		 * hidden test IREs to be found and try again.
5708 		 */
5709 		if (!(match_flags & MATCH_IRE_TESTHIDDEN))  {
5710 			match_flags |= MATCH_IRE_TESTHIDDEN;
5711 			goto again;
5712 		}
5713 
5714 		if (ipif != NULL)
5715 			ipif_refrele(ipif);
5716 		return (ENETUNREACH);
5717 	}
5718 
5719 	/*
5720 	 * We create one of three types of IREs as a result of this request
5721 	 * based on the netmask.  A netmask of all ones (which is automatically
5722 	 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5723 	 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5724 	 * created.  Otherwise, an IRE_PREFIX route is created for the
5725 	 * destination prefix.
5726 	 */
5727 	if (mask == IP_HOST_MASK)
5728 		type = IRE_HOST;
5729 	else if (mask == 0)
5730 		type = IRE_DEFAULT;
5731 	else
5732 		type = IRE_PREFIX;
5733 
5734 	/* check for a duplicate entry */
5735 	ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5736 	    ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5737 	    0, ipst, NULL);
5738 	if (ire != NULL) {
5739 		if (ipif != NULL)
5740 			ipif_refrele(ipif);
5741 		ire_refrele(gw_ire);
5742 		ire_refrele(ire);
5743 		return (EEXIST);
5744 	}
5745 
5746 	/* Security attribute exists */
5747 	if (sp != NULL) {
5748 		tsol_gcgrp_addr_t ga;
5749 
5750 		/* find or create the gateway credentials group */
5751 		ga.ga_af = AF_INET;
5752 		IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5753 
5754 		/* we hold reference to it upon success */
5755 		gcgrp = gcgrp_lookup(&ga, B_TRUE);
5756 		if (gcgrp == NULL) {
5757 			if (ipif != NULL)
5758 				ipif_refrele(ipif);
5759 			ire_refrele(gw_ire);
5760 			return (ENOMEM);
5761 		}
5762 
5763 		/*
5764 		 * Create and add the security attribute to the group; a
5765 		 * reference to the group is made upon allocating a new
5766 		 * entry successfully.  If it finds an already-existing
5767 		 * entry for the security attribute in the group, it simply
5768 		 * returns it and no new reference is made to the group.
5769 		 */
5770 		gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5771 		if (gc == NULL) {
5772 			if (ipif != NULL)
5773 				ipif_refrele(ipif);
5774 			/* release reference held by gcgrp_lookup */
5775 			GCGRP_REFRELE(gcgrp);
5776 			ire_refrele(gw_ire);
5777 			return (ENOMEM);
5778 		}
5779 	}
5780 
5781 	/* Create the IRE. */
5782 	ire = ire_create(
5783 	    (uchar_t *)&dst_addr,		/* dest address */
5784 	    (uchar_t *)&mask,			/* mask */
5785 	    (uchar_t *)&gw_addr,		/* gateway address */
5786 	    (ushort_t)type,			/* IRE type */
5787 	    ill,
5788 	    zoneid,
5789 	    flags,
5790 	    gc,					/* security attribute */
5791 	    ipst);
5792 
5793 	/*
5794 	 * The ire holds a reference to the 'gc' and the 'gc' holds a
5795 	 * reference to the 'gcgrp'. We can now release the extra reference
5796 	 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5797 	 */
5798 	if (gcgrp_xtraref)
5799 		GCGRP_REFRELE(gcgrp);
5800 	if (ire == NULL) {
5801 		if (gc != NULL)
5802 			GC_REFRELE(gc);
5803 		if (ipif != NULL)
5804 			ipif_refrele(ipif);
5805 		ire_refrele(gw_ire);
5806 		return (ENOMEM);
5807 	}
5808 
5809 	/* Before we add, check if an extra CGTP broadcast is needed */
5810 	cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5811 	    ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5812 
5813 	/* src address assigned by the caller? */
5814 	if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5815 		ire->ire_setsrc_addr = src_addr;
5816 
5817 	/*
5818 	 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5819 	 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5820 	 */
5821 
5822 	/* Add the new IRE. */
5823 	nire = ire_add(ire);
5824 	if (nire == NULL) {
5825 		/*
5826 		 * In the result of failure, ire_add() will have
5827 		 * already deleted the ire in question, so there
5828 		 * is no need to do that here.
5829 		 */
5830 		if (ipif != NULL)
5831 			ipif_refrele(ipif);
5832 		ire_refrele(gw_ire);
5833 		return (ENOMEM);
5834 	}
5835 	/*
5836 	 * Check if it was a duplicate entry. This handles
5837 	 * the case of two racing route adds for the same route
5838 	 */
5839 	if (nire != ire) {
5840 		ire_delete(nire);
5841 		ire_refrele(nire);
5842 		if (ipif != NULL)
5843 			ipif_refrele(ipif);
5844 		ire_refrele(gw_ire);
5845 		return (EEXIST);
5846 	}
5847 	ire = nire;
5848 
5849 	if (flags & RTF_MULTIRT) {
5850 		/*
5851 		 * Invoke the CGTP (multirouting) filtering module
5852 		 * to add the dst address in the filtering database.
5853 		 * Replicated inbound packets coming from that address
5854 		 * will be filtered to discard the duplicates.
5855 		 * It is not necessary to call the CGTP filter hook
5856 		 * when the dst address is a broadcast or multicast,
5857 		 * because an IP source address cannot be a broadcast
5858 		 * or a multicast.
5859 		 */
5860 		if (cgtp_broadcast) {
5861 			ip_cgtp_bcast_add(ire, ipst);
5862 			goto save_ire;
5863 		}
5864 		if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5865 		    !CLASSD(ire->ire_addr)) {
5866 			int res;
5867 			ipif_t *src_ipif;
5868 
5869 			/* Find the source address corresponding to gw_ire */
5870 			src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5871 			    NULL, zoneid, ipst);
5872 			if (src_ipif != NULL) {
5873 				res = ipst->ips_ip_cgtp_filter_ops->
5874 				    cfo_add_dest_v4(
5875 				    ipst->ips_netstack->netstack_stackid,
5876 				    ire->ire_addr,
5877 				    ire->ire_gateway_addr,
5878 				    ire->ire_setsrc_addr,
5879 				    src_ipif->ipif_lcl_addr);
5880 				ipif_refrele(src_ipif);
5881 			} else {
5882 				res = EADDRNOTAVAIL;
5883 			}
5884 			if (res != 0) {
5885 				if (ipif != NULL)
5886 					ipif_refrele(ipif);
5887 				ire_refrele(gw_ire);
5888 				ire_delete(ire);
5889 				ire_refrele(ire);	/* Held in ire_add */
5890 				return (res);
5891 			}
5892 		}
5893 	}
5894 
5895 save_ire:
5896 	if (gw_ire != NULL) {
5897 		ire_refrele(gw_ire);
5898 		gw_ire = NULL;
5899 	}
5900 	if (ill != NULL) {
5901 		/*
5902 		 * Save enough information so that we can recreate the IRE if
5903 		 * the interface goes down and then up.  The metrics associated
5904 		 * with the route will be saved as well when rts_setmetrics() is
5905 		 * called after the IRE has been created.  In the case where
5906 		 * memory cannot be allocated, none of this information will be
5907 		 * saved.
5908 		 */
5909 		ill_save_ire(ill, ire);
5910 	}
5911 	if (ioctl_msg)
5912 		ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5913 	if (ire_arg != NULL) {
5914 		/*
5915 		 * Store the ire that was successfully added into where ire_arg
5916 		 * points to so that callers don't have to look it up
5917 		 * themselves (but they are responsible for ire_refrele()ing
5918 		 * the ire when they are finished with it).
5919 		 */
5920 		*ire_arg = ire;
5921 	} else {
5922 		ire_refrele(ire);		/* Held in ire_add */
5923 	}
5924 	if (ipif != NULL)
5925 		ipif_refrele(ipif);
5926 	return (0);
5927 }
5928 
5929 /*
5930  * ip_rt_delete is called to delete an IPv4 route.
5931  * ill is passed in to associate it with the correct interface.
5932  */
5933 /* ARGSUSED4 */
5934 int
5935 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5936     uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5937     ip_stack_t *ipst, zoneid_t zoneid)
5938 {
5939 	ire_t	*ire = NULL;
5940 	ipif_t	*ipif;
5941 	uint_t	type;
5942 	uint_t	match_flags = MATCH_IRE_TYPE;
5943 	int	err = 0;
5944 
5945 	ip1dbg(("ip_rt_delete:"));
5946 	/*
5947 	 * If this is the case of RTF_HOST being set, then we set the netmask
5948 	 * to all ones.  Otherwise, we use the netmask if one was supplied.
5949 	 */
5950 	if (flags & RTF_HOST) {
5951 		mask = IP_HOST_MASK;
5952 		match_flags |= MATCH_IRE_MASK;
5953 	} else if (rtm_addrs & RTA_NETMASK) {
5954 		match_flags |= MATCH_IRE_MASK;
5955 	}
5956 
5957 	/*
5958 	 * Note that RTF_GATEWAY is never set on a delete, therefore
5959 	 * we check if the gateway address is one of our interfaces first,
5960 	 * and fall back on RTF_GATEWAY routes.
5961 	 *
5962 	 * This makes it possible to delete an original
5963 	 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
5964 	 * However, we have RTF_KERNEL set on the ones created by ipif_up
5965 	 * and those can not be deleted here.
5966 	 *
5967 	 * We use MATCH_IRE_ILL if we know the interface. If the caller
5968 	 * specified an interface (from the RTA_IFP sockaddr) we use it,
5969 	 * otherwise we use the ill derived from the gateway address.
5970 	 * We can always match the gateway address since we record it
5971 	 * in ire_gateway_addr.
5972 	 *
5973 	 * For more detail on specifying routes by gateway address and by
5974 	 * interface index, see the comments in ip_rt_add().
5975 	 */
5976 	ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5977 	if (ipif != NULL) {
5978 		ill_t	*ill_match;
5979 
5980 		if (ill != NULL)
5981 			ill_match = ill;
5982 		else
5983 			ill_match = ipif->ipif_ill;
5984 
5985 		match_flags |= MATCH_IRE_ILL;
5986 		if (ipif->ipif_ire_type == IRE_LOOPBACK) {
5987 			ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5988 			    ill_match, ALL_ZONES, NULL, match_flags, 0, ipst,
5989 			    NULL);
5990 		}
5991 		if (ire == NULL) {
5992 			match_flags |= MATCH_IRE_GW;
5993 			ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5994 			    IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
5995 			    match_flags, 0, ipst, NULL);
5996 		}
5997 		/* Avoid deleting routes created by kernel from an ipif */
5998 		if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
5999 			ire_refrele(ire);
6000 			ire = NULL;
6001 		}
6002 
6003 		/* Restore in case we didn't find a match */
6004 		match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6005 	}
6006 
6007 	if (ire == NULL) {
6008 		/*
6009 		 * At this point, the gateway address is not one of our own
6010 		 * addresses or a matching interface route was not found.  We
6011 		 * set the IRE type to lookup based on whether
6012 		 * this is a host route, a default route or just a prefix.
6013 		 *
6014 		 * If an ill was passed in, then the lookup is based on an
6015 		 * interface index so MATCH_IRE_ILL is added to match_flags.
6016 		 */
6017 		match_flags |= MATCH_IRE_GW;
6018 		if (ill != NULL)
6019 			match_flags |= MATCH_IRE_ILL;
6020 		if (mask == IP_HOST_MASK)
6021 			type = IRE_HOST;
6022 		else if (mask == 0)
6023 			type = IRE_DEFAULT;
6024 		else
6025 			type = IRE_PREFIX;
6026 		ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6027 		    ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6028 	}
6029 
6030 	if (ipif != NULL) {
6031 		ipif_refrele(ipif);
6032 		ipif = NULL;
6033 	}
6034 
6035 	if (ire == NULL)
6036 		return (ESRCH);
6037 
6038 	if (ire->ire_flags & RTF_MULTIRT) {
6039 		/*
6040 		 * Invoke the CGTP (multirouting) filtering module
6041 		 * to remove the dst address from the filtering database.
6042 		 * Packets coming from that address will no longer be
6043 		 * filtered to remove duplicates.
6044 		 */
6045 		if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6046 			err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6047 			    ipst->ips_netstack->netstack_stackid,
6048 			    ire->ire_addr, ire->ire_gateway_addr);
6049 		}
6050 		ip_cgtp_bcast_delete(ire, ipst);
6051 	}
6052 
6053 	ill = ire->ire_ill;
6054 	if (ill != NULL)
6055 		ill_remove_saved_ire(ill, ire);
6056 	if (ioctl_msg)
6057 		ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6058 	ire_delete(ire);
6059 	ire_refrele(ire);
6060 	return (err);
6061 }
6062 
6063 /*
6064  * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6065  */
6066 /* ARGSUSED */
6067 int
6068 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6069     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6070 {
6071 	ipaddr_t dst_addr;
6072 	ipaddr_t gw_addr;
6073 	ipaddr_t mask;
6074 	int error = 0;
6075 	mblk_t *mp1;
6076 	struct rtentry *rt;
6077 	ipif_t *ipif = NULL;
6078 	ip_stack_t	*ipst;
6079 
6080 	ASSERT(q->q_next == NULL);
6081 	ipst = CONNQ_TO_IPST(q);
6082 
6083 	ip1dbg(("ip_siocaddrt:"));
6084 	/* Existence of mp1 verified in ip_wput_nondata */
6085 	mp1 = mp->b_cont->b_cont;
6086 	rt = (struct rtentry *)mp1->b_rptr;
6087 
6088 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6089 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6090 
6091 	/*
6092 	 * If the RTF_HOST flag is on, this is a request to assign a gateway
6093 	 * to a particular host address.  In this case, we set the netmask to
6094 	 * all ones for the particular destination address.  Otherwise,
6095 	 * determine the netmask to be used based on dst_addr and the interfaces
6096 	 * in use.
6097 	 */
6098 	if (rt->rt_flags & RTF_HOST) {
6099 		mask = IP_HOST_MASK;
6100 	} else {
6101 		/*
6102 		 * Note that ip_subnet_mask returns a zero mask in the case of
6103 		 * default (an all-zeroes address).
6104 		 */
6105 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6106 	}
6107 
6108 	error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6109 	    B_TRUE, NULL, ipst, ALL_ZONES);
6110 	if (ipif != NULL)
6111 		ipif_refrele(ipif);
6112 	return (error);
6113 }
6114 
6115 /*
6116  * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6117  */
6118 /* ARGSUSED */
6119 int
6120 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6121     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6122 {
6123 	ipaddr_t dst_addr;
6124 	ipaddr_t gw_addr;
6125 	ipaddr_t mask;
6126 	int error;
6127 	mblk_t *mp1;
6128 	struct rtentry *rt;
6129 	ipif_t *ipif = NULL;
6130 	ip_stack_t	*ipst;
6131 
6132 	ASSERT(q->q_next == NULL);
6133 	ipst = CONNQ_TO_IPST(q);
6134 
6135 	ip1dbg(("ip_siocdelrt:"));
6136 	/* Existence of mp1 verified in ip_wput_nondata */
6137 	mp1 = mp->b_cont->b_cont;
6138 	rt = (struct rtentry *)mp1->b_rptr;
6139 
6140 	dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6141 	gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6142 
6143 	/*
6144 	 * If the RTF_HOST flag is on, this is a request to delete a gateway
6145 	 * to a particular host address.  In this case, we set the netmask to
6146 	 * all ones for the particular destination address.  Otherwise,
6147 	 * determine the netmask to be used based on dst_addr and the interfaces
6148 	 * in use.
6149 	 */
6150 	if (rt->rt_flags & RTF_HOST) {
6151 		mask = IP_HOST_MASK;
6152 	} else {
6153 		/*
6154 		 * Note that ip_subnet_mask returns a zero mask in the case of
6155 		 * default (an all-zeroes address).
6156 		 */
6157 		mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6158 	}
6159 
6160 	error = ip_rt_delete(dst_addr, mask, gw_addr,
6161 	    RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6162 	    ipst, ALL_ZONES);
6163 	if (ipif != NULL)
6164 		ipif_refrele(ipif);
6165 	return (error);
6166 }
6167 
6168 /*
6169  * Enqueue the mp onto the ipsq, chained by b_next.
6170  * b_prev stores the function to be executed later, and b_queue the queue
6171  * where this mp originated.
6172  */
6173 void
6174 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6175     ill_t *pending_ill)
6176 {
6177 	conn_t	*connp;
6178 	ipxop_t *ipx = ipsq->ipsq_xop;
6179 
6180 	ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6181 	ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6182 	ASSERT(func != NULL);
6183 
6184 	mp->b_queue = q;
6185 	mp->b_prev = (void *)func;
6186 	mp->b_next = NULL;
6187 
6188 	switch (type) {
6189 	case CUR_OP:
6190 		if (ipx->ipx_mptail != NULL) {
6191 			ASSERT(ipx->ipx_mphead != NULL);
6192 			ipx->ipx_mptail->b_next = mp;
6193 		} else {
6194 			ASSERT(ipx->ipx_mphead == NULL);
6195 			ipx->ipx_mphead = mp;
6196 		}
6197 		ipx->ipx_mptail = mp;
6198 		break;
6199 
6200 	case NEW_OP:
6201 		if (ipsq->ipsq_xopq_mptail != NULL) {
6202 			ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6203 			ipsq->ipsq_xopq_mptail->b_next = mp;
6204 		} else {
6205 			ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6206 			ipsq->ipsq_xopq_mphead = mp;
6207 		}
6208 		ipsq->ipsq_xopq_mptail = mp;
6209 		ipx->ipx_ipsq_queued = B_TRUE;
6210 		break;
6211 
6212 	case SWITCH_OP:
6213 		ASSERT(ipsq->ipsq_swxop != NULL);
6214 		/* only one switch operation is currently allowed */
6215 		ASSERT(ipsq->ipsq_switch_mp == NULL);
6216 		ipsq->ipsq_switch_mp = mp;
6217 		ipx->ipx_ipsq_queued = B_TRUE;
6218 		break;
6219 	default:
6220 		cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6221 	}
6222 
6223 	if (CONN_Q(q) && pending_ill != NULL) {
6224 		connp = Q_TO_CONN(q);
6225 		ASSERT(MUTEX_HELD(&connp->conn_lock));
6226 		connp->conn_oper_pending_ill = pending_ill;
6227 	}
6228 }
6229 
6230 /*
6231  * Dequeue the next message that requested exclusive access to this IPSQ's
6232  * xop.  Specifically:
6233  *
6234  *  1. If we're still processing the current operation on `ipsq', then
6235  *     dequeue the next message for the operation (from ipx_mphead), or
6236  *     return NULL if there are no queued messages for the operation.
6237  *     These messages are queued via CUR_OP to qwriter_ip() and friends.
6238  *
6239  *  2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6240  *     not set) see if the ipsq has requested an xop switch.  If so, switch
6241  *     `ipsq' to a different xop.  Xop switches only happen when joining or
6242  *     leaving IPMP groups and require a careful dance -- see the comments
6243  *     in-line below for details.  If we're leaving a group xop or if we're
6244  *     joining a group xop and become writer on it, then we proceed to (3).
6245  *     Otherwise, we return NULL and exit the xop.
6246  *
6247  *  3. For each IPSQ in the xop, return any switch operation stored on
6248  *     ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6249  *     any other messages queued on the IPSQ.  Otherwise, dequeue the next
6250  *     exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6251  *     Note that if the phyint tied to `ipsq' is not using IPMP there will
6252  *     only be one IPSQ in the xop.  Otherwise, there will be one IPSQ for
6253  *     each phyint in the group, including the IPMP meta-interface phyint.
6254  */
6255 static mblk_t *
6256 ipsq_dq(ipsq_t *ipsq)
6257 {
6258 	ill_t	*illv4, *illv6;
6259 	mblk_t	*mp;
6260 	ipsq_t	*xopipsq;
6261 	ipsq_t	*leftipsq = NULL;
6262 	ipxop_t *ipx;
6263 	phyint_t *phyi = ipsq->ipsq_phyint;
6264 	ip_stack_t *ipst = ipsq->ipsq_ipst;
6265 	boolean_t emptied = B_FALSE;
6266 
6267 	/*
6268 	 * Grab all the locks we need in the defined order (ill_g_lock ->
6269 	 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6270 	 */
6271 	rw_enter(&ipst->ips_ill_g_lock,
6272 	    ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6273 	mutex_enter(&ipsq->ipsq_lock);
6274 	ipx = ipsq->ipsq_xop;
6275 	mutex_enter(&ipx->ipx_lock);
6276 
6277 	/*
6278 	 * Dequeue the next message associated with the current exclusive
6279 	 * operation, if any.
6280 	 */
6281 	if ((mp = ipx->ipx_mphead) != NULL) {
6282 		ipx->ipx_mphead = mp->b_next;
6283 		if (ipx->ipx_mphead == NULL)
6284 			ipx->ipx_mptail = NULL;
6285 		mp->b_next = (void *)ipsq;
6286 		goto out;
6287 	}
6288 
6289 	if (ipx->ipx_current_ipif != NULL)
6290 		goto empty;
6291 
6292 	if (ipsq->ipsq_swxop != NULL) {
6293 		/*
6294 		 * The exclusive operation that is now being completed has
6295 		 * requested a switch to a different xop.  This happens
6296 		 * when an interface joins or leaves an IPMP group.  Joins
6297 		 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6298 		 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6299 		 * (phyint_free()), or interface plumb for an ill type
6300 		 * not in the IPMP group (ip_rput_dlpi_writer()).
6301 		 *
6302 		 * Xop switches are not allowed on the IPMP meta-interface.
6303 		 */
6304 		ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6305 		ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6306 		DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6307 
6308 		if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6309 			/*
6310 			 * We're switching back to our own xop, so we have two
6311 			 * xop's to drain/exit: our own, and the group xop
6312 			 * that we are leaving.
6313 			 *
6314 			 * First, pull ourselves out of the group ipsq list.
6315 			 * This is safe since we're writer on ill_g_lock.
6316 			 */
6317 			ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6318 
6319 			xopipsq = ipx->ipx_ipsq;
6320 			while (xopipsq->ipsq_next != ipsq)
6321 				xopipsq = xopipsq->ipsq_next;
6322 
6323 			xopipsq->ipsq_next = ipsq->ipsq_next;
6324 			ipsq->ipsq_next = ipsq;
6325 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6326 			ipsq->ipsq_swxop = NULL;
6327 
6328 			/*
6329 			 * Second, prepare to exit the group xop.  The actual
6330 			 * ipsq_exit() is done at the end of this function
6331 			 * since we cannot hold any locks across ipsq_exit().
6332 			 * Note that although we drop the group's ipx_lock, no
6333 			 * threads can proceed since we're still ipx_writer.
6334 			 */
6335 			leftipsq = xopipsq;
6336 			mutex_exit(&ipx->ipx_lock);
6337 
6338 			/*
6339 			 * Third, set ipx to point to our own xop (which was
6340 			 * inactive and therefore can be entered).
6341 			 */
6342 			ipx = ipsq->ipsq_xop;
6343 			mutex_enter(&ipx->ipx_lock);
6344 			ASSERT(ipx->ipx_writer == NULL);
6345 			ASSERT(ipx->ipx_current_ipif == NULL);
6346 		} else {
6347 			/*
6348 			 * We're switching from our own xop to a group xop.
6349 			 * The requestor of the switch must ensure that the
6350 			 * group xop cannot go away (e.g. by ensuring the
6351 			 * phyint associated with the xop cannot go away).
6352 			 *
6353 			 * If we can become writer on our new xop, then we'll
6354 			 * do the drain.  Otherwise, the current writer of our
6355 			 * new xop will do the drain when it exits.
6356 			 *
6357 			 * First, splice ourselves into the group IPSQ list.
6358 			 * This is safe since we're writer on ill_g_lock.
6359 			 */
6360 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6361 
6362 			xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6363 			while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6364 				xopipsq = xopipsq->ipsq_next;
6365 
6366 			xopipsq->ipsq_next = ipsq;
6367 			ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6368 			ipsq->ipsq_xop = ipsq->ipsq_swxop;
6369 			ipsq->ipsq_swxop = NULL;
6370 
6371 			/*
6372 			 * Second, exit our own xop, since it's now unused.
6373 			 * This is safe since we've got the only reference.
6374 			 */
6375 			ASSERT(ipx->ipx_writer == curthread);
6376 			ipx->ipx_writer = NULL;
6377 			VERIFY(--ipx->ipx_reentry_cnt == 0);
6378 			ipx->ipx_ipsq_queued = B_FALSE;
6379 			mutex_exit(&ipx->ipx_lock);
6380 
6381 			/*
6382 			 * Third, set ipx to point to our new xop, and check
6383 			 * if we can become writer on it.  If we cannot, then
6384 			 * the current writer will drain the IPSQ group when
6385 			 * it exits.  Our ipsq_xop is guaranteed to be stable
6386 			 * because we're still holding ipsq_lock.
6387 			 */
6388 			ipx = ipsq->ipsq_xop;
6389 			mutex_enter(&ipx->ipx_lock);
6390 			if (ipx->ipx_writer != NULL ||
6391 			    ipx->ipx_current_ipif != NULL) {
6392 				goto out;
6393 			}
6394 		}
6395 
6396 		/*
6397 		 * Fourth, become writer on our new ipx before we continue
6398 		 * with the drain.  Note that we never dropped ipsq_lock
6399 		 * above, so no other thread could've raced with us to
6400 		 * become writer first.  Also, we're holding ipx_lock, so
6401 		 * no other thread can examine the ipx right now.
6402 		 */
6403 		ASSERT(ipx->ipx_current_ipif == NULL);
6404 		ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6405 		VERIFY(ipx->ipx_reentry_cnt++ == 0);
6406 		ipx->ipx_writer = curthread;
6407 		ipx->ipx_forced = B_FALSE;
6408 #ifdef DEBUG
6409 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6410 #endif
6411 	}
6412 
6413 	xopipsq = ipsq;
6414 	do {
6415 		/*
6416 		 * So that other operations operate on a consistent and
6417 		 * complete phyint, a switch message on an IPSQ must be
6418 		 * handled prior to any other operations on that IPSQ.
6419 		 */
6420 		if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6421 			xopipsq->ipsq_switch_mp = NULL;
6422 			ASSERT(mp->b_next == NULL);
6423 			mp->b_next = (void *)xopipsq;
6424 			goto out;
6425 		}
6426 
6427 		if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6428 			xopipsq->ipsq_xopq_mphead = mp->b_next;
6429 			if (xopipsq->ipsq_xopq_mphead == NULL)
6430 				xopipsq->ipsq_xopq_mptail = NULL;
6431 			mp->b_next = (void *)xopipsq;
6432 			goto out;
6433 		}
6434 	} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6435 empty:
6436 	/*
6437 	 * There are no messages.  Further, we are holding ipx_lock, hence no
6438 	 * new messages can end up on any IPSQ in the xop.
6439 	 */
6440 	ipx->ipx_writer = NULL;
6441 	ipx->ipx_forced = B_FALSE;
6442 	VERIFY(--ipx->ipx_reentry_cnt == 0);
6443 	ipx->ipx_ipsq_queued = B_FALSE;
6444 	emptied = B_TRUE;
6445 #ifdef	DEBUG
6446 	ipx->ipx_depth = 0;
6447 #endif
6448 out:
6449 	mutex_exit(&ipx->ipx_lock);
6450 	mutex_exit(&ipsq->ipsq_lock);
6451 
6452 	/*
6453 	 * If we completely emptied the xop, then wake up any threads waiting
6454 	 * to enter any of the IPSQ's associated with it.
6455 	 */
6456 	if (emptied) {
6457 		xopipsq = ipsq;
6458 		do {
6459 			if ((phyi = xopipsq->ipsq_phyint) == NULL)
6460 				continue;
6461 
6462 			illv4 = phyi->phyint_illv4;
6463 			illv6 = phyi->phyint_illv6;
6464 
6465 			GRAB_ILL_LOCKS(illv4, illv6);
6466 			if (illv4 != NULL)
6467 				cv_broadcast(&illv4->ill_cv);
6468 			if (illv6 != NULL)
6469 				cv_broadcast(&illv6->ill_cv);
6470 			RELEASE_ILL_LOCKS(illv4, illv6);
6471 		} while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6472 	}
6473 	rw_exit(&ipst->ips_ill_g_lock);
6474 
6475 	/*
6476 	 * Now that all locks are dropped, exit the IPSQ we left.
6477 	 */
6478 	if (leftipsq != NULL)
6479 		ipsq_exit(leftipsq);
6480 
6481 	return (mp);
6482 }
6483 
6484 /*
6485  * Return completion status of previously initiated DLPI operations on
6486  * ills in the purview of an ipsq.
6487  */
6488 static boolean_t
6489 ipsq_dlpi_done(ipsq_t *ipsq)
6490 {
6491 	ipsq_t		*ipsq_start;
6492 	phyint_t	*phyi;
6493 	ill_t		*ill;
6494 
6495 	ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6496 	ipsq_start = ipsq;
6497 
6498 	do {
6499 		/*
6500 		 * The only current users of this function are ipsq_try_enter
6501 		 * and ipsq_enter which have made sure that ipsq_writer is
6502 		 * NULL before we reach here. ill_dlpi_pending is modified
6503 		 * only by an ipsq writer
6504 		 */
6505 		ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6506 		phyi = ipsq->ipsq_phyint;
6507 		/*
6508 		 * phyi could be NULL if a phyint that is part of an
6509 		 * IPMP group is being unplumbed. A more detailed
6510 		 * comment is in ipmp_grp_update_kstats()
6511 		 */
6512 		if (phyi != NULL) {
6513 			ill = phyi->phyint_illv4;
6514 			if (ill != NULL &&
6515 			    (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6516 			    ill->ill_arl_dlpi_pending))
6517 				return (B_FALSE);
6518 
6519 			ill = phyi->phyint_illv6;
6520 			if (ill != NULL &&
6521 			    ill->ill_dlpi_pending != DL_PRIM_INVAL)
6522 				return (B_FALSE);
6523 		}
6524 
6525 	} while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6526 
6527 	return (B_TRUE);
6528 }
6529 
6530 /*
6531  * Enter the ipsq corresponding to ill, by waiting synchronously till
6532  * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6533  * will have to drain completely before ipsq_enter returns success.
6534  * ipx_current_ipif will be set if some exclusive op is in progress,
6535  * and the ipsq_exit logic will start the next enqueued op after
6536  * completion of the current op. If 'force' is used, we don't wait
6537  * for the enqueued ops. This is needed when a conn_close wants to
6538  * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6539  * of an ill can also use this option. But we dont' use it currently.
6540  */
6541 #define	ENTER_SQ_WAIT_TICKS 100
6542 boolean_t
6543 ipsq_enter(ill_t *ill, boolean_t force, int type)
6544 {
6545 	ipsq_t	*ipsq;
6546 	ipxop_t *ipx;
6547 	boolean_t waited_enough = B_FALSE;
6548 	ip_stack_t *ipst = ill->ill_ipst;
6549 
6550 	/*
6551 	 * Note that the relationship between ill and ipsq is fixed as long as
6552 	 * the ill is not ILL_CONDEMNED.  Holding ipsq_lock ensures the
6553 	 * relationship between the IPSQ and xop cannot change.  However,
6554 	 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6555 	 * while we're waiting.  We wait on ill_cv and rely on ipsq_exit()
6556 	 * waking up all ills in the xop when it becomes available.
6557 	 */
6558 	for (;;) {
6559 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6560 		mutex_enter(&ill->ill_lock);
6561 		if (ill->ill_state_flags & ILL_CONDEMNED) {
6562 			mutex_exit(&ill->ill_lock);
6563 			rw_exit(&ipst->ips_ill_g_lock);
6564 			return (B_FALSE);
6565 		}
6566 
6567 		ipsq = ill->ill_phyint->phyint_ipsq;
6568 		mutex_enter(&ipsq->ipsq_lock);
6569 		ipx = ipsq->ipsq_xop;
6570 		mutex_enter(&ipx->ipx_lock);
6571 
6572 		if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6573 		    (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6574 		    waited_enough))
6575 			break;
6576 
6577 		rw_exit(&ipst->ips_ill_g_lock);
6578 
6579 		if (!force || ipx->ipx_writer != NULL) {
6580 			mutex_exit(&ipx->ipx_lock);
6581 			mutex_exit(&ipsq->ipsq_lock);
6582 			cv_wait(&ill->ill_cv, &ill->ill_lock);
6583 		} else {
6584 			mutex_exit(&ipx->ipx_lock);
6585 			mutex_exit(&ipsq->ipsq_lock);
6586 			(void) cv_reltimedwait(&ill->ill_cv,
6587 			    &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6588 			waited_enough = B_TRUE;
6589 		}
6590 		mutex_exit(&ill->ill_lock);
6591 	}
6592 
6593 	ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6594 	ASSERT(ipx->ipx_reentry_cnt == 0);
6595 	ipx->ipx_writer = curthread;
6596 	ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6597 	ipx->ipx_reentry_cnt++;
6598 #ifdef DEBUG
6599 	ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6600 #endif
6601 	mutex_exit(&ipx->ipx_lock);
6602 	mutex_exit(&ipsq->ipsq_lock);
6603 	mutex_exit(&ill->ill_lock);
6604 	rw_exit(&ipst->ips_ill_g_lock);
6605 
6606 	return (B_TRUE);
6607 }
6608 
6609 /*
6610  * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6611  * across the call to the core interface ipsq_try_enter() and hence calls this
6612  * function directly. This is explained more fully in ipif_set_values().
6613  * In order to support the above constraint, ipsq_try_enter is implemented as
6614  * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6615  */
6616 static ipsq_t *
6617 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6618     int type, boolean_t reentry_ok)
6619 {
6620 	ipsq_t	*ipsq;
6621 	ipxop_t	*ipx;
6622 	ip_stack_t *ipst = ill->ill_ipst;
6623 
6624 	/*
6625 	 * lock ordering:
6626 	 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6627 	 *
6628 	 * ipx of an ipsq can't change when ipsq_lock is held.
6629 	 */
6630 	ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6631 	GRAB_CONN_LOCK(q);
6632 	mutex_enter(&ill->ill_lock);
6633 	ipsq = ill->ill_phyint->phyint_ipsq;
6634 	mutex_enter(&ipsq->ipsq_lock);
6635 	ipx = ipsq->ipsq_xop;
6636 	mutex_enter(&ipx->ipx_lock);
6637 
6638 	/*
6639 	 * 1. Enter the ipsq if we are already writer and reentry is ok.
6640 	 *    (Note: If the caller does not specify reentry_ok then neither
6641 	 *    'func' nor any of its callees must ever attempt to enter the ipsq
6642 	 *    again. Otherwise it can lead to an infinite loop
6643 	 * 2. Enter the ipsq if there is no current writer and this attempted
6644 	 *    entry is part of the current operation
6645 	 * 3. Enter the ipsq if there is no current writer and this is a new
6646 	 *    operation and the operation queue is empty and there is no
6647 	 *    operation currently in progress and if all previously initiated
6648 	 *    DLPI operations have completed.
6649 	 */
6650 	if ((ipx->ipx_writer == curthread && reentry_ok) ||
6651 	    (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6652 	    !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6653 	    ipsq_dlpi_done(ipsq))))) {
6654 		/* Success. */
6655 		ipx->ipx_reentry_cnt++;
6656 		ipx->ipx_writer = curthread;
6657 		ipx->ipx_forced = B_FALSE;
6658 		mutex_exit(&ipx->ipx_lock);
6659 		mutex_exit(&ipsq->ipsq_lock);
6660 		mutex_exit(&ill->ill_lock);
6661 		RELEASE_CONN_LOCK(q);
6662 #ifdef DEBUG
6663 		ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6664 #endif
6665 		return (ipsq);
6666 	}
6667 
6668 	if (func != NULL)
6669 		ipsq_enq(ipsq, q, mp, func, type, ill);
6670 
6671 	mutex_exit(&ipx->ipx_lock);
6672 	mutex_exit(&ipsq->ipsq_lock);
6673 	mutex_exit(&ill->ill_lock);
6674 	RELEASE_CONN_LOCK(q);
6675 	return (NULL);
6676 }
6677 
6678 /*
6679  * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6680  * certain critical operations like plumbing (i.e. most set ioctls), etc.
6681  * There is one ipsq per phyint. The ipsq
6682  * serializes exclusive ioctls issued by applications on a per ipsq basis in
6683  * ipsq_xopq_mphead. It also protects against multiple threads executing in
6684  * the ipsq. Responses from the driver pertain to the current ioctl (say a
6685  * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6686  * up the interface) and are enqueued in ipx_mphead.
6687  *
6688  * If a thread does not want to reenter the ipsq when it is already writer,
6689  * it must make sure that the specified reentry point to be called later
6690  * when the ipsq is empty, nor any code path starting from the specified reentry
6691  * point must never ever try to enter the ipsq again. Otherwise it can lead
6692  * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6693  * When the thread that is currently exclusive finishes, it (ipsq_exit)
6694  * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6695  * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6696  * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6697  * ioctl if the current ioctl has completed. If the current ioctl is still
6698  * in progress it simply returns. The current ioctl could be waiting for
6699  * a response from another module (the driver or could be waiting for
6700  * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6701  * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6702  * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6703  * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6704  * all associated DLPI operations have completed.
6705  */
6706 
6707 /*
6708  * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6709  * and `ill' cannot both be specified).  Returns a pointer to the entered IPSQ
6710  * on success, or NULL on failure.  The caller ensures ipif/ill is valid by
6711  * refholding it as necessary.  If the IPSQ cannot be entered and `func' is
6712  * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6713  * can be entered.  If `func' is NULL, then `q' and `mp' are ignored.
6714  */
6715 ipsq_t *
6716 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6717     ipsq_func_t func, int type, boolean_t reentry_ok)
6718 {
6719 	ip_stack_t	*ipst;
6720 	ipsq_t		*ipsq;
6721 
6722 	/* Only 1 of ipif or ill can be specified */
6723 	ASSERT((ipif != NULL) ^ (ill != NULL));
6724 
6725 	if (ipif != NULL)
6726 		ill = ipif->ipif_ill;
6727 	ipst = ill->ill_ipst;
6728 
6729 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6730 	ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6731 	rw_exit(&ipst->ips_ill_g_lock);
6732 
6733 	return (ipsq);
6734 }
6735 
6736 /*
6737  * Try to enter the IPSQ corresponding to `ill' as writer.  The caller ensures
6738  * ill is valid by refholding it if necessary; we will refrele.  If the IPSQ
6739  * cannot be entered, the mp is queued for completion.
6740  */
6741 void
6742 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6743     boolean_t reentry_ok)
6744 {
6745 	ipsq_t	*ipsq;
6746 
6747 	ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6748 
6749 	/*
6750 	 * Drop the caller's refhold on the ill.  This is safe since we either
6751 	 * entered the IPSQ (and thus are exclusive), or failed to enter the
6752 	 * IPSQ, in which case we return without accessing ill anymore.  This
6753 	 * is needed because func needs to see the correct refcount.
6754 	 * e.g. removeif can work only then.
6755 	 */
6756 	ill_refrele(ill);
6757 	if (ipsq != NULL) {
6758 		(*func)(ipsq, q, mp, NULL);
6759 		ipsq_exit(ipsq);
6760 	}
6761 }
6762 
6763 /*
6764  * Exit the specified IPSQ.  If this is the final exit on it then drain it
6765  * prior to exiting.  Caller must be writer on the specified IPSQ.
6766  */
6767 void
6768 ipsq_exit(ipsq_t *ipsq)
6769 {
6770 	mblk_t *mp;
6771 	ipsq_t *mp_ipsq;
6772 	queue_t	*q;
6773 	phyint_t *phyi;
6774 	ipsq_func_t func;
6775 
6776 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6777 
6778 	ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6779 	if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6780 		ipsq->ipsq_xop->ipx_reentry_cnt--;
6781 		return;
6782 	}
6783 
6784 	for (;;) {
6785 		phyi = ipsq->ipsq_phyint;
6786 		mp = ipsq_dq(ipsq);
6787 		mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6788 
6789 		/*
6790 		 * If we've changed to a new IPSQ, and the phyint associated
6791 		 * with the old one has gone away, free the old IPSQ.  Note
6792 		 * that this cannot happen while the IPSQ is in a group.
6793 		 */
6794 		if (mp_ipsq != ipsq && phyi == NULL) {
6795 			ASSERT(ipsq->ipsq_next == ipsq);
6796 			ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6797 			ipsq_delete(ipsq);
6798 		}
6799 
6800 		if (mp == NULL)
6801 			break;
6802 
6803 		q = mp->b_queue;
6804 		func = (ipsq_func_t)mp->b_prev;
6805 		ipsq = mp_ipsq;
6806 		mp->b_next = mp->b_prev = NULL;
6807 		mp->b_queue = NULL;
6808 
6809 		/*
6810 		 * If 'q' is an conn queue, it is valid, since we did a
6811 		 * a refhold on the conn at the start of the ioctl.
6812 		 * If 'q' is an ill queue, it is valid, since close of an
6813 		 * ill will clean up its IPSQ.
6814 		 */
6815 		(*func)(ipsq, q, mp, NULL);
6816 	}
6817 }
6818 
6819 /*
6820  * Used to start any igmp or mld timers that could not be started
6821  * while holding ill_mcast_lock. The timers can't be started while holding
6822  * the lock, since mld/igmp_start_timers may need to call untimeout()
6823  * which can't be done while holding the lock which the timeout handler
6824  * acquires. Otherwise
6825  * there could be a deadlock since the timeout handlers
6826  * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6827  * ill_mcast_lock.
6828  */
6829 void
6830 ill_mcast_timer_start(ip_stack_t *ipst)
6831 {
6832 	int		next;
6833 
6834 	mutex_enter(&ipst->ips_igmp_timer_lock);
6835 	next = ipst->ips_igmp_deferred_next;
6836 	ipst->ips_igmp_deferred_next = INFINITY;
6837 	mutex_exit(&ipst->ips_igmp_timer_lock);
6838 
6839 	if (next != INFINITY)
6840 		igmp_start_timers(next, ipst);
6841 
6842 	mutex_enter(&ipst->ips_mld_timer_lock);
6843 	next = ipst->ips_mld_deferred_next;
6844 	ipst->ips_mld_deferred_next = INFINITY;
6845 	mutex_exit(&ipst->ips_mld_timer_lock);
6846 
6847 	if (next != INFINITY)
6848 		mld_start_timers(next, ipst);
6849 }
6850 
6851 /*
6852  * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6853  * and `ioccmd'.
6854  */
6855 void
6856 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6857 {
6858 	ill_t *ill = ipif->ipif_ill;
6859 	ipxop_t *ipx = ipsq->ipsq_xop;
6860 
6861 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6862 	ASSERT(ipx->ipx_current_ipif == NULL);
6863 	ASSERT(ipx->ipx_current_ioctl == 0);
6864 
6865 	ipx->ipx_current_done = B_FALSE;
6866 	ipx->ipx_current_ioctl = ioccmd;
6867 	mutex_enter(&ipx->ipx_lock);
6868 	ipx->ipx_current_ipif = ipif;
6869 	mutex_exit(&ipx->ipx_lock);
6870 
6871 	/*
6872 	 * Set IPIF_CHANGING on one or more ipifs associated with the
6873 	 * current exclusive operation.  IPIF_CHANGING prevents any new
6874 	 * references to the ipif (so that the references will eventually
6875 	 * drop to zero) and also prevents any "get" operations (e.g.,
6876 	 * SIOCGLIFFLAGS) from being able to access the ipif until the
6877 	 * operation has completed and the ipif is again in a stable state.
6878 	 *
6879 	 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6880 	 * ioctl.  For internal operations (where ioccmd is zero), all ipifs
6881 	 * on the ill are marked with IPIF_CHANGING since it's unclear which
6882 	 * ipifs will be affected.
6883 	 *
6884 	 * Note that SIOCLIFREMOVEIF is a special case as it sets
6885 	 * IPIF_CONDEMNED internally after identifying the right ipif to
6886 	 * operate on.
6887 	 */
6888 	switch (ioccmd) {
6889 	case SIOCLIFREMOVEIF:
6890 		break;
6891 	case 0:
6892 		mutex_enter(&ill->ill_lock);
6893 		ipif = ipif->ipif_ill->ill_ipif;
6894 		for (; ipif != NULL; ipif = ipif->ipif_next)
6895 			ipif->ipif_state_flags |= IPIF_CHANGING;
6896 		mutex_exit(&ill->ill_lock);
6897 		break;
6898 	default:
6899 		mutex_enter(&ill->ill_lock);
6900 		ipif->ipif_state_flags |= IPIF_CHANGING;
6901 		mutex_exit(&ill->ill_lock);
6902 	}
6903 }
6904 
6905 /*
6906  * Finish the current exclusive operation on `ipsq'.  Usually, this will allow
6907  * the next exclusive operation to begin once we ipsq_exit().  However, if
6908  * pending DLPI operations remain, then we will wait for the queue to drain
6909  * before allowing the next exclusive operation to begin.  This ensures that
6910  * DLPI operations from one exclusive operation are never improperly processed
6911  * as part of a subsequent exclusive operation.
6912  */
6913 void
6914 ipsq_current_finish(ipsq_t *ipsq)
6915 {
6916 	ipxop_t	*ipx = ipsq->ipsq_xop;
6917 	t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6918 	ipif_t	*ipif = ipx->ipx_current_ipif;
6919 
6920 	ASSERT(IAM_WRITER_IPSQ(ipsq));
6921 
6922 	/*
6923 	 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6924 	 * (but in that case, IPIF_CHANGING will already be clear and no
6925 	 * pending DLPI messages can remain).
6926 	 */
6927 	if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6928 		ill_t *ill = ipif->ipif_ill;
6929 
6930 		mutex_enter(&ill->ill_lock);
6931 		dlpi_pending = ill->ill_dlpi_pending;
6932 		if (ipx->ipx_current_ioctl == 0) {
6933 			ipif = ill->ill_ipif;
6934 			for (; ipif != NULL; ipif = ipif->ipif_next)
6935 				ipif->ipif_state_flags &= ~IPIF_CHANGING;
6936 		} else {
6937 			ipif->ipif_state_flags &= ~IPIF_CHANGING;
6938 		}
6939 		mutex_exit(&ill->ill_lock);
6940 	}
6941 
6942 	ASSERT(!ipx->ipx_current_done);
6943 	ipx->ipx_current_done = B_TRUE;
6944 	ipx->ipx_current_ioctl = 0;
6945 	if (dlpi_pending == DL_PRIM_INVAL) {
6946 		mutex_enter(&ipx->ipx_lock);
6947 		ipx->ipx_current_ipif = NULL;
6948 		mutex_exit(&ipx->ipx_lock);
6949 	}
6950 }
6951 
6952 /*
6953  * The ill is closing. Flush all messages on the ipsq that originated
6954  * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6955  * for this ill since ipsq_enter could not have entered until then.
6956  * New messages can't be queued since the CONDEMNED flag is set.
6957  */
6958 static void
6959 ipsq_flush(ill_t *ill)
6960 {
6961 	queue_t	*q;
6962 	mblk_t	*prev;
6963 	mblk_t	*mp;
6964 	mblk_t	*mp_next;
6965 	ipxop_t	*ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
6966 
6967 	ASSERT(IAM_WRITER_ILL(ill));
6968 
6969 	/*
6970 	 * Flush any messages sent up by the driver.
6971 	 */
6972 	mutex_enter(&ipx->ipx_lock);
6973 	for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
6974 		mp_next = mp->b_next;
6975 		q = mp->b_queue;
6976 		if (q == ill->ill_rq || q == ill->ill_wq) {
6977 			/* dequeue mp */
6978 			if (prev == NULL)
6979 				ipx->ipx_mphead = mp->b_next;
6980 			else
6981 				prev->b_next = mp->b_next;
6982 			if (ipx->ipx_mptail == mp) {
6983 				ASSERT(mp_next == NULL);
6984 				ipx->ipx_mptail = prev;
6985 			}
6986 			inet_freemsg(mp);
6987 		} else {
6988 			prev = mp;
6989 		}
6990 	}
6991 	mutex_exit(&ipx->ipx_lock);
6992 	(void) ipsq_pending_mp_cleanup(ill, NULL);
6993 	ipsq_xopq_mp_cleanup(ill, NULL);
6994 }
6995 
6996 /*
6997  * Parse an ifreq or lifreq struct coming down ioctls and refhold
6998  * and return the associated ipif.
6999  * Return value:
7000  *	Non zero: An error has occurred. ci may not be filled out.
7001  *	zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7002  *	a held ipif in ci.ci_ipif.
7003  */
7004 int
7005 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7006     cmd_info_t *ci)
7007 {
7008 	char		*name;
7009 	struct ifreq    *ifr;
7010 	struct lifreq    *lifr;
7011 	ipif_t		*ipif = NULL;
7012 	ill_t		*ill;
7013 	conn_t		*connp;
7014 	boolean_t	isv6;
7015 	boolean_t	exists;
7016 	mblk_t		*mp1;
7017 	zoneid_t	zoneid;
7018 	ip_stack_t	*ipst;
7019 
7020 	if (q->q_next != NULL) {
7021 		ill = (ill_t *)q->q_ptr;
7022 		isv6 = ill->ill_isv6;
7023 		connp = NULL;
7024 		zoneid = ALL_ZONES;
7025 		ipst = ill->ill_ipst;
7026 	} else {
7027 		ill = NULL;
7028 		connp = Q_TO_CONN(q);
7029 		isv6 = (connp->conn_family == AF_INET6);
7030 		zoneid = connp->conn_zoneid;
7031 		if (zoneid == GLOBAL_ZONEID) {
7032 			/* global zone can access ipifs in all zones */
7033 			zoneid = ALL_ZONES;
7034 		}
7035 		ipst = connp->conn_netstack->netstack_ip;
7036 	}
7037 
7038 	/* Has been checked in ip_wput_nondata */
7039 	mp1 = mp->b_cont->b_cont;
7040 
7041 	if (ipip->ipi_cmd_type == IF_CMD) {
7042 		/* This a old style SIOC[GS]IF* command */
7043 		ifr = (struct ifreq *)mp1->b_rptr;
7044 		/*
7045 		 * Null terminate the string to protect against buffer
7046 		 * overrun. String was generated by user code and may not
7047 		 * be trusted.
7048 		 */
7049 		ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7050 		name = ifr->ifr_name;
7051 		ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7052 		ci->ci_sin6 = NULL;
7053 		ci->ci_lifr = (struct lifreq *)ifr;
7054 	} else {
7055 		/* This a new style SIOC[GS]LIF* command */
7056 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7057 		lifr = (struct lifreq *)mp1->b_rptr;
7058 		/*
7059 		 * Null terminate the string to protect against buffer
7060 		 * overrun. String was generated by user code and may not
7061 		 * be trusted.
7062 		 */
7063 		lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7064 		name = lifr->lifr_name;
7065 		ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7066 		ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7067 		ci->ci_lifr = lifr;
7068 	}
7069 
7070 	if (ipip->ipi_cmd == SIOCSLIFNAME) {
7071 		/*
7072 		 * The ioctl will be failed if the ioctl comes down
7073 		 * an conn stream
7074 		 */
7075 		if (ill == NULL) {
7076 			/*
7077 			 * Not an ill queue, return EINVAL same as the
7078 			 * old error code.
7079 			 */
7080 			return (ENXIO);
7081 		}
7082 		ipif = ill->ill_ipif;
7083 		ipif_refhold(ipif);
7084 	} else {
7085 		ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE,
7086 		    &exists, isv6, zoneid, ipst);
7087 
7088 		/*
7089 		 * Ensure that get ioctls don't see any internal state changes
7090 		 * caused by set ioctls by deferring them if IPIF_CHANGING is
7091 		 * set.
7092 		 */
7093 		if (ipif != NULL && !(ipip->ipi_flags & IPI_WR) &&
7094 		    !IAM_WRITER_IPIF(ipif)) {
7095 			ipsq_t	*ipsq;
7096 
7097 			if (connp != NULL)
7098 				mutex_enter(&connp->conn_lock);
7099 			mutex_enter(&ipif->ipif_ill->ill_lock);
7100 			if (IPIF_IS_CHANGING(ipif) &&
7101 			    !IPIF_IS_CONDEMNED(ipif)) {
7102 				ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
7103 				mutex_enter(&ipsq->ipsq_lock);
7104 				mutex_enter(&ipsq->ipsq_xop->ipx_lock);
7105 				mutex_exit(&ipif->ipif_ill->ill_lock);
7106 				ipsq_enq(ipsq, q, mp, ip_process_ioctl,
7107 				    NEW_OP, ipif->ipif_ill);
7108 				mutex_exit(&ipsq->ipsq_xop->ipx_lock);
7109 				mutex_exit(&ipsq->ipsq_lock);
7110 				if (connp != NULL)
7111 					mutex_exit(&connp->conn_lock);
7112 				ipif_refrele(ipif);
7113 				return (EINPROGRESS);
7114 			}
7115 			mutex_exit(&ipif->ipif_ill->ill_lock);
7116 			if (connp != NULL)
7117 				mutex_exit(&connp->conn_lock);
7118 		}
7119 	}
7120 
7121 	/*
7122 	 * Old style [GS]IFCMD does not admit IPv6 ipif
7123 	 */
7124 	if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7125 		ipif_refrele(ipif);
7126 		return (ENXIO);
7127 	}
7128 
7129 	if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7130 	    name[0] == '\0') {
7131 		/*
7132 		 * Handle a or a SIOC?IF* with a null name
7133 		 * during plumb (on the ill queue before the I_PLINK).
7134 		 */
7135 		ipif = ill->ill_ipif;
7136 		ipif_refhold(ipif);
7137 	}
7138 
7139 	if (ipif == NULL)
7140 		return (ENXIO);
7141 
7142 	DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7143 	    int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7144 
7145 	ci->ci_ipif = ipif;
7146 	return (0);
7147 }
7148 
7149 /*
7150  * Return the total number of ipifs.
7151  */
7152 static uint_t
7153 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7154 {
7155 	uint_t numifs = 0;
7156 	ill_t	*ill;
7157 	ill_walk_context_t	ctx;
7158 	ipif_t	*ipif;
7159 
7160 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7161 	ill = ILL_START_WALK_V4(&ctx, ipst);
7162 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7163 		if (IS_UNDER_IPMP(ill))
7164 			continue;
7165 		for (ipif = ill->ill_ipif; ipif != NULL;
7166 		    ipif = ipif->ipif_next) {
7167 			if (ipif->ipif_zoneid == zoneid ||
7168 			    ipif->ipif_zoneid == ALL_ZONES)
7169 				numifs++;
7170 		}
7171 	}
7172 	rw_exit(&ipst->ips_ill_g_lock);
7173 	return (numifs);
7174 }
7175 
7176 /*
7177  * Return the total number of ipifs.
7178  */
7179 static uint_t
7180 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7181 {
7182 	uint_t numifs = 0;
7183 	ill_t	*ill;
7184 	ipif_t	*ipif;
7185 	ill_walk_context_t	ctx;
7186 
7187 	ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7188 
7189 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7190 	if (family == AF_INET)
7191 		ill = ILL_START_WALK_V4(&ctx, ipst);
7192 	else if (family == AF_INET6)
7193 		ill = ILL_START_WALK_V6(&ctx, ipst);
7194 	else
7195 		ill = ILL_START_WALK_ALL(&ctx, ipst);
7196 
7197 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7198 		if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7199 			continue;
7200 
7201 		for (ipif = ill->ill_ipif; ipif != NULL;
7202 		    ipif = ipif->ipif_next) {
7203 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7204 			    !(lifn_flags & LIFC_NOXMIT))
7205 				continue;
7206 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7207 			    !(lifn_flags & LIFC_TEMPORARY))
7208 				continue;
7209 			if (((ipif->ipif_flags &
7210 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7211 			    IPIF_DEPRECATED)) ||
7212 			    IS_LOOPBACK(ill) ||
7213 			    !(ipif->ipif_flags & IPIF_UP)) &&
7214 			    (lifn_flags & LIFC_EXTERNAL_SOURCE))
7215 				continue;
7216 
7217 			if (zoneid != ipif->ipif_zoneid &&
7218 			    ipif->ipif_zoneid != ALL_ZONES &&
7219 			    (zoneid != GLOBAL_ZONEID ||
7220 			    !(lifn_flags & LIFC_ALLZONES)))
7221 				continue;
7222 
7223 			numifs++;
7224 		}
7225 	}
7226 	rw_exit(&ipst->ips_ill_g_lock);
7227 	return (numifs);
7228 }
7229 
7230 uint_t
7231 ip_get_lifsrcofnum(ill_t *ill)
7232 {
7233 	uint_t numifs = 0;
7234 	ill_t	*ill_head = ill;
7235 	ip_stack_t	*ipst = ill->ill_ipst;
7236 
7237 	/*
7238 	 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7239 	 * other thread may be trying to relink the ILLs in this usesrc group
7240 	 * and adjusting the ill_usesrc_grp_next pointers
7241 	 */
7242 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7243 	if ((ill->ill_usesrc_ifindex == 0) &&
7244 	    (ill->ill_usesrc_grp_next != NULL)) {
7245 		for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7246 		    ill = ill->ill_usesrc_grp_next)
7247 			numifs++;
7248 	}
7249 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7250 
7251 	return (numifs);
7252 }
7253 
7254 /* Null values are passed in for ipif, sin, and ifreq */
7255 /* ARGSUSED */
7256 int
7257 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7258     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7259 {
7260 	int *nump;
7261 	conn_t *connp = Q_TO_CONN(q);
7262 
7263 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7264 
7265 	/* Existence of b_cont->b_cont checked in ip_wput_nondata */
7266 	nump = (int *)mp->b_cont->b_cont->b_rptr;
7267 
7268 	*nump = ip_get_numifs(connp->conn_zoneid,
7269 	    connp->conn_netstack->netstack_ip);
7270 	ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7271 	return (0);
7272 }
7273 
7274 /* Null values are passed in for ipif, sin, and ifreq */
7275 /* ARGSUSED */
7276 int
7277 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7278     queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7279 {
7280 	struct lifnum *lifn;
7281 	mblk_t	*mp1;
7282 	conn_t *connp = Q_TO_CONN(q);
7283 
7284 	ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7285 
7286 	/* Existence checked in ip_wput_nondata */
7287 	mp1 = mp->b_cont->b_cont;
7288 
7289 	lifn = (struct lifnum *)mp1->b_rptr;
7290 	switch (lifn->lifn_family) {
7291 	case AF_UNSPEC:
7292 	case AF_INET:
7293 	case AF_INET6:
7294 		break;
7295 	default:
7296 		return (EAFNOSUPPORT);
7297 	}
7298 
7299 	lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7300 	    connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7301 	ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7302 	return (0);
7303 }
7304 
7305 /* ARGSUSED */
7306 int
7307 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7308     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7309 {
7310 	STRUCT_HANDLE(ifconf, ifc);
7311 	mblk_t *mp1;
7312 	struct iocblk *iocp;
7313 	struct ifreq *ifr;
7314 	ill_walk_context_t	ctx;
7315 	ill_t	*ill;
7316 	ipif_t	*ipif;
7317 	struct sockaddr_in *sin;
7318 	int32_t	ifclen;
7319 	zoneid_t zoneid;
7320 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7321 
7322 	ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7323 
7324 	ip1dbg(("ip_sioctl_get_ifconf"));
7325 	/* Existence verified in ip_wput_nondata */
7326 	mp1 = mp->b_cont->b_cont;
7327 	iocp = (struct iocblk *)mp->b_rptr;
7328 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7329 
7330 	/*
7331 	 * The original SIOCGIFCONF passed in a struct ifconf which specified
7332 	 * the user buffer address and length into which the list of struct
7333 	 * ifreqs was to be copied.  Since AT&T Streams does not seem to
7334 	 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7335 	 * the SIOCGIFCONF operation was redefined to simply provide
7336 	 * a large output buffer into which we are supposed to jam the ifreq
7337 	 * array.  The same ioctl command code was used, despite the fact that
7338 	 * both the applications and the kernel code had to change, thus making
7339 	 * it impossible to support both interfaces.
7340 	 *
7341 	 * For reasons not good enough to try to explain, the following
7342 	 * algorithm is used for deciding what to do with one of these:
7343 	 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7344 	 * form with the output buffer coming down as the continuation message.
7345 	 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7346 	 * and we have to copy in the ifconf structure to find out how big the
7347 	 * output buffer is and where to copy out to.  Sure no problem...
7348 	 *
7349 	 */
7350 	STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7351 	if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7352 		int numifs = 0;
7353 		size_t ifc_bufsize;
7354 
7355 		/*
7356 		 * Must be (better be!) continuation of a TRANSPARENT
7357 		 * IOCTL.  We just copied in the ifconf structure.
7358 		 */
7359 		STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7360 		    (struct ifconf *)mp1->b_rptr);
7361 
7362 		/*
7363 		 * Allocate a buffer to hold requested information.
7364 		 *
7365 		 * If ifc_len is larger than what is needed, we only
7366 		 * allocate what we will use.
7367 		 *
7368 		 * If ifc_len is smaller than what is needed, return
7369 		 * EINVAL.
7370 		 *
7371 		 * XXX: the ill_t structure can hava 2 counters, for
7372 		 * v4 and v6 (not just ill_ipif_up_count) to store the
7373 		 * number of interfaces for a device, so we don't need
7374 		 * to count them here...
7375 		 */
7376 		numifs = ip_get_numifs(zoneid, ipst);
7377 
7378 		ifclen = STRUCT_FGET(ifc, ifc_len);
7379 		ifc_bufsize = numifs * sizeof (struct ifreq);
7380 		if (ifc_bufsize > ifclen) {
7381 			if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7382 				/* old behaviour */
7383 				return (EINVAL);
7384 			} else {
7385 				ifc_bufsize = ifclen;
7386 			}
7387 		}
7388 
7389 		mp1 = mi_copyout_alloc(q, mp,
7390 		    STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7391 		if (mp1 == NULL)
7392 			return (ENOMEM);
7393 
7394 		mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7395 	}
7396 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7397 	/*
7398 	 * the SIOCGIFCONF ioctl only knows about
7399 	 * IPv4 addresses, so don't try to tell
7400 	 * it about interfaces with IPv6-only
7401 	 * addresses. (Last parm 'isv6' is B_FALSE)
7402 	 */
7403 
7404 	ifr = (struct ifreq *)mp1->b_rptr;
7405 
7406 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7407 	ill = ILL_START_WALK_V4(&ctx, ipst);
7408 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7409 		if (IS_UNDER_IPMP(ill))
7410 			continue;
7411 		for (ipif = ill->ill_ipif; ipif != NULL;
7412 		    ipif = ipif->ipif_next) {
7413 			if (zoneid != ipif->ipif_zoneid &&
7414 			    ipif->ipif_zoneid != ALL_ZONES)
7415 				continue;
7416 			if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7417 				if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7418 					/* old behaviour */
7419 					rw_exit(&ipst->ips_ill_g_lock);
7420 					return (EINVAL);
7421 				} else {
7422 					goto if_copydone;
7423 				}
7424 			}
7425 			ipif_get_name(ipif, ifr->ifr_name,
7426 			    sizeof (ifr->ifr_name));
7427 			sin = (sin_t *)&ifr->ifr_addr;
7428 			*sin = sin_null;
7429 			sin->sin_family = AF_INET;
7430 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7431 			ifr++;
7432 		}
7433 	}
7434 if_copydone:
7435 	rw_exit(&ipst->ips_ill_g_lock);
7436 	mp1->b_wptr = (uchar_t *)ifr;
7437 
7438 	if (STRUCT_BUF(ifc) != NULL) {
7439 		STRUCT_FSET(ifc, ifc_len,
7440 		    (int)((uchar_t *)ifr - mp1->b_rptr));
7441 	}
7442 	return (0);
7443 }
7444 
7445 /*
7446  * Get the interfaces using the address hosted on the interface passed in,
7447  * as a source adddress
7448  */
7449 /* ARGSUSED */
7450 int
7451 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7452     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7453 {
7454 	mblk_t *mp1;
7455 	ill_t	*ill, *ill_head;
7456 	ipif_t	*ipif, *orig_ipif;
7457 	int	numlifs = 0;
7458 	size_t	lifs_bufsize, lifsmaxlen;
7459 	struct	lifreq *lifr;
7460 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7461 	uint_t	ifindex;
7462 	zoneid_t zoneid;
7463 	boolean_t isv6 = B_FALSE;
7464 	struct	sockaddr_in	*sin;
7465 	struct	sockaddr_in6	*sin6;
7466 	STRUCT_HANDLE(lifsrcof, lifs);
7467 	ip_stack_t		*ipst;
7468 
7469 	ipst = CONNQ_TO_IPST(q);
7470 
7471 	ASSERT(q->q_next == NULL);
7472 
7473 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7474 
7475 	/* Existence verified in ip_wput_nondata */
7476 	mp1 = mp->b_cont->b_cont;
7477 
7478 	/*
7479 	 * Must be (better be!) continuation of a TRANSPARENT
7480 	 * IOCTL.  We just copied in the lifsrcof structure.
7481 	 */
7482 	STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7483 	    (struct lifsrcof *)mp1->b_rptr);
7484 
7485 	if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7486 		return (EINVAL);
7487 
7488 	ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7489 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7490 	ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7491 	if (ipif == NULL) {
7492 		ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7493 		    ifindex));
7494 		return (ENXIO);
7495 	}
7496 
7497 	/* Allocate a buffer to hold requested information */
7498 	numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7499 	lifs_bufsize = numlifs * sizeof (struct lifreq);
7500 	lifsmaxlen =  STRUCT_FGET(lifs, lifs_maxlen);
7501 	/* The actual size needed is always returned in lifs_len */
7502 	STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7503 
7504 	/* If the amount we need is more than what is passed in, abort */
7505 	if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7506 		ipif_refrele(ipif);
7507 		return (0);
7508 	}
7509 
7510 	mp1 = mi_copyout_alloc(q, mp,
7511 	    STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7512 	if (mp1 == NULL) {
7513 		ipif_refrele(ipif);
7514 		return (ENOMEM);
7515 	}
7516 
7517 	mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7518 	bzero(mp1->b_rptr, lifs_bufsize);
7519 
7520 	lifr = (struct lifreq *)mp1->b_rptr;
7521 
7522 	ill = ill_head = ipif->ipif_ill;
7523 	orig_ipif = ipif;
7524 
7525 	/* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7526 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7527 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7528 
7529 	ill = ill->ill_usesrc_grp_next; /* start from next ill */
7530 	for (; (ill != NULL) && (ill != ill_head);
7531 	    ill = ill->ill_usesrc_grp_next) {
7532 
7533 		if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7534 			break;
7535 
7536 		ipif = ill->ill_ipif;
7537 		ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7538 		if (ipif->ipif_isv6) {
7539 			sin6 = (sin6_t *)&lifr->lifr_addr;
7540 			*sin6 = sin6_null;
7541 			sin6->sin6_family = AF_INET6;
7542 			sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7543 			lifr->lifr_addrlen = ip_mask_to_plen_v6(
7544 			    &ipif->ipif_v6net_mask);
7545 		} else {
7546 			sin = (sin_t *)&lifr->lifr_addr;
7547 			*sin = sin_null;
7548 			sin->sin_family = AF_INET;
7549 			sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7550 			lifr->lifr_addrlen = ip_mask_to_plen(
7551 			    ipif->ipif_net_mask);
7552 		}
7553 		lifr++;
7554 	}
7555 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
7556 	rw_exit(&ipst->ips_ill_g_lock);
7557 	ipif_refrele(orig_ipif);
7558 	mp1->b_wptr = (uchar_t *)lifr;
7559 	STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7560 
7561 	return (0);
7562 }
7563 
7564 /* ARGSUSED */
7565 int
7566 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7567     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7568 {
7569 	mblk_t *mp1;
7570 	int	list;
7571 	ill_t	*ill;
7572 	ipif_t	*ipif;
7573 	int	flags;
7574 	int	numlifs = 0;
7575 	size_t	lifc_bufsize;
7576 	struct	lifreq *lifr;
7577 	sa_family_t	family;
7578 	struct	sockaddr_in	*sin;
7579 	struct	sockaddr_in6	*sin6;
7580 	ill_walk_context_t	ctx;
7581 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7582 	int32_t	lifclen;
7583 	zoneid_t zoneid;
7584 	STRUCT_HANDLE(lifconf, lifc);
7585 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
7586 
7587 	ip1dbg(("ip_sioctl_get_lifconf"));
7588 
7589 	ASSERT(q->q_next == NULL);
7590 
7591 	zoneid = Q_TO_CONN(q)->conn_zoneid;
7592 
7593 	/* Existence verified in ip_wput_nondata */
7594 	mp1 = mp->b_cont->b_cont;
7595 
7596 	/*
7597 	 * An extended version of SIOCGIFCONF that takes an
7598 	 * additional address family and flags field.
7599 	 * AF_UNSPEC retrieve both IPv4 and IPv6.
7600 	 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7601 	 * interfaces are omitted.
7602 	 * Similarly, IPIF_TEMPORARY interfaces are omitted
7603 	 * unless LIFC_TEMPORARY is specified.
7604 	 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7605 	 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7606 	 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7607 	 * has priority over LIFC_NOXMIT.
7608 	 */
7609 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7610 
7611 	if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7612 		return (EINVAL);
7613 
7614 	/*
7615 	 * Must be (better be!) continuation of a TRANSPARENT
7616 	 * IOCTL.  We just copied in the lifconf structure.
7617 	 */
7618 	STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7619 
7620 	family = STRUCT_FGET(lifc, lifc_family);
7621 	flags = STRUCT_FGET(lifc, lifc_flags);
7622 
7623 	switch (family) {
7624 	case AF_UNSPEC:
7625 		/*
7626 		 * walk all ILL's.
7627 		 */
7628 		list = MAX_G_HEADS;
7629 		break;
7630 	case AF_INET:
7631 		/*
7632 		 * walk only IPV4 ILL's.
7633 		 */
7634 		list = IP_V4_G_HEAD;
7635 		break;
7636 	case AF_INET6:
7637 		/*
7638 		 * walk only IPV6 ILL's.
7639 		 */
7640 		list = IP_V6_G_HEAD;
7641 		break;
7642 	default:
7643 		return (EAFNOSUPPORT);
7644 	}
7645 
7646 	/*
7647 	 * Allocate a buffer to hold requested information.
7648 	 *
7649 	 * If lifc_len is larger than what is needed, we only
7650 	 * allocate what we will use.
7651 	 *
7652 	 * If lifc_len is smaller than what is needed, return
7653 	 * EINVAL.
7654 	 */
7655 	numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7656 	lifc_bufsize = numlifs * sizeof (struct lifreq);
7657 	lifclen = STRUCT_FGET(lifc, lifc_len);
7658 	if (lifc_bufsize > lifclen) {
7659 		if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7660 			return (EINVAL);
7661 		else
7662 			lifc_bufsize = lifclen;
7663 	}
7664 
7665 	mp1 = mi_copyout_alloc(q, mp,
7666 	    STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7667 	if (mp1 == NULL)
7668 		return (ENOMEM);
7669 
7670 	mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7671 	bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7672 
7673 	lifr = (struct lifreq *)mp1->b_rptr;
7674 
7675 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7676 	ill = ill_first(list, list, &ctx, ipst);
7677 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7678 		if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7679 			continue;
7680 
7681 		for (ipif = ill->ill_ipif; ipif != NULL;
7682 		    ipif = ipif->ipif_next) {
7683 			if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7684 			    !(flags & LIFC_NOXMIT))
7685 				continue;
7686 
7687 			if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7688 			    !(flags & LIFC_TEMPORARY))
7689 				continue;
7690 
7691 			if (((ipif->ipif_flags &
7692 			    (IPIF_NOXMIT|IPIF_NOLOCAL|
7693 			    IPIF_DEPRECATED)) ||
7694 			    IS_LOOPBACK(ill) ||
7695 			    !(ipif->ipif_flags & IPIF_UP)) &&
7696 			    (flags & LIFC_EXTERNAL_SOURCE))
7697 				continue;
7698 
7699 			if (zoneid != ipif->ipif_zoneid &&
7700 			    ipif->ipif_zoneid != ALL_ZONES &&
7701 			    (zoneid != GLOBAL_ZONEID ||
7702 			    !(flags & LIFC_ALLZONES)))
7703 				continue;
7704 
7705 			if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7706 				if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7707 					rw_exit(&ipst->ips_ill_g_lock);
7708 					return (EINVAL);
7709 				} else {
7710 					goto lif_copydone;
7711 				}
7712 			}
7713 
7714 			ipif_get_name(ipif, lifr->lifr_name,
7715 			    sizeof (lifr->lifr_name));
7716 			lifr->lifr_type = ill->ill_type;
7717 			if (ipif->ipif_isv6) {
7718 				sin6 = (sin6_t *)&lifr->lifr_addr;
7719 				*sin6 = sin6_null;
7720 				sin6->sin6_family = AF_INET6;
7721 				sin6->sin6_addr =
7722 				    ipif->ipif_v6lcl_addr;
7723 				lifr->lifr_addrlen =
7724 				    ip_mask_to_plen_v6(
7725 				    &ipif->ipif_v6net_mask);
7726 			} else {
7727 				sin = (sin_t *)&lifr->lifr_addr;
7728 				*sin = sin_null;
7729 				sin->sin_family = AF_INET;
7730 				sin->sin_addr.s_addr =
7731 				    ipif->ipif_lcl_addr;
7732 				lifr->lifr_addrlen =
7733 				    ip_mask_to_plen(
7734 				    ipif->ipif_net_mask);
7735 			}
7736 			lifr++;
7737 		}
7738 	}
7739 lif_copydone:
7740 	rw_exit(&ipst->ips_ill_g_lock);
7741 
7742 	mp1->b_wptr = (uchar_t *)lifr;
7743 	if (STRUCT_BUF(lifc) != NULL) {
7744 		STRUCT_FSET(lifc, lifc_len,
7745 		    (int)((uchar_t *)lifr - mp1->b_rptr));
7746 	}
7747 	return (0);
7748 }
7749 
7750 static void
7751 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7752 {
7753 	ip6_asp_t *table;
7754 	size_t table_size;
7755 	mblk_t *data_mp;
7756 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7757 	ip_stack_t	*ipst;
7758 
7759 	if (q->q_next == NULL)
7760 		ipst = CONNQ_TO_IPST(q);
7761 	else
7762 		ipst = ILLQ_TO_IPST(q);
7763 
7764 	/* These two ioctls are I_STR only */
7765 	if (iocp->ioc_count == TRANSPARENT) {
7766 		miocnak(q, mp, 0, EINVAL);
7767 		return;
7768 	}
7769 
7770 	data_mp = mp->b_cont;
7771 	if (data_mp == NULL) {
7772 		/* The user passed us a NULL argument */
7773 		table = NULL;
7774 		table_size = iocp->ioc_count;
7775 	} else {
7776 		/*
7777 		 * The user provided a table.  The stream head
7778 		 * may have copied in the user data in chunks,
7779 		 * so make sure everything is pulled up
7780 		 * properly.
7781 		 */
7782 		if (MBLKL(data_mp) < iocp->ioc_count) {
7783 			mblk_t *new_data_mp;
7784 			if ((new_data_mp = msgpullup(data_mp, -1)) ==
7785 			    NULL) {
7786 				miocnak(q, mp, 0, ENOMEM);
7787 				return;
7788 			}
7789 			freemsg(data_mp);
7790 			data_mp = new_data_mp;
7791 			mp->b_cont = data_mp;
7792 		}
7793 		table = (ip6_asp_t *)data_mp->b_rptr;
7794 		table_size = iocp->ioc_count;
7795 	}
7796 
7797 	switch (iocp->ioc_cmd) {
7798 	case SIOCGIP6ADDRPOLICY:
7799 		iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7800 		if (iocp->ioc_rval == -1)
7801 			iocp->ioc_error = EINVAL;
7802 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7803 		else if (table != NULL &&
7804 		    (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7805 			ip6_asp_t *src = table;
7806 			ip6_asp32_t *dst = (void *)table;
7807 			int count = table_size / sizeof (ip6_asp_t);
7808 			int i;
7809 
7810 			/*
7811 			 * We need to do an in-place shrink of the array
7812 			 * to match the alignment attributes of the
7813 			 * 32-bit ABI looking at it.
7814 			 */
7815 			/* LINTED: logical expression always true: op "||" */
7816 			ASSERT(sizeof (*src) > sizeof (*dst));
7817 			for (i = 1; i < count; i++)
7818 				bcopy(src + i, dst + i, sizeof (*dst));
7819 		}
7820 #endif
7821 		break;
7822 
7823 	case SIOCSIP6ADDRPOLICY:
7824 		ASSERT(mp->b_prev == NULL);
7825 		mp->b_prev = (void *)q;
7826 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7827 		/*
7828 		 * We pass in the datamodel here so that the ip6_asp_replace()
7829 		 * routine can handle converting from 32-bit to native formats
7830 		 * where necessary.
7831 		 *
7832 		 * A better way to handle this might be to convert the inbound
7833 		 * data structure here, and hang it off a new 'mp'; thus the
7834 		 * ip6_asp_replace() logic would always be dealing with native
7835 		 * format data structures..
7836 		 *
7837 		 * (An even simpler way to handle these ioctls is to just
7838 		 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7839 		 * and just recompile everything that depends on it.)
7840 		 */
7841 #endif
7842 		ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7843 		    iocp->ioc_flag & IOC_MODELS);
7844 		return;
7845 	}
7846 
7847 	DB_TYPE(mp) =  (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7848 	qreply(q, mp);
7849 }
7850 
7851 static void
7852 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7853 {
7854 	mblk_t 		*data_mp;
7855 	struct dstinforeq	*dir;
7856 	uint8_t		*end, *cur;
7857 	in6_addr_t	*daddr, *saddr;
7858 	ipaddr_t	v4daddr;
7859 	ire_t		*ire;
7860 	ipaddr_t	v4setsrc;
7861 	in6_addr_t	v6setsrc;
7862 	char		*slabel, *dlabel;
7863 	boolean_t	isipv4;
7864 	int		match_ire;
7865 	ill_t		*dst_ill;
7866 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7867 	conn_t		*connp = Q_TO_CONN(q);
7868 	zoneid_t	zoneid = IPCL_ZONEID(connp);
7869 	ip_stack_t	*ipst = connp->conn_netstack->netstack_ip;
7870 	uint64_t	ipif_flags;
7871 
7872 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7873 
7874 	/*
7875 	 * This ioctl is I_STR only, and must have a
7876 	 * data mblk following the M_IOCTL mblk.
7877 	 */
7878 	data_mp = mp->b_cont;
7879 	if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7880 		miocnak(q, mp, 0, EINVAL);
7881 		return;
7882 	}
7883 
7884 	if (MBLKL(data_mp) < iocp->ioc_count) {
7885 		mblk_t *new_data_mp;
7886 
7887 		if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7888 			miocnak(q, mp, 0, ENOMEM);
7889 			return;
7890 		}
7891 		freemsg(data_mp);
7892 		data_mp = new_data_mp;
7893 		mp->b_cont = data_mp;
7894 	}
7895 	match_ire = MATCH_IRE_DSTONLY;
7896 
7897 	for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7898 	    end - cur >= sizeof (struct dstinforeq);
7899 	    cur += sizeof (struct dstinforeq)) {
7900 		dir = (struct dstinforeq *)cur;
7901 		daddr = &dir->dir_daddr;
7902 		saddr = &dir->dir_saddr;
7903 
7904 		/*
7905 		 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7906 		 * v4 mapped addresses; ire_ftable_lookup_v6()
7907 		 * and ip_select_source_v6() do not.
7908 		 */
7909 		dir->dir_dscope = ip_addr_scope_v6(daddr);
7910 		dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7911 
7912 		isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7913 		if (isipv4) {
7914 			IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7915 			v4setsrc = INADDR_ANY;
7916 			ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7917 			    NULL, match_ire, B_TRUE, 0, ipst, &v4setsrc, NULL,
7918 			    NULL);
7919 		} else {
7920 			v6setsrc = ipv6_all_zeros;
7921 			ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7922 			    NULL, match_ire, B_TRUE, 0, ipst, &v6setsrc, NULL,
7923 			    NULL);
7924 		}
7925 		ASSERT(ire != NULL);
7926 		if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7927 			ire_refrele(ire);
7928 			dir->dir_dreachable = 0;
7929 
7930 			/* move on to next dst addr */
7931 			continue;
7932 		}
7933 		dir->dir_dreachable = 1;
7934 
7935 		dst_ill = ire_nexthop_ill(ire);
7936 		if (dst_ill == NULL) {
7937 			ire_refrele(ire);
7938 			continue;
7939 		}
7940 
7941 		/* With ipmp we most likely look at the ipmp ill here */
7942 		dir->dir_dmactype = dst_ill->ill_mactype;
7943 
7944 		if (isipv4) {
7945 			ipaddr_t v4saddr;
7946 
7947 			if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7948 			    connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7949 			    &v4saddr, NULL, &ipif_flags) != 0) {
7950 				v4saddr = INADDR_ANY;
7951 				ipif_flags = 0;
7952 			}
7953 			IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7954 		} else {
7955 			if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7956 			    zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7957 			    saddr, NULL, &ipif_flags) != 0) {
7958 				*saddr = ipv6_all_zeros;
7959 				ipif_flags = 0;
7960 			}
7961 		}
7962 
7963 		dir->dir_sscope = ip_addr_scope_v6(saddr);
7964 		slabel = ip6_asp_lookup(saddr, NULL, ipst);
7965 		dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7966 		dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7967 		ire_refrele(ire);
7968 		ill_refrele(dst_ill);
7969 	}
7970 	miocack(q, mp, iocp->ioc_count, 0);
7971 }
7972 
7973 /*
7974  * Check if this is an address assigned to this machine.
7975  * Skips interfaces that are down by using ire checks.
7976  * Translates mapped addresses to v4 addresses and then
7977  * treats them as such, returning true if the v4 address
7978  * associated with this mapped address is configured.
7979  * Note: Applications will have to be careful what they do
7980  * with the response; use of mapped addresses limits
7981  * what can be done with the socket, especially with
7982  * respect to socket options and ioctls - neither IPv4
7983  * options nor IPv6 sticky options/ancillary data options
7984  * may be used.
7985  */
7986 /* ARGSUSED */
7987 int
7988 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
7989     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
7990 {
7991 	struct sioc_addrreq *sia;
7992 	sin_t *sin;
7993 	ire_t *ire;
7994 	mblk_t *mp1;
7995 	zoneid_t zoneid;
7996 	ip_stack_t	*ipst;
7997 
7998 	ip1dbg(("ip_sioctl_tmyaddr"));
7999 
8000 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8001 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8002 	ipst = CONNQ_TO_IPST(q);
8003 
8004 	/* Existence verified in ip_wput_nondata */
8005 	mp1 = mp->b_cont->b_cont;
8006 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8007 	sin = (sin_t *)&sia->sa_addr;
8008 	switch (sin->sin_family) {
8009 	case AF_INET6: {
8010 		sin6_t *sin6 = (sin6_t *)sin;
8011 
8012 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8013 			ipaddr_t v4_addr;
8014 
8015 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8016 			    v4_addr);
8017 			ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8018 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8019 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8020 		} else {
8021 			in6_addr_t v6addr;
8022 
8023 			v6addr = sin6->sin6_addr;
8024 			ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8025 			    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8026 			    MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8027 		}
8028 		break;
8029 	}
8030 	case AF_INET: {
8031 		ipaddr_t v4addr;
8032 
8033 		v4addr = sin->sin_addr.s_addr;
8034 		ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8035 		    IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8036 		    NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8037 		break;
8038 	}
8039 	default:
8040 		return (EAFNOSUPPORT);
8041 	}
8042 	if (ire != NULL) {
8043 		sia->sa_res = 1;
8044 		ire_refrele(ire);
8045 	} else {
8046 		sia->sa_res = 0;
8047 	}
8048 	return (0);
8049 }
8050 
8051 /*
8052  * Check if this is an address assigned on-link i.e. neighbor,
8053  * and makes sure it's reachable from the current zone.
8054  * Returns true for my addresses as well.
8055  * Translates mapped addresses to v4 addresses and then
8056  * treats them as such, returning true if the v4 address
8057  * associated with this mapped address is configured.
8058  * Note: Applications will have to be careful what they do
8059  * with the response; use of mapped addresses limits
8060  * what can be done with the socket, especially with
8061  * respect to socket options and ioctls - neither IPv4
8062  * options nor IPv6 sticky options/ancillary data options
8063  * may be used.
8064  */
8065 /* ARGSUSED */
8066 int
8067 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8068     ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8069 {
8070 	struct sioc_addrreq *sia;
8071 	sin_t *sin;
8072 	mblk_t	*mp1;
8073 	ire_t *ire = NULL;
8074 	zoneid_t zoneid;
8075 	ip_stack_t	*ipst;
8076 
8077 	ip1dbg(("ip_sioctl_tonlink"));
8078 
8079 	ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8080 	zoneid = Q_TO_CONN(q)->conn_zoneid;
8081 	ipst = CONNQ_TO_IPST(q);
8082 
8083 	/* Existence verified in ip_wput_nondata */
8084 	mp1 = mp->b_cont->b_cont;
8085 	sia = (struct sioc_addrreq *)mp1->b_rptr;
8086 	sin = (sin_t *)&sia->sa_addr;
8087 
8088 	/*
8089 	 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8090 	 * to make sure we only look at on-link unicast address.
8091 	 */
8092 	switch (sin->sin_family) {
8093 	case AF_INET6: {
8094 		sin6_t *sin6 = (sin6_t *)sin;
8095 
8096 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8097 			ipaddr_t v4_addr;
8098 
8099 			IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8100 			    v4_addr);
8101 			if (!CLASSD(v4_addr)) {
8102 				ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8103 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8104 				    0, ipst, NULL);
8105 			}
8106 		} else {
8107 			in6_addr_t v6addr;
8108 
8109 			v6addr = sin6->sin6_addr;
8110 			if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8111 				ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8112 				    NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8113 				    ipst, NULL);
8114 			}
8115 		}
8116 		break;
8117 	}
8118 	case AF_INET: {
8119 		ipaddr_t v4addr;
8120 
8121 		v4addr = sin->sin_addr.s_addr;
8122 		if (!CLASSD(v4addr)) {
8123 			ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8124 			    zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8125 		}
8126 		break;
8127 	}
8128 	default:
8129 		return (EAFNOSUPPORT);
8130 	}
8131 	sia->sa_res = 0;
8132 	if (ire != NULL) {
8133 		ASSERT(!(ire->ire_type & IRE_MULTICAST));
8134 
8135 		if ((ire->ire_type & IRE_ONLINK) &&
8136 		    !(ire->ire_type & IRE_BROADCAST))
8137 			sia->sa_res = 1;
8138 		ire_refrele(ire);
8139 	}
8140 	return (0);
8141 }
8142 
8143 /*
8144  * TBD: implement when kernel maintaines a list of site prefixes.
8145  */
8146 /* ARGSUSED */
8147 int
8148 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8149     ip_ioctl_cmd_t *ipip, void *ifreq)
8150 {
8151 	return (ENXIO);
8152 }
8153 
8154 /* ARP IOCTLs. */
8155 /* ARGSUSED */
8156 int
8157 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8158     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8159 {
8160 	int		err;
8161 	ipaddr_t	ipaddr;
8162 	struct iocblk	*iocp;
8163 	conn_t		*connp;
8164 	struct arpreq	*ar;
8165 	struct xarpreq	*xar;
8166 	int		arp_flags, flags, alength;
8167 	uchar_t		*lladdr;
8168 	ip_stack_t	*ipst;
8169 	ill_t		*ill = ipif->ipif_ill;
8170 	ill_t		*proxy_ill = NULL;
8171 	ipmp_arpent_t	*entp = NULL;
8172 	boolean_t	proxyarp = B_FALSE;
8173 	boolean_t	if_arp_ioctl = B_FALSE;
8174 	ncec_t		*ncec = NULL;
8175 	nce_t		*nce;
8176 
8177 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8178 	connp = Q_TO_CONN(q);
8179 	ipst = connp->conn_netstack->netstack_ip;
8180 	iocp = (struct iocblk *)mp->b_rptr;
8181 
8182 	if (ipip->ipi_cmd_type == XARP_CMD) {
8183 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8184 		xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8185 		ar = NULL;
8186 
8187 		arp_flags = xar->xarp_flags;
8188 		lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8189 		if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8190 		/*
8191 		 * Validate against user's link layer address length
8192 		 * input and name and addr length limits.
8193 		 */
8194 		alength = ill->ill_phys_addr_length;
8195 		if (ipip->ipi_cmd == SIOCSXARP) {
8196 			if (alength != xar->xarp_ha.sdl_alen ||
8197 			    (alength + xar->xarp_ha.sdl_nlen >
8198 			    sizeof (xar->xarp_ha.sdl_data)))
8199 				return (EINVAL);
8200 		}
8201 	} else {
8202 		/* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8203 		ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8204 		xar = NULL;
8205 
8206 		arp_flags = ar->arp_flags;
8207 		lladdr = (uchar_t *)ar->arp_ha.sa_data;
8208 		/*
8209 		 * Theoretically, the sa_family could tell us what link
8210 		 * layer type this operation is trying to deal with. By
8211 		 * common usage AF_UNSPEC means ethernet. We'll assume
8212 		 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8213 		 * for now. Our new SIOC*XARP ioctls can be used more
8214 		 * generally.
8215 		 *
8216 		 * If the underlying media happens to have a non 6 byte
8217 		 * address, arp module will fail set/get, but the del
8218 		 * operation will succeed.
8219 		 */
8220 		alength = 6;
8221 		if ((ipip->ipi_cmd != SIOCDARP) &&
8222 		    (alength != ill->ill_phys_addr_length)) {
8223 			return (EINVAL);
8224 		}
8225 	}
8226 
8227 	/* Translate ATF* flags to NCE* flags */
8228 	flags = 0;
8229 	if (arp_flags & ATF_AUTHORITY)
8230 		flags |= NCE_F_AUTHORITY;
8231 	if (arp_flags & ATF_PERM)
8232 		flags |= NCE_F_NONUD; /* not subject to aging */
8233 	if (arp_flags & ATF_PUBL)
8234 		flags |= NCE_F_PUBLISH;
8235 
8236 	/*
8237 	 * IPMP ARP special handling:
8238 	 *
8239 	 * 1. Since ARP mappings must appear consistent across the group,
8240 	 *    prohibit changing ARP mappings on the underlying interfaces.
8241 	 *
8242 	 * 2. Since ARP mappings for IPMP data addresses are maintained by
8243 	 *    IP itself, prohibit changing them.
8244 	 *
8245 	 * 3. For proxy ARP, use a functioning hardware address in the group,
8246 	 *    provided one exists.  If one doesn't, just add the entry as-is;
8247 	 *    ipmp_illgrp_refresh_arpent() will refresh it if things change.
8248 	 */
8249 	if (IS_UNDER_IPMP(ill)) {
8250 		if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8251 			return (EPERM);
8252 	}
8253 	if (IS_IPMP(ill)) {
8254 		ipmp_illgrp_t *illg = ill->ill_grp;
8255 
8256 		switch (ipip->ipi_cmd) {
8257 		case SIOCSARP:
8258 		case SIOCSXARP:
8259 			proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8260 			if (proxy_ill != NULL) {
8261 				proxyarp = B_TRUE;
8262 				if (!ipmp_ill_is_active(proxy_ill))
8263 					proxy_ill = ipmp_illgrp_next_ill(illg);
8264 				if (proxy_ill != NULL)
8265 					lladdr = proxy_ill->ill_phys_addr;
8266 			}
8267 			/* FALLTHRU */
8268 		}
8269 	}
8270 
8271 	ipaddr = sin->sin_addr.s_addr;
8272 	/*
8273 	 * don't match across illgrp per case (1) and (2).
8274 	 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8275 	 */
8276 	nce = nce_lookup_v4(ill, &ipaddr);
8277 	if (nce != NULL)
8278 		ncec = nce->nce_common;
8279 
8280 	switch (iocp->ioc_cmd) {
8281 	case SIOCDARP:
8282 	case SIOCDXARP: {
8283 		/*
8284 		 * Delete the NCE if any.
8285 		 */
8286 		if (ncec == NULL) {
8287 			iocp->ioc_error = ENXIO;
8288 			break;
8289 		}
8290 		/* Don't allow changes to arp mappings of local addresses. */
8291 		if (NCE_MYADDR(ncec)) {
8292 			nce_refrele(nce);
8293 			return (ENOTSUP);
8294 		}
8295 		iocp->ioc_error = 0;
8296 
8297 		/*
8298 		 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8299 		 * This will delete all the nce entries on the under_ills.
8300 		 */
8301 		ncec_delete(ncec);
8302 		/*
8303 		 * Once the NCE has been deleted, then the ire_dep* consistency
8304 		 * mechanism will find any IRE which depended on the now
8305 		 * condemned NCE (as part of sending packets).
8306 		 * That mechanism handles redirects by deleting redirects
8307 		 * that refer to UNREACHABLE nces.
8308 		 */
8309 		break;
8310 	}
8311 	case SIOCGARP:
8312 	case SIOCGXARP:
8313 		if (ncec != NULL) {
8314 			lladdr = ncec->ncec_lladdr;
8315 			flags = ncec->ncec_flags;
8316 			iocp->ioc_error = 0;
8317 			ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8318 		} else {
8319 			iocp->ioc_error = ENXIO;
8320 		}
8321 		break;
8322 	case SIOCSARP:
8323 	case SIOCSXARP:
8324 		/* Don't allow changes to arp mappings of local addresses. */
8325 		if (ncec != NULL && NCE_MYADDR(ncec)) {
8326 			nce_refrele(nce);
8327 			return (ENOTSUP);
8328 		}
8329 
8330 		/* static arp entries will undergo NUD if ATF_PERM is not set */
8331 		flags |= NCE_F_STATIC;
8332 		if (!if_arp_ioctl) {
8333 			ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8334 			    lladdr, alength, flags);
8335 		} else {
8336 			ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8337 			if (ipif != NULL) {
8338 				ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8339 				    lladdr, alength, flags);
8340 				ipif_refrele(ipif);
8341 			}
8342 		}
8343 		if (nce != NULL) {
8344 			nce_refrele(nce);
8345 			nce = NULL;
8346 		}
8347 		/*
8348 		 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8349 		 * by nce_add_common()
8350 		 */
8351 		err = nce_lookup_then_add_v4(ill, lladdr,
8352 		    ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8353 		    &nce);
8354 		if (err == EEXIST) {
8355 			ncec = nce->nce_common;
8356 			mutex_enter(&ncec->ncec_lock);
8357 			ncec->ncec_state = ND_REACHABLE;
8358 			ncec->ncec_flags = flags;
8359 			nce_update(ncec, ND_UNCHANGED, lladdr);
8360 			mutex_exit(&ncec->ncec_lock);
8361 			err = 0;
8362 		}
8363 		if (nce != NULL) {
8364 			nce_refrele(nce);
8365 			nce = NULL;
8366 		}
8367 		if (IS_IPMP(ill) && err == 0) {
8368 			entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8369 			    proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8370 			    flags);
8371 			if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8372 				iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8373 				break;
8374 			}
8375 		}
8376 		iocp->ioc_error = err;
8377 	}
8378 
8379 	if (nce != NULL) {
8380 		nce_refrele(nce);
8381 	}
8382 
8383 	/*
8384 	 * If we created an IPMP ARP entry, mark that we've notified ARP.
8385 	 */
8386 	if (entp != NULL)
8387 		ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8388 
8389 	return (iocp->ioc_error);
8390 }
8391 
8392 /*
8393  * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8394  * the associated sin and refhold and return the associated ipif via `ci'.
8395  */
8396 int
8397 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8398     cmd_info_t *ci)
8399 {
8400 	mblk_t	*mp1;
8401 	sin_t	*sin;
8402 	conn_t	*connp;
8403 	ipif_t	*ipif;
8404 	ire_t	*ire = NULL;
8405 	ill_t	*ill = NULL;
8406 	boolean_t exists;
8407 	ip_stack_t *ipst;
8408 	struct arpreq *ar;
8409 	struct xarpreq *xar;
8410 	struct sockaddr_dl *sdl;
8411 
8412 	/* ioctl comes down on a conn */
8413 	ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8414 	connp = Q_TO_CONN(q);
8415 	if (connp->conn_family == AF_INET6)
8416 		return (ENXIO);
8417 
8418 	ipst = connp->conn_netstack->netstack_ip;
8419 
8420 	/* Verified in ip_wput_nondata */
8421 	mp1 = mp->b_cont->b_cont;
8422 
8423 	if (ipip->ipi_cmd_type == XARP_CMD) {
8424 		ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8425 		xar = (struct xarpreq *)mp1->b_rptr;
8426 		sin = (sin_t *)&xar->xarp_pa;
8427 		sdl = &xar->xarp_ha;
8428 
8429 		if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8430 			return (ENXIO);
8431 		if (sdl->sdl_nlen >= LIFNAMSIZ)
8432 			return (EINVAL);
8433 	} else {
8434 		ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8435 		ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8436 		ar = (struct arpreq *)mp1->b_rptr;
8437 		sin = (sin_t *)&ar->arp_pa;
8438 	}
8439 
8440 	if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8441 		ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8442 		    B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8443 		if (ipif == NULL)
8444 			return (ENXIO);
8445 		if (ipif->ipif_id != 0) {
8446 			ipif_refrele(ipif);
8447 			return (ENXIO);
8448 		}
8449 	} else {
8450 		/*
8451 		 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8452 		 * of 0: use the IP address to find the ipif.  If the IP
8453 		 * address is an IPMP test address, ire_ftable_lookup() will
8454 		 * find the wrong ill, so we first do an ipif_lookup_addr().
8455 		 */
8456 		ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8457 		    ipst);
8458 		if (ipif == NULL) {
8459 			ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8460 			    0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8461 			    NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8462 			if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8463 				if (ire != NULL)
8464 					ire_refrele(ire);
8465 				return (ENXIO);
8466 			}
8467 			ASSERT(ire != NULL && ill != NULL);
8468 			ipif = ill->ill_ipif;
8469 			ipif_refhold(ipif);
8470 			ire_refrele(ire);
8471 		}
8472 	}
8473 
8474 	if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8475 		ipif_refrele(ipif);
8476 		return (ENXIO);
8477 	}
8478 
8479 	ci->ci_sin = sin;
8480 	ci->ci_ipif = ipif;
8481 	return (0);
8482 }
8483 
8484 /*
8485  * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8486  * value of `ioccmd'.  While an illgrp is linked to an ipmp_grp_t, it is
8487  * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8488  * up and thus an ill can join that illgrp.
8489  *
8490  * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8491  * open()/close() primarily because close() is not allowed to fail or block
8492  * forever.  On the other hand, I_PUNLINK *can* fail, and there's no reason
8493  * why anyone should ever need to I_PUNLINK an in-use IPMP stream.  To ensure
8494  * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8495  * I_PUNLINK) we defer linking to I_PLINK.  Separately, we also fail attempts
8496  * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8497  * state if I_UNLINK didn't occur.
8498  *
8499  * Note that for each plumb/unplumb operation, we may end up here more than
8500  * once because of the way ifconfig works.  However, it's OK to link the same
8501  * illgrp more than once, or unlink an illgrp that's already unlinked.
8502  */
8503 static int
8504 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8505 {
8506 	int err;
8507 	ip_stack_t *ipst = ill->ill_ipst;
8508 
8509 	ASSERT(IS_IPMP(ill));
8510 	ASSERT(IAM_WRITER_ILL(ill));
8511 
8512 	switch (ioccmd) {
8513 	case I_LINK:
8514 		return (ENOTSUP);
8515 
8516 	case I_PLINK:
8517 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8518 		ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8519 		rw_exit(&ipst->ips_ipmp_lock);
8520 		break;
8521 
8522 	case I_PUNLINK:
8523 		/*
8524 		 * Require all UP ipifs be brought down prior to unlinking the
8525 		 * illgrp so any associated IREs (and other state) is torched.
8526 		 */
8527 		if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8528 			return (EBUSY);
8529 
8530 		/*
8531 		 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8532 		 * with an SIOCSLIFGROUPNAME request from an ill trying to
8533 		 * join this group.  Specifically: ills trying to join grab
8534 		 * ipmp_lock and bump a "pending join" counter checked by
8535 		 * ipmp_illgrp_unlink_grp().  During the unlink no new pending
8536 		 * joins can occur (since we have ipmp_lock).  Once we drop
8537 		 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8538 		 * find the illgrp (since we unlinked it) and will return
8539 		 * EAFNOSUPPORT.  This will then take them back through the
8540 		 * IPMP meta-interface plumbing logic in ifconfig, and thus
8541 		 * back through I_PLINK above.
8542 		 */
8543 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8544 		err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8545 		rw_exit(&ipst->ips_ipmp_lock);
8546 		return (err);
8547 	default:
8548 		break;
8549 	}
8550 	return (0);
8551 }
8552 
8553 /*
8554  * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8555  * atomically set/clear the muxids. Also complete the ioctl by acking or
8556  * naking it.  Note that the code is structured such that the link type,
8557  * whether it's persistent or not, is treated equally.  ifconfig(1M) and
8558  * its clones use the persistent link, while pppd(1M) and perhaps many
8559  * other daemons may use non-persistent link.  When combined with some
8560  * ill_t states, linking and unlinking lower streams may be used as
8561  * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8562  */
8563 /* ARGSUSED */
8564 void
8565 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8566 {
8567 	mblk_t		*mp1;
8568 	struct linkblk	*li;
8569 	int		ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8570 	int		err = 0;
8571 
8572 	ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8573 	    ioccmd == I_LINK || ioccmd == I_UNLINK);
8574 
8575 	mp1 = mp->b_cont;	/* This is the linkblk info */
8576 	li = (struct linkblk *)mp1->b_rptr;
8577 
8578 	err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8579 	if (err == EINPROGRESS)
8580 		return;
8581 done:
8582 	if (err == 0)
8583 		miocack(q, mp, 0, 0);
8584 	else
8585 		miocnak(q, mp, 0, err);
8586 
8587 	/* Conn was refheld in ip_sioctl_copyin_setup */
8588 	if (CONN_Q(q))
8589 		CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8590 }
8591 
8592 /*
8593  * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8594  * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8595  * module stream).  If `doconsist' is set, then do the extended consistency
8596  * checks requested by ifconfig(1M) and (atomically) set ill_muxid here.
8597  * Returns zero on success, EINPROGRESS if the operation is still pending, or
8598  * an error code on failure.
8599  */
8600 static int
8601 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8602     struct linkblk *li)
8603 {
8604 	int		err = 0;
8605 	ill_t  		*ill;
8606 	queue_t		*ipwq, *dwq;
8607 	const char	*name;
8608 	struct qinit	*qinfo;
8609 	boolean_t	islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8610 	boolean_t	entered_ipsq = B_FALSE;
8611 	boolean_t	is_ip = B_FALSE;
8612 	arl_t		*arl;
8613 
8614 	/*
8615 	 * Walk the lower stream to verify it's the IP module stream.
8616 	 * The IP module is identified by its name, wput function,
8617 	 * and non-NULL q_next.  STREAMS ensures that the lower stream
8618 	 * (li->l_qbot) will not vanish until this ioctl completes.
8619 	 */
8620 	for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8621 		qinfo = ipwq->q_qinfo;
8622 		name = qinfo->qi_minfo->mi_idname;
8623 		if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8624 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8625 			is_ip = B_TRUE;
8626 			break;
8627 		}
8628 		if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8629 		    qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8630 			break;
8631 		}
8632 	}
8633 
8634 	/*
8635 	 * If this isn't an IP module stream, bail.
8636 	 */
8637 	if (ipwq == NULL)
8638 		return (0);
8639 
8640 	if (!is_ip) {
8641 		arl = (arl_t *)ipwq->q_ptr;
8642 		ill = arl_to_ill(arl);
8643 		if (ill == NULL)
8644 			return (0);
8645 	} else {
8646 		ill = ipwq->q_ptr;
8647 	}
8648 	ASSERT(ill != NULL);
8649 
8650 	if (ipsq == NULL) {
8651 		ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8652 		    NEW_OP, B_FALSE);
8653 		if (ipsq == NULL) {
8654 			if (!is_ip)
8655 				ill_refrele(ill);
8656 			return (EINPROGRESS);
8657 		}
8658 		entered_ipsq = B_TRUE;
8659 	}
8660 	ASSERT(IAM_WRITER_ILL(ill));
8661 	mutex_enter(&ill->ill_lock);
8662 	if (!is_ip) {
8663 		if (islink && ill->ill_muxid == 0) {
8664 			/*
8665 			 * Plumbing has to be done with IP plumbed first, arp
8666 			 * second, but here we have arp being plumbed first.
8667 			 */
8668 			mutex_exit(&ill->ill_lock);
8669 			ipsq_exit(ipsq);
8670 			ill_refrele(ill);
8671 			return (EINVAL);
8672 		}
8673 	}
8674 	mutex_exit(&ill->ill_lock);
8675 	if (!is_ip) {
8676 		arl->arl_muxid = islink ? li->l_index : 0;
8677 		ill_refrele(ill);
8678 		goto done;
8679 	}
8680 
8681 	if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8682 		goto done;
8683 
8684 	/*
8685 	 * As part of I_{P}LINKing, stash the number of downstream modules and
8686 	 * the read queue of the module immediately below IP in the ill.
8687 	 * These are used during the capability negotiation below.
8688 	 */
8689 	ill->ill_lmod_rq = NULL;
8690 	ill->ill_lmod_cnt = 0;
8691 	if (islink && ((dwq = ipwq->q_next) != NULL)) {
8692 		ill->ill_lmod_rq = RD(dwq);
8693 		for (; dwq != NULL; dwq = dwq->q_next)
8694 			ill->ill_lmod_cnt++;
8695 	}
8696 
8697 	ill->ill_muxid = islink ? li->l_index : 0;
8698 
8699 	/*
8700 	 * Mark the ipsq busy until the capability operations initiated below
8701 	 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8702 	 * returns, but the capability operation may complete asynchronously
8703 	 * much later.
8704 	 */
8705 	ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8706 	/*
8707 	 * If there's at least one up ipif on this ill, then we're bound to
8708 	 * the underlying driver via DLPI.  In that case, renegotiate
8709 	 * capabilities to account for any possible change in modules
8710 	 * interposed between IP and the driver.
8711 	 */
8712 	if (ill->ill_ipif_up_count > 0) {
8713 		if (islink)
8714 			ill_capability_probe(ill);
8715 		else
8716 			ill_capability_reset(ill, B_FALSE);
8717 	}
8718 	ipsq_current_finish(ipsq);
8719 done:
8720 	if (entered_ipsq)
8721 		ipsq_exit(ipsq);
8722 
8723 	return (err);
8724 }
8725 
8726 /*
8727  * Search the ioctl command in the ioctl tables and return a pointer
8728  * to the ioctl command information. The ioctl command tables are
8729  * static and fully populated at compile time.
8730  */
8731 ip_ioctl_cmd_t *
8732 ip_sioctl_lookup(int ioc_cmd)
8733 {
8734 	int index;
8735 	ip_ioctl_cmd_t *ipip;
8736 	ip_ioctl_cmd_t *ipip_end;
8737 
8738 	if (ioc_cmd == IPI_DONTCARE)
8739 		return (NULL);
8740 
8741 	/*
8742 	 * Do a 2 step search. First search the indexed table
8743 	 * based on the least significant byte of the ioctl cmd.
8744 	 * If we don't find a match, then search the misc table
8745 	 * serially.
8746 	 */
8747 	index = ioc_cmd & 0xFF;
8748 	if (index < ip_ndx_ioctl_count) {
8749 		ipip = &ip_ndx_ioctl_table[index];
8750 		if (ipip->ipi_cmd == ioc_cmd) {
8751 			/* Found a match in the ndx table */
8752 			return (ipip);
8753 		}
8754 	}
8755 
8756 	/* Search the misc table */
8757 	ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8758 	for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8759 		if (ipip->ipi_cmd == ioc_cmd)
8760 			/* Found a match in the misc table */
8761 			return (ipip);
8762 	}
8763 
8764 	return (NULL);
8765 }
8766 
8767 /*
8768  * Wrapper function for resuming deferred ioctl processing
8769  * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
8770  * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
8771  */
8772 /* ARGSUSED */
8773 void
8774 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
8775     void *dummy_arg)
8776 {
8777 	ip_sioctl_copyin_setup(q, mp);
8778 }
8779 
8780 /*
8781  * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
8782  * that arrives.  Most of the IOCTLs are "socket" IOCTLs which we handle
8783  * in either I_STR or TRANSPARENT form, using the mi_copy facility.
8784  * We establish here the size of the block to be copied in.  mi_copyin
8785  * arranges for this to happen, an processing continues in ip_wput_nondata with
8786  * an M_IOCDATA message.
8787  */
8788 void
8789 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
8790 {
8791 	int	copyin_size;
8792 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8793 	ip_ioctl_cmd_t *ipip;
8794 	cred_t *cr;
8795 	ip_stack_t	*ipst;
8796 
8797 	if (CONN_Q(q))
8798 		ipst = CONNQ_TO_IPST(q);
8799 	else
8800 		ipst = ILLQ_TO_IPST(q);
8801 
8802 	ipip = ip_sioctl_lookup(iocp->ioc_cmd);
8803 	if (ipip == NULL) {
8804 		/*
8805 		 * The ioctl is not one we understand or own.
8806 		 * Pass it along to be processed down stream,
8807 		 * if this is a module instance of IP, else nak
8808 		 * the ioctl.
8809 		 */
8810 		if (q->q_next == NULL) {
8811 			goto nak;
8812 		} else {
8813 			putnext(q, mp);
8814 			return;
8815 		}
8816 	}
8817 
8818 	/*
8819 	 * If this is deferred, then we will do all the checks when we
8820 	 * come back.
8821 	 */
8822 	if ((iocp->ioc_cmd == SIOCGDSTINFO ||
8823 	    iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
8824 		ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
8825 		return;
8826 	}
8827 
8828 	/*
8829 	 * Only allow a very small subset of IP ioctls on this stream if
8830 	 * IP is a module and not a driver. Allowing ioctls to be processed
8831 	 * in this case may cause assert failures or data corruption.
8832 	 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
8833 	 * ioctls allowed on an IP module stream, after which this stream
8834 	 * normally becomes a multiplexor (at which time the stream head
8835 	 * will fail all ioctls).
8836 	 */
8837 	if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
8838 		goto nak;
8839 	}
8840 
8841 	/* Make sure we have ioctl data to process. */
8842 	if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
8843 		goto nak;
8844 
8845 	/*
8846 	 * Prefer dblk credential over ioctl credential; some synthesized
8847 	 * ioctls have kcred set because there's no way to crhold()
8848 	 * a credential in some contexts.  (ioc_cr is not crfree() by
8849 	 * the framework; the caller of ioctl needs to hold the reference
8850 	 * for the duration of the call).
8851 	 */
8852 	cr = msg_getcred(mp, NULL);
8853 	if (cr == NULL)
8854 		cr = iocp->ioc_cr;
8855 
8856 	/* Make sure normal users don't send down privileged ioctls */
8857 	if ((ipip->ipi_flags & IPI_PRIV) &&
8858 	    (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
8859 		/* We checked the privilege earlier but log it here */
8860 		miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
8861 		return;
8862 	}
8863 
8864 	/*
8865 	 * The ioctl command tables can only encode fixed length
8866 	 * ioctl data. If the length is variable, the table will
8867 	 * encode the length as zero. Such special cases are handled
8868 	 * below in the switch.
8869 	 */
8870 	if (ipip->ipi_copyin_size != 0) {
8871 		mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
8872 		return;
8873 	}
8874 
8875 	switch (iocp->ioc_cmd) {
8876 	case O_SIOCGIFCONF:
8877 	case SIOCGIFCONF:
8878 		/*
8879 		 * This IOCTL is hilarious.  See comments in
8880 		 * ip_sioctl_get_ifconf for the story.
8881 		 */
8882 		if (iocp->ioc_count == TRANSPARENT)
8883 			copyin_size = SIZEOF_STRUCT(ifconf,
8884 			    iocp->ioc_flag);
8885 		else
8886 			copyin_size = iocp->ioc_count;
8887 		mi_copyin(q, mp, NULL, copyin_size);
8888 		return;
8889 
8890 	case O_SIOCGLIFCONF:
8891 	case SIOCGLIFCONF:
8892 		copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
8893 		mi_copyin(q, mp, NULL, copyin_size);
8894 		return;
8895 
8896 	case SIOCGLIFSRCOF:
8897 		copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
8898 		mi_copyin(q, mp, NULL, copyin_size);
8899 		return;
8900 	case SIOCGIP6ADDRPOLICY:
8901 		ip_sioctl_ip6addrpolicy(q, mp);
8902 		ip6_asp_table_refrele(ipst);
8903 		return;
8904 
8905 	case SIOCSIP6ADDRPOLICY:
8906 		ip_sioctl_ip6addrpolicy(q, mp);
8907 		return;
8908 
8909 	case SIOCGDSTINFO:
8910 		ip_sioctl_dstinfo(q, mp);
8911 		ip6_asp_table_refrele(ipst);
8912 		return;
8913 
8914 	case I_PLINK:
8915 	case I_PUNLINK:
8916 	case I_LINK:
8917 	case I_UNLINK:
8918 		/*
8919 		 * We treat non-persistent link similarly as the persistent
8920 		 * link case, in terms of plumbing/unplumbing, as well as
8921 		 * dynamic re-plumbing events indicator.  See comments
8922 		 * in ip_sioctl_plink() for more.
8923 		 *
8924 		 * Request can be enqueued in the 'ipsq' while waiting
8925 		 * to become exclusive. So bump up the conn ref.
8926 		 */
8927 		if (CONN_Q(q))
8928 			CONN_INC_REF(Q_TO_CONN(q));
8929 		ip_sioctl_plink(NULL, q, mp, NULL);
8930 		return;
8931 
8932 	case ND_GET:
8933 	case ND_SET:
8934 		/*
8935 		 * Use of the nd table requires holding the reader lock.
8936 		 * Modifying the nd table thru nd_load/nd_unload requires
8937 		 * the writer lock.
8938 		 */
8939 		rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER);
8940 		if (nd_getset(q, ipst->ips_ip_g_nd, mp)) {
8941 			rw_exit(&ipst->ips_ip_g_nd_lock);
8942 
8943 			if (iocp->ioc_error)
8944 				iocp->ioc_count = 0;
8945 			mp->b_datap->db_type = M_IOCACK;
8946 			qreply(q, mp);
8947 			return;
8948 		}
8949 		rw_exit(&ipst->ips_ip_g_nd_lock);
8950 		/*
8951 		 * We don't understand this subioctl of ND_GET / ND_SET.
8952 		 * Maybe intended for some driver / module below us
8953 		 */
8954 		if (q->q_next) {
8955 			putnext(q, mp);
8956 		} else {
8957 			iocp->ioc_error = ENOENT;
8958 			mp->b_datap->db_type = M_IOCNAK;
8959 			iocp->ioc_count = 0;
8960 			qreply(q, mp);
8961 		}
8962 		return;
8963 
8964 	case IP_IOCTL:
8965 		ip_wput_ioctl(q, mp);
8966 		return;
8967 
8968 	case SIOCILB:
8969 		/* The ioctl length varies depending on the ILB command. */
8970 		copyin_size = iocp->ioc_count;
8971 		if (copyin_size < sizeof (ilb_cmd_t))
8972 			goto nak;
8973 		mi_copyin(q, mp, NULL, copyin_size);
8974 		return;
8975 
8976 	default:
8977 		cmn_err(CE_PANIC, "should not happen ");
8978 	}
8979 nak:
8980 	if (mp->b_cont != NULL) {
8981 		freemsg(mp->b_cont);
8982 		mp->b_cont = NULL;
8983 	}
8984 	iocp->ioc_error = EINVAL;
8985 	mp->b_datap->db_type = M_IOCNAK;
8986 	iocp->ioc_count = 0;
8987 	qreply(q, mp);
8988 }
8989 
8990 static void
8991 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
8992 {
8993 	struct arpreq *ar;
8994 	struct xarpreq *xar;
8995 	mblk_t	*tmp;
8996 	struct iocblk *iocp;
8997 	int x_arp_ioctl = B_FALSE;
8998 	int *flagsp;
8999 	char *storage = NULL;
9000 
9001 	ASSERT(ill != NULL);
9002 
9003 	iocp = (struct iocblk *)mp->b_rptr;
9004 	ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9005 
9006 	tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9007 	if ((iocp->ioc_cmd == SIOCGXARP) ||
9008 	    (iocp->ioc_cmd == SIOCSXARP)) {
9009 		x_arp_ioctl = B_TRUE;
9010 		xar = (struct xarpreq *)tmp->b_rptr;
9011 		flagsp = &xar->xarp_flags;
9012 		storage = xar->xarp_ha.sdl_data;
9013 	} else {
9014 		ar = (struct arpreq *)tmp->b_rptr;
9015 		flagsp = &ar->arp_flags;
9016 		storage = ar->arp_ha.sa_data;
9017 	}
9018 
9019 	/*
9020 	 * We're done if this is not an SIOCG{X}ARP
9021 	 */
9022 	if (x_arp_ioctl) {
9023 		storage += ill_xarp_info(&xar->xarp_ha, ill);
9024 		if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9025 		    sizeof (xar->xarp_ha.sdl_data)) {
9026 			iocp->ioc_error = EINVAL;
9027 			return;
9028 		}
9029 	}
9030 	*flagsp = ATF_INUSE;
9031 	/*
9032 	 * If /sbin/arp told us we are the authority using the "permanent"
9033 	 * flag, or if this is one of my addresses print "permanent"
9034 	 * in the /sbin/arp output.
9035 	 */
9036 	if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9037 		*flagsp |= ATF_AUTHORITY;
9038 	if (flags & NCE_F_NONUD)
9039 		*flagsp |= ATF_PERM; /* not subject to aging */
9040 	if (flags & NCE_F_PUBLISH)
9041 		*flagsp |= ATF_PUBL;
9042 	if (hwaddr != NULL) {
9043 		*flagsp |= ATF_COM;
9044 		bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9045 	}
9046 }
9047 
9048 /*
9049  * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9050  * interface) create the next available logical interface for this
9051  * physical interface.
9052  * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9053  * ipif with the specified name.
9054  *
9055  * If the address family is not AF_UNSPEC then set the address as well.
9056  *
9057  * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9058  * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9059  *
9060  * Executed as a writer on the ill.
9061  * So no lock is needed to traverse the ipif chain, or examine the
9062  * phyint flags.
9063  */
9064 /* ARGSUSED */
9065 int
9066 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9067     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9068 {
9069 	mblk_t	*mp1;
9070 	struct lifreq *lifr;
9071 	boolean_t	isv6;
9072 	boolean_t	exists;
9073 	char 	*name;
9074 	char	*endp;
9075 	char	*cp;
9076 	int	namelen;
9077 	ipif_t	*ipif;
9078 	long	id;
9079 	ipsq_t	*ipsq;
9080 	ill_t	*ill;
9081 	sin_t	*sin;
9082 	int	err = 0;
9083 	boolean_t found_sep = B_FALSE;
9084 	conn_t	*connp;
9085 	zoneid_t zoneid;
9086 	ip_stack_t *ipst = CONNQ_TO_IPST(q);
9087 
9088 	ASSERT(q->q_next == NULL);
9089 	ip1dbg(("ip_sioctl_addif\n"));
9090 	/* Existence of mp1 has been checked in ip_wput_nondata */
9091 	mp1 = mp->b_cont->b_cont;
9092 	/*
9093 	 * Null terminate the string to protect against buffer
9094 	 * overrun. String was generated by user code and may not
9095 	 * be trusted.
9096 	 */
9097 	lifr = (struct lifreq *)mp1->b_rptr;
9098 	lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9099 	name = lifr->lifr_name;
9100 	ASSERT(CONN_Q(q));
9101 	connp = Q_TO_CONN(q);
9102 	isv6 = (connp->conn_family == AF_INET6);
9103 	zoneid = connp->conn_zoneid;
9104 	namelen = mi_strlen(name);
9105 	if (namelen == 0)
9106 		return (EINVAL);
9107 
9108 	exists = B_FALSE;
9109 	if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9110 	    (mi_strcmp(name, ipif_loopback_name) == 0)) {
9111 		/*
9112 		 * Allow creating lo0 using SIOCLIFADDIF.
9113 		 * can't be any other writer thread. So can pass null below
9114 		 * for the last 4 args to ipif_lookup_name.
9115 		 */
9116 		ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9117 		    &exists, isv6, zoneid, ipst);
9118 		/* Prevent any further action */
9119 		if (ipif == NULL) {
9120 			return (ENOBUFS);
9121 		} else if (!exists) {
9122 			/* We created the ipif now and as writer */
9123 			ipif_refrele(ipif);
9124 			return (0);
9125 		} else {
9126 			ill = ipif->ipif_ill;
9127 			ill_refhold(ill);
9128 			ipif_refrele(ipif);
9129 		}
9130 	} else {
9131 		/* Look for a colon in the name. */
9132 		endp = &name[namelen];
9133 		for (cp = endp; --cp > name; ) {
9134 			if (*cp == IPIF_SEPARATOR_CHAR) {
9135 				found_sep = B_TRUE;
9136 				/*
9137 				 * Reject any non-decimal aliases for plumbing
9138 				 * of logical interfaces. Aliases with leading
9139 				 * zeroes are also rejected as they introduce
9140 				 * ambiguity in the naming of the interfaces.
9141 				 * Comparing with "0" takes care of all such
9142 				 * cases.
9143 				 */
9144 				if ((strncmp("0", cp+1, 1)) == 0)
9145 					return (EINVAL);
9146 
9147 				if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9148 				    id <= 0 || *endp != '\0') {
9149 					return (EINVAL);
9150 				}
9151 				*cp = '\0';
9152 				break;
9153 			}
9154 		}
9155 		ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9156 		if (found_sep)
9157 			*cp = IPIF_SEPARATOR_CHAR;
9158 		if (ill == NULL)
9159 			return (ENXIO);
9160 	}
9161 
9162 	ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9163 	    B_TRUE);
9164 
9165 	/*
9166 	 * Release the refhold due to the lookup, now that we are excl
9167 	 * or we are just returning
9168 	 */
9169 	ill_refrele(ill);
9170 
9171 	if (ipsq == NULL)
9172 		return (EINPROGRESS);
9173 
9174 	/* We are now exclusive on the IPSQ */
9175 	ASSERT(IAM_WRITER_ILL(ill));
9176 
9177 	if (found_sep) {
9178 		/* Now see if there is an IPIF with this unit number. */
9179 		for (ipif = ill->ill_ipif; ipif != NULL;
9180 		    ipif = ipif->ipif_next) {
9181 			if (ipif->ipif_id == id) {
9182 				err = EEXIST;
9183 				goto done;
9184 			}
9185 		}
9186 	}
9187 
9188 	/*
9189 	 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9190 	 * of lo0.  Plumbing for lo0:0 happens in ipif_lookup_on_name()
9191 	 * instead.
9192 	 */
9193 	if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9194 	    B_TRUE, B_TRUE, &err)) == NULL) {
9195 		goto done;
9196 	}
9197 
9198 	/* Return created name with ioctl */
9199 	(void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9200 	    IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9201 	ip1dbg(("created %s\n", lifr->lifr_name));
9202 
9203 	/* Set address */
9204 	sin = (sin_t *)&lifr->lifr_addr;
9205 	if (sin->sin_family != AF_UNSPEC) {
9206 		err = ip_sioctl_addr(ipif, sin, q, mp,
9207 		    &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9208 	}
9209 
9210 done:
9211 	ipsq_exit(ipsq);
9212 	return (err);
9213 }
9214 
9215 /*
9216  * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9217  * interface) delete it based on the IP address (on this physical interface).
9218  * Otherwise delete it based on the ipif_id.
9219  * Also, special handling to allow a removeif of lo0.
9220  */
9221 /* ARGSUSED */
9222 int
9223 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9224     ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9225 {
9226 	conn_t		*connp;
9227 	ill_t		*ill = ipif->ipif_ill;
9228 	boolean_t	 success;
9229 	ip_stack_t	*ipst;
9230 
9231 	ipst = CONNQ_TO_IPST(q);
9232 
9233 	ASSERT(q->q_next == NULL);
9234 	ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9235 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9236 	ASSERT(IAM_WRITER_IPIF(ipif));
9237 
9238 	connp = Q_TO_CONN(q);
9239 	/*
9240 	 * Special case for unplumbing lo0 (the loopback physical interface).
9241 	 * If unplumbing lo0, the incoming address structure has been
9242 	 * initialized to all zeros. When unplumbing lo0, all its logical
9243 	 * interfaces must be removed too.
9244 	 *
9245 	 * Note that this interface may be called to remove a specific
9246 	 * loopback logical interface (eg, lo0:1). But in that case
9247 	 * ipif->ipif_id != 0 so that the code path for that case is the
9248 	 * same as any other interface (meaning it skips the code directly
9249 	 * below).
9250 	 */
9251 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9252 		if (sin->sin_family == AF_UNSPEC &&
9253 		    (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9254 			/*
9255 			 * Mark it condemned. No new ref. will be made to ill.
9256 			 */
9257 			mutex_enter(&ill->ill_lock);
9258 			ill->ill_state_flags |= ILL_CONDEMNED;
9259 			for (ipif = ill->ill_ipif; ipif != NULL;
9260 			    ipif = ipif->ipif_next) {
9261 				ipif->ipif_state_flags |= IPIF_CONDEMNED;
9262 			}
9263 			mutex_exit(&ill->ill_lock);
9264 
9265 			ipif = ill->ill_ipif;
9266 			/* unplumb the loopback interface */
9267 			ill_delete(ill);
9268 			mutex_enter(&connp->conn_lock);
9269 			mutex_enter(&ill->ill_lock);
9270 
9271 			/* Are any references to this ill active */
9272 			if (ill_is_freeable(ill)) {
9273 				mutex_exit(&ill->ill_lock);
9274 				mutex_exit(&connp->conn_lock);
9275 				ill_delete_tail(ill);
9276 				mi_free(ill);
9277 				return (0);
9278 			}
9279 			success = ipsq_pending_mp_add(connp, ipif,
9280 			    CONNP_TO_WQ(connp), mp, ILL_FREE);
9281 			mutex_exit(&connp->conn_lock);
9282 			mutex_exit(&ill->ill_lock);
9283 			if (success)
9284 				return (EINPROGRESS);
9285 			else
9286 				return (EINTR);
9287 		}
9288 	}
9289 
9290 	if (ipif->ipif_id == 0) {
9291 		ipsq_t *ipsq;
9292 
9293 		/* Find based on address */
9294 		if (ipif->ipif_isv6) {
9295 			sin6_t *sin6;
9296 
9297 			if (sin->sin_family != AF_INET6)
9298 				return (EAFNOSUPPORT);
9299 
9300 			sin6 = (sin6_t *)sin;
9301 			/* We are a writer, so we should be able to lookup */
9302 			ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9303 			    ipst);
9304 		} else {
9305 			if (sin->sin_family != AF_INET)
9306 				return (EAFNOSUPPORT);
9307 
9308 			/* We are a writer, so we should be able to lookup */
9309 			ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9310 			    ipst);
9311 		}
9312 		if (ipif == NULL) {
9313 			return (EADDRNOTAVAIL);
9314 		}
9315 
9316 		/*
9317 		 * It is possible for a user to send an SIOCLIFREMOVEIF with
9318 		 * lifr_name of the physical interface but with an ip address
9319 		 * lifr_addr of a logical interface plumbed over it.
9320 		 * So update ipx_current_ipif now that ipif points to the
9321 		 * correct one.
9322 		 */
9323 		ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9324 		ipsq->ipsq_xop->ipx_current_ipif = ipif;
9325 
9326 		/* This is a writer */
9327 		ipif_refrele(ipif);
9328 	}
9329 
9330 	/*
9331 	 * Can not delete instance zero since it is tied to the ill.
9332 	 */
9333 	if (ipif->ipif_id == 0)
9334 		return (EBUSY);
9335 
9336 	mutex_enter(&ill->ill_lock);
9337 	ipif->ipif_state_flags |= IPIF_CONDEMNED;
9338 	mutex_exit(&ill->ill_lock);
9339 
9340 	ipif_free(ipif);
9341 
9342 	mutex_enter(&connp->conn_lock);
9343 	mutex_enter(&ill->ill_lock);
9344 
9345 	/* Are any references to this ipif active */
9346 	if (ipif_is_freeable(ipif)) {
9347 		mutex_exit(&ill->ill_lock);
9348 		mutex_exit(&connp->conn_lock);
9349 		ipif_non_duplicate(ipif);
9350 		(void) ipif_down_tail(ipif);
9351 		ipif_free_tail(ipif); /* frees ipif */
9352 		return (0);
9353 	}
9354 	success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9355 	    IPIF_FREE);
9356 	mutex_exit(&ill->ill_lock);
9357 	mutex_exit(&connp->conn_lock);
9358 	if (success)
9359 		return (EINPROGRESS);
9360 	else
9361 		return (EINTR);
9362 }
9363 
9364 /*
9365  * Restart the removeif ioctl. The refcnt has gone down to 0.
9366  * The ipif is already condemned. So can't find it thru lookups.
9367  */
9368 /* ARGSUSED */
9369 int
9370 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9371     mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9372 {
9373 	ill_t *ill = ipif->ipif_ill;
9374 
9375 	ASSERT(IAM_WRITER_IPIF(ipif));
9376 	ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9377 
9378 	ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9379 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9380 
9381 	if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9382 		ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9383 		ill_delete_tail(ill);
9384 		mi_free(ill);
9385 		return (0);
9386 	}
9387 
9388 	ipif_non_duplicate(ipif);
9389 	(void) ipif_down_tail(ipif);
9390 	ipif_free_tail(ipif);
9391 
9392 	return (0);
9393 }
9394 
9395 /*
9396  * Set the local interface address.
9397  * Allow an address of all zero when the interface is down.
9398  */
9399 /* ARGSUSED */
9400 int
9401 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9402     ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9403 {
9404 	int err = 0;
9405 	in6_addr_t v6addr;
9406 	boolean_t need_up = B_FALSE;
9407 
9408 	ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9409 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9410 
9411 	ASSERT(IAM_WRITER_IPIF(ipif));
9412 
9413 	if (ipif->ipif_isv6) {
9414 		sin6_t *sin6;
9415 		ill_t *ill;
9416 		phyint_t *phyi;
9417 
9418 		if (sin->sin_family != AF_INET6)
9419 			return (EAFNOSUPPORT);
9420 
9421 		sin6 = (sin6_t *)sin;
9422 		v6addr = sin6->sin6_addr;
9423 		ill = ipif->ipif_ill;
9424 		phyi = ill->ill_phyint;
9425 
9426 		/*
9427 		 * Enforce that true multicast interfaces have a link-local
9428 		 * address for logical unit 0.
9429 		 */
9430 		if (ipif->ipif_id == 0 &&
9431 		    (ill->ill_flags & ILLF_MULTICAST) &&
9432 		    !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9433 		    !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9434 		    !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9435 			return (EADDRNOTAVAIL);
9436 		}
9437 
9438 		/*
9439 		 * up interfaces shouldn't have the unspecified address
9440 		 * unless they also have the IPIF_NOLOCAL flags set and
9441 		 * have a subnet assigned.
9442 		 */
9443 		if ((ipif->ipif_flags & IPIF_UP) &&
9444 		    IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9445 		    (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9446 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9447 			return (EADDRNOTAVAIL);
9448 		}
9449 
9450 		if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9451 			return (EADDRNOTAVAIL);
9452 	} else {
9453 		ipaddr_t addr;
9454 
9455 		if (sin->sin_family != AF_INET)
9456 			return (EAFNOSUPPORT);
9457 
9458 		addr = sin->sin_addr.s_addr;
9459 
9460 		/* Allow 0 as the local address. */
9461 		if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9462 			return (EADDRNOTAVAIL);
9463 
9464 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9465 	}
9466 
9467 	/*
9468 	 * Even if there is no change we redo things just to rerun
9469 	 * ipif_set_default.
9470 	 */
9471 	if (ipif->ipif_flags & IPIF_UP) {
9472 		/*
9473 		 * Setting a new local address, make sure
9474 		 * we have net and subnet bcast ire's for
9475 		 * the old address if we need them.
9476 		 */
9477 		/*
9478 		 * If the interface is already marked up,
9479 		 * we call ipif_down which will take care
9480 		 * of ditching any IREs that have been set
9481 		 * up based on the old interface address.
9482 		 */
9483 		err = ipif_logical_down(ipif, q, mp);
9484 		if (err == EINPROGRESS)
9485 			return (err);
9486 		(void) ipif_down_tail(ipif);
9487 		need_up = 1;
9488 	}
9489 
9490 	err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9491 	return (err);
9492 }
9493 
9494 int
9495 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9496     boolean_t need_up)
9497 {
9498 	in6_addr_t v6addr;
9499 	in6_addr_t ov6addr;
9500 	ipaddr_t addr;
9501 	sin6_t	*sin6;
9502 	int	sinlen;
9503 	int	err = 0;
9504 	ill_t	*ill = ipif->ipif_ill;
9505 	boolean_t need_dl_down;
9506 	boolean_t need_arp_down;
9507 	struct iocblk *iocp;
9508 
9509 	iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9510 
9511 	ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9512 	    ill->ill_name, ipif->ipif_id, (void *)ipif));
9513 	ASSERT(IAM_WRITER_IPIF(ipif));
9514 
9515 	/* Must cancel any pending timer before taking the ill_lock */
9516 	if (ipif->ipif_recovery_id != 0)
9517 		(void) untimeout(ipif->ipif_recovery_id);
9518 	ipif->ipif_recovery_id = 0;
9519 
9520 	if (ipif->ipif_isv6) {
9521 		sin6 = (sin6_t *)sin;
9522 		v6addr = sin6->sin6_addr;
9523 		sinlen = sizeof (struct sockaddr_in6);
9524 	} else {
9525 		addr = sin->sin_addr.s_addr;
9526 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9527 		sinlen = sizeof (struct sockaddr_in);
9528 	}
9529 	mutex_enter(&ill->ill_lock);
9530 	ov6addr = ipif->ipif_v6lcl_addr;
9531 	ipif->ipif_v6lcl_addr = v6addr;
9532 	sctp_update_ipif_addr(ipif, ov6addr);
9533 	ipif->ipif_addr_ready = 0;
9534 
9535 	ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9536 
9537 	/*
9538 	 * If the interface was previously marked as a duplicate, then since
9539 	 * we've now got a "new" address, it should no longer be considered a
9540 	 * duplicate -- even if the "new" address is the same as the old one.
9541 	 * Note that if all ipifs are down, we may have a pending ARP down
9542 	 * event to handle.  This is because we want to recover from duplicates
9543 	 * and thus delay tearing down ARP until the duplicates have been
9544 	 * removed or disabled.
9545 	 */
9546 	need_dl_down = need_arp_down = B_FALSE;
9547 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9548 		need_arp_down = !need_up;
9549 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9550 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9551 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9552 			need_dl_down = B_TRUE;
9553 		}
9554 	}
9555 
9556 	ipif_set_default(ipif);
9557 
9558 	/*
9559 	 * If we've just manually set the IPv6 link-local address (0th ipif),
9560 	 * tag the ill so that future updates to the interface ID don't result
9561 	 * in this address getting automatically reconfigured from under the
9562 	 * administrator.
9563 	 */
9564 	if (ipif->ipif_isv6 && ipif->ipif_id == 0)
9565 		ill->ill_manual_linklocal = 1;
9566 
9567 	/*
9568 	 * When publishing an interface address change event, we only notify
9569 	 * the event listeners of the new address.  It is assumed that if they
9570 	 * actively care about the addresses assigned that they will have
9571 	 * already discovered the previous address assigned (if there was one.)
9572 	 *
9573 	 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9574 	 */
9575 	if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9576 		ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9577 		    NE_ADDRESS_CHANGE, sin, sinlen);
9578 	}
9579 
9580 	mutex_exit(&ill->ill_lock);
9581 
9582 	if (need_up) {
9583 		/*
9584 		 * Now bring the interface back up.  If this
9585 		 * is the only IPIF for the ILL, ipif_up
9586 		 * will have to re-bind to the device, so
9587 		 * we may get back EINPROGRESS, in which
9588 		 * case, this IOCTL will get completed in
9589 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9590 		 */
9591 		err = ipif_up(ipif, q, mp);
9592 	} else {
9593 		/* Perhaps ilgs should use this ill */
9594 		update_conn_ill(NULL, ill->ill_ipst);
9595 	}
9596 
9597 	if (need_dl_down)
9598 		ill_dl_down(ill);
9599 
9600 	if (need_arp_down && !ill->ill_isv6)
9601 		(void) ipif_arp_down(ipif);
9602 
9603 	/*
9604 	 * The default multicast interface might have changed (for
9605 	 * instance if the IPv6 scope of the address changed)
9606 	 */
9607 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9608 
9609 	return (err);
9610 }
9611 
9612 /*
9613  * Restart entry point to restart the address set operation after the
9614  * refcounts have dropped to zero.
9615  */
9616 /* ARGSUSED */
9617 int
9618 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9619     ip_ioctl_cmd_t *ipip, void *ifreq)
9620 {
9621 	ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9622 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9623 	ASSERT(IAM_WRITER_IPIF(ipif));
9624 	(void) ipif_down_tail(ipif);
9625 	return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9626 }
9627 
9628 /* ARGSUSED */
9629 int
9630 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9631     ip_ioctl_cmd_t *ipip, void *if_req)
9632 {
9633 	sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9634 	struct lifreq *lifr = (struct lifreq *)if_req;
9635 
9636 	ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9637 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9638 	/*
9639 	 * The net mask and address can't change since we have a
9640 	 * reference to the ipif. So no lock is necessary.
9641 	 */
9642 	if (ipif->ipif_isv6) {
9643 		*sin6 = sin6_null;
9644 		sin6->sin6_family = AF_INET6;
9645 		sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9646 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9647 		lifr->lifr_addrlen =
9648 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9649 	} else {
9650 		*sin = sin_null;
9651 		sin->sin_family = AF_INET;
9652 		sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9653 		if (ipip->ipi_cmd_type == LIF_CMD) {
9654 			lifr->lifr_addrlen =
9655 			    ip_mask_to_plen(ipif->ipif_net_mask);
9656 		}
9657 	}
9658 	return (0);
9659 }
9660 
9661 /*
9662  * Set the destination address for a pt-pt interface.
9663  */
9664 /* ARGSUSED */
9665 int
9666 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9667     ip_ioctl_cmd_t *ipip, void *if_req)
9668 {
9669 	int err = 0;
9670 	in6_addr_t v6addr;
9671 	boolean_t need_up = B_FALSE;
9672 
9673 	ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9674 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9675 	ASSERT(IAM_WRITER_IPIF(ipif));
9676 
9677 	if (ipif->ipif_isv6) {
9678 		sin6_t *sin6;
9679 
9680 		if (sin->sin_family != AF_INET6)
9681 			return (EAFNOSUPPORT);
9682 
9683 		sin6 = (sin6_t *)sin;
9684 		v6addr = sin6->sin6_addr;
9685 
9686 		if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9687 			return (EADDRNOTAVAIL);
9688 	} else {
9689 		ipaddr_t addr;
9690 
9691 		if (sin->sin_family != AF_INET)
9692 			return (EAFNOSUPPORT);
9693 
9694 		addr = sin->sin_addr.s_addr;
9695 		if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9696 			return (EADDRNOTAVAIL);
9697 
9698 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9699 	}
9700 
9701 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
9702 		return (0);	/* No change */
9703 
9704 	if (ipif->ipif_flags & IPIF_UP) {
9705 		/*
9706 		 * If the interface is already marked up,
9707 		 * we call ipif_down which will take care
9708 		 * of ditching any IREs that have been set
9709 		 * up based on the old pp dst address.
9710 		 */
9711 		err = ipif_logical_down(ipif, q, mp);
9712 		if (err == EINPROGRESS)
9713 			return (err);
9714 		(void) ipif_down_tail(ipif);
9715 		need_up = B_TRUE;
9716 	}
9717 	/*
9718 	 * could return EINPROGRESS. If so ioctl will complete in
9719 	 * ip_rput_dlpi_writer
9720 	 */
9721 	err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
9722 	return (err);
9723 }
9724 
9725 static int
9726 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9727     boolean_t need_up)
9728 {
9729 	in6_addr_t v6addr;
9730 	ill_t	*ill = ipif->ipif_ill;
9731 	int	err = 0;
9732 	boolean_t need_dl_down;
9733 	boolean_t need_arp_down;
9734 
9735 	ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
9736 	    ipif->ipif_id, (void *)ipif));
9737 
9738 	/* Must cancel any pending timer before taking the ill_lock */
9739 	if (ipif->ipif_recovery_id != 0)
9740 		(void) untimeout(ipif->ipif_recovery_id);
9741 	ipif->ipif_recovery_id = 0;
9742 
9743 	if (ipif->ipif_isv6) {
9744 		sin6_t *sin6;
9745 
9746 		sin6 = (sin6_t *)sin;
9747 		v6addr = sin6->sin6_addr;
9748 	} else {
9749 		ipaddr_t addr;
9750 
9751 		addr = sin->sin_addr.s_addr;
9752 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9753 	}
9754 	mutex_enter(&ill->ill_lock);
9755 	/* Set point to point destination address. */
9756 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
9757 		/*
9758 		 * Allow this as a means of creating logical
9759 		 * pt-pt interfaces on top of e.g. an Ethernet.
9760 		 * XXX Undocumented HACK for testing.
9761 		 * pt-pt interfaces are created with NUD disabled.
9762 		 */
9763 		ipif->ipif_flags |= IPIF_POINTOPOINT;
9764 		ipif->ipif_flags &= ~IPIF_BROADCAST;
9765 		if (ipif->ipif_isv6)
9766 			ill->ill_flags |= ILLF_NONUD;
9767 	}
9768 
9769 	/*
9770 	 * If the interface was previously marked as a duplicate, then since
9771 	 * we've now got a "new" address, it should no longer be considered a
9772 	 * duplicate -- even if the "new" address is the same as the old one.
9773 	 * Note that if all ipifs are down, we may have a pending ARP down
9774 	 * event to handle.
9775 	 */
9776 	need_dl_down = need_arp_down = B_FALSE;
9777 	if (ipif->ipif_flags & IPIF_DUPLICATE) {
9778 		need_arp_down = !need_up;
9779 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
9780 		if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9781 		    ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9782 			need_dl_down = B_TRUE;
9783 		}
9784 	}
9785 
9786 	/* Set the new address. */
9787 	ipif->ipif_v6pp_dst_addr = v6addr;
9788 	/* Make sure subnet tracks pp_dst */
9789 	ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
9790 	mutex_exit(&ill->ill_lock);
9791 
9792 	if (need_up) {
9793 		/*
9794 		 * Now bring the interface back up.  If this
9795 		 * is the only IPIF for the ILL, ipif_up
9796 		 * will have to re-bind to the device, so
9797 		 * we may get back EINPROGRESS, in which
9798 		 * case, this IOCTL will get completed in
9799 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
9800 		 */
9801 		err = ipif_up(ipif, q, mp);
9802 	}
9803 
9804 	if (need_dl_down)
9805 		ill_dl_down(ill);
9806 	if (need_arp_down && !ipif->ipif_isv6)
9807 		(void) ipif_arp_down(ipif);
9808 
9809 	return (err);
9810 }
9811 
9812 /*
9813  * Restart entry point to restart the dstaddress set operation after the
9814  * refcounts have dropped to zero.
9815  */
9816 /* ARGSUSED */
9817 int
9818 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9819     ip_ioctl_cmd_t *ipip, void *ifreq)
9820 {
9821 	ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
9822 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9823 	(void) ipif_down_tail(ipif);
9824 	return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
9825 }
9826 
9827 /* ARGSUSED */
9828 int
9829 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9830     ip_ioctl_cmd_t *ipip, void *if_req)
9831 {
9832 	sin6_t	*sin6 = (struct sockaddr_in6 *)sin;
9833 
9834 	ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
9835 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9836 	/*
9837 	 * Get point to point destination address. The addresses can't
9838 	 * change since we hold a reference to the ipif.
9839 	 */
9840 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
9841 		return (EADDRNOTAVAIL);
9842 
9843 	if (ipif->ipif_isv6) {
9844 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9845 		*sin6 = sin6_null;
9846 		sin6->sin6_family = AF_INET6;
9847 		sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
9848 	} else {
9849 		*sin = sin_null;
9850 		sin->sin_family = AF_INET;
9851 		sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
9852 	}
9853 	return (0);
9854 }
9855 
9856 /*
9857  * Check which flags will change by the given flags being set
9858  * silently ignore flags which userland is not allowed to control.
9859  * (Because these flags may change between SIOCGLIFFLAGS and
9860  * SIOCSLIFFLAGS, and that's outside of userland's control,
9861  * we need to silently ignore them rather than fail.)
9862  */
9863 static void
9864 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
9865     uint64_t *offp)
9866 {
9867 	ill_t		*ill = ipif->ipif_ill;
9868 	phyint_t 	*phyi = ill->ill_phyint;
9869 	uint64_t	cantchange_flags, intf_flags;
9870 	uint64_t	turn_on, turn_off;
9871 
9872 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9873 	cantchange_flags = IFF_CANTCHANGE;
9874 	if (IS_IPMP(ill))
9875 		cantchange_flags |= IFF_IPMP_CANTCHANGE;
9876 	turn_on = (flags ^ intf_flags) & ~cantchange_flags;
9877 	turn_off = intf_flags & turn_on;
9878 	turn_on ^= turn_off;
9879 	*onp = turn_on;
9880 	*offp = turn_off;
9881 }
9882 
9883 /*
9884  * Set interface flags.  Many flags require special handling (e.g.,
9885  * bringing the interface down); see below for details.
9886  *
9887  * NOTE : We really don't enforce that ipif_id zero should be used
9888  *	  for setting any flags other than IFF_LOGINT_FLAGS. This
9889  *	  is because applications generally does SICGLIFFLAGS and
9890  *	  ORs in the new flags (that affects the logical) and does a
9891  *	  SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
9892  *	  than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
9893  *	  flags that will be turned on is correct with respect to
9894  *	  ipif_id 0. For backward compatibility reasons, it is not done.
9895  */
9896 /* ARGSUSED */
9897 int
9898 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9899     ip_ioctl_cmd_t *ipip, void *if_req)
9900 {
9901 	uint64_t turn_on;
9902 	uint64_t turn_off;
9903 	int	err = 0;
9904 	phyint_t *phyi;
9905 	ill_t *ill;
9906 	conn_t *connp;
9907 	uint64_t intf_flags;
9908 	boolean_t phyint_flags_modified = B_FALSE;
9909 	uint64_t flags;
9910 	struct ifreq *ifr;
9911 	struct lifreq *lifr;
9912 	boolean_t set_linklocal = B_FALSE;
9913 
9914 	ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
9915 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9916 
9917 	ASSERT(IAM_WRITER_IPIF(ipif));
9918 
9919 	ill = ipif->ipif_ill;
9920 	phyi = ill->ill_phyint;
9921 
9922 	if (ipip->ipi_cmd_type == IF_CMD) {
9923 		ifr = (struct ifreq *)if_req;
9924 		flags =  (uint64_t)(ifr->ifr_flags & 0x0000ffff);
9925 	} else {
9926 		lifr = (struct lifreq *)if_req;
9927 		flags = lifr->lifr_flags;
9928 	}
9929 
9930 	intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
9931 
9932 	/*
9933 	 * Have the flags been set correctly until now?
9934 	 */
9935 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
9936 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
9937 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
9938 	/*
9939 	 * Compare the new flags to the old, and partition
9940 	 * into those coming on and those going off.
9941 	 * For the 16 bit command keep the bits above bit 16 unchanged.
9942 	 */
9943 	if (ipip->ipi_cmd == SIOCSIFFLAGS)
9944 		flags |= intf_flags & ~0xFFFF;
9945 
9946 	/*
9947 	 * Explicitly fail attempts to change flags that are always invalid on
9948 	 * an IPMP meta-interface.
9949 	 */
9950 	if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
9951 		return (EINVAL);
9952 
9953 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
9954 	if ((turn_on|turn_off) == 0)
9955 		return (0);	/* No change */
9956 
9957 	/*
9958 	 * All test addresses must be IFF_DEPRECATED (to ensure source address
9959 	 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
9960 	 * allow it to be turned off.
9961 	 */
9962 	if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
9963 	    (turn_on|intf_flags) & IFF_NOFAILOVER)
9964 		return (EINVAL);
9965 
9966 	if ((connp = Q_TO_CONN(q)) == NULL)
9967 		return (EINVAL);
9968 
9969 	/*
9970 	 * Only vrrp control socket is allowed to change IFF_UP and
9971 	 * IFF_NOACCEPT flags when IFF_VRRP is set.
9972 	 */
9973 	if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
9974 		if (!connp->conn_isvrrp)
9975 			return (EINVAL);
9976 	}
9977 
9978 	/*
9979 	 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
9980 	 * VRRP control socket.
9981 	 */
9982 	if ((turn_off | turn_on) & IFF_NOACCEPT) {
9983 		if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
9984 			return (EINVAL);
9985 	}
9986 
9987 	if (turn_on & IFF_NOFAILOVER) {
9988 		turn_on |= IFF_DEPRECATED;
9989 		flags |= IFF_DEPRECATED;
9990 	}
9991 
9992 	/*
9993 	 * On underlying interfaces, only allow applications to manage test
9994 	 * addresses -- otherwise, they may get confused when the address
9995 	 * moves as part of being brought up.  Likewise, prevent an
9996 	 * application-managed test address from being converted to a data
9997 	 * address.  To prevent migration of administratively up addresses in
9998 	 * the kernel, we don't allow them to be converted either.
9999 	 */
10000 	if (IS_UNDER_IPMP(ill)) {
10001 		const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10002 
10003 		if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10004 			return (EINVAL);
10005 
10006 		if ((turn_off & IFF_NOFAILOVER) &&
10007 		    (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10008 			return (EINVAL);
10009 	}
10010 
10011 	/*
10012 	 * Only allow IFF_TEMPORARY flag to be set on
10013 	 * IPv6 interfaces.
10014 	 */
10015 	if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10016 		return (EINVAL);
10017 
10018 	/*
10019 	 * cannot turn off IFF_NOXMIT on  VNI interfaces.
10020 	 */
10021 	if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10022 		return (EINVAL);
10023 
10024 	/*
10025 	 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10026 	 * interfaces.  It makes no sense in that context.
10027 	 */
10028 	if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10029 		return (EINVAL);
10030 
10031 	/*
10032 	 * For IPv6 ipif_id 0, don't allow the interface to be up without
10033 	 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10034 	 * If the link local address isn't set, and can be set, it will get
10035 	 * set later on in this function.
10036 	 */
10037 	if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10038 	    (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10039 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10040 		if (ipif_cant_setlinklocal(ipif))
10041 			return (EINVAL);
10042 		set_linklocal = B_TRUE;
10043 	}
10044 
10045 	/*
10046 	 * If we modify physical interface flags, we'll potentially need to
10047 	 * send up two routing socket messages for the changes (one for the
10048 	 * IPv4 ill, and another for the IPv6 ill).  Note that here.
10049 	 */
10050 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10051 		phyint_flags_modified = B_TRUE;
10052 
10053 	/*
10054 	 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10055 	 * (otherwise, we'd immediately use them, defeating standby).  Also,
10056 	 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10057 	 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10058 	 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared.  We
10059 	 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10060 	 * will not be honored.
10061 	 */
10062 	if (turn_on & PHYI_STANDBY) {
10063 		/*
10064 		 * No need to grab ill_g_usesrc_lock here; see the
10065 		 * synchronization notes in ip.c.
10066 		 */
10067 		if (ill->ill_usesrc_grp_next != NULL ||
10068 		    intf_flags & PHYI_INACTIVE)
10069 			return (EINVAL);
10070 		if (!(flags & PHYI_FAILED)) {
10071 			flags |= PHYI_INACTIVE;
10072 			turn_on |= PHYI_INACTIVE;
10073 		}
10074 	}
10075 
10076 	if (turn_off & PHYI_STANDBY) {
10077 		flags &= ~PHYI_INACTIVE;
10078 		turn_off |= PHYI_INACTIVE;
10079 	}
10080 
10081 	/*
10082 	 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10083 	 * would end up on.
10084 	 */
10085 	if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10086 	    (PHYI_FAILED | PHYI_INACTIVE))
10087 		return (EINVAL);
10088 
10089 	/*
10090 	 * If ILLF_ROUTER changes, we need to change the ip forwarding
10091 	 * status of the interface.
10092 	 */
10093 	if ((turn_on | turn_off) & ILLF_ROUTER)
10094 		(void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10095 
10096 	/*
10097 	 * If the interface is not UP and we are not going to
10098 	 * bring it UP, record the flags and return. When the
10099 	 * interface comes UP later, the right actions will be
10100 	 * taken.
10101 	 */
10102 	if (!(ipif->ipif_flags & IPIF_UP) &&
10103 	    !(turn_on & IPIF_UP)) {
10104 		/* Record new flags in their respective places. */
10105 		mutex_enter(&ill->ill_lock);
10106 		mutex_enter(&ill->ill_phyint->phyint_lock);
10107 		ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10108 		ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10109 		ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10110 		ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10111 		phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10112 		phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10113 		mutex_exit(&ill->ill_lock);
10114 		mutex_exit(&ill->ill_phyint->phyint_lock);
10115 
10116 		/*
10117 		 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10118 		 * same to the kernel: if any of them has been set by
10119 		 * userland, the interface cannot be used for data traffic.
10120 		 */
10121 		if ((turn_on|turn_off) &
10122 		    (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10123 			ASSERT(!IS_IPMP(ill));
10124 			/*
10125 			 * It's possible the ill is part of an "anonymous"
10126 			 * IPMP group rather than a real group.  In that case,
10127 			 * there are no other interfaces in the group and thus
10128 			 * no need to call ipmp_phyint_refresh_active().
10129 			 */
10130 			if (IS_UNDER_IPMP(ill))
10131 				ipmp_phyint_refresh_active(phyi);
10132 		}
10133 
10134 		if (phyint_flags_modified) {
10135 			if (phyi->phyint_illv4 != NULL) {
10136 				ip_rts_ifmsg(phyi->phyint_illv4->
10137 				    ill_ipif, RTSQ_DEFAULT);
10138 			}
10139 			if (phyi->phyint_illv6 != NULL) {
10140 				ip_rts_ifmsg(phyi->phyint_illv6->
10141 				    ill_ipif, RTSQ_DEFAULT);
10142 			}
10143 		}
10144 		/* The default multicast interface might have changed */
10145 		ire_increment_multicast_generation(ill->ill_ipst,
10146 		    ill->ill_isv6);
10147 
10148 		return (0);
10149 	} else if (set_linklocal) {
10150 		mutex_enter(&ill->ill_lock);
10151 		if (set_linklocal)
10152 			ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10153 		mutex_exit(&ill->ill_lock);
10154 	}
10155 
10156 	/*
10157 	 * Disallow IPv6 interfaces coming up that have the unspecified address,
10158 	 * or point-to-point interfaces with an unspecified destination. We do
10159 	 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10160 	 * have a subnet assigned, which is how in.ndpd currently manages its
10161 	 * onlink prefix list when no addresses are configured with those
10162 	 * prefixes.
10163 	 */
10164 	if (ipif->ipif_isv6 &&
10165 	    ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10166 	    (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10167 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10168 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10169 	    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10170 		return (EINVAL);
10171 	}
10172 
10173 	/*
10174 	 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10175 	 * from being brought up.
10176 	 */
10177 	if (!ipif->ipif_isv6 &&
10178 	    ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10179 	    ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10180 		return (EINVAL);
10181 	}
10182 
10183 	/*
10184 	 * If we are going to change one or more of the flags that are
10185 	 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10186 	 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10187 	 * IPIF_NOFAILOVER, we will take special action.  This is
10188 	 * done by bring the ipif down, changing the flags and bringing
10189 	 * it back up again.  For IPIF_NOFAILOVER, the act of bringing it
10190 	 * back up will trigger the address to be moved.
10191 	 *
10192 	 * If we are going to change IFF_NOACCEPT, we need to bring
10193 	 * all the ipifs down then bring them up again.	 The act of
10194 	 * bringing all the ipifs back up will trigger the local
10195 	 * ires being recreated with "no_accept" set/cleared.
10196 	 *
10197 	 * Note that ILLF_NOACCEPT is always set separately from the
10198 	 * other flags.
10199 	 */
10200 	if ((turn_on|turn_off) &
10201 	    (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10202 	    ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10203 	    IPIF_NOFAILOVER)) {
10204 		/*
10205 		 * ipif_down() will ire_delete bcast ire's for the subnet,
10206 		 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10207 		 * entries shared between multiple ipifs on the same subnet.
10208 		 */
10209 		if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10210 		    !(turn_off & IPIF_UP)) {
10211 			if (ipif->ipif_flags & IPIF_UP)
10212 				ill->ill_logical_down = 1;
10213 			turn_on &= ~IPIF_UP;
10214 		}
10215 		err = ipif_down(ipif, q, mp);
10216 		ip1dbg(("ipif_down returns %d err ", err));
10217 		if (err == EINPROGRESS)
10218 			return (err);
10219 		(void) ipif_down_tail(ipif);
10220 	} else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10221 		/*
10222 		 * If we can quiesce the ill, then continue.  If not, then
10223 		 * ip_sioctl_flags_tail() will be called from
10224 		 * ipif_ill_refrele_tail().
10225 		 */
10226 		ill_down_ipifs(ill, B_TRUE);
10227 
10228 		mutex_enter(&connp->conn_lock);
10229 		mutex_enter(&ill->ill_lock);
10230 		if (!ill_is_quiescent(ill)) {
10231 			boolean_t success;
10232 
10233 			success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10234 			    q, mp, ILL_DOWN);
10235 			mutex_exit(&ill->ill_lock);
10236 			mutex_exit(&connp->conn_lock);
10237 			return (success ? EINPROGRESS : EINTR);
10238 		}
10239 		mutex_exit(&ill->ill_lock);
10240 		mutex_exit(&connp->conn_lock);
10241 	}
10242 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10243 }
10244 
10245 static int
10246 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10247 {
10248 	ill_t	*ill;
10249 	phyint_t *phyi;
10250 	uint64_t turn_on, turn_off;
10251 	boolean_t phyint_flags_modified = B_FALSE;
10252 	int	err = 0;
10253 	boolean_t set_linklocal = B_FALSE;
10254 
10255 	ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10256 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
10257 
10258 	ASSERT(IAM_WRITER_IPIF(ipif));
10259 
10260 	ill = ipif->ipif_ill;
10261 	phyi = ill->ill_phyint;
10262 
10263 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10264 
10265 	/*
10266 	 * IFF_UP is handled separately.
10267 	 */
10268 	turn_on &= ~IFF_UP;
10269 	turn_off &= ~IFF_UP;
10270 
10271 	if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10272 		phyint_flags_modified = B_TRUE;
10273 
10274 	/*
10275 	 * Now we change the flags. Track current value of
10276 	 * other flags in their respective places.
10277 	 */
10278 	mutex_enter(&ill->ill_lock);
10279 	mutex_enter(&phyi->phyint_lock);
10280 	ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10281 	ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10282 	ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10283 	ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10284 	phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10285 	phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10286 	if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10287 		set_linklocal = B_TRUE;
10288 		ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10289 	}
10290 
10291 	mutex_exit(&ill->ill_lock);
10292 	mutex_exit(&phyi->phyint_lock);
10293 
10294 	if (set_linklocal)
10295 		(void) ipif_setlinklocal(ipif);
10296 
10297 	/*
10298 	 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10299 	 * the kernel: if any of them has been set by userland, the interface
10300 	 * cannot be used for data traffic.
10301 	 */
10302 	if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10303 		ASSERT(!IS_IPMP(ill));
10304 		/*
10305 		 * It's possible the ill is part of an "anonymous" IPMP group
10306 		 * rather than a real group.  In that case, there are no other
10307 		 * interfaces in the group and thus no need for us to call
10308 		 * ipmp_phyint_refresh_active().
10309 		 */
10310 		if (IS_UNDER_IPMP(ill))
10311 			ipmp_phyint_refresh_active(phyi);
10312 	}
10313 
10314 	if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10315 		/*
10316 		 * If the ILLF_NOACCEPT flag is changed, bring up all the
10317 		 * ipifs that were brought down.
10318 		 *
10319 		 * The routing sockets messages are sent as the result
10320 		 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10321 		 * as well.
10322 		 */
10323 		err = ill_up_ipifs(ill, q, mp);
10324 	} else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10325 		/*
10326 		 * XXX ipif_up really does not know whether a phyint flags
10327 		 * was modified or not. So, it sends up information on
10328 		 * only one routing sockets message. As we don't bring up
10329 		 * the interface and also set PHYI_ flags simultaneously
10330 		 * it should be okay.
10331 		 */
10332 		err = ipif_up(ipif, q, mp);
10333 	} else {
10334 		/*
10335 		 * Make sure routing socket sees all changes to the flags.
10336 		 * ipif_up_done* handles this when we use ipif_up.
10337 		 */
10338 		if (phyint_flags_modified) {
10339 			if (phyi->phyint_illv4 != NULL) {
10340 				ip_rts_ifmsg(phyi->phyint_illv4->
10341 				    ill_ipif, RTSQ_DEFAULT);
10342 			}
10343 			if (phyi->phyint_illv6 != NULL) {
10344 				ip_rts_ifmsg(phyi->phyint_illv6->
10345 				    ill_ipif, RTSQ_DEFAULT);
10346 			}
10347 		} else {
10348 			ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10349 		}
10350 		/*
10351 		 * Update the flags in SCTP's IPIF list, ipif_up() will do
10352 		 * this in need_up case.
10353 		 */
10354 		sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10355 	}
10356 
10357 	/* The default multicast interface might have changed */
10358 	ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10359 	return (err);
10360 }
10361 
10362 /*
10363  * Restart the flags operation now that the refcounts have dropped to zero.
10364  */
10365 /* ARGSUSED */
10366 int
10367 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10368     ip_ioctl_cmd_t *ipip, void *if_req)
10369 {
10370 	uint64_t flags;
10371 	struct ifreq *ifr = if_req;
10372 	struct lifreq *lifr = if_req;
10373 	uint64_t turn_on, turn_off;
10374 
10375 	ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10376 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10377 
10378 	if (ipip->ipi_cmd_type == IF_CMD) {
10379 		/* cast to uint16_t prevents unwanted sign extension */
10380 		flags = (uint16_t)ifr->ifr_flags;
10381 	} else {
10382 		flags = lifr->lifr_flags;
10383 	}
10384 
10385 	/*
10386 	 * If this function call is a result of the ILLF_NOACCEPT flag
10387 	 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10388 	 */
10389 	ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10390 	if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10391 		(void) ipif_down_tail(ipif);
10392 
10393 	return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10394 }
10395 
10396 /*
10397  * Can operate on either a module or a driver queue.
10398  */
10399 /* ARGSUSED */
10400 int
10401 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10402     ip_ioctl_cmd_t *ipip, void *if_req)
10403 {
10404 	/*
10405 	 * Has the flags been set correctly till now ?
10406 	 */
10407 	ill_t *ill = ipif->ipif_ill;
10408 	phyint_t *phyi = ill->ill_phyint;
10409 
10410 	ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10411 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10412 	ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10413 	ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10414 	ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10415 
10416 	/*
10417 	 * Need a lock since some flags can be set even when there are
10418 	 * references to the ipif.
10419 	 */
10420 	mutex_enter(&ill->ill_lock);
10421 	if (ipip->ipi_cmd_type == IF_CMD) {
10422 		struct ifreq *ifr = (struct ifreq *)if_req;
10423 
10424 		/* Get interface flags (low 16 only). */
10425 		ifr->ifr_flags = ((ipif->ipif_flags |
10426 		    ill->ill_flags | phyi->phyint_flags) & 0xffff);
10427 	} else {
10428 		struct lifreq *lifr = (struct lifreq *)if_req;
10429 
10430 		/* Get interface flags. */
10431 		lifr->lifr_flags = ipif->ipif_flags |
10432 		    ill->ill_flags | phyi->phyint_flags;
10433 	}
10434 	mutex_exit(&ill->ill_lock);
10435 	return (0);
10436 }
10437 
10438 /*
10439  * We allow the MTU to be set on an ILL, but not have it be different
10440  * for different IPIFs since we don't actually send packets on IPIFs.
10441  */
10442 /* ARGSUSED */
10443 int
10444 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10445     ip_ioctl_cmd_t *ipip, void *if_req)
10446 {
10447 	int mtu;
10448 	int ip_min_mtu;
10449 	struct ifreq	*ifr;
10450 	struct lifreq *lifr;
10451 	ill_t	*ill;
10452 
10453 	ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10454 	    ipif->ipif_id, (void *)ipif));
10455 	if (ipip->ipi_cmd_type == IF_CMD) {
10456 		ifr = (struct ifreq *)if_req;
10457 		mtu = ifr->ifr_metric;
10458 	} else {
10459 		lifr = (struct lifreq *)if_req;
10460 		mtu = lifr->lifr_mtu;
10461 	}
10462 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
10463 	if (ipif->ipif_id != 0)
10464 		return (EINVAL);
10465 
10466 	ill = ipif->ipif_ill;
10467 	if (ipif->ipif_isv6)
10468 		ip_min_mtu = IPV6_MIN_MTU;
10469 	else
10470 		ip_min_mtu = IP_MIN_MTU;
10471 
10472 	mutex_enter(&ill->ill_lock);
10473 	if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10474 		mutex_exit(&ill->ill_lock);
10475 		return (EINVAL);
10476 	}
10477 	/*
10478 	 * The dce and fragmentation code can handle changes to ill_mtu
10479 	 * concurrent with sending/fragmenting packets.
10480 	 */
10481 	ill->ill_mtu = mtu;
10482 	ill->ill_flags |= ILLF_FIXEDMTU;
10483 	mutex_exit(&ill->ill_lock);
10484 
10485 	/*
10486 	 * Make sure all dce_generation checks find out
10487 	 * that ill_mtu has changed.
10488 	 */
10489 	dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10490 
10491 	/* Update the MTU in SCTP's list */
10492 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10493 	return (0);
10494 }
10495 
10496 /* Get interface MTU. */
10497 /* ARGSUSED */
10498 int
10499 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10500 	ip_ioctl_cmd_t *ipip, void *if_req)
10501 {
10502 	struct ifreq	*ifr;
10503 	struct lifreq	*lifr;
10504 
10505 	ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10506 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10507 
10508 	/*
10509 	 * We allow a get on any logical interface even though the set
10510 	 * can only be done on logical unit 0.
10511 	 */
10512 	if (ipip->ipi_cmd_type == IF_CMD) {
10513 		ifr = (struct ifreq *)if_req;
10514 		ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10515 	} else {
10516 		lifr = (struct lifreq *)if_req;
10517 		lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10518 	}
10519 	return (0);
10520 }
10521 
10522 /* Set interface broadcast address. */
10523 /* ARGSUSED2 */
10524 int
10525 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10526 	ip_ioctl_cmd_t *ipip, void *if_req)
10527 {
10528 	ipaddr_t addr;
10529 	ire_t	*ire;
10530 	ill_t		*ill = ipif->ipif_ill;
10531 	ip_stack_t	*ipst = ill->ill_ipst;
10532 
10533 	ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10534 	    ipif->ipif_id));
10535 
10536 	ASSERT(IAM_WRITER_IPIF(ipif));
10537 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10538 		return (EADDRNOTAVAIL);
10539 
10540 	ASSERT(!(ipif->ipif_isv6));	/* No IPv6 broadcast */
10541 
10542 	if (sin->sin_family != AF_INET)
10543 		return (EAFNOSUPPORT);
10544 
10545 	addr = sin->sin_addr.s_addr;
10546 	if (ipif->ipif_flags & IPIF_UP) {
10547 		/*
10548 		 * If we are already up, make sure the new
10549 		 * broadcast address makes sense.  If it does,
10550 		 * there should be an IRE for it already.
10551 		 */
10552 		ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10553 		    ill, ipif->ipif_zoneid, NULL,
10554 		    (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10555 		if (ire == NULL) {
10556 			return (EINVAL);
10557 		} else {
10558 			ire_refrele(ire);
10559 		}
10560 	}
10561 	/*
10562 	 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10563 	 * needs to already exist we never need to change the set of
10564 	 * IRE_BROADCASTs when we are UP.
10565 	 */
10566 	if (addr != ipif->ipif_brd_addr)
10567 		IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10568 
10569 	return (0);
10570 }
10571 
10572 /* Get interface broadcast address. */
10573 /* ARGSUSED */
10574 int
10575 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10576     ip_ioctl_cmd_t *ipip, void *if_req)
10577 {
10578 	ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10579 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10580 	if (!(ipif->ipif_flags & IPIF_BROADCAST))
10581 		return (EADDRNOTAVAIL);
10582 
10583 	/* IPIF_BROADCAST not possible with IPv6 */
10584 	ASSERT(!ipif->ipif_isv6);
10585 	*sin = sin_null;
10586 	sin->sin_family = AF_INET;
10587 	sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10588 	return (0);
10589 }
10590 
10591 /*
10592  * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10593  */
10594 /* ARGSUSED */
10595 int
10596 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10597     ip_ioctl_cmd_t *ipip, void *if_req)
10598 {
10599 	int err = 0;
10600 	in6_addr_t v6mask;
10601 
10602 	ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10603 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10604 
10605 	ASSERT(IAM_WRITER_IPIF(ipif));
10606 
10607 	if (ipif->ipif_isv6) {
10608 		sin6_t *sin6;
10609 
10610 		if (sin->sin_family != AF_INET6)
10611 			return (EAFNOSUPPORT);
10612 
10613 		sin6 = (sin6_t *)sin;
10614 		v6mask = sin6->sin6_addr;
10615 	} else {
10616 		ipaddr_t mask;
10617 
10618 		if (sin->sin_family != AF_INET)
10619 			return (EAFNOSUPPORT);
10620 
10621 		mask = sin->sin_addr.s_addr;
10622 		V4MASK_TO_V6(mask, v6mask);
10623 	}
10624 
10625 	/*
10626 	 * No big deal if the interface isn't already up, or the mask
10627 	 * isn't really changing, or this is pt-pt.
10628 	 */
10629 	if (!(ipif->ipif_flags & IPIF_UP) ||
10630 	    IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10631 	    (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10632 		ipif->ipif_v6net_mask = v6mask;
10633 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10634 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10635 			    ipif->ipif_v6net_mask,
10636 			    ipif->ipif_v6subnet);
10637 		}
10638 		return (0);
10639 	}
10640 	/*
10641 	 * Make sure we have valid net and subnet broadcast ire's
10642 	 * for the old netmask, if needed by other logical interfaces.
10643 	 */
10644 	err = ipif_logical_down(ipif, q, mp);
10645 	if (err == EINPROGRESS)
10646 		return (err);
10647 	(void) ipif_down_tail(ipif);
10648 	err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10649 	return (err);
10650 }
10651 
10652 static int
10653 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10654 {
10655 	in6_addr_t v6mask;
10656 	int err = 0;
10657 
10658 	ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10659 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10660 
10661 	if (ipif->ipif_isv6) {
10662 		sin6_t *sin6;
10663 
10664 		sin6 = (sin6_t *)sin;
10665 		v6mask = sin6->sin6_addr;
10666 	} else {
10667 		ipaddr_t mask;
10668 
10669 		mask = sin->sin_addr.s_addr;
10670 		V4MASK_TO_V6(mask, v6mask);
10671 	}
10672 
10673 	ipif->ipif_v6net_mask = v6mask;
10674 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10675 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
10676 		    ipif->ipif_v6subnet);
10677 	}
10678 	err = ipif_up(ipif, q, mp);
10679 
10680 	if (err == 0 || err == EINPROGRESS) {
10681 		/*
10682 		 * The interface must be DL_BOUND if this packet has to
10683 		 * go out on the wire. Since we only go through a logical
10684 		 * down and are bound with the driver during an internal
10685 		 * down/up that is satisfied.
10686 		 */
10687 		if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
10688 			/* Potentially broadcast an address mask reply. */
10689 			ipif_mask_reply(ipif);
10690 		}
10691 	}
10692 	return (err);
10693 }
10694 
10695 /* ARGSUSED */
10696 int
10697 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10698     ip_ioctl_cmd_t *ipip, void *if_req)
10699 {
10700 	ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
10701 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10702 	(void) ipif_down_tail(ipif);
10703 	return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
10704 }
10705 
10706 /* Get interface net mask. */
10707 /* ARGSUSED */
10708 int
10709 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10710     ip_ioctl_cmd_t *ipip, void *if_req)
10711 {
10712 	struct lifreq *lifr = (struct lifreq *)if_req;
10713 	struct sockaddr_in6 *sin6 = (sin6_t *)sin;
10714 
10715 	ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
10716 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10717 
10718 	/*
10719 	 * net mask can't change since we have a reference to the ipif.
10720 	 */
10721 	if (ipif->ipif_isv6) {
10722 		ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10723 		*sin6 = sin6_null;
10724 		sin6->sin6_family = AF_INET6;
10725 		sin6->sin6_addr = ipif->ipif_v6net_mask;
10726 		lifr->lifr_addrlen =
10727 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10728 	} else {
10729 		*sin = sin_null;
10730 		sin->sin_family = AF_INET;
10731 		sin->sin_addr.s_addr = ipif->ipif_net_mask;
10732 		if (ipip->ipi_cmd_type == LIF_CMD) {
10733 			lifr->lifr_addrlen =
10734 			    ip_mask_to_plen(ipif->ipif_net_mask);
10735 		}
10736 	}
10737 	return (0);
10738 }
10739 
10740 /* ARGSUSED */
10741 int
10742 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10743     ip_ioctl_cmd_t *ipip, void *if_req)
10744 {
10745 	ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
10746 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10747 
10748 	/*
10749 	 * Since no applications should ever be setting metrics on underlying
10750 	 * interfaces, we explicitly fail to smoke 'em out.
10751 	 */
10752 	if (IS_UNDER_IPMP(ipif->ipif_ill))
10753 		return (EINVAL);
10754 
10755 	/*
10756 	 * Set interface metric.  We don't use this for
10757 	 * anything but we keep track of it in case it is
10758 	 * important to routing applications or such.
10759 	 */
10760 	if (ipip->ipi_cmd_type == IF_CMD) {
10761 		struct ifreq    *ifr;
10762 
10763 		ifr = (struct ifreq *)if_req;
10764 		ipif->ipif_metric = ifr->ifr_metric;
10765 	} else {
10766 		struct lifreq   *lifr;
10767 
10768 		lifr = (struct lifreq *)if_req;
10769 		ipif->ipif_metric = lifr->lifr_metric;
10770 	}
10771 	return (0);
10772 }
10773 
10774 /* ARGSUSED */
10775 int
10776 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10777     ip_ioctl_cmd_t *ipip, void *if_req)
10778 {
10779 	/* Get interface metric. */
10780 	ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
10781 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10782 
10783 	if (ipip->ipi_cmd_type == IF_CMD) {
10784 		struct ifreq    *ifr;
10785 
10786 		ifr = (struct ifreq *)if_req;
10787 		ifr->ifr_metric = ipif->ipif_metric;
10788 	} else {
10789 		struct lifreq   *lifr;
10790 
10791 		lifr = (struct lifreq *)if_req;
10792 		lifr->lifr_metric = ipif->ipif_metric;
10793 	}
10794 
10795 	return (0);
10796 }
10797 
10798 /* ARGSUSED */
10799 int
10800 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10801     ip_ioctl_cmd_t *ipip, void *if_req)
10802 {
10803 	int	arp_muxid;
10804 
10805 	ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
10806 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10807 	/*
10808 	 * Set the muxid returned from I_PLINK.
10809 	 */
10810 	if (ipip->ipi_cmd_type == IF_CMD) {
10811 		struct ifreq *ifr = (struct ifreq *)if_req;
10812 
10813 		ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
10814 		arp_muxid = ifr->ifr_arp_muxid;
10815 	} else {
10816 		struct lifreq *lifr = (struct lifreq *)if_req;
10817 
10818 		ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
10819 		arp_muxid = lifr->lifr_arp_muxid;
10820 	}
10821 	arl_set_muxid(ipif->ipif_ill, arp_muxid);
10822 	return (0);
10823 }
10824 
10825 /* ARGSUSED */
10826 int
10827 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10828     ip_ioctl_cmd_t *ipip, void *if_req)
10829 {
10830 	int	arp_muxid = 0;
10831 
10832 	ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
10833 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10834 	/*
10835 	 * Get the muxid saved in ill for I_PUNLINK.
10836 	 */
10837 	arp_muxid = arl_get_muxid(ipif->ipif_ill);
10838 	if (ipip->ipi_cmd_type == IF_CMD) {
10839 		struct ifreq *ifr = (struct ifreq *)if_req;
10840 
10841 		ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10842 		ifr->ifr_arp_muxid = arp_muxid;
10843 	} else {
10844 		struct lifreq *lifr = (struct lifreq *)if_req;
10845 
10846 		lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
10847 		lifr->lifr_arp_muxid = arp_muxid;
10848 	}
10849 	return (0);
10850 }
10851 
10852 /*
10853  * Set the subnet prefix. Does not modify the broadcast address.
10854  */
10855 /* ARGSUSED */
10856 int
10857 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10858     ip_ioctl_cmd_t *ipip, void *if_req)
10859 {
10860 	int err = 0;
10861 	in6_addr_t v6addr;
10862 	in6_addr_t v6mask;
10863 	boolean_t need_up = B_FALSE;
10864 	int addrlen;
10865 
10866 	ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
10867 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10868 
10869 	ASSERT(IAM_WRITER_IPIF(ipif));
10870 	addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
10871 
10872 	if (ipif->ipif_isv6) {
10873 		sin6_t *sin6;
10874 
10875 		if (sin->sin_family != AF_INET6)
10876 			return (EAFNOSUPPORT);
10877 
10878 		sin6 = (sin6_t *)sin;
10879 		v6addr = sin6->sin6_addr;
10880 		if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
10881 			return (EADDRNOTAVAIL);
10882 	} else {
10883 		ipaddr_t addr;
10884 
10885 		if (sin->sin_family != AF_INET)
10886 			return (EAFNOSUPPORT);
10887 
10888 		addr = sin->sin_addr.s_addr;
10889 		if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
10890 			return (EADDRNOTAVAIL);
10891 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10892 		/* Add 96 bits */
10893 		addrlen += IPV6_ABITS - IP_ABITS;
10894 	}
10895 
10896 	if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
10897 		return (EINVAL);
10898 
10899 	/* Check if bits in the address is set past the mask */
10900 	if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
10901 		return (EINVAL);
10902 
10903 	if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
10904 	    IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
10905 		return (0);	/* No change */
10906 
10907 	if (ipif->ipif_flags & IPIF_UP) {
10908 		/*
10909 		 * If the interface is already marked up,
10910 		 * we call ipif_down which will take care
10911 		 * of ditching any IREs that have been set
10912 		 * up based on the old interface address.
10913 		 */
10914 		err = ipif_logical_down(ipif, q, mp);
10915 		if (err == EINPROGRESS)
10916 			return (err);
10917 		(void) ipif_down_tail(ipif);
10918 		need_up = B_TRUE;
10919 	}
10920 
10921 	err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
10922 	return (err);
10923 }
10924 
10925 static int
10926 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
10927     queue_t *q, mblk_t *mp, boolean_t need_up)
10928 {
10929 	ill_t	*ill = ipif->ipif_ill;
10930 	int	err = 0;
10931 
10932 	ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
10933 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10934 
10935 	/* Set the new address. */
10936 	mutex_enter(&ill->ill_lock);
10937 	ipif->ipif_v6net_mask = v6mask;
10938 	if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10939 		V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
10940 		    ipif->ipif_v6subnet);
10941 	}
10942 	mutex_exit(&ill->ill_lock);
10943 
10944 	if (need_up) {
10945 		/*
10946 		 * Now bring the interface back up.  If this
10947 		 * is the only IPIF for the ILL, ipif_up
10948 		 * will have to re-bind to the device, so
10949 		 * we may get back EINPROGRESS, in which
10950 		 * case, this IOCTL will get completed in
10951 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
10952 		 */
10953 		err = ipif_up(ipif, q, mp);
10954 		if (err == EINPROGRESS)
10955 			return (err);
10956 	}
10957 	return (err);
10958 }
10959 
10960 /* ARGSUSED */
10961 int
10962 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10963     ip_ioctl_cmd_t *ipip, void *if_req)
10964 {
10965 	int	addrlen;
10966 	in6_addr_t v6addr;
10967 	in6_addr_t v6mask;
10968 	struct lifreq *lifr = (struct lifreq *)if_req;
10969 
10970 	ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
10971 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10972 	(void) ipif_down_tail(ipif);
10973 
10974 	addrlen = lifr->lifr_addrlen;
10975 	if (ipif->ipif_isv6) {
10976 		sin6_t *sin6;
10977 
10978 		sin6 = (sin6_t *)sin;
10979 		v6addr = sin6->sin6_addr;
10980 	} else {
10981 		ipaddr_t addr;
10982 
10983 		addr = sin->sin_addr.s_addr;
10984 		IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10985 		addrlen += IPV6_ABITS - IP_ABITS;
10986 	}
10987 	(void) ip_plen_to_mask_v6(addrlen, &v6mask);
10988 
10989 	return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
10990 }
10991 
10992 /* ARGSUSED */
10993 int
10994 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10995     ip_ioctl_cmd_t *ipip, void *if_req)
10996 {
10997 	struct lifreq *lifr = (struct lifreq *)if_req;
10998 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
10999 
11000 	ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11001 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11002 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11003 
11004 	if (ipif->ipif_isv6) {
11005 		*sin6 = sin6_null;
11006 		sin6->sin6_family = AF_INET6;
11007 		sin6->sin6_addr = ipif->ipif_v6subnet;
11008 		lifr->lifr_addrlen =
11009 		    ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11010 	} else {
11011 		*sin = sin_null;
11012 		sin->sin_family = AF_INET;
11013 		sin->sin_addr.s_addr = ipif->ipif_subnet;
11014 		lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11015 	}
11016 	return (0);
11017 }
11018 
11019 /*
11020  * Set the IPv6 address token.
11021  */
11022 /* ARGSUSED */
11023 int
11024 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11025     ip_ioctl_cmd_t *ipi, void *if_req)
11026 {
11027 	ill_t *ill = ipif->ipif_ill;
11028 	int err;
11029 	in6_addr_t v6addr;
11030 	in6_addr_t v6mask;
11031 	boolean_t need_up = B_FALSE;
11032 	int i;
11033 	sin6_t *sin6 = (sin6_t *)sin;
11034 	struct lifreq *lifr = (struct lifreq *)if_req;
11035 	int addrlen;
11036 
11037 	ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11038 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11039 	ASSERT(IAM_WRITER_IPIF(ipif));
11040 
11041 	addrlen = lifr->lifr_addrlen;
11042 	/* Only allow for logical unit zero i.e. not on "le0:17" */
11043 	if (ipif->ipif_id != 0)
11044 		return (EINVAL);
11045 
11046 	if (!ipif->ipif_isv6)
11047 		return (EINVAL);
11048 
11049 	if (addrlen > IPV6_ABITS)
11050 		return (EINVAL);
11051 
11052 	v6addr = sin6->sin6_addr;
11053 
11054 	/*
11055 	 * The length of the token is the length from the end.  To get
11056 	 * the proper mask for this, compute the mask of the bits not
11057 	 * in the token; ie. the prefix, and then xor to get the mask.
11058 	 */
11059 	if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11060 		return (EINVAL);
11061 	for (i = 0; i < 4; i++) {
11062 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11063 	}
11064 
11065 	if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11066 	    ill->ill_token_length == addrlen)
11067 		return (0);	/* No change */
11068 
11069 	if (ipif->ipif_flags & IPIF_UP) {
11070 		err = ipif_logical_down(ipif, q, mp);
11071 		if (err == EINPROGRESS)
11072 			return (err);
11073 		(void) ipif_down_tail(ipif);
11074 		need_up = B_TRUE;
11075 	}
11076 	err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11077 	return (err);
11078 }
11079 
11080 static int
11081 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11082     mblk_t *mp, boolean_t need_up)
11083 {
11084 	in6_addr_t v6addr;
11085 	in6_addr_t v6mask;
11086 	ill_t	*ill = ipif->ipif_ill;
11087 	int	i;
11088 	int	err = 0;
11089 
11090 	ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11091 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11092 	v6addr = sin6->sin6_addr;
11093 	/*
11094 	 * The length of the token is the length from the end.  To get
11095 	 * the proper mask for this, compute the mask of the bits not
11096 	 * in the token; ie. the prefix, and then xor to get the mask.
11097 	 */
11098 	(void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11099 	for (i = 0; i < 4; i++)
11100 		v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11101 
11102 	mutex_enter(&ill->ill_lock);
11103 	V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11104 	ill->ill_token_length = addrlen;
11105 	ill->ill_manual_token = 1;
11106 
11107 	/* Reconfigure the link-local address based on this new token */
11108 	ipif_setlinklocal(ill->ill_ipif);
11109 
11110 	mutex_exit(&ill->ill_lock);
11111 
11112 	if (need_up) {
11113 		/*
11114 		 * Now bring the interface back up.  If this
11115 		 * is the only IPIF for the ILL, ipif_up
11116 		 * will have to re-bind to the device, so
11117 		 * we may get back EINPROGRESS, in which
11118 		 * case, this IOCTL will get completed in
11119 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
11120 		 */
11121 		err = ipif_up(ipif, q, mp);
11122 		if (err == EINPROGRESS)
11123 			return (err);
11124 	}
11125 	return (err);
11126 }
11127 
11128 /* ARGSUSED */
11129 int
11130 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11131     ip_ioctl_cmd_t *ipi, void *if_req)
11132 {
11133 	ill_t *ill;
11134 	sin6_t *sin6 = (sin6_t *)sin;
11135 	struct lifreq *lifr = (struct lifreq *)if_req;
11136 
11137 	ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11138 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11139 	if (ipif->ipif_id != 0)
11140 		return (EINVAL);
11141 
11142 	ill = ipif->ipif_ill;
11143 	if (!ill->ill_isv6)
11144 		return (ENXIO);
11145 
11146 	*sin6 = sin6_null;
11147 	sin6->sin6_family = AF_INET6;
11148 	ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11149 	sin6->sin6_addr = ill->ill_token;
11150 	lifr->lifr_addrlen = ill->ill_token_length;
11151 	return (0);
11152 }
11153 
11154 /*
11155  * Set (hardware) link specific information that might override
11156  * what was acquired through the DL_INFO_ACK.
11157  */
11158 /* ARGSUSED */
11159 int
11160 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11161     ip_ioctl_cmd_t *ipi, void *if_req)
11162 {
11163 	ill_t		*ill = ipif->ipif_ill;
11164 	int		ip_min_mtu;
11165 	struct lifreq	*lifr = (struct lifreq *)if_req;
11166 	lif_ifinfo_req_t *lir;
11167 
11168 	ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11169 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11170 	lir = &lifr->lifr_ifinfo;
11171 	ASSERT(IAM_WRITER_IPIF(ipif));
11172 
11173 	/* Only allow for logical unit zero i.e. not on "bge0:17" */
11174 	if (ipif->ipif_id != 0)
11175 		return (EINVAL);
11176 
11177 	/* Set interface MTU. */
11178 	if (ipif->ipif_isv6)
11179 		ip_min_mtu = IPV6_MIN_MTU;
11180 	else
11181 		ip_min_mtu = IP_MIN_MTU;
11182 
11183 	/*
11184 	 * Verify values before we set anything. Allow zero to
11185 	 * mean unspecified.
11186 	 *
11187 	 * XXX We should be able to set the user-defined lir_mtu to some value
11188 	 * that is greater than ill_current_frag but less than ill_max_frag- the
11189 	 * ill_max_frag value tells us the max MTU that can be handled by the
11190 	 * datalink, whereas the ill_current_frag is dynamically computed for
11191 	 * some link-types like tunnels, based on the tunnel PMTU. However,
11192 	 * since there is currently no way of distinguishing between
11193 	 * administratively fixed link mtu values (e.g., those set via
11194 	 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11195 	 * for tunnels) we conservatively choose the  ill_current_frag as the
11196 	 * upper-bound.
11197 	 */
11198 	if (lir->lir_maxmtu != 0 &&
11199 	    (lir->lir_maxmtu > ill->ill_current_frag ||
11200 	    lir->lir_maxmtu < ip_min_mtu))
11201 		return (EINVAL);
11202 	if (lir->lir_reachtime != 0 &&
11203 	    lir->lir_reachtime > ND_MAX_REACHTIME)
11204 		return (EINVAL);
11205 	if (lir->lir_reachretrans != 0 &&
11206 	    lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11207 		return (EINVAL);
11208 
11209 	mutex_enter(&ill->ill_lock);
11210 	/*
11211 	 * The dce and fragmentation code can handle changes to ill_mtu
11212 	 * concurrent with sending/fragmenting packets.
11213 	 */
11214 	if (lir->lir_maxmtu != 0)
11215 		ill->ill_user_mtu = lir->lir_maxmtu;
11216 
11217 	if (lir->lir_reachtime != 0)
11218 		ill->ill_reachable_time = lir->lir_reachtime;
11219 
11220 	if (lir->lir_reachretrans != 0)
11221 		ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11222 
11223 	ill->ill_max_hops = lir->lir_maxhops;
11224 	ill->ill_max_buf = ND_MAX_Q;
11225 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11226 		/*
11227 		 * ill_mtu is the actual interface MTU, obtained as the min
11228 		 * of user-configured mtu and the value announced by the
11229 		 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11230 		 * we have already made the choice of requiring
11231 		 * ill_user_mtu < ill_current_frag by the time we get here,
11232 		 * the ill_mtu effectively gets assigned to the ill_user_mtu
11233 		 * here.
11234 		 */
11235 		ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11236 	}
11237 	mutex_exit(&ill->ill_lock);
11238 
11239 	/*
11240 	 * Make sure all dce_generation checks find out
11241 	 * that ill_mtu has changed.
11242 	 */
11243 	if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11244 		dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11245 
11246 	/*
11247 	 * Refresh IPMP meta-interface MTU if necessary.
11248 	 */
11249 	if (IS_UNDER_IPMP(ill))
11250 		ipmp_illgrp_refresh_mtu(ill->ill_grp);
11251 
11252 	return (0);
11253 }
11254 
11255 /* ARGSUSED */
11256 int
11257 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11258     ip_ioctl_cmd_t *ipi, void *if_req)
11259 {
11260 	struct lif_ifinfo_req *lir;
11261 	ill_t *ill = ipif->ipif_ill;
11262 
11263 	ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11264 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11265 	if (ipif->ipif_id != 0)
11266 		return (EINVAL);
11267 
11268 	lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11269 	lir->lir_maxhops = ill->ill_max_hops;
11270 	lir->lir_reachtime = ill->ill_reachable_time;
11271 	lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11272 	lir->lir_maxmtu = ill->ill_mtu;
11273 
11274 	return (0);
11275 }
11276 
11277 /*
11278  * Return best guess as to the subnet mask for the specified address.
11279  * Based on the subnet masks for all the configured interfaces.
11280  *
11281  * We end up returning a zero mask in the case of default, multicast or
11282  * experimental.
11283  */
11284 static ipaddr_t
11285 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11286 {
11287 	ipaddr_t net_mask;
11288 	ill_t	*ill;
11289 	ipif_t	*ipif;
11290 	ill_walk_context_t ctx;
11291 	ipif_t	*fallback_ipif = NULL;
11292 
11293 	net_mask = ip_net_mask(addr);
11294 	if (net_mask == 0) {
11295 		*ipifp = NULL;
11296 		return (0);
11297 	}
11298 
11299 	/* Let's check to see if this is maybe a local subnet route. */
11300 	/* this function only applies to IPv4 interfaces */
11301 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11302 	ill = ILL_START_WALK_V4(&ctx, ipst);
11303 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11304 		mutex_enter(&ill->ill_lock);
11305 		for (ipif = ill->ill_ipif; ipif != NULL;
11306 		    ipif = ipif->ipif_next) {
11307 			if (IPIF_IS_CONDEMNED(ipif))
11308 				continue;
11309 			if (!(ipif->ipif_flags & IPIF_UP))
11310 				continue;
11311 			if ((ipif->ipif_subnet & net_mask) ==
11312 			    (addr & net_mask)) {
11313 				/*
11314 				 * Don't trust pt-pt interfaces if there are
11315 				 * other interfaces.
11316 				 */
11317 				if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11318 					if (fallback_ipif == NULL) {
11319 						ipif_refhold_locked(ipif);
11320 						fallback_ipif = ipif;
11321 					}
11322 					continue;
11323 				}
11324 
11325 				/*
11326 				 * Fine. Just assume the same net mask as the
11327 				 * directly attached subnet interface is using.
11328 				 */
11329 				ipif_refhold_locked(ipif);
11330 				mutex_exit(&ill->ill_lock);
11331 				rw_exit(&ipst->ips_ill_g_lock);
11332 				if (fallback_ipif != NULL)
11333 					ipif_refrele(fallback_ipif);
11334 				*ipifp = ipif;
11335 				return (ipif->ipif_net_mask);
11336 			}
11337 		}
11338 		mutex_exit(&ill->ill_lock);
11339 	}
11340 	rw_exit(&ipst->ips_ill_g_lock);
11341 
11342 	*ipifp = fallback_ipif;
11343 	return ((fallback_ipif != NULL) ?
11344 	    fallback_ipif->ipif_net_mask : net_mask);
11345 }
11346 
11347 /*
11348  * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11349  */
11350 static void
11351 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11352 {
11353 	IOCP	iocp;
11354 	ipft_t	*ipft;
11355 	ipllc_t	*ipllc;
11356 	mblk_t	*mp1;
11357 	cred_t	*cr;
11358 	int	error = 0;
11359 	conn_t	*connp;
11360 
11361 	ip1dbg(("ip_wput_ioctl"));
11362 	iocp = (IOCP)mp->b_rptr;
11363 	mp1 = mp->b_cont;
11364 	if (mp1 == NULL) {
11365 		iocp->ioc_error = EINVAL;
11366 		mp->b_datap->db_type = M_IOCNAK;
11367 		iocp->ioc_count = 0;
11368 		qreply(q, mp);
11369 		return;
11370 	}
11371 
11372 	/*
11373 	 * These IOCTLs provide various control capabilities to
11374 	 * upstream agents such as ULPs and processes.	There
11375 	 * are currently two such IOCTLs implemented.  They
11376 	 * are used by TCP to provide update information for
11377 	 * existing IREs and to forcibly delete an IRE for a
11378 	 * host that is not responding, thereby forcing an
11379 	 * attempt at a new route.
11380 	 */
11381 	iocp->ioc_error = EINVAL;
11382 	if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11383 		goto done;
11384 
11385 	ipllc = (ipllc_t *)mp1->b_rptr;
11386 	for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11387 		if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11388 			break;
11389 	}
11390 	/*
11391 	 * prefer credential from mblk over ioctl;
11392 	 * see ip_sioctl_copyin_setup
11393 	 */
11394 	cr = msg_getcred(mp, NULL);
11395 	if (cr == NULL)
11396 		cr = iocp->ioc_cr;
11397 
11398 	/*
11399 	 * Refhold the conn in case the request gets queued up in some lookup
11400 	 */
11401 	ASSERT(CONN_Q(q));
11402 	connp = Q_TO_CONN(q);
11403 	CONN_INC_REF(connp);
11404 	if (ipft->ipft_pfi &&
11405 	    ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11406 	    pullupmsg(mp1, ipft->ipft_min_size))) {
11407 		error = (*ipft->ipft_pfi)(q,
11408 		    (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11409 	}
11410 	if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11411 		/*
11412 		 * CONN_OPER_PENDING_DONE happens in the function called
11413 		 * through ipft_pfi above.
11414 		 */
11415 		return;
11416 	}
11417 
11418 	CONN_OPER_PENDING_DONE(connp);
11419 	if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11420 		freemsg(mp);
11421 		return;
11422 	}
11423 	iocp->ioc_error = error;
11424 
11425 done:
11426 	mp->b_datap->db_type = M_IOCACK;
11427 	if (iocp->ioc_error)
11428 		iocp->ioc_count = 0;
11429 	qreply(q, mp);
11430 }
11431 
11432 /*
11433  * Assign a unique id for the ipif. This is used by sctp_addr.c
11434  * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11435  */
11436 static void
11437 ipif_assign_seqid(ipif_t *ipif)
11438 {
11439 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
11440 
11441 	ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11442 }
11443 
11444 /*
11445  * Clone the contents of `sipif' to `dipif'.  Requires that both ipifs are
11446  * administratively down (i.e., no DAD), of the same type, and locked.  Note
11447  * that the clone is complete -- including the seqid -- and the expectation is
11448  * that the caller will either free or overwrite `sipif' before it's unlocked.
11449  */
11450 static void
11451 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11452 {
11453 	ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11454 	ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11455 	ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11456 	ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11457 	ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11458 
11459 	dipif->ipif_flags = sipif->ipif_flags;
11460 	dipif->ipif_metric = sipif->ipif_metric;
11461 	dipif->ipif_zoneid = sipif->ipif_zoneid;
11462 	dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11463 	dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11464 	dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11465 	dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11466 	dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11467 
11468 	/*
11469 	 * As per the comment atop the function, we assume that these sipif
11470 	 * fields will be changed before sipif is unlocked.
11471 	 */
11472 	dipif->ipif_seqid = sipif->ipif_seqid;
11473 	dipif->ipif_state_flags = sipif->ipif_state_flags;
11474 }
11475 
11476 /*
11477  * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11478  * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11479  * (unreferenced) ipif.  Also, if `sipif' is used by the current xop, then
11480  * transfer the xop to `dipif'.  Requires that all ipifs are administratively
11481  * down (i.e., no DAD), of the same type, and unlocked.
11482  */
11483 static void
11484 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11485 {
11486 	ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11487 	ipxop_t *ipx = ipsq->ipsq_xop;
11488 
11489 	ASSERT(sipif != dipif);
11490 	ASSERT(sipif != virgipif);
11491 
11492 	/*
11493 	 * Grab all of the locks that protect the ipif in a defined order.
11494 	 */
11495 	GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11496 
11497 	ipif_clone(sipif, dipif);
11498 	if (virgipif != NULL) {
11499 		ipif_clone(virgipif, sipif);
11500 		mi_free(virgipif);
11501 	}
11502 
11503 	RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11504 
11505 	/*
11506 	 * Transfer ownership of the current xop, if necessary.
11507 	 */
11508 	if (ipx->ipx_current_ipif == sipif) {
11509 		ASSERT(ipx->ipx_pending_ipif == NULL);
11510 		mutex_enter(&ipx->ipx_lock);
11511 		ipx->ipx_current_ipif = dipif;
11512 		mutex_exit(&ipx->ipx_lock);
11513 	}
11514 
11515 	if (virgipif == NULL)
11516 		mi_free(sipif);
11517 }
11518 
11519 /*
11520  * checks if:
11521  *	- <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11522  *	- logical interface is within the allowed range
11523  */
11524 static int
11525 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11526 {
11527 	if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11528 		return (ENAMETOOLONG);
11529 
11530 	if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11531 		return (ERANGE);
11532 	return (0);
11533 }
11534 
11535 /*
11536  * Insert the ipif, so that the list of ipifs on the ill will be sorted
11537  * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11538  * be inserted into the first space available in the list. The value of
11539  * ipif_id will then be set to the appropriate value for its position.
11540  */
11541 static int
11542 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11543 {
11544 	ill_t *ill;
11545 	ipif_t *tipif;
11546 	ipif_t **tipifp;
11547 	int id, err;
11548 	ip_stack_t	*ipst;
11549 
11550 	ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11551 	    IAM_WRITER_IPIF(ipif));
11552 
11553 	ill = ipif->ipif_ill;
11554 	ASSERT(ill != NULL);
11555 	ipst = ill->ill_ipst;
11556 
11557 	/*
11558 	 * In the case of lo0:0 we already hold the ill_g_lock.
11559 	 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11560 	 * ipif_insert.
11561 	 */
11562 	if (acquire_g_lock)
11563 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11564 	mutex_enter(&ill->ill_lock);
11565 	id = ipif->ipif_id;
11566 	tipifp = &(ill->ill_ipif);
11567 	if (id == -1) {	/* need to find a real id */
11568 		id = 0;
11569 		while ((tipif = *tipifp) != NULL) {
11570 			ASSERT(tipif->ipif_id >= id);
11571 			if (tipif->ipif_id != id)
11572 				break; /* non-consecutive id */
11573 			id++;
11574 			tipifp = &(tipif->ipif_next);
11575 		}
11576 		if ((err = is_lifname_valid(ill, id)) != 0) {
11577 			mutex_exit(&ill->ill_lock);
11578 			if (acquire_g_lock)
11579 				rw_exit(&ipst->ips_ill_g_lock);
11580 			return (err);
11581 		}
11582 		ipif->ipif_id = id; /* assign new id */
11583 	} else if ((err = is_lifname_valid(ill, id)) == 0) {
11584 		/* we have a real id; insert ipif in the right place */
11585 		while ((tipif = *tipifp) != NULL) {
11586 			ASSERT(tipif->ipif_id != id);
11587 			if (tipif->ipif_id > id)
11588 				break; /* found correct location */
11589 			tipifp = &(tipif->ipif_next);
11590 		}
11591 	} else {
11592 		mutex_exit(&ill->ill_lock);
11593 		if (acquire_g_lock)
11594 			rw_exit(&ipst->ips_ill_g_lock);
11595 		return (err);
11596 	}
11597 
11598 	ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11599 
11600 	ipif->ipif_next = tipif;
11601 	*tipifp = ipif;
11602 	mutex_exit(&ill->ill_lock);
11603 	if (acquire_g_lock)
11604 		rw_exit(&ipst->ips_ill_g_lock);
11605 
11606 	return (0);
11607 }
11608 
11609 static void
11610 ipif_remove(ipif_t *ipif)
11611 {
11612 	ipif_t	**ipifp;
11613 	ill_t	*ill = ipif->ipif_ill;
11614 
11615 	ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11616 
11617 	mutex_enter(&ill->ill_lock);
11618 	ipifp = &ill->ill_ipif;
11619 	for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11620 		if (*ipifp == ipif) {
11621 			*ipifp = ipif->ipif_next;
11622 			break;
11623 		}
11624 	}
11625 	mutex_exit(&ill->ill_lock);
11626 }
11627 
11628 /*
11629  * Allocate and initialize a new interface control structure.  (Always
11630  * called as writer.)
11631  * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11632  * is not part of the global linked list of ills. ipif_seqid is unique
11633  * in the system and to preserve the uniqueness, it is assigned only
11634  * when ill becomes part of the global list. At that point ill will
11635  * have a name. If it doesn't get assigned here, it will get assigned
11636  * in ipif_set_values() as part of SIOCSLIFNAME processing.
11637  * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11638  * the interface flags or any other information from the DL_INFO_ACK for
11639  * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11640  * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11641  * second DL_INFO_ACK comes in from the driver.
11642  */
11643 static ipif_t *
11644 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11645     boolean_t insert, int *errorp)
11646 {
11647 	int err;
11648 	ipif_t	*ipif;
11649 	ip_stack_t *ipst = ill->ill_ipst;
11650 
11651 	ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11652 	    ill->ill_name, id, (void *)ill));
11653 	ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11654 
11655 	if (errorp != NULL)
11656 		*errorp = 0;
11657 
11658 	if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11659 		if (errorp != NULL)
11660 			*errorp = ENOMEM;
11661 		return (NULL);
11662 	}
11663 	*ipif = ipif_zero;	/* start clean */
11664 
11665 	ipif->ipif_ill = ill;
11666 	ipif->ipif_id = id;	/* could be -1 */
11667 	/*
11668 	 * Inherit the zoneid from the ill; for the shared stack instance
11669 	 * this is always the global zone
11670 	 */
11671 	ipif->ipif_zoneid = ill->ill_zoneid;
11672 
11673 	ipif->ipif_refcnt = 0;
11674 
11675 	if (insert) {
11676 		if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
11677 			mi_free(ipif);
11678 			if (errorp != NULL)
11679 				*errorp = err;
11680 			return (NULL);
11681 		}
11682 		/* -1 id should have been replaced by real id */
11683 		id = ipif->ipif_id;
11684 		ASSERT(id >= 0);
11685 	}
11686 
11687 	if (ill->ill_name[0] != '\0')
11688 		ipif_assign_seqid(ipif);
11689 
11690 	/*
11691 	 * If this is the zeroth ipif on the IPMP ill, create the illgrp
11692 	 * (which must not exist yet because the zeroth ipif is created once
11693 	 * per ill).  However, do not not link it to the ipmp_grp_t until
11694 	 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
11695 	 */
11696 	if (id == 0 && IS_IPMP(ill)) {
11697 		if (ipmp_illgrp_create(ill) == NULL) {
11698 			if (insert) {
11699 				rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11700 				ipif_remove(ipif);
11701 				rw_exit(&ipst->ips_ill_g_lock);
11702 			}
11703 			mi_free(ipif);
11704 			if (errorp != NULL)
11705 				*errorp = ENOMEM;
11706 			return (NULL);
11707 		}
11708 	}
11709 
11710 	/*
11711 	 * We grab ill_lock to protect the flag changes.  The ipif is still
11712 	 * not up and can't be looked up until the ioctl completes and the
11713 	 * IPIF_CHANGING flag is cleared.
11714 	 */
11715 	mutex_enter(&ill->ill_lock);
11716 
11717 	ipif->ipif_ire_type = ire_type;
11718 
11719 	if (ipif->ipif_isv6) {
11720 		ill->ill_flags |= ILLF_IPV6;
11721 	} else {
11722 		ipaddr_t inaddr_any = INADDR_ANY;
11723 
11724 		ill->ill_flags |= ILLF_IPV4;
11725 
11726 		/* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
11727 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11728 		    &ipif->ipif_v6lcl_addr);
11729 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11730 		    &ipif->ipif_v6subnet);
11731 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11732 		    &ipif->ipif_v6net_mask);
11733 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11734 		    &ipif->ipif_v6brd_addr);
11735 		IN6_IPADDR_TO_V4MAPPED(inaddr_any,
11736 		    &ipif->ipif_v6pp_dst_addr);
11737 	}
11738 
11739 	/*
11740 	 * Don't set the interface flags etc. now, will do it in
11741 	 * ip_ll_subnet_defaults.
11742 	 */
11743 	if (!initialize)
11744 		goto out;
11745 
11746 	/*
11747 	 * NOTE: The IPMP meta-interface is special-cased because it starts
11748 	 * with no underlying interfaces (and thus an unknown broadcast
11749 	 * address length), but all interfaces that can be placed into an IPMP
11750 	 * group are required to be broadcast-capable.
11751 	 */
11752 	if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
11753 		/*
11754 		 * Later detect lack of DLPI driver multicast capability by
11755 		 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
11756 		 */
11757 		ill->ill_flags |= ILLF_MULTICAST;
11758 		if (!ipif->ipif_isv6)
11759 			ipif->ipif_flags |= IPIF_BROADCAST;
11760 	} else {
11761 		if (ill->ill_net_type != IRE_LOOPBACK) {
11762 			if (ipif->ipif_isv6)
11763 				/*
11764 				 * Note: xresolv interfaces will eventually need
11765 				 * NOARP set here as well, but that will require
11766 				 * those external resolvers to have some
11767 				 * knowledge of that flag and act appropriately.
11768 				 * Not to be changed at present.
11769 				 */
11770 				ill->ill_flags |= ILLF_NONUD;
11771 			else
11772 				ill->ill_flags |= ILLF_NOARP;
11773 		}
11774 		if (ill->ill_phys_addr_length == 0) {
11775 			if (IS_VNI(ill)) {
11776 				ipif->ipif_flags |= IPIF_NOXMIT;
11777 			} else {
11778 				/* pt-pt supports multicast. */
11779 				ill->ill_flags |= ILLF_MULTICAST;
11780 				if (ill->ill_net_type != IRE_LOOPBACK)
11781 					ipif->ipif_flags |= IPIF_POINTOPOINT;
11782 			}
11783 		}
11784 	}
11785 out:
11786 	mutex_exit(&ill->ill_lock);
11787 	return (ipif);
11788 }
11789 
11790 /*
11791  * Remove the neighbor cache entries associated with this logical
11792  * interface.
11793  */
11794 int
11795 ipif_arp_down(ipif_t *ipif)
11796 {
11797 	ill_t	*ill = ipif->ipif_ill;
11798 	int	err = 0;
11799 
11800 	ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
11801 	ASSERT(IAM_WRITER_IPIF(ipif));
11802 
11803 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
11804 	    ill_t *, ill, ipif_t *, ipif);
11805 	ipif_nce_down(ipif);
11806 
11807 	/*
11808 	 * If this is the last ipif that is going down and there are no
11809 	 * duplicate addresses we may yet attempt to re-probe, then we need to
11810 	 * clean up ARP completely.
11811 	 */
11812 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
11813 	    !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
11814 		/*
11815 		 * If this was the last ipif on an IPMP interface, purge any
11816 		 * static ARP entries associated with it.
11817 		 */
11818 		if (IS_IPMP(ill))
11819 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
11820 
11821 		/* UNBIND, DETACH */
11822 		err = arp_ll_down(ill);
11823 	}
11824 
11825 	return (err);
11826 }
11827 
11828 /*
11829  * Get the resolver set up for a new IP address.  (Always called as writer.)
11830  * Called both for IPv4 and IPv6 interfaces, though it only does some
11831  * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
11832  *
11833  * The enumerated value res_act tunes the behavior:
11834  * 	* Res_act_initial: set up all the resolver structures for a new
11835  *	  IP address.
11836  *	* Res_act_defend: tell ARP that it needs to send a single gratuitous
11837  *	  ARP message in defense of the address.
11838  *	* Res_act_rebind: tell ARP to change the hardware address for an IP
11839  *	  address (and issue gratuitous ARPs).  Used by ipmp_ill_bind_ipif().
11840  *
11841  * Returns zero on success, or an errno upon failure.
11842  */
11843 int
11844 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
11845 {
11846 	ill_t		*ill = ipif->ipif_ill;
11847 	int		err;
11848 	boolean_t	was_dup;
11849 
11850 	ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
11851 	    ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
11852 	ASSERT(IAM_WRITER_IPIF(ipif));
11853 
11854 	was_dup = B_FALSE;
11855 	if (res_act == Res_act_initial) {
11856 		ipif->ipif_addr_ready = 0;
11857 		/*
11858 		 * We're bringing an interface up here.  There's no way that we
11859 		 * should need to shut down ARP now.
11860 		 */
11861 		mutex_enter(&ill->ill_lock);
11862 		if (ipif->ipif_flags & IPIF_DUPLICATE) {
11863 			ipif->ipif_flags &= ~IPIF_DUPLICATE;
11864 			ill->ill_ipif_dup_count--;
11865 			was_dup = B_TRUE;
11866 		}
11867 		mutex_exit(&ill->ill_lock);
11868 	}
11869 	if (ipif->ipif_recovery_id != 0)
11870 		(void) untimeout(ipif->ipif_recovery_id);
11871 	ipif->ipif_recovery_id = 0;
11872 	if (ill->ill_net_type != IRE_IF_RESOLVER) {
11873 		ipif->ipif_addr_ready = 1;
11874 		return (0);
11875 	}
11876 	/* NDP will set the ipif_addr_ready flag when it's ready */
11877 	if (ill->ill_isv6)
11878 		return (0);
11879 
11880 	err = ipif_arp_up(ipif, res_act, was_dup);
11881 	return (err);
11882 }
11883 
11884 /*
11885  * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
11886  * when a link has just gone back up.
11887  */
11888 static void
11889 ipif_nce_start_dad(ipif_t *ipif)
11890 {
11891 	ncec_t *ncec;
11892 	ill_t *ill = ipif->ipif_ill;
11893 	boolean_t isv6 = ill->ill_isv6;
11894 
11895 	if (isv6) {
11896 		ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
11897 		    &ipif->ipif_v6lcl_addr);
11898 	} else {
11899 		ipaddr_t v4addr;
11900 
11901 		if (ill->ill_net_type != IRE_IF_RESOLVER ||
11902 		    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
11903 		    ipif->ipif_lcl_addr == INADDR_ANY) {
11904 			/*
11905 			 * If we can't contact ARP for some reason,
11906 			 * that's not really a problem.  Just send
11907 			 * out the routing socket notification that
11908 			 * DAD completion would have done, and continue.
11909 			 */
11910 			ipif_mask_reply(ipif);
11911 			ipif_up_notify(ipif);
11912 			ipif->ipif_addr_ready = 1;
11913 			return;
11914 		}
11915 
11916 		IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
11917 		ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
11918 	}
11919 
11920 	if (ncec == NULL) {
11921 		ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
11922 		    (void *)ipif));
11923 		return;
11924 	}
11925 	if (!nce_restart_dad(ncec)) {
11926 		/*
11927 		 * If we can't restart DAD for some reason, that's not really a
11928 		 * problem.  Just send out the routing socket notification that
11929 		 * DAD completion would have done, and continue.
11930 		 */
11931 		ipif_up_notify(ipif);
11932 		ipif->ipif_addr_ready = 1;
11933 	}
11934 	ncec_refrele(ncec);
11935 }
11936 
11937 /*
11938  * Restart duplicate address detection on all interfaces on the given ill.
11939  *
11940  * This is called when an interface transitions from down to up
11941  * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
11942  *
11943  * Note that since the underlying physical link has transitioned, we must cause
11944  * at least one routing socket message to be sent here, either via DAD
11945  * completion or just by default on the first ipif.  (If we don't do this, then
11946  * in.mpathd will see long delays when doing link-based failure recovery.)
11947  */
11948 void
11949 ill_restart_dad(ill_t *ill, boolean_t went_up)
11950 {
11951 	ipif_t *ipif;
11952 
11953 	if (ill == NULL)
11954 		return;
11955 
11956 	/*
11957 	 * If layer two doesn't support duplicate address detection, then just
11958 	 * send the routing socket message now and be done with it.
11959 	 */
11960 	if (!ill->ill_isv6 && arp_no_defense) {
11961 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
11962 		return;
11963 	}
11964 
11965 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
11966 		if (went_up) {
11967 
11968 			if (ipif->ipif_flags & IPIF_UP) {
11969 				ipif_nce_start_dad(ipif);
11970 			} else if (ipif->ipif_flags & IPIF_DUPLICATE) {
11971 				/*
11972 				 * kick off the bring-up process now.
11973 				 */
11974 				ipif_do_recovery(ipif);
11975 			} else {
11976 				/*
11977 				 * Unfortunately, the first ipif is "special"
11978 				 * and represents the underlying ill in the
11979 				 * routing socket messages.  Thus, when this
11980 				 * one ipif is down, we must still notify so
11981 				 * that the user knows the IFF_RUNNING status
11982 				 * change.  (If the first ipif is up, then
11983 				 * we'll handle eventual routing socket
11984 				 * notification via DAD completion.)
11985 				 */
11986 				if (ipif == ill->ill_ipif) {
11987 					ip_rts_ifmsg(ill->ill_ipif,
11988 					    RTSQ_DEFAULT);
11989 				}
11990 			}
11991 		} else {
11992 			/*
11993 			 * After link down, we'll need to send a new routing
11994 			 * message when the link comes back, so clear
11995 			 * ipif_addr_ready.
11996 			 */
11997 			ipif->ipif_addr_ready = 0;
11998 		}
11999 	}
12000 
12001 	/*
12002 	 * If we've torn down links, then notify the user right away.
12003 	 */
12004 	if (!went_up)
12005 		ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12006 }
12007 
12008 static void
12009 ipsq_delete(ipsq_t *ipsq)
12010 {
12011 	ipxop_t *ipx = ipsq->ipsq_xop;
12012 
12013 	ipsq->ipsq_ipst = NULL;
12014 	ASSERT(ipsq->ipsq_phyint == NULL);
12015 	ASSERT(ipsq->ipsq_xop != NULL);
12016 	ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12017 	ASSERT(ipx->ipx_pending_mp == NULL);
12018 	kmem_free(ipsq, sizeof (ipsq_t));
12019 }
12020 
12021 static int
12022 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12023 {
12024 	int err = 0;
12025 	ipif_t *ipif;
12026 
12027 	if (ill == NULL)
12028 		return (0);
12029 
12030 	ASSERT(IAM_WRITER_ILL(ill));
12031 	ill->ill_up_ipifs = B_TRUE;
12032 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12033 		if (ipif->ipif_was_up) {
12034 			if (!(ipif->ipif_flags & IPIF_UP))
12035 				err = ipif_up(ipif, q, mp);
12036 			ipif->ipif_was_up = B_FALSE;
12037 			if (err != 0) {
12038 				ASSERT(err == EINPROGRESS);
12039 				return (err);
12040 			}
12041 		}
12042 	}
12043 	ill->ill_up_ipifs = B_FALSE;
12044 	return (0);
12045 }
12046 
12047 /*
12048  * This function is called to bring up all the ipifs that were up before
12049  * bringing the ill down via ill_down_ipifs().
12050  */
12051 int
12052 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12053 {
12054 	int err;
12055 
12056 	ASSERT(IAM_WRITER_ILL(ill));
12057 
12058 	if (ill->ill_replumbing) {
12059 		ill->ill_replumbing = 0;
12060 		/*
12061 		 * Send down REPLUMB_DONE notification followed by the
12062 		 * BIND_REQ on the arp stream.
12063 		 */
12064 		if (!ill->ill_isv6)
12065 			arp_send_replumb_conf(ill);
12066 	}
12067 	err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12068 	if (err != 0)
12069 		return (err);
12070 
12071 	return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12072 }
12073 
12074 /*
12075  * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12076  * down the ipifs without sending DL_UNBIND_REQ to the driver.
12077  */
12078 static void
12079 ill_down_ipifs(ill_t *ill, boolean_t logical)
12080 {
12081 	ipif_t *ipif;
12082 
12083 	ASSERT(IAM_WRITER_ILL(ill));
12084 
12085 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12086 		/*
12087 		 * We go through the ipif_down logic even if the ipif
12088 		 * is already down, since routes can be added based
12089 		 * on down ipifs. Going through ipif_down once again
12090 		 * will delete any IREs created based on these routes.
12091 		 */
12092 		if (ipif->ipif_flags & IPIF_UP)
12093 			ipif->ipif_was_up = B_TRUE;
12094 
12095 		if (logical) {
12096 			(void) ipif_logical_down(ipif, NULL, NULL);
12097 			ipif_non_duplicate(ipif);
12098 			(void) ipif_down_tail(ipif);
12099 		} else {
12100 			(void) ipif_down(ipif, NULL, NULL);
12101 		}
12102 	}
12103 }
12104 
12105 /*
12106  * Redo source address selection.  This makes IXAF_VERIFY_SOURCE take
12107  * a look again at valid source addresses.
12108  * This should be called each time after the set of source addresses has been
12109  * changed.
12110  */
12111 void
12112 ip_update_source_selection(ip_stack_t *ipst)
12113 {
12114 	/* We skip past SRC_GENERATION_VERIFY */
12115 	if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12116 	    SRC_GENERATION_VERIFY)
12117 		atomic_add_32(&ipst->ips_src_generation, 1);
12118 }
12119 
12120 /*
12121  * Finish the group join started in ip_sioctl_groupname().
12122  */
12123 /* ARGSUSED */
12124 static void
12125 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12126 {
12127 	ill_t		*ill = q->q_ptr;
12128 	phyint_t	*phyi = ill->ill_phyint;
12129 	ipmp_grp_t	*grp = phyi->phyint_grp;
12130 	ip_stack_t	*ipst = ill->ill_ipst;
12131 
12132 	/* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12133 	ASSERT(!IS_IPMP(ill) && grp != NULL);
12134 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12135 
12136 	if (phyi->phyint_illv4 != NULL) {
12137 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12138 		VERIFY(grp->gr_pendv4-- > 0);
12139 		rw_exit(&ipst->ips_ipmp_lock);
12140 		ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12141 	}
12142 	if (phyi->phyint_illv6 != NULL) {
12143 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12144 		VERIFY(grp->gr_pendv6-- > 0);
12145 		rw_exit(&ipst->ips_ipmp_lock);
12146 		ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12147 	}
12148 	freemsg(mp);
12149 }
12150 
12151 /*
12152  * Process an SIOCSLIFGROUPNAME request.
12153  */
12154 /* ARGSUSED */
12155 int
12156 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12157     ip_ioctl_cmd_t *ipip, void *ifreq)
12158 {
12159 	struct lifreq	*lifr = ifreq;
12160 	ill_t		*ill = ipif->ipif_ill;
12161 	ip_stack_t	*ipst = ill->ill_ipst;
12162 	phyint_t	*phyi = ill->ill_phyint;
12163 	ipmp_grp_t	*grp = phyi->phyint_grp;
12164 	mblk_t		*ipsq_mp;
12165 	int		err = 0;
12166 
12167 	/*
12168 	 * Note that phyint_grp can only change here, where we're exclusive.
12169 	 */
12170 	ASSERT(IAM_WRITER_ILL(ill));
12171 
12172 	if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12173 	    (phyi->phyint_flags & PHYI_VIRTUAL))
12174 		return (EINVAL);
12175 
12176 	lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12177 
12178 	rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12179 
12180 	/*
12181 	 * If the name hasn't changed, there's nothing to do.
12182 	 */
12183 	if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12184 		goto unlock;
12185 
12186 	/*
12187 	 * Handle requests to rename an IPMP meta-interface.
12188 	 *
12189 	 * Note that creation of the IPMP meta-interface is handled in
12190 	 * userland through the standard plumbing sequence.  As part of the
12191 	 * plumbing the IPMP meta-interface, its initial groupname is set to
12192 	 * the name of the interface (see ipif_set_values_tail()).
12193 	 */
12194 	if (IS_IPMP(ill)) {
12195 		err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12196 		goto unlock;
12197 	}
12198 
12199 	/*
12200 	 * Handle requests to add or remove an IP interface from a group.
12201 	 */
12202 	if (lifr->lifr_groupname[0] != '\0') {			/* add */
12203 		/*
12204 		 * Moves are handled by first removing the interface from
12205 		 * its existing group, and then adding it to another group.
12206 		 * So, fail if it's already in a group.
12207 		 */
12208 		if (IS_UNDER_IPMP(ill)) {
12209 			err = EALREADY;
12210 			goto unlock;
12211 		}
12212 
12213 		grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12214 		if (grp == NULL) {
12215 			err = ENOENT;
12216 			goto unlock;
12217 		}
12218 
12219 		/*
12220 		 * Check if the phyint and its ills are suitable for
12221 		 * inclusion into the group.
12222 		 */
12223 		if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12224 			goto unlock;
12225 
12226 		/*
12227 		 * Checks pass; join the group, and enqueue the remaining
12228 		 * illgrp joins for when we've become part of the group xop
12229 		 * and are exclusive across its IPSQs.  Since qwriter_ip()
12230 		 * requires an mblk_t to scribble on, and since `mp' will be
12231 		 * freed as part of completing the ioctl, allocate another.
12232 		 */
12233 		if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12234 			err = ENOMEM;
12235 			goto unlock;
12236 		}
12237 
12238 		/*
12239 		 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12240 		 * IPMP meta-interface ills needed by `phyi' cannot go away
12241 		 * before ip_join_illgrps() is called back.  See the comments
12242 		 * in ip_sioctl_plink_ipmp() for more.
12243 		 */
12244 		if (phyi->phyint_illv4 != NULL)
12245 			grp->gr_pendv4++;
12246 		if (phyi->phyint_illv6 != NULL)
12247 			grp->gr_pendv6++;
12248 
12249 		rw_exit(&ipst->ips_ipmp_lock);
12250 
12251 		ipmp_phyint_join_grp(phyi, grp);
12252 		ill_refhold(ill);
12253 		qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12254 		    SWITCH_OP, B_FALSE);
12255 		return (0);
12256 	} else {
12257 		/*
12258 		 * Request to remove the interface from a group.  If the
12259 		 * interface is not in a group, this trivially succeeds.
12260 		 */
12261 		rw_exit(&ipst->ips_ipmp_lock);
12262 		if (IS_UNDER_IPMP(ill))
12263 			ipmp_phyint_leave_grp(phyi);
12264 		return (0);
12265 	}
12266 unlock:
12267 	rw_exit(&ipst->ips_ipmp_lock);
12268 	return (err);
12269 }
12270 
12271 /*
12272  * Process an SIOCGLIFBINDING request.
12273  */
12274 /* ARGSUSED */
12275 int
12276 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12277     ip_ioctl_cmd_t *ipip, void *ifreq)
12278 {
12279 	ill_t		*ill;
12280 	struct lifreq	*lifr = ifreq;
12281 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12282 
12283 	if (!IS_IPMP(ipif->ipif_ill))
12284 		return (EINVAL);
12285 
12286 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12287 	if ((ill = ipif->ipif_bound_ill) == NULL)
12288 		lifr->lifr_binding[0] = '\0';
12289 	else
12290 		(void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12291 	rw_exit(&ipst->ips_ipmp_lock);
12292 	return (0);
12293 }
12294 
12295 /*
12296  * Process an SIOCGLIFGROUPNAME request.
12297  */
12298 /* ARGSUSED */
12299 int
12300 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12301     ip_ioctl_cmd_t *ipip, void *ifreq)
12302 {
12303 	ipmp_grp_t	*grp;
12304 	struct lifreq	*lifr = ifreq;
12305 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
12306 
12307 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12308 	if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12309 		lifr->lifr_groupname[0] = '\0';
12310 	else
12311 		(void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12312 	rw_exit(&ipst->ips_ipmp_lock);
12313 	return (0);
12314 }
12315 
12316 /*
12317  * Process an SIOCGLIFGROUPINFO request.
12318  */
12319 /* ARGSUSED */
12320 int
12321 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12322     ip_ioctl_cmd_t *ipip, void *dummy)
12323 {
12324 	ipmp_grp_t	*grp;
12325 	lifgroupinfo_t	*lifgr;
12326 	ip_stack_t	*ipst = CONNQ_TO_IPST(q);
12327 
12328 	/* ip_wput_nondata() verified mp->b_cont->b_cont */
12329 	lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12330 	lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12331 
12332 	rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12333 	if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12334 		rw_exit(&ipst->ips_ipmp_lock);
12335 		return (ENOENT);
12336 	}
12337 	ipmp_grp_info(grp, lifgr);
12338 	rw_exit(&ipst->ips_ipmp_lock);
12339 	return (0);
12340 }
12341 
12342 static void
12343 ill_dl_down(ill_t *ill)
12344 {
12345 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12346 
12347 	/*
12348 	 * The ill is down; unbind but stay attached since we're still
12349 	 * associated with a PPA. If we have negotiated DLPI capabilites
12350 	 * with the data link service provider (IDS_OK) then reset them.
12351 	 * The interval between unbinding and rebinding is potentially
12352 	 * unbounded hence we cannot assume things will be the same.
12353 	 * The DLPI capabilities will be probed again when the data link
12354 	 * is brought up.
12355 	 */
12356 	mblk_t	*mp = ill->ill_unbind_mp;
12357 
12358 	ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12359 
12360 	if (!ill->ill_replumbing) {
12361 		/* Free all ilms for this ill */
12362 		update_conn_ill(ill, ill->ill_ipst);
12363 	} else {
12364 		ill_leave_multicast(ill);
12365 	}
12366 
12367 	ill->ill_unbind_mp = NULL;
12368 	if (mp != NULL) {
12369 		ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12370 		    dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12371 		    ill->ill_name));
12372 		mutex_enter(&ill->ill_lock);
12373 		ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12374 		mutex_exit(&ill->ill_lock);
12375 		/*
12376 		 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12377 		 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12378 		 * ill_capability_dld_disable disable rightaway. If this is not
12379 		 * an unplumb operation then the disable happens on receipt of
12380 		 * the capab ack via ip_rput_dlpi_writer ->
12381 		 * ill_capability_ack_thr. In both cases the order of
12382 		 * the operations seen by DLD is capability disable followed
12383 		 * by DL_UNBIND. Also the DLD capability disable needs a
12384 		 * cv_wait'able context.
12385 		 */
12386 		if (ill->ill_state_flags & ILL_CONDEMNED)
12387 			ill_capability_dld_disable(ill);
12388 		ill_capability_reset(ill, B_FALSE);
12389 		ill_dlpi_send(ill, mp);
12390 	}
12391 	mutex_enter(&ill->ill_lock);
12392 	ill->ill_dl_up = 0;
12393 	ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12394 	mutex_exit(&ill->ill_lock);
12395 }
12396 
12397 void
12398 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12399 {
12400 	union DL_primitives *dlp;
12401 	t_uscalar_t prim;
12402 	boolean_t waitack = B_FALSE;
12403 
12404 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12405 
12406 	dlp = (union DL_primitives *)mp->b_rptr;
12407 	prim = dlp->dl_primitive;
12408 
12409 	ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12410 	    dl_primstr(prim), prim, ill->ill_name));
12411 
12412 	switch (prim) {
12413 	case DL_PHYS_ADDR_REQ:
12414 	{
12415 		dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12416 		ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12417 		break;
12418 	}
12419 	case DL_BIND_REQ:
12420 		mutex_enter(&ill->ill_lock);
12421 		ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12422 		mutex_exit(&ill->ill_lock);
12423 		break;
12424 	}
12425 
12426 	/*
12427 	 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12428 	 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12429 	 * we only wait for the ACK of the DL_UNBIND_REQ.
12430 	 */
12431 	mutex_enter(&ill->ill_lock);
12432 	if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12433 	    (prim == DL_UNBIND_REQ)) {
12434 		ill->ill_dlpi_pending = prim;
12435 		waitack = B_TRUE;
12436 	}
12437 
12438 	mutex_exit(&ill->ill_lock);
12439 	DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12440 	    char *, dl_primstr(prim), ill_t *, ill);
12441 	putnext(ill->ill_wq, mp);
12442 
12443 	/*
12444 	 * There is no ack for DL_NOTIFY_CONF messages
12445 	 */
12446 	if (waitack && prim == DL_NOTIFY_CONF)
12447 		ill_dlpi_done(ill, prim);
12448 }
12449 
12450 /*
12451  * Helper function for ill_dlpi_send().
12452  */
12453 /* ARGSUSED */
12454 static void
12455 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12456 {
12457 	ill_dlpi_send(q->q_ptr, mp);
12458 }
12459 
12460 /*
12461  * Send a DLPI control message to the driver but make sure there
12462  * is only one outstanding message. Uses ill_dlpi_pending to tell
12463  * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12464  * when an ACK or a NAK is received to process the next queued message.
12465  */
12466 void
12467 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12468 {
12469 	mblk_t **mpp;
12470 
12471 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12472 
12473 	/*
12474 	 * To ensure that any DLPI requests for current exclusive operation
12475 	 * are always completely sent before any DLPI messages for other
12476 	 * operations, require writer access before enqueuing.
12477 	 */
12478 	if (!IAM_WRITER_ILL(ill)) {
12479 		ill_refhold(ill);
12480 		/* qwriter_ip() does the ill_refrele() */
12481 		qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12482 		    NEW_OP, B_TRUE);
12483 		return;
12484 	}
12485 
12486 	mutex_enter(&ill->ill_lock);
12487 	if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12488 		/* Must queue message. Tail insertion */
12489 		mpp = &ill->ill_dlpi_deferred;
12490 		while (*mpp != NULL)
12491 			mpp = &((*mpp)->b_next);
12492 
12493 		ip1dbg(("ill_dlpi_send: deferring request for %s "
12494 		    "while %s pending\n", ill->ill_name,
12495 		    dl_primstr(ill->ill_dlpi_pending)));
12496 
12497 		*mpp = mp;
12498 		mutex_exit(&ill->ill_lock);
12499 		return;
12500 	}
12501 	mutex_exit(&ill->ill_lock);
12502 	ill_dlpi_dispatch(ill, mp);
12503 }
12504 
12505 void
12506 ill_capability_send(ill_t *ill, mblk_t *mp)
12507 {
12508 	ill->ill_capab_pending_cnt++;
12509 	ill_dlpi_send(ill, mp);
12510 }
12511 
12512 void
12513 ill_capability_done(ill_t *ill)
12514 {
12515 	ASSERT(ill->ill_capab_pending_cnt != 0);
12516 
12517 	ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12518 
12519 	ill->ill_capab_pending_cnt--;
12520 	if (ill->ill_capab_pending_cnt == 0 &&
12521 	    ill->ill_dlpi_capab_state == IDCS_OK)
12522 		ill_capability_reset_alloc(ill);
12523 }
12524 
12525 /*
12526  * Send all deferred DLPI messages without waiting for their ACKs.
12527  */
12528 void
12529 ill_dlpi_send_deferred(ill_t *ill)
12530 {
12531 	mblk_t *mp, *nextmp;
12532 
12533 	/*
12534 	 * Clear ill_dlpi_pending so that the message is not queued in
12535 	 * ill_dlpi_send().
12536 	 */
12537 	mutex_enter(&ill->ill_lock);
12538 	ill->ill_dlpi_pending = DL_PRIM_INVAL;
12539 	mp = ill->ill_dlpi_deferred;
12540 	ill->ill_dlpi_deferred = NULL;
12541 	mutex_exit(&ill->ill_lock);
12542 
12543 	for (; mp != NULL; mp = nextmp) {
12544 		nextmp = mp->b_next;
12545 		mp->b_next = NULL;
12546 		ill_dlpi_send(ill, mp);
12547 	}
12548 }
12549 
12550 /*
12551  * Check if the DLPI primitive `prim' is pending; print a warning if not.
12552  */
12553 boolean_t
12554 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12555 {
12556 	t_uscalar_t pending;
12557 
12558 	mutex_enter(&ill->ill_lock);
12559 	if (ill->ill_dlpi_pending == prim) {
12560 		mutex_exit(&ill->ill_lock);
12561 		return (B_TRUE);
12562 	}
12563 
12564 	/*
12565 	 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12566 	 * without waiting, so don't print any warnings in that case.
12567 	 */
12568 	if (ill->ill_state_flags & ILL_CONDEMNED) {
12569 		mutex_exit(&ill->ill_lock);
12570 		return (B_FALSE);
12571 	}
12572 	pending = ill->ill_dlpi_pending;
12573 	mutex_exit(&ill->ill_lock);
12574 
12575 	if (pending == DL_PRIM_INVAL) {
12576 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12577 		    "received unsolicited ack for %s on %s\n",
12578 		    dl_primstr(prim), ill->ill_name);
12579 	} else {
12580 		(void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12581 		    "received unexpected ack for %s on %s (expecting %s)\n",
12582 		    dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12583 	}
12584 	return (B_FALSE);
12585 }
12586 
12587 /*
12588  * Complete the current DLPI operation associated with `prim' on `ill' and
12589  * start the next queued DLPI operation (if any).  If there are no queued DLPI
12590  * operations and the ill's current exclusive IPSQ operation has finished
12591  * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12592  * allow the next exclusive IPSQ operation to begin upon ipsq_exit().  See
12593  * the comments above ipsq_current_finish() for details.
12594  */
12595 void
12596 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12597 {
12598 	mblk_t *mp;
12599 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12600 	ipxop_t *ipx = ipsq->ipsq_xop;
12601 
12602 	ASSERT(IAM_WRITER_IPSQ(ipsq));
12603 	mutex_enter(&ill->ill_lock);
12604 
12605 	ASSERT(prim != DL_PRIM_INVAL);
12606 	ASSERT(ill->ill_dlpi_pending == prim);
12607 
12608 	ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12609 	    dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12610 
12611 	if ((mp = ill->ill_dlpi_deferred) == NULL) {
12612 		ill->ill_dlpi_pending = DL_PRIM_INVAL;
12613 		if (ipx->ipx_current_done) {
12614 			mutex_enter(&ipx->ipx_lock);
12615 			ipx->ipx_current_ipif = NULL;
12616 			mutex_exit(&ipx->ipx_lock);
12617 		}
12618 		cv_signal(&ill->ill_cv);
12619 		mutex_exit(&ill->ill_lock);
12620 		return;
12621 	}
12622 
12623 	ill->ill_dlpi_deferred = mp->b_next;
12624 	mp->b_next = NULL;
12625 	mutex_exit(&ill->ill_lock);
12626 
12627 	ill_dlpi_dispatch(ill, mp);
12628 }
12629 
12630 /*
12631  * Queue a (multicast) DLPI control message to be sent to the driver by
12632  * later calling ill_dlpi_send_queued.
12633  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12634  * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12635  * for the same group to race.
12636  * We send DLPI control messages in order using ill_lock.
12637  * For IPMP we should be called on the cast_ill.
12638  */
12639 void
12640 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12641 {
12642 	mblk_t **mpp;
12643 
12644 	ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12645 
12646 	mutex_enter(&ill->ill_lock);
12647 	/* Must queue message. Tail insertion */
12648 	mpp = &ill->ill_dlpi_deferred;
12649 	while (*mpp != NULL)
12650 		mpp = &((*mpp)->b_next);
12651 
12652 	*mpp = mp;
12653 	mutex_exit(&ill->ill_lock);
12654 }
12655 
12656 /*
12657  * Send the messages that were queued. Make sure there is only
12658  * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
12659  * when an ACK or a NAK is received to process the next queued message.
12660  * For IPMP we are called on the upper ill, but when send what is queued
12661  * on the cast_ill.
12662  */
12663 void
12664 ill_dlpi_send_queued(ill_t *ill)
12665 {
12666 	mblk_t	*mp;
12667 	union DL_primitives *dlp;
12668 	t_uscalar_t prim;
12669 	ill_t *release_ill = NULL;
12670 
12671 	if (IS_IPMP(ill)) {
12672 		/* On the upper IPMP ill. */
12673 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12674 		if (release_ill == NULL) {
12675 			/* Avoid ever sending anything down to the ipmpstub */
12676 			return;
12677 		}
12678 		ill = release_ill;
12679 	}
12680 	mutex_enter(&ill->ill_lock);
12681 	while ((mp = ill->ill_dlpi_deferred) != NULL) {
12682 		if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12683 			/* Can't send. Somebody else will send it */
12684 			mutex_exit(&ill->ill_lock);
12685 			goto done;
12686 		}
12687 		ill->ill_dlpi_deferred = mp->b_next;
12688 		mp->b_next = NULL;
12689 		if (!ill->ill_dl_up) {
12690 			/*
12691 			 * Nobody there. All multicast addresses will be
12692 			 * re-joined when we get the DL_BIND_ACK bringing the
12693 			 * interface up.
12694 			 */
12695 			freemsg(mp);
12696 			continue;
12697 		}
12698 		dlp = (union DL_primitives *)mp->b_rptr;
12699 		prim = dlp->dl_primitive;
12700 
12701 		if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12702 		    (prim == DL_UNBIND_REQ)) {
12703 			ill->ill_dlpi_pending = prim;
12704 		}
12705 		mutex_exit(&ill->ill_lock);
12706 
12707 		DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
12708 		    char *, dl_primstr(prim), ill_t *, ill);
12709 		putnext(ill->ill_wq, mp);
12710 		mutex_enter(&ill->ill_lock);
12711 	}
12712 	mutex_exit(&ill->ill_lock);
12713 done:
12714 	if (release_ill != NULL)
12715 		ill_refrele(release_ill);
12716 }
12717 
12718 /*
12719  * Queue an IP (IGMP/MLD) message to be sent by IP from
12720  * ill_mcast_send_queued
12721  * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12722  * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
12723  * group to race.
12724  * We send them in order using ill_lock.
12725  * For IPMP we are called on the upper ill, but we queue on the cast_ill.
12726  */
12727 void
12728 ill_mcast_queue(ill_t *ill, mblk_t *mp)
12729 {
12730 	mblk_t **mpp;
12731 	ill_t *release_ill = NULL;
12732 
12733 	ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
12734 
12735 	if (IS_IPMP(ill)) {
12736 		/* On the upper IPMP ill. */
12737 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12738 		if (release_ill == NULL) {
12739 			/* Discard instead of queuing for the ipmp interface */
12740 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12741 			ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
12742 			    mp, ill);
12743 			freemsg(mp);
12744 			return;
12745 		}
12746 		ill = release_ill;
12747 	}
12748 
12749 	mutex_enter(&ill->ill_lock);
12750 	/* Must queue message. Tail insertion */
12751 	mpp = &ill->ill_mcast_deferred;
12752 	while (*mpp != NULL)
12753 		mpp = &((*mpp)->b_next);
12754 
12755 	*mpp = mp;
12756 	mutex_exit(&ill->ill_lock);
12757 	if (release_ill != NULL)
12758 		ill_refrele(release_ill);
12759 }
12760 
12761 /*
12762  * Send the IP packets that were queued by ill_mcast_queue.
12763  * These are IGMP/MLD packets.
12764  *
12765  * For IPMP we are called on the upper ill, but when send what is queued
12766  * on the cast_ill.
12767  *
12768  * Request loopback of the report if we are acting as a multicast
12769  * router, so that the process-level routing demon can hear it.
12770  * This will run multiple times for the same group if there are members
12771  * on the same group for multiple ipif's on the same ill. The
12772  * igmp_input/mld_input code will suppress this due to the loopback thus we
12773  * always loopback membership report.
12774  *
12775  * We also need to make sure that this does not get load balanced
12776  * by IPMP. We do this by passing an ill to ip_output_simple.
12777  */
12778 void
12779 ill_mcast_send_queued(ill_t *ill)
12780 {
12781 	mblk_t	*mp;
12782 	ip_xmit_attr_t ixas;
12783 	ill_t *release_ill = NULL;
12784 
12785 	if (IS_IPMP(ill)) {
12786 		/* On the upper IPMP ill. */
12787 		release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
12788 		if (release_ill == NULL) {
12789 			/*
12790 			 * We should have no messages on the ipmp interface
12791 			 * but no point in trying to send them.
12792 			 */
12793 			return;
12794 		}
12795 		ill = release_ill;
12796 	}
12797 	bzero(&ixas, sizeof (ixas));
12798 	ixas.ixa_zoneid = ALL_ZONES;
12799 	ixas.ixa_cred = kcred;
12800 	ixas.ixa_cpid = NOPID;
12801 	ixas.ixa_tsl = NULL;
12802 	/*
12803 	 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
12804 	 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
12805 	 * That is necessary to handle IGMP/MLD snooping switches.
12806 	 */
12807 	ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
12808 	ixas.ixa_ipst = ill->ill_ipst;
12809 
12810 	mutex_enter(&ill->ill_lock);
12811 	while ((mp = ill->ill_mcast_deferred) != NULL) {
12812 		ill->ill_mcast_deferred = mp->b_next;
12813 		mp->b_next = NULL;
12814 		if (!ill->ill_dl_up) {
12815 			/*
12816 			 * Nobody there. Just drop the ip packets.
12817 			 * IGMP/MLD will resend later, if this is a replumb.
12818 			 */
12819 			freemsg(mp);
12820 			continue;
12821 		}
12822 		mutex_enter(&ill->ill_phyint->phyint_lock);
12823 		if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
12824 			/*
12825 			 * When the ill is getting deactivated, we only want to
12826 			 * send the DLPI messages, so drop IGMP/MLD packets.
12827 			 * DLPI messages are handled by ill_dlpi_send_queued()
12828 			 */
12829 			mutex_exit(&ill->ill_phyint->phyint_lock);
12830 			freemsg(mp);
12831 			continue;
12832 		}
12833 		mutex_exit(&ill->ill_phyint->phyint_lock);
12834 		mutex_exit(&ill->ill_lock);
12835 
12836 		/* Check whether we are sending IPv4 or IPv6. */
12837 		if (ill->ill_isv6) {
12838 			ip6_t  *ip6h = (ip6_t *)mp->b_rptr;
12839 
12840 			ixas.ixa_multicast_ttl = ip6h->ip6_hops;
12841 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
12842 		} else {
12843 			ipha_t *ipha = (ipha_t *)mp->b_rptr;
12844 
12845 			ixas.ixa_multicast_ttl = ipha->ipha_ttl;
12846 			ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
12847 			ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
12848 		}
12849 
12850 		ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
12851 		(void) ip_output_simple(mp, &ixas);
12852 		ixa_cleanup(&ixas);
12853 
12854 		mutex_enter(&ill->ill_lock);
12855 	}
12856 	mutex_exit(&ill->ill_lock);
12857 
12858 done:
12859 	if (release_ill != NULL)
12860 		ill_refrele(release_ill);
12861 }
12862 
12863 /*
12864  * Take down a specific interface, but don't lose any information about it.
12865  * (Always called as writer.)
12866  * This function goes through the down sequence even if the interface is
12867  * already down. There are 2 reasons.
12868  * a. Currently we permit interface routes that depend on down interfaces
12869  *    to be added. This behaviour itself is questionable. However it appears
12870  *    that both Solaris and 4.3 BSD have exhibited this behaviour for a long
12871  *    time. We go thru the cleanup in order to remove these routes.
12872  * b. The bringup of the interface could fail in ill_dl_up i.e. we get
12873  *    DL_ERROR_ACK in response to the DL_BIND request. The interface is
12874  *    down, but we need to cleanup i.e. do ill_dl_down and
12875  *    ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
12876  *
12877  * IP-MT notes:
12878  *
12879  * Model of reference to interfaces.
12880  *
12881  * The following members in ipif_t track references to the ipif.
12882  *	int     ipif_refcnt;    Active reference count
12883  *
12884  * The following members in ill_t track references to the ill.
12885  *	int             ill_refcnt;     active refcnt
12886  *	uint_t          ill_ire_cnt;	Number of ires referencing ill
12887  *	uint_t          ill_ncec_cnt;	Number of ncecs referencing ill
12888  *	uint_t          ill_nce_cnt;	Number of nces referencing ill
12889  *	uint_t          ill_ilm_cnt;	Number of ilms referencing ill
12890  *
12891  * Reference to an ipif or ill can be obtained in any of the following ways.
12892  *
12893  * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
12894  * Pointers to ipif / ill from other data structures viz ire and conn.
12895  * Implicit reference to the ipif / ill by holding a reference to the ire.
12896  *
12897  * The ipif/ill lookup functions return a reference held ipif / ill.
12898  * ipif_refcnt and ill_refcnt track the reference counts respectively.
12899  * This is a purely dynamic reference count associated with threads holding
12900  * references to the ipif / ill. Pointers from other structures do not
12901  * count towards this reference count.
12902  *
12903  * ill_ire_cnt is the number of ire's associated with the
12904  * ill. This is incremented whenever a new ire is created referencing the
12905  * ill. This is done atomically inside ire_add_v[46] where the ire is
12906  * actually added to the ire hash table. The count is decremented in
12907  * ire_inactive where the ire is destroyed.
12908  *
12909  * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
12910  * This is incremented atomically in
12911  * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
12912  * table. Similarly it is decremented in ncec_inactive() where the ncec
12913  * is destroyed.
12914  *
12915  * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
12916  * incremented atomically in nce_add() where the nce is actually added to the
12917  * ill_nce. Similarly it is decremented in nce_inactive() where the nce
12918  * is destroyed.
12919  *
12920  * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
12921  * ilm_add() and decremented before the ilm is freed in ilm_delete().
12922  *
12923  * Flow of ioctls involving interface down/up
12924  *
12925  * The following is the sequence of an attempt to set some critical flags on an
12926  * up interface.
12927  * ip_sioctl_flags
12928  * ipif_down
12929  * wait for ipif to be quiescent
12930  * ipif_down_tail
12931  * ip_sioctl_flags_tail
12932  *
12933  * All set ioctls that involve down/up sequence would have a skeleton similar
12934  * to the above. All the *tail functions are called after the refcounts have
12935  * dropped to the appropriate values.
12936  *
12937  * SIOC ioctls during the IPIF_CHANGING interval.
12938  *
12939  * Threads handling SIOC set ioctls serialize on the squeue, but this
12940  * is not done for SIOC get ioctls. Since a set ioctl can cause several
12941  * steps of internal changes to the state, some of which are visible in
12942  * ipif_flags (such as IFF_UP being cleared and later set), and we want
12943  * the set ioctl to be atomic related to the get ioctls, the SIOC get code
12944  * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
12945  * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
12946  * the current exclusive operation completes. The IPIF_CHANGING check
12947  * and enqueue is atomic using the ill_lock and ipsq_lock. The
12948  * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
12949  * change while the ill_lock is held. Before dropping the ill_lock we acquire
12950  * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
12951  * until we release the ipsq_lock, even though the ill/ipif state flags
12952  * can change after we drop the ill_lock.
12953  */
12954 int
12955 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
12956 {
12957 	ill_t		*ill = ipif->ipif_ill;
12958 	conn_t		*connp;
12959 	boolean_t	success;
12960 	boolean_t	ipif_was_up = B_FALSE;
12961 	ip_stack_t	*ipst = ill->ill_ipst;
12962 
12963 	ASSERT(IAM_WRITER_IPIF(ipif));
12964 
12965 	ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12966 
12967 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
12968 	    ill_t *, ill, ipif_t *, ipif);
12969 
12970 	if (ipif->ipif_flags & IPIF_UP) {
12971 		mutex_enter(&ill->ill_lock);
12972 		ipif->ipif_flags &= ~IPIF_UP;
12973 		ASSERT(ill->ill_ipif_up_count > 0);
12974 		--ill->ill_ipif_up_count;
12975 		mutex_exit(&ill->ill_lock);
12976 		ipif_was_up = B_TRUE;
12977 		/* Update status in SCTP's list */
12978 		sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
12979 		ill_nic_event_dispatch(ipif->ipif_ill,
12980 		    MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
12981 	}
12982 
12983 	/*
12984 	 * Blow away memberships we established in ipif_multicast_up().
12985 	 */
12986 	ipif_multicast_down(ipif);
12987 
12988 	/*
12989 	 * Remove from the mapping for __sin6_src_id. We insert only
12990 	 * when the address is not INADDR_ANY. As IPv4 addresses are
12991 	 * stored as mapped addresses, we need to check for mapped
12992 	 * INADDR_ANY also.
12993 	 */
12994 	if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
12995 	    !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
12996 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
12997 		int err;
12998 
12999 		err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13000 		    ipif->ipif_zoneid, ipst);
13001 		if (err != 0) {
13002 			ip0dbg(("ipif_down: srcid_remove %d\n", err));
13003 		}
13004 	}
13005 
13006 	if (ipif_was_up) {
13007 		/* only delete if we'd added ire's before */
13008 		if (ipif->ipif_isv6)
13009 			ipif_delete_ires_v6(ipif);
13010 		else
13011 			ipif_delete_ires_v4(ipif);
13012 	}
13013 
13014 	if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13015 		/*
13016 		 * Since the interface is now down, it may have just become
13017 		 * inactive.  Note that this needs to be done even for a
13018 		 * lll_logical_down(), or ARP entries will not get correctly
13019 		 * restored when the interface comes back up.
13020 		 */
13021 		if (IS_UNDER_IPMP(ill))
13022 			ipmp_ill_refresh_active(ill);
13023 	}
13024 
13025 	/*
13026 	 * neighbor-discovery or arp entries for this interface. The ipif
13027 	 * has to be quiesced, so we walk all the nce's and delete those
13028 	 * that point at the ipif->ipif_ill. At the same time, we also
13029 	 * update IPMP so that ipifs for data addresses are unbound. We dont
13030 	 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13031 	 * that for ipif_down_tail()
13032 	 */
13033 	ipif_nce_down(ipif);
13034 
13035 	/*
13036 	 * If this is the last ipif on the ill, we also need to remove
13037 	 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13038 	 * never succeed.
13039 	 */
13040 	if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13041 		ire_walk_ill(0, 0, ill_downi, ill, ill);
13042 
13043 	/*
13044 	 * Walk all CONNs that can have a reference on an ire for this
13045 	 * ipif (we actually walk all that now have stale references).
13046 	 */
13047 	ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13048 
13049 	/*
13050 	 * If mp is NULL the caller will wait for the appropriate refcnt.
13051 	 * Eg. ip_sioctl_removeif -> ipif_free  -> ipif_down
13052 	 * and ill_delete -> ipif_free -> ipif_down
13053 	 */
13054 	if (mp == NULL) {
13055 		ASSERT(q == NULL);
13056 		return (0);
13057 	}
13058 
13059 	if (CONN_Q(q)) {
13060 		connp = Q_TO_CONN(q);
13061 		mutex_enter(&connp->conn_lock);
13062 	} else {
13063 		connp = NULL;
13064 	}
13065 	mutex_enter(&ill->ill_lock);
13066 	/*
13067 	 * Are there any ire's pointing to this ipif that are still active ?
13068 	 * If this is the last ipif going down, are there any ire's pointing
13069 	 * to this ill that are still active ?
13070 	 */
13071 	if (ipif_is_quiescent(ipif)) {
13072 		mutex_exit(&ill->ill_lock);
13073 		if (connp != NULL)
13074 			mutex_exit(&connp->conn_lock);
13075 		return (0);
13076 	}
13077 
13078 	ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13079 	    ill->ill_name, (void *)ill));
13080 	/*
13081 	 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13082 	 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13083 	 * which in turn is called by the last refrele on the ipif/ill/ire.
13084 	 */
13085 	success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13086 	if (!success) {
13087 		/* The conn is closing. So just return */
13088 		ASSERT(connp != NULL);
13089 		mutex_exit(&ill->ill_lock);
13090 		mutex_exit(&connp->conn_lock);
13091 		return (EINTR);
13092 	}
13093 
13094 	mutex_exit(&ill->ill_lock);
13095 	if (connp != NULL)
13096 		mutex_exit(&connp->conn_lock);
13097 	return (EINPROGRESS);
13098 }
13099 
13100 int
13101 ipif_down_tail(ipif_t *ipif)
13102 {
13103 	ill_t	*ill = ipif->ipif_ill;
13104 	int	err = 0;
13105 
13106 	DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13107 	    ill_t *, ill, ipif_t *, ipif);
13108 
13109 	/*
13110 	 * Skip any loopback interface (null wq).
13111 	 * If this is the last logical interface on the ill
13112 	 * have ill_dl_down tell the driver we are gone (unbind)
13113 	 * Note that lun 0 can ipif_down even though
13114 	 * there are other logical units that are up.
13115 	 * This occurs e.g. when we change a "significant" IFF_ flag.
13116 	 */
13117 	if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13118 	    ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13119 	    ill->ill_dl_up) {
13120 		ill_dl_down(ill);
13121 	}
13122 	if (!ipif->ipif_isv6)
13123 		err = ipif_arp_down(ipif);
13124 
13125 	ill->ill_logical_down = 0;
13126 
13127 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13128 	ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13129 	return (err);
13130 }
13131 
13132 /*
13133  * Bring interface logically down without bringing the physical interface
13134  * down e.g. when the netmask is changed. This avoids long lasting link
13135  * negotiations between an ethernet interface and a certain switches.
13136  */
13137 static int
13138 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13139 {
13140 	DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13141 	    ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13142 
13143 	/*
13144 	 * The ill_logical_down flag is a transient flag. It is set here
13145 	 * and is cleared once the down has completed in ipif_down_tail.
13146 	 * This flag does not indicate whether the ill stream is in the
13147 	 * DL_BOUND state with the driver. Instead this flag is used by
13148 	 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13149 	 * the driver. The state of the ill stream i.e. whether it is
13150 	 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13151 	 */
13152 	ipif->ipif_ill->ill_logical_down = 1;
13153 	return (ipif_down(ipif, q, mp));
13154 }
13155 
13156 /*
13157  * Initiate deallocate of an IPIF. Always called as writer. Called by
13158  * ill_delete or ip_sioctl_removeif.
13159  */
13160 static void
13161 ipif_free(ipif_t *ipif)
13162 {
13163 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13164 
13165 	ASSERT(IAM_WRITER_IPIF(ipif));
13166 
13167 	if (ipif->ipif_recovery_id != 0)
13168 		(void) untimeout(ipif->ipif_recovery_id);
13169 	ipif->ipif_recovery_id = 0;
13170 
13171 	/*
13172 	 * Take down the interface. We can be called either from ill_delete
13173 	 * or from ip_sioctl_removeif.
13174 	 */
13175 	(void) ipif_down(ipif, NULL, NULL);
13176 
13177 	/*
13178 	 * Now that the interface is down, there's no chance it can still
13179 	 * become a duplicate.  Cancel any timer that may have been set while
13180 	 * tearing down.
13181 	 */
13182 	if (ipif->ipif_recovery_id != 0)
13183 		(void) untimeout(ipif->ipif_recovery_id);
13184 	ipif->ipif_recovery_id = 0;
13185 
13186 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13187 	/* Remove pointers to this ill in the multicast routing tables */
13188 	reset_mrt_vif_ipif(ipif);
13189 	/* If necessary, clear the cached source ipif rotor. */
13190 	if (ipif->ipif_ill->ill_src_ipif == ipif)
13191 		ipif->ipif_ill->ill_src_ipif = NULL;
13192 	rw_exit(&ipst->ips_ill_g_lock);
13193 }
13194 
13195 static void
13196 ipif_free_tail(ipif_t *ipif)
13197 {
13198 	ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13199 
13200 	/*
13201 	 * Need to hold both ill_g_lock and ill_lock while
13202 	 * inserting or removing an ipif from the linked list
13203 	 * of ipifs hanging off the ill.
13204 	 */
13205 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13206 
13207 #ifdef DEBUG
13208 	ipif_trace_cleanup(ipif);
13209 #endif
13210 
13211 	/* Ask SCTP to take it out of it list */
13212 	sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13213 	ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13214 
13215 	/* Get it out of the ILL interface list. */
13216 	ipif_remove(ipif);
13217 	rw_exit(&ipst->ips_ill_g_lock);
13218 
13219 	ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13220 	ASSERT(ipif->ipif_recovery_id == 0);
13221 	ASSERT(ipif->ipif_ire_local == NULL);
13222 	ASSERT(ipif->ipif_ire_if == NULL);
13223 
13224 	/* Free the memory. */
13225 	mi_free(ipif);
13226 }
13227 
13228 /*
13229  * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13230  * is zero.
13231  */
13232 void
13233 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13234 {
13235 	char	lbuf[LIFNAMSIZ];
13236 	char	*name;
13237 	size_t	name_len;
13238 
13239 	buf[0] = '\0';
13240 	name = ipif->ipif_ill->ill_name;
13241 	name_len = ipif->ipif_ill->ill_name_length;
13242 	if (ipif->ipif_id != 0) {
13243 		(void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13244 		    ipif->ipif_id);
13245 		name = lbuf;
13246 		name_len = mi_strlen(name) + 1;
13247 	}
13248 	len -= 1;
13249 	buf[len] = '\0';
13250 	len = MIN(len, name_len);
13251 	bcopy(name, buf, len);
13252 }
13253 
13254 /*
13255  * Sets `buf' to an ill name.
13256  */
13257 void
13258 ill_get_name(const ill_t *ill, char *buf, int len)
13259 {
13260 	char	*name;
13261 	size_t	name_len;
13262 
13263 	name = ill->ill_name;
13264 	name_len = ill->ill_name_length;
13265 	len -= 1;
13266 	buf[len] = '\0';
13267 	len = MIN(len, name_len);
13268 	bcopy(name, buf, len);
13269 }
13270 
13271 /*
13272  * Find an IPIF based on the name passed in.  Names can be of the form <phys>
13273  * (e.g., le0) or <phys>:<#> (e.g., le0:1).  When there is no colon, the
13274  * implied unit id is zero. <phys> must correspond to the name of an ILL.
13275  * (May be called as writer.)
13276  */
13277 static ipif_t *
13278 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13279     boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13280 {
13281 	char	*cp;
13282 	char	*endp;
13283 	long	id;
13284 	ill_t	*ill;
13285 	ipif_t	*ipif;
13286 	uint_t	ire_type;
13287 	boolean_t did_alloc = B_FALSE;
13288 
13289 	/*
13290 	 * If the caller wants to us to create the ipif, make sure we have a
13291 	 * valid zoneid
13292 	 */
13293 	ASSERT(!do_alloc || zoneid != ALL_ZONES);
13294 
13295 	if (namelen == 0) {
13296 		return (NULL);
13297 	}
13298 
13299 	*exists = B_FALSE;
13300 	/* Look for a colon in the name. */
13301 	endp = &name[namelen];
13302 	for (cp = endp; --cp > name; ) {
13303 		if (*cp == IPIF_SEPARATOR_CHAR)
13304 			break;
13305 	}
13306 
13307 	if (*cp == IPIF_SEPARATOR_CHAR) {
13308 		/*
13309 		 * Reject any non-decimal aliases for logical
13310 		 * interfaces. Aliases with leading zeroes
13311 		 * are also rejected as they introduce ambiguity
13312 		 * in the naming of the interfaces.
13313 		 * In order to confirm with existing semantics,
13314 		 * and to not break any programs/script relying
13315 		 * on that behaviour, if<0>:0 is considered to be
13316 		 * a valid interface.
13317 		 *
13318 		 * If alias has two or more digits and the first
13319 		 * is zero, fail.
13320 		 */
13321 		if (&cp[2] < endp && cp[1] == '0') {
13322 			return (NULL);
13323 		}
13324 	}
13325 
13326 	if (cp <= name) {
13327 		cp = endp;
13328 	} else {
13329 		*cp = '\0';
13330 	}
13331 
13332 	/*
13333 	 * Look up the ILL, based on the portion of the name
13334 	 * before the slash. ill_lookup_on_name returns a held ill.
13335 	 * Temporary to check whether ill exists already. If so
13336 	 * ill_lookup_on_name will clear it.
13337 	 */
13338 	ill = ill_lookup_on_name(name, do_alloc, isv6,
13339 	    &did_alloc, ipst);
13340 	if (cp != endp)
13341 		*cp = IPIF_SEPARATOR_CHAR;
13342 	if (ill == NULL)
13343 		return (NULL);
13344 
13345 	/* Establish the unit number in the name. */
13346 	id = 0;
13347 	if (cp < endp && *endp == '\0') {
13348 		/* If there was a colon, the unit number follows. */
13349 		cp++;
13350 		if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13351 			ill_refrele(ill);
13352 			return (NULL);
13353 		}
13354 	}
13355 
13356 	mutex_enter(&ill->ill_lock);
13357 	/* Now see if there is an IPIF with this unit number. */
13358 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13359 		if (ipif->ipif_id == id) {
13360 			if (zoneid != ALL_ZONES &&
13361 			    zoneid != ipif->ipif_zoneid &&
13362 			    ipif->ipif_zoneid != ALL_ZONES) {
13363 				mutex_exit(&ill->ill_lock);
13364 				ill_refrele(ill);
13365 				return (NULL);
13366 			}
13367 			if (IPIF_CAN_LOOKUP(ipif)) {
13368 				ipif_refhold_locked(ipif);
13369 				mutex_exit(&ill->ill_lock);
13370 				if (!did_alloc)
13371 					*exists = B_TRUE;
13372 				/*
13373 				 * Drop locks before calling ill_refrele
13374 				 * since it can potentially call into
13375 				 * ipif_ill_refrele_tail which can end up
13376 				 * in trying to acquire any lock.
13377 				 */
13378 				ill_refrele(ill);
13379 				return (ipif);
13380 			}
13381 		}
13382 	}
13383 
13384 	if (!do_alloc) {
13385 		mutex_exit(&ill->ill_lock);
13386 		ill_refrele(ill);
13387 		return (NULL);
13388 	}
13389 
13390 	/*
13391 	 * If none found, atomically allocate and return a new one.
13392 	 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13393 	 * to support "receive only" use of lo0:1 etc. as is still done
13394 	 * below as an initial guess.
13395 	 * However, this is now likely to be overriden later in ipif_up_done()
13396 	 * when we know for sure what address has been configured on the
13397 	 * interface, since we might have more than one loopback interface
13398 	 * with a loopback address, e.g. in the case of zones, and all the
13399 	 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13400 	 */
13401 	if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13402 		ire_type = IRE_LOOPBACK;
13403 	else
13404 		ire_type = IRE_LOCAL;
13405 	ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13406 	if (ipif != NULL)
13407 		ipif_refhold_locked(ipif);
13408 	mutex_exit(&ill->ill_lock);
13409 	ill_refrele(ill);
13410 	return (ipif);
13411 }
13412 
13413 /*
13414  * This routine is called whenever a new address comes up on an ipif.  If
13415  * we are configured to respond to address mask requests, then we are supposed
13416  * to broadcast an address mask reply at this time.  This routine is also
13417  * called if we are already up, but a netmask change is made.  This is legal
13418  * but might not make the system manager very popular.	(May be called
13419  * as writer.)
13420  */
13421 void
13422 ipif_mask_reply(ipif_t *ipif)
13423 {
13424 	icmph_t	*icmph;
13425 	ipha_t	*ipha;
13426 	mblk_t	*mp;
13427 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
13428 	ip_xmit_attr_t ixas;
13429 
13430 #define	REPLY_LEN	(sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13431 
13432 	if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13433 		return;
13434 
13435 	/* ICMP mask reply is IPv4 only */
13436 	ASSERT(!ipif->ipif_isv6);
13437 	/* ICMP mask reply is not for a loopback interface */
13438 	ASSERT(ipif->ipif_ill->ill_wq != NULL);
13439 
13440 	if (ipif->ipif_lcl_addr == INADDR_ANY)
13441 		return;
13442 
13443 	mp = allocb(REPLY_LEN, BPRI_HI);
13444 	if (mp == NULL)
13445 		return;
13446 	mp->b_wptr = mp->b_rptr + REPLY_LEN;
13447 
13448 	ipha = (ipha_t *)mp->b_rptr;
13449 	bzero(ipha, REPLY_LEN);
13450 	*ipha = icmp_ipha;
13451 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13452 	ipha->ipha_src = ipif->ipif_lcl_addr;
13453 	ipha->ipha_dst = ipif->ipif_brd_addr;
13454 	ipha->ipha_length = htons(REPLY_LEN);
13455 	ipha->ipha_ident = 0;
13456 
13457 	icmph = (icmph_t *)&ipha[1];
13458 	icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13459 	bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13460 	icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13461 
13462 	bzero(&ixas, sizeof (ixas));
13463 	ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13464 	ixas.ixa_flags |= IXAF_SET_SOURCE;
13465 	ixas.ixa_zoneid = ALL_ZONES;
13466 	ixas.ixa_ifindex = 0;
13467 	ixas.ixa_ipst = ipst;
13468 	ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13469 	(void) ip_output_simple(mp, &ixas);
13470 	ixa_cleanup(&ixas);
13471 #undef	REPLY_LEN
13472 }
13473 
13474 /*
13475  * Join the ipif specific multicast groups.
13476  * Must be called after a mapping has been set up in the resolver.  (Always
13477  * called as writer.)
13478  */
13479 void
13480 ipif_multicast_up(ipif_t *ipif)
13481 {
13482 	int err;
13483 	ill_t *ill;
13484 	ilm_t *ilm;
13485 
13486 	ASSERT(IAM_WRITER_IPIF(ipif));
13487 
13488 	ill = ipif->ipif_ill;
13489 
13490 	ip1dbg(("ipif_multicast_up\n"));
13491 	if (!(ill->ill_flags & ILLF_MULTICAST) ||
13492 	    ipif->ipif_allhosts_ilm != NULL)
13493 		return;
13494 
13495 	if (ipif->ipif_isv6) {
13496 		in6_addr_t v6allmc = ipv6_all_hosts_mcast;
13497 		in6_addr_t v6solmc = ipv6_solicited_node_mcast;
13498 
13499 		v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
13500 
13501 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
13502 			return;
13503 
13504 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13505 
13506 		/*
13507 		 * Join the all hosts multicast address.  We skip this for
13508 		 * underlying IPMP interfaces since they should be invisible.
13509 		 */
13510 		if (!IS_UNDER_IPMP(ill)) {
13511 			ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
13512 			    &err);
13513 			if (ilm == NULL) {
13514 				ASSERT(err != 0);
13515 				ip0dbg(("ipif_multicast_up: "
13516 				    "all_hosts_mcast failed %d\n", err));
13517 				return;
13518 			}
13519 			ipif->ipif_allhosts_ilm = ilm;
13520 		}
13521 
13522 		/*
13523 		 * Enable multicast for the solicited node multicast address.
13524 		 * If IPMP we need to put the membership on the upper ill.
13525 		 */
13526 		if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
13527 			ill_t *mcast_ill = NULL;
13528 			boolean_t need_refrele;
13529 
13530 			if (IS_UNDER_IPMP(ill) &&
13531 			    (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
13532 				need_refrele = B_TRUE;
13533 			} else {
13534 				mcast_ill = ill;
13535 				need_refrele = B_FALSE;
13536 			}
13537 
13538 			ilm = ip_addmulti(&v6solmc, mcast_ill,
13539 			    ipif->ipif_zoneid, &err);
13540 			if (need_refrele)
13541 				ill_refrele(mcast_ill);
13542 
13543 			if (ilm == NULL) {
13544 				ASSERT(err != 0);
13545 				ip0dbg(("ipif_multicast_up: solicited MC"
13546 				    " failed %d\n", err));
13547 				if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
13548 					ipif->ipif_allhosts_ilm = NULL;
13549 					(void) ip_delmulti(ilm);
13550 				}
13551 				return;
13552 			}
13553 			ipif->ipif_solmulti_ilm = ilm;
13554 		}
13555 	} else {
13556 		in6_addr_t v6group;
13557 
13558 		if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
13559 			return;
13560 
13561 		/* Join the all hosts multicast address */
13562 		ip1dbg(("ipif_multicast_up - addmulti\n"));
13563 		IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
13564 
13565 		ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
13566 		if (ilm == NULL) {
13567 			ASSERT(err != 0);
13568 			ip0dbg(("ipif_multicast_up: failed %d\n", err));
13569 			return;
13570 		}
13571 		ipif->ipif_allhosts_ilm = ilm;
13572 	}
13573 }
13574 
13575 /*
13576  * Blow away any multicast groups that we joined in ipif_multicast_up().
13577  * (ilms from explicit memberships are handled in conn_update_ill.)
13578  */
13579 void
13580 ipif_multicast_down(ipif_t *ipif)
13581 {
13582 	ASSERT(IAM_WRITER_IPIF(ipif));
13583 
13584 	ip1dbg(("ipif_multicast_down\n"));
13585 
13586 	if (ipif->ipif_allhosts_ilm != NULL) {
13587 		(void) ip_delmulti(ipif->ipif_allhosts_ilm);
13588 		ipif->ipif_allhosts_ilm = NULL;
13589 	}
13590 	if (ipif->ipif_solmulti_ilm != NULL) {
13591 		(void) ip_delmulti(ipif->ipif_solmulti_ilm);
13592 		ipif->ipif_solmulti_ilm = NULL;
13593 	}
13594 }
13595 
13596 /*
13597  * Used when an interface comes up to recreate any extra routes on this
13598  * interface.
13599  */
13600 int
13601 ill_recover_saved_ire(ill_t *ill)
13602 {
13603 	mblk_t		*mp;
13604 	ip_stack_t	*ipst = ill->ill_ipst;
13605 
13606 	ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
13607 
13608 	mutex_enter(&ill->ill_saved_ire_lock);
13609 	for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
13610 		ire_t		*ire, *nire;
13611 		ifrt_t		*ifrt;
13612 
13613 		ifrt = (ifrt_t *)mp->b_rptr;
13614 		/*
13615 		 * Create a copy of the IRE with the saved address and netmask.
13616 		 */
13617 		if (ill->ill_isv6) {
13618 			ire = ire_create_v6(
13619 			    &ifrt->ifrt_v6addr,
13620 			    &ifrt->ifrt_v6mask,
13621 			    &ifrt->ifrt_v6gateway_addr,
13622 			    ifrt->ifrt_type,
13623 			    ill,
13624 			    ifrt->ifrt_zoneid,
13625 			    ifrt->ifrt_flags,
13626 			    NULL,
13627 			    ipst);
13628 		} else {
13629 			ire = ire_create(
13630 			    (uint8_t *)&ifrt->ifrt_addr,
13631 			    (uint8_t *)&ifrt->ifrt_mask,
13632 			    (uint8_t *)&ifrt->ifrt_gateway_addr,
13633 			    ifrt->ifrt_type,
13634 			    ill,
13635 			    ifrt->ifrt_zoneid,
13636 			    ifrt->ifrt_flags,
13637 			    NULL,
13638 			    ipst);
13639 		}
13640 		if (ire == NULL) {
13641 			mutex_exit(&ill->ill_saved_ire_lock);
13642 			return (ENOMEM);
13643 		}
13644 
13645 		if (ifrt->ifrt_flags & RTF_SETSRC) {
13646 			if (ill->ill_isv6) {
13647 				ire->ire_setsrc_addr_v6 =
13648 				    ifrt->ifrt_v6setsrc_addr;
13649 			} else {
13650 				ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
13651 			}
13652 		}
13653 
13654 		/*
13655 		 * Some software (for example, GateD and Sun Cluster) attempts
13656 		 * to create (what amount to) IRE_PREFIX routes with the
13657 		 * loopback address as the gateway.  This is primarily done to
13658 		 * set up prefixes with the RTF_REJECT flag set (for example,
13659 		 * when generating aggregate routes.)
13660 		 *
13661 		 * If the IRE type (as defined by ill->ill_net_type) is
13662 		 * IRE_LOOPBACK, then we map the request into a
13663 		 * IRE_IF_NORESOLVER.
13664 		 */
13665 		if (ill->ill_net_type == IRE_LOOPBACK)
13666 			ire->ire_type = IRE_IF_NORESOLVER;
13667 
13668 		/*
13669 		 * ire held by ire_add, will be refreled' towards the
13670 		 * the end of ipif_up_done
13671 		 */
13672 		nire = ire_add(ire);
13673 		/*
13674 		 * Check if it was a duplicate entry. This handles
13675 		 * the case of two racing route adds for the same route
13676 		 */
13677 		if (nire == NULL) {
13678 			ip1dbg(("ill_recover_saved_ire: FAILED\n"));
13679 		} else if (nire != ire) {
13680 			ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
13681 			    (void *)nire));
13682 			ire_delete(nire);
13683 		} else {
13684 			ip1dbg(("ill_recover_saved_ire: added ire %p\n",
13685 			    (void *)nire));
13686 		}
13687 		if (nire != NULL)
13688 			ire_refrele(nire);
13689 	}
13690 	mutex_exit(&ill->ill_saved_ire_lock);
13691 	return (0);
13692 }
13693 
13694 /*
13695  * Used to set the netmask and broadcast address to default values when the
13696  * interface is brought up.  (Always called as writer.)
13697  */
13698 static void
13699 ipif_set_default(ipif_t *ipif)
13700 {
13701 	ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
13702 
13703 	if (!ipif->ipif_isv6) {
13704 		/*
13705 		 * Interface holds an IPv4 address. Default
13706 		 * mask is the natural netmask.
13707 		 */
13708 		if (!ipif->ipif_net_mask) {
13709 			ipaddr_t	v4mask;
13710 
13711 			v4mask = ip_net_mask(ipif->ipif_lcl_addr);
13712 			V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
13713 		}
13714 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13715 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13716 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13717 		} else {
13718 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13719 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13720 		}
13721 		/*
13722 		 * NOTE: SunOS 4.X does this even if the broadcast address
13723 		 * has been already set thus we do the same here.
13724 		 */
13725 		if (ipif->ipif_flags & IPIF_BROADCAST) {
13726 			ipaddr_t	v4addr;
13727 
13728 			v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
13729 			IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
13730 		}
13731 	} else {
13732 		/*
13733 		 * Interface holds an IPv6-only address.  Default
13734 		 * mask is all-ones.
13735 		 */
13736 		if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
13737 			ipif->ipif_v6net_mask = ipv6_all_ones;
13738 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
13739 			/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
13740 			ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
13741 		} else {
13742 			V6_MASK_COPY(ipif->ipif_v6lcl_addr,
13743 			    ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
13744 		}
13745 	}
13746 }
13747 
13748 /*
13749  * Return 0 if this address can be used as local address without causing
13750  * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
13751  * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
13752  * Note that the same IPv6 link-local address is allowed as long as the ills
13753  * are not on the same link.
13754  */
13755 int
13756 ip_addr_availability_check(ipif_t *new_ipif)
13757 {
13758 	in6_addr_t our_v6addr;
13759 	ill_t *ill;
13760 	ipif_t *ipif;
13761 	ill_walk_context_t ctx;
13762 	ip_stack_t	*ipst = new_ipif->ipif_ill->ill_ipst;
13763 
13764 	ASSERT(IAM_WRITER_IPIF(new_ipif));
13765 	ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
13766 	ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
13767 
13768 	new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
13769 	if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
13770 	    IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
13771 		return (0);
13772 
13773 	our_v6addr = new_ipif->ipif_v6lcl_addr;
13774 
13775 	if (new_ipif->ipif_isv6)
13776 		ill = ILL_START_WALK_V6(&ctx, ipst);
13777 	else
13778 		ill = ILL_START_WALK_V4(&ctx, ipst);
13779 
13780 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
13781 		for (ipif = ill->ill_ipif; ipif != NULL;
13782 		    ipif = ipif->ipif_next) {
13783 			if ((ipif == new_ipif) ||
13784 			    !(ipif->ipif_flags & IPIF_UP) ||
13785 			    (ipif->ipif_flags & IPIF_UNNUMBERED) ||
13786 			    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
13787 			    &our_v6addr))
13788 				continue;
13789 
13790 			if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
13791 				new_ipif->ipif_flags |= IPIF_UNNUMBERED;
13792 			else if (ipif->ipif_flags & IPIF_POINTOPOINT)
13793 				ipif->ipif_flags |= IPIF_UNNUMBERED;
13794 			else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
13795 			    IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
13796 			    !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
13797 				continue;
13798 			else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
13799 			    ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
13800 				continue;
13801 			else if (new_ipif->ipif_ill == ill)
13802 				return (EADDRINUSE);
13803 			else
13804 				return (EADDRNOTAVAIL);
13805 		}
13806 	}
13807 
13808 	return (0);
13809 }
13810 
13811 /*
13812  * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
13813  * IREs for the ipif.
13814  * When the routine returns EINPROGRESS then mp has been consumed and
13815  * the ioctl will be acked from ip_rput_dlpi.
13816  */
13817 int
13818 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
13819 {
13820 	ill_t		*ill = ipif->ipif_ill;
13821 	boolean_t 	isv6 = ipif->ipif_isv6;
13822 	int		err = 0;
13823 	boolean_t	success;
13824 	uint_t		ipif_orig_id;
13825 	ip_stack_t	*ipst = ill->ill_ipst;
13826 
13827 	ASSERT(IAM_WRITER_IPIF(ipif));
13828 
13829 	ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13830 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
13831 	    ill_t *, ill, ipif_t *, ipif);
13832 
13833 	/* Shouldn't get here if it is already up. */
13834 	if (ipif->ipif_flags & IPIF_UP)
13835 		return (EALREADY);
13836 
13837 	/*
13838 	 * If this is a request to bring up a data address on an interface
13839 	 * under IPMP, then move the address to its IPMP meta-interface and
13840 	 * try to bring it up.  One complication is that the zeroth ipif for
13841 	 * an ill is special, in that every ill always has one, and that code
13842 	 * throughout IP deferences ill->ill_ipif without holding any locks.
13843 	 */
13844 	if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
13845 	    (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
13846 		ipif_t	*stubipif = NULL, *moveipif = NULL;
13847 		ill_t	*ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
13848 
13849 		/*
13850 		 * The ipif being brought up should be quiesced.  If it's not,
13851 		 * something has gone amiss and we need to bail out.  (If it's
13852 		 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
13853 		 */
13854 		mutex_enter(&ill->ill_lock);
13855 		if (!ipif_is_quiescent(ipif)) {
13856 			mutex_exit(&ill->ill_lock);
13857 			return (EINVAL);
13858 		}
13859 		mutex_exit(&ill->ill_lock);
13860 
13861 		/*
13862 		 * If we're going to need to allocate ipifs, do it prior
13863 		 * to starting the move (and grabbing locks).
13864 		 */
13865 		if (ipif->ipif_id == 0) {
13866 			if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13867 			    B_FALSE, &err)) == NULL) {
13868 				return (err);
13869 			}
13870 			if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
13871 			    B_FALSE, &err)) == NULL) {
13872 				mi_free(moveipif);
13873 				return (err);
13874 			}
13875 		}
13876 
13877 		/*
13878 		 * Grab or transfer the ipif to move.  During the move, keep
13879 		 * ill_g_lock held to prevent any ill walker threads from
13880 		 * seeing things in an inconsistent state.
13881 		 */
13882 		rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13883 		if (ipif->ipif_id != 0) {
13884 			ipif_remove(ipif);
13885 		} else {
13886 			ipif_transfer(ipif, moveipif, stubipif);
13887 			ipif = moveipif;
13888 		}
13889 
13890 		/*
13891 		 * Place the ipif on the IPMP ill.  If the zeroth ipif on
13892 		 * the IPMP ill is a stub (0.0.0.0 down address) then we
13893 		 * replace that one.  Otherwise, pick the next available slot.
13894 		 */
13895 		ipif->ipif_ill = ipmp_ill;
13896 		ipif_orig_id = ipif->ipif_id;
13897 
13898 		if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
13899 			ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
13900 			ipif = ipmp_ill->ill_ipif;
13901 		} else {
13902 			ipif->ipif_id = -1;
13903 			if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
13904 				/*
13905 				 * No more available ipif_id's -- put it back
13906 				 * on the original ill and fail the operation.
13907 				 * Since we're writer on the ill, we can be
13908 				 * sure our old slot is still available.
13909 				 */
13910 				ipif->ipif_id = ipif_orig_id;
13911 				ipif->ipif_ill = ill;
13912 				if (ipif_orig_id == 0) {
13913 					ipif_transfer(ipif, ill->ill_ipif,
13914 					    NULL);
13915 				} else {
13916 					VERIFY(ipif_insert(ipif, B_FALSE) == 0);
13917 				}
13918 				rw_exit(&ipst->ips_ill_g_lock);
13919 				return (err);
13920 			}
13921 		}
13922 		rw_exit(&ipst->ips_ill_g_lock);
13923 
13924 		/*
13925 		 * Tell SCTP that the ipif has moved.  Note that even if we
13926 		 * had to allocate a new ipif, the original sequence id was
13927 		 * preserved and therefore SCTP won't know.
13928 		 */
13929 		sctp_move_ipif(ipif, ill, ipmp_ill);
13930 
13931 		/*
13932 		 * If the ipif being brought up was on slot zero, then we
13933 		 * first need to bring up the placeholder we stuck there.  In
13934 		 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
13935 		 * call to ipif_up() itself, if we successfully bring up the
13936 		 * placeholder, we'll check ill_move_ipif and bring it up too.
13937 		 */
13938 		if (ipif_orig_id == 0) {
13939 			ASSERT(ill->ill_move_ipif == NULL);
13940 			ill->ill_move_ipif = ipif;
13941 			if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
13942 				ASSERT(ill->ill_move_ipif == NULL);
13943 			if (err != EINPROGRESS)
13944 				ill->ill_move_ipif = NULL;
13945 			return (err);
13946 		}
13947 
13948 		/*
13949 		 * Bring it up on the IPMP ill.
13950 		 */
13951 		return (ipif_up(ipif, q, mp));
13952 	}
13953 
13954 	/* Skip arp/ndp for any loopback interface. */
13955 	if (ill->ill_wq != NULL) {
13956 		conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
13957 		ipsq_t	*ipsq = ill->ill_phyint->phyint_ipsq;
13958 
13959 		if (!ill->ill_dl_up) {
13960 			/*
13961 			 * ill_dl_up is not yet set. i.e. we are yet to
13962 			 * DL_BIND with the driver and this is the first
13963 			 * logical interface on the ill to become "up".
13964 			 * Tell the driver to get going (via DL_BIND_REQ).
13965 			 * Note that changing "significant" IFF_ flags
13966 			 * address/netmask etc cause a down/up dance, but
13967 			 * does not cause an unbind (DL_UNBIND) with the driver
13968 			 */
13969 			return (ill_dl_up(ill, ipif, mp, q));
13970 		}
13971 
13972 		/*
13973 		 * ipif_resolver_up may end up needeing to bind/attach
13974 		 * the ARP stream, which in turn necessitates a
13975 		 * DLPI message exchange with the driver. ioctls are
13976 		 * serialized and so we cannot send more than one
13977 		 * interface up message at a time. If ipif_resolver_up
13978 		 * does need to wait for the DLPI handshake for the ARP stream,
13979 		 * we get EINPROGRESS and we will complete in arp_bringup_done.
13980 		 */
13981 
13982 		ASSERT(connp != NULL || !CONN_Q(q));
13983 		if (connp != NULL)
13984 			mutex_enter(&connp->conn_lock);
13985 		mutex_enter(&ill->ill_lock);
13986 		success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
13987 		mutex_exit(&ill->ill_lock);
13988 		if (connp != NULL)
13989 			mutex_exit(&connp->conn_lock);
13990 		if (!success)
13991 			return (EINTR);
13992 
13993 		/*
13994 		 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
13995 		 * complete when ipif_ndp_up returns.
13996 		 */
13997 		err = ipif_resolver_up(ipif, Res_act_initial);
13998 		if (err == EINPROGRESS) {
13999 			/* We will complete it in arp_bringup_done() */
14000 			return (err);
14001 		}
14002 
14003 		if (isv6 && err == 0)
14004 			err = ipif_ndp_up(ipif, B_TRUE);
14005 
14006 		ASSERT(err != EINPROGRESS);
14007 		mp = ipsq_pending_mp_get(ipsq, &connp);
14008 		ASSERT(mp != NULL);
14009 		if (err != 0)
14010 			return (err);
14011 	} else {
14012 		/*
14013 		 * Interfaces without underlying hardware don't do duplicate
14014 		 * address detection.
14015 		 */
14016 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14017 		ipif->ipif_addr_ready = 1;
14018 		err = ill_add_ires(ill);
14019 		/* allocation failure? */
14020 		if (err != 0)
14021 			return (err);
14022 	}
14023 
14024 	err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14025 	if (err == 0 && ill->ill_move_ipif != NULL) {
14026 		ipif = ill->ill_move_ipif;
14027 		ill->ill_move_ipif = NULL;
14028 		return (ipif_up(ipif, q, mp));
14029 	}
14030 	return (err);
14031 }
14032 
14033 /*
14034  * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14035  * The identical set of IREs need to be removed in ill_delete_ires().
14036  */
14037 int
14038 ill_add_ires(ill_t *ill)
14039 {
14040 	ire_t	*ire;
14041 	in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14042 	in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14043 
14044 	if (ill->ill_ire_multicast != NULL)
14045 		return (0);
14046 
14047 	/*
14048 	 * provide some dummy ire_addr for creating the ire.
14049 	 */
14050 	if (ill->ill_isv6) {
14051 		ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14052 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14053 	} else {
14054 		ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14055 		    ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14056 	}
14057 	if (ire == NULL)
14058 		return (ENOMEM);
14059 
14060 	ill->ill_ire_multicast = ire;
14061 	return (0);
14062 }
14063 
14064 void
14065 ill_delete_ires(ill_t *ill)
14066 {
14067 	if (ill->ill_ire_multicast != NULL) {
14068 		/*
14069 		 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14070 		 * which was taken without any th_tracing enabled.
14071 		 * We also mark it as condemned (note that it was never added)
14072 		 * so that caching conn's can move off of it.
14073 		 */
14074 		ire_make_condemned(ill->ill_ire_multicast);
14075 		ire_refrele_notr(ill->ill_ire_multicast);
14076 		ill->ill_ire_multicast = NULL;
14077 	}
14078 }
14079 
14080 /*
14081  * Perform a bind for the physical device.
14082  * When the routine returns EINPROGRESS then mp has been consumed and
14083  * the ioctl will be acked from ip_rput_dlpi.
14084  * Allocate an unbind message and save it until ipif_down.
14085  */
14086 static int
14087 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14088 {
14089 	mblk_t	*bind_mp = NULL;
14090 	mblk_t	*unbind_mp = NULL;
14091 	conn_t	*connp;
14092 	boolean_t success;
14093 	int	err;
14094 
14095 	DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14096 
14097 	ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14098 	ASSERT(IAM_WRITER_ILL(ill));
14099 	ASSERT(mp != NULL);
14100 
14101 	/*
14102 	 * Make sure we have an IRE_MULTICAST in case we immediately
14103 	 * start receiving packets.
14104 	 */
14105 	err = ill_add_ires(ill);
14106 	if (err != 0)
14107 		goto bad;
14108 
14109 	bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14110 	    DL_BIND_REQ);
14111 	if (bind_mp == NULL)
14112 		goto bad;
14113 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14114 	((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14115 
14116 	unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ);
14117 	if (unbind_mp == NULL)
14118 		goto bad;
14119 
14120 	/*
14121 	 * Record state needed to complete this operation when the
14122 	 * DL_BIND_ACK shows up.  Also remember the pre-allocated mblks.
14123 	 */
14124 	connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14125 	ASSERT(connp != NULL || !CONN_Q(q));
14126 	GRAB_CONN_LOCK(q);
14127 	mutex_enter(&ipif->ipif_ill->ill_lock);
14128 	success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14129 	mutex_exit(&ipif->ipif_ill->ill_lock);
14130 	RELEASE_CONN_LOCK(q);
14131 	if (!success)
14132 		goto bad;
14133 
14134 	/*
14135 	 * Save the unbind message for ill_dl_down(); it will be consumed when
14136 	 * the interface goes down.
14137 	 */
14138 	ASSERT(ill->ill_unbind_mp == NULL);
14139 	ill->ill_unbind_mp = unbind_mp;
14140 
14141 	ill_dlpi_send(ill, bind_mp);
14142 	/* Send down link-layer capabilities probe if not already done. */
14143 	ill_capability_probe(ill);
14144 
14145 	/*
14146 	 * Sysid used to rely on the fact that netboots set domainname
14147 	 * and the like. Now that miniroot boots aren't strictly netboots
14148 	 * and miniroot network configuration is driven from userland
14149 	 * these things still need to be set. This situation can be detected
14150 	 * by comparing the interface being configured here to the one
14151 	 * dhcifname was set to reference by the boot loader. Once sysid is
14152 	 * converted to use dhcp_ipc_getinfo() this call can go away.
14153 	 */
14154 	if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14155 	    (strcmp(ill->ill_name, dhcifname) == 0) &&
14156 	    (strlen(srpc_domain) == 0)) {
14157 		if (dhcpinit() != 0)
14158 			cmn_err(CE_WARN, "no cached dhcp response");
14159 	}
14160 
14161 	/*
14162 	 * This operation will complete in ip_rput_dlpi with either
14163 	 * a DL_BIND_ACK or DL_ERROR_ACK.
14164 	 */
14165 	return (EINPROGRESS);
14166 bad:
14167 	ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14168 
14169 	freemsg(bind_mp);
14170 	freemsg(unbind_mp);
14171 	return (ENOMEM);
14172 }
14173 
14174 /* Add room for tcp+ip headers */
14175 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14176 
14177 /*
14178  * DLPI and ARP is up.
14179  * Create all the IREs associated with an interface. Bring up multicast.
14180  * Set the interface flag and finish other initialization
14181  * that potentially had to be deferred to after DL_BIND_ACK.
14182  */
14183 int
14184 ipif_up_done(ipif_t *ipif)
14185 {
14186 	ill_t		*ill = ipif->ipif_ill;
14187 	int		err = 0;
14188 	boolean_t	loopback = B_FALSE;
14189 	boolean_t	update_src_selection = B_TRUE;
14190 	ipif_t		*tmp_ipif;
14191 
14192 	ip1dbg(("ipif_up_done(%s:%u)\n",
14193 	    ipif->ipif_ill->ill_name, ipif->ipif_id));
14194 	DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14195 	    ill_t *, ill, ipif_t *, ipif);
14196 
14197 	/* Check if this is a loopback interface */
14198 	if (ipif->ipif_ill->ill_wq == NULL)
14199 		loopback = B_TRUE;
14200 
14201 	ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14202 
14203 	/*
14204 	 * If all other interfaces for this ill are down or DEPRECATED,
14205 	 * or otherwise unsuitable for source address selection,
14206 	 * reset the src generation numbers to make sure source
14207 	 * address selection gets to take this new ipif into account.
14208 	 * No need to hold ill_lock while traversing the ipif list since
14209 	 * we are writer
14210 	 */
14211 	for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14212 	    tmp_ipif = tmp_ipif->ipif_next) {
14213 		if (((tmp_ipif->ipif_flags &
14214 		    (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14215 		    !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14216 		    (tmp_ipif == ipif))
14217 			continue;
14218 		/* first useable pre-existing interface */
14219 		update_src_selection = B_FALSE;
14220 		break;
14221 	}
14222 	if (update_src_selection)
14223 		ip_update_source_selection(ill->ill_ipst);
14224 
14225 	if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14226 		nce_t *loop_nce = NULL;
14227 		uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14228 
14229 		/*
14230 		 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14231 		 * ipif_lookup_on_name(), but in the case of zones we can have
14232 		 * several loopback addresses on lo0. So all the interfaces with
14233 		 * loopback addresses need to be marked IRE_LOOPBACK.
14234 		 */
14235 		if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14236 		    htonl(INADDR_LOOPBACK))
14237 			ipif->ipif_ire_type = IRE_LOOPBACK;
14238 		else
14239 			ipif->ipif_ire_type = IRE_LOCAL;
14240 		if (ill->ill_net_type != IRE_LOOPBACK)
14241 			flags |= NCE_F_PUBLISH;
14242 
14243 		/* add unicast nce for the local addr */
14244 		err = nce_lookup_then_add_v4(ill, NULL,
14245 		    ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14246 		    ND_REACHABLE, &loop_nce);
14247 		/* A shared-IP zone sees EEXIST for lo0:N */
14248 		if (err == 0 || err == EEXIST) {
14249 			ipif->ipif_added_nce = 1;
14250 			loop_nce->nce_ipif_cnt++;
14251 			nce_refrele(loop_nce);
14252 			err = 0;
14253 		} else {
14254 			ASSERT(loop_nce == NULL);
14255 			return (err);
14256 		}
14257 	}
14258 
14259 	/* Create all the IREs associated with this interface */
14260 	err = ipif_add_ires_v4(ipif, loopback);
14261 	if (err != 0) {
14262 		/*
14263 		 * see comments about return value from
14264 		 * ip_addr_availability_check() in ipif_add_ires_v4().
14265 		 */
14266 		if (err != EADDRINUSE) {
14267 			(void) ipif_arp_down(ipif);
14268 		} else {
14269 			/*
14270 			 * Make IPMP aware of the deleted ipif so that
14271 			 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14272 			 * can be completed. Note that we do not want to
14273 			 * destroy the nce that was created on the ipmp_ill
14274 			 * for the active copy of the duplicate address in
14275 			 * use.
14276 			 */
14277 			if (IS_IPMP(ill))
14278 				ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14279 			err = EADDRNOTAVAIL;
14280 		}
14281 		return (err);
14282 	}
14283 
14284 	if (ill->ill_ipif_up_count == 1 && !loopback) {
14285 		/* Recover any additional IREs entries for this ill */
14286 		(void) ill_recover_saved_ire(ill);
14287 	}
14288 
14289 	if (ill->ill_need_recover_multicast) {
14290 		/*
14291 		 * Need to recover all multicast memberships in the driver.
14292 		 * This had to be deferred until we had attached.  The same
14293 		 * code exists in ipif_up_done_v6() to recover IPv6
14294 		 * memberships.
14295 		 *
14296 		 * Note that it would be preferable to unconditionally do the
14297 		 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14298 		 * that since ill_join_allmulti() depends on ill_dl_up being
14299 		 * set, and it is not set until we receive a DL_BIND_ACK after
14300 		 * having called ill_dl_up().
14301 		 */
14302 		ill_recover_multicast(ill);
14303 	}
14304 
14305 	if (ill->ill_ipif_up_count == 1) {
14306 		/*
14307 		 * Since the interface is now up, it may now be active.
14308 		 */
14309 		if (IS_UNDER_IPMP(ill))
14310 			ipmp_ill_refresh_active(ill);
14311 
14312 		/*
14313 		 * If this is an IPMP interface, we may now be able to
14314 		 * establish ARP entries.
14315 		 */
14316 		if (IS_IPMP(ill))
14317 			ipmp_illgrp_refresh_arpent(ill->ill_grp);
14318 	}
14319 
14320 	/* Join the allhosts multicast address */
14321 	ipif_multicast_up(ipif);
14322 
14323 	if (!loopback && !update_src_selection &&
14324 	    !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14325 		ip_update_source_selection(ill->ill_ipst);
14326 
14327 	if (!loopback && ipif->ipif_addr_ready) {
14328 		/* Broadcast an address mask reply. */
14329 		ipif_mask_reply(ipif);
14330 	}
14331 	/* Perhaps ilgs should use this ill */
14332 	update_conn_ill(NULL, ill->ill_ipst);
14333 
14334 	/*
14335 	 * This had to be deferred until we had bound.  Tell routing sockets and
14336 	 * others that this interface is up if it looks like the address has
14337 	 * been validated.  Otherwise, if it isn't ready yet, wait for
14338 	 * duplicate address detection to do its thing.
14339 	 */
14340 	if (ipif->ipif_addr_ready)
14341 		ipif_up_notify(ipif);
14342 	return (0);
14343 }
14344 
14345 /*
14346  * Add the IREs associated with the ipif.
14347  * Those MUST be explicitly removed in ipif_delete_ires_v4.
14348  */
14349 static int
14350 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14351 {
14352 	ill_t		*ill = ipif->ipif_ill;
14353 	ip_stack_t	*ipst = ill->ill_ipst;
14354 	ire_t		*ire_array[20];
14355 	ire_t		**irep = ire_array;
14356 	ire_t		**irep1;
14357 	ipaddr_t	net_mask = 0;
14358 	ipaddr_t	subnet_mask, route_mask;
14359 	int		err;
14360 	ire_t		*ire_local = NULL;	/* LOCAL or LOOPBACK */
14361 	ire_t		*ire_if = NULL;
14362 
14363 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14364 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14365 		/*
14366 		 * If we're on a labeled system then make sure that zone-
14367 		 * private addresses have proper remote host database entries.
14368 		 */
14369 		if (is_system_labeled() &&
14370 		    ipif->ipif_ire_type != IRE_LOOPBACK &&
14371 		    !tsol_check_interface_address(ipif))
14372 			return (EINVAL);
14373 
14374 		/* Register the source address for __sin6_src_id */
14375 		err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14376 		    ipif->ipif_zoneid, ipst);
14377 		if (err != 0) {
14378 			ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14379 			return (err);
14380 		}
14381 
14382 		/* If the interface address is set, create the local IRE. */
14383 		ire_local = ire_create(
14384 		    (uchar_t *)&ipif->ipif_lcl_addr,	/* dest address */
14385 		    (uchar_t *)&ip_g_all_ones,		/* mask */
14386 		    NULL,				/* no gateway */
14387 		    ipif->ipif_ire_type,		/* LOCAL or LOOPBACK */
14388 		    ipif->ipif_ill,
14389 		    ipif->ipif_zoneid,
14390 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14391 		    RTF_PRIVATE : 0) | RTF_KERNEL,
14392 		    NULL,
14393 		    ipst);
14394 		ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14395 		    " for 0x%x\n", (void *)ipif, (void *)ire_local,
14396 		    ipif->ipif_ire_type,
14397 		    ntohl(ipif->ipif_lcl_addr)));
14398 		if (ire_local == NULL) {
14399 			ip1dbg(("ipif_up_done: NULL ire_local\n"));
14400 			err = ENOMEM;
14401 			goto bad;
14402 		}
14403 	} else {
14404 		ip1dbg((
14405 		    "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14406 		    ipif->ipif_ire_type,
14407 		    ntohl(ipif->ipif_lcl_addr),
14408 		    (uint_t)ipif->ipif_flags));
14409 	}
14410 	if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14411 	    !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14412 		net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14413 	} else {
14414 		net_mask = htonl(IN_CLASSA_NET);	/* fallback */
14415 	}
14416 
14417 	subnet_mask = ipif->ipif_net_mask;
14418 
14419 	/*
14420 	 * If mask was not specified, use natural netmask of
14421 	 * interface address. Also, store this mask back into the
14422 	 * ipif struct.
14423 	 */
14424 	if (subnet_mask == 0) {
14425 		subnet_mask = net_mask;
14426 		V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14427 		V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14428 		    ipif->ipif_v6subnet);
14429 	}
14430 
14431 	/* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14432 	if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14433 	    ipif->ipif_subnet != INADDR_ANY) {
14434 		/* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14435 
14436 		if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14437 			route_mask = IP_HOST_MASK;
14438 		} else {
14439 			route_mask = subnet_mask;
14440 		}
14441 
14442 		ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14443 		    "creating if IRE ill_net_type 0x%x for 0x%x\n",
14444 		    (void *)ipif, (void *)ill, ill->ill_net_type,
14445 		    ntohl(ipif->ipif_subnet)));
14446 		ire_if = ire_create(
14447 		    (uchar_t *)&ipif->ipif_subnet,
14448 		    (uchar_t *)&route_mask,
14449 		    (uchar_t *)&ipif->ipif_lcl_addr,
14450 		    ill->ill_net_type,
14451 		    ill,
14452 		    ipif->ipif_zoneid,
14453 		    ((ipif->ipif_flags & IPIF_PRIVATE) ?
14454 		    RTF_PRIVATE: 0) | RTF_KERNEL,
14455 		    NULL,
14456 		    ipst);
14457 		if (ire_if == NULL) {
14458 			ip1dbg(("ipif_up_done: NULL ire_if\n"));
14459 			err = ENOMEM;
14460 			goto bad;
14461 		}
14462 	}
14463 
14464 	/*
14465 	 * Create any necessary broadcast IREs.
14466 	 */
14467 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14468 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14469 		irep = ipif_create_bcast_ires(ipif, irep);
14470 
14471 	/* If an earlier ire_create failed, get out now */
14472 	for (irep1 = irep; irep1 > ire_array; ) {
14473 		irep1--;
14474 		if (*irep1 == NULL) {
14475 			ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
14476 			err = ENOMEM;
14477 			goto bad;
14478 		}
14479 	}
14480 
14481 	/*
14482 	 * Need to atomically check for IP address availability under
14483 	 * ip_addr_avail_lock.  ill_g_lock is held as reader to ensure no new
14484 	 * ills or new ipifs can be added while we are checking availability.
14485 	 */
14486 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14487 	mutex_enter(&ipst->ips_ip_addr_avail_lock);
14488 	/* Mark it up, and increment counters. */
14489 	ipif->ipif_flags |= IPIF_UP;
14490 	ill->ill_ipif_up_count++;
14491 	err = ip_addr_availability_check(ipif);
14492 	mutex_exit(&ipst->ips_ip_addr_avail_lock);
14493 	rw_exit(&ipst->ips_ill_g_lock);
14494 
14495 	if (err != 0) {
14496 		/*
14497 		 * Our address may already be up on the same ill. In this case,
14498 		 * the ARP entry for our ipif replaced the one for the other
14499 		 * ipif. So we don't want to delete it (otherwise the other ipif
14500 		 * would be unable to send packets).
14501 		 * ip_addr_availability_check() identifies this case for us and
14502 		 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
14503 		 * which is the expected error code.
14504 		 */
14505 		ill->ill_ipif_up_count--;
14506 		ipif->ipif_flags &= ~IPIF_UP;
14507 		goto bad;
14508 	}
14509 
14510 	/*
14511 	 * Add in all newly created IREs.  ire_create_bcast() has
14512 	 * already checked for duplicates of the IRE_BROADCAST type.
14513 	 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
14514 	 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
14515 	 * a /32 route.
14516 	 */
14517 	if (ire_if != NULL) {
14518 		ire_if = ire_add(ire_if);
14519 		if (ire_if == NULL) {
14520 			err = ENOMEM;
14521 			goto bad2;
14522 		}
14523 #ifdef DEBUG
14524 		ire_refhold_notr(ire_if);
14525 		ire_refrele(ire_if);
14526 #endif
14527 	}
14528 	if (ire_local != NULL) {
14529 		ire_local = ire_add(ire_local);
14530 		if (ire_local == NULL) {
14531 			err = ENOMEM;
14532 			goto bad2;
14533 		}
14534 #ifdef DEBUG
14535 		ire_refhold_notr(ire_local);
14536 		ire_refrele(ire_local);
14537 #endif
14538 	}
14539 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14540 	if (ire_local != NULL)
14541 		ipif->ipif_ire_local = ire_local;
14542 	if (ire_if != NULL)
14543 		ipif->ipif_ire_if = ire_if;
14544 	rw_exit(&ipst->ips_ill_g_lock);
14545 	ire_local = NULL;
14546 	ire_if = NULL;
14547 
14548 	/*
14549 	 * We first add all of them, and if that succeeds we refrele the
14550 	 * bunch. That enables us to delete all of them should any of the
14551 	 * ire_adds fail.
14552 	 */
14553 	for (irep1 = irep; irep1 > ire_array; ) {
14554 		irep1--;
14555 		ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
14556 		*irep1 = ire_add(*irep1);
14557 		if (*irep1 == NULL) {
14558 			err = ENOMEM;
14559 			goto bad2;
14560 		}
14561 	}
14562 
14563 	for (irep1 = irep; irep1 > ire_array; ) {
14564 		irep1--;
14565 		/* refheld by ire_add. */
14566 		if (*irep1 != NULL) {
14567 			ire_refrele(*irep1);
14568 			*irep1 = NULL;
14569 		}
14570 	}
14571 
14572 	if (!loopback) {
14573 		/*
14574 		 * If the broadcast address has been set, make sure it makes
14575 		 * sense based on the interface address.
14576 		 * Only match on ill since we are sharing broadcast addresses.
14577 		 */
14578 		if ((ipif->ipif_brd_addr != INADDR_ANY) &&
14579 		    (ipif->ipif_flags & IPIF_BROADCAST)) {
14580 			ire_t	*ire;
14581 
14582 			ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
14583 			    IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
14584 			    (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
14585 
14586 			if (ire == NULL) {
14587 				/*
14588 				 * If there isn't a matching broadcast IRE,
14589 				 * revert to the default for this netmask.
14590 				 */
14591 				ipif->ipif_v6brd_addr = ipv6_all_zeros;
14592 				mutex_enter(&ipif->ipif_ill->ill_lock);
14593 				ipif_set_default(ipif);
14594 				mutex_exit(&ipif->ipif_ill->ill_lock);
14595 			} else {
14596 				ire_refrele(ire);
14597 			}
14598 		}
14599 
14600 	}
14601 	return (0);
14602 
14603 bad2:
14604 	ill->ill_ipif_up_count--;
14605 	ipif->ipif_flags &= ~IPIF_UP;
14606 
14607 bad:
14608 	ip1dbg(("ipif_add_ires: FAILED \n"));
14609 	if (ire_local != NULL)
14610 		ire_delete(ire_local);
14611 	if (ire_if != NULL)
14612 		ire_delete(ire_if);
14613 
14614 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14615 	ire_local = ipif->ipif_ire_local;
14616 	ipif->ipif_ire_local = NULL;
14617 	ire_if = ipif->ipif_ire_if;
14618 	ipif->ipif_ire_if = NULL;
14619 	rw_exit(&ipst->ips_ill_g_lock);
14620 	if (ire_local != NULL) {
14621 		ire_delete(ire_local);
14622 		ire_refrele_notr(ire_local);
14623 	}
14624 	if (ire_if != NULL) {
14625 		ire_delete(ire_if);
14626 		ire_refrele_notr(ire_if);
14627 	}
14628 
14629 	while (irep > ire_array) {
14630 		irep--;
14631 		if (*irep != NULL) {
14632 			ire_delete(*irep);
14633 		}
14634 	}
14635 	(void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
14636 
14637 	return (err);
14638 }
14639 
14640 /* Remove all the IREs created by ipif_add_ires_v4 */
14641 void
14642 ipif_delete_ires_v4(ipif_t *ipif)
14643 {
14644 	ill_t		*ill = ipif->ipif_ill;
14645 	ip_stack_t	*ipst = ill->ill_ipst;
14646 	ire_t		*ire;
14647 
14648 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14649 	ire = ipif->ipif_ire_local;
14650 	ipif->ipif_ire_local = NULL;
14651 	rw_exit(&ipst->ips_ill_g_lock);
14652 	if (ire != NULL) {
14653 		/*
14654 		 * Move count to ipif so we don't loose the count due to
14655 		 * a down/up dance.
14656 		 */
14657 		atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
14658 
14659 		ire_delete(ire);
14660 		ire_refrele_notr(ire);
14661 	}
14662 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14663 	ire = ipif->ipif_ire_if;
14664 	ipif->ipif_ire_if = NULL;
14665 	rw_exit(&ipst->ips_ill_g_lock);
14666 	if (ire != NULL) {
14667 		ire_delete(ire);
14668 		ire_refrele_notr(ire);
14669 	}
14670 
14671 	/*
14672 	 * Delete the broadcast IREs.
14673 	 */
14674 	if ((ipif->ipif_flags & IPIF_BROADCAST) &&
14675 	    !(ipif->ipif_flags & IPIF_NOXMIT))
14676 		ipif_delete_bcast_ires(ipif);
14677 }
14678 
14679 /*
14680  * Checks for availbility of a usable source address (if there is one) when the
14681  * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
14682  * this selection is done regardless of the destination.
14683  */
14684 boolean_t
14685 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
14686     ip_stack_t *ipst)
14687 {
14688 	ipif_t		*ipif = NULL;
14689 	ill_t		*uill;
14690 
14691 	ASSERT(ifindex != 0);
14692 
14693 	uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
14694 	if (uill == NULL)
14695 		return (B_FALSE);
14696 
14697 	mutex_enter(&uill->ill_lock);
14698 	for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14699 		if (IPIF_IS_CONDEMNED(ipif))
14700 			continue;
14701 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14702 			continue;
14703 		if (!(ipif->ipif_flags & IPIF_UP))
14704 			continue;
14705 		if (ipif->ipif_zoneid != zoneid)
14706 			continue;
14707 		if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14708 		    ipif->ipif_lcl_addr == INADDR_ANY)
14709 			continue;
14710 		mutex_exit(&uill->ill_lock);
14711 		ill_refrele(uill);
14712 		return (B_TRUE);
14713 	}
14714 	mutex_exit(&uill->ill_lock);
14715 	ill_refrele(uill);
14716 	return (B_FALSE);
14717 }
14718 
14719 /*
14720  * Find an ipif with a good local address on the ill+zoneid.
14721  */
14722 ipif_t *
14723 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
14724 {
14725 	ipif_t		*ipif;
14726 
14727 	mutex_enter(&ill->ill_lock);
14728 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
14729 		if (IPIF_IS_CONDEMNED(ipif))
14730 			continue;
14731 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14732 			continue;
14733 		if (!(ipif->ipif_flags & IPIF_UP))
14734 			continue;
14735 		if (ipif->ipif_zoneid != zoneid &&
14736 		    ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
14737 			continue;
14738 		if (ill->ill_isv6 ?
14739 		    IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
14740 		    ipif->ipif_lcl_addr == INADDR_ANY)
14741 			continue;
14742 		ipif_refhold_locked(ipif);
14743 		mutex_exit(&ill->ill_lock);
14744 		return (ipif);
14745 	}
14746 	mutex_exit(&ill->ill_lock);
14747 	return (NULL);
14748 }
14749 
14750 /*
14751  * IP source address type, sorted from worst to best.  For a given type,
14752  * always prefer IP addresses on the same subnet.  All-zones addresses are
14753  * suboptimal because they pose problems with unlabeled destinations.
14754  */
14755 typedef enum {
14756 	IPIF_NONE,
14757 	IPIF_DIFFNET_DEPRECATED, 	/* deprecated and different subnet */
14758 	IPIF_SAMENET_DEPRECATED, 	/* deprecated and same subnet */
14759 	IPIF_DIFFNET_ALLZONES,		/* allzones and different subnet */
14760 	IPIF_SAMENET_ALLZONES,		/* allzones and same subnet */
14761 	IPIF_DIFFNET,			/* normal and different subnet */
14762 	IPIF_SAMENET,			/* normal and same subnet */
14763 	IPIF_LOCALADDR			/* local loopback */
14764 } ipif_type_t;
14765 
14766 /*
14767  * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
14768  * `zoneid'.  We rate usable ipifs from low -> high as per the ipif_type_t
14769  * enumeration, and return the highest-rated ipif.  If there's a tie, we pick
14770  * the first one, unless IPMP is used in which case we round-robin among them;
14771  * see below for more.
14772  *
14773  * Returns NULL if there is no suitable source address for the ill.
14774  * This only occurs when there is no valid source address for the ill.
14775  */
14776 ipif_t *
14777 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
14778     boolean_t allow_usesrc, boolean_t *notreadyp)
14779 {
14780 	ill_t	*usill = NULL;
14781 	ill_t	*ipmp_ill = NULL;
14782 	ipif_t	*start_ipif, *next_ipif, *ipif, *best_ipif;
14783 	ipif_type_t type, best_type;
14784 	tsol_tpc_t *src_rhtp, *dst_rhtp;
14785 	ip_stack_t *ipst = ill->ill_ipst;
14786 	boolean_t samenet;
14787 
14788 	if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
14789 		usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
14790 		    B_FALSE, ipst);
14791 		if (usill != NULL)
14792 			ill = usill;	/* Select source from usesrc ILL */
14793 		else
14794 			return (NULL);
14795 	}
14796 
14797 	/*
14798 	 * Test addresses should never be used for source address selection,
14799 	 * so if we were passed one, switch to the IPMP meta-interface.
14800 	 */
14801 	if (IS_UNDER_IPMP(ill)) {
14802 		if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
14803 			ill = ipmp_ill;	/* Select source from IPMP ill */
14804 		else
14805 			return (NULL);
14806 	}
14807 
14808 	/*
14809 	 * If we're dealing with an unlabeled destination on a labeled system,
14810 	 * make sure that we ignore source addresses that are incompatible with
14811 	 * the destination's default label.  That destination's default label
14812 	 * must dominate the minimum label on the source address.
14813 	 */
14814 	dst_rhtp = NULL;
14815 	if (is_system_labeled()) {
14816 		dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
14817 		if (dst_rhtp == NULL)
14818 			return (NULL);
14819 		if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
14820 			TPC_RELE(dst_rhtp);
14821 			dst_rhtp = NULL;
14822 		}
14823 	}
14824 
14825 	/*
14826 	 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
14827 	 * can be deleted. But an ipif/ill can get CONDEMNED any time.
14828 	 * After selecting the right ipif, under ill_lock make sure ipif is
14829 	 * not condemned, and increment refcnt. If ipif is CONDEMNED,
14830 	 * we retry. Inside the loop we still need to check for CONDEMNED,
14831 	 * but not under a lock.
14832 	 */
14833 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14834 retry:
14835 	/*
14836 	 * For source address selection, we treat the ipif list as circular
14837 	 * and continue until we get back to where we started.  This allows
14838 	 * IPMP to vary source address selection (which improves inbound load
14839 	 * spreading) by caching its last ending point and starting from
14840 	 * there.  NOTE: we don't have to worry about ill_src_ipif changing
14841 	 * ills since that can't happen on the IPMP ill.
14842 	 */
14843 	start_ipif = ill->ill_ipif;
14844 	if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
14845 		start_ipif = ill->ill_src_ipif;
14846 
14847 	ipif = start_ipif;
14848 	best_ipif = NULL;
14849 	best_type = IPIF_NONE;
14850 	do {
14851 		if ((next_ipif = ipif->ipif_next) == NULL)
14852 			next_ipif = ill->ill_ipif;
14853 
14854 		if (IPIF_IS_CONDEMNED(ipif))
14855 			continue;
14856 		/* Always skip NOLOCAL and ANYCAST interfaces */
14857 		if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
14858 			continue;
14859 		/* Always skip NOACCEPT interfaces */
14860 		if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
14861 			continue;
14862 		if (!(ipif->ipif_flags & IPIF_UP))
14863 			continue;
14864 
14865 		if (!ipif->ipif_addr_ready) {
14866 			if (notreadyp != NULL)
14867 				*notreadyp = B_TRUE;
14868 			continue;
14869 		}
14870 
14871 		if (zoneid != ALL_ZONES &&
14872 		    ipif->ipif_zoneid != zoneid &&
14873 		    ipif->ipif_zoneid != ALL_ZONES)
14874 			continue;
14875 
14876 		/*
14877 		 * Interfaces with 0.0.0.0 address are allowed to be UP, but
14878 		 * are not valid as source addresses.
14879 		 */
14880 		if (ipif->ipif_lcl_addr == INADDR_ANY)
14881 			continue;
14882 
14883 		/*
14884 		 * Check compatibility of local address for destination's
14885 		 * default label if we're on a labeled system.	Incompatible
14886 		 * addresses can't be used at all.
14887 		 */
14888 		if (dst_rhtp != NULL) {
14889 			boolean_t incompat;
14890 
14891 			src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
14892 			    IPV4_VERSION, B_FALSE);
14893 			if (src_rhtp == NULL)
14894 				continue;
14895 			incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
14896 			    src_rhtp->tpc_tp.tp_doi !=
14897 			    dst_rhtp->tpc_tp.tp_doi ||
14898 			    (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
14899 			    &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
14900 			    !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
14901 			    src_rhtp->tpc_tp.tp_sl_set_cipso));
14902 			TPC_RELE(src_rhtp);
14903 			if (incompat)
14904 				continue;
14905 		}
14906 
14907 		samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
14908 
14909 		if (ipif->ipif_lcl_addr == dst) {
14910 			type = IPIF_LOCALADDR;
14911 		} else if (ipif->ipif_flags & IPIF_DEPRECATED) {
14912 			type = samenet ? IPIF_SAMENET_DEPRECATED :
14913 			    IPIF_DIFFNET_DEPRECATED;
14914 		} else if (ipif->ipif_zoneid == ALL_ZONES) {
14915 			type = samenet ? IPIF_SAMENET_ALLZONES :
14916 			    IPIF_DIFFNET_ALLZONES;
14917 		} else {
14918 			type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
14919 		}
14920 
14921 		if (type > best_type) {
14922 			best_type = type;
14923 			best_ipif = ipif;
14924 			if (best_type == IPIF_LOCALADDR)
14925 				break; /* can't get better */
14926 		}
14927 	} while ((ipif = next_ipif) != start_ipif);
14928 
14929 	if ((ipif = best_ipif) != NULL) {
14930 		mutex_enter(&ipif->ipif_ill->ill_lock);
14931 		if (IPIF_IS_CONDEMNED(ipif)) {
14932 			mutex_exit(&ipif->ipif_ill->ill_lock);
14933 			goto retry;
14934 		}
14935 		ipif_refhold_locked(ipif);
14936 
14937 		/*
14938 		 * For IPMP, update the source ipif rotor to the next ipif,
14939 		 * provided we can look it up.  (We must not use it if it's
14940 		 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
14941 		 * ipif_free() checked ill_src_ipif.)
14942 		 */
14943 		if (IS_IPMP(ill) && ipif != NULL) {
14944 			next_ipif = ipif->ipif_next;
14945 			if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
14946 				ill->ill_src_ipif = next_ipif;
14947 			else
14948 				ill->ill_src_ipif = NULL;
14949 		}
14950 		mutex_exit(&ipif->ipif_ill->ill_lock);
14951 	}
14952 
14953 	rw_exit(&ipst->ips_ill_g_lock);
14954 	if (usill != NULL)
14955 		ill_refrele(usill);
14956 	if (ipmp_ill != NULL)
14957 		ill_refrele(ipmp_ill);
14958 	if (dst_rhtp != NULL)
14959 		TPC_RELE(dst_rhtp);
14960 
14961 #ifdef DEBUG
14962 	if (ipif == NULL) {
14963 		char buf1[INET6_ADDRSTRLEN];
14964 
14965 		ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
14966 		    ill->ill_name,
14967 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
14968 	} else {
14969 		char buf1[INET6_ADDRSTRLEN];
14970 		char buf2[INET6_ADDRSTRLEN];
14971 
14972 		ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
14973 		    ipif->ipif_ill->ill_name,
14974 		    inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
14975 		    inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
14976 		    buf2, sizeof (buf2))));
14977 	}
14978 #endif /* DEBUG */
14979 	return (ipif);
14980 }
14981 
14982 /*
14983  * Pick a source address based on the destination ill and an optional setsrc
14984  * address.
14985  * The result is stored in srcp. If generation is set, then put the source
14986  * generation number there before we look for the source address (to avoid
14987  * missing changes in the set of source addresses.
14988  * If flagsp is set, then us it to pass back ipif_flags.
14989  *
14990  * If the caller wants to cache the returned source address and detect when
14991  * that might be stale, the caller should pass in a generation argument,
14992  * which the caller can later compare against ips_src_generation
14993  *
14994  * The precedence order for selecting an IPv4 source address is:
14995  *  - RTF_SETSRC on the offlink ire always wins.
14996  *  - If usrsrc is set, swap the ill to be the usesrc one.
14997  *  - If IPMP is used on the ill, select a random address from the most
14998  *    preferred ones below:
14999  * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15000  * 2. Not deprecated, not ALL_ZONES
15001  * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15002  * 4. Not deprecated, ALL_ZONES
15003  * 5. If onlink destination, same subnet and deprecated
15004  * 6. Deprecated.
15005  *
15006  * We have lower preference for ALL_ZONES IP addresses,
15007  * as they pose problems with unlabeled destinations.
15008  *
15009  * Note that when multiple IP addresses match e.g., #1 we pick
15010  * the first one if IPMP is not in use. With IPMP we randomize.
15011  */
15012 int
15013 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15014     ipaddr_t multicast_ifaddr,
15015     zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15016     uint32_t *generation, uint64_t *flagsp)
15017 {
15018 	ipif_t *ipif;
15019 	boolean_t notready = B_FALSE;	/* Set if !ipif_addr_ready found */
15020 
15021 	if (flagsp != NULL)
15022 		*flagsp = 0;
15023 
15024 	/*
15025 	 * Need to grab the generation number before we check to
15026 	 * avoid a race with a change to the set of local addresses.
15027 	 * No lock needed since the thread which updates the set of local
15028 	 * addresses use ipif/ill locks and exit those (hence a store memory
15029 	 * barrier) before doing the atomic increase of ips_src_generation.
15030 	 */
15031 	if (generation != NULL) {
15032 		*generation = ipst->ips_src_generation;
15033 	}
15034 
15035 	if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15036 		*srcp = multicast_ifaddr;
15037 		return (0);
15038 	}
15039 
15040 	/* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15041 	if (setsrc != INADDR_ANY) {
15042 		*srcp = setsrc;
15043 		return (0);
15044 	}
15045 	ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, &notready);
15046 	if (ipif == NULL) {
15047 		if (notready)
15048 			return (ENETDOWN);
15049 		else
15050 			return (EADDRNOTAVAIL);
15051 	}
15052 	*srcp = ipif->ipif_lcl_addr;
15053 	if (flagsp != NULL)
15054 		*flagsp = ipif->ipif_flags;
15055 	ipif_refrele(ipif);
15056 	return (0);
15057 }
15058 
15059 /* ARGSUSED */
15060 int
15061 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15062 	ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15063 {
15064 	/*
15065 	 * ill_phyint_reinit merged the v4 and v6 into a single
15066 	 * ipsq.  We might not have been able to complete the
15067 	 * operation in ipif_set_values, if we could not become
15068 	 * exclusive.  If so restart it here.
15069 	 */
15070 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15071 }
15072 
15073 /*
15074  * Can operate on either a module or a driver queue.
15075  * Returns an error if not a module queue.
15076  */
15077 /* ARGSUSED */
15078 int
15079 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15080     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15081 {
15082 	queue_t		*q1 = q;
15083 	char 		*cp;
15084 	char		interf_name[LIFNAMSIZ];
15085 	uint_t		ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15086 
15087 	if (q->q_next == NULL) {
15088 		ip1dbg((
15089 		    "if_unitsel: IF_UNITSEL: no q_next\n"));
15090 		return (EINVAL);
15091 	}
15092 
15093 	if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15094 		return (EALREADY);
15095 
15096 	do {
15097 		q1 = q1->q_next;
15098 	} while (q1->q_next);
15099 	cp = q1->q_qinfo->qi_minfo->mi_idname;
15100 	(void) sprintf(interf_name, "%s%d", cp, ppa);
15101 
15102 	/*
15103 	 * Here we are not going to delay the ioack until after
15104 	 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15105 	 * original ioctl message before sending the requests.
15106 	 */
15107 	return (ipif_set_values(q, mp, interf_name, &ppa));
15108 }
15109 
15110 /* ARGSUSED */
15111 int
15112 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15113     ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15114 {
15115 	return (ENXIO);
15116 }
15117 
15118 /*
15119  * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15120  * `irep'.  Returns a pointer to the next free `irep' entry
15121  * A mirror exists in ipif_delete_bcast_ires().
15122  *
15123  * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15124  * done in ire_add.
15125  */
15126 static ire_t **
15127 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15128 {
15129 	ipaddr_t addr;
15130 	ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15131 	ipaddr_t subnetmask = ipif->ipif_net_mask;
15132 	ill_t *ill = ipif->ipif_ill;
15133 	zoneid_t zoneid = ipif->ipif_zoneid;
15134 
15135 	ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15136 
15137 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15138 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15139 
15140 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15141 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15142 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15143 
15144 	irep = ire_create_bcast(ill, 0, zoneid, irep);
15145 	irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15146 
15147 	/*
15148 	 * For backward compatibility, we create net broadcast IREs based on
15149 	 * the old "IP address class system", since some old machines only
15150 	 * respond to these class derived net broadcast.  However, we must not
15151 	 * create these net broadcast IREs if the subnetmask is shorter than
15152 	 * the IP address class based derived netmask.  Otherwise, we may
15153 	 * create a net broadcast address which is the same as an IP address
15154 	 * on the subnet -- and then TCP will refuse to talk to that address.
15155 	 */
15156 	if (netmask < subnetmask) {
15157 		addr = netmask & ipif->ipif_subnet;
15158 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15159 		irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15160 	}
15161 
15162 	/*
15163 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15164 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15165 	 * created.  Creating these broadcast IREs will only create confusion
15166 	 * as `addr' will be the same as the IP address.
15167 	 */
15168 	if (subnetmask != 0xFFFFFFFF) {
15169 		addr = ipif->ipif_subnet;
15170 		irep = ire_create_bcast(ill, addr, zoneid, irep);
15171 		irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15172 	}
15173 
15174 	return (irep);
15175 }
15176 
15177 /*
15178  * Mirror of ipif_create_bcast_ires()
15179  */
15180 static void
15181 ipif_delete_bcast_ires(ipif_t *ipif)
15182 {
15183 	ipaddr_t	addr;
15184 	ipaddr_t	netmask = ip_net_mask(ipif->ipif_lcl_addr);
15185 	ipaddr_t	subnetmask = ipif->ipif_net_mask;
15186 	ill_t		*ill = ipif->ipif_ill;
15187 	zoneid_t	zoneid = ipif->ipif_zoneid;
15188 	ire_t		*ire;
15189 
15190 	ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15191 	ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15192 
15193 	if (ipif->ipif_lcl_addr == INADDR_ANY ||
15194 	    (ipif->ipif_flags & IPIF_NOLOCAL))
15195 		netmask = htonl(IN_CLASSA_NET);		/* fallback */
15196 
15197 	ire = ire_lookup_bcast(ill, 0, zoneid);
15198 	ASSERT(ire != NULL);
15199 	ire_delete(ire); ire_refrele(ire);
15200 	ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15201 	ASSERT(ire != NULL);
15202 	ire_delete(ire); ire_refrele(ire);
15203 
15204 	/*
15205 	 * For backward compatibility, we create net broadcast IREs based on
15206 	 * the old "IP address class system", since some old machines only
15207 	 * respond to these class derived net broadcast.  However, we must not
15208 	 * create these net broadcast IREs if the subnetmask is shorter than
15209 	 * the IP address class based derived netmask.  Otherwise, we may
15210 	 * create a net broadcast address which is the same as an IP address
15211 	 * on the subnet -- and then TCP will refuse to talk to that address.
15212 	 */
15213 	if (netmask < subnetmask) {
15214 		addr = netmask & ipif->ipif_subnet;
15215 		ire = ire_lookup_bcast(ill, addr, zoneid);
15216 		ASSERT(ire != NULL);
15217 		ire_delete(ire); ire_refrele(ire);
15218 		ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15219 		ASSERT(ire != NULL);
15220 		ire_delete(ire); ire_refrele(ire);
15221 	}
15222 
15223 	/*
15224 	 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15225 	 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15226 	 * created.  Creating these broadcast IREs will only create confusion
15227 	 * as `addr' will be the same as the IP address.
15228 	 */
15229 	if (subnetmask != 0xFFFFFFFF) {
15230 		addr = ipif->ipif_subnet;
15231 		ire = ire_lookup_bcast(ill, addr, zoneid);
15232 		ASSERT(ire != NULL);
15233 		ire_delete(ire); ire_refrele(ire);
15234 		ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15235 		ASSERT(ire != NULL);
15236 		ire_delete(ire); ire_refrele(ire);
15237 	}
15238 }
15239 
15240 /*
15241  * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15242  * from lifr_flags and the name from lifr_name.
15243  * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15244  * since ipif_lookup_on_name uses the _isv6 flags when matching.
15245  * Returns EINPROGRESS when mp has been consumed by queueing it on
15246  * ipx_pending_mp and the ioctl will complete in ip_rput.
15247  *
15248  * Can operate on either a module or a driver queue.
15249  * Returns an error if not a module queue.
15250  */
15251 /* ARGSUSED */
15252 int
15253 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15254     ip_ioctl_cmd_t *ipip, void *if_req)
15255 {
15256 	ill_t	*ill = q->q_ptr;
15257 	phyint_t *phyi;
15258 	ip_stack_t *ipst;
15259 	struct lifreq *lifr = if_req;
15260 	uint64_t new_flags;
15261 
15262 	ASSERT(ipif != NULL);
15263 	ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15264 
15265 	if (q->q_next == NULL) {
15266 		ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15267 		return (EINVAL);
15268 	}
15269 
15270 	/*
15271 	 * If we are not writer on 'q' then this interface exists already
15272 	 * and previous lookups (ip_extract_lifreq()) found this ipif --
15273 	 * so return EALREADY.
15274 	 */
15275 	if (ill != ipif->ipif_ill)
15276 		return (EALREADY);
15277 
15278 	if (ill->ill_name[0] != '\0')
15279 		return (EALREADY);
15280 
15281 	/*
15282 	 * If there's another ill already with the requested name, ensure
15283 	 * that it's of the same type.  Otherwise, ill_phyint_reinit() will
15284 	 * fuse together two unrelated ills, which will cause chaos.
15285 	 */
15286 	ipst = ill->ill_ipst;
15287 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15288 	    lifr->lifr_name, NULL);
15289 	if (phyi != NULL) {
15290 		ill_t *ill_mate = phyi->phyint_illv4;
15291 
15292 		if (ill_mate == NULL)
15293 			ill_mate = phyi->phyint_illv6;
15294 		ASSERT(ill_mate != NULL);
15295 
15296 		if (ill_mate->ill_media->ip_m_mac_type !=
15297 		    ill->ill_media->ip_m_mac_type) {
15298 			ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15299 			    "use the same ill name on differing media\n"));
15300 			return (EINVAL);
15301 		}
15302 	}
15303 
15304 	/*
15305 	 * We start off as IFF_IPV4 in ipif_allocate and become
15306 	 * IFF_IPV4 or IFF_IPV6 here depending  on lifr_flags value.
15307 	 * The only flags that we read from user space are IFF_IPV4,
15308 	 * IFF_IPV6, and IFF_BROADCAST.
15309 	 *
15310 	 * This ill has not been inserted into the global list.
15311 	 * So we are still single threaded and don't need any lock
15312 	 *
15313 	 * Saniy check the flags.
15314 	 */
15315 
15316 	if ((lifr->lifr_flags & IFF_BROADCAST) &&
15317 	    ((lifr->lifr_flags & IFF_IPV6) ||
15318 	    (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15319 		ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15320 		    "or IPv6 i.e., no broadcast \n"));
15321 		return (EINVAL);
15322 	}
15323 
15324 	new_flags =
15325 	    lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15326 
15327 	if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15328 		ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15329 		    "IFF_IPV4 or IFF_IPV6\n"));
15330 		return (EINVAL);
15331 	}
15332 
15333 	/*
15334 	 * We always start off as IPv4, so only need to check for IPv6.
15335 	 */
15336 	if ((new_flags & IFF_IPV6) != 0) {
15337 		ill->ill_flags |= ILLF_IPV6;
15338 		ill->ill_flags &= ~ILLF_IPV4;
15339 	}
15340 
15341 	if ((new_flags & IFF_BROADCAST) != 0)
15342 		ipif->ipif_flags |= IPIF_BROADCAST;
15343 	else
15344 		ipif->ipif_flags &= ~IPIF_BROADCAST;
15345 
15346 	/* We started off as V4. */
15347 	if (ill->ill_flags & ILLF_IPV6) {
15348 		ill->ill_phyint->phyint_illv6 = ill;
15349 		ill->ill_phyint->phyint_illv4 = NULL;
15350 	}
15351 
15352 	return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15353 }
15354 
15355 /* ARGSUSED */
15356 int
15357 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15358     ip_ioctl_cmd_t *ipip, void *if_req)
15359 {
15360 	/*
15361 	 * ill_phyint_reinit merged the v4 and v6 into a single
15362 	 * ipsq.  We might not have been able to complete the
15363 	 * slifname in ipif_set_values, if we could not become
15364 	 * exclusive.  If so restart it here
15365 	 */
15366 	return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15367 }
15368 
15369 /*
15370  * Return a pointer to the ipif which matches the index, IP version type and
15371  * zoneid.
15372  */
15373 ipif_t *
15374 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15375     ip_stack_t *ipst)
15376 {
15377 	ill_t	*ill;
15378 	ipif_t	*ipif = NULL;
15379 
15380 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
15381 	if (ill != NULL) {
15382 		mutex_enter(&ill->ill_lock);
15383 		for (ipif = ill->ill_ipif; ipif != NULL;
15384 		    ipif = ipif->ipif_next) {
15385 			if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15386 			    zoneid == ipif->ipif_zoneid ||
15387 			    ipif->ipif_zoneid == ALL_ZONES)) {
15388 				ipif_refhold_locked(ipif);
15389 				break;
15390 			}
15391 		}
15392 		mutex_exit(&ill->ill_lock);
15393 		ill_refrele(ill);
15394 	}
15395 	return (ipif);
15396 }
15397 
15398 /*
15399  * Change an existing physical interface's index. If the new index
15400  * is acceptable we update the index and the phyint_list_avl_by_index tree.
15401  * Finally, we update other systems which may have a dependence on the
15402  * index value.
15403  */
15404 /* ARGSUSED */
15405 int
15406 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15407     ip_ioctl_cmd_t *ipip, void *ifreq)
15408 {
15409 	ill_t		*ill;
15410 	phyint_t	*phyi;
15411 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15412 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15413 	uint_t	old_index, index;
15414 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15415 	avl_index_t	where;
15416 
15417 	if (ipip->ipi_cmd_type == IF_CMD)
15418 		index = ifr->ifr_index;
15419 	else
15420 		index = lifr->lifr_index;
15421 
15422 	/*
15423 	 * Only allow on physical interface. Also, index zero is illegal.
15424 	 */
15425 	ill = ipif->ipif_ill;
15426 	phyi = ill->ill_phyint;
15427 	if (ipif->ipif_id != 0 || index == 0) {
15428 		return (EINVAL);
15429 	}
15430 
15431 	/* If the index is not changing, no work to do */
15432 	if (phyi->phyint_ifindex == index)
15433 		return (0);
15434 
15435 	/*
15436 	 * Use phyint_exists() to determine if the new interface index
15437 	 * is already in use. If the index is unused then we need to
15438 	 * change the phyint's position in the phyint_list_avl_by_index
15439 	 * tree. If we do not do this, subsequent lookups (using the new
15440 	 * index value) will not find the phyint.
15441 	 */
15442 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15443 	if (phyint_exists(index, ipst)) {
15444 		rw_exit(&ipst->ips_ill_g_lock);
15445 		return (EEXIST);
15446 	}
15447 
15448 	/*
15449 	 * The new index is unused. Set it in the phyint. However we must not
15450 	 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15451 	 * changes. The event must be bound to old ifindex value.
15452 	 */
15453 	ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15454 	    &index, sizeof (index));
15455 
15456 	old_index = phyi->phyint_ifindex;
15457 	phyi->phyint_ifindex = index;
15458 
15459 	avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15460 	(void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15461 	    &index, &where);
15462 	avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15463 	    phyi, where);
15464 	rw_exit(&ipst->ips_ill_g_lock);
15465 
15466 	/* Update SCTP's ILL list */
15467 	sctp_ill_reindex(ill, old_index);
15468 
15469 	/* Send the routing sockets message */
15470 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
15471 	if (ILL_OTHER(ill))
15472 		ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
15473 
15474 	/* Perhaps ilgs should use this ill */
15475 	update_conn_ill(NULL, ill->ill_ipst);
15476 	return (0);
15477 }
15478 
15479 /* ARGSUSED */
15480 int
15481 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15482     ip_ioctl_cmd_t *ipip, void *ifreq)
15483 {
15484 	struct ifreq	*ifr = (struct ifreq *)ifreq;
15485 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15486 
15487 	ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
15488 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15489 	/* Get the interface index */
15490 	if (ipip->ipi_cmd_type == IF_CMD) {
15491 		ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15492 	} else {
15493 		lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
15494 	}
15495 	return (0);
15496 }
15497 
15498 /* ARGSUSED */
15499 int
15500 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15501     ip_ioctl_cmd_t *ipip, void *ifreq)
15502 {
15503 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15504 
15505 	ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
15506 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15507 	/* Get the interface zone */
15508 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15509 	lifr->lifr_zoneid = ipif->ipif_zoneid;
15510 	return (0);
15511 }
15512 
15513 /*
15514  * Set the zoneid of an interface.
15515  */
15516 /* ARGSUSED */
15517 int
15518 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15519     ip_ioctl_cmd_t *ipip, void *ifreq)
15520 {
15521 	struct lifreq	*lifr = (struct lifreq *)ifreq;
15522 	int err = 0;
15523 	boolean_t need_up = B_FALSE;
15524 	zone_t *zptr;
15525 	zone_status_t status;
15526 	zoneid_t zoneid;
15527 
15528 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15529 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
15530 		if (!is_system_labeled())
15531 			return (ENOTSUP);
15532 		zoneid = GLOBAL_ZONEID;
15533 	}
15534 
15535 	/* cannot assign instance zero to a non-global zone */
15536 	if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
15537 		return (ENOTSUP);
15538 
15539 	/*
15540 	 * Cannot assign to a zone that doesn't exist or is shutting down.  In
15541 	 * the event of a race with the zone shutdown processing, since IP
15542 	 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
15543 	 * interface will be cleaned up even if the zone is shut down
15544 	 * immediately after the status check. If the interface can't be brought
15545 	 * down right away, and the zone is shut down before the restart
15546 	 * function is called, we resolve the possible races by rechecking the
15547 	 * zone status in the restart function.
15548 	 */
15549 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
15550 		return (EINVAL);
15551 	status = zone_status_get(zptr);
15552 	zone_rele(zptr);
15553 
15554 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
15555 		return (EINVAL);
15556 
15557 	if (ipif->ipif_flags & IPIF_UP) {
15558 		/*
15559 		 * If the interface is already marked up,
15560 		 * we call ipif_down which will take care
15561 		 * of ditching any IREs that have been set
15562 		 * up based on the old interface address.
15563 		 */
15564 		err = ipif_logical_down(ipif, q, mp);
15565 		if (err == EINPROGRESS)
15566 			return (err);
15567 		(void) ipif_down_tail(ipif);
15568 		need_up = B_TRUE;
15569 	}
15570 
15571 	err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
15572 	return (err);
15573 }
15574 
15575 static int
15576 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
15577     queue_t *q, mblk_t *mp, boolean_t need_up)
15578 {
15579 	int	err = 0;
15580 	ip_stack_t	*ipst;
15581 
15582 	ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
15583 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15584 
15585 	if (CONN_Q(q))
15586 		ipst = CONNQ_TO_IPST(q);
15587 	else
15588 		ipst = ILLQ_TO_IPST(q);
15589 
15590 	/*
15591 	 * For exclusive stacks we don't allow a different zoneid than
15592 	 * global.
15593 	 */
15594 	if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
15595 	    zoneid != GLOBAL_ZONEID)
15596 		return (EINVAL);
15597 
15598 	/* Set the new zone id. */
15599 	ipif->ipif_zoneid = zoneid;
15600 
15601 	/* Update sctp list */
15602 	sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
15603 
15604 	/* The default multicast interface might have changed */
15605 	ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
15606 
15607 	if (need_up) {
15608 		/*
15609 		 * Now bring the interface back up.  If this
15610 		 * is the only IPIF for the ILL, ipif_up
15611 		 * will have to re-bind to the device, so
15612 		 * we may get back EINPROGRESS, in which
15613 		 * case, this IOCTL will get completed in
15614 		 * ip_rput_dlpi when we see the DL_BIND_ACK.
15615 		 */
15616 		err = ipif_up(ipif, q, mp);
15617 	}
15618 	return (err);
15619 }
15620 
15621 /* ARGSUSED */
15622 int
15623 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15624     ip_ioctl_cmd_t *ipip, void *if_req)
15625 {
15626 	struct lifreq *lifr = (struct lifreq *)if_req;
15627 	zoneid_t zoneid;
15628 	zone_t *zptr;
15629 	zone_status_t status;
15630 
15631 	ASSERT(ipip->ipi_cmd_type == LIF_CMD);
15632 	if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
15633 		zoneid = GLOBAL_ZONEID;
15634 
15635 	ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
15636 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15637 
15638 	/*
15639 	 * We recheck the zone status to resolve the following race condition:
15640 	 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
15641 	 * 2) hme0:1 is up and can't be brought down right away;
15642 	 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
15643 	 * 3) zone "myzone" is halted; the zone status switches to
15644 	 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
15645 	 * the interfaces to remove - hme0:1 is not returned because it's not
15646 	 * yet in "myzone", so it won't be removed;
15647 	 * 4) the restart function for SIOCSLIFZONE is called; without the
15648 	 * status check here, we would have hme0:1 in "myzone" after it's been
15649 	 * destroyed.
15650 	 * Note that if the status check fails, we need to bring the interface
15651 	 * back to its state prior to ip_sioctl_slifzone(), hence the call to
15652 	 * ipif_up_done[_v6]().
15653 	 */
15654 	status = ZONE_IS_UNINITIALIZED;
15655 	if ((zptr = zone_find_by_id(zoneid)) != NULL) {
15656 		status = zone_status_get(zptr);
15657 		zone_rele(zptr);
15658 	}
15659 	if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
15660 		if (ipif->ipif_isv6) {
15661 			(void) ipif_up_done_v6(ipif);
15662 		} else {
15663 			(void) ipif_up_done(ipif);
15664 		}
15665 		return (EINVAL);
15666 	}
15667 
15668 	(void) ipif_down_tail(ipif);
15669 
15670 	return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
15671 	    B_TRUE));
15672 }
15673 
15674 /*
15675  * Return the number of addresses on `ill' with one or more of the values
15676  * in `set' set and all of the values in `clear' clear.
15677  */
15678 static uint_t
15679 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
15680 {
15681 	ipif_t	*ipif;
15682 	uint_t	cnt = 0;
15683 
15684 	ASSERT(IAM_WRITER_ILL(ill));
15685 
15686 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
15687 		if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
15688 			cnt++;
15689 
15690 	return (cnt);
15691 }
15692 
15693 /*
15694  * Return the number of migratable addresses on `ill' that are under
15695  * application control.
15696  */
15697 uint_t
15698 ill_appaddr_cnt(const ill_t *ill)
15699 {
15700 	return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
15701 	    IPIF_NOFAILOVER));
15702 }
15703 
15704 /*
15705  * Return the number of point-to-point addresses on `ill'.
15706  */
15707 uint_t
15708 ill_ptpaddr_cnt(const ill_t *ill)
15709 {
15710 	return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
15711 }
15712 
15713 /* ARGSUSED */
15714 int
15715 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15716 	ip_ioctl_cmd_t *ipip, void *ifreq)
15717 {
15718 	struct lifreq	*lifr = ifreq;
15719 
15720 	ASSERT(q->q_next == NULL);
15721 	ASSERT(CONN_Q(q));
15722 
15723 	ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
15724 	    ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
15725 	lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
15726 	ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
15727 
15728 	return (0);
15729 }
15730 
15731 /* Find the previous ILL in this usesrc group */
15732 static ill_t *
15733 ill_prev_usesrc(ill_t *uill)
15734 {
15735 	ill_t *ill;
15736 
15737 	for (ill = uill->ill_usesrc_grp_next;
15738 	    ASSERT(ill), ill->ill_usesrc_grp_next != uill;
15739 	    ill = ill->ill_usesrc_grp_next)
15740 		/* do nothing */;
15741 	return (ill);
15742 }
15743 
15744 /*
15745  * Release all members of the usesrc group. This routine is called
15746  * from ill_delete when the interface being unplumbed is the
15747  * group head.
15748  *
15749  * This silently clears the usesrc that ifconfig setup.
15750  * An alternative would be to keep that ifindex, and drop packets on the floor
15751  * since no source address can be selected.
15752  * Even if we keep the current semantics, don't need a lock and a linked list.
15753  * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
15754  * the one that is being removed. Issue is how we return the usesrc users
15755  * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
15756  * ill_usesrc_ifindex matching a target ill. We could also do that with an
15757  * ill walk, but the walker would need to insert in the ioctl response.
15758  */
15759 static void
15760 ill_disband_usesrc_group(ill_t *uill)
15761 {
15762 	ill_t *next_ill, *tmp_ill;
15763 	ip_stack_t	*ipst = uill->ill_ipst;
15764 
15765 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15766 	next_ill = uill->ill_usesrc_grp_next;
15767 
15768 	do {
15769 		ASSERT(next_ill != NULL);
15770 		tmp_ill = next_ill->ill_usesrc_grp_next;
15771 		ASSERT(tmp_ill != NULL);
15772 		next_ill->ill_usesrc_grp_next = NULL;
15773 		next_ill->ill_usesrc_ifindex = 0;
15774 		next_ill = tmp_ill;
15775 	} while (next_ill->ill_usesrc_ifindex != 0);
15776 	uill->ill_usesrc_grp_next = NULL;
15777 }
15778 
15779 /*
15780  * Remove the client usesrc ILL from the list and relink to a new list
15781  */
15782 int
15783 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
15784 {
15785 	ill_t *ill, *tmp_ill;
15786 	ip_stack_t	*ipst = ucill->ill_ipst;
15787 
15788 	ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
15789 	    (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
15790 
15791 	/*
15792 	 * Check if the usesrc client ILL passed in is not already
15793 	 * in use as a usesrc ILL i.e one whose source address is
15794 	 * in use OR a usesrc ILL is not already in use as a usesrc
15795 	 * client ILL
15796 	 */
15797 	if ((ucill->ill_usesrc_ifindex == 0) ||
15798 	    (uill->ill_usesrc_ifindex != 0)) {
15799 		return (-1);
15800 	}
15801 
15802 	ill = ill_prev_usesrc(ucill);
15803 	ASSERT(ill->ill_usesrc_grp_next != NULL);
15804 
15805 	/* Remove from the current list */
15806 	if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
15807 		/* Only two elements in the list */
15808 		ASSERT(ill->ill_usesrc_ifindex == 0);
15809 		ill->ill_usesrc_grp_next = NULL;
15810 	} else {
15811 		ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
15812 	}
15813 
15814 	if (ifindex == 0) {
15815 		ucill->ill_usesrc_ifindex = 0;
15816 		ucill->ill_usesrc_grp_next = NULL;
15817 		return (0);
15818 	}
15819 
15820 	ucill->ill_usesrc_ifindex = ifindex;
15821 	tmp_ill = uill->ill_usesrc_grp_next;
15822 	uill->ill_usesrc_grp_next = ucill;
15823 	ucill->ill_usesrc_grp_next =
15824 	    (tmp_ill != NULL) ? tmp_ill : uill;
15825 	return (0);
15826 }
15827 
15828 /*
15829  * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
15830  * ip.c for locking details.
15831  */
15832 /* ARGSUSED */
15833 int
15834 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15835     ip_ioctl_cmd_t *ipip, void *ifreq)
15836 {
15837 	struct lifreq *lifr = (struct lifreq *)ifreq;
15838 	boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
15839 	ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
15840 	int err = 0, ret;
15841 	uint_t ifindex;
15842 	ipsq_t *ipsq = NULL;
15843 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
15844 
15845 	ASSERT(IAM_WRITER_IPIF(ipif));
15846 	ASSERT(q->q_next == NULL);
15847 	ASSERT(CONN_Q(q));
15848 
15849 	isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
15850 
15851 	ifindex = lifr->lifr_index;
15852 	if (ifindex == 0) {
15853 		if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
15854 			/* non usesrc group interface, nothing to reset */
15855 			return (0);
15856 		}
15857 		ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
15858 		/* valid reset request */
15859 		reset_flg = B_TRUE;
15860 	}
15861 
15862 	usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15863 	if (usesrc_ill == NULL) {
15864 		return (ENXIO);
15865 	}
15866 
15867 	ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
15868 	    NEW_OP, B_TRUE);
15869 	if (ipsq == NULL) {
15870 		err = EINPROGRESS;
15871 		/* Operation enqueued on the ipsq of the usesrc ILL */
15872 		goto done;
15873 	}
15874 
15875 	/* USESRC isn't currently supported with IPMP */
15876 	if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
15877 		err = ENOTSUP;
15878 		goto done;
15879 	}
15880 
15881 	/*
15882 	 * USESRC isn't compatible with the STANDBY flag.  (STANDBY is only
15883 	 * used by IPMP underlying interfaces, but someone might think it's
15884 	 * more general and try to use it independently with VNI.)
15885 	 */
15886 	if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
15887 		err = ENOTSUP;
15888 		goto done;
15889 	}
15890 
15891 	/*
15892 	 * If the client is already in use as a usesrc_ill or a usesrc_ill is
15893 	 * already a client then return EINVAL
15894 	 */
15895 	if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
15896 		err = EINVAL;
15897 		goto done;
15898 	}
15899 
15900 	/*
15901 	 * If the ill_usesrc_ifindex field is already set to what it needs to
15902 	 * be then this is a duplicate operation.
15903 	 */
15904 	if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
15905 		err = 0;
15906 		goto done;
15907 	}
15908 
15909 	ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
15910 	    " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
15911 	    usesrc_ill->ill_isv6));
15912 
15913 	/*
15914 	 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
15915 	 * and the ill_usesrc_ifindex fields
15916 	 */
15917 	rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
15918 
15919 	if (reset_flg) {
15920 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
15921 		if (ret != 0) {
15922 			err = EINVAL;
15923 		}
15924 		rw_exit(&ipst->ips_ill_g_usesrc_lock);
15925 		goto done;
15926 	}
15927 
15928 	/*
15929 	 * Four possibilities to consider:
15930 	 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
15931 	 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
15932 	 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
15933 	 * 4. Both are part of their respective usesrc groups
15934 	 */
15935 	if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
15936 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15937 		ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
15938 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15939 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15940 		usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
15941 	} else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
15942 	    (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
15943 		usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
15944 		/* Insert at head of list */
15945 		usesrc_cli_ill->ill_usesrc_grp_next =
15946 		    usesrc_ill->ill_usesrc_grp_next;
15947 		usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
15948 	} else {
15949 		ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
15950 		    ifindex);
15951 		if (ret != 0)
15952 			err = EINVAL;
15953 	}
15954 	rw_exit(&ipst->ips_ill_g_usesrc_lock);
15955 
15956 done:
15957 	if (ipsq != NULL)
15958 		ipsq_exit(ipsq);
15959 	/* The refrele on the lifr_name ipif is done by ip_process_ioctl */
15960 	ill_refrele(usesrc_ill);
15961 
15962 	/* Let conn_ixa caching know that source address selection changed */
15963 	ip_update_source_selection(ipst);
15964 
15965 	return (err);
15966 }
15967 
15968 /*
15969  * comparison function used by avl.
15970  */
15971 static int
15972 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
15973 {
15974 
15975 	uint_t index;
15976 
15977 	ASSERT(phyip != NULL && index_ptr != NULL);
15978 
15979 	index = *((uint_t *)index_ptr);
15980 	/*
15981 	 * let the phyint with the lowest index be on top.
15982 	 */
15983 	if (((phyint_t *)phyip)->phyint_ifindex < index)
15984 		return (1);
15985 	if (((phyint_t *)phyip)->phyint_ifindex > index)
15986 		return (-1);
15987 	return (0);
15988 }
15989 
15990 /*
15991  * comparison function used by avl.
15992  */
15993 static int
15994 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
15995 {
15996 	ill_t *ill;
15997 	int res = 0;
15998 
15999 	ASSERT(phyip != NULL && name_ptr != NULL);
16000 
16001 	if (((phyint_t *)phyip)->phyint_illv4)
16002 		ill = ((phyint_t *)phyip)->phyint_illv4;
16003 	else
16004 		ill = ((phyint_t *)phyip)->phyint_illv6;
16005 	ASSERT(ill != NULL);
16006 
16007 	res = strcmp(ill->ill_name, (char *)name_ptr);
16008 	if (res > 0)
16009 		return (1);
16010 	else if (res < 0)
16011 		return (-1);
16012 	return (0);
16013 }
16014 
16015 /*
16016  * This function is called on the unplumb path via ill_glist_delete() when
16017  * there are no ills left on the phyint and thus the phyint can be freed.
16018  */
16019 static void
16020 phyint_free(phyint_t *phyi)
16021 {
16022 	ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16023 
16024 	ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16025 
16026 	/*
16027 	 * If this phyint was an IPMP meta-interface, blow away the group.
16028 	 * This is safe to do because all of the illgrps have already been
16029 	 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16030 	 * If we're cleaning up as a result of failed initialization,
16031 	 * phyint_grp may be NULL.
16032 	 */
16033 	if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16034 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16035 		ipmp_grp_destroy(phyi->phyint_grp);
16036 		phyi->phyint_grp = NULL;
16037 		rw_exit(&ipst->ips_ipmp_lock);
16038 	}
16039 
16040 	/*
16041 	 * If this interface was under IPMP, take it out of the group.
16042 	 */
16043 	if (phyi->phyint_grp != NULL)
16044 		ipmp_phyint_leave_grp(phyi);
16045 
16046 	/*
16047 	 * Delete the phyint and disassociate its ipsq.  The ipsq itself
16048 	 * will be freed in ipsq_exit().
16049 	 */
16050 	phyi->phyint_ipsq->ipsq_phyint = NULL;
16051 	phyi->phyint_name[0] = '\0';
16052 
16053 	mi_free(phyi);
16054 }
16055 
16056 /*
16057  * Attach the ill to the phyint structure which can be shared by both
16058  * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16059  * function is called from ipif_set_values and ill_lookup_on_name (for
16060  * loopback) where we know the name of the ill. We lookup the ill and if
16061  * there is one present already with the name use that phyint. Otherwise
16062  * reuse the one allocated by ill_init.
16063  */
16064 static void
16065 ill_phyint_reinit(ill_t *ill)
16066 {
16067 	boolean_t isv6 = ill->ill_isv6;
16068 	phyint_t *phyi_old;
16069 	phyint_t *phyi;
16070 	avl_index_t where = 0;
16071 	ill_t	*ill_other = NULL;
16072 	ip_stack_t	*ipst = ill->ill_ipst;
16073 
16074 	ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16075 
16076 	phyi_old = ill->ill_phyint;
16077 	ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16078 	    phyi_old->phyint_illv6 == NULL));
16079 	ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16080 	    phyi_old->phyint_illv4 == NULL));
16081 	ASSERT(phyi_old->phyint_ifindex == 0);
16082 
16083 	/*
16084 	 * Now that our ill has a name, set it in the phyint.
16085 	 */
16086 	(void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16087 
16088 	phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16089 	    ill->ill_name, &where);
16090 
16091 	/*
16092 	 * 1. We grabbed the ill_g_lock before inserting this ill into
16093 	 *    the global list of ills. So no other thread could have located
16094 	 *    this ill and hence the ipsq of this ill is guaranteed to be empty.
16095 	 * 2. Now locate the other protocol instance of this ill.
16096 	 * 3. Now grab both ill locks in the right order, and the phyint lock of
16097 	 *    the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16098 	 *    of neither ill can change.
16099 	 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16100 	 *    other ill.
16101 	 * 5. Release all locks.
16102 	 */
16103 
16104 	/*
16105 	 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16106 	 * we are initializing IPv4.
16107 	 */
16108 	if (phyi != NULL) {
16109 		ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16110 		ASSERT(ill_other->ill_phyint != NULL);
16111 		ASSERT((isv6 && !ill_other->ill_isv6) ||
16112 		    (!isv6 && ill_other->ill_isv6));
16113 		GRAB_ILL_LOCKS(ill, ill_other);
16114 		/*
16115 		 * We are potentially throwing away phyint_flags which
16116 		 * could be different from the one that we obtain from
16117 		 * ill_other->ill_phyint. But it is okay as we are assuming
16118 		 * that the state maintained within IP is correct.
16119 		 */
16120 		mutex_enter(&phyi->phyint_lock);
16121 		if (isv6) {
16122 			ASSERT(phyi->phyint_illv6 == NULL);
16123 			phyi->phyint_illv6 = ill;
16124 		} else {
16125 			ASSERT(phyi->phyint_illv4 == NULL);
16126 			phyi->phyint_illv4 = ill;
16127 		}
16128 
16129 		/*
16130 		 * Delete the old phyint and make its ipsq eligible
16131 		 * to be freed in ipsq_exit().
16132 		 */
16133 		phyi_old->phyint_illv4 = NULL;
16134 		phyi_old->phyint_illv6 = NULL;
16135 		phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16136 		phyi_old->phyint_name[0] = '\0';
16137 		mi_free(phyi_old);
16138 	} else {
16139 		mutex_enter(&ill->ill_lock);
16140 		/*
16141 		 * We don't need to acquire any lock, since
16142 		 * the ill is not yet visible globally  and we
16143 		 * have not yet released the ill_g_lock.
16144 		 */
16145 		phyi = phyi_old;
16146 		mutex_enter(&phyi->phyint_lock);
16147 		/* XXX We need a recovery strategy here. */
16148 		if (!phyint_assign_ifindex(phyi, ipst))
16149 			cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16150 
16151 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16152 		    (void *)phyi, where);
16153 
16154 		(void) avl_find(&ipst->ips_phyint_g_list->
16155 		    phyint_list_avl_by_index,
16156 		    &phyi->phyint_ifindex, &where);
16157 		avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16158 		    (void *)phyi, where);
16159 	}
16160 
16161 	/*
16162 	 * Reassigning ill_phyint automatically reassigns the ipsq also.
16163 	 * pending mp is not affected because that is per ill basis.
16164 	 */
16165 	ill->ill_phyint = phyi;
16166 
16167 	/*
16168 	 * Now that the phyint's ifindex has been assigned, complete the
16169 	 * remaining
16170 	 */
16171 	ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16172 	if (ill->ill_isv6) {
16173 		ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16174 		    ill->ill_phyint->phyint_ifindex;
16175 		ill->ill_mcast_type = ipst->ips_mld_max_version;
16176 	} else {
16177 		ill->ill_mcast_type = ipst->ips_igmp_max_version;
16178 	}
16179 
16180 	/*
16181 	 * Generate an event within the hooks framework to indicate that
16182 	 * a new interface has just been added to IP.  For this event to
16183 	 * be generated, the network interface must, at least, have an
16184 	 * ifindex assigned to it.  (We don't generate the event for
16185 	 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16186 	 *
16187 	 * This needs to be run inside the ill_g_lock perimeter to ensure
16188 	 * that the ordering of delivered events to listeners matches the
16189 	 * order of them in the kernel.
16190 	 */
16191 	if (!IS_LOOPBACK(ill)) {
16192 		ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16193 		    ill->ill_name_length);
16194 	}
16195 	RELEASE_ILL_LOCKS(ill, ill_other);
16196 	mutex_exit(&phyi->phyint_lock);
16197 }
16198 
16199 /*
16200  * Notify any downstream modules of the name of this interface.
16201  * An M_IOCTL is used even though we don't expect a successful reply.
16202  * Any reply message from the driver (presumably an M_IOCNAK) will
16203  * eventually get discarded somewhere upstream.  The message format is
16204  * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16205  * to IP.
16206  */
16207 static void
16208 ip_ifname_notify(ill_t *ill, queue_t *q)
16209 {
16210 	mblk_t *mp1, *mp2;
16211 	struct iocblk *iocp;
16212 	struct lifreq *lifr;
16213 
16214 	mp1 = mkiocb(SIOCSLIFNAME);
16215 	if (mp1 == NULL)
16216 		return;
16217 	mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16218 	if (mp2 == NULL) {
16219 		freeb(mp1);
16220 		return;
16221 	}
16222 
16223 	mp1->b_cont = mp2;
16224 	iocp = (struct iocblk *)mp1->b_rptr;
16225 	iocp->ioc_count = sizeof (struct lifreq);
16226 
16227 	lifr = (struct lifreq *)mp2->b_rptr;
16228 	mp2->b_wptr += sizeof (struct lifreq);
16229 	bzero(lifr, sizeof (struct lifreq));
16230 
16231 	(void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16232 	lifr->lifr_ppa = ill->ill_ppa;
16233 	lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16234 
16235 	DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16236 	    char *, "SIOCSLIFNAME", ill_t *, ill);
16237 	putnext(q, mp1);
16238 }
16239 
16240 static int
16241 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16242 {
16243 	int		err;
16244 	ip_stack_t	*ipst = ill->ill_ipst;
16245 	phyint_t	*phyi = ill->ill_phyint;
16246 
16247 	/* Set the obsolete NDD per-interface forwarding name. */
16248 	err = ill_set_ndd_name(ill);
16249 	if (err != 0) {
16250 		cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n",
16251 		    err);
16252 	}
16253 
16254 	/*
16255 	 * Now that ill_name is set, the configuration for the IPMP
16256 	 * meta-interface can be performed.
16257 	 */
16258 	if (IS_IPMP(ill)) {
16259 		rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16260 		/*
16261 		 * If phyi->phyint_grp is NULL, then this is the first IPMP
16262 		 * meta-interface and we need to create the IPMP group.
16263 		 */
16264 		if (phyi->phyint_grp == NULL) {
16265 			/*
16266 			 * If someone has renamed another IPMP group to have
16267 			 * the same name as our interface, bail.
16268 			 */
16269 			if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16270 				rw_exit(&ipst->ips_ipmp_lock);
16271 				return (EEXIST);
16272 			}
16273 			phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16274 			if (phyi->phyint_grp == NULL) {
16275 				rw_exit(&ipst->ips_ipmp_lock);
16276 				return (ENOMEM);
16277 			}
16278 		}
16279 		rw_exit(&ipst->ips_ipmp_lock);
16280 	}
16281 
16282 	/* Tell downstream modules where they are. */
16283 	ip_ifname_notify(ill, q);
16284 
16285 	/*
16286 	 * ill_dl_phys returns EINPROGRESS in the usual case.
16287 	 * Error cases are ENOMEM ...
16288 	 */
16289 	err = ill_dl_phys(ill, ipif, mp, q);
16290 
16291 	if (ill->ill_isv6) {
16292 		mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16293 		if (ipst->ips_mld_slowtimeout_id == 0) {
16294 			ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16295 			    (void *)ipst,
16296 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16297 		}
16298 		mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16299 	} else {
16300 		mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16301 		if (ipst->ips_igmp_slowtimeout_id == 0) {
16302 			ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16303 			    (void *)ipst,
16304 			    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16305 		}
16306 		mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16307 	}
16308 
16309 	return (err);
16310 }
16311 
16312 /*
16313  * Common routine for ppa and ifname setting. Should be called exclusive.
16314  *
16315  * Returns EINPROGRESS when mp has been consumed by queueing it on
16316  * ipx_pending_mp and the ioctl will complete in ip_rput.
16317  *
16318  * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16319  * the new name and new ppa in lifr_name and lifr_ppa respectively.
16320  * For SLIFNAME, we pass these values back to the userland.
16321  */
16322 static int
16323 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16324 {
16325 	ill_t	*ill;
16326 	ipif_t	*ipif;
16327 	ipsq_t	*ipsq;
16328 	char	*ppa_ptr;
16329 	char	*old_ptr;
16330 	char	old_char;
16331 	int	error;
16332 	ip_stack_t	*ipst;
16333 
16334 	ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16335 	ASSERT(q->q_next != NULL);
16336 	ASSERT(interf_name != NULL);
16337 
16338 	ill = (ill_t *)q->q_ptr;
16339 	ipst = ill->ill_ipst;
16340 
16341 	ASSERT(ill->ill_ipst != NULL);
16342 	ASSERT(ill->ill_name[0] == '\0');
16343 	ASSERT(IAM_WRITER_ILL(ill));
16344 	ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16345 	ASSERT(ill->ill_ppa == UINT_MAX);
16346 
16347 	ill->ill_defend_start = ill->ill_defend_count = 0;
16348 	/* The ppa is sent down by ifconfig or is chosen */
16349 	if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16350 		return (EINVAL);
16351 	}
16352 
16353 	/*
16354 	 * make sure ppa passed in is same as ppa in the name.
16355 	 * This check is not made when ppa == UINT_MAX in that case ppa
16356 	 * in the name could be anything. System will choose a ppa and
16357 	 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16358 	 */
16359 	if (*new_ppa_ptr != UINT_MAX) {
16360 		/* stoi changes the pointer */
16361 		old_ptr = ppa_ptr;
16362 		/*
16363 		 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16364 		 * (they don't have an externally visible ppa).  We assign one
16365 		 * here so that we can manage the interface.  Note that in
16366 		 * the past this value was always 0 for DLPI 1 drivers.
16367 		 */
16368 		if (*new_ppa_ptr == 0)
16369 			*new_ppa_ptr = stoi(&old_ptr);
16370 		else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16371 			return (EINVAL);
16372 	}
16373 	/*
16374 	 * terminate string before ppa
16375 	 * save char at that location.
16376 	 */
16377 	old_char = ppa_ptr[0];
16378 	ppa_ptr[0] = '\0';
16379 
16380 	ill->ill_ppa = *new_ppa_ptr;
16381 	/*
16382 	 * Finish as much work now as possible before calling ill_glist_insert
16383 	 * which makes the ill globally visible and also merges it with the
16384 	 * other protocol instance of this phyint. The remaining work is
16385 	 * done after entering the ipsq which may happen sometime later.
16386 	 * ill_set_ndd_name occurs after the ill has been made globally visible.
16387 	 */
16388 	ipif = ill->ill_ipif;
16389 
16390 	/* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16391 	ipif_assign_seqid(ipif);
16392 
16393 	if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16394 		ill->ill_flags |= ILLF_IPV4;
16395 
16396 	ASSERT(ipif->ipif_next == NULL);	/* Only one ipif on ill */
16397 	ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16398 
16399 	if (ill->ill_flags & ILLF_IPV6) {
16400 
16401 		ill->ill_isv6 = B_TRUE;
16402 		ill_set_inputfn(ill);
16403 		if (ill->ill_rq != NULL) {
16404 			ill->ill_rq->q_qinfo = &iprinitv6;
16405 		}
16406 
16407 		/* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16408 		ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16409 		ipif->ipif_v6subnet = ipv6_all_zeros;
16410 		ipif->ipif_v6net_mask = ipv6_all_zeros;
16411 		ipif->ipif_v6brd_addr = ipv6_all_zeros;
16412 		ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16413 		ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16414 		/*
16415 		 * point-to-point or Non-mulicast capable
16416 		 * interfaces won't do NUD unless explicitly
16417 		 * configured to do so.
16418 		 */
16419 		if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16420 		    !(ill->ill_flags & ILLF_MULTICAST)) {
16421 			ill->ill_flags |= ILLF_NONUD;
16422 		}
16423 		/* Make sure IPv4 specific flag is not set on IPv6 if */
16424 		if (ill->ill_flags & ILLF_NOARP) {
16425 			/*
16426 			 * Note: xresolv interfaces will eventually need
16427 			 * NOARP set here as well, but that will require
16428 			 * those external resolvers to have some
16429 			 * knowledge of that flag and act appropriately.
16430 			 * Not to be changed at present.
16431 			 */
16432 			ill->ill_flags &= ~ILLF_NOARP;
16433 		}
16434 		/*
16435 		 * Set the ILLF_ROUTER flag according to the global
16436 		 * IPv6 forwarding policy.
16437 		 */
16438 		if (ipst->ips_ipv6_forward != 0)
16439 			ill->ill_flags |= ILLF_ROUTER;
16440 	} else if (ill->ill_flags & ILLF_IPV4) {
16441 		ill->ill_isv6 = B_FALSE;
16442 		ill_set_inputfn(ill);
16443 		ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16444 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16445 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16446 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16447 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16448 		IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
16449 		/*
16450 		 * Set the ILLF_ROUTER flag according to the global
16451 		 * IPv4 forwarding policy.
16452 		 */
16453 		if (ipst->ips_ip_g_forward != 0)
16454 			ill->ill_flags |= ILLF_ROUTER;
16455 	}
16456 
16457 	ASSERT(ill->ill_phyint != NULL);
16458 
16459 	/*
16460 	 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
16461 	 * be completed in ill_glist_insert -> ill_phyint_reinit
16462 	 */
16463 	if (!ill_allocate_mibs(ill))
16464 		return (ENOMEM);
16465 
16466 	/*
16467 	 * Pick a default sap until we get the DL_INFO_ACK back from
16468 	 * the driver.
16469 	 */
16470 	ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
16471 	    ill->ill_media->ip_m_ipv4sap;
16472 
16473 	ill->ill_ifname_pending = 1;
16474 	ill->ill_ifname_pending_err = 0;
16475 
16476 	/*
16477 	 * When the first ipif comes up in ipif_up_done(), multicast groups
16478 	 * that were joined while this ill was not bound to the DLPI link need
16479 	 * to be recovered by ill_recover_multicast().
16480 	 */
16481 	ill->ill_need_recover_multicast = 1;
16482 
16483 	ill_refhold(ill);
16484 	rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
16485 	if ((error = ill_glist_insert(ill, interf_name,
16486 	    (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
16487 		ill->ill_ppa = UINT_MAX;
16488 		ill->ill_name[0] = '\0';
16489 		/*
16490 		 * undo null termination done above.
16491 		 */
16492 		ppa_ptr[0] = old_char;
16493 		rw_exit(&ipst->ips_ill_g_lock);
16494 		ill_refrele(ill);
16495 		return (error);
16496 	}
16497 
16498 	ASSERT(ill->ill_name_length <= LIFNAMSIZ);
16499 
16500 	/*
16501 	 * When we return the buffer pointed to by interf_name should contain
16502 	 * the same name as in ill_name.
16503 	 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
16504 	 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
16505 	 * so copy full name and update the ppa ptr.
16506 	 * When ppa passed in != UINT_MAX all values are correct just undo
16507 	 * null termination, this saves a bcopy.
16508 	 */
16509 	if (*new_ppa_ptr == UINT_MAX) {
16510 		bcopy(ill->ill_name, interf_name, ill->ill_name_length);
16511 		*new_ppa_ptr = ill->ill_ppa;
16512 	} else {
16513 		/*
16514 		 * undo null termination done above.
16515 		 */
16516 		ppa_ptr[0] = old_char;
16517 	}
16518 
16519 	/* Let SCTP know about this ILL */
16520 	sctp_update_ill(ill, SCTP_ILL_INSERT);
16521 
16522 	/*
16523 	 * ill_glist_insert has made the ill visible globally, and
16524 	 * ill_phyint_reinit could have changed the ipsq. At this point,
16525 	 * we need to hold the ips_ill_g_lock across the call to enter the
16526 	 * ipsq to enforce atomicity and prevent reordering. In the event
16527 	 * the ipsq has changed, and if the new ipsq is currently busy,
16528 	 * we need to make sure that this half-completed ioctl is ahead of
16529 	 * any subsequent ioctl. We achieve this by not dropping the
16530 	 * ips_ill_g_lock which prevents any ill lookup itself thereby
16531 	 * ensuring that new ioctls can't start.
16532 	 */
16533 	ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
16534 	    B_TRUE);
16535 
16536 	rw_exit(&ipst->ips_ill_g_lock);
16537 	ill_refrele(ill);
16538 	if (ipsq == NULL)
16539 		return (EINPROGRESS);
16540 
16541 	/*
16542 	 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
16543 	 */
16544 	if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
16545 		ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
16546 	else
16547 		ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
16548 
16549 	error = ipif_set_values_tail(ill, ipif, mp, q);
16550 	ipsq_exit(ipsq);
16551 	if (error != 0 && error != EINPROGRESS) {
16552 		/*
16553 		 * restore previous values
16554 		 */
16555 		ill->ill_isv6 = B_FALSE;
16556 		ill_set_inputfn(ill);
16557 	}
16558 	return (error);
16559 }
16560 
16561 void
16562 ipif_init(ip_stack_t *ipst)
16563 {
16564 	int i;
16565 
16566 	for (i = 0; i < MAX_G_HEADS; i++) {
16567 		ipst->ips_ill_g_heads[i].ill_g_list_head =
16568 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16569 		ipst->ips_ill_g_heads[i].ill_g_list_tail =
16570 		    (ill_if_t *)&ipst->ips_ill_g_heads[i];
16571 	}
16572 
16573 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16574 	    ill_phyint_compare_index,
16575 	    sizeof (phyint_t),
16576 	    offsetof(struct phyint, phyint_avl_by_index));
16577 	avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16578 	    ill_phyint_compare_name,
16579 	    sizeof (phyint_t),
16580 	    offsetof(struct phyint, phyint_avl_by_name));
16581 }
16582 
16583 /*
16584  * Save enough information so that we can recreate the IRE if
16585  * the interface goes down and then up.
16586  */
16587 void
16588 ill_save_ire(ill_t *ill, ire_t *ire)
16589 {
16590 	mblk_t	*save_mp;
16591 
16592 	save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
16593 	if (save_mp != NULL) {
16594 		ifrt_t	*ifrt;
16595 
16596 		save_mp->b_wptr += sizeof (ifrt_t);
16597 		ifrt = (ifrt_t *)save_mp->b_rptr;
16598 		bzero(ifrt, sizeof (ifrt_t));
16599 		ifrt->ifrt_type = ire->ire_type;
16600 		if (ire->ire_ipversion == IPV4_VERSION) {
16601 			ASSERT(!ill->ill_isv6);
16602 			ifrt->ifrt_addr = ire->ire_addr;
16603 			ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
16604 			ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
16605 			ifrt->ifrt_mask = ire->ire_mask;
16606 		} else {
16607 			ASSERT(ill->ill_isv6);
16608 			ifrt->ifrt_v6addr = ire->ire_addr_v6;
16609 			/* ire_gateway_addr_v6 can change due to RTM_CHANGE */
16610 			mutex_enter(&ire->ire_lock);
16611 			ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
16612 			mutex_exit(&ire->ire_lock);
16613 			ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
16614 			ifrt->ifrt_v6mask = ire->ire_mask_v6;
16615 		}
16616 		ifrt->ifrt_flags = ire->ire_flags;
16617 		ifrt->ifrt_zoneid = ire->ire_zoneid;
16618 		mutex_enter(&ill->ill_saved_ire_lock);
16619 		save_mp->b_cont = ill->ill_saved_ire_mp;
16620 		ill->ill_saved_ire_mp = save_mp;
16621 		ill->ill_saved_ire_cnt++;
16622 		mutex_exit(&ill->ill_saved_ire_lock);
16623 	}
16624 }
16625 
16626 /*
16627  * Remove one entry from ill_saved_ire_mp.
16628  */
16629 void
16630 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
16631 {
16632 	mblk_t	**mpp;
16633 	mblk_t	*mp;
16634 	ifrt_t	*ifrt;
16635 
16636 	/* Remove from ill_saved_ire_mp list if it is there */
16637 	mutex_enter(&ill->ill_saved_ire_lock);
16638 	for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
16639 	    mpp = &(*mpp)->b_cont) {
16640 		in6_addr_t	gw_addr_v6;
16641 
16642 		/*
16643 		 * On a given ill, the tuple of address, gateway, mask,
16644 		 * ire_type, and zoneid is unique for each saved IRE.
16645 		 */
16646 		mp = *mpp;
16647 		ifrt = (ifrt_t *)mp->b_rptr;
16648 		/* ire_gateway_addr_v6 can change - need lock */
16649 		mutex_enter(&ire->ire_lock);
16650 		gw_addr_v6 = ire->ire_gateway_addr_v6;
16651 		mutex_exit(&ire->ire_lock);
16652 
16653 		if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
16654 		    ifrt->ifrt_type != ire->ire_type)
16655 			continue;
16656 
16657 		if (ill->ill_isv6 ?
16658 		    (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
16659 		    &ire->ire_addr_v6) &&
16660 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
16661 		    &gw_addr_v6) &&
16662 		    IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
16663 		    &ire->ire_mask_v6)) :
16664 		    (ifrt->ifrt_addr == ire->ire_addr &&
16665 		    ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
16666 		    ifrt->ifrt_mask == ire->ire_mask)) {
16667 			*mpp = mp->b_cont;
16668 			ill->ill_saved_ire_cnt--;
16669 			freeb(mp);
16670 			break;
16671 		}
16672 	}
16673 	mutex_exit(&ill->ill_saved_ire_lock);
16674 }
16675 
16676 /*
16677  * IP multirouting broadcast routes handling
16678  * Append CGTP broadcast IREs to regular ones created
16679  * at ifconfig time.
16680  * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
16681  * the destination and the gateway are broadcast addresses.
16682  * The caller has verified that the destination is an IRE_BROADCAST and that
16683  * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
16684  * we create a MULTIRT IRE_BROADCAST.
16685  * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
16686  * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
16687  */
16688 static void
16689 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
16690 {
16691 	ire_t *ire_prim;
16692 
16693 	ASSERT(ire != NULL);
16694 
16695 	ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16696 	    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
16697 	    NULL);
16698 	if (ire_prim != NULL) {
16699 		/*
16700 		 * We are in the special case of broadcasts for
16701 		 * CGTP. We add an IRE_BROADCAST that holds
16702 		 * the RTF_MULTIRT flag, the destination
16703 		 * address and the low level
16704 		 * info of ire_prim. In other words, CGTP
16705 		 * broadcast is added to the redundant ipif.
16706 		 */
16707 		ill_t *ill_prim;
16708 		ire_t  *bcast_ire;
16709 
16710 		ill_prim = ire_prim->ire_ill;
16711 
16712 		ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
16713 		    (void *)ire_prim, (void *)ill_prim));
16714 
16715 		bcast_ire = ire_create(
16716 		    (uchar_t *)&ire->ire_addr,
16717 		    (uchar_t *)&ip_g_all_ones,
16718 		    (uchar_t *)&ire->ire_gateway_addr,
16719 		    IRE_BROADCAST,
16720 		    ill_prim,
16721 		    GLOBAL_ZONEID,	/* CGTP is only for the global zone */
16722 		    ire->ire_flags | RTF_KERNEL,
16723 		    NULL,
16724 		    ipst);
16725 
16726 		/*
16727 		 * Here we assume that ire_add does head insertion so that
16728 		 * the added IRE_BROADCAST comes before the existing IRE_HOST.
16729 		 */
16730 		if (bcast_ire != NULL) {
16731 			if (ire->ire_flags & RTF_SETSRC) {
16732 				bcast_ire->ire_setsrc_addr =
16733 				    ire->ire_setsrc_addr;
16734 			}
16735 			bcast_ire = ire_add(bcast_ire);
16736 			if (bcast_ire != NULL) {
16737 				ip2dbg(("ip_cgtp_filter_bcast_add: "
16738 				    "added bcast_ire %p\n",
16739 				    (void *)bcast_ire));
16740 
16741 				ill_save_ire(ill_prim, bcast_ire);
16742 				ire_refrele(bcast_ire);
16743 			}
16744 		}
16745 		ire_refrele(ire_prim);
16746 	}
16747 }
16748 
16749 /*
16750  * IP multirouting broadcast routes handling
16751  * Remove the broadcast ire.
16752  * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
16753  * the destination and the gateway are broadcast addresses.
16754  * The caller has only verified that RTF_MULTIRT was set. We check
16755  * that the destination is broadcast and that the gateway is a broadcast
16756  * address, and if so delete the IRE added by ip_cgtp_bcast_add().
16757  */
16758 static void
16759 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
16760 {
16761 	ASSERT(ire != NULL);
16762 
16763 	if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
16764 		ire_t *ire_prim;
16765 
16766 		ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
16767 		    IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
16768 		    ipst, NULL);
16769 		if (ire_prim != NULL) {
16770 			ill_t *ill_prim;
16771 			ire_t  *bcast_ire;
16772 
16773 			ill_prim = ire_prim->ire_ill;
16774 
16775 			ip2dbg(("ip_cgtp_filter_bcast_delete: "
16776 			    "ire_prim %p, ill_prim %p\n",
16777 			    (void *)ire_prim, (void *)ill_prim));
16778 
16779 			bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
16780 			    ire->ire_gateway_addr, IRE_BROADCAST,
16781 			    ill_prim, ALL_ZONES, NULL,
16782 			    MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
16783 			    MATCH_IRE_MASK, 0, ipst, NULL);
16784 
16785 			if (bcast_ire != NULL) {
16786 				ip2dbg(("ip_cgtp_filter_bcast_delete: "
16787 				    "looked up bcast_ire %p\n",
16788 				    (void *)bcast_ire));
16789 				ill_remove_saved_ire(bcast_ire->ire_ill,
16790 				    bcast_ire);
16791 				ire_delete(bcast_ire);
16792 				ire_refrele(bcast_ire);
16793 			}
16794 			ire_refrele(ire_prim);
16795 		}
16796 	}
16797 }
16798 
16799 /*
16800  * Derive an interface id from the link layer address.
16801  * Knows about IEEE 802 and IEEE EUI-64 mappings.
16802  */
16803 static void
16804 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16805 {
16806 	char		*addr;
16807 
16808 	/*
16809 	 * Note that some IPv6 interfaces get plumbed over links that claim to
16810 	 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
16811 	 * PPP links).  The ETHERADDRL check here ensures that we only set the
16812 	 * interface ID on IPv6 interfaces above links that actually have real
16813 	 * Ethernet addresses.
16814 	 */
16815 	if (ill->ill_phys_addr_length == ETHERADDRL) {
16816 		/* Form EUI-64 like address */
16817 		addr = (char *)&v6addr->s6_addr32[2];
16818 		bcopy(ill->ill_phys_addr, addr, 3);
16819 		addr[0] ^= 0x2;		/* Toggle Universal/Local bit */
16820 		addr[3] = (char)0xff;
16821 		addr[4] = (char)0xfe;
16822 		bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
16823 	}
16824 }
16825 
16826 /* ARGSUSED */
16827 static void
16828 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16829 {
16830 }
16831 
16832 typedef struct ipmp_ifcookie {
16833 	uint32_t	ic_hostid;
16834 	char		ic_ifname[LIFNAMSIZ];
16835 	char		ic_zonename[ZONENAME_MAX];
16836 } ipmp_ifcookie_t;
16837 
16838 /*
16839  * Construct a pseudo-random interface ID for the IPMP interface that's both
16840  * predictable and (almost) guaranteed to be unique.
16841  */
16842 static void
16843 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16844 {
16845 	zone_t		*zp;
16846 	uint8_t		*addr;
16847 	uchar_t		hash[16];
16848 	ulong_t 	hostid;
16849 	MD5_CTX		ctx;
16850 	ipmp_ifcookie_t	ic = { 0 };
16851 
16852 	ASSERT(IS_IPMP(ill));
16853 
16854 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
16855 	ic.ic_hostid = htonl((uint32_t)hostid);
16856 
16857 	(void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
16858 
16859 	if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
16860 		(void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
16861 		zone_rele(zp);
16862 	}
16863 
16864 	MD5Init(&ctx);
16865 	MD5Update(&ctx, &ic, sizeof (ic));
16866 	MD5Final(hash, &ctx);
16867 
16868 	/*
16869 	 * Map the hash to an interface ID per the basic approach in RFC3041.
16870 	 */
16871 	addr = &v6addr->s6_addr8[8];
16872 	bcopy(hash + 8, addr, sizeof (uint64_t));
16873 	addr[0] &= ~0x2;				/* set local bit */
16874 }
16875 
16876 /*
16877  * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
16878  */
16879 static void
16880 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
16881 {
16882 	phyint_t *phyi = ill->ill_phyint;
16883 
16884 	/*
16885 	 * Check PHYI_MULTI_BCAST and length of physical
16886 	 * address to determine if we use the mapping or the
16887 	 * broadcast address.
16888 	 */
16889 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16890 	    ill->ill_phys_addr_length != ETHERADDRL) {
16891 		ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
16892 		return;
16893 	}
16894 	m_physaddr[0] = 0x33;
16895 	m_physaddr[1] = 0x33;
16896 	m_physaddr[2] = m_ip6addr[12];
16897 	m_physaddr[3] = m_ip6addr[13];
16898 	m_physaddr[4] = m_ip6addr[14];
16899 	m_physaddr[5] = m_ip6addr[15];
16900 }
16901 
16902 /*
16903  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
16904  */
16905 static void
16906 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16907 {
16908 	phyint_t *phyi = ill->ill_phyint;
16909 
16910 	/*
16911 	 * Check PHYI_MULTI_BCAST and length of physical
16912 	 * address to determine if we use the mapping or the
16913 	 * broadcast address.
16914 	 */
16915 	if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
16916 	    ill->ill_phys_addr_length != ETHERADDRL) {
16917 		ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
16918 		return;
16919 	}
16920 	m_physaddr[0] = 0x01;
16921 	m_physaddr[1] = 0x00;
16922 	m_physaddr[2] = 0x5e;
16923 	m_physaddr[3] = m_ipaddr[1] & 0x7f;
16924 	m_physaddr[4] = m_ipaddr[2];
16925 	m_physaddr[5] = m_ipaddr[3];
16926 }
16927 
16928 /* ARGSUSED */
16929 static void
16930 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16931 {
16932 	/*
16933 	 * for the MULTI_BCAST case and other cases when we want to
16934 	 * use the link-layer broadcast address for multicast.
16935 	 */
16936 	uint8_t	*bphys_addr;
16937 	dl_unitdata_req_t *dlur;
16938 
16939 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
16940 	if (ill->ill_sap_length < 0) {
16941 		bphys_addr = (uchar_t *)dlur +
16942 		    dlur->dl_dest_addr_offset;
16943 	} else  {
16944 		bphys_addr = (uchar_t *)dlur +
16945 		    dlur->dl_dest_addr_offset + ill->ill_sap_length;
16946 	}
16947 
16948 	bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
16949 }
16950 
16951 /*
16952  * Derive IPoIB interface id from the link layer address.
16953  */
16954 static void
16955 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
16956 {
16957 	char		*addr;
16958 
16959 	ASSERT(ill->ill_phys_addr_length == 20);
16960 	addr = (char *)&v6addr->s6_addr32[2];
16961 	bcopy(ill->ill_phys_addr + 12, addr, 8);
16962 	/*
16963 	 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
16964 	 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
16965 	 * rules. In these cases, the IBA considers these GUIDs to be in
16966 	 * "Modified EUI-64" format, and thus toggling the u/l bit is not
16967 	 * required; vendors are required not to assign global EUI-64's
16968 	 * that differ only in u/l bit values, thus guaranteeing uniqueness
16969 	 * of the interface identifier. Whether the GUID is in modified
16970 	 * or proper EUI-64 format, the ipv6 identifier must have the u/l
16971 	 * bit set to 1.
16972 	 */
16973 	addr[0] |= 2;			/* Set Universal/Local bit to 1 */
16974 }
16975 
16976 /*
16977  * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
16978  * Note on mapping from multicast IP addresses to IPoIB multicast link
16979  * addresses. IPoIB multicast link addresses are based on IBA link addresses.
16980  * The format of an IPoIB multicast address is:
16981  *
16982  *  4 byte QPN      Scope Sign.  Pkey
16983  * +--------------------------------------------+
16984  * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
16985  * +--------------------------------------------+
16986  *
16987  * The Scope and Pkey components are properties of the IBA port and
16988  * network interface. They can be ascertained from the broadcast address.
16989  * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
16990  */
16991 static void
16992 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
16993 {
16994 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
16995 	    0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
16996 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16997 	uint8_t	*bphys_addr;
16998 	dl_unitdata_req_t *dlur;
16999 
17000 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17001 
17002 	/*
17003 	 * RFC 4391: IPv4 MGID is 28-bit long.
17004 	 */
17005 	m_physaddr[16] = m_ipaddr[0] & 0x0f;
17006 	m_physaddr[17] = m_ipaddr[1];
17007 	m_physaddr[18] = m_ipaddr[2];
17008 	m_physaddr[19] = m_ipaddr[3];
17009 
17010 
17011 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17012 	if (ill->ill_sap_length < 0) {
17013 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17014 	} else  {
17015 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17016 		    ill->ill_sap_length;
17017 	}
17018 	/*
17019 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17020 	 */
17021 	m_physaddr[5] = bphys_addr[5];
17022 	m_physaddr[8] = bphys_addr[8];
17023 	m_physaddr[9] = bphys_addr[9];
17024 }
17025 
17026 static void
17027 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17028 {
17029 	static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17030 	    0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17031 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17032 	uint8_t	*bphys_addr;
17033 	dl_unitdata_req_t *dlur;
17034 
17035 	bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17036 
17037 	/*
17038 	 * RFC 4391: IPv4 MGID is 80-bit long.
17039 	 */
17040 	bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17041 
17042 	dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17043 	if (ill->ill_sap_length < 0) {
17044 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17045 	} else  {
17046 		bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17047 		    ill->ill_sap_length;
17048 	}
17049 	/*
17050 	 * Now fill in the IBA scope/Pkey values from the broadcast address.
17051 	 */
17052 	m_physaddr[5] = bphys_addr[5];
17053 	m_physaddr[8] = bphys_addr[8];
17054 	m_physaddr[9] = bphys_addr[9];
17055 }
17056 
17057 /*
17058  * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17059  * tunnel).  The IPv4 address simply get placed in the lower 4 bytes of the
17060  * IPv6 interface id.  This is a suggested mechanism described in section 3.7
17061  * of RFC4213.
17062  */
17063 static void
17064 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17065 {
17066 	ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17067 	v6addr->s6_addr32[2] = 0;
17068 	bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17069 }
17070 
17071 /*
17072  * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17073  * tunnel).  The lower 8 bytes of the IPv6 address simply become the interface
17074  * id.
17075  */
17076 static void
17077 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17078 {
17079 	in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17080 
17081 	ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17082 	bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17083 }
17084 
17085 static void
17086 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17087 {
17088 	ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17089 }
17090 
17091 static void
17092 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17093 {
17094 	ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17095 }
17096 
17097 static void
17098 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17099 {
17100 	ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17101 }
17102 
17103 static void
17104 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17105 {
17106 	ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17107 }
17108 
17109 /*
17110  * Lookup an ill and verify that the zoneid has an ipif on that ill.
17111  * Returns an held ill, or NULL.
17112  */
17113 ill_t *
17114 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17115     ip_stack_t *ipst)
17116 {
17117 	ill_t	*ill;
17118 	ipif_t	*ipif;
17119 
17120 	ill = ill_lookup_on_ifindex(index, isv6, ipst);
17121 	if (ill == NULL)
17122 		return (NULL);
17123 
17124 	mutex_enter(&ill->ill_lock);
17125 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17126 		if (IPIF_IS_CONDEMNED(ipif))
17127 			continue;
17128 		if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17129 		    ipif->ipif_zoneid != ALL_ZONES)
17130 			continue;
17131 
17132 		mutex_exit(&ill->ill_lock);
17133 		return (ill);
17134 	}
17135 	mutex_exit(&ill->ill_lock);
17136 	ill_refrele(ill);
17137 	return (NULL);
17138 }
17139 
17140 /*
17141  * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17142  * If a pointer to an ipif_t is returned then the caller will need to do
17143  * an ill_refrele().
17144  */
17145 ipif_t *
17146 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17147     ip_stack_t *ipst)
17148 {
17149 	ipif_t *ipif;
17150 	ill_t *ill;
17151 
17152 	ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17153 	if (ill == NULL)
17154 		return (NULL);
17155 
17156 	mutex_enter(&ill->ill_lock);
17157 	if (ill->ill_state_flags & ILL_CONDEMNED) {
17158 		mutex_exit(&ill->ill_lock);
17159 		ill_refrele(ill);
17160 		return (NULL);
17161 	}
17162 
17163 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17164 		if (!IPIF_CAN_LOOKUP(ipif))
17165 			continue;
17166 		if (lifidx == ipif->ipif_id) {
17167 			ipif_refhold_locked(ipif);
17168 			break;
17169 		}
17170 	}
17171 
17172 	mutex_exit(&ill->ill_lock);
17173 	ill_refrele(ill);
17174 	return (ipif);
17175 }
17176 
17177 /*
17178  * Set ill_inputfn based on the current know state.
17179  * This needs to be called when any of the factors taken into
17180  * account changes.
17181  */
17182 void
17183 ill_set_inputfn(ill_t *ill)
17184 {
17185 	ip_stack_t	*ipst = ill->ill_ipst;
17186 
17187 	if (ill->ill_isv6) {
17188 		if (is_system_labeled())
17189 			ill->ill_inputfn = ill_input_full_v6;
17190 		else
17191 			ill->ill_inputfn = ill_input_short_v6;
17192 	} else {
17193 		if (is_system_labeled())
17194 			ill->ill_inputfn = ill_input_full_v4;
17195 		else if (ill->ill_dhcpinit != 0)
17196 			ill->ill_inputfn = ill_input_full_v4;
17197 		else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17198 		    != NULL)
17199 			ill->ill_inputfn = ill_input_full_v4;
17200 		else if (ipst->ips_ip_cgtp_filter &&
17201 		    ipst->ips_ip_cgtp_filter_ops != NULL)
17202 			ill->ill_inputfn = ill_input_full_v4;
17203 		else
17204 			ill->ill_inputfn = ill_input_short_v4;
17205 	}
17206 }
17207 
17208 /*
17209  * Re-evaluate ill_inputfn for all the IPv4 ills.
17210  * Used when RSVP and CGTP comes and goes.
17211  */
17212 void
17213 ill_set_inputfn_all(ip_stack_t *ipst)
17214 {
17215 	ill_walk_context_t	ctx;
17216 	ill_t			*ill;
17217 
17218 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17219 	ill = ILL_START_WALK_V4(&ctx, ipst);
17220 	for (; ill != NULL; ill = ill_next(&ctx, ill))
17221 		ill_set_inputfn(ill);
17222 
17223 	rw_exit(&ipst->ips_ill_g_lock);
17224 }
17225 
17226 /*
17227  * Set the physical address information for `ill' to the contents of the
17228  * dl_notify_ind_t pointed to by `mp'.  Must be called as writer, and will be
17229  * asynchronous if `ill' cannot immediately be quiesced -- in which case
17230  * EINPROGRESS will be returned.
17231  */
17232 int
17233 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17234 {
17235 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17236 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)mp->b_rptr;
17237 
17238 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17239 
17240 	if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17241 	    dlindp->dl_data != DL_CURR_DEST_ADDR &&
17242 	    dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17243 		/* Changing DL_IPV6_TOKEN is not yet supported */
17244 		return (0);
17245 	}
17246 
17247 	/*
17248 	 * We need to store up to two copies of `mp' in `ill'.  Due to the
17249 	 * design of ipsq_pending_mp_add(), we can't pass them as separate
17250 	 * arguments to ill_set_phys_addr_tail().  Instead, chain them
17251 	 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17252 	 */
17253 	if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17254 		freemsg(mp);
17255 		return (ENOMEM);
17256 	}
17257 
17258 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17259 	mutex_enter(&ill->ill_lock);
17260 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17261 	/* no more nce addition allowed */
17262 	mutex_exit(&ill->ill_lock);
17263 
17264 	/*
17265 	 * If we can quiesce the ill, then set the address.  If not, then
17266 	 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17267 	 */
17268 	ill_down_ipifs(ill, B_TRUE);
17269 	mutex_enter(&ill->ill_lock);
17270 	if (!ill_is_quiescent(ill)) {
17271 		/* call cannot fail since `conn_t *' argument is NULL */
17272 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17273 		    mp, ILL_DOWN);
17274 		mutex_exit(&ill->ill_lock);
17275 		return (EINPROGRESS);
17276 	}
17277 	mutex_exit(&ill->ill_lock);
17278 
17279 	ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17280 	return (0);
17281 }
17282 
17283 /*
17284  * Once the ill associated with `q' has quiesced, set its physical address
17285  * information to the values in `addrmp'.  Note that two copies of `addrmp'
17286  * are passed (linked by b_cont), since we sometimes need to save two distinct
17287  * copies in the ill_t, and our context doesn't permit sleeping or allocation
17288  * failure (we'll free the other copy if it's not needed).  Since the ill_t
17289  * is quiesced, we know any stale nce's with the old address information have
17290  * already been removed, so we don't need to call nce_flush().
17291  */
17292 /* ARGSUSED */
17293 static void
17294 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17295 {
17296 	ill_t		*ill = q->q_ptr;
17297 	mblk_t		*addrmp2 = unlinkb(addrmp);
17298 	dl_notify_ind_t	*dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17299 	uint_t		addrlen, addroff;
17300 	int		status;
17301 
17302 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17303 
17304 	addroff	= dlindp->dl_addr_offset;
17305 	addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17306 
17307 	switch (dlindp->dl_data) {
17308 	case DL_IPV6_LINK_LAYER_ADDR:
17309 		ill_set_ndmp(ill, addrmp, addroff, addrlen);
17310 		freemsg(addrmp2);
17311 		break;
17312 
17313 	case DL_CURR_DEST_ADDR:
17314 		freemsg(ill->ill_dest_addr_mp);
17315 		ill->ill_dest_addr = addrmp->b_rptr + addroff;
17316 		ill->ill_dest_addr_mp = addrmp;
17317 		if (ill->ill_isv6) {
17318 			ill_setdesttoken(ill);
17319 			ipif_setdestlinklocal(ill->ill_ipif);
17320 		}
17321 		freemsg(addrmp2);
17322 		break;
17323 
17324 	case DL_CURR_PHYS_ADDR:
17325 		freemsg(ill->ill_phys_addr_mp);
17326 		ill->ill_phys_addr = addrmp->b_rptr + addroff;
17327 		ill->ill_phys_addr_mp = addrmp;
17328 		ill->ill_phys_addr_length = addrlen;
17329 		if (ill->ill_isv6)
17330 			ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17331 		else
17332 			freemsg(addrmp2);
17333 		if (ill->ill_isv6) {
17334 			ill_setdefaulttoken(ill);
17335 			ipif_setlinklocal(ill->ill_ipif);
17336 		}
17337 		break;
17338 	default:
17339 		ASSERT(0);
17340 	}
17341 
17342 	/*
17343 	 * If there are ipifs to bring up, ill_up_ipifs() will return
17344 	 * EINPROGRESS, and ipsq_current_finish() will be called by
17345 	 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17346 	 * brought up.
17347 	 */
17348 	status = ill_up_ipifs(ill, q, addrmp);
17349 	mutex_enter(&ill->ill_lock);
17350 	if (ill->ill_dl_up)
17351 		ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17352 	mutex_exit(&ill->ill_lock);
17353 	if (status != EINPROGRESS)
17354 		ipsq_current_finish(ipsq);
17355 }
17356 
17357 /*
17358  * Helper routine for setting the ill_nd_lla fields.
17359  */
17360 void
17361 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17362 {
17363 	freemsg(ill->ill_nd_lla_mp);
17364 	ill->ill_nd_lla = ndmp->b_rptr + addroff;
17365 	ill->ill_nd_lla_mp = ndmp;
17366 	ill->ill_nd_lla_len = addrlen;
17367 }
17368 
17369 /*
17370  * Replumb the ill.
17371  */
17372 int
17373 ill_replumb(ill_t *ill, mblk_t *mp)
17374 {
17375 	ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17376 
17377 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17378 
17379 	ipsq_current_start(ipsq, ill->ill_ipif, 0);
17380 
17381 	mutex_enter(&ill->ill_lock);
17382 	ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17383 	/* no more nce addition allowed */
17384 	mutex_exit(&ill->ill_lock);
17385 
17386 	/*
17387 	 * If we can quiesce the ill, then continue.  If not, then
17388 	 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17389 	 */
17390 	ill_down_ipifs(ill, B_FALSE);
17391 
17392 	mutex_enter(&ill->ill_lock);
17393 	if (!ill_is_quiescent(ill)) {
17394 		/* call cannot fail since `conn_t *' argument is NULL */
17395 		(void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17396 		    mp, ILL_DOWN);
17397 		mutex_exit(&ill->ill_lock);
17398 		return (EINPROGRESS);
17399 	}
17400 	mutex_exit(&ill->ill_lock);
17401 
17402 	ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
17403 	return (0);
17404 }
17405 
17406 /* ARGSUSED */
17407 static void
17408 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
17409 {
17410 	ill_t *ill = q->q_ptr;
17411 	int err;
17412 	conn_t *connp = NULL;
17413 
17414 	ASSERT(IAM_WRITER_IPSQ(ipsq));
17415 	freemsg(ill->ill_replumb_mp);
17416 	ill->ill_replumb_mp = copyb(mp);
17417 
17418 	if (ill->ill_replumb_mp == NULL) {
17419 		/* out of memory */
17420 		ipsq_current_finish(ipsq);
17421 		return;
17422 	}
17423 
17424 	mutex_enter(&ill->ill_lock);
17425 	ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
17426 	    ill->ill_rq, ill->ill_replumb_mp, 0);
17427 	mutex_exit(&ill->ill_lock);
17428 
17429 	if (!ill->ill_up_ipifs) {
17430 		/* already closing */
17431 		ipsq_current_finish(ipsq);
17432 		return;
17433 	}
17434 	ill->ill_replumbing = 1;
17435 	err = ill_down_ipifs_tail(ill);
17436 
17437 	/*
17438 	 * Successfully quiesced and brought down the interface, now we send
17439 	 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
17440 	 * DL_NOTE_REPLUMB message.
17441 	 */
17442 	mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
17443 	    DL_NOTIFY_CONF);
17444 	ASSERT(mp != NULL);
17445 	((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
17446 	    DL_NOTE_REPLUMB_DONE;
17447 	ill_dlpi_send(ill, mp);
17448 
17449 	/*
17450 	 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
17451 	 * streams have to be unbound. When all the DLPI exchanges are done,
17452 	 * ipsq_current_finish() will be called by arp_bringup_done(). The
17453 	 * remainder of ipif bringup via ill_up_ipifs() will also be done in
17454 	 * arp_bringup_done().
17455 	 */
17456 	ASSERT(ill->ill_replumb_mp != NULL);
17457 	if (err == EINPROGRESS)
17458 		return;
17459 	else
17460 		ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
17461 	ASSERT(connp == NULL);
17462 	if (err == 0 && ill->ill_replumb_mp != NULL &&
17463 	    ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
17464 		return;
17465 	}
17466 	ipsq_current_finish(ipsq);
17467 }
17468 
17469 /*
17470  * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
17471  * which is `bufsize' bytes.  On success, zero is returned and `buf' updated
17472  * as per the ioctl.  On failure, an errno is returned.
17473  */
17474 static int
17475 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
17476 {
17477 	int rval;
17478 	struct strioctl iocb;
17479 
17480 	iocb.ic_cmd = cmd;
17481 	iocb.ic_timout = 15;
17482 	iocb.ic_len = bufsize;
17483 	iocb.ic_dp = buf;
17484 
17485 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
17486 }
17487 
17488 /*
17489  * Issue an SIOCGLIFCONF for address family `af' and store the result into a
17490  * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
17491  */
17492 static int
17493 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
17494     uint_t *bufsizep, cred_t *cr)
17495 {
17496 	int err;
17497 	struct lifnum lifn;
17498 
17499 	bzero(&lifn, sizeof (lifn));
17500 	lifn.lifn_family = af;
17501 	lifn.lifn_flags = LIFC_UNDER_IPMP;
17502 
17503 	if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
17504 		return (err);
17505 
17506 	/*
17507 	 * Pad the interface count to account for additional interfaces that
17508 	 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
17509 	 */
17510 	lifn.lifn_count += 4;
17511 	bzero(lifcp, sizeof (*lifcp));
17512 	lifcp->lifc_flags = LIFC_UNDER_IPMP;
17513 	lifcp->lifc_family = af;
17514 	lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
17515 	lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
17516 
17517 	err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
17518 	if (err != 0) {
17519 		kmem_free(lifcp->lifc_buf, *bufsizep);
17520 		return (err);
17521 	}
17522 
17523 	return (0);
17524 }
17525 
17526 /*
17527  * Helper for ip_interface_cleanup() that removes the loopback interface.
17528  */
17529 static void
17530 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17531 {
17532 	int err;
17533 	struct lifreq lifr;
17534 
17535 	bzero(&lifr, sizeof (lifr));
17536 	(void) strcpy(lifr.lifr_name, ipif_loopback_name);
17537 
17538 	err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
17539 	if (err != 0) {
17540 		ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
17541 		    "error %d\n", isv6 ? "v6" : "v4", err));
17542 	}
17543 }
17544 
17545 /*
17546  * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
17547  * groups and that IPMP data addresses are down.  These conditions must be met
17548  * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
17549  */
17550 static void
17551 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
17552 {
17553 	int af = isv6 ? AF_INET6 : AF_INET;
17554 	int i, nifs;
17555 	int err;
17556 	uint_t bufsize;
17557 	uint_t lifrsize = sizeof (struct lifreq);
17558 	struct lifconf lifc;
17559 	struct lifreq *lifrp;
17560 
17561 	if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
17562 		cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
17563 		    "(error %d); any IPMP interfaces cannot be shutdown", err);
17564 		return;
17565 	}
17566 
17567 	nifs = lifc.lifc_len / lifrsize;
17568 	for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
17569 		err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17570 		if (err != 0) {
17571 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
17572 			    "flags: error %d", lifrp->lifr_name, err);
17573 			continue;
17574 		}
17575 
17576 		if (lifrp->lifr_flags & IFF_IPMP) {
17577 			if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
17578 				continue;
17579 
17580 			lifrp->lifr_flags &= ~IFF_UP;
17581 			err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
17582 			if (err != 0) {
17583 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17584 				    "bring down (error %d); IPMP interface may "
17585 				    "not be shutdown", lifrp->lifr_name, err);
17586 			}
17587 
17588 			/*
17589 			 * Check if IFF_DUPLICATE is still set -- and if so,
17590 			 * reset the address to clear it.
17591 			 */
17592 			err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
17593 			if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
17594 				continue;
17595 
17596 			err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
17597 			if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
17598 			    lifrp, lifrsize, cr)) != 0) {
17599 				cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
17600 				    "reset DAD (error %d); IPMP interface may "
17601 				    "not be shutdown", lifrp->lifr_name, err);
17602 			}
17603 			continue;
17604 		}
17605 
17606 		lifrp->lifr_groupname[0] = '\0';
17607 		err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp, lifrsize, cr);
17608 		if (err != 0) {
17609 			cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot leave "
17610 			    "IPMP group (error %d); associated IPMP interface "
17611 			    "may not be shutdown", lifrp->lifr_name, err);
17612 			continue;
17613 		}
17614 	}
17615 
17616 	kmem_free(lifc.lifc_buf, bufsize);
17617 }
17618 
17619 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
17620 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
17621 
17622 /*
17623  * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
17624  * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
17625  * when the user-level processes in the zone are killed and the latter are
17626  * cleaned up by str_stack_shutdown().
17627  */
17628 void
17629 ip_interface_cleanup(ip_stack_t *ipst)
17630 {
17631 	ldi_handle_t	lh;
17632 	ldi_ident_t	li;
17633 	cred_t		*cr;
17634 	int		err;
17635 	int		i;
17636 	char		*devs[] = { UDP6DEV, UDPDEV };
17637 	netstackid_t	stackid = ipst->ips_netstack->netstack_stackid;
17638 
17639 	if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
17640 		cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
17641 		    " error %d", err);
17642 		return;
17643 	}
17644 
17645 	cr = zone_get_kcred(netstackid_to_zoneid(stackid));
17646 	ASSERT(cr != NULL);
17647 
17648 	/*
17649 	 * NOTE: loop executes exactly twice and is hardcoded to know that the
17650 	 * first iteration is IPv6.  (Unrolling yields repetitious code, hence
17651 	 * the loop.)
17652 	 */
17653 	for (i = 0; i < 2; i++) {
17654 		err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
17655 		if (err != 0) {
17656 			cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
17657 			    " error %d", devs[i], err);
17658 			continue;
17659 		}
17660 
17661 		ip_loopback_removeif(lh, i == 0, cr);
17662 		ip_ipmp_cleanup(lh, i == 0, cr);
17663 
17664 		(void) ldi_close(lh, FREAD|FWRITE, cr);
17665 	}
17666 
17667 	ldi_ident_release(li);
17668 	crfree(cr);
17669 }
17670 
17671 /*
17672  * This needs to be in-sync with nic_event_t definition
17673  */
17674 static const char *
17675 ill_hook_event2str(nic_event_t event)
17676 {
17677 	switch (event) {
17678 	case NE_PLUMB:
17679 		return ("PLUMB");
17680 	case NE_UNPLUMB:
17681 		return ("UNPLUMB");
17682 	case NE_UP:
17683 		return ("UP");
17684 	case NE_DOWN:
17685 		return ("DOWN");
17686 	case NE_ADDRESS_CHANGE:
17687 		return ("ADDRESS_CHANGE");
17688 	case NE_LIF_UP:
17689 		return ("LIF_UP");
17690 	case NE_LIF_DOWN:
17691 		return ("LIF_DOWN");
17692 	case NE_IFINDEX_CHANGE:
17693 		return ("IFINDEX_CHANGE");
17694 	default:
17695 		return ("UNKNOWN");
17696 	}
17697 }
17698 
17699 void
17700 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
17701     nic_event_data_t data, size_t datalen)
17702 {
17703 	ip_stack_t		*ipst = ill->ill_ipst;
17704 	hook_nic_event_int_t	*info;
17705 	const char		*str = NULL;
17706 
17707 	/* create a new nic event info */
17708 	if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
17709 		goto fail;
17710 
17711 	info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
17712 	info->hnei_event.hne_lif = lif;
17713 	info->hnei_event.hne_event = event;
17714 	info->hnei_event.hne_protocol = ill->ill_isv6 ?
17715 	    ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
17716 	info->hnei_event.hne_data = NULL;
17717 	info->hnei_event.hne_datalen = 0;
17718 	info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
17719 
17720 	if (data != NULL && datalen != 0) {
17721 		info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
17722 		if (info->hnei_event.hne_data == NULL)
17723 			goto fail;
17724 		bcopy(data, info->hnei_event.hne_data, datalen);
17725 		info->hnei_event.hne_datalen = datalen;
17726 	}
17727 
17728 	if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
17729 	    DDI_NOSLEEP) == DDI_SUCCESS)
17730 		return;
17731 
17732 fail:
17733 	if (info != NULL) {
17734 		if (info->hnei_event.hne_data != NULL) {
17735 			kmem_free(info->hnei_event.hne_data,
17736 			    info->hnei_event.hne_datalen);
17737 		}
17738 		kmem_free(info, sizeof (hook_nic_event_t));
17739 	}
17740 	str = ill_hook_event2str(event);
17741 	ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
17742 	    "information for %s (ENOMEM)\n", str, ill->ill_name));
17743 }
17744 
17745 static int
17746 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
17747 {
17748 	int		err = 0;
17749 	const in_addr_t	*addr = NULL;
17750 	nce_t		*nce = NULL;
17751 	ill_t		*ill = ipif->ipif_ill;
17752 	ill_t		*bound_ill;
17753 	boolean_t	added_ipif = B_FALSE;
17754 	uint16_t	state;
17755 	uint16_t	flags;
17756 
17757 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
17758 	    ill_t *, ill, ipif_t *, ipif);
17759 	if (ipif->ipif_lcl_addr != INADDR_ANY) {
17760 		addr = &ipif->ipif_lcl_addr;
17761 	}
17762 
17763 	if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
17764 		if (res_act != Res_act_initial)
17765 			return (EINVAL);
17766 	}
17767 
17768 	if (addr != NULL) {
17769 		ipmp_illgrp_t	*illg = ill->ill_grp;
17770 
17771 		/* add unicast nce for the local addr */
17772 
17773 		if (IS_IPMP(ill)) {
17774 			/*
17775 			 * If we're here via ipif_up(), then the ipif
17776 			 * won't be bound yet -- add it to the group,
17777 			 * which will bind it if possible. (We would
17778 			 * add it in ipif_up(), but deleting on failure
17779 			 * there is gruesome.)  If we're here via
17780 			 * ipmp_ill_bind_ipif(), then the ipif has
17781 			 * already been added to the group and we
17782 			 * just need to use the binding.
17783 			 */
17784 			if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
17785 				bound_ill  = ipmp_illgrp_add_ipif(illg, ipif);
17786 				if (bound_ill == NULL) {
17787 					/*
17788 					 * We couldn't bind the ipif to an ill
17789 					 * yet, so we have nothing to publish.
17790 					 * Mark the address as ready and return.
17791 					 */
17792 					ipif->ipif_addr_ready = 1;
17793 					return (0);
17794 				}
17795 				added_ipif = B_TRUE;
17796 			}
17797 		} else {
17798 			bound_ill = ill;
17799 		}
17800 
17801 		flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
17802 		    NCE_F_NONUD);
17803 		/*
17804 		 * If this is an initial bring-up (or the ipif was never
17805 		 * completely brought up), do DAD.  Otherwise, we're here
17806 		 * because IPMP has rebound an address to this ill: send
17807 		 * unsolicited advertisements (ARP announcements) to
17808 		 * inform others.
17809 		 */
17810 		if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
17811 			state = ND_UNCHANGED; /* compute in nce_add_common() */
17812 		} else {
17813 			state = ND_REACHABLE;
17814 			flags |= NCE_F_UNSOL_ADV;
17815 		}
17816 
17817 retry:
17818 		err = nce_lookup_then_add_v4(ill,
17819 		    bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
17820 		    addr, flags, state, &nce);
17821 
17822 		/*
17823 		 * note that we may encounter EEXIST if we are moving
17824 		 * the nce as a result of a rebind operation.
17825 		 */
17826 		switch (err) {
17827 		case 0:
17828 			ipif->ipif_added_nce = 1;
17829 			nce->nce_ipif_cnt++;
17830 			break;
17831 		case EEXIST:
17832 			ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
17833 			    ill->ill_name));
17834 			if (!NCE_MYADDR(nce->nce_common)) {
17835 				/*
17836 				 * A leftover nce from before this address
17837 				 * existed
17838 				 */
17839 				ncec_delete(nce->nce_common);
17840 				nce_refrele(nce);
17841 				nce = NULL;
17842 				goto retry;
17843 			}
17844 			if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
17845 				nce_refrele(nce);
17846 				nce = NULL;
17847 				ip1dbg(("ipif_arp_up: NCE already exists "
17848 				    "for %s:%u\n", ill->ill_name,
17849 				    ipif->ipif_id));
17850 				goto arp_up_done;
17851 			}
17852 			/*
17853 			 * Duplicate local addresses are permissible for
17854 			 * IPIF_POINTOPOINT interfaces which will get marked
17855 			 * IPIF_UNNUMBERED later in
17856 			 * ip_addr_availability_check().
17857 			 *
17858 			 * The nce_ipif_cnt field tracks the number of
17859 			 * ipifs that have nce_addr as their local address.
17860 			 */
17861 			ipif->ipif_addr_ready = 1;
17862 			ipif->ipif_added_nce = 1;
17863 			nce->nce_ipif_cnt++;
17864 			err = 0;
17865 			break;
17866 		default:
17867 			ASSERT(nce == NULL);
17868 			goto arp_up_done;
17869 		}
17870 		if (arp_no_defense) {
17871 			if ((ipif->ipif_flags & IPIF_UP) &&
17872 			    !ipif->ipif_addr_ready)
17873 				ipif_up_notify(ipif);
17874 			ipif->ipif_addr_ready = 1;
17875 		}
17876 	} else {
17877 		/* zero address. nothing to publish */
17878 		ipif->ipif_addr_ready = 1;
17879 	}
17880 	if (nce != NULL)
17881 		nce_refrele(nce);
17882 arp_up_done:
17883 	if (added_ipif && err != 0)
17884 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
17885 	return (err);
17886 }
17887 
17888 int
17889 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
17890 {
17891 	int 		err = 0;
17892 	ill_t 		*ill = ipif->ipif_ill;
17893 	boolean_t	first_interface, wait_for_dlpi = B_FALSE;
17894 
17895 	DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
17896 	    ill_t *, ill, ipif_t *, ipif);
17897 
17898 	/*
17899 	 * need to bring up ARP or setup mcast mapping only
17900 	 * when the first interface is coming UP.
17901 	 */
17902 	first_interface = (ill->ill_ipif_up_count == 0 &&
17903 	    ill->ill_ipif_dup_count == 0 && !was_dup);
17904 
17905 	if (res_act == Res_act_initial && first_interface) {
17906 		/*
17907 		 * Send ATTACH + BIND
17908 		 */
17909 		err = arp_ll_up(ill);
17910 		if (err != EINPROGRESS && err != 0)
17911 			return (err);
17912 
17913 		/*
17914 		 * Add NCE for local address. Start DAD.
17915 		 * we'll wait to hear that DAD has finished
17916 		 * before using the interface.
17917 		 */
17918 		if (err == EINPROGRESS)
17919 			wait_for_dlpi = B_TRUE;
17920 	}
17921 
17922 	if (!wait_for_dlpi)
17923 		(void) ipif_arp_up_done_tail(ipif, res_act);
17924 
17925 	return (!wait_for_dlpi ? 0 : EINPROGRESS);
17926 }
17927 
17928 /*
17929  * Finish processing of "arp_up" after all the DLPI message
17930  * exchanges have completed between arp and the driver.
17931  */
17932 void
17933 arp_bringup_done(ill_t *ill, int err)
17934 {
17935 	mblk_t	*mp1;
17936 	ipif_t  *ipif;
17937 	conn_t *connp = NULL;
17938 	ipsq_t	*ipsq;
17939 	queue_t *q;
17940 
17941 	ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
17942 
17943 	ASSERT(IAM_WRITER_ILL(ill));
17944 
17945 	ipsq = ill->ill_phyint->phyint_ipsq;
17946 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
17947 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
17948 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
17949 	if (mp1 == NULL) /* bringup was aborted by the user */
17950 		return;
17951 
17952 	/*
17953 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
17954 	 * must have an associated conn_t.  Otherwise, we're bringing this
17955 	 * interface back up as part of handling an asynchronous event (e.g.,
17956 	 * physical address change).
17957 	 */
17958 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
17959 		ASSERT(connp != NULL);
17960 		q = CONNP_TO_WQ(connp);
17961 	} else {
17962 		ASSERT(connp == NULL);
17963 		q = ill->ill_rq;
17964 	}
17965 	if (err == 0) {
17966 		if (ipif->ipif_isv6) {
17967 			if ((err = ipif_up_done_v6(ipif)) != 0)
17968 				ip0dbg(("arp_bringup_done: init failed\n"));
17969 		} else {
17970 			err = ipif_arp_up_done_tail(ipif, Res_act_initial);
17971 			if (err != 0 ||
17972 			    (err = ipif_up_done(ipif)) != 0) {
17973 				ip0dbg(("arp_bringup_done: "
17974 				    "init failed err %x\n", err));
17975 				(void) ipif_arp_down(ipif);
17976 			}
17977 
17978 		}
17979 	} else {
17980 		ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
17981 	}
17982 
17983 	if ((err == 0) && (ill->ill_up_ipifs)) {
17984 		err = ill_up_ipifs(ill, q, mp1);
17985 		if (err == EINPROGRESS)
17986 			return;
17987 	}
17988 
17989 	/*
17990 	 * If we have a moved ipif to bring up, and everything has succeeded
17991 	 * to this point, bring it up on the IPMP ill.  Otherwise, leave it
17992 	 * down -- the admin can try to bring it up by hand if need be.
17993 	 */
17994 	if (ill->ill_move_ipif != NULL) {
17995 		ipif = ill->ill_move_ipif;
17996 		ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
17997 		    ipif->ipif_ill->ill_name));
17998 		ill->ill_move_ipif = NULL;
17999 		if (err == 0) {
18000 			err = ipif_up(ipif, q, mp1);
18001 			if (err == EINPROGRESS)
18002 				return;
18003 		}
18004 	}
18005 
18006 	/*
18007 	 * The operation must complete without EINPROGRESS since
18008 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18009 	 * Otherwise, the operation will be stuck forever in the ipsq.
18010 	 */
18011 	ASSERT(err != EINPROGRESS);
18012 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18013 		DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18014 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18015 		    ill_t *, ill, ipif_t *, ipif);
18016 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18017 	} else {
18018 		ipsq_current_finish(ipsq);
18019 	}
18020 }
18021 
18022 /*
18023  * Finish processing of arp replumb after all the DLPI message
18024  * exchanges have completed between arp and the driver.
18025  */
18026 void
18027 arp_replumb_done(ill_t *ill, int err)
18028 {
18029 	mblk_t	*mp1;
18030 	ipif_t  *ipif;
18031 	conn_t *connp = NULL;
18032 	ipsq_t	*ipsq;
18033 	queue_t *q;
18034 
18035 	ASSERT(IAM_WRITER_ILL(ill));
18036 
18037 	ipsq = ill->ill_phyint->phyint_ipsq;
18038 	ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18039 	mp1 = ipsq_pending_mp_get(ipsq, &connp);
18040 	ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18041 	if (mp1 == NULL) {
18042 		ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18043 		    ipsq->ipsq_xop->ipx_current_ioctl));
18044 		/* bringup was aborted by the user */
18045 		return;
18046 	}
18047 	/*
18048 	 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18049 	 * must have an associated conn_t.  Otherwise, we're bringing this
18050 	 * interface back up as part of handling an asynchronous event (e.g.,
18051 	 * physical address change).
18052 	 */
18053 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18054 		ASSERT(connp != NULL);
18055 		q = CONNP_TO_WQ(connp);
18056 	} else {
18057 		ASSERT(connp == NULL);
18058 		q = ill->ill_rq;
18059 	}
18060 	if ((err == 0) && (ill->ill_up_ipifs)) {
18061 		err = ill_up_ipifs(ill, q, mp1);
18062 		if (err == EINPROGRESS)
18063 			return;
18064 	}
18065 	/*
18066 	 * The operation must complete without EINPROGRESS since
18067 	 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18068 	 * Otherwise, the operation will be stuck forever in the ipsq.
18069 	 */
18070 	ASSERT(err != EINPROGRESS);
18071 	if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18072 		DTRACE_PROBE4(ipif__ioctl, char *,
18073 		    "arp_replumb_done finish",
18074 		    int, ipsq->ipsq_xop->ipx_current_ioctl,
18075 		    ill_t *, ill, ipif_t *, ipif);
18076 		ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18077 	} else {
18078 		ipsq_current_finish(ipsq);
18079 	}
18080 }
18081 
18082 void
18083 ipif_up_notify(ipif_t *ipif)
18084 {
18085 	ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18086 	ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18087 	sctp_update_ipif(ipif, SCTP_IPIF_UP);
18088 	ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18089 	    NE_LIF_UP, NULL, 0);
18090 }
18091 
18092 /*
18093  * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18094  * this assumes the context is cv_wait'able.  Hence it shouldnt' be used on
18095  * TPI end points with STREAMS modules pushed above.  This is assured by not
18096  * having the IPI_MODOK flag for the ioctl.  And IP ensures the ILB ioctl
18097  * never ends up on an ipsq, otherwise we may end up processing the ioctl
18098  * while unwinding from the ispq and that could be a thread from the bottom.
18099  */
18100 /* ARGSUSED */
18101 int
18102 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18103     ip_ioctl_cmd_t *ipip, void *arg)
18104 {
18105 	mblk_t *cmd_mp = mp->b_cont->b_cont;
18106 	ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18107 	int ret = 0;
18108 	int i;
18109 	size_t size;
18110 	ip_stack_t *ipst;
18111 	zoneid_t zoneid;
18112 	ilb_stack_t *ilbs;
18113 
18114 	ipst = CONNQ_TO_IPST(q);
18115 	ilbs = ipst->ips_netstack->netstack_ilb;
18116 	zoneid = Q_TO_CONN(q)->conn_zoneid;
18117 
18118 	switch (command) {
18119 	case ILB_CREATE_RULE: {
18120 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18121 
18122 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18123 			ret = EINVAL;
18124 			break;
18125 		}
18126 
18127 		ret = ilb_rule_add(ilbs, zoneid, cmd);
18128 		break;
18129 	}
18130 	case ILB_DESTROY_RULE:
18131 	case ILB_ENABLE_RULE:
18132 	case ILB_DISABLE_RULE: {
18133 		ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18134 
18135 		if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18136 			ret = EINVAL;
18137 			break;
18138 		}
18139 
18140 		if (cmd->flags & ILB_RULE_ALLRULES) {
18141 			if (command == ILB_DESTROY_RULE) {
18142 				ilb_rule_del_all(ilbs, zoneid);
18143 				break;
18144 			} else if (command == ILB_ENABLE_RULE) {
18145 				ilb_rule_enable_all(ilbs, zoneid);
18146 				break;
18147 			} else if (command == ILB_DISABLE_RULE) {
18148 				ilb_rule_disable_all(ilbs, zoneid);
18149 				break;
18150 			}
18151 		} else {
18152 			if (command == ILB_DESTROY_RULE) {
18153 				ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18154 			} else if (command == ILB_ENABLE_RULE) {
18155 				ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18156 				    NULL);
18157 			} else if (command == ILB_DISABLE_RULE) {
18158 				ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18159 				    NULL);
18160 			}
18161 		}
18162 		break;
18163 	}
18164 	case ILB_NUM_RULES: {
18165 		ilb_num_rules_cmd_t *cmd;
18166 
18167 		if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18168 			ret = EINVAL;
18169 			break;
18170 		}
18171 		cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18172 		ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18173 		break;
18174 	}
18175 	case ILB_RULE_NAMES: {
18176 		ilb_rule_names_cmd_t *cmd;
18177 
18178 		cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18179 		if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18180 		    cmd->num_names == 0) {
18181 			ret = EINVAL;
18182 			break;
18183 		}
18184 		size = cmd->num_names * ILB_RULE_NAMESZ;
18185 		if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18186 		    size != cmd_mp->b_wptr) {
18187 			ret = EINVAL;
18188 			break;
18189 		}
18190 		ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18191 		break;
18192 	}
18193 	case ILB_NUM_SERVERS: {
18194 		ilb_num_servers_cmd_t *cmd;
18195 
18196 		if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18197 			ret = EINVAL;
18198 			break;
18199 		}
18200 		cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18201 		ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18202 		    &(cmd->num));
18203 		break;
18204 	}
18205 	case ILB_LIST_RULE: {
18206 		ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18207 
18208 		if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18209 			ret = EINVAL;
18210 			break;
18211 		}
18212 		ret = ilb_rule_list(ilbs, zoneid, cmd);
18213 		break;
18214 	}
18215 	case ILB_LIST_SERVERS: {
18216 		ilb_servers_info_cmd_t *cmd;
18217 
18218 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18219 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18220 		    cmd->num_servers == 0) {
18221 			ret = EINVAL;
18222 			break;
18223 		}
18224 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18225 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18226 		    size != cmd_mp->b_wptr) {
18227 			ret = EINVAL;
18228 			break;
18229 		}
18230 
18231 		ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18232 		    &cmd->num_servers);
18233 		break;
18234 	}
18235 	case ILB_ADD_SERVERS: {
18236 		ilb_servers_info_cmd_t *cmd;
18237 		ilb_rule_t *rule;
18238 
18239 		cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18240 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18241 			ret = EINVAL;
18242 			break;
18243 		}
18244 		size = cmd->num_servers * sizeof (ilb_server_info_t);
18245 		if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18246 		    size != cmd_mp->b_wptr) {
18247 			ret = EINVAL;
18248 			break;
18249 		}
18250 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18251 		if (rule == NULL) {
18252 			ASSERT(ret != 0);
18253 			break;
18254 		}
18255 		for (i = 0; i < cmd->num_servers; i++) {
18256 			ilb_server_info_t *s;
18257 
18258 			s = &cmd->servers[i];
18259 			s->err = ilb_server_add(ilbs, rule, s);
18260 		}
18261 		ILB_RULE_REFRELE(rule);
18262 		break;
18263 	}
18264 	case ILB_DEL_SERVERS:
18265 	case ILB_ENABLE_SERVERS:
18266 	case ILB_DISABLE_SERVERS: {
18267 		ilb_servers_cmd_t *cmd;
18268 		ilb_rule_t *rule;
18269 		int (*f)();
18270 
18271 		cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18272 		if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18273 			ret = EINVAL;
18274 			break;
18275 		}
18276 		size = cmd->num_servers * sizeof (ilb_server_arg_t);
18277 		if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18278 		    size != cmd_mp->b_wptr) {
18279 			ret = EINVAL;
18280 			break;
18281 		}
18282 
18283 		if (command == ILB_DEL_SERVERS)
18284 			f = ilb_server_del;
18285 		else if (command == ILB_ENABLE_SERVERS)
18286 			f = ilb_server_enable;
18287 		else if (command == ILB_DISABLE_SERVERS)
18288 			f = ilb_server_disable;
18289 
18290 		rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18291 		if (rule == NULL) {
18292 			ASSERT(ret != 0);
18293 			break;
18294 		}
18295 
18296 		for (i = 0; i < cmd->num_servers; i++) {
18297 			ilb_server_arg_t *s;
18298 
18299 			s = &cmd->servers[i];
18300 			s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18301 		}
18302 		ILB_RULE_REFRELE(rule);
18303 		break;
18304 	}
18305 	case ILB_LIST_NAT_TABLE: {
18306 		ilb_list_nat_cmd_t *cmd;
18307 
18308 		cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18309 		if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18310 			ret = EINVAL;
18311 			break;
18312 		}
18313 		size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18314 		if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18315 		    size != cmd_mp->b_wptr) {
18316 			ret = EINVAL;
18317 			break;
18318 		}
18319 
18320 		ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18321 		    &cmd->flags);
18322 		break;
18323 	}
18324 	case ILB_LIST_STICKY_TABLE: {
18325 		ilb_list_sticky_cmd_t *cmd;
18326 
18327 		cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18328 		if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18329 			ret = EINVAL;
18330 			break;
18331 		}
18332 		size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18333 		if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18334 		    size != cmd_mp->b_wptr) {
18335 			ret = EINVAL;
18336 			break;
18337 		}
18338 
18339 		ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18340 		    &cmd->num_sticky, &cmd->flags);
18341 		break;
18342 	}
18343 	default:
18344 		ret = EINVAL;
18345 		break;
18346 	}
18347 done:
18348 	return (ret);
18349 }
18350 
18351 /* Remove all cache entries for this logical interface */
18352 void
18353 ipif_nce_down(ipif_t *ipif)
18354 {
18355 	ill_t *ill = ipif->ipif_ill;
18356 	nce_t *nce;
18357 
18358 	DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18359 	    ill_t *, ill, ipif_t *, ipif);
18360 	if (ipif->ipif_added_nce) {
18361 		if (ipif->ipif_isv6)
18362 			nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18363 		else
18364 			nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18365 		if (nce != NULL) {
18366 			if (--nce->nce_ipif_cnt == 0)
18367 				ncec_delete(nce->nce_common);
18368 			ipif->ipif_added_nce = 0;
18369 			nce_refrele(nce);
18370 		} else {
18371 			/*
18372 			 * nce may already be NULL because it was already
18373 			 * flushed, e.g., due to a call to nce_flush
18374 			 */
18375 			ipif->ipif_added_nce = 0;
18376 		}
18377 	}
18378 	/*
18379 	 * Make IPMP aware of the deleted data address.
18380 	 */
18381 	if (IS_IPMP(ill))
18382 		ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18383 
18384 	/*
18385 	 * Remove all other nces dependent on this ill when the last ipif
18386 	 * is going away.
18387 	 */
18388 	if (ill->ill_ipif_up_count == 0) {
18389 		ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18390 		    (uchar_t *)ill, ill->ill_ipst);
18391 		if (IS_UNDER_IPMP(ill))
18392 			nce_flush(ill, B_TRUE);
18393 	}
18394 }
18395